ZombieCPU/main.py
2021-02-01 14:31:07 -05:00

99 lines
2.5 KiB
Python

#made by zombie
#02/01/21
#0-255 is internal registers and flags and etc
# program execcution starts at 256
#stack starts at 65535 FILO
#0 IP #Instruction Pointer
#1 EAX
#2 EBX
#3 ECX
#4 EDX
#5 ESI
#6 EDI
#7 ESP #stack pointer
#8 EBP
#9 FLAG #Read-Only contains the flags
#list of opcodes
#0 interrupt
#1 reset
#2 debug info (nop)
#3 mov
import math
import numpy as np
class Zomcpu:
def __init__(self, memorysize=None, debug=None):
print('Processor Init')
self._memorysize = memorysize
self._memory = np.zeros(memorysize)
self._debug = debug
def reset(self):
print('Processor Resetting')
self._memory = np.zeros(self._memorysize) #clear memory
self._memory[7] = self._memorysize - 1 #set stackpointer
self._memory[0] = 256 #set instructiojn pointer
def exec(self):
Go = 1
while Go: #change this to if not halt
#fetch
opcode = self._memory[int(self._memory[0])]
address = self._memory[int(self._memory[0] + 1)]
value = self._memory[int(self._memory[0] + 2)]
if self._debug:
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}\nIP: {self._memory[0]}')
if opcode == 0:
Go = value
if value == 0:
print('Processor Halted')
elif opcode == 1: #reset
self.reset()
elif opcode == 2: #debug (nop if debug is not set)
if self._debug:
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}')
elif opcode == 3: #mov
self.write_memory(address, value)
self._memory[0] += 3 #increment instruction pointer
def set_memory(self, memory):
self._memory = memory
def write_memory(self, address, value):
self._memory[int(address)] = value
def read_memory(self):
return self._memory
def read_memory_address(self, address):
return self._memory[address]
if __name__ == "__main__":
cpu = Zomcpu(65536, True)
cpu.reset()
print(cpu.read_memory_address(7))
#hard coded opcodes
cpu.write_memory(256, 3)
cpu.write_memory(257, 27)
cpu.write_memory(258, 105)
print(cpu.read_memory_address(27))
cpu.exec()
print(cpu.read_memory_address(27))