-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus.py
309 lines (254 loc) · 6.67 KB
/
bus.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import RPi.GPIO as GPIO
import sys
from time import sleep
memory = [0]*65536
end_code = 0
def code8(byte):
global end_code
memory[end_code] = byte
end_code += 1
def code16(addr):
code8(addr >> 8)
code8(addr & 0xFF)
def mov(src, dst, jmp=None):
if jmp is None:
jmp = end_code + 6
code16(jmp)
code16(src)
code16(dst)
def mov_indir8(src, offset, dst, jmp=None):
mov(offset, end_code + 6 + 2 + 1)
mov(src, dst, jmp)
def mov_indir16(hi, lo, dst, jmp=None):
mov(hi, end_code + 6*2 + 2)
mov(lo, end_code + 6 + 2 + 1)
mov(0, dst, jmp)
def merge4(hi, lo, dst, jmp=None):
mov_indir8(merge_hi, hi, merge4_t)
mov_indir16(merge4_t, lo, dst, jmp)
def rs4(src, dst, jmp=None):
mov_indir8(rs4_data, src, dst, jmp)
def add4(a, dst, jmp=None):
mov_indir8(sum4, a, dst, jmp)
def add4_lo(a, b, dst, jmp=None):
merge4(a, b, add4_lo_t)
add4(add4_lo_t, dst, jmp)
def add4_hi(a, b, dst, jmp=None):
rs4(a, add4_hi_a)
rs4(b, add4_hi_b)
add4_lo(add4_hi_a, add4_hi_b, dst, jmp)
def add8(a, b, dst, jmp=None):
add4_lo(a, b, add8_lo)
rs4(add8_lo, add8_carry)
add4_hi(a, b, add8_hi_nocarry)
add4_lo(add8_carry, add8_hi_nocarry, add8_hi)
merge4(add8_hi, add8_lo, dst, jmp)
def cond8(test, t, f, dst, jmp=None):
mov(t, cond8_t)
mov(f, cond8_f)
mov_indir8(cond8_data, test, cond8_lo)
mov_indir8(cond8_f & 0xFF00, cond8_lo, dst, jmp)
def br8(test, t_jmp, f_jmp=None):
if f_jmp is None:
f_jmp = end_code + 78
cond8(test, identity+(t_jmp>>8), identity+(f_jmp>>8), end_code + 72)
cond8(test, identity+(t_jmp&0xFF), identity+(f_jmp&0xFF), end_code + 36 + 1)
mov(0, 0, 0)
data = 65536
data -= 1
output = data
data -= 4095
data -= 4096
merge = data
for hi in range(16):
for lo in range(256):
memory[merge + (hi << 8 | lo)] = (hi << 4) | (lo & 0x0F)
data -= 256
merge_hi = data
for i in range(256):
memory[merge_hi + i] = ((merge >> 8) & 0xF0) | (i & 0x0F)
data -= 256
identity = data
for i in range(256):
memory[identity + i] = i
data -= 256
rs4_data = data
for i in range(256):
memory[rs4_data + i] = i >> 4
data -= 256
sum4 = data
for i in range(16):
for j in range(16):
memory[sum4 + i*16 + j] = i + j
data -= 256
cond8_data = data
data -= 1
cond8_t = data
data -= 1
cond8_f = data
data -= 1
cond8_lo = data
assert cond8_t >> 8 == cond8_f >> 8
assert cond8_t & 0xFF != cond8_f & 0xFF
memory[cond8_data] = cond8_f & 0xFF
for i in range(1, 256):
memory[cond8_data + i] = cond8_t & 0xFF
data -= 1
merge4_t = data
data -= 1
add4_lo_t = data
data -= 1
add4_hi_a = data
data -= 1
add4_hi_b = data
data -= 1
add8_lo = data
data -= 1
add8_carry = data
data -= 1
add8_hi_nocarry = data
data -= 1
add8_hi = data
data -= 1
count = data
data -= 1
prev = data
data -= 1
cur = data
data -= 1
next_ = data
for m in memory:
assert 0 <= m < 256
mov(identity+0, prev)
mov(prev, output)
mov(identity+1, cur)
mov(cur, output)
mov(identity+12, count)
loop = end_code
add8(prev, cur, next_)
mov(cur, prev)
mov(next_, cur)
mov(cur, output)
add8(identity+0xFF, count, count)
br8(count, loop)
done = end_code
mov(0, 0, done)
GPIO.setmode(GPIO.BCM)
cs = 0
sio = 1
sclk = 2
hclk = 3
bcs = 4
bsi = 5
rcs = 6
GPIO.setup([cs, sio], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup([sclk,hclk,bsi], GPIO.OUT, initial=0)
GPIO.setup(bcs, GPIO.OUT, initial=1)
GPIO.setup(rcs, GPIO.IN,pull_up_down=GPIO.PUD_UP)
num_counts = 0
def tick_lo():
global num_counts
num_counts += 1
if len(sys.argv) >= 2:
sleep(float(sys.argv[1]))
GPIO.output(sclk, 0)
GPIO.output(hclk, 0)
def tick_hi():
if len(sys.argv) >= 2:
sleep(float(sys.argv[1]))
GPIO.output(sclk, 1)
GPIO.output(hclk, 1)
def tick():
tick_lo()
tick_hi()
def init():
GPIO.output(bcs, 0)
for _ in range(6):
tick_lo()
GPIO.output(bsi, 0)
tick_hi()
for _ in range(2):
tick_lo()
GPIO.output(bsi, 1)
tick_hi()
for _ in range(8):
tick_lo()
GPIO.output(bsi, 0)
tick_hi()
def cycle():
try:
is_bootstrap = True
init()
while True:
while True:
tick()
if not GPIO.input(cs):
break
if GPIO.input(sio):
#print('Write')
for _ in range(24):
tick()
addr = ''
for _ in range(16):
tick()
addr += '1' if GPIO.input(sio) else '0'
addr = int(addr, 2)
value = ''
while True:
tick()
if GPIO.input(cs):
break
value += '1' if GPIO.input(sio) else '0'
assert len(value) % 8 == 0
value = [int(value[n:n+8], 2) for n in range(0, len(value), 8)]
for v in value:
#print("Addr: {} Value: {}".format(addr, v))
if addr == output:
print("Output: {}".format(v))
memory[addr] = v
addr += 1
else:
#print('Read')
for _ in range(24):
tick()
addr = ''
for _ in range(16):
tick()
addr += '1' if GPIO.input(sio) else '0'
addr = int(addr, 2)
for _ in range(25):
tick()
is_first = True
while True:
if GPIO.input(cs):
GPIO.setup(sio, GPIO.IN)
break
if is_bootstrap:
value = 0
else:
value = memory[addr]
#print('Addr: {} Value: {}'.format(addr, value))
value = '{:08b}'.format(value)
assert len(value) == 8
assert not GPIO.input(cs)
tick_lo()
if is_first:
GPIO.setup(sio, GPIO.OUT, initial=int(value[0]))
is_first = False
else:
GPIO.output(sio, int(value[0]))
tick_hi()
for v in value[1:]:
if GPIO.input(cs):
break
tick_lo()
GPIO.output(sio, int(v))
tick_hi()
addr += 1
is_bootstrap = 0
finally:
print(num_counts)
tick_lo()
GPIO.output(bcs, 1)
GPIO.cleanup()
cycle()