Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions class3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

Computer arch topics we have implemented in our project
- RAM, self.ram
- registers!
- CPU, self.run
- ALU
Why LS8?
- 8 bits
- not the registers, as you might think
- Also, 8-bit CPU and ALU architectures are those that are based on registers,
address buses, or data buses of that size
CPU
00000000
00000000
00000000
00000000
RAM
- with 8-bit address bus, the CPU can address 256 bytes of RAM
General purpose calculating machine vs specialized calculating machines
"computer" "calculator"
Can do anything hardwired for specific calculations
Broadly applicable Faster (often)
More memory? Stack!
- more variables
- function calls and nested function calls
To make a stack?
- Push
- Pop
- Memory space
-- RAM
-- 0-255 (256 bytes)
- a pointer to track where the top of the stack
-- variable that is a memory address
how to push
how to Pop
Handling 'leftovers' from push
Stack underflow
Why doesn't the CPU prevent underflow, or prevent wrapping around in memory?
- don't want the CPU to spend time/energy checking
- used to be dev's job, now it's the compiler's job
Stack overflow
- software's job to prevent this
Stacks and nested function calls
self.ram = [0] * 256
registers[7] = F3
SP = F3
memory[SP]
FF: 00
FE: 00
FD: 00
FC: 00
FB: 00
FA: 00
F9: 00
F8: 00
F7: 00
F6: 00
F5: 00
F4: 00
F3: 42 <--- SP
F2: 42
F1: 42
F0: 00
EF: 00
EE: 00
ED: 00
EC: 00
.
.
.
2B: 42
2A: 42
.
.
.
10: 42
9: 42
8: 42
7: JUMP
6: R3
5: PUSH
4: R3
3: 42
2: SAVE
1: PRINT_TIM
0: PRINT_TIM
R0: 99
R1: 42
R3: 42
PUSH R0:
- decrement the SP
- copy the value from the given register
PUSH R1:
- decrement the SP
- copy the value from the given register
POP R3:
- copy into the register
- increment SP
PUSH R0:
- decrement the SP
- copy the value from the given register
POP R3:
- copy into the register
- increment SP
Stack
700: 4
699: 2
698: 3
697: 6
696: 6
695: 7 <-- SP
694: 3
693: 6
registers[4] = 6
a = 4
def mult(x, y):
z = x * y
return z
def main():
a = 2
b = 3
c = a * b
d = mult(a, b)
e = 7
144 changes: 144 additions & 0 deletions class_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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]
Loading