framework for later dev

This commit is contained in:
2025-08-14 01:23:23 -04:00
commit 53d5212df8
10 changed files with 256 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
**/tmp/
**/test/
**/obj_dir/

7
LICENSE Normal file
View File

@@ -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.

View File

@@ -0,0 +1,47 @@
`include <bitstream.svh>
`include <params.svh>
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

View File

@@ -0,0 +1,20 @@
`ifndef __BITSTREAM_SVH__
`define __BITSTREAM_SVH__
`include <packet.svh>
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

72
src/testbench/packet.sv Normal file
View File

@@ -0,0 +1,72 @@
`include <packet.svh>
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

29
src/testbench/packet.svh Normal file
View File

@@ -0,0 +1,29 @@
`ifndef __PACKET_SVH__
`define __PACKET_SVH__
`include <params.svh>
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

26
src/testbench/params.svh Normal file
View File

@@ -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

15
src/testbench/spi.sv Normal file
View File

@@ -0,0 +1,15 @@
`include <spi.svh>
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

26
src/testbench/spi.svh Normal file
View File

@@ -0,0 +1,26 @@
`ifndef __SPI_SVH__
`define __SPI_SVH__
`include <bitstream.svh>
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

11
src/testbench/utils.svh Normal file
View File

@@ -0,0 +1,11 @@
`ifndef __UTILS_SVH__
`define __UTILS_SVH__
`include <params.svh>
`include <packet.svh>
function automatic logic get_bit(input Packet pkt, input int pos);
if (pos <= 0 | pos >=
endfunction // get_bit
`endif