Skip to content

This repository contains all the necessary Verilog code and supporting files to synthesize the 8-bit soft-core processor on an FPGA. The code is well-commented, following best practices in digital design to ensure clarity and maintainability.

License

Notifications You must be signed in to change notification settings

ammarmalik17/8BitMicroFPGA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

8-Bit Soft-Core FPGA Synthesized Microcontroller

License: MIT

Overview

This project implements a simple 8-bit soft-core processor using a Finite State Machine (FSM) model. The processor consists of various key modules such as the Arithmetic Logic Unit (ALU), Program Memory (PMem), Data Memory (DMem), and a series of control logic components. The Control_Logic module plays a crucial role in generating control signals based on the current state and instruction type, facilitating the execution of a set of basic operations.

The processor follows a sequence of states—LOAD, FETCH, DECODE, and EXECUTE—to process instructions stored in program memory. This design demonstrates fundamental concepts of computer architecture and digital design using Verilog HDL for FPGA implementation.

Features

  • 8-bit Architecture: Designed for efficient implementation on FPGAs
  • FSM-Based Control: Uses a four-state finite state machine for instruction execution
  • Modular Design: Well-structured Verilog modules for easy understanding and modification
  • Comprehensive ALU: Supports arithmetic, logical, and shift operations
  • Separate Memory Units: Dedicated program and data memory spaces
  • Simulation Ready: Includes testbench for functional verification
  • FPGA Synthesizable: Compatible with major FPGA development tools

Project Structure

├── Project.v       # Main processor module and submodules
├── testbench.v     # Testbench for simulation
├── progr.dat       # Sample program instructions file
├── README.md       # This file
└── LICENSE.txt     # MIT License

Architecture

The processor operates using a state-based approach, transitioning through the following states:

  • LOAD: Initializes the processor by loading the program into memory
  • FETCH: Fetches instructions from Program Memory based on the Program Counter (PC)
  • DECODE: Decodes the fetched instruction and prepares the necessary control signals
  • EXECUTE: Executes the instruction using the ALU and updates the relevant registers

State Machine Diagram

RESET
  ↓
LOAD → FETCH → DECODE → EXECUTE
  ↑                    ↓
  ←--------------------

Data Flow

[Program Memory] → [Instruction Register] → [Control Logic] → [ALU/Data Memory]
     ↑                                                        ↓
[Program Counter] ← [Adder/MUX] ← [Control Signals] ← [Status Register]

Instruction Set Architecture (ISA)

The processor uses 12-bit instructions with multiple formats depending on the operation type. Here are the supported instruction types:

ALU I-Type Instructions (IR[11] = 1)

These instructions perform ALU operations with an immediate value:

Bits [10:8] Operation Description
000 ADD Acc = Acc + Immediate
001 SUB Acc = Acc - Immediate
010 MOV A, Acc Acc = Acc (no change)
011 MOV A, Imm Acc = Immediate
100 AND Acc = Acc & Immediate
101 OR Acc = Acc
110 XOR Acc = Acc ^ Immediate
111 SUBR Acc = Immediate - Acc

Memory Operations (IR[9] = 1)

These instructions interact with data memory:

  • When IR[8] = 0: Read from data memory address IR[3:0] to Data Register
  • When IR[8] = 1: Write accumulator value to data memory address IR[3:0]

Jump Instructions (IR[10] = 1)

Conditional jumps based on status flags:

  • JZ (Jump if Zero): IR[9:8] = 00
  • JC (Jump if Carry): IR[9:8] = 01
  • JS (Jump if Sign): IR[9:8] = 10
  • JO (Jump if Overflow): IR[9:8] = 11

Other Operations

Instructions that don't match the above patterns simply increment the program counter.

Sample Program Analysis

The provided progr.dat file contains a sample program that demonstrates basic operations:

000000000000  // NOP - No operation, just increment PC
101100000001  // MOV A, #1 - Load immediate value 1 into accumulator
001000100000  // LOAD [0] - Load value from data memory address 0 to DR
101100000000  // MOV A, #0 - Load immediate value 0 into accumulator
001100110000  // STORE Acc to [0] - Store accumulator to data memory address 0
000100000101  // NOP - No operation, just increment PC
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP

Module Descriptions

1. Top-Level Project Module

The central module orchestrating the processor's functionality by defining and managing the state transitions. It coordinates the interaction between Program Memory, Data Memory, ALU, and Control Logic, ensuring seamless execution of instructions.

Key components:

  • State management (LOAD, FETCH, DECODE, EXECUTE)
  • Register file (PC, Acc, SR, DR, IR)
  • Control signal generation
  • Data path management

2. Control_Logic Module

Generates the necessary control signals to operate the processor's components based on the current state and the decoded instruction. It drives the behavior of registers, memory units, ALU, and multiplexers.

3. Program Memory (PMem)

Stores instructions and provides a mechanism to load instructions into memory using external files. The memory is initialized with program instructions during the LOAD state.

4. Data Memory (DMem)

Handles data storage and retrieval during program execution. Supports read and write operations controlled by enable signals.

5. Arithmetic Logic Unit (ALU)

Implements various arithmetic and logical operations. The ALU is responsible for performing core computations within the processor, supporting operations such as:

  • Addition and subtraction
  • Bitwise AND, OR, XOR
  • Logical and arithmetic shifts
  • Increment and decrement
  • Two's complement negation

6. Multiplexers (MUX1 & MUX2)

Implement data selection for various operations within the processor, choosing between different data sources based on control signals.

7. Adder Module

Performs simple addition, used primarily for incrementing the Program Counter.

Implementation Details

