]>
Witch of Git - minecraft-eda/blob - genlib.py
2 Generate a cell library for redstone circuits.
7 from dataclasses
import dataclass
8 from typing
import List
, Dict
15 outputs
: Dict
[str, str]
17 def lib(self
, tab_size
=2, indent
=0) -> str:
22 f
"{pad}cell({self.name}) {{",
23 f
"{pad2}area: {self.area};",
24 *(f
"{pad2}pin({name}) {{ direction: input; }}"
25 for name
in self
.inputs
),
26 *(f
"{pad2}pin({name}) {{ direction: output; function: \"{func}\"; }}"
27 for name
, func
in self
.outputs
.items()),
35 def lib(self
, tab_size
=2, indent
=0) -> str:
38 padding
= ' ' * (tab_size
* indent
)
39 return padding
+ self
.text
.replace('\n', '\n' + padding
)
41 def print_lib(lib_name
, cells
):
42 print(f
"library({lib_name}) {{")
44 print(cell
.lib(tab_size
=2, indent
=1))
47 def gen_cells(max_pins
: int, or_nor
: bool):
48 yield Cell("BUF", 1, ["A"], {"Y": "A"})
49 yield Cell("NOT", 4, ["A"], {"Y": "!A"})
50 # @TODO: Generate inputs more nicely
51 all_inputs
= "ABCDEFGHIJKLMNOPQRSTUV"
52 for pins
in range(2, max_pins
+1):
53 inputs
= all_inputs
[:pins
]
55 for neg
in range(pins
+1):
57 ["!" + inputs
[i
] for i
in range(neg
)] +
58 [inputs
[i
] for i
in range(neg
, pins
)],
60 pin_name
= f
"{pins - neg}N{neg}"
61 yield Cell(f
"OR_{pin_name}", area
+ 1, inputs
, {"Y": func
})
62 yield Cell(f
"NOR_{pin_name}", area
+ 2, inputs
, {"Y": f
"!({func})"})
65 f
"OR_NOR_{pin_name}", area
+ 3, inputs
,
66 {"Y": func
, "Z": f
"!({func})"},
72 ff(IQ, IQN) { clocked_on: C; next_state: D; }
73 pin(C) { direction: input; clock: true; }
74 pin(D) { direction: input; }
75 pin(Q) { direction: output; function: "IQ"; }
82 ff(IQ, IQN) { clocked_on: "!C"; next_state: D; }
83 pin(C) { direction: input; clock: true; }
84 pin(D) { direction: input; }
85 pin(Q) { direction: output; function: "IQ"; }
89 if __name__
== '__main__':
90 parser
= argparse
.ArgumentParser(
91 description
="Generate a liberty file for Minecraft standard cells.",
94 '-n', '--max-pins', metavar
='N', type=int, choices
=range(2, 17), default
=5,
95 help="the maximum number of input pins on a single cell",
98 '-o', '--or-nor', action
='store_true',
99 help="generate two output combined or/nor gates",
101 args
= parser
.parse_args()
102 cells
= list(gen_cells(args
.max_pins
, args
.or_nor
))
105 print_lib("redstone", cells
)