commit 53d5212df87e6d1bf8a59e97b401ef913fa7343e Author: Peisong Xiao Date: Thu Aug 14 01:23:23 2025 -0400 framework for later dev diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b9034d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/tmp/ +**/test/ +**/obj_dir/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..db618b3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2025 Peisong Xiao + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/src/testbench/bitstream.sv b/src/testbench/bitstream.sv new file mode 100644 index 0000000..83a5fa7 --- /dev/null +++ b/src/testbench/bitstream.sv @@ -0,0 +1,47 @@ +`include + +`include + +task bitstream::append_packet(input Packet pkt); + for (int i = 0; i < PACKET_SIZE; i++) begin + this.append_byte(pkt.get_pos(i)); + end +endtask : append_packet + +task bitstream::append_byte(input byte data); + for (int i = 0; i < 8; i++) begin + this.queue.push_back(data[i]); + end +endtask : append_byte + +task bitstream::append_bit(input bit data); + this.queue.push_back(data); +endtask : append_bit + +task bitstream::parse_packets(ref Packet results[]); + Packet pkt_tmp; + bit bit_tmp[PACKET_SIZE * 8]; + int curr = 0; + logic in_packet = 0; + for (int i = 0; i < this.queue.size(); i = i + 8) begin + if ((!in_packet) & (this.queue[i:i + 7] == 0)) + continue; + + in_packet = 1; + bit_tmp[curr:curr + 7] = this.queue[i:i + 7]; + curr = curr + 8; + + if (curr == PACKET_SIZE * 8) begin + pkt_tmp.construct(bit_tmp); + results.push_back(pkt_tmp); + curr = 0; + in_packet = 0; + end + end +endtask : parse_packets + +task bitstream::trim(); + while (this.queue.size() >= 8 & this.queue[0:7] == 0) + for (int i = 0; i < 8; i++) + this.queue.pop_front(); +endtask : trim diff --git a/src/testbench/bitstream.svh b/src/testbench/bitstream.svh new file mode 100644 index 0000000..83fe35c --- /dev/null +++ b/src/testbench/bitstream.svh @@ -0,0 +1,20 @@ +`ifndef __BITSTREAM_SVH__ +`define __BITSTREAM_SVH__ + +`include + +class bitstream; + bit queue[$]; + + extern task append_packet(input Packet pkt); + extern task append_byte(input byte data); + extern task append_bit(input bit data); + extern task parse_packets(ref Packet results[]); + extern task trim(); + + function int size(); + return this.queue.size(); + endfunction : size +endclass : bitstream + +`endif diff --git a/src/testbench/packet.sv b/src/testbench/packet.sv new file mode 100644 index 0000000..9d2aa28 --- /dev/null +++ b/src/testbench/packet.sv @@ -0,0 +1,72 @@ +`include + +function void Packet::init(byte command, byte src, byte dest); + this.data[0] = command; + this.data[1] = dest; + this.data[2] = src; + + for (int i = 3; i < PACKET_SIZE; i++) begin + this.data[i] = $urandom_range(0, 255)[7:0]; + end +endfunction : init + +function void Packet::construct(bit [PACKET_SIZE * 8 - 1: 0] stream); + for (int i = 0; i < PACKET_SIZE * 8; i++) begin + this.data[i] = stream[i +: 8]; + end +endfunction : construct + +function void Packet::iterate(); + if (this.pkt_addr != PACKET_SIZE) + this.pkt_addr++; +endfunction : iterate + +function void Packet::reset(); + this.pkt_addr = 0; +endfunction : reset + +function int Packet::addr(); + return this.pkt_addr; +endfunction : addr + +function logic Packet::is_complete(); + return this.pkt_addr == PACKET_SIZE; +endfunction : is_complete + +function void Packet::append(byte dat); + if (this.pkt_addr != PACKET_SIZE) begin + this.data[this.pkt_addr] = dat; + this.pkt_addr++; + end +endfunction : append + +function byte Packet::get(); + if (this.pkt_addr != 0) + return this.data[this.pkt_addr - 1]; + return 8'hFF; +endfunction : get + +function byte Packet::get_pos(int pos); + if (pos >= 0 && pos < PACKET_SIZE) + return this.data[pos]; + return 8'hFF; +endfunction : get_pos + +function bit Packet::get_bit(int pos); + if (pos >= 0 && pos <= PACKET_SIZE * 8) + return this.data[pos / 8][pos % 8]; + return 0; +endfunction : get_bit + +function void Packet::set(byte dat, int pos); + if (pos >= 0 && pos < PACKET_SIZE) + this.data[pos] = dat; +endfunction : set + +function logic Packet::compare(Packet other); + for (int i = 0; i < PACKET_SIZE; i++) begin + if (this.data[i] != other.data[i]) + return 0; + end + return 1; +endfunction : compare diff --git a/src/testbench/packet.svh b/src/testbench/packet.svh new file mode 100644 index 0000000..848ad51 --- /dev/null +++ b/src/testbench/packet.svh @@ -0,0 +1,29 @@ +`ifndef __PACKET_SVH__ +`define __PACKET_SVH__ + +`include + +class Packet; + int pkt_addr; + byte data [PACKET_SIZE]; + + function new(); + pkt_addr = 0; + endfunction // new + + extern function void init(byte command, byte src, byte dest); + extern function void construct(bit stream[]); + extern function void iterate(); + extern function void reset(); + extern function int addr(); + extern function logic is_complete(); + extern function void append(byte dat); + extern function byte get(); + extern function byte get_pos(int pos); + extern function bit get_bit(int pos); + extern function void set(byte dat, int pos); + extern function logic compare(Packet other); + +endclass : Packet + +`endif diff --git a/src/testbench/params.svh b/src/testbench/params.svh new file mode 100644 index 0000000..882765c --- /dev/null +++ b/src/testbench/params.svh @@ -0,0 +1,26 @@ +`ifndef __PARAMS_SVH__ +`define __PARAMS_SVH__ + +parameter int PACKET_SIZE = 64; +parameter int PACKET_ADDR_LEN = 6; + +parameter int ROSE_ADDR_LEN = 8; +parameter logic [PACKET_ADDR_LEN - 1:0] ROSE_DEST_INDEX = 1; + +parameter logic [7:0] TX_DEFAULT = 8'b00101010; + +parameter shortint QUEUE_SIZE = 1024; +parameter int QUEUE_ADDR_LEN = 10; +parameter int MEMORY_POOL_SIZE = QUEUE_SIZE * PACKET_SIZE; +parameter int MEMORY_POOL_ADDR_LEN = QUEUE_ADDR_LEN + PACKET_ADDR_LEN; +parameter int MEMORY_POOL_ADDR_SHIFT = 4; +parameter int INTERFACE_QUEUE_SIZE = 512; +parameter int INTERFACE_QUEUE_ADDR_LEN = 9; +parameter int INTERFACE_CNT = 4; +parameter int INTERFACE_ADDR_LEN = 2; +parameter int CRC_BITS = 8; +parameter shortint NEW_SLOT_COOLDOWN = 500; +parameter shortint RX_LOAD_COOLDOWN = 4; +parameter shortint TX_LOAD_COOLDOWN = 4; + +`endif diff --git a/src/testbench/spi.sv b/src/testbench/spi.sv new file mode 100644 index 0000000..df3c985 --- /dev/null +++ b/src/testbench/spi.sv @@ -0,0 +1,15 @@ +`include + +task spi::send(ref bitstream stream); + for (int i = 0; i < stream.queue.size(); i++) begin + @ (posedge ports.sclk); + ports.mosi = stream.queue[i]; + end +endtask : send + +task spi::recv(input int threshold, ref bitstream stream); + for (int i = 0; i < threshold; i++) begin + @ (negedge ports.sclk); + stream.append_bit(ports.miso); + end +endtask : recv diff --git a/src/testbench/spi.svh b/src/testbench/spi.svh new file mode 100644 index 0000000..1e8966d --- /dev/null +++ b/src/testbench/spi.svh @@ -0,0 +1,26 @@ +`ifndef __SPI_SVH__ +`define __SPI_SVH__ + +`include + +interface spi_ports; + logic mosi; + logic miso; + logic sclk; + logic cs_n; +endinterface : spi_ports + + +class spi; + virtual spi_ports ports; + + function new(virtual spi_ports _ports); + ports = _ports; + endfunction // new + + extern task send(ref bitstream stream); + extern task recv(input int threshold, ref bitstream stream); + +endclass : spi + +`endif diff --git a/src/testbench/utils.svh b/src/testbench/utils.svh new file mode 100644 index 0000000..20c586d --- /dev/null +++ b/src/testbench/utils.svh @@ -0,0 +1,11 @@ +`ifndef __UTILS_SVH__ +`define __UTILS_SVH__ + +`include +`include + +function automatic logic get_bit(input Packet pkt, input int pos); + if (pos <= 0 | pos >= +endfunction // get_bit + +`endif