Compare commits
4 commits
thomasrewr
...
master
Author | SHA1 | Date | |
---|---|---|---|
1683ca40a7 | |||
67299b55b5 | |||
d6cd7db0b8 | |||
77a9a00bd5 |
5 changed files with 87494 additions and 120 deletions
87350
fuuckfuckfuck2
Normal file
87350
fuuckfuckfuck2
Normal file
File diff suppressed because it is too large
Load diff
172
main.py
172
main.py
|
@ -1,104 +1,150 @@
|
||||||
#made by zombie
|
#made by zombie
|
||||||
#02/01/21
|
#02/01/21
|
||||||
|
|
||||||
import math
|
#0-255 is internal registers and flags and etc
|
||||||
import numpy as np
|
# program execcution starts at 256
|
||||||
|
#stack starts at end of memory FILO
|
||||||
|
|
||||||
|
#flags
|
||||||
|
#nil nil nil nil nil nil negative zero
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import opcodes as ops
|
import opcodes as ops
|
||||||
from console import Console
|
|
||||||
from memory import Memory
|
|
||||||
from registers import Registers
|
from registers import Registers
|
||||||
np.set_printoptions(threshold=np.inf)
|
np.set_printoptions(threshold=np.inf)
|
||||||
|
|
||||||
|
|
||||||
class ZCPU:
|
|
||||||
_halted = False
|
|
||||||
_regs = Registers()
|
|
||||||
_mem = None
|
|
||||||
|
|
||||||
def __init__(self, memory, debug=None):
|
class ZombieCPU:
|
||||||
print('Processor Init')
|
_regs = Registers()
|
||||||
|
def __init__(self, memorysize=None, debug=None):
|
||||||
|
self._memorysize = memorysize
|
||||||
|
self._memory = np.zeros(memorysize)
|
||||||
self._debug = debug
|
self._debug = debug
|
||||||
self._mem = memory
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
print('Processor Resetting')
|
if self._debug:
|
||||||
self._regs.sp = self._mem.size() - 1 # Set stackpointer
|
print('Processor Reseting')
|
||||||
self._regs.ip = 0 # Set instruction pointer
|
self._memory = np.zeros(self._memorysize) #clear memory
|
||||||
|
|
||||||
|
self._regs.esp = self._memorysize - 1 #set stackpointer
|
||||||
|
self._regs.eip = 256 #set instructiojn pointer #fuck you thomas
|
||||||
|
print(self._regs.eip)
|
||||||
def exec(self):
|
def exec(self):
|
||||||
while not self._halted:
|
Halted = False
|
||||||
# Fetch instruction
|
while not Halted: #change this to if not halt
|
||||||
opcode = self._mem.read(self._regs.ip)
|
|
||||||
address = self._mem.read(self._regs.ip + 1)
|
#fetch
|
||||||
value = self._mem.read(self._regs.ip + 2)
|
opcode = self._memory[int(self._regs.eip)]
|
||||||
|
address = self._memory[int(self._regs.eip + 1)]
|
||||||
|
value = self._memory[int(self._regs.eip + 2)]
|
||||||
|
|
||||||
if self._debug:
|
if self._debug:
|
||||||
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}\nIP: {self._regs.ip}')
|
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}\nIP: {self._regs.eip}')
|
||||||
|
|
||||||
if opcode == ops.NOP: # No-op
|
if opcode == ops.NOP:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
elif opcode == ops.HLT: # Halt
|
elif opcode == ops.HLT:
|
||||||
self._halted = True
|
Halted = True
|
||||||
|
if self._debug:
|
||||||
|
print('Processor Halted')
|
||||||
|
|
||||||
elif opcode == ops.RST: # Reset
|
elif opcode == ops.RST: #reset
|
||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
elif opcode == ops.DBG: # Debug (nop if debug is not set) -- This is stoopid
|
elif opcode == ops.DBG: #debug toggle
|
||||||
if self._debug:
|
self._debug = value
|
||||||
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}')
|
|
||||||
|
|
||||||
elif opcode == ops.LDI: # ldi
|
elif opcode == ops.LDI: #mov but actually load immediate
|
||||||
self._mem.write(address, value)
|
self.write_memory(address, value)
|
||||||
|
self.flags_zero(value)
|
||||||
|
self.flags_negative(value)
|
||||||
|
|
||||||
elif opcode == ops.PUSH: #push
|
elif opcode == ops.PUSH: #push
|
||||||
self._mem.write(self._regs.sp, value)
|
self.write_memory(self._regs.esp, value) #put value onto stack
|
||||||
self._regs.sp -= 1
|
self._regs.esp -= 1 #dec stackpointer
|
||||||
|
|
||||||
elif opcode == ops.POP: #pop
|
elif opcode == ops.POP: #pop
|
||||||
self._regs.sp += 1
|
self.write_memory(address, self.read_memory_address(self._regs.esp + 1)) #read the last value and put it into address (not the empty space)
|
||||||
self._mem.write(address, self._mem.read(self._regs.sp))
|
self._regs.esp += 1 #inc stackpointer
|
||||||
|
|
||||||
|
elif opcode == ops.JMP:
|
||||||
|
self._regs.eip = address - 3
|
||||||
|
|
||||||
|
elif opcode == ops.JZ:
|
||||||
|
if self._regs.flags & 0b00000001:
|
||||||
|
self._regs.eip = address - 3
|
||||||
|
|
||||||
|
elif opcode == ops.CMP:
|
||||||
|
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:
|
else:
|
||||||
raise RuntimeException(f'Invalid Opcode: {opcode}')
|
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
|
||||||
|
|
||||||
#increment instruction pointer
|
|
||||||
self._regs.ip += 3
|
|
||||||
|
|
||||||
|
|
||||||
|
#memory
|
||||||
|
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[int(address)]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
#hard coded opcodes
|
cpu = ZombieCPU(65536, False)
|
||||||
ram = Memory(65536)
|
|
||||||
|
|
||||||
ram.write(0, ops.LDI)
|
|
||||||
ram.write(1, 100)
|
|
||||||
ram.write(2, 5)
|
|
||||||
|
|
||||||
ram.write(3, ops.PUSH)
|
|
||||||
ram.write(4, 0)
|
|
||||||
ram.write(5, 69)
|
|
||||||
|
|
||||||
ram.write(6, ops.POP)
|
|
||||||
ram.write(7, 101)
|
|
||||||
ram.write(8, 0)
|
|
||||||
|
|
||||||
ram.write(9, ops.DBG)
|
|
||||||
ram.write(10, 1)
|
|
||||||
ram.write(11, 2)
|
|
||||||
|
|
||||||
ram.write(12, ops.HLT)
|
|
||||||
ram.write(13, 0)
|
|
||||||
ram.write(14, 0)
|
|
||||||
|
|
||||||
cpu = ZCPU(ram, False)
|
|
||||||
cpu.reset()
|
cpu.reset()
|
||||||
|
|
||||||
|
#hard coded opcodes
|
||||||
|
cpu.write_memory(256, ops.LDI) #mov
|
||||||
|
cpu.write_memory(257, 27) #address 27
|
||||||
|
cpu.write_memory(258, 105) #105 into ^
|
||||||
|
cpu.write_memory(259, ops.PUSH) #push
|
||||||
|
cpu.write_memory(260, 0) #unused in this op
|
||||||
|
cpu.write_memory(261, 27) #the value at address 27 onto the stack
|
||||||
|
cpu.write_memory(262, ops.POP) #pop
|
||||||
|
cpu.write_memory(263, 100) #into 100
|
||||||
|
cpu.write_memory(264, 0) #unused in this op
|
||||||
|
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.INC) #increment
|
||||||
|
cpu.write_memory(269, 100)
|
||||||
|
cpu.write_memory(270, 0) #unused
|
||||||
|
cpu.write_memory(271, ops.HLT)
|
||||||
|
|
||||||
cpu.exec()
|
cpu.exec()
|
||||||
|
|
||||||
print(ram.read(100))
|
print(cpu.read_memory_address(100))
|
||||||
print(ram.read(101))
|
|
||||||
print(ram.read(102))
|
|
||||||
|
|
||||||
#c = Console(cpu.read_memory()) ima finish this later
|
#c = Console(cpu.read_memory()) ima finish this later
|
||||||
#c.draw()
|
#c.draw()
|
||||||
|
|
27
memory.py
27
memory.py
|
@ -1,27 +0,0 @@
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
class Memory:
|
|
||||||
def __init__(self, size):
|
|
||||||
self._memory = np.zeros(size)
|
|
||||||
|
|
||||||
def write(self, address, data):
|
|
||||||
address = int(address)
|
|
||||||
if 0 > address >= self._memory.size:
|
|
||||||
# TODO: Raise an exception and throw an interrupt here
|
|
||||||
raise RuntimeException('Invalid write address')
|
|
||||||
|
|
||||||
self._memory[address] = data
|
|
||||||
|
|
||||||
def read(self, address):
|
|
||||||
address = int(address)
|
|
||||||
if 0 > address >= self._memory.size:
|
|
||||||
# TODO: Raise an exception and throw an interrupt here
|
|
||||||
raise RuntimeException('Invalid write address')
|
|
||||||
|
|
||||||
return self._memory[address]
|
|
||||||
|
|
||||||
def clear(self, data=0):
|
|
||||||
self._memory.fill(data)
|
|
||||||
|
|
||||||
def size(self):
|
|
||||||
return self._memory.size
|
|
|
@ -1,7 +1,12 @@
|
||||||
NOP = 0 # No-op
|
NOP = 0 # No operation
|
||||||
HLT = 1 # Halt
|
HLT = 1 # Halt
|
||||||
RST = 2 # Reset
|
RST = 2 # Reset
|
||||||
DBG = 3 # Debug
|
DBG = 3 # Enables Debug
|
||||||
LDI = 4 # Load Immediate
|
LDI = 4 # Load Immediate
|
||||||
PUSH = 5 # Push
|
PUSH = 5 # Push
|
||||||
POP = 6 # Pop
|
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
|
||||||
|
|
20
registers.py
20
registers.py
|
@ -1,11 +1,11 @@
|
||||||
class Registers:
|
class Registers:
|
||||||
ip = 0
|
eip = 0 # instruction pointer
|
||||||
ax = 0
|
eax = 0
|
||||||
bx = 0
|
ebx = 0
|
||||||
cx = 0
|
ecx = 0
|
||||||
dx = 0
|
edx = 0
|
||||||
si = 0
|
esi = 0 # source
|
||||||
di = 0
|
edi = 0 # destination
|
||||||
sp = 0
|
esp = 0 # stack pointer
|
||||||
bp = 0
|
ebp = 0
|
||||||
flags = 0
|
flags = 0b00000000 #flags ob
|
||||||
|
|
Loading…
Reference in a new issue