From f3a6a98ba8858e7a3eafa807c531501b848f4ac5 Mon Sep 17 00:00:00 2001
From: dwf1m12 <d.w.flynn@soton.ac.uk>
Date: Mon, 8 Jan 2024 16:49:49 +0000
Subject: [PATCH] fix hex-digit string handling in testbench file upload

---
 .../nanosoc_axi_stream_io_8_txd_from_file.v   | 32 ++++++++++++++++---
 1 file changed, 27 insertions(+), 5 deletions(-)

diff --git a/verif/trace/verilog/nanosoc_axi_stream_io_8_txd_from_file.v b/verif/trace/verilog/nanosoc_axi_stream_io_8_txd_from_file.v
index 76d41cb..e62e3e5 100644
--- a/verif/trace/verilog/nanosoc_axi_stream_io_8_txd_from_file.v
+++ b/verif/trace/verilog/nanosoc_axi_stream_io_8_txd_from_file.v
@@ -27,6 +27,28 @@ module nanosoc_axi_stream_io_8_txd_from_file
   output wire  [7:0] txd8_data
   );
 
+function [7:0] FNmap_hex_digit;
+input [3:0] nibble;
+case (nibble[3:0])
+4'b0000: FNmap_hex_digit = "0" & 8'hff;
+4'b0001: FNmap_hex_digit = "1" & 8'hff;
+4'b0010: FNmap_hex_digit = "2" & 8'hff;
+4'b0011: FNmap_hex_digit = "3" & 8'hff;
+4'b0100: FNmap_hex_digit = "4" & 8'hff;
+4'b0101: FNmap_hex_digit = "5" & 8'hff;
+4'b0110: FNmap_hex_digit = "6" & 8'hff;
+4'b0111: FNmap_hex_digit = "7" & 8'hff;
+4'b1000: FNmap_hex_digit = "8" & 8'hff;
+4'b1001: FNmap_hex_digit = "9" & 8'hff;
+4'b1010: FNmap_hex_digit = "a" & 8'hff;
+4'b1011: FNmap_hex_digit = "b" & 8'hff;
+4'b1100: FNmap_hex_digit = "c" & 8'hff;
+4'b1101: FNmap_hex_digit = "d" & 8'hff;
+4'b1110: FNmap_hex_digit = "e" & 8'hff;
+4'b1111: FNmap_hex_digit = "f" & 8'hff;
+//default: FNmap_hex_digit = "0" & 8'hff;
+endcase
+endfunction
 
  //----------------------------------------------
  //-- File I/O
@@ -84,11 +106,11 @@ localparam BUFSIZE = (64 * 1024);
        adpbuf[11] = 8'h0a; // newline
        adpbuf[12] = "U"; // set upload filesize (N bytes)
        adpbuf[13] = " "; // only up to 1Mbyte for now!
-       adpbuf[14] = "0" + codesize[19:16]; //
-       adpbuf[15] = "0" + codesize[15:12]; //
-       adpbuf[16] = "0" + codesize[11: 8]; //
-       adpbuf[17] = "0" + codesize[ 7: 4]; //
-       adpbuf[18] = "0" + codesize[ 3: 0]; //
+       adpbuf[14] = FNmap_hex_digit(codesize[19:16]); //
+       adpbuf[15] = FNmap_hex_digit(codesize[15:12]); //
+       adpbuf[16] = FNmap_hex_digit(codesize[11: 8]); //
+       adpbuf[17] = FNmap_hex_digit(codesize[ 7: 4]); //
+       adpbuf[18] = FNmap_hex_digit(codesize[ 3: 0]); //
        adpbuf[19] = 8'h0a; // newline
        $readmemh(CODEFILENAME, adpbuf, 20);
        adpbuf[clen+20] = "C"; // control
-- 
GitLab