-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f49fa64
commit 31cae6f
Showing
9 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
WORD_SIZE = 32 | ||
|
||
MAX_NUMBER = 1 << (WORD_SIZE - 1) - 1 | ||
MIN_NUMBER = -(1 << (WORD_SIZE - 1)) | ||
|
||
MEMORY_SIZE = 2048 | ||
STACK_SIZE = 1024 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from src.constants import MAX_NUMBER, MIN_NUMBER | ||
|
||
|
||
class Data: | ||
def __init__(self, value: int) -> None: | ||
assert MIN_NUMBER <= value <= MAX_NUMBER, f"Value '{value}' is out of bound" | ||
self.value = value | ||
|
||
def __str__(self) -> str: | ||
return f"{self.value}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from __future__ import annotations | ||
|
||
from src.isa.opcode import Opcode | ||
|
||
|
||
class Instruction: | ||
def __init__(self, opcode: Opcode, operand: int | None = None) -> None: | ||
self.opcode = opcode | ||
self.operand = operand | ||
|
||
def __str__(self) -> str: | ||
return f"{self.opcode.name:<6} " + (f"{self.operand}" if self.operand is not None else "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Generic, TypeVar | ||
|
||
from src.constants import MEMORY_SIZE | ||
from src.isa.data import Data | ||
from src.isa.instruction import Instruction | ||
|
||
MemoryType = TypeVar("MemoryType") | ||
|
||
|
||
class Memory(Generic[MemoryType]): | ||
def __init__(self, values: list[MemoryType]) -> None: | ||
assert 1 <= len(values) <= MEMORY_SIZE, f"Out of memory for {type(values[0]).__name__}" | ||
self.values = values | ||
|
||
def __str__(self) -> str: | ||
return "[\n " + ",\n ".join(str(value) for value in self.values) + "\n]" | ||
|
||
|
||
DataMemory = Memory[Data] | ||
InstructionMemory = Memory[Instruction] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from enum import StrEnum, unique | ||
|
||
|
||
@unique | ||
class Opcode(StrEnum): | ||
NOP = "nop" | ||
|
||
ADD = "add" | ||
SUB = "sub" | ||
MUL = "mul" | ||
DIV = "div" | ||
MOD = "mod" | ||
NEG = "neg" | ||
|
||
INC = "inc" | ||
DEC = "dec" | ||
|
||
AND = "and" | ||
OR = "or" | ||
NOT = "not" | ||
|
||
CMP = "cmp" | ||
|
||
JMP = "jmp" | ||
JZ = "jz" | ||
JNZ = "jnz" | ||
|
||
CALL = "call" | ||
RET = "ret" | ||
|
||
IN = "IN" | ||
OUT = "OUT" | ||
|
||
LIT = "lit" | ||
|
||
PUSH = "push" | ||
POP = "pop" | ||
DROP = "drop" | ||
DUP = "dup" | ||
SWAP = "swap" | ||
OVER = "over" | ||
|
||
HALT = "halt" | ||
|
||
def __init__(self, mnemonic: str): | ||
self.mnemonic = mnemonic | ||
|
||
def __str__(self): | ||
return str(self.value) | ||
|
||
@classmethod | ||
def from_string(cls, value: str): | ||
value = value.lower() | ||
for opcode in cls: | ||
if opcode.mnemonic == value: | ||
return opcode | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from src.isa.memory import InstructionMemory | ||
|
||
|
||
class Program: | ||
def __init__(self, instructions: InstructionMemory, init: int) -> None: | ||
self.instructions = instructions | ||
self.init = init | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, Program): | ||
return self.instructions.values == other.instructions.values and self.init == other.init | ||
return False | ||
|
||
def __str__(self) -> str: | ||
result = "" | ||
for i, instr in enumerate(self.instructions.values): | ||
if i == self.init: | ||
result += f"{("> " + str(i)+"."):<8} {instr}\n" | ||
else: | ||
result += f"{(" " + str(i)+"."):<8} {instr}\n" | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from src.isa.instruction import Instruction | ||
from src.isa.memory import InstructionMemory | ||
from src.isa.opcode import Opcode | ||
from src.isa.program import Program | ||
|
||
|
||
def start(): | ||
instr = InstructionMemory( | ||
[ | ||
Instruction(Opcode.PUSH), | ||
Instruction(Opcode.PUSH), | ||
Instruction(Opcode.ADD, 2), | ||
Instruction(Opcode.NOP, 1), | ||
] | ||
) | ||
print(Program(instr, 1)) |