Compare commits
4 commits
thomasrewr
...
master
Author | SHA1 | Date | |
---|---|---|---|
1683ca40a7 | |||
67299b55b5 | |||
d6cd7db0b8 | |||
77a9a00bd5 |
4 changed files with 87468 additions and 64 deletions
87350
fuuckfuckfuck2
Normal file
87350
fuuckfuckfuck2
Normal file
File diff suppressed because it is too large
Load diff
159
main.py
159
main.py
|
@ -3,87 +3,114 @@
|
|||
|
||||
#0-255 is internal registers and flags and etc
|
||||
# program execcution starts at 256
|
||||
#stack starts at 65535 FILO
|
||||
#stack starts at end of memory 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
|
||||
#flags
|
||||
#nil nil nil nil nil nil negative zero
|
||||
|
||||
|
||||
#list of opcodes
|
||||
#0 interrupt
|
||||
#1 reset
|
||||
#2 debug info (nop)
|
||||
#3 mov
|
||||
#4 push (onto stack)
|
||||
#5 pop (off of stack into address)
|
||||
|
||||
|
||||
import math
|
||||
import numpy as np
|
||||
from console import Console
|
||||
import opcodes as ops
|
||||
from registers import Registers
|
||||
np.set_printoptions(threshold=np.inf)
|
||||
|
||||
|
||||
|
||||
class Zomcpu:
|
||||
class ZombieCPU:
|
||||
_regs = Registers()
|
||||
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')
|
||||
if self._debug:
|
||||
print('Processor Reseting')
|
||||
self._memory = np.zeros(self._memorysize) #clear memory
|
||||
|
||||
self._memory[7] = self._memorysize - 1 #set stackpointer
|
||||
self._memory[0] = 256 #set instructiojn pointer
|
||||
|
||||
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):
|
||||
Go = 1
|
||||
while Go: #change this to if not halt
|
||||
Halted = False
|
||||
while not Halted: #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)]
|
||||
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:
|
||||
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}\nIP: {self._memory[0]}')
|
||||
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}\nIP: {self._regs.eip}')
|
||||
|
||||
if opcode == ops.NOP:
|
||||
pass
|
||||
|
||||
elif opcode == ops.HLT:
|
||||
Halted = True
|
||||
if self._debug:
|
||||
print('Processor Halted')
|
||||
|
||||
if opcode == 0:
|
||||
Go = value
|
||||
if value == 0:
|
||||
print('Processor Halted')
|
||||
|
||||
elif opcode == 1: #reset
|
||||
elif opcode == ops.RST: #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 == ops.DBG: #debug toggle
|
||||
self._debug = value
|
||||
|
||||
elif opcode == 3: #mov
|
||||
elif opcode == ops.LDI: #mov but actually load immediate
|
||||
self.write_memory(address, value)
|
||||
self.flags_zero(value)
|
||||
self.flags_negative(value)
|
||||
|
||||
elif opcode == ops.PUSH: #push
|
||||
self.write_memory(self._regs.esp, value) #put value onto stack
|
||||
self._regs.esp -= 1 #dec stackpointer
|
||||
|
||||
elif opcode == 4: #push
|
||||
self.write_memory(self._memory[7], value) #put value onto stack
|
||||
self.write_memory(7, self._memory[7] - 1) #dec stackpointer
|
||||
|
||||
elif opcode == 5: #pop
|
||||
self.write_memory(address, self.read_memory_address(self._memory[7] + 1)) #read the last value and put it into address (not the empty space)
|
||||
self.write_memory(7, self._memory[7] + 1) #inc stackpointer
|
||||
|
||||
self._memory[0] += 3 #increment instruction pointer
|
||||
elif opcode == ops.POP: #pop
|
||||
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._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:
|
||||
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):
|
||||
|
@ -92,28 +119,32 @@ class Zomcpu:
|
|||
return self._memory
|
||||
def read_memory_address(self, address):
|
||||
return self._memory[int(address)]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cpu = Zomcpu(65536, True)
|
||||
cpu = ZombieCPU(65536, False)
|
||||
cpu.reset()
|
||||
|
||||
#hard coded opcodes
|
||||
cpu.write_memory(256, 3) #mov
|
||||
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, 4) #push
|
||||
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, 5) #pop
|
||||
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()
|
||||
|
||||
print(cpu.read_memory_address(100))
|
||||
print(cpu.read_memory_address(264))
|
||||
print(cpu.read_memory_address(7))
|
||||
|
||||
#c = Console(cpu.read_memory()) ima finish this later
|
||||
#c.draw()
|
||||
|
|
12
opcodes.py
Normal file
12
opcodes.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
NOP = 0 # No operation
|
||||
HLT = 1 # Halt
|
||||
RST = 2 # Reset
|
||||
DBG = 3 # Enables Debug
|
||||
LDI = 4 # Load Immediate
|
||||
PUSH = 5 # Push
|
||||
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
|
11
registers.py
Normal file
11
registers.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
class Registers:
|
||||
eip = 0 # instruction pointer
|
||||
eax = 0
|
||||
ebx = 0
|
||||
ecx = 0
|
||||
edx = 0
|
||||
esi = 0 # source
|
||||
edi = 0 # destination
|
||||
esp = 0 # stack pointer
|
||||
ebp = 0
|
||||
flags = 0b00000000 #flags ob
|
Loading…
Reference in a new issue