Skip to content

Commit 48256c8

Browse files
oharboeclaude
andcommitted
util: carve identical macros out of a flat design, the parent a module
carve.py regroups a flat module's existing cells into identical tiles, one module built from the first tile and instantiated for every one, and leaves the rest as the parent: no RTL edited, no cell added. wrap_parent.py moves the parent's own cells into a module of their own, a port for every tile pin it meets, so the parent can be built as a macro beside its tiles and the top is instances only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 2860924 commit 48256c8

4 files changed

Lines changed: 1194 additions & 0 deletions

File tree

‎flow/test/test_carve.py‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#!/usr/bin/env python3
2+
"""carve.py on hand-written yosys JSON: two identical tiles and a parent."""
3+
4+
import copy
5+
import os
6+
import sys
7+
import unittest
8+
9+
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util"))
10+
11+
import carve # noqa: E402
12+
13+
14+
def dff(d, q, width=1):
15+
return {
16+
"type": "$dff",
17+
"parameters": {"WIDTH": format(width, "032b"), "CLK_POLARITY": "1"},
18+
"attributes": {},
19+
"port_directions": {"CLK": "input", "D": "input", "Q": "output"},
20+
"connections": {"CLK": [2], "D": d, "Q": q},
21+
}
22+
23+
24+
def xor(a, b, y):
25+
return {
26+
"type": "$xor",
27+
"parameters": {
28+
"A_WIDTH": format(len(a), "032b"),
29+
"B_WIDTH": format(len(b), "032b"),
30+
"Y_WIDTH": format(len(y), "032b"),
31+
},
32+
"attributes": {},
33+
"port_directions": {"A": "input", "B": "input", "Y": "output"},
34+
"connections": {"A": a, "B": b, "Y": y},
35+
}
36+
37+
38+
def design():
39+
"""in -> p (parent) ; t0 <= p ^ t1 ; t1 <= p ^ t0 ; out = t0 ^ t1 (parent).
40+
41+
Tiles a and b each own one register and the xor in front of it; the
42+
parent keeps p and the xor to the output. Bits: clk 2, in 3, out 4,
43+
p 5, t0 6, t1 7, xa 8, xb 9.
44+
"""
45+
return {
46+
"modules": {
47+
"top": {
48+
"attributes": {},
49+
"ports": {
50+
"clk": {"direction": "input", "bits": [2]},
51+
"in": {"direction": "input", "bits": [3]},
52+
"out": {"direction": "output", "bits": [4]},
53+
},
54+
"cells": {
55+
"p": dff([3], [5]),
56+
"xa": xor([5], [7], [8]),
57+
"ra": dff([8], [6]),
58+
"xb": xor([5], [6], [9]),
59+
"rb": dff([9], [7]),
60+
"xo": xor([6], [7], [4]),
61+
},
62+
"netnames": {
63+
"clk": {"hide_name": 0, "bits": [2]},
64+
"in": {"hide_name": 0, "bits": [3]},
65+
"out": {"hide_name": 0, "bits": [4]},
66+
"p": {"hide_name": 0, "bits": [5]},
67+
"t0": {"hide_name": 0, "bits": [6]},
68+
"t1": {"hide_name": 0, "bits": [7]},
69+
},
70+
}
71+
}
72+
}
73+
74+
75+
def spec():
76+
return {
77+
"module": "T",
78+
"tiles": [
79+
{
80+
"instance": "a",
81+
"seeds": [{"net": "t0"}],
82+
"ports": [
83+
{"net": "clk", "port": "clk"},
84+
{"net": "p", "port": "p"},
85+
{"net": "t1", "port": "other"},
86+
{"net": "t0", "port": "q"},
87+
],
88+
},
89+
{
90+
"instance": "b",
91+
"seeds": [{"net": "t1"}],
92+
"ports": [
93+
{"net": "clk", "port": "clk"},
94+
{"net": "p", "port": "p"},
95+
{"net": "t0", "port": "other"},
96+
{"net": "t1", "port": "q"},
97+
],
98+
},
99+
],
100+
}
101+
102+
103+
class CarveTest(unittest.TestCase):
104+
def test_two_tiles_and_parent(self):
105+
d = design()
106+
census = carve.carve(d, "top", spec())
107+
top = d["modules"]["top"]
108+
self.assertEqual(sorted(top["cells"]), ["a", "b", "p", "xo"])
109+
self.assertEqual(
110+
top["cells"]["a"]["connections"],
111+
{"clk": [2], "p": [5], "other": [7], "q": [6]},
112+
)
113+
self.assertEqual(
114+
top["cells"]["b"]["connections"],
115+
{"clk": [2], "p": [5], "other": [6], "q": [7]},
116+
)
117+
t = d["modules"]["T"]
118+
self.assertEqual(sorted(t["cells"]), ["ra", "xa"])
119+
self.assertEqual(
120+
{p: v["direction"] for p, v in t["ports"].items()},
121+
{"clk": "input", "p": "input", "other": "input", "q": "output"},
122+
)
123+
self.assertEqual([x["pins_in"] for x in census["tiles"]], [3, 3])
124+
self.assertEqual(census["parent_registers"], 1)
125+
126+
def test_multibit_register_split(self):
127+
m = {"cells": {"r": dff([3, 4], [5, 6], width=2)}}
128+
m["cells"]["r"]["type"] = "$sdff"
129+
m["cells"]["r"]["parameters"]["SRST_VALUE"] = "10"
130+
carve.split_flops(m)
131+
self.assertEqual(sorted(m["cells"]), ["r[0]", "r[1]"])
132+
self.assertEqual(m["cells"]["r[1]"]["connections"]["Q"], [6])
133+
self.assertEqual(m["cells"]["r[0]"]["parameters"]["SRST_VALUE"], "0")
134+
self.assertEqual(m["cells"]["r[1]"]["parameters"]["SRST_VALUE"], "1")
135+
136+
def test_unmapped_boundary_refused(self):
137+
s = spec()
138+
s["tiles"][0]["ports"] = s["tiles"][0]["ports"][:-1]
139+
with self.assertRaisesRegex(ValueError, "no port"):
140+
carve.carve(design(), "top", s)
141+
142+
def test_shared_cell_refused(self):
143+
d = design()
144+
# one xor in front of both registers
145+
top = d["modules"]["top"]
146+
top["cells"]["rb"]["connections"]["D"] = [8]
147+
del top["cells"]["xb"]
148+
with self.assertRaisesRegex(ValueError, "is in a and b"):
149+
carve.carve(d, "top", spec())
150+
151+
def test_different_boundary_refused(self):
152+
s = copy.deepcopy(spec())
153+
s["tiles"][1]["ports"][2]["port"] = "elsewhere"
154+
with self.assertRaisesRegex(ValueError, "different boundary"):
155+
carve.carve(design(), "top", s)
156+
157+
def test_stop_keeps_shared_logic_in_parent(self):
158+
# without the stop, xa and xb would both read p through one shared
159+
# inverter in front of them, claimed by both tiles
160+
d = design()
161+
top = d["modules"]["top"]
162+
top["cells"]["inv"] = {
163+
"type": "$not",
164+
"parameters": {"A_WIDTH": format(1, "032b"), "Y_WIDTH": format(1, "032b")},
165+
"attributes": {},
166+
"port_directions": {"A": "input", "Y": "output"},
167+
"connections": {"A": [5], "Y": [10]},
168+
}
169+
top["netnames"]["np"] = {"hide_name": 0, "bits": [10]}
170+
top["cells"]["xa"]["connections"]["A"] = [10]
171+
top["cells"]["xb"]["connections"]["A"] = [10]
172+
s = spec()
173+
for t in s["tiles"]:
174+
t["ports"][1] = {"net": "np", "port": "p"}
175+
with self.assertRaisesRegex(ValueError, "is in a and b"):
176+
carve.carve(copy.deepcopy(d), "top", copy.deepcopy(s))
177+
s["stops"] = [{"net": "np"}]
178+
carve.carve(d, "top", s)
179+
self.assertIn("inv", top["cells"])
180+
181+
def test_split_bitwise(self):
182+
m = {"cells": {"x": xor([3, 4], [5, 6], [7, 8])}}
183+
m["cells"]["m"] = {
184+
"type": "$mux",
185+
"parameters": {"WIDTH": format(2, "032b")},
186+
"attributes": {},
187+
"port_directions": {
188+
"A": "input",
189+
"B": "input",
190+
"S": "input",
191+
"Y": "output",
192+
},
193+
"connections": {"A": [3, 4], "B": [5, 6], "S": [9], "Y": [10, 11]},
194+
}
195+
carve.split_bitwise(m)
196+
self.assertEqual(sorted(m["cells"]), ["m[0]", "m[1]", "x[0]", "x[1]"])
197+
self.assertEqual(
198+
m["cells"]["x[1]"]["connections"], {"A": [4], "B": [6], "Y": [8]}
199+
)
200+
self.assertEqual(
201+
m["cells"]["m[1]"]["connections"], {"A": [4], "B": [6], "S": [9], "Y": [11]}
202+
)
203+
204+
205+
if __name__ == "__main__":
206+
unittest.main()

