1 bit CPU
0 Stars     5 Views    

Author: Sanket Patil

Forked from: Ruben Hillier/1 bit CPU

Project access type: Public

Description:

CPU architecture:

 

Instruction set:

1.       NOP (0000) – no operation this will be 8 clock cycles as that’s how many clock cycles are for each instruction.

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.       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.

9.       JZ (1101) – A conditional jump that checks if the data line 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.

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

 

Flags:

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

2.       Borrow Flag (B): This flag is set if an arithmetic operation requires a borrow, indicating a subtraction resulted in a 'negative' value.

 

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.

·      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: An 8-bit ring counter orchestrates the execution of instructions. Each instruction takes 8 clock cycles to complete, with different phases of the instruction's execution happening in each cycle.

 

Understanding the jump commands (JMP, JZ):

How do both of them move through memory; if you want to only skip one memory address then make sure the C, and B flags and accumulator (flag set) are set to 0, if you want to move ahead two memory addresses then set one of the flag set to 1, going ahead 3 memory positions will need to set two the flag set to 1 and if you want to move four memory positions set the three of the flag set to 1.

 

Here's what will happen during the execution:

·      Initialization: Set the program counter (PC) to the first instruction's address (in this case, 0).

·      Execution Cycle: The CPU will begin its execution cycle, which is managed by the ring counter. Each instruction will be carried through its phases across 8 clock cycles.

·      Instruction Execution:

1.       During the NOP cycles, the CPU will simply wait, doing nothing.

2.       The AND, OR, ADD, and SUB instructions will perform their respective operations using the data from the memory and the accumulator and both C and B flags.

3.       The LDA instruction will load data into the accumulator from the specified memory address.

4.       The STA instruction will store the data from the accumulator into the next memory address.

5.       The JMP instruction will cause the program counter to jump ahead 1-4 steps based on the condition of the accumulator and flags.

6.       The JZ instruction will conditionally jump based on the accumulator's value and the state of the flags, but only if the data line is 0.

7.       The HLT instruction will end the execution cycle, and the CPU will stop running the program.

·      Monitoring Execution: As the program runs, monitor the accumulator and flags to understand the CPU's state and to verify the program is running as intended.

·      Halting the CPU: Once the HLT instruction is reached, the CPU will stop. If you do not include an HLT instruction, the CPU may continue to run into an undefined state or loop indefinitely.

Created: Dec 17, 2023

Updated: Dec 17, 2023


Comments

You must login before you can post a comment.