A custom instruction set, assembler, compiler, and virtual machine in Python.
This is a hobby project for educational purposes.
bus
: The data, address, and intent lines connected to every device, as well as a clockwrite
: Putting an address to write to on the data lines, a value on the data lines, and setting the intent line to writeread
: Putting an address to read from on the address lines, and setting the intent line to readrespond
: Putting a value on the data lines without changing the intent line or address lines
device
: Any component connected to the bus like RAM, ROM, the processor, or peripheralsperipheral
: A device that largely encapsulates its entire operation, and only interacts with other devices via a few registers and bus respondsregister
: Small units of memory inside the processor which are used by instructionsA/B/C registers
: 8 bit registers used in most operations, where A and B are generally the operands and C usually contains the resultprogram counter
: The 16 bit P register, which keeps track of the current execution position, where the processor will load the next instruction fromstack pointer
: The 16 bit T register, which keeps track of the current height of the stack plus 1, the next stack position to be written to when the stack is pushed toD register
: A virtual 16 bit register also known as the "address" register where the L register is the low byte and the H register is the high byteE register
: A virtual 16 bit register that represents the A and B register, where A is the low byte and B is the high bytestatus register
: The 8 bit S register, stores different status flags from the processor
cycle
: One full clock cycle including a negative and then positive edgeinstruction
: Procedures defined inmlvm/instructions.py
which define steps of CPU operations based on opcodes in the ROM
By default, address space is laid out as shown below
0x0000 ... 0x5FFF
: Random Access Memory0x0000 ... 0x0FFF
: Stack0x1000 ... 0x5FFF
: Program Memory
0x6000 ... 0x7FFF
: Peripherals0x6000 ... 0x607F
: Video Peripheral Registers0x6080 ... 0x60FF
: Gamepad Peripheral Registers0x6100 ... 0x617F
: Timer Peripheral Registers
0x8000 ... 0xFFFF
: Read Only Memory0x8000 ... 0x????
: Binary Executable
It is heavily recommended to install pypy or the virtual machine will likely not be able to maintain a very fast clock speed
pip install -r requirements.txt
pypy -m pip install -r requirements.txt
You can easily compile, assemble, and run one of the examples
Compiling the code: python -m mlvm.compiler examples/mlvc/pong.mlvc pong.mlvs
Assembling and generating the ROM: python -m mlvm.assembler pong.mlvs pong.bin
Loading and running the ROM: pypy -m mlvm pong.bin