Skip to content
Open
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
38 changes: 36 additions & 2 deletions ls8/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

import sys

# op_code, constants (do not need to be an instance variable)
HLT = 0b00000001
LDI = 0b10000010
PRN = 0b01000111


class CPU:
"""Main CPU class."""

def __init__(self):
"""Construct a new CPU."""
pass
self.registers = [0] * 8
self.registers[7] = 0xF4
self.pc = 0
self.ram = [0] * 256
self.halted = False

def load(self):
"""Load a program into memory."""
Expand Down Expand Up @@ -60,6 +70,30 @@ def trace(self):

print()

def ram_read(self, address):
return self.ram[address]

def ram_write(self, value, address):
self.ram[address] == value

def run(self):
"""Run the CPU."""
pass
while not self.halted:
instruction_to_execute = self.ram_read(self.pc)
operand_a = self.ram_read(self.pc + 1)
operand_b = self.ram_read(self.pc + 2)
self.execute_instruction(instruction_to_execute, operand_a, operand_b)

def execute_instruction(self, instruction, operand_a, operand_b):
if instruction == HLT:
self.halted = True
self.pc += 1
elif instruction == PRN:
print(self.registers[operand_a])
self.pc +=2
elif instruction == LDI:
self.registers[operand_a] = operand_b
self.pc +=3
else:
print("ahhhhh, I don't know what do to")
pass