Add Batch 5 with 10 repos
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- AngeloJacobo_RISC-V/README.md +136 -0
- AngeloJacobo_RISC-V/Vivado Files/run_vivado.tcl +51 -0
- AngeloJacobo_RISC-V/rtl/fwb_master.v +558 -0
- AngeloJacobo_RISC-V/rtl/rv32i_alu.v +234 -0
- AngeloJacobo_RISC-V/rtl/rv32i_basereg.v +38 -0
- AngeloJacobo_RISC-V/rtl/rv32i_core.v +626 -0
- AngeloJacobo_RISC-V/rtl/rv32i_csr.v +540 -0
- AngeloJacobo_RISC-V/rtl/rv32i_decoder.v +249 -0
- AngeloJacobo_RISC-V/rtl/rv32i_fetch.v +140 -0
- AngeloJacobo_RISC-V/rtl/rv32i_forwarding.v +98 -0
- AngeloJacobo_RISC-V/rtl/rv32i_header.vh +62 -0
- AngeloJacobo_RISC-V/rtl/rv32i_memoryaccess.v +198 -0
- AngeloJacobo_RISC-V/rtl/rv32i_writeback.v +98 -0
- AngeloJacobo_RISC-V/test/extra/demo1.c +22 -0
- AngeloJacobo_RISC-V/test/extra/test_gpio.c +16 -0
- AngeloJacobo_RISC-V/test/extra/test_hygro.c +25 -0
- AngeloJacobo_RISC-V/test/extra/test_i2c.c +37 -0
- AngeloJacobo_RISC-V/test/extra/test_lcd.c +26 -0
- AngeloJacobo_RISC-V/test/extra/test_timer.c +26 -0
- AngeloJacobo_RISC-V/test/extra/test_uart.c +8 -0
- AngeloJacobo_RISC-V/test/extra/ultrasonic_sensor.c +36 -0
- AngeloJacobo_RISC-V/test/freertos/FreeRTOSConfig.h +143 -0
- AngeloJacobo_RISC-V/test/freertos/freertos.c +449 -0
- AngeloJacobo_RISC-V/test/freertos/freertos_old.c +254 -0
- AngeloJacobo_RISC-V/test/freertos/freertos_risc_v_chip_specific_extensions.h +74 -0
- AngeloJacobo_RISC-V/test/freertos/main_blinky.c +305 -0
- AngeloJacobo_RISC-V/test/lib/clint.c +140 -0
- AngeloJacobo_RISC-V/test/lib/gpio.c +91 -0
- AngeloJacobo_RISC-V/test/lib/hygro_pmod.c +238 -0
- AngeloJacobo_RISC-V/test/lib/i2c.c +49 -0
- AngeloJacobo_RISC-V/test/lib/lcd.c +115 -0
- AngeloJacobo_RISC-V/test/lib/printf.c +914 -0
- AngeloJacobo_RISC-V/test/lib/rv32i.h +282 -0
- AngeloJacobo_RISC-V/test/lib/uart.c +37 -0
- AngeloJacobo_RISC-V/test/lib/ultrasonic_sensor.c +27 -0
- AngeloJacobo_RISC-V/test/rv32i_soc.v +1357 -0
- AngeloJacobo_RISC-V/test/rv32i_soc_TB.v +189 -0
- AugustinJose1221_FPGA-Build/.github/FUNDING.yml +6 -0
- AugustinJose1221_FPGA-Build/README.md +367 -0
- AugustinJose1221_FPGA-Build/Templates/README.md +1 -0
- AugustinJose1221_FPGA-Build/Templates/controller.v +47 -0
- AugustinJose1221_FPGA-Build/Templates/controller_tb.v +34 -0
- AugustinJose1221_FPGA-Build/Templates/filter.v +148 -0
- AugustinJose1221_FPGA-Build/Templates/gaussian.v +12 -0
- AugustinJose1221_FPGA-Build/Templates/slave_1.v +18 -0
- AugustinJose1221_FPGA-Build/Templates/slave_1_tb.v +34 -0
- AugustinJose1221_FPGA-Build/design/Controller.v +69 -0
- AugustinJose1221_FPGA-Build/design/Grayscaler.v +93 -0
- AugustinJose1221_FPGA-Build/design/README.md +45 -0
- AugustinJose1221_FPGA-Build/design/RWM_1.v +127 -0
AngeloJacobo_RISC-V/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Table of Contents
|
| 2 |
+
- [Project Overview](https://github.com/AngeloJacobo/RISC-V/tree/main#project-overview)
|
| 3 |
+
- [Top Level Diagram (Classic 5-stage Pipeline)](https://github.com/AngeloJacobo/RISC-V/tree/main#top-level-diagram-classic-5-stage-pipeline)
|
| 4 |
+
- [Top Level Diagram (Memory-mapped Peripherals)](https://github.com/AngeloJacobo/RISC-V/tree/main#top-level-diagram-memory-mapped-peripherals)
|
| 5 |
+
- [Pipeline Features](https://github.com/AngeloJacobo/RISC-V/tree/main#pipeline-features)
|
| 6 |
+
- [Supported Features of Zicsr Extension Module](https://github.com/AngeloJacobo/RISC-V/tree/main#supported-features-of-zicsr-extension-module)
|
| 7 |
+
- [Regression Tests](https://github.com/AngeloJacobo/RISC-V/tree/main#regression-tests)
|
| 8 |
+
- [Run Individual Tests](https://github.com/AngeloJacobo/RISC-V/tree/main#run-individual-tests)
|
| 9 |
+
- [Install Design to FPGA (CMOD S7 FPGA Board)](https://github.com/AngeloJacobo/RISC-V/tree/main#install-design-to-fpga-cmod-s7-fpga-board)
|
| 10 |
+
- [Sample Application [Smart Garden Assistant with Real-time Monitoring and Security]](https://github.com/AngeloJacobo/RISC-V/tree/main#sample-application-smart-garden-assistant-with-real-time-monitoring-and-security)
|
| 11 |
+
- [Performance Metrics](https://github.com/AngeloJacobo/RISC-V/tree/main#performance-metrics)
|
| 12 |
+
- [Paper Access](https://github.com/AngeloJacobo/RISC-V/tree/main#paper-access)
|
| 13 |
+
|
| 14 |
+
## Project Overview
|
| 15 |
+
This project involves the design and implementation of the RISC-V Base 32 Integer core using Verilog HDL. This work includes a 5-stage pipeline processor core, which supports the Zicsr (Control Status Registers) extension. The design is RISC-V compliant and has successfully passed the `rv32ui` (RV32 User-Mode Integer-Only) and `rv32mi` (RV32 Machine-Mode Integer-Only) [tests provided by RISC-V International](https://github.com/riscv-software-src/riscv-tests). Additionally, this includes support for [FreeRTOS](https://www.freertos.org/).
|
| 16 |
+
|
| 17 |
+
The RISC-V ISA implemented here is based on [Volume 1, Unprivileged Spec v. 20191213](https://github.com/riscv/riscv-isa-manual/releases/tag/Ratified-IMAFDQC) and [Volume 2, Privileged Spec v. 20211203.](https://github.com/riscv/riscv-isa-manual/releases/tag/Priv-v1.12)
|
| 18 |
+
|
| 19 |
+
Inside the `rtl/` folder are the following:
|
| 20 |
+
- `rv32i_core.v` = top module for the RV32I core and contains formal verification properties
|
| 21 |
+
- `rv32i_forwarding.v` = operand forwarding logic for data dependency hazards
|
| 22 |
+
- `rv32i_basereg.v` = regfile controller for the 32 integer base registers
|
| 23 |
+
- `rv32i_fetch.v` = retrieves instruction from the memory [FETCH STAGE]
|
| 24 |
+
- `rv32i_decoder.v`= decodes the 32 bit instruction [DECODE STAGE]
|
| 25 |
+
- `rv32i_alu.v` = execute arithmetic operations and determines next `PC` and `rd` values [EXECUTE STAGE]
|
| 26 |
+
- `rv32i_memoryaccess.v` = sends and retrieves data to and from the memory [MEMORYACCESS STAGE]
|
| 27 |
+
- `rv32i_csr.v` = Zicsr extension module [executes parallel to MEMORYACCESS STAGE]
|
| 28 |
+
- `rv32i_writeback.v` = writes `rd` to basereg and handles pipeline flushes due to traps [WRITEBACK STAGE]
|
| 29 |
+
- `rv32i_header.vh` = header file which contains all necessary constants, magic numbers, and parameters
|
| 30 |
+
|
| 31 |
+
Inside the `test/` folder are the following:
|
| 32 |
+
- `test.sh` = bash script for automating regression tests, program compilation, and design installation to FPGA board
|
| 33 |
+
- `entry.s` = start-up assembly code used by C programs
|
| 34 |
+
- `rv32i_linkerscript.ld` = script used by linker for partitioning memory sections
|
| 35 |
+
- `rv32i_core.sby` = SymbiYosys script for formal verification
|
| 36 |
+
- `rv32i_soc_TB.v` = testbench for `rv32i_soc`
|
| 37 |
+
- `rv32i_soc.v` = complete package containing the rv32i core, main memory, IO peripherals (CLINT, I2C, UART, and GPIO), and the memory wrapper.
|
| 38 |
+
- `wave.do` = Modelsim waveform template file
|
| 39 |
+
- `wave.gtkw` = GTKWave waveform template file
|
| 40 |
+
- `freertos/` folder = contains files for running FreeRTOS (`FreeRTOSConfig.h` and `freertos_risc_v_chip_specific_extensions.h`)
|
| 41 |
+
- `extra/` folder = contains custom assembly testfiles for all basic instructions, system instructions, and pipeline hazards.
|
| 42 |
+
- `lib/` folder = contains custom software library. The function APIs can be found on `rv32i.h`. This includes:
|
| 43 |
+
- LCD 1602 driver
|
| 44 |
+
- HygroPMOD (Digilent) driver
|
| 45 |
+
- DS1307 Real-time Clock driver
|
| 46 |
+
- CLINT (Core Logic Interrupt) interface
|
| 47 |
+
- UART interface
|
| 48 |
+
- I2C interface
|
| 49 |
+
- GPIOs interface
|
| 50 |
+
- sprintf implementation
|
| 51 |
+
|
| 52 |
+
Inside the `Vivado Files/` folder are the following:
|
| 53 |
+
- `run_vivado.tcl` = script for running Vivado in non-project mode. Used by `test.sh` to synthesize, implement, and install the design to the FPGA board.
|
| 54 |
+
- `Cmod-S7-25-Master.xdc` = constraint file used by Vivado to install design to the [CMOD S7 FPGA Board](https://digilent.com/reference/programmable-logic/cmod-s7/start)
|
| 55 |
+
|
| 56 |
+
## Top Level Diagram (Classic 5-stage Pipeline)
|
| 57 |
+

|
| 58 |
+
## Top Level Diagram (Memory-mapped Peripherals)
|
| 59 |
+

|
| 60 |
+
|
| 61 |
+
## Pipeline Features
|
| 62 |
+
- 5 pipelined stages
|
| 63 |
+
- Separate data and instruction memory interface **[Harvard architecture]**
|
| 64 |
+
- Load instructions take a minimum of 3 clk cycles plus any additional memory stalls
|
| 65 |
+
- Taken branch and jump instructions take a minimum of 3 clk cycles **[No Branch Prediction Used]**
|
| 66 |
+
- An instruction with data dependency to the next instruction that is a CSR write or Load instruction will take a minimum of 2 clk cycles **[Operand Forwarding used]**
|
| 67 |
+
- **All remaining instructions take a minimum of 1 clk cycle**
|
| 68 |
+
|
| 69 |
+
## Supported Features of Zicsr Extension Module
|
| 70 |
+
- **CSR instructions**: `CSRRW`, `CSRRS`, `CSRRC`, `CSRRWI`, `CSRRSI`, `CSRRCI`
|
| 71 |
+
- **Interrupts**: `External Interrupt`, `Timer Interrupt`, `Software Interrupt`
|
| 72 |
+
- **Exceptions**: `Illegal Instruction`, `Instruction Address Misaligned`, `Ecall`, `Ebreak`, `Load/Store Address Misaligned`
|
| 73 |
+
- **All relevant machine level CSRs**
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
## Regression Tests
|
| 77 |
+
The RISC-V toolchain `riscv64-unknown-elf-` and Modelsim executables `vsim` and `vlog` must be callable from PATH. If Modelsim executables are missing, the script will then call Icarus Verilog executables `iverilog` and `vvp` instead. Run **regression tests** inside `test/` directory either with:
|
| 78 |
+
- `$ ./test.sh` = run regression tests for both `riscv-tests/isa/rv32ui/` and `riscv-tests/isa/rv32mi/`
|
| 79 |
+
- `$ ./test.sh rv32ui` = run regression tests only for the `riscv-tests/isa/rv32ui/`
|
| 80 |
+
- `$ ./test.sh rv32mi` = run regression tests only for the `riscv-tests/isa/rv32mi/`
|
| 81 |
+
- `$ ./test.sh extra` = run regression tests for `extra/`
|
| 82 |
+
- `$ ./test.sh all` = run regression tests for `riscv-tests/isa/rv32ui/`, `riscv-tests/isa/rv32mi/`, and `extra/`
|
| 83 |
+
- `$ ./test.sh compile` = compile-only the rtl files
|
| 84 |
+
|
| 85 |
+
## Run Individual Tests
|
| 86 |
+
- `$ ./test.sh <testfile>` = test and debug testfile (without simulating) which is located at INDIVIDUAL_TESTDIR
|
| 87 |
+
- `$ ./test.sh <testfile> -gui` = test and debug testfile and open wave in Icarus
|
| 88 |
+
- `$ ./test.sh <testfile> -nosim` = compile and debug testfile without simulating it
|
| 89 |
+
Below is the expected output after running `$ ./test.sh`:
|
| 90 |
+
|
| 91 |
+

|
| 92 |
+
|
| 93 |
+
## Install Design to FPGA (CMOD S7 FPGA Board)
|
| 94 |
+
- `$ ./test.sh <design> -install` = compile and install design (located at INDIVIDUAL_TESTDIR) to FPGA board
|
| 95 |
+
- `$ ./test.sh freertos -install` = compile and install FreeRTOS program to FPGA board
|
| 96 |
+
|
| 97 |
+
## Sample Application [Smart Garden Assistant with Real-time Monitoring and Security]
|
| 98 |
+
The application code for this project can be found in `test/freertos/freertos.c`. The schematic is presented below along with the mobile app to access the device via Bluetooth. This mobile app was developed using [MIT App Inventor](https://appinventor.mit.edu/). The `apk` file is located at `test/freertos/mobile_app.apk` and the `aia` file, which can be imported to MIT App Inventor for further customization, is at `test/freertos/mobile_app.aia`.
|
| 99 |
+

|
| 100 |
+
Below is the video demonstration for the sample application:
|
| 101 |
+
|
| 102 |
+
[](https://youtu.be/azBM6czbunY)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
## Performance Metrics
|
| 106 |
+
The whole design (core + zicsr + memory-mapped peripherals + 16KB memory) is implemented on an Arty S7 FPGA board. The following metrics were collected:
|
| 107 |
+
- Maximum operating frequency: 90MHz
|
| 108 |
+
- Resource Utilization: 1780 LUT, 1346 FF
|
| 109 |
+
- Total Power: 0.106W
|
| 110 |
+
|
| 111 |
+

|
| 112 |
+
|
| 113 |
+
## Paper Access
|
| 114 |
+
The title of the paper is `Design, Implementation, and Verification of a 32-bit RISC-V Processor with Real-Time Operating System and Regression Testing Framework`. As of this moment, the paper remains unpublished so I have refrained on linking it in this public repository. But I will post it here as soon as it is published (hopefully). But if you are still interested to have a look at the paper, you can chat me via [my LinkedIn](https://www.linkedin.com/in/angelo-jacobo/) so we can discuss it. Below is the abstract of the paper:
|
| 115 |
+
|
| 116 |
+
> RISC-V, an open standard Instruction Set Architecture (ISA), is gaining traction in various industries recently due to its open nature, straightforward design, modularity, and scalability. This paper presents the design, implementation, and verification of a 32-bit RISC-V processor with Real-Time Operating System (RTOS) support and a regression testing framework. The RISC-V core is designed using Verilog Hardware Description Language (HDL) based on the classic five-stage pipeline architecture. A comprehensive regression test is then conducted to ensure the processor core's compliance with the RISC-V ISA specifications. Thereafter, FreeRTOS is then integrated into the core, enabling effective management of time-sensitive microcontroller applications and multitasking, proving its relevance in the Internet of Things (IoT) domain. Top-level peripherals and a custom software library are also integrated to enhance the core's versatility and user-friendliness. A real-world application, the "Smart Garden Assistant with Real-time Monitoring and Security," is then developed to demonstrate the functioning RISC-V core with RTOS capability. Finally, a design evaluation is conducted and a comparative analysis is then performed to assess the design implementation against previous studies. This study aims to contribute to the growing body of research on RISC-V processors, emphasizing their value in the emerging era of open-source and customizable processor architectures.
|
| 117 |
+
|
| 118 |
+
## Goal Checklist
|
| 119 |
+
:white_check_mark: Automate the testbench
|
| 120 |
+
:white_check_mark: Add Zicsr extension
|
| 121 |
+
:white_check_mark: Pass the Official RISC-V International Tests
|
| 122 |
+
:white_check_mark: Convert FSM based core implementation to pipeline
|
| 123 |
+
:white_check_mark: Add formal verification
|
| 124 |
+
:white_check_mark: Be able to run C codes
|
| 125 |
+
:white_check_mark: Add FreeRTOS Support
|
| 126 |
+
:white_check_mark: Add custom software library
|
| 127 |
+
:white_check_mark: Create a sample application using the core
|
| 128 |
+
:black_square_button: Add AXI interface
|
| 129 |
+
|
| 130 |
+
# Donate
|
| 131 |
+
Support these open-source projects by donating
|
| 132 |
+
|
| 133 |
+
[](https://www.paypal.com/donate?hosted_button_id=GBJQGJNCJZVRU)
|
| 134 |
+
|
| 135 |
+
# Inquiries
|
| 136 |
+
Connect with me on LinkedIn: https://www.linkedin.com/in/angelo-jacobo/
|
AngeloJacobo_RISC-V/Vivado Files/run_vivado.tcl
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# run_vivado.tcl
|
| 2 |
+
# NOTE: typical usage would be "vivado -mode tcl -source run_vivado.tcl"
|
| 3 |
+
#
|
| 4 |
+
# STEP#0: define output directory area.
|
| 5 |
+
#
|
| 6 |
+
set outputDir ./runs
|
| 7 |
+
file delete -force ./runs
|
| 8 |
+
file mkdir $outputDir
|
| 9 |
+
#
|
| 10 |
+
# STEP#1: setup design sources and constraints
|
| 11 |
+
#
|
| 12 |
+
read_verilog [ glob ../rtl/*.v ]
|
| 13 |
+
read_verilog ../test/rv32i_soc.v
|
| 14 |
+
read_mem ../test/memory.mem
|
| 15 |
+
read_xdc ./Cmod-S7-25-Master.xdc
|
| 16 |
+
#
|
| 17 |
+
# STEP#2: run synthesis, report utilization and timing estimates, write checkpoint design
|
| 18 |
+
#
|
| 19 |
+
synth_design -top rv32i_soc -part xc7s25csga225-1
|
| 20 |
+
#
|
| 21 |
+
# STEP#3: run placement and logic optimzation, report utilization and timing estimates, write checkpoint design
|
| 22 |
+
#
|
| 23 |
+
opt_design
|
| 24 |
+
place_design
|
| 25 |
+
phys_opt_design
|
| 26 |
+
#
|
| 27 |
+
# STEP#4: run router, report actual utilization and timing, write checkpoint design, run drc, write verilog and xdc out
|
| 28 |
+
#
|
| 29 |
+
route_design
|
| 30 |
+
#
|
| 31 |
+
# STEP#5: generate a bitstream
|
| 32 |
+
#
|
| 33 |
+
write_bitstream -force $outputDir/rv32i_soc.bit
|
| 34 |
+
|
| 35 |
+
# Connect to the Digilent Cable on localhost:3121
|
| 36 |
+
open_hw_manager
|
| 37 |
+
connect_hw_server -url localhost:3121
|
| 38 |
+
current_hw_target [get_hw_targets */xilinx_tcf/Digilent/210376AC734EA]
|
| 39 |
+
open_hw_target
|
| 40 |
+
|
| 41 |
+
# Program and Refresh the XC7K325T Device
|
| 42 |
+
|
| 43 |
+
current_hw_device [lindex [get_hw_devices] 0]
|
| 44 |
+
refresh_hw_device -update_hw_probes false [lindex [get_hw_devices] 0]
|
| 45 |
+
set_property PROGRAM.FILE {./runs/rv32i_soc.bit} [lindex [get_hw_devices] 0]
|
| 46 |
+
|
| 47 |
+
program_hw_devices [lindex [get_hw_devices] 0]
|
| 48 |
+
refresh_hw_device [lindex [get_hw_devices] 0]
|
| 49 |
+
quit
|
| 50 |
+
|
| 51 |
+
|
AngeloJacobo_RISC-V/rtl/fwb_master.v
ADDED
|
@@ -0,0 +1,558 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 2 |
+
//
|
| 3 |
+
// Filename: fwb_master.v
|
| 4 |
+
// {{{
|
| 5 |
+
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
+
//
|
| 7 |
+
// Purpose: This file describes the rules of a wishbone interaction from the
|
| 8 |
+
// perspective of a wishbone master. These formal rules may be
|
| 9 |
+
// used with SymbiYosys to *prove* that the master properly handles
|
| 10 |
+
// outgoing transactions and incoming responses.
|
| 11 |
+
//
|
| 12 |
+
// This module contains no functional logic. It is intended for formal
|
| 13 |
+
// verification only. The outputs returned, the number of requests that
|
| 14 |
+
// have been made, the number of acknowledgements received, and the number
|
| 15 |
+
// of outstanding requests, are designed for further formal verification
|
| 16 |
+
// purposes *only*.
|
| 17 |
+
//
|
| 18 |
+
// This file is different from a companion formal_slave.v file in that the
|
| 19 |
+
// assertions are made on the outputs of the wishbone master: o_wb_cyc,
|
| 20 |
+
// o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, and o_wb_sel, while only
|
| 21 |
+
// assumptions are made about the inputs: i_wb_stall, i_wb_ack, i_wb_data,
|
| 22 |
+
// i_wb_err. In the formal_slave.v, assumptions are made about the
|
| 23 |
+
// slave inputs (the master outputs), and assertions are made about the
|
| 24 |
+
// slave outputs (the master inputs).
|
| 25 |
+
//
|
| 26 |
+
// In order to make it easier to compare the slave against the master,
|
| 27 |
+
// assumptions with respect to the slave have been marked with the
|
| 28 |
+
// `SLAVE_ASSUME macro. Similarly, assertions the slave would make have
|
| 29 |
+
// been marked with `SLAVE_ASSERT. This allows the master to redefine
|
| 30 |
+
// these two macros to be from his perspective, and therefore the
|
| 31 |
+
// diffs between the two files actually show true differences, rather
|
| 32 |
+
// than just these differences in perspective.
|
| 33 |
+
//
|
| 34 |
+
//
|
| 35 |
+
// Creator: Dan Gisselquist, Ph.D.
|
| 36 |
+
// Gisselquist Technology, LLC
|
| 37 |
+
//
|
| 38 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 39 |
+
// }}}
|
| 40 |
+
// Copyright (C) 2017-2023, Gisselquist Technology, LLC
|
| 41 |
+
// {{{
|
| 42 |
+
// This program is free software (firmware): you can redistribute it and/or
|
| 43 |
+
// modify it under the terms of the GNU General Public License as published
|
| 44 |
+
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 45 |
+
// your option) any later version.
|
| 46 |
+
//
|
| 47 |
+
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 48 |
+
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 49 |
+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 50 |
+
// for more details.
|
| 51 |
+
//
|
| 52 |
+
// You should have received a copy of the GNU General Public License along
|
| 53 |
+
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
| 54 |
+
// target there if the PDF file isn't present.) If not, see
|
| 55 |
+
// <http://www.gnu.org/licenses/> for a copy.
|
| 56 |
+
// }}}
|
| 57 |
+
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 58 |
+
// {{{
|
| 59 |
+
// http://www.gnu.org/licenses/gpl.html
|
| 60 |
+
//
|
| 61 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 62 |
+
//
|
| 63 |
+
//
|
| 64 |
+
`default_nettype none
|
| 65 |
+
// }}}
|
| 66 |
+
module fwb_master #(
|
| 67 |
+
// {{{
|
| 68 |
+
parameter AW=32, DW=32,
|
| 69 |
+
parameter F_MAX_STALL = 0,
|
| 70 |
+
F_MAX_ACK_DELAY = 0,
|
| 71 |
+
parameter F_LGDEPTH = 4,
|
| 72 |
+
parameter [(F_LGDEPTH-1):0] F_MAX_REQUESTS = 0,
|
| 73 |
+
// OPT_BUS_ABORT: If true, the master can drop CYC at any time
|
| 74 |
+
// and must drop CYC following any bus error
|
| 75 |
+
parameter [0:0] OPT_BUS_ABORT = 1'b1,
|
| 76 |
+
//
|
| 77 |
+
// If true, allow the bus to be kept open when there are no
|
| 78 |
+
// outstanding requests. This is useful for any master that
|
| 79 |
+
// might execute a read modify write cycle, such as an atomic
|
| 80 |
+
// add.
|
| 81 |
+
parameter [0:0] F_OPT_RMW_BUS_OPTION = 1,
|
| 82 |
+
//
|
| 83 |
+
//
|
| 84 |
+
// If true, allow the bus to issue multiple discontinuous
|
| 85 |
+
// requests.
|
| 86 |
+
// Unlike F_OPT_RMW_BUS_OPTION, these requests may be issued
|
| 87 |
+
// while other requests are outstanding
|
| 88 |
+
parameter [0:0] F_OPT_DISCONTINUOUS = 1,
|
| 89 |
+
//
|
| 90 |
+
//
|
| 91 |
+
// If true, insist that there be a minimum of a single clock
|
| 92 |
+
// delay between request and response. This defaults to off
|
| 93 |
+
// since the wishbone specification specifically doesn't
|
| 94 |
+
// require this. However, some interfaces do, so we allow it
|
| 95 |
+
// as an option here.
|
| 96 |
+
parameter [0:0] F_OPT_MINCLOCK_DELAY = 0,
|
| 97 |
+
//
|
| 98 |
+
//
|
| 99 |
+
//
|
| 100 |
+
localparam [(F_LGDEPTH-1):0] MAX_OUTSTANDING
|
| 101 |
+
= {(F_LGDEPTH){1'b1}},
|
| 102 |
+
localparam MAX_DELAY = (F_MAX_STALL > F_MAX_ACK_DELAY)
|
| 103 |
+
? F_MAX_STALL : F_MAX_ACK_DELAY,
|
| 104 |
+
localparam DLYBITS= (MAX_DELAY < 4) ? 2
|
| 105 |
+
: (MAX_DELAY >= 65536) ? 32
|
| 106 |
+
: $clog2(MAX_DELAY+1),
|
| 107 |
+
//
|
| 108 |
+
parameter [0:0] F_OPT_SHORT_CIRCUIT_PROOF = 0,
|
| 109 |
+
//
|
| 110 |
+
// If this is the source of a request, then we can assume STB and CYC
|
| 111 |
+
// will initially start out high. Master interfaces following the
|
| 112 |
+
// source on the way to the slave may not have this property
|
| 113 |
+
parameter [0:0] F_OPT_SOURCE = 0
|
| 114 |
+
//
|
| 115 |
+
//
|
| 116 |
+
// }}}
|
| 117 |
+
) (
|
| 118 |
+
// {{{
|
| 119 |
+
input wire i_clk, i_reset,
|
| 120 |
+
// The Wishbone bus
|
| 121 |
+
input wire i_wb_cyc, i_wb_stb, i_wb_we,
|
| 122 |
+
input wire [(AW-1):0] i_wb_addr,
|
| 123 |
+
input wire [(DW-1):0] i_wb_data,
|
| 124 |
+
input wire [(DW/8-1):0] i_wb_sel,
|
| 125 |
+
//
|
| 126 |
+
input wire i_wb_ack,
|
| 127 |
+
input wire i_wb_stall,
|
| 128 |
+
input wire [(DW-1):0] i_wb_idata,
|
| 129 |
+
input wire i_wb_err,
|
| 130 |
+
// Some convenience output parameters
|
| 131 |
+
output reg [(F_LGDEPTH-1):0] f_nreqs, f_nacks,
|
| 132 |
+
output wire [(F_LGDEPTH-1):0] f_outstanding
|
| 133 |
+
// }}}
|
| 134 |
+
);
|
| 135 |
+
|
| 136 |
+
`define SLAVE_ASSUME assert
|
| 137 |
+
`define SLAVE_ASSERT assume
|
| 138 |
+
//
|
| 139 |
+
// Let's just make sure our parameters are set up right
|
| 140 |
+
// {{{
|
| 141 |
+
initial assert(F_MAX_REQUESTS < {(F_LGDEPTH){1'b1}});
|
| 142 |
+
// }}}
|
| 143 |
+
|
| 144 |
+
// f_request
|
| 145 |
+
// {{{
|
| 146 |
+
// Wrap the request line in a bundle. The top bit, named STB_BIT,
|
| 147 |
+
// is the bit indicating whether the request described by this vector
|
| 148 |
+
// is a valid request or not.
|
| 149 |
+
//
|
| 150 |
+
localparam STB_BIT = 2+AW+DW+DW/8-1;
|
| 151 |
+
wire [STB_BIT:0] f_request;
|
| 152 |
+
assign f_request = { i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel };
|
| 153 |
+
// }}}
|
| 154 |
+
|
| 155 |
+
// f_past_valid and i_reset
|
| 156 |
+
// {{{
|
| 157 |
+
// A quick register to be used later to know if the $past() operator
|
| 158 |
+
// will yield valid result
|
| 159 |
+
reg f_past_valid;
|
| 160 |
+
initial f_past_valid = 1'b0;
|
| 161 |
+
always @(posedge i_clk)
|
| 162 |
+
f_past_valid <= 1'b1;
|
| 163 |
+
|
| 164 |
+
always @(*)
|
| 165 |
+
if (!f_past_valid)
|
| 166 |
+
`SLAVE_ASSUME(i_reset);
|
| 167 |
+
// }}}
|
| 168 |
+
////////////////////////////////////////////////////////////////////////
|
| 169 |
+
//
|
| 170 |
+
// Assertions regarding the initial (and reset) state
|
| 171 |
+
// {{{
|
| 172 |
+
////////////////////////////////////////////////////////////////////////
|
| 173 |
+
//
|
| 174 |
+
//
|
| 175 |
+
|
| 176 |
+
//
|
| 177 |
+
// Assume we start from a reset condition
|
| 178 |
+
initial assert(i_reset);
|
| 179 |
+
initial `SLAVE_ASSUME(!i_wb_cyc);
|
| 180 |
+
initial `SLAVE_ASSUME(!i_wb_stb);
|
| 181 |
+
//
|
| 182 |
+
initial `SLAVE_ASSERT(!i_wb_ack);
|
| 183 |
+
initial `SLAVE_ASSERT(!i_wb_err);
|
| 184 |
+
|
| 185 |
+
`ifdef VERIFIC
|
| 186 |
+
always @(*)
|
| 187 |
+
if (!f_past_valid)
|
| 188 |
+
begin
|
| 189 |
+
`SLAVE_ASSUME(!i_wb_cyc);
|
| 190 |
+
`SLAVE_ASSUME(!i_wb_stb);
|
| 191 |
+
//
|
| 192 |
+
`SLAVE_ASSERT(!i_wb_ack);
|
| 193 |
+
`SLAVE_ASSERT(!i_wb_err);
|
| 194 |
+
end
|
| 195 |
+
`endif
|
| 196 |
+
always @(posedge i_clk)
|
| 197 |
+
if ((!f_past_valid)||($past(i_reset)))
|
| 198 |
+
begin
|
| 199 |
+
`SLAVE_ASSUME(!i_wb_cyc);
|
| 200 |
+
`SLAVE_ASSUME(!i_wb_stb);
|
| 201 |
+
//
|
| 202 |
+
`SLAVE_ASSERT(!i_wb_ack);
|
| 203 |
+
`SLAVE_ASSERT(!i_wb_err);
|
| 204 |
+
end
|
| 205 |
+
|
| 206 |
+
always @(*)
|
| 207 |
+
if (!f_past_valid)
|
| 208 |
+
`SLAVE_ASSUME(!i_wb_cyc);
|
| 209 |
+
// }}}
|
| 210 |
+
////////////////////////////////////////////////////////////////////////
|
| 211 |
+
//
|
| 212 |
+
// Bus requests
|
| 213 |
+
// {{{
|
| 214 |
+
////////////////////////////////////////////////////////////////////////
|
| 215 |
+
//
|
| 216 |
+
//
|
| 217 |
+
|
| 218 |
+
// Following any bus error, the CYC line should be dropped to abort
|
| 219 |
+
// the transaction
|
| 220 |
+
always @(posedge i_clk)
|
| 221 |
+
if (f_past_valid && OPT_BUS_ABORT && $past(i_wb_err)&& $past(i_wb_cyc))
|
| 222 |
+
`SLAVE_ASSUME(!i_wb_cyc);
|
| 223 |
+
|
| 224 |
+
always @(*)
|
| 225 |
+
if (!OPT_BUS_ABORT && !i_reset && (f_nreqs != f_nacks))
|
| 226 |
+
`SLAVE_ASSUME(i_wb_cyc);
|
| 227 |
+
|
| 228 |
+
always @(posedge i_clk)
|
| 229 |
+
if (f_past_valid && !OPT_BUS_ABORT
|
| 230 |
+
&& $past(!i_reset && i_wb_stb && i_wb_stall))
|
| 231 |
+
`SLAVE_ASSUME(i_wb_cyc);
|
| 232 |
+
|
| 233 |
+
// STB can only be true if CYC is also true
|
| 234 |
+
always @(*)
|
| 235 |
+
if (i_wb_stb)
|
| 236 |
+
`SLAVE_ASSUME(i_wb_cyc);
|
| 237 |
+
|
| 238 |
+
// If a request was both outstanding and stalled on the last clock,
|
| 239 |
+
// then nothing should change on this clock regarding it.
|
| 240 |
+
always @(posedge i_clk)
|
| 241 |
+
if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wb_stb))
|
| 242 |
+
&&($past(i_wb_stall))&&(i_wb_cyc))
|
| 243 |
+
begin
|
| 244 |
+
`SLAVE_ASSUME(i_wb_stb);
|
| 245 |
+
`SLAVE_ASSUME(i_wb_we == $past(i_wb_we));
|
| 246 |
+
`SLAVE_ASSUME(i_wb_addr == $past(i_wb_addr));
|
| 247 |
+
`SLAVE_ASSUME(i_wb_sel == $past(i_wb_sel));
|
| 248 |
+
if (i_wb_we)
|
| 249 |
+
`SLAVE_ASSUME(i_wb_data == $past(i_wb_data));
|
| 250 |
+
end
|
| 251 |
+
|
| 252 |
+
// Within any series of STB/requests, the direction of the request
|
| 253 |
+
// may not change.
|
| 254 |
+
always @(posedge i_clk)
|
| 255 |
+
if ((f_past_valid)&&($past(i_wb_stb))&&(i_wb_stb))
|
| 256 |
+
`SLAVE_ASSUME(i_wb_we == $past(i_wb_we));
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
// Within any given bus cycle, the direction may *only* change when
|
| 260 |
+
// there are no further outstanding requests.
|
| 261 |
+
always @(posedge i_clk)
|
| 262 |
+
if ((f_past_valid)&&(f_outstanding > 0))
|
| 263 |
+
`SLAVE_ASSUME(i_wb_we == $past(i_wb_we));
|
| 264 |
+
|
| 265 |
+
// Write requests must also set one (or more) of i_wb_sel
|
| 266 |
+
//
|
| 267 |
+
// This test has been removed since down-sizers (taking bus from width
|
| 268 |
+
// DW to width dw < DW) might actually create empty requests that this
|
| 269 |
+
// would prevent. Re-enabling it would also complicate AXI to WB
|
| 270 |
+
// transfers, since AXI explicitly allows WSTRB == 0. Finally, this
|
| 271 |
+
// criteria isn't found in the WB spec--so while it might be a good
|
| 272 |
+
// idea to check, in hind sight there are too many exceptions to be
|
| 273 |
+
// dogmatic about it.
|
| 274 |
+
//
|
| 275 |
+
// always @(*)
|
| 276 |
+
// if ((i_wb_stb)&&(i_wb_we))
|
| 277 |
+
// `SLAVE_ASSUME(|i_wb_sel);
|
| 278 |
+
|
| 279 |
+
// }}}
|
| 280 |
+
////////////////////////////////////////////////////////////////////////
|
| 281 |
+
//
|
| 282 |
+
// Bus responses
|
| 283 |
+
// {{{
|
| 284 |
+
////////////////////////////////////////////////////////////////////////
|
| 285 |
+
//
|
| 286 |
+
//
|
| 287 |
+
|
| 288 |
+
// If CYC was low on the last clock, then both ACK and ERR should be
|
| 289 |
+
// low on this clock.
|
| 290 |
+
always @(posedge i_clk)
|
| 291 |
+
if ((f_past_valid)&&(!$past(i_wb_cyc))&&(!i_wb_cyc))
|
| 292 |
+
begin
|
| 293 |
+
`SLAVE_ASSERT(!i_wb_ack);
|
| 294 |
+
`SLAVE_ASSERT(!i_wb_err);
|
| 295 |
+
// Stall may still be true--such as when we are not
|
| 296 |
+
// selected at some arbiter between us and the slave
|
| 297 |
+
end
|
| 298 |
+
|
| 299 |
+
//
|
| 300 |
+
// Any time the CYC line drops, it is possible that there may be a
|
| 301 |
+
// remaining (registered) ACK or ERR that hasn't yet been returned.
|
| 302 |
+
// Restrict such out of band returns so that they are *only* returned
|
| 303 |
+
// if there is an outstanding operation.
|
| 304 |
+
//
|
| 305 |
+
// Update: As per spec, WB-classic to WB-pipeline conversions require
|
| 306 |
+
// that the ACK|ERR might come back on the same cycle that STB
|
| 307 |
+
// is low, yet also be registered. Hence, if STB & STALL are true on
|
| 308 |
+
// one cycle, then CYC is dropped, ACK|ERR might still be true on the
|
| 309 |
+
// cycle when CYC is dropped
|
| 310 |
+
always @(posedge i_clk)
|
| 311 |
+
if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wb_cyc))&&(!i_wb_cyc))
|
| 312 |
+
begin
|
| 313 |
+
// Note that, unlike f_outstanding, f_nreqs and f_nacks are both
|
| 314 |
+
// registered. Hence, we can check here if a response is still
|
| 315 |
+
// pending. If not, no response should be returned.
|
| 316 |
+
if (f_nreqs == f_nacks)
|
| 317 |
+
begin
|
| 318 |
+
`SLAVE_ASSERT(!i_wb_ack);
|
| 319 |
+
`SLAVE_ASSERT(!i_wb_err);
|
| 320 |
+
end
|
| 321 |
+
end
|
| 322 |
+
|
| 323 |
+
// ACK and ERR may never both be true at the same time
|
| 324 |
+
always @(*)
|
| 325 |
+
`SLAVE_ASSERT((!i_wb_ack)||(!i_wb_err));
|
| 326 |
+
// }}}
|
| 327 |
+
////////////////////////////////////////////////////////////////////////
|
| 328 |
+
//
|
| 329 |
+
// Stall checking
|
| 330 |
+
// {{{
|
| 331 |
+
////////////////////////////////////////////////////////////////////////
|
| 332 |
+
//
|
| 333 |
+
//
|
| 334 |
+
generate if (F_MAX_STALL > 0)
|
| 335 |
+
begin : MXSTALL
|
| 336 |
+
//
|
| 337 |
+
// Assume the slave cannnot stall for more than F_MAX_STALL
|
| 338 |
+
// counts. We'll count this forward any time STB and STALL
|
| 339 |
+
// are both true.
|
| 340 |
+
//
|
| 341 |
+
reg [(DLYBITS-1):0] f_stall_count;
|
| 342 |
+
|
| 343 |
+
initial f_stall_count = 0;
|
| 344 |
+
always @(posedge i_clk)
|
| 345 |
+
if ((!i_reset)&&(i_wb_stb)&&(i_wb_stall))
|
| 346 |
+
f_stall_count <= f_stall_count + 1'b1;
|
| 347 |
+
else
|
| 348 |
+
f_stall_count <= 0;
|
| 349 |
+
|
| 350 |
+
always @(*)
|
| 351 |
+
if (i_wb_cyc)
|
| 352 |
+
`SLAVE_ASSERT(f_stall_count < F_MAX_STALL);
|
| 353 |
+
end endgenerate
|
| 354 |
+
// }}}
|
| 355 |
+
////////////////////////////////////////////////////////////////////////
|
| 356 |
+
//
|
| 357 |
+
// Maximum delay in any response
|
| 358 |
+
// {{{
|
| 359 |
+
////////////////////////////////////////////////////////////////////////
|
| 360 |
+
//
|
| 361 |
+
//
|
| 362 |
+
|
| 363 |
+
generate if (F_MAX_ACK_DELAY > 0)
|
| 364 |
+
begin : MXWAIT
|
| 365 |
+
//
|
| 366 |
+
// Assume the slave will respond within F_MAX_ACK_DELAY cycles,
|
| 367 |
+
// counted either from the end of the last request, or from the
|
| 368 |
+
// last ACK received
|
| 369 |
+
//
|
| 370 |
+
reg [(DLYBITS-1):0] f_ackwait_count;
|
| 371 |
+
|
| 372 |
+
initial f_ackwait_count = 0;
|
| 373 |
+
always @(posedge i_clk)
|
| 374 |
+
if ((!i_reset)&&(i_wb_cyc)&&(!i_wb_stb)
|
| 375 |
+
&&(!i_wb_ack)&&(!i_wb_err)
|
| 376 |
+
&&(f_outstanding > 0))
|
| 377 |
+
f_ackwait_count <= f_ackwait_count + 1'b1;
|
| 378 |
+
else
|
| 379 |
+
f_ackwait_count <= 0;
|
| 380 |
+
|
| 381 |
+
always @(*)
|
| 382 |
+
if ((!i_reset)&&(i_wb_cyc)&&(!i_wb_stb)
|
| 383 |
+
&&(!i_wb_ack)&&(!i_wb_err)
|
| 384 |
+
&&(f_outstanding > 0))
|
| 385 |
+
`SLAVE_ASSERT(f_ackwait_count < F_MAX_ACK_DELAY);
|
| 386 |
+
end endgenerate
|
| 387 |
+
// }}}
|
| 388 |
+
////////////////////////////////////////////////////////////////////////
|
| 389 |
+
//
|
| 390 |
+
// Count outstanding requests vs acknowledgments
|
| 391 |
+
// {{{
|
| 392 |
+
////////////////////////////////////////////////////////////////////////
|
| 393 |
+
//
|
| 394 |
+
//
|
| 395 |
+
|
| 396 |
+
// Count the number of requests that have been received
|
| 397 |
+
//
|
| 398 |
+
initial f_nreqs = 0;
|
| 399 |
+
always @(posedge i_clk)
|
| 400 |
+
if ((i_reset)||(!i_wb_cyc))
|
| 401 |
+
f_nreqs <= 0;
|
| 402 |
+
else if ((i_wb_stb)&&(!i_wb_stall))
|
| 403 |
+
f_nreqs <= f_nreqs + 1'b1;
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
//
|
| 407 |
+
// Count the number of acknowledgements that have been returned
|
| 408 |
+
//
|
| 409 |
+
initial f_nacks = 0;
|
| 410 |
+
always @(posedge i_clk)
|
| 411 |
+
if (i_reset)
|
| 412 |
+
f_nacks <= 0;
|
| 413 |
+
else if (!i_wb_cyc)
|
| 414 |
+
f_nacks <= 0;
|
| 415 |
+
else if ((i_wb_ack)||(i_wb_err))
|
| 416 |
+
f_nacks <= f_nacks + 1'b1;
|
| 417 |
+
|
| 418 |
+
//
|
| 419 |
+
// The number of outstanding requests is the difference between
|
| 420 |
+
// the number of requests and the number of acknowledgements
|
| 421 |
+
//
|
| 422 |
+
assign f_outstanding = (i_wb_cyc) ? (f_nreqs - f_nacks):0;
|
| 423 |
+
|
| 424 |
+
always @(*)
|
| 425 |
+
if ((i_wb_cyc)&&(F_MAX_REQUESTS > 0))
|
| 426 |
+
begin
|
| 427 |
+
if (i_wb_stb)
|
| 428 |
+
begin
|
| 429 |
+
`SLAVE_ASSUME(f_nreqs < F_MAX_REQUESTS);
|
| 430 |
+
end else
|
| 431 |
+
`SLAVE_ASSUME(f_nreqs <= F_MAX_REQUESTS);
|
| 432 |
+
`SLAVE_ASSERT(f_nacks <= f_nreqs);
|
| 433 |
+
assert(f_outstanding < MAX_OUTSTANDING);
|
| 434 |
+
end else
|
| 435 |
+
assume(f_outstanding < MAX_OUTSTANDING);
|
| 436 |
+
|
| 437 |
+
always @(*)
|
| 438 |
+
if ((i_wb_cyc)&&(f_outstanding == 0))
|
| 439 |
+
begin
|
| 440 |
+
// If nothing is outstanding, then there should be
|
| 441 |
+
// no acknowledgements ... however, an acknowledgement
|
| 442 |
+
// *can* come back on the same clock as the stb is
|
| 443 |
+
// going out.
|
| 444 |
+
if (F_OPT_MINCLOCK_DELAY)
|
| 445 |
+
begin
|
| 446 |
+
`SLAVE_ASSERT(!i_wb_ack);
|
| 447 |
+
`SLAVE_ASSERT(!i_wb_err);
|
| 448 |
+
end else begin
|
| 449 |
+
`SLAVE_ASSERT((!i_wb_ack)||((i_wb_stb)&&(!i_wb_stall)));
|
| 450 |
+
// The same is true of errors. They may not be
|
| 451 |
+
// created before the request gets through
|
| 452 |
+
`SLAVE_ASSERT((!i_wb_err)||((i_wb_stb)&&(!i_wb_stall)));
|
| 453 |
+
end
|
| 454 |
+
end else if (!i_wb_cyc && f_nacks == f_nreqs)
|
| 455 |
+
begin
|
| 456 |
+
`SLAVE_ASSERT(!i_wb_ack);
|
| 457 |
+
`SLAVE_ASSERT(!i_wb_err);
|
| 458 |
+
end
|
| 459 |
+
// }}}
|
| 460 |
+
////////////////////////////////////////////////////////////////////////
|
| 461 |
+
//
|
| 462 |
+
// Bus direction
|
| 463 |
+
// {{{
|
| 464 |
+
////////////////////////////////////////////////////////////////////////
|
| 465 |
+
//
|
| 466 |
+
//
|
| 467 |
+
generate if (!F_OPT_RMW_BUS_OPTION)
|
| 468 |
+
begin
|
| 469 |
+
// If we aren't waiting for anything, and we aren't issuing
|
| 470 |
+
// any requests, then then our transaction is over and we
|
| 471 |
+
// should be dropping the CYC line.
|
| 472 |
+
always @(*)
|
| 473 |
+
if (f_outstanding == 0)
|
| 474 |
+
`SLAVE_ASSUME((i_wb_stb)||(!i_wb_cyc));
|
| 475 |
+
// Not all masters will abide by this restriction. Some
|
| 476 |
+
// masters may wish to implement read-modify-write bus
|
| 477 |
+
// interactions. These masters need to keep CYC high between
|
| 478 |
+
// transactions, even though nothing is outstanding. For
|
| 479 |
+
// these busses, turn F_OPT_RMW_BUS_OPTION on.
|
| 480 |
+
end endgenerate
|
| 481 |
+
// }}}
|
| 482 |
+
////////////////////////////////////////////////////////////////////////
|
| 483 |
+
//
|
| 484 |
+
// Discontinuous request checking
|
| 485 |
+
// {{{
|
| 486 |
+
////////////////////////////////////////////////////////////////////////
|
| 487 |
+
//
|
| 488 |
+
//
|
| 489 |
+
|
| 490 |
+
generate if ((!F_OPT_DISCONTINUOUS)&&(!F_OPT_RMW_BUS_OPTION))
|
| 491 |
+
begin : INSIST_ON_NO_DISCONTINUOUS_STBS
|
| 492 |
+
// Within my own code, once a request begins it goes to
|
| 493 |
+
// completion and the CYC line is dropped. The master
|
| 494 |
+
// is not allowed to raise STB again after dropping it.
|
| 495 |
+
// Doing so would be a *discontinuous* request.
|
| 496 |
+
//
|
| 497 |
+
// However, in any RMW scheme, discontinuous requests are
|
| 498 |
+
// necessary, and the spec doesn't disallow them. Hence we
|
| 499 |
+
// make this check optional.
|
| 500 |
+
always @(posedge i_clk)
|
| 501 |
+
if ((f_past_valid)&&($past(i_wb_cyc))&&(!$past(i_wb_stb)))
|
| 502 |
+
`SLAVE_ASSUME(!i_wb_stb);
|
| 503 |
+
end endgenerate
|
| 504 |
+
// }}}
|
| 505 |
+
////////////////////////////////////////////////////////////////////////
|
| 506 |
+
//
|
| 507 |
+
// Master only checks
|
| 508 |
+
// {{{
|
| 509 |
+
////////////////////////////////////////////////////////////////////////
|
| 510 |
+
//
|
| 511 |
+
//
|
| 512 |
+
|
| 513 |
+
generate if (F_OPT_SHORT_CIRCUIT_PROOF)
|
| 514 |
+
begin
|
| 515 |
+
// In many ways, we don't care what happens on the bus return
|
| 516 |
+
// lines if the cycle line is low, so restricting them to a
|
| 517 |
+
// known value makes a lot of sense.
|
| 518 |
+
//
|
| 519 |
+
// On the other hand, if something above *does* depend upon
|
| 520 |
+
// these values (when it shouldn't), then we might want to know
|
| 521 |
+
// about it.
|
| 522 |
+
//
|
| 523 |
+
//
|
| 524 |
+
always @(posedge i_clk)
|
| 525 |
+
begin
|
| 526 |
+
if (!i_wb_cyc)
|
| 527 |
+
begin
|
| 528 |
+
assume(!i_wb_stall);
|
| 529 |
+
assume($stable(i_wb_idata));
|
| 530 |
+
end else if ((!$past(i_wb_ack))&&(!i_wb_ack))
|
| 531 |
+
assume($stable(i_wb_idata));
|
| 532 |
+
end
|
| 533 |
+
end endgenerate
|
| 534 |
+
|
| 535 |
+
generate if (F_OPT_SOURCE)
|
| 536 |
+
begin : SRC
|
| 537 |
+
// Any opening bus request starts with both CYC and STB high
|
| 538 |
+
// This is true for the master only, and more specifically
|
| 539 |
+
// only for those masters that are the initial source of any
|
| 540 |
+
// transaction. By the time an interaction gets to the slave,
|
| 541 |
+
// the CYC line may go high or low without actually affecting
|
| 542 |
+
// the STB line of the slave.
|
| 543 |
+
always @(posedge i_clk)
|
| 544 |
+
if ((f_past_valid)&&(!$past(i_wb_cyc))&&(i_wb_cyc))
|
| 545 |
+
`SLAVE_ASSUME(i_wb_stb);
|
| 546 |
+
end endgenerate
|
| 547 |
+
// }}}
|
| 548 |
+
|
| 549 |
+
// Keep Verilator happy
|
| 550 |
+
// {{{
|
| 551 |
+
// Verilator lint_off UNUSED
|
| 552 |
+
w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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* The rv32i_alu module serves as the Arithmetic Logic Unit (ALU) for the RISC-V core
|
| 2 |
+
during the execute stage of the pipeline. The ALU is responsible for executing arithmetic,
|
| 3 |
+
logic, and comparison operations based on the instruction and operands provided. This
|
| 4 |
+
module is a crucial part of the RISC-V core, as it processes the instructions and computes
|
| 5 |
+
the results required for program execution. Function includes:
|
| 6 |
+
- Operand Selection: The module first selects the appropriate operands for the ALU operation
|
| 7 |
+
depending on the opcode. Operand A can be the program counter (PC) or the value of the
|
| 8 |
+
first source register (rs1), while operand B can be either the second source register
|
| 9 |
+
(rs2) or an immediate value.
|
| 10 |
+
- ALU Operation: The ALU performs various operations, such as ADD, SUB, SLT, SLTU, XOR, OR,
|
| 11 |
+
AND, SLL, SRL, SRA, EQ, NEQ, GE, and GEU, depending on the instruction type. The result
|
| 12 |
+
of the ALU operation is stored in the y_d register.
|
| 13 |
+
- Handling Branches and Jumps: The module computes the next PC value based on the instruction
|
| 14 |
+
type (e.g., branch, jump, or jump-and-link). It also generates the o_change_pc signal to
|
| 15 |
+
indicate whether the PC needs to jump to a new address.
|
| 16 |
+
- Register Writeback: The module computes the value to be written back to the destination
|
| 17 |
+
register (rd) and sets the appropriate control signals (o_wr_rd and o_rd_valid) based on
|
| 18 |
+
the instruction type. For example, it disables writing to the destination register for
|
| 19 |
+
branch or store instructions.
|
| 20 |
+
- Stalling and Flushing: The ALU manages stalling and flushing of the pipeline. It generates
|
| 21 |
+
the o_stall_from_alu signal to stall the memory-access stage for load/store instructions
|
| 22 |
+
since accessing data memory may take multiple cycles. It also handles pipeline stalls
|
| 23 |
+
and flushes based on the input signals (i_stall, i_force_stall, and i_flush).
|
| 24 |
+
*/
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
`timescale 1ns / 1ps
|
| 28 |
+
`default_nettype none
|
| 29 |
+
`include "rv32i_header.vh"
|
| 30 |
+
|
| 31 |
+
module rv32i_alu(
|
| 32 |
+
input wire i_clk,i_rst_n,
|
| 33 |
+
input wire[`ALU_WIDTH-1:0] i_alu, //alu operation type from previous stage
|
| 34 |
+
input wire[4:0] i_rs1_addr, //address for register source 1
|
| 35 |
+
output reg[4:0] o_rs1_addr, //address for register source 1
|
| 36 |
+
input wire[31:0] i_rs1, //Source register 1 value
|
| 37 |
+
output reg[31:0] o_rs1, //Source register 1 value
|
| 38 |
+
input wire[31:0] i_rs2, //Source register 2 value
|
| 39 |
+
output reg[31:0] o_rs2, //Source register 2 value
|
| 40 |
+
input wire[31:0] i_imm, //Immediate value from previous stage
|
| 41 |
+
output reg[11:0] o_imm, //Immediate value
|
| 42 |
+
input wire[2:0] i_funct3, //function type from previous stage
|
| 43 |
+
output reg[2:0] o_funct3, // function type
|
| 44 |
+
input wire[`OPCODE_WIDTH-1:0] i_opcode, //opcode type from previous stage
|
| 45 |
+
output reg[`OPCODE_WIDTH-1:0] o_opcode, //opcode type
|
| 46 |
+
input wire[`EXCEPTION_WIDTH-1:0] i_exception, //exception from decoder stage
|
| 47 |
+
output reg[`EXCEPTION_WIDTH-1:0] o_exception, //exception: illegal inst,ecall,ebreak,mret
|
| 48 |
+
output reg[31:0] o_y, //result of arithmetic operation
|
| 49 |
+
// PC Control
|
| 50 |
+
input wire[31:0] i_pc, //Program Counter
|
| 51 |
+
output reg[31:0] o_pc, //pc register in pipeline
|
| 52 |
+
output reg[31:0] o_next_pc, //new pc value
|
| 53 |
+
output reg o_change_pc, //high if PC needs to jump
|
| 54 |
+
// Basereg Control
|
| 55 |
+
output reg o_wr_rd, //write rd to the base reg if enabled
|
| 56 |
+
input wire[4:0] i_rd_addr, //address for destination register (from previous stage)
|
| 57 |
+
output reg[4:0] o_rd_addr, //address for destination register
|
| 58 |
+
output reg[31:0] o_rd, //value to be written back to destination register
|
| 59 |
+
output reg o_rd_valid, //high if o_rd is valid (not load nor csr instruction)
|
| 60 |
+
/// Pipeline Control ///
|
| 61 |
+
output reg o_stall_from_alu, //prepare to stall next stage(memory-access stage) for load/store instruction
|
| 62 |
+
input wire i_ce, // input clk enable for pipeline stalling of this stage
|
| 63 |
+
output reg o_ce, // output clk enable for pipeline stalling of next stage
|
| 64 |
+
input wire i_stall, //informs this stage to stall
|
| 65 |
+
input wire i_force_stall, //force this stage to stall
|
| 66 |
+
output reg o_stall, //informs pipeline to stall
|
| 67 |
+
input wire i_flush, //flush this stage
|
| 68 |
+
output reg o_flush //flush previous stages
|
| 69 |
+
);
|
| 70 |
+
|
| 71 |
+
wire alu_add = i_alu[`ADD];
|
| 72 |
+
wire alu_sub = i_alu[`SUB];
|
| 73 |
+
wire alu_slt = i_alu[`SLT];
|
| 74 |
+
wire alu_sltu = i_alu[`SLTU];
|
| 75 |
+
wire alu_xor = i_alu[`XOR];
|
| 76 |
+
wire alu_or = i_alu[`OR];
|
| 77 |
+
wire alu_and = i_alu[`AND];
|
| 78 |
+
wire alu_sll = i_alu[`SLL];
|
| 79 |
+
wire alu_srl = i_alu[`SRL];
|
| 80 |
+
wire alu_sra = i_alu[`SRA];
|
| 81 |
+
wire alu_eq = i_alu[`EQ];
|
| 82 |
+
wire alu_neq = i_alu[`NEQ];
|
| 83 |
+
wire alu_ge = i_alu[`GE];
|
| 84 |
+
wire alu_geu = i_alu[`GEU];
|
| 85 |
+
wire opcode_rtype = i_opcode[`RTYPE];
|
| 86 |
+
wire opcode_itype = i_opcode[`ITYPE];
|
| 87 |
+
wire opcode_load = i_opcode[`LOAD];
|
| 88 |
+
wire opcode_store = i_opcode[`STORE];
|
| 89 |
+
wire opcode_branch = i_opcode[`BRANCH];
|
| 90 |
+
wire opcode_jal = i_opcode[`JAL];
|
| 91 |
+
wire opcode_jalr = i_opcode[`JALR];
|
| 92 |
+
wire opcode_lui = i_opcode[`LUI];
|
| 93 |
+
wire opcode_auipc = i_opcode[`AUIPC];
|
| 94 |
+
wire opcode_system = i_opcode[`SYSTEM];
|
| 95 |
+
wire opcode_fence = i_opcode[`FENCE];
|
| 96 |
+
|
| 97 |
+
reg[31:0] a; //operand A
|
| 98 |
+
reg[31:0] b; //operand B
|
| 99 |
+
reg[31:0] y_d; //ALU output
|
| 100 |
+
reg[31:0] rd_d; //next value to be written back to destination register
|
| 101 |
+
reg wr_rd_d; //write rd to basereg if enabled
|
| 102 |
+
reg rd_valid_d; //high if rd is valid (not load nor csr instruction)
|
| 103 |
+
reg[31:0] a_pc;
|
| 104 |
+
wire[31:0] sum;
|
| 105 |
+
wire stall_bit = o_stall || i_stall;
|
| 106 |
+
|
| 107 |
+
//register the output of i_alu
|
| 108 |
+
always @(posedge i_clk, negedge i_rst_n) begin
|
| 109 |
+
if(!i_rst_n) begin
|
| 110 |
+
o_exception <= 0;
|
| 111 |
+
o_ce <= 0;
|
| 112 |
+
o_stall_from_alu <= 0;
|
| 113 |
+
end
|
| 114 |
+
else begin
|
| 115 |
+
if(i_ce && !stall_bit) begin //update register only if this stage is enabled
|
| 116 |
+
o_opcode <= i_opcode;
|
| 117 |
+
o_exception <= i_exception;
|
| 118 |
+
o_y <= y_d;
|
| 119 |
+
o_rs1_addr <= i_rs1_addr;
|
| 120 |
+
o_rs1 <= i_rs1;
|
| 121 |
+
o_rs2 <= i_rs2;
|
| 122 |
+
o_rd_addr <= i_rd_addr;
|
| 123 |
+
o_imm <= i_imm[11:0];
|
| 124 |
+
o_funct3 <= i_funct3;
|
| 125 |
+
o_rd <= rd_d;
|
| 126 |
+
o_rd_valid <= rd_valid_d;
|
| 127 |
+
o_wr_rd <= wr_rd_d;
|
| 128 |
+
o_stall_from_alu <= i_opcode[`STORE] || i_opcode[`LOAD]; //stall next stage(memory-access stage) when need to store/load
|
| 129 |
+
o_pc <= i_pc; //since accessing data memory always takes more than 1 cycle
|
| 130 |
+
end
|
| 131 |
+
if(i_flush && !stall_bit) begin //flush this stage so clock-enable of next stage is disabled at next clock cycle
|
| 132 |
+
o_ce <= 0;
|
| 133 |
+
end
|
| 134 |
+
else if(!stall_bit) begin //clock-enable will change only when not stalled
|
| 135 |
+
o_ce <= i_ce;
|
| 136 |
+
end
|
| 137 |
+
else if(stall_bit && !i_stall) o_ce <= 0; //if this stage is stalled but next stage is not, disable
|
| 138 |
+
//clock enable of next stage at next clock cycle (pipeline bubble)
|
| 139 |
+
end
|
| 140 |
+
|
| 141 |
+
end
|
| 142 |
+
|
| 143 |
+
// determine operation used then compute for y output
|
| 144 |
+
always @* begin
|
| 145 |
+
y_d = 0;
|
| 146 |
+
|
| 147 |
+
a = (opcode_jal || opcode_auipc)? i_pc:i_rs1; // a can either be pc or rs1
|
| 148 |
+
b = (opcode_rtype || opcode_branch)? i_rs2:i_imm; // b can either be rs2 or imm
|
| 149 |
+
|
| 150 |
+
if(alu_add) y_d = a + b;
|
| 151 |
+
if(alu_sub) y_d = a - b;
|
| 152 |
+
if(alu_slt || alu_sltu) begin
|
| 153 |
+
y_d = {31'b0, (a < b)};
|
| 154 |
+
if(alu_slt) y_d = (a[31] ^ b[31])? {31'b0,a[31]}:y_d;
|
| 155 |
+
end
|
| 156 |
+
if(alu_xor) y_d = a ^ b;
|
| 157 |
+
if(alu_or) y_d = a | b;
|
| 158 |
+
if(alu_and) y_d = a & b;
|
| 159 |
+
if(alu_sll) y_d = a << b[4:0];
|
| 160 |
+
if(alu_srl) y_d = a >> b[4:0];
|
| 161 |
+
if(alu_sra) y_d = $signed(a) >>> b[4:0];
|
| 162 |
+
if(alu_eq || alu_neq) begin
|
| 163 |
+
y_d = {31'b0, (a == b)};
|
| 164 |
+
if(alu_neq) y_d = {31'b0,!y_d[0]};
|
| 165 |
+
end
|
| 166 |
+
if(alu_ge || alu_geu) begin
|
| 167 |
+
y_d = {31'b0, (a >= b)};
|
| 168 |
+
if(alu_ge) y_d = (a[31] ^ b[31])? {31'b0, b[31]}:y_d;
|
| 169 |
+
end
|
| 170 |
+
end
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
//determine o_rd to be saved to baseg and next value of PC
|
| 174 |
+
always @* begin
|
| 175 |
+
o_flush = i_flush; //flush this stage along with the previous stages
|
| 176 |
+
rd_d = 0;
|
| 177 |
+
rd_valid_d = 0;
|
| 178 |
+
o_change_pc = 0;
|
| 179 |
+
o_next_pc = 0;
|
| 180 |
+
wr_rd_d = 0;
|
| 181 |
+
a_pc = i_pc;
|
| 182 |
+
if(!i_flush) begin
|
| 183 |
+
if(opcode_rtype || opcode_itype) rd_d = y_d;
|
| 184 |
+
if(opcode_branch && y_d[0]) begin
|
| 185 |
+
o_next_pc = sum; //branch iff value of ALU is 1(true)
|
| 186 |
+
o_change_pc = i_ce; //change PC when ce of this stage is high (o_change_pc is valid)
|
| 187 |
+
o_flush = i_ce;
|
| 188 |
+
end
|
| 189 |
+
if(opcode_jal || opcode_jalr) begin
|
| 190 |
+
if(opcode_jalr) a_pc = i_rs1;
|
| 191 |
+
o_next_pc = sum; //jump to new PC
|
| 192 |
+
o_change_pc = i_ce; //change PC when ce of this stage is high (o_change_pc is valid)
|
| 193 |
+
o_flush = i_ce;
|
| 194 |
+
rd_d = i_pc + 4; //register the next pc value to destination register
|
| 195 |
+
end
|
| 196 |
+
end
|
| 197 |
+
if(opcode_lui) rd_d = i_imm;
|
| 198 |
+
if(opcode_auipc) rd_d = sum;
|
| 199 |
+
|
| 200 |
+
if(opcode_branch || opcode_store || (opcode_system && i_funct3 == 0) || opcode_fence ) wr_rd_d = 0; //i_funct3==0 are the non-csr system instructions
|
| 201 |
+
else wr_rd_d = 1; //always write to the destination reg except when instruction is BRANCH or STORE or SYSTEM(except CSR system instruction)
|
| 202 |
+
|
| 203 |
+
if(opcode_load || (opcode_system && i_funct3!=0)) rd_valid_d = 0; //value of o_rd for load and CSR write is not yet available at this stage
|
| 204 |
+
else rd_valid_d = 1;
|
| 205 |
+
|
| 206 |
+
//stall logic (stall when upper stages are stalled, when forced to stall, or when needs to flush previous stages but are still stalled)
|
| 207 |
+
o_stall = (i_stall || i_force_stall) && !i_flush; //stall when alu needs wait time
|
| 208 |
+
end
|
| 209 |
+
|
| 210 |
+
assign sum = a_pc + i_imm; //share adder for all addition operation for less resource utilization
|
| 211 |
+
|
| 212 |
+
`ifdef FORMAL
|
| 213 |
+
// assumption on inputs(not more than one opcode and alu operation is high)
|
| 214 |
+
wire[4:0] f_alu=i_alu[`ADD]+i_alu[`SUB]+i_alu[`SLT]+i_alu[`SLTU]+i_alu[`XOR]+i_alu[`OR]+i_alu[`AND]+i_alu[`SLL]+i_alu[`SRL]+i_alu[`SRA]+i_alu[`EQ]+i_alu[`NEQ]+i_alu[`GE]+i_alu[`GEU]+0;
|
| 215 |
+
wire[4:0] f_opcode=i_opcode[`RTYPE]+i_opcode[`ITYPE]+i_opcode[`LOAD]+i_opcode[`STORE]+i_opcode[`BRANCH]+i_opcode[`JAL]+i_opcode[`JALR]+i_opcode[`LUI]+i_opcode[`AUIPC]+i_opcode[`SYSTEM]+i_opcode[`FENCE];
|
| 216 |
+
|
| 217 |
+
always @* begin
|
| 218 |
+
assume(f_alu <= 1);
|
| 219 |
+
assume(f_opcode <= 1);
|
| 220 |
+
end
|
| 221 |
+
|
| 222 |
+
// verify all operations with $signed/$unsigned distinctions
|
| 223 |
+
always @* begin
|
| 224 |
+
if(i_alu[`SLTU]) assert(y_d[0] == $unsigned(a) < $unsigned(b));
|
| 225 |
+
if(i_alu[`SLT]) assert(y_d[0] == $signed(a) < $signed(b));
|
| 226 |
+
if(i_alu[`SLL]) assert($unsigned(y_d) == $unsigned(a) << $unsigned(b[4:0]));
|
| 227 |
+
if(i_alu[`SRL]) assert($unsigned(y_d) == $unsigned(a) >> $unsigned(b[4:0]));
|
| 228 |
+
if(i_alu[`SRA]) assert($signed(y_d) == ($signed(a) >>> $unsigned(b[4:0])));
|
| 229 |
+
if(i_alu[`GEU]) assert(y_d[0] == ($unsigned(a) >= $unsigned(b)));
|
| 230 |
+
if(i_alu[`GE]) assert(y_d[0] == ($signed(a) >= $signed(b)));
|
| 231 |
+
end
|
| 232 |
+
|
| 233 |
+
`endif
|
| 234 |
+
endmodule
|
AngeloJacobo_RISC-V/rtl/rv32i_basereg.v
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//regfile controller for the 32 integer base registers
|
| 2 |
+
|
| 3 |
+
`timescale 1ns / 1ps
|
| 4 |
+
`default_nettype none
|
| 5 |
+
|
| 6 |
+
module rv32i_basereg
|
| 7 |
+
(
|
| 8 |
+
input wire i_clk,
|
| 9 |
+
input wire i_ce_read, //clock enable for reading from basereg [STAGE 2]
|
| 10 |
+
input wire[4:0] i_rs1_addr, //source register 1 address
|
| 11 |
+
input wire[4:0] i_rs2_addr, //source register 2 address
|
| 12 |
+
input wire[4:0] i_rd_addr, //destination register address
|
| 13 |
+
input wire[31:0] i_rd, //data to be written to destination register
|
| 14 |
+
input wire i_wr, //write enable
|
| 15 |
+
output wire[31:0] o_rs1, //source register 1 value
|
| 16 |
+
output wire[31:0] o_rs2 //source register 2 value
|
| 17 |
+
);
|
| 18 |
+
|
| 19 |
+
reg[4:0] rs1_addr_q, rs2_addr_q;
|
| 20 |
+
reg[31:0] base_regfile[31:1]; //base register file (base_regfile[0] is hardwired to zero)
|
| 21 |
+
wire write_to_basereg;
|
| 22 |
+
|
| 23 |
+
always @(posedge i_clk) begin
|
| 24 |
+
if(write_to_basereg) begin //only write to register if stage 5 is previously enabled (output of stage 5[WRITEBACK] is registered so delayed by 1 clk)
|
| 25 |
+
base_regfile[i_rd_addr] <= i_rd; //synchronous write
|
| 26 |
+
end
|
| 27 |
+
if(i_ce_read) begin //only read the register if stage 2 is enabled [DECODE]
|
| 28 |
+
rs1_addr_q <= i_rs1_addr; //synchronous read
|
| 29 |
+
rs2_addr_q <= i_rs2_addr; //synchronous read
|
| 30 |
+
end
|
| 31 |
+
end
|
| 32 |
+
|
| 33 |
+
assign write_to_basereg = i_wr && i_rd_addr!=0; //no need to write to basereg 0 (hardwired to zero)
|
| 34 |
+
assign o_rs1 = rs1_addr_q==0? 0: base_regfile[rs1_addr_q]; // if regfile is about to be written at the same time we read it
|
| 35 |
+
assign o_rs2 = rs2_addr_q==0? 0: base_regfile[rs2_addr_q]; //then return the next value to be written to that address
|
| 36 |
+
|
| 37 |
+
endmodule
|
| 38 |
+
|
AngeloJacobo_RISC-V/rtl/rv32i_core.v
ADDED
|
@@ -0,0 +1,626 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* The rv32i_core module represents the top module for an RV32I RISC-V processor
|
| 2 |
+
core. The module is structured around a 5-stage pipeline architecture, which
|
| 3 |
+
includes Fetch, Decode, Execute, Memory Access, and Writeback stages. The RV32I
|
| 4 |
+
core is designed to interface with separate instruction and data memories, and
|
| 5 |
+
supports external, software, and timer interrupts. The module contains several
|
| 6 |
+
sub-modules that carry out various functions within the processor:
|
| 7 |
+
- rv32i_forwarding: This sub-module is responsible for handling operand
|
| 8 |
+
forwarding. It ensures that the correct operand values are used in the ALU
|
| 9 |
+
stage, even when they have not yet been written back to the register file.
|
| 10 |
+
Operand forwarding helps to reduce pipeline stalls caused by data
|
| 11 |
+
dependencies.
|
| 12 |
+
- rv32i_basereg: This sub-module serves as a controller for the 32 integer
|
| 13 |
+
base registers. It manages reading from and writing to the register file,
|
| 14 |
+
with read operations occurring during the Decode stage and write operations
|
| 15 |
+
during the Writeback stage.
|
| 16 |
+
- rv32i_fetch: This sub-module is responsible for fetching instructions from
|
| 17 |
+
the instruction memory. It generates the instruction address, retrieves
|
| 18 |
+
the instruction, and controls the program counter (PC). It also manages
|
| 19 |
+
the pipeline stall and flush signals for the Fetch stage.
|
| 20 |
+
- rv32i_decoder: This sub-module takes care of decoding the fetched 32-bit
|
| 21 |
+
instruction. It extracts various fields from the instruction, such as opcode,
|
| 22 |
+
function type, immediate value, and register addresses. The sub-module also
|
| 23 |
+
detects exceptions, manages pipeline stall and flush signals for the Decode
|
| 24 |
+
stage, and provides a clock enable signal for the next stage.
|
| 25 |
+
- rv32i_alu: This sub-module is the Arithmetic Logic Unit (ALU) of the core.
|
| 26 |
+
It performs arithmetic and logical operations based on the opcode and
|
| 27 |
+
function type provided by the decoder. It also controls the program counter
|
| 28 |
+
for branches and jumps, manages register write enable signals, and handles
|
| 29 |
+
pipeline stall and flush signals for the Execute stage.
|
| 30 |
+
- rv32i_memoryaccess: This sub-module controls data memory access for load and
|
| 31 |
+
store operations. It computes the memory address based on the ALU output
|
| 32 |
+
and provides the appropriate signals for reading from or writing to the data
|
| 33 |
+
memory. The sub-module also manages pipeline stall and flush signals for
|
| 34 |
+
the Memory Access stage.
|
| 35 |
+
- rv32i_writeback: This sub-module is responsible for writing the results of ALU
|
| 36 |
+
and load operations back to the register file. It also manages the program
|
| 37 |
+
counter for returning from traps and provides clock enable signals for the
|
| 38 |
+
Writeback stage.
|
| 39 |
+
- rv32i_csr: This sub-module manages the Control and Status Registers (CSRs) in
|
| 40 |
+
the core. It handles traps and exceptions, updates CSR values, and controls
|
| 41 |
+
the program counter for trap handling.
|
| 42 |
+
*/
|
| 43 |
+
|
| 44 |
+
`timescale 1ns / 1ps
|
| 45 |
+
`default_nettype none
|
| 46 |
+
`include "rv32i_header.vh"
|
| 47 |
+
|
| 48 |
+
module rv32i_core #(parameter PC_RESET = 32'h00_00_00_00, TRAP_ADDRESS = 0, ZICSR_EXTENSION = 1) (
|
| 49 |
+
input wire i_clk, i_rst_n,
|
| 50 |
+
//Instruction Memory Interface (32 bit rom)
|
| 51 |
+
input wire[31:0] i_inst, //32-bit instruction
|
| 52 |
+
output wire[31:0] o_iaddr, //address of instruction
|
| 53 |
+
output wire o_stb_inst, //request for read access to instruction memory
|
| 54 |
+
input wire i_ack_inst, //ack (high if new instruction is ready)
|
| 55 |
+
//Data Memory Interface (32 bit ram)
|
| 56 |
+
output wire o_wb_cyc_data, //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 57 |
+
output wire o_wb_stb_data, //request for read/write access to data memory
|
| 58 |
+
output wire o_wb_we_data, //write-enable (1 = write, 0 = read)
|
| 59 |
+
output wire[31:0] o_wb_addr_data, //address of data memory for store/load
|
| 60 |
+
output wire[31:0] o_wb_data_data, //data to be stored to memory
|
| 61 |
+
output wire[3:0] o_wb_sel_data, //byte strobe for write (1 = write the byte) {byte3,byte2,byte1,byte0}
|
| 62 |
+
input wire i_wb_ack_data, //ack by data memory (high when read data is ready or when write data is already written)
|
| 63 |
+
input wire i_wb_stall_data, //stall by data memory
|
| 64 |
+
input wire[31:0] i_wb_data_data, //data retrieve from memory
|
| 65 |
+
//Interrupts
|
| 66 |
+
input wire i_external_interrupt, //interrupt from external source
|
| 67 |
+
input wire i_software_interrupt, //interrupt from software (inter-processor interrupt)
|
| 68 |
+
input wire i_timer_interrupt //interrupt from timer
|
| 69 |
+
);
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
//wires for basereg
|
| 73 |
+
wire[31:0] rs1_orig,rs2_orig;
|
| 74 |
+
wire[31:0] rs1,rs2;
|
| 75 |
+
wire ce_read;
|
| 76 |
+
|
| 77 |
+
//wires for rv32i_fetch
|
| 78 |
+
wire[31:0] fetch_pc;
|
| 79 |
+
wire[31:0] fetch_inst;
|
| 80 |
+
|
| 81 |
+
//wires for rv32i_decoder
|
| 82 |
+
wire[`ALU_WIDTH-1:0] decoder_alu;
|
| 83 |
+
wire[`OPCODE_WIDTH-1:0] decoder_opcode;
|
| 84 |
+
wire[31:0] decoder_pc;
|
| 85 |
+
wire[4:0] decoder_rs1_addr, decoder_rs2_addr;
|
| 86 |
+
wire[4:0] decoder_rs1_addr_q, decoder_rs2_addr_q;
|
| 87 |
+
wire[4:0] decoder_rd_addr;
|
| 88 |
+
wire[31:0] decoder_imm;
|
| 89 |
+
wire[2:0] decoder_funct3;
|
| 90 |
+
wire[`EXCEPTION_WIDTH-1:0] decoder_exception;
|
| 91 |
+
wire decoder_ce;
|
| 92 |
+
wire decoder_flush;
|
| 93 |
+
|
| 94 |
+
//wires for rv32i_alu
|
| 95 |
+
wire[`OPCODE_WIDTH-1:0] alu_opcode;
|
| 96 |
+
wire[4:0] alu_rs1_addr;
|
| 97 |
+
wire[31:0] alu_rs1;
|
| 98 |
+
wire[31:0] alu_rs2;
|
| 99 |
+
wire[11:0] alu_imm;
|
| 100 |
+
wire[2:0] alu_funct3;
|
| 101 |
+
wire[31:0] alu_y;
|
| 102 |
+
wire[31:0] alu_pc;
|
| 103 |
+
wire[31:0] alu_next_pc;
|
| 104 |
+
wire alu_change_pc;
|
| 105 |
+
wire alu_wr_rd;
|
| 106 |
+
wire[4:0] alu_rd_addr;
|
| 107 |
+
wire[31:0] alu_rd;
|
| 108 |
+
wire alu_rd_valid;
|
| 109 |
+
wire[`EXCEPTION_WIDTH-1:0] alu_exception;
|
| 110 |
+
wire alu_ce;
|
| 111 |
+
wire alu_flush;
|
| 112 |
+
wire alu_force_stall;
|
| 113 |
+
|
| 114 |
+
//wires for rv32i_memoryaccess
|
| 115 |
+
wire[`OPCODE_WIDTH-1:0] memoryaccess_opcode;
|
| 116 |
+
wire[2:0] memoryaccess_funct3;
|
| 117 |
+
wire[31:0] memoryaccess_pc;
|
| 118 |
+
wire memoryaccess_wr_rd;
|
| 119 |
+
wire[4:0] memoryaccess_rd_addr;
|
| 120 |
+
wire[31:0] memoryaccess_rd;
|
| 121 |
+
wire[31:0] memoryaccess_data_load;
|
| 122 |
+
wire memoryaccess_wr_mem;
|
| 123 |
+
wire memoryaccess_ce;
|
| 124 |
+
wire memoryaccess_flush;
|
| 125 |
+
wire o_stall_from_alu;
|
| 126 |
+
//wires for rv32i_writeback
|
| 127 |
+
wire writeback_wr_rd;
|
| 128 |
+
wire[4:0] writeback_rd_addr;
|
| 129 |
+
wire[31:0] writeback_rd;
|
| 130 |
+
wire[31:0] writeback_next_pc;
|
| 131 |
+
wire writeback_change_pc;
|
| 132 |
+
wire writeback_ce;
|
| 133 |
+
wire writeback_flush;
|
| 134 |
+
|
| 135 |
+
//wires for rv32i_csr
|
| 136 |
+
wire[31:0] csr_out; //CSR value to be stored to basereg
|
| 137 |
+
wire[31:0] csr_return_address; //mepc CSR
|
| 138 |
+
wire[31:0] csr_trap_address; //mtvec CSR
|
| 139 |
+
wire csr_go_to_trap; //high before going to trap (if exception/interrupt detected)
|
| 140 |
+
wire csr_return_from_trap; //high before returning from trap (via mret)
|
| 141 |
+
|
| 142 |
+
wire stall_decoder,
|
| 143 |
+
stall_alu,
|
| 144 |
+
stall_memoryaccess,
|
| 145 |
+
stall_writeback; //control stall of each pipeline stages
|
| 146 |
+
assign ce_read = decoder_ce && !stall_decoder; //reads basereg only decoder is not stalled
|
| 147 |
+
|
| 148 |
+
//module instantiations
|
| 149 |
+
rv32i_forwarding operand_forwarding ( //logic for operand forwarding
|
| 150 |
+
.i_rs1_orig(rs1_orig), //current rs1 value saved in basereg
|
| 151 |
+
.i_rs2_orig(rs2_orig), //current rs2 value saved in basereg
|
| 152 |
+
.i_decoder_rs1_addr_q(decoder_rs1_addr_q), //address of operand rs1 used in ALU stage
|
| 153 |
+
.i_decoder_rs2_addr_q(decoder_rs2_addr_q), //address of operand rs2 used in ALU stage
|
| 154 |
+
.o_alu_force_stall(alu_force_stall), //high to force ALU stage to stall
|
| 155 |
+
.o_rs1(rs1), //rs1 value with Operand Forwarding
|
| 156 |
+
.o_rs2(rs2), //rs2 value with Operand Forwarding
|
| 157 |
+
// Stage 4 [MEMORYACCESS]
|
| 158 |
+
.i_alu_rd_addr(alu_rd_addr), //destination register address
|
| 159 |
+
.i_alu_wr_rd(alu_wr_rd), //high if rd_addr will be written
|
| 160 |
+
.i_alu_rd_valid(alu_rd_valid), //high if rd is already valid at this stage (not LOAD nor CSR instruction)
|
| 161 |
+
.i_alu_rd(alu_rd), //rd value in stage 4
|
| 162 |
+
.i_memoryaccess_ce(memoryaccess_ce), //high if stage 4 is enabled
|
| 163 |
+
// Stage 5 [WRITEBACK]
|
| 164 |
+
.i_memoryaccess_rd_addr(memoryaccess_rd_addr), //destination register address
|
| 165 |
+
.i_memoryaccess_wr_rd(memoryaccess_wr_rd), //high if rd_addr will be written
|
| 166 |
+
.i_writeback_rd(writeback_rd), //rd value in stage 5
|
| 167 |
+
.i_writeback_ce(writeback_ce) //high if stage 4 is enabled
|
| 168 |
+
);
|
| 169 |
+
|
| 170 |
+
rv32i_basereg m0( //regfile controller for the 32 integer base registers
|
| 171 |
+
.i_clk(i_clk),
|
| 172 |
+
.i_ce_read(ce_read), //clock enable for reading from basereg [STAGE 2]
|
| 173 |
+
.i_rs1_addr(decoder_rs1_addr), //source register 1 address
|
| 174 |
+
.i_rs2_addr(decoder_rs2_addr), //source register 2 address
|
| 175 |
+
.i_rd_addr(writeback_rd_addr), //destination register address
|
| 176 |
+
.i_rd(writeback_rd), //data to be written to destination register
|
| 177 |
+
.i_wr(writeback_wr_rd), //write enable
|
| 178 |
+
.o_rs1(rs1_orig), //source register 1 value
|
| 179 |
+
.o_rs2(rs2_orig) //source register 2 value
|
| 180 |
+
);
|
| 181 |
+
|
| 182 |
+
rv32i_fetch #(.PC_RESET(PC_RESET)) m1( // logic for fetching instruction [FETCH STAGE , STAGE 1]
|
| 183 |
+
.i_clk(i_clk),
|
| 184 |
+
.i_rst_n(i_rst_n),
|
| 185 |
+
.o_iaddr(o_iaddr), //Instruction address
|
| 186 |
+
.o_pc(fetch_pc), //PC value of o_inst
|
| 187 |
+
.i_inst(i_inst), // retrieved instruction from Memory
|
| 188 |
+
.o_inst(fetch_inst), // instruction
|
| 189 |
+
.o_stb_inst(o_stb_inst), // request for instruction
|
| 190 |
+
.i_ack_inst(i_ack_inst), //ack (high if new instruction is ready)
|
| 191 |
+
// PC Control
|
| 192 |
+
.i_writeback_change_pc(writeback_change_pc), //high when PC needs to change when going to trap or returning from trap
|
| 193 |
+
.i_writeback_next_pc(writeback_next_pc), //next PC due to trap
|
| 194 |
+
.i_alu_change_pc(alu_change_pc), //high when PC needs to change for taken branches and jumps
|
| 195 |
+
.i_alu_next_pc(alu_next_pc), //next PC due to branch or jump
|
| 196 |
+
/// Pipeline Control ///
|
| 197 |
+
.o_ce(decoder_ce), // output clk enable for pipeline stalling of next stage
|
| 198 |
+
.i_stall((stall_decoder || stall_alu || stall_memoryaccess || stall_writeback)), //informs this stage to stall
|
| 199 |
+
.i_flush(decoder_flush) //flush this stage
|
| 200 |
+
);
|
| 201 |
+
|
| 202 |
+
rv32i_decoder m2( //logic for the decoding of the 32 bit instruction [DECODE STAGE , STAGE 2]
|
| 203 |
+
.i_clk(i_clk),
|
| 204 |
+
.i_rst_n(i_rst_n),
|
| 205 |
+
.i_inst(fetch_inst), //32 bit instruction
|
| 206 |
+
.i_pc(fetch_pc), //PC value from fetch stage
|
| 207 |
+
.o_pc(decoder_pc), //PC value
|
| 208 |
+
.o_rs1_addr(decoder_rs1_addr),// address for register source 1
|
| 209 |
+
.o_rs1_addr_q(decoder_rs1_addr_q), // registered address for register source 1
|
| 210 |
+
.o_rs2_addr(decoder_rs2_addr), // address for register source 2
|
| 211 |
+
.o_rs2_addr_q(decoder_rs2_addr_q), // registered address for register source 2
|
| 212 |
+
.o_rd_addr(decoder_rd_addr), // address for destination register
|
| 213 |
+
.o_imm(decoder_imm), // extended value for immediate
|
| 214 |
+
.o_funct3(decoder_funct3), // function type
|
| 215 |
+
.o_alu(decoder_alu), //alu operation type
|
| 216 |
+
.o_opcode(decoder_opcode), //opcode type
|
| 217 |
+
.o_exception(decoder_exception), //exceptions: illegal inst, ecall, ebreak, mret
|
| 218 |
+
/// Pipeline Control ///
|
| 219 |
+
.i_ce(decoder_ce), // input clk enable for pipeline stalling of this stage
|
| 220 |
+
.o_ce(alu_ce), // output clk enable for pipeline stalling of next stage
|
| 221 |
+
.i_stall((stall_alu || stall_memoryaccess || stall_writeback)), //informs this stage to stall
|
| 222 |
+
.o_stall(stall_decoder), //informs pipeline to stall
|
| 223 |
+
.i_flush(alu_flush), //flush this stage
|
| 224 |
+
.o_flush(decoder_flush) //flushes previous stages
|
| 225 |
+
);
|
| 226 |
+
|
| 227 |
+
rv32i_alu m3( //ALU combinational logic [EXECUTE STAGE , STAGE 3]
|
| 228 |
+
.i_clk(i_clk),
|
| 229 |
+
.i_rst_n(i_rst_n),
|
| 230 |
+
.i_alu(decoder_alu), //alu operation type
|
| 231 |
+
.i_rs1_addr(decoder_rs1_addr_q), //address for register source 1
|
| 232 |
+
.o_rs1_addr(alu_rs1_addr), //address for register source 1
|
| 233 |
+
.i_rs1(rs1), //Source register 1 value
|
| 234 |
+
.o_rs1(alu_rs1), //Source register 1 value
|
| 235 |
+
.i_rs2(rs2), //Source Register 2 value
|
| 236 |
+
.o_rs2(alu_rs2), //Source Register 2 value
|
| 237 |
+
.i_imm(decoder_imm), //Immediate value from previous stage
|
| 238 |
+
.o_imm(alu_imm), //Immediate value
|
| 239 |
+
.i_funct3(decoder_funct3), //function type from decoder stage
|
| 240 |
+
.o_funct3(alu_funct3), //function type
|
| 241 |
+
.i_opcode(decoder_opcode), //opcode type from previous stage
|
| 242 |
+
.o_opcode(alu_opcode), //opcode type
|
| 243 |
+
.i_exception(decoder_exception), //exception from decoder stage
|
| 244 |
+
.o_exception(alu_exception), //exception: illegal inst,ecall,ebreak,mret
|
| 245 |
+
.o_y(alu_y), //result of arithmetic operation
|
| 246 |
+
// PC Control
|
| 247 |
+
.i_pc(decoder_pc), //pc from decoder stage
|
| 248 |
+
.o_pc(alu_pc), // current pc
|
| 249 |
+
.o_next_pc(alu_next_pc), //next pc
|
| 250 |
+
.o_change_pc(alu_change_pc), //change pc if high
|
| 251 |
+
// Basereg Control
|
| 252 |
+
.o_wr_rd(alu_wr_rd), //write rd to basereg if enabled
|
| 253 |
+
.i_rd_addr(decoder_rd_addr), //address for destination register (from previous stage)
|
| 254 |
+
.o_rd_addr(alu_rd_addr), //address for destination register
|
| 255 |
+
.o_rd(alu_rd), //value to be written back to destination register
|
| 256 |
+
.o_rd_valid(alu_rd_valid), //high if o_rd is valid (not load nor csr instruction)
|
| 257 |
+
/// Pipeline Control ///
|
| 258 |
+
.o_stall_from_alu(o_stall_from_alu), //prepare to stall next stage(memory-access stage) for load/store instruction
|
| 259 |
+
.i_ce(alu_ce), // input clk enable for pipeline stalling of this stage
|
| 260 |
+
.o_ce(memoryaccess_ce), // output clk enable for pipeline stalling of next stage
|
| 261 |
+
.i_stall((stall_memoryaccess || stall_writeback)), //informs this stage to stall
|
| 262 |
+
.i_force_stall(alu_force_stall), //force this stage to stall
|
| 263 |
+
.o_stall(stall_alu), //informs pipeline to stall
|
| 264 |
+
.i_flush(memoryaccess_flush), //flush this stage
|
| 265 |
+
.o_flush(alu_flush) //flushes previous stages
|
| 266 |
+
);
|
| 267 |
+
|
| 268 |
+
rv32i_memoryaccess m4( //logic controller for data memory access (load/store) [MEMORY STAGE , STAGE 4]
|
| 269 |
+
.i_clk(i_clk),
|
| 270 |
+
.i_rst_n(i_rst_n),
|
| 271 |
+
.i_rs2(alu_rs2), //data to be stored to memory is always rs2
|
| 272 |
+
.i_y(alu_y), //y value from ALU (address of data to memory be stored or loaded)
|
| 273 |
+
.i_funct3(alu_funct3), //funct3 from previous stage
|
| 274 |
+
.o_funct3(memoryaccess_funct3), //funct3 (byte,halfword,word)
|
| 275 |
+
.i_opcode(alu_opcode), //opcode type from previous stage
|
| 276 |
+
.o_opcode(memoryaccess_opcode), //opcode type
|
| 277 |
+
.i_pc(alu_pc), //PC from previous stage
|
| 278 |
+
.o_pc(memoryaccess_pc), //PC value
|
| 279 |
+
// Basereg Control
|
| 280 |
+
.i_wr_rd(alu_wr_rd), //write rd to base reg is enabled (from memoryaccess stage)
|
| 281 |
+
.o_wr_rd(memoryaccess_wr_rd), //write rd to the base reg if enabled
|
| 282 |
+
.i_rd_addr(alu_rd_addr), //address for destination register (from previous stage)
|
| 283 |
+
.o_rd_addr(memoryaccess_rd_addr), //address for destination register
|
| 284 |
+
.i_rd(alu_rd), //value to be written back to destination reg
|
| 285 |
+
.o_rd(memoryaccess_rd), //value to be written back to destination register
|
| 286 |
+
// Data Memory Control
|
| 287 |
+
.o_wb_cyc_data(o_wb_cyc_data), //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 288 |
+
.o_wb_stb_data(o_wb_stb_data), //request for read/write access to data memory
|
| 289 |
+
.o_wb_we_data(o_wb_we_data), //write-enable (1 = write, 0 = read)
|
| 290 |
+
.o_wb_addr_data(o_wb_addr_data), //data memory address
|
| 291 |
+
.o_wb_data_data(o_wb_data_data), //data to be stored to memory (mask-aligned)
|
| 292 |
+
.o_wb_sel_data(o_wb_sel_data), //byte strobe for write (1 = write the byte) {byte3,byte2,byte1,byte0}
|
| 293 |
+
.i_wb_ack_data(i_wb_ack_data), //ack by data memory (high when read data is ready or when write data is already written
|
| 294 |
+
.i_wb_stall_data(i_wb_stall_data), //stall by data memory (1 = data memory is busy)
|
| 295 |
+
.i_wb_data_data(i_wb_data_data), //data retrieve from data memory
|
| 296 |
+
.o_data_load(memoryaccess_data_load), //data to be loaded to base reg (z-or-s extended)
|
| 297 |
+
/// Pipeline Control ///
|
| 298 |
+
.i_stall_from_alu(o_stall_from_alu), //stalls this stage when incoming instruction is a load/store
|
| 299 |
+
.i_ce(memoryaccess_ce), // input clk enable for pipeline stalling of this stage
|
| 300 |
+
.o_ce(writeback_ce), // output clk enable for pipeline stalling of next stage
|
| 301 |
+
.i_stall(stall_writeback), //informs this stage to stall
|
| 302 |
+
.o_stall(stall_memoryaccess), //informs pipeline to stall
|
| 303 |
+
.i_flush(writeback_flush), //flush this stage
|
| 304 |
+
.o_flush(memoryaccess_flush) //flushes previous stages
|
| 305 |
+
);
|
| 306 |
+
|
| 307 |
+
rv32i_writeback m5( //logic controller for the next PC and rd value [WRITEBACK STAGE , STAGE 5]
|
| 308 |
+
.i_funct3(memoryaccess_funct3), //function type
|
| 309 |
+
.i_data_load(memoryaccess_data_load), //data to be loaded to base reg (from previous stage)
|
| 310 |
+
.i_csr_out(csr_out), //CSR value to be loaded to basereg
|
| 311 |
+
.i_opcode_load(memoryaccess_opcode[`LOAD]),
|
| 312 |
+
.i_opcode_system(memoryaccess_opcode[`SYSTEM]),
|
| 313 |
+
// Basereg Control
|
| 314 |
+
.i_wr_rd(memoryaccess_wr_rd), //write rd to base reg is enabled (from memoryaccess stage)
|
| 315 |
+
.o_wr_rd(writeback_wr_rd), //write rd to the base reg if enabled
|
| 316 |
+
.i_rd_addr(memoryaccess_rd_addr), //address for destination register (from previous stage)
|
| 317 |
+
.o_rd_addr(writeback_rd_addr), //address for destination register
|
| 318 |
+
.i_rd(memoryaccess_rd), //value to be written back to destination reg
|
| 319 |
+
.o_rd(writeback_rd), //value to be written back to destination register
|
| 320 |
+
// PC Control
|
| 321 |
+
.i_pc(memoryaccess_pc), //pc value
|
| 322 |
+
.o_next_pc(writeback_next_pc), //new PC value
|
| 323 |
+
.o_change_pc(writeback_change_pc), //high if PC needs to jump
|
| 324 |
+
// Trap-Handler
|
| 325 |
+
.i_go_to_trap(csr_go_to_trap), //high before going to trap (if exception/interrupt detected)
|
| 326 |
+
.i_return_from_trap(csr_return_from_trap), //high before returning from trap (via mret)
|
| 327 |
+
.i_return_address(csr_return_address), //mepc CSR
|
| 328 |
+
.i_trap_address(csr_trap_address), //mtvec CSR
|
| 329 |
+
/// Pipeline Control ///
|
| 330 |
+
.i_ce(writeback_ce), // input clk enable for pipeline stalling of this stage
|
| 331 |
+
.o_stall(stall_writeback), //informs pipeline to stall
|
| 332 |
+
.o_flush(writeback_flush) //flushes previous stages
|
| 333 |
+
);
|
| 334 |
+
|
| 335 |
+
// removable extensions
|
| 336 |
+
if(ZICSR_EXTENSION == 1) begin: zicsr
|
| 337 |
+
rv32i_csr #(.TRAP_ADDRESS(TRAP_ADDRESS)) m6( // control logic for Control and Status Registers (CSR) [STAGE 4]
|
| 338 |
+
.i_clk(i_clk),
|
| 339 |
+
.i_rst_n(i_rst_n),
|
| 340 |
+
// Interrupts
|
| 341 |
+
.i_external_interrupt(i_external_interrupt), //interrupt from external source
|
| 342 |
+
.i_software_interrupt(i_software_interrupt), //interrupt from software (inter-processor interrupt)
|
| 343 |
+
.i_timer_interrupt(i_timer_interrupt), //interrupt from timer
|
| 344 |
+
/// Exceptions ///
|
| 345 |
+
.i_is_inst_illegal(alu_exception[`ILLEGAL]), //illegal instruction
|
| 346 |
+
.i_is_ecall(alu_exception[`ECALL]), //ecall instruction
|
| 347 |
+
.i_is_ebreak(alu_exception[`EBREAK]), //ebreak instruction
|
| 348 |
+
.i_is_mret(alu_exception[`MRET]), //mret (return from trap) instruction
|
| 349 |
+
/// Load/Store Misaligned Exception///
|
| 350 |
+
.i_opcode(alu_opcode), //opcode type from alu stage
|
| 351 |
+
.i_y(alu_y), //y value from ALU (address used in load/store/jump/branch)
|
| 352 |
+
/// CSR instruction ///
|
| 353 |
+
.i_funct3(alu_funct3), // CSR instruction operation
|
| 354 |
+
.i_csr_index(alu_imm), //immediate value decoded by decoder
|
| 355 |
+
.i_imm({27'b0,alu_rs1_addr}), //unsigned immediate for immediate type of CSR instruction (new value to be stored to CSR)
|
| 356 |
+
.i_rs1(alu_rs1), //Source register 1 value (new value to be stored to CSR)
|
| 357 |
+
.o_csr_out(csr_out), //CSR value to be loaded to basereg
|
| 358 |
+
// Trap-Handler
|
| 359 |
+
.i_pc(alu_pc), //Program Counter (three stages had already been filled [fetch -> decode -> execute ])
|
| 360 |
+
.writeback_change_pc(writeback_change_pc), //high if writeback will issue change_pc (which will override this stage)
|
| 361 |
+
.o_return_address(csr_return_address), //mepc CSR
|
| 362 |
+
.o_trap_address(csr_trap_address), //mtvec CSR
|
| 363 |
+
.o_go_to_trap_q(csr_go_to_trap), //high before going to trap (if exception/interrupt detected)
|
| 364 |
+
.o_return_from_trap_q(csr_return_from_trap), //high before returning from trap (via mret)
|
| 365 |
+
.i_minstret_inc(writeback_ce), //high for one clock cycle at the end of every instruction
|
| 366 |
+
/// Pipeline Control ///
|
| 367 |
+
.i_ce(memoryaccess_ce), // input clk enable for pipeline stalling of this stage
|
| 368 |
+
.i_stall((stall_writeback || stall_memoryaccess)) //informs this stage to stall
|
| 369 |
+
);
|
| 370 |
+
end
|
| 371 |
+
else begin: zicsr
|
| 372 |
+
assign csr_out = 0;
|
| 373 |
+
assign csr_return_address = 0;
|
| 374 |
+
assign csr_trap_address = 0;
|
| 375 |
+
assign csr_go_to_trap = 0;
|
| 376 |
+
assign csr_return_from_trap = 0;
|
| 377 |
+
end
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
`ifdef FORMAL
|
| 383 |
+
//f_past_valid logic
|
| 384 |
+
reg f_past_valid = 0;
|
| 385 |
+
always @(posedge i_clk) f_past_valid <= 1;
|
| 386 |
+
|
| 387 |
+
// assume initial conditions
|
| 388 |
+
initial begin
|
| 389 |
+
assume(i_rst_n == 0);
|
| 390 |
+
end
|
| 391 |
+
|
| 392 |
+
// assumption on inputs(not more than one opcode and alu operation is high)
|
| 393 |
+
wire[4:0] f_alu=decoder_alu[`ADD]+decoder_alu[`SUB]+decoder_alu[`SLT]+decoder_alu[`SLTU]+decoder_alu[`XOR]+decoder_alu[`OR]+decoder_alu[`AND]+decoder_alu[`SLL]+decoder_alu[`SRL]+decoder_alu[`SRA]+decoder_alu[`EQ]+decoder_alu[`NEQ]+decoder_alu[`GE]+decoder_alu[`GEU]+0;
|
| 394 |
+
wire[4:0] f_opcode=decoder_opcode[`RTYPE]+decoder_opcode[`ITYPE]+decoder_opcode[`LOAD]+decoder_opcode[`STORE]+decoder_opcode[`BRANCH]+decoder_opcode[`JAL]+decoder_opcode[`JALR]+decoder_opcode[`LUI]+decoder_opcode[`AUIPC]+decoder_opcode[`SYSTEM]+decoder_opcode[`FENCE]+0;
|
| 395 |
+
always @* begin
|
| 396 |
+
assume(f_alu <= 1);
|
| 397 |
+
assume(f_opcode <= 1);
|
| 398 |
+
end
|
| 399 |
+
|
| 400 |
+
wire[4:0] f_outstanding;
|
| 401 |
+
|
| 402 |
+
fwb_master #(
|
| 403 |
+
// {{{
|
| 404 |
+
.AW(32),
|
| 405 |
+
.DW(32),
|
| 406 |
+
.F_MAX_STALL(1),
|
| 407 |
+
.F_MAX_ACK_DELAY(1),
|
| 408 |
+
.F_LGDEPTH(4),
|
| 409 |
+
.F_MAX_REQUESTS(0),
|
| 410 |
+
// OPT_BUS_ABORT: If true, the master can drop CYC at any time
|
| 411 |
+
// and must drop CYC following any bus error
|
| 412 |
+
.OPT_BUS_ABORT(1'b1),
|
| 413 |
+
//
|
| 414 |
+
// If true, allow the bus to be kept open when there are no
|
| 415 |
+
// outstanding requests. This is useful for any master that
|
| 416 |
+
// might execute a read modify write cycle, such as an atomic
|
| 417 |
+
// add.
|
| 418 |
+
.F_OPT_RMW_BUS_OPTION (1),
|
| 419 |
+
//
|
| 420 |
+
//
|
| 421 |
+
// If true, allow the bus to issue multiple discontinuous
|
| 422 |
+
// requests.
|
| 423 |
+
// Unlike F_OPT_RMW_BUS_OPTION, these requests may be issued
|
| 424 |
+
// while other requests are outstanding
|
| 425 |
+
.F_OPT_DISCONTINUOUS(1),
|
| 426 |
+
//
|
| 427 |
+
//
|
| 428 |
+
// If true, insist that there be a minimum of a single clock
|
| 429 |
+
// delay between request and response. This defaults to off
|
| 430 |
+
// since the wishbone specification specifically doesn't
|
| 431 |
+
// require this. However, some interfaces do, so we allow it
|
| 432 |
+
// as an option here.
|
| 433 |
+
.F_OPT_MINCLOCK_DELAY(1)
|
| 434 |
+
) fwb_master (
|
| 435 |
+
// {{{
|
| 436 |
+
.i_clk(i_clk),
|
| 437 |
+
.i_reset(!i_rst_n),
|
| 438 |
+
// The Wishbone bus
|
| 439 |
+
.i_wb_cyc(o_wb_cyc_data),
|
| 440 |
+
.i_wb_stb(o_wb_stb_data),
|
| 441 |
+
.i_wb_we(o_wb_we_data),
|
| 442 |
+
.i_wb_addr(o_wb_addr_data),
|
| 443 |
+
.i_wb_data(o_wb_data_data),
|
| 444 |
+
.i_wb_sel(o_wb_sel_data),
|
| 445 |
+
//
|
| 446 |
+
.i_wb_ack(i_wb_ack_data),
|
| 447 |
+
.i_wb_stall(i_wb_stall_data),
|
| 448 |
+
.i_wb_idata(i_wb_data_data),
|
| 449 |
+
.i_wb_err(1'b0),
|
| 450 |
+
// Some convenience output parameters
|
| 451 |
+
.f_nreqs(),
|
| 452 |
+
.f_nacks(),
|
| 453 |
+
.f_outstanding(f_outstanding)
|
| 454 |
+
// }}}
|
| 455 |
+
);
|
| 456 |
+
always @* begin
|
| 457 |
+
assert(f_outstanding <= 1);
|
| 458 |
+
if(f_outstanding == 1) begin
|
| 459 |
+
assert(!o_wb_stb_data);
|
| 460 |
+
end
|
| 461 |
+
end
|
| 462 |
+
|
| 463 |
+
/*
|
| 464 |
+
//////////////////////////////////////////////// verify Operand Forwarding ///////////////////////////////////////////////////
|
| 465 |
+
reg[4:0] f_alu_rs2_addr;
|
| 466 |
+
reg[4:0] f_memoryaccess_rs1_addr;
|
| 467 |
+
reg[4:0] f_memoryaccess_rs2_addr;
|
| 468 |
+
reg[31:0] f_memoryaccess_rs1;
|
| 469 |
+
reg[31:0] f_memoryaccess_rs2;
|
| 470 |
+
|
| 471 |
+
always @(posedge i_clk) begin
|
| 472 |
+
if(alu_ce && !(stall[`ALU] || stall[`MEMORYACCESS] || stall[`WRITEBACK])) begin //store rs2_addr pipeline register for ALU stage
|
| 473 |
+
f_alu_rs2_addr <= decoder_rs2_addr_q;
|
| 474 |
+
end
|
| 475 |
+
if(memoryaccess_ce && !(stall[`MEMORYACCESS] || stall[`WRITEBACK])) begin //store rs1_addr, rs2_addr, rs1, and rs2 pipeline registers for STAGE 4
|
| 476 |
+
f_memoryaccess_rs1_addr <= alu_rs1_addr;
|
| 477 |
+
f_memoryaccess_rs2_addr <= f_alu_rs2_addr;
|
| 478 |
+
f_memoryaccess_rs1 <= alu_rs1;
|
| 479 |
+
f_memoryaccess_rs2 <= alu_rs2;
|
| 480 |
+
end
|
| 481 |
+
end
|
| 482 |
+
always @(posedge i_clk) begin
|
| 483 |
+
if(writeback_ce) begin //Stage 5 is enabled
|
| 484 |
+
if(f_memoryaccess_rs1_addr != 0) begin
|
| 485 |
+
assert(f_memoryaccess_rs1 == m0.base_regfile[f_memoryaccess_rs1_addr]); //verify that the rs1 value used from the ALU stage is the MOST updated value
|
| 486 |
+
end
|
| 487 |
+
else assert(f_memoryaccess_rs1 == 0);
|
| 488 |
+
|
| 489 |
+
if(f_memoryaccess_rs2_addr != 0) begin
|
| 490 |
+
assert(f_memoryaccess_rs2 == m0.base_regfile[f_memoryaccess_rs2_addr]); //verify that the rs2 value used from the ALU stage is the MOST updated value
|
| 491 |
+
end
|
| 492 |
+
else assert(f_memoryaccess_rs2 == 0);
|
| 493 |
+
end
|
| 494 |
+
end
|
| 495 |
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
///////////////////////////////// verify that taken branches, jumps, and traps will update PC ///////////////////////////////////
|
| 499 |
+
always @(posedge i_clk) begin
|
| 500 |
+
// change_pc in stage 5 (due to traps) will force first stage to change PC in next clk cycle and all _ce to be
|
| 501 |
+
// disabled
|
| 502 |
+
if($past(writeback_change_pc) && $past(writeback_ce) && i_rst_n && f_past_valid) begin
|
| 503 |
+
assert(o_iaddr == $past(writeback_next_pc));
|
| 504 |
+
assert({writeback_ce,memoryaccess_ce,alu_ce,decoder_ce} == 0);
|
| 505 |
+
end
|
| 506 |
+
|
| 507 |
+
// change_pc in stage 3 (due to jumps and branches) will force first stage to change PC in next clock cycle unless
|
| 508 |
+
// stalled by stage 3(due to data dependency) or stage 4(due to load instruction) or be flushed by stage 5(due traps)
|
| 509 |
+
// and all _ce of previous stages of STAGE 3 to be disabled
|
| 510 |
+
else if($past(alu_change_pc) && $past(alu_ce) && !$past(stall_alu) && i_rst_n && f_past_valid) begin
|
| 511 |
+
assert(o_iaddr == $past(alu_next_pc));
|
| 512 |
+
assert({alu_ce,decoder_ce} == 0);
|
| 513 |
+
end
|
| 514 |
+
|
| 515 |
+
// verify that if no taken branches,jumps,or traps then PC will just be added by 4
|
| 516 |
+
if(!$past(writeback_change_pc) && !$past(alu_change_pc) && !$past(stall_decoder || stall_alu || stall_memoryaccess || stall_writeback)
|
| 517 |
+
&& $past(i_rst_n) && i_rst_n && f_past_valid) begin
|
| 518 |
+
assert(o_iaddr == $past(o_iaddr)+4);
|
| 519 |
+
end
|
| 520 |
+
end
|
| 521 |
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 522 |
+
*/
|
| 523 |
+
|
| 524 |
+
/*
|
| 525 |
+
//////////////////////////////////////// verify valid writes to basereg and data memory /////////////////////////////////////////
|
| 526 |
+
always @(posedge i_clk) begin
|
| 527 |
+
// verify that basereg will be written only if writeback_ce is high
|
| 528 |
+
if(writeback_wr_rd) assert(writeback_ce);
|
| 529 |
+
|
| 530 |
+
// verify data memory will be written at next clk cycle only if memoryaccess_ce is high and stage 5 does not have to change PC
|
| 531 |
+
if(o_wr_en) assert($past(memoryaccess_ce) && !$past(writeback_change_pc) && !writeback_change_pc);
|
| 532 |
+
end
|
| 533 |
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
/////////////////////////////////////////////////// verify pipeline stalls /////////////////////////////////////////////////////
|
| 538 |
+
reg cover_tick = 0;
|
| 539 |
+
always @(posedge i_clk) begin
|
| 540 |
+
// verify that when stalled, PC address and _ce will not change
|
| 541 |
+
if(($past(stall)!=0) && i_rst_n && f_past_valid) assert(o_iaddr == $past(o_iaddr));
|
| 542 |
+
if($past(stall[`WRITEBACK]) && i_rst_n && f_past_valid) begin
|
| 543 |
+
assert(writeback_ce == $past(writeback_ce));
|
| 544 |
+
assert(memoryaccess_ce == $past(memoryaccess_ce));
|
| 545 |
+
assert(alu_ce == $past(alu_ce));
|
| 546 |
+
assert(decoder_ce == $past(decoder_ce));
|
| 547 |
+
assert(fetch_ce == $past(fetch_ce));
|
| 548 |
+
end
|
| 549 |
+
if($past(stall[`MEMORYACCESS]) && i_rst_n && f_past_valid) begin
|
| 550 |
+
assert(memoryaccess_ce == $past(memoryaccess_ce));
|
| 551 |
+
assert(alu_ce == $past(alu_ce));
|
| 552 |
+
assert(decoder_ce == $past(decoder_ce));
|
| 553 |
+
assert(fetch_ce == $past(fetch_ce));
|
| 554 |
+
end
|
| 555 |
+
if($past(stall[`ALU]) && i_rst_n && f_past_valid) begin
|
| 556 |
+
assert(alu_ce == $past(alu_ce));
|
| 557 |
+
assert(decoder_ce == $past(decoder_ce));
|
| 558 |
+
assert(fetch_ce == $past(fetch_ce));
|
| 559 |
+
end
|
| 560 |
+
if($past(stall[`DECODER]) && i_rst_n && f_past_valid) begin
|
| 561 |
+
assert(decoder_ce == $past(decoder_ce));
|
| 562 |
+
assert(fetch_ce == $past(fetch_ce));
|
| 563 |
+
end
|
| 564 |
+
if($past(stall[`FETCH]) && i_rst_n && f_past_valid) begin
|
| 565 |
+
assert(fetch_ce == $past(fetch_ce));
|
| 566 |
+
end
|
| 567 |
+
|
| 568 |
+
//verify that output states of ALU stage will not change if pipeline is stalled
|
| 569 |
+
if($past(alu_ce) && $past(stall[`MEMORYACCESS]) && i_rst_n && f_past_valid) begin
|
| 570 |
+
assert(alu_change_pc == $past(alu_change_pc));
|
| 571 |
+
assert(alu_next_pc == $past(alu_next_pc));
|
| 572 |
+
assert(alu_force_stall == $past(alu_force_stall));
|
| 573 |
+
end
|
| 574 |
+
|
| 575 |
+
// verify that if a stage is stalled, then the previous stage should be stalled too
|
| 576 |
+
if(stall[`WRITEBACK]) assert(stall[`MEMORYACCESS]);
|
| 577 |
+
if(stall[`MEMORYACCESS] || (alu_force_stall && !writeback_change_pc)) assert(stall[`ALU]);
|
| 578 |
+
if(stall[`ALU]) assert(stall[`DECODER]);
|
| 579 |
+
if(stall[`DECODER]) assert(stall[`FETCH]);
|
| 580 |
+
if(writeback_change_pc) assert(stall == 0); //pipeline will never be stalled and flushed(by stage 5) at same time
|
| 581 |
+
//No stall can stop flush from stage 5
|
| 582 |
+
|
| 583 |
+
// verify that if stage 4 is stalled while stage 5 is not, stage 5 will be disabled at next clk cycle (writeback_ce wil be low) (pipeline bubbling)
|
| 584 |
+
if($past(stall[`MEMORYACCESS]) && !$past(stall[`WRITEBACK]) && $past(i_rst_n) && i_rst_n && f_past_valid) begin
|
| 585 |
+
assert(memoryaccess_ce && !writeback_ce);
|
| 586 |
+
end
|
| 587 |
+
// verify that if stage 3 is stalled while stage 4 is not, stage 4 will be disabled at next clk cycle (memoryaccess_ce will be low) (pipeline bubbling)
|
| 588 |
+
if($past(alu_force_stall) && !$past(stall[`MEMORYACCESS]) && $past(i_rst_n) && i_rst_n && !$past(writeback_change_pc) && f_past_valid) begin
|
| 589 |
+
assert(alu_ce && !memoryaccess_ce);
|
| 590 |
+
end
|
| 591 |
+
|
| 592 |
+
end
|
| 593 |
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 594 |
+
*/
|
| 595 |
+
|
| 596 |
+
/*
|
| 597 |
+
//////////////////////////////////////// verify increments of mcycle and minstret CSR ///////////////////////////////////////////
|
| 598 |
+
always @(posedge i_clk) begin
|
| 599 |
+
//verify mcycle will always increment
|
| 600 |
+
if(!$past(zicsr.m6.mcountinhibit_cy) && $past(i_rst_n) && i_rst_n && f_past_valid) begin
|
| 601 |
+
assert(zicsr.m6.mcycle == $past(zicsr.m6.mcycle) + 1);
|
| 602 |
+
end
|
| 603 |
+
|
| 604 |
+
//verify minstret will increment for every instruction executed (except for go_to_trap and return_from_trap)
|
| 605 |
+
if($past(!zicsr.m6.mcountinhibit_ir && writeback_ce && !stall[`WRITEBACK] && !csr_go_to_trap && !csr_return_from_trap && i_rst_n) && i_rst_n) begin
|
| 606 |
+
assert(zicsr.m6.minstret == $past(zicsr.m6.minstret) + 1);
|
| 607 |
+
end
|
| 608 |
+
end
|
| 609 |
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 610 |
+
*/
|
| 611 |
+
|
| 612 |
+
|
| 613 |
+
////////////////////////////////////////////////////// COVER STATEMENTS /////////////////////////////////////////////////////////
|
| 614 |
+
/*
|
| 615 |
+
always @(posedge i_clk) begin
|
| 616 |
+
// cover 10 instruction executed
|
| 617 |
+
cover(zicsr.m6.minstret == 10);
|
| 618 |
+
// cover write to basereg address 2
|
| 619 |
+
cover(($past(m0.base_regfile[2]) != m0.base_regfile[2]) && f_past_valid);
|
| 620 |
+
// cover if basereg can change without the wr_rd enabled by writeback stage [FAIL]
|
| 621 |
+
//cover(($past(m0.base_regfile[3]) != m0.base_regfile[3] && f_past_valid) && !$past(writeback_wr_rd));
|
| 622 |
+
end
|
| 623 |
+
*/
|
| 624 |
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 625 |
+
`endif
|
| 626 |
+
endmodule
|
AngeloJacobo_RISC-V/rtl/rv32i_csr.v
ADDED
|
@@ -0,0 +1,540 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ale 1ns / 1ps
|
| 4 |
+
`default_nettype none
|
| 5 |
+
`include "rv32i_header.vh"
|
| 6 |
+
|
| 7 |
+
module rv32i_csr #(parameter TRAP_ADDRESS = 0) (
|
| 8 |
+
input wire i_clk, i_rst_n,
|
| 9 |
+
// Interrupts
|
| 10 |
+
input wire i_external_interrupt, //interrupt from external source
|
| 11 |
+
input wire i_software_interrupt, //interrupt from software (inter-processor interrupt)
|
| 12 |
+
input wire i_timer_interrupt, //interrupt from timer
|
| 13 |
+
/// Exceptions ///
|
| 14 |
+
input wire i_is_inst_illegal, //illegal instruction
|
| 15 |
+
input wire i_is_ecall, //ecall instruction
|
| 16 |
+
input wire i_is_ebreak, //ebreak instruction
|
| 17 |
+
input wire i_is_mret, //mret (return from trap) instruction
|
| 18 |
+
/// Instruction/Load/Store Misaligned Exception///
|
| 19 |
+
input wire[`OPCODE_WIDTH-1:0] i_opcode, //opcode types
|
| 20 |
+
input wire[31:0] i_y, //y value from ALU (address used in load/store/jump/branch)
|
| 21 |
+
/// CSR instruction ///
|
| 22 |
+
input wire[2:0] i_funct3, // CSR instruction operation
|
| 23 |
+
input wire[11:0] i_csr_index, // immediate value from decoder
|
| 24 |
+
input wire[31:0] i_imm, //unsigned immediate for immediate type of CSR instruction (new value to be stored to CSR)
|
| 25 |
+
input wire[31:0] i_rs1, //Source register 1 value (new value to be stored to CSR)
|
| 26 |
+
output reg[31:0] o_csr_out, //CSR value to be loaded to basereg
|
| 27 |
+
// Trap-Handler
|
| 28 |
+
input wire[31:0] i_pc, //Program Counter
|
| 29 |
+
input wire writeback_change_pc, //high if writeback will issue change_pc (which will override this stage)
|
| 30 |
+
output reg[31:0] o_return_address, //mepc CSR
|
| 31 |
+
output reg[31:0] o_trap_address, //mtvec CSR
|
| 32 |
+
output reg o_go_to_trap_q, //high before going to trap (if exception/interrupt detected)
|
| 33 |
+
output reg o_return_from_trap_q, //high before returning from trap (via mret)
|
| 34 |
+
input wire i_minstret_inc, //increment minstret after executing an instruction
|
| 35 |
+
/// Pipeline Control ///
|
| 36 |
+
input wire i_ce, // input clk enable for pipeline stalling of this stage
|
| 37 |
+
input wire i_stall //informs this stage to stall
|
| 38 |
+
);
|
| 39 |
+
|
| 40 |
+
//CSR operation type
|
| 41 |
+
localparam CSRRW = 3'b001,
|
| 42 |
+
CSRRS = 3'b010,
|
| 43 |
+
CSRRC = 3'b011,
|
| 44 |
+
CSRRWI = 3'b101,
|
| 45 |
+
CSRRSI = 3'b110,
|
| 46 |
+
CSRRCI = 3'b111;
|
| 47 |
+
|
| 48 |
+
//CSR addresses
|
| 49 |
+
//machine info
|
| 50 |
+
localparam MVENDORID = 12'hF11,
|
| 51 |
+
MARCHID = 12'hF12,
|
| 52 |
+
MIMPID = 12'hF13,
|
| 53 |
+
MHARTID = 12'hF14,
|
| 54 |
+
//machine trap setup
|
| 55 |
+
MSTATUS = 12'h300,
|
| 56 |
+
MISA = 12'h301,
|
| 57 |
+
MIE = 12'h304,
|
| 58 |
+
MTVEC = 12'h305,
|
| 59 |
+
//machine trap handling
|
| 60 |
+
MSCRATCH = 12'h340,
|
| 61 |
+
MEPC = 12'h341,
|
| 62 |
+
MCAUSE = 12'h342,
|
| 63 |
+
MTVAL = 12'h343,
|
| 64 |
+
MIP = 12'h344,
|
| 65 |
+
//machine counters/timers
|
| 66 |
+
MCYCLE = 12'hB00,
|
| 67 |
+
MCYCLEH = 12'hB80,
|
| 68 |
+
//TIME = 12'hC01,
|
| 69 |
+
//TIMEH = 12'hC81,
|
| 70 |
+
MINSTRET = 12'hB02,
|
| 71 |
+
MINSTRETH = 12'hBB2,
|
| 72 |
+
MCOUNTINHIBIT = 12'h320;
|
| 73 |
+
|
| 74 |
+
//mcause codes
|
| 75 |
+
localparam MACHINE_SOFTWARE_INTERRUPT =3,
|
| 76 |
+
MACHINE_TIMER_INTERRUPT = 7,
|
| 77 |
+
MACHINE_EXTERNAL_INTERRUPT = 11,
|
| 78 |
+
INSTRUCTION_ADDRESS_MISALIGNED = 0,
|
| 79 |
+
ILLEGAL_INSTRUCTION = 2,
|
| 80 |
+
EBREAK = 3,
|
| 81 |
+
LOAD_ADDRESS_MISALIGNED = 4,
|
| 82 |
+
STORE_ADDRESS_MISALIGNED = 6,
|
| 83 |
+
ECALL = 11;
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
wire opcode_store=i_opcode[`STORE];
|
| 87 |
+
wire opcode_load=i_opcode[`LOAD];
|
| 88 |
+
wire opcode_branch=i_opcode[`BRANCH];
|
| 89 |
+
wire opcode_jal=i_opcode[`JAL];
|
| 90 |
+
wire opcode_jalr=i_opcode[`JALR];
|
| 91 |
+
wire opcode_system=i_opcode[`SYSTEM];
|
| 92 |
+
reg[31:0] csr_in; //value to be stored to CSR
|
| 93 |
+
reg[31:0] csr_data; //value at current CSR address
|
| 94 |
+
wire csr_enable = opcode_system && i_funct3!=0 && i_ce && !writeback_change_pc; //csr read/write operation is enabled only at this conditions
|
| 95 |
+
reg[1:0] new_pc = 0; //last two bits of i_pc that will be used in taken branch and jumps
|
| 96 |
+
reg go_to_trap; //high before going to trap (if exception/interrupt detected)
|
| 97 |
+
reg return_from_trap; //high before returning from trap (via mret)
|
| 98 |
+
reg is_load_addr_misaligned;
|
| 99 |
+
reg is_store_addr_misaligned;
|
| 100 |
+
reg is_inst_addr_misaligned;
|
| 101 |
+
//reg timer_interrupt;
|
| 102 |
+
reg external_interrupt_pending;
|
| 103 |
+
reg software_interrupt_pending;
|
| 104 |
+
reg timer_interrupt_pending;
|
| 105 |
+
reg is_interrupt;
|
| 106 |
+
reg is_exception;
|
| 107 |
+
reg is_trap;
|
| 108 |
+
wire stall_bit =i_stall;
|
| 109 |
+
|
| 110 |
+
// CSR register bits
|
| 111 |
+
reg mstatus_mie; //Machine Interrupt Enable
|
| 112 |
+
reg mstatus_mpie; //Machine Previous Interrupt Enable
|
| 113 |
+
reg[1:0] mstatus_mpp; //MPP
|
| 114 |
+
reg mie_meie; //machine external interrupt enable
|
| 115 |
+
reg mie_mtie; //machine timer interrupt enable
|
| 116 |
+
reg mie_msie; //machine software interrupt enable
|
| 117 |
+
reg[29:0] mtvec_base; //address of i_pc after returning from interrupt (via MRET)
|
| 118 |
+
reg[1:0] mtvec_mode; //vector mode addressing
|
| 119 |
+
reg[31:0] mscratch; //dedicated for use by machine code
|
| 120 |
+
reg[31:0] mepc; //machine exception i_pc (address of interrupted instruction)
|
| 121 |
+
reg mcause_intbit; //interrupt(1) or exception(0)
|
| 122 |
+
reg[3:0] mcause_code; //indicates event that caused the trap
|
| 123 |
+
reg[31:0] mtval; //exception-specific infotmation to assist software in handling trap
|
| 124 |
+
reg mip_meip; //machine external interrupt pending
|
| 125 |
+
reg mip_mtip; //machine timer interrupt pending
|
| 126 |
+
reg mip_msip; //machine software interrupt pending
|
| 127 |
+
reg[63:0] mcycle; //counts number of i_clk cycle executed by core
|
| 128 |
+
//reg[63:0] mtime; //real-time i_clk (millisecond increment)
|
| 129 |
+
//reg[$clog2(MILLISEC_WRAP)-1:0] millisec; //counter with period of 1 millisec
|
| 130 |
+
//reg[63:0] mtimecmp; //compare register for mtime
|
| 131 |
+
reg[63:0] minstret; //counts number instructions retired/executed by core
|
| 132 |
+
reg mcountinhibit_cy; //controls increment of mcycle
|
| 133 |
+
reg mcountinhibit_ir; //controls increment of minstret
|
| 134 |
+
|
| 135 |
+
//control logic for load/store/instruction misaligned exception detection
|
| 136 |
+
always @* begin
|
| 137 |
+
is_load_addr_misaligned = 0;
|
| 138 |
+
is_store_addr_misaligned = 0;
|
| 139 |
+
is_inst_addr_misaligned = 0;
|
| 140 |
+
new_pc = 0;
|
| 141 |
+
|
| 142 |
+
// Misaligned Load/Store Address
|
| 143 |
+
if(i_funct3[1:0] == 2'b01) begin //halfword load/store
|
| 144 |
+
is_load_addr_misaligned = opcode_load? i_y[0] : 0;
|
| 145 |
+
is_store_addr_misaligned = opcode_store? i_y[0] : 0;
|
| 146 |
+
end
|
| 147 |
+
if(i_funct3[1:0] == 2'b10) begin //word load/store
|
| 148 |
+
is_load_addr_misaligned = opcode_load? i_y[1:0]!=2'b00 : 0;
|
| 149 |
+
is_store_addr_misaligned = opcode_store? i_y[1:0]!=2'b00 : 0;
|
| 150 |
+
end
|
| 151 |
+
|
| 152 |
+
// Misaligned Instruction Address
|
| 153 |
+
/* Volume 1 pg. 15: Instructions are 32 bits in length and must be aligned on a four-byte boundary in memory.
|
| 154 |
+
An instruction-address-misaligned exception is generated on a taken branch or unconditional jump
|
| 155 |
+
if the target address is not four-byte aligned. This exception is reported on the branch or jump
|
| 156 |
+
instruction, not on the target instruction. No instruction-address-misaligned exception is generated
|
| 157 |
+
for a conditional branch that is not taken. */
|
| 158 |
+
if((opcode_branch && i_y[0]) || opcode_jal || opcode_jalr) begin // branch or jump to new instruction
|
| 159 |
+
new_pc = i_pc[1:0] + i_csr_index[1:0];
|
| 160 |
+
if(opcode_jalr) new_pc = i_rs1[1:0] + i_csr_index[1:0];
|
| 161 |
+
is_inst_addr_misaligned = (new_pc == 2'b00)? 1'b0:1'b1; //i_pc (instruction address) must always be four bytes aligned
|
| 162 |
+
end
|
| 163 |
+
|
| 164 |
+
end
|
| 165 |
+
|
| 166 |
+
//control logic for writing to CSRs
|
| 167 |
+
always @(posedge i_clk,negedge i_rst_n) begin
|
| 168 |
+
if(!i_rst_n) begin
|
| 169 |
+
o_go_to_trap_q <= 0;
|
| 170 |
+
o_return_from_trap_q <= 0;
|
| 171 |
+
mstatus_mie <= 0;
|
| 172 |
+
mstatus_mpie <= 0;
|
| 173 |
+
mstatus_mpp <= 2'b11;
|
| 174 |
+
mie_meie <= 0;
|
| 175 |
+
mie_mtie <= 0;
|
| 176 |
+
mie_msie <= 0;
|
| 177 |
+
mtvec_base <= TRAP_ADDRESS[31:2];
|
| 178 |
+
mtvec_mode <= TRAP_ADDRESS[1:0];
|
| 179 |
+
mscratch <= 0;
|
| 180 |
+
mepc <= 0;
|
| 181 |
+
mcause_intbit <= 0;
|
| 182 |
+
mcause_code <= 0;
|
| 183 |
+
mtval <= 0;
|
| 184 |
+
mip_meip <= 0;
|
| 185 |
+
mip_meip <= 0;
|
| 186 |
+
mip_msip <= 0;
|
| 187 |
+
mcycle <= 0;
|
| 188 |
+
//mtime <= 0;
|
| 189 |
+
//millisec <= 0;
|
| 190 |
+
//mtimecmp <= -1; //timer interrup will be triggered uninttentionally if reset at 0 (equal to mtime)
|
| 191 |
+
minstret <= 0;
|
| 192 |
+
mcountinhibit_cy <= 0;
|
| 193 |
+
mcountinhibit_ir <= 0;
|
| 194 |
+
end
|
| 195 |
+
else if(!stall_bit) begin
|
| 196 |
+
/***************************************************** CSR control logic *****************************************************/
|
| 197 |
+
//MSTATUS (controls hart's current operating state (mie and mpie are the only configurable bits))
|
| 198 |
+
if(i_csr_index == MSTATUS && csr_enable) begin
|
| 199 |
+
mstatus_mie <= csr_in[3];
|
| 200 |
+
mstatus_mpie <= csr_in[7];
|
| 201 |
+
//mstatus_mpp <= csr_in[12:11];
|
| 202 |
+
end
|
| 203 |
+
else begin
|
| 204 |
+
if(go_to_trap && !o_go_to_trap_q) begin
|
| 205 |
+
/* Volume 2 pg. 21: xPIE holds the value of the interrupt-enable bit active prior to the trap.
|
| 206 |
+
When a trap is taken from privilege mode y into privilege mode x,xPIE is set to the value of x IE;
|
| 207 |
+
x IE is set to 0; and xPP is set to y. */
|
| 208 |
+
mstatus_mie <= 0; //no nested interrupt allowed
|
| 209 |
+
mstatus_mpie <= mstatus_mie;
|
| 210 |
+
mstatus_mpp <= 2'b11;
|
| 211 |
+
end
|
| 212 |
+
else if(return_from_trap) begin
|
| 213 |
+
/* Volume 2 pg. 21: An MRET or SRET instruction is used to return from a trap in M-mode or S-mode respectively.
|
| 214 |
+
When executing an xRET instruction, supposing xPP holds the value y, xIE is set to xPIE; the
|
| 215 |
+
privilege mode is changed to y; xPIE is set to 1; */
|
| 216 |
+
mstatus_mie <= mstatus_mpie;
|
| 217 |
+
mstatus_mpie <= 1;
|
| 218 |
+
mstatus_mpp <= 2'b11;
|
| 219 |
+
end
|
| 220 |
+
end
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
//MIE (interrupt enable bits)
|
| 224 |
+
if(i_csr_index == MIE && csr_enable) begin
|
| 225 |
+
mie_msie <= csr_in[3];
|
| 226 |
+
mie_mtie <= csr_in[7];
|
| 227 |
+
mie_meie <= csr_in[11];
|
| 228 |
+
end
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
//MTVEC (trap vector configuration (base+mode))
|
| 232 |
+
if(i_csr_index == MTVEC && csr_enable) begin
|
| 233 |
+
mtvec_base <= csr_in[31:2];
|
| 234 |
+
mtvec_mode <= csr_in[1:0];
|
| 235 |
+
end
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
//MSCRATCH (dedicated for use by machine code)
|
| 239 |
+
if(i_csr_index == MSCRATCH && csr_enable) begin
|
| 240 |
+
mscratch <= csr_in;
|
| 241 |
+
end
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
//MEPC (address of interrupted instruction)
|
| 245 |
+
if(i_csr_index == MEPC && csr_enable) begin
|
| 246 |
+
mepc <= {csr_in[31:2],2'b00};
|
| 247 |
+
end
|
| 248 |
+
/* Volume 2 pg. 38: When a trap is taken into M-mode, mepc is written with the virtual address of the
|
| 249 |
+
instruction that was interrupted or that encountered the exception */
|
| 250 |
+
if(go_to_trap && !o_go_to_trap_q) mepc <= i_pc;
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
//MCAUSE (indicates cause of trap(either interrupt or exception))
|
| 254 |
+
if(i_csr_index == MCAUSE && csr_enable) begin
|
| 255 |
+
mcause_intbit <= csr_in[31];
|
| 256 |
+
mcause_code <= csr_in[3:0];
|
| 257 |
+
end
|
| 258 |
+
/* Volume 2 pg. 38: When a trap is taken into M-mode, mcause is written with a code indicating the event that caused the trap */
|
| 259 |
+
// Interrupts have priority (external first, then s/w, then timer---[2] sec 3.1.9), then synchronous traps.
|
| 260 |
+
if(go_to_trap && !o_go_to_trap_q) begin
|
| 261 |
+
if(external_interrupt_pending) begin
|
| 262 |
+
mcause_code <= MACHINE_EXTERNAL_INTERRUPT;
|
| 263 |
+
mcause_intbit <= 1;
|
| 264 |
+
end
|
| 265 |
+
else if(software_interrupt_pending) begin
|
| 266 |
+
mcause_code <= MACHINE_SOFTWARE_INTERRUPT;
|
| 267 |
+
mcause_intbit <= 1;
|
| 268 |
+
end
|
| 269 |
+
else if(timer_interrupt_pending) begin
|
| 270 |
+
mcause_code <= MACHINE_TIMER_INTERRUPT;
|
| 271 |
+
mcause_intbit <= 1;
|
| 272 |
+
end
|
| 273 |
+
else if(i_is_inst_illegal) begin
|
| 274 |
+
mcause_code <= ILLEGAL_INSTRUCTION;
|
| 275 |
+
mcause_intbit <= 0 ;
|
| 276 |
+
end
|
| 277 |
+
else if(is_inst_addr_misaligned) begin
|
| 278 |
+
mcause_code <= INSTRUCTION_ADDRESS_MISALIGNED;
|
| 279 |
+
mcause_intbit <= 0;
|
| 280 |
+
end
|
| 281 |
+
else if(i_is_ecall) begin
|
| 282 |
+
mcause_code <= ECALL;
|
| 283 |
+
mcause_intbit <= 0;
|
| 284 |
+
end
|
| 285 |
+
else if(i_is_ebreak) begin
|
| 286 |
+
mcause_code <= EBREAK;
|
| 287 |
+
mcause_intbit <= 0;
|
| 288 |
+
end
|
| 289 |
+
else if(is_load_addr_misaligned) begin
|
| 290 |
+
mcause_code <= LOAD_ADDRESS_MISALIGNED;
|
| 291 |
+
mcause_intbit <= 0;
|
| 292 |
+
end
|
| 293 |
+
else if(is_store_addr_misaligned) begin
|
| 294 |
+
mcause_code <= STORE_ADDRESS_MISALIGNED;
|
| 295 |
+
mcause_intbit <= 0;
|
| 296 |
+
end
|
| 297 |
+
end
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
//MTVAL (exception-specific information to assist software in handling trap)
|
| 301 |
+
if(i_csr_index == MTVAL && csr_enable) begin
|
| 302 |
+
mtval <= csr_in;
|
| 303 |
+
end
|
| 304 |
+
/*If mtval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or
|
| 305 |
+
page-fault exception occurs on an instruction fetch, load, or store, then mtval will contain the
|
| 306 |
+
faulting virtual address.*/
|
| 307 |
+
if(go_to_trap && !o_go_to_trap_q) begin
|
| 308 |
+
if(is_load_addr_misaligned || is_store_addr_misaligned) mtval <= i_y;
|
| 309 |
+
end
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
//MCYCLE (counts number of i_clk cycle executed by core [LOWER HALF])
|
| 313 |
+
if(i_csr_index == MCYCLE && csr_enable) begin
|
| 314 |
+
mcycle[31:0] <= csr_in;
|
| 315 |
+
end
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
//MCYCLEH (counts number of i_clk cycle executed by core [UPPER HALF])
|
| 319 |
+
if(i_csr_index == MCYCLEH && csr_enable) begin
|
| 320 |
+
mcycle[63:32] <= csr_in;
|
| 321 |
+
end
|
| 322 |
+
mcycle <= mcountinhibit_cy? mcycle : mcycle + 1; //increments mcycle every clock cycle
|
| 323 |
+
|
| 324 |
+
//MTIME (real-time counter [millisecond increment])
|
| 325 |
+
/* Volume 2 pg. 44: Platforms provide a real-time counter, exposed as a memory-mapped machine-mode
|
| 326 |
+
read-write register, mtime. mtime must increment at constant frequency, and the platform must provide a
|
| 327 |
+
mechanism for determining the period of an mtime tick. */
|
| 328 |
+
/*
|
| 329 |
+
if(i_mtime_wr) begin
|
| 330 |
+
mtime<=i_mtime_din;
|
| 331 |
+
millisec <= 0;
|
| 332 |
+
end
|
| 333 |
+
else begin
|
| 334 |
+
millisec <= (millisec == MILLISEC_WRAP)? 0 : millisec + 1'b1; //mod-one-millisecond counter
|
| 335 |
+
mtime <= mtime + ((millisec==MILLISEC_WRAP)? 1:0); //counter that increments every 1 millisecond
|
| 336 |
+
end
|
| 337 |
+
*/
|
| 338 |
+
/* Volume 2 pg. 44: Platforms provide a 64-bit memory-mapped machine-mode timer compare register (mtimecmp).
|
| 339 |
+
A machine timer interrupt becomes pending whenever mtime contains a value greater than or equal to mtimecmp,
|
| 340 |
+
treating the values as unsigned integers. The interrupt remains posted until mtimecmp becomes greater than
|
| 341 |
+
mtime (typically as a result of writing mtimecmp). */
|
| 342 |
+
/*
|
| 343 |
+
if(i_mtimecmp_wr) begin
|
| 344 |
+
mtimecmp <= i_mtimecmp_din;
|
| 345 |
+
end
|
| 346 |
+
timer_interrupt = (mtime >= mtimecmp)? 1:0;
|
| 347 |
+
*/
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
//MIP (pending interrupts)
|
| 352 |
+
mip_msip <= i_software_interrupt;
|
| 353 |
+
mip_mtip <= i_timer_interrupt;
|
| 354 |
+
mip_meip <= i_external_interrupt;
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
//MINSTRET (counts number instructions retired/executed by core [upper half])
|
| 358 |
+
if(i_csr_index == MINSTRET && csr_enable) begin
|
| 359 |
+
minstret[31:0] <= csr_in;
|
| 360 |
+
end
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
//MINSTRETH (counts number instructions retired/executed by core [lower half])
|
| 364 |
+
if(i_csr_index == MINSTRETH && csr_enable) begin
|
| 365 |
+
minstret[63:32] <= csr_in;
|
| 366 |
+
end
|
| 367 |
+
minstret <= mcountinhibit_ir? minstret : minstret + {63'b0,(i_minstret_inc && !o_go_to_trap_q && !o_return_from_trap_q)}; //increment minstret every instruction
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
//MCOUNTINHIBIT (controls which hardware performance-monitoring counters can increment)
|
| 371 |
+
if(i_csr_index == MCOUNTINHIBIT && csr_enable) begin
|
| 372 |
+
mcountinhibit_cy <= csr_in[0];
|
| 373 |
+
mcountinhibit_ir <= csr_in[2];
|
| 374 |
+
end
|
| 375 |
+
|
| 376 |
+
/****************************************************************************************************************************/
|
| 377 |
+
|
| 378 |
+
/************************************** Registered Outputs for Trap Handlers ************************************************/
|
| 379 |
+
if(i_ce) begin
|
| 380 |
+
o_go_to_trap_q <= go_to_trap;
|
| 381 |
+
o_return_from_trap_q <= return_from_trap;
|
| 382 |
+
o_return_address <= mepc;
|
| 383 |
+
/* Volume 2 pg. 30: When MODE=Direct (0), all traps into machine mode cause the i_pc to be set to the address in the
|
| 384 |
+
BASE field. When MODE=Vectored (1), all synchronous exceptions into machine mode cause the i_pc to be set to the address
|
| 385 |
+
in the BASE field, whereas interrupts cause the i_pc to be set to the address in the BASE field plus four times the
|
| 386 |
+
interrupt cause number */
|
| 387 |
+
if(mtvec_mode[1] && is_interrupt) o_trap_address <= {mtvec_base,2'b00} + {28'b0,mcause_code<<2};
|
| 388 |
+
else o_trap_address <= {mtvec_base,2'b00};
|
| 389 |
+
|
| 390 |
+
/****************************************************************************************************************************/
|
| 391 |
+
|
| 392 |
+
o_csr_out <= csr_data;
|
| 393 |
+
end
|
| 394 |
+
else begin //THIS SOLVES THE PROBLEM OF FREERTOS NOT WORKING
|
| 395 |
+
o_go_to_trap_q <= 0;
|
| 396 |
+
o_return_from_trap_q <= 0;
|
| 397 |
+
end
|
| 398 |
+
end
|
| 399 |
+
else begin
|
| 400 |
+
// this CSR will always be updated
|
| 401 |
+
mcycle <= mcountinhibit_cy? mcycle : mcycle + 1; //increments mcycle every clock cycle
|
| 402 |
+
minstret <= mcountinhibit_ir? minstret : minstret + {63'b0,(i_minstret_inc && !o_go_to_trap_q && !o_return_from_trap_q)}; //increment minstret every instruction
|
| 403 |
+
end
|
| 404 |
+
end
|
| 405 |
+
|
| 406 |
+
always @* begin
|
| 407 |
+
/************************************************** control logic for trap detection **************************************************/
|
| 408 |
+
external_interrupt_pending = 0;
|
| 409 |
+
software_interrupt_pending = 0;
|
| 410 |
+
timer_interrupt_pending = 0;
|
| 411 |
+
is_interrupt = 0;
|
| 412 |
+
is_exception = 0;
|
| 413 |
+
is_trap = 0;
|
| 414 |
+
go_to_trap = 0;
|
| 415 |
+
return_from_trap = 0;
|
| 416 |
+
|
| 417 |
+
if(i_ce) begin
|
| 418 |
+
external_interrupt_pending = mstatus_mie && mie_meie && (mip_meip); //machine_interrupt_enable + machine_external_interrupt_enable + machine_external_interrupt_pending must all be high
|
| 419 |
+
software_interrupt_pending = mstatus_mie && mie_msie && mip_msip; //machine_interrupt_enable + machine_software_interrupt_enable + machine_software_interrupt_pending must all be high
|
| 420 |
+
timer_interrupt_pending = mstatus_mie && mie_mtie && mip_mtip; //machine_interrupt_enable + machine_timer_interrupt_enable + machine_timer_interrupt_pending must all be high
|
| 421 |
+
|
| 422 |
+
is_interrupt = external_interrupt_pending || software_interrupt_pending || timer_interrupt_pending;
|
| 423 |
+
is_exception = (i_is_inst_illegal || is_inst_addr_misaligned || i_is_ecall || i_is_ebreak || is_load_addr_misaligned || is_store_addr_misaligned) && !writeback_change_pc;
|
| 424 |
+
is_trap = is_interrupt || is_exception;
|
| 425 |
+
go_to_trap = is_trap; //a trap is taken, save i_pc, and go to trap address
|
| 426 |
+
return_from_trap = i_is_mret; // return from trap, go back to saved i_pc
|
| 427 |
+
|
| 428 |
+
end
|
| 429 |
+
/*************************************************************************************************************************************/
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
csr_data = 0;
|
| 433 |
+
csr_in = 0;
|
| 434 |
+
/************************************ specify csr_data (data CURRENTLY stored at the CSR) *********************************************/
|
| 435 |
+
case(i_csr_index)
|
| 436 |
+
//machine info
|
| 437 |
+
MVENDORID: csr_data = 32'h0; //MVENDORID (JEDEC manufacturer ID)
|
| 438 |
+
MARCHID: csr_data = 32'h0; //MARCHID (open-source project architecture ID allocated by RISC-V International ( https://github.com/riscv/riscv-isa-manual/blob/master/marchid.md ))
|
| 439 |
+
MIMPID: csr_data = 32'h0; //MIMPID (version of the processor implementation (provided by author of source code))
|
| 440 |
+
MHARTID: csr_data = 32'h0; //MHARTID (integer ID of the hart that is currently running the code (one hart must have an ID of zero))
|
| 441 |
+
|
| 442 |
+
//machine trap setup
|
| 443 |
+
MSTATUS: begin //MSTATUS (controls hart's current operating state (mie and mpie are the only configurable bits))
|
| 444 |
+
csr_data[3] = mstatus_mie;
|
| 445 |
+
csr_data[7] = mstatus_mpie;
|
| 446 |
+
csr_data[12:11] = mstatus_mpp; //MPP
|
| 447 |
+
end
|
| 448 |
+
|
| 449 |
+
MISA: begin //MISA (control and monitor hart's current operating state)
|
| 450 |
+
csr_data[8] = 1'b1; //RV32I/64I/128I base ISA (ISA supported by the hart)
|
| 451 |
+
csr_data[31:30] = 2'b01; //Base 32
|
| 452 |
+
end
|
| 453 |
+
|
| 454 |
+
MIE: begin //MIE (interrupt enable bits)
|
| 455 |
+
csr_data[3] = mie_msie;
|
| 456 |
+
csr_data[7] = mie_mtie;
|
| 457 |
+
csr_data[11] = mie_meie;
|
| 458 |
+
end
|
| 459 |
+
|
| 460 |
+
MTVEC: begin //MTVEC (trap vector configuration (base+mode))
|
| 461 |
+
csr_data = {mtvec_base,mtvec_mode};
|
| 462 |
+
end
|
| 463 |
+
|
| 464 |
+
//machine trap handling
|
| 465 |
+
MSCRATCH: begin //MSCRATCH (dedicated for use by machine code)
|
| 466 |
+
csr_data = mscratch;
|
| 467 |
+
end
|
| 468 |
+
|
| 469 |
+
MEPC: begin //MEPC (address of interrupted instruction)
|
| 470 |
+
csr_data = mepc;
|
| 471 |
+
end
|
| 472 |
+
|
| 473 |
+
MCAUSE: begin //MCAUSE (indicates cause of trap(either interrupt or exception))
|
| 474 |
+
csr_data[31] = mcause_intbit;
|
| 475 |
+
csr_data[3:0] = mcause_code;
|
| 476 |
+
end
|
| 477 |
+
|
| 478 |
+
MTVAL: begin //MTVAL (exception-specific information to assist software in handling trap)
|
| 479 |
+
csr_data = mtval;
|
| 480 |
+
end
|
| 481 |
+
|
| 482 |
+
MIP: begin //MIP (pending interrupts)
|
| 483 |
+
csr_data[3] = mip_msip;
|
| 484 |
+
csr_data[7] = mip_mtip;
|
| 485 |
+
csr_data[11] = mip_meip;
|
| 486 |
+
end
|
| 487 |
+
|
| 488 |
+
//machine counters/timers
|
| 489 |
+
MCYCLE: begin //MCYCLE (counts number of i_clk cycle executed by core [LOWER HALF])
|
| 490 |
+
csr_data = mcycle[31:0];
|
| 491 |
+
end
|
| 492 |
+
|
| 493 |
+
MCYCLEH: begin //MCYCLE (counts number of i_clk cycle executed by core [UPPER HALF])
|
| 494 |
+
csr_data = mcycle[63:32];
|
| 495 |
+
end
|
| 496 |
+
/* timer is brought outside as part of CLINT (Core Logic
|
| 497 |
+
Interrupt and this will be a memory-mapped register
|
| 498 |
+
TIME: begin //TIME (real-time i_clk [millisecond increment] [LOWER HALF])
|
| 499 |
+
csr_data = mtime[31:0];
|
| 500 |
+
end
|
| 501 |
+
|
| 502 |
+
TIMEH: begin //TIME (real-time i_clk [millisecond increment] [LOWER HALF])
|
| 503 |
+
csr_data = mtime[63:32];
|
| 504 |
+
end
|
| 505 |
+
*/
|
| 506 |
+
MINSTRET: begin //MINSTRET (counts number instructions retired/executed by core [LOWER half])
|
| 507 |
+
csr_data = minstret[31:0];
|
| 508 |
+
end
|
| 509 |
+
|
| 510 |
+
MINSTRETH: begin //MINSTRET (counts number instructions retired/executed by core [UPPER half])
|
| 511 |
+
csr_data = minstret[63:32];
|
| 512 |
+
end
|
| 513 |
+
|
| 514 |
+
MCOUNTINHIBIT: begin //MCOUNTINHIBIT (controls which hardware performance-monitoring counters can increment)
|
| 515 |
+
csr_data[0] = mcountinhibit_cy;
|
| 516 |
+
csr_data[2] = mcountinhibit_ir;
|
| 517 |
+
end
|
| 518 |
+
|
| 519 |
+
default: csr_data = 0;
|
| 520 |
+
endcase
|
| 521 |
+
/*****************************************************************************************************************************/
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
/************************************ specify csr_in (data TO BE stored at the CSR ) *****************************************/
|
| 526 |
+
// specify csr_in (data TO BE stored to CSR)
|
| 527 |
+
case(i_funct3) //csr instruction type
|
| 528 |
+
CSRRW: csr_in = i_rs1; //CSR read-write
|
| 529 |
+
CSRRS: csr_in = csr_data | i_rs1; //CSR read-set
|
| 530 |
+
CSRRC: csr_in = csr_data & (~i_rs1); //CSR read-clear
|
| 531 |
+
CSRRWI: csr_in = i_imm; //csr read-write immediate
|
| 532 |
+
CSRRSI: csr_in = csr_data | i_imm; //csr read-set immediate
|
| 533 |
+
CSRRCI: csr_in = csr_data & (~i_imm); //csr read-clear immediate
|
| 534 |
+
default: csr_in = 0;
|
| 535 |
+
endcase
|
| 536 |
+
/*****************************************************************************************************************************/
|
| 537 |
+
|
| 538 |
+
end
|
| 539 |
+
|
| 540 |
+
endmodule
|
AngeloJacobo_RISC-V/rtl/rv32i_decoder.v
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* The rv32i_decoder module is responsible for decoding the 32-bit RISC-V instructions,
|
| 2 |
+
producing the necessary control signals, and extracting the operand addresses and immediate
|
| 3 |
+
values required by the execution stage of the RISC-V processor. It is a crucial component
|
| 4 |
+
in the decode stage of the pipeline. This module has several key components:
|
| 5 |
+
- Operand address extraction:
|
| 6 |
+
The module extracts the source and destination register addresses (rs1, rs2, and rd)
|
| 7 |
+
from the input instruction. These addresses are used to access the register file in
|
| 8 |
+
the next stage of the pipeline.
|
| 9 |
+
- Immediate value extraction: Depending on the instruction type, this module extracts
|
| 10 |
+
and sign-extends the immediate value (imm) from the input instruction. This value is
|
| 11 |
+
used as an operand for arithmetic or load/store instructions, or as an offset for
|
| 12 |
+
branch and jump instructions.
|
| 13 |
+
- ALU operation decoding: The module decodes the required ALU operation based on the
|
| 14 |
+
opcode and funct3 fields of the instruction. It sets the appropriate signals
|
| 15 |
+
(alu_add_d, alu_sub_d, alu_slt_d, etc.) to indicate the desired operation to be
|
| 16 |
+
executed in the ALU during the execution stage.
|
| 17 |
+
- Opcode type decoding: The module identifies the type of instruction based on its opcode
|
| 18 |
+
(opcode_rtype_d, opcode_itype_d, opcode_load_d, etc.). These signals are used in the
|
| 19 |
+
next stages of the pipeline to control the flow of data and determine the required
|
| 20 |
+
operations.
|
| 21 |
+
- Exception decoding: The module checks for illegal instructions, system instructions
|
| 22 |
+
(ECALL, EBREAK, and MRET), and unsupported shift operations. If any of these conditions
|
| 23 |
+
are detected, the corresponding exception signals (o_exception) are set.
|
| 24 |
+
- Pipeline control: The module supports pipeline stalling and flushing. If the next stage
|
| 25 |
+
of the pipeline is stalled (i_stall), the module will stall the decode stage (o_stall)
|
| 26 |
+
and prevent updating the output registers. If a flush signal (i_flush) is received, the
|
| 27 |
+
module will flush its internal state and disable the clock enable signal (o_ce) for the
|
| 28 |
+
next stage.
|
| 29 |
+
*/
|
| 30 |
+
|
| 31 |
+
`timescale 1ns / 1ps
|
| 32 |
+
`default_nettype none
|
| 33 |
+
`include "rv32i_header.vh"
|
| 34 |
+
|
| 35 |
+
module rv32i_decoder(
|
| 36 |
+
input wire i_clk,i_rst_n,
|
| 37 |
+
input wire[31:0] i_inst, //32 bit instruction
|
| 38 |
+
input wire[31:0] i_pc, //PC value from previous stage
|
| 39 |
+
output reg[31:0] o_pc, //PC value
|
| 40 |
+
output wire[4:0] o_rs1_addr,//address for register source 1
|
| 41 |
+
output reg[4:0] o_rs1_addr_q,//registered address for register source 1
|
| 42 |
+
output wire[4:0] o_rs2_addr, //address for register source 2
|
| 43 |
+
output reg[4:0] o_rs2_addr_q, //registered address for register source 2
|
| 44 |
+
output reg[4:0] o_rd_addr, //address for destination address
|
| 45 |
+
output reg[31:0] o_imm, //extended value for immediate
|
| 46 |
+
output reg[2:0] o_funct3, //function type
|
| 47 |
+
output reg[`ALU_WIDTH-1:0] o_alu, //alu operation type
|
| 48 |
+
output reg[`OPCODE_WIDTH-1:0] o_opcode, //opcode type
|
| 49 |
+
output reg[`EXCEPTION_WIDTH-1:0] o_exception, //exceptions: illegal inst, ecall, ebreak, mret
|
| 50 |
+
/// Pipeline Control ///
|
| 51 |
+
input wire i_ce, // input clk enable for pipeline stalling of this stage
|
| 52 |
+
output reg o_ce, // output clk enable for pipeline stalling of next stage
|
| 53 |
+
input wire i_stall, //informs this stage to stall
|
| 54 |
+
output reg o_stall, //informs pipeline to stall
|
| 55 |
+
input wire i_flush, //flush this stage
|
| 56 |
+
output reg o_flush //flush previous stages
|
| 57 |
+
);
|
| 58 |
+
|
| 59 |
+
assign o_rs2_addr = i_inst[24:20]; //o_rs1_addrando_rs2_addr are not registered
|
| 60 |
+
assign o_rs1_addr = i_inst[19:15]; //since rv32i_basereg module do the registering itself
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
wire[2:0] funct3_d = i_inst[14:12];
|
| 64 |
+
wire[6:0] opcode = i_inst[6:0];
|
| 65 |
+
|
| 66 |
+
reg[31:0] imm_d;
|
| 67 |
+
reg alu_add_d;
|
| 68 |
+
reg alu_sub_d;
|
| 69 |
+
reg alu_slt_d;
|
| 70 |
+
reg alu_sltu_d;
|
| 71 |
+
reg alu_xor_d;
|
| 72 |
+
reg alu_or_d;
|
| 73 |
+
reg alu_and_d;
|
| 74 |
+
reg alu_sll_d;
|
| 75 |
+
reg alu_srl_d;
|
| 76 |
+
reg alu_sra_d;
|
| 77 |
+
reg alu_eq_d;
|
| 78 |
+
reg alu_neq_d;
|
| 79 |
+
reg alu_ge_d;
|
| 80 |
+
reg alu_geu_d;
|
| 81 |
+
|
| 82 |
+
reg opcode_rtype_d;
|
| 83 |
+
reg opcode_itype_d;
|
| 84 |
+
reg opcode_load_d;
|
| 85 |
+
reg opcode_store_d;
|
| 86 |
+
reg opcode_branch_d;
|
| 87 |
+
reg opcode_jal_d;
|
| 88 |
+
reg opcode_jalr_d;
|
| 89 |
+
reg opcode_lui_d;
|
| 90 |
+
reg opcode_auipc_d;
|
| 91 |
+
reg opcode_system_d;
|
| 92 |
+
reg opcode_fence_d;
|
| 93 |
+
|
| 94 |
+
reg system_noncsr = 0;
|
| 95 |
+
reg valid_opcode = 0;
|
| 96 |
+
reg illegal_shift = 0;
|
| 97 |
+
wire stall_bit = o_stall || i_stall; //stall this stage when next stages are stalled
|
| 98 |
+
|
| 99 |
+
//register the outputs of this decoder module for shorter combinational timing paths
|
| 100 |
+
always @(posedge i_clk, negedge i_rst_n) begin
|
| 101 |
+
if(!i_rst_n) begin
|
| 102 |
+
o_ce <= 0;
|
| 103 |
+
end
|
| 104 |
+
else begin
|
| 105 |
+
if(i_ce && !stall_bit) begin //update registers only if this stage is enabled and pipeline is not stalled
|
| 106 |
+
o_pc <= i_pc;
|
| 107 |
+
o_rs1_addr_q <= o_rs1_addr;
|
| 108 |
+
o_rs2_addr_q <= o_rs2_addr;
|
| 109 |
+
o_rd_addr <= i_inst[11:7];
|
| 110 |
+
o_funct3 <= funct3_d;
|
| 111 |
+
o_imm <= imm_d;
|
| 112 |
+
|
| 113 |
+
/// ALU Operations ////
|
| 114 |
+
o_alu[`ADD] <= alu_add_d;
|
| 115 |
+
o_alu[`SUB] <= alu_sub_d;
|
| 116 |
+
o_alu[`SLT] <= alu_slt_d;
|
| 117 |
+
o_alu[`SLTU] <= alu_sltu_d;
|
| 118 |
+
o_alu[`XOR] <= alu_xor_d;
|
| 119 |
+
o_alu[`OR] <= alu_or_d;
|
| 120 |
+
o_alu[`AND] <= alu_and_d;
|
| 121 |
+
o_alu[`SLL] <= alu_sll_d;
|
| 122 |
+
o_alu[`SRL] <= alu_srl_d;
|
| 123 |
+
o_alu[`SRA] <= alu_sra_d;
|
| 124 |
+
o_alu[`EQ] <= alu_eq_d;
|
| 125 |
+
o_alu[`NEQ] <= alu_neq_d;
|
| 126 |
+
o_alu[`GE] <= alu_ge_d;
|
| 127 |
+
o_alu[`GEU] <= alu_geu_d;
|
| 128 |
+
|
| 129 |
+
o_opcode[`RTYPE] <= opcode_rtype_d;
|
| 130 |
+
o_opcode[`ITYPE] <= opcode_itype_d;
|
| 131 |
+
o_opcode[`LOAD] <= opcode_load_d;
|
| 132 |
+
o_opcode[`STORE] <= opcode_store_d;
|
| 133 |
+
o_opcode[`BRANCH] <= opcode_branch_d;
|
| 134 |
+
o_opcode[`JAL] <= opcode_jal_d;
|
| 135 |
+
o_opcode[`JALR] <= opcode_jalr_d;
|
| 136 |
+
o_opcode[`LUI] <= opcode_lui_d;
|
| 137 |
+
o_opcode[`AUIPC] <= opcode_auipc_d;
|
| 138 |
+
o_opcode[`SYSTEM] <= opcode_system_d;
|
| 139 |
+
o_opcode[`FENCE] <= opcode_fence_d;
|
| 140 |
+
|
| 141 |
+
/*********************** decode possible exceptions ***********************/
|
| 142 |
+
o_exception[`ILLEGAL] <= !valid_opcode || illegal_shift;
|
| 143 |
+
|
| 144 |
+
// Check if ECALL
|
| 145 |
+
o_exception[`ECALL] <= (system_noncsr && i_inst[21:20]==2'b00)? 1:0;
|
| 146 |
+
|
| 147 |
+
// Check if EBREAK
|
| 148 |
+
o_exception[`EBREAK] <= (system_noncsr && i_inst[21:20]==2'b01)? 1:0;
|
| 149 |
+
|
| 150 |
+
// Check if MRET
|
| 151 |
+
o_exception[`MRET] <= (system_noncsr && i_inst[21:20]==2'b10)? 1:0;
|
| 152 |
+
/***************************************************************************/
|
| 153 |
+
end
|
| 154 |
+
if(i_flush && !stall_bit) begin //flush this stage so clock-enable of next stage is disabled at next clock cycle
|
| 155 |
+
o_ce <= 0;
|
| 156 |
+
end
|
| 157 |
+
else if(!stall_bit) begin //clock-enable will change only when not stalled
|
| 158 |
+
o_ce <= i_ce;
|
| 159 |
+
end
|
| 160 |
+
else if(stall_bit && !i_stall) o_ce <= 0; //if this stage is stalled but next stage is not, disable
|
| 161 |
+
//clock enable of next stage at next clock cycle (pipeline bubble)
|
| 162 |
+
end
|
| 163 |
+
end
|
| 164 |
+
always @* begin
|
| 165 |
+
//// Opcode Type ////
|
| 166 |
+
opcode_rtype_d = opcode == `OPCODE_RTYPE;
|
| 167 |
+
opcode_itype_d = opcode == `OPCODE_ITYPE;
|
| 168 |
+
opcode_load_d = opcode == `OPCODE_LOAD;
|
| 169 |
+
opcode_store_d = opcode == `OPCODE_STORE;
|
| 170 |
+
opcode_branch_d = opcode == `OPCODE_BRANCH;
|
| 171 |
+
opcode_jal_d = opcode == `OPCODE_JAL;
|
| 172 |
+
opcode_jalr_d = opcode == `OPCODE_JALR;
|
| 173 |
+
opcode_lui_d = opcode == `OPCODE_LUI;
|
| 174 |
+
opcode_auipc_d = opcode == `OPCODE_AUIPC;
|
| 175 |
+
opcode_system_d = opcode == `OPCODE_SYSTEM;
|
| 176 |
+
opcode_fence_d = opcode == `OPCODE_FENCE;
|
| 177 |
+
|
| 178 |
+
/*********************** decode possible exceptions ***********************/
|
| 179 |
+
system_noncsr = opcode == `OPCODE_SYSTEM && funct3_d == 0 ; //system instruction but not CSR operation
|
| 180 |
+
|
| 181 |
+
// Check if instruction is illegal
|
| 182 |
+
valid_opcode = (opcode_rtype_d || opcode_itype_d || opcode_load_d || opcode_store_d || opcode_branch_d || opcode_jal_d || opcode_jalr_d || opcode_lui_d || opcode_auipc_d || opcode_system_d || opcode_fence_d);
|
| 183 |
+
illegal_shift = (opcode_itype_d && (alu_sll_d || alu_srl_d || alu_sra_d)) && i_inst[25];
|
| 184 |
+
end
|
| 185 |
+
|
| 186 |
+
//decode operation for ALU and the extended value of immediate
|
| 187 |
+
always @* begin
|
| 188 |
+
o_stall = i_stall; //stall previous stage when decoder needs wait time
|
| 189 |
+
o_flush = i_flush; //flush this stage along with the previous stages
|
| 190 |
+
imm_d = 0;
|
| 191 |
+
alu_add_d = 0;
|
| 192 |
+
alu_sub_d = 0;
|
| 193 |
+
alu_slt_d = 0;
|
| 194 |
+
alu_sltu_d = 0;
|
| 195 |
+
alu_xor_d = 0;
|
| 196 |
+
alu_or_d = 0;
|
| 197 |
+
alu_and_d = 0;
|
| 198 |
+
alu_sll_d = 0;
|
| 199 |
+
alu_srl_d = 0;
|
| 200 |
+
alu_sra_d = 0;
|
| 201 |
+
alu_eq_d = 0;
|
| 202 |
+
alu_neq_d = 0;
|
| 203 |
+
alu_ge_d = 0;
|
| 204 |
+
alu_geu_d = 0;
|
| 205 |
+
|
| 206 |
+
/********** Decode ALU Operation **************/
|
| 207 |
+
if(opcode == `OPCODE_RTYPE || opcode == `OPCODE_ITYPE) begin
|
| 208 |
+
if(opcode == `OPCODE_RTYPE) begin
|
| 209 |
+
alu_add_d = funct3_d == `FUNCT3_ADD ? !i_inst[30] : 0; //add and sub has same o_funct3 code
|
| 210 |
+
alu_sub_d = funct3_d == `FUNCT3_ADD ? i_inst[30] : 0; //differs on i_inst[30]
|
| 211 |
+
end
|
| 212 |
+
else alu_add_d = funct3_d == `FUNCT3_ADD;
|
| 213 |
+
alu_slt_d = funct3_d == `FUNCT3_SLT;
|
| 214 |
+
alu_sltu_d = funct3_d == `FUNCT3_SLTU;
|
| 215 |
+
alu_xor_d = funct3_d == `FUNCT3_XOR;
|
| 216 |
+
alu_or_d = funct3_d == `FUNCT3_OR;
|
| 217 |
+
alu_and_d = funct3_d == `FUNCT3_AND;
|
| 218 |
+
alu_sll_d = funct3_d == `FUNCT3_SLL;
|
| 219 |
+
alu_srl_d = funct3_d == `FUNCT3_SRA ? !i_inst[30]:0; //srl and sra has same o_funct3 code
|
| 220 |
+
alu_sra_d = funct3_d == `FUNCT3_SRA ? i_inst[30]:0 ; //differs on i_inst[30]
|
| 221 |
+
end
|
| 222 |
+
|
| 223 |
+
else if(opcode == `OPCODE_BRANCH) begin
|
| 224 |
+
alu_eq_d = funct3_d == `FUNCT3_EQ;
|
| 225 |
+
alu_neq_d = funct3_d == `FUNCT3_NEQ;
|
| 226 |
+
alu_slt_d = funct3_d == `FUNCT3_LT;
|
| 227 |
+
alu_ge_d = funct3_d == `FUNCT3_GE;
|
| 228 |
+
alu_sltu_d = funct3_d == `FUNCT3_LTU;
|
| 229 |
+
alu_geu_d= funct3_d == `FUNCT3_GEU;
|
| 230 |
+
end
|
| 231 |
+
|
| 232 |
+
else alu_add_d = 1'b1; //add operation for all remaining instructions
|
| 233 |
+
/*********************************************/
|
| 234 |
+
|
| 235 |
+
/************************** extend the immediate (o_imm) *********************/
|
| 236 |
+
case(opcode)
|
| 237 |
+
`OPCODE_ITYPE , `OPCODE_LOAD , `OPCODE_JALR: imm_d = {{20{i_inst[31]}},i_inst[31:20]};
|
| 238 |
+
`OPCODE_STORE: imm_d = {{20{i_inst[31]}},i_inst[31:25],i_inst[11:7]};
|
| 239 |
+
`OPCODE_BRANCH: imm_d = {{19{i_inst[31]}},i_inst[31],i_inst[7],i_inst[30:25],i_inst[11:8],1'b0};
|
| 240 |
+
`OPCODE_JAL: imm_d = {{11{i_inst[31]}},i_inst[31],i_inst[19:12],i_inst[20],i_inst[30:21],1'b0};
|
| 241 |
+
`OPCODE_LUI , `OPCODE_AUIPC: imm_d = {i_inst[31:12],12'h000};
|
| 242 |
+
`OPCODE_SYSTEM , `OPCODE_FENCE: imm_d = {20'b0,i_inst[31:20]};
|
| 243 |
+
default: imm_d = 0;
|
| 244 |
+
endcase
|
| 245 |
+
/**************************************************************************/
|
| 246 |
+
|
| 247 |
+
end
|
| 248 |
+
|
| 249 |
+
endmodule
|
AngeloJacobo_RISC-V/rtl/rv32i_fetch.v
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* The rv32i_fetch module is primarily for fetching instructions from the memory
|
| 2 |
+
and prepare them for the decode stage of the pipeline. The module is responsible
|
| 3 |
+
for managing the Program Counter (PC), fetching instructions, and controlling
|
| 4 |
+
the pipeline. Below are the key functions of the rv32i_fetch module:
|
| 5 |
+
- Program Counter (PC) management: The module maintains a Program Counter (PC)
|
| 6 |
+
that holds the address of the current instruction in memory. The PC is
|
| 7 |
+
initialized at the reset vector address (specified by the parameter PC_RESET),
|
| 8 |
+
and it is incremented or updated based on the instruction flow control,
|
| 9 |
+
such as branches, jumps, or traps.
|
| 10 |
+
- Instruction fetching: The module fetches the instruction from memory based on
|
| 11 |
+
the current PC value. It sends a request for a new instruction (o_stb_inst)
|
| 12 |
+
when the fetch stage is enabled (ce), and waits for an acknowledgment
|
| 13 |
+
(i_ack_inst) from the memory. The fetched instruction (i_inst) is then sent
|
| 14 |
+
to the pipeline (o_inst).
|
| 15 |
+
- Pipeline control: The rv32i_fetch module manages the pipeline by controlling
|
| 16 |
+
clock enable (o_ce) signals for the next stage. It can stall the fetch stage
|
| 17 |
+
(stall_fetch) when the next stages are stalled (i_stall), a requested
|
| 18 |
+
instruction has not yet been acknowledged, or when there is no request for a
|
| 19 |
+
new instruction. Moreover, it can create pipeline bubbles when the PC needs
|
| 20 |
+
to be changed, disabling clock enable signals for the next stages, ensuring
|
| 21 |
+
no instructions are executed during this period.
|
| 22 |
+
- PC control: The module updates the PC based on the control signals received
|
| 23 |
+
from other stages in the pipeline. It can update the PC with a new address
|
| 24 |
+
(i_writeback_next_pc) when handling traps, or with the address of a taken
|
| 25 |
+
branch or jump (i_alu_next_pc). The fetch stage can be stalled during this
|
| 26 |
+
process to prevent instructions from being executed in the pipeline.
|
| 27 |
+
- Handling stalls and flushes: The rv32i_fetch module can stall the fetch
|
| 28 |
+
stage based on different conditions and store the current PC and instruction
|
| 29 |
+
values. When the stall condition is resolved, it can return to the stored
|
| 30 |
+
values and continue fetching instructions. The module can also flush the
|
| 31 |
+
fetch stage when required (i_flush), disabling the clock enable signal
|
| 32 |
+
for the next stage, effectively clearing any pending instructions.
|
| 33 |
+
*/
|
| 34 |
+
`timescale 1ns / 1ps
|
| 35 |
+
`default_nettype none
|
| 36 |
+
`include "rv32i_header.vh"
|
| 37 |
+
|
| 38 |
+
module rv32i_fetch #(parameter PC_RESET = 32'h00_00_00_00) (
|
| 39 |
+
input wire i_clk,i_rst_n,
|
| 40 |
+
output reg[31:0] o_iaddr, //instruction memory address
|
| 41 |
+
output reg[31:0] o_pc, //PC value of current instruction
|
| 42 |
+
input wire[31:0] i_inst, // retrieved instruction from Memory
|
| 43 |
+
output reg[31:0] o_inst, // instruction sent to pipeline
|
| 44 |
+
output wire o_stb_inst, // request for instruction
|
| 45 |
+
input wire i_ack_inst, //ack (high if new instruction is now on the bus)
|
| 46 |
+
// PC Control
|
| 47 |
+
input wire i_writeback_change_pc, //high when PC needs to change when going to trap or returning from trap
|
| 48 |
+
input wire[31:0] i_writeback_next_pc, //next PC due to trap
|
| 49 |
+
input wire i_alu_change_pc, //high when PC needs to change for taken branches and jumps
|
| 50 |
+
input wire[31:0] i_alu_next_pc, //next PC due to branch or jump
|
| 51 |
+
/// Pipeline Control ///
|
| 52 |
+
output reg o_ce, // output clk enable for pipeline stalling of next stage
|
| 53 |
+
input wire i_stall, //stall logic for whole pipeline
|
| 54 |
+
input wire i_flush //flush this stage
|
| 55 |
+
);
|
| 56 |
+
|
| 57 |
+
reg[31:0] iaddr_d, prev_pc, stalled_inst, stalled_pc;
|
| 58 |
+
reg ce, ce_d;
|
| 59 |
+
reg stall_fetch;
|
| 60 |
+
reg stall_q;
|
| 61 |
+
//stall this stage when:
|
| 62 |
+
//- next stages are stalled
|
| 63 |
+
//- you have request but no ack yeti
|
| 64 |
+
//- you dont have a request at all (no request then no instruction to execute for this stage)
|
| 65 |
+
wire stall_bit = stall_fetch || i_stall || (o_stb_inst && !i_ack_inst) || !o_stb_inst;
|
| 66 |
+
assign o_stb_inst = ce; //request for new instruction if this stage is enabled
|
| 67 |
+
|
| 68 |
+
//ce logic for fetch stage
|
| 69 |
+
always @(posedge i_clk, negedge i_rst_n) begin
|
| 70 |
+
if(!i_rst_n) ce <= 0;
|
| 71 |
+
else if((i_alu_change_pc || i_writeback_change_pc) && !(i_stall || stall_fetch)) ce <= 0; //do pipeline bubble when need to change pc so that next stages will be disabled
|
| 72 |
+
else ce <= 1; //and will not execute the instructions already inside the pipeline
|
| 73 |
+
end
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
always @(posedge i_clk, negedge i_rst_n) begin
|
| 78 |
+
if(!i_rst_n) begin
|
| 79 |
+
o_ce <= 0;
|
| 80 |
+
o_iaddr <= PC_RESET;
|
| 81 |
+
prev_pc <= PC_RESET;
|
| 82 |
+
stalled_inst <= 0;
|
| 83 |
+
o_pc <= 0;
|
| 84 |
+
end
|
| 85 |
+
else begin
|
| 86 |
+
if((ce && !stall_bit) || (stall_bit && !o_ce && ce) || i_writeback_change_pc) begin //update registers only if this stage is enabled and next stages are not stalled
|
| 87 |
+
o_iaddr <= iaddr_d;
|
| 88 |
+
o_pc <= stall_q? stalled_pc:prev_pc;
|
| 89 |
+
o_inst <= stall_q? stalled_inst:i_inst;
|
| 90 |
+
end
|
| 91 |
+
if(i_flush && !stall_bit) begin //flush this stage(only when not stalled) so that clock-enable of next stage is disabled at next clock cycle
|
| 92 |
+
o_ce <= 0;
|
| 93 |
+
end
|
| 94 |
+
else if(!stall_bit) begin //clock-enable will change only when not stalled
|
| 95 |
+
o_ce <= ce_d;
|
| 96 |
+
end
|
| 97 |
+
//if this stage is stalled but next stage is not, disable
|
| 98 |
+
//clock enable of next stage at next clock cycle (pipeline bubble)
|
| 99 |
+
else if(stall_bit && !i_stall) o_ce <= 0;
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
stall_q <= i_stall || stall_fetch; //raise stall when any of 5 stages is stalled
|
| 103 |
+
|
| 104 |
+
//store both instruction and PC before stalling so that we can
|
| 105 |
+
//come back to these values when we need to return from stall
|
| 106 |
+
if(stall_bit && !stall_q) begin
|
| 107 |
+
stalled_pc <= prev_pc;
|
| 108 |
+
stalled_inst <= i_inst;
|
| 109 |
+
end
|
| 110 |
+
prev_pc <= o_iaddr; //this is the first delay to align the PC to the pipeline
|
| 111 |
+
end
|
| 112 |
+
end
|
| 113 |
+
// logic for PC and pipeline clock_enable control
|
| 114 |
+
always @* begin
|
| 115 |
+
iaddr_d = 0;
|
| 116 |
+
ce_d = 0;
|
| 117 |
+
stall_fetch = i_stall; //stall when retrieving instructions need wait time
|
| 118 |
+
//prepare next PC when changing pc, then do a pipeline bubble
|
| 119 |
+
//to disable the ce of next stage
|
| 120 |
+
if(i_writeback_change_pc) begin
|
| 121 |
+
iaddr_d = i_writeback_next_pc;
|
| 122 |
+
ce_d = 0;
|
| 123 |
+
end
|
| 124 |
+
else if(i_alu_change_pc) begin
|
| 125 |
+
iaddr_d = i_alu_next_pc;
|
| 126 |
+
ce_d = 0;
|
| 127 |
+
end
|
| 128 |
+
else begin
|
| 129 |
+
iaddr_d = o_iaddr + 32'd4;
|
| 130 |
+
ce_d = ce;
|
| 131 |
+
end
|
| 132 |
+
end
|
| 133 |
+
|
| 134 |
+
endmodule
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
AngeloJacobo_RISC-V/rtl/rv32i_forwarding.v
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*The rv32i_forwarding module is responsible for handling data hazards in the
|
| 2 |
+
pipelined processor by implementing operand forwarding. Data hazards occur
|
| 3 |
+
when a register value is about to be overwritten by previous instructions that
|
| 4 |
+
are still in the pipeline and have not yet been written to the base register.
|
| 5 |
+
Operand forwarding resolves this issue by either stalling the pipeline until
|
| 6 |
+
the base register is updated (less efficient) or forwarding the updated operand
|
| 7 |
+
value directly from the pipeline stage where it is currently being computed. Key
|
| 8 |
+
functionalities of the rv32i_forwarding module include:
|
| 9 |
+
- Forwarding rs1 and rs2 operands: The module initially sets the output values
|
| 10 |
+
o_rs1 and o_rs2 to their original values from the base register (i_rs1_orig
|
| 11 |
+
and i_rs2_orig). It then checks for any data hazards by comparing the register
|
| 12 |
+
addresses of the operands (i_decoder_rs1_addr_q and i_decoder_rs2_addr_q) with
|
| 13 |
+
the destination register addresses in the pipeline stages (i_alu_rd_addr and
|
| 14 |
+
i_memoryaccess_rd_addr).
|
| 15 |
+
- Operand forwarding for rs1:If the next value of rs1 is in stage 4 (Memory Access),
|
| 16 |
+
and the Memory Access stage is enabled and if the next value of rs1 comes from a
|
| 17 |
+
load or CSR instruction (i.e., rd is not valid at stage 4), the module stalls the
|
| 18 |
+
ALU stage by asserting o_alu_force_stall. Otherwise, the module forwards the value
|
| 19 |
+
of rd from stage 4 (i_alu_rd) to o_rs1. If the next value of rs1 is in stage 5
|
| 20 |
+
(Writeback), and the Writeback stage is enabled, the module forwards the value of
|
| 21 |
+
rd from stage 5 (i_writeback_rd) to o_rs1.
|
| 22 |
+
- Operand forwarding for rs2: If the next value of rs2 is in stage 4 (Memory Access),
|
| 23 |
+
and the Memory Access stage is enabled and if the next value of rs2 comes from a
|
| 24 |
+
load or CSR instruction (i.e., rd is not yet valid at stage 4), the module stalls
|
| 25 |
+
the ALU stage by asserting o_alu_force_stall. Otherwise, the module forwards the
|
| 26 |
+
value of rd from stage 4 (i_alu_rd) to o_rs2. If the next value of rs2 is in stage
|
| 27 |
+
5 (Writeback), and the Writeback stage is enabled, the module forwards the value
|
| 28 |
+
of rd from stage 5 (i_writeback_rd) to o_rs2.
|
| 29 |
+
- Handling zero register (x0) forwarding: The module ensures that no operation
|
| 30 |
+
forwarding is performed when the register address is zero, as this register is
|
| 31 |
+
hardwired to zero. If either i_decoder_rs1_addr_q or i_decoder_rs2_addr_q is zero,
|
| 32 |
+
the corresponding output register (o_rs1 or o_rs2) is set to zero. By implementing
|
| 33 |
+
operand forwarding, the rv32i_forwarding module helps to mitigate data hazards,
|
| 34 |
+
ensuring the correct execution of instructions and improving the overall efficiency
|
| 35 |
+
of the RV32I pipelined processor.
|
| 36 |
+
*/
|
| 37 |
+
|
| 38 |
+
`timescale 1ns / 1ps
|
| 39 |
+
`default_nettype none
|
| 40 |
+
`include "rv32i_header.vh"
|
| 41 |
+
|
| 42 |
+
module rv32i_forwarding (
|
| 43 |
+
input wire[31:0] i_rs1_orig, //current rs1 value saved in basereg
|
| 44 |
+
input wire[31:0] i_rs2_orig, //current rs2 value saved in basereg
|
| 45 |
+
input wire[4:0] i_decoder_rs1_addr_q, //address of operand rs1 used in ALU stage
|
| 46 |
+
input wire[4:0] i_decoder_rs2_addr_q, //address of operand rs2 used in ALU stage
|
| 47 |
+
output reg o_alu_force_stall, //high to force ALU stage to stall
|
| 48 |
+
output reg[31:0] o_rs1, //rs1 value with Operand Forwarding
|
| 49 |
+
output reg[31:0] o_rs2, //rs2 value with Operand Forwarding
|
| 50 |
+
// Stage 4 [MEMORYACCESS]
|
| 51 |
+
input wire[4:0] i_alu_rd_addr, //destination register address
|
| 52 |
+
input wire i_alu_wr_rd, //high if rd_addr will be written
|
| 53 |
+
input wire i_alu_rd_valid, //high if rd is already valid at this stage (not LOAD nor CSR instruction)
|
| 54 |
+
input wire[31:0] i_alu_rd, //rd value in stage 4
|
| 55 |
+
input wire i_memoryaccess_ce, //high if stage 4 is enabled
|
| 56 |
+
// Stage 5 [WRITEBACK]
|
| 57 |
+
input wire[4:0] i_memoryaccess_rd_addr, //destination register address
|
| 58 |
+
input wire i_memoryaccess_wr_rd, //high if rd_addr will be written
|
| 59 |
+
input wire[31:0] i_writeback_rd, //rd value in stage 5
|
| 60 |
+
input wire i_writeback_ce //high if stage 4 is enabled
|
| 61 |
+
);
|
| 62 |
+
|
| 63 |
+
always @* begin
|
| 64 |
+
o_rs1 = i_rs1_orig; //original value from basereg
|
| 65 |
+
o_rs2 = i_rs2_orig; //original value from basereg
|
| 66 |
+
o_alu_force_stall = 0;
|
| 67 |
+
|
| 68 |
+
// Data Hazard = Register value is about to be overwritten by previous instructions but are still on the pipeline and are not yet written to basereg.
|
| 69 |
+
// The solution to make sure the updated value of rs1 or rs2 is used is to either stall the pipeline until the basereg is updated (very inefficient) or use Operand Forwarding
|
| 70 |
+
|
| 71 |
+
// Operand Forwarding for rs1
|
| 72 |
+
if((i_decoder_rs1_addr_q == i_alu_rd_addr) && i_alu_wr_rd && i_memoryaccess_ce) begin //next value of rs1 is currently on stage 4
|
| 73 |
+
if(!i_alu_rd_valid) begin //if next value of rs1 comes from load or CSR instruction then we must stall from ALU stage and wait until
|
| 74 |
+
o_alu_force_stall = 1; //stage 4(Memoryaccess) becomes disabled, which means next value of rs1 is already at stage 5
|
| 75 |
+
end
|
| 76 |
+
o_rs1 = i_alu_rd;
|
| 77 |
+
end
|
| 78 |
+
else if((i_decoder_rs1_addr_q == i_memoryaccess_rd_addr) && i_memoryaccess_wr_rd && i_writeback_ce) begin //next value of rs1 is currently on stage 5
|
| 79 |
+
o_rs1 = i_writeback_rd;
|
| 80 |
+
end
|
| 81 |
+
|
| 82 |
+
// Operand Forwarding for rs2
|
| 83 |
+
if((i_decoder_rs2_addr_q == i_alu_rd_addr) && i_alu_wr_rd && i_memoryaccess_ce) begin //next value of rs2 is currently on stage 4
|
| 84 |
+
if(!i_alu_rd_valid) begin //if next value of rs2 comes from load or CSR instruction(rd is only available at stage 5) then we must stall from ALU stage and wait until
|
| 85 |
+
o_alu_force_stall = 1; //stage 4(Memoryaccess) becomes disabled (which implicitly means that next value of rs2 is already at stage 5)
|
| 86 |
+
end
|
| 87 |
+
o_rs2 = i_alu_rd;
|
| 88 |
+
end
|
| 89 |
+
else if((i_decoder_rs2_addr_q == i_memoryaccess_rd_addr) && i_memoryaccess_wr_rd && i_writeback_ce) begin //next value of rs2 is currently on stage 5
|
| 90 |
+
o_rs2 = i_writeback_rd;
|
| 91 |
+
end
|
| 92 |
+
|
| 93 |
+
// No operation forwarding necessary when addr is zero since that address is hardwired to zero
|
| 94 |
+
if(i_decoder_rs1_addr_q == 0) o_rs1 = 0;
|
| 95 |
+
if(i_decoder_rs2_addr_q == 0) o_rs2 = 0;
|
| 96 |
+
end
|
| 97 |
+
|
| 98 |
+
endmodule
|
AngeloJacobo_RISC-V/rtl/rv32i_header.vh
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`define ALU_WIDTH 14
|
| 2 |
+
`define ADD 0
|
| 3 |
+
`define SUB 1
|
| 4 |
+
`define SLT 2
|
| 5 |
+
`define SLTU 3
|
| 6 |
+
`define XOR 4
|
| 7 |
+
`define OR 5
|
| 8 |
+
`define AND 6
|
| 9 |
+
`define SLL 7
|
| 10 |
+
`define SRL 8
|
| 11 |
+
`define SRA 9
|
| 12 |
+
`define EQ 10
|
| 13 |
+
`define NEQ 11
|
| 14 |
+
`define GE 12
|
| 15 |
+
`define GEU 13
|
| 16 |
+
|
| 17 |
+
`define OPCODE_WIDTH 11
|
| 18 |
+
`define RTYPE 0
|
| 19 |
+
`define ITYPE 1
|
| 20 |
+
`define LOAD 2
|
| 21 |
+
`define STORE 3
|
| 22 |
+
`define BRANCH 4
|
| 23 |
+
`define JAL 5
|
| 24 |
+
`define JALR 6
|
| 25 |
+
`define LUI 7
|
| 26 |
+
`define AUIPC 8
|
| 27 |
+
`define SYSTEM 9
|
| 28 |
+
`define FENCE 10
|
| 29 |
+
|
| 30 |
+
`define EXCEPTION_WIDTH 4
|
| 31 |
+
`define ILLEGAL 0
|
| 32 |
+
`define ECALL 1
|
| 33 |
+
`define EBREAK 2
|
| 34 |
+
`define MRET 3
|
| 35 |
+
|
| 36 |
+
`define OPCODE_RTYPE 7'b0110011
|
| 37 |
+
`define OPCODE_ITYPE 7'b0010011
|
| 38 |
+
`define OPCODE_LOAD 7'b0000011
|
| 39 |
+
`define OPCODE_STORE 7'b0100011
|
| 40 |
+
`define OPCODE_BRANCH 7'b1100011
|
| 41 |
+
`define OPCODE_JAL 7'b1101111
|
| 42 |
+
`define OPCODE_JALR 7'b1100111
|
| 43 |
+
`define OPCODE_LUI 7'b0110111
|
| 44 |
+
`define OPCODE_AUIPC 7'b0010111
|
| 45 |
+
`define OPCODE_SYSTEM 7'b1110011
|
| 46 |
+
`define OPCODE_FENCE 7'b0001111
|
| 47 |
+
|
| 48 |
+
`define FUNCT3_ADD 3'b000
|
| 49 |
+
`define FUNCT3_SLT 3'b010
|
| 50 |
+
`define FUNCT3_SLTU 3'b011
|
| 51 |
+
`define FUNCT3_XOR 3'b100
|
| 52 |
+
`define FUNCT3_OR 3'b110
|
| 53 |
+
`define FUNCT3_AND 3'b111
|
| 54 |
+
`define FUNCT3_SLL 3'b001
|
| 55 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* The rv32i_memoryaccess module serves as the memory access stage of the
|
| 2 |
+
pipelined processor. This module primarily handles data memory access for
|
| 3 |
+
load and store instructions, as well as passing the necessary information
|
| 4 |
+
to subsequent pipeline stas. The module is responsible for generating
|
| 5 |
+
appropriate data memory addresses, data to be stored, and write masks for
|
| 6 |
+
different load/store operations, as well as handling pipeline stalls and
|
| 7 |
+
flushes when required. Key functionalities of the rv32i_memoryaccess module
|
| 8 |
+
include:
|
| 9 |
+
- Address and data handling for load/store operations: The module uses the
|
| 10 |
+
incoming address (i_y) to generate the appropriate data memory address (o_wb_addr_data)
|
| 11 |
+
and stores it in the o_data_store register. It also selects the correct byte,
|
| 12 |
+
halfword, or word data from the data memory input (i_wb_data_data) based on the
|
| 13 |
+
instruction's funct3 field and stores it in the o_data_load register. The
|
| 14 |
+
write mask (o_wb_sel_data) is generated based on the address and the size of the
|
| 15 |
+
operation (byte, halfword, or word). The mask is used to control which part
|
| 16 |
+
of the data memory will be written during store operations.
|
| 17 |
+
- Register writeback control: The module i_wb_stall_datadetermines whether a destination register
|
| 18 |
+
should be written (o_wr_rd) based on the input i_wr_rd signal. It passes the
|
| 19 |
+
destination register address (o_rd_addr) and the data to be written (o_rd) to the
|
| 20 |
+
next stage.
|
| 21 |
+
- Data memory control: The module controls the data memory read/write requests by
|
| 22 |
+
generating the o_stb_data signal, which indicates a request for data memory access.
|
| 23 |
+
It also generates the o_wb_we_data signal, which indicates whether a write operation
|
| 24 |
+
should be performed on the data memory.
|
| 25 |
+
- Pipeline control: The module can stall the pipeline by asserting the o_stall signal
|
| 26 |
+
if the data memory access is not yet acknowledged (i_wb_ack_data) or if there is a stall
|
| 27 |
+
request from the ALU stage (i_stall_from_alu). It can also flush the current stage and
|
| 28 |
+
previous stages using the o_flush signal based on the input i_flush signal. The module
|
| 29 |
+
controls the clock enable signals (o_ce) for the next stage based on the stall and flush
|
| 30 |
+
conditions.
|
| 31 |
+
*/
|
| 32 |
+
|
| 33 |
+
`timescale 1ns / 1ps
|
| 34 |
+
`default_nettype none
|
| 35 |
+
`include "rv32i_header.vh"
|
| 36 |
+
|
| 37 |
+
module rv32i_memoryaccess(
|
| 38 |
+
input wire i_clk, i_rst_n,
|
| 39 |
+
input wire[31:0] i_rs2, //data to be stored to memory is always i_rs2
|
| 40 |
+
input wire[31:0] i_y, //y value from ALU (address of data to memory be stored or loaded)
|
| 41 |
+
input wire[2:0] i_funct3, //funct3 from previous stage
|
| 42 |
+
output reg[2:0] o_funct3, //funct3 (byte,halfword,word)
|
| 43 |
+
input wire[`OPCODE_WIDTH-1:0] i_opcode, //determines if data_store will be to stored to data memory
|
| 44 |
+
output reg[`OPCODE_WIDTH-1:0] o_opcode,//opcode type
|
| 45 |
+
input wire[31:0] i_pc, //PC from previous stage
|
| 46 |
+
output reg[31:0] o_pc, //PC value
|
| 47 |
+
// Basereg Control
|
| 48 |
+
input wire i_wr_rd, //write rd to base reg is enabled (from memoryaccess stage)
|
| 49 |
+
output reg o_wr_rd, //write rd to the base reg if enabled
|
| 50 |
+
input wire[4:0] i_rd_addr, //address for destination register (from previous stage)
|
| 51 |
+
output reg[4:0] o_rd_addr, //address for destination register
|
| 52 |
+
input wire[31:0] i_rd, //value to be written back to destination reg
|
| 53 |
+
output reg[31:0] o_rd, //value to be written back to destination register
|
| 54 |
+
// Data Memory Control
|
| 55 |
+
output reg o_wb_cyc_data, //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 56 |
+
output reg o_wb_stb_data, //request for read/write access to data memory
|
| 57 |
+
output reg o_wb_we_data, //write-enable (1 = write, 0 = read)
|
| 58 |
+
output reg [31:0] o_wb_addr_data, //data memory address
|
| 59 |
+
output reg[31:0] o_wb_data_data, //data to be stored to memory
|
| 60 |
+
output reg[3:0] o_wb_sel_data, //byte strobe for write (1 = write the byte) {byte3,byte2,byte1,byte0}
|
| 61 |
+
input wire i_wb_ack_data, //ack by data memory (high when data to be read is ready or when write data is already written)
|
| 62 |
+
input wire i_wb_stall_data, //stall by data memory (1 = data memory is busy)
|
| 63 |
+
input wire[31:0] i_wb_data_data, //data retrieve from data memory
|
| 64 |
+
output reg[31:0] o_data_load, //data to be loaded to base reg (z-or-s extended)
|
| 65 |
+
/// Pipeline Control ///
|
| 66 |
+
input wire i_stall_from_alu, //stalls this stage when incoming instruction is a load/store
|
| 67 |
+
input wire i_ce, // input clk enable for pipeline stalling of this stage
|
| 68 |
+
output reg o_ce, // output clk enable for pipeline stalling of next stage
|
| 69 |
+
input wire i_stall, //informs this stage to stall
|
| 70 |
+
output reg o_stall, //informs pipeline to stall
|
| 71 |
+
input wire i_flush, //flush this stage
|
| 72 |
+
output reg o_flush //flush previous stages
|
| 73 |
+
);
|
| 74 |
+
|
| 75 |
+
reg[31:0] data_store_d; //data to be stored to memory
|
| 76 |
+
reg[31:0] data_load_d; //data to be loaded to basereg
|
| 77 |
+
reg[3:0] wr_mask_d;
|
| 78 |
+
reg pending_request; //high if there is still a pending request (request which have not yet acknowledged)
|
| 79 |
+
wire[1:0] addr_2 = i_y[1:0]; //last 2 bits of data memory address
|
| 80 |
+
wire stall_bit = i_stall || o_stall;
|
| 81 |
+
|
| 82 |
+
//register the outputs of this module
|
| 83 |
+
always @(posedge i_clk, negedge i_rst_n) begin
|
| 84 |
+
if(!i_rst_n) begin
|
| 85 |
+
o_wr_rd <= 0;
|
| 86 |
+
o_wb_we_data <= 0;
|
| 87 |
+
o_ce <= 0;
|
| 88 |
+
o_wb_stb_data <= 0;
|
| 89 |
+
pending_request <= 0;
|
| 90 |
+
o_wb_cyc_data <= 0;
|
| 91 |
+
end
|
| 92 |
+
else begin
|
| 93 |
+
// wishbone cycle will only be high if this stage is enabled
|
| 94 |
+
o_wb_cyc_data <= i_ce;
|
| 95 |
+
//request completed after ack
|
| 96 |
+
if(i_wb_ack_data) begin
|
| 97 |
+
pending_request <= 0;
|
| 98 |
+
end
|
| 99 |
+
|
| 100 |
+
//update register only if this stage is enabled and not stalled (after load/store operation)
|
| 101 |
+
if(i_ce && !stall_bit) begin
|
| 102 |
+
o_rd_addr <= i_rd_addr;
|
| 103 |
+
o_funct3 <= i_funct3;
|
| 104 |
+
o_opcode <= i_opcode;
|
| 105 |
+
o_pc <= i_pc;
|
| 106 |
+
o_wr_rd <= i_wr_rd;
|
| 107 |
+
o_rd <= i_rd;
|
| 108 |
+
o_data_load <= data_load_d;
|
| 109 |
+
end
|
| 110 |
+
//update request to memory when no pending request yet
|
| 111 |
+
if(i_ce && !pending_request) begin
|
| 112 |
+
//stb goes high when instruction is a load/store and when
|
| 113 |
+
//request is not already high (request lasts for 1 clk cycle
|
| 114 |
+
//only)
|
| 115 |
+
o_wb_stb_data <= i_opcode[`LOAD] || i_opcode[`STORE];
|
| 116 |
+
o_wb_sel_data <= wr_mask_d;
|
| 117 |
+
o_wb_we_data <= i_opcode[`STORE];
|
| 118 |
+
pending_request <= i_opcode[`LOAD] || i_opcode[`STORE];
|
| 119 |
+
o_wb_addr_data <= i_y;
|
| 120 |
+
o_wb_data_data <= data_store_d;
|
| 121 |
+
end
|
| 122 |
+
|
| 123 |
+
// if there is pending request but no stall from memory: idle the stb line
|
| 124 |
+
if(pending_request && !i_wb_stall_data) begin
|
| 125 |
+
o_wb_stb_data <= 0;
|
| 126 |
+
end
|
| 127 |
+
|
| 128 |
+
if(!i_ce) begin
|
| 129 |
+
o_wb_stb_data <= 0;
|
| 130 |
+
end
|
| 131 |
+
|
| 132 |
+
//flush this stage so clock-enable of next stage is disabled at next clock cycle
|
| 133 |
+
if(i_flush && !stall_bit) begin
|
| 134 |
+
o_ce <= 0;
|
| 135 |
+
end
|
| 136 |
+
else if(!stall_bit) begin //clock-enable will change only when not stalled
|
| 137 |
+
o_ce <= i_ce;
|
| 138 |
+
end
|
| 139 |
+
|
| 140 |
+
//if this stage is stalled but next stage is not, disable
|
| 141 |
+
//clock enable of next stage at next clock cycle (pipeline bubble)
|
| 142 |
+
else if(stall_bit && !i_stall) o_ce <= 0;
|
| 143 |
+
end
|
| 144 |
+
|
| 145 |
+
end
|
| 146 |
+
|
| 147 |
+
//determine data to be loaded to basereg or stored to data memory
|
| 148 |
+
always @* begin
|
| 149 |
+
//stall while data memory has not yet acknowledged i.e.write data is not yet written or
|
| 150 |
+
//read data is not yet available (no ack yet). Don't stall when need to flush by next stage
|
| 151 |
+
o_stall = ((i_stall_from_alu && i_ce && !i_wb_ack_data) || i_stall) && !i_flush;
|
| 152 |
+
o_flush = i_flush; //flush this stage along with previous stages
|
| 153 |
+
data_store_d = 0;
|
| 154 |
+
data_load_d = 0;
|
| 155 |
+
wr_mask_d = 0;
|
| 156 |
+
|
| 157 |
+
case(i_funct3[1:0])
|
| 158 |
+
2'b00: begin //byte load/store
|
| 159 |
+
case(addr_2) //choose which of the 4 byte will be loaded to basereg
|
| 160 |
+
2'b00: data_load_d = {24'b0, i_wb_data_data[7:0]};
|
| 161 |
+
2'b01: data_load_d = {24'b0, i_wb_data_data[15:8]};
|
| 162 |
+
2'b10: data_load_d = {24'b0, i_wb_data_data[23:16]};
|
| 163 |
+
2'b11: data_load_d = {24'b0, i_wb_data_data[31:24]};
|
| 164 |
+
endcase
|
| 165 |
+
data_load_d = {{{24{!i_funct3[2]}} & {24{data_load_d[7]}}} , data_load_d[7:0]}; //signed and unsigned extension in 1 equation
|
| 166 |
+
wr_mask_d = 4'b0001<<addr_2; //mask 1 of the 4 bytes
|
| 167 |
+
data_store_d = i_rs2<<{addr_2,3'b000}; //i_rs2<<(addr_2*8) , align data to mask
|
| 168 |
+
end
|
| 169 |
+
2'b01: begin //halfword load/store
|
| 170 |
+
data_load_d = addr_2[1]? {16'b0,i_wb_data_data[31:16]}: {16'b0,i_wb_data_data[15:0]}; //choose which of the 2 halfwords will be loaded to basereg
|
| 171 |
+
data_load_d = {{{16{!i_funct3[2]}} & {16{data_load_d[15]}}},data_load_d[15:0]}; //signed and unsigned extension in 1 equation
|
| 172 |
+
wr_mask_d = 4'b0011<<{addr_2[1],1'b0}; //mask either the upper or lower half-word
|
| 173 |
+
data_store_d = i_rs2<<{addr_2[1],4'b0000}; //i_rs2<<(addr_2[1]*16) , align data to mask
|
| 174 |
+
end
|
| 175 |
+
2'b10: begin //word load/store
|
| 176 |
+
data_load_d = i_wb_data_data;
|
| 177 |
+
wr_mask_d = 4'b1111; //mask all
|
| 178 |
+
data_store_d = i_rs2;
|
| 179 |
+
end
|
| 180 |
+
default: begin
|
| 181 |
+
data_store_d = 0;
|
| 182 |
+
data_load_d = 0;
|
| 183 |
+
wr_mask_d = 0;
|
| 184 |
+
end
|
| 185 |
+
endcase
|
| 186 |
+
end
|
| 187 |
+
|
| 188 |
+
`ifdef FORMAL
|
| 189 |
+
always @* begin
|
| 190 |
+
if(o_wb_stb_data) begin
|
| 191 |
+
assert(pending_request);
|
| 192 |
+
end
|
| 193 |
+
end
|
| 194 |
+
|
| 195 |
+
`endif
|
| 196 |
+
|
| 197 |
+
endmodule
|
| 198 |
+
|
AngeloJacobo_RISC-V/rtl/rv32i_writeback.v
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* The rv32i_writeback module serves as the writeback stage of the pipelined
|
| 2 |
+
processor. This stage is responsible for determining the next value of the
|
| 3 |
+
program counter (PC), writing data back to the destination register, and
|
| 4 |
+
handling trap-related operations (interrupts and exceptions). In addition,
|
| 5 |
+
the module manages pipeline control, such as stalling and flushing previous
|
| 6 |
+
stages. Key functionalities of the rv32i_writeback module include:
|
| 7 |
+
- Determining the next value of the program counter (PC) and updating the
|
| 8 |
+
o_next_pc register: If an interrupt or exception is detected, the module
|
| 9 |
+
sets the PC to the trap address (i_trap_address) and asserts the o_change_pc
|
| 10 |
+
signal. If the processor is returning from a trap, the module sets the PC to
|
| 11 |
+
the return address (i_return_address) and asserts the o_change_pc signal. In
|
| 12 |
+
normal operation, the PC value from the previous stage (i_pc) is passed through.
|
| 13 |
+
- Handling writeback to destination registers: The module writes data back to the
|
| 14 |
+
destination register based on the opcode and funct3 fields of the instruction:
|
| 15 |
+
If the instruction is a load operation, the data from the memory (i_data_load)
|
| 16 |
+
is written back. If the instruction is a CSR write operation, the CSR value
|
| 17 |
+
(i_csr_out) is written back. In other cases, the data is computed at the ALU
|
| 18 |
+
stage (i_rd) and is written back. The o_wr_rd signal is set based on the i_wr_rd
|
| 19 |
+
input and the current pipeline control state (i_ce and o_stall). The destination
|
| 20 |
+
register address (o_rd_addr) is passed through from the i_rd_addr input.
|
| 21 |
+
- Trap-handler control: The module handles interrupts and exceptions by checking the
|
| 22 |
+
i_go_to_trap and i_return_from_trap input signals. When the processor goes to a
|
| 23 |
+
trap, the o_next_pc register is set to the trap address (i_trap_address) and the
|
| 24 |
+
pipeline is flushed. When the processor returns from a trap, the o_next_pc register
|
| 25 |
+
is set to the return address (i_return_address) and the pipeline is flushed.
|
| 26 |
+
- Pipeline control: The module can stall the pipeline by asserting the o_stall signal
|
| 27 |
+
when necessary. It can also flush the current stage and previous stages by asserting
|
| 28 |
+
the o_flush signal based on the state of the pipeline and trap-related operations.
|
| 29 |
+
*/
|
| 30 |
+
|
| 31 |
+
//logic controller for the next PC and rd value [WRITEBACK STAGE]
|
| 32 |
+
|
| 33 |
+
`timescale 1ns / 1ps
|
| 34 |
+
`default_nettype none
|
| 35 |
+
`include "rv32i_header.vh"
|
| 36 |
+
|
| 37 |
+
module rv32i_writeback (
|
| 38 |
+
input wire[2:0] i_funct3, //function type
|
| 39 |
+
input wire[31:0] i_data_load, //data to be loaded to base reg
|
| 40 |
+
input wire[31:0] i_csr_out, //CSR value to be loaded to basereg
|
| 41 |
+
input wire i_opcode_load,
|
| 42 |
+
input wire i_opcode_system,
|
| 43 |
+
// Basereg Control
|
| 44 |
+
input wire i_wr_rd, //write rd to basereg if enabled (from previous stage)
|
| 45 |
+
output reg o_wr_rd, //write rd to the base reg if enabled
|
| 46 |
+
input wire[4:0] i_rd_addr, //address for destination register (from previous stage)
|
| 47 |
+
output reg[4:0] o_rd_addr, //address for destination register
|
| 48 |
+
input wire[31:0] i_rd, //value to be written back to destination register (from previous stage)
|
| 49 |
+
output reg[31:0] o_rd, //value to be written back to destination register
|
| 50 |
+
// PC Control
|
| 51 |
+
input wire[31:0] i_pc, // pc value (from previous stage)
|
| 52 |
+
output reg[31:0] o_next_pc, //new pc value
|
| 53 |
+
output reg o_change_pc, //high if PC needs to jump
|
| 54 |
+
// Trap-Handler
|
| 55 |
+
input wire i_go_to_trap, //high before going to trap (if exception/interrupt detected)
|
| 56 |
+
input wire i_return_from_trap, //high before returning from trap (via mret)
|
| 57 |
+
input wire[31:0] i_return_address, //mepc CSR
|
| 58 |
+
input wire[31:0] i_trap_address, //mtvec CSR
|
| 59 |
+
/// Pipeline Control ///
|
| 60 |
+
input wire i_ce, // input clk enable for pipeline stalling of this stage
|
| 61 |
+
output reg o_stall, //informs pipeline to stall
|
| 62 |
+
output reg o_flush //flush previous stages
|
| 63 |
+
);
|
| 64 |
+
//
|
| 65 |
+
//determine next value of pc and o_rd
|
| 66 |
+
always @* begin
|
| 67 |
+
o_stall = 0; //stall when this stage needs wait time
|
| 68 |
+
o_flush = 0; //flush this stage along with previous stages when changing PC
|
| 69 |
+
o_wr_rd = i_wr_rd && i_ce && !o_stall;
|
| 70 |
+
o_rd_addr = i_rd_addr;
|
| 71 |
+
o_rd = 0;
|
| 72 |
+
o_next_pc = 0;
|
| 73 |
+
o_change_pc = 0;
|
| 74 |
+
|
| 75 |
+
if(i_go_to_trap) begin
|
| 76 |
+
o_change_pc = 1; //change PC only when ce of this stage is high (o_change_pc is valid)
|
| 77 |
+
o_next_pc = i_trap_address; //interrupt or exception detected so go to trap address (mtvec value)
|
| 78 |
+
o_flush = i_ce;
|
| 79 |
+
o_wr_rd = 0;
|
| 80 |
+
end
|
| 81 |
+
|
| 82 |
+
else if(i_return_from_trap) begin
|
| 83 |
+
o_change_pc = 1; //change PC only when ce of this stage is high (o_change_pc is valid)
|
| 84 |
+
o_next_pc = i_return_address; //return from trap via mret (mepc value)
|
| 85 |
+
o_flush = i_ce;
|
| 86 |
+
o_wr_rd = 0;
|
| 87 |
+
end
|
| 88 |
+
|
| 89 |
+
else begin //normal operation
|
| 90 |
+
if(i_opcode_load) o_rd = i_data_load; //load data from memory to basereg
|
| 91 |
+
else if(i_opcode_system && i_funct3!=0) begin //CSR write
|
| 92 |
+
o_rd = i_csr_out;
|
| 93 |
+
end
|
| 94 |
+
else o_rd = i_rd; //rd value is already computed at ALU stage
|
| 95 |
+
end
|
| 96 |
+
|
| 97 |
+
end
|
| 98 |
+
endmodule
|
AngeloJacobo_RISC-V/test/extra/demo1.c
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int main() {
|
| 5 |
+
LCD_Init(); // Initialize LCD module with I2C address = 0x4E
|
| 6 |
+
|
| 7 |
+
while(1){
|
| 8 |
+
LCD_Set_Cursor(1, 1); //set cursor to row 1 col 1
|
| 9 |
+
LCD_Write_String("Demonstration #1");
|
| 10 |
+
LCD_Set_Cursor(2, 1); //set cursor to row 2 col 1
|
| 11 |
+
LCD_Write_String("Hello World!!!");
|
| 12 |
+
}
|
| 13 |
+
return 0;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
AngeloJacobo_RISC-V/test/extra/test_gpio.c
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int main(){
|
| 5 |
+
int counter = 0;
|
| 6 |
+
gpio_write_pin(10, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 7 |
+
delay_ticks(100); //after 100 cpu clock ticks
|
| 8 |
+
gpio_write_pin(10, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 9 |
+
delay_ticks(100); //after 100 cpu clock ticks
|
| 10 |
+
gpio_write_pin(10, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 11 |
+
delay_ticks(100); //after 100 cpu clock ticks
|
| 12 |
+
/*while(1)*/{
|
| 13 |
+
toggle_gpio(5); //toggle a specific GPIO pin (automatically set pin to write mode)
|
| 14 |
+
delay_ticks(100); //after 100 cpu clock ticks
|
| 15 |
+
}
|
| 16 |
+
}
|
AngeloJacobo_RISC-V/test/extra/test_hygro.c
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <stdio.h>
|
| 3 |
+
#include <rv32i.h>
|
| 4 |
+
|
| 5 |
+
int main(){
|
| 6 |
+
uart_print("Start HygroPMOD....\n");
|
| 7 |
+
|
| 8 |
+
// Capture and print temperature and humidity data 2x per second
|
| 9 |
+
int val;
|
| 10 |
+
char msg[5]; //max of 5 chars
|
| 11 |
+
|
| 12 |
+
while(1){
|
| 13 |
+
hygroi2c_begin();
|
| 14 |
+
val = (int) hygroi2c_getTemperature();
|
| 15 |
+
itoa(val, msg, 10);
|
| 16 |
+
uart_print("\nTemperature: ");
|
| 17 |
+
uart_print(msg);
|
| 18 |
+
|
| 19 |
+
val = (int) hygroi2c_getHumidity();
|
| 20 |
+
itoa(val, msg, 10);
|
| 21 |
+
uart_print("\nHumidity: ");
|
| 22 |
+
uart_print(msg);
|
| 23 |
+
delay_ms(1000); // 1 sample per second (temp + humidity) maximum
|
| 24 |
+
}
|
| 25 |
+
}
|
AngeloJacobo_RISC-V/test/extra/test_i2c.c
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int main() {
|
| 5 |
+
i2c_write_address(0xaa);
|
| 6 |
+
i2c_write_byte('A');
|
| 7 |
+
i2c_write_byte('Z');
|
| 8 |
+
i2c_stop();
|
| 9 |
+
return 0;
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
/* I2C Address Finder
|
| 13 |
+
uart_print("\n\nSTART THE I2C ADDRES FINDER\n\n");
|
| 14 |
+
int address;
|
| 15 |
+
uint8_t ack;
|
| 16 |
+
for(address=1; address<128; address++){
|
| 17 |
+
ack = i2c_write_address(address<<1); //rightmost bit is 0(write)
|
| 18 |
+
i2c_stop(); //make sure to stop before accessing new address slave
|
| 19 |
+
if(ack){
|
| 20 |
+
uart_print("\nFound the address:");
|
| 21 |
+
//char str_address[20];
|
| 22 |
+
// sprintf(str_address,"%d",address);
|
| 23 |
+
//uart_print(str_address);
|
| 24 |
+
uart_print("\n\n\n\n");
|
| 25 |
+
}
|
| 26 |
+
else{
|
| 27 |
+
uart_print("\nWRONG:");
|
| 28 |
+
//char str_address[20];
|
| 29 |
+
//sprintf(str_address,"%d",address);
|
| 30 |
+
//uart_print(str_address);
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
return 0;
|
| 35 |
+
*/
|
| 36 |
+
}
|
| 37 |
+
|
AngeloJacobo_RISC-V/test/extra/test_lcd.c
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int main(void) {
|
| 5 |
+
|
| 6 |
+
uart_print("INITIALIZING LCD MODULE.....\n");
|
| 7 |
+
LCD_Init(0x4E); // Initialize LCD module with I2C address = 0x4E
|
| 8 |
+
uart_print("INITIALIZING DONE!\n\n");
|
| 9 |
+
|
| 10 |
+
LCD_Set_Cursor(1, 1);
|
| 11 |
+
LCD_Write_String(" Angelo Jacobo");
|
| 12 |
+
LCD_Set_Cursor(2, 1);
|
| 13 |
+
LCD_Write_String("BSECE-4A");
|
| 14 |
+
//while(1){
|
| 15 |
+
//}
|
| 16 |
+
|
| 17 |
+
return 1;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
AngeloJacobo_RISC-V/test/extra/test_timer.c
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int finish;
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
void __attribute__((interrupt)) trap_handler(void) {
|
| 8 |
+
mtime_set_timecmp(-1);
|
| 9 |
+
finish = 1;
|
| 10 |
+
}
|
| 11 |
+
int main() {
|
| 12 |
+
trap_handler_setup(trap_handler); //configure MTVEC to call "trap_handler" and initially disable all interrupts
|
| 13 |
+
csr_set(MSTATUS, 1<<MSTATUS_MIE); //set global interrupt enable
|
| 14 |
+
csr_set(MIE, 1<<MIE_MTIE); //set timer interrupt enable
|
| 15 |
+
csr_set(MIP, 1<<MIP_MTIP); //set timer interrupt pending enable
|
| 16 |
+
|
| 17 |
+
mtime_set_timecmp(mtime_get_time() + 1000); //set time compare to +1000 ticks of current time
|
| 18 |
+
finish = 0;
|
| 19 |
+
|
| 20 |
+
while(1){ //wait here until interrupt fires
|
| 21 |
+
if(finish) return 0;
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
AngeloJacobo_RISC-V/test/extra/test_uart.c
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int main() {
|
| 5 |
+
uart_print("Angelo Jacobo");
|
| 6 |
+
return 0;
|
| 7 |
+
}
|
| 8 |
+
|
AngeloJacobo_RISC-V/test/extra/ultrasonic_sensor.c
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
int main(){
|
| 5 |
+
int trig_pin = 0;
|
| 6 |
+
int echo_pin = 1;
|
| 7 |
+
int pulse_duration_us;
|
| 8 |
+
int distance_cm;
|
| 9 |
+
char string[16]; //max of 16 chars
|
| 10 |
+
|
| 11 |
+
gpio_set_mode_pin(trig_pin, 1); //set mode setting of a single GPIO pin(read = 0, write = 1)
|
| 12 |
+
gpio_set_mode_pin(echo_pin, 0); //set mode setting of a single GPIO pin(read = 0, write = 1)
|
| 13 |
+
uart_print("Start Ultrasonic Sensor\n\n");
|
| 14 |
+
|
| 15 |
+
while(1) {
|
| 16 |
+
// set trig_pin for 10us
|
| 17 |
+
gpio_write_pin(trig_pin, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 18 |
+
delay_us(2); // delay function based on microseconds
|
| 19 |
+
gpio_write_pin(trig_pin, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 20 |
+
delay_us(10); // delay function based on microseconds
|
| 21 |
+
gpio_write_pin(trig_pin, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 22 |
+
|
| 23 |
+
pulse_duration_us = gpio_pulse_duration_us(echo_pin, 1); //measure how long will be the high pulse
|
| 24 |
+
distance_cm = pulse_duration_us*(0.034/2);
|
| 25 |
+
|
| 26 |
+
//convert distance_cm to string
|
| 27 |
+
itoa(distance_cm, string, 10);
|
| 28 |
+
uart_print("Distance (cm): ");
|
| 29 |
+
uart_print(string);
|
| 30 |
+
uart_print("\n");
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
AngeloJacobo_RISC-V/test/freertos/FreeRTOSConfig.h
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
|
| 3 |
+
All rights reserved
|
| 4 |
+
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
|
| 5 |
+
This file is part of the FreeRTOS distribution.
|
| 6 |
+
FreeRTOS is free software; you can redistribute it and/or modify it under
|
| 7 |
+
the terms of the GNU General Public License (version 2) as published by the
|
| 8 |
+
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
|
| 9 |
+
***************************************************************************
|
| 10 |
+
>>! NOTE: The modification to the GPL is included to allow you to !<<
|
| 11 |
+
>>! distribute a combined work that includes FreeRTOS without being !<<
|
| 12 |
+
>>! obliged to provide the source code for proprietary components !<<
|
| 13 |
+
>>! outside of the FreeRTOS kernel. !<<
|
| 14 |
+
***************************************************************************
|
| 15 |
+
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
|
| 16 |
+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
| 17 |
+
FOR A PARTICULAR PURPOSE. Full license text is available on the following
|
| 18 |
+
link: http://www.freertos.org/a00114.html
|
| 19 |
+
***************************************************************************
|
| 20 |
+
* *
|
| 21 |
+
* FreeRTOS provides completely free yet professionally developed, *
|
| 22 |
+
* robust, strictly quality controlled, supported, and cross *
|
| 23 |
+
* platform software that is more than just the market leader, it *
|
| 24 |
+
* is the industry's de facto standard. *
|
| 25 |
+
* *
|
| 26 |
+
* Help yourself get started quickly while simultaneously helping *
|
| 27 |
+
* to support the FreeRTOS project by purchasing a FreeRTOS *
|
| 28 |
+
* tutorial book, reference manual, or both: *
|
| 29 |
+
* http://www.FreeRTOS.org/Documentation *
|
| 30 |
+
* *
|
| 31 |
+
***************************************************************************
|
| 32 |
+
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
|
| 33 |
+
the FAQ page "My application does not run, what could be wrong?". Have you
|
| 34 |
+
defined configASSERT()?
|
| 35 |
+
http://www.FreeRTOS.org/support - In return for receiving this top quality
|
| 36 |
+
embedded software for free we request you assist our global community by
|
| 37 |
+
participating in the support forum.
|
| 38 |
+
http://www.FreeRTOS.org/training - Investing in training allows your team to
|
| 39 |
+
be as productive as possible as early as possible. Now you can receive
|
| 40 |
+
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
|
| 41 |
+
Ltd, and the world's leading authority on the world's leading RTOS.
|
| 42 |
+
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
|
| 43 |
+
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
|
| 44 |
+
compatible FAT file system, and our tiny thread aware UDP/IP stack.
|
| 45 |
+
http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
|
| 46 |
+
Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
|
| 47 |
+
http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
|
| 48 |
+
Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
|
| 49 |
+
licenses offer ticketed support, indemnification and commercial middleware.
|
| 50 |
+
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
|
| 51 |
+
engineered and independently SIL3 certified version for use in safety and
|
| 52 |
+
mission critical applications that require provable dependability.
|
| 53 |
+
1 tab == 4 spaces!
|
| 54 |
+
*/
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
#ifndef FREERTOS_CONFIG_H
|
| 58 |
+
#define FREERTOS_CONFIG_H
|
| 59 |
+
|
| 60 |
+
//#include "clock_config.h"
|
| 61 |
+
|
| 62 |
+
/*-----------------------------------------------------------
|
| 63 |
+
* Application specific definitions.
|
| 64 |
+
*
|
| 65 |
+
* These definitions should be adjusted for your particular hardware and
|
| 66 |
+
* application requirements.
|
| 67 |
+
*
|
| 68 |
+
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
|
| 69 |
+
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
|
| 70 |
+
*
|
| 71 |
+
* See http://www.freertos.org/a00110.html.
|
| 72 |
+
*----------------------------------------------------------*/
|
| 73 |
+
|
| 74 |
+
/* See https://www.freertos.org/Using-FreeRTOS-on-RISC-V.html */
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
/******************************************************************************
|
| 78 |
+
* Modified for the RISC-V core: https://github.com/AngeloJacobo/RISC-V
|
| 79 |
+
******************************************************************************/
|
| 80 |
+
#define configMTIME_BASE_ADDRESS ( 0x80000000UL )
|
| 81 |
+
#define configMTIMECMP_BASE_ADDRESS ( 0x80000008UL )
|
| 82 |
+
|
| 83 |
+
#define configISR_STACK_SIZE_WORDS ( 128 )
|
| 84 |
+
|
| 85 |
+
#define configUSE_PREEMPTION 1
|
| 86 |
+
#define configUSE_IDLE_HOOK 1
|
| 87 |
+
#define configUSE_TICK_HOOK 1
|
| 88 |
+
#define configCPU_CLOCK_HZ 12000000 //Frequency in Hz at which the internal clock that drives the peripheral used to generate the tick interrupt will be executing
|
| 89 |
+
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) //
|
| 90 |
+
#define configMAX_PRIORITIES ( 5 ) //Each task is assigned a priority from 0 to ( configMAX_PRIORITIES - 1 )
|
| 91 |
+
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 128 ) /* Can be as low as 60 but some of the demo tasks that use this constant require it to be higher. */
|
| 92 |
+
#define configSUPPORT_DYNAMIC_ALLOCATION 1
|
| 93 |
+
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 14*1024 ) ) //RAM length is set to 16K
|
| 94 |
+
//You can make this configTOTAL_HEAP_SIZE as big as you want, the linker will issue an error when you’re running out of RAM.
|
| 95 |
+
//https://www.freertos.org/FreeRTOS_Support_Forum_Archive/March_2015/freertos_How_to_configure_the_Total_Heap_Size_5a94a34cj.html
|
| 96 |
+
#define configMAX_TASK_NAME_LEN ( 16 ) //The maximum permissible length of the descriptive name given to a task when the task is created.
|
| 97 |
+
#define configUSE_TRACE_FACILITY 1
|
| 98 |
+
#define configUSE_16_BIT_TICKS 0 //Defining configUSE_16_BIT_TICKS as 0 causes TickType_t to be defined (typedef'ed) as an unsigned 32bit type.
|
| 99 |
+
#define configIDLE_SHOULD_YIELD 0 //Setting configIDLE_SHOULD_YIELD to 0 prevents the idle task from yielding processing time until the end of its time slice. This ensure all tasks at the idle priority are allocated an equal amount of processing time
|
| 100 |
+
#define configUSE_MUTEXES 1
|
| 101 |
+
#define configQUEUE_REGISTRY_SIZE 8
|
| 102 |
+
#define configCHECK_FOR_STACK_OVERFLOW 2
|
| 103 |
+
#define configUSE_RECURSIVE_MUTEXES 1
|
| 104 |
+
#define configUSE_MALLOC_FAILED_HOOK 1
|
| 105 |
+
#define configUSE_APPLICATION_TASK_TAG 0
|
| 106 |
+
#define configUSE_COUNTING_SEMAPHORES 1
|
| 107 |
+
#define configGENERATE_RUN_TIME_STATS 0
|
| 108 |
+
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 4
|
| 109 |
+
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
|
| 110 |
+
|
| 111 |
+
/* Co-routine definitions. */
|
| 112 |
+
#define configUSE_CO_ROUTINES 0
|
| 113 |
+
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
|
| 114 |
+
|
| 115 |
+
/* Software timer definitions. */
|
| 116 |
+
#define configUSE_TIMERS 1
|
| 117 |
+
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
|
| 118 |
+
#define configTIMER_QUEUE_LENGTH 4
|
| 119 |
+
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE )
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
/* Set the following definitions to 1 to include the API function, or zero
|
| 124 |
+
to exclude the API function. */
|
| 125 |
+
#define INCLUDE_vTaskPrioritySet 1
|
| 126 |
+
#define INCLUDE_uxTaskPriorityGet 1
|
| 127 |
+
#define INCLUDE_vTaskDelete 1
|
| 128 |
+
#define INCLUDE_vTaskCleanUpResources 1
|
| 129 |
+
#define INCLUDE_vTaskSuspend 1
|
| 130 |
+
#define INCLUDE_vTaskDelayUntil 1
|
| 131 |
+
#define INCLUDE_vTaskDelay 1
|
| 132 |
+
#define INCLUDE_eTaskGetState 1
|
| 133 |
+
#define INCLUDE_xTimerPendFunctionCall 1
|
| 134 |
+
#define INCLUDE_xTaskAbortDelay 1
|
| 135 |
+
#define INCLUDE_xTaskGetHandle 1
|
| 136 |
+
#define INCLUDE_xSemaphoreGetMutexHolder 1
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
/* Normal assert() semantics without relying on the provision of an assert.h
|
| 140 |
+
header file. */
|
| 141 |
+
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); __asm volatile( "ebreak" ); for( ;; ); }
|
| 142 |
+
|
| 143 |
+
#endif /* FREERTOS_CONFIG_H */
|
AngeloJacobo_RISC-V/test/freertos/freertos.c
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
| 5 |
+
#include "rv32i.h"
|
| 6 |
+
|
| 7 |
+
/* Kernel includes. */
|
| 8 |
+
#include "FreeRTOS.h"
|
| 9 |
+
#include "task.h"
|
| 10 |
+
#include "queue.h"
|
| 11 |
+
#include "semphr.h"
|
| 12 |
+
|
| 13 |
+
// CONFIGURABLES
|
| 14 |
+
int moisture_sensor_pin = 0; //gpio pin for moisture sensor
|
| 15 |
+
int motor_pump_pin = 1; //gpio pin fot water pump motor
|
| 16 |
+
int trig_pin = 2; //trigger pin for ultrasonic sensor
|
| 17 |
+
int echo_pin = 3; //echo pin for ultrasonic sensor
|
| 18 |
+
int buzzer_pin = 4; //buzzer pin
|
| 19 |
+
char buzzer_on_code[2] = "a"; //code for buzzer on
|
| 20 |
+
char buzzer_off_code[2] = "b"; //code for buzzer off
|
| 21 |
+
char water_pump_on_code[2] = "c"; //code for turning on water pump
|
| 22 |
+
|
| 23 |
+
// Tasks function prototypes
|
| 24 |
+
void vBluetoothReceive( void *pvParameters );
|
| 25 |
+
void vBluetoothSend( void *pvParameters );
|
| 26 |
+
void vHygroTempSensor( void *pvParameters );
|
| 27 |
+
void vMoistureSensor( void *pvParameters );
|
| 28 |
+
void vWaterPumpMotor( void *pvParameters );
|
| 29 |
+
void vUltraSonicSensor( void *pvParameters );
|
| 30 |
+
void vBuzzerOn( void *pvParameters );
|
| 31 |
+
void vBuzzerOff( void *pvParameters );
|
| 32 |
+
void vRTC( void *pvParameters );
|
| 33 |
+
void vLCD( void *pvParameters );
|
| 34 |
+
|
| 35 |
+
// Freertos functions
|
| 36 |
+
extern void freertos_risc_v_trap_handler( void );
|
| 37 |
+
void vApplicationTickHook( void );
|
| 38 |
+
|
| 39 |
+
// Global variables shared by tasks
|
| 40 |
+
char rx_data; //stores data received from bluetooth
|
| 41 |
+
int humidity, temperature; //stores humidity and temperature values
|
| 42 |
+
int buzzer_on; //turns on-off the buzzer
|
| 43 |
+
int moist; //stores if moist detected
|
| 44 |
+
int ultrasonic_distance_cm; //stores distance in cm detected by ultrasonic sensor
|
| 45 |
+
SemaphoreHandle_t i2c_mutex; //mutex for accessing I2C peripheral
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
// main function
|
| 49 |
+
int main( void )
|
| 50 |
+
{
|
| 51 |
+
BaseType_t vBluetoothReceive_task,
|
| 52 |
+
vBluetoothSend_task,
|
| 53 |
+
vHygroTempSensor_task,
|
| 54 |
+
vMoistureSensor_task,
|
| 55 |
+
vWaterPumpMotor_task,
|
| 56 |
+
vUltraSonicSensor_task,
|
| 57 |
+
vBuzzerOn_task,
|
| 58 |
+
vBuzzerOff_task,
|
| 59 |
+
vLCD_task;
|
| 60 |
+
|
| 61 |
+
csr_write(MTVEC, (uint32_t) &freertos_risc_v_trap_handler); // set the trap handler to FreeRTOS
|
| 62 |
+
i2c_mutex = xSemaphoreCreateMutex(); //create semaphoe for accessing I2C peripheral
|
| 63 |
+
|
| 64 |
+
// Create tasks
|
| 65 |
+
vBluetoothReceive_task =
|
| 66 |
+
xTaskCreate( vBluetoothReceive, /* The function that implements the task. */
|
| 67 |
+
"vBluetoothReceive", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 68 |
+
100, /* The size of the stack to allocate to the task. */
|
| 69 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 70 |
+
1, /* The priority assigned to the task. */
|
| 71 |
+
NULL );
|
| 72 |
+
|
| 73 |
+
vBluetoothSend_task =
|
| 74 |
+
xTaskCreate( vBluetoothSend, /* The function that implements the task. */
|
| 75 |
+
"vBluetoothSend", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 76 |
+
300, /* The size of the stack to allocate to the task. */
|
| 77 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 78 |
+
1, /* The priority assigned to the task. */
|
| 79 |
+
NULL );
|
| 80 |
+
|
| 81 |
+
vHygroTempSensor_task =
|
| 82 |
+
xTaskCreate( vHygroTempSensor, /* The function that implements the task. */
|
| 83 |
+
"vHygroTempSensor", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 84 |
+
300, /* The size of the stack to allocate to the task. */
|
| 85 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 86 |
+
1, /* The priority assigned to the task. */
|
| 87 |
+
NULL );
|
| 88 |
+
|
| 89 |
+
vMoistureSensor_task =
|
| 90 |
+
xTaskCreate( vMoistureSensor, /* The function that implements the task. */
|
| 91 |
+
"vMoistureSensor", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 92 |
+
300, /* The size of the stack to allocate to the task. */
|
| 93 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 94 |
+
1, /* The priority assigned to the task. */
|
| 95 |
+
NULL );
|
| 96 |
+
|
| 97 |
+
vWaterPumpMotor_task =
|
| 98 |
+
xTaskCreate( vWaterPumpMotor, /* The function that implements the task. */
|
| 99 |
+
"vWaterPumpMotor", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 100 |
+
200, /* The size of the stack to allocate to the task. */
|
| 101 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 102 |
+
1, /* The priority assigned to the task. */
|
| 103 |
+
NULL );
|
| 104 |
+
|
| 105 |
+
vUltraSonicSensor_task =
|
| 106 |
+
xTaskCreate( vUltraSonicSensor, /* The function that implements the task. */
|
| 107 |
+
"vUltraSonicSensor", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 108 |
+
300, /* The size of the stack to allocate to the task. */
|
| 109 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 110 |
+
1, /* The priority assigned to the task. */
|
| 111 |
+
NULL );
|
| 112 |
+
|
| 113 |
+
vBuzzerOn_task =
|
| 114 |
+
xTaskCreate( vBuzzerOn, /* The function that implements the task. */
|
| 115 |
+
"vBuzzerOn", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 116 |
+
200, /* The size of the stack to allocate to the task. */
|
| 117 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 118 |
+
1, /* The priority assigned to the task. */
|
| 119 |
+
NULL );
|
| 120 |
+
|
| 121 |
+
vBuzzerOff_task =
|
| 122 |
+
xTaskCreate( vBuzzerOff, /* The function that implements the task. */
|
| 123 |
+
"vBuzzerOff", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 124 |
+
200, /* The size of the stack to allocate to the task. */
|
| 125 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 126 |
+
1, /* The priority assigned to the task. */
|
| 127 |
+
NULL );
|
| 128 |
+
|
| 129 |
+
vLCD_task =
|
| 130 |
+
xTaskCreate( vLCD, /* The function that implements the task. */
|
| 131 |
+
"vLCD", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 132 |
+
300, /* The size of the stack to allocate to the task. */
|
| 133 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 134 |
+
1, /* The priority assigned to the task. */
|
| 135 |
+
NULL );
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
// Check if all task creation passed
|
| 139 |
+
|
| 140 |
+
if( vBluetoothReceive_task != pdPASS )
|
| 141 |
+
{
|
| 142 |
+
uart_print("vBluetoothReceive Task Failed to Create\n");
|
| 143 |
+
return(1);
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
if( vBluetoothSend_task != pdPASS )
|
| 147 |
+
{
|
| 148 |
+
uart_print("vBluetoothSend Task Failed to Create\n");
|
| 149 |
+
return(1);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
if( vHygroTempSensor_task != pdPASS )
|
| 153 |
+
{
|
| 154 |
+
uart_print("vHygroTempSensor Task Failed to Create\n");
|
| 155 |
+
return(1);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
if( vMoistureSensor_task != pdPASS )
|
| 159 |
+
{
|
| 160 |
+
uart_print("vMoistureSensor Task Failed to Create\n");
|
| 161 |
+
return(1);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
if( vWaterPumpMotor_task != pdPASS )
|
| 165 |
+
{
|
| 166 |
+
uart_print("vWaterPumpMotor Task Failed to Create\n");
|
| 167 |
+
return(1);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
if( vUltraSonicSensor_task != pdPASS )
|
| 171 |
+
{
|
| 172 |
+
uart_print("vUltraSonicSensor Task Failed to Create\n");
|
| 173 |
+
return(1);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
if( vBuzzerOn_task != pdPASS )
|
| 177 |
+
{
|
| 178 |
+
uart_print("vBuzzerOn Task Failed to Create\n");
|
| 179 |
+
return(1);
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
if( vBuzzerOff_task != pdPASS )
|
| 183 |
+
{
|
| 184 |
+
uart_print("vBuzzerOff Task Failed to Create\n");
|
| 185 |
+
return(1);
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
if( vLCD_task != pdPASS )
|
| 189 |
+
{
|
| 190 |
+
uart_print("vLCD Task Failed to Create\n");
|
| 191 |
+
return(1);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
/* Start the tasks and timer running. */
|
| 195 |
+
vTaskStartScheduler();
|
| 196 |
+
|
| 197 |
+
uart_print("ERROR: You reached past the vTaskStartScheduler()");
|
| 198 |
+
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
void vBluetoothReceive( void *pvParameters ){
|
| 202 |
+
int buffer_full;
|
| 203 |
+
while(1){
|
| 204 |
+
buffer_full = uart_rx_buffer_full(); //check if read buffer is full and data can be read
|
| 205 |
+
if(buffer_full){
|
| 206 |
+
rx_data = uart_read(); //read data from buffer (make sure to check first if rx buffer is full)
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
void vBluetoothSend( void *pvParameters ){
|
| 212 |
+
char msg[10];
|
| 213 |
+
while(1){
|
| 214 |
+
sprintf_(msg, "%d", temperature); //convert temperatue value in integer to char array
|
| 215 |
+
uart_print(msg); //print serially to bluetooth
|
| 216 |
+
uart_print(";"); //delimiter
|
| 217 |
+
sprintf_(msg, "%d", humidity); //convert humidity value in integer to char array
|
| 218 |
+
uart_print(msg); //print serially to bluetooth
|
| 219 |
+
uart_print(";"); //delimiter
|
| 220 |
+
|
| 221 |
+
if(buzzer_on){
|
| 222 |
+
uart_print(buzzer_on_code); //code for buzzer on
|
| 223 |
+
uart_print(";"); //delimiter
|
| 224 |
+
}
|
| 225 |
+
else {
|
| 226 |
+
uart_print(buzzer_off_code); //code for buzzer off
|
| 227 |
+
uart_print(";"); //delimiter
|
| 228 |
+
}
|
| 229 |
+
uart_print("\n");
|
| 230 |
+
delay_ms(500);
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
void vHygroTempSensor( void *pvParameters ){
|
| 235 |
+
while(1){
|
| 236 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 237 |
+
hygroi2c_begin(); //restart hygroi2c sensor
|
| 238 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 239 |
+
delay_ms(1); //add delay between taking semaphores
|
| 240 |
+
|
| 241 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 242 |
+
temperature = (int)hygroi2c_getTemperature(); //retrieve temperature value
|
| 243 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 244 |
+
delay_ms(1);
|
| 245 |
+
|
| 246 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantim
|
| 247 |
+
humidity = (int)hygroi2c_getHumidity(); //retrieve humidity value
|
| 248 |
+
xSemaphoreGive(i2c_mutex);
|
| 249 |
+
delay_ms(1);
|
| 250 |
+
}
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
void vMoistureSensor( void *pvParameters ){
|
| 254 |
+
while(1){
|
| 255 |
+
moist = !gpio_read_pin(moisture_sensor_pin); //moisture sensor is active low (0 when moist detected)
|
| 256 |
+
delay_ms(1);
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
void vWaterPumpMotor( void *pvParameters ){
|
| 261 |
+
|
| 262 |
+
gpio_write_pin(motor_pump_pin, 1);
|
| 263 |
+
while(1){
|
| 264 |
+
if(!moist) {
|
| 265 |
+
gpio_write_pin(motor_pump_pin, 0);
|
| 266 |
+
}
|
| 267 |
+
else{
|
| 268 |
+
gpio_write_pin(motor_pump_pin, 1);
|
| 269 |
+
}
|
| 270 |
+
if(rx_data == water_pump_on_code[0]) {
|
| 271 |
+
rx_data = 0;
|
| 272 |
+
gpio_write_pin(motor_pump_pin, 0);
|
| 273 |
+
delay_ms(3000);
|
| 274 |
+
gpio_write_pin(motor_pump_pin, 1);
|
| 275 |
+
}
|
| 276 |
+
delay_ms(1);
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
void vUltraSonicSensor( void *pvParameters ){
|
| 281 |
+
while(1){
|
| 282 |
+
ultrasonic_distance_cm = ultrasonic_sensor_cm(trig_pin, echo_pin); // returns distance in cm detected by the ultrasonic sensor
|
| 283 |
+
delay_ms(1);
|
| 284 |
+
}
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
void vBuzzerOff( void *pvParameters ){
|
| 288 |
+
while(1){
|
| 289 |
+
if(rx_data == buzzer_off_code[0]){
|
| 290 |
+
gpio_write_pin(buzzer_pin, 0); //turn off buzzer using serial line
|
| 291 |
+
rx_data = 0;
|
| 292 |
+
buzzer_on = 0;
|
| 293 |
+
gpio_write_pin(8, 0); //buzzer will turn on when distance detected is less than 10cm
|
| 294 |
+
}
|
| 295 |
+
delay_ms(1);
|
| 296 |
+
}
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
void vBuzzerOn( void *pvParameters ){
|
| 300 |
+
gpio_write_pin(buzzer_pin, 0); //buzzer off
|
| 301 |
+
buzzer_on = 0;
|
| 302 |
+
delay_ms(5000);
|
| 303 |
+
while(1){
|
| 304 |
+
if(ultrasonic_distance_cm < 10){
|
| 305 |
+
gpio_write_pin(buzzer_pin, 1); //buzzer will turn on when distance detected is less than 10cm
|
| 306 |
+
buzzer_on = 1;
|
| 307 |
+
delay_ms(10000); //buzzer remain on for 10 sec
|
| 308 |
+
}
|
| 309 |
+
else{
|
| 310 |
+
gpio_write_pin(buzzer_pin, 0); //else turn buzzer off
|
| 311 |
+
buzzer_on = 0;
|
| 312 |
+
}
|
| 313 |
+
delay_ms(1);
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
void vLCD( void *pvParameters ){
|
| 318 |
+
char msg[10];
|
| 319 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 320 |
+
LCD_Init(); // Initialize LCD module
|
| 321 |
+
LCD_Set_Cursor(1, 1);
|
| 322 |
+
LCD_Write_String("RISC-V with RTOS");
|
| 323 |
+
LCD_Set_Cursor(2, 1);
|
| 324 |
+
LCD_Write_String("Team GraduatECEs");
|
| 325 |
+
delay_ms(1000);
|
| 326 |
+
LCD_Clear();
|
| 327 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 328 |
+
while(1){
|
| 329 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 330 |
+
LCD_Set_Cursor(1, 1);
|
| 331 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 332 |
+
delay_ms(1);
|
| 333 |
+
|
| 334 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 335 |
+
LCD_Write_String("Temp:");
|
| 336 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 337 |
+
delay_ms(1);
|
| 338 |
+
|
| 339 |
+
sprintf_(msg, "%d", temperature); //convert humidity value in integer to char array
|
| 340 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 341 |
+
LCD_Write_String(msg);
|
| 342 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 343 |
+
delay_ms(1);
|
| 344 |
+
|
| 345 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 346 |
+
LCD_Write_String("C ");
|
| 347 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 348 |
+
delay_ms(1);
|
| 349 |
+
|
| 350 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 351 |
+
LCD_Write_String("Hum:");
|
| 352 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 353 |
+
delay_ms(1);
|
| 354 |
+
|
| 355 |
+
sprintf_(msg, "%d", humidity); //convert humidity value in integer to char array
|
| 356 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 357 |
+
LCD_Write_String(msg);
|
| 358 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 359 |
+
delay_ms(1);
|
| 360 |
+
|
| 361 |
+
xSemaphoreTake(i2c_mutex, portMAX_DELAY); //gain access to the i2c peripheral and not let other task to use it for the meantime
|
| 362 |
+
LCD_Write_String("%");
|
| 363 |
+
xSemaphoreGive(i2c_mutex); //release the mutex
|
| 364 |
+
delay_ms(1);
|
| 365 |
+
}
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
/* This handler is responsible for handling all interrupts. Only the machine timer interrupt is handled by the kernel. */
|
| 370 |
+
void SystemIrqHandler( uint32_t mcause )
|
| 371 |
+
{
|
| 372 |
+
uart_print("freeRTOS: Unknown interrupt \n");
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
void vApplicationTickHook( void ){
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
void vApplicationMallocFailedHook( void )
|
| 382 |
+
{
|
| 383 |
+
/* vApplicationMallocFailedHook() will only be called if
|
| 384 |
+
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
|
| 385 |
+
function that will get called if a call to pvPortMalloc() fails.
|
| 386 |
+
pvPortMalloc() is called internally by the kernel whenever a task, queue,
|
| 387 |
+
timer or semaphore is created. It is also called by various parts of the
|
| 388 |
+
demo application. If heap_1.c or heap_2.c are used, then the size of the
|
| 389 |
+
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
|
| 390 |
+
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
|
| 391 |
+
to query the size of free heap space that remains (although it does not
|
| 392 |
+
provide information on how the remaining heap might be fragmented). */
|
| 393 |
+
taskDISABLE_INTERRUPTS();
|
| 394 |
+
uart_print("FreeRTOS_FAULT: vApplicationMallocFailedHook (solution: increase 'configTOTAL_HEAP_SIZE' in FreeRTOSConfig.h)\n");
|
| 395 |
+
__asm volatile( "nop" );
|
| 396 |
+
__asm volatile( "ebreak" );
|
| 397 |
+
for( ;; );
|
| 398 |
+
}
|
| 399 |
+
/*-----------------------------------------------------------*/
|
| 400 |
+
|
| 401 |
+
void vApplicationIdleHook( void )
|
| 402 |
+
{
|
| 403 |
+
/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
|
| 404 |
+
to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
|
| 405 |
+
task. It is essential that code added to this hook function never attempts
|
| 406 |
+
to block in any way (for example, call xQueueReceive() with a block time
|
| 407 |
+
specified, or call vTaskDelay()). If the application makes use of the
|
| 408 |
+
vTaskDelete() API function (as this demo application does) then it is also
|
| 409 |
+
important that vApplicationIdleHook() is permitted to return to its calling
|
| 410 |
+
function, because it is the responsibility of the idle task to clean up
|
| 411 |
+
memory allocated by the kernel to any task that has since been deleted. */
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
/*-----------------------------------------------------------*/
|
| 415 |
+
|
| 416 |
+
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
|
| 417 |
+
{
|
| 418 |
+
( void ) pcTaskName;
|
| 419 |
+
( void ) pxTask;
|
| 420 |
+
|
| 421 |
+
/* Run time stack overflow checking is performed if
|
| 422 |
+
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
|
| 423 |
+
function is called if a stack overflow is detected. */
|
| 424 |
+
taskDISABLE_INTERRUPTS();
|
| 425 |
+
uart_print("FreeRTOS_FAULT: vApplicationStackOverflowHook\n");
|
| 426 |
+
__asm volatile( "nop" );
|
| 427 |
+
__asm volatile( "nop" );
|
| 428 |
+
__asm volatile( "ebreak" );
|
| 429 |
+
for( ;; );
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
|
AngeloJacobo_RISC-V/test/freertos/freertos_old.c
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// This is sourced from: https://github.com/stnolting/neorv32/blob/main/sw/example/demo_freeRTOS/main.c
|
| 2 |
+
|
| 3 |
+
/* Standard includes. */
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include "rv32i.h"
|
| 6 |
+
|
| 7 |
+
/* Kernel includes. */
|
| 8 |
+
#include "FreeRTOS.h"
|
| 9 |
+
#include "task.h"
|
| 10 |
+
#include "queue.h"
|
| 11 |
+
#include "semphr.h"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
void vApplicationTickHook( void );
|
| 15 |
+
void vUartSend( void *pvParameters );
|
| 16 |
+
void vUARTReceive( void *pvParameters );
|
| 17 |
+
void vToggleGPIO( void *pvParameters );
|
| 18 |
+
void vLCD( void *pvParameters );
|
| 19 |
+
extern void freertos_risc_v_trap_handler( void );
|
| 20 |
+
|
| 21 |
+
char rx_data[2];
|
| 22 |
+
SemaphoreHandle_t uart_mutex;
|
| 23 |
+
|
| 24 |
+
int main( void )
|
| 25 |
+
{
|
| 26 |
+
BaseType_t a;
|
| 27 |
+
BaseType_t b;
|
| 28 |
+
BaseType_t c;
|
| 29 |
+
BaseType_t d;
|
| 30 |
+
uart_print("FreeRTOS DEMO\n");
|
| 31 |
+
csr_write(MTVEC, (uint32_t) &freertos_risc_v_trap_handler);
|
| 32 |
+
uart_mutex = xSemaphoreCreateMutex();
|
| 33 |
+
|
| 34 |
+
a = xTaskCreate( vUartSend, /* The function that implements the task. */
|
| 35 |
+
"UART_SEND", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 36 |
+
500, /* The size of the stack to allocate to the task. */
|
| 37 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 38 |
+
1, /* The priority assigned to the task. */
|
| 39 |
+
NULL ); /* The task handle is not required, so NULL is passed. */
|
| 40 |
+
|
| 41 |
+
if( a != pdPASS )
|
| 42 |
+
{
|
| 43 |
+
uart_print("First Task Failed to Create\n");
|
| 44 |
+
return(1);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
b = xTaskCreate( vLCD, /* The function that implements the task. */
|
| 48 |
+
"LCD_WRITE", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 49 |
+
500, /* The size of the stack to allocate to the task. */
|
| 50 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 51 |
+
1, /* The priority assigned to the task. */
|
| 52 |
+
NULL );
|
| 53 |
+
|
| 54 |
+
if( b != pdPASS )
|
| 55 |
+
{
|
| 56 |
+
uart_print("Second Task Failed to Create\n");
|
| 57 |
+
return(2);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
c = xTaskCreate( vUARTReceive, /* The function that implements the task. */
|
| 61 |
+
"LCD_WRITE", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 62 |
+
500, /* The size of the stack to allocate to the task. */
|
| 63 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 64 |
+
1, /* The priority assigned to the task. */
|
| 65 |
+
NULL );
|
| 66 |
+
|
| 67 |
+
if( c != pdPASS )
|
| 68 |
+
{
|
| 69 |
+
uart_print("Third Task Failed to Create\n");
|
| 70 |
+
return(3);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
d = xTaskCreate( vToggleGPIO, /* The function that implements the task. */
|
| 74 |
+
"LCD_WRITE", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 75 |
+
500, /* The size of the stack to allocate to the task. */
|
| 76 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 77 |
+
1, /* The priority assigned to the task. */
|
| 78 |
+
NULL );
|
| 79 |
+
|
| 80 |
+
if( d != pdPASS )
|
| 81 |
+
{
|
| 82 |
+
uart_print("Fourth Task Failed to Create\n");
|
| 83 |
+
return(3);
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* Start the tasks and timer running. */
|
| 87 |
+
vTaskStartScheduler();
|
| 88 |
+
|
| 89 |
+
uart_print("ERROR: You reached past the vTaskStartScheduler()");
|
| 90 |
+
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
void vUartSend( void *pvParameters ){
|
| 94 |
+
while(1){
|
| 95 |
+
xSemaphoreTake(uart_mutex, portMAX_DELAY);
|
| 96 |
+
uart_print("This is the 1st line and this is pretty long do you understand?\n");
|
| 97 |
+
xSemaphoreGive(uart_mutex);
|
| 98 |
+
vTaskDelay(1); //100 ticks
|
| 99 |
+
xSemaphoreTake(uart_mutex, portMAX_DELAY);
|
| 100 |
+
uart_print("This is the 2nd line and I guess I'm already out of words. Let's see if I can think of more things to say or am I stuck?\n");
|
| 101 |
+
xSemaphoreGive(uart_mutex);
|
| 102 |
+
vTaskDelay(1); //100 ticks
|
| 103 |
+
xSemaphoreTake(uart_mutex, portMAX_DELAY);
|
| 104 |
+
uart_print("This is the 3rd line and this is pretty long do you understand?\n");
|
| 105 |
+
xSemaphoreGive(uart_mutex);
|
| 106 |
+
vTaskDelay(1); //100 ticks
|
| 107 |
+
xSemaphoreTake(uart_mutex, portMAX_DELAY);
|
| 108 |
+
uart_print("This is the 4th line and I guess I'm already out of words. Let's see if I can think of more things to say or am I stuck?\n");
|
| 109 |
+
xSemaphoreGive(uart_mutex);
|
| 110 |
+
vTaskDelay(1); //100 ticks
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
void vUARTReceive( void *pvParameters ){
|
| 116 |
+
int buffer_full;
|
| 117 |
+
rx_data[1] = '\0';
|
| 118 |
+
while(1){
|
| 119 |
+
buffer_full = uart_rx_buffer_full(); //check if read buffer is full and data can be read
|
| 120 |
+
if(buffer_full){
|
| 121 |
+
rx_data[0] = uart_read(); //read data from buffer (make sure to check first if rx buffer is full)
|
| 122 |
+
}
|
| 123 |
+
xSemaphoreTake(uart_mutex, portMAX_DELAY);
|
| 124 |
+
uart_print("This is from vUARTReceive, is this still jumbled?\n");
|
| 125 |
+
xSemaphoreGive(uart_mutex);
|
| 126 |
+
vTaskDelay(1); //100 ticks
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
void vToggleGPIO( void *pvParameters ){
|
| 131 |
+
while(1){
|
| 132 |
+
if(rx_data[0] == '0'){
|
| 133 |
+
gpio_write_pin(8, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 134 |
+
gpio_write_pin(9, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 135 |
+
gpio_write_pin(10, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 136 |
+
gpio_write_pin(11, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 137 |
+
}
|
| 138 |
+
else if(rx_data[0] == '1') {
|
| 139 |
+
gpio_write_pin(8, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 140 |
+
gpio_write_pin(9, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 141 |
+
gpio_write_pin(10, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 142 |
+
gpio_write_pin(11, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
void vLCD( void *pvParameters ){
|
| 148 |
+
int counter = 0;
|
| 149 |
+
int length = 0;
|
| 150 |
+
char string[16]; //max of 16 chars
|
| 151 |
+
//uart_print("INITIALIZING LCD MODULE.....\n");
|
| 152 |
+
LCD_Init(0x4E); // Initialize LCD module with I2C address = 0x4E
|
| 153 |
+
//uart_print("INITIALIZING DONE!\n\n");
|
| 154 |
+
|
| 155 |
+
LCD_Set_Cursor(1, 1);
|
| 156 |
+
LCD_Write_String(" Angelo Jacobo");
|
| 157 |
+
LCD_Set_Cursor(2, 3);
|
| 158 |
+
//LCD_Clear();
|
| 159 |
+
delay_ms(1000);
|
| 160 |
+
while(1){
|
| 161 |
+
//convert counter to string
|
| 162 |
+
sprintf_(string, "%d", counter);
|
| 163 |
+
//print to LCD
|
| 164 |
+
LCD_Set_Cursor(2, 7);
|
| 165 |
+
LCD_Write_String(string);
|
| 166 |
+
delay_ms(1000);
|
| 167 |
+
//increment counter
|
| 168 |
+
counter++;
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
/* This handler is responsible for handling all interrupts. Only the machine timer interrupt is handled by the kernel. */
|
| 175 |
+
void SystemIrqHandler( uint32_t mcause )
|
| 176 |
+
{
|
| 177 |
+
uart_print("freeRTOS: Unknown interrupt \n");
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
void vApplicationTickHook( void ){
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
void vApplicationMallocFailedHook( void )
|
| 187 |
+
{
|
| 188 |
+
/* vApplicationMallocFailedHook() will only be called if
|
| 189 |
+
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
|
| 190 |
+
function that will get called if a call to pvPortMalloc() fails.
|
| 191 |
+
pvPortMalloc() is called internally by the kernel whenever a task, queue,
|
| 192 |
+
timer or semaphore is created. It is also called by various parts of the
|
| 193 |
+
demo application. If heap_1.c or heap_2.c are used, then the size of the
|
| 194 |
+
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
|
| 195 |
+
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
|
| 196 |
+
to query the size of free heap space that remains (although it does not
|
| 197 |
+
provide information on how the remaining heap might be fragmented). */
|
| 198 |
+
taskDISABLE_INTERRUPTS();
|
| 199 |
+
uart_print("FreeRTOS_FAULT: vApplicationMallocFailedHook (solution: increase 'configTOTAL_HEAP_SIZE' in FreeRTOSConfig.h)\n");
|
| 200 |
+
__asm volatile( "nop" );
|
| 201 |
+
__asm volatile( "ebreak" );
|
| 202 |
+
for( ;; );
|
| 203 |
+
}
|
| 204 |
+
/*-----------------------------------------------------------*/
|
| 205 |
+
|
| 206 |
+
void vApplicationIdleHook( void )
|
| 207 |
+
{
|
| 208 |
+
/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
|
| 209 |
+
to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
|
| 210 |
+
task. It is essential that code added to this hook function never attempts
|
| 211 |
+
to block in any way (for example, call xQueueReceive() with a block time
|
| 212 |
+
specified, or call vTaskDelay()). If the application makes use of the
|
| 213 |
+
vTaskDelete() API function (as this demo application does) then it is also
|
| 214 |
+
important that vApplicationIdleHook() is permitted to return to its calling
|
| 215 |
+
function, because it is the responsibility of the idle task to clean up
|
| 216 |
+
memory allocated by the kernel to any task that has since been deleted. */
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
/*-----------------------------------------------------------*/
|
| 220 |
+
|
| 221 |
+
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
|
| 222 |
+
{
|
| 223 |
+
( void ) pcTaskName;
|
| 224 |
+
( void ) pxTask;
|
| 225 |
+
|
| 226 |
+
/* Run time stack overflow checking is performed if
|
| 227 |
+
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
|
| 228 |
+
function is called if a stack overflow is detected. */
|
| 229 |
+
taskDISABLE_INTERRUPTS();
|
| 230 |
+
uart_print("FreeRTOS_FAULT: vApplicationStackOverflowHook\n");
|
| 231 |
+
__asm volatile( "nop" );
|
| 232 |
+
__asm volatile( "nop" );
|
| 233 |
+
__asm volatile( "ebreak" );
|
| 234 |
+
for( ;; );
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
|
AngeloJacobo_RISC-V/test/freertos/freertos_risc_v_chip_specific_extensions.h
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* FreeRTOS Kernel V10.3.1
|
| 3 |
+
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
| 4 |
+
*
|
| 5 |
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
| 6 |
+
* this software and associated documentation files (the "Software"), to deal in
|
| 7 |
+
* the Software without restriction, including without limitation the rights to
|
| 8 |
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
| 9 |
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
| 10 |
+
* subject to the following conditions:
|
| 11 |
+
*
|
| 12 |
+
* The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
* copies or substantial portions of the Software.
|
| 14 |
+
*
|
| 15 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
| 17 |
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
| 18 |
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
| 19 |
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 20 |
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
| 21 |
+
*
|
| 22 |
+
* http://www.FreeRTOS.org
|
| 23 |
+
* http://aws.amazon.com/freertos
|
| 24 |
+
*
|
| 25 |
+
* 1 tab == 4 spaces!
|
| 26 |
+
*/
|
| 27 |
+
|
| 28 |
+
/*
|
| 29 |
+
* The FreeRTOS kernel's RISC-V port is split between the the code that is
|
| 30 |
+
* common across all currently supported RISC-V chips (implementations of the
|
| 31 |
+
* RISC-V ISA), and code that tailors the port to a specific RISC-V chip:
|
| 32 |
+
*
|
| 33 |
+
* + FreeRTOS\Source\portable\GCC\RISC-V-RV32\portASM.S contains the code that
|
| 34 |
+
* is common to all currently supported RISC-V chips. There is only one
|
| 35 |
+
* portASM.S file because the same file is built for all RISC-V target chips.
|
| 36 |
+
*
|
| 37 |
+
* + Header files called freertos_risc_v_chip_specific_extensions.h contain the
|
| 38 |
+
* code that tailors the FreeRTOS kernel's RISC-V port to a specific RISC-V
|
| 39 |
+
* chip. There are multiple freertos_risc_v_chip_specific_extensions.h files
|
| 40 |
+
* as there are multiple RISC-V chip implementations.
|
| 41 |
+
*
|
| 42 |
+
* !!!NOTE!!!
|
| 43 |
+
* TAKE CARE TO INCLUDE THE CORRECT freertos_risc_v_chip_specific_extensions.h
|
| 44 |
+
* HEADER FILE FOR THE CHIP IN USE. This is done using the assembler's (not the
|
| 45 |
+
* compiler's!) include path. For example, if the chip in use includes a core
|
| 46 |
+
* local interrupter (CLINT) and does not include any chip specific register
|
| 47 |
+
* extensions then add the path below to the assembler's include path:
|
| 48 |
+
* FreeRTOS\Source\portable\GCC\RISC-V-RV32\chip_specific_extensions\RV32I_CLINT_no_extensions
|
| 49 |
+
*
|
| 50 |
+
*/
|
| 51 |
+
|
| 52 |
+
/*
|
| 53 |
+
* NEORV32 chip specific extensions
|
| 54 |
+
*/
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
#ifndef __FREERTOS_RISC_V_EXTENSIONS_H__
|
| 58 |
+
#define __FREERTOS_RISC_V_EXTENSIONS_H__
|
| 59 |
+
|
| 60 |
+
#define portasmHAS_SIFIVE_CLINT 0
|
| 61 |
+
#define portasmHAS_MTIME 1
|
| 62 |
+
#define portasmADDITIONAL_CONTEXT_SIZE 0 /* Must be even number on 32-bit cores. */
|
| 63 |
+
|
| 64 |
+
.macro portasmSAVE_ADDITIONAL_REGISTERS
|
| 65 |
+
/* No additional registers to save, so this macro does nothing. */
|
| 66 |
+
.endm
|
| 67 |
+
|
| 68 |
+
.macro portasmRESTORE_ADDITIONAL_REGISTERS
|
| 69 |
+
/* No additional registers to restore, so this macro does nothing. */
|
| 70 |
+
.endm
|
| 71 |
+
|
| 72 |
+
#endif /* __FREERTOS_RISC_V_EXTENSIONS_H__ */
|
| 73 |
+
|
| 74 |
+
|
AngeloJacobo_RISC-V/test/freertos/main_blinky.c
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// This is sourced from: https://github.com/stnolting/neorv32/blob/main/sw/example/demo_freeRTOS/blinky_demo/main_blinky.c
|
| 2 |
+
|
| 3 |
+
/*
|
| 4 |
+
* FreeRTOS Kernel V10.3.0
|
| 5 |
+
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
| 6 |
+
*
|
| 7 |
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
| 8 |
+
* this software and associated documentation files (the "Software"), to deal in
|
| 9 |
+
* the Software without restriction, including without limitation the rights to
|
| 10 |
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
| 11 |
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
| 12 |
+
* subject to the following conditions:
|
| 13 |
+
*
|
| 14 |
+
* The above copyright notice and this permission notice shall be included in all
|
| 15 |
+
* copies or substantial portions of the Software.
|
| 16 |
+
*
|
| 17 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
| 19 |
+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
| 20 |
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
| 21 |
+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 22 |
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
| 23 |
+
*
|
| 24 |
+
* http://www.FreeRTOS.org
|
| 25 |
+
* http://aws.amazon.com/freertos
|
| 26 |
+
*
|
| 27 |
+
* 1 tab == 4 spaces!
|
| 28 |
+
*/
|
| 29 |
+
|
| 30 |
+
/******************************************************************************
|
| 31 |
+
* NOTE 1: This project provides two demo applications. A simple blinky
|
| 32 |
+
* style project, and a more comprehensive test and demo application. The
|
| 33 |
+
* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
|
| 34 |
+
* between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
|
| 35 |
+
* in main.c. This file implements the simply blinky style version.
|
| 36 |
+
*
|
| 37 |
+
* NOTE 2: This file only contains the source code that is specific to the
|
| 38 |
+
* basic demo. Generic functions, such FreeRTOS hook functions, and functions
|
| 39 |
+
* required to configure the hardware are defined in main.c.
|
| 40 |
+
******************************************************************************
|
| 41 |
+
*
|
| 42 |
+
* main_blinky() creates one queue, and two tasks. It then starts the
|
| 43 |
+
* scheduler.
|
| 44 |
+
*
|
| 45 |
+
* The Queue Send Task:
|
| 46 |
+
* The queue send task is implemented by the prvQueueSendTask() function in
|
| 47 |
+
* this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
|
| 48 |
+
* block for 1000 milliseconds, before sending the value 100 to the queue that
|
| 49 |
+
* was created within main_blinky(). Once the value is sent, the task loops
|
| 50 |
+
* back around to block for another 1000 milliseconds...and so on.
|
| 51 |
+
*
|
| 52 |
+
* The Queue Receive Task:
|
| 53 |
+
* The queue receive task is implemented by the prvQueueReceiveTask() function
|
| 54 |
+
* in this file. prvQueueReceiveTask() sits in a loop where it repeatedly
|
| 55 |
+
* blocks on attempts to read data from the queue that was created within
|
| 56 |
+
* main_blinky(). When data is received, the task checks the value of the
|
| 57 |
+
* data, and if the value equals the expected 100, writes 'Blink' to the UART
|
| 58 |
+
* (the UART is used in place of the LED to allow easy execution in QEMU). The
|
| 59 |
+
* 'block time' parameter passed to the queue receive function specifies that
|
| 60 |
+
* the task should be held in the Blocked state indefinitely to wait for data to
|
| 61 |
+
* be available on the queue. The queue receive task will only leave the
|
| 62 |
+
* Blocked state when the queue send task writes to the queue. As the queue
|
| 63 |
+
* send task writes to the queue every 1000 milliseconds, the queue receive
|
| 64 |
+
* task leaves the Blocked state every 1000 milliseconds, and therefore toggles
|
| 65 |
+
* the LED every 200 milliseconds.
|
| 66 |
+
*/
|
| 67 |
+
|
| 68 |
+
/* Standard includes. */
|
| 69 |
+
#include <stdio.h>
|
| 70 |
+
#include <string.h>
|
| 71 |
+
#include <unistd.h>
|
| 72 |
+
#include "rv32i.h"
|
| 73 |
+
|
| 74 |
+
/* Kernel includes. */
|
| 75 |
+
#include "FreeRTOS.h"
|
| 76 |
+
#include "task.h"
|
| 77 |
+
#include "queue.h"
|
| 78 |
+
|
| 79 |
+
/* Priorities used by the tasks. */
|
| 80 |
+
#define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
| 81 |
+
#define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
| 82 |
+
|
| 83 |
+
/* The rate at which data is sent to the queue. The 200ms value is converted
|
| 84 |
+
to ticks using the pdMS_TO_TICKS() macro. */
|
| 85 |
+
#define mainQUEUE_SEND_FREQUENCY_MS pdMS_TO_TICKS( 1000 )
|
| 86 |
+
|
| 87 |
+
/* The maximum number items the queue can hold. The priority of the receiving
|
| 88 |
+
task is above the priority of the sending task, so the receiving task will
|
| 89 |
+
preempt the sending task and remove the queue items each time the sending task
|
| 90 |
+
writes to the queue. Therefore the queue will never have more than one item in
|
| 91 |
+
it at any time, and even with a queue length of 1, the sending task will never
|
| 92 |
+
find the queue full. */
|
| 93 |
+
#define mainQUEUE_LENGTH ( 1 )
|
| 94 |
+
|
| 95 |
+
/*-----------------------------------------------------------*/
|
| 96 |
+
|
| 97 |
+
/*
|
| 98 |
+
* Called by main when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 in
|
| 99 |
+
* main.c.
|
| 100 |
+
*/
|
| 101 |
+
void main_blinky( void );
|
| 102 |
+
|
| 103 |
+
/*
|
| 104 |
+
* The tasks as described in the comments at the top of this file.
|
| 105 |
+
*/
|
| 106 |
+
static void prvQueueReceiveTask( void *pvParameters );
|
| 107 |
+
static void prvQueueSendTask( void *pvParameters );
|
| 108 |
+
|
| 109 |
+
/*-----------------------------------------------------------*/
|
| 110 |
+
|
| 111 |
+
/* The queue used by both tasks. */
|
| 112 |
+
static QueueHandle_t xQueue = NULL;
|
| 113 |
+
|
| 114 |
+
/*-----------------------------------------------------------*/
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
void vApplicationTickHook( void );
|
| 118 |
+
extern void freertos_risc_v_trap_handler( void );
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
int main( void )
|
| 122 |
+
{
|
| 123 |
+
uart_print("FreeRTOS DEMO\n");
|
| 124 |
+
csr_write(MTVEC, (uint32_t) &freertos_risc_v_trap_handler);
|
| 125 |
+
main_blinky();
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
/* This handler is responsible for handling all interrupts. Only the machine timer interrupt is handled by the kernel. */
|
| 130 |
+
void SystemIrqHandler( uint32_t mcause )
|
| 131 |
+
{
|
| 132 |
+
uart_print("freeRTOS: Unknown interrupt \n");
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
void vApplicationTickHook( void ){
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
void vApplicationMallocFailedHook( void )
|
| 142 |
+
{
|
| 143 |
+
/* vApplicationMallocFailedHook() will only be called if
|
| 144 |
+
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
|
| 145 |
+
function that will get called if a call to pvPortMalloc() fails.
|
| 146 |
+
pvPortMalloc() is called internally by the kernel whenever a task, queue,
|
| 147 |
+
timer or semaphore is created. It is also called by various parts of the
|
| 148 |
+
demo application. If heap_1.c or heap_2.c are used, then the size of the
|
| 149 |
+
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
|
| 150 |
+
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
|
| 151 |
+
to query the size of free heap space that remains (although it does not
|
| 152 |
+
provide information on how the remaining heap might be fragmented). */
|
| 153 |
+
taskDISABLE_INTERRUPTS();
|
| 154 |
+
uart_print("FreeRTOS_FAULT: vApplicationMallocFailedHook (solution: increase 'configTOTAL_HEAP_SIZE' in FreeRTOSConfig.h)\n");
|
| 155 |
+
__asm volatile( "nop" );
|
| 156 |
+
__asm volatile( "ebreak" );
|
| 157 |
+
for( ;; );
|
| 158 |
+
}
|
| 159 |
+
/*-----------------------------------------------------------*/
|
| 160 |
+
|
| 161 |
+
void vApplicationIdleHook( void )
|
| 162 |
+
{
|
| 163 |
+
/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
|
| 164 |
+
to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
|
| 165 |
+
task. It is essential that code added to this hook function never attempts
|
| 166 |
+
to block in any way (for example, call xQueueReceive() with a block time
|
| 167 |
+
specified, or call vTaskDelay()). If the application makes use of the
|
| 168 |
+
vTaskDelete() API function (as this demo application does) then it is also
|
| 169 |
+
important that vApplicationIdleHook() is permitted to return to its calling
|
| 170 |
+
function, because it is the responsibility of the idle task to clean up
|
| 171 |
+
memory allocated by the kernel to any task that has since been deleted. */
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
/*-----------------------------------------------------------*/
|
| 175 |
+
|
| 176 |
+
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
|
| 177 |
+
{
|
| 178 |
+
( void ) pcTaskName;
|
| 179 |
+
( void ) pxTask;
|
| 180 |
+
|
| 181 |
+
/* Run time stack overflow checking is performed if
|
| 182 |
+
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
|
| 183 |
+
function is called if a stack overflow is detected. */
|
| 184 |
+
taskDISABLE_INTERRUPTS();
|
| 185 |
+
uart_print("FreeRTOS_FAULT: vApplicationStackOverflowHook\n");
|
| 186 |
+
__asm volatile( "nop" );
|
| 187 |
+
__asm volatile( "nop" );
|
| 188 |
+
__asm volatile( "ebreak" );
|
| 189 |
+
for( ;; );
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
void main_blinky( void )
|
| 194 |
+
{
|
| 195 |
+
/* Create the queue. */
|
| 196 |
+
xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );
|
| 197 |
+
|
| 198 |
+
if( xQueue != NULL )
|
| 199 |
+
{
|
| 200 |
+
/* Start the two tasks as described in the comments at the top of this
|
| 201 |
+
file. */
|
| 202 |
+
xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
|
| 203 |
+
"Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
|
| 204 |
+
configMINIMAL_STACK_SIZE * 2U, /* The size of the stack to allocate to the task. */
|
| 205 |
+
NULL, /* The parameter passed to the task - not used in this case. */
|
| 206 |
+
mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
|
| 207 |
+
NULL ); /* The task handle is not required, so NULL is passed. */
|
| 208 |
+
|
| 209 |
+
xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE * 2U, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
|
| 210 |
+
|
| 211 |
+
/* Start the tasks and timer running. */
|
| 212 |
+
vTaskStartScheduler();
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
/* If all is well, the scheduler will now be running, and the following
|
| 216 |
+
line will never be reached. If the following line does execute, then
|
| 217 |
+
there was insufficient FreeRTOS heap memory available for the Idle and/or
|
| 218 |
+
timer tasks to be created. See the memory management section on the
|
| 219 |
+
FreeRTOS web site for more details on the FreeRTOS heap
|
| 220 |
+
http://www.freertos.org/a00111.html. */
|
| 221 |
+
for( ;; );
|
| 222 |
+
}
|
| 223 |
+
/*-----------------------------------------------------------*/
|
| 224 |
+
|
| 225 |
+
static void prvQueueSendTask( void *pvParameters )
|
| 226 |
+
{
|
| 227 |
+
TickType_t xNextWakeTime;
|
| 228 |
+
const unsigned long ulValueToSend = 100UL;
|
| 229 |
+
BaseType_t xReturned;
|
| 230 |
+
|
| 231 |
+
/* Remove compiler warning about unused parameter. */
|
| 232 |
+
( void ) pvParameters;
|
| 233 |
+
|
| 234 |
+
/* Initialise xNextWakeTime - this only needs to be done once. */
|
| 235 |
+
xNextWakeTime = xTaskGetTickCount();
|
| 236 |
+
|
| 237 |
+
for( ;; )
|
| 238 |
+
{
|
| 239 |
+
/* Place this task in the blocked state until it is time to run again. */
|
| 240 |
+
vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
|
| 241 |
+
|
| 242 |
+
/* Send to the queue - causing the queue receive task to unblock and
|
| 243 |
+
toggle the LED. 0 is used as the block time so the sending operation
|
| 244 |
+
will not block - it shouldn't need to block as the queue should always
|
| 245 |
+
be empty at this point in the code. */
|
| 246 |
+
xReturned = xQueueSend( xQueue, &ulValueToSend, 0U );
|
| 247 |
+
configASSERT( xReturned == pdPASS );
|
| 248 |
+
}
|
| 249 |
+
}
|
| 250 |
+
/*-----------------------------------------------------------*/
|
| 251 |
+
|
| 252 |
+
static void prvQueueReceiveTask( void *pvParameters )
|
| 253 |
+
{
|
| 254 |
+
unsigned long ulReceivedValue;
|
| 255 |
+
const unsigned long ulExpectedValue = 100UL;
|
| 256 |
+
char * const pcPassMessage = "Blink\r\n";
|
| 257 |
+
char * const pcFailMessage = "Unexpected value received\r\n";
|
| 258 |
+
|
| 259 |
+
/* Remove compiler warning about unused parameter. */
|
| 260 |
+
( void ) pvParameters;
|
| 261 |
+
|
| 262 |
+
for( ;; )
|
| 263 |
+
{
|
| 264 |
+
/* Wait until something arrives in the queue - this task will block
|
| 265 |
+
indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
|
| 266 |
+
FreeRTOSConfig.h. */
|
| 267 |
+
xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
|
| 268 |
+
|
| 269 |
+
/* To get here something must have been received from the queue, but
|
| 270 |
+
is it the expected value? If it is, toggle the LED. */
|
| 271 |
+
if( ulReceivedValue == ulExpectedValue )
|
| 272 |
+
{
|
| 273 |
+
uart_print( pcPassMessage );
|
| 274 |
+
ulReceivedValue = 0U;
|
| 275 |
+
}
|
| 276 |
+
else
|
| 277 |
+
{
|
| 278 |
+
uart_print( pcFailMessage );
|
| 279 |
+
}
|
| 280 |
+
}
|
| 281 |
+
}
|
| 282 |
+
/*-----------------------------------------------------------*/
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
|
AngeloJacobo_RISC-V/test/lib/clint.c
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
volatile uint32_t *mtime_low = (volatile uint32_t *) MTIME_BASE_ADDRESS;
|
| 6 |
+
volatile uint32_t *mtime_hi = (volatile uint32_t *) (MTIME_BASE_ADDRESS + 4);
|
| 7 |
+
volatile uint32_t *mtimecmp_low = (volatile uint32_t *) MTIMECMP_BASE_ADDRESS;
|
| 8 |
+
volatile uint32_t *mtimecmp_hi = (volatile uint32_t *) (MTIMECMP_BASE_ADDRESS + 4);
|
| 9 |
+
volatile uint32_t *software_interrupt = (volatile uint32_t *) MSIP_BASE_ADDRESS;
|
| 10 |
+
|
| 11 |
+
// Set current system time.
|
| 12 |
+
void mtime_set_time(uint64_t time) {
|
| 13 |
+
|
| 14 |
+
union {
|
| 15 |
+
uint64_t uint64;
|
| 16 |
+
uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
|
| 17 |
+
} time_union;
|
| 18 |
+
|
| 19 |
+
time_union.uint64 = time;
|
| 20 |
+
//set it up this way to not trigger false timer interrupt
|
| 21 |
+
*mtime_low = 0;
|
| 22 |
+
*mtime_hi = time_union.uint32[1];
|
| 23 |
+
*mtime_low = time_union.uint32[0];
|
| 24 |
+
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
// Get current system time.
|
| 29 |
+
uint64_t mtime_get_time(void) {
|
| 30 |
+
|
| 31 |
+
union {
|
| 32 |
+
uint64_t uint64;
|
| 33 |
+
uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
|
| 34 |
+
} time_union;
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
time_union.uint32[0] = *mtime_low;
|
| 38 |
+
time_union.uint32[1] = *mtime_hi;
|
| 39 |
+
|
| 40 |
+
return time_union.uint64;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
// Set compare time register (MTIMECMP) for generating interrupts.
|
| 45 |
+
void mtime_set_timecmp(uint64_t timecmp) {
|
| 46 |
+
|
| 47 |
+
union {
|
| 48 |
+
uint64_t uint64;
|
| 49 |
+
uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
|
| 50 |
+
} timecmp_union;
|
| 51 |
+
|
| 52 |
+
timecmp_union.uint64 = timecmp;
|
| 53 |
+
|
| 54 |
+
*mtimecmp_low = -1; // prevent MTIMECMP from temporarily becoming smaller than the lesser of the old and new values
|
| 55 |
+
*mtimecmp_hi = timecmp_union.uint32[1];
|
| 56 |
+
*mtimecmp_low = timecmp_union.uint32[0];
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
// Get compare time register (MTIMECMP).
|
| 61 |
+
uint64_t mtime_get_timecmp(void) {
|
| 62 |
+
|
| 63 |
+
union {
|
| 64 |
+
uint64_t uint64;
|
| 65 |
+
uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
|
| 66 |
+
} timecmp_union;
|
| 67 |
+
|
| 68 |
+
timecmp_union.uint32[0] = *mtimecmp_low;
|
| 69 |
+
timecmp_union.uint32[1] = *mtimecmp_hi;
|
| 70 |
+
|
| 71 |
+
return timecmp_union.uint64;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
//setup trap handler by setting MTVEC and initially disabling all interrupts
|
| 75 |
+
//NOTE: trap handler function MUST HAVE ATTRIBUTE INTERRUPT
|
| 76 |
+
void trap_handler_setup(void (*trap_handler)(void)) { //this is a pointer to a function with void arguments and returns void
|
| 77 |
+
csr_write(MTVEC,(uint32_t) trap_handler); //store the address of the function to MTVEC (the input is a pointer which is simply an address)
|
| 78 |
+
//disable all interrupts
|
| 79 |
+
csr_write(MSTATUS, 0);
|
| 80 |
+
csr_write(MIE, 0);
|
| 81 |
+
csr_write(MIP, 0);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// trurn on software interrupt
|
| 85 |
+
void enable_software_interrupt(void){
|
| 86 |
+
*software_interrupt = 1;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// turn off software interrupt
|
| 90 |
+
void disable_software_interrupt(void){
|
| 91 |
+
*software_interrupt = 0;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
// convert milliseconds input to cpu clock ticks
|
| 96 |
+
uint64_t ms_to_cpu_ticks (uint64_t ms){
|
| 97 |
+
uint64_t cpu_clk_ticks = ms*(CPU_CLK_HZ/1000);
|
| 98 |
+
return cpu_clk_ticks;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// convert milliseconds input to cpu clock ticks
|
| 102 |
+
uint64_t us_to_cpu_ticks (uint64_t us){
|
| 103 |
+
uint64_t cpu_clk_ticks = us*(CPU_CLK_HZ/1000000);
|
| 104 |
+
return cpu_clk_ticks;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// convert cpu clock ticks to us
|
| 108 |
+
uint32_t cpu_ticks_to_us (uint64_t ticks){
|
| 109 |
+
uint32_t us = (ticks*1000000)/CPU_CLK_HZ;
|
| 110 |
+
return us;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
// delay function based on milliseconds
|
| 115 |
+
void delay_ms(uint64_t ms) {
|
| 116 |
+
uint64_t initial_time = mtime_get_time();
|
| 117 |
+
uint64_t ms_in_ticks = ms_to_cpu_ticks(ms);
|
| 118 |
+
while ((initial_time + ms_in_ticks) > (uint64_t)mtime_get_time()){ //do nothing while delay has not yet passed
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// delay function based on microseconds
|
| 123 |
+
void delay_us(uint64_t us) {
|
| 124 |
+
uint64_t initial_time = mtime_get_time();
|
| 125 |
+
uint64_t us_in_ticks = us_to_cpu_ticks(us);
|
| 126 |
+
while ((initial_time + us_in_ticks) > (uint64_t)mtime_get_time()){ //do nothing while delay has not yet passed
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// delay function based on cpu clock tick
|
| 131 |
+
void delay_ticks(uint32_t ticks) {
|
| 132 |
+
uint64_t initial_time = mtime_get_time();
|
| 133 |
+
while ((initial_time + ticks) > (uint64_t)mtime_get_time()){ //do nothing while delay has not yet passed
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
AngeloJacobo_RISC-V/test/lib/gpio.c
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
volatile uint32_t *gpio_mode_reg = (volatile uint32_t *) GPIO_MODE;
|
| 5 |
+
volatile uint32_t *gpio_write_reg = (volatile uint32_t *) GPIO_WRITE;
|
| 6 |
+
volatile uint32_t *gpio_read_reg = (volatile uint32_t *) GPIO_READ;
|
| 7 |
+
|
| 8 |
+
//read mode setting of the GPIOs (read = 0, write = 1)
|
| 9 |
+
uint32_t gpio_read_mode(){
|
| 10 |
+
return *gpio_mode_reg;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
//set mode setting og the GPIOs (read = 0, write = 1)
|
| 14 |
+
void gpio_set_mode(uint32_t mode){
|
| 15 |
+
*gpio_mode_reg = mode;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
//write to GPIOs
|
| 19 |
+
void gpio_write(uint32_t write){
|
| 20 |
+
*gpio_write_reg = write;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
//read current write value of GPIOs
|
| 24 |
+
uint32_t gpio_write_value(){
|
| 25 |
+
return *gpio_write_reg;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
//read GPIO
|
| 29 |
+
uint32_t gpio_read(){
|
| 30 |
+
return *gpio_read_reg;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
//toggle a specific GPIO pin
|
| 34 |
+
void toggle_gpio(uint32_t pin_number){
|
| 35 |
+
gpio_set_mode_pin(pin_number, 1); //set pin to write mode
|
| 36 |
+
uint32_t value;
|
| 37 |
+
value = gpio_write_value(); //read current write value
|
| 38 |
+
gpio_write(value ^ (1<<pin_number)); //reverse the value of the pin
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
//write to a specific GPIO pin
|
| 42 |
+
void gpio_write_pin(uint32_t pin_number, uint32_t val){
|
| 43 |
+
gpio_set_mode_pin(pin_number, 1); //set pin to write mode
|
| 44 |
+
uint32_t value;
|
| 45 |
+
value = gpio_write_value(); //read current write value
|
| 46 |
+
if(val) gpio_write(value | (1<<pin_number)); //set the pin high
|
| 47 |
+
else gpio_write(value & (~(1<<pin_number))); //set the pin low
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
//read a specific GPIO pin
|
| 51 |
+
uint32_t gpio_read_pin(uint32_t pin_number){
|
| 52 |
+
gpio_set_mode_pin(pin_number, 0); //set pin to read mode
|
| 53 |
+
uint32_t value;
|
| 54 |
+
value = gpio_read();
|
| 55 |
+
if(value & (1<<pin_number)){
|
| 56 |
+
return 1;
|
| 57 |
+
}
|
| 58 |
+
else{
|
| 59 |
+
return 0;
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
//set mode setting of a single GPIO pin(read = 0, write = 1)
|
| 65 |
+
void gpio_set_mode_pin(uint32_t pin_number, uint32_t mode){
|
| 66 |
+
uint32_t all_modes = gpio_read_mode();
|
| 67 |
+
if(mode){ //write
|
| 68 |
+
gpio_set_mode(all_modes | (1<<pin_number));
|
| 69 |
+
}
|
| 70 |
+
else{ //read
|
| 71 |
+
gpio_set_mode(all_modes & (~(1<<pin_number)));
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
//measure pulse duration of a GPIO pin in us
|
| 76 |
+
uint32_t gpio_pulse_duration_us(uint32_t pin_number, uint32_t val){
|
| 77 |
+
uint64_t time;
|
| 78 |
+
|
| 79 |
+
while(gpio_read_pin(pin_number) != val); //wait until pin value becomes val
|
| 80 |
+
time = mtime_get_time(); //record time
|
| 81 |
+
while(gpio_read_pin(pin_number) == val);// wait until pin value changes
|
| 82 |
+
time = (uint32_t) (mtime_get_time() - time);
|
| 83 |
+
return cpu_ticks_to_us(time); // convert cpu clock ticks to us
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
AngeloJacobo_RISC-V/test/lib/hygro_pmod.c
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 4 |
+
/* */
|
| 5 |
+
/************************************************************************/
|
| 6 |
+
/* Author: Arthur Brown */
|
| 7 |
+
/* Copyright 2017, Digilent Inc. */
|
| 8 |
+
/************************************************************************/
|
| 9 |
+
/* File Description: */
|
| 10 |
+
/* This file defines functions for HYGROI2C */
|
| 11 |
+
/* */
|
| 12 |
+
/************************************************************************/
|
| 13 |
+
/* Revision History: */
|
| 14 |
+
/* */
|
| 15 |
+
/* 01/30/2017(ArtVVB): created */
|
| 16 |
+
/* */
|
| 17 |
+
/************************************************************************/
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
/* ------------------------------------------------------------ */
|
| 21 |
+
/* Include File Definitions */
|
| 22 |
+
/* ------------------------------------------------------------ */
|
| 23 |
+
|
| 24 |
+
#include <stdint.h>
|
| 25 |
+
#include <stdbool.h>
|
| 26 |
+
#include <rv32i.h>
|
| 27 |
+
/* ------------------------------------------------------------ */
|
| 28 |
+
/* Procedure Definitions */
|
| 29 |
+
/* ------------------------------------------------------------ */
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
/* ------------------------------------------------------------ */
|
| 34 |
+
/* HYGROI2C::writeRegI2C
|
| 35 |
+
**
|
| 36 |
+
** Synopsis:
|
| 37 |
+
** writeRegI2C(bConfig);
|
| 38 |
+
**
|
| 39 |
+
** Parameters:
|
| 40 |
+
** uint8_t bReg - the register address to be written to
|
| 41 |
+
** uint16_t bVal - the bytes to be written
|
| 42 |
+
**
|
| 43 |
+
** Return Values:
|
| 44 |
+
** void
|
| 45 |
+
**
|
| 46 |
+
** Errors:
|
| 47 |
+
** none
|
| 48 |
+
**
|
| 49 |
+
** Description:
|
| 50 |
+
** This function writes to a register over I2C.
|
| 51 |
+
**
|
| 52 |
+
*/
|
| 53 |
+
uint8_t hygroi2c_writeRegI2C(uint8_t bReg, uint16_t bVal)
|
| 54 |
+
{
|
| 55 |
+
uint8_t ack;
|
| 56 |
+
ack = i2c_write_address(HYGROI2C_I2C_ADDR<<1); // start i2c by writing slave address (returns slave ack)
|
| 57 |
+
i2c_write_byte(bReg); // write to slave (returns slave ack) (after i2c_write_address())
|
| 58 |
+
i2c_write_byte((bVal>>8)&0xff); // send upper byte
|
| 59 |
+
i2c_write_byte((bVal)&0xff); // send lower byte
|
| 60 |
+
i2c_stop(); // stop current i2c transaction
|
| 61 |
+
return ack;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/* ------------------------------------------------------------ */
|
| 65 |
+
/* HYGROI2C::readRegI2C
|
| 66 |
+
**
|
| 67 |
+
** Synopsis:
|
| 68 |
+
** readRegI2C(bReg, rVal, delay_ms);
|
| 69 |
+
**
|
| 70 |
+
** Parameters:
|
| 71 |
+
** uint8_t bReg - the register address to be written to
|
| 72 |
+
** uint16_t* rVal - the return location for the read bytes
|
| 73 |
+
** unsigned int delay_ms - the number of milliseconds required for the HYGRO to convert the desired data
|
| 74 |
+
**
|
| 75 |
+
** Return Values:
|
| 76 |
+
** bool success - whether valid data has been successfully captured
|
| 77 |
+
**
|
| 78 |
+
** Errors:
|
| 79 |
+
** failure on bad rVal pointer
|
| 80 |
+
**
|
| 81 |
+
** Description:
|
| 82 |
+
** This function reads a register over I2C.
|
| 83 |
+
**
|
| 84 |
+
*/
|
| 85 |
+
uint8_t hygroi2c_readRegI2C(uint8_t bReg, uint16_t *rVal, uint32_t delay_in_ms)
|
| 86 |
+
{
|
| 87 |
+
int n, i;
|
| 88 |
+
uint8_t ack;
|
| 89 |
+
char msg[20];
|
| 90 |
+
i2c_write_address(HYGROI2C_I2C_ADDR<<1); // start i2c by writing slave address (returns slave ack)
|
| 91 |
+
i2c_write_byte(bReg); // write to slave (returns slave ack) (after i2c_write_address())
|
| 92 |
+
if (delay_in_ms > 0)
|
| 93 |
+
delay_ms(delay_in_ms); // wait for conversion to complete
|
| 94 |
+
i2c_stop(); // stop current i2c transaction
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
ack = i2c_write_address(((HYGROI2C_I2C_ADDR<<1) | 0x01)); // start i2c by writing slave address (returns slave ack)
|
| 98 |
+
//read two bytes from slave
|
| 99 |
+
*rVal |= (uint16_t)i2c_read_byte(); //read a byte from the slave (after i2c_write_address())
|
| 100 |
+
*rVal <<= 8;
|
| 101 |
+
*rVal |= (uint16_t)i2c_read_byte(); //read a byte from the slave (after i2c_write_address())
|
| 102 |
+
i2c_stop(); // stop current i2c transaction
|
| 103 |
+
|
| 104 |
+
return ack;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
/* ------------------------------------------------------------ */
|
| 109 |
+
/* HYGROI2C::begin
|
| 110 |
+
**
|
| 111 |
+
** Synopsis:
|
| 112 |
+
** myHYGROI2C.begin();
|
| 113 |
+
**
|
| 114 |
+
** Parameters:
|
| 115 |
+
**
|
| 116 |
+
** Return Values:
|
| 117 |
+
** void
|
| 118 |
+
**
|
| 119 |
+
** Errors:
|
| 120 |
+
**
|
| 121 |
+
** Description:
|
| 122 |
+
** This function initializes the I2C interface #1 that is used to communicate with PmodAD2.
|
| 123 |
+
**
|
| 124 |
+
*/
|
| 125 |
+
void hygroi2c_begin()
|
| 126 |
+
{
|
| 127 |
+
uint8_t ack;
|
| 128 |
+
delay_ms(15);
|
| 129 |
+
ack = hygroi2c_writeRegI2C(HYGROI2C_CONFIG_REG, 0x00); // use non-sequential acquisition mode, all other config bits are default
|
| 130 |
+
if(!ack){
|
| 131 |
+
//uart_print("hygroi2c_begin() FAILED\n");
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
/* ------------------------------------------------------------ */
|
| 138 |
+
/* HYGROI2C::getTemperature
|
| 139 |
+
**
|
| 140 |
+
** Synopsis:
|
| 141 |
+
** myHYGROI2C.getTemperature();
|
| 142 |
+
**
|
| 143 |
+
** Parameters:
|
| 144 |
+
**
|
| 145 |
+
** Return Values:
|
| 146 |
+
** float deg_c - the temperature reading in degrees celsius
|
| 147 |
+
**
|
| 148 |
+
** Errors: - modify to manage read failures
|
| 149 |
+
**
|
| 150 |
+
** Description:
|
| 151 |
+
** This function captures a temperature reading from the Pmod HYGRO.
|
| 152 |
+
**
|
| 153 |
+
*/
|
| 154 |
+
float hygroi2c_getTemperature()
|
| 155 |
+
{
|
| 156 |
+
uint8_t ack;
|
| 157 |
+
uint16_t raw_t;
|
| 158 |
+
float deg_c;
|
| 159 |
+
ack = hygroi2c_readRegI2C(HYGROI2C_TMP_REG, &raw_t, 7); // conversion time for temperature at 14 bit resolution is 6.5 ms
|
| 160 |
+
deg_c = (float)raw_t / 0x10000;
|
| 161 |
+
deg_c *= 165.0;
|
| 162 |
+
deg_c -= 40.0; // conversion provided in reference manual
|
| 163 |
+
return deg_c;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
/* ------------------------------------------------------------ */
|
| 167 |
+
/* HYGROI2C::getHumidity
|
| 168 |
+
**
|
| 169 |
+
** Synopsis:
|
| 170 |
+
** HYGROI2C.getHumidity();
|
| 171 |
+
**
|
| 172 |
+
** Parameters:
|
| 173 |
+
**
|
| 174 |
+
** Return Values:
|
| 175 |
+
** float per_rh - the humidity reading in percent relative humidity.
|
| 176 |
+
**
|
| 177 |
+
** Errors: - modify to manage read failures
|
| 178 |
+
**
|
| 179 |
+
** Description:
|
| 180 |
+
** This function captures a humidity reading from the Pmod HYGRO.
|
| 181 |
+
**
|
| 182 |
+
*/
|
| 183 |
+
float hygroi2c_getHumidity() {
|
| 184 |
+
uint16_t raw_h;
|
| 185 |
+
float per_rh;
|
| 186 |
+
uint8_t ack;
|
| 187 |
+
ack = hygroi2c_readRegI2C(HYGROI2C_HUM_REG, &raw_h, 7); // conversion time for humidity at 14 bit resolution is 6.35 ms
|
| 188 |
+
per_rh = (float)raw_h / 0x10000;
|
| 189 |
+
per_rh *= 100.0; // conversion provided in reference manual
|
| 190 |
+
return per_rh;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
/* ------------------------------------------------------------ */
|
| 194 |
+
/* HYGROI2C::tempF2C
|
| 195 |
+
**
|
| 196 |
+
** Synopsis:
|
| 197 |
+
** HYGROI2C.tempF2C(deg_f);
|
| 198 |
+
**
|
| 199 |
+
** Parameters:
|
| 200 |
+
** float deg_f - the temperature in degrees fahrenheit
|
| 201 |
+
** Return Values:
|
| 202 |
+
** float deg_c - the temperature in degrees celsius
|
| 203 |
+
**
|
| 204 |
+
** Errors:
|
| 205 |
+
**
|
| 206 |
+
** Description:
|
| 207 |
+
** This function converts a fahrenheit temperature to celsius
|
| 208 |
+
**
|
| 209 |
+
*/
|
| 210 |
+
float hygroi2c_tempF2C(float deg_f)
|
| 211 |
+
{
|
| 212 |
+
return (deg_f - 32) / 1.8;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
/* ------------------------------------------------------------ */
|
| 216 |
+
/* HYGROI2C::tempC2F
|
| 217 |
+
**
|
| 218 |
+
** Synopsis:
|
| 219 |
+
** HYGROI2C.tempC2F(deg_c);
|
| 220 |
+
**
|
| 221 |
+
** Parameters:
|
| 222 |
+
** float deg_c - the temperature in degrees celsius
|
| 223 |
+
** Return Values:
|
| 224 |
+
** float deg_f - the temperature in degrees fahrenheit
|
| 225 |
+
**
|
| 226 |
+
** Errors:
|
| 227 |
+
**
|
| 228 |
+
** Description:
|
| 229 |
+
** This function converts a celsius temperature to fahrenheit
|
| 230 |
+
**
|
| 231 |
+
*/
|
| 232 |
+
float hygroi2c_tempC2F(float deg_c)
|
| 233 |
+
{
|
| 234 |
+
return deg_c * 1.8 + 32;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
AngeloJacobo_RISC-V/test/lib/i2c.c
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// [REPEATED START NOT SUPPORTED]
|
| 2 |
+
#include <stdint.h>
|
| 3 |
+
#include <rv32i.h>
|
| 4 |
+
|
| 5 |
+
volatile uint32_t *i2c_start = (volatile uint32_t *) I2C_START;
|
| 6 |
+
volatile uint32_t *i2c_write = (volatile uint32_t *) I2C_WRITE;
|
| 7 |
+
volatile uint32_t *i2c_busy = (volatile uint32_t *) I2C_BUSY;
|
| 8 |
+
volatile uint32_t *i2c_halt = (volatile uint32_t *) I2C_STOP;
|
| 9 |
+
volatile uint32_t *i2c_ack = (volatile uint32_t *) I2C_ACK;
|
| 10 |
+
volatile uint32_t *i2c_read_ready = (volatile uint32_t *) I2C_READ_DATA_READY;
|
| 11 |
+
volatile uint32_t *i2c_read = (volatile uint32_t *) I2C_READ;
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
// start i2c by writing slave address (returns slave ack)
|
| 15 |
+
uint8_t i2c_write_address(uint8_t addr){
|
| 16 |
+
uint8_t ack;
|
| 17 |
+
while(*i2c_busy); //stay here if busy
|
| 18 |
+
*i2c_start = addr; //write to i2c address of slave
|
| 19 |
+
while(*i2c_busy); //wait until write is finished
|
| 20 |
+
ack = *i2c_ack; //check if slave acknowledged
|
| 21 |
+
return ack;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// stop current i2c transaction
|
| 25 |
+
void i2c_stop(void){
|
| 26 |
+
while(*i2c_busy);
|
| 27 |
+
*i2c_halt = 0x01;
|
| 28 |
+
while(*i2c_busy);
|
| 29 |
+
*i2c_halt = 0x00; //set it back to zero in preparation for next transaction
|
| 30 |
+
delay_ticks(100);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
uint8_t i2c_write_byte(uint8_t data){
|
| 34 |
+
uint8_t ack;
|
| 35 |
+
while(*i2c_busy); //stay here if busy
|
| 36 |
+
*i2c_write = data; //write data byte to slave
|
| 37 |
+
while(*i2c_busy); //wait until write is finished
|
| 38 |
+
ack = *i2c_ack; //check if slave acknowledged
|
| 39 |
+
return ack;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
uint8_t i2c_read_byte(){ //read a byte from the slave (after i2c_write_address())
|
| 43 |
+
uint8_t read_data;
|
| 44 |
+
while(*i2c_busy);
|
| 45 |
+
while(*i2c_read_ready == 0){ //while read data is not yet available
|
| 46 |
+
}
|
| 47 |
+
read_data = *i2c_read; //retrieve data
|
| 48 |
+
};
|
| 49 |
+
|
AngeloJacobo_RISC-V/test/lib/lcd.c
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
// Source: https://deepbluembedded.com/interfacing-i2c-lcd-16x2-tutorial-with-pic-microcontrollers-mplab-xc8/
|
| 4 |
+
|
| 5 |
+
unsigned char RS, i2c_add, BackLight_State = LCD_BACKLIGHT;
|
| 6 |
+
|
| 7 |
+
void LCD_Init() //initialize LCD with proper routine
|
| 8 |
+
{
|
| 9 |
+
i2c_add = LCD_I2C_ADDR;
|
| 10 |
+
IO_Expander_Write(0x00);
|
| 11 |
+
delay_ms(30);
|
| 12 |
+
LCD_CMD(0x03);
|
| 13 |
+
delay_ms(5);
|
| 14 |
+
LCD_CMD(0x03);
|
| 15 |
+
delay_ms(5);
|
| 16 |
+
LCD_CMD(0x03);
|
| 17 |
+
delay_ms(5);
|
| 18 |
+
LCD_CMD(LCD_RETURN_HOME);
|
| 19 |
+
delay_ms(5);
|
| 20 |
+
LCD_CMD(0x20 | (LCD_TYPE << 2));
|
| 21 |
+
delay_ms(50);
|
| 22 |
+
LCD_CMD(LCD_TURN_ON);
|
| 23 |
+
delay_ms(50);
|
| 24 |
+
LCD_CMD(LCD_CLEAR);
|
| 25 |
+
delay_ms(50);
|
| 26 |
+
LCD_CMD(LCD_ENTRY_MODE_SET | LCD_RETURN_HOME);
|
| 27 |
+
delay_ms(50);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
void IO_Expander_Write(unsigned char Data)
|
| 31 |
+
{
|
| 32 |
+
uint8_t addr_ack, data_ack;
|
| 33 |
+
addr_ack = i2c_write_address(i2c_add<<1);
|
| 34 |
+
data_ack = i2c_write_byte(Data | BackLight_State);
|
| 35 |
+
i2c_stop();
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
void LCD_Write_4Bit(unsigned char Nibble)
|
| 39 |
+
{
|
| 40 |
+
// Get The RS Value To LSB OF Data
|
| 41 |
+
Nibble |= RS;
|
| 42 |
+
IO_Expander_Write(Nibble | 0x04);
|
| 43 |
+
IO_Expander_Write(Nibble & 0xFB);
|
| 44 |
+
delay_ms(50);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
void LCD_CMD(unsigned char CMD)
|
| 48 |
+
{
|
| 49 |
+
RS = 0; // Command Register Select
|
| 50 |
+
LCD_Write_4Bit(CMD & 0xF0);
|
| 51 |
+
LCD_Write_4Bit((CMD << 4) & 0xF0);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
void LCD_Write_Char(char Data)
|
| 55 |
+
{
|
| 56 |
+
RS = 1; // Data Register Select
|
| 57 |
+
LCD_Write_4Bit(Data & 0xF0);
|
| 58 |
+
LCD_Write_4Bit((Data << 4) & 0xF0);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
void LCD_Write_String(char* Str) //write string to LCD
|
| 62 |
+
{
|
| 63 |
+
for(int i=0; Str[i]!='\0'; i++)
|
| 64 |
+
LCD_Write_Char(Str[i]);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
void LCD_Set_Cursor(unsigned char ROW, unsigned char COL) //Set cursor where to start writing to LCD
|
| 68 |
+
{
|
| 69 |
+
switch(ROW)
|
| 70 |
+
{
|
| 71 |
+
case 2:
|
| 72 |
+
LCD_CMD(0xC0 + COL-1);
|
| 73 |
+
break;
|
| 74 |
+
case 3:
|
| 75 |
+
LCD_CMD(0x94 + COL-1);
|
| 76 |
+
break;
|
| 77 |
+
case 4:
|
| 78 |
+
LCD_CMD(0xD4 + COL-1);
|
| 79 |
+
break;
|
| 80 |
+
// Case 1
|
| 81 |
+
default:
|
| 82 |
+
LCD_CMD(0x80 + COL-1);
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
void Backlight(void) //turn on backlight (initially turned on)
|
| 87 |
+
{
|
| 88 |
+
BackLight_State = LCD_BACKLIGHT;
|
| 89 |
+
IO_Expander_Write(0);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
void noBacklight(void) //turn off backlight
|
| 93 |
+
{
|
| 94 |
+
BackLight_State = LCD_NOBACKLIGHT;
|
| 95 |
+
IO_Expander_Write(0);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
void LCD_SL(void)
|
| 99 |
+
{
|
| 100 |
+
LCD_CMD(0x18);
|
| 101 |
+
delay_ms(40);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
void LCD_SR(void)
|
| 105 |
+
{
|
| 106 |
+
LCD_CMD(0x1C);
|
| 107 |
+
delay_ms(40);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
void LCD_Clear(void)
|
| 111 |
+
{
|
| 112 |
+
LCD_CMD(0x01);
|
| 113 |
+
delay_ms(40);
|
| 114 |
+
}
|
| 115 |
+
|
AngeloJacobo_RISC-V/test/lib/printf.c
ADDED
|
@@ -0,0 +1,914 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 2 |
+
// \author (c) Marco Paland (info@paland.com)
|
| 3 |
+
// 2014-2019, PALANDesign Hannover, Germany
|
| 4 |
+
//
|
| 5 |
+
// \license The MIT License (MIT)
|
| 6 |
+
//
|
| 7 |
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 8 |
+
// of this software and associated documentation files (the "Software"), to deal
|
| 9 |
+
// in the Software without restriction, including without limitation the rights
|
| 10 |
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 11 |
+
// copies of the Software, and to permit persons to whom the Software is
|
| 12 |
+
// furnished to do so, subject to the following conditions:
|
| 13 |
+
//
|
| 14 |
+
// The above copyright notice and this permission notice shall be included in
|
| 15 |
+
// all copies or substantial portions of the Software.
|
| 16 |
+
//
|
| 17 |
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 19 |
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 20 |
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 21 |
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 22 |
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 23 |
+
// THE SOFTWARE.
|
| 24 |
+
//
|
| 25 |
+
// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
|
| 26 |
+
// embedded systems with a very limited resources. These routines are thread
|
| 27 |
+
// safe and reentrant!
|
| 28 |
+
// Use this instead of the bloated standard/newlib printf cause these use
|
| 29 |
+
// malloc for printf (and may not be thread safe).
|
| 30 |
+
//
|
| 31 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 32 |
+
|
| 33 |
+
#include <stdbool.h>
|
| 34 |
+
#include <stdint.h>
|
| 35 |
+
|
| 36 |
+
#include "rv32i.h"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
|
| 40 |
+
// printf_config.h header file
|
| 41 |
+
// default: undefined
|
| 42 |
+
#ifdef PRINTF_INCLUDE_CONFIG_H
|
| 43 |
+
#include "printf_config.h"
|
| 44 |
+
#endif
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
|
| 48 |
+
// numeric number including padded zeros (dynamically created on stack)
|
| 49 |
+
// default: 32 byte
|
| 50 |
+
#ifndef PRINTF_NTOA_BUFFER_SIZE
|
| 51 |
+
#define PRINTF_NTOA_BUFFER_SIZE 32U
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
// 'ftoa' conversion buffer size, this must be big enough to hold one converted
|
| 55 |
+
// float number including padded zeros (dynamically created on stack)
|
| 56 |
+
// default: 32 byte
|
| 57 |
+
#ifndef PRINTF_FTOA_BUFFER_SIZE
|
| 58 |
+
#define PRINTF_FTOA_BUFFER_SIZE 32U
|
| 59 |
+
#endif
|
| 60 |
+
|
| 61 |
+
// support for the floating point type (%f)
|
| 62 |
+
// default: activated
|
| 63 |
+
#ifndef PRINTF_DISABLE_SUPPORT_FLOAT
|
| 64 |
+
#define PRINTF_SUPPORT_FLOAT
|
| 65 |
+
#endif
|
| 66 |
+
|
| 67 |
+
// support for exponential floating point notation (%e/%g)
|
| 68 |
+
// default: activated
|
| 69 |
+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
|
| 70 |
+
#define PRINTF_SUPPORT_EXPONENTIAL
|
| 71 |
+
#endif
|
| 72 |
+
|
| 73 |
+
// define the default floating point precision
|
| 74 |
+
// default: 6 digits
|
| 75 |
+
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
|
| 76 |
+
#define PRINTF_DEFAULT_FLOAT_PRECISION 6U
|
| 77 |
+
#endif
|
| 78 |
+
|
| 79 |
+
// define the largest float suitable to print with %f
|
| 80 |
+
// default: 1e9
|
| 81 |
+
#ifndef PRINTF_MAX_FLOAT
|
| 82 |
+
#define PRINTF_MAX_FLOAT 1e9
|
| 83 |
+
#endif
|
| 84 |
+
|
| 85 |
+
// support for the long long types (%llu or %p)
|
| 86 |
+
// default: activated
|
| 87 |
+
#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
|
| 88 |
+
#define PRINTF_SUPPORT_LONG_LONG
|
| 89 |
+
#endif
|
| 90 |
+
|
| 91 |
+
// support for the ptrdiff_t type (%t)
|
| 92 |
+
// ptrdiff_t is normally defined in <stddef.h> as long or long long type
|
| 93 |
+
// default: activated
|
| 94 |
+
#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
|
| 95 |
+
#define PRINTF_SUPPORT_PTRDIFF_T
|
| 96 |
+
#endif
|
| 97 |
+
|
| 98 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 99 |
+
|
| 100 |
+
// internal flag definitions
|
| 101 |
+
#define FLAGS_ZEROPAD (1U << 0U)
|
| 102 |
+
#define FLAGS_LEFT (1U << 1U)
|
| 103 |
+
#define FLAGS_PLUS (1U << 2U)
|
| 104 |
+
#define FLAGS_SPACE (1U << 3U)
|
| 105 |
+
#define FLAGS_HASH (1U << 4U)
|
| 106 |
+
#define FLAGS_UPPERCASE (1U << 5U)
|
| 107 |
+
#define FLAGS_CHAR (1U << 6U)
|
| 108 |
+
#define FLAGS_SHORT (1U << 7U)
|
| 109 |
+
#define FLAGS_LONG (1U << 8U)
|
| 110 |
+
#define FLAGS_LONG_LONG (1U << 9U)
|
| 111 |
+
#define FLAGS_PRECISION (1U << 10U)
|
| 112 |
+
#define FLAGS_ADAPT_EXP (1U << 11U)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
// import float.h for DBL_MAX
|
| 116 |
+
#if defined(PRINTF_SUPPORT_FLOAT)
|
| 117 |
+
#include <float.h>
|
| 118 |
+
#endif
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
// output function type
|
| 122 |
+
typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
// wrapper (used as buffer) for output function type
|
| 126 |
+
typedef struct {
|
| 127 |
+
void (*fct)(char character, void* arg);
|
| 128 |
+
void* arg;
|
| 129 |
+
} out_fct_wrap_type;
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
// internal buffer output
|
| 133 |
+
static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)
|
| 134 |
+
{
|
| 135 |
+
if (idx < maxlen) {
|
| 136 |
+
((char*)buffer)[idx] = character;
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
// internal null output
|
| 142 |
+
static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen)
|
| 143 |
+
{
|
| 144 |
+
(void)character; (void)buffer; (void)idx; (void)maxlen;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
// internal _putchar wrapper
|
| 149 |
+
static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen)
|
| 150 |
+
{
|
| 151 |
+
(void)buffer; (void)idx; (void)maxlen;
|
| 152 |
+
if (character) {
|
| 153 |
+
_putchar(character);
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
// internal output function wrapper
|
| 159 |
+
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen)
|
| 160 |
+
{
|
| 161 |
+
(void)idx; (void)maxlen;
|
| 162 |
+
if (character) {
|
| 163 |
+
// buffer is the output fct pointer
|
| 164 |
+
((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg);
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
// internal secure strlen
|
| 170 |
+
// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
|
| 171 |
+
static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
|
| 172 |
+
{
|
| 173 |
+
const char* s;
|
| 174 |
+
for (s = str; *s && maxsize--; ++s);
|
| 175 |
+
return (unsigned int)(s - str);
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
// internal test if char is a digit (0-9)
|
| 180 |
+
// \return true if char is a digit
|
| 181 |
+
static inline bool _is_digit(char ch)
|
| 182 |
+
{
|
| 183 |
+
return (ch >= '0') && (ch <= '9');
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
// internal ASCII string to unsigned int conversion
|
| 188 |
+
static unsigned int _atoi(const char** str)
|
| 189 |
+
{
|
| 190 |
+
unsigned int i = 0U;
|
| 191 |
+
while (_is_digit(**str)) {
|
| 192 |
+
i = i * 10U + (unsigned int)(*((*str)++) - '0');
|
| 193 |
+
}
|
| 194 |
+
return i;
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
// output the specified string in reverse, taking care of any zero-padding
|
| 199 |
+
static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)
|
| 200 |
+
{
|
| 201 |
+
const size_t start_idx = idx;
|
| 202 |
+
|
| 203 |
+
// pad spaces up to given width
|
| 204 |
+
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
|
| 205 |
+
for (size_t i = len; i < width; i++) {
|
| 206 |
+
out(' ', buffer, idx++, maxlen);
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// reverse string
|
| 211 |
+
while (len) {
|
| 212 |
+
out(buf[--len], buffer, idx++, maxlen);
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
// append pad spaces up to given width
|
| 216 |
+
if (flags & FLAGS_LEFT) {
|
| 217 |
+
while (idx - start_idx < width) {
|
| 218 |
+
out(' ', buffer, idx++, maxlen);
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return idx;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
// internal itoa format
|
| 227 |
+
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
|
| 228 |
+
{
|
| 229 |
+
// pad leading zeros
|
| 230 |
+
if (!(flags & FLAGS_LEFT)) {
|
| 231 |
+
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
| 232 |
+
width--;
|
| 233 |
+
}
|
| 234 |
+
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
| 235 |
+
buf[len++] = '0';
|
| 236 |
+
}
|
| 237 |
+
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
| 238 |
+
buf[len++] = '0';
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
// handle hash
|
| 243 |
+
if (flags & FLAGS_HASH) {
|
| 244 |
+
if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
|
| 245 |
+
len--;
|
| 246 |
+
if (len && (base == 16U)) {
|
| 247 |
+
len--;
|
| 248 |
+
}
|
| 249 |
+
}
|
| 250 |
+
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
| 251 |
+
buf[len++] = 'x';
|
| 252 |
+
}
|
| 253 |
+
else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
| 254 |
+
buf[len++] = 'X';
|
| 255 |
+
}
|
| 256 |
+
else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
| 257 |
+
buf[len++] = 'b';
|
| 258 |
+
}
|
| 259 |
+
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
| 260 |
+
buf[len++] = '0';
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
| 265 |
+
if (negative) {
|
| 266 |
+
buf[len++] = '-';
|
| 267 |
+
}
|
| 268 |
+
else if (flags & FLAGS_PLUS) {
|
| 269 |
+
buf[len++] = '+'; // ignore the space if the '+' exists
|
| 270 |
+
}
|
| 271 |
+
else if (flags & FLAGS_SPACE) {
|
| 272 |
+
buf[len++] = ' ';
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
// internal itoa for 'long' type
|
| 281 |
+
static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
|
| 282 |
+
{
|
| 283 |
+
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
| 284 |
+
size_t len = 0U;
|
| 285 |
+
|
| 286 |
+
// no hash for 0 values
|
| 287 |
+
if (!value) {
|
| 288 |
+
flags &= ~FLAGS_HASH;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
// write if precision != 0 and value is != 0
|
| 292 |
+
if (!(flags & FLAGS_PRECISION) || value) {
|
| 293 |
+
do {
|
| 294 |
+
const char digit = (char)(value % base);
|
| 295 |
+
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
|
| 296 |
+
value /= base;
|
| 297 |
+
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
// internal itoa for 'long long' type
|
| 305 |
+
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
| 306 |
+
static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
|
| 307 |
+
{
|
| 308 |
+
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
| 309 |
+
size_t len = 0U;
|
| 310 |
+
|
| 311 |
+
// no hash for 0 values
|
| 312 |
+
if (!value) {
|
| 313 |
+
flags &= ~FLAGS_HASH;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
// write if precision != 0 and value is != 0
|
| 317 |
+
if (!(flags & FLAGS_PRECISION) || value) {
|
| 318 |
+
do {
|
| 319 |
+
const char digit = (char)(value % base);
|
| 320 |
+
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
|
| 321 |
+
value /= base;
|
| 322 |
+
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
|
| 326 |
+
}
|
| 327 |
+
#endif // PRINTF_SUPPORT_LONG_LONG
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
#if defined(PRINTF_SUPPORT_FLOAT)
|
| 331 |
+
|
| 332 |
+
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
| 333 |
+
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
| 334 |
+
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);
|
| 335 |
+
#endif
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
// internal ftoa for fixed decimal floating point
|
| 339 |
+
static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
|
| 340 |
+
{
|
| 341 |
+
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
| 342 |
+
size_t len = 0U;
|
| 343 |
+
double diff = 0.0;
|
| 344 |
+
|
| 345 |
+
// powers of 10
|
| 346 |
+
static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
| 347 |
+
|
| 348 |
+
// test for special values
|
| 349 |
+
if (value != value)
|
| 350 |
+
return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
|
| 351 |
+
if (value < -DBL_MAX)
|
| 352 |
+
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
|
| 353 |
+
if (value > DBL_MAX)
|
| 354 |
+
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
|
| 355 |
+
|
| 356 |
+
// test for very large values
|
| 357 |
+
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
|
| 358 |
+
if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
|
| 359 |
+
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
| 360 |
+
return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
|
| 361 |
+
#else
|
| 362 |
+
return 0U;
|
| 363 |
+
#endif
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// test for negative
|
| 367 |
+
bool negative = false;
|
| 368 |
+
if (value < 0) {
|
| 369 |
+
negative = true;
|
| 370 |
+
value = 0 - value;
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
// set default precision, if not set explicitly
|
| 374 |
+
if (!(flags & FLAGS_PRECISION)) {
|
| 375 |
+
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
| 376 |
+
}
|
| 377 |
+
// limit precision to 9, cause a prec >= 10 can lead to overflow errors
|
| 378 |
+
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
|
| 379 |
+
buf[len++] = '0';
|
| 380 |
+
prec--;
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
+
int whole = (int)value;
|
| 384 |
+
double tmp = (value - whole) * pow10[prec];
|
| 385 |
+
unsigned long frac = (unsigned long)tmp;
|
| 386 |
+
diff = tmp - frac;
|
| 387 |
+
|
| 388 |
+
if (diff > 0.5) {
|
| 389 |
+
++frac;
|
| 390 |
+
// handle rollover, e.g. case 0.99 with prec 1 is 1.0
|
| 391 |
+
if (frac >= pow10[prec]) {
|
| 392 |
+
frac = 0;
|
| 393 |
+
++whole;
|
| 394 |
+
}
|
| 395 |
+
}
|
| 396 |
+
else if (diff < 0.5) {
|
| 397 |
+
}
|
| 398 |
+
else if ((frac == 0U) || (frac & 1U)) {
|
| 399 |
+
// if halfway, round up if odd OR if last digit is 0
|
| 400 |
+
++frac;
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
if (prec == 0U) {
|
| 404 |
+
diff = value - (double)whole;
|
| 405 |
+
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
|
| 406 |
+
// exactly 0.5 and ODD, then round up
|
| 407 |
+
// 1.5 -> 2, but 2.5 -> 2
|
| 408 |
+
++whole;
|
| 409 |
+
}
|
| 410 |
+
}
|
| 411 |
+
else {
|
| 412 |
+
unsigned int count = prec;
|
| 413 |
+
// now do fractional part, as an unsigned number
|
| 414 |
+
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
| 415 |
+
--count;
|
| 416 |
+
buf[len++] = (char)(48U + (frac % 10U));
|
| 417 |
+
if (!(frac /= 10U)) {
|
| 418 |
+
break;
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
// add extra 0s
|
| 422 |
+
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
|
| 423 |
+
buf[len++] = '0';
|
| 424 |
+
}
|
| 425 |
+
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
| 426 |
+
// add decimal
|
| 427 |
+
buf[len++] = '.';
|
| 428 |
+
}
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
// do whole part, number is reversed
|
| 432 |
+
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
| 433 |
+
buf[len++] = (char)(48 + (whole % 10));
|
| 434 |
+
if (!(whole /= 10)) {
|
| 435 |
+
break;
|
| 436 |
+
}
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
// pad leading zeros
|
| 440 |
+
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
|
| 441 |
+
if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
| 442 |
+
width--;
|
| 443 |
+
}
|
| 444 |
+
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
|
| 445 |
+
buf[len++] = '0';
|
| 446 |
+
}
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
| 450 |
+
if (negative) {
|
| 451 |
+
buf[len++] = '-';
|
| 452 |
+
}
|
| 453 |
+
else if (flags & FLAGS_PLUS) {
|
| 454 |
+
buf[len++] = '+'; // ignore the space if the '+' exists
|
| 455 |
+
}
|
| 456 |
+
else if (flags & FLAGS_SPACE) {
|
| 457 |
+
buf[len++] = ' ';
|
| 458 |
+
}
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
| 466 |
+
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
| 467 |
+
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
|
| 468 |
+
{
|
| 469 |
+
// check for NaN and special values
|
| 470 |
+
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
| 471 |
+
return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
// determine the sign
|
| 475 |
+
const bool negative = value < 0;
|
| 476 |
+
if (negative) {
|
| 477 |
+
value = -value;
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
// default precision
|
| 481 |
+
if (!(flags & FLAGS_PRECISION)) {
|
| 482 |
+
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
// determine the decimal exponent
|
| 486 |
+
// based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
|
| 487 |
+
union {
|
| 488 |
+
uint64_t U;
|
| 489 |
+
double F;
|
| 490 |
+
} conv;
|
| 491 |
+
|
| 492 |
+
conv.F = value;
|
| 493 |
+
int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2
|
| 494 |
+
conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
|
| 495 |
+
// now approximate log10 from the log2 integer part and an expansion of ln around 1.5
|
| 496 |
+
int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
|
| 497 |
+
// now we want to compute 10^expval but we want to be sure it won't overflow
|
| 498 |
+
exp2 = (int)(expval * 3.321928094887362 + 0.5);
|
| 499 |
+
const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
|
| 500 |
+
const double z2 = z * z;
|
| 501 |
+
conv.U = (uint64_t)(exp2 + 1023) << 52U;
|
| 502 |
+
// compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
|
| 503 |
+
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
|
| 504 |
+
// correct for rounding errors
|
| 505 |
+
if (value < conv.F) {
|
| 506 |
+
expval--;
|
| 507 |
+
conv.F /= 10;
|
| 508 |
+
}
|
| 509 |
+
|
| 510 |
+
// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
|
| 511 |
+
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
|
| 512 |
+
|
| 513 |
+
// in "%g" mode, "prec" is the number of *significant figures* not decimals
|
| 514 |
+
if (flags & FLAGS_ADAPT_EXP) {
|
| 515 |
+
// do we want to fall-back to "%f" mode?
|
| 516 |
+
if ((value >= 1e-4) && (value < 1e6)) {
|
| 517 |
+
if ((int)prec > expval) {
|
| 518 |
+
prec = (unsigned)((int)prec - expval - 1);
|
| 519 |
+
}
|
| 520 |
+
else {
|
| 521 |
+
prec = 0;
|
| 522 |
+
}
|
| 523 |
+
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
|
| 524 |
+
// no characters in exponent
|
| 525 |
+
minwidth = 0U;
|
| 526 |
+
expval = 0;
|
| 527 |
+
}
|
| 528 |
+
else {
|
| 529 |
+
// we use one sigfig for the whole part
|
| 530 |
+
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
|
| 531 |
+
--prec;
|
| 532 |
+
}
|
| 533 |
+
}
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
// will everything fit?
|
| 537 |
+
unsigned int fwidth = width;
|
| 538 |
+
if (width > minwidth) {
|
| 539 |
+
// we didn't fall-back so subtract the characters required for the exponent
|
| 540 |
+
fwidth -= minwidth;
|
| 541 |
+
} else {
|
| 542 |
+
// not enough characters, so go back to default sizing
|
| 543 |
+
fwidth = 0U;
|
| 544 |
+
}
|
| 545 |
+
if ((flags & FLAGS_LEFT) && minwidth) {
|
| 546 |
+
// if we're padding on the right, DON'T pad the floating part
|
| 547 |
+
fwidth = 0U;
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
// rescale the float value
|
| 551 |
+
if (expval) {
|
| 552 |
+
value /= conv.F;
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
// output the floating part
|
| 556 |
+
const size_t start_idx = idx;
|
| 557 |
+
idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
|
| 558 |
+
|
| 559 |
+
// output the exponent part
|
| 560 |
+
if (minwidth) {
|
| 561 |
+
// output the exponential symbol
|
| 562 |
+
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
|
| 563 |
+
// output the exponent value
|
| 564 |
+
idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);
|
| 565 |
+
// might need to right-pad spaces
|
| 566 |
+
if (flags & FLAGS_LEFT) {
|
| 567 |
+
while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
|
| 568 |
+
}
|
| 569 |
+
}
|
| 570 |
+
return idx;
|
| 571 |
+
}
|
| 572 |
+
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
| 573 |
+
#endif // PRINTF_SUPPORT_FLOAT
|
| 574 |
+
|
| 575 |
+
|
| 576 |
+
// internal vsnprintf
|
| 577 |
+
static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
|
| 578 |
+
{
|
| 579 |
+
unsigned int flags, width, precision, n;
|
| 580 |
+
size_t idx = 0U;
|
| 581 |
+
|
| 582 |
+
if (!buffer) {
|
| 583 |
+
// use null output function
|
| 584 |
+
out = _out_null;
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
while (*format)
|
| 588 |
+
{
|
| 589 |
+
// format specifier? %[flags][width][.precision][length]
|
| 590 |
+
if (*format != '%') {
|
| 591 |
+
// no
|
| 592 |
+
out(*format, buffer, idx++, maxlen);
|
| 593 |
+
format++;
|
| 594 |
+
continue;
|
| 595 |
+
}
|
| 596 |
+
else {
|
| 597 |
+
// yes, evaluate it
|
| 598 |
+
format++;
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
// evaluate flags
|
| 602 |
+
flags = 0U;
|
| 603 |
+
do {
|
| 604 |
+
switch (*format) {
|
| 605 |
+
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
|
| 606 |
+
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
|
| 607 |
+
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
|
| 608 |
+
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
|
| 609 |
+
case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
|
| 610 |
+
default : n = 0U; break;
|
| 611 |
+
}
|
| 612 |
+
} while (n);
|
| 613 |
+
|
| 614 |
+
// evaluate width field
|
| 615 |
+
width = 0U;
|
| 616 |
+
if (_is_digit(*format)) {
|
| 617 |
+
width = _atoi(&format);
|
| 618 |
+
}
|
| 619 |
+
else if (*format == '*') {
|
| 620 |
+
const int w = va_arg(va, int);
|
| 621 |
+
if (w < 0) {
|
| 622 |
+
flags |= FLAGS_LEFT; // reverse padding
|
| 623 |
+
width = (unsigned int)-w;
|
| 624 |
+
}
|
| 625 |
+
else {
|
| 626 |
+
width = (unsigned int)w;
|
| 627 |
+
}
|
| 628 |
+
format++;
|
| 629 |
+
}
|
| 630 |
+
|
| 631 |
+
// evaluate precision field
|
| 632 |
+
precision = 0U;
|
| 633 |
+
if (*format == '.') {
|
| 634 |
+
flags |= FLAGS_PRECISION;
|
| 635 |
+
format++;
|
| 636 |
+
if (_is_digit(*format)) {
|
| 637 |
+
precision = _atoi(&format);
|
| 638 |
+
}
|
| 639 |
+
else if (*format == '*') {
|
| 640 |
+
const int prec = (int)va_arg(va, int);
|
| 641 |
+
precision = prec > 0 ? (unsigned int)prec : 0U;
|
| 642 |
+
format++;
|
| 643 |
+
}
|
| 644 |
+
}
|
| 645 |
+
|
| 646 |
+
// evaluate length field
|
| 647 |
+
switch (*format) {
|
| 648 |
+
case 'l' :
|
| 649 |
+
flags |= FLAGS_LONG;
|
| 650 |
+
format++;
|
| 651 |
+
if (*format == 'l') {
|
| 652 |
+
flags |= FLAGS_LONG_LONG;
|
| 653 |
+
format++;
|
| 654 |
+
}
|
| 655 |
+
break;
|
| 656 |
+
case 'h' :
|
| 657 |
+
flags |= FLAGS_SHORT;
|
| 658 |
+
format++;
|
| 659 |
+
if (*format == 'h') {
|
| 660 |
+
flags |= FLAGS_CHAR;
|
| 661 |
+
format++;
|
| 662 |
+
}
|
| 663 |
+
break;
|
| 664 |
+
#if defined(PRINTF_SUPPORT_PTRDIFF_T)
|
| 665 |
+
case 't' :
|
| 666 |
+
flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
| 667 |
+
format++;
|
| 668 |
+
break;
|
| 669 |
+
#endif
|
| 670 |
+
case 'j' :
|
| 671 |
+
flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
| 672 |
+
format++;
|
| 673 |
+
break;
|
| 674 |
+
case 'z' :
|
| 675 |
+
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
| 676 |
+
format++;
|
| 677 |
+
break;
|
| 678 |
+
default :
|
| 679 |
+
break;
|
| 680 |
+
}
|
| 681 |
+
|
| 682 |
+
// evaluate specifier
|
| 683 |
+
switch (*format) {
|
| 684 |
+
case 'd' :
|
| 685 |
+
case 'i' :
|
| 686 |
+
case 'u' :
|
| 687 |
+
case 'x' :
|
| 688 |
+
case 'X' :
|
| 689 |
+
case 'o' :
|
| 690 |
+
case 'b' : {
|
| 691 |
+
// set the base
|
| 692 |
+
unsigned int base;
|
| 693 |
+
if (*format == 'x' || *format == 'X') {
|
| 694 |
+
base = 16U;
|
| 695 |
+
}
|
| 696 |
+
else if (*format == 'o') {
|
| 697 |
+
base = 8U;
|
| 698 |
+
}
|
| 699 |
+
else if (*format == 'b') {
|
| 700 |
+
base = 2U;
|
| 701 |
+
}
|
| 702 |
+
else {
|
| 703 |
+
base = 10U;
|
| 704 |
+
flags &= ~FLAGS_HASH; // no hash for dec format
|
| 705 |
+
}
|
| 706 |
+
// uppercase
|
| 707 |
+
if (*format == 'X') {
|
| 708 |
+
flags |= FLAGS_UPPERCASE;
|
| 709 |
+
}
|
| 710 |
+
|
| 711 |
+
// no plus or space flag for u, x, X, o, b
|
| 712 |
+
if ((*format != 'i') && (*format != 'd')) {
|
| 713 |
+
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
|
| 714 |
+
}
|
| 715 |
+
|
| 716 |
+
// ignore '0' flag when precision is given
|
| 717 |
+
if (flags & FLAGS_PRECISION) {
|
| 718 |
+
flags &= ~FLAGS_ZEROPAD;
|
| 719 |
+
}
|
| 720 |
+
|
| 721 |
+
// convert the integer
|
| 722 |
+
if ((*format == 'i') || (*format == 'd')) {
|
| 723 |
+
// signed
|
| 724 |
+
if (flags & FLAGS_LONG_LONG) {
|
| 725 |
+
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
| 726 |
+
const long long value = va_arg(va, long long);
|
| 727 |
+
idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
| 728 |
+
#endif
|
| 729 |
+
}
|
| 730 |
+
else if (flags & FLAGS_LONG) {
|
| 731 |
+
const long value = va_arg(va, long);
|
| 732 |
+
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
| 733 |
+
}
|
| 734 |
+
else {
|
| 735 |
+
const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int);
|
| 736 |
+
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
| 737 |
+
}
|
| 738 |
+
}
|
| 739 |
+
else {
|
| 740 |
+
// unsigned
|
| 741 |
+
if (flags & FLAGS_LONG_LONG) {
|
| 742 |
+
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
| 743 |
+
idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
|
| 744 |
+
#endif
|
| 745 |
+
}
|
| 746 |
+
else if (flags & FLAGS_LONG) {
|
| 747 |
+
idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
|
| 748 |
+
}
|
| 749 |
+
else {
|
| 750 |
+
const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
|
| 751 |
+
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
|
| 752 |
+
}
|
| 753 |
+
}
|
| 754 |
+
format++;
|
| 755 |
+
break;
|
| 756 |
+
}
|
| 757 |
+
#if defined(PRINTF_SUPPORT_FLOAT)
|
| 758 |
+
case 'f' :
|
| 759 |
+
case 'F' :
|
| 760 |
+
if (*format == 'F') flags |= FLAGS_UPPERCASE;
|
| 761 |
+
idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
|
| 762 |
+
format++;
|
| 763 |
+
break;
|
| 764 |
+
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
| 765 |
+
case 'e':
|
| 766 |
+
case 'E':
|
| 767 |
+
case 'g':
|
| 768 |
+
case 'G':
|
| 769 |
+
if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP;
|
| 770 |
+
if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE;
|
| 771 |
+
idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
|
| 772 |
+
format++;
|
| 773 |
+
break;
|
| 774 |
+
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
| 775 |
+
#endif // PRINTF_SUPPORT_FLOAT
|
| 776 |
+
case 'c' : {
|
| 777 |
+
unsigned int l = 1U;
|
| 778 |
+
// pre padding
|
| 779 |
+
if (!(flags & FLAGS_LEFT)) {
|
| 780 |
+
while (l++ < width) {
|
| 781 |
+
out(' ', buffer, idx++, maxlen);
|
| 782 |
+
}
|
| 783 |
+
}
|
| 784 |
+
// char output
|
| 785 |
+
out((char)va_arg(va, int), buffer, idx++, maxlen);
|
| 786 |
+
// post padding
|
| 787 |
+
if (flags & FLAGS_LEFT) {
|
| 788 |
+
while (l++ < width) {
|
| 789 |
+
out(' ', buffer, idx++, maxlen);
|
| 790 |
+
}
|
| 791 |
+
}
|
| 792 |
+
format++;
|
| 793 |
+
break;
|
| 794 |
+
}
|
| 795 |
+
|
| 796 |
+
case 's' : {
|
| 797 |
+
const char* p = va_arg(va, char*);
|
| 798 |
+
unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
|
| 799 |
+
// pre padding
|
| 800 |
+
if (flags & FLAGS_PRECISION) {
|
| 801 |
+
l = (l < precision ? l : precision);
|
| 802 |
+
}
|
| 803 |
+
if (!(flags & FLAGS_LEFT)) {
|
| 804 |
+
while (l++ < width) {
|
| 805 |
+
out(' ', buffer, idx++, maxlen);
|
| 806 |
+
}
|
| 807 |
+
}
|
| 808 |
+
// string output
|
| 809 |
+
while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
|
| 810 |
+
out(*(p++), buffer, idx++, maxlen);
|
| 811 |
+
}
|
| 812 |
+
// post padding
|
| 813 |
+
if (flags & FLAGS_LEFT) {
|
| 814 |
+
while (l++ < width) {
|
| 815 |
+
out(' ', buffer, idx++, maxlen);
|
| 816 |
+
}
|
| 817 |
+
}
|
| 818 |
+
format++;
|
| 819 |
+
break;
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
case 'p' : {
|
| 823 |
+
width = sizeof(void*) * 2U;
|
| 824 |
+
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
|
| 825 |
+
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
| 826 |
+
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
|
| 827 |
+
if (is_ll) {
|
| 828 |
+
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
|
| 829 |
+
}
|
| 830 |
+
else {
|
| 831 |
+
#endif
|
| 832 |
+
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
|
| 833 |
+
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
| 834 |
+
}
|
| 835 |
+
#endif
|
| 836 |
+
format++;
|
| 837 |
+
break;
|
| 838 |
+
}
|
| 839 |
+
|
| 840 |
+
case '%' :
|
| 841 |
+
out('%', buffer, idx++, maxlen);
|
| 842 |
+
format++;
|
| 843 |
+
break;
|
| 844 |
+
|
| 845 |
+
default :
|
| 846 |
+
out(*format, buffer, idx++, maxlen);
|
| 847 |
+
format++;
|
| 848 |
+
break;
|
| 849 |
+
}
|
| 850 |
+
}
|
| 851 |
+
|
| 852 |
+
// termination
|
| 853 |
+
out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
|
| 854 |
+
|
| 855 |
+
// return written chars without terminating \0
|
| 856 |
+
return (int)idx;
|
| 857 |
+
}
|
| 858 |
+
|
| 859 |
+
|
| 860 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 861 |
+
|
| 862 |
+
int printf_(const char* format, ...)
|
| 863 |
+
{
|
| 864 |
+
va_list va;
|
| 865 |
+
va_start(va, format);
|
| 866 |
+
char buffer[1];
|
| 867 |
+
const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
|
| 868 |
+
va_end(va);
|
| 869 |
+
return ret;
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
|
| 873 |
+
int sprintf_(char* buffer, const char* format, ...)
|
| 874 |
+
{
|
| 875 |
+
va_list va;
|
| 876 |
+
va_start(va, format);
|
| 877 |
+
const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va);
|
| 878 |
+
va_end(va);
|
| 879 |
+
return ret;
|
| 880 |
+
}
|
| 881 |
+
|
| 882 |
+
|
| 883 |
+
int snprintf_(char* buffer, size_t count, const char* format, ...)
|
| 884 |
+
{
|
| 885 |
+
va_list va;
|
| 886 |
+
va_start(va, format);
|
| 887 |
+
const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
|
| 888 |
+
va_end(va);
|
| 889 |
+
return ret;
|
| 890 |
+
}
|
| 891 |
+
|
| 892 |
+
|
| 893 |
+
int vprintf_(const char* format, va_list va)
|
| 894 |
+
{
|
| 895 |
+
char buffer[1];
|
| 896 |
+
return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
|
| 897 |
+
}
|
| 898 |
+
|
| 899 |
+
|
| 900 |
+
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
|
| 901 |
+
{
|
| 902 |
+
return _vsnprintf(_out_buffer, buffer, count, format, va);
|
| 903 |
+
}
|
| 904 |
+
|
| 905 |
+
|
| 906 |
+
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)
|
| 907 |
+
{
|
| 908 |
+
va_list va;
|
| 909 |
+
va_start(va, format);
|
| 910 |
+
const out_fct_wrap_type out_fct_wrap = { out, arg };
|
| 911 |
+
const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va);
|
| 912 |
+
va_end(va);
|
| 913 |
+
return ret;
|
| 914 |
+
}
|
AngeloJacobo_RISC-V/test/lib/rv32i.h
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
|
| 3 |
+
// I2C memory-mapped registers
|
| 4 |
+
#define I2C_START 0x800000A0
|
| 5 |
+
#define I2C_WRITE 0x800000A4
|
| 6 |
+
#define I2C_READ 0x800000A8
|
| 7 |
+
#define I2C_BUSY 0x800000AC
|
| 8 |
+
#define I2C_ACK 0x800000B0
|
| 9 |
+
#define I2C_READ_DATA_READY 0x800000B4
|
| 10 |
+
#define I2C_STOP 0x800000B8
|
| 11 |
+
|
| 12 |
+
// UART memory-mapped registers
|
| 13 |
+
#define UART_TX_DATA 0x80000050
|
| 14 |
+
#define UART_TX_BUSY 0x80000054
|
| 15 |
+
#define UART_RX_BUFFER_FULL 0x80000058
|
| 16 |
+
#define UART_RX_DATA 0x8000005C
|
| 17 |
+
|
| 18 |
+
//GPIO memory-mapped registers
|
| 19 |
+
#define GPIO_MODE 0x800000F0
|
| 20 |
+
#define GPIO_READ 0x800000F4
|
| 21 |
+
#define GPIO_WRITE 0x800000F8
|
| 22 |
+
|
| 23 |
+
// CLINT memory-mapped registers
|
| 24 |
+
#define CPU_CLK_HZ 12000000
|
| 25 |
+
#define MTIME_BASE_ADDRESS 0x80000000
|
| 26 |
+
#define MTIMECMP_BASE_ADDRESS 0x80000008
|
| 27 |
+
#define MSIP_BASE_ADDRESS 0x80000010
|
| 28 |
+
|
| 29 |
+
// Registers used in HygroPMOD
|
| 30 |
+
#define HYGROI2C_I2C_ADDR 0x40
|
| 31 |
+
#define HYGROI2C_TMP_REG 0x00
|
| 32 |
+
#define HYGROI2C_HUM_REG 0x01
|
| 33 |
+
#define HYGROI2C_CONFIG_REG 0x02
|
| 34 |
+
|
| 35 |
+
// Control Status Registers
|
| 36 |
+
#define MARCHID 0xF12
|
| 37 |
+
#define MIMPID 0xF13
|
| 38 |
+
#define MHARTID 0xF14
|
| 39 |
+
#define MSTATUS 0x300
|
| 40 |
+
#define MISA 0x301
|
| 41 |
+
#define MIE 0x304
|
| 42 |
+
#define MTVEC 0x305
|
| 43 |
+
#define MSCRATCH 0x340
|
| 44 |
+
#define MEPC 0x341
|
| 45 |
+
#define MCAUSE 0x342
|
| 46 |
+
#define MTVAL 0x343
|
| 47 |
+
#define MIP 0x344
|
| 48 |
+
#define MCYCLE 0xB00
|
| 49 |
+
#define MCYCLEH 0xB80
|
| 50 |
+
#define TIME 0xC01
|
| 51 |
+
#define TIMEH 0xC81
|
| 52 |
+
#define MINSTRET 0xB02
|
| 53 |
+
#define MINSTRETH 0xBB2
|
| 54 |
+
#define MCOUNTINHIBIT 0x320
|
| 55 |
+
|
| 56 |
+
#define MSTATUS_MIE 3
|
| 57 |
+
#define MIP_MSIP 3
|
| 58 |
+
#define MIP_MTIP 7
|
| 59 |
+
#define MIP_MEIP 11
|
| 60 |
+
#define MIE_MSIE 3
|
| 61 |
+
#define MIE_MTIE 7
|
| 62 |
+
#define MIE_MEIE 11
|
| 63 |
+
|
| 64 |
+
// LCD cpnfigurations
|
| 65 |
+
#define LCD_I2C_ADDR 0x27
|
| 66 |
+
#define LCD_BACKLIGHT 0x08
|
| 67 |
+
#define LCD_NOBACKLIGHT 0x00
|
| 68 |
+
#define LCD_FIRST_ROW 0x80
|
| 69 |
+
#define LCD_SECOND_ROW 0xC0
|
| 70 |
+
#define LCD_THIRD_ROW 0x94
|
| 71 |
+
#define LCD_FOURTH_ROW 0xD4
|
| 72 |
+
#define LCD_CLEAR 0x01
|
| 73 |
+
#define LCD_RETURN_HOME 0x02
|
| 74 |
+
#define LCD_ENTRY_MODE_SET 0x04
|
| 75 |
+
#define LCD_CURSOR_OFF 0x0C
|
| 76 |
+
#define LCD_UNDERLINE_ON 0x0E
|
| 77 |
+
#define LCD_BLINK_CURSOR_ON 0x0F
|
| 78 |
+
#define LCD_MOVE_CURSOR_LEFT 0x10
|
| 79 |
+
#define LCD_MOVE_CURSOR_RIGHT 0x14
|
| 80 |
+
#define LCD_TURN_ON 0x0C
|
| 81 |
+
#define LCD_TURN_OFF 0x08
|
| 82 |
+
#define LCD_SHIFT_LEFT 0x18
|
| 83 |
+
#define LCD_SHIFT_RIGHT 0x1E
|
| 84 |
+
#define LCD_TYPE 2 // 0 -> 5x7 | 1 -> 5x10 | 2 -> 2 lines
|
| 85 |
+
|
| 86 |
+
// Function prototypes for clint.c
|
| 87 |
+
void mtime_set_time(uint64_t time); // set current system time.
|
| 88 |
+
uint64_t mtime_get_time(void) ; // return current system time.
|
| 89 |
+
void mtime_set_timecmp(uint64_t timecmp); // set compare time register (generates timer interrupts when mtime>=mtimecmp)
|
| 90 |
+
uint64_t mtime_get_timecmp(void); // Get compare time register
|
| 91 |
+
void trap_handler_setup(void (*trap_handler)(void)); //setup trap handler by setting MTVEC and initially disabling all interrupts (NOTE: trap handler function MUST HAVE ATTRIBUTE INTERRUPT)
|
| 92 |
+
void enable_software_interrupt(void); // trurn on software interrupt
|
| 93 |
+
void disable_software_interrupt(void); // turn off software interrupt
|
| 94 |
+
uint64_t ms_to_cpu_ticks (uint64_t ms); // convert milliseconds input to cpu clock ticks
|
| 95 |
+
void delay_ms(uint64_t ms); // delay function based on milliseconds
|
| 96 |
+
void delay_ticks(uint32_t ticks); // delay function based on cpu clock tick
|
| 97 |
+
void delay_us(uint64_t us); // delay function based on microseconds
|
| 98 |
+
uint32_t cpu_ticks_to_us (uint64_t ticks); // convert cpu clock ticks to us
|
| 99 |
+
|
| 100 |
+
// Inline functions go to header file
|
| 101 |
+
static inline void __attribute__ ((always_inline)) csr_set(const int csr_id, uint32_t mask) { // set bits in CSR
|
| 102 |
+
uint32_t csr_data = mask;
|
| 103 |
+
asm volatile ("csrs %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
|
| 104 |
+
}
|
| 105 |
+
inline void __attribute__ ((always_inline)) csr_write(const int csr_id, uint32_t data) { // write to csr
|
| 106 |
+
uint32_t csr_data = data;
|
| 107 |
+
asm volatile ("csrw %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Function prototypes for i2c.c [[REPEATED START NOT SUPPORTED]]
|
| 111 |
+
uint8_t i2c_write_address(uint8_t addr); // start i2c by writing slave address (returns slave ack)
|
| 112 |
+
void i2c_stop(void); // stop current i2c transaction
|
| 113 |
+
uint8_t i2c_write_byte(uint8_t data); // write to slave (returns slave ack) (after i2c_write_address())
|
| 114 |
+
uint8_t i2c_read_byte(); //read a byte from the slave (after i2c_write_address())
|
| 115 |
+
|
| 116 |
+
// Function prototypes for uart.c
|
| 117 |
+
void uart_print(char *message); // print characters serially via UART
|
| 118 |
+
int uart_rx_buffer_full(); //check if read buffer is full and data can be read
|
| 119 |
+
char uart_read(); //read data from buffer (make sure to check first if rx buffer is full)
|
| 120 |
+
|
| 121 |
+
// Function prototypes for gpio.c
|
| 122 |
+
void toggle_gpio(uint32_t pin_number); //toggle a specific GPIO pin (automatically set pin to write mode)
|
| 123 |
+
void gpio_set_mode_pin(uint32_t pin_number, uint32_t mode); //set mode setting of a single GPIO pin(read = 0, write = 1)
|
| 124 |
+
void gpio_write_pin(uint32_t pin_number, uint32_t val); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 125 |
+
uint32_t gpio_read_pin(uint32_t pin_number); //read a specific GPIO pin
|
| 126 |
+
uint32_t gpio_pulse_duration_us(uint32_t pin_number, uint32_t val); //measure pulse duration of a GPIO pin in us
|
| 127 |
+
uint32_t gpio_read_mode(); //read mode setting of the GPIOs (read = 0, write = 1)
|
| 128 |
+
void gpio_set_mode(uint32_t mode); //set mode setting og the GPIOs (read = 0, write = 1)
|
| 129 |
+
void gpio_write(uint32_t write); //write to GPIOs
|
| 130 |
+
uint32_t gpio_write_value(); //read current write value of GPIOs
|
| 131 |
+
uint32_t gpio_read(); //read GPIO
|
| 132 |
+
|
| 133 |
+
// Function prototypes for lcd.c
|
| 134 |
+
void LCD_Init(); //initialize LCD with proper routine
|
| 135 |
+
void LCD_Set_Cursor(unsigned char ROW, unsigned char COL); //Set cursor where to start writing to LCD
|
| 136 |
+
void LCD_Write_String(char*); //write string to LCD
|
| 137 |
+
void Backlight(void); //turn on backlight (initially turned on)
|
| 138 |
+
void noBacklight(void); //turn off backlight
|
| 139 |
+
void IO_Expander_Write(unsigned char Data);
|
| 140 |
+
void LCD_Write_4Bit(unsigned char Nibble);
|
| 141 |
+
void LCD_CMD(unsigned char CMD);
|
| 142 |
+
void LCD_Write_Char(char);
|
| 143 |
+
void LCD_SR(void);
|
| 144 |
+
void LCD_SL(void);
|
| 145 |
+
void LCD_Clear(void);
|
| 146 |
+
|
| 147 |
+
// Function prototypes for hygro_pmod.c
|
| 148 |
+
float hygroi2c_getTemperature(); //captures a temperature reading from the Pmod HYGRO
|
| 149 |
+
float hygroi2c_getHumidity(); //captures a humidity reading from the Pmod HYGRO
|
| 150 |
+
void hygroi2c_begin(); //initializes the Hygro I2C interface (must be done before every temp and humidity measurement)
|
| 151 |
+
uint8_t hygroi2c_writeRegI2C(uint8_t bReg, uint16_t bVal);
|
| 152 |
+
uint8_t hygroi2c_readRegI2C(uint8_t bReg, uint16_t *rVal, uint32_t delay_in_ms);
|
| 153 |
+
float hygroi2c_tempC2F(float tempC);
|
| 154 |
+
float hygroi2c_tempF2C(float tempF);
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
// Function prototypes for ultrasonic_sensor.c
|
| 158 |
+
int ultrasonic_sensor_cm(int trig_pin, int echo_pin); // returns distance in cm detected by the ultrasonic sensor
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
// Header file for prinf.c Sourced from: https://github.com/mpaland/printf
|
| 162 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 163 |
+
// \author (c) Marco Paland (info@paland.com)
|
| 164 |
+
// 2014-2019, PALANDesign Hannover, Germany
|
| 165 |
+
//
|
| 166 |
+
// \license The MIT License (MIT)
|
| 167 |
+
//
|
| 168 |
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 169 |
+
// of this software and associated documentation files (the "Software"), to deal
|
| 170 |
+
// in the Software without restriction, including without limitation the rights
|
| 171 |
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 172 |
+
// copies of the Software, and to permit persons to whom the Software is
|
| 173 |
+
// furnished to do so, subject to the following conditions:
|
| 174 |
+
//
|
| 175 |
+
// The above copyright notice and this permission notice shall be included in
|
| 176 |
+
// all copies or substantial portions of the Software.
|
| 177 |
+
//
|
| 178 |
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 179 |
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 180 |
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 181 |
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 182 |
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 183 |
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 184 |
+
// THE SOFTWARE.
|
| 185 |
+
//
|
| 186 |
+
// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on
|
| 187 |
+
// embedded systems with a very limited resources.
|
| 188 |
+
// Use this instead of bloated standard/newlib printf.
|
| 189 |
+
// These routines are thread safe and reentrant.
|
| 190 |
+
//
|
| 191 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 192 |
+
|
| 193 |
+
#ifndef _PRINTF_H_
|
| 194 |
+
#define _PRINTF_H_
|
| 195 |
+
|
| 196 |
+
#include <stdarg.h>
|
| 197 |
+
#include <stddef.h>
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
#ifdef __cplusplus
|
| 201 |
+
extern "C" {
|
| 202 |
+
#endif
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
/**
|
| 206 |
+
* Output a character to a custom device like UART, used by the printf() function
|
| 207 |
+
* This function is declared here only. You have to write your custom implementation somewhere
|
| 208 |
+
* \param character Character to output
|
| 209 |
+
*/
|
| 210 |
+
void _putchar(char character);
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
/**
|
| 214 |
+
* Tiny printf implementation
|
| 215 |
+
* You have to implement _putchar if you use printf()
|
| 216 |
+
* To avoid conflicts with the regular printf() API it is overridden by macro defines
|
| 217 |
+
* and internal underscore-appended functions like printf_() are used
|
| 218 |
+
* \param format A string that specifies the format of the output
|
| 219 |
+
* \return The number of characters that are written into the array, not counting the terminating null character
|
| 220 |
+
*/
|
| 221 |
+
#define printf printf_
|
| 222 |
+
int printf_(const char* format, ...);
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
/**
|
| 226 |
+
* Tiny sprintf implementation
|
| 227 |
+
* Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!
|
| 228 |
+
* \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!
|
| 229 |
+
* \param format A string that specifies the format of the output
|
| 230 |
+
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
|
| 231 |
+
*/
|
| 232 |
+
#define sprintf sprintf_
|
| 233 |
+
int sprintf_(char* buffer, const char* format, ...);
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
/**
|
| 237 |
+
* Tiny snprintf/vsnprintf implementation
|
| 238 |
+
* \param buffer A pointer to the buffer where to store the formatted string
|
| 239 |
+
* \param count The maximum number of characters to store in the buffer, including a terminating null character
|
| 240 |
+
* \param format A string that specifies the format of the output
|
| 241 |
+
* \param va A value identifying a variable arguments list
|
| 242 |
+
* \return The number of characters that COULD have been written into the buffer, not counting the terminating
|
| 243 |
+
* null character. A value equal or larger than count indicates truncation. Only when the returned value
|
| 244 |
+
* is non-negative and less than count, the string has been completely written.
|
| 245 |
+
*/
|
| 246 |
+
#define snprintf snprintf_
|
| 247 |
+
#define vsnprintf vsnprintf_
|
| 248 |
+
int snprintf_(char* buffer, size_t count, const char* format, ...);
|
| 249 |
+
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
/**
|
| 253 |
+
* Tiny vprintf implementation
|
| 254 |
+
* \param format A string that specifies the format of the output
|
| 255 |
+
* \param va A value identifying a variable arguments list
|
| 256 |
+
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
|
| 257 |
+
*/
|
| 258 |
+
#define vprintf vprintf_
|
| 259 |
+
int vprintf_(const char* format, va_list va);
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
/**
|
| 263 |
+
* printf with output function
|
| 264 |
+
* You may use this as dynamic alternative to printf() with its fixed _putchar() output
|
| 265 |
+
* \param out An output function which takes one character and an argument pointer
|
| 266 |
+
* \param arg An argument pointer for user data passed to output function
|
| 267 |
+
* \param format A string that specifies the format of the output
|
| 268 |
+
* \return The number of characters that are sent to the output function, not counting the terminating null character
|
| 269 |
+
*/
|
| 270 |
+
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
#ifdef __cplusplus
|
| 274 |
+
}
|
| 275 |
+
#endif
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
#endif // _PRINTF_H_
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
|
AngeloJacobo_RISC-V/test/lib/uart.c
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
volatile uint32_t *uart_tx_data = (volatile uint32_t *) UART_TX_DATA;
|
| 5 |
+
volatile uint32_t *uart_tx_busy = (volatile uint32_t *) UART_TX_BUSY;
|
| 6 |
+
volatile uint32_t *uart_rx_full = (volatile uint32_t *) UART_RX_BUFFER_FULL;
|
| 7 |
+
volatile uint32_t *uart_rx_data = (volatile uint32_t *) UART_RX_DATA;
|
| 8 |
+
|
| 9 |
+
// print characters serially via UART
|
| 10 |
+
void uart_print(char *message) {
|
| 11 |
+
int i = 0;
|
| 12 |
+
while (message[i] != '\0') {
|
| 13 |
+
while (*uart_tx_busy); // wait for UART to be ready
|
| 14 |
+
*uart_tx_data = message[i];
|
| 15 |
+
i++;
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
//check if read buffer is full and data can be read
|
| 20 |
+
int uart_rx_buffer_full(){
|
| 21 |
+
int ready = *uart_rx_full;
|
| 22 |
+
return ready;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
//read data from buffer (make sure to check first if rx buffer is full)
|
| 26 |
+
char uart_read(){
|
| 27 |
+
char read_data;
|
| 28 |
+
read_data = *uart_rx_data;
|
| 29 |
+
return read_data;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
AngeloJacobo_RISC-V/test/lib/ultrasonic_sensor.c
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <stdint.h>
|
| 2 |
+
#include <rv32i.h>
|
| 3 |
+
|
| 4 |
+
// returns distance in cm detected by the ultrasonic sensor
|
| 5 |
+
int ultrasonic_sensor_cm(int trig_pin, int echo_pin){
|
| 6 |
+
int pulse_duration_us;
|
| 7 |
+
int distance_cm;
|
| 8 |
+
|
| 9 |
+
gpio_set_mode_pin(trig_pin, 1); //set mode setting of a single GPIO pin(read = 0, write = 1)
|
| 10 |
+
gpio_set_mode_pin(echo_pin, 0); //set mode setting of a single GPIO pin(read = 0, write = 1)
|
| 11 |
+
|
| 12 |
+
// set trig_pin for 10us
|
| 13 |
+
gpio_write_pin(trig_pin, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 14 |
+
delay_us(2); // delay function based on microseconds
|
| 15 |
+
gpio_write_pin(trig_pin, 1); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 16 |
+
delay_us(10); // delay function based on microseconds
|
| 17 |
+
gpio_write_pin(trig_pin, 0); //write to a specific GPIO pin (automatically set pin to write mode)
|
| 18 |
+
|
| 19 |
+
pulse_duration_us = gpio_pulse_duration_us(echo_pin, 1); //measure how long will be the high pulse
|
| 20 |
+
distance_cm = pulse_duration_us*(0.034/2);
|
| 21 |
+
return distance_cm;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
AngeloJacobo_RISC-V/test/rv32i_soc.v
ADDED
|
@@ -0,0 +1,1357 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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+
//`define ICARUS use faster UARt and I2C rate for faster simulation
|
| 6 |
+
|
| 7 |
+
//complete package containing the rv32i_core, RAM, and IO peripherals (I2C and UART)
|
| 8 |
+
module rv32i_soc #(parameter CLK_FREQ_MHZ=12, PC_RESET=32'h00_00_00_00, TRAP_ADDRESS=32'h00_00_00_00, ZICSR_EXTENSION=1, MEMORY_DEPTH=81920, GPIO_COUNT = 12) (
|
| 9 |
+
input wire i_clk,
|
| 10 |
+
input wire i_rst,
|
| 11 |
+
//UART
|
| 12 |
+
input wire uart_rx,
|
| 13 |
+
output wire uart_tx,
|
| 14 |
+
//I2C
|
| 15 |
+
inout wire i2c_sda,
|
| 16 |
+
inout wire i2c_scl,
|
| 17 |
+
//GPIO
|
| 18 |
+
inout wire[GPIO_COUNT-1:0] gpio_pins
|
| 19 |
+
);
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
//Instruction Memory Interface
|
| 23 |
+
wire[31:0] inst;
|
| 24 |
+
wire[31:0] iaddr;
|
| 25 |
+
wire i_stb_inst;
|
| 26 |
+
wire o_ack_inst;
|
| 27 |
+
|
| 28 |
+
//Data Memory Interface
|
| 29 |
+
wire[31:0] i_wb_data_data; //data retrieved from memory
|
| 30 |
+
wire[31:0] o_wb_data_data; //data to be stored to memory
|
| 31 |
+
wire[31:0] wb_addr_data; //address of data memory for store/load
|
| 32 |
+
wire[3:0] wb_sel_data; //byte strobe for write (1 = write the byte) {byte3,byte2,byte1,byte0}
|
| 33 |
+
wire wb_we_data; //write-enable (1 = write, 0 = read)
|
| 34 |
+
wire wb_stb_data; //request for read/write access to data memory
|
| 35 |
+
wire wb_ack_data; //ack by data memory (high when data to be read is ready or when write data is already written
|
| 36 |
+
wire wb_cyc_data; //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 37 |
+
wire wb_stall_data; //stall by data memory
|
| 38 |
+
|
| 39 |
+
//Interrupts
|
| 40 |
+
wire i_external_interrupt = 0; //interrupt from external source
|
| 41 |
+
wire o_timer_interrupt; //interrupt from CLINT
|
| 42 |
+
wire o_software_interrupt; //interrupt from CLINT
|
| 43 |
+
|
| 44 |
+
//Memory Wrapper
|
| 45 |
+
wire device0_wb_cyc;
|
| 46 |
+
wire device0_wb_stb;
|
| 47 |
+
wire device0_wb_we;
|
| 48 |
+
wire[31:0] device0_wb_addr;
|
| 49 |
+
wire[31:0] o_device0_wb_data;
|
| 50 |
+
wire[3:0] device0_wb_sel;
|
| 51 |
+
wire device0_wb_ack;
|
| 52 |
+
wire device0_wb_stall;
|
| 53 |
+
wire[31:0] i_device0_wb_data;
|
| 54 |
+
|
| 55 |
+
wire device1_wb_cyc;
|
| 56 |
+
wire device1_wb_stb;
|
| 57 |
+
wire device1_wb_we;
|
| 58 |
+
wire[31:0] device1_wb_addr;
|
| 59 |
+
wire[31:0] o_device1_wb_data;
|
| 60 |
+
wire[3:0] device1_wb_sel;
|
| 61 |
+
wire device1_wb_ack;
|
| 62 |
+
wire device1_wb_stall;
|
| 63 |
+
wire[31:0] i_device1_wb_data;
|
| 64 |
+
|
| 65 |
+
wire device2_wb_cyc;
|
| 66 |
+
wire device2_wb_stb;
|
| 67 |
+
wire device2_wb_we;
|
| 68 |
+
wire[31:0] device2_wb_addr;
|
| 69 |
+
wire[31:0] o_device2_wb_data;
|
| 70 |
+
wire[3:0] device2_wb_sel;
|
| 71 |
+
wire device2_wb_ack;
|
| 72 |
+
wire device2_wb_stall;
|
| 73 |
+
wire[31:0] i_device2_wb_data;
|
| 74 |
+
|
| 75 |
+
wire device3_wb_cyc;
|
| 76 |
+
wire device3_wb_stb;
|
| 77 |
+
wire device3_wb_we;
|
| 78 |
+
wire[31:0] device3_wb_addr;
|
| 79 |
+
wire[31:0] o_device3_wb_data;
|
| 80 |
+
wire[3:0] device3_wb_sel;
|
| 81 |
+
wire device3_wb_ack;
|
| 82 |
+
wire device3_wb_stall;
|
| 83 |
+
wire[31:0] i_device3_wb_data;
|
| 84 |
+
|
| 85 |
+
wire device4_wb_cyc;
|
| 86 |
+
wire device4_wb_stb;
|
| 87 |
+
wire device4_wb_we;
|
| 88 |
+
wire[31:0] device4_wb_addr;
|
| 89 |
+
wire[31:0] o_device4_wb_data;
|
| 90 |
+
wire[3:0] device4_wb_sel;
|
| 91 |
+
wire device4_wb_ack;
|
| 92 |
+
wire device4_wb_stall;
|
| 93 |
+
wire[31:0] i_device4_wb_data;
|
| 94 |
+
|
| 95 |
+
wire device5_wb_cyc;
|
| 96 |
+
wire device5_wb_stb;
|
| 97 |
+
wire device5_wb_we;
|
| 98 |
+
wire[31:0] device5_wb_addr;
|
| 99 |
+
wire[31:0] o_device5_wb_data;
|
| 100 |
+
wire[3:0] device5_wb_sel;
|
| 101 |
+
wire device5_wb_ack;
|
| 102 |
+
wire device5_wb_stall;
|
| 103 |
+
wire[31:0] i_device5_wb_data;
|
| 104 |
+
|
| 105 |
+
rv32i_core #(.PC_RESET(PC_RESET), .TRAP_ADDRESS(TRAP_ADDRESS), .ZICSR_EXTENSION(ZICSR_EXTENSION)) m0( //main RV32I core
|
| 106 |
+
.i_clk(i_clk),
|
| 107 |
+
.i_rst_n(!i_rst),
|
| 108 |
+
//Instruction Memory Interface
|
| 109 |
+
.i_inst(inst), //32-bit instruction
|
| 110 |
+
.o_iaddr(iaddr), //address of instruction
|
| 111 |
+
.o_stb_inst(i_stb_inst), //request for read access to instruction memory
|
| 112 |
+
.i_ack_inst(o_ack_inst), //ack (high if new instruction is ready)
|
| 113 |
+
//Data Memory Interface
|
| 114 |
+
.o_wb_cyc_data(wb_cyc_data), //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 115 |
+
.o_wb_stb_data(wb_stb_data), //request for read/write access to data memory
|
| 116 |
+
.o_wb_we_data(wb_we_data), //write-enable (1 = write, 0 = read)
|
| 117 |
+
.o_wb_addr_data(wb_addr_data), //address of data memory for store/load
|
| 118 |
+
.o_wb_data_data(o_wb_data_data), //data to be stored to memory
|
| 119 |
+
.o_wb_sel_data(wb_sel_data), //byte strobe for write (1 = write the byte) {byte3,byte2,byte1,byte0}
|
| 120 |
+
.i_wb_ack_data(wb_ack_data), //ack by data memory (high when read data is ready or when write data is already written)
|
| 121 |
+
.i_wb_stall_data(wb_stall_data), //stall by data memory
|
| 122 |
+
.i_wb_data_data(i_wb_data_data), //data retrieved from memory
|
| 123 |
+
//Interrupts
|
| 124 |
+
.i_external_interrupt(i_external_interrupt), //interrupt from external source
|
| 125 |
+
.i_software_interrupt(o_software_interrupt), //interrupt from software (inter-processor interrupt)
|
| 126 |
+
.i_timer_interrupt(o_timer_interrupt) //interrupt from timer
|
| 127 |
+
);
|
| 128 |
+
|
| 129 |
+
memory_wrapper wrapper( //decodes address and access the corresponding memory-mapped device
|
| 130 |
+
//RISC-V Core
|
| 131 |
+
.i_wb_cyc(wb_cyc_data),
|
| 132 |
+
.i_wb_stb(wb_stb_data),
|
| 133 |
+
.i_wb_we(wb_we_data),
|
| 134 |
+
.i_wb_addr(wb_addr_data),
|
| 135 |
+
.i_wb_data(o_wb_data_data),
|
| 136 |
+
.i_wb_sel(wb_sel_data),
|
| 137 |
+
.o_wb_ack(wb_ack_data),
|
| 138 |
+
.o_wb_stall(wb_stall_data),
|
| 139 |
+
.o_wb_data(i_wb_data_data),
|
| 140 |
+
|
| 141 |
+
//Device 0 Interface (RAM)
|
| 142 |
+
.o_device0_wb_cyc(device0_wb_cyc),
|
| 143 |
+
.o_device0_wb_stb(device0_wb_stb),
|
| 144 |
+
.o_device0_wb_we(device0_wb_we),
|
| 145 |
+
.o_device0_wb_addr(device0_wb_addr),
|
| 146 |
+
.o_device0_wb_data(o_device0_wb_data),
|
| 147 |
+
.o_device0_wb_sel(device0_wb_sel),
|
| 148 |
+
.i_device0_wb_ack(device0_wb_ack),
|
| 149 |
+
.i_device0_wb_stall(device0_wb_stall),
|
| 150 |
+
.i_device0_wb_data(i_device0_wb_data),
|
| 151 |
+
|
| 152 |
+
//Device 1 Interface (CLINT)
|
| 153 |
+
.o_device1_wb_cyc(device1_wb_cyc),
|
| 154 |
+
.o_device1_wb_stb(device1_wb_stb),
|
| 155 |
+
.o_device1_wb_we(device1_wb_we),
|
| 156 |
+
.o_device1_wb_addr(device1_wb_addr),
|
| 157 |
+
.o_device1_wb_data(o_device1_wb_data),
|
| 158 |
+
.o_device1_wb_sel(device1_wb_sel),
|
| 159 |
+
.i_device1_wb_ack(device1_wb_ack),
|
| 160 |
+
.i_device1_wb_stall(device1_wb_stall),
|
| 161 |
+
.i_device1_wb_data(i_device1_wb_data),
|
| 162 |
+
|
| 163 |
+
//Device 2 Interface (UART)
|
| 164 |
+
.o_device2_wb_cyc(device2_wb_cyc),
|
| 165 |
+
.o_device2_wb_stb(device2_wb_stb),
|
| 166 |
+
.o_device2_wb_we(device2_wb_we),
|
| 167 |
+
.o_device2_wb_addr(device2_wb_addr),
|
| 168 |
+
.o_device2_wb_data(o_device2_wb_data),
|
| 169 |
+
.o_device2_wb_sel(device2_wb_sel),
|
| 170 |
+
.i_device2_wb_ack(device2_wb_ack),
|
| 171 |
+
.i_device2_wb_stall(device2_wb_stall),
|
| 172 |
+
.i_device2_wb_data(i_device2_wb_data),
|
| 173 |
+
|
| 174 |
+
//Device 3 Interface (I2C)
|
| 175 |
+
.o_device3_wb_cyc(device3_wb_cyc),
|
| 176 |
+
.o_device3_wb_stb(device3_wb_stb),
|
| 177 |
+
.o_device3_wb_we(device3_wb_we),
|
| 178 |
+
.o_device3_wb_addr(device3_wb_addr),
|
| 179 |
+
.o_device3_wb_data(o_device3_wb_data),
|
| 180 |
+
.o_device3_wb_sel(device3_wb_sel),
|
| 181 |
+
.i_device3_wb_ack(device3_wb_ack),
|
| 182 |
+
.i_device3_wb_stall(device3_wb_stall),
|
| 183 |
+
.i_device3_wb_data(i_device3_wb_data),
|
| 184 |
+
|
| 185 |
+
//Device 4 Interface (GPIO)
|
| 186 |
+
.o_device4_wb_cyc(device4_wb_cyc),
|
| 187 |
+
.o_device4_wb_stb(device4_wb_stb),
|
| 188 |
+
.o_device4_wb_we(device4_wb_we),
|
| 189 |
+
.o_device4_wb_addr(device4_wb_addr),
|
| 190 |
+
.o_device4_wb_data(o_device4_wb_data),
|
| 191 |
+
.o_device4_wb_sel(device4_wb_sel),
|
| 192 |
+
.i_device4_wb_ack(device4_wb_ack),
|
| 193 |
+
.i_device4_wb_stall(device4_wb_stall),
|
| 194 |
+
.i_device4_wb_data(i_device4_wb_data),
|
| 195 |
+
|
| 196 |
+
//Device 5 Interface (DDR3)
|
| 197 |
+
.o_device5_wb_cyc(device5_wb_cyc),
|
| 198 |
+
.o_device5_wb_stb(device5_wb_stb),
|
| 199 |
+
.o_device5_wb_we(device5_wb_we),
|
| 200 |
+
.o_device5_wb_addr(device5_wb_addr),
|
| 201 |
+
.o_device5_wb_data(o_device5_wb_data),
|
| 202 |
+
.o_device5_wb_sel(device5_wb_sel),
|
| 203 |
+
.i_device5_wb_ack(device5_wb_ack),
|
| 204 |
+
.i_device5_wb_stall(device5_wb_stall),
|
| 205 |
+
.i_device5_wb_data(i_device5_wb_data)
|
| 206 |
+
);
|
| 207 |
+
|
| 208 |
+
// DEVICE 0
|
| 209 |
+
main_memory #(.MEMORY_DEPTH(MEMORY_DEPTH)) m1( //Instruction and Data memory (combined memory)
|
| 210 |
+
.i_clk(i_clk),
|
| 211 |
+
// Instruction Memory
|
| 212 |
+
.i_inst_addr(iaddr[$clog2(MEMORY_DEPTH)-1:0]),
|
| 213 |
+
.o_inst_out(inst),
|
| 214 |
+
.i_stb_inst(i_stb_inst),
|
| 215 |
+
.o_ack_inst(o_ack_inst),
|
| 216 |
+
// Data Memory
|
| 217 |
+
.i_wb_cyc(device0_wb_cyc),
|
| 218 |
+
.i_wb_stb(device0_wb_stb),
|
| 219 |
+
.i_wb_we(device0_wb_we),
|
| 220 |
+
.i_wb_addr(device0_wb_addr[$clog2(MEMORY_DEPTH)-1:0]),
|
| 221 |
+
.i_wb_data(o_device0_wb_data),
|
| 222 |
+
.i_wb_sel(device0_wb_sel),
|
| 223 |
+
.o_wb_ack(device0_wb_ack),
|
| 224 |
+
.o_wb_stall(device0_wb_stall),
|
| 225 |
+
.o_wb_data(i_device0_wb_data)
|
| 226 |
+
);
|
| 227 |
+
|
| 228 |
+
// DEVICE 1
|
| 229 |
+
rv32i_clint #( //Core Logic Interrupt [memory-mapped to < h50 (MSB=1)]
|
| 230 |
+
.CLK_FREQ_MHZ(CLK_FREQ_MHZ), //input clock frequency in MHz
|
| 231 |
+
.MTIME_BASE_ADDRESS(32'h8000_0000), //Machine-level timer register (64-bits, 2 words)
|
| 232 |
+
.MTIMECMP_BASE_ADDRESS(32'h8000_0008), //Machine-level Time Compare register (64-bits, 2 words)
|
| 233 |
+
.MSIP_BASE_ADDRESS(32'h8000_0010) //Machine-level Software Interrupt register
|
| 234 |
+
) clint (
|
| 235 |
+
.clk(i_clk),
|
| 236 |
+
.rst_n(!i_rst),
|
| 237 |
+
.i_wb_cyc(device1_wb_cyc),
|
| 238 |
+
.i_wb_stb(device1_wb_stb),
|
| 239 |
+
.i_wb_we(device1_wb_we),
|
| 240 |
+
.i_wb_addr(device1_wb_addr),
|
| 241 |
+
.i_wb_data(o_device1_wb_data),
|
| 242 |
+
.i_wb_sel(device1_wb_sel),
|
| 243 |
+
.o_wb_ack(device1_wb_ack),
|
| 244 |
+
.o_wb_stall(device1_wb_stall),
|
| 245 |
+
.o_wb_data(i_device1_wb_data),
|
| 246 |
+
// Interrupts
|
| 247 |
+
.o_timer_interrupt(o_timer_interrupt),
|
| 248 |
+
.o_software_interrupt(o_software_interrupt)
|
| 249 |
+
);
|
| 250 |
+
|
| 251 |
+
// DEVICE 2
|
| 252 |
+
uart #( .CLOCK_FREQ(CLK_FREQ_MHZ*1_000_000), //UART (TX only) [memory-mapped to >=h50,<hA0 (MSB=1)]
|
| 253 |
+
.BAUD_RATE( //UART Baud rate
|
| 254 |
+
`ifdef ICARUS
|
| 255 |
+
2_000_000 //faster simulation delay_count <= 5;
|
| 256 |
+
|
| 257 |
+
`else
|
| 258 |
+
9600 //9600 Baud
|
| 259 |
+
`endif),
|
| 260 |
+
.UART_TX_DATA(32'h8000_0050), //memory-mapped address for TX
|
| 261 |
+
.UART_TX_BUSY(32'h8000_0054), //memory-mapped address to check if TX is busy (has ongoing request)
|
| 262 |
+
.UART_RX_BUFFER_FULL(32'h8000_0058), //memory-mapped address to check if a read has completed
|
| 263 |
+
.UART_RX_DATA(32'h8000_005C), //memory-mapped address for RX
|
| 264 |
+
.DBIT(8), //UART Data Bits
|
| 265 |
+
.SBIT(1) //UART Stop Bits
|
| 266 |
+
) uart
|
| 267 |
+
(
|
| 268 |
+
.clk(i_clk),
|
| 269 |
+
.rst_n(!i_rst),
|
| 270 |
+
.i_wb_cyc(device2_wb_cyc),
|
| 271 |
+
.i_wb_stb(device2_wb_stb),
|
| 272 |
+
.i_wb_we(device2_wb_we),
|
| 273 |
+
.i_wb_addr(device2_wb_addr),
|
| 274 |
+
.i_wb_data(o_device2_wb_data[7:0]),
|
| 275 |
+
.i_wb_sel(device2_wb_sel),
|
| 276 |
+
.o_wb_ack(device2_wb_ack),
|
| 277 |
+
.o_wb_stall(device2_wb_stall),
|
| 278 |
+
.o_wb_data(i_device2_wb_data[7:0]),
|
| 279 |
+
.uart_rx(uart_rx), //UART RX line
|
| 280 |
+
.uart_tx(uart_tx) //UART TX line
|
| 281 |
+
);
|
| 282 |
+
|
| 283 |
+
// CONTINUE////////////////////////////////////////
|
| 284 |
+
//DEVICE 3
|
| 285 |
+
i2c #(.main_clock(CLK_FREQ_MHZ*1_000_000), //SCCB mode(no pullups resistors needed) [memory-mapped to >=A0,<F0 (MSB=1)]
|
| 286 |
+
.freq( //i2c freqeuncy
|
| 287 |
+
`ifdef ICARUS
|
| 288 |
+
2_000_000 //faster simulation
|
| 289 |
+
`else
|
| 290 |
+
100_000 //100KHz
|
| 291 |
+
`endif),
|
| 292 |
+
.addr_bytes(1), //addr_bytes=number of bytes of an address
|
| 293 |
+
.I2C_START(32'h8000_00A0), //write-only memory-mapped address to start i2c (write the i2c slave address)
|
| 294 |
+
.I2C_WRITE(32'h8000_00A4), //write-only memory-mapped address for sending data to slave
|
| 295 |
+
.I2C_READ(32'h8000_00A8), //read-only memory-mapped address to read data received from slave (this will also continue reading from slave)
|
| 296 |
+
.I2C_BUSY(32'h8000_00AC), //read-only memory-mapped address to check if i2c is busy (cannot accept request)
|
| 297 |
+
.I2C_ACK(32'h8000_00B0), //read-only memory-mapped address to check if last access has benn acknowledge by slave
|
| 298 |
+
.I2C_READ_DATA_READY(32'h8000_00B4), //read-only memory-mapped address to check if data to be received from slave is ready
|
| 299 |
+
.I2C_STOP(32'h8000_00B8) //write-only memory-mapped address to stop i2c (this is persistent thus must be manually turned off after stopping i2c)
|
| 300 |
+
) i2c
|
| 301 |
+
(
|
| 302 |
+
.clk(i_clk),
|
| 303 |
+
.rst_n(!i_rst),
|
| 304 |
+
.i_wb_cyc(device3_wb_cyc),
|
| 305 |
+
.i_wb_stb(device3_wb_stb),
|
| 306 |
+
.i_wb_we(device3_wb_we),
|
| 307 |
+
.i_wb_addr(device3_wb_addr),
|
| 308 |
+
.i_wb_data(o_device3_wb_data[7:0]),
|
| 309 |
+
.i_wb_sel(device3_wb_sel),
|
| 310 |
+
.o_wb_ack(device3_wb_ack),
|
| 311 |
+
.o_wb_stall(device3_wb_stall),
|
| 312 |
+
.o_wb_data(i_device3_wb_data[7:0]),
|
| 313 |
+
.scl(i2c_scl), //i2c bidrectional clock line
|
| 314 |
+
.sda(i2c_sda) //i2c bidrectional data line
|
| 315 |
+
);
|
| 316 |
+
|
| 317 |
+
//DEVICE 4
|
| 318 |
+
gpio #( //General-Purpose Input-Ouput
|
| 319 |
+
.GPIO_MODE(32'h8000_00F0), //set if GPIO will be read(0) or write(1)
|
| 320 |
+
.GPIO_READ(32'h8000_00F4), //read GPIO value
|
| 321 |
+
.GPIO_WRITE(32'h8000_00F8), //write to GPIO
|
| 322 |
+
.GPIO_COUNT(12)
|
| 323 |
+
) gpio (
|
| 324 |
+
.clk(i_clk),
|
| 325 |
+
.rst_n(!i_rst),
|
| 326 |
+
.i_wb_cyc(device4_wb_cyc),
|
| 327 |
+
.i_wb_stb(device4_wb_stb),
|
| 328 |
+
.i_wb_we(device4_wb_we),
|
| 329 |
+
.i_wb_addr(device4_wb_addr),
|
| 330 |
+
.i_wb_data(o_device4_wb_data[GPIO_COUNT-1:0]),
|
| 331 |
+
.i_wb_sel(device4_wb_sel),
|
| 332 |
+
.o_wb_ack(device4_wb_ack),
|
| 333 |
+
.o_wb_stall(device4_wb_stall),
|
| 334 |
+
.o_wb_data(i_device4_wb_data[GPIO_COUNT-1:0]),
|
| 335 |
+
//GPIO
|
| 336 |
+
.gpio(gpio_pins) //gpio pins
|
| 337 |
+
);
|
| 338 |
+
|
| 339 |
+
`ifdef DDR3
|
| 340 |
+
wire clk_locked;
|
| 341 |
+
wire i_controller_clk, i_ddr3_clk, i_ref_clk, i_ddr3_clk_90;
|
| 342 |
+
|
| 343 |
+
clk_wiz_0 clk_wiz_inst
|
| 344 |
+
(
|
| 345 |
+
// Clock out ports
|
| 346 |
+
.clk_out1(i_controller_clk), //83.33333 Mhz
|
| 347 |
+
.clk_out2(i_ddr3_clk), // 333.33333 MHz
|
| 348 |
+
.clk_out3(i_ref_clk), //200MHz
|
| 349 |
+
.clk_out4(i_ddr3_clk_90), // 333.33333 MHz vs 90 degrees shift
|
| 350 |
+
// Status and control signals
|
| 351 |
+
.reset(i_rst),
|
| 352 |
+
.locked(clk_locked),
|
| 353 |
+
// Clock in ports
|
| 354 |
+
.clk_in1(i_clk)
|
| 355 |
+
);
|
| 356 |
+
|
| 357 |
+
//DEVICE 5 (DDR3 Controller)
|
| 358 |
+
ddr3_top #(
|
| 359 |
+
.CONTROLLER_CLK_PERIOD(12_000), //ps, clock period of the controller interface
|
| 360 |
+
.DDR3_CLK_PERIOD(3_000), //ps, clock period of the DDR3 RAM device (must be 1/4 of the CONTROLLER_CLK_PERIOD)
|
| 361 |
+
.ROW_BITS(14), //width of row address
|
| 362 |
+
.COL_BITS(10), //width of column address
|
| 363 |
+
.BA_BITS(3), //width of bank address
|
| 364 |
+
.DQ_BITS(8), //device width
|
| 365 |
+
.LANES(2), //number of DDR3 device to be controlled
|
| 366 |
+
.AUX_WIDTH(4), //width of aux line (must be >= 4)
|
| 367 |
+
.WB2_ADDR_BITS(32), //width of 2nd wishbone address bus
|
| 368 |
+
.WB2_DATA_BITS(32), //width of 2nd wishbone data bus
|
| 369 |
+
.OPT_LOWPOWER(1), //1 = low power, 0 = low logic
|
| 370 |
+
.OPT_BUS_ABORT(1), //1 = can abort bus, 0 = no absort (i_wb_cyc will be ignored, ideal for an AXI implementation which cannot abort transaction)
|
| 371 |
+
.MICRON_SIM(0), //enable faster simulation for micron ddr3 model (shorten POWER_ON_RESET_HIGH and INITIAL_CKE_LOW)
|
| 372 |
+
.ODELAY_SUPPORTED(0), //set to 1 when ODELAYE2 is supported
|
| 373 |
+
.SECOND_WISHBONE(0) //set to 1 if 2nd wishbone is needed
|
| 374 |
+
) ddr3_top
|
| 375 |
+
(
|
| 376 |
+
//clock and reset
|
| 377 |
+
.i_controller_clk(i_controller_clk),
|
| 378 |
+
.i_ddr3_clk(i_ddr3_clk), //i_controller_clk has period of CONTROLLER_CLK_PERIOD, i_ddr3_clk has period of DDR3_CLK_PERIOD
|
| 379 |
+
.i_ref_clk(i_ref_clk),
|
| 380 |
+
.i_ddr3_clk_90(i_ddr3_clk_90),
|
| 381 |
+
.i_rst_n(!i_rst && clk_locked),
|
| 382 |
+
//
|
| 383 |
+
// Wishbone inputs
|
| 384 |
+
.i_wb_cyc(device5_wb_cyc), //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 385 |
+
.i_wb_stb(device5_wb_stb), //request a transfer
|
| 386 |
+
.i_wb_we(device5_wb_we), //write-enable (1 = write, 0 = read)
|
| 387 |
+
.i_wb_addr(device5_wb_addr), //burst-addressable {row,bank,col}
|
| 388 |
+
.i_wb_data(o_device5_wb_data), //write data, for a 4:1 controller data width is 8 times the number of pins on the device
|
| 389 |
+
.i_wb_sel(device5_wb_sel), //byte strobe for write (1 = write the byte)
|
| 390 |
+
.i_aux(0), //for AXI-interface compatibility (given upon strobe)
|
| 391 |
+
// Wishbone outputs
|
| 392 |
+
.o_wb_stall(device5_wb_stall), //1 = busy, cannot accept requests
|
| 393 |
+
.o_wb_ack(device5_wb_ack), //1 = read/write request has completed
|
| 394 |
+
.o_wb_data(i_device5_wb_data), //read data, for a 4:1 controller data width is 8 times the number of pins on the device
|
| 395 |
+
.o_aux(),
|
| 396 |
+
//
|
| 397 |
+
// Wishbone 2 (PHY) inputs
|
| 398 |
+
.i_wb2_cyc(), //bus cycle active (1 = normal operation, 0 = all ongoing transaction are to be cancelled)
|
| 399 |
+
.i_wb2_stb(), //request a transfer
|
| 400 |
+
.i_wb2_we(), //write-enable (1 = write, 0 = read)
|
| 401 |
+
.i_wb2_addr(), //burst-addressable {row,bank,col}
|
| 402 |
+
.i_wb2_data(), //write data, for a 4:1 controller data width is 8 times the number of pins on the device
|
| 403 |
+
.i_wb2_sel(), //byte strobe for write (1 = write the byte)
|
| 404 |
+
// Wishbone 2 (Controller) outputs
|
| 405 |
+
.o_wb2_stall(), //1 = busy, cannot accept requests
|
| 406 |
+
.o_wb2_ack(), //1 = read/write request has completed
|
| 407 |
+
.o_wb2_data(), //read data, for a 4:1 controller data width is 8 times the number of pins on the device
|
| 408 |
+
//
|
| 409 |
+
// DDR3 I/O Interface
|
| 410 |
+
.o_ddr3_clk_p(ddr3_clk_p),
|
| 411 |
+
.o_ddr3_clk_n(ddr3_clk_n),
|
| 412 |
+
.o_ddr3_reset_n(ddr3_reset_n),
|
| 413 |
+
.o_ddr3_cke(ddr3_cke), // CKE
|
| 414 |
+
.o_ddr3_cs_n(ddr3_cs_n), // chip select signal (controls rank 1 only)
|
| 415 |
+
.o_ddr3_ras_n(ddr3_ras_n), // RAS#
|
| 416 |
+
.o_ddr3_cas_n(ddr3_cas_n), // CAS#
|
| 417 |
+
.o_ddr3_we_n(ddr3_we_n), // WE#
|
| 418 |
+
.o_ddr3_addr(ddr3_addr),
|
| 419 |
+
.o_ddr3_ba_addr(ddr3_ba),
|
| 420 |
+
.io_ddr3_dq(ddr3_dq),
|
| 421 |
+
.io_ddr3_dqs(ddr3_dqs_p),
|
| 422 |
+
.io_ddr3_dqs_n(ddr3_dqs_n),
|
| 423 |
+
.o_ddr3_dm(ddr3_dm),
|
| 424 |
+
.o_ddr3_odt(ddr3_odt), // on-die termination
|
| 425 |
+
// Debug outputs
|
| 426 |
+
.o_debug1(),
|
| 427 |
+
.o_debug2(),
|
| 428 |
+
.o_debug3(),
|
| 429 |
+
.o_ddr3_debug_read_dqs_p(),
|
| 430 |
+
.o_ddr3_debug_read_dqs_n()
|
| 431 |
+
////////////////////////////////////
|
| 432 |
+
);
|
| 433 |
+
|
| 434 |
+
`endif
|
| 435 |
+
|
| 436 |
+
endmodule
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
module memory_wrapper ( //decodes address and access the corresponding memory-mapped device
|
| 440 |
+
//RISC-V Core
|
| 441 |
+
input wire i_wb_cyc,
|
| 442 |
+
input wire i_wb_stb,
|
| 443 |
+
input wire i_wb_we,
|
| 444 |
+
input wire[31:0] i_wb_addr,
|
| 445 |
+
input wire[31:0] i_wb_data,
|
| 446 |
+
input wire[3:0] i_wb_sel,
|
| 447 |
+
output reg o_wb_ack,
|
| 448 |
+
output reg o_wb_stall,
|
| 449 |
+
output reg[31:0] o_wb_data,
|
| 450 |
+
|
| 451 |
+
//Device 0 Interface (RAM)
|
| 452 |
+
output reg o_device0_wb_cyc,
|
| 453 |
+
output reg o_device0_wb_stb,
|
| 454 |
+
output reg o_device0_wb_we,
|
| 455 |
+
output reg[31:0] o_device0_wb_addr,
|
| 456 |
+
output reg[31:0] o_device0_wb_data,
|
| 457 |
+
output reg[3:0] o_device0_wb_sel,
|
| 458 |
+
input wire i_device0_wb_ack,
|
| 459 |
+
input wire i_device0_wb_stall,
|
| 460 |
+
input wire[31:0] i_device0_wb_data,
|
| 461 |
+
|
| 462 |
+
//Device 1 Interface (CLINT)
|
| 463 |
+
output reg o_device1_wb_cyc,
|
| 464 |
+
output reg o_device1_wb_stb,
|
| 465 |
+
output reg o_device1_wb_we,
|
| 466 |
+
output reg[31:0] o_device1_wb_addr,
|
| 467 |
+
output reg[31:0] o_device1_wb_data,
|
| 468 |
+
output reg[3:0] o_device1_wb_sel,
|
| 469 |
+
input wire i_device1_wb_ack,
|
| 470 |
+
input wire i_device1_wb_stall,
|
| 471 |
+
input wire[31:0] i_device1_wb_data,
|
| 472 |
+
|
| 473 |
+
//Device 2 Interface (UART)
|
| 474 |
+
output reg o_device2_wb_cyc,
|
| 475 |
+
output reg o_device2_wb_stb,
|
| 476 |
+
output reg o_device2_wb_we,
|
| 477 |
+
output reg[31:0] o_device2_wb_addr,
|
| 478 |
+
output reg[31:0] o_device2_wb_data,
|
| 479 |
+
output reg[3:0] o_device2_wb_sel,
|
| 480 |
+
input wire i_device2_wb_ack,
|
| 481 |
+
input wire i_device2_wb_stall,
|
| 482 |
+
input wire[31:0] i_device2_wb_data,
|
| 483 |
+
|
| 484 |
+
//Device 3 Interface (I2C)
|
| 485 |
+
output reg o_device3_wb_cyc,
|
| 486 |
+
output reg o_device3_wb_stb,
|
| 487 |
+
output reg o_device3_wb_we,
|
| 488 |
+
output reg[31:0] o_device3_wb_addr,
|
| 489 |
+
output reg[31:0] o_device3_wb_data,
|
| 490 |
+
output reg[3:0] o_device3_wb_sel,
|
| 491 |
+
input wire i_device3_wb_ack,
|
| 492 |
+
input wire i_device3_wb_stall,
|
| 493 |
+
input wire[31:0] i_device3_wb_data,
|
| 494 |
+
|
| 495 |
+
//Device 4 Interface (GPIO)
|
| 496 |
+
output reg o_device4_wb_cyc,
|
| 497 |
+
output reg o_device4_wb_stb,
|
| 498 |
+
output reg o_device4_wb_we,
|
| 499 |
+
output reg[31:0] o_device4_wb_addr,
|
| 500 |
+
output reg[31:0] o_device4_wb_data,
|
| 501 |
+
output reg[3:0] o_device4_wb_sel,
|
| 502 |
+
input wire i_device4_wb_ack,
|
| 503 |
+
input wire i_device4_wb_stall,
|
| 504 |
+
input wire[31:0] i_device4_wb_data,
|
| 505 |
+
|
| 506 |
+
//Device 5 Interface (DDR3)
|
| 507 |
+
output reg o_device5_wb_cyc,
|
| 508 |
+
output reg o_device5_wb_stb,
|
| 509 |
+
output reg o_device5_wb_we,
|
| 510 |
+
output reg[31:0] o_device5_wb_addr,
|
| 511 |
+
output reg[31:0] o_device5_wb_data,
|
| 512 |
+
output reg[3:0] o_device5_wb_sel,
|
| 513 |
+
input wire i_device5_wb_ack,
|
| 514 |
+
input wire i_device5_wb_stall,
|
| 515 |
+
input wire[31:0] i_device5_wb_data
|
| 516 |
+
);
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
always @* begin
|
| 520 |
+
o_wb_ack = 0;
|
| 521 |
+
o_wb_stall = 0;
|
| 522 |
+
o_wb_data = 0;
|
| 523 |
+
|
| 524 |
+
o_device0_wb_cyc = 0;
|
| 525 |
+
o_device0_wb_stb = 0;
|
| 526 |
+
o_device0_wb_we = 0;
|
| 527 |
+
o_device0_wb_addr = 0;
|
| 528 |
+
o_device0_wb_data = 0;
|
| 529 |
+
o_device0_wb_sel = 0;
|
| 530 |
+
|
| 531 |
+
o_device1_wb_cyc = 0;
|
| 532 |
+
o_device1_wb_stb = 0;
|
| 533 |
+
o_device1_wb_we = 0;
|
| 534 |
+
o_device1_wb_addr = 0;
|
| 535 |
+
o_device1_wb_data = 0;
|
| 536 |
+
o_device1_wb_sel = 0;
|
| 537 |
+
|
| 538 |
+
o_device2_wb_cyc = 0;
|
| 539 |
+
o_device2_wb_stb = 0;
|
| 540 |
+
o_device2_wb_we = 0;
|
| 541 |
+
o_device2_wb_addr = 0;
|
| 542 |
+
o_device2_wb_data = 0;
|
| 543 |
+
o_device2_wb_sel = 0;
|
| 544 |
+
|
| 545 |
+
o_device3_wb_cyc = 0;
|
| 546 |
+
o_device3_wb_stb = 0;
|
| 547 |
+
o_device3_wb_we = 0;
|
| 548 |
+
o_device3_wb_addr = 0;
|
| 549 |
+
o_device3_wb_data = 0;
|
| 550 |
+
o_device3_wb_sel = 0;
|
| 551 |
+
|
| 552 |
+
o_device4_wb_cyc = 0;
|
| 553 |
+
o_device4_wb_stb = 0;
|
| 554 |
+
o_device4_wb_we = 0;
|
| 555 |
+
o_device4_wb_addr = 0;
|
| 556 |
+
o_device4_wb_data = 0;
|
| 557 |
+
o_device4_wb_sel = 0;
|
| 558 |
+
|
| 559 |
+
o_device5_wb_cyc = 0;
|
| 560 |
+
o_device5_wb_stb = 0;
|
| 561 |
+
o_device5_wb_we = 0;
|
| 562 |
+
o_device5_wb_addr = 0;
|
| 563 |
+
o_device5_wb_data = 0;
|
| 564 |
+
o_device5_wb_sel = 0;
|
| 565 |
+
|
| 566 |
+
// Memory-mapped peripherals address has MSB set to 1
|
| 567 |
+
if(i_wb_addr[31]) begin
|
| 568 |
+
if(i_wb_addr[11:0] < 12'h50) begin //Device 1 Interface (CLINT) (20 words)
|
| 569 |
+
o_device1_wb_cyc = i_wb_cyc;
|
| 570 |
+
o_device1_wb_stb = i_wb_stb;
|
| 571 |
+
o_device1_wb_we = i_wb_we;
|
| 572 |
+
o_device1_wb_addr = i_wb_addr;
|
| 573 |
+
o_device1_wb_data = i_wb_data;
|
| 574 |
+
o_device1_wb_sel = i_wb_sel;
|
| 575 |
+
o_wb_ack = i_device1_wb_ack;
|
| 576 |
+
o_wb_stall = i_device1_wb_stall;
|
| 577 |
+
o_wb_data = i_device1_wb_data;
|
| 578 |
+
end
|
| 579 |
+
|
| 580 |
+
if(i_wb_addr[11:0] >= 12'h50 && i_wb_addr[11:0] < 12'hA0) begin //Device 2 Interface (UART) (20 words)
|
| 581 |
+
o_device2_wb_cyc = i_wb_cyc;
|
| 582 |
+
o_device2_wb_stb = i_wb_stb;
|
| 583 |
+
o_device2_wb_we = i_wb_we;
|
| 584 |
+
o_device2_wb_addr = i_wb_addr;
|
| 585 |
+
o_device2_wb_data = i_wb_data;
|
| 586 |
+
o_device2_wb_sel = i_wb_sel;
|
| 587 |
+
o_wb_ack = i_device2_wb_ack;
|
| 588 |
+
o_wb_stall = i_device2_wb_stall;
|
| 589 |
+
o_wb_data = i_device2_wb_data;
|
| 590 |
+
end
|
| 591 |
+
|
| 592 |
+
if(i_wb_addr[11:0] >= 12'hA0 && i_wb_addr[11:0] < 12'hF0) begin //Device 3 Interface (I2C) (20 words)
|
| 593 |
+
o_device3_wb_cyc = i_wb_cyc;
|
| 594 |
+
o_device3_wb_stb = i_wb_stb;
|
| 595 |
+
o_device3_wb_we = i_wb_we;
|
| 596 |
+
o_device3_wb_addr = i_wb_addr;
|
| 597 |
+
o_device3_wb_data = i_wb_data;
|
| 598 |
+
o_device3_wb_sel = i_wb_sel;
|
| 599 |
+
o_wb_ack = i_device3_wb_ack;
|
| 600 |
+
o_wb_stall = i_device3_wb_stall;
|
| 601 |
+
o_wb_data = i_device3_wb_data;
|
| 602 |
+
end
|
| 603 |
+
|
| 604 |
+
if(i_wb_addr[11:0] >= 12'hF0 && i_wb_addr[11:0] < 12'h140) begin //Device 4 Interface (GPIO) (20 words)
|
| 605 |
+
o_device4_wb_cyc = i_wb_cyc;
|
| 606 |
+
o_device4_wb_stb = i_wb_stb;
|
| 607 |
+
o_device4_wb_we = i_wb_we;
|
| 608 |
+
o_device4_wb_addr = i_wb_addr;
|
| 609 |
+
o_device4_wb_data = i_wb_data;
|
| 610 |
+
o_device4_wb_sel = i_wb_sel;
|
| 611 |
+
o_wb_ack = i_device4_wb_ack;
|
| 612 |
+
o_wb_stall = i_device4_wb_stall;
|
| 613 |
+
o_wb_data = i_device4_wb_data;
|
| 614 |
+
end
|
| 615 |
+
|
| 616 |
+
if(i_wb_addr[30]) begin //Device 5 Interface (DDR3) (last two bits of address are high)
|
| 617 |
+
o_device5_wb_cyc = i_wb_cyc;
|
| 618 |
+
o_device5_wb_stb = i_wb_stb;
|
| 619 |
+
o_device5_wb_we = i_wb_we;
|
| 620 |
+
o_device5_wb_addr = i_wb_addr;
|
| 621 |
+
o_device5_wb_data = i_wb_data;
|
| 622 |
+
o_device5_wb_sel = i_wb_sel;
|
| 623 |
+
o_wb_ack = i_device5_wb_ack;
|
| 624 |
+
o_wb_stall = i_device5_wb_stall;
|
| 625 |
+
o_wb_data = i_device5_wb_data;
|
| 626 |
+
end
|
| 627 |
+
end
|
| 628 |
+
|
| 629 |
+
// Else access RAM
|
| 630 |
+
else begin //Device 0 Interface (RAM)
|
| 631 |
+
o_device0_wb_cyc = i_wb_cyc;
|
| 632 |
+
o_device0_wb_stb = i_wb_stb;
|
| 633 |
+
o_device0_wb_we = i_wb_we;
|
| 634 |
+
o_device0_wb_addr = i_wb_addr;
|
| 635 |
+
o_device0_wb_data = i_wb_data;
|
| 636 |
+
o_device0_wb_sel = i_wb_sel;
|
| 637 |
+
o_wb_ack = i_device0_wb_ack;
|
| 638 |
+
o_wb_stall = i_device0_wb_stall;
|
| 639 |
+
o_wb_data = i_device0_wb_data;
|
| 640 |
+
end
|
| 641 |
+
end
|
| 642 |
+
|
| 643 |
+
endmodule
|
| 644 |
+
module main_memory #(parameter MEMORY_DEPTH=1024) ( //Instruction and Data memory (combined memory)
|
| 645 |
+
input wire i_clk,
|
| 646 |
+
// Instruction Memory
|
| 647 |
+
input wire[$clog2(MEMORY_DEPTH)-1:0] i_inst_addr,
|
| 648 |
+
output reg[31:0] o_inst_out,
|
| 649 |
+
input wire i_stb_inst, // request for instruction
|
| 650 |
+
output reg o_ack_inst, //ack (high if new instruction is now on the bus)
|
| 651 |
+
// Data Memory
|
| 652 |
+
input wire i_wb_cyc,
|
| 653 |
+
input wire i_wb_stb,
|
| 654 |
+
input wire i_wb_we,
|
| 655 |
+
input wire[$clog2(MEMORY_DEPTH)-1:0] i_wb_addr,
|
| 656 |
+
input wire[31:0] i_wb_data,
|
| 657 |
+
input wire[3:0] i_wb_sel,
|
| 658 |
+
output reg o_wb_ack,
|
| 659 |
+
output wire o_wb_stall,
|
| 660 |
+
output reg[31:0] o_wb_data
|
| 661 |
+
);
|
| 662 |
+
reg[31:0] memory_regfile[MEMORY_DEPTH/4 - 1:0];
|
| 663 |
+
integer i;
|
| 664 |
+
assign o_wb_stall = 0; // never stall
|
| 665 |
+
|
| 666 |
+
initial begin //initialize memory to zero
|
| 667 |
+
o_ack_inst <= 0;
|
| 668 |
+
o_wb_ack <= 0;
|
| 669 |
+
o_inst_out <= 0;
|
| 670 |
+
end
|
| 671 |
+
|
| 672 |
+
//reading must be registered to be inferred as block ram
|
| 673 |
+
always @(posedge i_clk) begin
|
| 674 |
+
o_ack_inst <= i_stb_inst; //go high next cycle after receiving request (data o_inst_out is also sent at next cycle)
|
| 675 |
+
o_wb_ack <= i_wb_stb && i_wb_cyc;
|
| 676 |
+
o_inst_out <= memory_regfile[{i_inst_addr>>2}]; //read instruction
|
| 677 |
+
o_wb_data <= memory_regfile[i_wb_addr[$clog2(MEMORY_DEPTH)-1:2]]; //read data
|
| 678 |
+
end
|
| 679 |
+
|
| 680 |
+
// write data
|
| 681 |
+
always @(posedge i_clk) begin
|
| 682 |
+
if(i_wb_we && i_wb_stb && i_wb_cyc) begin
|
| 683 |
+
if(i_wb_sel[0]) memory_regfile[i_wb_addr[$clog2(MEMORY_DEPTH)-1:2]][7:0] <= i_wb_data[7:0];
|
| 684 |
+
if(i_wb_sel[1]) memory_regfile[i_wb_addr[$clog2(MEMORY_DEPTH)-1:2]][15:8] <= i_wb_data[15:8];
|
| 685 |
+
if(i_wb_sel[2]) memory_regfile[i_wb_addr[$clog2(MEMORY_DEPTH)-1:2]][23:16] <= i_wb_data[23:16];
|
| 686 |
+
if(i_wb_sel[3]) memory_regfile[i_wb_addr[$clog2(MEMORY_DEPTH)-1:2]][31:24] <= i_wb_data[31:24];
|
| 687 |
+
end
|
| 688 |
+
|
| 689 |
+
end
|
| 690 |
+
|
| 691 |
+
endmodule
|
| 692 |
+
|
| 693 |
+
|
| 694 |
+
module uart #( //UART (TX only)
|
| 695 |
+
parameter CLOCK_FREQ = 12_000_000,//Input clock frequency
|
| 696 |
+
parameter BAUD_RATE = 9600, //UART Baud rate
|
| 697 |
+
parameter UART_TX_DATA = 8140, //memory-mapped address for TX (write to UART)
|
| 698 |
+
parameter UART_TX_BUSY = 8144, //memory-mapped address to check if TX is busy (has ongoing request)
|
| 699 |
+
parameter UART_RX_BUFFER_FULL = 8148, //memory-mapped address to check if a read has completed
|
| 700 |
+
parameter UART_RX_DATA = 8152, //memory-mapped address for RX (read the data)
|
| 701 |
+
parameter DBIT = 8, //UART Data Bits
|
| 702 |
+
parameter SBIT = 1 //UART Stop Bits
|
| 703 |
+
)(
|
| 704 |
+
input wire clk,
|
| 705 |
+
input wire rst_n,
|
| 706 |
+
input wire i_wb_cyc,
|
| 707 |
+
input wire i_wb_stb,
|
| 708 |
+
input wire i_wb_we,
|
| 709 |
+
input wire[31:0] i_wb_addr,
|
| 710 |
+
input wire[DBIT - 1:0 ] i_wb_data,
|
| 711 |
+
input wire[3:0] i_wb_sel,
|
| 712 |
+
output reg o_wb_ack,
|
| 713 |
+
output wire o_wb_stall,
|
| 714 |
+
output reg[DBIT - 1:0] o_wb_data,
|
| 715 |
+
input wire uart_rx, //UART RX line
|
| 716 |
+
output wire uart_tx //UART TX line
|
| 717 |
+
);
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
localparam DVSR = CLOCK_FREQ/(16*BAUD_RATE);
|
| 721 |
+
localparam DVSR_WIDTH = $clog2(DVSR); //array size needed by DVSR
|
| 722 |
+
localparam SB_TICK = 16*SBIT;
|
| 723 |
+
|
| 724 |
+
//FSM state declarations
|
| 725 |
+
localparam[1:0] idle=2'd0,
|
| 726 |
+
start=2'd1,
|
| 727 |
+
data=2'd2,
|
| 728 |
+
stop=2'd3;
|
| 729 |
+
|
| 730 |
+
reg[DBIT - 1:0] uart_busy;
|
| 731 |
+
reg tx_done_tick;
|
| 732 |
+
reg[1:0] state_reg,state_nxt;
|
| 733 |
+
reg[3:0] s_reg,s_nxt; //count to 16 for every data bit
|
| 734 |
+
reg[2:0] n_reg,n_nxt; //count the number of data bits already transmitted
|
| 735 |
+
reg[DBIT - 1:0] din_reg,din_nxt; //stores the word to be transmitted
|
| 736 |
+
reg tx_reg,tx_nxt;
|
| 737 |
+
reg s_tick;
|
| 738 |
+
reg wr_uart;
|
| 739 |
+
reg[1:0] state_reg_rx,state_nxt_rx;
|
| 740 |
+
reg[3:0] s_reg_rx,s_nxt_rx; //check if number of ticks is 7(middle of start bit), or 15(middle of a data bit)
|
| 741 |
+
reg[2:0] n_reg_rx,n_nxt_rx; //checks how many data bits is already passed(value is 7 for last bit)
|
| 742 |
+
reg[7:0] b_reg,b_nxt; //stores 8-bit binary value of received data bits
|
| 743 |
+
reg[7:0] dout; //data read from UART
|
| 744 |
+
reg rx_done_tick; //goes high if a read is done
|
| 745 |
+
reg rx_buffer_full; //goes high if a read is done
|
| 746 |
+
|
| 747 |
+
assign o_wb_stall = 0;
|
| 748 |
+
|
| 749 |
+
//baud tick generator
|
| 750 |
+
reg[DVSR_WIDTH-1:0] counter=0;
|
| 751 |
+
always @(posedge clk,negedge rst_n) begin
|
| 752 |
+
if(!rst_n) counter<=0;
|
| 753 |
+
else begin
|
| 754 |
+
s_tick=0;
|
| 755 |
+
if(counter == DVSR-1) begin
|
| 756 |
+
s_tick=1;
|
| 757 |
+
counter<=0;
|
| 758 |
+
end
|
| 759 |
+
else begin
|
| 760 |
+
counter<=counter+1;
|
| 761 |
+
end
|
| 762 |
+
|
| 763 |
+
end
|
| 764 |
+
end
|
| 765 |
+
//Read memory-mapped registers
|
| 766 |
+
always @(posedge clk, negedge rst_n) begin
|
| 767 |
+
if(!rst_n) begin
|
| 768 |
+
o_wb_data <= 0;
|
| 769 |
+
o_wb_ack <= 0;
|
| 770 |
+
end
|
| 771 |
+
else begin
|
| 772 |
+
if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == UART_TX_BUSY) begin //read request to UART_TX_BUSY_ADDR (check if there is an ongoing request)
|
| 773 |
+
o_wb_data <= uart_busy;
|
| 774 |
+
end
|
| 775 |
+
else if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == UART_RX_BUFFER_FULL) begin //read request to UART_RX_BUFFER_FULL (check if a read is completed)
|
| 776 |
+
o_wb_data <= rx_buffer_full;
|
| 777 |
+
end
|
| 778 |
+
else if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == UART_RX_DATA) begin //read request to UART_RX_DATA (read the data)
|
| 779 |
+
o_wb_data <= dout;
|
| 780 |
+
end
|
| 781 |
+
o_wb_ack <= i_wb_stb && i_wb_cyc;
|
| 782 |
+
end
|
| 783 |
+
end
|
| 784 |
+
|
| 785 |
+
|
| 786 |
+
/******************************** UART TX ****************************************/
|
| 787 |
+
|
| 788 |
+
|
| 789 |
+
//FSM register operation
|
| 790 |
+
always @(posedge clk,negedge rst_n) begin
|
| 791 |
+
if(!rst_n) begin
|
| 792 |
+
state_reg<=idle;
|
| 793 |
+
s_reg<=0;
|
| 794 |
+
n_reg<=0;
|
| 795 |
+
din_reg<=0;
|
| 796 |
+
tx_reg<=0;
|
| 797 |
+
end
|
| 798 |
+
else begin
|
| 799 |
+
state_reg<=state_nxt;
|
| 800 |
+
s_reg<=s_nxt;
|
| 801 |
+
n_reg<=n_nxt;
|
| 802 |
+
din_reg<=din_nxt;
|
| 803 |
+
tx_reg<=tx_nxt;
|
| 804 |
+
end
|
| 805 |
+
end
|
| 806 |
+
|
| 807 |
+
//FSM next-state logic
|
| 808 |
+
always @* begin
|
| 809 |
+
state_nxt=state_reg;
|
| 810 |
+
s_nxt=s_reg;
|
| 811 |
+
n_nxt=n_reg;
|
| 812 |
+
din_nxt=din_reg;
|
| 813 |
+
tx_nxt=tx_reg;
|
| 814 |
+
tx_done_tick=0;
|
| 815 |
+
uart_busy= 1; //uart is busy unless its in idle state
|
| 816 |
+
case(state_reg)
|
| 817 |
+
idle: begin
|
| 818 |
+
tx_nxt=1;
|
| 819 |
+
uart_busy = 0;
|
| 820 |
+
//start transmit operation when there is a write request to UART_TX_DATA_ADDR and we are in idle
|
| 821 |
+
if(i_wb_we && i_wb_stb && i_wb_cyc && i_wb_addr == UART_TX_DATA && !uart_busy) begin
|
| 822 |
+
din_nxt=i_wb_data;
|
| 823 |
+
s_nxt=0;
|
| 824 |
+
state_nxt=start;
|
| 825 |
+
uart_busy = 1;
|
| 826 |
+
end
|
| 827 |
+
end
|
| 828 |
+
start: begin //wait to finish the start bit
|
| 829 |
+
tx_nxt=0;
|
| 830 |
+
if(s_tick==1) begin
|
| 831 |
+
if(s_reg==15) begin
|
| 832 |
+
s_nxt=0;
|
| 833 |
+
n_nxt=0;
|
| 834 |
+
state_nxt=data;
|
| 835 |
+
end
|
| 836 |
+
else s_nxt=s_reg+1;
|
| 837 |
+
end
|
| 838 |
+
end
|
| 839 |
+
data: begin //wait for all data bits to be transmitted serially
|
| 840 |
+
tx_nxt=din_reg[0];
|
| 841 |
+
if(s_tick==1) begin
|
| 842 |
+
if(s_reg==15) begin
|
| 843 |
+
din_nxt=din_reg>>1;
|
| 844 |
+
s_nxt=0;
|
| 845 |
+
if(n_reg==DBIT-1) state_nxt=stop;
|
| 846 |
+
else n_nxt=n_reg+1;
|
| 847 |
+
end
|
| 848 |
+
else s_nxt=s_reg+1;
|
| 849 |
+
end
|
| 850 |
+
end
|
| 851 |
+
stop: begin //wait to finish the stop bit
|
| 852 |
+
tx_nxt=1;
|
| 853 |
+
if(s_tick==1) begin
|
| 854 |
+
if(s_reg==SB_TICK-1) begin
|
| 855 |
+
tx_done_tick=1;
|
| 856 |
+
state_nxt=idle;
|
| 857 |
+
end
|
| 858 |
+
else s_nxt=s_reg+1;
|
| 859 |
+
end
|
| 860 |
+
end
|
| 861 |
+
default: state_nxt=idle;
|
| 862 |
+
endcase
|
| 863 |
+
end
|
| 864 |
+
assign uart_tx=tx_reg;
|
| 865 |
+
/*********************************************************************************/
|
| 866 |
+
|
| 867 |
+
/******************************** UART RX ****************************************/
|
| 868 |
+
|
| 869 |
+
//FSM register operation
|
| 870 |
+
always @(posedge clk,negedge rst_n) begin
|
| 871 |
+
if(!rst_n) begin
|
| 872 |
+
state_reg_rx<=idle;
|
| 873 |
+
s_reg_rx<=0;
|
| 874 |
+
n_reg_rx<=0;
|
| 875 |
+
b_reg<=0;
|
| 876 |
+
dout<=0;
|
| 877 |
+
rx_buffer_full<=0;
|
| 878 |
+
end
|
| 879 |
+
else begin
|
| 880 |
+
state_reg_rx<=state_nxt_rx;
|
| 881 |
+
s_reg_rx<=s_nxt_rx;
|
| 882 |
+
n_reg_rx<=n_nxt_rx;
|
| 883 |
+
b_reg<=b_nxt;
|
| 884 |
+
if(rx_done_tick) begin
|
| 885 |
+
dout <= b_reg; //memory-mapped register storing the completed read data
|
| 886 |
+
rx_buffer_full <= 1'b1; //memory-mapped register to check if a read is done
|
| 887 |
+
end
|
| 888 |
+
else if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == UART_RX_DATA) begin //read request to UART_RX_DATA (read the data)
|
| 889 |
+
rx_buffer_full <= 1'b0;
|
| 890 |
+
end
|
| 891 |
+
end
|
| 892 |
+
end
|
| 893 |
+
|
| 894 |
+
//FSM next-state logic
|
| 895 |
+
always @* begin
|
| 896 |
+
state_nxt_rx=state_reg_rx;
|
| 897 |
+
s_nxt_rx=s_reg_rx;
|
| 898 |
+
n_nxt_rx=n_reg_rx;
|
| 899 |
+
b_nxt=b_reg;
|
| 900 |
+
rx_done_tick=0;
|
| 901 |
+
case(state_reg_rx)
|
| 902 |
+
idle: if(uart_rx==0) begin //wait for start bit(rx of zero)
|
| 903 |
+
s_nxt_rx=0;
|
| 904 |
+
state_nxt_rx=start;
|
| 905 |
+
end
|
| 906 |
+
start: if(s_tick==1) begin //wait for middle of start bit
|
| 907 |
+
if(s_reg_rx==7) begin
|
| 908 |
+
s_nxt_rx=0;
|
| 909 |
+
n_nxt_rx=0;
|
| 910 |
+
state_nxt_rx=data;
|
| 911 |
+
end
|
| 912 |
+
else s_nxt_rx=s_reg_rx+1;
|
| 913 |
+
end
|
| 914 |
+
data: if(s_tick==1) begin //wait to pass all middle points of every data bits
|
| 915 |
+
if(s_reg_rx==15) begin
|
| 916 |
+
b_nxt={uart_rx,b_reg[7:1]};
|
| 917 |
+
s_nxt_rx=0;
|
| 918 |
+
if(n_reg_rx==DBIT-1) state_nxt_rx=stop;
|
| 919 |
+
else n_nxt_rx=n_reg_rx+1;
|
| 920 |
+
end
|
| 921 |
+
else s_nxt_rx=s_reg_rx+1;
|
| 922 |
+
end
|
| 923 |
+
stop: if(s_tick==1) begin //wait to pass the required stop bits
|
| 924 |
+
if(s_reg_rx==SB_TICK-1) begin
|
| 925 |
+
rx_done_tick=1;
|
| 926 |
+
state_nxt_rx=idle;
|
| 927 |
+
end
|
| 928 |
+
else s_nxt_rx=s_reg_rx+1;
|
| 929 |
+
end
|
| 930 |
+
default: state_nxt_rx=idle;
|
| 931 |
+
endcase
|
| 932 |
+
end
|
| 933 |
+
/*********************************************************************************/
|
| 934 |
+
|
| 935 |
+
endmodule
|
| 936 |
+
|
| 937 |
+
|
| 938 |
+
|
| 939 |
+
module i2c //SCCB mode(no pullups resistors needed) [REPEATED START NOT SUPPORTED]
|
| 940 |
+
#(parameter main_clock=12_000_000, //frequency of clk
|
| 941 |
+
freq=100_000, //i2c freqeuncy
|
| 942 |
+
addr_bytes=2,//addr_bytes=number of bytes of an address
|
| 943 |
+
I2C_START=8100, //write-only memory-mapped address to start i2c (write the i2c slave address)
|
| 944 |
+
I2C_WRITE=8104, //write-only memory-mapped address for sending data to slave
|
| 945 |
+
I2C_READ=8108, //read-only memory-mapped address to read data received from slave (this will also continue reading from slave)
|
| 946 |
+
I2C_BUSY=8112, //read-only memory-mapped address to check if i2c is busy (cannot accept request)
|
| 947 |
+
I2C_ACK=8116, //read-only memory-mapped address to check if last access has benn acknowledge by slave
|
| 948 |
+
I2C_READ_DATA_READY=8120, //read-only memory-mapped address to check if data to be received from slave is ready
|
| 949 |
+
I2C_STOP=8124 //write-only memory-mapped address to stop i2c (this is persistent thus must be manually turned off after stopping i2c)
|
| 950 |
+
)
|
| 951 |
+
(
|
| 952 |
+
input wire clk,
|
| 953 |
+
input wire rst_n,
|
| 954 |
+
// Wishbone Interface
|
| 955 |
+
input wire i_wb_cyc,
|
| 956 |
+
input wire i_wb_stb,
|
| 957 |
+
input wire i_wb_we,
|
| 958 |
+
input wire[31:0] i_wb_addr,
|
| 959 |
+
input wire[7:0] i_wb_data,
|
| 960 |
+
input wire[3:0] i_wb_sel,
|
| 961 |
+
output reg o_wb_ack,
|
| 962 |
+
output wire o_wb_stall,
|
| 963 |
+
output reg[7:0] o_wb_data,
|
| 964 |
+
inout wire scl, sda //i2c bidrectional clock and data line
|
| 965 |
+
);
|
| 966 |
+
|
| 967 |
+
|
| 968 |
+
//memory-mapped registers for controlling i2c
|
| 969 |
+
wire[7:0] i2c_busy = {7'b0, !((state_q == idle) || (state_q == stop_or_write) || (state_q == stop_or_read))}; //check if busy (busy unless we are on these states)
|
| 970 |
+
wire[7:0] i2c_read_data_ready = {7'b0, (state_q == stop_or_read)}; //check if data is ready to be read (data is ready ONLY WHEN we are already waiting for another read request!)
|
| 971 |
+
reg[7:0] i2c_ack; //check last access request has been acknowledged by slave
|
| 972 |
+
reg[7:0] i2c_stop; //write non-zero data here to stop current read/write transaction
|
| 973 |
+
|
| 974 |
+
|
| 975 |
+
wire start = i_wb_stb && i_wb_cyc;
|
| 976 |
+
wire[7:0] wr_data = i_wb_data;
|
| 977 |
+
reg ack;
|
| 978 |
+
reg rd_tick;
|
| 979 |
+
|
| 980 |
+
localparam full= (main_clock)/(2*freq),
|
| 981 |
+
half= full/2,
|
| 982 |
+
counter_width=$clog2(full);
|
| 983 |
+
|
| 984 |
+
//FSM state declarations
|
| 985 |
+
localparam[3:0] idle=0,
|
| 986 |
+
starting=1,
|
| 987 |
+
packet=2,
|
| 988 |
+
ack_servant=3,
|
| 989 |
+
read=4,
|
| 990 |
+
ack_master=5,
|
| 991 |
+
stop_1=6,
|
| 992 |
+
stop_2=7,
|
| 993 |
+
stop_or_read = 8,
|
| 994 |
+
stop_or_write = 9;
|
| 995 |
+
reg[3:0] state_q=idle,state_d;
|
| 996 |
+
reg op_q=0,op_d;
|
| 997 |
+
reg[3:0] idx_q=0,idx_d;
|
| 998 |
+
reg[8:0] wr_data_q=0,wr_data_d;
|
| 999 |
+
reg[7:0] rd_data_q,rd_data_d;
|
| 1000 |
+
reg scl_q=0,scl_d;
|
| 1001 |
+
reg sda_q=0,sda_d;
|
| 1002 |
+
reg[counter_width-1:0] counter_q=0,counter_d;
|
| 1003 |
+
reg[1:0] addr_bytes_q=0,addr_bytes_d;
|
| 1004 |
+
wire scl_lo,scl_hi;
|
| 1005 |
+
wire sda_in, sda_out;
|
| 1006 |
+
|
| 1007 |
+
assign o_wb_stall = 0;
|
| 1008 |
+
//access memory-mapped register
|
| 1009 |
+
always @(posedge clk, negedge rst_n) begin
|
| 1010 |
+
if(!rst_n) begin
|
| 1011 |
+
i2c_stop <= 0;
|
| 1012 |
+
o_wb_ack <= 0;
|
| 1013 |
+
end
|
| 1014 |
+
else begin
|
| 1015 |
+
if(i_wb_stb && i_wb_cyc && i_wb_we && i_wb_addr == I2C_STOP) i2c_stop <= i_wb_data; //write to i2c_stop to stop transaction
|
| 1016 |
+
if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == I2C_ACK) o_wb_data <= i2c_ack; //read i2c_ack to know if last access request has been ack by slave
|
| 1017 |
+
if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == I2C_READ_DATA_READY) o_wb_data <= i2c_read_data_ready;//read this to know if data is ready to be read
|
| 1018 |
+
if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == I2C_BUSY) o_wb_data <= i2c_busy; //read this to know if i2c is still busy
|
| 1019 |
+
if(i_wb_stb && i_wb_cyc && !i_wb_we && i_wb_addr == I2C_READ) o_wb_data <= rd_data_q; //read this to know what has been read from slave (make sure I2C_READ_DATA_READY is already high)
|
| 1020 |
+
|
| 1021 |
+
o_wb_ack <= i_wb_stb && i_wb_cyc;
|
| 1022 |
+
end
|
| 1023 |
+
end
|
| 1024 |
+
|
| 1025 |
+
//register operations
|
| 1026 |
+
always@(posedge clk, negedge rst_n) begin
|
| 1027 |
+
if(!rst_n) begin
|
| 1028 |
+
state_q<=idle;
|
| 1029 |
+
idx_q<=0;
|
| 1030 |
+
wr_data_q<=0;
|
| 1031 |
+
scl_q<=0;
|
| 1032 |
+
sda_q<=0;
|
| 1033 |
+
counter_q<=0;
|
| 1034 |
+
rd_data_q<=0;
|
| 1035 |
+
addr_bytes_q<=0;
|
| 1036 |
+
i2c_ack <= 0;
|
| 1037 |
+
end
|
| 1038 |
+
else begin
|
| 1039 |
+
state_q<=state_d;
|
| 1040 |
+
op_q<=op_d;
|
| 1041 |
+
idx_q<=idx_d;
|
| 1042 |
+
wr_data_q<=wr_data_d;
|
| 1043 |
+
scl_q<=scl_d;
|
| 1044 |
+
sda_q<=sda_d;
|
| 1045 |
+
if(i2c_busy[0]) counter_q<=counter_d; //freeze the scl (by freezing the counter) if we are on wait/idle state (not busy states)
|
| 1046 |
+
rd_data_q<=rd_data_d;
|
| 1047 |
+
addr_bytes_q<=addr_bytes_d;
|
| 1048 |
+
i2c_ack <= {7'd0,ack};
|
| 1049 |
+
end
|
| 1050 |
+
end
|
| 1051 |
+
|
| 1052 |
+
|
| 1053 |
+
//free-running clk, freq depends on parameter "freq"
|
| 1054 |
+
always @* begin
|
| 1055 |
+
counter_d=counter_q+1;
|
| 1056 |
+
scl_d=scl_q;
|
| 1057 |
+
if(state_q==idle || state_q==starting) scl_d=1'b1;
|
| 1058 |
+
else if(counter_q==full[counter_width-1:0]) begin
|
| 1059 |
+
counter_d=0;
|
| 1060 |
+
scl_d=(scl_q==0)?1'b1:1'b0;
|
| 1061 |
+
end
|
| 1062 |
+
end
|
| 1063 |
+
|
| 1064 |
+
//I2C_START
|
| 1065 |
+
//FSM next-state logic
|
| 1066 |
+
always @* begin
|
| 1067 |
+
state_d=state_q;
|
| 1068 |
+
op_d=op_q;
|
| 1069 |
+
idx_d=idx_q;
|
| 1070 |
+
wr_data_d=wr_data_q;
|
| 1071 |
+
rd_data_d=rd_data_q;
|
| 1072 |
+
addr_bytes_d=addr_bytes_q;
|
| 1073 |
+
sda_d=sda_q;
|
| 1074 |
+
rd_tick=0;
|
| 1075 |
+
ack=i2c_ack[0];
|
| 1076 |
+
case(state_q)
|
| 1077 |
+
idle: begin //wait for user to start i2c by writing the slave address to I2C_START
|
| 1078 |
+
sda_d=1'b1;
|
| 1079 |
+
addr_bytes_d=addr_bytes;
|
| 1080 |
+
if(start==1'b1 && i_wb_we && i_wb_addr == I2C_START) begin //wait for a request
|
| 1081 |
+
wr_data_d={wr_data,1'b1}; //the last 1'b1 is for the ACK coming from the servant("1" means high impedance or "reading")
|
| 1082 |
+
op_d= (wr_data[0])? 1:0; // if last bit(R/W bit) is one:read operation, else write operation
|
| 1083 |
+
idx_d=8; //index to be used on transmitting the wr_data serially(MSB first)
|
| 1084 |
+
state_d=starting;
|
| 1085 |
+
end
|
| 1086 |
+
end
|
| 1087 |
+
|
| 1088 |
+
starting: if(scl_hi) begin //start command, change sda to low while scl is high
|
| 1089 |
+
sda_d=0;
|
| 1090 |
+
state_d=packet;
|
| 1091 |
+
end
|
| 1092 |
+
|
| 1093 |
+
packet: if(scl_lo) begin //transmit wr_data serially(MSB first)
|
| 1094 |
+
sda_d= (wr_data_q[idx_q]==0)? 0:1'b1;
|
| 1095 |
+
idx_d= idx_q-1;
|
| 1096 |
+
if(idx_q==0) begin
|
| 1097 |
+
state_d=ack_servant;
|
| 1098 |
+
idx_d=0;
|
| 1099 |
+
end
|
| 1100 |
+
end
|
| 1101 |
+
|
| 1102 |
+
ack_servant: if(scl_hi) begin //wait for ACK bit response(9th bit) from servant
|
| 1103 |
+
ack=!sda_in;
|
| 1104 |
+
if(i2c_stop[0]) state_d=stop_1; //master can forcefully stops the transaction (i2c_stop is memory-mapped)
|
| 1105 |
+
else if(op_q/* && addr_bytes_q==0*/) begin //start reading after writing "addr_bytes" of packets for address
|
| 1106 |
+
idx_d=7;
|
| 1107 |
+
state_d=read;
|
| 1108 |
+
end
|
| 1109 |
+
else begin //write next packet
|
| 1110 |
+
state_d = stop_or_write;
|
| 1111 |
+
idx_d=8;
|
| 1112 |
+
end
|
| 1113 |
+
end
|
| 1114 |
+
|
| 1115 |
+
stop_or_write: if(i2c_stop[0]) begin //wait until user explicitly say to either stop i2c or continue writing
|
| 1116 |
+
state_d = stop_1;
|
| 1117 |
+
end
|
| 1118 |
+
else if(start && i_wb_we && i_wb_addr == I2C_WRITE) begin//continue writing
|
| 1119 |
+
state_d = packet;
|
| 1120 |
+
wr_data_d={wr_data,1'b1};
|
| 1121 |
+
addr_bytes_d=addr_bytes_q-1;
|
| 1122 |
+
end
|
| 1123 |
+
|
| 1124 |
+
read: if(scl_hi) begin //read data from slave(MSB first)
|
| 1125 |
+
rd_data_d[idx_q[2:0]]=sda_in;
|
| 1126 |
+
idx_d=idx_q-1;
|
| 1127 |
+
if(idx_q==0) state_d=ack_master;
|
| 1128 |
+
end
|
| 1129 |
+
|
| 1130 |
+
ack_master: if(scl_lo) begin //master must ACK after receiving data from servant
|
| 1131 |
+
sda_d=1'b0;
|
| 1132 |
+
if(sda_q==0) begin //one whole bit(two scl_lo) had passed
|
| 1133 |
+
rd_tick=1;
|
| 1134 |
+
idx_d=7;
|
| 1135 |
+
if(i2c_stop[0]) state_d=stop_1; //after receiving data, master can opt to stop
|
| 1136 |
+
else state_d=stop_or_read;
|
| 1137 |
+
end
|
| 1138 |
+
end
|
| 1139 |
+
stop_or_read: if(i2c_stop[0]) begin //wait until user explicitly say to either stop i2c or continue reading
|
| 1140 |
+
state_d = stop_1;
|
| 1141 |
+
end
|
| 1142 |
+
else if(start && !i_wb_we && i_wb_addr == I2C_READ) begin //continue reading when current data is read
|
| 1143 |
+
state_d = read;
|
| 1144 |
+
end
|
| 1145 |
+
|
| 1146 |
+
stop_1: if(scl_lo) begin
|
| 1147 |
+
sda_d=1'b0;
|
| 1148 |
+
state_d=stop_2;
|
| 1149 |
+
end
|
| 1150 |
+
stop_2: if(scl_hi) begin
|
| 1151 |
+
sda_d=1'b1;
|
| 1152 |
+
state_d=idle;
|
| 1153 |
+
end
|
| 1154 |
+
default: state_d=idle;
|
| 1155 |
+
endcase
|
| 1156 |
+
end
|
| 1157 |
+
|
| 1158 |
+
//i2c IO logic requires pull-ups (2 logic levels: 0 or Z)
|
| 1159 |
+
//assign scl=scl_q? 1'bz:0; //bidiectional logic for pull-up scl
|
| 1160 |
+
//assign sda=sda_q? 1'bz:0; //bidirectional logic for pull-up scl
|
| 1161 |
+
//assign sda_in=sda;
|
| 1162 |
+
|
| 1163 |
+
//We don't used pull-ups here so logic can be 0 or 1 (instead of high
|
| 1164 |
+
//impedance). This is similar to SCCB protocol.
|
| 1165 |
+
wire is_reading;
|
| 1166 |
+
assign is_reading = (state_q==read || state_q==ack_servant);
|
| 1167 |
+
assign sda_out = sda_q;
|
| 1168 |
+
|
| 1169 |
+
//Vivado, use IOBUF primitive
|
| 1170 |
+
`ifndef ICARUS
|
| 1171 |
+
IOBUF sda_iobuf ( //Vivado IOBUF instantiationGPIO_COUNT-1
|
| 1172 |
+
.IO(sda),
|
| 1173 |
+
.I(sda_out),//write SDA when is_reading low
|
| 1174 |
+
.T(is_reading),
|
| 1175 |
+
.O(sda_in) //read SDA when is_reading high
|
| 1176 |
+
);
|
| 1177 |
+
`endif
|
| 1178 |
+
//Icarus simulator
|
| 1179 |
+
`ifdef ICARUS
|
| 1180 |
+
assign sda = sda_q;
|
| 1181 |
+
`endif
|
| 1182 |
+
|
| 1183 |
+
assign scl = scl_q;
|
| 1184 |
+
assign scl_hi= scl_q==1'b1 && counter_q==half[counter_width-1:0] /*&& scl==1'b1*/; //scl is on the middle of a high(1) bit
|
| 1185 |
+
assign scl_lo= scl_q==1'b0 && counter_q==half[counter_width-1:0]; //scl is on the middle of a low(0) bit
|
| 1186 |
+
|
| 1187 |
+
endmodule
|
| 1188 |
+
|
| 1189 |
+
|
| 1190 |
+
|
| 1191 |
+
module rv32i_clint #( //Core Logic Interrupt
|
| 1192 |
+
parameter CLK_FREQ_MHZ = 12, //input clock frequency in MHz
|
| 1193 |
+
// A MTIMER device has two separate base addresses: one for the MTIME register and another for the MTIMECMP registers.
|
| 1194 |
+
parameter MTIME_BASE_ADDRESS = 8008,
|
| 1195 |
+
MTIMECMP_BASE_ADDRESS = 8016,
|
| 1196 |
+
MSIP_BASE_ADDRESS = 8024
|
| 1197 |
+
)(
|
| 1198 |
+
input wire clk,
|
| 1199 |
+
input wire rst_n,
|
| 1200 |
+
input wire i_wb_cyc,
|
| 1201 |
+
input wire i_wb_stb,
|
| 1202 |
+
input wire i_wb_we,
|
| 1203 |
+
input wire[31:0] i_wb_addr,
|
| 1204 |
+
input wire[31:0] i_wb_data,
|
| 1205 |
+
input wire[3:0] i_wb_sel,
|
| 1206 |
+
output reg o_wb_ack,
|
| 1207 |
+
output wire o_wb_stall,
|
| 1208 |
+
output reg[31:0] o_wb_data,
|
| 1209 |
+
// Interrupts
|
| 1210 |
+
output wire o_timer_interrupt,
|
| 1211 |
+
output wire o_software_interrupt
|
| 1212 |
+
);
|
| 1213 |
+
// This is based from RISC-V Advanced Core Local Interruptor
|
| 1214 |
+
// Specification: https://github.com/riscv/riscv-aclint/blob/main/riscv-aclint.adoc
|
| 1215 |
+
|
| 1216 |
+
// This RISC-V ACLINT specification defines a set of memory mapped devices which provide
|
| 1217 |
+
// inter-processor interrupts (IPI) and timer functionalities.
|
| 1218 |
+
// The MTIMER device provides machine-level timer functionality for a set of HARTs on a RISC-V platform.
|
| 1219 |
+
// It has a single fixed-frequency monotonic time counter (MTIME) register and a time
|
| 1220 |
+
// compare register (MTIMECMP) for each HART connected to the MTIMER device.
|
| 1221 |
+
reg[63:0] mtime = 0;
|
| 1222 |
+
reg[63:0] mtimecmp = {64{1'b1}};
|
| 1223 |
+
reg msip = 0; //Inter-processor (or software) interrupts
|
| 1224 |
+
assign o_wb_stall = 0;
|
| 1225 |
+
|
| 1226 |
+
//READ memory-mapped registers
|
| 1227 |
+
always @(posedge clk, negedge rst_n) begin
|
| 1228 |
+
if(!rst_n) begin
|
| 1229 |
+
o_wb_ack <= 0;
|
| 1230 |
+
o_wb_data <= 0;
|
| 1231 |
+
end
|
| 1232 |
+
else begin
|
| 1233 |
+
if(i_wb_stb && i_wb_cyc && !i_wb_we) begin //read the memory-mapped register
|
| 1234 |
+
if(i_wb_addr == MTIME_BASE_ADDRESS) o_wb_data <= mtime[31:0]; //first half
|
| 1235 |
+
else if(i_wb_addr == MTIME_BASE_ADDRESS + 4) o_wb_data <= mtime[63:32]; //second half
|
| 1236 |
+
if(i_wb_addr == MTIMECMP_BASE_ADDRESS) o_wb_data <= mtimecmp[31:0]; //first half
|
| 1237 |
+
else if(i_wb_addr == MTIMECMP_BASE_ADDRESS + 4) o_wb_data <= mtimecmp[63:32]; //second half
|
| 1238 |
+
if(i_wb_addr == MSIP_BASE_ADDRESS) o_wb_data <= {31'b0, msip}; //machine software interrupt
|
| 1239 |
+
end
|
| 1240 |
+
o_wb_ack <= i_wb_stb && i_wb_cyc; //wishbone protocol stb-ack mechanism
|
| 1241 |
+
end
|
| 1242 |
+
end
|
| 1243 |
+
|
| 1244 |
+
|
| 1245 |
+
//WRITE to memory-mapped registers
|
| 1246 |
+
always @(posedge clk, negedge rst_n) begin
|
| 1247 |
+
if(!rst_n) begin
|
| 1248 |
+
mtime <= 64'd0;
|
| 1249 |
+
mtimecmp <= {64{1'b1}}; //timer interrupt will be triggered unintentionally if reset at 0 (equal to mtime)
|
| 1250 |
+
//thus we set it at highest value (all 1s)
|
| 1251 |
+
msip <= 0;
|
| 1252 |
+
end
|
| 1253 |
+
else begin
|
| 1254 |
+
if(i_wb_stb && i_wb_cyc && i_wb_we) begin //write to the memory-mapped registers
|
| 1255 |
+
if(i_wb_addr == MTIME_BASE_ADDRESS) mtime[31:0] <= i_wb_data; //first half
|
| 1256 |
+
else if(i_wb_addr == MTIME_BASE_ADDRESS + 4) mtime[63:32] <= i_wb_data; //second half
|
| 1257 |
+
if(i_wb_addr == MTIMECMP_BASE_ADDRESS) mtimecmp[31:0] <= i_wb_data; //first half
|
| 1258 |
+
else if(i_wb_addr == MTIMECMP_BASE_ADDRESS + 4) mtimecmp[63:32] <= i_wb_data; //second half
|
| 1259 |
+
if(i_wb_addr == MSIP_BASE_ADDRESS) msip <= i_wb_data[0]; //machine software interrupt
|
| 1260 |
+
end
|
| 1261 |
+
mtime <= mtime + 1'b1; //increment every clock tick (so timer freq is same as cpu clock freq)
|
| 1262 |
+
end
|
| 1263 |
+
end
|
| 1264 |
+
|
| 1265 |
+
//Volume 2 pg. 44: Platforms provide a 64-bit memory-mapped machine-mode timer compare register (mtimecmp).
|
| 1266 |
+
//A machine timer interrupt becomes pending whenever mtime contains a value greater than or equal to mtimecmp,
|
| 1267 |
+
//treating the values as unsigned integers. The interrupt remains posted until mtimecmp becomes greater than
|
| 1268 |
+
//mtime (typically as a result of writing mtimecmp).
|
| 1269 |
+
assign o_timer_interrupt = (mtime >= mtimecmp);
|
| 1270 |
+
|
| 1271 |
+
//Each MSIP register is a 32-bit wide WARL register where the upper 31 bits are wired to zero.
|
| 1272 |
+
//The least significant bit is reflected in MSIP of the mip CSR. A machine-level software interrupt
|
| 1273 |
+
//for a HART is pending or cleared by writing 1 or 0 respectively to the corresponding MSIP register.
|
| 1274 |
+
assign o_software_interrupt = msip;
|
| 1275 |
+
|
| 1276 |
+
endmodule
|
| 1277 |
+
|
| 1278 |
+
|
| 1279 |
+
|
| 1280 |
+
module gpio #( //UART (TX only)
|
| 1281 |
+
parameter GPIO_MODE = 32'hF0, //set if GPIO will be read(0) or write(1)
|
| 1282 |
+
parameter GPIO_READ = 32'hF4, //read from GPIO
|
| 1283 |
+
parameter GPIO_WRITE = 32'hF8, //write to GPIO
|
| 1284 |
+
parameter GPIO_COUNT = 12
|
| 1285 |
+
)(
|
| 1286 |
+
input wire clk,
|
| 1287 |
+
input wire rst_n,
|
| 1288 |
+
// Wishbone Interface
|
| 1289 |
+
input wire i_wb_cyc,
|
| 1290 |
+
input wire i_wb_stb,
|
| 1291 |
+
input wire i_wb_we,
|
| 1292 |
+
input wire[31:0] i_wb_addr,
|
| 1293 |
+
input wire[GPIO_COUNT-1:0] i_wb_data,
|
| 1294 |
+
input wire[3:0] i_wb_sel,
|
| 1295 |
+
output reg o_wb_ack,
|
| 1296 |
+
output wire o_wb_stall,
|
| 1297 |
+
output reg[GPIO_COUNT-1:0] o_wb_data,
|
| 1298 |
+
//GPIO
|
| 1299 |
+
inout wire[11:0] gpio //gpio pins
|
| 1300 |
+
);
|
| 1301 |
+
|
| 1302 |
+
|
| 1303 |
+
reg[GPIO_COUNT-1:0] gpio_reg;
|
| 1304 |
+
reg[GPIO_COUNT-1:0] gpio_write;
|
| 1305 |
+
wire[GPIO_COUNT-1:0] gpio_read;
|
| 1306 |
+
reg[GPIO_COUNT-1:0] gpio_mode;
|
| 1307 |
+
|
| 1308 |
+
assign o_wb_stall = 0;
|
| 1309 |
+
always @(posedge clk,negedge rst_n) begin
|
| 1310 |
+
if(!rst_n) begin
|
| 1311 |
+
gpio_write <= 0;
|
| 1312 |
+
gpio_mode <= 0;
|
| 1313 |
+
gpio_reg <= 0;
|
| 1314 |
+
end
|
| 1315 |
+
else begin
|
| 1316 |
+
if(i_wb_stb && i_wb_we && i_wb_addr == GPIO_MODE) gpio_mode <= i_wb_data; //set mode of the gpio (write(1) or low(0))
|
| 1317 |
+
if(i_wb_stb && !i_wb_we && i_wb_addr == GPIO_MODE) o_wb_data <= gpio_mode; //read gpio mode
|
| 1318 |
+
if(i_wb_stb && i_wb_we && i_wb_addr == GPIO_WRITE) gpio_write <= i_wb_data; //write to gpio
|
| 1319 |
+
if(i_wb_stb && !i_wb_we && i_wb_addr == GPIO_WRITE) o_wb_data <= gpio_write; //read write value to gpio
|
| 1320 |
+
if(i_wb_stb && !i_wb_we && i_wb_addr == GPIO_READ) o_wb_data <= gpio_read; //read from gpio
|
| 1321 |
+
|
| 1322 |
+
o_wb_ack <= i_wb_stb;
|
| 1323 |
+
end
|
| 1324 |
+
end
|
| 1325 |
+
|
| 1326 |
+
`ifndef ICARUS
|
| 1327 |
+
genvar i;
|
| 1328 |
+
generate
|
| 1329 |
+
for(i = 0 ; i < GPIO_COUNT ; i = i+1) begin
|
| 1330 |
+
IOBUF gpio_iobuf ( //Vivado IOBUF instantiation
|
| 1331 |
+
.IO(gpio[i]),
|
| 1332 |
+
.I(gpio_write[i]),//write to GPIO when gpio_mode is high
|
| 1333 |
+
.T(!gpio_mode[i]),
|
| 1334 |
+
.O(gpio_read[i]) //read from GPIO when gpio_mode is low
|
| 1335 |
+
);
|
| 1336 |
+
end
|
| 1337 |
+
endgenerate
|
| 1338 |
+
`else
|
| 1339 |
+
genvar i;
|
| 1340 |
+
for(i = 0 ; i < GPIO_COUNT ; i = i+1) begin
|
| 1341 |
+
assign gpio[i] = gpio_mode[i]? gpio_write[i]:1'bz; //in icarus simulation we will only write to the pin
|
| 1342 |
+
end
|
| 1343 |
+
`endif
|
| 1344 |
+
|
| 1345 |
+
|
| 1346 |
+
|
| 1347 |
+
endmodule
|
| 1348 |
+
|
| 1349 |
+
|
| 1350 |
+
|
| 1351 |
+
|
| 1352 |
+
|
| 1353 |
+
|
| 1354 |
+
|
| 1355 |
+
|
| 1356 |
+
|
| 1357 |
+
|
AngeloJacobo_RISC-V/test/rv32i_soc_TB.v
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
`default_nettype none
|
| 3 |
+
`define DISPLAY
|
| 4 |
+
//`define HALT_ON_ILLEGAL_INSTRUCTION // stop core when instruction is illegal
|
| 5 |
+
//`define HALT_ON_EBREAK // halt core on ebreak
|
| 6 |
+
// `define HALT_ON_ECALL // halt core on ecall
|
| 7 |
+
`include "rv32i_header.vh"
|
| 8 |
+
|
| 9 |
+
module rv32i_soc_TB;
|
| 10 |
+
parameter MEMORY="memory.mem";
|
| 11 |
+
parameter ZICSR_EXTENSION = 1;
|
| 12 |
+
/******************************* MODIFY ****************************************/
|
| 13 |
+
localparam MEMORY_DEPTH = 81920, //number of memory bytes
|
| 14 |
+
DATA_START_ADDR = 32'h1004; //starting address of data memory to be displayed
|
| 15 |
+
/*******************************************************************************/
|
| 16 |
+
|
| 17 |
+
reg clk,rst_n;
|
| 18 |
+
reg temp;
|
| 19 |
+
integer i,j;
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
rv32i_soc #(.PC_RESET(32'h00_00_00_00), .MEMORY_DEPTH(MEMORY_DEPTH), .CLK_FREQ_MHZ(100), .TRAP_ADDRESS(32'h00000004), .ZICSR_EXTENSION(ZICSR_EXTENSION)) uut (
|
| 23 |
+
.i_clk(clk),
|
| 24 |
+
.i_rst(!rst_n)
|
| 25 |
+
);
|
| 26 |
+
|
| 27 |
+
always #5 clk=!clk; //100MHz clock
|
| 28 |
+
|
| 29 |
+
initial begin //2nd reset to test resetting while core is executing instruction
|
| 30 |
+
#200;
|
| 31 |
+
rst_n = 0;
|
| 32 |
+
#500;
|
| 33 |
+
rst_n = 1;
|
| 34 |
+
end
|
| 35 |
+
|
| 36 |
+
/*********************** initialize instruction memory and data memory **************************/
|
| 37 |
+
initial begin
|
| 38 |
+
#1;
|
| 39 |
+
$readmemh(MEMORY,uut.m1.memory_regfile); //write instruction and data to memory
|
| 40 |
+
//uut.m1.memory_regfile[{32'h0000_1000>>2}] = 32'h12345678; //initial data memory
|
| 41 |
+
end
|
| 42 |
+
/***********************************************************************************************/
|
| 43 |
+
reg[1024:0] cause;
|
| 44 |
+
|
| 45 |
+
initial begin
|
| 46 |
+
$dumpfile("wave.vcd");
|
| 47 |
+
$dumpvars(0,rv32i_soc_TB);
|
| 48 |
+
$dumpvars(0,uut.m0.m0.base_regfile[1],uut.m0.m0.base_regfile[2],uut.m0.m0.base_regfile[3],uut.m0.m0.base_regfile[4],uut.m0.m0.base_regfile[5]);
|
| 49 |
+
$dumpvars(0,uut.m0.m0.base_regfile[6],uut.m0.m0.base_regfile[7],uut.m0.m0.base_regfile[8],uut.m0.m0.base_regfile[9],uut.m0.m0.base_regfile[10]);
|
| 50 |
+
$dumpvars(0,uut.m0.m0.base_regfile[11],uut.m0.m0.base_regfile[12],uut.m0.m0.base_regfile[13],uut.m0.m0.base_regfile[14],uut.m0.m0.base_regfile[15]);
|
| 51 |
+
$dumpvars(0,uut.m0.m0.base_regfile[16],uut.m0.m0.base_regfile[17],uut.m0.m0.base_regfile[18],uut.m0.m0.base_regfile[19],uut.m0.m0.base_regfile[20]);
|
| 52 |
+
$dumpvars(0,uut.m0.m0.base_regfile[21],uut.m0.m0.base_regfile[22],uut.m0.m0.base_regfile[23],uut.m0.m0.base_regfile[24],uut.m0.m0.base_regfile[25]);
|
| 53 |
+
$dumpvars(0,uut.m0.m0.base_regfile[26],uut.m0.m0.base_regfile[27],uut.m0.m0.base_regfile[28],uut.m0.m0.base_regfile[29],uut.m0.m0.base_regfile[30]);
|
| 54 |
+
$dumpvars(0,uut.m0.m0.base_regfile[31]);
|
| 55 |
+
|
| 56 |
+
rst_n = 1;
|
| 57 |
+
#50;
|
| 58 |
+
clk=0;
|
| 59 |
+
rst_n=0;
|
| 60 |
+
#50;
|
| 61 |
+
|
| 62 |
+
rst_n=1; //release reset
|
| 63 |
+
|
| 64 |
+
$display("\nStart executing instructions......\n");
|
| 65 |
+
$display("Monitor All Writes to Base Register and Data Memory");
|
| 66 |
+
|
| 67 |
+
/**************************************************************************************************************************/
|
| 68 |
+
|
| 69 |
+
while( `ifdef HALT_ON_ILLEGAL_INSTRUCTION
|
| 70 |
+
uut.iaddr < MEMORY_DEPTH-4 && !(uut.m0.zicsr.m6.i_is_inst_illegal && uut.m0.zicsr.m6.i_ce) //exception testing (halt core only when instruction is illegal)
|
| 71 |
+
`elsif HALT_ON_EBREAK
|
| 72 |
+
!uut.m0.alu_exception[`EBREAK] //ebreak test (halt core on ebreak)
|
| 73 |
+
`elsif HALT_ON_ECALL
|
| 74 |
+
!uut.m0.alu_exception[`ECALL] //ecall test (halt core on ecall)
|
| 75 |
+
`else
|
| 76 |
+
!uut.m0.alu_exception[`ECALL] && !uut.m0.alu_exception[`EBREAK] //normal test (halt core on ebreak/ecall)
|
| 77 |
+
`endif
|
| 78 |
+
) begin
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@(negedge clk);
|
| 82 |
+
`ifdef DISPLAY
|
| 83 |
+
if(ZICSR_EXTENSION != 0) begin
|
| 84 |
+
if(!uut.m0.stall_memoryaccess && uut.m0.zicsr.m6.csr_enable) begin //csr is written
|
| 85 |
+
$display("\nPC: %h %h [%s]\n [CSR] address:0x%0h value:0x%h ",uut.m0.zicsr.m6.i_pc, uut.m1.memory_regfile[{uut.m0.zicsr.m6.i_pc}>>2],"SYSTEM",uut.m0.zicsr.m6.i_csr_index,uut.m0.zicsr.m6.csr_in); //display address of csr changed and its new value
|
| 86 |
+
end
|
| 87 |
+
end
|
| 88 |
+
|
| 89 |
+
if(uut.m0.writeback_ce && !uut.m0.stall_writeback) begin
|
| 90 |
+
if(uut.m0.memoryaccess_opcode[`RTYPE]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"RTYPE"); //Display PC and instruction
|
| 91 |
+
else if(uut.m0.memoryaccess_opcode[`ITYPE]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"ITYPE"); //Display PC and instruction
|
| 92 |
+
else if(uut.m0.memoryaccess_opcode[`LOAD]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"LOAD"); //Display PC and instruction
|
| 93 |
+
else if(uut.m0.memoryaccess_opcode[`STORE]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"STORE"); //Display PC and instruction
|
| 94 |
+
else if(uut.m0.memoryaccess_opcode[`BRANCH]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"BRANCH"); //Display PC and instruction
|
| 95 |
+
else if(uut.m0.memoryaccess_opcode[`JAL]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"JAL"); //Display PC and instruction
|
| 96 |
+
else if(uut.m0.memoryaccess_opcode[`JALR]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"JALR"); //Display PC and instruction
|
| 97 |
+
else if(uut.m0.memoryaccess_opcode[`LUI]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"LUI"); //Display PC and instruction
|
| 98 |
+
else if(uut.m0.memoryaccess_opcode[`AUIPC]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"AUIPC"); //Display PC and instruction
|
| 99 |
+
else if(uut.m0.memoryaccess_opcode[`SYSTEM]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"SYSTEM"); //Display PC and instruction
|
| 100 |
+
else if(uut.m0.memoryaccess_opcode[`FENCE]) $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"FENCE"); //Display PC and instruction
|
| 101 |
+
else $display("\nPC: %h %h [%s]", uut.m0.m5.i_pc, uut.m1.memory_regfile[{uut.m0.m5.i_pc}>>2],"UNKNOWN INSTRUCTION"); //Display PC and instruction
|
| 102 |
+
|
| 103 |
+
#1;
|
| 104 |
+
if(ZICSR_EXTENSION != 0) begin
|
| 105 |
+
if(uut.m0.csr_go_to_trap) begin //exception or interrupt detected
|
| 106 |
+
case({uut.m0.zicsr.m6.mcause_intbit,uut.m0.zicsr.m6.mcause_code})
|
| 107 |
+
{1'b1,4'd3}: $display(" GO TO TRAP: %s","SOFTWARE INTERRUPT");
|
| 108 |
+
{1'b1,4'd7}: $display(" GO TO TRAP: %s","TIMER INTERRUPT");
|
| 109 |
+
{1'b1,4'd11}: $display(" GO TO TRAP: %s","EXTERNAL INTERRUPT");
|
| 110 |
+
{1'b0,4'd0}: $display(" GO TO TRAP: %s","INSTRUCTION ADDRESS MISALIGNED");
|
| 111 |
+
{1'b0,4'd2}: $display(" GO TO TRAP: %s","ILLEGAL INSTRUCTION");
|
| 112 |
+
{1'b0,4'd3}: $display(" GO TO TRAP: %s","EBREAK");
|
| 113 |
+
{1'b0,4'd4}: $display(" GO TO TRAP: %s","LOAD ADDRESS MISALIGNED");
|
| 114 |
+
{1'b0,4'd6}: $display(" GO TO TRAP: %s","STORE ADDRESS MISALIGNED");
|
| 115 |
+
{1'b0,4'd11}: $display(" GO TO TRAP: %s","ECALL");
|
| 116 |
+
default: $display(" GO TO TRAP: %s","UNKNOWN TRAP");
|
| 117 |
+
endcase
|
| 118 |
+
end
|
| 119 |
+
end
|
| 120 |
+
if(uut.m1.i_wb_we) begin //data memory is written
|
| 121 |
+
$display(" [MEMORY] address:0x%h value:0x%h [MASK:%b]",uut.m1.i_wb_addr,uut.m1.i_wb_data,uut.m1.i_wb_sel); //display address of memory changed and its new value
|
| 122 |
+
end
|
| 123 |
+
|
| 124 |
+
if(uut.m0.m5.o_wr_rd && uut.m0.m5.o_rd_addr!=0) begin //base register is written
|
| 125 |
+
$display(" [BASEREG] address:0x%0d value:0x%h",uut.m0.m5.o_rd_addr,uut.m0.m5.o_rd); //display address of base reg changed and its new value
|
| 126 |
+
end
|
| 127 |
+
|
| 128 |
+
if(uut.m0.csr_return_from_trap) begin
|
| 129 |
+
$display(" RETURN FROM TRAP"); //go back from trap via mret
|
| 130 |
+
end
|
| 131 |
+
|
| 132 |
+
end
|
| 133 |
+
#1;
|
| 134 |
+
`endif
|
| 135 |
+
|
| 136 |
+
end
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@(negedge clk);
|
| 140 |
+
$display("\nAll instructions executed......");
|
| 141 |
+
|
| 142 |
+
/************* Dump Base Register and Memory Values *******************/
|
| 143 |
+
$display("\nFinal Register State:");
|
| 144 |
+
|
| 145 |
+
for(i=0; i<8; i=i+1) begin
|
| 146 |
+
for(j=0; j<4 ; j=j+1) begin
|
| 147 |
+
$write("0x%02d: 0x%h\t",4*i+j,uut.m0.m0.base_regfile[4*i+j]);
|
| 148 |
+
end
|
| 149 |
+
$write("\n");
|
| 150 |
+
end
|
| 151 |
+
$display("\n\nFinal Memory State:");
|
| 152 |
+
for(i=DATA_START_ADDR; i<(DATA_START_ADDR+10*4) ; i=i+4) begin
|
| 153 |
+
$display("0x%0h: 0x%h",i,uut.m1.memory_regfile[i>>2]);
|
| 154 |
+
end
|
| 155 |
+
|
| 156 |
+
/***********************************************************************/
|
| 157 |
+
if(ZICSR_EXTENSION != 0) begin
|
| 158 |
+
if(uut.m0.m0.base_regfile[17] == 32'h5d) begin //Exit test using RISC-V International's riscv-tests pass/fail criteria
|
| 159 |
+
if(uut.m0.m0.base_regfile[10] == 0)
|
| 160 |
+
$display("\nPASS: exit code = 0x%h \n[%0d instructions in %0d clk cycles]\n",uut.m0.m0.base_regfile[10]>>1,uut.m0.zicsr.m6.minstret,uut.m0.zicsr.m6.mcycle);
|
| 161 |
+
else begin
|
| 162 |
+
$display("\nFAIL: exit code = 0x%h \n[%0d instructions in %0d clk cycles]\n",uut.m0.m0.base_regfile[10]>>1,uut.m0.zicsr.m6.minstret,uut.m0.zicsr.m6.mcycle);
|
| 163 |
+
end
|
| 164 |
+
end
|
| 165 |
+
else $display("\nUNKNOWN: basereg[17] = 0x%h (must be 0x0000005d)",uut.m0.m0.base_regfile[17]);
|
| 166 |
+
end
|
| 167 |
+
else begin
|
| 168 |
+
if(uut.m0.m0.base_regfile[17] == 32'h5d) begin //Exit test using RISC-V International's riscv-tests pass/fail criteria
|
| 169 |
+
if(uut.m0.m0.base_regfile[10] == 0)
|
| 170 |
+
$display("\nPASS: exit code = 0x%h\n",uut.m0.m0.base_regfile[10]>>1);
|
| 171 |
+
else begin
|
| 172 |
+
$display("\nFAIL: exit code = 0x%h\n",uut.m0.m0.base_regfile[10]>>1);
|
| 173 |
+
end
|
| 174 |
+
end
|
| 175 |
+
else $display("\nUNKNOWN: basereg[17] = 0x%h (must be 0x0000005d)",uut.m0.m0.base_regfile[17]);
|
| 176 |
+
end
|
| 177 |
+
$stop;
|
| 178 |
+
|
| 179 |
+
/**************************************************************************************************************************/
|
| 180 |
+
|
| 181 |
+
end
|
| 182 |
+
initial begin
|
| 183 |
+
#100_000; //simulation time limit
|
| 184 |
+
`ifdef LONGER_SIM_LIMIT
|
| 185 |
+
#25_000_000;
|
| 186 |
+
`endif
|
| 187 |
+
$stop;
|
| 188 |
+
end
|
| 189 |
+
endmodule
|
AugustinJose1221_FPGA-Build/.github/FUNDING.yml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# These are supported funding model platforms
|
| 2 |
+
|
| 3 |
+
patreon: augustinjose
|
| 4 |
+
ko_fi: augustinjose
|
| 5 |
+
liberapay: augustinjose
|
| 6 |
+
custom: ['www.buymeacoffee.com/augustinjose']
|
AugustinJose1221_FPGA-Build/README.md
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[![Contributors][contributors-shield]][contributors-url]
|
| 2 |
+
[![Forks][forks-shield]][forks-url]
|
| 3 |
+
[![Stargazers][stars-shield]][stars-url]
|
| 4 |
+
[![Issues][issues-shield]][issues-url]
|
| 5 |
+
[![MIT License][license-shield]][license-url]
|
| 6 |
+
[![LinkedIn][linkedin-shield]][linkedin-url]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
<!-- PROJECT LOGO -->
|
| 11 |
+
<br />
|
| 12 |
+
<p align="center">
|
| 13 |
+
<a href="https://github.com/AugustinJose1221/FPGA-Build">
|
| 14 |
+
<img src="img/logo.png" alt="Logo" width="209" height="100">
|
| 15 |
+
</a>
|
| 16 |
+
|
| 17 |
+
<h3 align="center">FPGA Architecture for Real-time Video Stitching</h3>
|
| 18 |
+
|
| 19 |
+
<p align="center">
|
| 20 |
+
A novel architectural design for stitching video streams in real-time on an FPGA.
|
| 21 |
+
<br />
|
| 22 |
+
<a href="https://github.com/AugustinJose1221/FPGA-Build"><strong>Explore the docs »</strong></a>
|
| 23 |
+
<br />
|
| 24 |
+
<br />
|
| 25 |
+
</p>
|
| 26 |
+
</p>
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
<!-- TABLE OF CONTENTS -->
|
| 31 |
+
<details open="open">
|
| 32 |
+
<summary>Table of Contents</summary>
|
| 33 |
+
<ol>
|
| 34 |
+
<li>
|
| 35 |
+
<a href="#about-the-project">About The Project</a>
|
| 36 |
+
<ul>
|
| 37 |
+
<li><a href="#algorithm">Algorithm</a></li>
|
| 38 |
+
<li><a href="#top-level-design">Top Level Design</a></li>
|
| 39 |
+
</ul>
|
| 40 |
+
</li>
|
| 41 |
+
<li>
|
| 42 |
+
<a href="#getting-started">Getting Started</a>
|
| 43 |
+
<ul>
|
| 44 |
+
<li><a href="#prerequisites">Prerequisites</a></li>
|
| 45 |
+
<li><a href="#installation">Installation</a></li>
|
| 46 |
+
</ul>
|
| 47 |
+
</li>
|
| 48 |
+
<li><a href="#usage">Usage</a></li>
|
| 49 |
+
<li><a href="#roadmap">Roadmap</a></li>
|
| 50 |
+
<li><a href="#contributing">Contributing</a></li>
|
| 51 |
+
<li><a href="#license">License</a></li>
|
| 52 |
+
<li><a href="#contact">Contact</a></li>
|
| 53 |
+
</ol>
|
| 54 |
+
</details>
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
<!-- ABOUT THE PROJECT -->
|
| 59 |
+
## About The Project
|
| 60 |
+
The designed architecture generates a video having a wider feild of view by stitching two video input based on features and keypoints. In simple terms, the output generated will be a panorama but with video. The architecture is optimized such that the output can be produced in real-time.
|
| 61 |
+
|
| 62 |
+
### Algorithm
|
| 63 |
+
The figure below illustrates the block diagram of the system depicting each step of the algorithm.
|
| 64 |
+
|
| 65 |
+

|
| 66 |
+
|
| 67 |
+
The system can be broadly divided into three subystems:
|
| 68 |
+
* Preprocessing
|
| 69 |
+
* SIFT Based Feature Extraction
|
| 70 |
+
* Frame Stitching
|
| 71 |
+
|
| 72 |
+
#### Preprocessing
|
| 73 |
+
|
| 74 |
+
The input video stream for the system is in 8 bit RGB format. The input 8 bit image is shown in figure. Each individual frame of the video stream will have three channels corresponding to red, green and blue. The colour information in the video frames does not enhance feature detection. Moreover, computation on a 3 channel 8 bit image takes more time compared to a single channel 8 bit image. Therefore, the RGB video frame is converted to an 8 bit grayscale image. The generated grayscale images will have lesser noise, more details in the shadows and provides better computational efficiency, shown in figure.
|
| 75 |
+
|
| 76 |
+
|  |  |
|
| 77 |
+
|:---:|:---:|
|
| 78 |
+
| Input image | Grayscale image |
|
| 79 |
+
|
| 80 |
+
#### SIFT Based Feature Extraction
|
| 81 |
+
|
| 82 |
+
Feature extraction from the grayscale images is done using SIFT algorithm. SIFT algorithm can be separated into two main steps:
|
| 83 |
+
* Keypoint Detection
|
| 84 |
+
|
| 85 |
+
SIFT operation begins with discrete convolution of the input image with different Gaussian filters. A Gaussian filter is a widely used image smoothing algorithm defined as:
|
| 86 |
+
<p align="center">
|
| 87 |
+
<img src="https://latex.codecogs.com/svg.latex?\Large&space;G(x,%20y,%20\sigma%20)%20=\frac{1}{2\pi%20\sigma%20^{2}}%20e^{-\frac{(x^{2}%20+%20y^{2})}{2\sigma%20^{2}}}">
|
| 88 |
+
</p>
|
| 89 |
+
In the above equation, G is the Gaussian kernel at the point (x, y) and σ is the Gaussian parameter. Using a larger value of σ produces a greater smoothing effect on the image. Discrete convolution of the image with Gaussian kernel generates an image with lesser noise and lesser details. In SIFT, discrete convolution with Gaussian kernel is done with four different values of σ. Progressively higher values of σ is used to generate a set of blurred images or an octave.
|
| 90 |
+
|  |  |  |  |  |
|
| 91 |
+
|:---:|:---:|:---:|:---:|:---:|
|
| 92 |
+
| Input image | Sigma = 1.6 | Sigma = 2.26 | Sigma = 3.2 | Sigma = 4.5 |
|
| 93 |
+
|
| 94 |
+
For a given value of σ, the sum of all coefficients in the convolution kernal should be equal to unity. Therefore, the size of the kernal increases as the value of σ increases.
|
| 95 |
+
|
| 96 |
+
Once the octave is generated, a DoG space is built based on the four images in the octave. DoG stands for difference of Gaussian. DoG is a very computationally efficient approximation of Laplacian of Gaussian (LoG). The DoG space is built by computing the difference between two adjacent Gaussian scale images, pixel by pixel. DoG space of four images in the octave will have three levels.
|
| 97 |
+
|  |  |  |
|
| 98 |
+
|:---:|:---:|:---:|
|
| 99 |
+
| Top level DoG | Middle level DoG | Bottom level DoG |
|
| 100 |
+
|
| 101 |
+
Keypoints are extracted from the DoG space by finding the local maxima or minima values. A pixel is considered a keypoint if it is a local maxima or minima within a 26 pixel neighbourhood consisting of 9 pixels in the top level, 8 pixels in the middle level and 9 pixels in the bottom level.
|
| 102 |
+
<p align = "center">
|
| 103 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/res/KEYPOINTS4.jpg"> <br>
|
| 104 |
+
Keypoints
|
| 105 |
+
</p>
|
| 106 |
+
|
| 107 |
+
|  |  |  |
|
| 108 |
+
|:---:|:---:|:---:|
|
| 109 |
+
| Keypoints using OpenCV sift function | Keypoints using SIFT implementation in Python | Keypoint generated by the FPGA design |
|
| 110 |
+
* Descriptor Generation
|
| 111 |
+
|
| 112 |
+
Keypoint descriptor is a unique identifier for a particular keypoint. SIFT uses gradient magnitude and direction of the keypoint as the basis for the descriptor. Gradient magnitude and direction at a point can be calculated by discrete convolution of the image with Sobel filters.
|
| 113 |
+
<p align = "center">
|
| 114 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/res/SOBEL1.jpg"> <br>
|
| 115 |
+
Sobel convolution output
|
| 116 |
+
</p>
|
| 117 |
+
To generate the keypoint descriptor, gradient magnitude and direction of every point inside a 16x16 window around each keypoint is calculated. The gradient magnitudes of the 16x16 window is convolved with a Gaussian kernel. The gradient magnitudes in every 4x4 cell is combined such that the 16x16 window is reduced to a 4x4 window and 16 gradient directions. Finally, these 16 gradient directions are transferred into eight bins. Hence a 128 element vector is built which acts as the keypoint descriptor.
|
| 118 |
+
|
| 119 |
+
#### Frame Stitching
|
| 120 |
+
|
| 121 |
+
Frame stitching is the process of combining two frames into a single image. Frame stitching is done in two steps:
|
| 122 |
+
* Keypoint Matching
|
| 123 |
+
|
| 124 |
+
The keypoint descriptors of keypoints in the video frames from both camera sensors are compared. If the difference between the keypoint descriptors of two keypoints, one from each camera sensor, is below a error threshold, then they are considered as a keypoint pair. The keypoint pair with the least difference between their keypoint descriptors is taken as the reference keypoints.
|
| 125 |
+
|  |  |
|
| 126 |
+
|:---:|:---:|
|
| 127 |
+
| Input image from left camera | Input image from right camera |
|
| 128 |
+
* Image Blending
|
| 129 |
+
|
| 130 |
+
A weighed average method is used to blend the two frames into a single image. The values of pixels in the overlapped region is equal to the weighted average values of pixels of both the frames. The weights are chosen based on the distance between the overlapped pixel and the border of the corresponding frame.
|
| 131 |
+
<p align = "center">
|
| 132 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/res/STITCH1.jpg"> <br>
|
| 133 |
+
Stitched image
|
| 134 |
+
</p>
|
| 135 |
+
### Top Level Design
|
| 136 |
+
The block schematic of the architecture from top level is shown in figure below.
|
| 137 |
+
<p align = "center">
|
| 138 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/img/Diagram0.png"> <br>
|
| 139 |
+
Block Schematic
|
| 140 |
+
</p>
|
| 141 |
+
|
| 142 |
+
The top level design is divided into five stages:
|
| 143 |
+
* [Preprocessing stage](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/README.md#preprocessing-stage)
|
| 144 |
+
* [Filter stage](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/README.md#filter-stage)
|
| 145 |
+
* [Keypoint stage](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/README.md#keypoint-stage)
|
| 146 |
+
* [Keypoint matching stage](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/README.md#keypoint-matching-stage)
|
| 147 |
+
* [Frame blending stage](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/README.md#frame-blending-stage)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
<!-- GETTING STARTED -->
|
| 151 |
+
## Getting Started
|
| 152 |
+
### Prerequisites
|
| 153 |
+
The following packages needs to be installed on the Linux system before executing the source code.
|
| 154 |
+
* Icarus Verilog
|
| 155 |
+
```sh
|
| 156 |
+
apt-get install iverilog
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
* Python
|
| 160 |
+
```sh
|
| 161 |
+
apt-get install python3
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
* OpenCV
|
| 165 |
+
```sh
|
| 166 |
+
pip3 install opencv-contrib-python
|
| 167 |
+
```
|
| 168 |
+
* numpy
|
| 169 |
+
```sh
|
| 170 |
+
pip3 install numpy
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
* PIL (Python Image Library)
|
| 174 |
+
```sh
|
| 175 |
+
pip3 install pillow
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
### Installation
|
| 179 |
+
|
| 180 |
+
1. Clone the repo
|
| 181 |
+
```sh
|
| 182 |
+
git clone https://github.com/AugustinJose1221/FPGA-Build.git
|
| 183 |
+
```
|
| 184 |
+
2. Change working directory
|
| 185 |
+
```sh
|
| 186 |
+
cd FPGA-Build/make
|
| 187 |
+
```
|
| 188 |
+
3. Compile the design
|
| 189 |
+
```sh
|
| 190 |
+
make create
|
| 191 |
+
```
|
| 192 |
+
4. To view the RTL waveform
|
| 193 |
+
```sh
|
| 194 |
+
make simulate
|
| 195 |
+
```
|
| 196 |
+
5. Generate output image
|
| 197 |
+
```sh
|
| 198 |
+
python3 hexToImage.py
|
| 199 |
+
```
|
| 200 |
+
<!-- USAGE EXAMPLES -->
|
| 201 |
+
## Usage
|
| 202 |
+
### Project Tree
|
| 203 |
+
* [Templates](./Templates)
|
| 204 |
+
* [controller_tb.vcd](./Templates/controller_tb.vcd)
|
| 205 |
+
* [controller.v](./Templates/controller.v)
|
| 206 |
+
* [slave_1_tb.v](./Templates/slave_1_tb.v)
|
| 207 |
+
* [slave_1_tb.vcd](./Templates/slave_1_tb.vcd)
|
| 208 |
+
* [slave_1.v](./Templates/slave_1.v)
|
| 209 |
+
* [controller_tb.v](./Templates/controller_tb.v)
|
| 210 |
+
* [Makefile](./Templates/Makefile)
|
| 211 |
+
* [README.md](./Templates/README.md)
|
| 212 |
+
* [filter.v](./Templates/filter.v)
|
| 213 |
+
* [gaussian.v](./Templates/gaussian.v)
|
| 214 |
+
* [vcd](./vcd)
|
| 215 |
+
* [BRWM_tb.vcd](./vcd/BRWM_tb.vcd)
|
| 216 |
+
* [README.md](./vcd/README.md)
|
| 217 |
+
* [filter5x5.vcd](./vcd/filter5x5.vcd)
|
| 218 |
+
* [image.vcd](./vcd/image.vcd)
|
| 219 |
+
* [video_stitcher_tb.vcd](./vcd/video_stitcher_tb.vcd)
|
| 220 |
+
* [interface_tb.vcd](./vcd/interface_tb.vcd)
|
| 221 |
+
* [design](./design)
|
| 222 |
+
* [README.md](./design/README.md)
|
| 223 |
+
* [Controller.v](./design/Controller.v)
|
| 224 |
+
* [matcher.v](./design/matcher.v)
|
| 225 |
+
* [display.v](./design/display.v)
|
| 226 |
+
* [camera.v](./design/camera.v)
|
| 227 |
+
* [descriptor.v](./design/descriptor.v)
|
| 228 |
+
* [filter5x5.v](./design/filter5x5.v)
|
| 229 |
+
* [Grayscaler.v](./design/Grayscaler.v)
|
| 230 |
+
* [image.v](./design/image.v)
|
| 231 |
+
* [image2.v](./design/image2.v)
|
| 232 |
+
* [keypoints.v](./design/keypoints.v)
|
| 233 |
+
* [RWM_1.v](./design/RWM_1.v)
|
| 234 |
+
* [RWM_2.v](./design/RWM_2.v)
|
| 235 |
+
* [sobel_filter.v](./design/sobel_filter.v)
|
| 236 |
+
* [stitcher.v](./design/stitcher.v)
|
| 237 |
+
* [res](./res)
|
| 238 |
+
* [CV2SIFT.jpg](./res/CV2SIFT.jpg)
|
| 239 |
+
* [DIFF1.jpg](./res/DIFF1.jpg)
|
| 240 |
+
* [DIFF2.jpg](./res/DIFF2.jpg)
|
| 241 |
+
* [DIFF3.jpg](./res/DIFF3.jpg)
|
| 242 |
+
* [data1.txt](./res/data1.txt)
|
| 243 |
+
* [data2.txt](./res/data2.txt)
|
| 244 |
+
* [data.txt](./res/data.txt)
|
| 245 |
+
* [FilterOut3.jpg](./res/FilterOut3.jpg)
|
| 246 |
+
* [FILTEROUT3.jpg](./res/FILTEROUT3.jpg)
|
| 247 |
+
* [FilterOut5.jpg](./res/FilterOut5.jpg)
|
| 248 |
+
* [FILTEROUT5.jpg](./res/FILTEROUT5.jpg)
|
| 249 |
+
* [FILTEROUT6.jpg](./res/FILTEROUT6.jpg)
|
| 250 |
+
* [FilterOut7.jpg](./res/FilterOut7.jpg)
|
| 251 |
+
* [FILTEROUT7.jpg](./res/FILTEROUT7.jpg)
|
| 252 |
+
* [FilterOut9.jpg](./res/FilterOut9.jpg)
|
| 253 |
+
* [Final1.jpg](./res/Final1.jpg)
|
| 254 |
+
* [Final2.jpg](./res/Final2.jpg)
|
| 255 |
+
* [Final.jpg](./res/Final.jpg)
|
| 256 |
+
* [Grayscale-Out.jpg](./res/Grayscale-Out.jpg)
|
| 257 |
+
* [KEYPOINTS1.jpg](./res/KEYPOINTS1.jpg)
|
| 258 |
+
* [KEYPOINTS2.jpg](./res/KEYPOINTS2.jpg)
|
| 259 |
+
* [KEYPOINTS3.jpg](./res/KEYPOINTS3.jpg)
|
| 260 |
+
* [KEYPOINTS4.jpg](./res/KEYPOINTS4.jpg)
|
| 261 |
+
* [KEYPOINTS5.jpg](./res/KEYPOINTS5.jpg)
|
| 262 |
+
* [KEYPOINTS6.jpg](./res/KEYPOINTS6.jpg)
|
| 263 |
+
* [OUT1.jpg](./res/OUT1.jpg)
|
| 264 |
+
* [OUT2.jpg](./res/OUT2.jpg)
|
| 265 |
+
* [Out.jpg](./res/Out.jpg)
|
| 266 |
+
* [OUT.jpg](./res/OUT.jpg)
|
| 267 |
+
* [random.jpg](./res/random.jpg)
|
| 268 |
+
* [sift.py](./res/sift.py)
|
| 269 |
+
* [SIGMA_1_6.jpg](./res/SIGMA_1_6.jpg)
|
| 270 |
+
* [SIGMA_2_26.jpg](./res/SIGMA_2_26.jpg)
|
| 271 |
+
* [SIGMA_3_2.jpg](./res/SIGMA_3_2.jpg)
|
| 272 |
+
* [SIGMA_4_5.jpg](./res/SIGMA_4_5.jpg)
|
| 273 |
+
* [SUBKEYPOINTS1.jpg](./res/SUBKEYPOINTS1.jpg)
|
| 274 |
+
* [left.jpg](./res/left.jpg)
|
| 275 |
+
* [left.txt](./res/left.txt)
|
| 276 |
+
* [right.jpg](./res/right.jpg)
|
| 277 |
+
* [right.txt](./res/right.txt)
|
| 278 |
+
* [SOBEL1.jpg](./res/SOBEL1.jpg)
|
| 279 |
+
* [SOBEL.jpg](./res/SOBEL.jpg)
|
| 280 |
+
* [SOBEL_X.jpg](./res/SOBEL_X.jpg)
|
| 281 |
+
* [SOBEL_Y.jpg](./res/SOBEL_Y.jpg)
|
| 282 |
+
* [imageToHex.py](./res/imageToHex.py)
|
| 283 |
+
* [STITCH1.jpg](./res/STITCH1.jpg)
|
| 284 |
+
* [out.txt](./res/out.txt)
|
| 285 |
+
* [hexToImage.py](./res/hexToImage.py)
|
| 286 |
+
* [make](./make)
|
| 287 |
+
* [Makefile](./make/Makefile)
|
| 288 |
+
* [outfiles](./outfiles)
|
| 289 |
+
* [display](./outfiles/display)
|
| 290 |
+
* [FILTER](./outfiles/FILTER)
|
| 291 |
+
* [Gaussian](./outfiles/Gaussian)
|
| 292 |
+
* [image](./outfiles/image)
|
| 293 |
+
* [interface](./outfiles/interface)
|
| 294 |
+
* [output.bin](./outfiles/output.bin)
|
| 295 |
+
* [testbenches](./testbenches)
|
| 296 |
+
* [BRWM_tb.v](./testbenches/BRWM_tb.v)
|
| 297 |
+
* [README.md](./testbenches/README.md)
|
| 298 |
+
* [video_stitcher_tb.v](./testbenches/video_stitcher_tb.v)
|
| 299 |
+
* [interface_tb.v](./testbenches/interface_tb.v)
|
| 300 |
+
* [top_tb.v](./testbenches/top_tb.v)
|
| 301 |
+
* [display_tb.v](./testbenches/display_tb.v)
|
| 302 |
+
* [filter5x5_tb.v](./testbenches/filter5x5_tb.v)
|
| 303 |
+
* [image_tb.v](./testbenches/image_tb.v)
|
| 304 |
+
* [interface.v](./testbenches/interface.v)
|
| 305 |
+
* [top.v](./testbenches/top.v)
|
| 306 |
+
* [img](./img)
|
| 307 |
+
* [System Design.jpg](./img/System%20Design.jpg)
|
| 308 |
+
* [Final1.jpg](./img/Final1.jpg)
|
| 309 |
+
* [OUT2.jpg](./img/OUT2.jpg)
|
| 310 |
+
* [README.md](./README.md)
|
| 311 |
+
* [tree.sh](./tree.sh)
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
<!-- ROADMAP -->
|
| 315 |
+
## Roadmap
|
| 316 |
+
|
| 317 |
+
See the [open issues](https://github.com/AugustinJose1221/FPGA-Build/issues) for a list of proposed features (and known issues).
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
<!-- CONTRIBUTING -->
|
| 322 |
+
## Contributing
|
| 323 |
+
Any contributions you make are **greatly appreciated**.
|
| 324 |
+
|
| 325 |
+
1. Fork the Project
|
| 326 |
+
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
|
| 327 |
+
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
|
| 328 |
+
4. Push to the Branch (`git push origin feature/AmazingFeature`)
|
| 329 |
+
5. Open a Pull Request
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
<!-- LICENSE -->
|
| 334 |
+
## License
|
| 335 |
+
|
| 336 |
+
Distributed under the MIT License. See `LICENSE` for more information.
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
<!-- CONTACT -->
|
| 341 |
+
## Contact
|
| 342 |
+
|
| 343 |
+
Twitter: [@augustinjose121](https://twitter.com/augustinjose121) <br>
|
| 344 |
+
Gmail: [augustinjose1221@gmail..com](https://mail.google.com/mail/?view=cm&fs=1&to=augustinjose1221@gmail.com) <br>
|
| 345 |
+
Discuss: [Github Discussions](https://github.com/AugustinJose1221/FPGA-Build/discussions) <br>
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
<!-- MARKDOWN LINKS & IMAGES -->
|
| 354 |
+
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
|
| 355 |
+
[contributors-shield]: https://img.shields.io/github/contributors/AugustinJose1221/FPGA-Build.svg?style=for-the-badge
|
| 356 |
+
[contributors-url]: https://github.com/AugustinJose1221/FPGA-Build/graphs/contributors
|
| 357 |
+
[forks-shield]: https://img.shields.io/github/forks/AugustinJose1221/FPGA-Build.svg?style=for-the-badge
|
| 358 |
+
[forks-url]: https://github.com/AugustinJose1221/FPGA-Build/network/members
|
| 359 |
+
[stars-shield]: https://img.shields.io/github/stars/AugustinJose1221/FPGA-Build.svg?style=for-the-badge
|
| 360 |
+
[stars-url]: https://github.com/AugustinJose1221/FPGA-Build/stargazers
|
| 361 |
+
[issues-shield]: https://img.shields.io/github/issues/AugustinJose1221/FPGA-Build.svg?style=for-the-badge
|
| 362 |
+
[issues-url]: https://github.com/AugustinJose1221/FPGA-Build/issues
|
| 363 |
+
[license-shield]: https://img.shields.io/github/license/AugustinJose1221/FPGA-Build.svg?style=for-the-badge
|
| 364 |
+
[license-url]: https://github.com/AugustinJose1221/FPGA-Build/blob/master/LICENSE.txt
|
| 365 |
+
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
|
| 366 |
+
[linkedin-url]: https://linkedin.com/in/augustin-jose1221
|
| 367 |
+
[product-screenshot]: images/screenshot.png
|
AugustinJose1221_FPGA-Build/Templates/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
This folder contains a set of module templates that is used accross all the design files.
|
AugustinJose1221_FPGA-Build/Templates/controller.v
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
module controller(
|
| 3 |
+
input clk, //clock
|
| 4 |
+
input rst_n, //external reset
|
| 5 |
+
input done_1, //
|
| 6 |
+
input done_2,
|
| 7 |
+
input start, //external start command
|
| 8 |
+
output trigger_1, //
|
| 9 |
+
output trigger_2
|
| 10 |
+
);
|
| 11 |
+
|
| 12 |
+
parameter [1:0] IDLE = 2'b00, SLAVE_1 = 2'b01, SLAVE_2 = 2'b10;
|
| 13 |
+
reg [1:0] CS, NS;
|
| 14 |
+
|
| 15 |
+
always @(posedge clk or negedge rst_n)
|
| 16 |
+
begin
|
| 17 |
+
if (~rst_n)
|
| 18 |
+
CS <= IDLE;
|
| 19 |
+
else CS <= NS;
|
| 20 |
+
end
|
| 21 |
+
|
| 22 |
+
always @(start, done_1, done_2)
|
| 23 |
+
begin
|
| 24 |
+
case(CS)
|
| 25 |
+
IDLE: begin
|
| 26 |
+
if (start == 1'b1)
|
| 27 |
+
NS = SLAVE_1;
|
| 28 |
+
else NS = IDLE;
|
| 29 |
+
end
|
| 30 |
+
SLAVE_1: begin
|
| 31 |
+
if (done_1 == 1'b1)
|
| 32 |
+
NS = SLAVE_2;
|
| 33 |
+
else NS = SLAVE_1;
|
| 34 |
+
end
|
| 35 |
+
SLAVE_2: begin
|
| 36 |
+
if (done_2 == 1'b1)
|
| 37 |
+
NS = IDLE;
|
| 38 |
+
else NS = SLAVE_2;
|
| 39 |
+
end
|
| 40 |
+
default: NS = IDLE;
|
| 41 |
+
endcase
|
| 42 |
+
end
|
| 43 |
+
|
| 44 |
+
assign trigger_1 = (CS == SLAVE_1) ? 1'b1 : 1'b0;
|
| 45 |
+
assign trigger_2 = (CS == SLAVE_2) ? 1'b1 : 1'b0;
|
| 46 |
+
|
| 47 |
+
endmodule
|
AugustinJose1221_FPGA-Build/Templates/controller_tb.v
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
module controller_tb();
|
| 4 |
+
|
| 5 |
+
reg clk, rst_n, start;
|
| 6 |
+
wire done_1, done_2, trigger_1, trigger_2;
|
| 7 |
+
wire [3:0] slave_out_1, slave_out_2;
|
| 8 |
+
|
| 9 |
+
slave_1 one (clk, rst_n, trigger_1, done_1, slave_out_1);
|
| 10 |
+
slave_1 two (clk, rst_n, trigger_2, done_2, slave_out_2);
|
| 11 |
+
controller control (clk, rst_n, done_1, done_2, start, trigger_1, trigger_2);
|
| 12 |
+
|
| 13 |
+
initial
|
| 14 |
+
begin
|
| 15 |
+
$dumpfile("controller_tb.vcd");
|
| 16 |
+
$dumpvars(0, controller_tb);
|
| 17 |
+
|
| 18 |
+
rst_n = 0;
|
| 19 |
+
start = 0;
|
| 20 |
+
#10;
|
| 21 |
+
rst_n = 1;
|
| 22 |
+
start = 1;
|
| 23 |
+
#200;
|
| 24 |
+
$finish;
|
| 25 |
+
end
|
| 26 |
+
|
| 27 |
+
always
|
| 28 |
+
begin
|
| 29 |
+
clk = 1'b1;
|
| 30 |
+
#5;
|
| 31 |
+
clk = 1'b0;
|
| 32 |
+
#5;
|
| 33 |
+
end
|
| 34 |
+
endmodule
|
AugustinJose1221_FPGA-Build/Templates/filter.v
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Working of this module:
|
| 3 |
+
It uses two reg arrays, one to store the image of resolution n x m pixels in one frame and the other to select the required pixels for the convolution.
|
| 4 |
+
Here one pixel is considered to be of 1 byte wide. To do this, the entire operation is divided into 3 subtasks:-
|
| 5 |
+
a) storage of the n x m pixels
|
| 6 |
+
b) selecting the 9 bytes needed to perform the sobel filter convolution
|
| 7 |
+
c) performing the 3 x 3 sobel convolution
|
| 8 |
+
This cycle repeats for every subsequent frames. A FSM with 4 states is defined to sequence these subtasks correctly.
|
| 9 |
+
*/
|
| 10 |
+
`timescale 1ns/1ns
|
| 11 |
+
|
| 12 |
+
module filter(
|
| 13 |
+
input [7:0] Din,
|
| 14 |
+
input data_valid, rst, clk,
|
| 15 |
+
output fill_now, //status signal to indicate whether the first reg matrix is empty or not
|
| 16 |
+
output [15:0] Dout
|
| 17 |
+
);
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
reg [7:0] storage[0:N*M-1]; //reg array to store the image pixels in a frame of operation
|
| 21 |
+
reg [7:0] image_kernal[0:24]; //reg array to house the 9 bytes for the sobel convolution
|
| 22 |
+
reg [15:0] result;
|
| 23 |
+
reg [15:0] result0;
|
| 24 |
+
reg [15:0] result1;
|
| 25 |
+
reg [15:0] result2;
|
| 26 |
+
reg [15:0] result3;
|
| 27 |
+
reg [15:0] result4;
|
| 28 |
+
reg [1:0] PS, NS;
|
| 29 |
+
|
| 30 |
+
integer count, i, j, k;
|
| 31 |
+
|
| 32 |
+
parameter IDLE = 2'b00, STORE = 2'b01, FIX = 2'b10, CONVOLUTE = 2'b11;
|
| 33 |
+
parameter N = 450, M = 600; //resolution of the image
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
//sequential logic
|
| 37 |
+
always @(posedge clk or posedge rst)
|
| 38 |
+
begin
|
| 39 |
+
if (~rst)
|
| 40 |
+
PS <= IDLE; //every value is reset to its default value in IDLE
|
| 41 |
+
else PS <= NS;
|
| 42 |
+
end
|
| 43 |
+
|
| 44 |
+
always @(posedge clk)
|
| 45 |
+
begin
|
| 46 |
+
if(data_valid)//PS == STORE)
|
| 47 |
+
begin
|
| 48 |
+
storage[i] <= Din; //to store the incoming pixel byte into the next position in the storage array
|
| 49 |
+
i <= (i == N*M-1) ? 0 : i + 1;
|
| 50 |
+
end
|
| 51 |
+
else i <= 0;
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
+
//combinatorial logic
|
| 55 |
+
always @(Din,data_valid,i,count,k,PS)
|
| 56 |
+
begin
|
| 57 |
+
case (PS)
|
| 58 |
+
IDLE: begin
|
| 59 |
+
result = 16'h0000;
|
| 60 |
+
result0 = 16'h0000;
|
| 61 |
+
result1 = 16'h0000;
|
| 62 |
+
result2 = 16'h0000;
|
| 63 |
+
result3 = 16'h0000;
|
| 64 |
+
result4 = 16'h0000;
|
| 65 |
+
count = 0; //to count the number of convolutions in a frame
|
| 66 |
+
j = 0;
|
| 67 |
+
k = 0;
|
| 68 |
+
|
| 69 |
+
if(data_valid)
|
| 70 |
+
NS = STORE;
|
| 71 |
+
else NS = IDLE;
|
| 72 |
+
end
|
| 73 |
+
STORE: begin
|
| 74 |
+
NS = (i == N*M-1) ? FIX : STORE;
|
| 75 |
+
end
|
| 76 |
+
FIX: begin
|
| 77 |
+
//to place the the required bytes in the kernal for convolution
|
| 78 |
+
image_kernal[0] = storage[j];
|
| 79 |
+
image_kernal[1] = storage[j+1];
|
| 80 |
+
image_kernal[2] = storage[j+2];
|
| 81 |
+
image_kernal[3] = storage[j+3];
|
| 82 |
+
image_kernal[4] = storage[j+4];
|
| 83 |
+
|
| 84 |
+
image_kernal[5] = storage[j+M];
|
| 85 |
+
image_kernal[6] = storage[j+M+1];
|
| 86 |
+
image_kernal[7] = storage[j+M+2];
|
| 87 |
+
image_kernal[8] = storage[j+M+3];
|
| 88 |
+
image_kernal[9] = storage[j+M+4];
|
| 89 |
+
|
| 90 |
+
image_kernal[10] = storage[j+(2*M)];
|
| 91 |
+
image_kernal[11] = storage[j+(2*M)+1];
|
| 92 |
+
image_kernal[12] = storage[j+(2*M)+2];
|
| 93 |
+
image_kernal[13] = storage[j+(2*M)+3];
|
| 94 |
+
image_kernal[14] = storage[j+(2*M)+4];
|
| 95 |
+
|
| 96 |
+
image_kernal[15] = storage[j+(3*M)];
|
| 97 |
+
image_kernal[16] = storage[j+(3*M)+1];
|
| 98 |
+
image_kernal[17] = storage[j+(3*M)+2];
|
| 99 |
+
image_kernal[18] = storage[j+(3*M)+3];
|
| 100 |
+
image_kernal[19] = storage[j+(3*M)+4];
|
| 101 |
+
|
| 102 |
+
image_kernal[20] = storage[j+(4*M)];
|
| 103 |
+
image_kernal[21] = storage[j+(4*M)+1];
|
| 104 |
+
image_kernal[22] = storage[j+(4*M)+2];
|
| 105 |
+
image_kernal[23] = storage[j+(4*M)+3];
|
| 106 |
+
image_kernal[23] = storage[j+(4*M)+4];
|
| 107 |
+
|
| 108 |
+
NS = CONVOLUTE;
|
| 109 |
+
end
|
| 110 |
+
CONVOLUTE: begin
|
| 111 |
+
// sigma = 3
|
| 112 |
+
result0 = (0.011339 * image_kernal[0]) + (0.013395 * image_kernal[1]) + (0.01416 * image_kernal[2]) + (0.013395 * image_kernal[3]) + (0.011339 * image_kernal[4]) + (0.013395 * image_kernal[5]) + (0.015824 * image_kernal[6]) + (0.016728 * image_kernal[7]) + (0.015824 * image_kernal[8]) + (0.013395 * image_kernal[9]) + (0.01416 * image_kernal[10]) + (0.016728 * image_kernal[11]) + (0.017684 * image_kernal[12]) + (0.016728 * image_kernal[13]) + (0.01416 * image_kernal[14]) + (0.013395 * image_kernal[15]) + (0.015824 * image_kernal[16]) + (0.016728 * image_kernal[17]) + (0.015824 * image_kernal[18]) + (0.013395 * image_kernal[19]) + (0.011339 * image_kernal[20]) + (0.013395 * image_kernal[21]) + (0.01416 * image_kernal[22]) + (0.013395 * image_kernal[23]) + (0.011339 * image_kernal[24]);
|
| 113 |
+
// sigma = 5
|
| 114 |
+
result1 = (0.005425 * image_kernal[0]) + (0.00576 * image_kernal[1]) + (0.005877 * image_kernal[2]) + (0.00576 * image_kernal[3]) + (0.005425 * image_kernal[4]) + (0.00576 * image_kernal[5]) + (0.006117 * image_kernal[6]) + (0.00624 * image_kernal[7]) + (0.006117 * image_kernal[8]) + (0.00576 * image_kernal[9]) + (0.005877 * image_kernal[10]) + (0.00624 * image_kernal[11]) + (0.006366 * image_kernal[12]) + (0.00624 * image_kernal[13]) + (0.005877 * image_kernal[14]) + (0.00576 * image_kernal[15]) + (0.006117 * image_kernal[16]) + (0.00624 * image_kernal[17]) + (0.006117 * image_kernal[18]) + (0.00576 * image_kernal[19]) + (0.005425 * image_kernal[20]) + (0.00576 * image_kernal[21]) + (0.005877 * image_kernal[22]) + (0.00576 * image_kernal[23]) + (0.005425 * image_kernal[24]);
|
| 115 |
+
// sigma = 7
|
| 116 |
+
result2 = (0.002993 * image_kernal[0]) + (0.003086 * image_kernal[1]) + (0.003118 * image_kernal[2]) + (0.003086 * image_kernal[3]) + (0.002993 * image_kernal[4]) + (0.003086 * image_kernal[5]) + (0.003182 * image_kernal[6]) + (0.003215 * image_kernal[7]) + (0.003182 * image_kernal[8]) + (0.003086 * image_kernal[9]) + (0.003118 * image_kernal[10]) + (0.003215 * image_kernal[11]) + (0.003248 * image_kernal[12]) + (0.003215 * image_kernal[13]) + (0.003118 * image_kernal[14]) + (0.003086 * image_kernal[15]) + (0.003182 * image_kernal[16]) + (0.003215 * image_kernal[17]) + (0.003182 * image_kernal[18]) + (0.003086 * image_kernal[19]) + (0.002993 * image_kernal[20]) + (0.003086 * image_kernal[21]) + (0.003118 * image_kernal[22]) + (0.003086 * image_kernal[23]) + (0.002993 * image_kernal[24]);
|
| 117 |
+
// sigma = 9
|
| 118 |
+
result3 = (0.00187 * image_kernal[0]) + (0.001905 * image_kernal[1]) + (0.001917 * image_kernal[2]) + (0.001905 * image_kernal[3]) + (0.00187 * image_kernal[4]) + (0.001905 * image_kernal[5]) + (0.001941 * image_kernal[6]) + (0.001953 * image_kernal[7]) + (0.001941 * image_kernal[8]) + (0.001905 * image_kernal[9]) + (0.001917 * image_kernal[10]) + (0.001953 * image_kernal[11]) + (0.001965 * image_kernal[12]) + (0.001953 * image_kernal[13]) + (0.001917 * image_kernal[14]) + (0.001905 * image_kernal[15]) + (0.001941 * image_kernal[16]) + (0.001953 * image_kernal[17]) + (0.001941 * image_kernal[18]) + (0.001905 * image_kernal[19]) + (0.00187 * image_kernal[20]) + (0.001905 * image_kernal[21]) + (0.001917 * image_kernal[22]) + (0.001905 * image_kernal[23]) + (0.00187 * image_kernal[24]);
|
| 119 |
+
|
| 120 |
+
result4 = (0.002915 * image_kernal[0]) + (0.013064 * image_kernal[1]) + (0.021539 * image_kernal[2]) + (0.013064 * image_kernal[3]) + (0.002915 * image_kernal[4]) + (0.013064 * image_kernal[5]) + (0.05855 * image_kernal[6]) + (0.096532 * image_kernal[7]) + (0.05855 * image_kernal[8]) + (0.013064 * image_kernal[9]) + (0.021539 * image_kernal[10]) + (0.096532 * image_kernal[11]) + (0.159155 * image_kernal[12]) + (0.096532 * image_kernal[13]) + (0.021539 * image_kernal[14]) + (0.013064 * image_kernal[15]) + (0.05855 * image_kernal[16]) + (0.096532 * image_kernal[17]) + (0.05855 * image_kernal[18]) + (0.013064 * image_kernal[19]) + (0.002915 * image_kernal[20]) + (0.013064 * image_kernal[21]) + (0.021539 * image_kernal[22]) + (0.013064 * image_kernal[23]) + (0.002915 * image_kernal[24]);
|
| 121 |
+
|
| 122 |
+
result = image_kernal[0];
|
| 123 |
+
count = count + 1;
|
| 124 |
+
if(count != (N-4)*(M-4))
|
| 125 |
+
begin
|
| 126 |
+
if(k != (M-4))
|
| 127 |
+
begin
|
| 128 |
+
j = j + 1;
|
| 129 |
+
k = k + 1;
|
| 130 |
+
NS = FIX;
|
| 131 |
+
end
|
| 132 |
+
else
|
| 133 |
+
begin
|
| 134 |
+
j = j + 5;
|
| 135 |
+
k = 0;
|
| 136 |
+
NS = FIX;
|
| 137 |
+
end
|
| 138 |
+
end
|
| 139 |
+
else NS = IDLE;
|
| 140 |
+
end
|
| 141 |
+
|
| 142 |
+
endcase
|
| 143 |
+
end
|
| 144 |
+
|
| 145 |
+
assign fill_now = (PS==CONVOLUTE) ? 1'b1 : 1'b0; //storage is full when PS is in FIX and CONVOLUTE states.
|
| 146 |
+
assign Dout = (PS==CONVOLUTE) ? result : 16'hzzzz; //output data is available when PS is in CONVOLUTE state.
|
| 147 |
+
|
| 148 |
+
endmodule
|
AugustinJose1221_FPGA-Build/Templates/gaussian.v
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module gaussian();
|
| 2 |
+
reg [7:0] s;
|
| 3 |
+
reg [7:0] size;
|
| 4 |
+
reg [7:0] G;
|
| 5 |
+
|
| 6 |
+
initial
|
| 7 |
+
begin
|
| 8 |
+
size = 4*s + 1;
|
| 9 |
+
G = 1/(2*3.1415);
|
| 10 |
+
$display("%f", G);
|
| 11 |
+
end
|
| 12 |
+
endmodule
|
AugustinJose1221_FPGA-Build/Templates/slave_1.v
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
module slave_1(
|
| 3 |
+
input clk,
|
| 4 |
+
input rst_n,
|
| 5 |
+
input slave_en,
|
| 6 |
+
output slave_done,
|
| 7 |
+
output reg [3:0] slave_out
|
| 8 |
+
);
|
| 9 |
+
always @(posedge clk or negedge rst_n)
|
| 10 |
+
begin
|
| 11 |
+
if (~rst_n)
|
| 12 |
+
slave_out <= 0;
|
| 13 |
+
else if (slave_en)
|
| 14 |
+
slave_out <= slave_out + 1;
|
| 15 |
+
else slave_out <= 0;
|
| 16 |
+
end
|
| 17 |
+
assign slave_done = (slave_out == 4) ? 1'b1 : 1'b0;
|
| 18 |
+
endmodule
|
AugustinJose1221_FPGA-Build/Templates/slave_1_tb.v
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
module slave_1_tb();
|
| 4 |
+
|
| 5 |
+
reg clk, rst_n, slave_en;
|
| 6 |
+
wire slave_done;
|
| 7 |
+
wire [3:0] slave_out;
|
| 8 |
+
|
| 9 |
+
slave_1 obey (clk, rst_n, slave_en, slave_done, slave_out);
|
| 10 |
+
|
| 11 |
+
initial
|
| 12 |
+
begin
|
| 13 |
+
$dumpfile("slave_1_tb.vcd");
|
| 14 |
+
$dumpvars(0, slave_1_tb);
|
| 15 |
+
|
| 16 |
+
rst_n = 0;
|
| 17 |
+
slave_en = 0;
|
| 18 |
+
#10;
|
| 19 |
+
rst_n = 1;
|
| 20 |
+
slave_en = 1;
|
| 21 |
+
#40;
|
| 22 |
+
slave_en = 0;
|
| 23 |
+
#200;
|
| 24 |
+
$finish;
|
| 25 |
+
end
|
| 26 |
+
|
| 27 |
+
always
|
| 28 |
+
begin
|
| 29 |
+
clk = 1'b1;
|
| 30 |
+
#5;
|
| 31 |
+
clk = 1'b0;
|
| 32 |
+
#5;
|
| 33 |
+
end
|
| 34 |
+
endmodule
|
AugustinJose1221_FPGA-Build/design/Controller.v
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
//`timescale 1ns/1ns
|
| 3 |
+
|
| 4 |
+
module Controller(
|
| 5 |
+
input clk, //clock
|
| 6 |
+
input rst_n, //external asynchronous active low reset
|
| 7 |
+
input RWM_1_done, //status signal from the RWM_1 module
|
| 8 |
+
input RWM_2_done, //status signal from the RWM_2 module
|
| 9 |
+
input GS_done, //status signal from the Grayscaler module
|
| 10 |
+
input start, //external start command from user
|
| 11 |
+
output RWM_1_enable, //status signal to the RWM_1 module
|
| 12 |
+
output rw_1, //status signal to the RWM_1 module
|
| 13 |
+
output RWM_2_enable, //status signal to the RWM_2 module
|
| 14 |
+
output rw_2, //status signal to the RWM_2 module
|
| 15 |
+
output camera_enable, //status signal to the camera
|
| 16 |
+
output GS_enable //status signal to the Grayscaler module
|
| 17 |
+
);
|
| 18 |
+
|
| 19 |
+
parameter [2:0] IDLE = 3'b000, CAMERA_READ = 3'b001, GRAYSCALE = 3'b010, FILTER = 3'b011;
|
| 20 |
+
reg [2:0] CS, NS;
|
| 21 |
+
|
| 22 |
+
//Sequential logic
|
| 23 |
+
always @(posedge clk or negedge rst_n)
|
| 24 |
+
begin
|
| 25 |
+
if (~rst_n)
|
| 26 |
+
CS <= IDLE;
|
| 27 |
+
else CS <= NS;
|
| 28 |
+
end
|
| 29 |
+
|
| 30 |
+
//Combinatorial logic
|
| 31 |
+
always @(start, GS_done, RWM_1_done, RWM_2_done)
|
| 32 |
+
begin
|
| 33 |
+
case (CS)
|
| 34 |
+
IDLE:
|
| 35 |
+
begin
|
| 36 |
+
if (start == 1'b1)
|
| 37 |
+
NS = CAMERA_READ;
|
| 38 |
+
else NS = IDLE;
|
| 39 |
+
end
|
| 40 |
+
CAMERA_READ:
|
| 41 |
+
begin
|
| 42 |
+
if (RWM_1_done == 1'b1)
|
| 43 |
+
NS = GRAYSCALE;
|
| 44 |
+
else NS = CAMERA_READ;
|
| 45 |
+
end
|
| 46 |
+
GRAYSCALE:
|
| 47 |
+
begin
|
| 48 |
+
if (RWM_1_done == 1'b1)
|
| 49 |
+
NS = FILTER;
|
| 50 |
+
else NS = GRAYSCALE;
|
| 51 |
+
end
|
| 52 |
+
FILTER:
|
| 53 |
+
begin
|
| 54 |
+
if ((GS_done == 1'b1) || RWM_2_done == 1'b0)
|
| 55 |
+
NS = FILTER;
|
| 56 |
+
else NS = IDLE;
|
| 57 |
+
end
|
| 58 |
+
default: NS = IDLE;
|
| 59 |
+
endcase
|
| 60 |
+
end
|
| 61 |
+
|
| 62 |
+
assign camera_enable = (CS == CAMERA_READ) ? 1'b1 : 1'b0;
|
| 63 |
+
assign RWM_1_enable = ((CS == CAMERA_READ) || (CS == GRAYSCALE)) ? 1'b1 : 1'b0;
|
| 64 |
+
assign rw_1 = (CS == CAMERA_READ) ? 1'b1 :((CS == GRAYSCALE) ? 1'b0 : 1'bz);
|
| 65 |
+
assign RWM_2_enable = ((CS == GRAYSCALE) || (CS == FILTER)) ? 1'b1 : 1'b0;
|
| 66 |
+
assign rw_2 = (CS == GRAYSCALE) ? 1'b1 : ((CS == FILTER) ? 1'b0 : 1'bz);
|
| 67 |
+
assign GS_enable = (CS == GRAYSCALE) ? 1'b1 : 1'b0;
|
| 68 |
+
|
| 69 |
+
endmodule
|
AugustinJose1221_FPGA-Build/design/Grayscaler.v
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
MODULE OVERVIEW:
|
| 3 |
+
Function of this module:
|
| 4 |
+
To calculate the grayscale values of each colour pixel(R,G,B respectively) stored in first memory module.
|
| 5 |
+
It communicates with the controller and the memory modules.
|
| 6 |
+
|
| 7 |
+
Working:
|
| 8 |
+
The module makes use of a FSM with 3 states:-
|
| 9 |
+
1)IDLE: Whenever the module is not in use, it is in this state. It waits for further commands from the controller.
|
| 10 |
+
|
| 11 |
+
2)FILL: Here every three pixel bytes at the input data bus is loaded into the internal registers for grayscale calculation.
|
| 12 |
+
First byte is stored in 'red', second in 'green' and third in 'blue'.
|
| 13 |
+
After the third byte is recieved, a status signal is sent to the first memory module to pause its operation.
|
| 14 |
+
|
| 15 |
+
3)CALCULATE: To find the grayscale value of the three bytes recieved.
|
| 16 |
+
After placing the computed value in the output bus, a status signal is sent to the second memory module to store this value.
|
| 17 |
+
*/
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
module Grayscaler(
|
| 21 |
+
input clk, //clock
|
| 22 |
+
input rst_n, //external asynchronous active low reset
|
| 23 |
+
input GS_enable, //to enable or disable this module. Driven by controller
|
| 24 |
+
input RWM_valid, //an active high signal indicating the presence of desired data at the output data bus
|
| 25 |
+
input [7:0] Din, //input data bus. Connected to RWM_1 module
|
| 26 |
+
output [7:0] Dout, //output data bus. Connected to RWM_2 module
|
| 27 |
+
output GS_valid, //an active high signal that tells the RWM_2 module that desired data bytes is present in the output data bus
|
| 28 |
+
output pause, //an active high signal that tells the RWM_1 module to pause whatever operation it is doing.
|
| 29 |
+
output reg GS_done //after the completion of an operation done is set to 1. It is a status signal to drive the controller
|
| 30 |
+
);
|
| 31 |
+
|
| 32 |
+
parameter N = 450, M = 450;
|
| 33 |
+
|
| 34 |
+
reg [7:0] red, green, blue, result;
|
| 35 |
+
integer c, d, k=0;
|
| 36 |
+
|
| 37 |
+
parameter IDLE = 2'b00, FILL = 2'b01, CALCULATE = 2'b10;
|
| 38 |
+
reg [1:0] CS, NS;
|
| 39 |
+
|
| 40 |
+
always @(posedge clk or negedge rst_n)
|
| 41 |
+
begin
|
| 42 |
+
if(~rst_n)
|
| 43 |
+
CS <= IDLE;
|
| 44 |
+
else
|
| 45 |
+
begin
|
| 46 |
+
CS <= NS;
|
| 47 |
+
k = (RWM_valid) ? k + 1 : 0;
|
| 48 |
+
d = (k == 2) ? d + 1 : d;
|
| 49 |
+
end
|
| 50 |
+
end
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
always @(*)
|
| 54 |
+
begin
|
| 55 |
+
case (CS)
|
| 56 |
+
IDLE:
|
| 57 |
+
begin
|
| 58 |
+
d = 0;
|
| 59 |
+
c = 0;
|
| 60 |
+
red = 8'h00;
|
| 61 |
+
green = 8'h00;
|
| 62 |
+
blue = 8'h00;
|
| 63 |
+
GS_done = 1'b0;
|
| 64 |
+
if(GS_enable)
|
| 65 |
+
begin
|
| 66 |
+
NS = FILL;
|
| 67 |
+
end
|
| 68 |
+
else NS = IDLE;
|
| 69 |
+
end
|
| 70 |
+
FILL:
|
| 71 |
+
begin
|
| 72 |
+
GS_done = 1'b0;
|
| 73 |
+
c = (c != 3) ? c + 1 : 1;
|
| 74 |
+
red = (k == 0) ? Din : red;
|
| 75 |
+
green = (k == 1) ? Din : green;
|
| 76 |
+
blue = (k == 2) ? Din : blue;
|
| 77 |
+
NS = (k == 2) ? CALCULATE : FILL;
|
| 78 |
+
end
|
| 79 |
+
CALCULATE:
|
| 80 |
+
begin
|
| 81 |
+
result = (red>>2) + (red>>5) + (green>>1) + (green>>4) + (blue>>4) + (blue>>5);
|
| 82 |
+
NS = (d == N*M) ? IDLE : FILL;
|
| 83 |
+
GS_done = (d == N*M) ? 1'b1 : 1'b0;
|
| 84 |
+
end
|
| 85 |
+
default: NS = IDLE;
|
| 86 |
+
endcase
|
| 87 |
+
end
|
| 88 |
+
|
| 89 |
+
assign pause = ((CS == FILL) && (c == 3) && (d != N*M)) ? 1'b1 : 1'b0;
|
| 90 |
+
assign Dout = (CS == CALCULATE) ? result : 8'hzz;
|
| 91 |
+
assign GS_valid = ((CS == FILL) && (k == 2)) ? 1'b1 : 1'b0;
|
| 92 |
+
|
| 93 |
+
endmodule
|
AugustinJose1221_FPGA-Build/design/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Top Level Design
|
| 2 |
+
|
| 3 |
+
The top level design is divided into five stages:
|
| 4 |
+
* Preprocessing stage
|
| 5 |
+
* Filter stage
|
| 6 |
+
* Keypoint stage
|
| 7 |
+
* Keypoint matching stage
|
| 8 |
+
* Frame blending stage
|
| 9 |
+
|
| 10 |
+
### Preprocessing Stage
|
| 11 |
+
A detailed veiw of this stage of operation is shown in figure:
|
| 12 |
+
<p align = "center">
|
| 13 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/img/Diagram1.png"> <br>
|
| 14 |
+
Preprocessing stage
|
| 15 |
+
</p>
|
| 16 |
+
|
| 17 |
+
To emulate the working of camera sensors, [image.v](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/image.v) and [image2.v](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/image.v) are used, which inputs images corresponding to left and right camera sensor respectively. [RWM_1.v](https://github.com/AugustinJose1221/FPGA-Build/blob/beta/design/RWM_1.v) is a read-write memory that stores the 8 bit RGB image. When it is WRITE mode, the RGB image pixel data is written into the memory. After all the pixel values are stored, the memory is put in READ mode. In READ mode, each pixel value is read sequentially from the memory.
|
| 18 |
+
|
| 19 |
+
### Filter Stage
|
| 20 |
+
A detailed veiw of this stage of operation is shown in figure:
|
| 21 |
+
<p align = "center">
|
| 22 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/img/Diagram3.png"> <br>
|
| 23 |
+
Filter stage
|
| 24 |
+
</p>
|
| 25 |
+
|
| 26 |
+
### Keypoint Stage
|
| 27 |
+
A detailed veiw of this stage of operation is shown in figure:
|
| 28 |
+
<p align = "center">
|
| 29 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/img/Diagram4.png"> <br>
|
| 30 |
+
Keypoint stage
|
| 31 |
+
</p>
|
| 32 |
+
|
| 33 |
+
### Keypoint Matching Stage
|
| 34 |
+
A detailed veiw of this stage of operation is shown in figure:
|
| 35 |
+
<p align = "center">
|
| 36 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/img/Diagram5.png"> <br>
|
| 37 |
+
Keypoint matching stage
|
| 38 |
+
</p>
|
| 39 |
+
|
| 40 |
+
### Frame Blending Stage
|
| 41 |
+
A detailed veiw of this stage of operation is shown in figure:
|
| 42 |
+
<p align = "center">
|
| 43 |
+
<img src = "https://github.com/AugustinJose1221/FPGA-Build/blob/beta/img/Diagram6.png"> <br>
|
| 44 |
+
Frame blending stage
|
| 45 |
+
</p>
|
AugustinJose1221_FPGA-Build/design/RWM_1.v
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
MODULE OVERVIEW:
|
| 3 |
+
Purpose of this module:
|
| 4 |
+
To store the RGB pixel bytes coming from the camera. It works with the camera, controller and the grayscaling module.
|
| 5 |
+
|
| 6 |
+
Working:
|
| 7 |
+
The 5 states of the FSM used in this module are described below:-
|
| 8 |
+
1)INACTIVE: Whenever the module is not in use, it is in this state. It waits for further commands from the controller.
|
| 9 |
+
|
| 10 |
+
2)WRITE: Writes the contents of the input data bus to the internal register array. After completion, it goes back to INACTIVE.
|
| 11 |
+
|
| 12 |
+
3)READ: Writes the contents of the internal data bus to the output data bus. After completion, it goes back to INACTIVE.
|
| 13 |
+
|
| 14 |
+
4)WAIT: The grayscaling module can interrupt this module during the READ operation by asserting the 'pause' signal.
|
| 15 |
+
If this happens, the module goes to this state, where it preserves the location address.
|
| 16 |
+
It then waits for the 'pause' signal to be disabled so that it can go back to READ state.
|
| 17 |
+
|
| 18 |
+
5)CLEANUP: Clears the contents of the internal register array to 8'h00 upon receiving the 'clear' command from the controller.
|
| 19 |
+
After completion, it goes back to INACTIVE.
|
| 20 |
+
*/
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
module RWM_1(
|
| 24 |
+
input clk, // clock
|
| 25 |
+
input rst_n, // external asynchronous active low reset
|
| 26 |
+
input RWM_enable, // to enable or disable the R/W memory. Driven by controller
|
| 27 |
+
input rw, // rw = 0: read, rw = 1: write. Driven by controller
|
| 28 |
+
input clear, // an active high signal to clear all the contents of the R/W memory. Driven by controller
|
| 29 |
+
input pause, // an active high signal that tells the module to pause whatever operation it is doing. Driven by Grayscaler
|
| 30 |
+
input [7:0] data_in, // input data bus. Comes from the camera
|
| 31 |
+
output [7:0] data_out, // ouput data bus. Connected to Grayscaling module
|
| 32 |
+
output RWM_valid, // an active high signal indicating the presence of desired data at the output data bus
|
| 33 |
+
output reg RWM_done // after the completion of an operation done is set to 1. It is a status signal to drive the controller
|
| 34 |
+
);
|
| 35 |
+
|
| 36 |
+
parameter N = 450, M = 450;
|
| 37 |
+
|
| 38 |
+
reg [7:0] DATA[0:(3*N*M - 1)]; // RWM register array
|
| 39 |
+
|
| 40 |
+
reg [2:0] CS, NS; // RWM state variables
|
| 41 |
+
|
| 42 |
+
//RWM states
|
| 43 |
+
parameter INACTIVE = 3'b000, READ = 3'b001, WRITE = 3'b010, WAIT = 3'b011, CLEANUP = 3'b100;
|
| 44 |
+
|
| 45 |
+
integer i, j, k = 0; // Loop variable for addressing the RWM register array
|
| 46 |
+
|
| 47 |
+
// Sequential Logic
|
| 48 |
+
always @(posedge clk or negedge rst_n)
|
| 49 |
+
begin
|
| 50 |
+
if (~rst_n)
|
| 51 |
+
CS <= INACTIVE;
|
| 52 |
+
else
|
| 53 |
+
begin
|
| 54 |
+
CS <= NS;
|
| 55 |
+
k = (CS == READ) ? k + 1 : 0;
|
| 56 |
+
end
|
| 57 |
+
end
|
| 58 |
+
|
| 59 |
+
always @(posedge clk)
|
| 60 |
+
begin
|
| 61 |
+
case (CS)
|
| 62 |
+
INACTIVE: i <= 0; // Keep the memory address pointer at 0
|
| 63 |
+
WRITE:
|
| 64 |
+
begin
|
| 65 |
+
DATA[i] <= data_in; // Writing into RWM
|
| 66 |
+
i <= (i == 3*N*M - 1) ? 0 : i + 1;
|
| 67 |
+
end
|
| 68 |
+
READ:
|
| 69 |
+
begin // Reading from RWM
|
| 70 |
+
i <= (i == 3*N*M - 1) ? 0 : i + 1;
|
| 71 |
+
end
|
| 72 |
+
WAIT: i <= i; // Preserve the address location
|
| 73 |
+
CLEANUP:
|
| 74 |
+
begin
|
| 75 |
+
for(j = 0; j < N*M*3; j = j+1)
|
| 76 |
+
begin
|
| 77 |
+
DATA[j] <= 8'h00; // Clearing RWM registers
|
| 78 |
+
end
|
| 79 |
+
end
|
| 80 |
+
endcase
|
| 81 |
+
end
|
| 82 |
+
|
| 83 |
+
// Combinatorial Logic
|
| 84 |
+
always @(RWM_enable, rw, i, pause)
|
| 85 |
+
begin
|
| 86 |
+
case (CS)
|
| 87 |
+
INACTIVE:
|
| 88 |
+
begin
|
| 89 |
+
RWM_done = 1'b0;
|
| 90 |
+
if (RWM_enable == 1'b0)
|
| 91 |
+
NS = INACTIVE;
|
| 92 |
+
else if (clear == 1'b1)
|
| 93 |
+
NS = CLEANUP;
|
| 94 |
+
else NS = (rw == 1) ? WRITE : READ;
|
| 95 |
+
end
|
| 96 |
+
WRITE:
|
| 97 |
+
begin
|
| 98 |
+
NS = (i == 3*N*M - 1) ? INACTIVE : WRITE;
|
| 99 |
+
RWM_done = (i == 3*N*M - 1) ? 1'b1 : 1'b0;
|
| 100 |
+
end
|
| 101 |
+
READ:
|
| 102 |
+
begin
|
| 103 |
+
if (k == 2 && i != 3*N*M - 1)
|
| 104 |
+
NS = WAIT;
|
| 105 |
+
else NS = (i == 3*N*M - 1) ? INACTIVE : READ;
|
| 106 |
+
RWM_done = (i == 3*N*M - 1) ? 1'b1 : 1'b0;
|
| 107 |
+
end
|
| 108 |
+
WAIT:
|
| 109 |
+
begin
|
| 110 |
+
RWM_done = 1'b0;
|
| 111 |
+
if (k == 3)
|
| 112 |
+
NS = READ;
|
| 113 |
+
else NS = WAIT;
|
| 114 |
+
end
|
| 115 |
+
CLEANUP:
|
| 116 |
+
begin
|
| 117 |
+
NS = (j == 3*N*M - 1) ? INACTIVE : CLEANUP;
|
| 118 |
+
RWM_done = (j == 3*N*M - 1) ? 1'b1 : 1'b0;
|
| 119 |
+
end
|
| 120 |
+
default: NS = INACTIVE;
|
| 121 |
+
endcase
|
| 122 |
+
end
|
| 123 |
+
|
| 124 |
+
assign data_out = (CS == READ) ? DATA[i] : 8'hzz;
|
| 125 |
+
assign RWM_valid = (CS == READ) ? 1'b1 : 1'b0;
|
| 126 |
+
|
| 127 |
+
endmodule
|