1 bit CPU
1 Stars     70 Views    

Author: Ruben Hillier

Project access type: Public


CPU architecture:


Instruction set:

1.       BFH (0000) – Sets the borrow flag to high.

2.       AND (0001) – checks if the accumulator is the same as data in memory at the current address, then sets the accumulator to 1 if it is, so 0 in the accumulator and 0 in the data bus will give an output of 1.

3.       OR (0010) – if the accumulator or data in memory at the current address is not 0    set the accumulator to 1. For example, the accumulator is 1. This will give an output of 1 from the ALU to be put into the accumulator, and the same if data at the current memory address is 1.

4.       ADD (0011) – adds data from memory at the current address to the accumulator.

5.       SUB (0100) – subtracts the accumulator by data in memory at the current address.

6.       LDA (0101) – loads data from memory at the current address into the accumulator.

7.       STA (0111) – transfers the data in the accumulator to the following memory address.

8.       NOP (1000) - no operation this will be 13 clock cycles as that’s how many clock cycles are for each instruction.

9.       FAS (1001) – Sets the A flag to 1.

10.    FBS (1010) – Sets the B flag to 1.

11.    FCS (1011) – Sets the C flag to 1.

12.    JMP (1100) – An unconditional jump that will move the program counter ahead by 1, 2, 3, or 4 steps based on a combination of the accumulator, C flag, and B flag being set.

13.    JZ (1101) – A conditional jump that checks if the accumulator is 0 and then uses the same logic as JMP to determine how many addresses to jump ahead, based on the status of the accumulator and C flag and B flag.

14.    RAF (1110) – Set A, B and C flags to 0.

15.    HLT (1111) – Halt the execution of data.


Flags (D is for data):

1.       Carry Flag (CD): This flag is set if an arithmetic operation results in a carry-out, which might influence subsequent arithmetic operations.

2.       Borrow Flag (BD): This flag becomes active during an arithmetic operation when a subtraction results in a value less than zero, indicating that a borrow is necessary. 

3.       A Flag (A): This flag is used in jumping memory addresses.

4.       B Flag (B): This flag is used in jumping memory addresses.

5.       C Flag (C): This flag is used in jumping memory addresses.

Understanding the CPU Architecture:

·      Memory Address: The CPU uses a 4-bit memory address system, which means it can directly address up to 16 unique locations in memory, this can be increased by increasing the size of the ring counter in the program counter.

·      Data Bus: It has a 1-bit data bus, allowing for the transfer of one bit of data at a time between memory and the CPU.

·      Instruction Set: The CPU recognizes a 4-bit instruction set, meaning each operation the CPU can perform is represented by a 4-bit code.

·      Ring Counter: A 16-bit ring counter orchestrates the execution of instructions. Each instruction takes 16 clock cycles to complete, with different phases of the instruction's execution happening in each cycle.






Understanding the jump commands (JMP, JZ):

To control movement through memory addresses using the jump commands (JMP, JZ), you'll work with three key components: the A flag (A), the B flag (B), and the C flag (C). Each of these components can either be 0 or 1, and together they form a binary number that determines how many memory addresses you'll move. Here's how it works:


·      000 (A=0, C=0, B=0): Move by 1 memory address.

·      001 (A=0, C=0, B=1): Move by 2 memory addresses.

·      010 (A=0, C=1, B=0): Move by 3 memory addresses.

·      011 (A=0, C=1, B=1): Move by 4 memory addresses.

·      100 (A=1, C=0, B=0): Move by 5 memory addresses.

·      And so on, increasing the binary number formed by A, B, and C increases the number of memory addresses you'll move through.


Each bit in the binary number has a specific value, with the accumulator being the least significant bit (worth 1), the carry flag the middle bit (worth 2), and the borrow flag the most significant bit (worth 4). Adjust these flags according to the binary value that corresponds to the number of memory addresses you wish to move.


Example program:



Here's what will happen during the execution:

·      Initialization: The CPU starts by setting the Program Counter (PC) to the first instruction's address, usually 0, preparing the CPU to start execution from the beginning of the program.

·      Execution Cycle: Managed by the ring counter, the CPU begins its execution cycle, systematically processing instructions across 16 clock cycles. Each cycle is dedicated to a specific phase of instruction processing.

·      Instruction Execution Phases:

o   NOP (No Operation): The CPU does nothing, effectively waiting and allowing time cycles to pass without any action.

o   AND, OR, ADD, SUB: These arithmetic and logical instructions use data from memory, the accumulator, and the flags (Carry and Borrow) to perform their specific operations and update the accumulator and flags accordingly.

o   LDA (Load Accumulator): This instruction loads data from a specified memory address into the accumulator, updating its value.

o   STA (Store Accumulator): STA stores the data from the accumulator into the specified memory address, effectively saving the accumulator's value into memory.

o   JMP (Jump): JMP modifies the Program Counter based on the binary value formed by the accumulator and flags, enabling the CPU to jump to a new instruction address, ranging from 1-4 steps ahead.

o   JZ (Jump if Zero): This conditional jump instruction checks if the accumulator is 0 and the accumulator's value and flags meet certain conditions, then jumps to a new instruction address if true.

o   HLT (Halt): This instruction ends the execution cycle, stopping the CPU from processing further instructions. It's crucial for preventing the CPU from running into an undefined state or looping indefinitely.

·      Monitoring Execution: Throughout execution, it's important to keep an eye on the accumulator and flags to track the CPU's state and ensure the program runs as expected.

·      Halting the CPU: The execution halts when the HLT instruction is encountered. Without HLT, the CPU might continue executing in an undefined manner or loop endlessly.

Created: Dec 04, 2023

Updated: Feb 21, 2024


You must login before you can post a comment.