From d06dcc6ceb38ab4ee4ecb23fbe0adb37b7077b58 Mon Sep 17 00:00:00 2001 From: Mario Romero Date: Sat, 11 Feb 2023 19:29:27 -0300 Subject: [PATCH] Add Data Memory test --- CMakeLists.txt | 2 +- fw/cmake/riscv-toolchain.cmake | 14 ------ fw/cmake/utils.cmake | 31 ------------- fw/test/CMakeLists.txt | 2 +- rtl/DataMemory.sv | 12 ++--- test/CMakeLists.txt | 11 +++-- test/Test_DataMemory.sv | 82 +++++++++++++++++++++------------- 7 files changed, 66 insertions(+), 88 deletions(-) delete mode 100644 fw/cmake/riscv-toolchain.cmake delete mode 100644 fw/cmake/utils.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 19ac0fe..62abad2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ include(ExternalProject) ExternalProject_Add(firmware SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/fw BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build/fw - CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_SOURCE_DIR}/fw/cmake/riscv-toolchain.cmake + CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/riscv-toolchain.cmake INSTALL_COMMAND "" ) diff --git a/fw/cmake/riscv-toolchain.cmake b/fw/cmake/riscv-toolchain.cmake deleted file mode 100644 index 4eb572a..0000000 --- a/fw/cmake/riscv-toolchain.cmake +++ /dev/null @@ -1,14 +0,0 @@ -set(CMAKE_SYSTEM_NAME Generic) - -find_program(RISCV_GCC_FOUND - NAMES riscv-none-elf-gcc riscv32-unknown-elf-gcc) - -get_filename_component(GCC_BIN ${RISCV_GCC_FOUND} NAME) - -string(REPLACE gcc "" TOOLCHAIN_PREFIX ${GCC_BIN}) - -set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PREFIX}as) -set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc) -set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++) -set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy) -set(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}objdump) diff --git a/fw/cmake/utils.cmake b/fw/cmake/utils.cmake deleted file mode 100644 index cffec0e..0000000 --- a/fw/cmake/utils.cmake +++ /dev/null @@ -1,31 +0,0 @@ -cmake_minimum_required(VERSION 3.10) - -function(rvscc_bin_to_verilog_mem_file) - cmake_parse_arguments(RVSCC - "" - "TARGET" - "" - ${ARGN} - ) - add_custom_command(TARGET ${RVSCC_TARGET} POST_BUILD - COMMAND ${CMAKE_OBJCOPY} -j .text - -O verilog - --verilog-data-width=1 - --reverse-bytes=4 - "$" ${RVSCC_TARGET}.mem - COMMENT "Invoking: Verilog Hexdump" - ) -endfunction() - -function(rvscc_dissasemble) - cmake_parse_arguments(RVSCC - "" - "TARGET" - "" - ${ARGN} - ) - add_custom_command(TARGET ${RVSCC_TARGET} POST_BUILD - COMMAND ${CMAKE_OBJDUMP} -S "$" > ${RVSCC_TARGET}.disasm - COMMENT "Invoking: Disassemble" - ) -endfunction() diff --git a/fw/test/CMakeLists.txt b/fw/test/CMakeLists.txt index 4471e75..7779492 100644 --- a/fw/test/CMakeLists.txt +++ b/fw/test/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.10) project(test ASM) -include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/utils.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/utils.cmake) option(DISSASEMBLY "Enable dissasembly" OFF) diff --git a/rtl/DataMemory.sv b/rtl/DataMemory.sv index a1ef1a5..6d99ed4 100644 --- a/rtl/DataMemory.sv +++ b/rtl/DataMemory.sv @@ -1,16 +1,16 @@ `timescale 1ns / 1ps module DataMemory #( -parameter N = 32, -parameter SIZE = 32, -parameter BYTE_WIDTH = 8)( +parameter int N = 32, +parameter int SIZE = 32, +parameter int BYTE_WIDTH = 8)( input logic clk, rst, input logic[N-1:0] addr, input logic write_enable, input logic[N-1:0] write_data, output logic[N-1:0] read_data ); - logic[BYTE_WIDTH-1:0] mem[SIZE*BYTE_WIDTH-1:0]; + logic[SIZE*BYTE_WIDTH-1:0][BYTE_WIDTH-1:0] mem; assign read_data = {mem[addr + 'd0], mem[addr + 'd1], @@ -18,11 +18,11 @@ parameter BYTE_WIDTH = 8)( mem[addr + 'd3]}; always_ff @(posedge clk) begin if (rst) - mem = '{default: '0}; + mem <= '{default: '0}; else if (write_enable) {mem[addr + 'd0], mem[addr + 'd1], mem[addr + 'd2], mem[addr + 'd3]} <= write_data; end -endmodule \ No newline at end of file +endmodule diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9e8efa3..a0ac7f2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,7 +12,7 @@ find_package(Threads REQUIRED) find_package(SystemCLanguage QUIET) -function(custom_add_test) +function(rvscc_add_test) cmake_parse_arguments(TEST "" "NAME" @@ -37,12 +37,17 @@ function(custom_add_test) add_test(NAME ${TEST_TARGET_NAME} COMMAND ${TEST_TARGET_NAME}) endfunction() -custom_add_test(NAME alu +rvscc_add_test(NAME alu SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Test_ALU.sv ${CMAKE_CURRENT_SOURCE_DIR}/../rtl/ALU.sv ) -custom_add_test(NAME priority-encoder +rvscc_add_test(NAME priority-encoder SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Test_PriorityEncoder.sv ${CMAKE_CURRENT_SOURCE_DIR}/../rtl/PriorityEncoder.sv ) + +rvscc_add_test(NAME data-memory + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Test_DataMemory.sv + ${CMAKE_CURRENT_SOURCE_DIR}/../rtl/DataMemory.sv +) diff --git a/test/Test_DataMemory.sv b/test/Test_DataMemory.sv index 7a8e4ce..400c729 100644 --- a/test/Test_DataMemory.sv +++ b/test/Test_DataMemory.sv @@ -1,36 +1,54 @@ `timescale 1ns / 1ps -module Test_DataMemory(); - logic clk, rst; - logic[31:0] addr; - logic write_enable; - logic[31:0] write_data; - logic[31:0] read_data; - - DataMemory #( - .SIZE(16) - ) data_memory( - clk, - rst, - addr, - write_enable, - write_data, - read_data - ); - - always #1 clk = ~clk; - initial begin - clk = 0; - rst = 1; - write_enable = 0; - #4; - rst = 0; - #1; - write_enable = 1; - for(int i = 0; i < 16; i++) begin - addr = $urandom_range(15); - write_data = $urandom(); - #2; - end +module Test_DataMemory (); + logic clk, rst; + logic [31:0] addr; + logic write_enable; + logic [31:0] write_data; + logic [31:0] read_data; + + localparam int MemorySize = 16; + DataMemory #( + .SIZE(MemorySize) + ) data_memory ( + .clk(clk), + .rst(rst), + .addr(addr), + .write_enable(write_enable), + .write_data(write_data), + .read_data(read_data) + ); + + always #1 clk = ~clk; + + localparam int MemoryWriteRange = 16; + + logic [MemoryWriteRange:0][31:0] write_values; + int start_addr; + initial begin + // Reset + clk = 0; + rst = 1; + write_enable = 0; + #4; + rst = 0; + #1; + // Write to a range of values in memory + write_enable = 1; + start_addr = $urandom_range(15); + for (int i = 0; i < MemoryWriteRange; i++) begin + addr = start_addr + i; + write_values[i] = $urandom(); + write_data = write_values[i]; + #2; end + // Read and compare the same range of values + write_enable = 0; + for (int i = 0; i < 16; i++) begin + addr = start_addr + i; + assert (read_data == write_values[i]) + else $error("Read failed at address %h", addr); + end + $finish; + end endmodule