Skip to content

Commit d3c650e

Browse files
committed
Add JSON metadata to generated schematics
1 parent 2f53c77 commit d3c650e

File tree

2 files changed

+93
-14
lines changed

2 files changed

+93
-14
lines changed

python/src/mlogv32/preprocessor/app.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
LocalVariablesEnv,
2424
)
2525
from .filters import FILTERS, ram_var
26-
from .models import BuildConfig
26+
from .models import BuildConfig, Metadata
2727
from .parser import (
2828
MlogError,
2929
check_unsaved_variables,
@@ -157,6 +157,8 @@ def build(
157157
include_memory = True
158158
include_debugger = True
159159

160+
meta = Metadata()
161+
160162
config = BuildConfig.load(yaml_path)
161163

162164
if cpu_config_name:
@@ -183,6 +185,8 @@ def build(
183185
yaml_path.parent / "generated_config.mlog",
184186
**config_args,
185187
)
188+
189+
meta.uart_fifo_capacity = int(config_args["UART_FIFO_CAPACITY"])
186190
else:
187191
if include_memory:
188192
raise ValueError(
@@ -386,6 +390,7 @@ def add_with_label(block: Block, **labels: Unpack[Labels]):
386390
uart_links = list[ProcessorLink]()
387391
for x in lenrange(5, 4, 2):
388392
for y in lenrange(18, -4, -2):
393+
meta.uarts.append((x, y))
389394
add_peripheral(simple_block(Content.MEMORY_BANK, x, y))
390395
uart_links.append(ProcessorLink(x, y, ""))
391396

@@ -407,6 +412,10 @@ def add_with_label(block: Block, **labels: Unpack[Labels]):
407412
incr_link = ProcessorLink(9, 17, "")
408413
config_link = ProcessorLink(9, 16, "")
409414

415+
meta.registers = (registers_link.x, registers_link.y)
416+
meta.csrs = (csrs_link.x, csrs_link.y)
417+
meta.config = (config_link.x, config_link.y)
418+
410419
add_with_label(
411420
simple_block(Content.MESSAGE, 11, 19),
412421
left="REGISTERS",
@@ -433,6 +442,11 @@ def add_with_label(block: Block, **labels: Unpack[Labels]):
433442
pause_switch_link = ProcessorLink(11, 17, "")
434443
single_step_switch_link = ProcessorLink(11, 16, "")
435444

445+
meta.error_output = (error_output_link.x, error_output_link.y)
446+
meta.power_switch = (power_switch_link.x, power_switch_link.y)
447+
meta.pause_switch = (pause_switch_link.x, pause_switch_link.y)
448+
meta.single_step_switch = (single_step_switch_link.x, single_step_switch_link.y)
449+
436450
add_peripheral(ram_schem, 13, 16)
437451
add_peripheral(ram_schem, 12, 17)
438452
add_peripheral(ram_schem, 13, 17)
@@ -474,6 +488,10 @@ def add_with_label(block: Block, **labels: Unpack[Labels]):
474488
controller_link = ProcessorLink(16, 0, "")
475489

476490
if include_cpu:
491+
meta.cpu = (controller_link.x, controller_link.y)
492+
meta.cpu_width = width
493+
meta.cpu_height = height
494+
477495
schem.add_block(
478496
Block(
479497
block=Content.WORLD_PROCESSOR,
@@ -545,19 +563,26 @@ def add_with_label(block: Block, **labels: Unpack[Labels]):
545563
else:
546564
data = bytes()
547565

566+
base_x = config_link.x + config_args["MEMORY_X_OFFSET"]
548567
base_y = config_link.y + config_args["MEMORY_Y_OFFSET"]
549-
for y in lenrange(
550-
0,
551-
config_args["PROGRAM_ROM_ROWS"]
552-
+ config_args["DATA_ROM_ROWS"]
553-
+ config_args["RAM_ROWS"]
554-
+ config_args["ICACHE_ROWS"],
555-
):
556-
for x in lenrange(
557-
config_link.x + config_args["MEMORY_X_OFFSET"],
558-
config_args["MEMORY_WIDTH"],
559-
):
560-
if y < config_args["PROGRAM_ROM_ROWS"] + config_args["DATA_ROM_ROWS"]:
568+
569+
memory_width = config_args["MEMORY_WIDTH"]
570+
rom_height = config_args["PROGRAM_ROM_ROWS"] + config_args["DATA_ROM_ROWS"]
571+
memory_height = (
572+
rom_height + config_args["RAM_ROWS"] + config_args["ICACHE_ROWS"]
573+
)
574+
575+
meta.memory = (base_x, base_y)
576+
meta.memory_width = memory_width
577+
meta.memory_height = memory_height
578+
579+
meta.rom_processors = memory_width * rom_height
580+
meta.ram_processors = memory_width * config_args["RAM_ROWS"]
581+
meta.icache_processors = memory_width * config_args["ICACHE_ROWS"]
582+
583+
for y in lenrange(0, memory_height):
584+
for x in lenrange(base_x, memory_width):
585+
if y < rom_height:
561586
if i < len(data):
562587
payload = "".join(chr(174 + c) for c in data[i : i + 16384])
563588
i += 16384
@@ -617,8 +642,15 @@ def add_with_label(block: Block, **labels: Unpack[Labels]):
617642
)
618643

619644
if schem.tiles:
645+
_, _, x_offset, y_offset = schem.get_dimensions(offsets=True)
646+
schem.tags["mlogv32_metadata"] = meta.model_dump_json(
647+
exclude_defaults=True,
648+
context={"offsets": (x_offset, y_offset)},
649+
)
650+
620651
w, h = schem.get_dimensions()
621652
print(f"Schematic size: {w}x{h}")
653+
622654
if output:
623655
print(f"Writing schematic to file: {output}")
624656
schem.write_file(str(output))

python/src/mlogv32/preprocessor/models.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
from typing import Annotated, Any, Iterable, Iterator, Literal
88

99
import yaml
10-
from pydantic import AfterValidator, BaseModel, Field, field_validator
10+
from pydantic import (
11+
AfterValidator,
12+
BaseModel,
13+
Field,
14+
SerializationInfo,
15+
WrapSerializer,
16+
field_validator,
17+
)
1118

1219
from mlogv32.preprocessor.constants import CSRS
1320

@@ -149,3 +156,43 @@ def load(cls, path: str | Path):
149156

150157
with relative_path_root(path.parent):
151158
return cls.model_validate(data)
159+
160+
161+
def _serialize_point(point: tuple[int, int], handler: Any, info: SerializationInfo):
162+
if info.context:
163+
x, y = point
164+
x_offset, y_offset = info.context["offsets"]
165+
return handler((x + x_offset, y + y_offset))
166+
return handler()
167+
168+
169+
type MetaPoint2 = Annotated[
170+
tuple[int, int],
171+
WrapSerializer(_serialize_point),
172+
]
173+
174+
175+
class Metadata(BaseModel):
176+
uarts: list[MetaPoint2] = Field(default_factory=list)
177+
178+
registers: MetaPoint2 | None = None
179+
csrs: MetaPoint2 | None = None
180+
config: MetaPoint2 | None = None
181+
uart_fifo_capacity: int | None = None
182+
183+
error_output: MetaPoint2 | None = None
184+
power_switch: MetaPoint2 | None = None
185+
pause_switch: MetaPoint2 | None = None
186+
single_step_switch: MetaPoint2 | None = None
187+
188+
cpu: MetaPoint2 | None = None
189+
cpu_width: int | None = None
190+
cpu_height: int | None = None
191+
192+
memory: MetaPoint2 | None = None
193+
memory_width: int | None = None
194+
memory_height: int | None = None
195+
196+
rom_processors: int | None = None
197+
ram_processors: int | None = None
198+
icache_processors: int | None = None

0 commit comments

Comments
 (0)