Protium CPU
A 16-bit CPU emulator
This CPU emulator could support complex actions. It had almost 100 different operations and could support arithmetic operations, logical operations, subroutines, branching, and more. It could even support additional functionality that the user wants to add through expansion ports which enable it to connect to different actions. Protium is written in C++. Here is the link to the github repository.
Example programs
Here are some example programs:
BYTE recursiveFactorialProgram[] = {
CPU::op::SETC, 0x02, 0x00, // set C to 2
CPU::op::ININT, CPU::regID::A, // input number into A
CPU::op::SETDST, 0x0C, 0x50, // set dst to 0x500C, address of factorial subroutine
CPU::op::CALL, // call the factorial subroutine
CPU::op::OUTINT, CPU::regID::A, // result of factorial is stored in A, so output it
CPU::op::HLT, // halt at the end of main
// start of subroutine, A has the number to be factorialized
CPU::op::CMP, CPU::regID::A, CPU::regID::C, // compare a and c (2)
CPU::op::JNSR, 0x09, 0x00, 0x00, 0x00, // if a >= 2, then jump to recursion part
CPU::op::SETA, 0x01, 0x00, // if a is < 2, set factorial result to 1
CPU::op::RET, // return
CPU::op::PUSH, CPU::regID::A, // push a to store it so that it can be multiplied by (a-1)!
CPU::op::DEC, CPU::regID::A, // decrement A
CPU::op::CALL, // call factorial subroutine
CPU::op::POP, CPU::regID::B, // B now holds the stored value of A
CPU::op::MUL, CPU::regID::A, CPU::regID::A, CPU::regID::B, // a = a * b
CPU::op::RET
};
This program recursively calls a subroutine to compute the factorial of a number. This demonstrates the capabilities of recursion, arithmetic operations, and branching.
BYTE guessingGameProgram[] = {
CPU::op::SETSRC, 0x1C, 0xF0, // set src to address of system random
CPU::op::LDA, // set a to the random value
CPU::op::SETB, 0x64, 0x00, // set B to 100
CPU::op::MOD, CPU::regID::A, CPU::regID::A, CPU::regID::B, // make random number 0 to 100
// start loop
CPU::op::ININT, CPU::regID::B, // b now has the guess
CPU::op::CMP, CPU::regID::A, CPU::regID::B, // compare A and B
CPU::op::JIZR, 0x21, 0x00, 0x00, 0x00, // jump out of loop (33 bytes forward) if equal
CPU::op::JISR, 0x0A, 0x00, 0x00, 0x00, // if sign, then b > a so jump 10 bytes forward
CPU::op::JNSR, 0x0E, 0x00, 0x00, 0x00, // if not sign, b < a so jump 14 bytes foward
// case b > a (JISR)
CPU::op::OUTIM, '>', // if b > a, output >
CPU::op::OUTIM, '\n', // newline
CPU::op::JMPR, 0x00, 0x00, 0x18, 0x00, // go back to start of loop, 24 bytes back
// case b < a (JNSR)
CPU::op::OUTIM, '<', // if b < a, output <
CPU::op::OUTIM, '\n', // newline
CPU::op::JMPR, 0x00, 0x00, 0x21, 0x00, // go back to start of loop (33 bytes back)
// case equal
CPU::op::OUTIM, '=', // if equal, output =
CPU::op::HLT // end
};
This program plays a guessing game with the user where it chooses a number and the user has to guess it. This demonstrates the capabilities of branching and jumping.
Running these programs is very simple:
protium.StoreProgram(0x5000, guessingGameProgram, sizeof(guessingGameProgram));
protium.SetStartingPoint(0x5000);
protium.Start();