| 1 |
# |
| 2 |
# nextpnr -- Next Generation Place and Route |
| 3 |
# |
| 4 |
# Copyright (C) 2024 The Project Peppercorn Authors. |
| 5 |
# |
| 6 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 7 |
# purpose with or without fee is hereby granted, provided that the above |
| 8 |
# copyright notice and this permission notice appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
# |
| 18 |
|
| 19 |
import os |
| 20 |
from os import path |
| 21 |
import re |
| 22 |
import sys |
| 23 |
import argparse |
| 24 |
|
| 25 |
sys.path.append(path.join(path.dirname(__file__), "../../..")) |
| 26 |
from himbaechel_dbgen.chip import * |
| 27 |
|
| 28 |
PIP_EXTRA_MUX = 1 |
| 29 |
|
| 30 |
MUX_INVERT = 1 |
| 31 |
MUX_VISIBLE = 2 |
| 32 |
MUX_CONFIG = 4 |
| 33 |
MUX_ROUTING = 8 |
| 34 |
|
| 35 |
parser = argparse.ArgumentParser() |
| 36 |
parser.add_argument("--lib", help="Project Peppercorn python database script path", type=str, required=True) |
| 37 |
parser.add_argument("--device", help="name of device to export", type=str, required=True) |
| 38 |
parser.add_argument("--bba", help="bba file to write", type=str, required=True) |
| 39 |
args = parser.parse_args() |
| 40 |
|
| 41 |
sys.path.append(os.path.expanduser(args.lib)) |
| 42 |
sys.path += args.lib |
| 43 |
import chip |
| 44 |
import die |
| 45 |
|
| 46 |
pip_tmg_names = set() |
| 47 |
|
| 48 |
@dataclass |
| 49 |
class TileExtraData(BBAStruct): |
| 50 |
die : int = 0 |
| 51 |
bit_x: int = 0 |
| 52 |
bit_y: int = 0 |
| 53 |
tile_x: int = 0 |
| 54 |
tile_y: int = 0 |
| 55 |
prim_id : int = 0 |
| 56 |
|
| 57 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 58 |
pass |
| 59 |
def serialise(self, context: str, bba: BBAWriter): |
| 60 |
bba.u8(self.die) |
| 61 |
bba.u8(self.bit_x) |
| 62 |
bba.u8(self.bit_y) |
| 63 |
bba.u8(self.tile_x) |
| 64 |
bba.u8(self.tile_y) |
| 65 |
bba.u8(self.prim_id) |
| 66 |
bba.u16(0) |
| 67 |
|
| 68 |
@dataclass |
| 69 |
class PipExtraData(BBAStruct): |
| 70 |
pip_type: int |
| 71 |
name: IdString |
| 72 |
bits: int = 0 |
| 73 |
value: int = 0 |
| 74 |
invert: int = 0 |
| 75 |
plane: int = 0 |
| 76 |
block: int = 0 |
| 77 |
resource: int = 0 |
| 78 |
group_index: int = 0 |
| 79 |
|
| 80 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 81 |
pass |
| 82 |
def serialise(self, context: str, bba: BBAWriter): |
| 83 |
bba.u32(self.name.index) |
| 84 |
bba.u8(self.bits) |
| 85 |
bba.u8(self.value) |
| 86 |
bba.u8(self.invert) |
| 87 |
bba.u8(self.pip_type) |
| 88 |
bba.u8(self.plane) |
| 89 |
bba.u8(0) |
| 90 |
bba.u16(0) |
| 91 |
bba.u32(self.block) |
| 92 |
bba.u32(self.resource) |
| 93 |
bba.u32(self.group_index) |
| 94 |
|
| 95 |
@dataclass |
| 96 |
class BelPinConstraint(BBAStruct): |
| 97 |
index: int |
| 98 |
pin_name: IdString |
| 99 |
constr_x: int = 0 |
| 100 |
constr_y: int = 0 |
| 101 |
constr_z: int = 0 |
| 102 |
|
| 103 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 104 |
pass |
| 105 |
def serialise(self, context: str, bba: BBAWriter): |
| 106 |
bba.u32(self.pin_name.index) |
| 107 |
bba.u16(self.constr_x) |
| 108 |
bba.u16(self.constr_y) |
| 109 |
bba.u16(self.constr_z) |
| 110 |
bba.u16(0) |
| 111 |
|
| 112 |
@dataclass |
| 113 |
class BelExtraData(BBAStruct): |
| 114 |
constraints: list[BelPinConstraint] = field(default_factory = list) |
| 115 |
|
| 116 |
def add_constraints(self, pin: IdString, x: int, y: int, z:int): |
| 117 |
item = BelPinConstraint(len(self.constraints),pin,x,y,z) |
| 118 |
self.constraints.append(item) |
| 119 |
|
| 120 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 121 |
self.constraints.sort(key=lambda p: p.pin_name.index) |
| 122 |
bba.label(f"{context}_constraints") |
| 123 |
for i, t in enumerate(self.constraints): |
| 124 |
t.serialise(f"{context}_constraint{i}", bba) |
| 125 |
pass |
| 126 |
def serialise(self, context: str, bba: BBAWriter): |
| 127 |
bba.slice(f"{context}_constraints", len(self.constraints)) |
| 128 |
|
| 129 |
@dataclass |
| 130 |
class PadExtraData(BBAStruct): |
| 131 |
x: int = 0 |
| 132 |
y: int = 0 |
| 133 |
z: int = 0 |
| 134 |
|
| 135 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 136 |
pass |
| 137 |
def serialise(self, context: str, bba: BBAWriter): |
| 138 |
bba.u16(self.x) |
| 139 |
bba.u16(self.y) |
| 140 |
bba.u16(self.z) |
| 141 |
bba.u16(0) |
| 142 |
|
| 143 |
@dataclass |
| 144 |
class TimingExtraData(BBAStruct): |
| 145 |
name: IdString |
| 146 |
delay: TimingValue = field(default_factory=TimingValue) |
| 147 |
|
| 148 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 149 |
pass |
| 150 |
def serialise(self, context: str, bba: BBAWriter): |
| 151 |
bba.u32(self.name.index) |
| 152 |
self.delay.serialise(context, bba) |
| 153 |
|
| 154 |
@dataclass |
| 155 |
class SpeedGradeExtraData(BBAStruct): |
| 156 |
timings: list[TimingExtraData] = field(default_factory = list) |
| 157 |
|
| 158 |
def add_timing(self, name: IdString, delay: TimingValue): |
| 159 |
item = TimingExtraData(name,delay) |
| 160 |
self.timings.append(item) |
| 161 |
|
| 162 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 163 |
bba.label(f"{context}_timings") |
| 164 |
for i, t in enumerate(self.timings): |
| 165 |
t.serialise(f"{context}_timing{i}", bba) |
| 166 |
pass |
| 167 |
def serialise(self, context: str, bba: BBAWriter): |
| 168 |
bba.slice(f"{context}_timings", len(self.timings)) |
| 169 |
|
| 170 |
@dataclass |
| 171 |
class DieRegion(BBAStruct): |
| 172 |
name: IdString |
| 173 |
x1: int = 0 |
| 174 |
y1: int = 0 |
| 175 |
x2: int = 0 |
| 176 |
y2: int = 0 |
| 177 |
|
| 178 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 179 |
pass |
| 180 |
def serialise(self, context: str, bba: BBAWriter): |
| 181 |
bba.u32(self.name.index) |
| 182 |
bba.u16(self.x1) |
| 183 |
bba.u16(self.y1) |
| 184 |
bba.u16(self.x2) |
| 185 |
bba.u16(self.y2) |
| 186 |
|
| 187 |
@dataclass |
| 188 |
class ChipExtraData(BBAStruct): |
| 189 |
dies: list[DieRegion] = field(default_factory = list) |
| 190 |
|
| 191 |
def add_die(self, name: IdString, x1: int, y1: int, x2:int, y2:int): |
| 192 |
item = DieRegion(name,x1,y1,x2,y2) |
| 193 |
self.dies.append(item) |
| 194 |
|
| 195 |
def serialise_lists(self, context: str, bba: BBAWriter): |
| 196 |
self.dies.sort(key=lambda p: p.name.index) |
| 197 |
bba.label(f"{context}_dies") |
| 198 |
for i, t in enumerate(self.dies): |
| 199 |
t.serialise(f"{context}_die{i}", bba) |
| 200 |
pass |
| 201 |
def serialise(self, context: str, bba: BBAWriter): |
| 202 |
bba.slice(f"{context}_dies", len(self.dies)) |
| 203 |
|
| 204 |
def convert_timing(tim): |
| 205 |
return TimingValue(tim.rise.min, tim.rise.max, tim.fall.min, tim.fall.max) |
| 206 |
|
| 207 |
def set_timings(ch): |
| 208 |
speed_grades = ["best_lpr", "best_eco", "best_spd", |
| 209 |
"typ_lpr", "typ_eco", "typ_spd", |
| 210 |
"worst_lpr", "worst_eco", "worst_spd"] |
| 211 |
rename_table = { |
| 212 |
"CINX_OUT1": "_ARBLUT_CINX_OUT1", |
| 213 |
"CINX_OUT2": "_ARBLUT_CINX_OUT2", |
| 214 |
"PINX_OUT1": "_ARBLUT_PINX_OUT1", |
| 215 |
"PINX_OUT2": "_ARBLUT_PINX_OUT2", |
| 216 |
"PINY1_OUT1": "_ARBLUT_PINY1_OUT1", |
| 217 |
"PINY1_OUT2": "_ARBLUT_PINY1_OUT2", |
| 218 |
} |
| 219 |
tmg = ch.set_speed_grades(speed_grades) |
| 220 |
for speed in speed_grades: |
| 221 |
print(f"Loading timings for {speed}...") |
| 222 |
timing = dict(sorted(chip.get_timings(speed).items())) |
| 223 |
tmg.get_speed_grade(speed).extra_data = SpeedGradeExtraData() |
| 224 |
for name, val in timing.items(): |
| 225 |
if name.startswith(("sb_del_t", "im_x", "om_x", "sb_rim_xy", "edge_xy")): |
| 226 |
continue |
| 227 |
name = "timing_" + re.sub(r"[-= >]", "_", rename_table.get(name, name)) |
| 228 |
tmg.get_speed_grade(speed).extra_data.add_timing(name=ch.strs.id(name), delay=convert_timing(val)) |
| 229 |
for k in pip_tmg_names: |
| 230 |
assert k in timing, f"pip class {k} not found in timing data" |
| 231 |
tmg.set_pip_class(grade=speed, name=k, delay=convert_timing(timing[k])) |
| 232 |
|
| 233 |
EXPECTED_VERSION = 1.12 |
| 234 |
|
| 235 |
RESOURCE_NAMES = { |
| 236 |
1 << 0: "C_SELX", |
| 237 |
1 << 1: "C_SELY1", |
| 238 |
1 << 2: "C_SELY2", |
| 239 |
1 << 3: "C_SEL_C", |
| 240 |
1 << 4: "C_SEL_P", |
| 241 |
1 << 5: "C_Y12", |
| 242 |
1 << 6: "C_CX_I", |
| 243 |
1 << 7: "C_CY1_I", |
| 244 |
1 << 8: "C_CY2_I", |
| 245 |
1 << 9: "C_PX_I", |
| 246 |
1 << 10: "C_PY1_I", |
| 247 |
1 << 11: "C_PY2_I", |
| 248 |
} |
| 249 |
|
| 250 |
def main(): |
| 251 |
# Range needs to be +1, but we are adding +2 more to coordinates, since |
| 252 |
# they are starting from -2 instead of zero required for nextpnr |
| 253 |
dev = chip.get_device(args.device) |
| 254 |
ch = Chip("gatemate", args.device, dev.max_col() + 3, dev.max_row() + 3) |
| 255 |
# Init constant ids |
| 256 |
ch.strs.read_constids(path.join(path.dirname(__file__), "..", "constids.inc")) |
| 257 |
ch.read_gfxids(path.join(path.dirname(__file__), "..", "gfxids.inc")) |
| 258 |
try: |
| 259 |
if chip.get_version()!=EXPECTED_VERSION: |
| 260 |
print("==============================================================================") |
| 261 |
print(f"ERROR: Expected v{EXPECTED_VERSION} and current v{chip.get_version()} chip database mismatch") |
| 262 |
print(" Please update prjpeppercorn and/or nextpnr") |
| 263 |
print("==============================================================================") |
| 264 |
os._exit(-1) |
| 265 |
except AttributeError: |
| 266 |
print("==============================================================================") |
| 267 |
print("ERROR: Unable to determine prjpepercorn version") |
| 268 |
print(" Please update prjpeppercorn and/or nextpnr") |
| 269 |
print("==============================================================================") |
| 270 |
os._exit(-1) |
| 271 |
|
| 272 |
|
| 273 |
if not chip.check_dly_available(): |
| 274 |
print("==============================================================================") |
| 275 |
print("ERROR: Delay files not, found") |
| 276 |
print(" Run delay.sh in prjpeppercorn to download needed files") |
| 277 |
print("==============================================================================") |
| 278 |
os._exit(-1) |
| 279 |
|
| 280 |
ch.extra_data = ChipExtraData() |
| 281 |
for d in sorted(dev.dies): |
| 282 |
ch.extra_data.add_die(ch.strs.id(dev.dies[d].name), dev.dies[d].offset_x, dev.dies[d].offset_y, dev.dies[d].offset_x + die.num_cols() - 1, dev.dies[d].offset_y + die.num_rows()-1) |
| 283 |
|
| 284 |
new_wires = dict() |
| 285 |
wire_delay = dict() |
| 286 |
for _,nodes in dev.get_connections(): |
| 287 |
for conn in nodes: |
| 288 |
if conn.endpoint: |
| 289 |
t_name = dev.get_tile_type(conn.x,conn.y) |
| 290 |
if t_name not in new_wires: |
| 291 |
new_wires[t_name] = set() |
| 292 |
new_wires[t_name].add(conn.name) |
| 293 |
# Check to confirm no duplicates |
| 294 |
if conn.name in wire_delay: |
| 295 |
assert wire_delay[conn.name] == conn.delay, f"conflict delay {conn.name}" |
| 296 |
wire_delay[conn.name] = conn.delay |
| 297 |
|
| 298 |
for type_name in sorted(die.get_tile_type_list()): |
| 299 |
tt = ch.create_tile_type(type_name) |
| 300 |
for group in sorted(die.get_groups_for_type(type_name)): |
| 301 |
tt.create_group(group.name, group.type) |
| 302 |
if ("CPE" in type_name): |
| 303 |
for name in RESOURCE_NAMES.values(): |
| 304 |
tt.create_group(name, "RESOURCE") |
| 305 |
for wire in sorted(die.get_endpoints_for_type(type_name)): |
| 306 |
tt.create_wire(wire.name, wire.type) |
| 307 |
if type_name in new_wires: |
| 308 |
for wire in sorted(new_wires[type_name]): |
| 309 |
tt.create_wire(wire+"_n", "NODE_WIRE") |
| 310 |
for prim in sorted(die.get_primitives_for_type(type_name)): |
| 311 |
bel = tt.create_bel(prim.name, prim.type, prim.z) |
| 312 |
if (prim.name in ["CPE_LT_FULL", "CPE_BRIDGE"]): |
| 313 |
bel.flags |= BEL_FLAG_HIDDEN |
| 314 |
extra = BelExtraData() |
| 315 |
for constr in sorted(die.get_pins_constraint(type_name, prim.name, prim.type)): |
| 316 |
extra.add_constraints(ch.strs.id(constr.name),constr.rel_x,constr.rel_y,4 if constr.pin_num==2 else 5) |
| 317 |
bel.extra_data = extra |
| 318 |
for pin in sorted(die.get_primitive_pins(prim.type)): |
| 319 |
tt.add_bel_pin(bel, pin.name, die.get_pin_connection_name(prim,pin), pin.dir) |
| 320 |
for mux in sorted(die.get_mux_connections_for_type(type_name)): |
| 321 |
if len(mux.delay)>0: |
| 322 |
pip_tmg_names.add(mux.delay) |
| 323 |
pp = tt.create_pip(mux.src, mux.dst, mux.delay) |
| 324 |
if mux.name: |
| 325 |
mux_flags = MUX_INVERT if mux.invert else 0 |
| 326 |
mux_flags |= MUX_VISIBLE if mux.visible else 0 |
| 327 |
mux_flags |= MUX_CONFIG if mux.config else 0 |
| 328 |
plane = 0 |
| 329 |
if mux.name.startswith("IM"): |
| 330 |
plane = int(mux.name[4:6]) |
| 331 |
if mux.name.startswith("SB_SML") or mux.name.startswith("SB_BIG"): |
| 332 |
plane = int(mux.name[8:10]) |
| 333 |
if mux.name.startswith("SB_DRIVE"): |
| 334 |
plane = int(mux.name[10:12]) |
| 335 |
if mux.name == "CPE.C_SN": |
| 336 |
mux_flags |= MUX_ROUTING |
| 337 |
group_index = 0 |
| 338 |
if mux.resource > 0: |
| 339 |
group = RESOURCE_NAMES.get(mux.resource, "UNKNOWN") |
| 340 |
group_index = tt._group2idx[tt.strs.id(group)] |
| 341 |
tt.add_pip_to_group(pp, group) |
| 342 |
pp.extra_data = PipExtraData(PIP_EXTRA_MUX, ch.strs.id(mux.name), mux.bits, mux.value, mux_flags, plane, mux.block, mux.resource, group_index) |
| 343 |
if type_name in new_wires: |
| 344 |
for wire in sorted(new_wires[type_name]): |
| 345 |
delay = wire_delay[wire] |
| 346 |
if len(delay)>0: |
| 347 |
pip_tmg_names.add(delay) |
| 348 |
pp = tt.create_pip(wire+"_n", wire, delay) |
| 349 |
plane = 0 |
| 350 |
if wire.startswith("IM"): |
| 351 |
plane = int(wire[4:6]) |
| 352 |
if wire.startswith("SB_SML") or wire.startswith("SB_BIG"): |
| 353 |
plane = int(wire[8:10]) |
| 354 |
if wire.startswith("SB_DRIVE"): |
| 355 |
plane = int(wire[10:12]) |
| 356 |
pp.extra_data = PipExtraData(PIP_EXTRA_MUX, ch.strs.id(""), 0, 0, 0, plane) |
| 357 |
|
| 358 |
# Setup tile grid |
| 359 |
for x in range(dev.max_col() + 3): |
| 360 |
for y in range(dev.max_row() + 3): |
| 361 |
ti = ch.set_tile_type(x, y, dev.get_tile_type(x - 2,y - 2)) |
| 362 |
tileinfo = dev.get_tile_info(x - 2,y - 2) |
| 363 |
ti.extra_data = TileExtraData(tileinfo.die, tileinfo.bit_x, tileinfo.bit_y, tileinfo.tile_x, tileinfo.tile_y, tileinfo.prim_index) |
| 364 |
|
| 365 |
# Create nodes between tiles |
| 366 |
for _,nodes in dev.get_connections(): |
| 367 |
node = [] |
| 368 |
for conn in sorted(nodes): |
| 369 |
node.append(NodeWire(conn.x + 2, conn.y + 2, (conn.name + "_n") if conn.endpoint else conn.name)) |
| 370 |
ch.add_node(node) |
| 371 |
set_timings(ch) |
| 372 |
|
| 373 |
for package in dev.get_packages(): |
| 374 |
pkg = ch.create_package(package) |
| 375 |
for pad in sorted(dev.get_package_pads(package)): |
| 376 |
pp = pkg.create_pad(pad.name, f"X{pad.x+2}Y{pad.y+2}", pad.bel, pad.function, pad.bank, pad.flags) |
| 377 |
pp.extra_data = PadExtraData(pad.ddr.x+2, pad.ddr.y+2, 4 if pad.ddr.z==0 else 5) |
| 378 |
|
| 379 |
ch.write_bba(args.bba) |
| 380 |
|
| 381 |
if __name__ == '__main__': |
| 382 |
main() |