Added increment ↗️ and decrement ↘️ and fixed flags

This commit is contained in:
zombie maniac 2021-02-03 16:55:54 -05:00
parent 67299b55b5
commit 1683ca40a7
2 changed files with 42 additions and 20 deletions

54
main.py
View file

@ -34,6 +34,7 @@ class ZombieCPU:
def exec(self):
Halted = False
while not Halted: #change this to if not halt
#fetch
opcode = self._memory[int(self._regs.eip)]
address = self._memory[int(self._regs.eip + 1)]
@ -58,15 +59,8 @@ class ZombieCPU:
elif opcode == ops.LDI: #mov but actually load immediate
self.write_memory(address, value)
if value == 0:
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)')
self.flags_zero(value)
self.flags_negative(value)
elif opcode == ops.PUSH: #push
self.write_memory(self._regs.esp, value) #put value onto stack
@ -84,15 +78,39 @@ class ZombieCPU:
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.flags_zero(address - value)
elif opcode == ops.INC:
self.write_memory(address, self.read_memory_address(address) + 1)
self.flags_zero(self.read_memory_address(address))
self.flags_negative(self.read_memory_address(address))
elif opcode == ops.DEC:
self.write_memory(address, self.read_memory_address(address) - 1)
self.flags_zero(self.read_memory_address(address))
self.flags_negative(self.read_memory_address(address))
self._regs.eip += 3 #increment instruction pointer
#flag shit
def read_flags(self, i):
return self._regs
def flags_zero(self, value):
if value == 0:
self._regs.flags = self._regs.flags | 0b00000001
else:
self._regs.flags = self._regs.flags & ~0b00000001
def flags_negative(self, value):
if value < 0:
self._regs.flags = self._regs.flags | 0b00000010
else:
self._regs.flags = self._regs.flags & ~0b00000010
#memory
def set_memory(self, memory):
self._memory = memory
def write_memory(self, address, value):
@ -101,8 +119,6 @@ class ZombieCPU:
return self._memory
def read_memory_address(self, address):
return self._memory[int(address)]
def read_flags(self, i):
return self._regs
if __name__ == "__main__":
cpu = ZombieCPU(65536, False)
@ -121,10 +137,14 @@ if __name__ == "__main__":
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.write_memory(268, ops.INC) #increment
cpu.write_memory(269, 100)
cpu.write_memory(270, 0) #unused
cpu.write_memory(271, ops.HLT)
cpu.exec()
print(cpu.read_memory_address(100))
#c = Console(cpu.read_memory()) ima finish this later
#c.draw()

View file

@ -8,3 +8,5 @@ POP = 6 # Pop
JMP = 7 # Jump to address
JZ = 8 # Jump to address if zero
CMP = 9 # Compare and set zero flag
INC = 10 # increment
DEC = 11 # decrement