✨ Added increment ↗️ and decrement ↘️ and fixed flags
This commit is contained in:
parent
67299b55b5
commit
1683ca40a7
2 changed files with 42 additions and 20 deletions
54
main.py
54
main.py
|
@ -34,6 +34,7 @@ class ZombieCPU:
|
||||||
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)]
|
||||||
|
@ -58,15 +59,8 @@ class ZombieCPU:
|
||||||
|
|
||||||
elif opcode == ops.LDI: #mov but actually load immediate
|
elif opcode == ops.LDI: #mov but actually load immediate
|
||||||
self.write_memory(address, value)
|
self.write_memory(address, value)
|
||||||
if value == 0:
|
self.flags_zero(value)
|
||||||
self._regs.flags = self._regs.flags | 0b00000001
|
self.flags_negative(value)
|
||||||
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
|
||||||
|
@ -84,15 +78,39 @@ class ZombieCPU:
|
||||||
self._regs.eip = address - 3
|
self._regs.eip = address - 3
|
||||||
|
|
||||||
elif opcode == ops.CMP:
|
elif opcode == ops.CMP:
|
||||||
if address - value == 0:
|
self.flags_zero(address - value)
|
||||||
self._regs.flags = self._regs.flags | 0b00000001 #set zero flag
|
|
||||||
else:
|
elif opcode == ops.INC:
|
||||||
print('unset flag here (you shouldnt be reading this!) (zero)')
|
self.write_memory(address, self.read_memory_address(address) + 1)
|
||||||
print(self._regs.flags)
|
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
|
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):
|
def set_memory(self, memory):
|
||||||
self._memory = memory
|
self._memory = memory
|
||||||
def write_memory(self, address, value):
|
def write_memory(self, address, value):
|
||||||
|
@ -101,8 +119,6 @@ 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)
|
||||||
|
@ -121,10 +137,14 @@ if __name__ == "__main__":
|
||||||
cpu.write_memory(265, ops.CMP)
|
cpu.write_memory(265, ops.CMP)
|
||||||
cpu.write_memory(266, 10)
|
cpu.write_memory(266, 10)
|
||||||
cpu.write_memory(267, 11) #should set the zero flag
|
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()
|
cpu.exec()
|
||||||
|
|
||||||
|
print(cpu.read_memory_address(100))
|
||||||
|
|
||||||
#c = Console(cpu.read_memory()) ima finish this later
|
#c = Console(cpu.read_memory()) ima finish this later
|
||||||
#c.draw()
|
#c.draw()
|
||||||
|
|
|
@ -8,3 +8,5 @@ POP = 6 # Pop
|
||||||
JMP = 7 # Jump to address
|
JMP = 7 # Jump to address
|
||||||
JZ = 8 # Jump to address if zero
|
JZ = 8 # Jump to address if zero
|
||||||
CMP = 9 # Compare and set zero flag
|
CMP = 9 # Compare and set zero flag
|
||||||
|
INC = 10 # increment
|
||||||
|
DEC = 11 # decrement
|
||||||
|
|
Loading…
Reference in a new issue