summaryrefslogtreecommitdiffstats
path: root/src/bus.py
blob: 5197e72436c1dbc3dc67604b8a8d182ec1573bfc (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import sys

from tpu import *

# connect up parts to a bus.. layouting not important

# we designate X0 Y0 as the input port from the bus
# we designate X1 Y0 as the output port to the bus

bus_upper_out_port = """
start:
    mov RIGHT, ACC
    jne {addr}, fwd
    mov {addr}, DOWN
    mov RIGHT, DOWN
    mov RIGHT, DOWN
    mov RIGHT, DOWN
    mov RIGHT, DOWN
    jmp start
fwd:
    mov ACC, LEFT
    mov RIGHT, LEFT
    mov RIGHT, LEFT
    mov RIGHT, LEFT
    mov RIGHT, LEFT
"""

bus_lower_out_port = """
start:
    mov ANY, ACC
    jne {addr}, fwd
    mov ANY, DOWN
    mov ANY, DOWN
    mov ANY, DOWN
    mov ANY, DOWN
    jmp start
fwd:
    mov ACC, RIGHT
    mov LEFT, RIGHT
    mov LEFT, RIGHT
    mov LEFT, RIGHT
    mov LEFT, RIGHT
"""

bus_lower_in_port = """
start:
    mov ANY, ACC
    sav
    mov ANY, ACC
    jne {addr}, fwd
    swp
    mov ACC, UP
    mov ACC, RIGHT
    mov {addr}, UP
    mov {addr}, RIGHT
    mov DOWN, ACC
    mov ACC, UP
    mov ACC, RIGHT
    mov DOWN, ACC
    mov ACC, UP
    mov ACC, RIGHT
    mov DOWN, ACC
    mov ACC, UP
    mov ACC, RIGHT
    jmp start
fwd:
    swp
    mov ACC, RIGHT
    swp
    mov ACC, RIGHT
    mov LEFT, RIGHT
    mov LEFT, RIGHT
    mov LEFT, RIGHT
"""

bus_lower_rail = """
mov ANY, RIGHT
"""

bus_upper_rail = """
mov ANY, LEFT
"""

bus_rail_end = """
mov ANY, NIL
"""

def does_collide(main, offx, part):
    for (x,y),_ in part["tpus"].items():
        if (x+offx,y) in main["tpus"]:
            return True
    dirs = {"UP": (0, -1), "RIGHT": (1, 0), "DOWN": (0, 1), "LEFT": (-1, 0)}
    for port in ("stdin", "stdout"):
        if port in part:
            x,y,d = part[port]
            dx,dy = dirs[d]
            if (x+offx+dx,y+dy) in main["tpus"]:
                return True
        if port in main:
            x,y,d = main[port]
            dx,dy = dirs[d]
            if (x+dx-offx,y+dy) in part["tpus"]:
                return True
    return False

def add_part(main, main_maxx, bus_ports, part):
    portx = main_maxx
    while portx in bus_ports or portx+1 in bus_ports or does_collide(main, portx, part):
        portx += 1
    bus_ports[portx] = ("in", part["addr"])
    bus_ports[portx+1] = ("out", part["addr"])
    for (x,y),ls in part["tpus"].items():
        main["tpus"][(x+portx,y)] = ls
    if "stdin" in part:
        assert("stdin" not in main)
        x,y,d = part["stdin"]
        main["stdin"] = (portx+x,y,d)
    if "stdout" in part:
        assert("stdout" not in main)
        x,y,d = part["stdout"]
        main["stdout"] = (portx+x,y,d)
    return portx + 4

def tolines(asm):
    return asm.strip("\n").split("\n")

def apply(part, transforms):
    old_tpus = part["tpus"]
    part["tpus"] = TPUDict({})
    for (x,y),ls in old_tpus.items():
        nx = transforms[0](x, y)
        ny = transforms[1](x, y)
        part["tpus"][(nx,ny)] = ls

def rotate(part, r90=0):
    dirs = ("UP", "RIGHT", "DOWN", "LEFT")
    transforms = (
        (lambda x,_: x, lambda _,y: y),
        (lambda _,y: -y, lambda x,_: x),
        (lambda x,_: -x, lambda _,y: -y),
        (lambda _,y: y, lambda x,_: -x),
    )
    apply(part, transforms[r90])
    for _,ls in part["tpus"].items():
        for li,l in enumerate(ls):
            for d in dirs:
                l = l.replace(d, d.upper())
            for i,d in enumerate(dirs):
                nd = dirs[(i+r90)%len(dirs)]
                l = l.replace(d, nd.lower())
            for d in dirs:
                l = l.replace(d.lower(), d)
            ls[li] = l

def add_ports(main, bus_ports):
    for x,(dir,addr) in bus_ports.items():
        dir,addr = bus_ports[x]
        if dir == "in":
            main["tpus"][(x,-2)] = tolines(bus_upper_out_port.format(addr=addr))
            main["tpus"][(x,-1)] = tolines(bus_lower_out_port.format(addr=addr))
        else:
            main["tpus"][(x,-2)] = tolines(bus_upper_rail)
            main["tpus"][(x,-1)] = tolines(bus_lower_in_port.format(addr=addr))

def add_rail(main, bus_ports):
    bus_start = min(bus_ports)
    bus_end = max(bus_ports) + 1
    for x in range(bus_start, bus_end + 1):
        if x not in bus_ports:
            main["tpus"][(x,-2)] = tolines(bus_upper_rail)
            main["tpus"][(x,-1)] = tolines(bus_lower_rail)
    main["tpus"][(bus_start-1,-1)] = tolines(bus_rail_end)
    main["tpus"][(bus_start-1,-2)] = tolines(bus_rail_end)
    main["tpus"][(bus_end+1,-1)] = tolines(bus_rail_end)
    main["tpus"][(bus_end+1,-2)] = tolines(bus_rail_end)

if __name__ == "__main__":
    main = { "tpus": TPUDict({}) }
    files = sys.argv[2:]
    if "--" in files:
        split = files.index("--")
    else:
        split = None

    bus_ports = {}
    main_maxx = 0
    for arg in files[:split]:
        main_maxx = add_part(main, main_maxx,
            bus_ports, parse(open(arg).read()))
    add_ports(main, bus_ports)

    if split is not None:
        rotate(main, 2)
        apply(main, (lambda x,_: x, lambda _,y: y-3))

        all_ports = {-k:v for k,v in bus_ports.items()}
        main_maxx = min(all_ports)
        for arg in files[split+1:]:
            main_maxx = add_part(main, main_maxx,
                all_ports, parse(open(arg).read()))
        add_ports(main, {k:v for k,v in all_ports.items() if -k not in bus_ports})
        add_rail(main, all_ports)
    else:
        add_rail(main, bus_ports)

    write(main, sys.argv[1])