Registers and Memory

  • Program Counter (PC): Tracks the address of the next instruction (8-bit)
  • Accumulator (Acc): Stores results of arithmetic and logic operations (8-bit)
  • Status Register (SR): Holds status flags such as zero, carry, sign, and overflow (4-bit)
    • SR[3]: Zero flag (Z)
    • SR[2]: Carry flag (C)
    • SR[1]: Sign flag (S)
    • SR[0]: Overflow flag (O)
  • Data Register (DR): Temporarily holds data during execution (8-bit)
  • Instruction Register (IR): Stores the currently fetched instruction (12-bit)

ALU Operations

The ALU supports a variety of operations controlled by a 4-bit mode input:

Mode Operation Description
0000 ADD Addition
0001 SUB Subtraction
0010 MOV A, Operand1 Move first operand to Acc
0011 MOV A, Operand2 Move second operand to Acc
0100 AND Bitwise AND
0101 OR Bitwise OR
0110 XOR Bitwise XOR
0111 SUBR Reverse subtraction
1000 INC Increment second operand
1001 DEC Decrement second operand
1010 ROL Rotate left
1011 ROR Rotate right
1100 SHL Shift left
1101 SHR Shift right
1110 ASHR Arithmetic shift right
1111 NEG Two's complement negation

Memory Handling

The processor utilizes Program Memory (PMem) to store instructions and Data Memory (DMem) to store data. Instructions are loaded into PMem during the LOAD state, while DMem handles data read and write operations during program execution.

Instructions are loaded from an external file (progr.dat) during initialization. The format of this file should match the 12-bit instruction format expected by the processor.

Getting Started

Prerequisites

To simulate and test this processor, you'll need:

  1. Icarus Verilog - Open source Verilog simulation and synthesis tool
  2. GTKWave - Open source waveform viewer (optional but recommended)
  3. Text editor - For viewing and modifying Verilog files

Alternatively, you can use commercial tools like:

  • Xilinx Vivado
  • Intel Quartus
  • ModelSim

Installation

  1. Install Icarus Verilog:

    # Ubuntu/Debian
    sudo apt-get install iverilog gtkwave
    
    # macOS (using Homebrew)
    brew install icarus-verilog
    
    # Windows (using Chocolatey)
    choco install icarusverilog
  2. Clone or download this repository:

    git clone <repository-url>
    cd 8BitMicroFPGA

Running the Simulation

  1. Compile the design with the testbench:

    iverilog -o processor.out Project.v testbench.v
  2. Run the simulation:

    ./processor.out
  3. To view waveforms, modify the testbench to include waveform dumping:

    module testbench;
    // Inputs
    reg clk;
    reg rst;
    
    // Instantiate the Unit Under Test (UUT)
    Project uut (
        .clk(clk), 
        .rst(rst)
    );
    
    initial begin
        // Initialize Inputs
        rst = 1;
        // Wait 100 ns for global reset to finish
        #100;
        rst = 0;
        
        // Dump waves for debugging
        $dumpfile("processor.vcd");
        $dumpvars(0, testbench);
        
        // Run simulation for specific time
        #1000 $finish;
    end
    
    initial begin 
        clk = 0;
        forever #10 clk = ~clk;
    end
    
    endmodule
  4. Then compile and run again:

    iverilog -o processor.out Project.v testbench.v
    ./processor.out
    gtkwave processor.vcd

Creating Your Own Program

To create your own program:

  1. Edit the progr.dat file with your 12-bit binary instructions
  2. Each line represents one instruction in binary format
  3. The processor supports up to 10 instructions (addresses 0-9)

Example program that adds two numbers:

101100000010  // MOV A, #2 (Load 2 into accumulator)
001000000001  // ADD [1] (Add value from data memory address 1)
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP
000000000000  // NOP

Before running this program, you would need to initialize data memory location 1 with a value.

Testing

The processor design has been simulated and tested using testbenches to verify its correct operation. The modular approach facilitates easy testing of individual components as well as the overall processor.

The included testbench.v file provides a basic testing framework that:

  • Resets the processor
  • Provides clock signals
  • Allows observation of internal signals

For more comprehensive testing, you can extend the testbench to:

  • Monitor specific signals
  • Add assertions for expected behavior
  • Generate test vectors
  • Automate regression testing

Testing with Waveform Visualization

To properly analyze the processor behavior:

  1. Modify the testbench to include waveform dumping as shown in the "Running the Simulation" section
  2. Run the simulation
  3. Open the VCD file with GTKWave:
    gtkwave processor.vcd

In GTKWave, you can observe:

  • Clock and reset signals
  • Current state of the FSM
  • Register values (PC, Acc, SR, DR, IR)
  • Control signals
  • Memory contents

Contributing

Contributions are welcome! Here are some ways you can contribute:

  1. Bug Reports: If you find any issues, please open an issue with a detailed description
  2. Feature Requests: Suggest enhancements or new features
  3. Code Improvements: Submit pull requests with optimizations or bug fixes
  4. Documentation: Help improve this README or add comments to the code
  5. Testing: Add more comprehensive test cases
  6. Additional Instructions: Implement more instructions for the ISA

Before submitting a pull request:

  1. Ensure your code follows Verilog best practices
  2. Test your changes thoroughly
  3. Update documentation as needed
  4. Keep changes focused and well-described

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

MIT License

Copyright (c) 2024 Malik Mohammad Ammar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contact

For questions, suggestions, or feedback, please open an issue on the GitHub repository.


This repository contains all the necessary Verilog code and supporting files to synthesize the 8-bit soft-core processor on an FPGA. The code is well-commented, following best practices in digital design to ensure clarity and maintainability.

Releases

No releases published

Packages

No packages published