‎flow/test/test_wrap_parent.py‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env python3
2+
"""wrap_parent.py on a parent with two tiles: ports per tile pin, ties
3+
inside the parent, nets between tiles left at the top."""
4+
5+
import os
6+
import sys
7+
import unittest
8+
9+
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util"))
10+
11+
import wrap_parent # noqa: E402
12+
13+
TILE = {
14+
"ports": {
15+
"clk": {"direction": "input", "bits": [2]},
16+
"a": {"direction": "input", "bits": [3, 4]},
17+
"z": {"direction": "output", "bits": [5, 6]},
18+
},
19+
"cells": {},
20+
"netnames": {},
21+
}
22+
23+
24+
def design():
25+
# top: port x (bits 2, 3) feeds a parent cell r driving net 10; tile
26+
# t0 reads clk and net 10 / a constant; t1 reads t0's output alone
27+
return {
28+
"modules": {
29+
"top": {
30+
"ports": {
31+
"clk": {"direction": "input", "bits": [2]},
32+
"x": {"direction": "input", "bits": [3]},
33+
"y": {"direction": "output", "bits": [21]},
34+
},
35+
"cells": {
36+
"r": {
37+
"type": "$not",
38+
"port_directions": {"A": "input", "Y": "output"},
39+
"connections": {"A": [3], "Y": [10]},
40+
},
41+
"s": {
42+
"type": "$not",
43+
"port_directions": {"A": "input", "Y": "output"},
44+
"connections": {"A": [13], "Y": [21]},
45+
},
46+
"u_b0_0": {
47+
"type": "tile",
48+
"connections": {"clk": [2], "a": [10, "0"], "z": [11, 12]},
49+
},
50+
"u_b0_1": {
51+
"type": "tile",
52+
"connections": {"clk": [2], "a": [11, 12], "z": [13, 14]},
53+
},
54+
},
55+
"netnames": {
56+
"clk": {"bits": [2]},
57+
"x": {"bits": [3]},
58+
"y": {"bits": [21]},
59+
},
60+
},
61+
"tile": TILE,
62+
}
63+
}
64+
65+
66+
class WrapParent(unittest.TestCase):
67+
def setUp(self):
68+
self.d = wrap_parent.wrap(design(), "top", "rest")
69+
self.top = self.d["modules"]["top"]
70+
self.rest = self.d["modules"]["rest"]
71+
72+
def test_top_is_instances_only(self):
73+
self.assertEqual(sorted(self.top["cells"]), ["u_b0_0", "u_b0_1", "u_rest"])
74+
types = {c["type"] for c in self.top["cells"].values()}
75+
self.assertEqual(types, {"tile", "rest"})
76+
77+
def test_parent_cells_and_ports(self):
78+
self.assertIn("r", self.rest["cells"])
79+
self.assertIn("s", self.rest["cells"])
80+
for p in ["clk", "x", "y"]:
81+
self.assertIn(p, self.rest["ports"])
82+
83+
def test_a_port_per_tile_pin(self):
84+
ports = self.rest["ports"]
85+
# both tiles' clocks get a port each, so each sits on its tile
86+
self.assertEqual(ports["u_b0_0__clk"]["direction"], "output")
87+
self.assertEqual(ports["u_b0_1__clk"]["direction"], "output")
88+
self.assertEqual(ports["u_b0_0__clk"]["bits"], [2])
89+
# t1's output goes to the parent's cell s
90+
self.assertEqual(ports["u_b0_1__z"]["direction"], "input")
91+
self.assertEqual(ports["u_b0_1__z"]["bits"], [13])
92+
93+
def test_constant_tied_inside(self):
94+
a = self.rest["ports"]["u_b0_0__a"]["bits"]
95+
self.assertEqual(a[0], 10)
96+
ties = [c for c in self.rest["cells"].values() if c["type"] == "$pos"]
97+
self.assertEqual(len(ties), 1)
98+
self.assertEqual(ties[0]["connections"]["A"], ["0"])
99+
self.assertEqual(ties[0]["connections"]["Y"], [a[1]])
100+
101+
def test_tile_to_tile_stays_at_top(self):
102+
t0 = self.top["cells"]["u_b0_0"]["connections"]
103+
t1 = self.top["cells"]["u_b0_1"]["connections"]
104+
self.assertEqual(t0["z"], [11, 12])
105+
self.assertEqual(t1["a"], [11, 12])
106+
self.assertNotIn("u_b0_0__z", self.rest["ports"])
107+
self.assertNotIn("u_b0_1__a", self.rest["ports"])
108+
109+
def test_parent_instance_meets_tiles(self):
110+
rest = self.top["cells"]["u_rest"]["connections"]
111+
t0 = self.top["cells"]["u_b0_0"]["connections"]
112+
self.assertEqual(rest["u_b0_0__clk"], t0["clk"])
113+
self.assertEqual(rest["u_b0_0__a"], t0["a"])
114+
self.assertNotEqual(t0["clk"], [2])
115+
116+
117+
if __name__ == "__main__":
118+
unittest.main()

0 commit comments

Comments
 (0)