|
| 1 | +// *************************************************************************** |
| 2 | +// *************************************************************************** |
| 3 | +// Copyright (C) 2026 Analog Devices, Inc. All rights reserved. |
| 4 | +// |
| 5 | +// In this HDL repository, there are many different and unique modules, consisting |
| 6 | +// of various HDL (Verilog or VHDL) components. The individual modules are |
| 7 | +// developed independently, and may be accompanied by separate and unique license |
| 8 | +// terms. |
| 9 | +// |
| 10 | +// The user should read each of these license terms, and understand the |
| 11 | +// freedoms and responsibilities that he or she has by using this source/core. |
| 12 | +// |
| 13 | +// This core is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 15 | +// A PARTICULAR PURPOSE. |
| 16 | +// |
| 17 | +// Redistribution and use of source or resulting binaries, with or without modification |
| 18 | +// of this file, are permitted under one of the following two license terms: |
| 19 | +// |
| 20 | +// 1. The GNU General Public License version 2 as published by the |
| 21 | +// Free Software Foundation, which can be found in the top level directory |
| 22 | +// of this repository (LICENSE_GPL2), and also online at: |
| 23 | +// <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html> |
| 24 | +// |
| 25 | +// OR |
| 26 | +// |
| 27 | +// 2. An ADI specific BSD license, which can be found in the top level directory |
| 28 | +// of this repository (LICENSE_ADIBSD), and also on-line at: |
| 29 | +// https://github.com/analogdevicesinc/hdl/blob/main/LICENSE_ADIBSD |
| 30 | +// This will allow to generate bit files and not release the source code, |
| 31 | +// as long as it attaches to an ADI device. |
| 32 | +// |
| 33 | +// *************************************************************************** |
| 34 | +// *************************************************************************** |
| 35 | + |
| 36 | +`timescale 1ns/100ps |
| 37 | +module cpack_32ch_tb; |
| 38 | + parameter VCD_FILE = {"cpack_32ch_tb.vcd"}; |
| 39 | + parameter NUM_OF_CHANNELS = 32; |
| 40 | + parameter SAMPLES_PER_CHANNEL = 1; |
| 41 | + parameter ENABLE_RANDOM = 0; |
| 42 | + parameter PIPELINE_STAGES = 2; |
| 43 | + |
| 44 | + `define TIMEOUT 10000000 |
| 45 | + `include "tb_base.v" |
| 46 | + |
| 47 | + localparam NUM_OF_PORTS = SAMPLES_PER_CHANNEL * NUM_OF_CHANNELS; |
| 48 | + |
| 49 | + reg fifo_wr_en = 1'b1; |
| 50 | + reg [NUM_OF_PORTS*16-1:0] fifo_wr_data = 'h00; |
| 51 | + |
| 52 | + wire packed_fifo_wr_en; |
| 53 | + wire [NUM_OF_PORTS*16-1:0] packed_fifo_wr_data; |
| 54 | + reg [NUM_OF_PORTS*16-1:0] expected_packed_fifo_wr_data; |
| 55 | + |
| 56 | + reg [NUM_OF_CHANNELS-1:0] enable = 'h1; |
| 57 | + |
| 58 | + integer counter; |
| 59 | + integer test_index; |
| 60 | + |
| 61 | + // Test representative enable masks instead of all 2^32 combinations |
| 62 | + // Include all channels, single channels, non_power_of_two configs |
| 63 | + reg [NUM_OF_CHANNELS-1:0] test_enables [0:31]; |
| 64 | + initial begin |
| 65 | + test_enables[0] = 32'hFFFFFFFF; // All 32 channels |
| 66 | + test_enables[1] = 32'h7FFFFFFF; // 31 channels (non-pow2) |
| 67 | + test_enables[2] = 32'hFFFFFFFE; // 31 channels (different pattern) |
| 68 | + test_enables[3] = 32'h55555555; // Alternating pattern (16 channels) |
| 69 | + test_enables[4] = 32'hAAAAAAAA; // Alternating inverse (16 channels) |
| 70 | + test_enables[5] = 32'h0000FFFF; // Lower 16 channels |
| 71 | + test_enables[6] = 32'hFFFF0000; // Upper 16 channels |
| 72 | + test_enables[7] = 32'h00FF00FF; // 16 channels grouped |
| 73 | + test_enables[8] = 32'hFF00FF00; // 16 channels grouped inverse |
| 74 | + test_enables[9] = 32'h0F0F0F0F; // 16 channels nibble pattern |
| 75 | + test_enables[10] = 32'h00000007; // 3 channels (non-pow2) |
| 76 | + test_enables[11] = 32'h0000000F; // 4 channels |
| 77 | + test_enables[12] = 32'h0000001F; // 5 channels (non-pow2) |
| 78 | + test_enables[13] = 32'h0000003F; // 6 channels (non-pow2) |
| 79 | + test_enables[14] = 32'h0000007F; // 7 channels (non-pow2) |
| 80 | + test_enables[15] = 32'h000000FF; // 8 channels |
| 81 | + test_enables[16] = 32'h000001FF; // 9 channels (non-pow2) |
| 82 | + test_enables[17] = 32'h00000FFF; // 12 channels (non-pow2) |
| 83 | + test_enables[18] = 32'h00001FFF; // 13 channels (non-pow2) |
| 84 | + test_enables[19] = 32'h00007FFF; // 15 channels (non-pow2) |
| 85 | + test_enables[20] = 32'h0001FFFF; // 17 channels (non-pow2) |
| 86 | + test_enables[21] = 32'h001FFFFF; // 21 channels (non-pow2) |
| 87 | + test_enables[22] = 32'h00FFFFFF; // 24 channels (non-pow2) |
| 88 | + test_enables[23] = 32'h01FFFFFF; // 25 channels (non-pow2) |
| 89 | + test_enables[24] = 32'h07FFFFFF; // 27 channels (non-pow2) |
| 90 | + test_enables[25] = 32'h1FFFFFFF; // 29 channels (non-pow2) |
| 91 | + test_enables[26] = 32'h3FFFFFFF; // 30 channels (non-pow2) |
| 92 | + test_enables[27] = 32'hFFFFFFF7; // 31 channels (1 disabled middle) |
| 93 | + test_enables[28] = 32'hFFFF7FFF; // 31 channels (1 disabled middle) |
| 94 | + test_enables[29] = 32'hF7F7F7F7; // 28 channels (scattered) |
| 95 | + test_enables[30] = 32'h77777777; // 24 channels (non-pow2 scattered) |
| 96 | + test_enables[31] = 32'h00000001; // 1 channel |
| 97 | + end |
| 98 | + |
| 99 | + always @(*) begin |
| 100 | + if (counter == 15) do_trigger_reset(); |
| 101 | + end |
| 102 | + |
| 103 | + always @(posedge clk) begin |
| 104 | + if (trigger_reset == 1'b1) begin |
| 105 | + if (test_index < 31) begin |
| 106 | + test_index <= test_index + 1; |
| 107 | + enable <= test_enables[test_index + 1]; |
| 108 | + end else begin |
| 109 | + if (failed == 1'b0) |
| 110 | + $display("SUCCESS"); |
| 111 | + else |
| 112 | + $display("FAILED"); |
| 113 | + $finish; |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + reg reset_data = 1'b0; |
| 119 | + integer reset_counter = 'h00; |
| 120 | + |
| 121 | + always @(posedge clk) begin |
| 122 | + if (reset == 1'b1) begin |
| 123 | + reset_data <= 1'b1; |
| 124 | + reset_counter <= 'h00; |
| 125 | + if (test_index == 0) begin |
| 126 | + enable <= test_enables[0]; |
| 127 | + end |
| 128 | + end else begin |
| 129 | + reset_counter <= reset_counter + 1'b1; |
| 130 | + if (reset_counter == 'h5) begin |
| 131 | + reset_data <= 1'b0; |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + always @(posedge clk) begin |
| 137 | + if (reset == 1'b1) begin |
| 138 | + counter <= 'h00; |
| 139 | + test_index <= 0; |
| 140 | + end else if (packed_fifo_wr_en == 1'b1) begin |
| 141 | + counter <= counter + 1; |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + always @(posedge clk) begin |
| 146 | + if (reset == 1'b0 && packed_fifo_wr_en == 1'b1 && |
| 147 | + expected_packed_fifo_wr_data !== packed_fifo_wr_data) begin |
| 148 | + failed <= 1'b1; |
| 149 | + $display("Failed for enable mask: %x. Expected data %x, got %x", |
| 150 | + enable, expected_packed_fifo_wr_data, packed_fifo_wr_data); |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + integer i; |
| 155 | + integer j; |
| 156 | + integer h; |
| 157 | + |
| 158 | + always @(posedge clk) begin |
| 159 | + if (reset == 1'b1) begin |
| 160 | + j = 0; |
| 161 | + for (h = 0; h < SAMPLES_PER_CHANNEL; h = h + 1) begin |
| 162 | + for (i = 0; i < NUM_OF_CHANNELS; i = i + 1) begin |
| 163 | + if (enable[i] == 1'b1) begin |
| 164 | + fifo_wr_data[(i*SAMPLES_PER_CHANNEL+h)*16+:16] <= j; |
| 165 | + j = j + 1; |
| 166 | + end else begin |
| 167 | + fifo_wr_data[(i*SAMPLES_PER_CHANNEL+h)*16+:16] <= 'hxxxx; |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end else if (fifo_wr_en == 1'b1) begin |
| 172 | + for (h = 0; h < SAMPLES_PER_CHANNEL; h = h + 1) begin |
| 173 | + for (i = 0; i < NUM_OF_CHANNELS; i = i + 1) begin |
| 174 | + if (enable[i] == 1'b1) begin |
| 175 | + fifo_wr_data[(i*SAMPLES_PER_CHANNEL+h)*16+:16] <= j; |
| 176 | + j = j + 1; |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + always @(posedge clk) begin |
| 184 | + if (reset == 1'b1) begin |
| 185 | + for (i = 0; i < NUM_OF_PORTS; i = i + 1) begin |
| 186 | + expected_packed_fifo_wr_data[i*16+:16] <= i; |
| 187 | + end |
| 188 | + end else if (packed_fifo_wr_en == 1'b1) begin |
| 189 | + for (i = 0; i < NUM_OF_PORTS; i = i + 1) begin |
| 190 | + expected_packed_fifo_wr_data[i*16+:16] <= expected_packed_fifo_wr_data[i*16+:16] + NUM_OF_PORTS; |
| 191 | + end |
| 192 | + end |
| 193 | + end |
| 194 | + |
| 195 | + always @(posedge clk) begin |
| 196 | + if (reset_data == 1'b1) begin |
| 197 | + fifo_wr_en <= 1'b0; |
| 198 | + end else begin |
| 199 | + fifo_wr_en <= ENABLE_RANDOM ? ($random & 1'b1) : ~fifo_wr_en; |
| 200 | + end |
| 201 | + end |
| 202 | + |
| 203 | + util_cpack2_impl #( |
| 204 | + .NUM_OF_CHANNELS(NUM_OF_CHANNELS), |
| 205 | + .SAMPLES_PER_CHANNEL(SAMPLES_PER_CHANNEL), |
| 206 | + .SAMPLE_DATA_WIDTH(16), |
| 207 | + .PIPELINE_STAGES(PIPELINE_STAGES) |
| 208 | + ) i_cpack ( |
| 209 | + .clk(clk), |
| 210 | + .reset(reset), |
| 211 | + .enable(enable), |
| 212 | + .fifo_wr_en({NUM_OF_CHANNELS{fifo_wr_en}}), |
| 213 | + .fifo_wr_data(fifo_wr_data), |
| 214 | + .packed_fifo_wr_en(packed_fifo_wr_en), |
| 215 | + .packed_fifo_wr_data(packed_fifo_wr_data), |
| 216 | + .packed_fifo_wr_overflow(1'b0)); |
| 217 | +endmodule |
0 commit comments