Skip to content

Instantly share code, notes, and snippets.

@pepsipu
Created October 14, 2020 23:41
Show Gist options
  • Save pepsipu/6417ae9e3a274e356d81ff82359533e5 to your computer and use it in GitHub Desktop.
Save pepsipu/6417ae9e3a274e356d81ff82359533e5 to your computer and use it in GitHub Desktop.
from ram import RAM
# 0x00 -> add one to register 0
# 0x01 -> add one to register 1
# 0x09 -> register 0 = register 0 + register 1
# 0xde 0x?? -> load the value at 0x?? in memory into register 0
# 0xed 0x?? 0x!! -> store 0x!! at 0x?? in memory
# 0xee 0x?? 0xrr -> store registers[0xrr] at 0x?? in memory
# 0xde 0xde 0x00 0x00 0x01 0x01 0x09 0xed 0x00 0x01
class CPU:
ram = RAM()
registers = [0] * 4
ip = 0
def step():
# fetch instruction
instruction = ram.read(ip)
# decoding the instruction
if instruction == '\x00':
# execute
register[0] += 1
if instruction == '\x01':
# execute
register[1] += 1
if instruction == '\x09':
# execute
register[0] += register[1]
if instruction == '\xde':
ip += 1
index = ram.read(ip)
register[0] = index
if instruction == '\xed':
ip += 1
index = ram.read(ip)
ip += 1
value = ram.read(ip)
ram.write(value, index)
if instruction == '\xee':
ip += 1
index = ram.read(ip)
ip += 1
register_index = ram.read(ip)
ram.write(registers[register_index], index)
ip += 1
bytes_of_memory = 128
class RAM:
memory = [0] * bytes_of_memory
def write(self, what, where):
self.memory[where] = what
def read(self, where):
return self.memory[where]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment