💻 Put the opcodes in a sperate file and labeled them
This commit is contained in:
parent
9f5924784f
commit
77a9a00bd5
3 changed files with 87378 additions and 18 deletions
87350
fuuckfuckfuck2
Normal file
87350
fuuckfuckfuck2
Normal file
File diff suppressed because it is too large
Load diff
35
main.py
35
main.py
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from console import Console
|
import opcodes as ops
|
||||||
np.set_printoptions(threshold=np.inf)
|
np.set_printoptions(threshold=np.inf)
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,8 +48,8 @@ class Zomcpu:
|
||||||
self._memory[0] = 256 #set instructiojn pointer
|
self._memory[0] = 256 #set instructiojn pointer
|
||||||
|
|
||||||
def exec(self):
|
def exec(self):
|
||||||
Go = 1
|
Halted = False
|
||||||
while Go: #change this to if not halt
|
while not Halted: #change this to if not halt
|
||||||
#fetch
|
#fetch
|
||||||
opcode = self._memory[int(self._memory[0])]
|
opcode = self._memory[int(self._memory[0])]
|
||||||
address = self._memory[int(self._memory[0] + 1)]
|
address = self._memory[int(self._memory[0] + 1)]
|
||||||
|
@ -58,26 +58,27 @@ class Zomcpu:
|
||||||
if self._debug:
|
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._memory[0]}')
|
||||||
|
|
||||||
if opcode == 0:
|
if opcode == ops.NOP:
|
||||||
Go = value
|
pass
|
||||||
if value == 0:
|
|
||||||
|
elif opcode == ops.HLT:
|
||||||
|
Halted = True
|
||||||
print('Processor Halted')
|
print('Processor Halted')
|
||||||
|
|
||||||
elif opcode == 1: #reset
|
elif opcode == ops.RST: #reset
|
||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
elif opcode == 2: #debug (nop if debug is not set)
|
elif opcode == ops.DBG: #debug enable
|
||||||
if self._debug:
|
self._debug = value
|
||||||
print(f'Opcode: {opcode}\nAddress: {address}\nValue: {value}')
|
|
||||||
|
|
||||||
elif opcode == 3: #mov
|
elif opcode == ops.LDI: #mov but actually load immediate
|
||||||
self.write_memory(address, value)
|
self.write_memory(address, value)
|
||||||
|
|
||||||
elif opcode == 4: #push
|
elif opcode == ops.PUSH: #push
|
||||||
self.write_memory(self._memory[7], value) #put value onto stack
|
self.write_memory(self._memory[7], value) #put value onto stack
|
||||||
self.write_memory(7, self._memory[7] - 1) #dec stackpointer
|
self.write_memory(7, self._memory[7] - 1) #dec stackpointer
|
||||||
|
|
||||||
elif opcode == 5: #pop
|
elif opcode == ops.POP: #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(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.write_memory(7, self._memory[7] + 1) #inc stackpointer
|
||||||
|
|
||||||
|
@ -98,15 +99,17 @@ if __name__ == "__main__":
|
||||||
cpu.reset()
|
cpu.reset()
|
||||||
|
|
||||||
#hard coded opcodes
|
#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(257, 27) #address 27
|
||||||
cpu.write_memory(258, 105) #105 into ^
|
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(260, 0) #unused in this op
|
||||||
cpu.write_memory(261, 27) #the value at address 27 onto the stack
|
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(263, 100) #into 100
|
||||||
cpu.write_memory(264, 0) #unused in this op
|
cpu.write_memory(264, 0) #unused in this op
|
||||||
|
cpu.write_memory(265, 1) #halt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
7
opcodes.py
Normal file
7
opcodes.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
NOP = 0 # No operation
|
||||||
|
HLT = 1 # Halt
|
||||||
|
RST = 2 # Reset
|
||||||
|
DBG = 3 # Enables Debug
|
||||||
|
LDI = 4 # Load Immediate
|
||||||
|
PUSH = 5 # Push
|
||||||
|
POP = 6 # Pop
|
Loading…
Reference in a new issue