Debian Codegraph @

himbaechel/uarch/gowin/gowin_arch_gen.py

raw
1 from os import path
2 import sys
3
4 import importlib.resources
5 import gzip
6 import re
7 import argparse
8
9 sys.path.append(path.join(path.dirname(__file__), "../.."))
10 from himbaechel_dbgen.chip import *
11 from apycula import chipdb
12 from apycula.chipdb import load_chipdb
13
14 # Bel flags
15 BEL_FLAG_SIMPLE_IO = 0x100
16
17 # Wire flags
18 WIRE_FLAG_CLOCK_GATE = 0x1
19 WIRE_FLAG_BOTTOM_HALF = 0x2 # the wire is located in the bottom half of the chip
20
21 # Chip flags
22 CHIP_HAS_SP32 = 0x1
23 CHIP_NEED_SP_FIX = 0x2
24 CHIP_NEED_BSRAM_OUTREG_FIX = 0x4
25 CHIP_NEED_BLKSEL_FIX = 0x8
26 CHIP_HAS_BANDGAP = 0x10
27 CHIP_HAS_PLL_HCLK = 0x20
28 CHIP_HAS_CLKDIV_HCLK = 0x40
29 CHIP_HAS_PINCFG = 0x80
30 CHIP_HAS_DFF67 = 0x100
31 CHIP_HAS_CIN_MUX = 0x200
32 CHIP_NEED_BSRAM_RESET_FIX = 0x400
33 CHIP_NEED_SDP_FIX = 0x800
34 CHIP_NEED_CFGPINS_INVERSION = 0x1000
35 CHIP_HAS_I2CCFG = 0x2000
36 CHIP_HAS_5A_DSP = 0x4000
37 CHIP_NEED_BSRAM_DP_CE_FIX = 0x8000
38
39 # Tile flags
40 TILE_I3C_CAPABLE_IO = 0x1
41
42 # Z of the bels
43 # sync with C++ part!
44 LUT0_Z = 0 # z(DFFx) = z(LUTx) + 1
45 LUT7_Z = 14
46 MUX20_Z = 16
47 MUX21_Z = 18
48 MUX23_Z = 22
49 MUX27_Z = 29
50 ALU0_Z = 30 # : 35, 6 ALUs
51 RAMW_Z = 36 # RAM16SDP4
52
53 IOBA_Z = 50
54 IOBB_Z = 51
55
56 IOLOGICA_Z = 70
57 IDES16_Z = 74
58 OSER16_Z = 75
59
60 BUFG_Z = 76 # : 81 reserve just in case
61 BSRAM_Z = 100
62
63 OSC_Z = 274
64 PLL_Z = 275
65 GSR_Z = 276
66 VCC_Z = 277
67 GND_Z = 278
68 BANDGAP_Z = 279
69
70 DQCE_Z = 280 # : 286 reserve for 6 DQCEs
71 DCS_Z = 286 # : 288 reserve for 2 DCSs
72 DHCEN_Z = 288 # : 298
73
74 USERFLASH_Z = 298
75
76 EMCU_Z = 300
77
78 MIPIOBUF_Z = 301
79 MIPIIBUF_Z = 302
80
81 DLLDLY_Z = 303 # : 305 reserve for 2 DLLDLYs
82
83 PINCFG_Z = 400
84 ADC_Z = 401
85
86 DSP_Z = 509
87
88 DSP_0_Z = 511 # DSP macro 0
89 PADD18_0_0_Z = 512
90 PADD9_0_0_Z = 512 + 1
91 PADD9_0_1_Z = 512 + 2
92 PADD18_0_1_Z = 516
93 PADD9_0_2_Z = 516 + 1
94 PADD9_0_3_Z = 516 + 2
95
96 MULT18X18_0_0_Z = 520
97 MULT9X9_0_0_Z = 520 + 1
98 MULT9X9_0_1_Z = 520 + 2
99 MULT18X18_0_1_Z = 524
100 MULT9X9_0_2_Z = 524 + 1
101 MULT9X9_0_3_Z = 524 + 2
102
103 ALU54D_0_Z = 524 + 3
104 MULTALU18X18_0_Z = 528
105 MULTALU36X18_0_Z = 528 + 1
106 MULTADDALU18X18_0_Z = 528 + 2
107
108 MULT36X36_Z = 528 + 3
109
110 DSP_1_Z = 543 # DSP macro 1
111 PADD18_1_0_Z = 544
112 PADD9_1_0_Z = 544 + 1
113 PADD9_1_1_Z = 544 + 2
114 PADD18_1_1_Z = 548
115 PADD9_1_2_Z = 548 + 1
116 PADD9_1_3_Z = 548 + 2
117
118 MULT18X18_1_0_Z = 552
119 MULT9X9_1_0_Z = 552 + 1
120 MULT9X9_1_1_Z = 552 + 2
121 MULT18X18_1_1_Z = 556
122 MULT9X9_1_2_Z = 556 + 1
123 MULT9X9_1_3_Z = 556 + 2
124
125 ALU54D_1_Z = 556 + 3
126 MULTALU18X18_1_Z = 560
127 MULTALU36X18_1_Z = 560 + 1
128 MULTADDALU18X18_1_Z = 560 + 2
129
130 CLKDIV2_0_Z = 610
131 CLKDIV2_1_Z = 611
132 CLKDIV2_2_Z = 612
133 CLKDIV2_3_Z = 613
134
135
136 CLKDIV_0_Z = 620
137 CLKDIV_1_Z = 621
138 CLKDIV_2_Z = 622
139 CLKDIV_3_Z = 623
140
141 MULT12X12_0_Z = 640
142 MULT12X12_1_Z = 641
143 MULTADDALU12X12_Z = 642
144 MULTALU27X18_Z = 643
145
146 # =======================================
147 # Chipdb additional info
148 # =======================================
149 @dataclass
150 class TileExtraData(BBAStruct):
151 tile_class: IdString # The general functionality of the slightly different tiles,
152 # let's say the behavior of LUT+DFF in the tiles are completely identical,
153 # but one of them also contains clock-wire switches,
154 # then we assign them to the same LOGIC class.
155 io16_x_off: int = 0 # OSER16/IDES16 offsets to the aux cell
156 io16_y_off: int = 0
157 tile_flags: int = 0
158
159 def serialise_lists(self, context: str, bba: BBAWriter):
160 pass
161 def serialise(self, context: str, bba: BBAWriter):
162 bba.u32(self.tile_class.index)
163 bba.u16(self.io16_x_off)
164 bba.u16(self.io16_y_off)
165 bba.u32(self.tile_flags)
166
167 @dataclass
168 class BottomIOCnd(BBAStruct):
169 wire_a_net: IdString
170 wire_b_net: IdString
171
172 def serialise_lists(self, context: str, bba: BBAWriter):
173 pass
174 def serialise(self, context: str, bba: BBAWriter):
175 bba.u32(self.wire_a_net.index)
176 bba.u32(self.wire_b_net.index)
177
178 @dataclass
179 class BottomIO(BBAStruct):
180 conditions: list[BottomIOCnd] = field(default_factory = list)
181
182 def serialise_lists(self, context: str, bba: BBAWriter):
183 bba.label(f"{context}_conditions")
184 for i, cnd in enumerate(self.conditions):
185 cnd.serialise(f"{context}_cnd{i}", bba)
186
187 def serialise(self, context: str, bba: BBAWriter):
188 bba.slice(f"{context}_conditions", len(self.conditions))
189
190 # spine -> bel for different bels
191 @dataclass
192 class SpineBel(BBAStruct):
193 spine: IdString
194 bel_x: int
195 bel_y: int
196 bel_z: int
197
198 def serialise_lists(self, context: str, bba: BBAWriter):
199 pass
200 def serialise(self, context: str, bba: BBAWriter):
201 bba.u32(self.spine.index)
202 bba.u32(self.bel_x)
203 bba.u32(self.bel_y)
204 bba.u32(self.bel_z)
205
206 # io -> dlldly bels
207 @dataclass
208 class IoBel(BBAStruct):
209 io: IdString
210 dlldly: IdString
211
212 def serialise_lists(self, context: str, bba: BBAWriter):
213 pass
214 def serialise(self, context: str, bba: BBAWriter):
215 bba.u32(self.io.index)
216 bba.u32(self.dlldly.index)
217
218 # wire -> bel for DHCEN bels
219 @dataclass
220 class WireBel(BBAStruct):
221 pip_xy: IdString
222 pip_dst: IdString
223 pip_src: IdString
224 bel_x: int
225 bel_y: int
226 bel_z: int
227 hclk_side: IdString
228
229 def serialise_lists(self, context: str, bba: BBAWriter):
230 pass
231 def serialise(self, context: str, bba: BBAWriter):
232 bba.u32(self.pip_xy.index)
233 bba.u32(self.pip_dst.index)
234 bba.u32(self.pip_src.index)
235 bba.u32(self.bel_x)
236 bba.u32(self.bel_y)
237 bba.u32(self.bel_z)
238 bba.u32(self.hclk_side.index)
239
240 # segment column description
241 @dataclass
242 class Segment(BBAStruct):
243 x: int
244 seg_idx: int
245 min_x: int
246 min_y: int
247 max_x: int
248 max_y: int
249 top_row: int
250 bottom_row: int
251 top_wire: IdString
252 bottom_wire: IdString
253 top_gate_wire: list[IdString] = field(default_factory = list)
254 bottom_gate_wire: list[IdString] = field(default_factory = list)
255
256 def serialise_lists(self, context: str, bba: BBAWriter):
257 bba.label(f"{context}_top_gate_wire")
258 for i, wire in enumerate(self.top_gate_wire):
259 bba.u32(wire.index)
260 bba.label(f"{context}_bottom_gate_wire")
261 for i, wire in enumerate(self.bottom_gate_wire):
262 bba.u32(wire.index)
263
264 def serialise(self, context: str, bba: BBAWriter):
265 bba.u16(self.x)
266 bba.u16(self.seg_idx)
267 bba.u16(self.min_x)
268 bba.u16(self.min_y)
269 bba.u16(self.max_x)
270 bba.u16(self.max_y)
271 bba.u16(self.top_row)
272 bba.u16(self.bottom_row)
273 bba.u32(self.top_wire.index)
274 bba.u32(self.bottom_wire.index)
275 bba.slice(f"{context}_top_gate_wire", len(self.top_gate_wire))
276 bba.slice(f"{context}_bottom_gate_wire", len(self.bottom_gate_wire))
277
278 @dataclass
279 class SpineSelectWire(BBAStruct):
280 spine: IdString
281 x: int
282 y: int
283 wire: IdString
284 vcc_gnd: int
285
286 def serialise_lists(self, context: str, bba: BBAWriter):
287 pass
288 def serialise(self, context: str, bba: BBAWriter):
289 bba.u32(self.spine.index)
290 bba.u16(self.x)
291 bba.u16(self.y)
292 bba.u32(self.wire.index)
293 bba.u32(self.vcc_gnd)
294
295 @dataclass
296 class ChipExtraData(BBAStruct):
297 strs: StringPool
298 flags: int
299 center_row: int
300 center_col: int
301 dcs_prefix: IdString = field(default = None)
302 bottom_io: BottomIO = field(default = None)
303 diff_io_types: list[IdString] = field(default_factory = list)
304 dqce_bels: list[SpineBel] = field(default_factory = list)
305 dcs_bels: list[SpineBel] = field(default_factory = list)
306 dhcen_bels: list[WireBel] = field(default_factory = list)
307 io_dlldly_bels: list[IoBel] = field(default_factory = list)
308 segments: list[Segment] = field(default_factory = list)
309 spine_select_wires_top: list[SpineSelectWire] = field(default_factory = list)
310 spine_select_wires_bottom: list[SpineSelectWire] = field(default_factory = list)
311
312 def set_dcs_prefix(self, prefix: str):
313 self.dcs_prefix = self.strs.id(prefix)
314
315 def create_bottom_io(self):
316 self.bottom_io = BottomIO()
317
318 def add_bottom_io_cnd(self, net_a: str, net_b: str):
319 self.bottom_io.conditions.append(BottomIOCnd(self.strs.id(net_a), self.strs.id(net_b)))
320
321 def add_diff_io_type(self, diff_type: str):
322 self.diff_io_types.append(self.strs.id(diff_type))
323
324 def add_dhcen_bel(self, pip_xy: str, pip_dst: str, pip_src, x: int, y: int, z: int, side: str):
325 self.dhcen_bels.append(WireBel(self.strs.id(pip_xy), self.strs.id(pip_dst), self.strs.id(pip_src), x, y, z, self.strs.id(side)))
326
327 def add_dqce_bel(self, spine: str, x: int, y: int, z: int):
328 self.dqce_bels.append(SpineBel(self.strs.id(spine), x, y, z))
329
330 def add_dcs_bel(self, spine: str, x: int, y: int, z: int):
331 self.dcs_bels.append(SpineBel(self.strs.id(spine), x, y, z))
332
333 def add_io_dlldly_bel(self, io: str, dlldly: str):
334 self.io_dlldly_bels.append(IoBel(self.strs.id(io), self.strs.id(dlldly)))
335
336 def add_segment(self, x: int, seg_idx: int, min_x: int, min_y: int, max_x: int, max_y: int,
337 top_row: int, bottom_row: int, top_wire: str, bottom_wire: str, top_gate_wire: list, bottom_gate_wire: list):
338 new_seg = Segment(x, seg_idx, min_x, min_y, max_x, max_y, top_row, bottom_row,
339 self.strs.id(top_wire), self.strs.id(bottom_wire),
340 [self.strs.id(top_gate_wire[0])], [self.strs.id(bottom_gate_wire[0])])
341 if top_gate_wire[1]:
342 new_seg.top_gate_wire.append(self.strs.id(top_gate_wire[1]))
343 else:
344 new_seg.top_gate_wire.append(self.strs.id(''))
345 if bottom_gate_wire[1]:
346 new_seg.bottom_gate_wire.append(self.strs.id(bottom_gate_wire[1]))
347 else:
348 new_seg.bottom_gate_wire.append(self.strs.id(''))
349 self.segments.append(new_seg)
350
351 def add_spine_select_wire_top(self, spine: str, x: int, y: int, wire: str, vcc_gnd: int):
352 self.spine_select_wires_top.append(SpineSelectWire(self.strs.id(spine), x, y, self.strs.id(wire), vcc_gnd))
353
354 def add_spine_select_wire_bottom(self, spine: str, x: int, y: int, wire: str, vcc_gnd: int):
355 self.spine_select_wires_bottom.append(SpineSelectWire(self.strs.id(spine), x, y, self.strs.id(wire), vcc_gnd))
356
357 def serialise_lists(self, context: str, bba: BBAWriter):
358 self.bottom_io.serialise_lists(f"{context}_bottom_io", bba)
359 for i, t in enumerate(self.segments):
360 t.serialise_lists(f"{context}_segment{i}", bba)
361 bba.label(f"{context}_diff_io_types")
362 for i, diff_io_type in enumerate(self.diff_io_types):
363 bba.u32(diff_io_type.index)
364 bba.label(f"{context}_dqce_bels")
365 for i, t in enumerate(self.dqce_bels):
366 t.serialise(f"{context}_dqce_bel{i}", bba)
367 bba.label(f"{context}_dcs_bels")
368 for i, t in enumerate(self.dcs_bels):
369 t.serialise(f"{context}_dcs_bel{i}", bba)
370 bba.label(f"{context}_dhcen_bels")
371 for i, t in enumerate(self.dhcen_bels):
372 t.serialise(f"{context}_dhcen_bel{i}", bba)
373 bba.label(f"{context}_io_dlldly_bels")
374 for i, t in enumerate(self.io_dlldly_bels):
375 t.serialise(f"{context}_io_dlldly_bel{i}", bba)
376 bba.label(f"{context}_segments")
377 for i, t in enumerate(self.segments):
378 t.serialise(f"{context}_segment{i}", bba)
379 bba.label(f"{context}_spine_select_wires_top")
380 for i, t in enumerate(self.spine_select_wires_top):
381 t.serialise(f"{context}_spine_select_wire_top{i}", bba)
382 bba.label(f"{context}_spine_select_wires_bottom")
383 for i, t in enumerate(self.spine_select_wires_bottom):
384 t.serialise(f"{context}_spine_select_wire_bottom{i}", bba)
385
386 def serialise(self, context: str, bba: BBAWriter):
387 bba.u32(self.flags)
388 bba.u32(self.dcs_prefix.index)
389 bba.u16(self.center_row)
390 bba.u16(self.center_col)
391 self.bottom_io.serialise(f"{context}_bottom_io", bba)
392 bba.slice(f"{context}_diff_io_types", len(self.diff_io_types))
393 bba.slice(f"{context}_dqce_bels", len(self.dqce_bels))
394 bba.slice(f"{context}_dcs_bels", len(self.dcs_bels))
395 bba.slice(f"{context}_dhcen_bels", len(self.dhcen_bels))
396 bba.slice(f"{context}_io_dlldly_bels", len(self.io_dlldly_bels))
397 bba.slice(f"{context}_segments", len(self.segments))
398 bba.slice(f"{context}_spine_select_wires_top", len(self.spine_select_wires_top))
399 bba.slice(f"{context}_spine_select_wires_bottom", len(self.spine_select_wires_bottom))
400
401 @dataclass
402 class PackageExtraData(BBAStruct):
403 strs: StringPool
404 cst: list
405
406 def serialise_lists(self, context: str, bba: BBAWriter):
407 bba.label(f"{context}_constraints")
408 for (net, row, col, bel, iostd) in self.cst:
409 bba.u32(self.strs.id(net).index)
410 bba.u32(row)
411 bba.u32(col)
412 bba.u32(ord(bel[0])-ord('A')+IOBA_Z)
413 bba.u32(self.strs.id(iostd).index if iostd else 0)
414
415 def serialise(self, context: str, bba: BBAWriter):
416 bba.slice(f"{context}_constraints", len(self.cst))
417
418 @dataclass
419 class PadExtraData(BBAStruct):
420 # Which PLL does this pad belong to.
421 pll_tile: IdString
422 pll_bel: IdString
423 pll_type: IdString
424
425 def serialise_lists(self, context: str, bba: BBAWriter):
426 pass
427 def serialise(self, context: str, bba: BBAWriter):
428 bba.u32(self.pll_tile.index)
429 bba.u32(self.pll_bel.index)
430 bba.u32(self.pll_type.index)
431
432 # Unique features of the tiletype
433 class TypeDesc:
434 def __init__(self, dups, tiletype = '', extra_func = None, sfx = 0):
435 self.tiletype = tiletype
436 self.extra_func = extra_func
437 self.dups = dups
438 self.sfx = sfx
439 created_tiletypes = {}
440
441 # get timing class by wire name
442 def get_tm_class(db: chipdb, wire: str):
443 assert wire in db.wire_delay, f"Unknown timing class for {wire}"
444 return db.wire_delay[wire]
445
446 # u-turn at the rim
447 uturnlut = {'N': 'S', 'S': 'N', 'E': 'W', 'W': 'E'}
448 def uturn(db: chipdb, x: int, y: int, wire: str):
449 m = re.match(r"([NESW])([128]\d)(\d)", wire)
450 if m:
451 direction, num, segment = m.groups()
452 # wires wrap around the edges
453 # assumes 0-based indexes
454 if y < 0:
455 y = -1 - y
456 direction = uturnlut[direction]
457 if x < 0:
458 x = -1 - x
459 direction = uturnlut[direction]
460 if y > db.rows - 1:
461 y = 2 * db.rows - 1 - y
462 direction = uturnlut[direction]
463 if x > db.cols - 1:
464 x = 2 * db.cols - 1 - x
465 direction = uturnlut[direction]
466 wire = f'{direction}{num}{segment}'
467 return (x, y, wire)
468
469 def create_nodes(chip: Chip, db: chipdb):
470 # : (x, y)
471 dirs = { 'N': (0, -1), 'S': (0, 1), 'W': (-1, 0), 'E': (1, 0) }
472 X = db.cols
473 Y = db.rows
474 global_nodes = {}
475 for y in range(Y):
476 for x in range(X):
477 nodes = []
478 tt = chip.tile_type_at(x, y)
479 extra_tile_data = tt.extra_data
480 # SN and EW
481 for i in [1, 2]:
482 nodes.append([NodeWire(x, y, f'SN{i}0'),
483 NodeWire(*uturn(db, x, y - 1, f'N1{i}1')),
484 NodeWire(*uturn(db, x, y + 1, f'S1{i}1'))])
485 nodes.append([NodeWire(x, y, f'EW{i}0'),
486 NodeWire(*uturn(db, x - 1, y, f'W1{i}1')),
487 NodeWire(*uturn(db, x + 1, y, f'E1{i}1'))])
488 for d, offs in dirs.items():
489 # 1-hop
490 for i in [0, 3]:
491 nodes.append([NodeWire(x, y, f'{d}1{i}0'),
492 NodeWire(*uturn(db, x + offs[0], y + offs[1], f'{d}1{i}1'))])
493 # 2-hop
494 for i in range(8):
495 nodes.append([NodeWire(x, y, f'{d}2{i}0'),
496 NodeWire(*uturn(db, x + offs[0], y + offs[1], f'{d}2{i}1')),
497 NodeWire(*uturn(db, x + offs[0] * 2, y + offs[1] * 2, f'{d}2{i}2'))])
498 # 4-hop
499 for i in range(4):
500 nodes.append([NodeWire(x, y, f'{d}8{i}0'),
501 NodeWire(*uturn(db, x + offs[0] * 4, y + offs[1] * 4, f'{d}8{i}4')),
502 NodeWire(*uturn(db, x + offs[0] * 8, y + offs[1] * 8, f'{d}8{i}8'))])
503 # I0 for MUX2_LUT8
504 if (x < X - 1 and extra_tile_data.tile_class == chip.strs.id('LOGIC')
505 and chip.tile_type_at(x + 1, y).extra_data.tile_class == chip.strs.id('LOGIC')):
506 nodes.append([NodeWire(x, y, 'OF30'),
507 NodeWire(x + 1, y, 'OF3')])
508
509 # ALU
510 if extra_tile_data.tile_class == chip.strs.id('LOGIC'):
511 # local carry chain
512 for i in range(5):
513 nodes.append([NodeWire(x, y, f'COUT{i}'),
514 NodeWire(x, y, f'CIN{i + 1}')]);
515 # global carry chain
516 if x > 1 and chip.tile_type_at(x - 1, y).extra_data.tile_class == chip.strs.id('LOGIC'):
517 nodes.append([NodeWire(x, y, f'CIN0'),
518 NodeWire(x - 1, y, f'COUT5')])
519
520 for node in nodes:
521 chip.add_node(node)
522
523 # add nodes from the apicula db
524 for node_name, node_hdr in db.nodes.items():
525 wire_type, node = node_hdr
526 if len(node) < 2:
527 continue
528 min_wire_name_len = 0
529 if node:
530 min_wire_name_len = len(next(iter(node))[2])
531 for y, x, wire in node:
532 if wire_type:
533 if not chip.tile_type_at(x, y).has_wire(wire):
534 chip.tile_type_at(x, y).create_wire(wire, wire_type)
535 else:
536 chip.tile_type_at(x, y).set_wire_type(wire, wire_type)
537 new_node = NodeWire(x, y, wire)
538 gl_nodes = global_nodes.setdefault(node_name, [])
539 if new_node not in gl_nodes:
540 if len(wire) < min_wire_name_len:
541 min_wire_name_len = len(wire)
542 gl_nodes.insert(0, new_node)
543 else:
544 gl_nodes.append(new_node)
545
546 for name, node in global_nodes.items():
547 chip.add_node(node)
548
549 def create_switch_matrix(tt: TileType, db: chipdb, x: int, y: int):
550 def get_wire_type(name):
551 if name in {'XD0', 'XD1', 'XD2', 'XD3', 'XD4', 'XD5',}:
552 return "X0"
553 if name in {"PCLK_DUMMY"}:
554 return "GLOBAL_CLK"
555 if name in {"DLLDLY_OUT"}:
556 return "DLLDLY_O"
557 if name in {'LT00', 'LT10', 'LT20', 'LT30', 'LT02', 'LT13'}:
558 return "LW_TAP"
559 return ""
560
561 for dst, srcs in db[y, x].pips.items():
562 if not tt.has_wire(dst):
563 tt.create_wire(dst, get_wire_type(dst))
564 for src in srcs.keys():
565 assert src in db.wire_delay, f"No timing info for {src} wire"
566 if not tt.has_wire(src):
567 if src in {"VSS", "VCC"}:
568 tt.create_wire(src, get_wire_type(src), const_value = src)
569 else:
570 tt.create_wire(src, get_wire_type(src))
571 tt.create_pip(src, dst, get_tm_class(db, src))
572
573 # clock wires
574 # always mark clock wires with location flag
575 for dst, srcs in db[y, x].clock_pips.items():
576 if not tt.has_wire(dst):
577 wire = tt.create_wire(dst, "GLOBAL_CLK")
578 if hasattr(db, "last_top_row") and y > db.last_top_row:
579 wire.flags |= WIRE_FLAG_BOTTOM_HALF
580 for src in srcs.keys():
581 if not tt.has_wire(src):
582 wire = tt.create_wire(src, "GLOBAL_CLK")
583 if hasattr(db, "last_top_row") and y > db.last_top_row:
584 wire.flags |= WIRE_FLAG_BOTTOM_HALF
585 src_tm_class = get_tm_class(db, src)
586 tt.create_pip(src, dst, src_tm_class)
587
588 def create_hclk_switch_matrix(tt: TileType, db: chipdb, x: int, y: int):
589 if (y, x) not in db.hclk_pips:
590 return
591 # hclk wires
592 for dst, srcs in db.hclk_pips[y, x].items():
593 if not tt.has_wire(dst):
594 tt.create_wire(dst, "HCLK")
595 for src in srcs.keys():
596 if not tt.has_wire(src):
597 tt.create_wire(src, "HCLK")
598 tt.create_pip(src, dst, get_tm_class(db, "X01")) # XXX
599
600 hclk_bel_zs = {
601 "CLKDIV2_HCLK0_SECT0": CLKDIV2_0_Z,
602 "CLKDIV2_HCLK0_SECT1": CLKDIV2_1_Z,
603 "CLKDIV2_HCLK1_SECT0": CLKDIV2_2_Z,
604 "CLKDIV2_HCLK1_SECT1": CLKDIV2_3_Z,
605 "CLKDIV_HCLK0_SECT0": CLKDIV_0_Z,
606 "CLKDIV_HCLK0_SECT1": CLKDIV_1_Z,
607 "CLKDIV_HCLK1_SECT0": CLKDIV_2_Z,
608 "CLKDIV_HCLK1_SECT1": CLKDIV_3_Z
609 }
610
611 for bel_name, bel_props in db[y, x].bels.items():
612 if (bel_name not in hclk_bel_zs):
613 continue
614 this_portmap = bel_props.portmap
615
616 if bel_name.startswith("CLKDIV2_"):
617 bel_type = "CLKDIV2"
618 elif bel_name.startswith("CLKDIV_"):
619 bel_type = "CLKDIV"
620 this_bel = tt.create_bel(bel_name, bel_type, hclk_bel_zs[bel_name])
621
622 if (bel_name in ["CLKDIV_HCLK0_SECT1", "CLKDIV_HCLK1_SECT1"]):
623 this_bel.flags |= BEL_FLAG_HIDDEN
624 if bel_type=="CLKDIV":
625 this_bel.flags |= BEL_FLAG_GLOBAL
626
627 known_pins = ["HCLKIN", "RESETN", "CLKOUT"]
628 if bel_type == "CLKDIV":
629 known_pins.append("CALIB")
630
631 for pin in this_portmap.keys():
632 assert pin in known_pins, f"Unknown pin {pin} for bel {this_bel}"
633 if pin in ["CALIB", "RESETN", "HCLKIN"]:
634 pin_direction = PinType.INPUT
635 elif pin in ["CLKOUT"]:
636 pin_direction = PinType.OUTPUT
637 wire_type = "HCLK_CTRL" if pin in ("CALIB", "RESETN") else "HCLK"
638 add_port_wire(tt, this_bel, this_portmap, pin, wire_type, pin_direction)
639
640
641 # map spine -> dqce bel
642 dqce_bels = {}
643 # map spine -> dcs bel
644 dcs_bels = {}
645
646 # map HCLKIN wire -> dhcen bel
647 dhcen_bels = {}
648
649 # map io bel -> dlldly bel
650 io_dlldly_bels = {}
651
652 def create_extra_funcs(tt: TileType, db: chipdb, x: int, y: int):
653 if (y, x) not in db.extra_func:
654 return
655 for func, desc in db.extra_func[(y, x)].items():
656 if func == 'osc':
657 osc_type = desc['type']
658 portmap = db[y, x].bels[osc_type].portmap
659 for port, wire in portmap.items():
660 if not tt.has_wire(wire):
661 tt.create_wire(wire, port)
662 bel = tt.create_bel(osc_type, osc_type, z = OSC_Z)
663 for port, wire in portmap.items():
664 if 'OUT' in port:
665 tt.add_bel_pin(bel, port, wire, PinType.OUTPUT)
666 else:
667 tt.add_bel_pin(bel, port, wire, PinType.INPUT)
668 elif func == 'gsr':
669 wire = desc['wire']
670 if not tt.has_wire(wire):
671 tt.create_wire(wire)
672 bel = tt.create_bel("GSR", "GSR", z = GSR_Z)
673 tt.add_bel_pin(bel, "GSRI", wire, PinType.INPUT)
674 elif func == 'bandgap':
675 wire = desc['wire']
676 if not tt.has_wire(wire):
677 tt.create_wire(wire)
678 bel = tt.create_bel("BANDGAP", "BANDGAP", z = BANDGAP_Z)
679 tt.add_bel_pin(bel, "BGEN", wire, PinType.INPUT)
680 elif func == 'dhcen':
681 for idx, dhcen in enumerate(desc):
682 wire = dhcen['ce']
683 if not tt.has_wire(wire):
684 tt.create_wire(wire)
685 bel_z = DHCEN_Z + idx
686 bel = tt.create_bel(f"DHCEN{idx}", "DHCEN", z = bel_z)
687 tt.add_bel_pin(bel, "CE", wire, PinType.INPUT)
688 pip_xy, pip_dst, pip_src, side = dhcen['pip']
689 dhcen_bels[pip_xy, pip_dst, pip_src] = (x, y, bel_z, side)
690 elif func == 'dlldly':
691 for idx, dlldly in desc.items():
692 bel_z = DLLDLY_Z + idx
693 bel = tt.create_bel(f"DLLDLY{idx}", "DLLDLY", z = bel_z)
694 for pin, wire in dlldly['in_wires'].items():
695 if not tt.has_wire(wire):
696 tt.create_wire(wire)
697 tt.add_bel_pin(bel, pin, wire, PinType.INPUT)
698 for pin, wire in dlldly['out_wires'].items():
699 if not tt.has_wire(wire):
700 tt.create_wire(wire)
701 tt.add_bel_pin(bel, pin, wire, PinType.OUTPUT)
702 io_dlldly_bels[f"{dlldly['io_loc']}/{dlldly['io_bel']}"] = f"X{x}Y{y}/DLLDLY{idx}"
703
704 elif func == 'dqce':
705 for idx in range(6):
706 bel_z = DQCE_Z + idx
707 bel = tt.create_bel(f"DQCE{idx}", "DQCE", bel_z)
708 wire = desc[idx]['clkin']
709 dqce_bels[wire] = (x, y, bel_z)
710 if not tt.has_wire(wire):
711 tt.create_wire(wire, "GLOBAL_CLK")
712 tt.add_bel_pin(bel, "CLKIN", wire, PinType.INPUT)
713 tt.add_bel_pin(bel, "CLKOUT", wire, PinType.OUTPUT)
714 wire = desc[idx]['ce']
715 if not tt.has_wire(wire):
716 tt.create_wire(wire)
717 tt.add_bel_pin(bel, "CE", wire, PinType.INPUT)
718 elif func == 'dcs':
719 for idx in range(2):
720 if idx not in desc:
721 continue
722 dcs_prefix = 'CLK'
723 if hasattr(db, "dcs_prefix"):
724 dcs_prefix = db.dcs_prefix
725 bel_z = DCS_Z + idx
726 bel = tt.create_bel(f"DCS{idx}", "DCS", bel_z)
727 wire = desc[idx]['clkout']
728 if not tt.has_wire(wire):
729 tt.create_wire(wire)
730 tt.add_bel_pin(bel, "CLKOUT", wire, PinType.OUTPUT)
731 clkout_wire = wire
732 for clk_idx, wire in enumerate(desc[idx]['clk']):
733 if not tt.has_wire(wire):
734 tt.create_wire(wire, "GLOBAL_CLK")
735 tt.add_bel_pin(bel, f"{dcs_prefix}{clk_idx}", wire, PinType.INPUT)
736 # This is a fake PIP that allows routing “through” this
737 # primitive from the CLK input to the CLKOUT output.
738 tt.create_pip(wire, clkout_wire)
739 dcs_bels[wire] = (x, y, bel_z)
740 for i, wire in enumerate(desc[idx]['clksel']):
741 if not tt.has_wire(wire):
742 tt.create_wire(wire)
743 tt.add_bel_pin(bel, f"CLKSEL{i}", wire, PinType.INPUT)
744 wire = desc[idx]['selforce']
745 if not tt.has_wire(wire):
746 tt.create_wire(wire)
747 tt.add_bel_pin(bel, "SELFORCE", wire, PinType.INPUT)
748 elif func == 'io16':
749 role = desc['role']
750 if role == 'MAIN':
751 y_off, x_off = desc['pair']
752 tt.extra_data.io16_x_off = x_off
753 tt.extra_data.io16_y_off = y_off
754
755 for io_type, z in {('IDES16', IDES16_Z), ('OSER16', OSER16_Z)}:
756 bel = tt.create_bel(io_type, io_type, z = z)
757 portmap = db[y, x].bels[io_type].portmap
758 for port, wire in portmap.items():
759 if port == 'FCLK': # XXX compatibility
760 wire = 'FCLKA'
761 if not tt.has_wire(wire):
762 if port in {'CLK', 'PCLK'}:
763 tt.create_wire(wire, "TILE_CLK")
764 else:
765 tt.create_wire(wire, "IOL_PORT")
766 if 'OUT' in port:
767 tt.add_bel_pin(bel, port, wire, PinType.OUTPUT)
768 else:
769 tt.add_bel_pin(bel, port, wire, PinType.INPUT)
770 elif func == 'i3c_capable':
771 tt.extra_data.tile_flags |= TILE_I3C_CAPABLE_IO
772 elif func == 'mipi_obuf':
773 bel = tt.create_bel('MIPI_OBUF', 'MIPI_OBUF', MIPIOBUF_Z)
774 elif func == 'mipi_ibuf':
775 bel = tt.create_bel('MIPI_IBUF', 'MIPI_IBUF', MIPIIBUF_Z)
776 wire = desc['HSREN']
777 if not tt.has_wire(wire):
778 tt.create_wire(wire)
779 tt.add_bel_pin(bel, 'HSREN', wire, PinType.INPUT)
780 wire = 'MIPIOL'
781 if not tt.has_wire(wire):
782 tt.create_wire(wire)
783 tt.add_bel_pin(bel, 'OL', wire, PinType.OUTPUT)
784 for i in range(2):
785 wire = f'MIPIEN{i}'
786 if not tt.has_wire(wire):
787 tt.create_wire(wire)
788 tt.add_bel_pin(bel, f'MIPIEN{i}', wire, PinType.INPUT)
789 elif func == 'buf':
790 for buf_type, wires in desc.items():
791 for i, wire in enumerate(wires):
792 if not tt.has_wire(wire):
793 tt.create_wire(wire, "TILE_CLK")
794 wire_out = f'{buf_type}{i}_O'
795 tt.create_wire(wire_out, "BUFG_O")
796 # XXX make Z from buf_type
797 bel = tt.create_bel(f'{buf_type}{i}', buf_type, z = BUFG_Z + i)
798 bel.flags = BEL_FLAG_GLOBAL
799 tt.add_bel_pin(bel, "I", wire, PinType.INPUT)
800 tt.add_bel_pin(bel, "O", wire_out, PinType.OUTPUT)
801 elif func == 'userflash':
802 bel = tt.create_bel("USERFLASH", desc['type'], USERFLASH_Z)
803 portmap = desc['ins']
804 for port, wire in portmap.items():
805 if not tt.has_wire(wire):
806 tt.create_wire(wire, "FLASH_IN")
807 tt.add_bel_pin(bel, port, wire, PinType.INPUT)
808 portmap = desc['outs']
809 for port, wire in portmap.items():
810 if not tt.has_wire(wire):
811 tt.create_wire(wire, "FLASH_OUT")
812 tt.add_bel_pin(bel, port, wire, PinType.OUTPUT)
813 elif func == 'emcu':
814 bel = tt.create_bel("EMCU", "EMCU", EMCU_Z)
815 portmap = desc['ins']
816 for port, wire in portmap.items():
817 if not tt.has_wire(wire):
818 tt.create_wire(wire, "EMCU_IN")
819 tt.add_bel_pin(bel, port, wire, PinType.INPUT)
820 portmap = desc['outs']
821 for port, wire in portmap.items():
822 if not tt.has_wire(wire):
823 tt.create_wire(wire, "EMCU_OUT")
824 tt.add_bel_pin(bel, port, wire, PinType.OUTPUT)
825 elif func == 'pincfg':
826 bel = tt.create_bel("PINCFG", "PINCFG", PINCFG_Z)
827 portmap = desc['ins']
828 for port, wire in portmap.items():
829 if not tt.has_wire(wire):
830 tt.create_wire(wire, "PINCFG_IN")
831 tt.add_bel_pin(bel, port, wire, PinType.INPUT)
832 elif func == 'pll':
833 pll = tt.create_bel("PLL", "PLLA", z = PLL_Z)
834 pll.flags = BEL_FLAG_GLOBAL
835 for pin, wire in desc['outputs'].items():
836 tt.create_wire(wire, "PLL_O")
837 tt.add_bel_pin(pll, pin, wire, PinType.OUTPUT)
838 for pin, wire in desc['inputs'].items():
839 tt.create_wire(wire, "PLL_I")
840 tt.add_bel_pin(pll, pin, wire, PinType.INPUT)
841 elif func == 'adc':
842 pll = tt.create_bel("ADC", "ADC", z = ADC_Z)
843 for pin, wire in desc['outputs'].items():
844 tt.create_wire(wire, "ADC_O")
845 tt.add_bel_pin(pll, pin, wire, PinType.OUTPUT)
846 for pin, wire in desc['inputs'].items():
847 if pin == 'CLK' or pin == 'MDRP_CLK':
848 tt.create_wire(wire, "TILE_CLK")
849 else:
850 tt.create_wire(wire, "ADC_I")
851 tt.add_bel_pin(pll, pin, wire, PinType.INPUT)
852 elif func == 'gnd_source':
853 # GND is the logic low level generator
854 tt.create_wire('VSS', 'GND', const_value = 'VSS')
855 gnd = tt.create_bel('GND', 'GND', z = GND_Z)
856 tt.add_bel_pin(gnd, "G", "VSS", PinType.OUTPUT)
857 elif func == 'vcc_source':
858 # VCC is the logic high level generator
859 tt.create_wire('VCC', 'VCC', const_value = 'VCC')
860 gnd = tt.create_bel('VCC', 'VCC', z = VCC_Z)
861 tt.add_bel_pin(gnd, "V", "VCC", PinType.OUTPUT)
862
863
864 def set_wire_flags(tt: TileType, tdesc: TypeDesc):
865 if tdesc.extra_func and 'clock_gates' in tdesc.extra_func:
866 for wire_name in tdesc.extra_func['clock_gates']:
867 wname_id = tt.strs.id(wire_name)
868 for wire_data in tt.wires:
869 if wire_data.name == wname_id:
870 wire_data.flags |= WIRE_FLAG_CLOCK_GATE
871
872 def create_tiletype(create_func, chip: Chip, db: chipdb, x: int, y: int, ttyp: int):
873 has_extra_func = (y, x) in db.extra_func
874
875 # (found, TypeDesc)
876 def find_or_make_dup():
877 for d in created_tiletypes[ttyp].dups:
878 if has_extra_func and d.extra_func == db.extra_func[(y, x)]:
879 return (True, d)
880 elif not has_extra_func and not d.extra_func:
881 return (True, d)
882 sfx = len(created_tiletypes[ttyp].dups) + 1
883 if has_extra_func:
884 tdesc = TypeDesc(extra_func = db.extra_func[(y, x)], sfx = sfx, dups = [])
885 else:
886 tdesc = TypeDesc(sfx = sfx, dups = [])
887 created_tiletypes[ttyp].dups.append(tdesc)
888 return (False, tdesc)
889
890 old_type = False
891 if ttyp not in created_tiletypes:
892 # new type
893 if has_extra_func:
894 tdesc = TypeDesc(extra_func = db.extra_func[(y, x)], dups = [])
895 else:
896 tdesc = TypeDesc(dups = [])
897 created_tiletypes.update({ttyp: tdesc})
898 else:
899 # find similar
900 if has_extra_func:
901 if created_tiletypes[ttyp].extra_func == db.extra_func[(y, x)]:
902 tdesc = created_tiletypes[ttyp]
903 old_type = True
904 else:
905 old_type, tdesc = find_or_make_dup()
906 elif not created_tiletypes[ttyp].extra_func:
907 tdesc = created_tiletypes[ttyp]
908 old_type = True
909 else:
910 old_type, tdesc = find_or_make_dup()
911
912 if old_type:
913 chip.set_tile_type(x, y, tdesc.tiletype)
914 return
915
916 tt = create_func(chip, db, x, y, ttyp, tdesc)
917
918 create_extra_funcs(tt, db, x, y)
919 create_hclk_switch_matrix(tt, db, x, y)
920 create_switch_matrix(tt, db, x, y)
921 set_wire_flags(tt, tdesc)
922 chip.set_tile_type(x, y, tdesc.tiletype)
923
924 def add_port_wire(tt, bel, portmap, name, wire_type, port_type, pin_name = None):
925 wire = portmap[name]
926 if not tt.has_wire(wire):
927 if name.startswith('CLK'):
928 tt.create_wire(wire, "TILE_CLK")
929 else:
930 tt.create_wire(wire, wire_type)
931 if pin_name:
932 tt.add_bel_pin(bel, pin_name, wire, port_type)
933 else:
934 tt.add_bel_pin(bel, name, wire, port_type)
935
936 def create_null_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
937 typename = "NULL"
938 tiletype = f"{typename}_{ttyp}"
939 if tdesc.sfx != 0:
940 tiletype += f"_{tdesc.sfx}"
941 tt = chip.create_tile_type(tiletype)
942 tt.extra_data = TileExtraData(chip.strs.id(typename))
943 tdesc.tiletype = tiletype
944 return tt
945
946 # IO
947 def create_io_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
948 typename = "IO"
949 tiletype = f"{typename}_{ttyp}"
950 if tdesc.sfx != 0:
951 tiletype += f"_{tdesc.sfx}"
952 tt = chip.create_tile_type(tiletype)
953 tt.extra_data = TileExtraData(chip.strs.id(typename))
954
955 simple_io = y in db.simplio_rows and chip.name in {'GW1N-1', 'GW1NZ-1', 'GW1N-4'}
956 if simple_io:
957 rng = 10
958 else:
959 rng = 2
960 for i in range(rng):
961 name = 'IOB' + 'ABCDEFGHIJ'[i]
962 # XXX some IOBs excluded from generic chipdb for some reason
963 if name not in db[y, x].bels:
964 continue
965 # wires
966 portmap = db[y, x].bels[name].portmap
967 tt.create_wire(portmap['I'], "IO_I")
968 tt.create_wire(portmap['O'], "IO_O")
969 tt.create_wire(portmap['OE'], "IO_OE")
970 # bels
971 io = tt.create_bel(name, "IOB", z = IOBA_Z + i)
972 if simple_io and chip.name in {'GW1N-1'}:
973 io.flags |= BEL_FLAG_SIMPLE_IO
974 tt.add_bel_pin(io, "I", portmap['I'], PinType.INPUT)
975 tt.add_bel_pin(io, "OEN", portmap['OE'], PinType.INPUT)
976 tt.add_bel_pin(io, "O", portmap['O'], PinType.OUTPUT)
977 if 'ADCEN' in portmap:
978 tt.create_wire(portmap['ADCEN'], "IO_ADCEN")
979 tt.add_bel_pin(io, "ADCEN", portmap['ADCEN'], PinType.INPUT)
980
981 # bottom io
982 if 'BOTTOM_IO_PORT_A' in portmap and portmap['BOTTOM_IO_PORT_A']:
983 if not tt.has_wire(portmap['BOTTOM_IO_PORT_A']):
984 tt.create_wire(portmap['BOTTOM_IO_PORT_A'], "IO_I")
985 tt.create_wire(portmap['BOTTOM_IO_PORT_B'], "IO_I")
986 tt.add_bel_pin(io, "BOTTOM_IO_PORT_A", portmap['BOTTOM_IO_PORT_A'], PinType.INPUT)
987 tt.add_bel_pin(io, "BOTTOM_IO_PORT_B", portmap['BOTTOM_IO_PORT_B'], PinType.INPUT)
988 # create IOLOGIC bels if any
989 for idx, name in {(IOLOGICA_Z, 'IOLOGICA'), (IOLOGICA_Z + 1, 'IOLOGICB')}:
990 if name not in db[y, x].bels:
991 continue
992 for off, io_type in {(0, 'O'), (2, 'I')}:
993 iol = tt.create_bel(f"{name}{io_type}", f"IOLOGIC{io_type}", z = idx + off)
994 for port, wire in db[y, x].bels[name].portmap.items():
995 if port == 'FCLK': # XXX compatibility
996 wire = f'FCLK{name[-1]}'
997 if not tt.has_wire(wire):
998 if port in {'CLK', 'PCLK', 'MCLK'}:
999 tt.create_wire(wire, "TILE_CLK")
1000 else:
1001 tt.create_wire(wire, "IOL_PORT")
1002 if port in {'Q', 'Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'DF', 'LAG', 'LEAD'}:
1003 tt.add_bel_pin(iol, port, wire, PinType.OUTPUT)
1004 else:
1005 tt.add_bel_pin(iol, port, wire, PinType.INPUT)
1006 tdesc.tiletype = tiletype
1007 return tt
1008
1009 # logic: luts, dffs, alu etc
1010 def create_logic_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
1011 typename = "LOGIC"
1012 tiletype = f"{typename}_{ttyp}"
1013 if tdesc.sfx != 0:
1014 tiletype += f"_{tdesc.sfx}"
1015 tt = chip.create_tile_type(tiletype)
1016 tt.extra_data = TileExtraData(chip.strs.id(typename))
1017
1018 lut_inputs = ['A', 'B', 'C', 'D']
1019 # setup LUT wires
1020 for i in range(8):
1021 for inp_name in lut_inputs:
1022 tt.create_wire(f"{inp_name}{i}", "LUT_IN")
1023 tt.create_wire(f"F{i}", "LUT_OUT")
1024 # experimental. the wire is false - it is assumed that DFF is always
1025 # connected to the LUT's output F{i}, but we can place primitives
1026 # arbitrarily and create a pass-through LUT afterwards.
1027 # just out of curiosity
1028 tt.create_wire(f"XD{i}", "FF_INPUT")
1029 tt.create_wire(f"Q{i}", "FF_OUT")
1030 # setup DFF wires
1031 for j in range(3):
1032 tt.create_wire(f"CLK{j}", "TILE_CLK")
1033 tt.create_wire(f"LSR{j}", "TILE_LSR")
1034 tt.create_wire(f"CE{j}", "TILE_CE")
1035 # setup MUX2 wires
1036 for j in range(8):
1037 tt.create_wire(f"OF{j}", "MUX_OUT")
1038 tt.create_wire(f"SEL{j}", "MUX_SEL")
1039 tt.create_wire("OF30", "MUX_OUT")
1040 # setup ALU wires
1041 for j in range(6):
1042 tt.create_wire(f"CIN{j}", "ALU_CIN")
1043 tt.create_wire(f"COUT{j}", "ALU_COUT")
1044
1045 # create logic cells
1046 for i in range(8):
1047 # LUT
1048 lut = tt.create_bel(f"LUT{i}", "LUT4", z = (i * 2 + 0))
1049 for j, inp_name in enumerate(lut_inputs):
1050 tt.add_bel_pin(lut, f"I{j}", f"{inp_name}{i}", PinType.INPUT)
1051 tt.add_bel_pin(lut, "F", f"F{i}", PinType.OUTPUT)
1052 if i < 6 or "HAS_DFF67" in db.chip_flags:
1053 tt.create_pip(f"F{i}", f"XD{i}", get_tm_class(db, f"F{i}"))
1054 # also experimental input for FF using SEL wire - this theory will
1055 # allow to place unrelated LUT and FF next to each other
1056 # don't create for now
1057 #tt.create_pip(f"SEL{i}", f"XD{i}", get_tm_class(db, f"SEL{i}"))
1058
1059 # FF
1060 ff = tt.create_bel(f"DFF{i}", "DFF", z =(i * 2 + 1))
1061 tt.add_bel_pin(ff, "D", f"XD{i}", PinType.INPUT)
1062 tt.add_bel_pin(ff, "Q", f"Q{i}", PinType.OUTPUT)
1063 if i < 6:
1064 tt.add_bel_pin(ff, "CLK", f"CLK{i // 2}", PinType.INPUT)
1065 tt.add_bel_pin(ff, "SET", f"LSR{i // 2}", PinType.INPUT)
1066 tt.add_bel_pin(ff, "RESET", f"LSR{i // 2}", PinType.INPUT)
1067 tt.add_bel_pin(ff, "PRESET", f"LSR{i // 2}", PinType.INPUT)
1068 tt.add_bel_pin(ff, "CLEAR", f"LSR{i // 2}", PinType.INPUT)
1069 tt.add_bel_pin(ff, "CE", f"CE{i // 2}", PinType.INPUT)
1070 else:
1071 tt.add_bel_pin(ff, "CLK", "CLK2", PinType.INPUT)
1072 tt.add_bel_pin(ff, "SET", "LSR2", PinType.INPUT)
1073 tt.add_bel_pin(ff, "RESET", "LSR2", PinType.INPUT)
1074 tt.add_bel_pin(ff, "PRESET", "LSR2", PinType.INPUT)
1075 tt.add_bel_pin(ff, "CLEAR", "LSR2", PinType.INPUT)
1076 tt.add_bel_pin(ff, "CE", "CE2", PinType.INPUT)
1077
1078 if i < 6:
1079 # ALU
1080 ff = tt.create_bel(f"ALU{i}", "ALU", z = i + ALU0_Z)
1081 tt.add_bel_pin(ff, "SUM", f"F{i}", PinType.OUTPUT)
1082 tt.add_bel_pin(ff, "COUT", f"COUT{i}", PinType.OUTPUT)
1083 tt.add_bel_pin(ff, "CIN", f"CIN{i}", PinType.INPUT)
1084 # pinout for the ADDSUB ALU mode
1085 tt.add_bel_pin(ff, "I0", f"A{i}", PinType.INPUT)
1086 tt.add_bel_pin(ff, "I1", f"B{i}", PinType.INPUT)
1087 tt.add_bel_pin(ff, "I2", f"C{i}", PinType.INPUT)
1088 tt.add_bel_pin(ff, "I3", f"D{i}", PinType.INPUT)
1089
1090 # wide luts
1091 for i in range(4):
1092 ff = tt.create_bel(f"MUX{i * 2}", "MUX2_LUT5", z = MUX20_Z + i * 4)
1093 tt.add_bel_pin(ff, "I0", f"F{i * 2}", PinType.INPUT)
1094 tt.add_bel_pin(ff, "I1", f"F{i * 2 + 1}", PinType.INPUT)
1095 tt.add_bel_pin(ff, "O", f"OF{i * 2}", PinType.OUTPUT)
1096 tt.add_bel_pin(ff, "S0", f"SEL{i * 2}", PinType.INPUT)
1097 for i in range(2):
1098 ff = tt.create_bel(f"MUX{i * 4 + 1}", "MUX2_LUT6", z = MUX21_Z + i * 8)
1099 tt.add_bel_pin(ff, "I0", f"OF{i * 4 + 2}", PinType.INPUT)
1100 tt.add_bel_pin(ff, "I1", f"OF{i * 4}", PinType.INPUT)
1101 tt.add_bel_pin(ff, "O", f"OF{i * 4 + 1}", PinType.OUTPUT)
1102 tt.add_bel_pin(ff, "S0", f"SEL{i * 4 + 1}", PinType.INPUT)
1103 ff = tt.create_bel(f"MUX3", "MUX2_LUT7", z = MUX23_Z)
1104 tt.add_bel_pin(ff, "I0", f"OF5", PinType.INPUT)
1105 tt.add_bel_pin(ff, "I1", f"OF1", PinType.INPUT)
1106 tt.add_bel_pin(ff, "O", f"OF3", PinType.OUTPUT)
1107 tt.add_bel_pin(ff, "S0", f"SEL3", PinType.INPUT)
1108 ff = tt.create_bel(f"MUX7", "MUX2_LUT8", z = MUX27_Z)
1109 tt.add_bel_pin(ff, "I0", f"OF30", PinType.INPUT)
1110 tt.add_bel_pin(ff, "I1", f"OF3", PinType.INPUT)
1111 tt.add_bel_pin(ff, "O", f"OF7", PinType.OUTPUT)
1112 tt.add_bel_pin(ff, "S0", f"SEL7", PinType.INPUT)
1113
1114 tdesc.tiletype = tiletype
1115 return tt
1116
1117 def create_ssram_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
1118 # SSRAM is LUT based, so it's logic-like
1119 tt = create_logic_tiletype(chip, db, x, y, ttyp, tdesc)
1120
1121 lut_inputs = ['A', 'B', 'C', 'D']
1122 ff = tt.create_bel(f"RAM16SDP4", "RAM16SDP4", z = RAMW_Z)
1123 for i in range(4):
1124 tt.add_bel_pin(ff, f"DI[{i}]", f"{lut_inputs[i]}5", PinType.INPUT)
1125 tt.add_bel_pin(ff, f"WAD[{i}]", f"{lut_inputs[i]}4", PinType.INPUT)
1126 # RAD[0] is assumed to be connected to A3, A2, A1 and A0. But
1127 # for now we connect it only to A0, the others will be connected
1128 # directly during packing. RAD[1...3] - similarly.
1129 tt.add_bel_pin(ff, f"RAD[{i}]", f"{lut_inputs[i]}0", PinType.INPUT)
1130 tt.add_bel_pin(ff, f"DO[{i}]", f"F{i}", PinType.OUTPUT)
1131
1132 tt.add_bel_pin(ff, "CLK", "CLK2", PinType.INPUT)
1133 tt.add_bel_pin(ff, "CE", "CE2", PinType.INPUT)
1134 tt.add_bel_pin(ff, "WRE", "LSR2", PinType.INPUT)
1135 return tt
1136
1137 # BSRAM
1138 _bsram_inputs = {'CLK', 'OCE', 'CE', 'RESET', 'WRE'}
1139 def create_bsram_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
1140 typename = "BSRAM"
1141 tiletype = f"{typename}_{ttyp}"
1142 if tdesc.sfx != 0:
1143 tiletype += f"_{tdesc.sfx}"
1144 tt = chip.create_tile_type(tiletype)
1145 tt.extra_data = TileExtraData(chip.strs.id(typename))
1146
1147 portmap = db[y, x].bels['BSRAM'].portmap
1148 bsram = tt.create_bel("BSRAM", "BSRAM", z = BSRAM_Z)
1149
1150
1151 for sfx in {'', 'A', 'B'}:
1152 for inp in _bsram_inputs:
1153 add_port_wire(tt, bsram, portmap, f"{inp}{sfx}", "BSRAM_I", PinType.INPUT)
1154 for idx in range(3):
1155 add_port_wire(tt, bsram, portmap, f"BLKSEL{sfx}{idx}", "BSRAM_I", PinType.INPUT)
1156 for idx in range(14):
1157 add_port_wire(tt, bsram, portmap, f"AD{sfx}{idx}", "BSRAM_I", PinType.INPUT)
1158 for idx in range(18):
1159 add_port_wire(tt, bsram, portmap, f"DI{sfx}{idx}", "BSRAM_I", PinType.INPUT)
1160 add_port_wire(tt, bsram, portmap, f"DO{sfx}{idx}", "BSRAM_O", PinType.OUTPUT)
1161 if not sfx:
1162 for idx in range(18, 36):
1163 add_port_wire(tt, bsram, portmap, f"DI{idx}", "BSRAM_I", PinType.INPUT)
1164 add_port_wire(tt, bsram, portmap, f"DO{idx}", "BSRAM_O", PinType.OUTPUT)
1165
1166 tdesc.tiletype = tiletype
1167 return tt
1168
1169
1170 # GW5A series has different DSP
1171 def create_dsp_5a_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
1172 typename = "DSP"
1173 tiletype = f"{typename}_{ttyp}"
1174 if tdesc.sfx != 0:
1175 tiletype += f"_{tdesc.sfx}"
1176 tt = chip.create_tile_type(tiletype)
1177 tt.extra_data = TileExtraData(chip.strs.id(typename))
1178
1179 # create big DSP
1180 belname = f'DSP'
1181 dsp = tt.create_bel(belname, "DSP", DSP_Z)
1182 dsp.flags = BEL_FLAG_HIDDEN
1183
1184 # create DSP macro
1185 belname = 'DSP0'
1186 dsp = tt.create_bel(belname, "DSP", DSP_0_Z)
1187 dsp.flags = BEL_FLAG_HIDDEN
1188
1189 # create multipliers
1190 for idx in range(2):
1191 belname = f'MULT12X120{idx}'
1192 portmap = db[y, x].bels[belname].portmap
1193 dsp = tt.create_bel(belname, "MULT12X12", eval(f'MULT12X12_{idx}_Z'))
1194
1195 for sfx in {'A', 'B'}:
1196 for inp in range(12):
1197 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1198 for inp in range(2):
1199 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1200 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1201 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1202 for outp in range(24):
1203 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1204
1205 # create MultAlu27x18
1206 belname = f'MULTALU27X1800'
1207 portmap = db[y, x].bels[belname].portmap
1208 dsp = tt.create_bel(belname, "MULTALU27X18", MULTALU27X18_Z)
1209
1210 for sfx, qnt in {('A', 27) , ('B', 18), ('C', 48), ('D', 26)}:
1211 for inp in range(qnt):
1212 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1213 for inp in range(2):
1214 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1215 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1216 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1217 add_port_wire(tt, dsp, portmap, f"ADDSUB{inp}", "DSP_I", PinType.INPUT)
1218 add_port_wire(tt, dsp, portmap, f"ACCSEL{inp}", "DSP_I", PinType.INPUT)
1219 add_port_wire(tt, dsp, portmap, "CASISEL", "DSP_I", PinType.INPUT)
1220 add_port_wire(tt, dsp, portmap, "ASEL", "DSP_I", PinType.INPUT)
1221 add_port_wire(tt, dsp, portmap, "CSEL", "DSP_I", PinType.INPUT)
1222 add_port_wire(tt, dsp, portmap, "PSEL", "DSP_I", PinType.INPUT)
1223 add_port_wire(tt, dsp, portmap, "PADDSUB", "DSP_I", PinType.INPUT)
1224 for outp in range(48):
1225 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1226
1227 # create MultAddAlu12x12
1228 belname = f'MULTADDALU12X1200'
1229 portmap = db[y, x].bels[belname].portmap
1230 dsp = tt.create_bel(belname, "MULTADDALU12X12", MULTADDALU12X12_Z)
1231
1232 for sfx in {'A', 'B'}:
1233 for mult in range(2):
1234 for inp in range(12):
1235 add_port_wire(tt, dsp, portmap, f"{sfx}{mult}{inp}", "DSP_I", PinType.INPUT)
1236 for inp in range(2):
1237 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1238 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1239 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1240 add_port_wire(tt, dsp, portmap, f"ADDSUB{inp}", "DSP_I", PinType.INPUT)
1241 add_port_wire(tt, dsp, portmap, f"ACCSEL{inp}", "DSP_I", PinType.INPUT)
1242 add_port_wire(tt, dsp, portmap, "CASISEL", "DSP_I", PinType.INPUT)
1243 for outp in range(48):
1244 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1245
1246 tdesc.tiletype = tiletype
1247 return tt
1248
1249 # DSP
1250 _mult_inputs = {'ASEL', 'BSEL', 'ASIGN', 'BSIGN'}
1251 def create_dsp_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
1252 typename = "DSP"
1253 tiletype = f"{typename}_{ttyp}"
1254 if tdesc.sfx != 0:
1255 tiletype += f"_{tdesc.sfx}"
1256 tt = chip.create_tile_type(tiletype)
1257 tt.extra_data = TileExtraData(chip.strs.id(typename))
1258
1259 # create big DSP
1260 belname = f'DSP'
1261 dsp = tt.create_bel(belname, "DSP", DSP_Z)
1262 dsp.flags = BEL_FLAG_HIDDEN
1263
1264 # create DSP macros
1265 for idx in range(2):
1266 belname = f'DSP{idx}'
1267 portmap = db[y, x].bels[belname].portmap
1268 dsp = tt.create_bel(belname, "DSP", eval(f'DSP_{idx}_Z'))
1269 dsp.flags = BEL_FLAG_HIDDEN
1270
1271 # create pre-adders
1272 for mac, idx in [(mac, idx) for mac in range(2) for idx in range(4)]:
1273 belname = f'PADD9{mac}{idx}'
1274 portmap = db[y, x].bels[belname].portmap
1275 dsp = tt.create_bel(belname, "PADD9", eval(f'PADD9_{mac}_{idx}_Z'))
1276
1277 add_port_wire(tt, dsp, portmap, "ADDSUB", "DSP_I", PinType.INPUT)
1278 for sfx in {'A', 'B'}:
1279 for inp in range(9):
1280 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1281 for inp in range(9):
1282 add_port_wire(tt, dsp, portmap, f"C{inp}", "DSP_I", PinType.INPUT)
1283 for inp in range(4):
1284 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1285 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1286 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1287 add_port_wire(tt, dsp, portmap, "ASEL", "DSP_I", PinType.INPUT)
1288 for outp in range(9):
1289 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1290
1291 for mac, idx in [(mac, idx) for mac in range(2) for idx in range(2)]:
1292 belname = f'PADD18{mac}{idx}'
1293 portmap = db[y, x].bels[belname].portmap
1294 dsp = tt.create_bel(belname, "PADD18", eval(f'PADD18_{mac}_{idx}_Z'))
1295
1296 add_port_wire(tt, dsp, portmap, "ADDSUB", "DSP_I", PinType.INPUT)
1297 for sfx in {'A', 'B'}:
1298 for inp in range(18):
1299 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1300 for inp in range(18):
1301 add_port_wire(tt, dsp, portmap, f"C{inp}", "DSP_I", PinType.INPUT)
1302 for inp in range(4):
1303 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1304 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1305 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1306 add_port_wire(tt, dsp, portmap, "ASEL", "DSP_I", PinType.INPUT)
1307 for outp in range(18):
1308 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1309
1310 # create multipliers
1311 # mult 9x9
1312 for mac, idx in [(mac, idx) for mac in range(2) for idx in range(4)]:
1313 belname = f'MULT9X9{mac}{idx}'
1314 portmap = db[y, x].bels[belname].portmap
1315 dsp = tt.create_bel(belname, "MULT9X9", eval(f'MULT9X9_{mac}_{idx}_Z'))
1316
1317 for sfx in {'A', 'B'}:
1318 for inp in range(9):
1319 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1320 for inp in _mult_inputs:
1321 add_port_wire(tt, dsp, portmap, inp, "DSP_I", PinType.INPUT)
1322 for inp in range(4):
1323 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1324 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1325 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1326 for outp in range(18):
1327 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1328
1329 # mult 18x18
1330 for mac, idx in [(mac, idx) for mac in range(2) for idx in range(2)]:
1331 belname = f'MULT18X18{mac}{idx}'
1332 portmap = db[y, x].bels[belname].portmap
1333 dsp = tt.create_bel(belname, "MULT18X18", eval(f'MULT18X18_{mac}_{idx}_Z'))
1334
1335 for sfx in {'A', 'B'}:
1336 for inp in range(18):
1337 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1338 for inp in _mult_inputs:
1339 add_port_wire(tt, dsp, portmap, inp, "DSP_I", PinType.INPUT)
1340 for inp in range(4):
1341 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1342 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1343 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1344 for outp in range(36):
1345 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1346
1347 # mult 36x36
1348 belname = 'MULT36X36'
1349 portmap = db[y, x].bels[belname].portmap
1350 dsp = tt.create_bel(belname, "MULT36X36", MULT36X36_Z)
1351
1352 # LSB 18x18 multipliers sign ports must be zero
1353 add_port_wire(tt, dsp, db[y, x].bels['MULT18X1800'].portmap, 'ASIGN', "DSP_I", PinType.INPUT, 'ZERO_ASIGN0')
1354 add_port_wire(tt, dsp, db[y, x].bels['MULT18X1800'].portmap, 'BSIGN', "DSP_I", PinType.INPUT, 'ZERO_BSIGN0')
1355 add_port_wire(tt, dsp, db[y, x].bels['MULT18X1801'].portmap, 'BSIGN', "DSP_I", PinType.INPUT, 'ZERO_BSIGN1')
1356 add_port_wire(tt, dsp, db[y, x].bels['MULT18X1810'].portmap, 'ASIGN', "DSP_I", PinType.INPUT, 'ZERO_ASIGN1')
1357 for i in range(2):
1358 for sfx in {'A', 'B'}:
1359 for inp in range(36):
1360 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}{i}", "DSP_I", PinType.INPUT)
1361 for inp in {'ASIGN', 'BSIGN'}:
1362 add_port_wire(tt, dsp, portmap, f"{inp}{i}", "DSP_I", PinType.INPUT)
1363 for inp in range(4):
1364 add_port_wire(tt, dsp, portmap, f"CE{inp}{i}", "DSP_I", PinType.INPUT)
1365 add_port_wire(tt, dsp, portmap, f"CLK{inp}{i}", "DSP_I", PinType.INPUT)
1366 add_port_wire(tt, dsp, portmap, f"RESET{inp}{i}", "DSP_I", PinType.INPUT)
1367 for outp in range(72):
1368 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1369
1370 # create alus
1371 for mac in range(2):
1372 belname = f'ALU54D{mac}'
1373 portmap = db[y, x].bels[belname].portmap
1374 dsp = tt.create_bel(belname, "ALU54D", eval(f'ALU54D_{mac}_Z'))
1375
1376 for sfx in {'A', 'B'}:
1377 for inp in range(54):
1378 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1379 for inp in {'ASIGN', 'BSIGN'}:
1380 add_port_wire(tt, dsp, portmap, inp, "DSP_I", PinType.INPUT)
1381 for inp in range(4):
1382 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1383 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1384 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1385 if inp < 2:
1386 add_port_wire(tt, dsp, portmap, f"ACCLOAD{inp}", "DSP_I", PinType.INPUT)
1387 for outp in range(54):
1388 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1389
1390 # create multalus
1391 # MULTALU18X18
1392 for mac in range(2):
1393 belname = f'MULTALU18X18{mac}'
1394 portmap = db[y, x].bels[belname].portmap
1395 dsp = tt.create_bel(belname, "MULTALU18X18", eval(f'MULTALU18X18_{mac}_Z'))
1396
1397 for i in range(2):
1398 for sfx in {'ASIGN', 'BSIGN'}:
1399 add_port_wire(tt, dsp, portmap, f"{sfx}{i}", "DSP_I", PinType.INPUT)
1400 for sfx in {'A', 'B'}:
1401 for inp in range(18):
1402 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}{i}", "DSP_I", PinType.INPUT)
1403 for sfx in {'C', 'D'}:
1404 for inp in range(54):
1405 add_port_wire(tt, dsp, portmap, f"{sfx}{inp}", "DSP_I", PinType.INPUT)
1406 add_port_wire(tt, dsp, portmap, "DSIGN", "DSP_I", PinType.INPUT)
1407 for inp in range(4):
1408 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1409 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1410 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1411 if inp < 2:
1412 add_port_wire(tt, dsp, portmap, f"ACCLOAD{inp}", "DSP_I", PinType.INPUT)
1413 for outp in range(54):
1414 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1415
1416 # MULTALU36X18
1417 for mac in range(2):
1418 belname = f'MULTALU36X18{mac}'
1419 portmap = db[y, x].bels[belname].portmap
1420 dsp = tt.create_bel(belname, "MULTALU36X18", eval(f'MULTALU36X18_{mac}_Z'))
1421
1422 for i in range(2):
1423 for sfx in {'ASIGN', 'BSIGN'}:
1424 add_port_wire(tt, dsp, portmap, f"{sfx}{i}", "DSP_I", PinType.INPUT)
1425 for inp in range(18):
1426 add_port_wire(tt, dsp, portmap, f"A{inp}{i}", "DSP_I", PinType.INPUT)
1427 for inp in range(7):
1428 add_port_wire(tt, dsp, portmap, f"ALUSEL{inp}", "DSP_I", PinType.INPUT)
1429 for inp in range(36):
1430 add_port_wire(tt, dsp, portmap, f"B{inp}", "DSP_I", PinType.INPUT)
1431 for inp in range(54):
1432 add_port_wire(tt, dsp, portmap, f"C{inp}", "DSP_I", PinType.INPUT)
1433 for inp in range(4):
1434 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1435 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1436 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1437 for outp in range(54):
1438 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1439
1440 # MULTADDALU18X18
1441 for mac in range(2):
1442 belname = f'MULTADDALU18X18{mac}'
1443 portmap = db[y, x].bels[belname].portmap
1444 dsp = tt.create_bel(belname, "MULTADDALU18X18", eval(f'MULTADDALU18X18_{mac}_Z'))
1445
1446 for i in range(2):
1447 for sfx in {'ASIGN', 'BSIGN', 'ASEL', 'BSEL'}:
1448 add_port_wire(tt, dsp, portmap, f"{sfx}{i}", "DSP_I", PinType.INPUT)
1449 for inp in range(18):
1450 add_port_wire(tt, dsp, portmap, f"A{inp}{i}", "DSP_I", PinType.INPUT)
1451 add_port_wire(tt, dsp, portmap, f"B{inp}{i}", "DSP_I", PinType.INPUT)
1452 for inp in range(7):
1453 add_port_wire(tt, dsp, portmap, f"ALUSEL{inp}", "DSP_I", PinType.INPUT)
1454 for inp in range(54):
1455 add_port_wire(tt, dsp, portmap, f"C{inp}", "DSP_I", PinType.INPUT)
1456 for inp in range(4):
1457 add_port_wire(tt, dsp, portmap, f"CE{inp}", "DSP_I", PinType.INPUT)
1458 add_port_wire(tt, dsp, portmap, f"CLK{inp}", "DSP_I", PinType.INPUT)
1459 add_port_wire(tt, dsp, portmap, f"RESET{inp}", "DSP_I", PinType.INPUT)
1460 for outp in range(54):
1461 add_port_wire(tt, dsp, portmap, f"DOUT{outp}", "DSP_O", PinType.OUTPUT)
1462
1463 tdesc.tiletype = tiletype
1464 return tt
1465
1466 # PLL main tile
1467 def create_pll_tiletype(chip: Chip, db: chipdb, x: int, y: int, ttyp: int, tdesc: TypeDesc):
1468 typename = "PLL"
1469 tiletype = f"{typename}_{ttyp}"
1470 if tdesc.sfx != 0:
1471 tiletype += f"_{tdesc.sfx}"
1472
1473 # disabled PLLs
1474 if tdesc.extra_func and 'disabled' in tdesc.extra_func and 'PLL' in tdesc.extra_func['disabled']:
1475 tiletype += '_disabled'
1476 tt = chip.create_tile_type(tiletype)
1477 tt.extra_data = TileExtraData(chip.strs.id(typename))
1478 tdesc.tiletype = tiletype
1479 return tt
1480 tt = chip.create_tile_type(tiletype)
1481 tt.extra_data = TileExtraData(chip.strs.id(typename))
1482
1483 # wires
1484 pll_outputs = {'CLKOUT', 'LOCK', 'CLKOUTP', 'CLKOUTD', 'CLKOUTD3'}
1485 if chip.name == 'GW1NS-4':
1486 pll_name = 'PLLVR'
1487 bel_type = 'PLLVR'
1488 else:
1489 pll_name = 'RPLLA'
1490 bel_type = 'rPLL'
1491 portmap = db[y, x].bels[pll_name].portmap
1492 pll = tt.create_bel("PLL", bel_type, z = PLL_Z)
1493 pll.flags = BEL_FLAG_GLOBAL
1494 for pin, wire in portmap.items():
1495 if pin in pll_outputs:
1496 tt.create_wire(wire, "PLL_O")
1497 tt.add_bel_pin(pll, pin, wire, PinType.OUTPUT)
1498 else:
1499 tt.create_wire(wire, "PLL_I")
1500 tt.add_bel_pin(pll, pin, wire, PinType.INPUT)
1501 tdesc.tiletype = tiletype
1502 return tt
1503
1504 # add Pll's bel to the pad
1505 def add_pll(chip: Chip, db: chipdb, pad: PadInfo, ioloc: str):
1506 try:
1507 if ioloc in db.pad_pll:
1508 row, col, ttyp, bel_name = db.pad_pll[ioloc]
1509 pad.extra_data = PadExtraData(chip.strs.id(f'X{col}Y{row}'), chip.strs.id(bel_name), chip.strs.id(ttyp))
1510 except:
1511 return
1512
1513 # pinouts, packages...
1514 _tbrlre = re.compile(r"IO([TBRL])(\d+)(\w)")
1515 def create_packages(chip: Chip, db: chipdb):
1516 def ioloc_to_tile_bel(ioloc):
1517 side, num, bel_idx = _tbrlre.match(ioloc).groups()
1518 if side == 'T':
1519 row = 0
1520 col = int(num) - 1
1521 elif side == 'B':
1522 row = db.rows - 1
1523 col = int(num) - 1
1524 elif side == 'L':
1525 row = int(num) - 1
1526 col = 0
1527 elif side == 'R':
1528 row = int(num) - 1
1529 col = db.cols - 1
1530 return (f'X{col}Y{row}', f'IOB{bel_idx}')
1531
1532 created_pkgs = set()
1533 for partno_spd, partdata in db.packages.items():
1534 pkgname, variant, spd = partdata
1535 partno = partno_spd.removesuffix(spd) # drop SPEED like 'C7/I6'
1536 if partno in created_pkgs:
1537 continue
1538 created_pkgs.add(partno)
1539 pkg = chip.create_package(partno)
1540
1541 if variant in db.sip_cst and pkgname in db.sip_cst[variant]:
1542 pkg.extra_data = PackageExtraData(chip.strs, db.sip_cst[variant][pkgname])
1543
1544 for pinno, pininfo in db.pinout[variant][pkgname].items():
1545 io_loc, cfgs = pininfo
1546 tile, bel = ioloc_to_tile_bel(io_loc)
1547 pad_func = ""
1548 for cfg in cfgs:
1549 pad_func += cfg + "/"
1550 pad_func = pad_func.rstrip('/')
1551 bank = int(db.pin_bank[io_loc])
1552 pad = pkg.create_pad(pinno, tile, bel, pad_func, bank)
1553 # add PLL if any is connected
1554 add_pll(chip, db, pad, io_loc)
1555
1556 # Extra chip data
1557 def create_extra_data(chip: Chip, db: chipdb, chip_flags: int):
1558 # The coordinates of the chip center are useful when building a DSP chain
1559 # because there is an area around this particular point that does not
1560 # contain any DSP blocks, but there are cascade and shift wires, so the gap
1561 # between adjacent DSPs is larger than usual at this point. The coordinates
1562 # of this particular cell may be useful when working with 138k clock MUXs
1563 # in the future.
1564 center_row = 0
1565 center_col = 0
1566 if hasattr(db, 'center_row'):
1567 center_row = db.center_row
1568 center_col = db.center_col
1569
1570 chip.extra_data = ChipExtraData(chip.strs, chip_flags, center_row, center_col)
1571 if hasattr(db, "dcs_prefix"):
1572 chip.extra_data.set_dcs_prefix(db.dcs_prefix)
1573 else:
1574 chip.extra_data.set_dcs_prefix("CLK")
1575 chip.extra_data.create_bottom_io()
1576 for net_a, net_b in db.bottom_io[2]:
1577 chip.extra_data.add_bottom_io_cnd(net_a, net_b)
1578 for diff_type in db.diff_io_types:
1579 chip.extra_data.add_diff_io_type(diff_type)
1580 # create hclk wire->dhcen bel map
1581 for pip, bel in dhcen_bels.items():
1582 chip.extra_data.add_dhcen_bel(pip[0], pip[1], pip[2], bel[0], bel[1], bel[2], bel[3])
1583 # create spine->dqce bel map
1584 for spine, bel in dqce_bels.items():
1585 chip.extra_data.add_dqce_bel(spine, bel[0], bel[1], bel[2])
1586 # create spine->dcs bel map
1587 for spine, bel in dcs_bels.items():
1588 chip.extra_data.add_dcs_bel(spine, bel[0], bel[1], bel[2])
1589 # create iob->dlldly bel map
1590 for io, dlldly in io_dlldly_bels.items():
1591 chip.extra_data.add_io_dlldly_bel(io, dlldly)
1592 # create segments
1593 if hasattr(db, "segments"):
1594 for y_x_idx, seg in db.segments.items():
1595 _, x, idx = y_x_idx
1596 chip.extra_data.add_segment(x, idx, seg['min_x'], seg['min_y'], seg['max_x'], seg['max_y'],
1597 seg['top_row'], seg['bottom_row'], seg['top_wire'], seg['bottom_wire'],
1598 seg['top_gate_wire'], seg['bottom_gate_wire'])
1599 # add segment nodes
1600 lt_node = [NodeWire(x, seg['top_row'], seg['top_wire'])]
1601 lt_node.append(NodeWire(x, seg['bottom_row'], seg['bottom_wire']))
1602 for row in range(seg['min_y'], seg['max_y'] + 1):
1603 lt_node.append(NodeWire(x, row, f'LT0{1 + (idx // 4) * 3}'))
1604 node = [NodeWire(x, row, f'LBO{idx // 4}')]
1605 for col in range(seg['min_x'], seg['max_x'] + 1):
1606 node.append(NodeWire(col, row, f'LB{idx}1'))
1607 chip.add_node(node)
1608 chip.add_node(lt_node)
1609 # create spine select wires
1610 if hasattr(db, "spine_select_wires"):
1611 if 'top' in db.spine_select_wires:
1612 for spine, wire_desc in db.spine_select_wires['top'].items():
1613 for y, x, wire, vcc_gnd in wire_desc:
1614 chip.extra_data.add_spine_select_wire_top(spine, x, y, wire, vcc_gnd)
1615
1616 if 'bottom' in db.spine_select_wires:
1617 for spine, wire_desc in db.spine_select_wires['bottom'].items():
1618 for y, x, wire, vcc_gnd in wire_desc:
1619 chip.extra_data.add_spine_select_wire_bottom(spine, x, y, wire, vcc_gnd)
1620
1621 def create_timing_info(chip: Chip, db: chipdb.Device):
1622 def group_to_timingvalue(group):
1623 # if himbaechel ever recognises unateness, this should match that order.
1624 ff = int(group[0] * 1000)
1625 fr = int(group[1] * 1000)
1626 rr = int(group[2] * 1000)
1627 rf = int(group[3] * 1000)
1628 return TimingValue(min(ff, fr, rf, rr), max(ff, fr, rf, rr))
1629
1630 def add_bram_bus_input(cell, clock, bus, width, group):
1631 for i in range(width):
1632 cell.add_setup_hold(clock, f"{bus}{i}", ClockEdge.RISING, group_to_timingvalue(arc[f"{group}_set"]), group_to_timingvalue(arc[f"{group}_hold"]))
1633
1634 def add_bram_bus_output(cell, clock, bus, width, group):
1635 for i in range(width):
1636 cell.add_clock_out(clock, f"{bus}{i}", ClockEdge.RISING, group_to_timingvalue(arc[group]))
1637
1638
1639 speed_grades = []
1640 for speed in db.timing.keys():
1641 speed_grades.append(speed)
1642
1643 tmg = chip.set_speed_grades(speed_grades)
1644
1645 for speed, groups in db.timing.items():
1646 for group, arc in groups.items():
1647 if group == "lut":
1648 lut = tmg.add_cell_variant(speed, "LUT4")
1649 lut.add_comb_arc("I0", "F", group_to_timingvalue(arc["a_f"]))
1650 lut.add_comb_arc("I1", "F", group_to_timingvalue(arc["b_f"]))
1651 lut.add_comb_arc("I2", "F", group_to_timingvalue(arc["c_f"]))
1652 lut.add_comb_arc("I3", "F", group_to_timingvalue(arc["d_f"]))
1653 mux5 = tmg.add_cell_variant(speed, "MUX2_LUT5")
1654 mux5.add_comb_arc("I0", "O", group_to_timingvalue(arc["m0_ofx0"]))
1655 mux5.add_comb_arc("I1", "O", group_to_timingvalue(arc["m1_ofx1"]))
1656 mux5.add_comb_arc("S0", "O", group_to_timingvalue(arc["fx_ofx1"]))
1657 mux6 = tmg.add_cell_variant(speed, "MUX2_LUT6")
1658 mux6.add_comb_arc("I0", "O", group_to_timingvalue(arc["m0_ofx0"]))
1659 mux6.add_comb_arc("I1", "O", group_to_timingvalue(arc["m1_ofx1"]))
1660 mux6.add_comb_arc("S0", "O", group_to_timingvalue(arc["fx_ofx1"]))
1661 mux7 = tmg.add_cell_variant(speed, "MUX2_LUT7")
1662 mux7.add_comb_arc("I0", "O", group_to_timingvalue(arc["m0_ofx0"]))
1663 mux7.add_comb_arc("I1", "O", group_to_timingvalue(arc["m1_ofx1"]))
1664 mux7.add_comb_arc("S0", "O", group_to_timingvalue(arc["fx_ofx1"]))
1665 mux8 = tmg.add_cell_variant(speed, "MUX2_LUT8")
1666 mux8.add_comb_arc("I0", "O", group_to_timingvalue(arc["m0_ofx0"]))
1667 mux8.add_comb_arc("I1", "O", group_to_timingvalue(arc["m1_ofx1"]))
1668 mux8.add_comb_arc("S0", "O", group_to_timingvalue(arc["fx_ofx1"]))
1669 elif group == "alu":
1670 alu = tmg.add_cell_variant(speed, "ALU")
1671 alu.add_comb_arc("I0", "SUM", group_to_timingvalue(arc["a_f"]))
1672 alu.add_comb_arc("I1", "SUM", group_to_timingvalue(arc["b_f"]))
1673 alu.add_comb_arc("I3", "SUM", group_to_timingvalue(arc["d_f"]))
1674 alu.add_comb_arc("CIN", "SUM", group_to_timingvalue(arc["fci_f0"]))
1675 alu.add_comb_arc("I0", "COUT", group_to_timingvalue(arc["a0_fco"]))
1676 alu.add_comb_arc("I1", "COUT", group_to_timingvalue(arc["b0_fco"]))
1677 alu.add_comb_arc("I3", "COUT", group_to_timingvalue(arc["d0_fco"]))
1678 alu.add_comb_arc("CIN", "COUT", group_to_timingvalue(arc["fci_fco"]))
1679 elif group == "sram":
1680 sram = tmg.add_cell_variant(speed, "RAM16SDP4")
1681 for do in range(4):
1682 for rad in range(4):
1683 sram.add_comb_arc(f"RAD[{rad}]", f"DO[{do}]", group_to_timingvalue(arc[f"rad{rad}_do"]))
1684 sram.add_clock_out("CLK", f"DO[{do}]", ClockEdge.RISING, group_to_timingvalue(arc["clk_do"]))
1685 for di in range(4):
1686 sram.add_setup_hold("CLK", f"DI[{di}", ClockEdge.RISING, group_to_timingvalue(arc["clk_di_set"]), group_to_timingvalue(arc["clk_di_hold"]))
1687 sram.add_setup_hold("CLK", "WRE", ClockEdge.RISING, group_to_timingvalue(arc["clk_wre_set"]), group_to_timingvalue(arc["clk_wre_hold"]))
1688 for wad in range(4):
1689 sram.add_setup_hold("CLK", f"WAD[{wad}]", ClockEdge.RISING, group_to_timingvalue(arc[f"clk_wad{wad}_set"]), group_to_timingvalue(arc[f"clk_wad{wad}_hold"]))
1690 elif group == "dff":
1691 for reset_type in ('', 'P', 'C', 'S', 'R'):
1692 for clock_enable in ('', 'E'):
1693 cell_name = "DFF{}{}".format(reset_type, clock_enable)
1694 dff = tmg.add_cell_variant(speed, cell_name)
1695 dff.add_setup_hold("CLK", "D", ClockEdge.RISING, group_to_timingvalue(arc["di_clksetpos"]), group_to_timingvalue(arc["di_clkholdpos"]))
1696 dff.add_setup_hold("CLK", "CE", ClockEdge.RISING, group_to_timingvalue(arc["ce_clksetpos"]), group_to_timingvalue(arc["ce_clkholdpos"]))
1697 dff.add_clock_out("CLK", "Q", ClockEdge.RISING, group_to_timingvalue(arc["clk_qpos"]))
1698
1699 if reset_type in ('S', 'R'):
1700 port = "RESET" if reset_type == 'R' else "SET"
1701 dff.add_setup_hold("CLK", port, ClockEdge.RISING, group_to_timingvalue(arc["lsr_clksetpos_syn"]), group_to_timingvalue(arc["lsr_clkholdpos_syn"]))
1702 elif reset_type in ('P', 'C'):
1703 port = "CLEAR" if reset_type == 'C' else "PRESET"
1704 dff.add_setup_hold("CLK", port, ClockEdge.RISING, group_to_timingvalue(arc["lsr_clksetpos_asyn"]), group_to_timingvalue(arc["lsr_clkholdpos_asyn"]))
1705 dff.add_comb_arc(port, "Q", group_to_timingvalue(arc["lsr_q"]))
1706
1707 cell_name = "DFFN{}{}".format(reset_type, clock_enable)
1708 dff = tmg.add_cell_variant(speed, cell_name)
1709 dff.add_setup_hold("CLK", "D", ClockEdge.FALLING, group_to_timingvalue(arc["di_clksetneg"]), group_to_timingvalue(arc["di_clkholdneg"]))
1710 dff.add_setup_hold("CLK", "CE", ClockEdge.FALLING, group_to_timingvalue(arc["ce_clksteneg"]), group_to_timingvalue(arc["ce_clkholdneg"])) # the DBs have a typo...
1711 dff.add_clock_out("CLK", "Q", ClockEdge.FALLING, group_to_timingvalue(arc["clk_qneg"]))
1712
1713 if reset_type in ('S', 'R'):
1714 port = "RESET" if reset_type == 'R' else "SET"
1715 dff.add_setup_hold("CLK", port, ClockEdge.FALLING, group_to_timingvalue(arc["lsr_clksetneg_syn"]), group_to_timingvalue(arc["lsr_clkholdneg_syn"]))
1716 elif reset_type in ('P', 'C'):
1717 port = "CLEAR" if reset_type == 'C' else "PRESET"
1718 dff.add_setup_hold("CLK", port, ClockEdge.FALLING, group_to_timingvalue(arc["lsr_clksetneg_asyn"]), group_to_timingvalue(arc["lsr_clkholdneg_asyn"]))
1719 dff.add_comb_arc(port, "Q", group_to_timingvalue(arc["lsr_q"]))
1720 elif group == "bram":
1721 for sp_type in ("SP", "SPX9"):
1722 sp = tmg.add_cell_variant(speed, sp_type)
1723 add_bram_bus_output(sp, "CLK", "DO", 36 if sp_type == "SPX9" else 32, "clk_do_bypass")
1724 add_bram_bus_input(sp, "CLK", "DI", 36 if sp_type == "SPX9" else 32, "clk_di")
1725 add_bram_bus_input(sp, "CLK", "AD", 14, "clk_ad")
1726 add_bram_bus_input(sp, "CLK", "BLKSEL", 3, "clk_blksel")
1727
1728 for sig in ["CE", "WRE", "OCE", "RESET"]:
1729 sp.add_setup_hold("CLK", sig, ClockEdge.RISING, group_to_timingvalue(arc[f"clk_{sig.lower()}_set"]), group_to_timingvalue(arc[f"clk_{sig.lower()}_hold"]))
1730 for sdp_type in ("SDP", "SDPX9", "SDPB", "SDPX9B"):
1731 sdp = tmg.add_cell_variant(speed, sdp_type)
1732 add_bram_bus_output(sdp, "CLKB", "DO", 36 if sdp_type.startswith("SDPX9") else 32, "clkb_do_bypass")
1733 add_bram_bus_input(sdp, "CLKA", "DI", 36 if sdp_type.startswith("SDPX9") else 32, "clka_di")
1734 add_bram_bus_input(sdp, "CLKA", "ADA", 14, "clka_ada")
1735 add_bram_bus_input(sdp, "CLKB", "ADB", 14, "clkb_adb")
1736
1737 add_bram_bus_input(sdp, "CLKA", "BLKSELA", 3, "clka_blksel")
1738 add_bram_bus_input(sdp, "CLKB", "BLKSELB", 3, "clkb_blksel")
1739
1740 for sig in ["CEA", "WREA", "RESETA"]:
1741 sdp.add_setup_hold("CLKA", sig, ClockEdge.RISING, group_to_timingvalue(arc[f"clka_{sig.lower()}_set"]), group_to_timingvalue(arc[f"clka_{sig.lower()}_hold"]))
1742
1743 for sig in ["CEB", "OCEB", "RESETB"]:
1744 sdp.add_setup_hold("CLKB", sig, ClockEdge.RISING, group_to_timingvalue(arc[f"clkb_{sig.lower()}_set"]), group_to_timingvalue(arc[f"clkb_{sig.lower()}_hold"]))
1745
1746 for dp_type in ("DP", "DPX9", "DPB", "DPX9B"):
1747 dp = tmg.add_cell_variant(speed, dp_type)
1748 add_bram_bus_output(dp, "CLKA", "DOA", 36 if dp_type.startswith("DPX9") else 32, "clka_doa_bypass")
1749 add_bram_bus_output(dp, "CLKB", "DO", 36 if dp_type.startswith("DPX9") else 32, "clkb_dob_bypass")
1750 add_bram_bus_input(dp, "CLKA", "DIA", 36 if dp_type.startswith("DPX9") else 32, "clka_dia")
1751 add_bram_bus_input(dp, "CLKB", "DIB", 36 if dp_type.startswith("DPX9") else 32, "clkb_dib")
1752 add_bram_bus_input(dp, "CLKA", "ADA", 14, "clka_ada")
1753 add_bram_bus_input(sp, "CLKB", "ADB", 14, "clkb_adb")
1754
1755 add_bram_bus_input(dp, "CLKA", "BLKSELA", 3, "clka_blksel")
1756 add_bram_bus_input(dp, "CLKB", "BLKSELB", 3, "clkb_blksel")
1757
1758 for sig in ["CEA", "OCEA", "WREA", "RESETA"]:
1759 dp.add_setup_hold("CLKA", sig, ClockEdge.RISING, group_to_timingvalue(arc[f"clka_{sig.lower()}_set"]), group_to_timingvalue(arc[f"clka_{sig.lower()}_hold"]))
1760
1761 for sig in ["CEB", "OCEB", "WREB", "RESETB"]:
1762 dp.add_setup_hold("CLKB", sig, ClockEdge.RISING, group_to_timingvalue(arc[f"clkb_{sig.lower()}_set"]), group_to_timingvalue(arc[f"clkb_{sig.lower()}_hold"]))
1763
1764
1765 elif group == "fanout":
1766 pass # handled in "wire"
1767 elif group == "glbsrc":
1768 # no fanout delay for clock wires
1769 for name in ["CENT_SPINE_PCLK", "SPINE_TAP_PCLK", "TAP_BRANCH_PCLK"]:
1770 tmg.set_pip_class(speed, name, group_to_timingvalue(arc[name]))
1771 tmg.set_pip_class(speed, 'GCLK_BRANCH', group_to_timingvalue(arc['BRANCH_PCLK']))
1772 elif group == "hclk":
1773 for name in ['HclkInMux', 'HclkHbrgMux', 'HclkOutMux', 'HclkDivMux']:
1774 tmg.set_pip_class(speed, name, group_to_timingvalue(arc[name]))
1775 elif group == "iodelay":
1776 for name in ['GI_DO', 'SDTAP_DO', 'SETN_DO', 'VALUE_DO', 'SDTAP_DF', 'SETN_DF', 'VALUE_DF']:
1777 tmg.set_pip_class(speed, name, group_to_timingvalue(arc[name]))
1778 elif group == "wire":
1779 # wires with delay and fanout delay
1780 for name in ["X0", "X2", "X8"]:
1781 tmg.set_pip_class(speed, name, group_to_timingvalue(arc[name]), group_to_timingvalue(groups["fanout"][f"{name}Fan"]), TimingValue(round(1e6 / groups["fanout"][f"{name}FanNum"])))
1782 # wires with delay but no fanout delay
1783 for name in ["X0CTL", "X0CLK", "FX1"]:
1784 tmg.set_pip_class(speed, name, group_to_timingvalue(arc[name]))
1785 # wires with presently-unknown delay
1786 for name in ["LUT_IN", "DI", "SEL", "CIN", "COUT", "VCC", "VSS", "LW_TAP", "LW_TAP_0", "LW_BRANCH", "LW_SPAN", "ISB"]:
1787 tmg.set_pip_class(speed, name, TimingValue())
1788 # wires with fanout-only delay; used on cell output pips
1789 for name, mapping in [("LUT_OUT", "FFan"), ("FF_OUT", "QFan"), ("OF", "OFFan")]:
1790 tmg.set_pip_class(speed, name, TimingValue(), group_to_timingvalue(groups["fanout"][mapping]), TimingValue(round(1e6 / groups["fanout"][f"{mapping}Num"])))
1791
1792 # If Apicula does not specify a special location for the global GND and VCC
1793 # sources, place them at X0Y0.
1794 def check_place_VCC_GND(db: chipdb.Device):
1795 for funcs in db.extra_func.values():
1796 if 'gnd_source' in funcs or 'vcc_source' in funcs:
1797 return
1798 db.extra_func.setdefault((0, 0), {}).update({'gnd_source':{}, 'vcc_source': {}})
1799
1800 # *******************************
1801 def main():
1802 parser = argparse.ArgumentParser(description='Make Gowin BBA')
1803 parser.add_argument('-d', '--device', required=True)
1804 parser.add_argument('-o', '--output', default="out.bba")
1805
1806 args = parser.parse_args()
1807
1808 device = args.device
1809 with importlib.resources.as_file(importlib.resources.files("apycula").joinpath(f"{device}.msgpack.xz")) as chipdb_path:
1810 db = load_chipdb(chipdb_path)
1811
1812 chip_flags = 0;
1813 # XXX compatibility
1814 if not hasattr(db, "chip_flags"):
1815 if device not in {"GW1NS-4", "GW1N-9"}:
1816 chip_flags |= CHIP_HAS_SP32;
1817 else:
1818 if "HAS_SP32" in db.chip_flags:
1819 chip_flags |= CHIP_HAS_SP32;
1820 if "NEED_SP_FIX" in db.chip_flags:
1821 chip_flags |= CHIP_NEED_SP_FIX;
1822 if "NEED_BSRAM_OUTREG_FIX" in db.chip_flags:
1823 chip_flags |= CHIP_NEED_BSRAM_OUTREG_FIX;
1824 if "NEED_BSRAM_DP_CE_FIX" in db.chip_flags:
1825 chip_flags |= CHIP_NEED_BSRAM_DP_CE_FIX;
1826 if "NEED_BLKSEL_FIX" in db.chip_flags:
1827 chip_flags |= CHIP_NEED_BLKSEL_FIX;
1828 if "HAS_BANDGAP" in db.chip_flags:
1829 chip_flags |= CHIP_HAS_BANDGAP;
1830 if "HAS_PLL_HCLK" in db.chip_flags:
1831 chip_flags |= CHIP_HAS_PLL_HCLK;
1832 if "HAS_CLKDIV_HCLK" in db.chip_flags:
1833 chip_flags |= CHIP_HAS_CLKDIV_HCLK;
1834 if "HAS_PINCFG" in db.chip_flags:
1835 chip_flags |= CHIP_HAS_PINCFG;
1836 if "HAS_DFF67" in db.chip_flags:
1837 chip_flags |= CHIP_HAS_DFF67;
1838 if "HAS_CIN_MUX" in db.chip_flags:
1839 chip_flags |= CHIP_HAS_CIN_MUX;
1840 if "NEED_BSRAM_RESET_FIX" in db.chip_flags:
1841 chip_flags |= CHIP_NEED_BSRAM_RESET_FIX;
1842 if "NEED_SDP_FIX" in db.chip_flags:
1843 chip_flags |= CHIP_NEED_SDP_FIX;
1844 if "NEED_CFGPINS_INVERSION" in db.chip_flags:
1845 chip_flags |= CHIP_NEED_CFGPINS_INVERSION;
1846 if "HAS_I2CCFG" in db.chip_flags:
1847 chip_flags |= CHIP_HAS_I2CCFG;
1848 if "HAS_5A_DSP" in db.chip_flags:
1849 chip_flags |= CHIP_HAS_5A_DSP;
1850
1851 X = db.cols;
1852 Y = db.rows;
1853
1854 ch = Chip("gowin", device, X, Y)
1855
1856 # Init constant ids
1857 ch.strs.read_constids(path.join(path.dirname(__file__), "constids.inc"))
1858
1859 # packages from parntnumbers
1860 create_packages(ch, db)
1861
1862 # The manufacturer distinguishes by externally identical tiles, so keep
1863 # these differences (in case it turns out later that there is a slightly
1864 # different routing or something like that).
1865 logic_tiletypes = db.tile_types['C']
1866 io_tiletypes = db.tile_types['I']
1867 ssram_tiletypes = db.tile_types['M']
1868 pll_tiletypes = db.tile_types['P']
1869 bsram_tiletypes = db.tile_types.get('B', set())
1870 dsp_tiletypes = db.tile_types.get('D', set())
1871 dsp_5a_tiletypes = db.tile_types.get('D5A', set())
1872
1873 # If Apicula does not specify a special location for the global GND and VCC
1874 # sources, place them at X0Y0.
1875 check_place_VCC_GND(db)
1876
1877 # Setup tile grid
1878 for x in range(X):
1879 for y in range(Y):
1880 ttyp = db.grid[y][x]
1881 if ttyp in logic_tiletypes:
1882 create_tiletype(create_logic_tiletype, ch, db, x, y, ttyp)
1883 elif ttyp in ssram_tiletypes:
1884 create_tiletype(create_ssram_tiletype, ch, db, x, y, ttyp)
1885 elif ttyp in io_tiletypes:
1886 create_tiletype(create_io_tiletype, ch, db, x, y, ttyp)
1887 elif ttyp in pll_tiletypes:
1888 create_tiletype(create_pll_tiletype, ch, db, x, y, ttyp)
1889 elif ttyp in bsram_tiletypes:
1890 create_tiletype(create_bsram_tiletype, ch, db, x, y, ttyp)
1891 elif ttyp in dsp_tiletypes:
1892 create_tiletype(create_dsp_tiletype, ch, db, x, y, ttyp)
1893 elif ttyp in dsp_5a_tiletypes:
1894 create_tiletype(create_dsp_5a_tiletype, ch, db, x, y, ttyp)
1895 else:
1896 create_tiletype(create_null_tiletype, ch, db, x, y, ttyp)
1897
1898 # Create nodes between tiles
1899 create_nodes(ch, db)
1900 create_extra_data(ch, db, chip_flags)
1901 create_timing_info(ch, db)
1902 ch.write_bba(args.output)
1903 if __name__ == '__main__':
1904 main()