| 1 |
from os import path |
| 2 |
import sys |
| 3 |
sys.path.append(path.join(path.dirname(__file__), "../..")) |
| 4 |
from himbaechel_dbgen.chip import * |
| 5 |
|
| 6 |
# Grid size including IOBs at edges |
| 7 |
X = 100 |
| 8 |
Y = 100 |
| 9 |
# LUT input count |
| 10 |
K = 4 |
| 11 |
# SLICEs per tile |
| 12 |
N = 8 |
| 13 |
# number of local wires |
| 14 |
Wl = N * (K + 1) + 16 |
| 15 |
# 1/Fc for bel input wire pips; local wire pips and neighbour pips |
| 16 |
Si = 6 |
| 17 |
Sq = 6 |
| 18 |
Sl = 1 |
| 19 |
|
| 20 |
dirs = [ # name, dx, dy |
| 21 |
("N", 0, -1), |
| 22 |
("NE", 1, -1), |
| 23 |
("E", 1, 0), |
| 24 |
("SE", 1, 1), |
| 25 |
("S", 0, 1), |
| 26 |
("SW", -1, 1), |
| 27 |
("W", -1, 0), |
| 28 |
("NW", -1, -1) |
| 29 |
] |
| 30 |
|
| 31 |
def create_switch_matrix(tt: TileType, inputs: list[str], outputs: list[str]): |
| 32 |
# FIXME: terrible routing matrix, just for a toy example... |
| 33 |
# constant wires |
| 34 |
tt.create_wire("GND", "GND", const_value="GND") |
| 35 |
tt.create_wire("VCC", "VCC", const_value="VCC") |
| 36 |
# switch wires |
| 37 |
for i in range(Wl): |
| 38 |
tt.create_wire(f"SWITCH{i}", "SWITCH") |
| 39 |
# neighbor wires |
| 40 |
for i in range(Wl): |
| 41 |
for d, dx, dy in dirs: |
| 42 |
tt.create_wire(f"{d}{i}", f"NEIGH_{d}") |
| 43 |
# input pips |
| 44 |
for i, w in enumerate(inputs): |
| 45 |
for j in range((i % Si), Wl, Si): |
| 46 |
tt.create_pip(f"SWITCH{j}", w, timing_class="SWINPUT") |
| 47 |
# output pips |
| 48 |
for i, w in enumerate(outputs): |
| 49 |
for j in range((i % Sq), Wl, Sq): |
| 50 |
tt.create_pip(w, f"SWITCH{j}", timing_class="SWINPUT") |
| 51 |
# constant pips |
| 52 |
for i in range(Wl): |
| 53 |
tt.create_pip("GND", f"SWITCH{i}") |
| 54 |
tt.create_pip("VCC", f"SWITCH{i}") |
| 55 |
# neighbour local pips |
| 56 |
for i in range(Wl): |
| 57 |
for j, (d, dx, dy) in enumerate(dirs): |
| 58 |
tt.create_pip(f"{d}{(i + j) % Wl}", f"SWITCH{i}", timing_class="SWNEIGH") |
| 59 |
# clock "ladder" |
| 60 |
if not tt.has_wire("CLK"): |
| 61 |
tt.create_wire(f"CLK", "TILE_CLK") |
| 62 |
tt.create_wire(f"CLK_PREV", "CLK_ROUTE") |
| 63 |
tt.create_pip(f"CLK_PREV", f"CLK") |
| 64 |
|
| 65 |
tt.create_group("SWITCHBOX", "SWITCHBOX") |
| 66 |
|
| 67 |
def create_logic_tiletype(chip: Chip): |
| 68 |
tt = chip.create_tile_type("LOGIC") |
| 69 |
# setup wires |
| 70 |
inputs = [] |
| 71 |
outputs = [] |
| 72 |
for i in range(N): |
| 73 |
for j in range(K): |
| 74 |
inputs.append(f"L{i}_I{j}") |
| 75 |
tt.create_wire(f"L{i}_I{j}", "LUT_INPUT") |
| 76 |
tt.create_wire(f"L{i}_D", "FF_DATA") |
| 77 |
tt.create_wire(f"L{i}_O", "LUT_OUT") |
| 78 |
tt.create_wire(f"L{i}_Q", "FF_OUT") |
| 79 |
outputs += [f"L{i}_O", f"L{i}_Q"] |
| 80 |
tt.create_wire(f"CLK", "TILE_CLK") |
| 81 |
# create logic cells |
| 82 |
for i in range(N): |
| 83 |
# LUT |
| 84 |
lut = tt.create_bel(f"L{i}_LUT", "LUT4", z=(i*2 + 0)) |
| 85 |
for j in range(K): |
| 86 |
tt.add_bel_pin(lut, f"I[{j}]", f"L{i}_I{j}", PinType.INPUT) |
| 87 |
tt.add_bel_pin(lut, "F", f"L{i}_O", PinType.OUTPUT) |
| 88 |
# FF data can come from LUT output or LUT I3 |
| 89 |
tt.create_pip(f"L{i}_O", f"L{i}_D") |
| 90 |
tt.create_pip(f"L{i}_I{K-1}", f"L{i}_D") |
| 91 |
# FF |
| 92 |
ff = tt.create_bel(f"L{i}_FF", "DFF", z=(i*2 + 1)) |
| 93 |
tt.add_bel_pin(ff, "D", f"L{i}_D", PinType.INPUT) |
| 94 |
tt.add_bel_pin(ff, "CLK", "CLK", PinType.INPUT) |
| 95 |
tt.add_bel_pin(ff, "Q", f"L{i}_Q", PinType.OUTPUT) |
| 96 |
create_switch_matrix(tt, inputs, outputs) |
| 97 |
return tt |
| 98 |
|
| 99 |
N_io = 2 |
| 100 |
|
| 101 |
def create_io_tiletype(chip: Chip): |
| 102 |
tt = chip.create_tile_type("IO") |
| 103 |
# setup wires |
| 104 |
inputs = [] |
| 105 |
outputs = [] |
| 106 |
for i in range(N_io): |
| 107 |
tt.create_wire(f"IO{i}_T", "IO_T") |
| 108 |
tt.create_wire(f"IO{i}_I", "IO_I") |
| 109 |
tt.create_wire(f"IO{i}_O", "IO_O") |
| 110 |
tt.create_wire(f"IO{i}_PAD", "IO_PAD") |
| 111 |
inputs += [f"IO{i}_T", f"IO{i}_I"] |
| 112 |
outputs += [f"IO{i}_O", ] |
| 113 |
tt.create_wire(f"CLK", "TILE_CLK") |
| 114 |
for i in range(N_io): |
| 115 |
io = tt.create_bel(f"IO{i}", "IOB", z=i) |
| 116 |
tt.add_bel_pin(io, "I", f"IO{i}_I", PinType.INPUT) |
| 117 |
tt.add_bel_pin(io, "T", f"IO{i}_T", PinType.INPUT) |
| 118 |
tt.add_bel_pin(io, "O", f"IO{i}_O", PinType.OUTPUT) |
| 119 |
tt.add_bel_pin(io, "PAD", f"IO{i}_PAD", PinType.INOUT) |
| 120 |
# Actually used in top left IO only |
| 121 |
tt.create_wire("GCLK_OUT", "GCLK") |
| 122 |
tt.create_pip("IO0_O", "GCLK_OUT") |
| 123 |
create_switch_matrix(tt, inputs, outputs) |
| 124 |
return tt |
| 125 |
|
| 126 |
def create_bram_tiletype(chip: Chip): |
| 127 |
Aw = 9 |
| 128 |
Dw = 16 |
| 129 |
|
| 130 |
tt = chip.create_tile_type("BRAM") |
| 131 |
inputs = [f"RAM_WA{i}" for i in range(Aw)] |
| 132 |
inputs += [f"RAM_RA{i}" for i in range(Aw)] |
| 133 |
inputs += [f"RAM_WE{i}" for i in range(Dw // 8)] |
| 134 |
inputs += [f"RAM_DI{i}" for i in range(Dw)] |
| 135 |
outputs = [f"RAM_DO{i}" for i in range(Dw)] |
| 136 |
for w in inputs: |
| 137 |
tt.create_wire(w, "RAM_IN") |
| 138 |
for w in outputs: |
| 139 |
tt.create_wire(w, "RAM_OUT") |
| 140 |
tt.create_wire(f"CLK", "TILE_CLK") |
| 141 |
ram = tt.create_bel(f"RAM", F"BRAM_{2**Aw}X{Dw}", z=0) |
| 142 |
tt.add_bel_pin(ram, "CLK", f"CLK", PinType.INPUT) |
| 143 |
for i in range(Aw): |
| 144 |
tt.add_bel_pin(ram, f"WA[{i}]", f"RAM_WA{i}", PinType.INPUT) |
| 145 |
tt.add_bel_pin(ram, f"RA[{i}]", f"RAM_RA{i}", PinType.INPUT) |
| 146 |
for i in range(Dw//8): |
| 147 |
tt.add_bel_pin(ram, f"WE[{i}]", f"RAM_WE{i}", PinType.INPUT) |
| 148 |
for i in range(Dw): |
| 149 |
tt.add_bel_pin(ram, f"DI[{i}]", f"RAM_DI{i}", PinType.INPUT) |
| 150 |
tt.add_bel_pin(ram, f"DO[{i}]", f"RAM_DO{i}", PinType.OUTPUT) |
| 151 |
create_switch_matrix(tt, inputs, outputs) |
| 152 |
return tt |
| 153 |
|
| 154 |
def create_corner_tiletype(ch): |
| 155 |
tt = ch.create_tile_type("NULL") |
| 156 |
tt.create_wire(f"CLK", "TILE_CLK") |
| 157 |
tt.create_wire(f"CLK_PREV", "CLK_ROUTE") |
| 158 |
tt.create_pip(f"CLK_PREV", f"CLK") |
| 159 |
|
| 160 |
tt.create_wire(f"GND", "GND", const_value="GND") |
| 161 |
tt.create_wire(f"VCC", "VCC", const_value="VCC") |
| 162 |
|
| 163 |
gnd = tt.create_bel(f"GND_DRV", f"GND_DRV", z=0) |
| 164 |
tt.add_bel_pin(gnd, "GND", "GND", PinType.OUTPUT) |
| 165 |
vcc = tt.create_bel(f"VCC_DRV", f"VCC_DRV", z=1) |
| 166 |
tt.add_bel_pin(vcc, "VCC", "VCC", PinType.OUTPUT) |
| 167 |
|
| 168 |
return tt |
| 169 |
|
| 170 |
def is_corner(x, y): |
| 171 |
return ((x == 0) or (x == (X-1))) and ((y == 0) or (y == (Y-1))) |
| 172 |
|
| 173 |
def create_nodes(ch): |
| 174 |
for y in range(Y): |
| 175 |
#print(f"generating nodes for row {y}") |
| 176 |
for x in range(X): |
| 177 |
if not is_corner(x, y): |
| 178 |
# connect up actual neighbours |
| 179 |
local_nodes = [[NodeWire(x, y, f"SWITCH{i}")] for i in range(Wl)] |
| 180 |
for d, dx, dy in dirs: |
| 181 |
x1 = x - dx |
| 182 |
y1 = y - dy |
| 183 |
if x1 < 0 or x1 >= X or y1 < 0 or y1 >= Y or is_corner(x1, y1): |
| 184 |
continue |
| 185 |
for i in range(Wl): |
| 186 |
local_nodes[i].append(NodeWire(x1, y1, f"{d}{i}")) |
| 187 |
for n in local_nodes: |
| 188 |
ch.add_node(n) |
| 189 |
# connect up clock ladder (not intended to be a sensible clock structure) |
| 190 |
if y != 1: # special case where the node has 3 wires |
| 191 |
if y == 0: |
| 192 |
if x == 0: |
| 193 |
# clock source: IO |
| 194 |
clk_node = [NodeWire(1, 0, "GCLK_OUT")] |
| 195 |
else: |
| 196 |
# clock source: left |
| 197 |
clk_node = [NodeWire(x-1, y, "CLK")] |
| 198 |
else: |
| 199 |
# clock source: above |
| 200 |
clk_node = [NodeWire(x, y-1, "CLK")] |
| 201 |
clk_node.append(NodeWire(x, y, "CLK_PREV")) |
| 202 |
if y == 0: |
| 203 |
clk_node.append(NodeWire(x, y+1, "CLK_PREV")) |
| 204 |
ch.add_node(clk_node) |
| 205 |
|
| 206 |
def set_timings(ch): |
| 207 |
speed = "DEFAULT" |
| 208 |
tmg = ch.set_speed_grades([speed]) |
| 209 |
# --- Routing Delays --- |
| 210 |
# Notes: A simpler timing model could just use intrinsic delay and ignore R and Cs. |
| 211 |
# R and C values don't have to be physically realistic, just in agreement with themselves to provide |
| 212 |
# a meaningful scaling of delay with fanout. Units are subject to change. |
| 213 |
tmg.set_pip_class(grade=speed, name="SWINPUT", |
| 214 |
delay=TimingValue(80), # 80ps intrinstic delay |
| 215 |
in_cap=TimingValue(5000), # 5pF |
| 216 |
out_res=TimingValue(1000), # 1ohm |
| 217 |
) |
| 218 |
tmg.set_pip_class(grade=speed, name="SWOUTPUT", |
| 219 |
delay=TimingValue(100), # 100ps intrinstic delay |
| 220 |
in_cap=TimingValue(5000), # 5pF |
| 221 |
out_res=TimingValue(800), # 0.8ohm |
| 222 |
) |
| 223 |
tmg.set_pip_class(grade=speed, name="SWNEIGH", |
| 224 |
delay=TimingValue(120), # 120ps intrinstic delay |
| 225 |
in_cap=TimingValue(7000), # 7pF |
| 226 |
out_res=TimingValue(1200), # 1.2ohm |
| 227 |
) |
| 228 |
# TODO: also support node/wire delays and add an example of them |
| 229 |
|
| 230 |
# --- Cell delays --- |
| 231 |
lut = ch.timing.add_cell_variant(speed, "LUT4") |
| 232 |
for j in range(K): |
| 233 |
lut.add_comb_arc(f"I[{j}]", "F", TimingValue(150 + j * 15)) |
| 234 |
dff = ch.timing.add_cell_variant(speed, "DFF") |
| 235 |
dff.add_setup_hold("CLK", "D", ClockEdge.RISING, TimingValue(150), TimingValue(25)) |
| 236 |
dff.add_clock_out("CLK", "Q", ClockEdge.RISING, TimingValue(200)) |
| 237 |
|
| 238 |
def main(): |
| 239 |
ch = Chip("example", "EX1", X, Y) |
| 240 |
# Init constant ids |
| 241 |
ch.strs.read_constids(path.join(path.dirname(__file__), "constids.inc")) |
| 242 |
ch.read_gfxids(path.join(path.dirname(__file__), "gfxids.inc")) |
| 243 |
logic = create_logic_tiletype(ch) |
| 244 |
io = create_io_tiletype(ch) |
| 245 |
bram = create_bram_tiletype(ch) |
| 246 |
null = create_corner_tiletype(ch) |
| 247 |
# Setup tile grid |
| 248 |
for x in range(X): |
| 249 |
for y in range(Y): |
| 250 |
if x == 0 or x == X-1: # left/right side IO |
| 251 |
if y == 0 or y == Y-1: # corner |
| 252 |
ch.set_tile_type(x, y, "NULL") |
| 253 |
else: |
| 254 |
ch.set_tile_type(x, y, "IO") |
| 255 |
elif y == 0 or y == Y-1: # top/bottom side IO |
| 256 |
ch.set_tile_type(x, y, "IO") |
| 257 |
elif (y % 15) == 7: # BRAM |
| 258 |
ch.set_tile_type(x, y, "BRAM") |
| 259 |
else: |
| 260 |
ch.set_tile_type(x, y, "LOGIC") |
| 261 |
# Create nodes between tiles |
| 262 |
create_nodes(ch) |
| 263 |
set_timings(ch) |
| 264 |
ch.write_bba(sys.argv[1]) |
| 265 |
|
| 266 |
if __name__ == '__main__': |
| 267 |
main() |