Get simulator to run a few instructions
This commit is contained in:
parent
7e842bd7a6
commit
61361c5f3f
81
src/cpu.mpy
81
src/cpu.mpy
|
|
@ -1,3 +1,5 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
class CPU:
|
class CPU:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
@ -9,41 +11,82 @@ class CPU:
|
||||||
|
|
||||||
def load_memory(self, bytes):
|
def load_memory(self, bytes):
|
||||||
self.memory = bytes + bytearray(256 - len(bytes))
|
self.memory = bytes + bytearray(256 - len(bytes))
|
||||||
print(self.memory)
|
print('mem 254', self.memory[254])
|
||||||
|
# print(self.memory)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.running = True
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
if self.IP >= 256:
|
if self.IP >= 256:
|
||||||
self.IP = 0
|
self.IP = 0
|
||||||
print(self.IP)
|
print("IP:", self.IP)
|
||||||
self.instruction['opcode'] = self.memory[self.IP]
|
self.instruction['opcode'] = self.memory[self.IP]
|
||||||
self.IP = self.IP+1
|
self.IP = self.IP+1
|
||||||
self.instruction['operand'] = self.memory[self.IP]
|
self.instruction['operand'] = self.memory[self.IP]
|
||||||
self.IP = self.IP+1
|
self.IP = self.IP+1
|
||||||
print(self.instruction)
|
print("instr:", self.instruction['opcode'], self.instruction['operand'])
|
||||||
|
print("mnem:", self.nums2mnems[self.instruction['opcode']])
|
||||||
nums2mnems = {
|
self.nums2mnems[self.instruction['opcode']](self, self.instruction['operand'])
|
||||||
'hlt': '00',
|
print("acc:", self.acc)
|
||||||
'nop': '01',
|
print("running:", self.running)
|
||||||
'add_lit': '04'
|
print()
|
||||||
}
|
if not self.running:
|
||||||
|
sys.exit()
|
||||||
def hlt(self):
|
|
||||||
self.running = false
|
|
||||||
|
|
||||||
def nop(self):
|
def hlt(self, operand):
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
def nop(self, operand):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def add_lit(num):
|
def add_lit(self, operand):
|
||||||
self.acc = self.acc + num
|
self.acc = self.acc + operand
|
||||||
|
|
||||||
|
def add_mem(self, operand):
|
||||||
|
self.acc = self.acc + self.memory[operand]
|
||||||
|
|
||||||
|
def jmp_lit(self, operand):
|
||||||
|
self.IP = operand
|
||||||
|
|
||||||
|
nums2mnems = {
|
||||||
|
0: hlt,
|
||||||
|
1: nop,
|
||||||
|
2: 'lda_lit',
|
||||||
|
3: 'sta_lit',
|
||||||
|
4: add_lit,
|
||||||
|
5: 'sub_lit',
|
||||||
|
6: jmp_lit,
|
||||||
|
7: 'ske',
|
||||||
|
8: 'skz',
|
||||||
|
9: 'skn',
|
||||||
|
10: 'skc',
|
||||||
|
11: 'cst',
|
||||||
|
12: 'ccl',
|
||||||
|
16: 'hlt',
|
||||||
|
17: 'nop',
|
||||||
|
18: 'lda_mem',
|
||||||
|
19: 'sta_mem',
|
||||||
|
20: add_mem,
|
||||||
|
21: 'sub_mem',
|
||||||
|
22: 'jmp_mem',
|
||||||
|
23: 'ske',
|
||||||
|
24: 'skz',
|
||||||
|
25: 'skn',
|
||||||
|
26: 'skc',
|
||||||
|
27: 'cst',
|
||||||
|
28: 'ccl',
|
||||||
|
}
|
||||||
|
|
||||||
cpu = CPU()
|
cpu = CPU()
|
||||||
print(cpu)
|
|
||||||
print(cpu.running)
|
|
||||||
|
|
||||||
prog = '04 01 14 01 00 00'
|
prog = '04 01 14 01 00 00'
|
||||||
b = bytes.fromhex(prog)
|
program_bytes = bytes.fromhex(prog)
|
||||||
cpu.load_memory(b)
|
# Add jmp at addr 254:
|
||||||
|
program_with_jump = program_bytes + bytearray(254 - len(program_bytes)) + bytes.fromhex('06 00')
|
||||||
|
cpu.load_memory(program_with_jump)
|
||||||
|
|
||||||
|
cpu.start()
|
||||||
cpu.step()
|
cpu.step()
|
||||||
cpu.step()
|
cpu.step()
|
||||||
cpu.step()
|
cpu.step()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue