-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lc3.py
35 lines (26 loc) · 777 Bytes
/
lc3.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
from control_unit import execute
from traps import Halt
from memory import mem_read, mem_write, reg_read, reg_write, Registers, load_image
import array
import sys
PC_START = 0x3000
def read_Rom_file(name):
with open(name, 'br') as image:
load_image(image)
def main():
if len(sys.argv) < 2:
print('Usage: python3 lc3.py [obj-file]')
exit(2)
reg_write(Registers.PC, PC_START)
read_Rom_file(sys.argv[1])
try:
while True:
pc = reg_read(Registers.PC)
instruction = mem_read(pc)
# increment PC by #1.
reg_write(Registers.PC, reg_read(Registers.PC) + 1)
execute(instruction)
except Halt: # TODO
print('Halted.')
if __name__ == "__main__":
main()