aboutsummaryrefslogtreecommitdiffstats
path: root/ice40/chipdb.py
blob: 9e783dd0a5efa3a959ef9d11c607bddf9e0ada3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3

import sys

dev_name = None
dev_width = None
dev_height = None
num_wires = None

wire_uphill = dict()
wire_downhill = dict()
wire_bidir = dict()

with open(sys.argv[1], "r") as f:
    mode = None

    for line in f:
        line = line.split()

        if len(line) == 0 or line[0] == "#":
            continue

        if line[0] == ".device":
            dev_name = line[1]
            dev_width = int(line[2])
            dev_height = int(line[3])
            num_wires = int(line[4])
            continue

        if line[0] == ".net":
            mode = ("net", int(line[1]))
            continue

        if line[0] == ".buffer":
            mode = ("buffer", int(line[3]))
            continue

        if line[0] == ".routing":
            mode = ("routing", int(line[3]))
            continue

        if (line[0][0] == ".") or (mode is None):
            mode = None
            continue

        if mode[0] == "net":
            continue

        if mode[0] == "buffer":
            wire_a = int(line[1])
            wire_b = mode[1]
            if wire_a not in wire_downhill:
                wire_downhill[wire_a] = set()
            if wire_b not in wire_uphill:
                wire_uphill[wire_b] = set()
            wire_downhill[wire_a].add(wire_b)
            wire_uphill[wire_b].add(wire_a)
            continue

        if mode[0] == "routing":
            wire_a = int(line[1])
            wire_b = mode[1]
            if wire_a not in wire_bidir:
                wire_bidir[wire_a] = set()
            if wire_b not in wire_bidir:
                wire_bidir[wire_b] = set()
            wire_bidir[wire_a].add(wire_b)
            wire_bidir[wire_b].add(wire_b)
            continue

print('#include "chip.h"')

wireinfo = list()

for wire in range(num_wires):
    num_uphill = 0
    num_downhill = 0
    num_bidir = 0

    has_bel_uphill = False
    num_bels_downhill = 0

    if wire in wire_uphill:
        num_uphill = len(wire_uphill[wire])
        print("static WireDelayPOD wire%d_uphill[] = {" % wire)
        print(",\n".join(["  {%d, 1.0}" % other_wire for other_wire in wire_uphill[wire]]))
        print("};")

    if wire in wire_downhill:
        num_downhill = len(wire_downhill[wire])
        print("static WireDelayPOD wire%d_downhill[] = {" % wire)
        print(",\n".join(["  {%d, 1.0}" % other_wire for other_wire in wire_downhill[wire]]))
        print("};")

    if wire in wire_bidir:
        num_bidir = len(wire_bidir[wire])
        print("static WireDelayPOD wire%d_bidir[] = {" % wire)
        print(",\n".join(["  {%d, 1.0}" % other_wire for other_wire in wire_bidir[wire]]))
        print("};")

    info = "  {"
    info += "\"wire%d\", " % wire
    info += "%d, %d, %d, " % (num_uphill, num_downhill, num_bidir)
    info += ("wire%d_uphill, " % wire) if num_uphill > 0 else "nullptr, "
    info += ("wire%d_downhill, " % wire) if num_downhill > 0 else "nullptr, "
    info += ("wire%d_bidir, " % wire) if num_bidir > 0 else "nullptr, "
    info += "}"

    wireinfo.append(info)

print("int num_wires_%s = %d;" % (dev_name, num_wires))
print("WireInfoPOD wire_data_%s[%d] = {" % (dev_name, num_wires))
print(",\n".join(wireinfo))
print("};")