|
| 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() |
0 commit comments