forked from bloominstituteoftechnology/Computer-Architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_example.py
144 lines (127 loc) · 2.75 KB
/
class_example.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
PRINT_TIM = 0b00000001
HALT = 0b00000010
PRINT_NUM = 0b00000011 # a 2-byte command, takes 1 argument
SAVE = 0b00000100 # a 3-byte command, takes 2 arguments
PRINT_REG = 0b00000101
PRINT_SUM = 0b00000110
# a data-driven machine
# function call
# a "variable" == registers for our programs to save things into
# RAM
memory = [
PRINT_TIM,
PRINT_TIM,
PRINT_NUM, # print 99 or some other number
42,
SAVE, # save 99 into register 2 # <-- PC
99, # the number to save
2, # the register to put it into
PRINT_REG,
2,
PRINT_SUM, # R1 + R2
HALT,
]
# registers, R0-R7
registers = [0] * 8
running = True
# program counter
pc = 0
while running:
command = memory[pc]
if command == PRINT_TIM:
print('tim!')
elif command == PRINT_NUM:
num_to_print = memory[pc + 1] # we already incremented PC!
print(num_to_print)
pc += 1 # but increment again
elif command == SAVE:
num_to_save = memory[pc + 1]
register_address = memory[pc + 2]
registers[register_address] = num_to_save
# shorter:
# registers[memory + 2] = memory[pc + 1]
pc += 2
elif command == PRINT_REG:
reg_address = memory[pc + 1]
saved_number = registers[reg_address]
print(saved_number)
# print(registers[memory[pc + 1]])
elif command == HALT:
running = False
number
pc += 1 # so we don't get sucked into an infinite loop!
# masking
# >> and use & with ob1 to essentially delete any higher bits
ob1010
& ob0000
------
0b1010
& 0b0011 zeros where you don't care, ones where you do
------
0b0010
shift to the right, then masking
v
10100000 >> 5
101
00000101
00000101
&00000001
--------
00000001
# LOADING STUFF
# file i/o in python
try:
if len(sys.argv) < 2:
print(f'error from {sys.argv[0]}: {sys.argv[1]} not found')
sys.exit(1)
# add a counter that loads memory at that index
ram_index = 0
with open(file_name, sys.argv[1]) as f:
for line in f:
print(line.split("#")[0])
print(split_line.strip())
# how to only get the numbers
if stripped_split_line != "":
print(stripped_split_line)
command = int(stripped_split_line, 2)
# loead command into memory
memory[ram_index] = command
ram_index += 1
except FileNotFoundError:
print(f"could not find that file {sys.argv[1]}")
file = open("print8.ls8", 'r')
lines = file.read()
print(lines)
# make sure you close files
file.close()
# read in filename from command line
memory = [0] * 256
elif command = ADD:
reg1_address = memory[pc + 1]
reg2_address = memory[pc + 2]
registers[reg1_address] += registers[reg2_address]