Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- Weiyet_RTLStructLib/source/.gitignore +10 -0
- Weiyet_RTLStructLib/source/Doubly_Linked_List/README.md +156 -0
- Weiyet_RTLStructLib/source/Dual_Edge_FF/README.md +46 -0
- Weiyet_RTLStructLib/source/FIFO/README.md +95 -0
- Weiyet_RTLStructLib/source/Hash_Table/README.md +91 -0
- Weiyet_RTLStructLib/source/LICENSE +21 -0
- Weiyet_RTLStructLib/source/LIFO/README.md +114 -0
- Weiyet_RTLStructLib/source/List/README.md +85 -0
- Weiyet_RTLStructLib/source/README.md +101 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/README.md +147 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/src/rtl_file.f +1 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/src/singly_linked_list.sv +388 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/FILES_CREATED.md +142 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/Makefile +61 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/QUICKSTART.md +190 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/README.md +282 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/__init__.py +17 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/tb_sll.py +79 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/uvm/README.md +242 -0
- Weiyet_RTLStructLib/source/Singly_Linked_List/vip/uvm/agent/sll_vip_driver.sv +62 -0
- Weiyet_RTLStructLib/source/Systolic_Array/README.md +240 -0
- Weiyet_RTLStructLib/source/Systolic_Array/src/pe.sv +121 -0
- Weiyet_RTLStructLib/source/Systolic_Array/src/rtl_list.f +3 -0
- Weiyet_RTLStructLib/source/Systolic_Array/src/systolic_array.sv +165 -0
- Weiyet_RTLStructLib/source/Systolic_Array/src/systolic_array_top.sv +349 -0
- Weiyet_RTLStructLib/source/Systolic_Array/tb/cocotb/Makefile +46 -0
- Weiyet_RTLStructLib/source/Systolic_Array/tb/cocotb/tb.py +378 -0
- Weiyet_RTLStructLib/source/Systolic_Array/tb/sv/Makefile +28 -0
- Weiyet_RTLStructLib/source/Systolic_Array/tb/sv/tb.sv +527 -0
- Weiyet_RTLStructLib/source/Table/README.md +83 -0
- Weiyet_RTLStructLib/source/Table/src/rtl_file.f +1 -0
- Weiyet_RTLStructLib/source/Table/src/table.sv +60 -0
- Weiyet_RTLStructLib/source/Table/tb/cocotb/Makefile +44 -0
- Weiyet_RTLStructLib/source/Table/tb/cocotb/tb.py +193 -0
- Weiyet_RTLStructLib/source/Table/tb/sv/Makefile +39 -0
- Weiyet_RTLStructLib/source/Table/tb/sv/tb.sv +181 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/Makefile +48 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/README.md +74 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/__init__.py +1 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/__init__.py +1 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_agent.py +33 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_driver.py +92 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_monitor.py +77 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_sequencer.py +7 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/common/__init__.py +7 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/common/table_vip_config.py +11 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/common/table_vip_seq_item.py +70 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/env/__init__.py +1 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/env/table_vip_env.py +33 -0
- Weiyet_RTLStructLib/source/Table/vip/pyuvm/env/table_vip_scoreboard.py +74 -0
Weiyet_RTLStructLib/source/.gitignore
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore simulation files
|
| 2 |
+
*.log
|
| 3 |
+
*.vcd
|
| 4 |
+
*.sim
|
| 5 |
+
XilinxUnisimLibrary
|
| 6 |
+
*netlist.v
|
| 7 |
+
__pycache__
|
| 8 |
+
iverilog_dump.v
|
| 9 |
+
results.xml
|
| 10 |
+
sim_build
|
Weiyet_RTLStructLib/source/Doubly_Linked_List/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Doubly Linked List
|
| 2 |
+
|
| 3 |
+
A configurable hardware implementation of a doubly linked list data structure in Verilog. The module supports bidirectional traversal with both forward and backward pointers, providing efficient insertion, deletion, and reading operations at specific addresses or indices.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Configurable data width and maximum node count
|
| 8 |
+
- Bidirectional traversal support
|
| 9 |
+
- Multiple insertion and deletion operations
|
| 10 |
+
- Hardware-based node management
|
| 11 |
+
- Full status monitoring (full, empty, length)
|
| 12 |
+
- Fault detection
|
| 13 |
+
- Head and tail pointer tracking
|
| 14 |
+
- Synchronous operation with start/done handshaking
|
| 15 |
+
|
| 16 |
+
## Parameters
|
| 17 |
+
|
| 18 |
+
| Parameter | Description | Default |
|
| 19 |
+
|-------------|--------------------------------------------------|---------|
|
| 20 |
+
| DATA_WIDTH | Width of data stored in each node | 8 |
|
| 21 |
+
| MAX_NODE | Maximum number of nodes supported | 8 |
|
| 22 |
+
|
| 23 |
+
## Interface Signals
|
| 24 |
+
|
| 25 |
+
| Signal | Direction | Width | Description |
|
| 26 |
+
|-----------------|-----------|---------------|------------------------------------------------|
|
| 27 |
+
| clk | Input | 1 | System clock |
|
| 28 |
+
| rst | Input | 1 | Active high reset |
|
| 29 |
+
| data_in | Input | DATA_WIDTH | Input data for insertion |
|
| 30 |
+
| addr_in | Input | ADDR_WIDTH | Target address/index for operations |
|
| 31 |
+
| op | Input | 3 | Operation code |
|
| 32 |
+
| op_start | Input | 1 | Start signal for operation |
|
| 33 |
+
| op_done | Output | 1 | Operation completion signal |
|
| 34 |
+
| data_out | Output | DATA_WIDTH | Output data from read operations |
|
| 35 |
+
| pre_node_addr | Output | ADDR_WIDTH | Address of previous node in list |
|
| 36 |
+
| next_node_addr | Output | ADDR_WIDTH | Address of next node in list |
|
| 37 |
+
| length | Output | ADDR_WIDTH | Current number of nodes in list |
|
| 38 |
+
| head | Output | ADDR_WIDTH | Address of first node |
|
| 39 |
+
| tail | Output | ADDR_WIDTH | Address of last node |
|
| 40 |
+
| full | Output | 1 | List full indicator |
|
| 41 |
+
| empty | Output | 1 | List empty indicator |
|
| 42 |
+
| fault | Output | 1 | Error indicator |
|
| 43 |
+
|
| 44 |
+
## Supported Operations
|
| 45 |
+
|
| 46 |
+
| Op Code | Operation | Description |
|
| 47 |
+
|---------|------------------|---------------------------------------------------------|
|
| 48 |
+
| 0 | Read_Addr | Read data at specified address |
|
| 49 |
+
| 1 | Insert_Addr | Insert new node at specified address |
|
| 50 |
+
| 2 | Delete_Value | Delete first occurrence of specified value |
|
| 51 |
+
| 3 | Delete_Addr | Delete node at specified address |
|
| 52 |
+
| 5 | Insert_Index | Insert new node at specified index |
|
| 53 |
+
| 7 | Delete_Index | Delete node at specified index |
|
| 54 |
+
|
| 55 |
+
## State Machine
|
| 56 |
+
|
| 57 |
+
The module implements a FSM with the following states:
|
| 58 |
+
|
| 59 |
+
1. **IDLE (3'b000)**
|
| 60 |
+
- Initial state, waits for operation start
|
| 61 |
+
- Validates operation parameters
|
| 62 |
+
- Handles direct head/tail operations
|
| 63 |
+
|
| 64 |
+
2. **FIND_ADDR (3'b001)**
|
| 65 |
+
- Traverses list to find target address
|
| 66 |
+
- Used for address-based operations
|
| 67 |
+
|
| 68 |
+
3. **FIND_VALUE (3'b010)**
|
| 69 |
+
- Traverses list to find target value
|
| 70 |
+
- Used for value-based deletion
|
| 71 |
+
|
| 72 |
+
4. **FIND_INDEX (3'b110)**
|
| 73 |
+
- Traverses list to find target index
|
| 74 |
+
- Supports both ascending and descending search
|
| 75 |
+
|
| 76 |
+
5. **INSERT_STG1 (3'b011)**
|
| 77 |
+
- First stage of node insertion
|
| 78 |
+
- Updates bidirectional links
|
| 79 |
+
|
| 80 |
+
6. **EXECUTE (3'b100)**
|
| 81 |
+
- Completes operation
|
| 82 |
+
- Updates status signals
|
| 83 |
+
|
| 84 |
+
7. **FAULT (3'b101)**
|
| 85 |
+
- Handles error conditions
|
| 86 |
+
|
| 87 |
+
## Memory Organization
|
| 88 |
+
|
| 89 |
+
The implementation uses three separate memory arrays:
|
| 90 |
+
1. Data storage (`node_data`)
|
| 91 |
+
2. Forward pointers (`node_next_node_addr`)
|
| 92 |
+
3. Backward pointers (`node_pre_node_addr`)
|
| 93 |
+
|
| 94 |
+
This organization allows for:
|
| 95 |
+
- Independent access to data and pointers
|
| 96 |
+
- Efficient pointer updates
|
| 97 |
+
- Parallel validity checking
|
| 98 |
+
|
| 99 |
+
## Usage Example
|
| 100 |
+
|
| 101 |
+
```verilog
|
| 102 |
+
// Instantiate a doubly linked list with 16 nodes and 32-bit data
|
| 103 |
+
doubly_linked_list #(
|
| 104 |
+
.DATA_WIDTH(32),
|
| 105 |
+
.MAX_NODE(16)
|
| 106 |
+
) list_inst (
|
| 107 |
+
.clk(system_clock),
|
| 108 |
+
.rst(reset),
|
| 109 |
+
.data_in(input_data),
|
| 110 |
+
.addr_in(target_addr),
|
| 111 |
+
.op(operation_code),
|
| 112 |
+
.op_start(start_signal),
|
| 113 |
+
.op_done(done_signal),
|
| 114 |
+
.data_out(output_data),
|
| 115 |
+
.pre_node_addr(prev_addr),
|
| 116 |
+
.next_node_addr(next_addr),
|
| 117 |
+
.length(list_length),
|
| 118 |
+
.head(head_addr),
|
| 119 |
+
.tail(tail_addr),
|
| 120 |
+
.full(list_full),
|
| 121 |
+
.empty(list_empty),
|
| 122 |
+
.fault(error_signal)
|
| 123 |
+
);
|
| 124 |
+
```
|
| 125 |
+
## Reset Behavior
|
| 126 |
+
|
| 127 |
+
On assertion of reset:
|
| 128 |
+
- All nodes are invalidated
|
| 129 |
+
- Head and tail pointers set to NULL
|
| 130 |
+
- Length cleared to zero
|
| 131 |
+
- All node data cleared
|
| 132 |
+
- All status signals deasserted
|
| 133 |
+
|
| 134 |
+
## Implementation Notes
|
| 135 |
+
|
| 136 |
+
1. **Memory Organization**:
|
| 137 |
+
- Next node pointers stored separately
|
| 138 |
+
- Valid bits for node tracking
|
| 139 |
+
|
| 140 |
+
2. **Address Handling**:
|
| 141 |
+
- NULL address = MAX_NODE
|
| 142 |
+
- Valid addresses: 0 to (MAX_NODE-1)
|
| 143 |
+
|
| 144 |
+
3. **Error Conditions**:
|
| 145 |
+
- List full during insertion
|
| 146 |
+
- List empty during deletion/read
|
| 147 |
+
- Invalid address/index
|
| 148 |
+
- Address overflow
|
| 149 |
+
|
| 150 |
+
## Limitations
|
| 151 |
+
|
| 152 |
+
- Fixed maximum size defined by MAX_NODE
|
| 153 |
+
- Sequential search for operations
|
| 154 |
+
- Single operation at a time
|
| 155 |
+
- No concurrent access support
|
| 156 |
+
|
Weiyet_RTLStructLib/source/Dual_Edge_FF/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dual Edge Flip Flop
|
| 2 |
+
## Overview
|
| 3 |
+
This Verilog module implements a dual-edge triggered flip-flop that can capture data on both the positive and negative edges of the clock signal. This design effectively doubles the data throughput rate compared to traditional single-edge flip-flops by utilizing both clock transitions for data latching operations.
|
| 4 |
+
|
| 5 |
+
## Parameters
|
| 6 |
+
| Parameter | Description | Default |
|
| 7 |
+
|-------------|---------------------------------------|---------|
|
| 8 |
+
| DATA_WIDTH | Width of data bus in bits | 8 |
|
| 9 |
+
| RESET_VALUE | Reset value of flops | 0 |
|
| 10 |
+
|
| 11 |
+
## Port Descriptions
|
| 12 |
+
### Input Ports
|
| 13 |
+
| Port | Width | Description |
|
| 14 |
+
|-------------------|-------------|------------------------------------------------|
|
| 15 |
+
| clk | 1 | System clock signal |
|
| 16 |
+
| rst_n | 1 | Active-low asynchronous reset signal |
|
| 17 |
+
| data_in | DATA_WIDTH | Input data to be latched |
|
| 18 |
+
| pos_edge_latch_en | DATA_WIDTH | Per-bit latch enable for positive clock edge |
|
| 19 |
+
| neg_edge_latch_en | DATA_WIDTH | Per-bit latch enable for negative clock edge |
|
| 20 |
+
|
| 21 |
+
### Output Ports
|
| 22 |
+
| Port | Width | Description |
|
| 23 |
+
|----------|------------|--------------------------------|
|
| 24 |
+
| data_out | DATA_WIDTH | Latched output data |
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
## Usage Example
|
| 28 |
+
Below shows a simple usage of a counter that increments on both edges of the clock.
|
| 29 |
+
```verilog
|
| 30 |
+
always @ (*) begin
|
| 31 |
+
if (data_out <= {(DATA_WIDTH){1'b1}}) input_data <= data_out + 1;
|
| 32 |
+
else input_data <= data_out;
|
| 33 |
+
end
|
| 34 |
+
|
| 35 |
+
dual_edge_ff #(
|
| 36 |
+
.DATA_WIDTH(16),
|
| 37 |
+
.RESET_VALUE(16'h0000)
|
| 38 |
+
) dual_ff_inst (
|
| 39 |
+
.clk(system_clk),
|
| 40 |
+
.rst_n(system_rst_n),
|
| 41 |
+
.data_in(input_data),
|
| 42 |
+
.pos_edge_latch_en(16'hFFFF), // Enable all bits on positive edge
|
| 43 |
+
.neg_edge_latch_en(16'hFFFF), // Enable all bits on negative edge
|
| 44 |
+
.data_out(output_data)
|
| 45 |
+
);
|
| 46 |
+
```
|
Weiyet_RTLStructLib/source/FIFO/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# FIFO (Queue)
|
| 2 |
+
|
| 3 |
+
A configurable FIFO (First-In-First-Out) buffer implementation in Verilog with support for both synchronous and asynchronous clock domains. The module uses Gray code for pointer synchronization in asynchronous mode to prevent metastability issues.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Configurable data width and depth
|
| 8 |
+
- Support for both synchronous and asynchronous clock domains
|
| 9 |
+
- Optional registered read output
|
| 10 |
+
- Gray code synchronization for clock domain crossing
|
| 11 |
+
- Full and empty status flags
|
| 12 |
+
- Automatic pointer wraparound
|
| 13 |
+
- Parameterized design for easy customization
|
| 14 |
+
|
| 15 |
+
## Parameters
|
| 16 |
+
|
| 17 |
+
| Parameter | Description | Default |
|
| 18 |
+
|-------------|--------------------------------------------------|---------|
|
| 19 |
+
| DEPTH | Number of entries in the FIFO | 12 |
|
| 20 |
+
| DATA_WIDTH | Width of each data entry | 8 |
|
| 21 |
+
| ASYNC | Enable asynchronous mode (1) or synchronous (0) | 1 |
|
| 22 |
+
| RD_BUFFER | Enable registered read output (1) or bypass (0) | 1 |
|
| 23 |
+
|
| 24 |
+
## Interface Signals
|
| 25 |
+
|
| 26 |
+
| Signal | Direction | Width | Description |
|
| 27 |
+
|-------------|-----------|---------------|------------------------------------------------|
|
| 28 |
+
| rd_clk | Input | 1 | Read clock domain |
|
| 29 |
+
| wr_clk | Input | 1 | Write clock domain |
|
| 30 |
+
| rst | Input | 1 | Active high reset |
|
| 31 |
+
| data_wr | Input | DATA_WIDTH | Data input for writing |
|
| 32 |
+
| wr_en | Input | 1 | Write enable signal |
|
| 33 |
+
| fifo_full | Output | 1 | FIFO full indicator |
|
| 34 |
+
| data_rd | Output | DATA_WIDTH | Data output from reading |
|
| 35 |
+
| rd_en | Input | 1 | Read enable signal |
|
| 36 |
+
| fifo_empty | Output | 1 | FIFO empty indicator |
|
| 37 |
+
|
| 38 |
+
## Usage Example
|
| 39 |
+
|
| 40 |
+
```verilog
|
| 41 |
+
// Instantiate a 16-deep, 32-bit wide asynchronous FIFO with registered output
|
| 42 |
+
fifo #(
|
| 43 |
+
.DEPTH(16),
|
| 44 |
+
.DATA_WIDTH(32),
|
| 45 |
+
.ASYNC(1),
|
| 46 |
+
.RD_BUFFER(1)
|
| 47 |
+
) fifo_inst (
|
| 48 |
+
.rd_clk(read_clock),
|
| 49 |
+
.wr_clk(write_clock),
|
| 50 |
+
.rst(reset),
|
| 51 |
+
.data_wr(write_data),
|
| 52 |
+
.wr_en(write_enable),
|
| 53 |
+
.fifo_full(full_flag),
|
| 54 |
+
.data_rd(read_data),
|
| 55 |
+
.rd_en(read_enable),
|
| 56 |
+
.fifo_empty(empty_flag)
|
| 57 |
+
);
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
## Design Notes
|
| 61 |
+
|
| 62 |
+
1. **Clock Domain Crossing**: In asynchronous mode (ASYNC=1), the design uses Gray code encoding for pointer synchronization to prevent metastability issues when crossing clock domains.
|
| 63 |
+
|
| 64 |
+
2. **Read Buffer**: When RD_BUFFER=1, the read data output is registered, adding one clock cycle of latency but improving timing. When RD_BUFFER=0, the read data is combinatorial.
|
| 65 |
+
|
| 66 |
+
3. **Full/Empty Flags**:
|
| 67 |
+
- The empty flag is asserted when read and write pointers are equal
|
| 68 |
+
- The full flag is asserted when the next write pointer would equal the current read pointer
|
| 69 |
+
|
| 70 |
+
4. **Reset Behavior**:
|
| 71 |
+
- All pointers are reset to zero
|
| 72 |
+
- All memory locations are cleared
|
| 73 |
+
- All flags are deasserted
|
| 74 |
+
|
| 75 |
+
## Implementation Details
|
| 76 |
+
|
| 77 |
+
The module uses separate read and write pointers for tracking FIFO occupancy. In asynchronous mode, these pointers are converted to Gray code before being synchronized across clock domains. The implementation includes:
|
| 78 |
+
|
| 79 |
+
- Binary counters for read/write operation
|
| 80 |
+
- Gray code conversion for pointer synchronization
|
| 81 |
+
- Two-stage synchronizers for clock domain crossing
|
| 82 |
+
- Configurable output registration
|
| 83 |
+
- Automatic pointer wraparound at FIFO boundaries
|
| 84 |
+
|
| 85 |
+
## Timing Considerations
|
| 86 |
+
|
| 87 |
+
1. In asynchronous mode, allow at least 2 clock cycles for pointer synchronization
|
| 88 |
+
2. When RD_BUFFER=1, read data is available one clock cycle after rd_en assertion
|
| 89 |
+
3. Full and empty flags are registered outputs
|
| 90 |
+
|
| 91 |
+
## Limitations
|
| 92 |
+
|
| 93 |
+
- The DEPTH parameter must be a power of 2 for proper wraparound behavior
|
| 94 |
+
- Simultaneous read/write at full/empty conditions should be managed by external logic
|
| 95 |
+
- The reset signal must be synchronized to both clock domains
|
Weiyet_RTLStructLib/source/Hash_Table/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hash Table Module
|
| 2 |
+
## Overview
|
| 3 |
+
This Verilog module implements a flexible hash table with configurable collision resolution methods and hash algorithms. It provides support for key-value storage with operations such as insert, delete, and search.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
- Configurable key and value widths
|
| 7 |
+
- Support for different collision resolution methods
|
| 8 |
+
- Multiple hash algorithm options
|
| 9 |
+
- Synchronous operation with reset capability
|
| 10 |
+
- Collision tracking and error reporting
|
| 11 |
+
|
| 12 |
+
## Parameters
|
| 13 |
+
| Parameter | Description | Default |
|
| 14 |
+
|-----------|-------------|---------|
|
| 15 |
+
| KEY_WIDTH | Width of keys in bits | 32 |
|
| 16 |
+
| VALUE_WIDTH | Width of values in bits | 32 |
|
| 17 |
+
| TOTAL_ENTRY | Number of indices in the hash table | 64 |
|
| 18 |
+
| CHAINING_SIZE | Maximum chain length for collision resolution | 4 |
|
| 19 |
+
| COLLISION_METHOD | Method for handling collisions | "MULTI_STAGE_CHAINING" |
|
| 20 |
+
| HASH_ALGORITHM | Algorithm used for hashing | "MODULUS" |
|
| 21 |
+
|
| 22 |
+
## Port Descriptions
|
| 23 |
+
### Input Ports
|
| 24 |
+
| Port | Width | Description |
|
| 25 |
+
|------|-------|-------------|
|
| 26 |
+
| clk | 1 | System clock signal |
|
| 27 |
+
| rst | 1 | Active-high reset signal |
|
| 28 |
+
| key_in | KEY_WIDTH | Key for insert, delete, or search operations |
|
| 29 |
+
| value_in | VALUE_WIDTH | Value to be stored (for insert operations) |
|
| 30 |
+
| op_sel | 2 | Operation selector (00: Insert, 01: Delete, 10: Search) |
|
| 31 |
+
| op_en | 1 | Operation enable signal |
|
| 32 |
+
|
| 33 |
+
### Output Ports
|
| 34 |
+
| Port | Width | Description |
|
| 35 |
+
|------|-------|-------------|
|
| 36 |
+
| value_out | VALUE_WIDTH | Value retrieved during search operations |
|
| 37 |
+
| op_done | 1 | Operation completion indicator |
|
| 38 |
+
| op_error | 1 | Error indicator (FULL for insert, KEY_NOT_FOUND for delete/search) |
|
| 39 |
+
| collision_count | log2(CHAINING_SIZE) | Number of collisions encountered |
|
| 40 |
+
|
| 41 |
+
## Collision Methods
|
| 42 |
+
- **MULTI_STAGE_CHAINING**: Multiple entries at the same index using a linked list approach
|
| 43 |
+
- **LINEAR_PROBING**: Referenced in parameters but not fully implemented in the provided code
|
| 44 |
+
|
| 45 |
+
## Hash Algorithms
|
| 46 |
+
- **MODULUS**: Simple modulus operation (key % TABLE_SIZE)
|
| 47 |
+
- **SHA1**, **FNV1A**: Referenced in parameters but not fully implemented in the provided code
|
| 48 |
+
|
| 49 |
+
## Timing
|
| 50 |
+
- All operations are synchronized to the positive edge of the clock
|
| 51 |
+
- Reset is asynchronous and active-high
|
| 52 |
+
- Operations are initiated when op_en is asserted
|
| 53 |
+
- op_done indicates completion of an operation
|
| 54 |
+
|
| 55 |
+
## Usage Example
|
| 56 |
+
```verilog
|
| 57 |
+
hash_table #(
|
| 58 |
+
.KEY_WIDTH(32),
|
| 59 |
+
.VALUE_WIDTH(32),
|
| 60 |
+
.TOTAL_ENTRY(128),
|
| 61 |
+
.CHAINING_SIZE(8),
|
| 62 |
+
.COLLISION_METHOD("MULTI_STAGE_CHAINING"),
|
| 63 |
+
.HASH_ALGORITHM("MODULUS")
|
| 64 |
+
) hash_module (
|
| 65 |
+
.clk(system_clk),
|
| 66 |
+
.rst(system_rst),
|
| 67 |
+
.key_in(key),
|
| 68 |
+
.value_in(value),
|
| 69 |
+
.op_sel(operation),
|
| 70 |
+
.op_en(enable),
|
| 71 |
+
.value_out(retrieved_value),
|
| 72 |
+
.op_done(operation_complete),
|
| 73 |
+
.op_error(operation_error),
|
| 74 |
+
.collision_count(collisions)
|
| 75 |
+
);
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
## Implementation Details
|
| 79 |
+
- Uses a state machine for operation control
|
| 80 |
+
- Implements chained hash entries for collision resolution
|
| 81 |
+
- Provides error reporting for table overflow or key not found conditions
|
| 82 |
+
- Uses Verilog parameter-based configuration for flexibility
|
| 83 |
+
|
| 84 |
+
## Limitations and Issues
|
| 85 |
+
- Implementation for alternative hash algorithms is mentioned but not provided
|
| 86 |
+
|
| 87 |
+
## Performance Considerations
|
| 88 |
+
- Search and delete operations may require multiple cycles depending on chain length
|
| 89 |
+
- Performance degrades as collision chains grow longer
|
| 90 |
+
- No optimization for locality or cache behavior
|
| 91 |
+
- Please consider using [CAM(Content Addressable Memory)](https://en.wikipedia.org/wiki/Content-addressable_memory) if you have the resources.
|
Weiyet_RTLStructLib/source/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) [Wei Yet Ng]
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, 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,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
Weiyet_RTLStructLib/source/LIFO/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# LIFO (Stack)
|
| 2 |
+
|
| 3 |
+
A configurable LIFO (Last-In-First-Out) buffer implementation in Verilog. This module implements a stack-like behavior where the last element written is the first one to be read out. The design includes bypass functionality for simultaneous read/write operations.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Configurable data width and depth
|
| 8 |
+
- Single clock domain operation
|
| 9 |
+
- Full and empty status flags
|
| 10 |
+
- Bypass path for simultaneous read/write
|
| 11 |
+
- Registered read output
|
| 12 |
+
- Automatic pointer management
|
| 13 |
+
- Parameterized design for easy customization
|
| 14 |
+
|
| 15 |
+
## Parameters
|
| 16 |
+
|
| 17 |
+
| Parameter | Description | Default |
|
| 18 |
+
|-------------|--------------------------------------------------|---------|
|
| 19 |
+
| DEPTH | Number of entries in the LIFO | 12 |
|
| 20 |
+
| DATA_WIDTH | Width of each data entry | 8 |
|
| 21 |
+
|
| 22 |
+
## Interface Signals
|
| 23 |
+
|
| 24 |
+
| Signal | Direction | Width | Description |
|
| 25 |
+
|-------------|-----------|---------------|------------------------------------------------|
|
| 26 |
+
| clk | Input | 1 | System clock |
|
| 27 |
+
| rst | Input | 1 | Active high reset |
|
| 28 |
+
| data_wr | Input | DATA_WIDTH | Data input for writing |
|
| 29 |
+
| wr_en | Input | 1 | Write enable signal |
|
| 30 |
+
| lifo_full | Output | 1 | LIFO full indicator |
|
| 31 |
+
| data_rd | Output | DATA_WIDTH | Data output from reading |
|
| 32 |
+
| rd_en | Input | 1 | Read enable signal |
|
| 33 |
+
| lifo_empty | Output | 1 | LIFO empty indicator |
|
| 34 |
+
|
| 35 |
+
## Usage Example
|
| 36 |
+
|
| 37 |
+
```verilog
|
| 38 |
+
// Instantiate a 16-deep, 32-bit wide LIFO
|
| 39 |
+
lifo #(
|
| 40 |
+
.DEPTH(16),
|
| 41 |
+
.DATA_WIDTH(32)
|
| 42 |
+
) lifo_inst (
|
| 43 |
+
.clk(system_clock),
|
| 44 |
+
.rst(reset),
|
| 45 |
+
.data_wr(write_data),
|
| 46 |
+
.wr_en(write_enable),
|
| 47 |
+
.lifo_full(full_flag),
|
| 48 |
+
.data_rd(read_data),
|
| 49 |
+
.rd_en(read_enable),
|
| 50 |
+
.lifo_empty(empty_flag)
|
| 51 |
+
);
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
## Operation Modes
|
| 55 |
+
|
| 56 |
+
The module supports three operation modes:
|
| 57 |
+
|
| 58 |
+
1. **Write Operation (wr_op)**
|
| 59 |
+
- Activated when wr_en=1 and rd_en=0
|
| 60 |
+
- Data is written to current pointer location
|
| 61 |
+
- Pointer increments if not full
|
| 62 |
+
|
| 63 |
+
2. **Read Operation (rd_op)**
|
| 64 |
+
- Activated when rd_en=1 and wr_en=0
|
| 65 |
+
- Data is read from (pointer-1) location
|
| 66 |
+
- Pointer decrements if not empty
|
| 67 |
+
|
| 68 |
+
3. **Bypass Operation (bypass_op)**
|
| 69 |
+
- Activated when both wr_en=1 and rd_en=1
|
| 70 |
+
- Input data (data_wr) is directly passed to output (data_rd)
|
| 71 |
+
- Pointer remains unchanged
|
| 72 |
+
|
| 73 |
+
## Design Notes
|
| 74 |
+
|
| 75 |
+
1. **Pointer Management**:
|
| 76 |
+
- Single pointer tracks both read and write positions
|
| 77 |
+
- Increments on write, decrements on read
|
| 78 |
+
- Range: 0 to (DEPTH-1)
|
| 79 |
+
|
| 80 |
+
2. **Memory Organization**:
|
| 81 |
+
- Stack-like structure with DEPTH entries
|
| 82 |
+
- Each entry is DATA_WIDTH bits wide
|
| 83 |
+
- Last written data is first to be read
|
| 84 |
+
|
| 85 |
+
3. **Flag Generation**:
|
| 86 |
+
- Empty flag is combinatorial (pointer = 0)
|
| 87 |
+
- Full flag is registered (pointer = DEPTH-1)
|
| 88 |
+
- Flags prevent invalid operations
|
| 89 |
+
|
| 90 |
+
## Reset Behavior
|
| 91 |
+
|
| 92 |
+
On assertion of reset:
|
| 93 |
+
- Pointer is cleared to zero
|
| 94 |
+
- All memory locations are cleared
|
| 95 |
+
- Full flag is deasserted
|
| 96 |
+
- Output data is cleared
|
| 97 |
+
- Empty flag becomes active
|
| 98 |
+
|
| 99 |
+
## Timing Considerations
|
| 100 |
+
|
| 101 |
+
1. All outputs except lifo_empty are registered
|
| 102 |
+
2. Data is available on the next clock cycle after rd_en assertion
|
| 103 |
+
3. Bypass operation provides data in the same clock cycle
|
| 104 |
+
|
| 105 |
+
## Limitations
|
| 106 |
+
|
| 107 |
+
- Single clock domain operation only
|
| 108 |
+
- No protection against overflow/underflow if flags are ignored
|
| 109 |
+
- DEPTH parameter should be at least 2 for proper operation
|
| 110 |
+
- Simultaneous read/write operations bypass the stack memory
|
| 111 |
+
|
| 112 |
+
## Alternative Implementation Note
|
| 113 |
+
|
| 114 |
+
The commented code at the bottom of the module suggests an alternative implementation for full flag handling and read operations, which could be implemented if different timing behavior is needed.
|
Weiyet_RTLStructLib/source/List/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# List Module
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
The List module is a versatile data storage and manipulation component implemented in Verilog. It provides various operations for managing and processing data, including reading, writing, summing, and sorting functionalities. This module can be particularly useful in FPGA designs requiring data buffering, accumulation, or processing.
|
| 5 |
+
|
| 6 |
+
## Features
|
| 7 |
+
- Data storage with configurable width and length
|
| 8 |
+
- Multiple data manipulation operations:
|
| 9 |
+
- Read/write operations
|
| 10 |
+
- Element searching
|
| 11 |
+
- Data summation with multiple implementation methods
|
| 12 |
+
- Sorting (ascending and descending)
|
| 13 |
+
- Configurable sum calculation methods:
|
| 14 |
+
- Parallel sum (combinatorial)
|
| 15 |
+
- Sequential sum
|
| 16 |
+
- Adder tree
|
| 17 |
+
|
| 18 |
+
## Module Instantiation
|
| 19 |
+
``` verilog
|
| 20 |
+
// Instantiation with custom parameters
|
| 21 |
+
list #(
|
| 22 |
+
.DATA_WIDTH(16), // 16-bit data elements
|
| 23 |
+
.LENGTH(32), // List can store up to 32 elements
|
| 24 |
+
.SUM_METHOD(1) // Use sequential summation method
|
| 25 |
+
) custom_list (
|
| 26 |
+
.clk(clk),
|
| 27 |
+
.rst(rst),
|
| 28 |
+
.op_sel(op_sel),
|
| 29 |
+
.op_en(op_en),
|
| 30 |
+
.data_in(data_in),
|
| 31 |
+
.index_in(index_in),
|
| 32 |
+
.data_out(data_out),
|
| 33 |
+
.op_done(op_done),
|
| 34 |
+
.op_in_progress(op_in_progress),
|
| 35 |
+
.op_error(op_error)
|
| 36 |
+
);
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
## Parameters
|
| 40 |
+
|
| 41 |
+
| Parameter | Description | Default |
|
| 42 |
+
|--------------|--------------------------------------------------|---------|
|
| 43 |
+
| DATA_WIDTH | Width of each data element in bits | 32 |
|
| 44 |
+
| LENGTH | Maximum number of elements in the list | 8 |
|
| 45 |
+
| SUM_METHOD | Method for calculating sum (0: parallel, 1: sequential, 2: adder tree) | 0 |
|
| 46 |
+
|
| 47 |
+
## IO Ports
|
| 48 |
+
|
| 49 |
+
| Port | Direction | Width | Description |
|
| 50 |
+
|----------------|-----------|-------------------------------|-------------------------------------------|
|
| 51 |
+
| clk | input | 1 | System clock |
|
| 52 |
+
| rst | input | 1 | Reset signal (active high) |
|
| 53 |
+
| op_sel | input | 2 | Operation selector |
|
| 54 |
+
| op_en | input | 1 | Operation enable |
|
| 55 |
+
| data_in | input | DATA_WIDTH | Input data for write operations |
|
| 56 |
+
| index_in | input | LENGTH_WIDTH | Index for read/write operations |
|
| 57 |
+
| data_out | output | LENGTH_WIDTH+DATA_WIDTH | Output data |
|
| 58 |
+
| op_done | output | 1 | Operation completion indicator |
|
| 59 |
+
| op_in_progress | output | 1 | Operation is in progress |
|
| 60 |
+
| op_error | output | 1 | Operation error indicator |
|
| 61 |
+
|
| 62 |
+
## Operation Codes
|
| 63 |
+
|
| 64 |
+
| op_sel | Operation | Description |
|
| 65 |
+
|--------|---------------------|------------------------------------------|
|
| 66 |
+
| 3'b000 | Read | Read data from specified index |
|
| 67 |
+
| 3'b001 | Insert | Write data to specified index |
|
| 68 |
+
| 3'b010 | Find All Indices | Find all indices matching data_in |
|
| 69 |
+
| 3'b011 | Find First Index | Find first index matching data_in |
|
| 70 |
+
| 3'b100 | Sum | Calculate sum of all elements |
|
| 71 |
+
| 3'b101 | Sort Ascending | Sort elements in ascending order |
|
| 72 |
+
| 3'b110 | Sort Descending | Sort elements in descending order |
|
| 73 |
+
|
| 74 |
+
## State Machine
|
| 75 |
+
The module implements a simple state machine with the following states:
|
| 76 |
+
- **IDLE**: Default state, waiting for operation
|
| 77 |
+
- **SUM**: Calculating sum in progress (for sequential or adder tree methods)
|
| 78 |
+
- **SORT**: Sorting the list in progress (implementation in progress)
|
| 79 |
+
- **ACCESS_DONE**: Operation completed, transitioning back to IDLE
|
| 80 |
+
|
| 81 |
+
## Implementation Notes
|
| 82 |
+
- The internal storage is implemented as a register array, which could be replaced with RAM for larger data sizes
|
| 83 |
+
- The parallel sum implementation uses combinatorial logic
|
| 84 |
+
- Sequential sum and adder tree implementations are placeholders in the current version
|
| 85 |
+
|
Weiyet_RTLStructLib/source/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<p align="center">
|
| 2 |
+
<img src="./assets/RTLStructLib_logo.png" width="240""/>
|
| 3 |
+
</p>
|
| 4 |
+
|
| 5 |
+
<div align="center">
|
| 6 |
+
|
| 7 |
+

|
| 8 |
+

|
| 9 |
+

|
| 10 |
+

|
| 11 |
+

|
| 12 |
+
|
| 13 |
+
</div>
|
| 14 |
+
|
| 15 |
+
# RTLStructLib
|
| 16 |
+
Highly optimized (trying my best), synthesizable data structures module/IP library for hardware design
|
| 17 |
+
|
| 18 |
+
### Overview
|
| 19 |
+
RTLStructlib is an open-source project providing a collection of synthesizable RTL data structures implemented at the Register-Transfer Level (RTL). These modules are designed for high performance, scalability, and ease of integration into digital systems, serving as a standard library for FPGA and ASIC engineers.
|
| 20 |
+
By using these pre-built RTL modules, engineers can accelerate development, reduce verification time, and focus on higher-level system design.
|
| 21 |
+
|
| 22 |
+
### Features
|
| 23 |
+
✅ Synthesizable, Optimized, Modular and Reusable <br>
|
| 24 |
+
✅ Fully parameterized <br>
|
| 25 |
+
✅ Comprehensive verification sequence and testbench <br>
|
| 26 |
+
✅ Verification IP (VIP) <br>
|
| 27 |
+
✅ Open-source and community-driven <br>
|
| 28 |
+
|
| 29 |
+
### Supported Data Structures
|
| 30 |
+
- FIFO (First-In-First-Out) Queue – Parameterized depth, support for synchronous & asynchronous modes <br>
|
| 31 |
+
- LIFO (Last-In-First-Out) Stack – Configurable width and depth <br>
|
| 32 |
+
- Singly Linked List – Efficient memory utilization, dynamic data handling <br>
|
| 33 |
+
- Doubly Linked List – Bi-directional traversal support <br>
|
| 34 |
+
- Table - Indexed storage mechanism, similar to a register file, enabling rapid direct access and simultaneous read write access to data without hashing. <br>
|
| 35 |
+
- List - Support sorting, find_index, delete, insert operations <br>
|
| 36 |
+
- Circular Linked List (WIP)
|
| 37 |
+
- Hash Table – Optimized for high-speed lookups, currently only supports modulus hashing and simple multi-staged chaining to handle collision <br>
|
| 38 |
+
- Dual Edge Flip Flop - Double input and output rate structure which can latch data on both rising and falling edge <br>
|
| 39 |
+
- Systolic Array (WIP) - Organizes processing elements in a regular grid where data flows rhythmically, enabling parallel computation. <br>
|
| 40 |
+
- Binary Tree (WIP) – Fundamental structure for hierarchical data organization <br>
|
| 41 |
+
- AVL Tree (WIP) – Self-balancing binary search tree for efficient operations <br>
|
| 42 |
+
- And More and More and More (WIP)
|
| 43 |
+
|
| 44 |
+
### License
|
| 45 |
+
This project is licensed under the MIT License – see the LICENSE file for details.
|
| 46 |
+
|
| 47 |
+
### Getting Started
|
| 48 |
+
1️⃣ Install required tools and package
|
| 49 |
+
``` bash
|
| 50 |
+
sudo apt install make git iverilog yosys gtkwave
|
| 51 |
+
pip install cocotb
|
| 52 |
+
pip install cocotb-bus
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
1️⃣ Clone the Repository <br>
|
| 56 |
+
``` bash
|
| 57 |
+
git clone https://github.com/Weiyet/RTL_Data_Structure.git
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
2️⃣ Directory Structure of Each Data Structure Module <br>
|
| 61 |
+
````
|
| 62 |
+
📦 <data structure>/ # Data Structure Module as folder name <br>
|
| 63 |
+
├── 📃 readme.md # Documentation of waveform, modules IOs, parameter. <br>
|
| 64 |
+
├── 📂 src/ # RTL Source Code <br>
|
| 65 |
+
│ ├── 📃 rtl_list.f # RTL file list required for the modules <br>
|
| 66 |
+
├── 📂 tb/ # Testbench Directory <br>
|
| 67 |
+
│ ├── 📂 cocotb/ # Python Cocotb (Non-UVM) Testbench <br>
|
| 68 |
+
│ ├── 📂 sv/ # SystemVerilog (Non-UVM) Testbench <br>
|
| 69 |
+
├── 📂 vip/ # Verification IP <br>
|
| 70 |
+
│ ├── 📂 uvm/ # system verilog UVM <br>
|
| 71 |
+
| | ├── 📃 readme.md # Documentation of VIP <br>
|
| 72 |
+
│ ├── 📂 pyuvm/ # python UVM <br>
|
| 73 |
+
| | ├── 📃 readme.md # Documentation of VIP <br>
|
| 74 |
+
````
|
| 75 |
+
2️⃣ RTL Simulation and Verification
|
| 76 |
+
``` bash
|
| 77 |
+
# System Verilog Simulation
|
| 78 |
+
cd <Data Structure>/tb/sv
|
| 79 |
+
make sim
|
| 80 |
+
# Python CocoTB Simulation
|
| 81 |
+
cd <Data Structure>/tb/cocotb
|
| 82 |
+
make
|
| 83 |
+
```
|
| 84 |
+
3️⃣ Synthesis and Netlist simulation
|
| 85 |
+
``` bash
|
| 86 |
+
make synth
|
| 87 |
+
```
|
| 88 |
+
4️⃣ To view VCD waveform
|
| 89 |
+
``` bash
|
| 90 |
+
gtkwave <waveform.vcd>
|
| 91 |
+
```
|
| 92 |
+
5️⃣ Integrate to your project
|
| 93 |
+
Include file list <Data structure>/src/rtl_list.f to your simulation or project.
|
| 94 |
+
|
| 95 |
+
### Work in Progress/Future Works 🚀
|
| 96 |
+
🔹 Implementing Hash Binary Tree, AVL Tree and more and more <br>
|
| 97 |
+
🔹 Improving performance & adding more use cases <br>
|
| 98 |
+
🔹 Study research paper and implement more hardware oriented algorithm or data streamline for HW data structure <br>
|
| 99 |
+
|
| 100 |
+
### Disclaimer
|
| 101 |
+
Hardware is often highly customized — these modules are designed as references, and you're encouraged to tweak them as needed (e.g., swap registers with RAM, adjust logic latency based on your STA, use content addressable RAM instead of RTL hash table).
|
Weiyet_RTLStructLib/source/Singly_Linked_List/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Singly Linked List
|
| 2 |
+
|
| 3 |
+
A configurable hardware implementation of a singly linked list data structure in Verilog. The module supports basic linked list operations including insertion, deletion, and reading at specific addresses or indices, with full hardware synchronization and status monitoring.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Configurable data width and maximum node count
|
| 8 |
+
- Multiple insertion and deletion operations
|
| 9 |
+
- Hardware-based node management
|
| 10 |
+
- Status monitoring (full, empty, length)
|
| 11 |
+
- Fault detection
|
| 12 |
+
- Head and tail pointer tracking
|
| 13 |
+
- Synchronous operation with start/done handshaking
|
| 14 |
+
|
| 15 |
+
## Parameters
|
| 16 |
+
|
| 17 |
+
| Parameter | Description | Default |
|
| 18 |
+
|-------------|--------------------------------------------------|---------|
|
| 19 |
+
| DATA_WIDTH | Width of data stored in each node | 8 |
|
| 20 |
+
| MAX_NODE | Maximum number of nodes supported | 8 |
|
| 21 |
+
|
| 22 |
+
## Interface Signals
|
| 23 |
+
|
| 24 |
+
| Signal | Direction | Width | Description |
|
| 25 |
+
|-----------------|-----------|---------------|------------------------------------------------|
|
| 26 |
+
| clk | Input | 1 | System clock |
|
| 27 |
+
| rst | Input | 1 | Active high reset |
|
| 28 |
+
| data_in | Input | DATA_WIDTH | Input data for insertion |
|
| 29 |
+
| addr_in | Input | ADDR_WIDTH | Target address/index for operations |
|
| 30 |
+
| op | Input | 3 | Operation code |
|
| 31 |
+
| op_start | Input | 1 | Start signal for operation |
|
| 32 |
+
| op_done | Output | 1 | Operation completion signal |
|
| 33 |
+
| data_out | Output | DATA_WIDTH | Output data from read operations |
|
| 34 |
+
| next_node_addr | Output | ADDR_WIDTH | Address of next node in list |
|
| 35 |
+
| length | Output | ADDR_WIDTH | Current number of nodes in list |
|
| 36 |
+
| head | Output | ADDR_WIDTH | Address of first node |
|
| 37 |
+
| tail | Output | ADDR_WIDTH | Address of last node |
|
| 38 |
+
| full | Output | 1 | List full indicator |
|
| 39 |
+
| empty | Output | 1 | List empty indicator |
|
| 40 |
+
| fault | Output | 1 | Error indicator |
|
| 41 |
+
|
| 42 |
+
## Supported Operations
|
| 43 |
+
|
| 44 |
+
| Op Code | Operation | Description |
|
| 45 |
+
|---------|--------------------|---------------------------------------------------------|
|
| 46 |
+
| 0 | Read_Addr | Read data at specified address |
|
| 47 |
+
| 1 | Insert_At_Addr | Insert new node at specified address |
|
| 48 |
+
| 2 | Delete_Value | Delete first occurrence of specified value |
|
| 49 |
+
| 3 | Delete_At_Addr | Delete node at specified address |
|
| 50 |
+
| 5 | Insert_At_Index | Insert new node at specified index |
|
| 51 |
+
| 7 | Delete_At_Index | Delete node at specified index |
|
| 52 |
+
|
| 53 |
+
## State Machine
|
| 54 |
+
|
| 55 |
+
The module implements a state machine with the following states:
|
| 56 |
+
|
| 57 |
+
1. **IDLE (3'b000)**
|
| 58 |
+
- Waits for operation start
|
| 59 |
+
- Validates operation parameters
|
| 60 |
+
- Initializes operation sequence
|
| 61 |
+
|
| 62 |
+
2. **FIND_ADDR (3'b001)**
|
| 63 |
+
- Traverses list to find target address
|
| 64 |
+
- Used for address-based operations
|
| 65 |
+
|
| 66 |
+
3. **FIND_VALUE (3'b010)**
|
| 67 |
+
- Traverses list to find target value
|
| 68 |
+
- Used for value-based deletion
|
| 69 |
+
|
| 70 |
+
4. **INSERT_STG1 (3'b011)**
|
| 71 |
+
- First stage of node insertion
|
| 72 |
+
- Updates node links
|
| 73 |
+
|
| 74 |
+
5. **EXECUTE (3'b100)**
|
| 75 |
+
- Completes operation
|
| 76 |
+
- Updates status signals
|
| 77 |
+
|
| 78 |
+
6. **FAULT (3'b101)**
|
| 79 |
+
- Handles error conditions
|
| 80 |
+
|
| 81 |
+
## Usage Example
|
| 82 |
+
|
| 83 |
+
```verilog
|
| 84 |
+
// Instantiate a linked list with 16 nodes and 32-bit data
|
| 85 |
+
singly_linked_list #(
|
| 86 |
+
.DATA_WIDTH(32),
|
| 87 |
+
.MAX_NODE(16)
|
| 88 |
+
) list_inst (
|
| 89 |
+
.clk(system_clock),
|
| 90 |
+
.rst(reset),
|
| 91 |
+
.data_in(input_data),
|
| 92 |
+
.addr_in(target_addr),
|
| 93 |
+
.op(operation_code),
|
| 94 |
+
.op_start(start_signal),
|
| 95 |
+
.op_done(done_signal),
|
| 96 |
+
.data_out(output_data),
|
| 97 |
+
.next_node_addr(next_addr),
|
| 98 |
+
.length(list_length),
|
| 99 |
+
.head(head_addr),
|
| 100 |
+
.tail(tail_addr),
|
| 101 |
+
.full(list_full),
|
| 102 |
+
.empty(list_empty),
|
| 103 |
+
.fault(error_signal)
|
| 104 |
+
);
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
## Operation Protocol
|
| 108 |
+
|
| 109 |
+
1. Set `data_in` and/or `addr_in` as required by operation
|
| 110 |
+
2. Set `op` to desired operation code
|
| 111 |
+
3. Assert `op_start` for one clock cycle
|
| 112 |
+
4. Wait for `op_done` assertion
|
| 113 |
+
5. Check `fault` signal for operation status
|
| 114 |
+
6. Read `data_out` if applicable
|
| 115 |
+
|
| 116 |
+
## Reset Behavior
|
| 117 |
+
|
| 118 |
+
On assertion of reset:
|
| 119 |
+
- All nodes are invalidated
|
| 120 |
+
- Head and tail pointers set to NULL
|
| 121 |
+
- Length cleared to zero
|
| 122 |
+
- All node data cleared
|
| 123 |
+
- All status signals deasserted
|
| 124 |
+
|
| 125 |
+
## Implementation Notes
|
| 126 |
+
|
| 127 |
+
1. **Memory Organization**:
|
| 128 |
+
- Next node pointers stored separately
|
| 129 |
+
- Valid bits for node tracking
|
| 130 |
+
|
| 131 |
+
2. **Address Handling**:
|
| 132 |
+
- NULL address = MAX_NODE
|
| 133 |
+
- Valid addresses: 0 to (MAX_NODE-1)
|
| 134 |
+
|
| 135 |
+
3. **Error Conditions**:
|
| 136 |
+
- List full during insertion
|
| 137 |
+
- List empty during deletion/read
|
| 138 |
+
- Invalid address/index
|
| 139 |
+
- Address overflow
|
| 140 |
+
|
| 141 |
+
## Limitations
|
| 142 |
+
|
| 143 |
+
- Fixed maximum size defined by MAX_NODE
|
| 144 |
+
- Sequential search for operations
|
| 145 |
+
- Single operation at a time
|
| 146 |
+
- No concurrent access support
|
| 147 |
+
- User could actually use distributed RAM to stored data if necessary
|
Weiyet_RTLStructLib/source/Singly_Linked_List/src/rtl_file.f
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
singly_linked_list.sv
|
Weiyet_RTLStructLib/source/Singly_Linked_List/src/singly_linked_list.sv
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
// Create Date: 06/25/2024 08:46:52 PM
|
| 4 |
+
// Last Update: 02/20/2025 10:06 PM
|
| 5 |
+
// Module Name: singly_linked_list
|
| 6 |
+
// Description: Supported Operation
|
| 7 |
+
// 0. Read_Addr(addr_in) -> data_out
|
| 8 |
+
// 1. Insert_At_Addr(addr_in, data_in)
|
| 9 |
+
// 5. Insert_At_Index(addr_in, data_in)
|
| 10 |
+
// 2. Delete_Value(data_in)
|
| 11 |
+
// 3. Delete_At_Addr(addr_in)
|
| 12 |
+
// 7. Delete_At_Index(addr_in)
|
| 13 |
+
// Additional Comments: .
|
| 14 |
+
//
|
| 15 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
module singly_linked_list #(
|
| 19 |
+
parameter DATA_WIDTH = 8, // Data Width
|
| 20 |
+
parameter MAX_NODE = 8 // Maximum number of nodes stored
|
| 21 |
+
)(
|
| 22 |
+
input rst,
|
| 23 |
+
input clk,
|
| 24 |
+
input [DATA_WIDTH-1:0] data_in,
|
| 25 |
+
input [ADDR_WIDTH-1:0] addr_in, // index of element
|
| 26 |
+
input [2:0] op, // 0: Read(addr_in); 1: Delete_value(data_in); 2: Push_back(data_in); 3: Push_front(data_in)
|
| 27 |
+
input op_start,
|
| 28 |
+
output reg op_done,
|
| 29 |
+
output reg [DATA_WIDTH-1:0] data_out,
|
| 30 |
+
output reg [ADDR_WIDTH-1:0] next_node_addr, // Addr of next node
|
| 31 |
+
// status
|
| 32 |
+
output reg [ADDR_WIDTH-1:0] length,
|
| 33 |
+
output reg [ADDR_WIDTH-1:0] head, // Addr of head
|
| 34 |
+
output reg [ADDR_WIDTH-1:0] tail, // Addr of tail
|
| 35 |
+
output wire full,
|
| 36 |
+
output wire empty,
|
| 37 |
+
output reg fault
|
| 38 |
+
);
|
| 39 |
+
|
| 40 |
+
localparam ADDR_WIDTH = $clog2(MAX_NODE+1); // Reserve {ADDR_WIDTH(1'b1)} as NULL/INVALID ADDR.
|
| 41 |
+
localparam NODE_WIDTH = $clog2(MAX_NODE);
|
| 42 |
+
localparam ADDR_NULL = (MAX_NODE);
|
| 43 |
+
|
| 44 |
+
// typedef struct {
|
| 45 |
+
// reg [DATA_WIDTH-1:0] data; // RAM
|
| 46 |
+
// reg [ADDR_WIDTH-1:0] next_node_addr; // RAM
|
| 47 |
+
// reg valid; // Register
|
| 48 |
+
// } node_st;
|
| 49 |
+
|
| 50 |
+
//node_st node [0:MAX_NODE-1];
|
| 51 |
+
|
| 52 |
+
reg [DATA_WIDTH-1:0] node_data [0:MAX_NODE-1];
|
| 53 |
+
reg [ADDR_WIDTH-1:0] node_next_node_addr [0:MAX_NODE-1];
|
| 54 |
+
reg node_valid [0:MAX_NODE-1];
|
| 55 |
+
wire [MAX_NODE-1:0] valid_bits;
|
| 56 |
+
wire op_is_read;
|
| 57 |
+
wire op_is_insert_at_addr;
|
| 58 |
+
wire op_is_insert_at_index;
|
| 59 |
+
wire op_is_delete_by_addr;
|
| 60 |
+
wire op_is_delete_by_index;
|
| 61 |
+
wire op_is_delete_by_value;
|
| 62 |
+
wire addr_match;
|
| 63 |
+
wire [ADDR_WIDTH-1:0] head_idx_sel;
|
| 64 |
+
wire [ADDR_WIDTH-1:0] tail_idx_sel;
|
| 65 |
+
wire addr_overflow;
|
| 66 |
+
reg [NODE_WIDTH-1:0] index;
|
| 67 |
+
reg [ADDR_WIDTH-1:0] cur_ptr;
|
| 68 |
+
reg [ADDR_WIDTH-1:0] pre_ptr;
|
| 69 |
+
//reg [ADDR_WIDTH-1:0] head; // Addr of head
|
| 70 |
+
//reg [ADDR_WIDTH-1:0] tail; // Addr of tail
|
| 71 |
+
reg [2:0] state;
|
| 72 |
+
reg [2:0] next_state;
|
| 73 |
+
reg wr_req;
|
| 74 |
+
reg rd_req;
|
| 75 |
+
reg valid_rd_buf;
|
| 76 |
+
reg valid_wr;
|
| 77 |
+
reg [ADDR_WIDTH-1:0] next_addr_rd_buf;
|
| 78 |
+
reg [DATA_WIDTH-1:0] data_rd_buf;
|
| 79 |
+
reg [ADDR_WIDTH-1:0] target_idx;
|
| 80 |
+
reg [ADDR_WIDTH-1:0] next_node_addr_idx;
|
| 81 |
+
reg [ADDR_WIDTH-1:0] next_node_addr_in;
|
| 82 |
+
|
| 83 |
+
integer i;
|
| 84 |
+
|
| 85 |
+
localparam IDLE = 3'b000;
|
| 86 |
+
localparam FIND_ADDR = 3'b001;
|
| 87 |
+
localparam FIND_VALUE = 3'b010;
|
| 88 |
+
localparam INSERT_STG1 = 3'b011;
|
| 89 |
+
localparam FAULT = 3'b101;
|
| 90 |
+
localparam EXECUTE = 3'b100;
|
| 91 |
+
|
| 92 |
+
assign op_is_read = op[2:0] == 3'd0 & op_start;
|
| 93 |
+
assign op_is_insert_at_addr = op[2:0] == 3'd1 & op_start;
|
| 94 |
+
assign op_is_insert_at_index = op[2:0] == 3'd5 & op_start;
|
| 95 |
+
assign op_is_delete_by_value = op[2:0] == 3'd2 & op_start;
|
| 96 |
+
assign op_is_delete_by_index = op[2:0] == 3'd7 & op_start;
|
| 97 |
+
assign op_is_delete_by_addr = op[2:0] == 3'd3 & op_start;
|
| 98 |
+
|
| 99 |
+
always @ (posedge clk or posedge rst) begin
|
| 100 |
+
if (rst) begin
|
| 101 |
+
for (i = 0; i < MAX_NODE; i = i+1) begin
|
| 102 |
+
node_data[i] <= {DATA_WIDTH{1'b0}};
|
| 103 |
+
node_valid[i] <= 1'b0;
|
| 104 |
+
end
|
| 105 |
+
end else if (wr_req & state != INSERT_STG1 & target_idx != ADDR_NULL) begin
|
| 106 |
+
node_data[target_idx[NODE_WIDTH-1:0]] <= data_in;
|
| 107 |
+
node_valid[target_idx[NODE_WIDTH-1:0]] <= valid_wr;
|
| 108 |
+
end
|
| 109 |
+
end
|
| 110 |
+
|
| 111 |
+
always @ (posedge clk or posedge rst) begin
|
| 112 |
+
if (rst) begin
|
| 113 |
+
for (i = 0; i < MAX_NODE; i = i+1) begin
|
| 114 |
+
node_next_node_addr[i] <= ADDR_NULL;
|
| 115 |
+
end
|
| 116 |
+
end else if (wr_req & next_node_addr_idx != ADDR_NULL) begin
|
| 117 |
+
node_next_node_addr[next_node_addr_idx[NODE_WIDTH-1:0]] <= next_node_addr_in;
|
| 118 |
+
end
|
| 119 |
+
end
|
| 120 |
+
|
| 121 |
+
assign addr_match = op_is_insert_at_index ? addr_in == (index + 1) :
|
| 122 |
+
op_is_delete_by_index ? addr_in == index :
|
| 123 |
+
op_is_insert_at_addr ? next_addr_rd_buf == addr_in :
|
| 124 |
+
op_is_delete_by_addr ? cur_ptr == addr_in : 0;
|
| 125 |
+
assign head_idx_sel = op[2] ? 0 : head;
|
| 126 |
+
assign tail_idx_sel = op[2] ? (length-1) : tail;
|
| 127 |
+
assign addr_overflow = (op[2] & (addr_in >= length)) | (addr_in >= ADDR_NULL);
|
| 128 |
+
|
| 129 |
+
always @ (posedge clk or posedge rst) begin
|
| 130 |
+
if (rst) begin
|
| 131 |
+
state <= 3'b0;
|
| 132 |
+
end else begin
|
| 133 |
+
state <= next_state;
|
| 134 |
+
end
|
| 135 |
+
end
|
| 136 |
+
|
| 137 |
+
// op_done
|
| 138 |
+
always @ (*) begin
|
| 139 |
+
op_done <= 1'b0;
|
| 140 |
+
rd_req <= 1'b0;
|
| 141 |
+
wr_req <= 1'b0;
|
| 142 |
+
target_idx <= {ADDR_WIDTH{1'b0}};
|
| 143 |
+
next_node_addr_idx <= {ADDR_WIDTH{1'b0}};
|
| 144 |
+
next_node_addr_in <= {ADDR_WIDTH{1'b0}};
|
| 145 |
+
fault <= 1'b0;
|
| 146 |
+
next_state <= IDLE;
|
| 147 |
+
valid_wr <= 1'b0;
|
| 148 |
+
case(state)
|
| 149 |
+
IDLE: begin
|
| 150 |
+
if (op_is_insert_at_addr | op_is_insert_at_index) begin
|
| 151 |
+
if (full) begin
|
| 152 |
+
next_state <= FAULT;
|
| 153 |
+
end else if (addr_in == head_idx_sel) begin // push_front
|
| 154 |
+
// Add new node
|
| 155 |
+
// Next node addr of new node point to head
|
| 156 |
+
wr_req <= 1'b1;
|
| 157 |
+
target_idx <= find_next_ptr(valid_bits);
|
| 158 |
+
valid_wr <= 1'b1;
|
| 159 |
+
next_node_addr_idx <= find_next_ptr(valid_bits);
|
| 160 |
+
next_node_addr_in <= head;
|
| 161 |
+
next_state <= EXECUTE;
|
| 162 |
+
end else if (addr_overflow) begin // push_back
|
| 163 |
+
// Add new node
|
| 164 |
+
// Next node addr of tail point to new node
|
| 165 |
+
wr_req <= 1'b1;
|
| 166 |
+
target_idx <= find_next_ptr(valid_bits);
|
| 167 |
+
valid_wr <= 1'b1;
|
| 168 |
+
next_node_addr_idx <= tail;
|
| 169 |
+
next_node_addr_in <= find_next_ptr(valid_bits);
|
| 170 |
+
next_state <= EXECUTE;
|
| 171 |
+
end else begin
|
| 172 |
+
rd_req <= 1'b1;
|
| 173 |
+
target_idx <= head;
|
| 174 |
+
next_state <= FIND_ADDR;
|
| 175 |
+
end
|
| 176 |
+
end else if (op_is_read) begin
|
| 177 |
+
if(empty | addr_overflow) begin
|
| 178 |
+
next_state <= FAULT;
|
| 179 |
+
end else begin
|
| 180 |
+
rd_req <= 1'b1;
|
| 181 |
+
target_idx <= addr_in;
|
| 182 |
+
next_state <= EXECUTE;
|
| 183 |
+
end
|
| 184 |
+
end else if (op_is_delete_by_value) begin
|
| 185 |
+
if(empty) begin
|
| 186 |
+
next_state <= FAULT;
|
| 187 |
+
end else begin
|
| 188 |
+
rd_req <= 1'b1;
|
| 189 |
+
target_idx <= head;
|
| 190 |
+
next_state <= FIND_VALUE;
|
| 191 |
+
end
|
| 192 |
+
end else if (op_is_delete_by_addr | op_is_delete_by_index) begin
|
| 193 |
+
if(empty | addr_overflow) begin
|
| 194 |
+
next_state <= FAULT;
|
| 195 |
+
end else begin
|
| 196 |
+
rd_req <= 1'b1;
|
| 197 |
+
target_idx <= head;
|
| 198 |
+
next_state <= FIND_ADDR;
|
| 199 |
+
end
|
| 200 |
+
end else if(op_start) begin
|
| 201 |
+
next_state <= FAULT; // Invalid operation
|
| 202 |
+
end else begin
|
| 203 |
+
next_state <= IDLE;
|
| 204 |
+
end
|
| 205 |
+
end
|
| 206 |
+
FIND_ADDR: begin // to get pre pos (pre_ptr)
|
| 207 |
+
// if (!valid_rd_buf) begin
|
| 208 |
+
// next_state <= FAULT;
|
| 209 |
+
if(addr_match) begin // change to addr_match
|
| 210 |
+
if(op_is_delete_by_addr | op_is_delete_by_index) begin
|
| 211 |
+
// update curr pos to invalid
|
| 212 |
+
wr_req <= 1'b1;
|
| 213 |
+
target_idx <= cur_ptr;
|
| 214 |
+
valid_wr <= 1'b0;
|
| 215 |
+
// update next_node_addr of pre pos to next pos
|
| 216 |
+
next_node_addr_idx <= pre_ptr;
|
| 217 |
+
next_node_addr_in <= next_addr_rd_buf;
|
| 218 |
+
next_state <= EXECUTE;
|
| 219 |
+
end else if (op_is_insert_at_index | op_is_insert_at_addr) begin
|
| 220 |
+
// insert new pos
|
| 221 |
+
wr_req <= 1'b1;
|
| 222 |
+
target_idx <= find_next_ptr(valid_bits);
|
| 223 |
+
valid_wr <= 1'b1;
|
| 224 |
+
// update next_node_addr of pre pos to next pos
|
| 225 |
+
next_node_addr_idx <= cur_ptr;
|
| 226 |
+
next_node_addr_in <= find_next_ptr(valid_bits);
|
| 227 |
+
next_state <= INSERT_STG1;
|
| 228 |
+
end
|
| 229 |
+
end else if (index >= (length - 1)) begin
|
| 230 |
+
next_state <= FAULT;
|
| 231 |
+
end else begin
|
| 232 |
+
rd_req <= 1'b1;
|
| 233 |
+
target_idx <= next_addr_rd_buf;
|
| 234 |
+
next_state <= FIND_ADDR;
|
| 235 |
+
end
|
| 236 |
+
end
|
| 237 |
+
FIND_VALUE: begin
|
| 238 |
+
// if (!valid_rd_buf) begin
|
| 239 |
+
// next_state <= FAULT;
|
| 240 |
+
if(data_rd_buf == data_in) begin
|
| 241 |
+
// update curr pos to invalid
|
| 242 |
+
wr_req <= 1'b1;
|
| 243 |
+
target_idx <= cur_ptr;
|
| 244 |
+
valid_wr <= 1'b0;
|
| 245 |
+
// update next_node_addr of pre pos to next pos
|
| 246 |
+
next_node_addr_idx <= pre_ptr;
|
| 247 |
+
next_node_addr_in <= next_addr_rd_buf;
|
| 248 |
+
next_state <= EXECUTE;
|
| 249 |
+
end else if (index >= (length - 1)) begin
|
| 250 |
+
next_state <= FAULT;
|
| 251 |
+
end else begin
|
| 252 |
+
rd_req <= 1'b1;
|
| 253 |
+
target_idx <= next_addr_rd_buf;
|
| 254 |
+
next_state <= FIND_VALUE;
|
| 255 |
+
end
|
| 256 |
+
end
|
| 257 |
+
INSERT_STG1: begin
|
| 258 |
+
wr_req <= 1'b1;
|
| 259 |
+
next_node_addr_idx <= cur_ptr; //new inserted node
|
| 260 |
+
next_node_addr_in <= next_addr_rd_buf;
|
| 261 |
+
next_state <= EXECUTE;
|
| 262 |
+
end
|
| 263 |
+
EXECUTE: begin
|
| 264 |
+
op_done <= 1'b1;
|
| 265 |
+
fault <= op_is_read & !valid_rd_buf;
|
| 266 |
+
next_state <= IDLE;
|
| 267 |
+
end
|
| 268 |
+
FAULT: begin
|
| 269 |
+
fault <= 1'b1;
|
| 270 |
+
op_done <= 1'b1;
|
| 271 |
+
next_state <= IDLE;
|
| 272 |
+
end
|
| 273 |
+
default: begin
|
| 274 |
+
next_state <= IDLE;
|
| 275 |
+
end
|
| 276 |
+
endcase
|
| 277 |
+
end
|
| 278 |
+
|
| 279 |
+
always @ (posedge clk, posedge rst) begin
|
| 280 |
+
if (rst) begin
|
| 281 |
+
index <= 0;
|
| 282 |
+
end else if (state == FIND_ADDR | state == FIND_VALUE) begin
|
| 283 |
+
index <= index + 1;
|
| 284 |
+
end else begin
|
| 285 |
+
index <= 0;
|
| 286 |
+
end
|
| 287 |
+
end
|
| 288 |
+
|
| 289 |
+
always @ (posedge clk, posedge rst) begin
|
| 290 |
+
if (rst) begin
|
| 291 |
+
data_rd_buf <= {DATA_WIDTH{1'b0}};
|
| 292 |
+
valid_rd_buf <= 1'b0;
|
| 293 |
+
next_addr_rd_buf= {ADDR_WIDTH{1'b0}};
|
| 294 |
+
end else if (rd_req) begin
|
| 295 |
+
data_rd_buf <= node_data[target_idx];
|
| 296 |
+
valid_rd_buf <= node_valid[target_idx];
|
| 297 |
+
next_addr_rd_buf <= node_next_node_addr[target_idx];
|
| 298 |
+
end
|
| 299 |
+
end
|
| 300 |
+
|
| 301 |
+
always @ (posedge clk, posedge rst) begin
|
| 302 |
+
if (rst) begin
|
| 303 |
+
data_out <= {DATA_WIDTH{1'b0}};
|
| 304 |
+
next_node_addr <= {ADDR_WIDTH{1'b0}};
|
| 305 |
+
end else if (op_is_read & (next_state == EXECUTE)) begin
|
| 306 |
+
data_out <= node_data[target_idx];
|
| 307 |
+
next_node_addr <= node_next_node_addr[target_idx];
|
| 308 |
+
end
|
| 309 |
+
end
|
| 310 |
+
|
| 311 |
+
always @ (posedge clk, posedge rst) begin
|
| 312 |
+
if (rst) begin
|
| 313 |
+
cur_ptr <= ADDR_NULL;
|
| 314 |
+
pre_ptr <= ADDR_NULL;
|
| 315 |
+
end else if(next_state == IDLE) begin
|
| 316 |
+
cur_ptr <= ADDR_NULL;
|
| 317 |
+
pre_ptr <= ADDR_NULL;
|
| 318 |
+
end
|
| 319 |
+
else if (rd_req | wr_req) begin
|
| 320 |
+
cur_ptr <= target_idx;
|
| 321 |
+
pre_ptr <= cur_ptr;
|
| 322 |
+
end
|
| 323 |
+
end
|
| 324 |
+
|
| 325 |
+
genvar j;
|
| 326 |
+
// Status
|
| 327 |
+
generate
|
| 328 |
+
for (j = 0; j < MAX_NODE; j = j+1) begin
|
| 329 |
+
assign valid_bits[j] = node_valid[j];
|
| 330 |
+
end
|
| 331 |
+
endgenerate
|
| 332 |
+
|
| 333 |
+
assign full = & valid_bits;
|
| 334 |
+
assign empty = ~(| valid_bits);
|
| 335 |
+
|
| 336 |
+
always @ (posedge clk or posedge rst) begin
|
| 337 |
+
if (rst) begin
|
| 338 |
+
head <= ADDR_NULL;
|
| 339 |
+
end else if ((op_is_insert_at_index | op_is_insert_at_addr) & ((addr_in == head_idx_sel) | empty) & (next_state == EXECUTE)) begin //INVALID addr
|
| 340 |
+
head <= find_next_ptr(valid_bits);
|
| 341 |
+
end else if ((op_is_delete_by_value | op_is_delete_by_addr | op_is_delete_by_index) & (next_state == EXECUTE) & (length == 1)) begin
|
| 342 |
+
head <= ADDR_NULL;
|
| 343 |
+
end else if ((op_is_delete_by_addr | op_is_delete_by_index) & (addr_in == head_idx_sel) & (next_state == EXECUTE)) begin
|
| 344 |
+
head <= next_addr_rd_buf;
|
| 345 |
+
end else if (op_is_delete_by_value & (cur_ptr == head) & (next_state == EXECUTE)) begin
|
| 346 |
+
head <= next_addr_rd_buf;
|
| 347 |
+
end
|
| 348 |
+
end
|
| 349 |
+
|
| 350 |
+
always @ (posedge clk or posedge rst) begin
|
| 351 |
+
if (rst) begin
|
| 352 |
+
tail <= ADDR_NULL;
|
| 353 |
+
end else if ((op_is_insert_at_index | op_is_insert_at_addr) & (addr_overflow | empty) & (next_state == EXECUTE)) begin
|
| 354 |
+
tail <= find_next_ptr(valid_bits);
|
| 355 |
+
end else if ((op_is_delete_by_value | op_is_delete_by_addr | op_is_delete_by_index) & (next_state == EXECUTE) & (length == 1)) begin
|
| 356 |
+
tail <= ADDR_NULL;
|
| 357 |
+
end else if ((op_is_delete_by_addr | op_is_delete_by_index) & (addr_in == tail_idx_sel) & (next_state == EXECUTE)) begin
|
| 358 |
+
tail <= pre_ptr;
|
| 359 |
+
end else if (op_is_delete_by_value & (cur_ptr == tail) & (next_state == EXECUTE)) begin
|
| 360 |
+
tail <= pre_ptr;
|
| 361 |
+
end
|
| 362 |
+
end
|
| 363 |
+
|
| 364 |
+
always @ (posedge clk or posedge rst) begin
|
| 365 |
+
if (rst) begin
|
| 366 |
+
length <= 0;
|
| 367 |
+
end else if ((op_is_insert_at_index | op_is_insert_at_addr) & (next_state == EXECUTE)) begin
|
| 368 |
+
length <= length + 1;
|
| 369 |
+
end else if ((op_is_delete_by_addr | op_is_delete_by_index) & (next_state == EXECUTE)) begin
|
| 370 |
+
length <= length - 1;
|
| 371 |
+
end else if (op_is_delete_by_value & (next_state == EXECUTE)) begin
|
| 372 |
+
length <= length - 1;
|
| 373 |
+
end
|
| 374 |
+
end
|
| 375 |
+
|
| 376 |
+
function integer find_next_ptr(input integer valid_bits);
|
| 377 |
+
integer done;
|
| 378 |
+
done = 0;
|
| 379 |
+
find_next_ptr = 0;
|
| 380 |
+
for (i = 0; i < MAX_NODE ; i = i+1) begin
|
| 381 |
+
if(valid_bits[i] == 0 & done == 0) begin
|
| 382 |
+
find_next_ptr = i;
|
| 383 |
+
done = 1;
|
| 384 |
+
end
|
| 385 |
+
end
|
| 386 |
+
endfunction
|
| 387 |
+
|
| 388 |
+
endmodule
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/FILES_CREATED.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Singly Linked List pyUVM VIP - Files Created
|
| 2 |
+
|
| 3 |
+
## Complete File List (24 files total)
|
| 4 |
+
|
| 5 |
+
### 📂 Root Directory (6 files)
|
| 6 |
+
- ✅ `__init__.py` - Root package initialization
|
| 7 |
+
- ✅ `Makefile` - Build and run system
|
| 8 |
+
- ✅ `tb_sll.py` - Main testbench with cocotb tests
|
| 9 |
+
- ✅ `README.md` - Complete documentation
|
| 10 |
+
- ✅ `QUICKSTART.md` - Quick start guide
|
| 11 |
+
- ✅ `FILES_CREATED.md` - This file
|
| 12 |
+
|
| 13 |
+
### 📂 common/ (4 files)
|
| 14 |
+
- ✅ `__init__.py` - Package initialization
|
| 15 |
+
- ✅ `sll_vip_types.py` - Enums (SllOp)
|
| 16 |
+
- ✅ `sll_vip_config.py` - Configuration class
|
| 17 |
+
- ✅ `sll_vip_seq_item.py` - Transaction item with randomization
|
| 18 |
+
|
| 19 |
+
### 📂 agent/ (5 files)
|
| 20 |
+
- ✅ `__init__.py` - Package initialization
|
| 21 |
+
- ✅ `sll_vip_sequencer.py` - Sequencer component
|
| 22 |
+
- ✅ `sll_vip_driver.py` - Driver with async/await (all operations)
|
| 23 |
+
- ✅ `sll_vip_monitor.py` - Monitor with analysis port
|
| 24 |
+
- ✅ `sll_vip_agent.py` - Agent wrapper
|
| 25 |
+
|
| 26 |
+
### 📂 env/ (3 files)
|
| 27 |
+
- ✅ `__init__.py` - Package initialization
|
| 28 |
+
- ✅ `sll_vip_scoreboard.py` - Self-checking scoreboard with list model
|
| 29 |
+
- ✅ `sll_vip_env.py` - Environment connecting agent and scoreboard
|
| 30 |
+
|
| 31 |
+
### 📂 sequences/ (4 files)
|
| 32 |
+
- ✅ `__init__.py` - Package initialization
|
| 33 |
+
- ✅ `sll_vip_base_seq.py` - Base sequence class
|
| 34 |
+
- ✅ `sll_vip_insert_seq.py` - Insert sequence (at addr or index)
|
| 35 |
+
- ✅ `sll_vip_read_seq.py` - Read sequence
|
| 36 |
+
- ✅ `sll_vip_delete_seq.py` - Delete sequence (by value, addr, or index)
|
| 37 |
+
|
| 38 |
+
### 📂 tests/ (3 files)
|
| 39 |
+
- ✅ `__init__.py` - Package initialization
|
| 40 |
+
- ✅ `sll_vip_base_test.py` - Base test class with config
|
| 41 |
+
- ✅ `sll_vip_simple_test.py` - SimpleTest and RandomTest classes
|
| 42 |
+
|
| 43 |
+
## How to Use
|
| 44 |
+
|
| 45 |
+
### 1. Run Tests
|
| 46 |
+
```bash
|
| 47 |
+
cd Singly_Linked_List/vip/pyuvm
|
| 48 |
+
make # Run all tests
|
| 49 |
+
make WAVES=0 # Disable waveforms
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### 2. View Waveforms
|
| 53 |
+
```bash
|
| 54 |
+
gtkwave singly_linked_list.vcd
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### 3. Modify Tests
|
| 58 |
+
Edit files in `tests/` directory:
|
| 59 |
+
- `sll_vip_simple_test.py` - Add new test classes
|
| 60 |
+
- Inherit from `BaseTest` class
|
| 61 |
+
|
| 62 |
+
### 4. Add Sequences
|
| 63 |
+
Create new files in `sequences/` directory:
|
| 64 |
+
- Inherit from `SllVipBaseSeq`
|
| 65 |
+
- Implement `async def body()`
|
| 66 |
+
|
| 67 |
+
## Comparison with SystemVerilog UVM
|
| 68 |
+
|
| 69 |
+
| Item | SV UVM | pyUVM |
|
| 70 |
+
|------|--------|-------|
|
| 71 |
+
| **Total Files** | 16 | 24 (includes __init__.py) |
|
| 72 |
+
| **Config** | sll_vip_config.sv | sll_vip_config.py |
|
| 73 |
+
| **Driver** | sll_vip_driver.sv | sll_vip_driver.py |
|
| 74 |
+
| **Monitor** | sll_vip_monitor.sv | sll_vip_monitor.py |
|
| 75 |
+
| **Agent** | sll_vip_agent.sv | sll_vip_agent.py |
|
| 76 |
+
| **Env** | sll_vip_env.sv | sll_vip_env.py |
|
| 77 |
+
| **Scoreboard** | sll_vip_scoreboard.sv | sll_vip_scoreboard.py |
|
| 78 |
+
| **Sequences** | .sv files (3 sequences) | .py files (3 sequences) |
|
| 79 |
+
| **Tests** | .sv files | .py files |
|
| 80 |
+
| **Build System** | None (part of tb) | Makefile |
|
| 81 |
+
|
| 82 |
+
## Key Differences from SV UVM
|
| 83 |
+
|
| 84 |
+
1. **Python Modules**: Each directory has `__init__.py` for Python imports
|
| 85 |
+
2. **Makefile**: Standalone Makefile for building and running
|
| 86 |
+
3. **Async/Await**: Uses Python coroutines instead of tasks
|
| 87 |
+
4. **No Macros**: Clean Python syntax vs SV macros
|
| 88 |
+
5. **Cocotb Integration**: Direct DUT signal access via cocotb
|
| 89 |
+
6. **Singly Linked**: Forward pointers only (no prev pointer)
|
| 90 |
+
|
| 91 |
+
## Supported Operations
|
| 92 |
+
|
| 93 |
+
The pyUVM VIP supports all Singly Linked List operations:
|
| 94 |
+
|
| 95 |
+
| Operation | Sequence | Description |
|
| 96 |
+
|-----------|----------|-------------|
|
| 97 |
+
| READ_ADDR | SllVipReadSeq | Read data at address |
|
| 98 |
+
| INSERT_AT_ADDR | SllVipInsertSeq | Insert data at address |
|
| 99 |
+
| INSERT_AT_INDEX | SllVipInsertSeq | Insert data at index |
|
| 100 |
+
| DELETE_VALUE | SllVipDeleteSeq | Delete by value |
|
| 101 |
+
| DELETE_AT_ADDR | SllVipDeleteSeq | Delete at address |
|
| 102 |
+
| DELETE_AT_INDEX | SllVipDeleteSeq | Delete at index |
|
| 103 |
+
|
| 104 |
+
## Dependencies
|
| 105 |
+
|
| 106 |
+
### Required
|
| 107 |
+
- Python 3.7+
|
| 108 |
+
- pyuvm (pip install pyuvm)
|
| 109 |
+
- cocotb (pip install cocotb)
|
| 110 |
+
|
| 111 |
+
### Simulator
|
| 112 |
+
- Icarus Verilog (free, recommended)
|
| 113 |
+
|
| 114 |
+
## Next Steps
|
| 115 |
+
|
| 116 |
+
1. **Read QUICKSTART.md** - Get running in 5 minutes
|
| 117 |
+
2. **Read README.md** - Full documentation
|
| 118 |
+
3. **Compare with SV UVM** - See ../uvm/ directory
|
| 119 |
+
4. **Modify Tests** - Add your own test scenarios
|
| 120 |
+
|
| 121 |
+
## Architecture Match
|
| 122 |
+
|
| 123 |
+
The pyUVM VIP matches the SystemVerilog UVM VIP:
|
| 124 |
+
|
| 125 |
+
```
|
| 126 |
+
SV UVM pyUVM
|
| 127 |
+
------ -----
|
| 128 |
+
sll_vip_pkg.sv ←→ __init__.py (imports all)
|
| 129 |
+
sll_vip_config.sv ←→ sll_vip_config.py
|
| 130 |
+
sll_vip_seq_item.sv ←→ sll_vip_seq_item.py
|
| 131 |
+
sll_vip_driver.sv ←→ sll_vip_driver.py
|
| 132 |
+
sll_vip_monitor.sv ←→ sll_vip_monitor.py
|
| 133 |
+
sll_vip_sequencer.sv ←→ sll_vip_sequencer.py
|
| 134 |
+
sll_vip_agent.sv ←→ sll_vip_agent.py
|
| 135 |
+
sll_vip_env.sv ←→ sll_vip_env.py
|
| 136 |
+
sll_vip_scoreboard.sv ←→ sll_vip_scoreboard.py
|
| 137 |
+
sll_vip_*_seq.sv ←→ sll_vip_*_seq.py
|
| 138 |
+
sll_vip_*_test.sv ←→ sll_vip_*_test.py
|
| 139 |
+
tb_top.sv ←→ tb_sll.py + Makefile
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
**Happy Verifying with pyUVM! 🐍🚀**
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/Makefile
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Makefile for Singly Linked List pyUVM VIP
|
| 2 |
+
|
| 3 |
+
SIM ?= icarus
|
| 4 |
+
TOPLEVEL_LANG ?= verilog
|
| 5 |
+
WAVES ?= 1 # Set 1 to enable waveform dump
|
| 6 |
+
|
| 7 |
+
PWD=$(shell pwd)
|
| 8 |
+
|
| 9 |
+
# RTL source files
|
| 10 |
+
VERILOG_SOURCES = $(PWD)/../../src/singly_linked_list.sv
|
| 11 |
+
|
| 12 |
+
TOPLEVEL = singly_linked_list # DUT Top
|
| 13 |
+
MODULE = tb_sll # Top Python file (without .py)
|
| 14 |
+
|
| 15 |
+
# DUT parameters - passed to simulator
|
| 16 |
+
COMPILE_ARGS = -Psingly_linked_list.DATA_WIDTH=8
|
| 17 |
+
COMPILE_ARGS += -Psingly_linked_list.MAX_NODE=8
|
| 18 |
+
|
| 19 |
+
# Set RANDOM_SEED number (optional)
|
| 20 |
+
#PLUSARGS = +seed=42
|
| 21 |
+
|
| 22 |
+
# Time units
|
| 23 |
+
COCOTB_HDL_TIMEUNIT = 1ns
|
| 24 |
+
COCOTB_HDL_TIMEPRECISION = 1ps
|
| 25 |
+
|
| 26 |
+
# Icarus Verilog waveform dump
|
| 27 |
+
ifeq ($(SIM), icarus)
|
| 28 |
+
ifeq ($(WAVES), 1)
|
| 29 |
+
$(shell echo 'module iverilog_dump();' > iverilog_dump.v)
|
| 30 |
+
$(shell echo 'initial begin' >> iverilog_dump.v)
|
| 31 |
+
$(shell echo ' $$dumpfile("$(TOPLEVEL).vcd");' >> iverilog_dump.v)
|
| 32 |
+
$(shell echo ' $$dumpvars(0, $(TOPLEVEL));' >> iverilog_dump.v)
|
| 33 |
+
$(shell echo 'end' >> iverilog_dump.v)
|
| 34 |
+
$(shell echo 'endmodule' >> iverilog_dump.v)
|
| 35 |
+
VERILOG_SOURCES += $(PWD)/iverilog_dump.v
|
| 36 |
+
COMPILE_ARGS += -s iverilog_dump
|
| 37 |
+
endif
|
| 38 |
+
endif
|
| 39 |
+
|
| 40 |
+
# Include cocotb Makefile
|
| 41 |
+
include $(shell cocotb-config --makefiles)/Makefile.sim
|
| 42 |
+
|
| 43 |
+
# Additional targets
|
| 44 |
+
.PHONY: help
|
| 45 |
+
help:
|
| 46 |
+
@echo "Singly Linked List pyUVM VIP Makefile"
|
| 47 |
+
@echo "======================================"
|
| 48 |
+
@echo ""
|
| 49 |
+
@echo "Usage:"
|
| 50 |
+
@echo " make - Run all tests with Icarus Verilog"
|
| 51 |
+
@echo " make WAVES=0 - Disable waveforms"
|
| 52 |
+
@echo " make clean - Clean build files"
|
| 53 |
+
@echo ""
|
| 54 |
+
@echo "Environment Variables:"
|
| 55 |
+
@echo " WAVES - Enable waveforms (0/1)"
|
| 56 |
+
@echo " PLUSARGS - Additional simulator arguments"
|
| 57 |
+
@echo ""
|
| 58 |
+
@echo "Examples:"
|
| 59 |
+
@echo " make # Run with icarus, waves enabled"
|
| 60 |
+
@echo " make WAVES=0 # Run without waveforms (faster)"
|
| 61 |
+
@echo " make PLUSARGS=+seed=123 # Run with specific seed"
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/QUICKSTART.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Singly Linked List pyUVM VIP - Quick Start Guide
|
| 2 |
+
|
| 3 |
+
## 🚀 Getting Started
|
| 4 |
+
|
| 5 |
+
### 1. Install Dependencies
|
| 6 |
+
|
| 7 |
+
```bash
|
| 8 |
+
# Install Python packages
|
| 9 |
+
pip install pyuvm cocotb
|
| 10 |
+
|
| 11 |
+
# For Icarus Verilog (recommended for beginners)
|
| 12 |
+
# Windows: Download from http://bleyer.org/icarus/
|
| 13 |
+
# Linux: sudo apt-get install iverilog
|
| 14 |
+
# Mac: brew install icarus-verilog
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
### 2. Run Your First Test
|
| 18 |
+
|
| 19 |
+
```bash
|
| 20 |
+
cd Singly_Linked_List/vip/pyuvm
|
| 21 |
+
|
| 22 |
+
# Run with Icarus Verilog
|
| 23 |
+
make
|
| 24 |
+
|
| 25 |
+
# Run with waveforms disabled (faster)
|
| 26 |
+
make WAVES=0
|
| 27 |
+
|
| 28 |
+
# Run specific test
|
| 29 |
+
make TESTCASE=sll_simple_test
|
| 30 |
+
|
| 31 |
+
# Clean build files
|
| 32 |
+
make clean
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
### 3. View Waveforms
|
| 36 |
+
|
| 37 |
+
After running with `WAVES=1` (default):
|
| 38 |
+
```bash
|
| 39 |
+
# View with GTKWave
|
| 40 |
+
gtkwave singly_linked_list.vcd
|
| 41 |
+
|
| 42 |
+
# Or on Windows
|
| 43 |
+
gtkwave.exe singly_linked_list.vcd
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
## 📂 File Structure Quick Reference
|
| 47 |
+
|
| 48 |
+
```
|
| 49 |
+
pyuvm/
|
| 50 |
+
├── Makefile ← Run "make" here
|
| 51 |
+
├── tb_sll.py ← Main testbench
|
| 52 |
+
├── common/ ← Config & transaction types
|
| 53 |
+
├── agent/ ← Driver, monitor, sequencer
|
| 54 |
+
├── env/ ← Environment & scoreboard
|
| 55 |
+
├── sequences/ ← Test sequences
|
| 56 |
+
└── tests/ ← Test classes
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
## 🧪 Available Tests
|
| 60 |
+
|
| 61 |
+
The testbench includes 2 pre-built tests:
|
| 62 |
+
|
| 63 |
+
1. **sll_simple_test** - Insert, read, delete operations
|
| 64 |
+
2. **sll_random_test** - Random mixed operations with different delete modes
|
| 65 |
+
|
| 66 |
+
## 🔧 Customize DUT Parameters
|
| 67 |
+
|
| 68 |
+
Edit the `Makefile` to change Singly Linked List parameters:
|
| 69 |
+
|
| 70 |
+
```makefile
|
| 71 |
+
COMPILE_ARGS = -Psingly_linked_list.DATA_WIDTH=8 # Change data width
|
| 72 |
+
COMPILE_ARGS += -Psingly_linked_list.MAX_NODE=8 # Change max nodes
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
Then run:
|
| 76 |
+
```bash
|
| 77 |
+
make clean # Must clean first!
|
| 78 |
+
make
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
## 🐛 Debugging
|
| 82 |
+
|
| 83 |
+
### Enable Verbose Logging
|
| 84 |
+
|
| 85 |
+
In `tb_sll.py`, add:
|
| 86 |
+
```python
|
| 87 |
+
cocotb.log.setLevel(cocotb.logging.DEBUG)
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### Python Debugger
|
| 91 |
+
|
| 92 |
+
Add breakpoint in Python code:
|
| 93 |
+
```python
|
| 94 |
+
import pdb; pdb.set_trace()
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
### Check Simulator Output
|
| 98 |
+
|
| 99 |
+
```bash
|
| 100 |
+
# Output is in terminal and sim_build/
|
| 101 |
+
cat sim_build/sim.log
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
## 📊 Expected Output
|
| 105 |
+
|
| 106 |
+
```
|
| 107 |
+
============================================================
|
| 108 |
+
Starting Singly Linked List Simple Test (pyUVM)
|
| 109 |
+
============================================================
|
| 110 |
+
DUT Parameters: DATA_WIDTH=8, MAX_NODE=8
|
| 111 |
+
DUT initialization complete
|
| 112 |
+
Test: Inserting 5 nodes
|
| 113 |
+
Test: Reading 5 nodes
|
| 114 |
+
Test: Deleting 2 nodes
|
| 115 |
+
Test: Reading after delete
|
| 116 |
+
============================================================
|
| 117 |
+
Singly Linked List Simple Test Complete
|
| 118 |
+
============================================================
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
## 🆚 Comparison with SystemVerilog UVM
|
| 122 |
+
|
| 123 |
+
| Task | SystemVerilog UVM | pyUVM (This VIP) |
|
| 124 |
+
|------|-------------------|------------------|
|
| 125 |
+
| **Run test** | `vsim +UVM_TESTNAME=simple_test` | `make` |
|
| 126 |
+
| **Change params** | Edit tb_top.sv | Edit Makefile |
|
| 127 |
+
| **View waves** | Open in simulator | `gtkwave singly_linked_list.vcd` |
|
| 128 |
+
| **Debug** | $display + waveforms | print() + pdb |
|
| 129 |
+
| **Add test** | Create .sv file | Create .py function |
|
| 130 |
+
|
| 131 |
+
## ❓ Troubleshooting
|
| 132 |
+
|
| 133 |
+
### Issue: "No module named 'pyuvm'"
|
| 134 |
+
```bash
|
| 135 |
+
pip install pyuvm
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
### Issue: "No module named 'cocotb'"
|
| 139 |
+
```bash
|
| 140 |
+
pip install cocotb
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### Issue: "make: cocotb-config: Command not found"
|
| 144 |
+
```bash
|
| 145 |
+
# Check installation
|
| 146 |
+
which cocotb-config
|
| 147 |
+
python -m cocotb --help
|
| 148 |
+
|
| 149 |
+
# Reinstall if needed
|
| 150 |
+
pip uninstall cocotb
|
| 151 |
+
pip install cocotb
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
### Issue: Waveform file not generated
|
| 155 |
+
```bash
|
| 156 |
+
# Make sure WAVES=1 (default)
|
| 157 |
+
make clean
|
| 158 |
+
make WAVES=1
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
### Issue: Simulator not found
|
| 162 |
+
```bash
|
| 163 |
+
# Install Icarus Verilog
|
| 164 |
+
# Windows: http://bleyer.org/icarus/
|
| 165 |
+
# Linux: sudo apt-get install iverilog
|
| 166 |
+
# Mac: brew install icarus-verilog
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
## 📚 Next Steps
|
| 170 |
+
|
| 171 |
+
1. **Read the full README.md** - Comprehensive documentation
|
| 172 |
+
2. **Study the code** - Start with `tb_sll.py`, then explore agent/
|
| 173 |
+
3. **Modify tests** - Edit `tests/sll_vip_simple_test.py`
|
| 174 |
+
4. **Add sequences** - Create new sequences in `sequences/`
|
| 175 |
+
5. **Compare with SV UVM** - See `../uvm/` for SystemVerilog version
|
| 176 |
+
|
| 177 |
+
## 🔗 Useful Links
|
| 178 |
+
|
| 179 |
+
- **pyUVM Docs**: https://pyuvm.github.io/pyuvm/
|
| 180 |
+
- **Cocotb Docs**: https://docs.cocotb.org/
|
| 181 |
+
|
| 182 |
+
## 💡 Tips
|
| 183 |
+
|
| 184 |
+
1. **Start Simple**: Run the default tests first before modifying
|
| 185 |
+
2. **Use Waveforms**: Always check waveforms when debugging
|
| 186 |
+
3. **Print Debugging**: Python's print() is your friend
|
| 187 |
+
4. **Incremental**: Make small changes and test frequently
|
| 188 |
+
5. **Clean Often**: Run `make clean` when changing DUT parameters
|
| 189 |
+
|
| 190 |
+
**Happy Testing! 🎉**
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/README.md
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Singly Linked List pyUVM VIP (Verification IP) User Guide
|
| 2 |
+
|
| 3 |
+
## 📁 Directory Structure
|
| 4 |
+
|
| 5 |
+
```
|
| 6 |
+
pyuvm/
|
| 7 |
+
├── README.md [User guide and documentation]
|
| 8 |
+
├── common/ [Core VIP source files]
|
| 9 |
+
│ ├── __init__.py
|
| 10 |
+
│ ├── sll_vip_types.py [Enums and types]
|
| 11 |
+
│ ├── sll_vip_config.py [Configuration class]
|
| 12 |
+
│ └── sll_vip_seq_item.py [Transaction definitions]
|
| 13 |
+
├── agent/ [Agent layer components]
|
| 14 |
+
│ ├── __init__.py
|
| 15 |
+
│ ├── sll_vip_driver.py [Driver implementation]
|
| 16 |
+
│ ├── sll_vip_monitor.py [Monitor implementation]
|
| 17 |
+
│ ├── sll_vip_sequencer.py [Sequencer]
|
| 18 |
+
│ └── sll_vip_agent.py [Agent wrapper]
|
| 19 |
+
├── env/ [Environment layer]
|
| 20 |
+
│ ├── __init__.py
|
| 21 |
+
│ ├── sll_vip_env.py [Environment]
|
| 22 |
+
│ └── sll_vip_scoreboard.py [Checking components]
|
| 23 |
+
├── sequences/ [Test sequences]
|
| 24 |
+
│ ├── __init__.py
|
| 25 |
+
│ ├── sll_vip_base_seq.py [Base sequence]
|
| 26 |
+
│ ├── sll_vip_insert_seq.py [Insert sequences]
|
| 27 |
+
│ ├── sll_vip_read_seq.py [Read sequences]
|
| 28 |
+
│ └── sll_vip_delete_seq.py [Delete sequences]
|
| 29 |
+
├── tests/ [Test classes]
|
| 30 |
+
│ ├── __init__.py
|
| 31 |
+
│ ├── sll_vip_base_test.py [Base test]
|
| 32 |
+
│ └── sll_vip_simple_test.py [Simple & random tests]
|
| 33 |
+
└── tb_sll.py [Testbench top with cocotb]
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## 🚀 Quick Start
|
| 37 |
+
|
| 38 |
+
**Step 1:** Install dependencies
|
| 39 |
+
```bash
|
| 40 |
+
pip install pyuvm cocotb
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
**Step 2:** Update configuration in your test:
|
| 44 |
+
```python
|
| 45 |
+
from common.sll_vip_config import SllVipConfig
|
| 46 |
+
|
| 47 |
+
# Create config
|
| 48 |
+
cfg = SllVipConfig("cfg")
|
| 49 |
+
cfg.DATA_WIDTH = 8
|
| 50 |
+
cfg.MAX_NODE = 8
|
| 51 |
+
ConfigDB().set(None, "*", "sll_vip_cfg", cfg)
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
**Step 3:** Create and run test:
|
| 55 |
+
```python
|
| 56 |
+
from tests.sll_vip_simple_test import SimpleTest
|
| 57 |
+
|
| 58 |
+
# In your cocotb test
|
| 59 |
+
await uvm_root().run_test("SimpleTest")
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## 🧪 Running Tests
|
| 63 |
+
|
| 64 |
+
### Quick Start
|
| 65 |
+
|
| 66 |
+
```bash
|
| 67 |
+
cd Singly_Linked_List/vip/pyuvm
|
| 68 |
+
|
| 69 |
+
# Run all tests with Icarus Verilog
|
| 70 |
+
make
|
| 71 |
+
|
| 72 |
+
# Run without waveforms (faster)
|
| 73 |
+
make WAVES=0
|
| 74 |
+
|
| 75 |
+
# Run specific test
|
| 76 |
+
make TESTCASE=sll_simple_test
|
| 77 |
+
|
| 78 |
+
# View waveforms
|
| 79 |
+
gtkwave singly_linked_list.vcd
|
| 80 |
+
|
| 81 |
+
# Clean build files
|
| 82 |
+
make clean
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### Available Make Targets
|
| 86 |
+
|
| 87 |
+
| Command | Description |
|
| 88 |
+
|---------|-------------|
|
| 89 |
+
| `make` | Run all tests with Icarus Verilog, waves enabled |
|
| 90 |
+
| `make WAVES=0` | Disable waveform generation |
|
| 91 |
+
| `make TESTCASE=<name>` | Run specific test only |
|
| 92 |
+
| `make clean` | Clean build files |
|
| 93 |
+
| `make help` | Show help message |
|
| 94 |
+
|
| 95 |
+
### DUT Parameters
|
| 96 |
+
|
| 97 |
+
Parameters are configured in `Makefile`:
|
| 98 |
+
```makefile
|
| 99 |
+
COMPILE_ARGS = -Psingly_linked_list.DATA_WIDTH=8
|
| 100 |
+
COMPILE_ARGS += -Psingly_linked_list.MAX_NODE=8
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
**Note:** Run `make clean` before changing parameters!
|
| 104 |
+
|
| 105 |
+
## 📋 Singly Linked List Operations
|
| 106 |
+
|
| 107 |
+
The VIP supports 7 operation types:
|
| 108 |
+
|
| 109 |
+
| Operation | Description | Op Code |
|
| 110 |
+
|-----------|-------------|---------|
|
| 111 |
+
| **READ_ADDR** | Read data at address | 0 |
|
| 112 |
+
| **INSERT_AT_ADDR** | Insert data at address | 1 |
|
| 113 |
+
| **DELETE_VALUE** | Delete by value | 2 |
|
| 114 |
+
| **DELETE_AT_ADDR** | Delete at address | 3 |
|
| 115 |
+
| **IDLE** | No operation | 4 |
|
| 116 |
+
| **INSERT_AT_INDEX** | Insert at index | 5 |
|
| 117 |
+
| **DELETE_AT_INDEX** | Delete at index | 7 |
|
| 118 |
+
|
| 119 |
+
## 🚀 Available Sequences
|
| 120 |
+
|
| 121 |
+
**Insert Sequence:**
|
| 122 |
+
```python
|
| 123 |
+
from sequences.sll_vip_insert_seq import SllVipInsertSeq
|
| 124 |
+
|
| 125 |
+
insert_seq = SllVipInsertSeq("insert_seq")
|
| 126 |
+
insert_seq.num_inserts = 5
|
| 127 |
+
insert_seq.use_index = False # Use INSERT_AT_ADDR
|
| 128 |
+
await insert_seq.start(env.get_sequencer())
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
**Read Sequence:**
|
| 132 |
+
```python
|
| 133 |
+
from sequences.sll_vip_read_seq import SllVipReadSeq
|
| 134 |
+
|
| 135 |
+
read_seq = SllVipReadSeq("read_seq")
|
| 136 |
+
read_seq.num_reads = 5
|
| 137 |
+
await read_seq.start(env.get_sequencer())
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
**Delete Sequence:**
|
| 141 |
+
```python
|
| 142 |
+
from sequences.sll_vip_delete_seq import SllVipDeleteSeq
|
| 143 |
+
|
| 144 |
+
delete_seq = SllVipDeleteSeq("delete_seq")
|
| 145 |
+
delete_seq.num_deletes = 3
|
| 146 |
+
delete_seq.delete_mode = "addr" # "addr", "index", or "value"
|
| 147 |
+
await delete_seq.start(env.get_sequencer())
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
## ✅ Self-Checking Features
|
| 151 |
+
|
| 152 |
+
The scoreboard automatically verifies:
|
| 153 |
+
- ✅ Data integrity (read returns correct values)
|
| 154 |
+
- ✅ Insert operations (at address or index)
|
| 155 |
+
- ✅ Delete operations (by value, address, or index)
|
| 156 |
+
- ✅ List length tracking
|
| 157 |
+
- ✅ Head and tail pointer management
|
| 158 |
+
- ✅ Next pointer correctness (singly linked)
|
| 159 |
+
- ✅ Error conditions (invalid address, empty list, full list)
|
| 160 |
+
|
| 161 |
+
## 🔧 Key Features
|
| 162 |
+
|
| 163 |
+
1. **Singly Linked List**: Forward pointers only (no prev pointer)
|
| 164 |
+
2. **Multiple Insert/Delete Modes**: By address, index, or value
|
| 165 |
+
3. **Python-Based**: Easy to extend and modify
|
| 166 |
+
4. **Cocotb Integration**: Works with cocotb simulator interface
|
| 167 |
+
5. **Self-Checking**: Automatic scoreboard verification
|
| 168 |
+
6. **Configurable**: Supports different parameters
|
| 169 |
+
7. **Reference Model**: Python lists track expected behavior
|
| 170 |
+
|
| 171 |
+
## 📊 Comparison: SystemVerilog UVM vs pyUVM
|
| 172 |
+
|
| 173 |
+
| Aspect | SV UVM | pyUVM |
|
| 174 |
+
|--------|--------|-------|
|
| 175 |
+
| **Language** | SystemVerilog | Python |
|
| 176 |
+
| **Syntax** | Complex macros | Clean Python |
|
| 177 |
+
| **Debug** | Waveforms + logs | Print statements + logs |
|
| 178 |
+
| **Extensibility** | Limited | High (Python ecosystem) |
|
| 179 |
+
| **Learning Curve** | Steep | Moderate |
|
| 180 |
+
| **Performance** | Faster | Slightly slower |
|
| 181 |
+
|
| 182 |
+
## 🚨 Common Issues & Solutions
|
| 183 |
+
|
| 184 |
+
### Issue: Import errors
|
| 185 |
+
**Solution:** Make sure all `__init__.py` files are present and pyUVM is installed:
|
| 186 |
+
```bash
|
| 187 |
+
pip install pyuvm
|
| 188 |
+
```
|
| 189 |
+
|
| 190 |
+
### Issue: DUT signals not found
|
| 191 |
+
**Solution:** Check that signal names match your RTL:
|
| 192 |
+
```python
|
| 193 |
+
# In driver.py
|
| 194 |
+
self.dut.op_start.value = 1 # Make sure 'op_start' matches RTL port name
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
### Issue: Cocotb not finding testbench
|
| 198 |
+
**Solution:** Ensure MODULE variable in Makefile points to correct Python file:
|
| 199 |
+
```makefile
|
| 200 |
+
MODULE = tb_sll # Without .py extension
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
## 📚 Example Test
|
| 204 |
+
|
| 205 |
+
```python
|
| 206 |
+
import cocotb
|
| 207 |
+
from cocotb.clock import Clock
|
| 208 |
+
from cocotb.triggers import Timer
|
| 209 |
+
from pyuvm import *
|
| 210 |
+
from tests.sll_vip_simple_test import SimpleTest
|
| 211 |
+
|
| 212 |
+
@cocotb.test()
|
| 213 |
+
async def my_sll_test(dut):
|
| 214 |
+
# Start clock
|
| 215 |
+
cocotb.start_soon(Clock(dut.clk, 10, units="ns").start())
|
| 216 |
+
|
| 217 |
+
# Reset
|
| 218 |
+
dut.rst.value = 1
|
| 219 |
+
await Timer(50, units="ns")
|
| 220 |
+
dut.rst.value = 0
|
| 221 |
+
await Timer(10, units="ns")
|
| 222 |
+
|
| 223 |
+
# Run UVM test
|
| 224 |
+
await uvm_root().run_test("SimpleTest")
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
## 🎯 Key Characteristics
|
| 228 |
+
|
| 229 |
+
- **Pythonic UVM**: UVM methodology in Python
|
| 230 |
+
- **Async/Await**: Uses Python coroutines for concurrency
|
| 231 |
+
- **Cocotb-Based**: Leverages cocotb for RTL interaction
|
| 232 |
+
- **Modular**: Easy to reuse components
|
| 233 |
+
- **Extensible**: Add new sequences and tests easily
|
| 234 |
+
|
| 235 |
+
## 📈 Architecture
|
| 236 |
+
|
| 237 |
+
```
|
| 238 |
+
Test (SimpleTest)
|
| 239 |
+
└── Environment (SllVipEnv)
|
| 240 |
+
├── Agent (SllVipAgent)
|
| 241 |
+
│ ├── Driver (SllVipDriver)
|
| 242 |
+
│ ├── Monitor (SllVipMonitor)
|
| 243 |
+
│ └── Sequencer (SllVipSequencer)
|
| 244 |
+
└── Scoreboard (SllVipScoreboard)
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
## 🔍 Python vs SystemVerilog UVM Mapping
|
| 248 |
+
|
| 249 |
+
| SystemVerilog | Python/pyUVM |
|
| 250 |
+
|---------------|--------------|
|
| 251 |
+
| `uvm_config_db::set()` | `ConfigDB().set()` |
|
| 252 |
+
| `uvm_config_db::get()` | `ConfigDB().get()` |
|
| 253 |
+
| `` `uvm_component_utils()`` | Inherit from `uvm_component` |
|
| 254 |
+
| `task run_phase()` | `async def run_phase()` |
|
| 255 |
+
| `@(posedge clk)` | `await RisingEdge(dut.clk)` |
|
| 256 |
+
| `#100ns` | `await Timer(100, units="ns")` |
|
| 257 |
+
| `start_item()` | `await self.start_item()` |
|
| 258 |
+
|
| 259 |
+
## 💡 Advantages of pyUVM
|
| 260 |
+
|
| 261 |
+
1. **Easier Debugging**: Python print() and pdb debugger
|
| 262 |
+
2. **Rich Ecosystem**: Use NumPy, Pandas for analysis
|
| 263 |
+
3. **Rapid Development**: Faster iteration cycle
|
| 264 |
+
4. **Better Readability**: Clean Python syntax
|
| 265 |
+
5. **Cross-Platform**: Works on Linux, Windows, Mac
|
| 266 |
+
|
| 267 |
+
## 🚧 Limitations
|
| 268 |
+
|
| 269 |
+
1. **Performance**: Slightly slower than SystemVerilog
|
| 270 |
+
2. **Tool Support**: Fewer commercial tools support Python UVM
|
| 271 |
+
3. **Community**: Smaller than SystemVerilog UVM community
|
| 272 |
+
4. **Coverage**: Functional coverage less mature
|
| 273 |
+
|
| 274 |
+
## 🔗 Resources
|
| 275 |
+
|
| 276 |
+
- pyUVM Documentation: https://pyuvm.github.io/pyuvm/
|
| 277 |
+
- Cocotb Documentation: https://docs.cocotb.org/
|
| 278 |
+
- SystemVerilog UVM Reference: https://www.accellera.org/downloads/standards/uvm
|
| 279 |
+
|
| 280 |
+
**Happy Verifying with Python! 🐍🚀**
|
| 281 |
+
|
| 282 |
+
*This pyUVM VIP demonstrates modern verification using Python and UVM methodology.*
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Singly Linked List VIP - pyUVM Implementation
|
| 3 |
+
Create Date: 01/05/2026
|
| 4 |
+
|
| 5 |
+
A complete pyUVM-based Verification IP for Singly Linked List verification.
|
| 6 |
+
Follows UVM methodology with Python and cocotb.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
__version__ = "1.0.0"
|
| 10 |
+
__author__ = "RTLStructLib"
|
| 11 |
+
|
| 12 |
+
# Import all VIP components
|
| 13 |
+
from .common import *
|
| 14 |
+
from .agent import *
|
| 15 |
+
from .env import *
|
| 16 |
+
from .sequences import *
|
| 17 |
+
from .tests import *
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/pyuvm/tb_sll.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Singly Linked List VIP Testbench
|
| 3 |
+
Create Date: 01/05/2026
|
| 4 |
+
|
| 5 |
+
Main testbench file for Singly Linked List pyUVM VIP
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import cocotb
|
| 9 |
+
from cocotb.clock import Clock
|
| 10 |
+
from cocotb.triggers import Timer, RisingEdge
|
| 11 |
+
from pyuvm import *
|
| 12 |
+
|
| 13 |
+
# Import test classes
|
| 14 |
+
from tests.sll_vip_simple_test import SimpleTest, RandomTest
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
async def dut_init(dut):
|
| 18 |
+
"""Initialize DUT"""
|
| 19 |
+
# Get DUT parameters
|
| 20 |
+
data_width = dut.DATA_WIDTH.value
|
| 21 |
+
max_node = dut.MAX_NODE.value
|
| 22 |
+
|
| 23 |
+
cocotb.log.info("=" * 60)
|
| 24 |
+
cocotb.log.info("Singly Linked List VIP Testbench Initialization")
|
| 25 |
+
cocotb.log.info("=" * 60)
|
| 26 |
+
cocotb.log.info(f"DUT Parameters: DATA_WIDTH={data_width}, MAX_NODE={max_node}")
|
| 27 |
+
|
| 28 |
+
# Start clock
|
| 29 |
+
cocotb.start_soon(Clock(dut.clk, 10, units="ns").start())
|
| 30 |
+
|
| 31 |
+
# Reset
|
| 32 |
+
dut.rst.value = 1
|
| 33 |
+
dut.op.value = 0
|
| 34 |
+
dut.op_start.value = 0
|
| 35 |
+
dut.data_in.value = 0
|
| 36 |
+
dut.addr_in.value = 0
|
| 37 |
+
|
| 38 |
+
await Timer(100, units="ns")
|
| 39 |
+
dut.rst.value = 0
|
| 40 |
+
await Timer(10, units="ns")
|
| 41 |
+
|
| 42 |
+
cocotb.log.info("DUT initialization complete")
|
| 43 |
+
|
| 44 |
+
# Store DUT in ConfigDB for VIP components
|
| 45 |
+
ConfigDB().set(None, "*", "sll_vip_dut", dut)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@cocotb.test()
|
| 49 |
+
async def sll_simple_test(dut):
|
| 50 |
+
"""Simple test for Singly Linked List"""
|
| 51 |
+
await dut_init(dut)
|
| 52 |
+
|
| 53 |
+
cocotb.log.info("=" * 60)
|
| 54 |
+
cocotb.log.info("Starting Singly Linked List Simple Test (pyUVM)")
|
| 55 |
+
cocotb.log.info("=" * 60)
|
| 56 |
+
|
| 57 |
+
# Run UVM test
|
| 58 |
+
await uvm_root().run_test("SimpleTest")
|
| 59 |
+
|
| 60 |
+
cocotb.log.info("=" * 60)
|
| 61 |
+
cocotb.log.info("Singly Linked List Simple Test Complete")
|
| 62 |
+
cocotb.log.info("=" * 60)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@cocotb.test()
|
| 66 |
+
async def sll_random_test(dut):
|
| 67 |
+
"""Random test for Singly Linked List"""
|
| 68 |
+
await dut_init(dut)
|
| 69 |
+
|
| 70 |
+
cocotb.log.info("=" * 60)
|
| 71 |
+
cocotb.log.info("Starting Singly Linked List Random Test (pyUVM)")
|
| 72 |
+
cocotb.log.info("=" * 60)
|
| 73 |
+
|
| 74 |
+
# Run UVM test
|
| 75 |
+
await uvm_root().run_test("RandomTest")
|
| 76 |
+
|
| 77 |
+
cocotb.log.info("=" * 60)
|
| 78 |
+
cocotb.log.info("Singly Linked List Random Test Complete")
|
| 79 |
+
cocotb.log.info("=" * 60)
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/uvm/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Singly Linked List UVM VIP (Verification IP) User Guide
|
| 2 |
+
|
| 3 |
+
## 📁 Directory Structure
|
| 4 |
+
|
| 5 |
+
```
|
| 6 |
+
uvm/
|
| 7 |
+
├── README.md [User guide and documentation]
|
| 8 |
+
├── common/ [Core VIP source files]
|
| 9 |
+
│ ├── sll_vip_pkg.sv [Main package + enums]
|
| 10 |
+
│ ├── sll_vip_config.sv [Configuration class]
|
| 11 |
+
│ └── sll_vip_seq_item.sv [Transaction definitions]
|
| 12 |
+
├── interface/ [Interface definition]
|
| 13 |
+
│ └── sll_vip_if.sv [Virtual interface]
|
| 14 |
+
├── agent/ [Agent layer components]
|
| 15 |
+
│ ├── sll_vip_driver.sv [Driver implementation]
|
| 16 |
+
│ ├── sll_vip_monitor.sv [Monitor implementation]
|
| 17 |
+
│ ├── sll_vip_sequencer.sv [Sequencer]
|
| 18 |
+
│ └── sll_vip_agent.sv [Agent wrapper]
|
| 19 |
+
├── env/ [Environment layer]
|
| 20 |
+
│ ├── sll_vip_env.sv [Environment]
|
| 21 |
+
│ └── sll_vip_scoreboard.sv [Checking with singly linked list model]
|
| 22 |
+
├── sequences/ [Test sequences]
|
| 23 |
+
│ ├── sll_vip_base_seq.sv [Base sequence]
|
| 24 |
+
│ ├── sll_vip_insert_seq.sv [Insert sequences (addr & index)]
|
| 25 |
+
│ ├── sll_vip_read_seq.sv [Read sequence]
|
| 26 |
+
│ └── sll_vip_delete_seq.sv [Delete sequences (addr, index, value)]
|
| 27 |
+
└── sim/ [Testbench]
|
| 28 |
+
├── tb_top.sv [Testbench top module]
|
| 29 |
+
└── tests/
|
| 30 |
+
├── sll_vip_base_test.sv [Base test class]
|
| 31 |
+
└── sll_vip_simple_test.sv [Simple & random tests]
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
## 🚀 Quick Start
|
| 35 |
+
|
| 36 |
+
**Step 1:** Update interface signals in your testbench:
|
| 37 |
+
```systemverilog
|
| 38 |
+
sll_vip_if dut_if(clk);
|
| 39 |
+
// Connect to DUT
|
| 40 |
+
uvm_config_db#(virtual sll_vip_if)::set(null, "*", "sll_vip_vif", dut_if);
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
**Step 2:** Configure DUT parameters in your test:
|
| 44 |
+
```systemverilog
|
| 45 |
+
cfg = sll_vip_config::type_id::create("cfg");
|
| 46 |
+
cfg.DATA_WIDTH = 8;
|
| 47 |
+
cfg.MAX_NODE = 15;
|
| 48 |
+
uvm_config_db#(sll_vip_config)::set(this, "*", "sll_vip_cfg", cfg);
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
**Step 3:** Create environment:
|
| 52 |
+
```systemverilog
|
| 53 |
+
sll_env = sll_vip_env::type_id::create("sll_env", this);
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
## 🚀 Available Sequences
|
| 57 |
+
|
| 58 |
+
**Insert at Address:**
|
| 59 |
+
```systemverilog
|
| 60 |
+
sll_vip_insert_at_addr_seq insert_seq = sll_vip_insert_at_addr_seq::type_id::create("insert_seq");
|
| 61 |
+
insert_seq.num_inserts = 5;
|
| 62 |
+
insert_seq.start(env.get_sequencer());
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
**Insert at Index:**
|
| 66 |
+
```systemverilog
|
| 67 |
+
sll_vip_insert_at_index_seq insert_seq = sll_vip_insert_at_index_seq::type_id::create("insert_seq");
|
| 68 |
+
insert_seq.num_inserts = 5;
|
| 69 |
+
insert_seq.start(env.get_sequencer());
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
**Read from Address:**
|
| 73 |
+
```systemverilog
|
| 74 |
+
sll_vip_read_seq read_seq = sll_vip_read_seq::type_id::create("read_seq");
|
| 75 |
+
read_seq.num_reads = 5;
|
| 76 |
+
read_seq.start(env.get_sequencer());
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
**Delete at Address:**
|
| 80 |
+
```systemverilog
|
| 81 |
+
sll_vip_delete_at_addr_seq delete_seq = sll_vip_delete_at_addr_seq::type_id::create("delete_seq");
|
| 82 |
+
delete_seq.num_deletes = 3;
|
| 83 |
+
delete_seq.start(env.get_sequencer());
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
**Delete at Index:**
|
| 87 |
+
```systemverilog
|
| 88 |
+
sll_vip_delete_at_index_seq delete_seq = sll_vip_delete_at_index_seq::type_id::create("delete_seq");
|
| 89 |
+
delete_seq.num_deletes = 3;
|
| 90 |
+
delete_seq.start(env.get_sequencer());
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
**Delete by Value:**
|
| 94 |
+
```systemverilog
|
| 95 |
+
sll_vip_delete_value_seq delete_seq = sll_vip_delete_value_seq::type_id::create("delete_seq");
|
| 96 |
+
delete_seq.num_deletes = 2;
|
| 97 |
+
delete_seq.start(env.get_sequencer());
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## 🧪 Available Tests
|
| 101 |
+
|
| 102 |
+
### 1. Simple Test
|
| 103 |
+
Basic insert, read, and delete operations:
|
| 104 |
+
```bash
|
| 105 |
+
vsim -c +UVM_TESTNAME=simple_test -do "run -all; quit"
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### 2. Random Test
|
| 109 |
+
Random sequence of insert/read/delete operations:
|
| 110 |
+
```bash
|
| 111 |
+
vsim -c +UVM_TESTNAME=random_test -do "run -all; quit"
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
## 📋 Singly Linked List Operations
|
| 115 |
+
|
| 116 |
+
The VIP supports 6 operations matching the DUT (singly_linked_list.sv):
|
| 117 |
+
|
| 118 |
+
| Operation | op | Description | Inputs | Outputs |
|
| 119 |
+
|-----------|-------|-------------|--------|---------|
|
| 120 |
+
| **READ_ADDR** | 3'b000 | Read data at address | addr_in | data_out, next_node_addr |
|
| 121 |
+
| **INSERT_AT_ADDR** | 3'b001 | Insert after address | addr_in, data_in | head, tail, length |
|
| 122 |
+
| **DELETE_VALUE** | 3'b010 | Delete first occurrence of value | data_in | head, tail, length |
|
| 123 |
+
| **DELETE_AT_ADDR** | 3'b011 | Delete node at address | addr_in | head, tail, length |
|
| 124 |
+
| **INSERT_AT_INDEX** | 3'b101 | Insert at index position | addr_in (index), data_in | head, tail, length |
|
| 125 |
+
| **DELETE_AT_INDEX** | 3'b111 | Delete at index position | addr_in (index) | head, tail, length |
|
| 126 |
+
|
| 127 |
+
## ✅ Self-Checking Features
|
| 128 |
+
|
| 129 |
+
The scoreboard automatically verifies:
|
| 130 |
+
- ✅ Data integrity for READ operations
|
| 131 |
+
- ✅ Correct list length after INSERT/DELETE
|
| 132 |
+
- ✅ INSERT when full behavior
|
| 133 |
+
- ✅ READ/DELETE when empty behavior
|
| 134 |
+
- ✅ Address-based operations vs index-based operations
|
| 135 |
+
- ✅ DELETE_VALUE finds correct first occurrence
|
| 136 |
+
- ✅ Fault flag correctness
|
| 137 |
+
- ✅ Head/tail pointer management
|
| 138 |
+
- ✅ Next node pointer correctness
|
| 139 |
+
|
| 140 |
+
## 📊 Test Coverage Comparison
|
| 141 |
+
|
| 142 |
+
| tb/sv/tb.sv | UVM VIP Equivalent | Description |
|
| 143 |
+
|-------------|-------------------|-------------|
|
| 144 |
+
| Insert operations | `sll_vip_insert_at_addr_seq` | Insert at address |
|
| 145 |
+
| Insert operations | `sll_vip_insert_at_index_seq` | Insert at index |
|
| 146 |
+
| Read operations | `sll_vip_read_seq` | Read from address |
|
| 147 |
+
| Delete operations | `sll_vip_delete_at_addr_seq` | Delete at address |
|
| 148 |
+
| Delete operations | `sll_vip_delete_at_index_seq` | Delete at index |
|
| 149 |
+
| Delete operations | `sll_vip_delete_value_seq` | Delete by value |
|
| 150 |
+
|
| 151 |
+
## 🔧 Key Features
|
| 152 |
+
|
| 153 |
+
1. **Address-based and Index-based Operations**: Supports both addressing schemes
|
| 154 |
+
2. **Singly Linked Structure**: Tracks only next node addresses (forward direction)
|
| 155 |
+
3. **Value-based Deletion**: Can delete nodes by searching for value
|
| 156 |
+
4. **Intelligent Scoreboard**: Models linked list with address and data tracking
|
| 157 |
+
5. **Fault Detection**: Validates boundary conditions and error flags
|
| 158 |
+
6. **Head/Tail Tracking**: Monitors list head and tail pointers
|
| 159 |
+
|
| 160 |
+
## 🔍 Differences from Doubly Linked List
|
| 161 |
+
|
| 162 |
+
| Aspect | Singly Linked List | Doubly Linked List |
|
| 163 |
+
|--------|-------------------|-------------------|
|
| 164 |
+
| **Node Structure** | data + next pointer | data + next + previous pointers |
|
| 165 |
+
| **Traversal** | Forward only | Forward and backward |
|
| 166 |
+
| **Operations** | 6 ops (insert/delete/read) | 6 ops (same operations) |
|
| 167 |
+
| **Address Outputs** | Single next pointer | Both pre and next pointers |
|
| 168 |
+
| **Memory Efficiency** | Lower overhead (one pointer) | Higher overhead (two pointers) |
|
| 169 |
+
|
| 170 |
+
## 🚨 Common Issues & Solutions
|
| 171 |
+
|
| 172 |
+
### Issue: Interface signal width mismatch
|
| 173 |
+
**Solution:** Update `sll_vip_if.sv` lines 9-18 to match your SLL's parameters:
|
| 174 |
+
```systemverilog
|
| 175 |
+
logic [DATA_WIDTH-1:0] data_in;
|
| 176 |
+
logic [ADDR_WIDTH-1:0] addr_in;
|
| 177 |
+
logic [DATA_WIDTH-1:0] data_out;
|
| 178 |
+
logic [ADDR_WIDTH-1:0] next_node_addr;
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
### Issue: Address vs Index confusion
|
| 182 |
+
**Solution:**
|
| 183 |
+
- **Address operations (op[2]==0)**: Use actual node addresses from DUT
|
| 184 |
+
- **Index operations (op[2]==1)**: Use sequential index (0, 1, 2, ...)
|
| 185 |
+
|
| 186 |
+
### Future Work
|
| 187 |
+
- Add assertions for singly-linked integrity (next pointer validity)
|
| 188 |
+
- Add functional coverage for:
|
| 189 |
+
- Insert/delete at head/tail/middle
|
| 190 |
+
- Empty to full transitions
|
| 191 |
+
- Sequential vs random operations
|
| 192 |
+
- Enhanced scoreboard to track full node address mapping
|
| 193 |
+
|
| 194 |
+
## 📚 Example Usage
|
| 195 |
+
|
| 196 |
+
```systemverilog
|
| 197 |
+
class my_test extends base_test;
|
| 198 |
+
`uvm_component_utils(my_test)
|
| 199 |
+
|
| 200 |
+
task run_phase(uvm_phase phase);
|
| 201 |
+
sll_vip_insert_at_index_seq insert_seq;
|
| 202 |
+
sll_vip_read_seq read_seq;
|
| 203 |
+
sll_vip_delete_value_seq delete_seq;
|
| 204 |
+
|
| 205 |
+
phase.raise_objection(this);
|
| 206 |
+
|
| 207 |
+
// Build a list
|
| 208 |
+
insert_seq = sll_vip_insert_at_index_seq::type_id::create("insert_seq");
|
| 209 |
+
insert_seq.num_inserts = 5;
|
| 210 |
+
insert_seq.start(env.get_sequencer());
|
| 211 |
+
|
| 212 |
+
#500ns;
|
| 213 |
+
|
| 214 |
+
// Read nodes
|
| 215 |
+
read_seq = sll_vip_read_seq::type_id::create("read_seq");
|
| 216 |
+
read_seq.num_reads = 5;
|
| 217 |
+
read_seq.start(env.get_sequencer());
|
| 218 |
+
|
| 219 |
+
#500ns;
|
| 220 |
+
|
| 221 |
+
// Delete specific values
|
| 222 |
+
delete_seq = sll_vip_delete_value_seq::type_id::create("delete_seq");
|
| 223 |
+
delete_seq.num_deletes = 2;
|
| 224 |
+
delete_seq.start(env.get_sequencer());
|
| 225 |
+
|
| 226 |
+
#500ns;
|
| 227 |
+
phase.drop_objection(this);
|
| 228 |
+
endtask
|
| 229 |
+
endclass
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
## 🎯 Key Characteristics
|
| 233 |
+
|
| 234 |
+
- **Unidirectional Traversal**: Can only navigate forward (head→tail)
|
| 235 |
+
- **Address Management**: Internal addresses managed by DUT, not sequential
|
| 236 |
+
- **Flexible Insert/Delete**: Supports both address-based and index-based operations
|
| 237 |
+
- **Value Search**: Can find and delete nodes by data value
|
| 238 |
+
- **Memory Efficient**: Only stores next pointer per node (no previous pointer)
|
| 239 |
+
|
| 240 |
+
**Happy Verifying! 🚀**
|
| 241 |
+
|
| 242 |
+
*This VIP demonstrates advanced verification of pointer-based data structures with UVM.*
|
Weiyet_RTLStructLib/source/Singly_Linked_List/vip/uvm/agent/sll_vip_driver.sv
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 2 |
+
// Create Date: 01/04/2026
|
| 3 |
+
// Module Name: sll_vip_driver
|
| 4 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 5 |
+
|
| 6 |
+
class sll_vip_driver extends uvm_driver #(sll_vip_seq_item);
|
| 7 |
+
`uvm_component_utils(sll_vip_driver)
|
| 8 |
+
|
| 9 |
+
virtual sll_vip_if vif;
|
| 10 |
+
sll_vip_config cfg;
|
| 11 |
+
|
| 12 |
+
function new(string name = "sll_vip_driver", uvm_component parent = null);
|
| 13 |
+
super.new(name, parent);
|
| 14 |
+
endfunction
|
| 15 |
+
|
| 16 |
+
function void build_phase(uvm_phase phase);
|
| 17 |
+
super.build_phase(phase);
|
| 18 |
+
if (!uvm_config_db#(virtual sll_vip_if)::get(this, "", "sll_vip_vif", vif))
|
| 19 |
+
`uvm_fatal("DRIVER", "No virtual interface")
|
| 20 |
+
if (!uvm_config_db#(sll_vip_config)::get(this, "", "sll_vip_cfg", cfg))
|
| 21 |
+
`uvm_fatal("DRIVER", "No config")
|
| 22 |
+
endfunction
|
| 23 |
+
|
| 24 |
+
task run_phase(uvm_phase phase);
|
| 25 |
+
vif.cb.op <= 3'b000;
|
| 26 |
+
vif.cb.op_start <= 1'b0;
|
| 27 |
+
vif.cb.data_in <= '0;
|
| 28 |
+
vif.cb.addr_in <= '0;
|
| 29 |
+
|
| 30 |
+
forever begin
|
| 31 |
+
seq_item_port.get_next_item(req);
|
| 32 |
+
drive_item(req);
|
| 33 |
+
seq_item_port.item_done();
|
| 34 |
+
end
|
| 35 |
+
endtask
|
| 36 |
+
|
| 37 |
+
task drive_item(sll_vip_seq_item item);
|
| 38 |
+
@(vif.cb);
|
| 39 |
+
vif.cb.op <= item.op;
|
| 40 |
+
vif.cb.op_start <= 1'b1;
|
| 41 |
+
vif.cb.data_in <= item.data[cfg.DATA_WIDTH-1:0];
|
| 42 |
+
vif.cb.addr_in <= item.addr[$clog2(cfg.MAX_NODE+1)-1:0];
|
| 43 |
+
|
| 44 |
+
@(vif.cb);
|
| 45 |
+
wait(vif.cb.op_done);
|
| 46 |
+
|
| 47 |
+
item.result_data = vif.cb.data_out;
|
| 48 |
+
item.result_next_addr = vif.cb.next_node_addr;
|
| 49 |
+
item.op_done = vif.cb.op_done;
|
| 50 |
+
item.fault = vif.cb.fault;
|
| 51 |
+
item.current_len = vif.cb.length;
|
| 52 |
+
item.current_head = vif.cb.head;
|
| 53 |
+
item.current_tail = vif.cb.tail;
|
| 54 |
+
|
| 55 |
+
@(vif.cb);
|
| 56 |
+
vif.cb.op_start <= 1'b0;
|
| 57 |
+
|
| 58 |
+
`uvm_info("DRIVER", $sformatf("%s: addr=%0d data=0x%0h fault=%0b",
|
| 59 |
+
item.op.name(), item.addr, item.data, item.fault), UVM_HIGH)
|
| 60 |
+
endtask
|
| 61 |
+
|
| 62 |
+
endclass
|
Weiyet_RTLStructLib/source/Systolic_Array/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Systolic Array
|
| 2 |
+
|
| 3 |
+
A production-ready, parameterized systolic array implementation for matrix multiplication in SystemVerilog. This design uses weight-stationary dataflow, commonly found in AI accelerators like Google's TPU, and is suitable for FPGA and ASIC synthesis.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- **Configurable array dimensions** (rows × columns)
|
| 8 |
+
- **Weight-stationary dataflow** - weights loaded once, activations streamed
|
| 9 |
+
- **Parameterized data widths** for activations, weights, and accumulators
|
| 10 |
+
- **Automatic input skewing** for proper systolic operation
|
| 11 |
+
- **Signed/unsigned arithmetic** support
|
| 12 |
+
- **AXI-Stream-like handshaking** for easy integration
|
| 13 |
+
- **Fully synchronous design** with active-low reset
|
| 14 |
+
- **Modular architecture** - PE, array core, and top wrapper
|
| 15 |
+
|
| 16 |
+
## Architecture
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
Weight Inputs (top)
|
| 20 |
+
↓ ↓ ↓ ↓
|
| 21 |
+
+---+---+---+---+
|
| 22 |
+
Act → → |PE |PE |PE |PE | → → (cascade)
|
| 23 |
+
+---+---+---+---+
|
| 24 |
+
Act → → |PE |PE |PE |PE | → →
|
| 25 |
+
+---+---+---+---+
|
| 26 |
+
Act → → |PE |PE |PE |PE | → →
|
| 27 |
+
+---+---+---+---+
|
| 28 |
+
Act → → |PE |PE |PE |PE | → →
|
| 29 |
+
+---+---+---+---+
|
| 30 |
+
↓ ↓ ↓ ↓
|
| 31 |
+
Result Outputs (bottom)
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
### Dataflow
|
| 35 |
+
|
| 36 |
+
1. **Weight Loading**: Weights from matrix B are loaded into PEs column-by-column
|
| 37 |
+
2. **Computation**: Activations from matrix A stream left-to-right with automatic skewing
|
| 38 |
+
3. **Accumulation**: Partial sums flow top-to-bottom through each column
|
| 39 |
+
4. **Result Extraction**: Final results emerge from the bottom after drain cycles
|
| 40 |
+
|
| 41 |
+
## Files
|
| 42 |
+
|
| 43 |
+
| File | Description |
|
| 44 |
+
|------|-------------|
|
| 45 |
+
| `src/pe.sv` | Processing Element - MAC unit with weight register |
|
| 46 |
+
| `src/systolic_array.sv` | 2D array of PEs with interconnects |
|
| 47 |
+
| `src/systolic_array_top.sv` | Top wrapper with FSM and handshaking |
|
| 48 |
+
|
| 49 |
+
## Parameters
|
| 50 |
+
|
| 51 |
+
### Top Module (`systolic_array_top`)
|
| 52 |
+
|
| 53 |
+
| Parameter | Description | Default |
|
| 54 |
+
|-----------|-------------|---------|
|
| 55 |
+
| ARRAY_ROWS | Number of PE rows (M dimension tile) | 4 |
|
| 56 |
+
| ARRAY_COLS | Number of PE columns (N dimension tile) | 4 |
|
| 57 |
+
| K_DIM | Inner/reduction dimension | 4 |
|
| 58 |
+
| DATA_WIDTH | Activation bit width | 8 |
|
| 59 |
+
| WEIGHT_WIDTH | Weight bit width | 8 |
|
| 60 |
+
| ACC_WIDTH | Accumulator bit width | 32 |
|
| 61 |
+
| SIGNED_MATH | Enable signed arithmetic (1) or unsigned (0) | 1 |
|
| 62 |
+
| INPUT_SKEW | Auto-skew inputs (1) or assume pre-skewed (0) | 1 |
|
| 63 |
+
|
| 64 |
+
### Processing Element (`pe`)
|
| 65 |
+
|
| 66 |
+
| Parameter | Description | Default |
|
| 67 |
+
|-----------|-------------|---------|
|
| 68 |
+
| DATA_WIDTH | Input activation bit width | 8 |
|
| 69 |
+
| WEIGHT_WIDTH | Weight bit width | 8 |
|
| 70 |
+
| ACC_WIDTH | Accumulator bit width | 32 |
|
| 71 |
+
| SIGNED_MATH | Signed (1) or unsigned (0) arithmetic | 1 |
|
| 72 |
+
|
| 73 |
+
## Interface Signals
|
| 74 |
+
|
| 75 |
+
### Control Interface
|
| 76 |
+
|
| 77 |
+
| Signal | Direction | Width | Description |
|
| 78 |
+
|--------|-----------|-------|-------------|
|
| 79 |
+
| clk | Input | 1 | System clock |
|
| 80 |
+
| rst_n | Input | 1 | Active-low synchronous reset |
|
| 81 |
+
| start | Input | 1 | Start computation pulse |
|
| 82 |
+
| busy | Output | 1 | Operation in progress |
|
| 83 |
+
| done | Output | 1 | Computation complete pulse |
|
| 84 |
+
|
| 85 |
+
### Weight Loading Interface
|
| 86 |
+
|
| 87 |
+
| Signal | Direction | Width | Description |
|
| 88 |
+
|--------|-----------|-------|-------------|
|
| 89 |
+
| weight_data | Input | ARRAY_COLS × WEIGHT_WIDTH | One row of weights |
|
| 90 |
+
| weight_valid | Input | 1 | Weight data valid |
|
| 91 |
+
| weight_ready | Output | 1 | Ready to accept weights |
|
| 92 |
+
|
| 93 |
+
### Activation Streaming Interface
|
| 94 |
+
|
| 95 |
+
| Signal | Direction | Width | Description |
|
| 96 |
+
|--------|-----------|-------|-------------|
|
| 97 |
+
| act_data | Input | ARRAY_ROWS × DATA_WIDTH | One column of activations |
|
| 98 |
+
| act_valid | Input | 1 | Activation data valid |
|
| 99 |
+
| act_ready | Output | 1 | Ready to accept activations |
|
| 100 |
+
|
| 101 |
+
### Result Output Interface
|
| 102 |
+
|
| 103 |
+
| Signal | Direction | Width | Description |
|
| 104 |
+
|--------|-----------|-------|-------------|
|
| 105 |
+
| result_data | Output | ARRAY_COLS × ACC_WIDTH | One row of results |
|
| 106 |
+
| result_valid | Output | 1 | Result data valid |
|
| 107 |
+
| result_ready | Input | 1 | Downstream ready for results |
|
| 108 |
+
|
| 109 |
+
## Usage Example
|
| 110 |
+
|
| 111 |
+
```verilog
|
| 112 |
+
// Instantiate a 4x4 systolic array for 8-bit signed matrix multiplication
|
| 113 |
+
systolic_array_top #(
|
| 114 |
+
.ARRAY_ROWS (4),
|
| 115 |
+
.ARRAY_COLS (4),
|
| 116 |
+
.K_DIM (4),
|
| 117 |
+
.DATA_WIDTH (8),
|
| 118 |
+
.WEIGHT_WIDTH (8),
|
| 119 |
+
.ACC_WIDTH (32),
|
| 120 |
+
.SIGNED_MATH (1),
|
| 121 |
+
.INPUT_SKEW (1)
|
| 122 |
+
) u_systolic_array (
|
| 123 |
+
.clk (clk),
|
| 124 |
+
.rst_n (rst_n),
|
| 125 |
+
|
| 126 |
+
// Control
|
| 127 |
+
.start (start),
|
| 128 |
+
.busy (busy),
|
| 129 |
+
.done (done),
|
| 130 |
+
|
| 131 |
+
// Weight loading
|
| 132 |
+
.weight_data (weight_data),
|
| 133 |
+
.weight_valid (weight_valid),
|
| 134 |
+
.weight_ready (weight_ready),
|
| 135 |
+
|
| 136 |
+
// Activation streaming
|
| 137 |
+
.act_data (act_data),
|
| 138 |
+
.act_valid (act_valid),
|
| 139 |
+
.act_ready (act_ready),
|
| 140 |
+
|
| 141 |
+
// Results
|
| 142 |
+
.result_data (result_data),
|
| 143 |
+
.result_valid (result_valid),
|
| 144 |
+
.result_ready (result_ready)
|
| 145 |
+
);
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
## Operation Sequence
|
| 149 |
+
|
| 150 |
+
### 1. Weight Loading Phase
|
| 151 |
+
```
|
| 152 |
+
for k = 0 to K_DIM-1:
|
| 153 |
+
for row = 0 to ARRAY_ROWS-1:
|
| 154 |
+
weight_data = B[row][k*ARRAY_COLS : (k+1)*ARRAY_COLS-1]
|
| 155 |
+
weight_valid = 1
|
| 156 |
+
wait(weight_ready)
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
### 2. Computation Phase
|
| 160 |
+
```
|
| 161 |
+
for k = 0 to K_DIM-1:
|
| 162 |
+
act_data = A[0:ARRAY_ROWS-1][k] // Column k of activation matrix
|
| 163 |
+
act_valid = 1
|
| 164 |
+
wait(act_ready)
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
### 3. Result Collection
|
| 168 |
+
```
|
| 169 |
+
while (!done):
|
| 170 |
+
if (result_valid && result_ready):
|
| 171 |
+
store result_data
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
## Design Notes
|
| 175 |
+
|
| 176 |
+
### 1. Matrix Dimensions
|
| 177 |
+
For computing C = A × B where A is (M × K) and B is (K × N):
|
| 178 |
+
- Configure `ARRAY_ROWS` ≤ M (number of output rows per tile)
|
| 179 |
+
- Configure `ARRAY_COLS` ≤ N (number of output columns per tile)
|
| 180 |
+
- Configure `K_DIM` = K (reduction dimension)
|
| 181 |
+
|
| 182 |
+
For matrices larger than the array, tile the computation and accumulate partial results.
|
| 183 |
+
|
| 184 |
+
### 2. Input Skewing
|
| 185 |
+
The systolic array requires input data to be "skewed" so that data arrives at each PE at the correct time:
|
| 186 |
+
- Row 0: No delay
|
| 187 |
+
- Row 1: 1 cycle delay
|
| 188 |
+
- Row N: N cycles delay
|
| 189 |
+
|
| 190 |
+
When `INPUT_SKEW=1` (default), this is handled automatically. Set to 0 if providing pre-skewed data.
|
| 191 |
+
|
| 192 |
+
### 3. Accumulator Width
|
| 193 |
+
To prevent overflow, set `ACC_WIDTH` to accommodate:
|
| 194 |
+
```
|
| 195 |
+
ACC_WIDTH ≥ DATA_WIDTH + WEIGHT_WIDTH + ceil(log2(K_DIM))
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
For 8-bit inputs and K_DIM=128: ACC_WIDTH ≥ 8 + 8 + 7 = 23 bits (use 32 for safety).
|
| 199 |
+
|
| 200 |
+
### 4. Weight-Stationary Tradeoffs
|
| 201 |
+
**Advantages:**
|
| 202 |
+
- Weights loaded once, reused across all activation rows
|
| 203 |
+
- Efficient for inference with fixed weights
|
| 204 |
+
- Lower memory bandwidth for weights
|
| 205 |
+
|
| 206 |
+
**Considerations:**
|
| 207 |
+
- Weight loading adds latency before computation
|
| 208 |
+
- Best when weight matrix changes infrequently
|
| 209 |
+
|
| 210 |
+
### 5. Pipelining and Throughput
|
| 211 |
+
- Array latency: `ARRAY_ROWS + K_DIM` cycles after activations start
|
| 212 |
+
- Throughput: 1 result column per cycle during drain phase
|
| 213 |
+
- Multiple matrices can be pipelined by overlapping weight loads
|
| 214 |
+
|
| 215 |
+
## Resource Utilization
|
| 216 |
+
|
| 217 |
+
The design scales approximately as:
|
| 218 |
+
- **Registers**: O(ARRAY_ROWS × ARRAY_COLS × (DATA_WIDTH + WEIGHT_WIDTH + ACC_WIDTH))
|
| 219 |
+
- **DSP blocks**: O(ARRAY_ROWS × ARRAY_COLS) (one multiplier per PE)
|
| 220 |
+
- **LUTs**: O(ARRAY_ROWS × ARRAY_COLS × ACC_WIDTH) for adders
|
| 221 |
+
|
| 222 |
+
### Example (Xilinx 7-Series, 4×4 array, 8-bit data):
|
| 223 |
+
| Resource | Count |
|
| 224 |
+
|----------|-------|
|
| 225 |
+
| LUT | ~500 |
|
| 226 |
+
| FF | ~800 |
|
| 227 |
+
| DSP48 | 16 |
|
| 228 |
+
|
| 229 |
+
## Limitations
|
| 230 |
+
|
| 231 |
+
1. **Single clock domain**: Design assumes single clock for all operations
|
| 232 |
+
2. **Fixed K dimension**: K_DIM must be set at synthesis time
|
| 233 |
+
3. **No output buffering**: Downstream must be ready when results are valid
|
| 234 |
+
4. **Rectangular arrays only**: ARRAY_ROWS and ARRAY_COLS are independent
|
| 235 |
+
|
| 236 |
+
## References
|
| 237 |
+
|
| 238 |
+
1. H.T. Kung, "Why Systolic Architectures?", Computer, 1982
|
| 239 |
+
2. N. Jouppi et al., "In-Datacenter Performance Analysis of a Tensor Processing Unit", ISCA 2017
|
| 240 |
+
3. Y. Chen et al., "Eyeriss: An Energy-Efficient Reconfigurable Accelerator for Deep CNNs", ISSCC 2016
|
Weiyet_RTLStructLib/source/Systolic_Array/src/pe.sv
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
// Module Name: pe (Processing Element)
|
| 4 |
+
// Description: Single processing element for systolic array matrix multiplication.
|
| 5 |
+
// Implements weight-stationary dataflow with multiply-accumulate.
|
| 6 |
+
//
|
| 7 |
+
// Dataflow:
|
| 8 |
+
// - Weight is pre-loaded and remains stationary during computation
|
| 9 |
+
// - Activation flows left-to-right (horizontal propagation)
|
| 10 |
+
// - Partial sum flows top-to-bottom (vertical propagation)
|
| 11 |
+
// - Each PE computes: psum_out = psum_in + (weight * activation)
|
| 12 |
+
//
|
| 13 |
+
// Operation Modes:
|
| 14 |
+
// - IDLE: PE is inactive, passes data through
|
| 15 |
+
// - LOAD: Load weight into weight register
|
| 16 |
+
// - COMPUTE: Perform MAC operation and propagate data
|
| 17 |
+
// - OUTPUT: Output accumulated result (when configured for output-stationary)
|
| 18 |
+
//
|
| 19 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 20 |
+
|
| 21 |
+
module pe #(
|
| 22 |
+
parameter DATA_WIDTH = 8, // Input data bit width
|
| 23 |
+
parameter WEIGHT_WIDTH = 8, // Weight bit width
|
| 24 |
+
parameter ACC_WIDTH = 32, // Accumulator bit width (must handle full precision)
|
| 25 |
+
parameter SIGNED_MATH = 1 // 1 = signed, 0 = unsigned arithmetic
|
| 26 |
+
)(
|
| 27 |
+
input wire clk,
|
| 28 |
+
input wire rst_n, // Active-low reset
|
| 29 |
+
|
| 30 |
+
// Control signals
|
| 31 |
+
input wire enable, // Enable PE operation
|
| 32 |
+
input wire clear_acc, // Clear accumulator
|
| 33 |
+
input wire load_weight, // Load weight register
|
| 34 |
+
|
| 35 |
+
// Data inputs
|
| 36 |
+
input wire [DATA_WIDTH-1:0] act_in, // Activation input (from left)
|
| 37 |
+
input wire [WEIGHT_WIDTH-1:0] weight_in, // Weight input for loading
|
| 38 |
+
input wire [ACC_WIDTH-1:0] psum_in, // Partial sum input (from top)
|
| 39 |
+
|
| 40 |
+
// Data outputs
|
| 41 |
+
output reg [DATA_WIDTH-1:0] act_out, // Activation output (to right)
|
| 42 |
+
output reg [ACC_WIDTH-1:0] psum_out, // Partial sum output (to bottom)
|
| 43 |
+
|
| 44 |
+
// Status
|
| 45 |
+
output wire [WEIGHT_WIDTH-1:0] weight_stored // Current stored weight (for debug)
|
| 46 |
+
);
|
| 47 |
+
|
| 48 |
+
//--------------------------------------------------------------------------
|
| 49 |
+
// Internal signals
|
| 50 |
+
//--------------------------------------------------------------------------
|
| 51 |
+
reg [WEIGHT_WIDTH-1:0] weight_reg; // Weight register (stationary)
|
| 52 |
+
wire [DATA_WIDTH+WEIGHT_WIDTH-1:0] product; // Multiplication result
|
| 53 |
+
wire [ACC_WIDTH-1:0] mac_result; // MAC result
|
| 54 |
+
|
| 55 |
+
//--------------------------------------------------------------------------
|
| 56 |
+
// Weight register - loads and holds weight value
|
| 57 |
+
//--------------------------------------------------------------------------
|
| 58 |
+
always @(posedge clk or negedge rst_n) begin
|
| 59 |
+
if (!rst_n) begin
|
| 60 |
+
weight_reg <= {WEIGHT_WIDTH{1'b0}};
|
| 61 |
+
end else if (load_weight) begin
|
| 62 |
+
weight_reg <= weight_in;
|
| 63 |
+
end
|
| 64 |
+
end
|
| 65 |
+
|
| 66 |
+
assign weight_stored = weight_reg;
|
| 67 |
+
|
| 68 |
+
//--------------------------------------------------------------------------
|
| 69 |
+
// Multiply-Accumulate (MAC) computation
|
| 70 |
+
//--------------------------------------------------------------------------
|
| 71 |
+
generate
|
| 72 |
+
if (SIGNED_MATH == 1) begin : gen_signed_mac
|
| 73 |
+
// Signed multiplication
|
| 74 |
+
wire signed [DATA_WIDTH-1:0] act_signed = $signed(act_in);
|
| 75 |
+
wire signed [WEIGHT_WIDTH-1:0] weight_signed = $signed(weight_reg);
|
| 76 |
+
wire signed [DATA_WIDTH+WEIGHT_WIDTH-1:0] product_signed;
|
| 77 |
+
|
| 78 |
+
assign product_signed = act_signed * weight_signed;
|
| 79 |
+
assign product = product_signed;
|
| 80 |
+
|
| 81 |
+
// Sign-extend product to accumulator width and add partial sum
|
| 82 |
+
wire signed [ACC_WIDTH-1:0] product_extended = {{(ACC_WIDTH-DATA_WIDTH-WEIGHT_WIDTH){product_signed[DATA_WIDTH+WEIGHT_WIDTH-1]}}, product_signed};
|
| 83 |
+
wire signed [ACC_WIDTH-1:0] psum_signed = $signed(psum_in);
|
| 84 |
+
|
| 85 |
+
assign mac_result = product_extended + psum_signed;
|
| 86 |
+
end else begin : gen_unsigned_mac
|
| 87 |
+
// Unsigned multiplication
|
| 88 |
+
assign product = act_in * weight_reg;
|
| 89 |
+
|
| 90 |
+
// Zero-extend product to accumulator width and add partial sum
|
| 91 |
+
wire [ACC_WIDTH-1:0] product_extended = {{(ACC_WIDTH-DATA_WIDTH-WEIGHT_WIDTH){1'b0}}, product};
|
| 92 |
+
|
| 93 |
+
assign mac_result = product_extended + psum_in;
|
| 94 |
+
end
|
| 95 |
+
endgenerate
|
| 96 |
+
|
| 97 |
+
//--------------------------------------------------------------------------
|
| 98 |
+
// Output registers - pipeline stage for data propagation
|
| 99 |
+
//--------------------------------------------------------------------------
|
| 100 |
+
|
| 101 |
+
// Activation propagation (left to right) - 1 cycle delay
|
| 102 |
+
always @(posedge clk or negedge rst_n) begin
|
| 103 |
+
if (!rst_n) begin
|
| 104 |
+
act_out <= {DATA_WIDTH{1'b0}};
|
| 105 |
+
end else if (enable) begin
|
| 106 |
+
act_out <= act_in;
|
| 107 |
+
end
|
| 108 |
+
end
|
| 109 |
+
|
| 110 |
+
// Partial sum propagation (top to bottom) - MAC result
|
| 111 |
+
always @(posedge clk or negedge rst_n) begin
|
| 112 |
+
if (!rst_n) begin
|
| 113 |
+
psum_out <= {ACC_WIDTH{1'b0}};
|
| 114 |
+
end else if (clear_acc) begin
|
| 115 |
+
psum_out <= {ACC_WIDTH{1'b0}};
|
| 116 |
+
end else if (enable) begin
|
| 117 |
+
psum_out <= mac_result;
|
| 118 |
+
end
|
| 119 |
+
end
|
| 120 |
+
|
| 121 |
+
endmodule
|
Weiyet_RTLStructLib/source/Systolic_Array/src/rtl_list.f
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pe.sv
|
| 2 |
+
systolic_array.sv
|
| 3 |
+
systolic_array_top.sv
|
Weiyet_RTLStructLib/source/Systolic_Array/src/systolic_array.sv
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
// Module Name: systolic_array
|
| 4 |
+
// Description: Weight-stationary systolic array for matrix multiplication.
|
| 5 |
+
// Computes C = A × B where:
|
| 6 |
+
// - A is (ARRAY_ROWS × K) activation matrix, streamed row by row
|
| 7 |
+
// - B is (K × ARRAY_COLS) weight matrix, pre-loaded
|
| 8 |
+
// - C is (ARRAY_ROWS × ARRAY_COLS) output matrix
|
| 9 |
+
//
|
| 10 |
+
// Architecture:
|
| 11 |
+
// - 2D grid of Processing Elements (PEs) sized ARRAY_ROWS × ARRAY_COLS
|
| 12 |
+
// - Weights are pre-loaded into PEs and remain stationary
|
| 13 |
+
// - Activations flow left-to-right with proper skewing
|
| 14 |
+
// - Partial sums flow top-to-bottom
|
| 15 |
+
// - Results appear at bottom row after (ARRAY_ROWS + K - 1) cycles
|
| 16 |
+
//
|
| 17 |
+
// Dataflow:
|
| 18 |
+
// 1. LOAD_WEIGHT: Load weight matrix B into PEs (column by column)
|
| 19 |
+
// 2. COMPUTE: Stream activation matrix A rows with skewing
|
| 20 |
+
// 3. DRAIN: Extract results from bottom row
|
| 21 |
+
//
|
| 22 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 23 |
+
|
| 24 |
+
module systolic_array #(
|
| 25 |
+
parameter ARRAY_ROWS = 4, // Number of PE rows (M dimension tile)
|
| 26 |
+
parameter ARRAY_COLS = 4, // Number of PE columns (N dimension tile)
|
| 27 |
+
parameter DATA_WIDTH = 8, // Activation data width
|
| 28 |
+
parameter WEIGHT_WIDTH = 8, // Weight data width
|
| 29 |
+
parameter ACC_WIDTH = 32, // Accumulator width
|
| 30 |
+
parameter SIGNED_MATH = 1 // 1 = signed, 0 = unsigned
|
| 31 |
+
)(
|
| 32 |
+
input wire clk,
|
| 33 |
+
input wire rst_n,
|
| 34 |
+
|
| 35 |
+
// Control signals
|
| 36 |
+
input wire enable, // Global enable
|
| 37 |
+
input wire clear_acc, // Clear all accumulators
|
| 38 |
+
input wire [ARRAY_COLS-1:0] load_weight, // Load weight per column
|
| 39 |
+
input wire [$clog2(ARRAY_ROWS)-1:0] weight_row_sel, // Row select for weight load
|
| 40 |
+
|
| 41 |
+
// Activation inputs (left side) - one per row
|
| 42 |
+
input wire [ARRAY_ROWS*DATA_WIDTH-1:0] act_in, // Packed activation inputs
|
| 43 |
+
|
| 44 |
+
// Weight inputs (top side) - one per column during loading
|
| 45 |
+
input wire [ARRAY_COLS*WEIGHT_WIDTH-1:0] weight_in, // Packed weight inputs
|
| 46 |
+
|
| 47 |
+
// Partial sum inputs (top side) - normally zero, for cascading arrays
|
| 48 |
+
input wire [ARRAY_COLS*ACC_WIDTH-1:0] psum_in, // Packed psum inputs (top)
|
| 49 |
+
|
| 50 |
+
// Partial sum outputs (bottom side) - computation results
|
| 51 |
+
output wire [ARRAY_COLS*ACC_WIDTH-1:0] psum_out, // Packed psum outputs (bottom)
|
| 52 |
+
|
| 53 |
+
// Activation outputs (right side) - for cascading arrays
|
| 54 |
+
output wire [ARRAY_ROWS*DATA_WIDTH-1:0] act_out // Packed activation outputs
|
| 55 |
+
);
|
| 56 |
+
|
| 57 |
+
//--------------------------------------------------------------------------
|
| 58 |
+
// Local parameters
|
| 59 |
+
//--------------------------------------------------------------------------
|
| 60 |
+
localparam ROW_SEL_WIDTH = $clog2(ARRAY_ROWS) > 0 ? $clog2(ARRAY_ROWS) : 1;
|
| 61 |
+
|
| 62 |
+
//--------------------------------------------------------------------------
|
| 63 |
+
// Internal interconnect wires
|
| 64 |
+
//--------------------------------------------------------------------------
|
| 65 |
+
// Horizontal activation wires (left to right through PE columns)
|
| 66 |
+
// act_h[row][col] connects PE[row][col-1].act_out to PE[row][col].act_in
|
| 67 |
+
wire [DATA_WIDTH-1:0] act_h [ARRAY_ROWS-1:0][ARRAY_COLS:0];
|
| 68 |
+
|
| 69 |
+
// Vertical partial sum wires (top to bottom through PE rows)
|
| 70 |
+
// psum_v[row][col] connects PE[row-1][col].psum_out to PE[row][col].psum_in
|
| 71 |
+
wire [ACC_WIDTH-1:0] psum_v [ARRAY_ROWS:0][ARRAY_COLS-1:0];
|
| 72 |
+
|
| 73 |
+
// Weight load enable per PE
|
| 74 |
+
wire load_weight_pe [ARRAY_ROWS-1:0][ARRAY_COLS-1:0];
|
| 75 |
+
|
| 76 |
+
//--------------------------------------------------------------------------
|
| 77 |
+
// Weight input unpacking
|
| 78 |
+
//--------------------------------------------------------------------------
|
| 79 |
+
wire [WEIGHT_WIDTH-1:0] weight_col [ARRAY_COLS-1:0];
|
| 80 |
+
|
| 81 |
+
genvar c;
|
| 82 |
+
generate
|
| 83 |
+
for (c = 0; c < ARRAY_COLS; c = c + 1) begin : gen_weight_unpack
|
| 84 |
+
assign weight_col[c] = weight_in[(c+1)*WEIGHT_WIDTH-1 -: WEIGHT_WIDTH];
|
| 85 |
+
end
|
| 86 |
+
endgenerate
|
| 87 |
+
|
| 88 |
+
//--------------------------------------------------------------------------
|
| 89 |
+
// Connect activation inputs (left edge)
|
| 90 |
+
//--------------------------------------------------------------------------
|
| 91 |
+
genvar r;
|
| 92 |
+
generate
|
| 93 |
+
for (r = 0; r < ARRAY_ROWS; r = r + 1) begin : gen_act_in_connect
|
| 94 |
+
assign act_h[r][0] = act_in[(r+1)*DATA_WIDTH-1 -: DATA_WIDTH];
|
| 95 |
+
end
|
| 96 |
+
endgenerate
|
| 97 |
+
|
| 98 |
+
//--------------------------------------------------------------------------
|
| 99 |
+
// Connect partial sum inputs (top edge)
|
| 100 |
+
//--------------------------------------------------------------------------
|
| 101 |
+
generate
|
| 102 |
+
for (c = 0; c < ARRAY_COLS; c = c + 1) begin : gen_psum_in_connect
|
| 103 |
+
assign psum_v[0][c] = psum_in[(c+1)*ACC_WIDTH-1 -: ACC_WIDTH];
|
| 104 |
+
end
|
| 105 |
+
endgenerate
|
| 106 |
+
|
| 107 |
+
//--------------------------------------------------------------------------
|
| 108 |
+
// Generate PE array with interconnections
|
| 109 |
+
//--------------------------------------------------------------------------
|
| 110 |
+
generate
|
| 111 |
+
for (r = 0; r < ARRAY_ROWS; r = r + 1) begin : gen_row
|
| 112 |
+
for (c = 0; c < ARRAY_COLS; c = c + 1) begin : gen_col
|
| 113 |
+
|
| 114 |
+
// Weight load enable: column selected AND row matches
|
| 115 |
+
assign load_weight_pe[r][c] = load_weight[c] && (weight_row_sel == r[ROW_SEL_WIDTH-1:0]);
|
| 116 |
+
|
| 117 |
+
// Instantiate Processing Element
|
| 118 |
+
pe #(
|
| 119 |
+
.DATA_WIDTH (DATA_WIDTH),
|
| 120 |
+
.WEIGHT_WIDTH (WEIGHT_WIDTH),
|
| 121 |
+
.ACC_WIDTH (ACC_WIDTH),
|
| 122 |
+
.SIGNED_MATH (SIGNED_MATH)
|
| 123 |
+
) pe_inst (
|
| 124 |
+
.clk (clk),
|
| 125 |
+
.rst_n (rst_n),
|
| 126 |
+
|
| 127 |
+
// Control
|
| 128 |
+
.enable (enable),
|
| 129 |
+
.clear_acc (clear_acc),
|
| 130 |
+
.load_weight (load_weight_pe[r][c]),
|
| 131 |
+
|
| 132 |
+
// Data flow
|
| 133 |
+
.act_in (act_h[r][c]),
|
| 134 |
+
.weight_in (weight_col[c]),
|
| 135 |
+
.psum_in (psum_v[r][c]),
|
| 136 |
+
.act_out (act_h[r][c+1]),
|
| 137 |
+
.psum_out (psum_v[r+1][c]),
|
| 138 |
+
|
| 139 |
+
// Debug
|
| 140 |
+
.weight_stored () // Not connected at this level
|
| 141 |
+
);
|
| 142 |
+
|
| 143 |
+
end
|
| 144 |
+
end
|
| 145 |
+
endgenerate
|
| 146 |
+
|
| 147 |
+
//--------------------------------------------------------------------------
|
| 148 |
+
// Connect activation outputs (right edge)
|
| 149 |
+
//--------------------------------------------------------------------------
|
| 150 |
+
generate
|
| 151 |
+
for (r = 0; r < ARRAY_ROWS; r = r + 1) begin : gen_act_out_connect
|
| 152 |
+
assign act_out[(r+1)*DATA_WIDTH-1 -: DATA_WIDTH] = act_h[r][ARRAY_COLS];
|
| 153 |
+
end
|
| 154 |
+
endgenerate
|
| 155 |
+
|
| 156 |
+
//--------------------------------------------------------------------------
|
| 157 |
+
// Connect partial sum outputs (bottom edge)
|
| 158 |
+
//--------------------------------------------------------------------------
|
| 159 |
+
generate
|
| 160 |
+
for (c = 0; c < ARRAY_COLS; c = c + 1) begin : gen_psum_out_connect
|
| 161 |
+
assign psum_out[(c+1)*ACC_WIDTH-1 -: ACC_WIDTH] = psum_v[ARRAY_ROWS][c];
|
| 162 |
+
end
|
| 163 |
+
endgenerate
|
| 164 |
+
|
| 165 |
+
endmodule
|
Weiyet_RTLStructLib/source/Systolic_Array/src/systolic_array_top.sv
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
// Module Name: systolic_array_top
|
| 4 |
+
// Description: Production-ready systolic array for matrix multiplication with
|
| 5 |
+
// complete control logic, input skewing, and AXI-Stream interface.
|
| 6 |
+
//
|
| 7 |
+
// Computes C = A × B where:
|
| 8 |
+
// - A is (M × K) activation matrix
|
| 9 |
+
// - B is (K × N) weight matrix
|
| 10 |
+
// - C is (M × N) output matrix
|
| 11 |
+
//
|
| 12 |
+
// The array is tiled as (ARRAY_ROWS × ARRAY_COLS) PEs.
|
| 13 |
+
// For larger matrices, multiple passes or array cascading is needed.
|
| 14 |
+
//
|
| 15 |
+
// Operation Sequence:
|
| 16 |
+
// 1. Assert start signal
|
| 17 |
+
// 2. Load weights: provide K×N weights via weight_data/weight_valid
|
| 18 |
+
// 3. Stream activations: provide M×K activations via act_data/act_valid
|
| 19 |
+
// 4. Collect results: read M×N results via result_data/result_valid
|
| 20 |
+
// 5. done signal asserts when complete
|
| 21 |
+
//
|
| 22 |
+
// Dataflow (Weight-Stationary):
|
| 23 |
+
// - Weights are loaded column-by-column, row-by-row within each column
|
| 24 |
+
// - Activations are skewed automatically for proper timing
|
| 25 |
+
// - Results are collected from bottom of array with de-skewing
|
| 26 |
+
//
|
| 27 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 28 |
+
|
| 29 |
+
module systolic_array_top #(
|
| 30 |
+
// Array dimensions
|
| 31 |
+
parameter ARRAY_ROWS = 4, // PE rows (process this many output rows)
|
| 32 |
+
parameter ARRAY_COLS = 4, // PE columns (output columns)
|
| 33 |
+
parameter K_DIM = 4, // Inner dimension (reduction dimension)
|
| 34 |
+
|
| 35 |
+
// Data widths
|
| 36 |
+
parameter DATA_WIDTH = 8, // Activation bit width
|
| 37 |
+
parameter WEIGHT_WIDTH = 8, // Weight bit width
|
| 38 |
+
parameter ACC_WIDTH = 32, // Accumulator bit width
|
| 39 |
+
|
| 40 |
+
// Options
|
| 41 |
+
parameter SIGNED_MATH = 1, // 1 = signed arithmetic
|
| 42 |
+
parameter INPUT_SKEW = 1 // 1 = auto-skew inputs, 0 = assume pre-skewed
|
| 43 |
+
)(
|
| 44 |
+
input wire clk,
|
| 45 |
+
input wire rst_n,
|
| 46 |
+
|
| 47 |
+
//--------------------------------------------------------------------------
|
| 48 |
+
// Control interface
|
| 49 |
+
//--------------------------------------------------------------------------
|
| 50 |
+
input wire start, // Start computation
|
| 51 |
+
output reg busy, // Operation in progress
|
| 52 |
+
output reg done, // Computation complete
|
| 53 |
+
|
| 54 |
+
//--------------------------------------------------------------------------
|
| 55 |
+
// Weight loading interface (load before computation)
|
| 56 |
+
//--------------------------------------------------------------------------
|
| 57 |
+
input wire [ARRAY_COLS*WEIGHT_WIDTH-1:0] weight_data, // One row of weights
|
| 58 |
+
input wire weight_valid, // Weight data valid
|
| 59 |
+
output wire weight_ready, // Ready to accept weights
|
| 60 |
+
|
| 61 |
+
//--------------------------------------------------------------------------
|
| 62 |
+
// Activation streaming interface
|
| 63 |
+
//--------------------------------------------------------------------------
|
| 64 |
+
input wire [ARRAY_ROWS*DATA_WIDTH-1:0] act_data, // One column of activations
|
| 65 |
+
input wire act_valid, // Activation data valid
|
| 66 |
+
output wire act_ready, // Ready to accept activations
|
| 67 |
+
|
| 68 |
+
//--------------------------------------------------------------------------
|
| 69 |
+
// Result output interface
|
| 70 |
+
//--------------------------------------------------------------------------
|
| 71 |
+
output wire [ARRAY_COLS*ACC_WIDTH-1:0] result_data, // One row of results
|
| 72 |
+
output reg result_valid, // Result data valid
|
| 73 |
+
input wire result_ready // Downstream ready for results
|
| 74 |
+
);
|
| 75 |
+
|
| 76 |
+
//--------------------------------------------------------------------------
|
| 77 |
+
// Local parameters
|
| 78 |
+
//--------------------------------------------------------------------------
|
| 79 |
+
localparam ROW_SEL_WIDTH = $clog2(ARRAY_ROWS) > 0 ? $clog2(ARRAY_ROWS) : 1;
|
| 80 |
+
localparam K_CNT_WIDTH = $clog2(K_DIM) > 0 ? $clog2(K_DIM) : 1;
|
| 81 |
+
localparam DRAIN_CYCLES = ARRAY_ROWS + K_DIM; // Cycles to drain all results
|
| 82 |
+
|
| 83 |
+
//--------------------------------------------------------------------------
|
| 84 |
+
// FSM states
|
| 85 |
+
//--------------------------------------------------------------------------
|
| 86 |
+
localparam [2:0] STATE_IDLE = 3'd0;
|
| 87 |
+
localparam [2:0] STATE_LOAD_WEIGHT = 3'd1;
|
| 88 |
+
localparam [2:0] STATE_CLEAR = 3'd2;
|
| 89 |
+
localparam [2:0] STATE_COMPUTE = 3'd3;
|
| 90 |
+
localparam [2:0] STATE_DRAIN = 3'd4;
|
| 91 |
+
localparam [2:0] STATE_DONE = 3'd5;
|
| 92 |
+
|
| 93 |
+
reg [2:0] state, next_state;
|
| 94 |
+
|
| 95 |
+
//--------------------------------------------------------------------------
|
| 96 |
+
// Counters
|
| 97 |
+
//--------------------------------------------------------------------------
|
| 98 |
+
reg [ROW_SEL_WIDTH-1:0] weight_row_cnt; // Current weight row being loaded
|
| 99 |
+
reg [K_CNT_WIDTH-1:0] k_cnt; // K dimension counter (weight columns / act rows)
|
| 100 |
+
reg [$clog2(DRAIN_CYCLES+1)-1:0] drain_cnt; // Drain cycle counter
|
| 101 |
+
|
| 102 |
+
//--------------------------------------------------------------------------
|
| 103 |
+
// Internal control signals
|
| 104 |
+
//--------------------------------------------------------------------------
|
| 105 |
+
reg sa_enable; // Systolic array enable
|
| 106 |
+
reg sa_clear_acc; // Clear accumulators
|
| 107 |
+
reg [ARRAY_COLS-1:0] sa_load_weight; // Load weight per column
|
| 108 |
+
|
| 109 |
+
//--------------------------------------------------------------------------
|
| 110 |
+
// Input skewing registers (delay lines for each row)
|
| 111 |
+
//--------------------------------------------------------------------------
|
| 112 |
+
reg [DATA_WIDTH-1:0] act_skew [ARRAY_ROWS-1:0][ARRAY_ROWS-1:0]; // Skew delay lines
|
| 113 |
+
wire [ARRAY_ROWS*DATA_WIDTH-1:0] act_skewed; // Skewed activation data
|
| 114 |
+
|
| 115 |
+
//--------------------------------------------------------------------------
|
| 116 |
+
// Output de-skewing registers
|
| 117 |
+
//--------------------------------------------------------------------------
|
| 118 |
+
reg [ACC_WIDTH-1:0] result_deskew [ARRAY_COLS-1:0][ARRAY_COLS-1:0];
|
| 119 |
+
reg [ARRAY_COLS-1:0] result_deskew_valid;
|
| 120 |
+
|
| 121 |
+
//--------------------------------------------------------------------------
|
| 122 |
+
// Systolic array instance
|
| 123 |
+
//--------------------------------------------------------------------------
|
| 124 |
+
wire [ARRAY_COLS*ACC_WIDTH-1:0] sa_psum_out;
|
| 125 |
+
|
| 126 |
+
systolic_array #(
|
| 127 |
+
.ARRAY_ROWS (ARRAY_ROWS),
|
| 128 |
+
.ARRAY_COLS (ARRAY_COLS),
|
| 129 |
+
.DATA_WIDTH (DATA_WIDTH),
|
| 130 |
+
.WEIGHT_WIDTH (WEIGHT_WIDTH),
|
| 131 |
+
.ACC_WIDTH (ACC_WIDTH),
|
| 132 |
+
.SIGNED_MATH (SIGNED_MATH)
|
| 133 |
+
) u_systolic_array (
|
| 134 |
+
.clk (clk),
|
| 135 |
+
.rst_n (rst_n),
|
| 136 |
+
|
| 137 |
+
.enable (sa_enable),
|
| 138 |
+
.clear_acc (sa_clear_acc),
|
| 139 |
+
.load_weight (sa_load_weight),
|
| 140 |
+
.weight_row_sel (weight_row_cnt),
|
| 141 |
+
|
| 142 |
+
.act_in (INPUT_SKEW ? act_skewed : act_data),
|
| 143 |
+
.weight_in (weight_data),
|
| 144 |
+
.psum_in ({ARRAY_COLS*ACC_WIDTH{1'b0}}), // No cascading
|
| 145 |
+
.psum_out (sa_psum_out),
|
| 146 |
+
.act_out () // Not used
|
| 147 |
+
);
|
| 148 |
+
|
| 149 |
+
//--------------------------------------------------------------------------
|
| 150 |
+
// FSM: State register
|
| 151 |
+
//--------------------------------------------------------------------------
|
| 152 |
+
always @(posedge clk or negedge rst_n) begin
|
| 153 |
+
if (!rst_n) begin
|
| 154 |
+
state <= STATE_IDLE;
|
| 155 |
+
end else begin
|
| 156 |
+
state <= next_state;
|
| 157 |
+
end
|
| 158 |
+
end
|
| 159 |
+
|
| 160 |
+
//--------------------------------------------------------------------------
|
| 161 |
+
// FSM: Next state logic
|
| 162 |
+
//--------------------------------------------------------------------------
|
| 163 |
+
always @(*) begin
|
| 164 |
+
next_state = state;
|
| 165 |
+
|
| 166 |
+
case (state)
|
| 167 |
+
STATE_IDLE: begin
|
| 168 |
+
if (start) begin
|
| 169 |
+
next_state = STATE_LOAD_WEIGHT;
|
| 170 |
+
end
|
| 171 |
+
end
|
| 172 |
+
|
| 173 |
+
STATE_LOAD_WEIGHT: begin
|
| 174 |
+
// Load K rows of weights (one row per column configuration)
|
| 175 |
+
if (weight_valid && (weight_row_cnt == ARRAY_ROWS - 1) && (k_cnt == K_DIM - 1)) begin
|
| 176 |
+
next_state = STATE_CLEAR;
|
| 177 |
+
end
|
| 178 |
+
end
|
| 179 |
+
|
| 180 |
+
STATE_CLEAR: begin
|
| 181 |
+
// Single cycle to clear accumulators
|
| 182 |
+
next_state = STATE_COMPUTE;
|
| 183 |
+
end
|
| 184 |
+
|
| 185 |
+
STATE_COMPUTE: begin
|
| 186 |
+
// Stream activations for K cycles
|
| 187 |
+
if (act_valid && (k_cnt == K_DIM - 1)) begin
|
| 188 |
+
next_state = STATE_DRAIN;
|
| 189 |
+
end
|
| 190 |
+
end
|
| 191 |
+
|
| 192 |
+
STATE_DRAIN: begin
|
| 193 |
+
// Wait for results to propagate through array
|
| 194 |
+
if (drain_cnt >= DRAIN_CYCLES - 1) begin
|
| 195 |
+
next_state = STATE_DONE;
|
| 196 |
+
end
|
| 197 |
+
end
|
| 198 |
+
|
| 199 |
+
STATE_DONE: begin
|
| 200 |
+
next_state = STATE_IDLE;
|
| 201 |
+
end
|
| 202 |
+
|
| 203 |
+
default: begin
|
| 204 |
+
next_state = STATE_IDLE;
|
| 205 |
+
end
|
| 206 |
+
endcase
|
| 207 |
+
end
|
| 208 |
+
|
| 209 |
+
//--------------------------------------------------------------------------
|
| 210 |
+
// FSM: Output and control logic
|
| 211 |
+
//--------------------------------------------------------------------------
|
| 212 |
+
always @(posedge clk or negedge rst_n) begin
|
| 213 |
+
if (!rst_n) begin
|
| 214 |
+
busy <= 1'b0;
|
| 215 |
+
done <= 1'b0;
|
| 216 |
+
weight_row_cnt <= {ROW_SEL_WIDTH{1'b0}};
|
| 217 |
+
k_cnt <= {K_CNT_WIDTH{1'b0}};
|
| 218 |
+
drain_cnt <= 0;
|
| 219 |
+
sa_enable <= 1'b0;
|
| 220 |
+
sa_clear_acc <= 1'b0;
|
| 221 |
+
sa_load_weight <= {ARRAY_COLS{1'b0}};
|
| 222 |
+
end else begin
|
| 223 |
+
// Default values
|
| 224 |
+
done <= 1'b0;
|
| 225 |
+
sa_clear_acc <= 1'b0;
|
| 226 |
+
sa_load_weight <= {ARRAY_COLS{1'b0}};
|
| 227 |
+
|
| 228 |
+
case (state)
|
| 229 |
+
STATE_IDLE: begin
|
| 230 |
+
busy <= 1'b0;
|
| 231 |
+
weight_row_cnt <= {ROW_SEL_WIDTH{1'b0}};
|
| 232 |
+
k_cnt <= {K_CNT_WIDTH{1'b0}};
|
| 233 |
+
drain_cnt <= 0;
|
| 234 |
+
sa_enable <= 1'b0;
|
| 235 |
+
if (start) begin
|
| 236 |
+
busy <= 1'b1;
|
| 237 |
+
end
|
| 238 |
+
end
|
| 239 |
+
|
| 240 |
+
STATE_LOAD_WEIGHT: begin
|
| 241 |
+
busy <= 1'b1;
|
| 242 |
+
if (weight_valid) begin
|
| 243 |
+
sa_load_weight <= {ARRAY_COLS{1'b1}}; // Load all columns
|
| 244 |
+
if (weight_row_cnt == ARRAY_ROWS - 1) begin
|
| 245 |
+
weight_row_cnt <= {ROW_SEL_WIDTH{1'b0}};
|
| 246 |
+
k_cnt <= k_cnt + 1'b1;
|
| 247 |
+
end else begin
|
| 248 |
+
weight_row_cnt <= weight_row_cnt + 1'b1;
|
| 249 |
+
end
|
| 250 |
+
end
|
| 251 |
+
end
|
| 252 |
+
|
| 253 |
+
STATE_CLEAR: begin
|
| 254 |
+
busy <= 1'b1;
|
| 255 |
+
sa_clear_acc <= 1'b1;
|
| 256 |
+
sa_enable <= 1'b1;
|
| 257 |
+
k_cnt <= {K_CNT_WIDTH{1'b0}};
|
| 258 |
+
end
|
| 259 |
+
|
| 260 |
+
STATE_COMPUTE: begin
|
| 261 |
+
busy <= 1'b1;
|
| 262 |
+
sa_enable <= 1'b1;
|
| 263 |
+
if (act_valid) begin
|
| 264 |
+
k_cnt <= k_cnt + 1'b1;
|
| 265 |
+
end
|
| 266 |
+
end
|
| 267 |
+
|
| 268 |
+
STATE_DRAIN: begin
|
| 269 |
+
busy <= 1'b1;
|
| 270 |
+
sa_enable <= 1'b1;
|
| 271 |
+
drain_cnt <= drain_cnt + 1'b1;
|
| 272 |
+
end
|
| 273 |
+
|
| 274 |
+
STATE_DONE: begin
|
| 275 |
+
busy <= 1'b0;
|
| 276 |
+
done <= 1'b1;
|
| 277 |
+
sa_enable <= 1'b0;
|
| 278 |
+
end
|
| 279 |
+
|
| 280 |
+
default: begin
|
| 281 |
+
busy <= 1'b0;
|
| 282 |
+
end
|
| 283 |
+
endcase
|
| 284 |
+
end
|
| 285 |
+
end
|
| 286 |
+
|
| 287 |
+
//--------------------------------------------------------------------------
|
| 288 |
+
// Input skewing logic
|
| 289 |
+
// Row 0: no delay, Row 1: 1 cycle delay, Row N: N cycles delay
|
| 290 |
+
//--------------------------------------------------------------------------
|
| 291 |
+
generate
|
| 292 |
+
if (INPUT_SKEW == 1) begin : gen_input_skew
|
| 293 |
+
genvar r, d;
|
| 294 |
+
|
| 295 |
+
for (r = 0; r < ARRAY_ROWS; r = r + 1) begin : gen_skew_row
|
| 296 |
+
if (r == 0) begin : gen_no_skew
|
| 297 |
+
// Row 0: direct connection (no skew)
|
| 298 |
+
assign act_skewed[(r+1)*DATA_WIDTH-1 -: DATA_WIDTH] =
|
| 299 |
+
act_data[(r+1)*DATA_WIDTH-1 -: DATA_WIDTH];
|
| 300 |
+
end else begin : gen_skew
|
| 301 |
+
// Row r: r cycles of delay
|
| 302 |
+
always @(posedge clk or negedge rst_n) begin
|
| 303 |
+
if (!rst_n) begin
|
| 304 |
+
for (int i = 0; i < r; i = i + 1) begin
|
| 305 |
+
act_skew[r][i] <= {DATA_WIDTH{1'b0}};
|
| 306 |
+
end
|
| 307 |
+
end else if (sa_enable) begin
|
| 308 |
+
// Shift register chain
|
| 309 |
+
act_skew[r][0] <= act_data[(r+1)*DATA_WIDTH-1 -: DATA_WIDTH];
|
| 310 |
+
for (int i = 1; i < r; i = i + 1) begin
|
| 311 |
+
act_skew[r][i] <= act_skew[r][i-1];
|
| 312 |
+
end
|
| 313 |
+
end
|
| 314 |
+
end
|
| 315 |
+
|
| 316 |
+
assign act_skewed[(r+1)*DATA_WIDTH-1 -: DATA_WIDTH] = act_skew[r][r-1];
|
| 317 |
+
end
|
| 318 |
+
end
|
| 319 |
+
end else begin : gen_no_input_skew
|
| 320 |
+
assign act_skewed = act_data;
|
| 321 |
+
end
|
| 322 |
+
endgenerate
|
| 323 |
+
|
| 324 |
+
//--------------------------------------------------------------------------
|
| 325 |
+
// Result collection
|
| 326 |
+
//--------------------------------------------------------------------------
|
| 327 |
+
// In weight-stationary mode, results emerge from the bottom of the array
|
| 328 |
+
// after all K activations have been processed. Results need de-skewing
|
| 329 |
+
// similar to input skewing (reversed).
|
| 330 |
+
|
| 331 |
+
// Simplified: direct output from bottom row
|
| 332 |
+
assign result_data = sa_psum_out;
|
| 333 |
+
|
| 334 |
+
always @(posedge clk or negedge rst_n) begin
|
| 335 |
+
if (!rst_n) begin
|
| 336 |
+
result_valid <= 1'b0;
|
| 337 |
+
end else begin
|
| 338 |
+
// Results are valid during drain phase, starting after initial propagation
|
| 339 |
+
result_valid <= (state == STATE_DRAIN) && (drain_cnt >= ARRAY_ROWS);
|
| 340 |
+
end
|
| 341 |
+
end
|
| 342 |
+
|
| 343 |
+
//--------------------------------------------------------------------------
|
| 344 |
+
// Handshake signals
|
| 345 |
+
//--------------------------------------------------------------------------
|
| 346 |
+
assign weight_ready = (state == STATE_LOAD_WEIGHT);
|
| 347 |
+
assign act_ready = (state == STATE_COMPUTE);
|
| 348 |
+
|
| 349 |
+
endmodule
|
Weiyet_RTLStructLib/source/Systolic_Array/tb/cocotb/Makefile
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SIM ?= icarus
|
| 2 |
+
TOPLEVEL_LANG ?= verilog
|
| 3 |
+
WAVES ?= 1 #set 1 to enable waveform dump.
|
| 4 |
+
|
| 5 |
+
PWD=$(shell pwd)
|
| 6 |
+
|
| 7 |
+
VERILOG_SOURCES = $(PWD)/../../src/pe.sv
|
| 8 |
+
VERILOG_SOURCES += $(PWD)/../../src/systolic_array.sv
|
| 9 |
+
VERILOG_SOURCES += $(PWD)/../../src/systolic_array_top.sv
|
| 10 |
+
|
| 11 |
+
TOPLEVEL = systolic_array_top # DUT Top
|
| 12 |
+
MODULE = tb # top python file
|
| 13 |
+
|
| 14 |
+
# Use , separator to run multiple TESTCASE, by default all @cocotb.test will be run
|
| 15 |
+
#TESTCASE = test_simple_values,test_identity_matrix,test_random_matrices
|
| 16 |
+
#TESTCASE = test_simple_values
|
| 17 |
+
|
| 18 |
+
# DUT parameters - use "-p" iverilog command flags
|
| 19 |
+
COMPILE_ARGS = -Psystolic_array_top.ARRAY_ROWS=4
|
| 20 |
+
COMPILE_ARGS += -Psystolic_array_top.ARRAY_COLS=4
|
| 21 |
+
COMPILE_ARGS += -Psystolic_array_top.K_DIM=4
|
| 22 |
+
COMPILE_ARGS += -Psystolic_array_top.DATA_WIDTH=8
|
| 23 |
+
COMPILE_ARGS += -Psystolic_array_top.WEIGHT_WIDTH=8
|
| 24 |
+
COMPILE_ARGS += -Psystolic_array_top.ACC_WIDTH=32
|
| 25 |
+
COMPILE_ARGS += -Psystolic_array_top.SIGNED_MATH=1
|
| 26 |
+
COMPILE_ARGS += -Psystolic_array_top.INPUT_SKEW=1
|
| 27 |
+
# Run make clean before running with new parameters.
|
| 28 |
+
|
| 29 |
+
# Set RANDOM_SEED number
|
| 30 |
+
#PLUSARGS = +seed=1
|
| 31 |
+
|
| 32 |
+
COCOTB_HDL_TIMEUNIT = 1ns
|
| 33 |
+
COCOTB_HDL_TIMEPRECISION = 1ps
|
| 34 |
+
|
| 35 |
+
ifeq ($(SIM), icarus)
|
| 36 |
+
$(shell echo 'module iverilog_dump();' > iverilog_dump.v)
|
| 37 |
+
$(shell echo 'initial begin' >> iverilog_dump.v)
|
| 38 |
+
$(shell echo ' $$dumpfile("$(TOPLEVEL).vcd");' >> iverilog_dump.v)
|
| 39 |
+
$(shell echo ' $$dumpvars(0, $(TOPLEVEL));' >> iverilog_dump.v)
|
| 40 |
+
$(shell echo 'end' >> iverilog_dump.v)
|
| 41 |
+
$(shell echo 'endmodule' >> iverilog_dump.v)
|
| 42 |
+
VERILOG_SOURCES += $(PWD)/iverilog_dump.v
|
| 43 |
+
COMPILE_ARGS += -s iverilog_dump
|
| 44 |
+
endif
|
| 45 |
+
|
| 46 |
+
include $(shell cocotb-config --makefiles)/Makefile.sim
|
Weiyet_RTLStructLib/source/Systolic_Array/tb/cocotb/tb.py
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Cocotb testbench for Systolic Array matrix multiplication.
|
| 3 |
+
|
| 4 |
+
Tests:
|
| 5 |
+
1. Simple matrix multiplication with known values
|
| 6 |
+
2. Identity matrix multiplication
|
| 7 |
+
3. Random matrix multiplication
|
| 8 |
+
4. Edge cases (zeros, signed values)
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import random
|
| 12 |
+
import cocotb
|
| 13 |
+
from cocotb.triggers import Timer, RisingEdge, FallingEdge
|
| 14 |
+
from cocotb.clock import Clock
|
| 15 |
+
|
| 16 |
+
# Default parameters (overridden by DUT parameters)
|
| 17 |
+
ARRAY_ROWS = 4
|
| 18 |
+
ARRAY_COLS = 4
|
| 19 |
+
K_DIM = 4
|
| 20 |
+
DATA_WIDTH = 8
|
| 21 |
+
WEIGHT_WIDTH = 8
|
| 22 |
+
ACC_WIDTH = 32
|
| 23 |
+
SIGNED_MATH = 1
|
| 24 |
+
|
| 25 |
+
CLK_PERIOD = 10 # ns
|
| 26 |
+
err_cnt = 0
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def signed_value(val, width):
|
| 30 |
+
"""Convert unsigned to signed value."""
|
| 31 |
+
if val >= (1 << (width - 1)):
|
| 32 |
+
return val - (1 << width)
|
| 33 |
+
return val
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def to_unsigned(val, width):
|
| 37 |
+
"""Convert signed to unsigned value."""
|
| 38 |
+
if val < 0:
|
| 39 |
+
return val + (1 << width)
|
| 40 |
+
return val
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
async def dut_init(dut):
|
| 44 |
+
"""Initialize DUT and start clock."""
|
| 45 |
+
global ARRAY_ROWS, ARRAY_COLS, K_DIM, DATA_WIDTH, WEIGHT_WIDTH, ACC_WIDTH, SIGNED_MATH
|
| 46 |
+
|
| 47 |
+
# Read parameters from DUT
|
| 48 |
+
ARRAY_ROWS = dut.ARRAY_ROWS.value
|
| 49 |
+
ARRAY_COLS = dut.ARRAY_COLS.value
|
| 50 |
+
K_DIM = dut.K_DIM.value
|
| 51 |
+
DATA_WIDTH = dut.DATA_WIDTH.value
|
| 52 |
+
WEIGHT_WIDTH = dut.WEIGHT_WIDTH.value
|
| 53 |
+
ACC_WIDTH = dut.ACC_WIDTH.value
|
| 54 |
+
SIGNED_MATH = dut.SIGNED_MATH.value
|
| 55 |
+
|
| 56 |
+
dut._log.info(f"Array Size: {ARRAY_ROWS}x{ARRAY_COLS}, K={K_DIM}")
|
| 57 |
+
dut._log.info(f"Data Width: {DATA_WIDTH}, Weight Width: {WEIGHT_WIDTH}, Acc Width: {ACC_WIDTH}")
|
| 58 |
+
dut._log.info(f"Signed Math: {SIGNED_MATH}")
|
| 59 |
+
|
| 60 |
+
# Start clock
|
| 61 |
+
await cocotb.start(Clock(dut.clk, CLK_PERIOD, units="ns").start())
|
| 62 |
+
|
| 63 |
+
# Initialize signals
|
| 64 |
+
dut.rst_n.value = 0
|
| 65 |
+
dut.start.value = 0
|
| 66 |
+
dut.weight_data.value = 0
|
| 67 |
+
dut.weight_valid.value = 0
|
| 68 |
+
dut.act_data.value = 0
|
| 69 |
+
dut.act_valid.value = 0
|
| 70 |
+
dut.result_ready.value = 0
|
| 71 |
+
|
| 72 |
+
# Reset sequence
|
| 73 |
+
await Timer(100, 'ns')
|
| 74 |
+
dut.rst_n.value = 1
|
| 75 |
+
await Timer(100, 'ns')
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def compute_expected_result(matrix_a, matrix_b):
|
| 79 |
+
"""Compute expected matrix multiplication result."""
|
| 80 |
+
result = [[0] * ARRAY_COLS for _ in range(ARRAY_ROWS)]
|
| 81 |
+
|
| 82 |
+
for i in range(ARRAY_ROWS):
|
| 83 |
+
for j in range(ARRAY_COLS):
|
| 84 |
+
acc = 0
|
| 85 |
+
for k in range(K_DIM):
|
| 86 |
+
if SIGNED_MATH:
|
| 87 |
+
a_val = signed_value(matrix_a[i][k], DATA_WIDTH)
|
| 88 |
+
b_val = signed_value(matrix_b[k][j], WEIGHT_WIDTH)
|
| 89 |
+
else:
|
| 90 |
+
a_val = matrix_a[i][k]
|
| 91 |
+
b_val = matrix_b[k][j]
|
| 92 |
+
acc += a_val * b_val
|
| 93 |
+
result[i][j] = acc
|
| 94 |
+
|
| 95 |
+
return result
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
async def load_weights(dut, matrix_b):
|
| 99 |
+
"""Load weight matrix into systolic array."""
|
| 100 |
+
dut._log.info("Loading weights...")
|
| 101 |
+
|
| 102 |
+
# Wait for weight_ready
|
| 103 |
+
while dut.weight_ready.value != 1:
|
| 104 |
+
await RisingEdge(dut.clk)
|
| 105 |
+
|
| 106 |
+
# Load weights: K dimension outer, rows inner
|
| 107 |
+
for k in range(K_DIM):
|
| 108 |
+
for row in range(ARRAY_ROWS):
|
| 109 |
+
await RisingEdge(dut.clk)
|
| 110 |
+
|
| 111 |
+
# Pack all columns
|
| 112 |
+
weight_packed = 0
|
| 113 |
+
for col in range(ARRAY_COLS):
|
| 114 |
+
weight_packed |= (matrix_b[k][col] & ((1 << WEIGHT_WIDTH) - 1)) << (col * WEIGHT_WIDTH)
|
| 115 |
+
|
| 116 |
+
dut.weight_data.value = weight_packed
|
| 117 |
+
dut.weight_valid.value = 1
|
| 118 |
+
|
| 119 |
+
await RisingEdge(dut.clk)
|
| 120 |
+
dut.weight_valid.value = 0
|
| 121 |
+
dut.weight_data.value = 0
|
| 122 |
+
dut._log.info("Weight loading complete")
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
async def stream_activations(dut, matrix_a):
|
| 126 |
+
"""Stream activation matrix into systolic array."""
|
| 127 |
+
dut._log.info("Streaming activations...")
|
| 128 |
+
|
| 129 |
+
# Wait for act_ready
|
| 130 |
+
while dut.act_ready.value != 1:
|
| 131 |
+
await RisingEdge(dut.clk)
|
| 132 |
+
|
| 133 |
+
# Stream column by column (K dimension)
|
| 134 |
+
for k in range(K_DIM):
|
| 135 |
+
await RisingEdge(dut.clk)
|
| 136 |
+
|
| 137 |
+
# Pack all rows
|
| 138 |
+
act_packed = 0
|
| 139 |
+
for row in range(ARRAY_ROWS):
|
| 140 |
+
act_packed |= (matrix_a[row][k] & ((1 << DATA_WIDTH) - 1)) << (row * DATA_WIDTH)
|
| 141 |
+
|
| 142 |
+
dut.act_data.value = act_packed
|
| 143 |
+
dut.act_valid.value = 1
|
| 144 |
+
|
| 145 |
+
await RisingEdge(dut.clk)
|
| 146 |
+
dut.act_valid.value = 0
|
| 147 |
+
dut.act_data.value = 0
|
| 148 |
+
dut._log.info("Activation streaming complete")
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
async def collect_results(dut):
|
| 152 |
+
"""Collect results from systolic array."""
|
| 153 |
+
dut._log.info("Collecting results...")
|
| 154 |
+
|
| 155 |
+
results = []
|
| 156 |
+
dut.result_ready.value = 1
|
| 157 |
+
timeout = 0
|
| 158 |
+
|
| 159 |
+
while dut.done.value != 1 and timeout < 1000:
|
| 160 |
+
await RisingEdge(dut.clk)
|
| 161 |
+
timeout += 1
|
| 162 |
+
|
| 163 |
+
if dut.result_valid.value == 1:
|
| 164 |
+
# Unpack result row
|
| 165 |
+
result_packed = dut.result_data.value.integer
|
| 166 |
+
row_result = []
|
| 167 |
+
for col in range(ARRAY_COLS):
|
| 168 |
+
val = (result_packed >> (col * ACC_WIDTH)) & ((1 << ACC_WIDTH) - 1)
|
| 169 |
+
if SIGNED_MATH:
|
| 170 |
+
val = signed_value(val, ACC_WIDTH)
|
| 171 |
+
row_result.append(val)
|
| 172 |
+
results.append(row_result)
|
| 173 |
+
dut._log.info(f"Received result row: {row_result}")
|
| 174 |
+
|
| 175 |
+
dut.result_ready.value = 0
|
| 176 |
+
dut._log.info("Result collection complete")
|
| 177 |
+
return results
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
async def run_matmul_test(dut, matrix_a, matrix_b, test_name):
|
| 181 |
+
"""Run a single matrix multiplication test."""
|
| 182 |
+
global err_cnt
|
| 183 |
+
|
| 184 |
+
dut._log.info(f"\n{'='*50}")
|
| 185 |
+
dut._log.info(f"Test: {test_name}")
|
| 186 |
+
dut._log.info(f"{'='*50}")
|
| 187 |
+
|
| 188 |
+
# Print matrices
|
| 189 |
+
dut._log.info(f"Matrix A ({ARRAY_ROWS}x{K_DIM}):")
|
| 190 |
+
for row in matrix_a:
|
| 191 |
+
if SIGNED_MATH:
|
| 192 |
+
dut._log.info(f" {[signed_value(v, DATA_WIDTH) for v in row]}")
|
| 193 |
+
else:
|
| 194 |
+
dut._log.info(f" {row}")
|
| 195 |
+
|
| 196 |
+
dut._log.info(f"Matrix B ({K_DIM}x{ARRAY_COLS}):")
|
| 197 |
+
for row in matrix_b:
|
| 198 |
+
if SIGNED_MATH:
|
| 199 |
+
dut._log.info(f" {[signed_value(v, WEIGHT_WIDTH) for v in row]}")
|
| 200 |
+
else:
|
| 201 |
+
dut._log.info(f" {row}")
|
| 202 |
+
|
| 203 |
+
# Compute expected result
|
| 204 |
+
expected = compute_expected_result(matrix_a, matrix_b)
|
| 205 |
+
dut._log.info(f"Expected C ({ARRAY_ROWS}x{ARRAY_COLS}):")
|
| 206 |
+
for row in expected:
|
| 207 |
+
dut._log.info(f" {row}")
|
| 208 |
+
|
| 209 |
+
# Start computation
|
| 210 |
+
await RisingEdge(dut.clk)
|
| 211 |
+
dut.start.value = 1
|
| 212 |
+
await RisingEdge(dut.clk)
|
| 213 |
+
dut.start.value = 0
|
| 214 |
+
|
| 215 |
+
# Wait for busy
|
| 216 |
+
while dut.busy.value != 1:
|
| 217 |
+
await RisingEdge(dut.clk)
|
| 218 |
+
|
| 219 |
+
# Run load, stream, and collect concurrently
|
| 220 |
+
await load_weights(dut, matrix_b)
|
| 221 |
+
await stream_activations(dut, matrix_a)
|
| 222 |
+
actual = await collect_results(dut)
|
| 223 |
+
|
| 224 |
+
# Wait for done
|
| 225 |
+
while dut.done.value != 1:
|
| 226 |
+
await RisingEdge(dut.clk)
|
| 227 |
+
|
| 228 |
+
# Compare results
|
| 229 |
+
local_err = 0
|
| 230 |
+
dut._log.info("\n--- Result Comparison ---")
|
| 231 |
+
|
| 232 |
+
if len(actual) > 0:
|
| 233 |
+
for i in range(min(len(actual), ARRAY_ROWS)):
|
| 234 |
+
for j in range(ARRAY_COLS):
|
| 235 |
+
if actual[i][j] != expected[i][j]:
|
| 236 |
+
dut._log.error(f"C[{i}][{j}] mismatch: Expected {expected[i][j]}, Got {actual[i][j]}")
|
| 237 |
+
local_err += 1
|
| 238 |
+
err_cnt += 1
|
| 239 |
+
else:
|
| 240 |
+
dut._log.warning("No results collected")
|
| 241 |
+
|
| 242 |
+
if local_err == 0:
|
| 243 |
+
dut._log.info(f"Test '{test_name}': PASSED")
|
| 244 |
+
else:
|
| 245 |
+
dut._log.error(f"Test '{test_name}': FAILED with {local_err} errors")
|
| 246 |
+
|
| 247 |
+
# Wait between tests
|
| 248 |
+
for _ in range(10):
|
| 249 |
+
await RisingEdge(dut.clk)
|
| 250 |
+
|
| 251 |
+
return local_err == 0
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
def generate_simple_matrices():
|
| 255 |
+
"""Generate simple test matrices with known values."""
|
| 256 |
+
matrix_a = [[i * K_DIM + k + 1 for k in range(K_DIM)] for i in range(ARRAY_ROWS)]
|
| 257 |
+
matrix_b = [[k * ARRAY_COLS + j + 1 for j in range(ARRAY_COLS)] for k in range(K_DIM)]
|
| 258 |
+
|
| 259 |
+
# Convert to unsigned representation
|
| 260 |
+
for i in range(ARRAY_ROWS):
|
| 261 |
+
for k in range(K_DIM):
|
| 262 |
+
matrix_a[i][k] = to_unsigned(matrix_a[i][k], DATA_WIDTH) if matrix_a[i][k] < 0 else matrix_a[i][k]
|
| 263 |
+
|
| 264 |
+
for k in range(K_DIM):
|
| 265 |
+
for j in range(ARRAY_COLS):
|
| 266 |
+
matrix_b[k][j] = to_unsigned(matrix_b[k][j], WEIGHT_WIDTH) if matrix_b[k][j] < 0 else matrix_b[k][j]
|
| 267 |
+
|
| 268 |
+
return matrix_a, matrix_b
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def generate_identity_matrices():
|
| 272 |
+
"""Generate identity matrix test."""
|
| 273 |
+
matrix_a = [[random.randint(1, 10) for _ in range(K_DIM)] for _ in range(ARRAY_ROWS)]
|
| 274 |
+
matrix_b = [[1 if k == j else 0 for j in range(ARRAY_COLS)] for k in range(K_DIM)]
|
| 275 |
+
return matrix_a, matrix_b
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def generate_random_matrices():
|
| 279 |
+
"""Generate random test matrices."""
|
| 280 |
+
max_val = (1 << DATA_WIDTH) - 1
|
| 281 |
+
matrix_a = [[random.randint(0, max_val) for _ in range(K_DIM)] for _ in range(ARRAY_ROWS)]
|
| 282 |
+
matrix_b = [[random.randint(0, max_val) for _ in range(ARRAY_COLS)] for _ in range(K_DIM)]
|
| 283 |
+
return matrix_a, matrix_b
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
def generate_zero_matrices():
|
| 287 |
+
"""Generate zero matrix test."""
|
| 288 |
+
matrix_a = [[0 for _ in range(K_DIM)] for _ in range(ARRAY_ROWS)]
|
| 289 |
+
matrix_b = [[random.randint(1, 10) for _ in range(ARRAY_COLS)] for _ in range(K_DIM)]
|
| 290 |
+
return matrix_a, matrix_b
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def generate_signed_matrices():
|
| 294 |
+
"""Generate matrices with positive and negative values."""
|
| 295 |
+
matrix_a = []
|
| 296 |
+
for i in range(ARRAY_ROWS):
|
| 297 |
+
row = []
|
| 298 |
+
for k in range(K_DIM):
|
| 299 |
+
val = (k + 1) if ((i + k) % 2 == 0) else -(k + 1)
|
| 300 |
+
row.append(to_unsigned(val, DATA_WIDTH))
|
| 301 |
+
matrix_a.append(row)
|
| 302 |
+
|
| 303 |
+
matrix_b = []
|
| 304 |
+
for k in range(K_DIM):
|
| 305 |
+
row = []
|
| 306 |
+
for j in range(ARRAY_COLS):
|
| 307 |
+
val = (j + 1) if ((k + j) % 2 == 0) else -(j + 1)
|
| 308 |
+
row.append(to_unsigned(val, WEIGHT_WIDTH))
|
| 309 |
+
matrix_b.append(row)
|
| 310 |
+
|
| 311 |
+
return matrix_a, matrix_b
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
@cocotb.test()
|
| 315 |
+
async def test_simple_values(dut):
|
| 316 |
+
"""Test with simple known values."""
|
| 317 |
+
await dut_init(dut)
|
| 318 |
+
matrix_a, matrix_b = generate_simple_matrices()
|
| 319 |
+
await run_matmul_test(dut, matrix_a, matrix_b, "Simple Known Values")
|
| 320 |
+
|
| 321 |
+
if err_cnt > 0:
|
| 322 |
+
raise cocotb.result.TestFailure(f"Test failed with {err_cnt} errors")
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
@cocotb.test()
|
| 326 |
+
async def test_identity_matrix(dut):
|
| 327 |
+
"""Test with identity matrix."""
|
| 328 |
+
global err_cnt
|
| 329 |
+
err_cnt = 0
|
| 330 |
+
await dut_init(dut)
|
| 331 |
+
matrix_a, matrix_b = generate_identity_matrices()
|
| 332 |
+
await run_matmul_test(dut, matrix_a, matrix_b, "Identity Matrix")
|
| 333 |
+
|
| 334 |
+
if err_cnt > 0:
|
| 335 |
+
raise cocotb.result.TestFailure(f"Test failed with {err_cnt} errors")
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
@cocotb.test()
|
| 339 |
+
async def test_random_matrices(dut):
|
| 340 |
+
"""Test with random matrices."""
|
| 341 |
+
global err_cnt
|
| 342 |
+
err_cnt = 0
|
| 343 |
+
await dut_init(dut)
|
| 344 |
+
matrix_a, matrix_b = generate_random_matrices()
|
| 345 |
+
await run_matmul_test(dut, matrix_a, matrix_b, "Random Matrices")
|
| 346 |
+
|
| 347 |
+
if err_cnt > 0:
|
| 348 |
+
raise cocotb.result.TestFailure(f"Test failed with {err_cnt} errors")
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
@cocotb.test()
|
| 352 |
+
async def test_zero_matrix(dut):
|
| 353 |
+
"""Test with zero matrix."""
|
| 354 |
+
global err_cnt
|
| 355 |
+
err_cnt = 0
|
| 356 |
+
await dut_init(dut)
|
| 357 |
+
matrix_a, matrix_b = generate_zero_matrices()
|
| 358 |
+
await run_matmul_test(dut, matrix_a, matrix_b, "Zero Matrix")
|
| 359 |
+
|
| 360 |
+
if err_cnt > 0:
|
| 361 |
+
raise cocotb.result.TestFailure(f"Test failed with {err_cnt} errors")
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
@cocotb.test()
|
| 365 |
+
async def test_signed_values(dut):
|
| 366 |
+
"""Test with signed values."""
|
| 367 |
+
global err_cnt
|
| 368 |
+
err_cnt = 0
|
| 369 |
+
await dut_init(dut)
|
| 370 |
+
|
| 371 |
+
if SIGNED_MATH:
|
| 372 |
+
matrix_a, matrix_b = generate_signed_matrices()
|
| 373 |
+
await run_matmul_test(dut, matrix_a, matrix_b, "Signed Values")
|
| 374 |
+
else:
|
| 375 |
+
dut._log.info("Skipping signed test (SIGNED_MATH=0)")
|
| 376 |
+
|
| 377 |
+
if err_cnt > 0:
|
| 378 |
+
raise cocotb.result.TestFailure(f"Test failed with {err_cnt} errors")
|
Weiyet_RTLStructLib/source/Systolic_Array/tb/sv/Makefile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DUT = systolic_array
|
| 2 |
+
SIM_OPTS ?= ../../src/pe.sv ../../src/systolic_array.sv ../../src/systolic_array_top.sv
|
| 3 |
+
SEED ?= $$(shuf -i 1-10000 -n 1)
|
| 4 |
+
XILINX_LIB_URL ?= https://github.com/Xilinx/XilinxUnisimLibrary.git
|
| 5 |
+
XILINX_LIB_DIR ?= XilinxUnisimLibrary/verilog/src
|
| 6 |
+
|
| 7 |
+
sim: gen_sim
|
| 8 |
+
./${DUT}.sim +VCDFILE=sim.vcd +VCDLEVEL=0 +SEED=${SEED} | tee sim.log
|
| 9 |
+
|
| 10 |
+
gen_sim: tb.sv ${SIM_OPTS} # to generate executable file by using iverilog
|
| 11 |
+
iverilog -g2012 -s tb -o ${DUT}.sim $^
|
| 12 |
+
|
| 13 |
+
synth: ${SIM_OPTS}
|
| 14 |
+
yosys -p synth_xilinx ${SIM_OPTS} -L synth.log -o ${DUT}.netlist.v
|
| 15 |
+
|
| 16 |
+
netlist_sim: gen_netlist_sim
|
| 17 |
+
./${DUT}.netlist.sim +VCDFILE=netlist_sim.vcd +VCDLEVEL=0 +SEED=${SEED} | tee netlist_sim.log
|
| 18 |
+
|
| 19 |
+
gen_netlist_sim: tb.sv ${DUT}.netlist.v | ${XILINX_LIB_DIR}/.git
|
| 20 |
+
iverilog -g2012 -DXILINX_GLS -s tb -y ${XILINX_LIB_DIR} -y ${XILINX_LIB_DIR}/unisims -DXILINX_GLS -o ${DUT}.netlist.sim tb.sv ${DUT}.netlist.v
|
| 21 |
+
|
| 22 |
+
${XILINX_LIB_DIR}/.git:
|
| 23 |
+
git clone ${XILINX_LIB_URL}
|
| 24 |
+
|
| 25 |
+
clean:
|
| 26 |
+
rm -f ${DUT}.sim sim.log sim.vcd
|
| 27 |
+
rm -f ${DUT}.netlist.v synth.log
|
| 28 |
+
rm -f ${DUT}.netlist.sim netlist_sim.log netlist_sim.vcd
|
Weiyet_RTLStructLib/source/Systolic_Array/tb/sv/tb.sv
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
//
|
| 4 |
+
// Create Date: 2025
|
| 5 |
+
// Module Name: tb
|
| 6 |
+
// Description: Comprehensive testbench for systolic array matrix multiplication.
|
| 7 |
+
// Tests include:
|
| 8 |
+
// 1. Basic matrix multiplication with known values
|
| 9 |
+
// 2. Identity matrix multiplication
|
| 10 |
+
// 3. Random matrix multiplication
|
| 11 |
+
// 4. Edge cases (zeros, max values)
|
| 12 |
+
//
|
| 13 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 14 |
+
|
| 15 |
+
module tb();
|
| 16 |
+
|
| 17 |
+
//--------------------------------------------------------------------------
|
| 18 |
+
// Parameters
|
| 19 |
+
//--------------------------------------------------------------------------
|
| 20 |
+
parameter ARRAY_ROWS = 4; // PE rows
|
| 21 |
+
parameter ARRAY_COLS = 4; // PE columns
|
| 22 |
+
parameter K_DIM = 4; // Inner dimension
|
| 23 |
+
parameter DATA_WIDTH = 8; // Activation width
|
| 24 |
+
parameter WEIGHT_WIDTH = 8; // Weight width
|
| 25 |
+
parameter ACC_WIDTH = 32; // Accumulator width
|
| 26 |
+
parameter SIGNED_MATH = 1; // Signed arithmetic
|
| 27 |
+
parameter INPUT_SKEW = 1; // Auto-skew
|
| 28 |
+
|
| 29 |
+
parameter CLK_PERIOD = 10; // Clock period in ns
|
| 30 |
+
parameter SIM_TIMEOUT = 500000; // Simulation timeout
|
| 31 |
+
|
| 32 |
+
localparam MAX_SIGNED_VAL = (1 << (DATA_WIDTH-1)) - 1;
|
| 33 |
+
localparam MIN_SIGNED_VAL = -(1 << (DATA_WIDTH-1));
|
| 34 |
+
|
| 35 |
+
//--------------------------------------------------------------------------
|
| 36 |
+
// DUT signals
|
| 37 |
+
//--------------------------------------------------------------------------
|
| 38 |
+
reg clk;
|
| 39 |
+
reg rst_n;
|
| 40 |
+
reg start;
|
| 41 |
+
wire busy;
|
| 42 |
+
wire done;
|
| 43 |
+
|
| 44 |
+
reg [ARRAY_COLS*WEIGHT_WIDTH-1:0] weight_data;
|
| 45 |
+
reg weight_valid;
|
| 46 |
+
wire weight_ready;
|
| 47 |
+
|
| 48 |
+
reg [ARRAY_ROWS*DATA_WIDTH-1:0] act_data;
|
| 49 |
+
reg act_valid;
|
| 50 |
+
wire act_ready;
|
| 51 |
+
|
| 52 |
+
wire [ARRAY_COLS*ACC_WIDTH-1:0] result_data;
|
| 53 |
+
wire result_valid;
|
| 54 |
+
reg result_ready;
|
| 55 |
+
|
| 56 |
+
//--------------------------------------------------------------------------
|
| 57 |
+
// Test matrices
|
| 58 |
+
//--------------------------------------------------------------------------
|
| 59 |
+
// Matrix A: ARRAY_ROWS x K_DIM (activations)
|
| 60 |
+
reg signed [DATA_WIDTH-1:0] matrix_a [ARRAY_ROWS-1:0][K_DIM-1:0];
|
| 61 |
+
// Matrix B: K_DIM x ARRAY_COLS (weights) - stored transposed for easier loading
|
| 62 |
+
reg signed [WEIGHT_WIDTH-1:0] matrix_b [K_DIM-1:0][ARRAY_COLS-1:0];
|
| 63 |
+
// Expected result: ARRAY_ROWS x ARRAY_COLS
|
| 64 |
+
reg signed [ACC_WIDTH-1:0] expected_c [ARRAY_ROWS-1:0][ARRAY_COLS-1:0];
|
| 65 |
+
// Actual result from DUT
|
| 66 |
+
reg signed [ACC_WIDTH-1:0] actual_c [ARRAY_ROWS-1:0][ARRAY_COLS-1:0];
|
| 67 |
+
|
| 68 |
+
//--------------------------------------------------------------------------
|
| 69 |
+
// Test variables
|
| 70 |
+
//--------------------------------------------------------------------------
|
| 71 |
+
integer i, j, k;
|
| 72 |
+
integer err_cnt = 0;
|
| 73 |
+
integer test_num = 0;
|
| 74 |
+
integer result_row_cnt;
|
| 75 |
+
string test_name;
|
| 76 |
+
|
| 77 |
+
//--------------------------------------------------------------------------
|
| 78 |
+
// DUT instantiation
|
| 79 |
+
//--------------------------------------------------------------------------
|
| 80 |
+
systolic_array_top #(
|
| 81 |
+
.ARRAY_ROWS (ARRAY_ROWS),
|
| 82 |
+
.ARRAY_COLS (ARRAY_COLS),
|
| 83 |
+
.K_DIM (K_DIM),
|
| 84 |
+
.DATA_WIDTH (DATA_WIDTH),
|
| 85 |
+
.WEIGHT_WIDTH (WEIGHT_WIDTH),
|
| 86 |
+
.ACC_WIDTH (ACC_WIDTH),
|
| 87 |
+
.SIGNED_MATH (SIGNED_MATH),
|
| 88 |
+
.INPUT_SKEW (INPUT_SKEW)
|
| 89 |
+
) DUT (
|
| 90 |
+
.clk (clk),
|
| 91 |
+
.rst_n (rst_n),
|
| 92 |
+
.start (start),
|
| 93 |
+
.busy (busy),
|
| 94 |
+
.done (done),
|
| 95 |
+
.weight_data (weight_data),
|
| 96 |
+
.weight_valid (weight_valid),
|
| 97 |
+
.weight_ready (weight_ready),
|
| 98 |
+
.act_data (act_data),
|
| 99 |
+
.act_valid (act_valid),
|
| 100 |
+
.act_ready (act_ready),
|
| 101 |
+
.result_data (result_data),
|
| 102 |
+
.result_valid (result_valid),
|
| 103 |
+
.result_ready (result_ready)
|
| 104 |
+
);
|
| 105 |
+
|
| 106 |
+
//--------------------------------------------------------------------------
|
| 107 |
+
// Clock generation
|
| 108 |
+
//--------------------------------------------------------------------------
|
| 109 |
+
initial clk = 0;
|
| 110 |
+
always #(CLK_PERIOD/2) clk = ~clk;
|
| 111 |
+
|
| 112 |
+
//--------------------------------------------------------------------------
|
| 113 |
+
// Compute expected result (golden model)
|
| 114 |
+
//--------------------------------------------------------------------------
|
| 115 |
+
task compute_expected_result();
|
| 116 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 117 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 118 |
+
expected_c[i][j] = 0;
|
| 119 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 120 |
+
expected_c[i][j] = expected_c[i][j] +
|
| 121 |
+
($signed(matrix_a[i][k]) * $signed(matrix_b[k][j]));
|
| 122 |
+
end
|
| 123 |
+
end
|
| 124 |
+
end
|
| 125 |
+
endtask
|
| 126 |
+
|
| 127 |
+
//--------------------------------------------------------------------------
|
| 128 |
+
// Load weights into DUT
|
| 129 |
+
//--------------------------------------------------------------------------
|
| 130 |
+
task load_weights();
|
| 131 |
+
integer kk, rr;
|
| 132 |
+
$display("%0t Loading weights into systolic array...", $realtime);
|
| 133 |
+
|
| 134 |
+
// Wait for weight_ready
|
| 135 |
+
while (!weight_ready) @(posedge clk);
|
| 136 |
+
|
| 137 |
+
// Load weights: iterate through K dimension, then rows
|
| 138 |
+
for (kk = 0; kk < K_DIM; kk = kk + 1) begin
|
| 139 |
+
for (rr = 0; rr < ARRAY_ROWS; rr = rr + 1) begin
|
| 140 |
+
@(posedge clk);
|
| 141 |
+
// Pack all columns for this row
|
| 142 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 143 |
+
weight_data[(j+1)*WEIGHT_WIDTH-1 -: WEIGHT_WIDTH] = matrix_b[kk][j];
|
| 144 |
+
end
|
| 145 |
+
weight_valid = 1;
|
| 146 |
+
end
|
| 147 |
+
end
|
| 148 |
+
|
| 149 |
+
@(posedge clk);
|
| 150 |
+
weight_valid = 0;
|
| 151 |
+
weight_data = 0;
|
| 152 |
+
$display("%0t Weight loading complete", $realtime);
|
| 153 |
+
endtask
|
| 154 |
+
|
| 155 |
+
//--------------------------------------------------------------------------
|
| 156 |
+
// Stream activations into DUT
|
| 157 |
+
//--------------------------------------------------------------------------
|
| 158 |
+
task stream_activations();
|
| 159 |
+
integer kk;
|
| 160 |
+
$display("%0t Streaming activations...", $realtime);
|
| 161 |
+
|
| 162 |
+
// Wait for act_ready
|
| 163 |
+
while (!act_ready) @(posedge clk);
|
| 164 |
+
|
| 165 |
+
// Stream activations column by column (K dimension)
|
| 166 |
+
for (kk = 0; kk < K_DIM; kk = kk + 1) begin
|
| 167 |
+
@(posedge clk);
|
| 168 |
+
// Pack all rows for this K column
|
| 169 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 170 |
+
act_data[(i+1)*DATA_WIDTH-1 -: DATA_WIDTH] = matrix_a[i][kk];
|
| 171 |
+
end
|
| 172 |
+
act_valid = 1;
|
| 173 |
+
end
|
| 174 |
+
|
| 175 |
+
@(posedge clk);
|
| 176 |
+
act_valid = 0;
|
| 177 |
+
act_data = 0;
|
| 178 |
+
$display("%0t Activation streaming complete", $realtime);
|
| 179 |
+
endtask
|
| 180 |
+
|
| 181 |
+
//--------------------------------------------------------------------------
|
| 182 |
+
// Collect results from DUT
|
| 183 |
+
//--------------------------------------------------------------------------
|
| 184 |
+
task collect_results();
|
| 185 |
+
integer timeout_cnt;
|
| 186 |
+
$display("%0t Collecting results...", $realtime);
|
| 187 |
+
|
| 188 |
+
result_ready = 1;
|
| 189 |
+
result_row_cnt = 0;
|
| 190 |
+
timeout_cnt = 0;
|
| 191 |
+
|
| 192 |
+
// Wait for results
|
| 193 |
+
while (!done && timeout_cnt < 1000) begin
|
| 194 |
+
@(posedge clk);
|
| 195 |
+
timeout_cnt = timeout_cnt + 1;
|
| 196 |
+
|
| 197 |
+
if (result_valid) begin
|
| 198 |
+
// Unpack result row
|
| 199 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 200 |
+
actual_c[result_row_cnt][j] = $signed(result_data[(j+1)*ACC_WIDTH-1 -: ACC_WIDTH]);
|
| 201 |
+
end
|
| 202 |
+
result_row_cnt = result_row_cnt + 1;
|
| 203 |
+
$display("%0t Received result row %0d", $realtime, result_row_cnt);
|
| 204 |
+
end
|
| 205 |
+
end
|
| 206 |
+
|
| 207 |
+
result_ready = 0;
|
| 208 |
+
$display("%0t Result collection complete", $realtime);
|
| 209 |
+
endtask
|
| 210 |
+
|
| 211 |
+
//--------------------------------------------------------------------------
|
| 212 |
+
// Compare results
|
| 213 |
+
//--------------------------------------------------------------------------
|
| 214 |
+
task compare_results();
|
| 215 |
+
integer local_err = 0;
|
| 216 |
+
$display("\n=== Result Comparison ===");
|
| 217 |
+
|
| 218 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 219 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 220 |
+
if (actual_c[i][j] !== expected_c[i][j]) begin
|
| 221 |
+
$display("ERROR: C[%0d][%0d] mismatch - Expected: %0d, Actual: %0d",
|
| 222 |
+
i, j, expected_c[i][j], actual_c[i][j]);
|
| 223 |
+
local_err = local_err + 1;
|
| 224 |
+
err_cnt = err_cnt + 1;
|
| 225 |
+
end
|
| 226 |
+
end
|
| 227 |
+
end
|
| 228 |
+
|
| 229 |
+
if (local_err == 0) begin
|
| 230 |
+
$display("Test %0d (%s): PASSED", test_num, test_name);
|
| 231 |
+
end else begin
|
| 232 |
+
$display("Test %0d (%s): FAILED with %0d errors", test_num, test_name, local_err);
|
| 233 |
+
end
|
| 234 |
+
endtask
|
| 235 |
+
|
| 236 |
+
//--------------------------------------------------------------------------
|
| 237 |
+
// Print matrices
|
| 238 |
+
//--------------------------------------------------------------------------
|
| 239 |
+
task print_matrices();
|
| 240 |
+
$display("\nMatrix A (%0d x %0d):", ARRAY_ROWS, K_DIM);
|
| 241 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 242 |
+
$write(" [");
|
| 243 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 244 |
+
$write("%4d ", matrix_a[i][k]);
|
| 245 |
+
end
|
| 246 |
+
$display("]");
|
| 247 |
+
end
|
| 248 |
+
|
| 249 |
+
$display("\nMatrix B (%0d x %0d):", K_DIM, ARRAY_COLS);
|
| 250 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 251 |
+
$write(" [");
|
| 252 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 253 |
+
$write("%4d ", matrix_b[k][j]);
|
| 254 |
+
end
|
| 255 |
+
$display("]");
|
| 256 |
+
end
|
| 257 |
+
|
| 258 |
+
$display("\nExpected C (%0d x %0d):", ARRAY_ROWS, ARRAY_COLS);
|
| 259 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 260 |
+
$write(" [");
|
| 261 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 262 |
+
$write("%6d ", expected_c[i][j]);
|
| 263 |
+
end
|
| 264 |
+
$display("]");
|
| 265 |
+
end
|
| 266 |
+
endtask
|
| 267 |
+
|
| 268 |
+
//--------------------------------------------------------------------------
|
| 269 |
+
// Run single test
|
| 270 |
+
//--------------------------------------------------------------------------
|
| 271 |
+
task run_single_test();
|
| 272 |
+
compute_expected_result();
|
| 273 |
+
print_matrices();
|
| 274 |
+
|
| 275 |
+
// Start computation
|
| 276 |
+
@(posedge clk);
|
| 277 |
+
start = 1;
|
| 278 |
+
@(posedge clk);
|
| 279 |
+
start = 0;
|
| 280 |
+
|
| 281 |
+
// Wait for busy
|
| 282 |
+
while (!busy) @(posedge clk);
|
| 283 |
+
|
| 284 |
+
// Load weights and stream activations in parallel (sequentially in FSM)
|
| 285 |
+
fork
|
| 286 |
+
load_weights();
|
| 287 |
+
stream_activations();
|
| 288 |
+
collect_results();
|
| 289 |
+
join
|
| 290 |
+
|
| 291 |
+
// Wait for done
|
| 292 |
+
while (!done) @(posedge clk);
|
| 293 |
+
|
| 294 |
+
compare_results();
|
| 295 |
+
|
| 296 |
+
// Gap between tests
|
| 297 |
+
repeat(10) @(posedge clk);
|
| 298 |
+
endtask
|
| 299 |
+
|
| 300 |
+
//--------------------------------------------------------------------------
|
| 301 |
+
// Test 1: Simple known values
|
| 302 |
+
//--------------------------------------------------------------------------
|
| 303 |
+
task test_simple_values();
|
| 304 |
+
test_num = test_num + 1;
|
| 305 |
+
test_name = "Simple Known Values";
|
| 306 |
+
$display("\n========================================");
|
| 307 |
+
$display("Test %0d: %s", test_num, test_name);
|
| 308 |
+
$display("========================================");
|
| 309 |
+
|
| 310 |
+
// Initialize A with simple values [1,2,3,4; 5,6,7,8; ...]
|
| 311 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 312 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 313 |
+
matrix_a[i][k] = i * K_DIM + k + 1;
|
| 314 |
+
end
|
| 315 |
+
end
|
| 316 |
+
|
| 317 |
+
// Initialize B with simple values [1,2,3,4; 5,6,7,8; ...]
|
| 318 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 319 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 320 |
+
matrix_b[k][j] = k * ARRAY_COLS + j + 1;
|
| 321 |
+
end
|
| 322 |
+
end
|
| 323 |
+
|
| 324 |
+
run_single_test();
|
| 325 |
+
endtask
|
| 326 |
+
|
| 327 |
+
//--------------------------------------------------------------------------
|
| 328 |
+
// Test 2: Identity matrix multiplication
|
| 329 |
+
//--------------------------------------------------------------------------
|
| 330 |
+
task test_identity_matrix();
|
| 331 |
+
test_num = test_num + 1;
|
| 332 |
+
test_name = "Identity Matrix";
|
| 333 |
+
$display("\n========================================");
|
| 334 |
+
$display("Test %0d: %s", test_num, test_name);
|
| 335 |
+
$display("========================================");
|
| 336 |
+
|
| 337 |
+
// A = random values
|
| 338 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 339 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 340 |
+
matrix_a[i][k] = $urandom_range(1, 10);
|
| 341 |
+
end
|
| 342 |
+
end
|
| 343 |
+
|
| 344 |
+
// B = identity (assuming square matrix)
|
| 345 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 346 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 347 |
+
matrix_b[k][j] = (k == j) ? 1 : 0;
|
| 348 |
+
end
|
| 349 |
+
end
|
| 350 |
+
|
| 351 |
+
run_single_test();
|
| 352 |
+
endtask
|
| 353 |
+
|
| 354 |
+
//--------------------------------------------------------------------------
|
| 355 |
+
// Test 3: Random matrix multiplication
|
| 356 |
+
//--------------------------------------------------------------------------
|
| 357 |
+
task test_random_matrices();
|
| 358 |
+
test_num = test_num + 1;
|
| 359 |
+
test_name = "Random Matrices";
|
| 360 |
+
$display("\n========================================");
|
| 361 |
+
$display("Test %0d: %s", test_num, test_name);
|
| 362 |
+
$display("========================================");
|
| 363 |
+
|
| 364 |
+
// Random A
|
| 365 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 366 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 367 |
+
if (SIGNED_MATH)
|
| 368 |
+
matrix_a[i][k] = $random % (MAX_SIGNED_VAL + 1);
|
| 369 |
+
else
|
| 370 |
+
matrix_a[i][k] = $urandom_range(0, (1 << DATA_WIDTH) - 1);
|
| 371 |
+
end
|
| 372 |
+
end
|
| 373 |
+
|
| 374 |
+
// Random B
|
| 375 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 376 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 377 |
+
if (SIGNED_MATH)
|
| 378 |
+
matrix_b[k][j] = $random % (MAX_SIGNED_VAL + 1);
|
| 379 |
+
else
|
| 380 |
+
matrix_b[k][j] = $urandom_range(0, (1 << WEIGHT_WIDTH) - 1);
|
| 381 |
+
end
|
| 382 |
+
end
|
| 383 |
+
|
| 384 |
+
run_single_test();
|
| 385 |
+
endtask
|
| 386 |
+
|
| 387 |
+
//--------------------------------------------------------------------------
|
| 388 |
+
// Test 4: Zero matrix
|
| 389 |
+
//--------------------------------------------------------------------------
|
| 390 |
+
task test_zero_matrix();
|
| 391 |
+
test_num = test_num + 1;
|
| 392 |
+
test_name = "Zero Matrix";
|
| 393 |
+
$display("\n========================================");
|
| 394 |
+
$display("Test %0d: %s", test_num, test_name);
|
| 395 |
+
$display("========================================");
|
| 396 |
+
|
| 397 |
+
// A = all zeros
|
| 398 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 399 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 400 |
+
matrix_a[i][k] = 0;
|
| 401 |
+
end
|
| 402 |
+
end
|
| 403 |
+
|
| 404 |
+
// B = random
|
| 405 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 406 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 407 |
+
matrix_b[k][j] = $urandom_range(1, 10);
|
| 408 |
+
end
|
| 409 |
+
end
|
| 410 |
+
|
| 411 |
+
run_single_test();
|
| 412 |
+
endtask
|
| 413 |
+
|
| 414 |
+
//--------------------------------------------------------------------------
|
| 415 |
+
// Test 5: Negative values (signed)
|
| 416 |
+
//--------------------------------------------------------------------------
|
| 417 |
+
task test_signed_values();
|
| 418 |
+
test_num = test_num + 1;
|
| 419 |
+
test_name = "Signed Values";
|
| 420 |
+
$display("\n========================================");
|
| 421 |
+
$display("Test %0d: %s", test_num, test_name);
|
| 422 |
+
$display("========================================");
|
| 423 |
+
|
| 424 |
+
// A with positive and negative values
|
| 425 |
+
for (i = 0; i < ARRAY_ROWS; i = i + 1) begin
|
| 426 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 427 |
+
matrix_a[i][k] = ((i + k) % 2 == 0) ? (k + 1) : -(k + 1);
|
| 428 |
+
end
|
| 429 |
+
end
|
| 430 |
+
|
| 431 |
+
// B with positive and negative values
|
| 432 |
+
for (k = 0; k < K_DIM; k = k + 1) begin
|
| 433 |
+
for (j = 0; j < ARRAY_COLS; j = j + 1) begin
|
| 434 |
+
matrix_b[k][j] = ((k + j) % 2 == 0) ? (j + 1) : -(j + 1);
|
| 435 |
+
end
|
| 436 |
+
end
|
| 437 |
+
|
| 438 |
+
run_single_test();
|
| 439 |
+
endtask
|
| 440 |
+
|
| 441 |
+
//--------------------------------------------------------------------------
|
| 442 |
+
// Main test sequence
|
| 443 |
+
//--------------------------------------------------------------------------
|
| 444 |
+
initial begin
|
| 445 |
+
string vcdfile;
|
| 446 |
+
int vcdlevel;
|
| 447 |
+
int seed;
|
| 448 |
+
|
| 449 |
+
// Initialize signals
|
| 450 |
+
rst_n = 0;
|
| 451 |
+
start = 0;
|
| 452 |
+
weight_data = 0;
|
| 453 |
+
weight_valid = 0;
|
| 454 |
+
act_data = 0;
|
| 455 |
+
act_valid = 0;
|
| 456 |
+
result_ready = 0;
|
| 457 |
+
|
| 458 |
+
// VCD dump setup
|
| 459 |
+
if ($value$plusargs("VCDFILE=%s", vcdfile))
|
| 460 |
+
$dumpfile(vcdfile);
|
| 461 |
+
if ($value$plusargs("VCDLEVEL=%d", vcdlevel))
|
| 462 |
+
$dumpvars(vcdlevel, tb);
|
| 463 |
+
if ($value$plusargs("SEED=%d", seed)) begin
|
| 464 |
+
$urandom(seed);
|
| 465 |
+
$display("Seed = %d", seed);
|
| 466 |
+
end
|
| 467 |
+
|
| 468 |
+
$display("\n");
|
| 469 |
+
$display("============================================");
|
| 470 |
+
$display(" Systolic Array Testbench");
|
| 471 |
+
$display("============================================");
|
| 472 |
+
$display("Array Size: %0d x %0d", ARRAY_ROWS, ARRAY_COLS);
|
| 473 |
+
$display("K Dimension: %0d", K_DIM);
|
| 474 |
+
$display("Data Width: %0d bits", DATA_WIDTH);
|
| 475 |
+
$display("Weight Width: %0d bits", WEIGHT_WIDTH);
|
| 476 |
+
$display("Acc Width: %0d bits", ACC_WIDTH);
|
| 477 |
+
$display("Signed Math: %s", SIGNED_MATH ? "Yes" : "No");
|
| 478 |
+
$display("============================================\n");
|
| 479 |
+
|
| 480 |
+
// Reset sequence
|
| 481 |
+
repeat(5) @(posedge clk);
|
| 482 |
+
rst_n = 1;
|
| 483 |
+
repeat(5) @(posedge clk);
|
| 484 |
+
|
| 485 |
+
// Run tests
|
| 486 |
+
test_simple_values();
|
| 487 |
+
test_identity_matrix();
|
| 488 |
+
test_random_matrices();
|
| 489 |
+
test_zero_matrix();
|
| 490 |
+
|
| 491 |
+
if (SIGNED_MATH) begin
|
| 492 |
+
test_signed_values();
|
| 493 |
+
end
|
| 494 |
+
|
| 495 |
+
// Multiple random tests
|
| 496 |
+
repeat(3) begin
|
| 497 |
+
test_random_matrices();
|
| 498 |
+
end
|
| 499 |
+
|
| 500 |
+
// Summary
|
| 501 |
+
$display("\n============================================");
|
| 502 |
+
$display(" Test Summary");
|
| 503 |
+
$display("============================================");
|
| 504 |
+
$display("Total Tests: %0d", test_num);
|
| 505 |
+
if (err_cnt == 0) begin
|
| 506 |
+
$display("Result: ALL TESTS PASSED");
|
| 507 |
+
end else begin
|
| 508 |
+
$display("Result: FAILED");
|
| 509 |
+
$display("Total Errors: %0d", err_cnt);
|
| 510 |
+
end
|
| 511 |
+
$display("============================================\n");
|
| 512 |
+
|
| 513 |
+
#100;
|
| 514 |
+
$finish;
|
| 515 |
+
end
|
| 516 |
+
|
| 517 |
+
//--------------------------------------------------------------------------
|
| 518 |
+
// Timeout watchdog
|
| 519 |
+
//--------------------------------------------------------------------------
|
| 520 |
+
initial begin
|
| 521 |
+
#(SIM_TIMEOUT);
|
| 522 |
+
$display("\n%0t ERROR: Simulation timeout!", $realtime);
|
| 523 |
+
$display("TEST FAILED - SIM TIMEOUT\n");
|
| 524 |
+
$finish;
|
| 525 |
+
end
|
| 526 |
+
|
| 527 |
+
endmodule
|
Weiyet_RTLStructLib/source/Table/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multi-port table
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
This Verilog module implements a flexible table structure that supports multiple simultaneous read and write operations. It functions similarly to a register file, providing direct indexed access to stored data without the need for hashing mechanisms.
|
| 5 |
+
|
| 6 |
+
## Features
|
| 7 |
+
- Configurable table size and data width
|
| 8 |
+
- Support for multiple simultaneous write operations
|
| 9 |
+
- Support for multiple simultaneous read operations
|
| 10 |
+
- Synchronous operation with reset capability
|
| 11 |
+
- Direct indexed access to stored data
|
| 12 |
+
|
| 13 |
+
## Parameters
|
| 14 |
+
|
| 15 |
+
| Parameter | Description | Default |
|
| 16 |
+
|-------------|------------------------------------------------|---------|
|
| 17 |
+
| TABLE_SIZE | Number of entries in the table | 32 |
|
| 18 |
+
| DATA_WIDTH | Width of each data entry in bits | 8 |
|
| 19 |
+
| INPUT_RATE | Number of simultaneous write operations | 2 |
|
| 20 |
+
| OUTPUT_RATE | Number of simultaneous read operations | 2 |
|
| 21 |
+
|
| 22 |
+
## Port Descriptions
|
| 23 |
+
|
| 24 |
+
### Input Ports
|
| 25 |
+
|
| 26 |
+
| Port | Width | Description |
|
| 27 |
+
|-----------|----------------------------------|--------------------------------|
|
| 28 |
+
| clk | 1 | System clock signal |
|
| 29 |
+
| rst | 1 | Active-high reset signal |
|
| 30 |
+
| wr_en | INPUT_RATE | Write enable signals |
|
| 31 |
+
| rd_en | 1 | Read enable signal |
|
| 32 |
+
| index_wr | INPUT_RATE * log2(TABLE_SIZE) | Write address indices |
|
| 33 |
+
| index_rd | OUTPUT_RATE * log2(TABLE_SIZE) | Read address indices |
|
| 34 |
+
| data_wr | INPUT_RATE * DATA_WIDTH | Write data input |
|
| 35 |
+
|
| 36 |
+
### Output Ports
|
| 37 |
+
|
| 38 |
+
| Port | Width | Description |
|
| 39 |
+
|-----------|--------------------------|----------------------|
|
| 40 |
+
| data_rd | OUTPUT_RATE * DATA_WIDTH | Read data output |
|
| 41 |
+
|
| 42 |
+
## Timing
|
| 43 |
+
- All operations are synchronized to the positive edge of the clock
|
| 44 |
+
- Reset is asynchronous and active-high
|
| 45 |
+
- Write operations occur on the next clock edge after wr_en is asserted
|
| 46 |
+
- Read operations occur on the next clock edge after rd_en is asserted
|
| 47 |
+
|
| 48 |
+
## Usage Example
|
| 49 |
+
```verilog
|
| 50 |
+
table_top #(
|
| 51 |
+
.TABLE_SIZE(64),
|
| 52 |
+
.DATA_WIDTH(16),
|
| 53 |
+
.INPUT_RATE(2),
|
| 54 |
+
.OUTPUT_RATE(2)
|
| 55 |
+
) table_inst (
|
| 56 |
+
.clk(system_clk),
|
| 57 |
+
.rst(system_rst),
|
| 58 |
+
.wr_en(write_enable),
|
| 59 |
+
.rd_en(read_enable),
|
| 60 |
+
.index_wr(write_indices),
|
| 61 |
+
.index_rd(read_indices),
|
| 62 |
+
.data_wr(write_data),
|
| 63 |
+
.data_rd(read_data)
|
| 64 |
+
);
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Implementation Details
|
| 68 |
+
- Uses a 2D register array for data storage
|
| 69 |
+
- Implements parallel write operations using generate blocks
|
| 70 |
+
- Supports concurrent read operations
|
| 71 |
+
- Reset initializes all storage elements to zero
|
| 72 |
+
- Uses Verilog-2001 syntax and features
|
| 73 |
+
|
| 74 |
+
## Performance Considerations
|
| 75 |
+
- All read and write operations complete in a single clock cycle
|
| 76 |
+
- No read-after-write hazard protection is implemented
|
| 77 |
+
- Care should be taken when accessing the same index simultaneously
|
| 78 |
+
|
| 79 |
+
## Limitations
|
| 80 |
+
- No built-in error checking for invalid indices
|
| 81 |
+
- No protection against simultaneous read/write to same location
|
| 82 |
+
- Write operations take precedence in case of conflicts
|
| 83 |
+
|
Weiyet_RTLStructLib/source/Table/src/rtl_file.f
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
table.sv
|
Weiyet_RTLStructLib/source/Table/src/table.sv
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
// Create Date: 31/01/2025 12:46:52 PM
|
| 4 |
+
// Last Update Date: 09/02/2024 15:40 PM
|
| 5 |
+
// Author: https://www.linkedin.com/in/wei-yet-ng-065485119/
|
| 6 |
+
// Module Name: table
|
| 7 |
+
// Description: Table Structure that supports multiple read, write oepration simultaneously
|
| 8 |
+
// Additional Comments: .
|
| 9 |
+
//
|
| 10 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 11 |
+
|
| 12 |
+
module table_top #(
|
| 13 |
+
parameter TABLE_SIZE = 32,
|
| 14 |
+
parameter DATA_WIDTH = 8,
|
| 15 |
+
parameter INPUT_RATE = 2,
|
| 16 |
+
parameter OUTPUT_RATE = 2
|
| 17 |
+
)(
|
| 18 |
+
input wire clk,
|
| 19 |
+
input wire rst,
|
| 20 |
+
input wire [INPUT_RATE-1:0] wr_en,
|
| 21 |
+
input wire rd_en,
|
| 22 |
+
input wire [INPUT_RATE*$clog2(TABLE_SIZE)-1:0] index_wr,
|
| 23 |
+
input wire [OUTPUT_RATE*$clog2(TABLE_SIZE)-1:0] index_rd,
|
| 24 |
+
input wire [INPUT_RATE*DATA_WIDTH-1:0] data_wr,
|
| 25 |
+
output reg [OUTPUT_RATE*DATA_WIDTH-1:0] data_rd
|
| 26 |
+
);
|
| 27 |
+
localparam INDEX_WIDTH = $clog2(TABLE_SIZE);
|
| 28 |
+
reg [DATA_WIDTH-1:0] data_stored [TABLE_SIZE];
|
| 29 |
+
integer i;
|
| 30 |
+
|
| 31 |
+
always @ (posedge clk or posedge rst) begin
|
| 32 |
+
if(rst) begin
|
| 33 |
+
for (i=0; i<TABLE_SIZE; i=i+1) begin
|
| 34 |
+
data_stored[i] = 'b0;
|
| 35 |
+
end
|
| 36 |
+
end else begin
|
| 37 |
+
for (i=1; i<=INPUT_RATE; i=i+1) begin
|
| 38 |
+
if (wr_en[i-1])
|
| 39 |
+
data_stored[index_wr[i*INDEX_WIDTH-1 -:INDEX_WIDTH]] = data_wr[i*DATA_WIDTH-1 -:DATA_WIDTH];
|
| 40 |
+
end
|
| 41 |
+
end
|
| 42 |
+
end
|
| 43 |
+
|
| 44 |
+
always @ (posedge clk or posedge rst) begin
|
| 45 |
+
if(rst) begin
|
| 46 |
+
for (i=1; i<=OUTPUT_RATE; i=i+1) begin
|
| 47 |
+
data_rd[i*DATA_WIDTH-1 -: DATA_WIDTH] = 'b0;
|
| 48 |
+
end
|
| 49 |
+
end else if (rd_en) begin
|
| 50 |
+
for (i=1; i<=OUTPUT_RATE; i=i+1) begin
|
| 51 |
+
data_rd[i*DATA_WIDTH-1 -: DATA_WIDTH] = data_stored[index_rd[i*INDEX_WIDTH-1 -:INDEX_WIDTH]];
|
| 52 |
+
end
|
| 53 |
+
end
|
| 54 |
+
end
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
endmodule
|
| 59 |
+
|
| 60 |
+
|
Weiyet_RTLStructLib/source/Table/tb/cocotb/Makefile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SIM ?= icarus
|
| 2 |
+
TOPLEVEL_LANG ?= verilog
|
| 3 |
+
WAVES ?= 1 #set 1 to enable waveform dump.
|
| 4 |
+
|
| 5 |
+
PWD=$(shell pwd)
|
| 6 |
+
|
| 7 |
+
#export PYTHONPATH := $(PWD)/../model:$(PYTHONPATH)
|
| 8 |
+
|
| 9 |
+
VERILOG_SOURCES = $(PWD)/../../src/table.sv
|
| 10 |
+
|
| 11 |
+
# DUT Top
|
| 12 |
+
TOPLEVEL = table_top
|
| 13 |
+
# top python file name
|
| 14 |
+
MODULE = tb
|
| 15 |
+
|
| 16 |
+
#use , separtor to run multiple TESTCASE, by default all @cocotb.test will be run
|
| 17 |
+
#TESTCASE = lifo_rand_op_test
|
| 18 |
+
TESTCASE = table_rand_test
|
| 19 |
+
|
| 20 |
+
COMPILE_ARGS = -Ptable_top.TABLE_SIZE=32 # DUT parameter #"-p" (parameter) iverilog command flags
|
| 21 |
+
COMPILE_ARGS += -Ptable_top.DATA_WIDTH=8 # DUT parameter #"-p" (parameter) iverilog command flags
|
| 22 |
+
COMPILE_ARGS += -Ptable_top.INPUT_RATE=1 # DUT parameter #"-p" (parameter) iverilog command flags
|
| 23 |
+
COMPILE_ARGS += -Ptable_top.OUTPUT_RATE=1 # DUT parameter #"-p" (parameter) iverilog command flags
|
| 24 |
+
#run make clean before running with new parameter.
|
| 25 |
+
|
| 26 |
+
#Set RANDOM_SEED number
|
| 27 |
+
#PLUSARGS = +seed=1716033254
|
| 28 |
+
COCOTB_HDL_TIMEUNIT = 1ns
|
| 29 |
+
COCOTB_HDL_TIMEPRECISION = 1ps
|
| 30 |
+
|
| 31 |
+
ifeq ($(SIM), icarus)
|
| 32 |
+
$(shell echo 'module iverilog_dump();' > iverilog_dump.v)
|
| 33 |
+
$(shell echo 'initial begin' >> iverilog_dump.v)
|
| 34 |
+
$(shell echo ' $$dumpfile("$(TOPLEVEL).vcd");' >> iverilog_dump.v)
|
| 35 |
+
$(shell echo ' $$dumpvars(0, $(TOPLEVEL));' >> iverilog_dump.v)
|
| 36 |
+
$(shell echo 'end' >> iverilog_dump.v)
|
| 37 |
+
$(shell echo 'endmodule' >> iverilog_dump.v)
|
| 38 |
+
VERILOG_SOURCES += $(PWD)/iverilog_dump.v
|
| 39 |
+
COMPILE_ARGS += -s iverilog_dump
|
| 40 |
+
endif
|
| 41 |
+
|
| 42 |
+
include $(shell cocotb-config --makefiles)/Makefile.sim
|
| 43 |
+
|
| 44 |
+
|
Weiyet_RTLStructLib/source/Table/tb/cocotb/tb.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
#import asyncio
|
| 3 |
+
import math
|
| 4 |
+
import cocotb
|
| 5 |
+
from cocotb.triggers import Timer, RisingEdge, ReadOnly
|
| 6 |
+
from cocotb.clock import Clock
|
| 7 |
+
from cocotb_bus.drivers import BusDriver
|
| 8 |
+
from cocotb_bus.monitors import BusMonitor
|
| 9 |
+
from cocotb.binary import BinaryValue
|
| 10 |
+
|
| 11 |
+
#BIN string
|
| 12 |
+
#BinaryValue(dut.data_wr.value, n_bits=8) ; BinaryValue.integar ; BinaryValue.hex ; BinaryValue.binstr; BinaryValue.signed_integer ; can represent x,z
|
| 13 |
+
|
| 14 |
+
TABLE_SIZE = 32 # DUT parameter
|
| 15 |
+
DATA_WIDTH = 8 # DUT paramter
|
| 16 |
+
INPUT_RATE = 1 # DUT parameter
|
| 17 |
+
OUTPUT_RATE = 1 # DUT parameter
|
| 18 |
+
INDEX_WIDTH = 1
|
| 19 |
+
TB_CLK_PERIOD = 30 # TB clk generator
|
| 20 |
+
TB_SIM_TIMEOUT = 30 # TB sim timeout 30ms
|
| 21 |
+
TB_TEST_WEIGHT = 1
|
| 22 |
+
table_expected = [0 for i in range(TABLE_SIZE)]
|
| 23 |
+
err_cnt = 0
|
| 24 |
+
|
| 25 |
+
# table_top #(
|
| 26 |
+
# .TABLE_SIZE(TABLE_SIZE),
|
| 27 |
+
# .DATA_WIDTH(DATA_WIDTH),
|
| 28 |
+
# .INPUT_RATE(INPUT_RATE),
|
| 29 |
+
# .OUTPUT_RATE(OUTPUT_RATE)) DUT (
|
| 30 |
+
# /*input wire*/ .clk(clk),
|
| 31 |
+
# /*input wire*/ .rst(rst),
|
| 32 |
+
# /*input wire*/ .wr_en(wr_en),
|
| 33 |
+
# /*input wire*/ .rd_en(rd_en),
|
| 34 |
+
# /*input wire [INPUT_RATE*$clog2(TABLE_SIZE)-1:0]*/ .index_wr(index_wr),
|
| 35 |
+
# /*input wire [OUTPUT_RATE*$clog2(TABLE_SIZE)-1:0]*/ .index_rd(index_rd),
|
| 36 |
+
# /*input wire [INPUT_RATE*DATA_WIDTH-1:0]*/ .data_wr(data_wr),
|
| 37 |
+
# /*output reg [OUTPUT_RATE*DATA_WIDTH-1:0]*/ .data_rd(data_rd));
|
| 38 |
+
|
| 39 |
+
class OutputDriver(BusDriver):
|
| 40 |
+
_signals = ["wr_en", "rd_en", "index_wr", "index_rd", "data_wr"] #dut.wr_en ; dut.rd_en; dut.index_wr; dut.data_wr
|
| 41 |
+
|
| 42 |
+
def __init__(self, dut, name, clk):
|
| 43 |
+
BusDriver.__init__(self, dut, name, clk)
|
| 44 |
+
self.bus.wr_en.value = 0
|
| 45 |
+
self.bus.rd_en.value = 0
|
| 46 |
+
self.bus.index_wr.value = 0
|
| 47 |
+
self.bus.index_rd.value = 0
|
| 48 |
+
self.bus.data_wr.value = 0
|
| 49 |
+
self.dut = dut
|
| 50 |
+
self.clk = clk
|
| 51 |
+
|
| 52 |
+
# async def _driver_send(self):
|
| 53 |
+
async def write_burst(self, input_data):
|
| 54 |
+
for i in range(0,len(input_data)//INPUT_RATE):
|
| 55 |
+
await RisingEdge(self.clk)
|
| 56 |
+
await Timer (1, units = 'ns')
|
| 57 |
+
data_wr = 0
|
| 58 |
+
index_wr = 0
|
| 59 |
+
for j in range(0,INPUT_RATE):
|
| 60 |
+
data_wr = (data_wr<<(j)*DATA_WIDTH) + input_data[i*INPUT_RATE+j][1]
|
| 61 |
+
index_wr = (index_wr<<(j)*INDEX_WIDTH) + input_data[i*INPUT_RATE+j][0]
|
| 62 |
+
self.bus.wr_en.value = 2**INPUT_RATE - 1
|
| 63 |
+
self.bus.data_wr.value = data_wr
|
| 64 |
+
self.bus.index_wr.value = index_wr
|
| 65 |
+
await RisingEdge(self.clk)
|
| 66 |
+
await Timer (1, units = 'ns')
|
| 67 |
+
self.bus.wr_en.value = 0
|
| 68 |
+
|
| 69 |
+
async def read_burst(self, target_index):
|
| 70 |
+
for i in range(0,len(target_index)//OUTPUT_RATE):
|
| 71 |
+
await RisingEdge(self.clk)
|
| 72 |
+
await Timer (1, units = 'ns')
|
| 73 |
+
index_rd = 0
|
| 74 |
+
for j in range(0,OUTPUT_RATE):
|
| 75 |
+
index_rd = (index_rd<<(j)*INDEX_WIDTH) + target_index[i*OUTPUT_RATE+j]
|
| 76 |
+
#print(index_rd)
|
| 77 |
+
self.bus.rd_en.value = 1
|
| 78 |
+
self.bus.index_rd.value = index_rd
|
| 79 |
+
await RisingEdge(self.clk)
|
| 80 |
+
await Timer (1, units = 'ns')
|
| 81 |
+
self.bus.rd_en.value = 0
|
| 82 |
+
|
| 83 |
+
class InputMonitor(BusMonitor):
|
| 84 |
+
_signals = ["wr_en","rd_en","index_wr","index_rd","data_wr","data_rd"]
|
| 85 |
+
|
| 86 |
+
def __init__(self, dut, name, clk, reset):
|
| 87 |
+
BusMonitor.__init__(self, dut, name, clk, reset)
|
| 88 |
+
self.clk = clk
|
| 89 |
+
self.reset = reset
|
| 90 |
+
self.dut = dut
|
| 91 |
+
|
| 92 |
+
async def _monitor_recv(self): #this will be called in init.
|
| 93 |
+
global err_cnt
|
| 94 |
+
global table_expected
|
| 95 |
+
while True:
|
| 96 |
+
await RisingEdge(self.clock)
|
| 97 |
+
await ReadOnly()
|
| 98 |
+
#await Timer (1, units = 'ns')
|
| 99 |
+
|
| 100 |
+
if self.reset.value == 1:
|
| 101 |
+
self.bus.wr_en.value = 0
|
| 102 |
+
self.bus.rd_en.value = 0
|
| 103 |
+
self.bus.index_wr.value = 0
|
| 104 |
+
self.bus.index_rd.value = 0
|
| 105 |
+
self.bus.data_wr.value = 0
|
| 106 |
+
table_expected = [0 for i in range(TABLE_SIZE)]
|
| 107 |
+
continue
|
| 108 |
+
|
| 109 |
+
if self.bus.wr_en.value == (2**INPUT_RATE -1) and self.bus.rd_en.value == 1:
|
| 110 |
+
self.read_update()
|
| 111 |
+
self.write_update()
|
| 112 |
+
elif self.bus.wr_en.value == (2**INPUT_RATE - 1):
|
| 113 |
+
self.write_update()
|
| 114 |
+
elif self.bus.rd_en.value == 1:
|
| 115 |
+
self.read_update()
|
| 116 |
+
|
| 117 |
+
def write_update(self):
|
| 118 |
+
global table_expected
|
| 119 |
+
for j in range(0,INPUT_RATE):
|
| 120 |
+
target_index = self.bus.index_wr.value & ((2**INDEX_WIDTH - 1) << (INDEX_WIDTH*j))
|
| 121 |
+
target_index = target_index >> (INDEX_WIDTH*j)
|
| 122 |
+
exp_data_wr = self.bus.data_wr.value & ((2**DATA_WIDTH - 1) << (DATA_WIDTH*j))
|
| 123 |
+
exp_data_wr = exp_data_wr >> (DATA_WIDTH*j)
|
| 124 |
+
table_expected[target_index] = exp_data_wr
|
| 125 |
+
cocotb.log.info("WRITE OPERATION: INDEX = d%0d, DATA = d%0d", target_index, exp_data_wr)
|
| 126 |
+
|
| 127 |
+
def read_update(self):
|
| 128 |
+
global err_cnt
|
| 129 |
+
for j in range(0,OUTPUT_RATE):
|
| 130 |
+
target_index = self.bus.index_rd.value & ((2**INDEX_WIDTH - 1) << (INDEX_WIDTH*j))
|
| 131 |
+
target_index = target_index >> (INDEX_WIDTH*j)
|
| 132 |
+
act_data_rd = self.bus.data_rd.value & ((2**DATA_WIDTH - 1) << (DATA_WIDTH*j))
|
| 133 |
+
act_data_rd = act_data_rd >> (DATA_WIDTH*j)
|
| 134 |
+
exp_data_rd = table_expected[target_index]
|
| 135 |
+
if (act_data_rd == exp_data_rd):
|
| 136 |
+
cocotb.log.info("READ OPERATION: INDEX = d%0d, DATA = d%0d", target_index,act_data_rd)
|
| 137 |
+
else:
|
| 138 |
+
err_cnt += 1
|
| 139 |
+
cocotb.log.error("READ OPERATION: INDEX = d%0d, ACT DATA = d%0d, EXP DATA = d%0d, ", target_index, act_data_rd, exp_data_rd)
|
| 140 |
+
|
| 141 |
+
async def dut_init(dut):
|
| 142 |
+
global TABLE_SIZE # DUT parameter
|
| 143 |
+
global DATA_WIDTH # DUT paramter
|
| 144 |
+
global INPUT_RATE # DUT parameter
|
| 145 |
+
global OUTPUT_RATE # DUT parameter
|
| 146 |
+
global INDEX_WIDTH
|
| 147 |
+
TABLE_SIZE = dut.TABLE_SIZE.value
|
| 148 |
+
DATA_WIDTH = dut.DATA_WIDTH.value
|
| 149 |
+
INPUT_RATE = dut.INPUT_RATE.value
|
| 150 |
+
OUTPUT_RATE = dut.OUTPUT_RATE.value
|
| 151 |
+
INDEX_WIDTH = math.ceil(math.log2(TABLE_SIZE));
|
| 152 |
+
await cocotb.start(Clock(dut.clk, TB_CLK_PERIOD, units="ns").start())
|
| 153 |
+
dut.data_wr.value = 0
|
| 154 |
+
dut.index_wr.value = 0
|
| 155 |
+
dut.index_rd.value = 0
|
| 156 |
+
dut.rd_en.value = 0
|
| 157 |
+
dut.wr_en.value = 0
|
| 158 |
+
dut.rst.value = 1
|
| 159 |
+
await(Timer(100,'ns'))
|
| 160 |
+
dut.rst.value = 0
|
| 161 |
+
await(Timer(100,'ns'))
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
@cocotb.test()
|
| 165 |
+
async def table_rand_test(dut):
|
| 166 |
+
await dut_init(dut)
|
| 167 |
+
driver = OutputDriver(dut, None, dut.clk) #set name='None', refer to Bus class
|
| 168 |
+
monitor = InputMonitor(dut, None, dut.clk, dut.rst)
|
| 169 |
+
cocotb.log.info("SEED NUMBER = %d",cocotb.RANDOM_SEED)
|
| 170 |
+
input_data = []
|
| 171 |
+
read_index = []
|
| 172 |
+
|
| 173 |
+
for j in range(TB_TEST_WEIGHT):
|
| 174 |
+
for i in range(30):
|
| 175 |
+
#data_wr = cocotb.binary.BinaryValue(n_bits=INPUT_RATE*DATA_WIDTH)
|
| 176 |
+
#index_wr = cocotb.binary.BinaryValue(n_bits=INDEX_WIDTH)
|
| 177 |
+
data_wr = 0
|
| 178 |
+
index_wr = 0
|
| 179 |
+
for k in range(0,INPUT_RATE):
|
| 180 |
+
#data_wr = (data_wr<<(k-1)*DATA_WIDTH) + random.randint(0, 2**DATA_WIDTH-1)
|
| 181 |
+
data_wr = random.randint(0, 2**DATA_WIDTH-1)
|
| 182 |
+
#index_wr = (index_wr<<(k-1)*INDEX_WIDTH) + random.randint(0, TABLE_SIZE-1)
|
| 183 |
+
index_wr = random.randint(0, TABLE_SIZE-1)
|
| 184 |
+
input_data.append([index_wr,data_wr])
|
| 185 |
+
read_index.append(index_wr)
|
| 186 |
+
await driver.write_burst(input_data)
|
| 187 |
+
await Timer (500, units = 'ns')
|
| 188 |
+
await driver.read_burst(read_index)
|
| 189 |
+
await Timer (500, units = 'ns')
|
| 190 |
+
|
| 191 |
+
if (err_cnt > 0):
|
| 192 |
+
cocotb.log.error("Errors count = %d",err_cnt)
|
| 193 |
+
cocotb.result.test_fail()
|
Weiyet_RTLStructLib/source/Table/tb/sv/Makefile
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
DUT ?= table_top
|
| 3 |
+
SIM_OPTS ?= ../../src/table.sv
|
| 4 |
+
SEED ?= $$(shuf -i 1-10000 -n 1)
|
| 5 |
+
|
| 6 |
+
# DUT parameter #"-p" iverilog command flags
|
| 7 |
+
COMPILE_ARGS = -P tb.TABLE_SIZE=32
|
| 8 |
+
COMPILE_ARGS += -P tb.DATA_WIDTH=8
|
| 9 |
+
COMPILE_ARGS += -P tb.INPUT_RATE=2
|
| 10 |
+
COMPILE_ARGS += -P tb.OUTPUT_RATE=2
|
| 11 |
+
COMPILE_ARGS += -P tb.TB_CLK_PERIOD=100
|
| 12 |
+
COMPILE_ARGS += -P tb.TB_TEST_WEIGHT=1
|
| 13 |
+
COMPILE_ARGS += -P tb.TB_SIM_TIMEOUT=30 #//ms.
|
| 14 |
+
|
| 15 |
+
XILINX_LIB_URL ?= https://github.com/Xilinx/XilinxUnisimLibrary.git
|
| 16 |
+
XILINX_LIB_DIR ?= XilinxUnisimLibrary/verilog/src
|
| 17 |
+
|
| 18 |
+
sim: gen_sim
|
| 19 |
+
./${DUT}.sim +VCDFILE=sim.vcd +VCDLEVEL=0 +SEED=${SEED} | tee sim.log
|
| 20 |
+
|
| 21 |
+
gen_sim: tb.sv ${SIM_OPTS} # to generate executable file by using iverilator
|
| 22 |
+
iverilog -g2012 -s tb ${COMPILE_ARGS} -o ${DUT}.sim $^
|
| 23 |
+
|
| 24 |
+
synth: ${SIM_OPTS}
|
| 25 |
+
yosys -p synth_xilinx ${SIM_OPTS} -L synth.log -o ${DUT}.netlist.v
|
| 26 |
+
#timeout
|
| 27 |
+
|
| 28 |
+
netlist_sim: gen_netlist_sim
|
| 29 |
+
./${DUT}.netlist.sim +VCDFILE=netlist_sim.vcd +VCDLEVEL=0 +SEED=${SEED} | tee netlist_sim.log
|
| 30 |
+
|
| 31 |
+
gen_netlist_sim: tb.sv ${DUT}.netlist.v | ${XILINX_LIBS_DIR}/.git
|
| 32 |
+
iverilog -g2012 -s tb -y ${XILINX_LIB_DIR} -y ${XILINX_LIB_DIR}/unisims -DXILINX_GLS ${COMPILE_ARGS} -o ${DUT}.netlist.sim $^
|
| 33 |
+
|
| 34 |
+
${XILINX_LIBS_DIR}/.git:
|
| 35 |
+
git clone ${XILINX_LIB_URL}
|
| 36 |
+
|
| 37 |
+
clean:
|
| 38 |
+
rm -f ${DUT}_sim sim.log sim.vcd
|
| 39 |
+
rm -f ${DUT}.netlist.v synth.log
|
Weiyet_RTLStructLib/source/Table/tb/sv/tb.sv
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
`timescale 1ns / 1ps
|
| 2 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 3 |
+
//
|
| 4 |
+
// Create Date: 05/05/2024 03:37:34 PM
|
| 5 |
+
// Last Update Date: 01/02/2024 10:37:12 AM
|
| 6 |
+
// Module Name: tb
|
| 7 |
+
// Author: https://www.linkedin.com/in/wei-yet-ng-065485119/
|
| 8 |
+
// Description: 1. table_write_random() --> table_read_compare()
|
| 9 |
+
// Additional Comments:
|
| 10 |
+
//
|
| 11 |
+
//////////////////////////////////////////////////////////////////////////////////
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
module tb(
|
| 15 |
+
);
|
| 16 |
+
parameter TABLE_SIZE = 32;
|
| 17 |
+
parameter DATA_WIDTH = 8;
|
| 18 |
+
parameter INPUT_RATE = 2;
|
| 19 |
+
parameter OUTPUT_RATE = 2;
|
| 20 |
+
parameter TB_CLK_PERIOD = 100;
|
| 21 |
+
parameter TB_TEST_WEIGHT = 1;
|
| 22 |
+
parameter TB_SIM_TIMEOUT = 30; //ms.
|
| 23 |
+
|
| 24 |
+
localparam MAX_DATA = 2**(DATA_WIDTH) - 1;
|
| 25 |
+
localparam INDEX_WIDTH = $clog2(TABLE_SIZE-1);
|
| 26 |
+
|
| 27 |
+
bit [DATA_WIDTH-1:0] table_expected [TABLE_SIZE];
|
| 28 |
+
integer index_queue[$];
|
| 29 |
+
|
| 30 |
+
reg clk=0;
|
| 31 |
+
reg rst=0;
|
| 32 |
+
reg wr_en=0;
|
| 33 |
+
reg rd_en=0;
|
| 34 |
+
reg [INPUT_RATE*$clog2(TABLE_SIZE)-1:0] index_wr=0;
|
| 35 |
+
reg [OUTPUT_RATE*$clog2(TABLE_SIZE)-1:0] index_rd=0;
|
| 36 |
+
reg [INPUT_RATE*DATA_WIDTH-1:0] data_wr=0;
|
| 37 |
+
wire [OUTPUT_RATE*DATA_WIDTH-1:0] data_rd;
|
| 38 |
+
|
| 39 |
+
reg [DATA_WIDTH-1:0] data_wr_rand;
|
| 40 |
+
reg [$clog2(TABLE_SIZE)-1:0] index_wr_rand;
|
| 41 |
+
|
| 42 |
+
integer err_cnt = 0;
|
| 43 |
+
|
| 44 |
+
`ifdef XILINX_GLS
|
| 45 |
+
glbl glbl (); // for Xilinx GLS
|
| 46 |
+
`endif
|
| 47 |
+
|
| 48 |
+
table_top #(
|
| 49 |
+
.TABLE_SIZE(TABLE_SIZE),
|
| 50 |
+
.DATA_WIDTH(DATA_WIDTH),
|
| 51 |
+
.INPUT_RATE(INPUT_RATE),
|
| 52 |
+
.OUTPUT_RATE(OUTPUT_RATE)) DUT (
|
| 53 |
+
/*input wire*/ .clk(clk),
|
| 54 |
+
/*input wire*/ .rst(rst),
|
| 55 |
+
/*input wire*/ .wr_en(wr_en),
|
| 56 |
+
/*input wire*/ .rd_en(rd_en),
|
| 57 |
+
/*input wire [INPUT_RATE*$clog2(TABLE_SIZE)-1:0]*/ .index_wr(index_wr),
|
| 58 |
+
/*input wire [OUTPUT_RATE*$clog2(TABLE_SIZE)-1:0]*/ .index_rd(index_rd),
|
| 59 |
+
/*input wire [INPUT_RATE*DATA_WIDTH-1:0]*/ .data_wr(data_wr),
|
| 60 |
+
/*output reg [OUTPUT_RATE*DATA_WIDTH-1:0]*/ .data_rd(data_rd));
|
| 61 |
+
|
| 62 |
+
always #(TB_CLK_PERIOD>>1) clk = ~clk;
|
| 63 |
+
|
| 64 |
+
task table_write_random (input integer count);
|
| 65 |
+
for (int i=0; i<count; i=i+1) begin
|
| 66 |
+
@(posedge(clk)) begin
|
| 67 |
+
#1;
|
| 68 |
+
for (int j=1; j<=INPUT_RATE; j=j+1) begin
|
| 69 |
+
data_wr_rand = $urandom_range(0,MAX_DATA);
|
| 70 |
+
index_wr_rand = $urandom_range(0,TABLE_SIZE-1);
|
| 71 |
+
data_wr[j*DATA_WIDTH-1 -: DATA_WIDTH] = data_wr_rand;
|
| 72 |
+
index_wr[j*INDEX_WIDTH-1 -:INDEX_WIDTH] = index_wr_rand;
|
| 73 |
+
table_expected[index_wr_rand] = data_wr_rand;
|
| 74 |
+
index_queue.push_back(index_wr_rand);
|
| 75 |
+
$display("%0t table_write_random: Data %0d is written to index %0d", $realtime, data_wr_rand, index_wr_rand);
|
| 76 |
+
end
|
| 77 |
+
wr_en <= 1;
|
| 78 |
+
end
|
| 79 |
+
end
|
| 80 |
+
@(posedge(clk))
|
| 81 |
+
#1; wr_en <= 0;
|
| 82 |
+
endtask
|
| 83 |
+
|
| 84 |
+
integer setup_done = 0;
|
| 85 |
+
|
| 86 |
+
bit [INDEX_WIDTH-1:0] index_target;
|
| 87 |
+
integer index_temp[$];
|
| 88 |
+
|
| 89 |
+
task table_read_compare();
|
| 90 |
+
setup_done = 0;
|
| 91 |
+
while($size(index_queue) != 0) begin
|
| 92 |
+
@(posedge(clk)) begin
|
| 93 |
+
#1;
|
| 94 |
+
rd_en <= 1;
|
| 95 |
+
for (int j=0; j<OUTPUT_RATE; j=j+1) begin
|
| 96 |
+
index_target = index_queue.pop_front();
|
| 97 |
+
index_rd[(j+1)*INDEX_WIDTH-1 -: INDEX_WIDTH] = index_target;
|
| 98 |
+
index_temp.push_back(index_target);
|
| 99 |
+
if(setup_done) begin
|
| 100 |
+
index_target = index_temp.pop_front();
|
| 101 |
+
if(data_rd[(j+1)*DATA_WIDTH-1 -: DATA_WIDTH] !== table_expected[index_target]) begin
|
| 102 |
+
err_cnt = err_cnt + 1;
|
| 103 |
+
$error("%0t table_read_compare: data read at index %0d is incorrect, ACT:%0d , EXP:%0d ", $realtime, index_target, data_rd[(j+1)*DATA_WIDTH-1 -: DATA_WIDTH], table_expected[index_target]);
|
| 104 |
+
end else begin
|
| 105 |
+
$display("%0t table_read_compare: Data %0d is read from index %0d", $realtime, data_rd[(j+1)*DATA_WIDTH-1 -: DATA_WIDTH], index_target);
|
| 106 |
+
end
|
| 107 |
+
end
|
| 108 |
+
end
|
| 109 |
+
setup_done = 1;
|
| 110 |
+
end
|
| 111 |
+
end
|
| 112 |
+
//final index_queue request
|
| 113 |
+
@(posedge(clk)) begin
|
| 114 |
+
#1;
|
| 115 |
+
for (int j=0; j<OUTPUT_RATE; j=j+1) begin
|
| 116 |
+
if(setup_done) begin
|
| 117 |
+
index_target = index_temp.pop_front();
|
| 118 |
+
if(data_rd[(j+1)*DATA_WIDTH-1 -: DATA_WIDTH] !== table_expected[index_target]) begin
|
| 119 |
+
err_cnt = err_cnt + 1;
|
| 120 |
+
$error("%0t table_read_compare: data read at index %0d is incorrect, ACT:%0d , EXP:%0d ", $realtime, index_target, data_rd[(j+1)*DATA_WIDTH-1 -: DATA_WIDTH], table_expected[index_target]);
|
| 121 |
+
end else begin
|
| 122 |
+
$display("%0t table_read_compare: Data %0d is read from index %0d", $realtime, data_rd[(j+1)*DATA_WIDTH-1 -: DATA_WIDTH], index_target);
|
| 123 |
+
end
|
| 124 |
+
end
|
| 125 |
+
end
|
| 126 |
+
setup_done = 1;
|
| 127 |
+
rd_en <= 0;
|
| 128 |
+
end
|
| 129 |
+
endtask
|
| 130 |
+
|
| 131 |
+
initial begin
|
| 132 |
+
string vcdfile;
|
| 133 |
+
int vcdlevel;
|
| 134 |
+
int seed;
|
| 135 |
+
int temp;
|
| 136 |
+
|
| 137 |
+
rst = 1'b1;
|
| 138 |
+
if ($value$plusargs("VCDFILE=%s",vcdfile))
|
| 139 |
+
$dumpfile(vcdfile);
|
| 140 |
+
if ($value$plusargs("VCDLEVEL=%d",vcdlevel))
|
| 141 |
+
$dumpvars(vcdlevel,tb);
|
| 142 |
+
if ($value$plusargs("SEED=%d",seed)) begin
|
| 143 |
+
temp = $urandom(seed);
|
| 144 |
+
$display("Seed = %d",seed);
|
| 145 |
+
end
|
| 146 |
+
// rst = 1;
|
| 147 |
+
// #100
|
| 148 |
+
// rst = 0;
|
| 149 |
+
repeat(TB_TEST_WEIGHT) begin
|
| 150 |
+
rst = 1;
|
| 151 |
+
#100
|
| 152 |
+
for (int i=0; i<TABLE_SIZE; i=i+1) begin
|
| 153 |
+
table_expected[i] = 'b0;
|
| 154 |
+
end
|
| 155 |
+
rst = 0;
|
| 156 |
+
$display("\n%0t TABLE RANDOM WRITE SEQ",$realtime);
|
| 157 |
+
table_write_random(20);
|
| 158 |
+
#1000;
|
| 159 |
+
$display("\n%0t TABLE READ SEQ",$realtime);
|
| 160 |
+
table_read_compare();
|
| 161 |
+
#1000;
|
| 162 |
+
end
|
| 163 |
+
|
| 164 |
+
#1000;
|
| 165 |
+
if (err_cnt > 0) begin
|
| 166 |
+
$display("\n%0t TEST FAILED",$realtime);
|
| 167 |
+
$display("Error count = %d\n", err_cnt);
|
| 168 |
+
end else
|
| 169 |
+
$display("\n%0t TEST PASSED\n", $realtime);
|
| 170 |
+
$finish;
|
| 171 |
+
end
|
| 172 |
+
|
| 173 |
+
initial begin
|
| 174 |
+
#((TB_SIM_TIMEOUT)* 1ms);
|
| 175 |
+
$display("\n%0t TEST FAILED", $realtime);
|
| 176 |
+
$display("SIM TIMEOUT!\n");
|
| 177 |
+
$finish;
|
| 178 |
+
end
|
| 179 |
+
|
| 180 |
+
endmodule
|
| 181 |
+
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/Makefile
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Makefile for Table pyUVM VIP
|
| 2 |
+
|
| 3 |
+
SIM ?= icarus
|
| 4 |
+
TOPLEVEL_LANG ?= verilog
|
| 5 |
+
WAVES ?= 1
|
| 6 |
+
|
| 7 |
+
PWD=$(shell pwd)
|
| 8 |
+
|
| 9 |
+
# RTL source files
|
| 10 |
+
VERILOG_SOURCES = $(PWD)/../../src/table.sv
|
| 11 |
+
|
| 12 |
+
TOPLEVEL = table_top
|
| 13 |
+
MODULE = tb_table
|
| 14 |
+
|
| 15 |
+
# DUT parameters
|
| 16 |
+
COMPILE_ARGS = -Ptable_top.TABLE_SIZE=32
|
| 17 |
+
COMPILE_ARGS += -Ptable_top.DATA_WIDTH=8
|
| 18 |
+
COMPILE_ARGS += -Ptable_top.INPUT_RATE=2
|
| 19 |
+
COMPILE_ARGS += -Ptable_top.OUTPUT_RATE=2
|
| 20 |
+
|
| 21 |
+
COCOTB_HDL_TIMEUNIT = 1ns
|
| 22 |
+
COCOTB_HDL_TIMEPRECISION = 1ps
|
| 23 |
+
|
| 24 |
+
# Waveform dump
|
| 25 |
+
ifeq ($(SIM), icarus)
|
| 26 |
+
ifeq ($(WAVES), 1)
|
| 27 |
+
$(shell echo 'module iverilog_dump();' > iverilog_dump.v)
|
| 28 |
+
$(shell echo 'initial begin' >> iverilog_dump.v)
|
| 29 |
+
$(shell echo ' $$dumpfile("$(TOPLEVEL).vcd");' >> iverilog_dump.v)
|
| 30 |
+
$(shell echo ' $$dumpvars(0, $(TOPLEVEL));' >> iverilog_dump.v)
|
| 31 |
+
$(shell echo 'end' >> iverilog_dump.v)
|
| 32 |
+
$(shell echo 'endmodule' >> iverilog_dump.v)
|
| 33 |
+
VERILOG_SOURCES += $(PWD)/iverilog_dump.v
|
| 34 |
+
COMPILE_ARGS += -s iverilog_dump
|
| 35 |
+
endif
|
| 36 |
+
endif
|
| 37 |
+
|
| 38 |
+
include $(shell cocotb-config --makefiles)/Makefile.sim
|
| 39 |
+
|
| 40 |
+
.PHONY: help
|
| 41 |
+
help:
|
| 42 |
+
@echo "Table pyUVM VIP Makefile"
|
| 43 |
+
@echo "========================"
|
| 44 |
+
@echo ""
|
| 45 |
+
@echo "Usage:"
|
| 46 |
+
@echo " make - Run test with Icarus Verilog"
|
| 47 |
+
@echo " make WAVES=0 - Disable waveforms"
|
| 48 |
+
@echo " make clean - Clean build files"
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Table pyUVM VIP User Guide
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
Complete pyUVM-based Verification IP for **Table** - multi-port read/write memory structure supporting simultaneous operations.
|
| 6 |
+
|
| 7 |
+
## Quick Start
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
cd Table/vip/pyuvm
|
| 11 |
+
make # Run test
|
| 12 |
+
gtkwave table_top.vcd # View waveforms
|
| 13 |
+
```
|
| 14 |
+
|
| 15 |
+
## Table Features
|
| 16 |
+
|
| 17 |
+
**Key Capability**: Multi-port table with 2 simultaneous write ports and 2 simultaneous read ports
|
| 18 |
+
|
| 19 |
+
| Signal | Description | Width |
|
| 20 |
+
|--------|-------------|-------|
|
| 21 |
+
| `wr_en` | Write enable (2 ports) | 2 |
|
| 22 |
+
| `index_wr` | Write indices (packed) | 10 ([9:5]=idx[1], [4:0]=idx[0]) |
|
| 23 |
+
| `data_wr` | Write data (packed) | 16 ([15:8]=data[1], [7:0]=data[0]) |
|
| 24 |
+
| `rd_en` | Read enable | 1 |
|
| 25 |
+
| `index_rd` | Read indices (packed) | 10 ([9:5]=idx[1], [4:0]=idx[0]) |
|
| 26 |
+
| `data_rd` | Read data (packed) | 16 ([15:8]=data[1], [7:0]=data[0]) |
|
| 27 |
+
|
| 28 |
+
## Configuration
|
| 29 |
+
|
| 30 |
+
```python
|
| 31 |
+
cfg = TableVipConfig("cfg")
|
| 32 |
+
cfg.TABLE_SIZE = 32
|
| 33 |
+
cfg.DATA_WIDTH = 8
|
| 34 |
+
cfg.INPUT_RATE = 2 # Number of write ports
|
| 35 |
+
cfg.OUTPUT_RATE = 2 # Number of read ports
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Self-Checking
|
| 39 |
+
|
| 40 |
+
The scoreboard uses a Python dictionary as the table reference model:
|
| 41 |
+
- WRITE: Updates `table_model[index] = data` for each enabled write port
|
| 42 |
+
- READ: Compares `data_rd` with `table_model[index]` for each read port
|
| 43 |
+
|
| 44 |
+
## Example
|
| 45 |
+
|
| 46 |
+
```python
|
| 47 |
+
from sequences.table_vip_write_seq import TableVipWriteSeq
|
| 48 |
+
from sequences.table_vip_read_seq import TableVipReadSeq
|
| 49 |
+
|
| 50 |
+
# Write data
|
| 51 |
+
write_seq = TableVipWriteSeq("write_seq")
|
| 52 |
+
write_seq.num_trans = 10
|
| 53 |
+
await write_seq.start(env.get_sequencer())
|
| 54 |
+
|
| 55 |
+
# Read data
|
| 56 |
+
read_seq = TableVipReadSeq("read_seq")
|
| 57 |
+
read_seq.num_trans = 5
|
| 58 |
+
await read_seq.start(env.get_sequencer())
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
## Available Sequences
|
| 62 |
+
|
| 63 |
+
- `TableVipWriteSeq` - Write-only operations (multi-port)
|
| 64 |
+
- `TableVipReadSeq` - Read-only operations (multi-port)
|
| 65 |
+
- `TableVipRandomSeq` - Random READ/WRITE mix
|
| 66 |
+
|
| 67 |
+
## Use Cases
|
| 68 |
+
|
| 69 |
+
- Multi-port memory verification
|
| 70 |
+
- Lookup table testing
|
| 71 |
+
- Simultaneous read/write validation
|
| 72 |
+
- Register file verification
|
| 73 |
+
|
| 74 |
+
**Happy Verifying!**
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP - pyUVM Verification IP for Table (Multi-port Read/Write Memory)"""
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Agent Components"""
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_agent.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Agent"""
|
| 2 |
+
from pyuvm import uvm_agent
|
| 3 |
+
from .table_vip_driver import TableVipDriver
|
| 4 |
+
from .table_vip_monitor import TableVipMonitor
|
| 5 |
+
from .table_vip_sequencer import TableVipSequencer
|
| 6 |
+
|
| 7 |
+
class TableVipAgent(uvm_agent):
|
| 8 |
+
def __init__(self, name, parent):
|
| 9 |
+
super().__init__(name, parent)
|
| 10 |
+
self.driver = None
|
| 11 |
+
self.monitor = None
|
| 12 |
+
self.sequencer = None
|
| 13 |
+
self.cfg = None
|
| 14 |
+
|
| 15 |
+
def build_phase(self):
|
| 16 |
+
super().build_phase()
|
| 17 |
+
from pyuvm import ConfigDB
|
| 18 |
+
arr = []
|
| 19 |
+
if not ConfigDB().get(self, "", "table_vip_cfg", arr):
|
| 20 |
+
self.logger.critical("No config found in ConfigDB!")
|
| 21 |
+
else:
|
| 22 |
+
self.cfg = arr[0]
|
| 23 |
+
|
| 24 |
+
# Always create monitor
|
| 25 |
+
self.monitor = TableVipMonitor("monitor", self)
|
| 26 |
+
|
| 27 |
+
# Create driver and sequencer (always active for table)
|
| 28 |
+
self.driver = TableVipDriver("driver", self)
|
| 29 |
+
self.sequencer = TableVipSequencer("sequencer", self)
|
| 30 |
+
|
| 31 |
+
def connect_phase(self):
|
| 32 |
+
super().connect_phase()
|
| 33 |
+
self.driver.seq_item_port.connect(self.sequencer.seq_item_export)
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_driver.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Driver"""
|
| 2 |
+
from pyuvm import uvm_driver
|
| 3 |
+
from cocotb.triggers import RisingEdge
|
| 4 |
+
from ..common.table_vip_seq_item import TableVipSeqItem
|
| 5 |
+
from ..common import TableOp
|
| 6 |
+
|
| 7 |
+
class TableVipDriver(uvm_driver):
|
| 8 |
+
def __init__(self, name, parent):
|
| 9 |
+
super().__init__(name, parent)
|
| 10 |
+
self.dut = None
|
| 11 |
+
self.cfg = None
|
| 12 |
+
|
| 13 |
+
def build_phase(self):
|
| 14 |
+
super().build_phase()
|
| 15 |
+
from pyuvm import ConfigDB
|
| 16 |
+
arr = []
|
| 17 |
+
if not ConfigDB().get(self, "", "table_vip_dut", arr):
|
| 18 |
+
self.logger.critical("No DUT found in ConfigDB!")
|
| 19 |
+
else:
|
| 20 |
+
self.dut = arr[0]
|
| 21 |
+
|
| 22 |
+
arr = []
|
| 23 |
+
if not ConfigDB().get(self, "", "table_vip_cfg", arr):
|
| 24 |
+
self.logger.critical("No config found in ConfigDB!")
|
| 25 |
+
else:
|
| 26 |
+
self.cfg = arr[0]
|
| 27 |
+
|
| 28 |
+
async def run_phase(self):
|
| 29 |
+
# Initialize signals
|
| 30 |
+
self.dut.wr_en.value = 0
|
| 31 |
+
self.dut.rd_en.value = 0
|
| 32 |
+
self.dut.index_wr.value = 0
|
| 33 |
+
self.dut.index_rd.value = 0
|
| 34 |
+
self.dut.data_wr.value = 0
|
| 35 |
+
|
| 36 |
+
while True:
|
| 37 |
+
item = await self.seq_item_port.get_next_item()
|
| 38 |
+
await self.drive_item(item)
|
| 39 |
+
self.seq_item_port.item_done()
|
| 40 |
+
|
| 41 |
+
async def drive_item(self, item):
|
| 42 |
+
"""Drive transaction to DUT"""
|
| 43 |
+
if item.op == TableOp.WRITE:
|
| 44 |
+
await self.drive_write(item)
|
| 45 |
+
else:
|
| 46 |
+
await self.drive_read(item)
|
| 47 |
+
|
| 48 |
+
async def drive_write(self, item):
|
| 49 |
+
"""Drive WRITE operation"""
|
| 50 |
+
await RisingEdge(self.dut.clk)
|
| 51 |
+
|
| 52 |
+
# Pack write enables (2 bits)
|
| 53 |
+
wr_en_val = (item.wr_en[1] << 1) | item.wr_en[0]
|
| 54 |
+
self.dut.wr_en.value = wr_en_val
|
| 55 |
+
self.dut.rd_en.value = 0
|
| 56 |
+
|
| 57 |
+
# Pack write indices (10 bits: [9:5] for idx[1], [4:0] for idx[0])
|
| 58 |
+
index_wr_val = (item.index_wr[1] << 5) | item.index_wr[0]
|
| 59 |
+
self.dut.index_wr.value = index_wr_val
|
| 60 |
+
|
| 61 |
+
# Pack write data (16 bits: [15:8] for data[1], [7:0] for data[0])
|
| 62 |
+
data_wr_val = (item.data_wr[1] << 8) | item.data_wr[0]
|
| 63 |
+
self.dut.data_wr.value = data_wr_val
|
| 64 |
+
|
| 65 |
+
await RisingEdge(self.dut.clk)
|
| 66 |
+
self.dut.wr_en.value = 0
|
| 67 |
+
|
| 68 |
+
self.logger.debug(f"WRITE: wr_en=0x{wr_en_val:x} idx[0]={item.index_wr[0]} "
|
| 69 |
+
f"data[0]=0x{item.data_wr[0]:x} idx[1]={item.index_wr[1]} "
|
| 70 |
+
f"data[1]=0x{item.data_wr[1]:x}")
|
| 71 |
+
|
| 72 |
+
async def drive_read(self, item):
|
| 73 |
+
"""Drive READ operation"""
|
| 74 |
+
await RisingEdge(self.dut.clk)
|
| 75 |
+
self.dut.rd_en.value = 1
|
| 76 |
+
self.dut.wr_en.value = 0
|
| 77 |
+
|
| 78 |
+
# Pack read indices (10 bits: [9:5] for idx[1], [4:0] for idx[0])
|
| 79 |
+
index_rd_val = (item.index_rd[1] << 5) | item.index_rd[0]
|
| 80 |
+
self.dut.index_rd.value = index_rd_val
|
| 81 |
+
|
| 82 |
+
await RisingEdge(self.dut.clk)
|
| 83 |
+
|
| 84 |
+
# Unpack read data
|
| 85 |
+
data_rd_val = int(self.dut.data_rd.value)
|
| 86 |
+
item.data_rd[0] = data_rd_val & 0xFF
|
| 87 |
+
item.data_rd[1] = (data_rd_val >> 8) & 0xFF
|
| 88 |
+
|
| 89 |
+
self.dut.rd_en.value = 0
|
| 90 |
+
|
| 91 |
+
self.logger.debug(f"READ: idx[0]={item.index_rd[0]} data[0]=0x{item.data_rd[0]:x} "
|
| 92 |
+
f"idx[1]={item.index_rd[1]} data[1]=0x{item.data_rd[1]:x}")
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_monitor.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Monitor"""
|
| 2 |
+
from pyuvm import uvm_monitor
|
| 3 |
+
from cocotb.triggers import RisingEdge
|
| 4 |
+
from ..common.table_vip_seq_item import TableVipSeqItem
|
| 5 |
+
from ..common import TableOp
|
| 6 |
+
|
| 7 |
+
class TableVipMonitor(uvm_monitor):
|
| 8 |
+
def __init__(self, name, parent):
|
| 9 |
+
super().__init__(name, parent)
|
| 10 |
+
self.dut = None
|
| 11 |
+
self.cfg = None
|
| 12 |
+
from pyuvm import uvm_analysis_port
|
| 13 |
+
self.ap = uvm_analysis_port("ap", self)
|
| 14 |
+
|
| 15 |
+
def build_phase(self):
|
| 16 |
+
super().build_phase()
|
| 17 |
+
from pyuvm import ConfigDB
|
| 18 |
+
arr = []
|
| 19 |
+
if not ConfigDB().get(self, "", "table_vip_dut", arr):
|
| 20 |
+
self.logger.critical("No DUT found in ConfigDB!")
|
| 21 |
+
else:
|
| 22 |
+
self.dut = arr[0]
|
| 23 |
+
|
| 24 |
+
arr = []
|
| 25 |
+
if not ConfigDB().get(self, "", "table_vip_cfg", arr):
|
| 26 |
+
self.logger.critical("No config found in ConfigDB!")
|
| 27 |
+
else:
|
| 28 |
+
self.cfg = arr[0]
|
| 29 |
+
|
| 30 |
+
async def run_phase(self):
|
| 31 |
+
while True:
|
| 32 |
+
await RisingEdge(self.dut.clk)
|
| 33 |
+
|
| 34 |
+
wr_en_val = int(self.dut.wr_en.value)
|
| 35 |
+
rd_en_val = int(self.dut.rd_en.value)
|
| 36 |
+
|
| 37 |
+
# Detect write operation
|
| 38 |
+
if wr_en_val != 0:
|
| 39 |
+
item = TableVipSeqItem("item")
|
| 40 |
+
item.op = TableOp.WRITE
|
| 41 |
+
item.wr_en = [wr_en_val & 1, (wr_en_val >> 1) & 1]
|
| 42 |
+
item.rd_en = 0
|
| 43 |
+
|
| 44 |
+
# Unpack write indices
|
| 45 |
+
index_wr_val = int(self.dut.index_wr.value)
|
| 46 |
+
item.index_wr[0] = index_wr_val & 0x1F
|
| 47 |
+
item.index_wr[1] = (index_wr_val >> 5) & 0x1F
|
| 48 |
+
|
| 49 |
+
# Unpack write data
|
| 50 |
+
data_wr_val = int(self.dut.data_wr.value)
|
| 51 |
+
item.data_wr[0] = data_wr_val & 0xFF
|
| 52 |
+
item.data_wr[1] = (data_wr_val >> 8) & 0xFF
|
| 53 |
+
|
| 54 |
+
self.ap.write(item)
|
| 55 |
+
self.logger.debug(f"Observed {item}")
|
| 56 |
+
|
| 57 |
+
# Detect read operation
|
| 58 |
+
if rd_en_val != 0:
|
| 59 |
+
item = TableVipSeqItem("item")
|
| 60 |
+
item.op = TableOp.READ
|
| 61 |
+
item.rd_en = 1
|
| 62 |
+
item.wr_en = [0, 0]
|
| 63 |
+
|
| 64 |
+
# Unpack read indices
|
| 65 |
+
index_rd_val = int(self.dut.index_rd.value)
|
| 66 |
+
item.index_rd[0] = index_rd_val & 0x1F
|
| 67 |
+
item.index_rd[1] = (index_rd_val >> 5) & 0x1F
|
| 68 |
+
|
| 69 |
+
await RisingEdge(self.dut.clk)
|
| 70 |
+
|
| 71 |
+
# Unpack read data
|
| 72 |
+
data_rd_val = int(self.dut.data_rd.value)
|
| 73 |
+
item.data_rd[0] = data_rd_val & 0xFF
|
| 74 |
+
item.data_rd[1] = (data_rd_val >> 8) & 0xFF
|
| 75 |
+
|
| 76 |
+
self.ap.write(item)
|
| 77 |
+
self.logger.debug(f"Observed {item}")
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/agent/table_vip_sequencer.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Sequencer"""
|
| 2 |
+
from pyuvm import uvm_sequencer
|
| 3 |
+
from ..common.table_vip_seq_item import TableVipSeqItem
|
| 4 |
+
|
| 5 |
+
class TableVipSequencer(uvm_sequencer):
|
| 6 |
+
def __init__(self, name, parent):
|
| 7 |
+
super().__init__(name, parent)
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/common/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Common Components"""
|
| 2 |
+
from enum import IntEnum
|
| 3 |
+
|
| 4 |
+
class TableOp(IntEnum):
|
| 5 |
+
"""Table Operation Types"""
|
| 6 |
+
WRITE = 0
|
| 7 |
+
READ = 1
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/common/table_vip_config.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Configuration"""
|
| 2 |
+
from pyuvm import uvm_object
|
| 3 |
+
|
| 4 |
+
class TableVipConfig(uvm_object):
|
| 5 |
+
def __init__(self, name="table_vip_config"):
|
| 6 |
+
super().__init__(name)
|
| 7 |
+
# DUT parameters
|
| 8 |
+
self.TABLE_SIZE = 32
|
| 9 |
+
self.DATA_WIDTH = 8
|
| 10 |
+
self.INPUT_RATE = 2
|
| 11 |
+
self.OUTPUT_RATE = 2
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/common/table_vip_seq_item.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Sequence Item"""
|
| 2 |
+
from pyuvm import uvm_sequence_item
|
| 3 |
+
from . import TableOp
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
class TableVipSeqItem(uvm_sequence_item):
|
| 7 |
+
def __init__(self, name="table_vip_seq_item"):
|
| 8 |
+
super().__init__(name)
|
| 9 |
+
# Operation type
|
| 10 |
+
self.op = TableOp.WRITE
|
| 11 |
+
|
| 12 |
+
# Write operation (supports multiple writes)
|
| 13 |
+
self.wr_en = [0, 0] # 2 write enables
|
| 14 |
+
self.index_wr = [0, 0] # 2 write indices
|
| 15 |
+
self.data_wr = [0, 0] # 2 write data
|
| 16 |
+
|
| 17 |
+
# Read operation (supports multiple reads)
|
| 18 |
+
self.rd_en = 0
|
| 19 |
+
self.index_rd = [0, 0] # 2 read indices
|
| 20 |
+
|
| 21 |
+
# Read results
|
| 22 |
+
self.data_rd = [0, 0] # 2 read data
|
| 23 |
+
|
| 24 |
+
# Config reference
|
| 25 |
+
self.cfg = None
|
| 26 |
+
|
| 27 |
+
def randomize(self):
|
| 28 |
+
"""Randomize transaction fields"""
|
| 29 |
+
# Randomize operation
|
| 30 |
+
self.op = random.choice([TableOp.WRITE, TableOp.READ])
|
| 31 |
+
|
| 32 |
+
if self.op == TableOp.WRITE:
|
| 33 |
+
# At least one write enable
|
| 34 |
+
self.wr_en = [random.randint(0, 1), random.randint(0, 1)]
|
| 35 |
+
if self.wr_en[0] == 0 and self.wr_en[1] == 0:
|
| 36 |
+
self.wr_en[0] = 1 # Ensure at least one write
|
| 37 |
+
self.rd_en = 0
|
| 38 |
+
|
| 39 |
+
# Randomize write indices (valid range)
|
| 40 |
+
table_size = self.cfg.TABLE_SIZE if self.cfg else 32
|
| 41 |
+
self.index_wr = [random.randint(0, table_size-1),
|
| 42 |
+
random.randint(0, table_size-1)]
|
| 43 |
+
|
| 44 |
+
# Randomize write data
|
| 45 |
+
max_val = (1 << (self.cfg.DATA_WIDTH if self.cfg else 8)) - 1
|
| 46 |
+
self.data_wr = [random.randint(0, max_val),
|
| 47 |
+
random.randint(0, max_val)]
|
| 48 |
+
|
| 49 |
+
else: # READ
|
| 50 |
+
self.rd_en = 1
|
| 51 |
+
self.wr_en = [0, 0]
|
| 52 |
+
|
| 53 |
+
# Randomize read indices
|
| 54 |
+
table_size = self.cfg.TABLE_SIZE if self.cfg else 32
|
| 55 |
+
self.index_rd = [random.randint(0, table_size-1),
|
| 56 |
+
random.randint(0, table_size-1)]
|
| 57 |
+
|
| 58 |
+
return True
|
| 59 |
+
|
| 60 |
+
def __str__(self):
|
| 61 |
+
if self.op == TableOp.WRITE:
|
| 62 |
+
s = "WRITE: "
|
| 63 |
+
if self.wr_en[0]:
|
| 64 |
+
s += f"idx[0]={self.index_wr[0]} data[0]=0x{self.data_wr[0]:x} "
|
| 65 |
+
if self.wr_en[1]:
|
| 66 |
+
s += f"idx[1]={self.index_wr[1]} data[1]=0x{self.data_wr[1]:x} "
|
| 67 |
+
return s
|
| 68 |
+
else:
|
| 69 |
+
return (f"READ: idx[0]={self.index_rd[0]} data[0]=0x{self.data_rd[0]:x} "
|
| 70 |
+
f"idx[1]={self.index_rd[1]} data[1]=0x{self.data_rd[1]:x}")
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/env/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Environment Components"""
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/env/table_vip_env.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Environment"""
|
| 2 |
+
from pyuvm import uvm_env, ConfigDB
|
| 3 |
+
from ..agent.table_vip_agent import TableVipAgent
|
| 4 |
+
from .table_vip_scoreboard import TableVipScoreboard
|
| 5 |
+
|
| 6 |
+
class TableVipEnv(uvm_env):
|
| 7 |
+
def __init__(self, name, parent):
|
| 8 |
+
super().__init__(name, parent)
|
| 9 |
+
self.agent = None
|
| 10 |
+
self.scoreboard = None
|
| 11 |
+
self.cfg = None
|
| 12 |
+
|
| 13 |
+
def build_phase(self):
|
| 14 |
+
super().build_phase()
|
| 15 |
+
arr = []
|
| 16 |
+
if not ConfigDB().get(self, "", "table_vip_cfg", arr):
|
| 17 |
+
self.logger.critical("No config found in ConfigDB!")
|
| 18 |
+
else:
|
| 19 |
+
self.cfg = arr[0]
|
| 20 |
+
|
| 21 |
+
# Create agent and scoreboard
|
| 22 |
+
self.agent = TableVipAgent("agent", self)
|
| 23 |
+
self.scoreboard = TableVipScoreboard("scoreboard", self)
|
| 24 |
+
|
| 25 |
+
def connect_phase(self):
|
| 26 |
+
super().connect_phase()
|
| 27 |
+
self.agent.monitor.ap.connect(self.scoreboard.analysis_export)
|
| 28 |
+
|
| 29 |
+
def get_sequencer(self):
|
| 30 |
+
"""Return sequencer for use by sequences"""
|
| 31 |
+
if self.agent and self.agent.sequencer:
|
| 32 |
+
return self.agent.sequencer
|
| 33 |
+
return None
|
Weiyet_RTLStructLib/source/Table/vip/pyuvm/env/table_vip_scoreboard.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Table VIP Scoreboard"""
|
| 2 |
+
from pyuvm import uvm_scoreboard, uvm_analysis_imp
|
| 3 |
+
from ..common import TableOp
|
| 4 |
+
|
| 5 |
+
class TableVipScoreboard(uvm_scoreboard):
|
| 6 |
+
def __init__(self, name, parent):
|
| 7 |
+
super().__init__(name, parent)
|
| 8 |
+
self.analysis_export = uvm_analysis_imp("analysis_export", self)
|
| 9 |
+
self.cfg = None
|
| 10 |
+
self.table_model = {} # Python dict as table reference model
|
| 11 |
+
self.write_count = 0
|
| 12 |
+
self.read_count = 0
|
| 13 |
+
|
| 14 |
+
def build_phase(self):
|
| 15 |
+
super().build_phase()
|
| 16 |
+
from pyuvm import ConfigDB
|
| 17 |
+
arr = []
|
| 18 |
+
if not ConfigDB().get(self, "", "table_vip_cfg", arr):
|
| 19 |
+
self.logger.critical("No config found in ConfigDB!")
|
| 20 |
+
else:
|
| 21 |
+
self.cfg = arr[0]
|
| 22 |
+
|
| 23 |
+
# Initialize table model
|
| 24 |
+
for i in range(32):
|
| 25 |
+
self.table_model[i] = 0x00
|
| 26 |
+
|
| 27 |
+
def write(self, item):
|
| 28 |
+
"""Receive transaction from monitor"""
|
| 29 |
+
self.logger.info(f"Checking: {item}")
|
| 30 |
+
|
| 31 |
+
if item.op == TableOp.WRITE:
|
| 32 |
+
self.check_write(item)
|
| 33 |
+
elif item.op == TableOp.READ:
|
| 34 |
+
self.check_read(item)
|
| 35 |
+
|
| 36 |
+
def check_write(self, item):
|
| 37 |
+
"""Check WRITE operation"""
|
| 38 |
+
self.write_count += 1
|
| 39 |
+
|
| 40 |
+
# Update model based on write enables
|
| 41 |
+
if item.wr_en[0]:
|
| 42 |
+
self.table_model[item.index_wr[0]] = item.data_wr[0]
|
| 43 |
+
self.logger.debug(f"Updated table[{item.index_wr[0]}] = 0x{item.data_wr[0]:x}")
|
| 44 |
+
|
| 45 |
+
if item.wr_en[1]:
|
| 46 |
+
self.table_model[item.index_wr[1]] = item.data_wr[1]
|
| 47 |
+
self.logger.debug(f"Updated table[{item.index_wr[1]}] = 0x{item.data_wr[1]:x}")
|
| 48 |
+
|
| 49 |
+
def check_read(self, item):
|
| 50 |
+
"""Check READ operation"""
|
| 51 |
+
self.read_count += 1
|
| 52 |
+
|
| 53 |
+
# Check read data[0]
|
| 54 |
+
if item.data_rd[0] != self.table_model[item.index_rd[0]]:
|
| 55 |
+
self.logger.error(f"Read[0] mismatch at index {item.index_rd[0]}: "
|
| 56 |
+
f"expected=0x{self.table_model[item.index_rd[0]]:x} "
|
| 57 |
+
f"actual=0x{item.data_rd[0]:x}")
|
| 58 |
+
else:
|
| 59 |
+
self.logger.debug(f"Read[0] matched: table[{item.index_rd[0]}] = 0x{item.data_rd[0]:x}")
|
| 60 |
+
|
| 61 |
+
# Check read data[1]
|
| 62 |
+
if item.data_rd[1] != self.table_model[item.index_rd[1]]:
|
| 63 |
+
self.logger.error(f"Read[1] mismatch at index {item.index_rd[1]}: "
|
| 64 |
+
f"expected=0x{self.table_model[item.index_rd[1]]:x} "
|
| 65 |
+
f"actual=0x{item.data_rd[1]:x}")
|
| 66 |
+
else:
|
| 67 |
+
self.logger.debug(f"Read[1] matched: table[{item.index_rd[1]}] = 0x{item.data_rd[1]:x}")
|
| 68 |
+
|
| 69 |
+
def report_phase(self):
|
| 70 |
+
self.logger.info("=" * 50)
|
| 71 |
+
self.logger.info("=== Table Statistics ===")
|
| 72 |
+
self.logger.info(f"Total Writes: {self.write_count}")
|
| 73 |
+
self.logger.info(f"Total Reads: {self.read_count}")
|
| 74 |
+
self.logger.info("=" * 50)
|