Merge old work on laptop with new python work

This commit is contained in:
n loewen 2025-02-28 10:52:10 +00:00
commit 7e842bd7a6
1 changed files with 49 additions and 0 deletions

49
src/cpu.mpy Normal file
View File

@ -0,0 +1,49 @@
class CPU:
def __init__(self):
self.running = False
self.IP = 254
self.acc = 0
self.flags = { 'C': False, 'Z': False, 'N': False, 'Eq': False }
self.instruction = { 'opcode': False, 'operand': False }
self.memory = False
def load_memory(self, bytes):
self.memory = bytes + bytearray(256 - len(bytes))
print(self.memory)
def step(self):
if self.IP >= 256:
self.IP = 0
print(self.IP)
self.instruction['opcode'] = self.memory[self.IP]
self.IP = self.IP+1
self.instruction['operand'] = self.memory[self.IP]
self.IP = self.IP+1
print(self.instruction)
nums2mnems = {
'hlt': '00',
'nop': '01',
'add_lit': '04'
}
def hlt(self):
self.running = false
def nop(self):
pass
def add_lit(num):
self.acc = self.acc + num
cpu = CPU()
print(cpu)
print(cpu.running)
prog = '04 01 14 01 00 00'
b = bytes.fromhex(prog)
cpu.load_memory(b)
cpu.step()
cpu.step()
cpu.step()