Skip to content

Commit

Permalink
feat: ✨ start isa
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbarsukov committed May 18, 2024
1 parent f49fa64 commit 31cae6f
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ description = "Computer system architecture Lab #3 -- Experiment (stack machine)
authors = ["Max Barsukov <maximbarsukov@bk.ru>"]
license = "GPL-3.0"
readme = "README.md"
packages = [{include = "*.py", from="src"}]

[tool.poetry.scripts]
main = "src.main:start"

[tool.poetry.dependencies]
python = "^3.12"
Expand Down Expand Up @@ -82,6 +86,7 @@ ignore = [
"COM812", # The following rules may cause conflicts when used with the formatter: `COM812`.
"E501", # allow lines longer than 80 symbols (up to 120)
"T201", # `print` found
"PD011", # Breaks .values
"RUF002", # Docstring contains ambiguous `В` (CYRILLIC CAPITAL LETTER VE). Did you mean `B` (LATIN CAPITAL LETTER B)
"RUF003", # Comment contains ambiguous `с` (CYRILLIC SMALL LETTER ES). Did you mean `c` (LATIN SMALL LETTER C)
"PTH123", # `open()` should be replaced by `Path.open()`
Expand Down
7 changes: 7 additions & 0 deletions src/constants.py
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 added src/isa/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions src/isa/data.py
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}"
12 changes: 12 additions & 0 deletions src/isa/instruction.py
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 "")
22 changes: 22 additions & 0 deletions src/isa/memory.py
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]
59 changes: 59 additions & 0 deletions src/isa/opcode.py
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
22 changes: 22 additions & 0 deletions src/isa/program.py
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
16 changes: 16 additions & 0 deletions src/main.py
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))

0 comments on commit 31cae6f

Please sign in to comment.