-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
431 lines (369 loc) · 13.8 KB
/
main.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from program_counter import ProgramCounter
from ram import RAM
from adder import Adder
from alu import Alu
from and_gate import AndGate
from buffer import Buffer
from data_latch import DataLatch
from dynamic_adder import DynamicAdder
from dynamic_register import DynamicRegister
from eight_bit_adder import EightBitAdder
from eight_bit_register import EightBitRegister
from four_bit_adder import FourBitAdder
from four_bit_and_gate import FourBitAndGate
from four_bit_register import FourBitRegister
from nand_gate import NandGate
from nor_gate import NorGate
from not_gate import NotGate
from or_gate import OrGate
from sr_latch import SrLatch
from xnor_gate import XnorGate
from xor_gate import XorGate
from bus import Bus
from helper import *
import time
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
pygame.init()
def next_instruction(x):
if(x == 0):
#NOP
pass
elif(x == 1):
#LDA
print("LDA")
bus.update(instructions_register.output[4:])
draw_bus()
ram.address_in(bus)
draw_ram()
print(" IO MI")
tick()
ram.ram_out(bus)
draw_ram(True)
draw_bus()
a_register.register_in(bus)
draw_a_register()
alu.update(a_register.output+b_register.output+[False])
draw_alu()
print(" RO AI")
tick()
elif(x == 2):
print("ADD")
#ADD
bus.update(instructions_register.output[4:])
draw_bus()
ram.address_in(bus)
draw_ram()
print(" IO MI")
tick()
ram.ram_out(bus)
draw_bus()
draw_ram(True)
b_register.register_in(bus)
draw_b_register()
print(" RO BI")
tick()
draw_ram()
alu.sum_out(bus, a_register,b_register,[False])
draw_bus()
draw_alu(True)
a_register.register_in(bus)
draw_a_register()
#alu.update(a_register.output+b_register.output+[False])
print(" AO AI")
tick()
draw_alu()
elif(x == 3):
#SUB
print("SUB")
bus.update(instructions_register.output[4:])
draw_bus()
ram.address_in(bus)
draw_ram()
print(" IO MI")
tick()
ram.ram_out(bus)
draw_bus()
draw_ram(True)
b_register.register_in(bus)
draw_b_register()
print(" RO BI")
tick()
draw_ram()
alu.sum_out(bus, a_register,b_register,[True])
draw_bus()
draw_alu(True)
a_register.register_in(bus)
draw_a_register()
#alu.update(a_register.output+b_register.output+[False])
print(" AO AI")
tick()
draw_alu()
elif(x == 4):
#STA
print("STA")
bus.update(instructions_register.output[4:])
draw_bus()
ram.address_in(bus)
draw_ram()
print(" IO RI")
tick()
a_register.register_out(bus)
#draw a with out signal
ram.ram_in(bus)
draw_ram()
print(" AO RI")
elif(x == 5):
#LDI
print("LDI")
bus.update(instructions_register.output[4:])
draw_bus()
a_register.register_in(bus)
draw_a_register()
print(" IO AI")
tick()
elif(x == 6):
#J
print("J")
bus.update(instructions_register.output[4:])
draw_bus()
pc.jump(bus)
draw_program_counter()
print(" IO J")
elif(x == 7):
#JC
print("JC")
if(alu.carry_flag):
bus.update(instructions_register.output[4:])
draw_bus()
pc.jump(bus)
draw_program_counter()
print(" IO J")
else: print(" NC")
tick()
elif(x == 8):
#JZ
print("JZ")
if(alu.zero_flag):
bus.update(instructions_register.output[4:])
draw_bus()
pc.jump(bus)
draw_program_counter()
print(" IO J")
else: print(" NC")
tick()
elif(x == 14):
#OUT
print("OUT")
print(" ",array_to_decimal(a_register.output))
elif(x == 15):
#HLT
print("HLT")
exit()
def multiply(ram):
ram.registers[0].update(parse_bits("1 0001 00001101"))#lda A
ram.registers[1].update(parse_bits("1 0011 00001111"))#sub 1
ram.registers[2].update(parse_bits("1 0111 00000110"))#jc 6
ram.registers[3].update(parse_bits("1 0001 00001100"))#lda 0
ram.registers[4].update(parse_bits("1 1110 00000000"))#out
ram.registers[5].update(parse_bits("1 1111 00000000"))#hlt
ram.registers[6].update(parse_bits("1 0100 00001101"))#sta A
ram.registers[7].update(parse_bits("1 0001 00001110"))#lda B
ram.registers[8].update(parse_bits("1 0010 00001100"))#add 0
ram.registers[9].update(parse_bits("1 0100 00001100"))#sta 0
ram.registers[10].update(parse_bits("1 0110 00000000"))#j 0
ram.registers[12].update(parse_bits("1 0000 00000000"))#0
ram.registers[13].update(parse_bits("1 0000 00001101"))#A 13
ram.registers[14].update(parse_bits("1 0000 00001001"))#B 9
ram.registers[15].update(parse_bits("1 0000 00000001"))#1
"""
#include <stdio.h>
#define ADD(a, b, carry) a = a + b; carry = !!(a & 0x100); a &= 0xff
#define SUB(a, b, carry) a = a + ((0xff ^ b) + 1); carry = !!(a & 0x100); a &= 0xff
int a; // 8-bit register A
int carry; // carry bit
int M12 = 0; // 8-bit accumulator
int M13 = 20; // input 1
int M14 = 3; // input 2
int M15 = 1; // 8-bit constant 1
while (1) {
// LDA [13]
a = M13;
// SUB [15]
SUB(a, M15, carry);
// JC
if (!carry) {
// LDA [12]
a = M12;
// OUT
printf("OUT: %d\n", a);
// HLT
break;
}
// STA [13]
M13 = a;
// LDA [14]
a = M14;
// ADD [12]
ADD(a, M12, carry);
// STA [12]
M12 = a;
// JMP
}
return 0;
"""
myfont = pygame.font.SysFont("monospace", 15)
text_color = (0,0,0)
def draw_a_register(out = False):
for i in range(len(a_register.output)):
pygame.draw.line(win,(234*a_register.output[i],12,12), (800+i*12,60),(800+i*12,260) )
pygame.draw.rect(win, (200,200,200),(780,60,130,60))
label = myfont.render("A-Register", 1, text_color)
win.blit(label, (795, 75))
for i,j in enumerate(a_register.output):
pygame.draw.circle(win, (234*j,12,12),(800+i*12,100),5)
pygame.draw.line(win, (234*j,0,0),(910,70+i*6),(995-6*i,70+i*6))
def draw_b_register():
for i in range(len(b_register.output)):
pygame.draw.line(win,(234*b_register.output[i],12,12), (800+i*12,460),(800+i*12,330) )
pygame.draw.rect(win, (200,200,200),(780,460,130,60))
label = myfont.render("B-Register", 1, text_color)
win.blit(label, (795, 475))
for i,j in enumerate(b_register.output):
pygame.draw.circle(win, (234*j,12,12),(800+i*12,500),5)
def draw_alu(out = False):
pygame.draw.rect(win, (200,200,200),(765,260,130,70))
label = myfont.render("ALU", 1, text_color)
win.blit(label, (795, 275))
pygame.draw.circle(win, (234*alu.carry_flag,12,12),(778,300),5)
pygame.draw.rect(win, (200,200,200), (680,260,20,70))
for i,j in enumerate(alu.output):
pygame.draw.circle(win, (234*j,12,12),(800+i*12,300),5)
pygame.draw.line(win,(234*j,12,12),(765,270+i*7),(700,270+i*7))
pygame.draw.line(win,(out*234*j,12,12),(679,270+i*7),(680-56+i*7,270+i*7))
pygame.draw.line(win,(out*234*j,12,12),(680-56+i*7,270+i*7),(680-56+i*7,400+i*7))
pygame.draw.line(win,(out*234*j,12,12),(680-56+i*7,400+i*7),(400+(i+4)*12,400+i*7))
pygame.draw.circle(win, (out*234*j,12,12),(400+(i+4)*12,400+i*7),2)
def draw_bus():
pygame.draw.rect(win, (200,200,200),(390,40,150,70))
pygame.draw.rect(win, 0,(390,80,53,30),1)
pygame.draw.rect(win, 0,(390+52,80,99,30),1)
offset = 0
if(len(bus.output)==8):
offset = 4
for i in range(4):
pygame.draw.line(win,0,(400+i*12, 110),(400+i*12, 930))
pygame.draw.circle(win, 0, (400+i*12,100),5)
for i in range(len(bus.output)):
pygame.draw.circle(win, (253*bus.output[i],0,0), (400+(i+offset)*12,100),5)
pygame.draw.line(win,(bus.output[i]*255,0,0),(400+(i+offset)*12, 110),(400+(i+offset)*12, 930))
label = myfont.render("BUS", 1, text_color)
win.blit(label, (400, 40))
label = myfont.render("Instr.", 1, text_color)
win.blit(label, (390, 80))
label = myfont.render("Data", 1, text_color)
win.blit(label, (460, 80))
for i in range(8):
pygame.draw.line(win, (bus.output[len(bus.output)-1-i]*253,0,0),(780,70+i*6),(700+i*6,70+i*6))
pygame.draw.line(win, (bus.output[len(bus.output)-1-i]*253,0,0),(700+i*6,70+i*6),(700+i*6,140+i*6+60))
pygame.draw.line(win, (bus.output[len(bus.output)-1-i]*253,0,0),(700+i*6,140+i*6+60),(400+(11-i)*12,140+i*6+60))
pygame.draw.circle(win, (bus.output[len(bus.output)-1-i]*253,0,0), (400+(11-i)*12,140+i*6+60),2)
#B Register
pygame.draw.line(win, (bus.output[len(bus.output)-1-i]*253,0,0),(780,470+i*6),(400+(11-i)*12,470+i*6))
pygame.draw.circle(win, (bus.output[len(bus.output)-1-i]*253,0,0), (400+(11-i)*12,470+i*6),2)
#RAM
pygame.draw.line(win, (bus.output[len(bus.output)-1-i]*253,0,0), (250,120+i*7),(580-(i+4)*12,120+i*7))
pygame.draw.circle(win, (bus.output[len(bus.output)-1-i]*253,0,0),(580-(i+4)*12,120+i*7),2)
def draw_ram(out=False):
pygame.draw.rect(win, (200,200,200),(100,110,150,70))
pygame.draw.rect(win, (200,200,200),(100,250,150,70))
label = myfont.render("Memory Address", 1, text_color)
win.blit(label, (100, 110))
label = myfont.render(
f"Dec:{str(array_to_decimal(ram.address_memory.output))}",
1,
text_color,
)
win.blit(label, (100, 150))
for i in range(8):
pygame.draw.circle(win, (ram.address_memory.output[i]*253,0,0), (107+i*12,140),5)
pygame.draw.circle(win, (ram.registers[array_to_decimal(ram.address_memory.output)].output[i+4]*253,0,0), (107+i*12,290),5)
pygame.draw.line(win, (ram.address_memory.output[i]*253,0,0),(107+i*12,180),(107+i*12,249))
#pygame.draw.line(win, (ram.registers[array_to_decimal(ram.address_memory.output)].output[4+i]*253,0,0), (250,260+i*7),(400+(i+4)*12,260+i*7))
pygame.draw.line(win,(ram.registers[array_to_decimal(ram.address_memory.output)].output[4+i]*253,0,0),(250,260+i*7),(300,260+i*7))
pygame.draw.line(win,(out*ram.registers[array_to_decimal(ram.address_memory.output)].output[4+i]*253*out,0,0),(320,260+i*7),(400+(i+4)*12,260+i*7))
pygame.draw.rect(win,(200,200,200),(300,250,20,70))
pygame.draw.circle(win, (out*ram.registers[array_to_decimal(ram.address_memory.output)].output[4+i]*253,0,0),(400+(i+4)*12,260+i*7),2)
label = myfont.render("Data", 1, text_color)
win.blit(label, (100, 250))
label = myfont.render(
f"Dec:{str(array_to_decimal(ram.registers[array_to_decimal(ram.address_memory.output)].output))}",
1,
text_color,
)
win.blit(label, (100, 300))
def draw_program_counter(out=False):
pygame.draw.rect(win, (200,200,200),(100,850,150,70))
pygame.draw.rect(win,(200,200,200),(300,850,20,70))
for i in range(8):
pygame.draw.circle(win, (pc.counter.output[i]*253,0,0), (107+i*12,890),5)
pygame.draw.line(win,(pc.counter.output[i]*253,0,0),(250,860+i*7),(300,860+i*7))
pygame.draw.line(win,(out*pc.counter.output[i]*253*out,0,0),(320,860+i*7),(400+(i+4)*12,860+i*7))
pygame.draw.circle(win, (out*pc.counter.output[i]*253,0,0),(400+(i+4)*12,860+i*7),2)
label = myfont.render("Program Counter", 1, text_color)
win.blit(label, (100, 850))
label = myfont.render(
f"Dec:{str(array_to_decimal(pc.counter.output))}", 1, text_color
)
win.blit(label, (100, 900))
#TODO ram input output check
TICK = False
def tick(x = 0.05):
return
if __name__ == "__main__":
win = pygame.display.set_mode((1000,1000))
pygame.display.set_caption("Racing game")
clock = pygame.time.Clock()
#--------components---------#
bus = Bus(8,[])
pc = ProgramCounter()
pc.counter.update([True]+bus.output)
ram = RAM([])
multiply(ram)
ram.address_in(bus)
instructions_register = DynamicRegister(12,[])
a_register = DynamicRegister(8, parse_bits("1 00000000"))
b_register = DynamicRegister(8, parse_bits("1 00000000"))
alu = Alu(8)
alu.update(a_register.output+b_register.output+[False])
while True:
win.fill((125,124, 110))
#fetch instructions from memory
draw_a_register()
draw_b_register()
draw_alu()
draw_bus()
draw_ram()
draw_program_counter()
print("FETCH")
pc.counter_out(bus)
ram.address_in(bus)
draw_program_counter(True)
draw_bus()
draw_ram()
print(" CO MI")
tick()
draw_program_counter()
ram.ram_out(bus)
draw_ram(True)
draw_bus()
instructions_register.update([True]+bus.output)
pc.count_enable()
draw_program_counter()
print(" RO II CE")
#draw instrctions register
tick()
draw_ram()
next_instruction(array_to_decimal(instructions_register.output[:4]))
pygame.display.flip()