🏁 Flags are a bit more proper and added compare

This commit is contained in:
zombie maniac 2021-02-03 03:19:59 -05:00
parent d6cd7db0b8
commit 67299b55b5
2 changed files with 25 additions and 5 deletions

27
main.py
View file

@ -5,6 +5,8 @@
# program execcution starts at 256 # program execcution starts at 256
#stack starts at end of memory FILO #stack starts at end of memory FILO
#flags
#nil nil nil nil nil nil negative zero
import numpy as np import numpy as np
import opcodes as ops import opcodes as ops
@ -28,12 +30,11 @@ class ZombieCPU:
self._regs.esp = self._memorysize - 1 #set stackpointer self._regs.esp = self._memorysize - 1 #set stackpointer
self._regs.eip = 256 #set instructiojn pointer #fuck you thomas self._regs.eip = 256 #set instructiojn pointer #fuck you thomas
print(self._regs.eip)
def exec(self): def exec(self):
Halted = False Halted = False
while not Halted: #change this to if not halt while not Halted: #change this to if not halt
#fetch #fetch
opcode = self._memory[int(self._regs.eip)] opcode = self._memory[int(self._regs.eip)]
address = self._memory[int(self._regs.eip + 1)] address = self._memory[int(self._regs.eip + 1)]
value = self._memory[int(self._regs.eip + 2)] value = self._memory[int(self._regs.eip + 2)]
@ -59,6 +60,13 @@ class ZombieCPU:
self.write_memory(address, value) self.write_memory(address, value)
if value == 0: if value == 0:
self._regs.flags = self._regs.flags | 0b00000001 self._regs.flags = self._regs.flags | 0b00000001
else:
print('unset flag here (you shouldnt be reading this!) (zero)')
if value < 0:
self._regs.flags = self._regs.flags | 0b00000010
else:
print('unset flag here (you shouldnt be reading this!) (neg)')
elif opcode == ops.PUSH: #push elif opcode == ops.PUSH: #push
self.write_memory(self._regs.esp, value) #put value onto stack self.write_memory(self._regs.esp, value) #put value onto stack
@ -75,6 +83,13 @@ class ZombieCPU:
if self._regs.flags & 0b00000001: if self._regs.flags & 0b00000001:
self._regs.eip = address - 3 self._regs.eip = address - 3
elif opcode == ops.CMP:
if address - value == 0:
self._regs.flags = self._regs.flags | 0b00000001 #set zero flag
else:
print('unset flag here (you shouldnt be reading this!) (zero)')
print(self._regs.flags)
self._regs.eip += 3 #increment instruction pointer self._regs.eip += 3 #increment instruction pointer
@ -86,6 +101,8 @@ class ZombieCPU:
return self._memory return self._memory
def read_memory_address(self, address): def read_memory_address(self, address):
return self._memory[int(address)] return self._memory[int(address)]
def read_flags(self, i):
return self._regs
if __name__ == "__main__": if __name__ == "__main__":
cpu = ZombieCPU(65536, False) cpu = ZombieCPU(65536, False)
@ -101,8 +118,10 @@ if __name__ == "__main__":
cpu.write_memory(262, ops.POP) #pop cpu.write_memory(262, ops.POP) #pop
cpu.write_memory(263, 100) #into 100 cpu.write_memory(263, 100) #into 100
cpu.write_memory(264, 0) #unused in this op cpu.write_memory(264, 0) #unused in this op
cpu.write_memory(265, ops.HLT) #jmp cpu.write_memory(265, ops.CMP)
cpu.write_memory(266, 10)
cpu.write_memory(267, 11) #should set the zero flag
cpu.write_memory(268, ops.HLT)
cpu.exec() cpu.exec()

View file

@ -6,4 +6,5 @@ LDI = 4 # Load Immediate
PUSH = 5 # Push PUSH = 5 # Push
POP = 6 # Pop POP = 6 # Pop
JMP = 7 # Jump to address JMP = 7 # Jump to address
JNZ = 8 # Jump to address if not zero JZ = 8 # Jump to address if zero
CMP = 9 # Compare and set zero flag