Skip to content

Commit 145dd75

Browse files
committed
Generate config files from YAML
1 parent 7886682 commit 145dd75

File tree

10 files changed

+96
-55
lines changed

10 files changed

+96
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ debug.json
1010
/src/init.mlog
1111
/src/main.mlog
1212
/src/debugger.mlog
13+
/src/config/*.mlog
1314

1415
node_modules/
1516

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ asm: $(ASM_PROGRAMS)
1515
rust: $(RUST_PROGRAMS)
1616

1717
.PHONY: mlog
18-
mlog: $(MLOG_PROGRAMS)
18+
mlog: $(MLOG_PROGRAMS) mlog-configs
1919

2020
.PHONY: coremark
2121
coremark:
@@ -49,7 +49,11 @@ build/%.o: asm/%.s | build
4949
riscv32-unknown-elf-gcc --compile -march=rv32ima_zicsr -o build/$*.o asm/$*.s
5050

5151
src/%.mlog: src/%.mlog.jinja
52-
python -m mlogv32.preprocessor -o src/$*.mlog src/$*.mlog.jinja
52+
python -m mlogv32.preprocessor file -o src/$*.mlog src/$*.mlog.jinja
53+
54+
.PHONY: mlog-configs
55+
mlog-configs: src/config/configs.yaml
56+
python -m mlogv32.preprocessor configs src/config/configs.yaml
5357

5458
build:
5559
mkdir -p build

nodemon.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"watch": ["src", "python"],
3-
"ext": "jinja,py",
4-
"exec": "make mlog"
3+
"ext": "jinja,py,yaml",
4+
"exec": "make mlog || exit 1"
55
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies = [
1111
"pydantic>=2.11.5",
1212
"pymsch>=0.0.11",
1313
"pyperclip>=1.9.0",
14+
"pyyaml>=5.2",
1415
"riscof",
1516
"riscv-isac",
1617
"tqdm>=4.67.1",

python/src/mlogv32/preprocessor/app.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
2-
from typing import Annotated
2+
from typing import Annotated, Any, TypedDict
33

4+
import yaml
45
from jinja2 import Environment, FileSystemLoader, StrictUndefined
56
from typer import Option, Typer
67

@@ -13,15 +14,41 @@
1314
)
1415

1516

16-
@app.command()
17-
def main(
17+
@app.command(name="file")
18+
def command_file(
1819
path: Path,
1920
output: Annotated[Path | None, Option("-o", "--output")] = None,
2021
):
2122
path = path.resolve()
2223

24+
env = create_jinja_env(path.parent)
25+
template = env.get_template(path.name)
26+
result = template.render()
27+
28+
if output:
29+
output.parent.mkdir(parents=True, exist_ok=True)
30+
output.write_text(result, "utf-8")
31+
else:
32+
print(result)
33+
34+
35+
@app.command()
36+
def configs(yaml_path: Path):
37+
with yaml_path.open("rb") as f:
38+
data: ConfigsYaml = yaml.load(f, yaml.Loader)
39+
40+
output_dir = yaml_path.parent
41+
env = create_jinja_env(output_dir)
42+
template = env.get_template(data["template"])
43+
44+
for name, args in data["configs"].items():
45+
result = template.render(**data["defaults"], **args)
46+
(output_dir / name).with_suffix(".mlog").write_text(result, "utf-8")
47+
48+
49+
def create_jinja_env(template_dir: Path):
2350
env = Environment(
24-
loader=FileSystemLoader(path.parent),
51+
loader=FileSystemLoader(template_dir),
2552
line_statement_prefix="#%",
2653
line_comment_prefix="#%#",
2754
autoescape=False,
@@ -37,15 +64,13 @@ def main(
3764
"quote": filters.quote,
3865
"csr": filters.csr,
3966
}
67+
return env
4068

41-
template = env.get_template(path.name)
42-
result = template.render()
4369

44-
if output:
45-
output.parent.mkdir(parents=True, exist_ok=True)
46-
output.write_text(result, "utf-8")
47-
else:
48-
print(result)
70+
class ConfigsYaml(TypedDict):
71+
template: str
72+
defaults: dict[str, Any]
73+
configs: dict[str, dict[str, Any]]
4974

5075

5176
if __name__ == "__main__":

src/config/base.mlog.jinja

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# per-world config options for mlogv32
2+
3+
# reloaded after unpausing
4+
set TARGET_IPT {{#TARGET_IPT}}
5+
set BREAKPOINT_ADDRESS {{#BREAKPOINT_ADDRESS}}
6+
7+
# loaded at reset only
8+
set MEMORY_X_OFFSET {{#MEMORY_X_OFFSET}} # x offset from this proc to bottom left memory proc
9+
set MEMORY_Y_OFFSET {{#MEMORY_Y_OFFSET}} # y offset from this proc to bottom left memory proc
10+
set MEMORY_WIDTH {{#MEMORY_WIDTH}} # physical width of the memory procs
11+
12+
set ROM_SIZE {{#ROM_SIZE}} # ROM size in bytes (rx)
13+
set RAM_SIZE {{#RAM_SIZE}} # RAM size in bytes (rwx)
14+
set ICACHE_SIZE {{#ICACHE_SIZE}} # icache size in variables, or bytes of memory it can represent; 4x less dense than ROM/RAM
15+
16+
# computed values
17+
op add MEMORY_X @thisx MEMORY_X_OFFSET
18+
op add MEMORY_Y @thisy MEMORY_Y_OFFSET
19+
20+
stop
21+
22+
#%- if false
23+
# {% raw %}
24+
set {{ null
25+
set TARGET_IPT TARGET_IPT
26+
set BREAKPOINT_ADDRESS BREAKPOINT_ADDRESS
27+
set MEMORY_WIDTH MEMORY_WIDTH
28+
set ROM_SIZE ROM_SIZE
29+
set RAM_SIZE RAM_SIZE
30+
set ICACHE_SIZE ICACHE_SIZE
31+
set MEMORY_X MEMORY_X
32+
set MEMORY_Y MEMORY_Y
33+
# {% endraw %}
34+
#% endif

src/config/configs.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
template: base.mlog.jinja
2+
3+
defaults:
4+
TARGET_IPT: 500000
5+
BREAKPOINT_ADDRESS: '0x'
6+
7+
configs:
8+
riscv-arch-test:
9+
MEMORY_X_OFFSET: -11
10+
MEMORY_Y_OFFSET: -26
11+
MEMORY_WIDTH: 32
12+
ROM_SIZE: '0x480000'
13+
RAM_SIZE: '0x480000'
14+
ICACHE_SIZE: '0xc0000'

src/config/default.mlog

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/config/riscv-arch-test.mlog

Lines changed: 0 additions & 20 deletions
This file was deleted.

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)