
This project is a small 4-bit stack-based computer with:
The CPU is designed to execute simple arithmetic, logic, stack, and conditional jump operations using a compact instruction set.
This project implements a small 4-bit stack-based CPU. Every instruction is encoded as a single 8-bit word:
[7:4] — opcode
[3:0] — operand / data
The supported instructions are:
| Opcode | Instruction | Description |
|---|---|---|
1000 |
NAND |
Performs a NAND operation with top of stack and operand |
1001 |
NOR |
Performs a NOR operation with top of stack and operand |
1010 |
XOR |
Performs an XOR operation with top of stack and operand |
1011 |
ADD |
Adds top of stack with operand |
1101 |
PUSH_COMP |
Load the operand into the dedicated comparision register |
1110 |
PUSH |
Pushes the 4-bit operand onto the stack |
0010 |
JMPC |
Conditionally jumps depending on the value at the top of the stack |
Each instruction requires only one 8-bit instruction word.
For example:
1110 0101
means:
1110 → PUSH
0101 → value 5
So this instruction pushes the value 5 onto the stack.
Note:
PUSH_COMPis used together withJMPCto provide simple conditional program flow. It compare the value pushed by PUSH-COMP and the top of the stack, if the value is equal it will jump to location indicated by the operand.
Use the following sample program:
PUSH_COMP 1001
PUSH 0001
ADD 0001
JMPC 0010
PUSH 0001
To test the CPU:
program_mem_write_button and program_in.program_stack should increase, indicating the next program memory address.start_button to start CPU execution.read_result_button.after_compute output.For this sample program, the expected output is:
after_compute = 0001
LEDs for output signals such as after_compute and program_stack.
Push buttons for:
read_result_buttonstart_buttonprogram_mem_write_buttonSwitches for program_in (the switch values should remain stable while loading the instruction).
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | program_in[0] | after_compute[0] | read_result_button |
| 1 | program_in[1] | after_compute[1] | start_button |
| 2 | program_in[2] | after_compute[2] | program_mem_write_button |
| 3 | program_in[3] | after_compute[3] | |
| 4 | program_in[4] | program_stack[0] | |
| 5 | program_in[5] | program_stack[1] | |
| 6 | program_in[6] | program_stack[2] | |
| 7 | program_in[7] | program_stack[3] |