Skip to content

Commit

Permalink
refactor to move reused/duplicated code into sim_util_pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
reed-foster committed Dec 1, 2023
1 parent d5a44c2 commit 8642f99
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
10 changes: 5 additions & 5 deletions dds_test.srcs/sim_1/new/axis_differentiator_test.sv
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sim_util_pkg::*;

`timescale 1ns / 1ps
module axis_differentiator_test();

Expand All @@ -20,6 +22,8 @@ int_t received[$];
int_t expected[$];
int_t sent[$];

sim_util_pkg::generic #(int_t) util;

always @(posedge clk) begin
if (reset) begin
data_in_if.data <= '0;
Expand All @@ -45,10 +49,6 @@ always @(posedge clk) begin
end
end

function int_t abs(input int_t x);
return (x < 0) ? -x : x;
endfunction

task check_results();
$display("received.size() = %0d", received.size());
$display("expected.size() = %0d", expected.size());
Expand All @@ -59,7 +59,7 @@ task check_results();
// check the values match, like with axis_x2_test, the rounding could lead
// to an off-by-one error
while (received.size() > 0 && expected.size() > 0) begin
if (abs(expected[$] - received[$]) > 1) begin
if (util.abs(expected[$] - received[$]) > 1) begin
$warning("mismatch: got %x, expected %x", received[$], expected[$]);
error_count = error_count + 1;
end
Expand Down
8 changes: 5 additions & 3 deletions dds_test.srcs/sim_1/new/axis_width_converter_test.sv
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sim_util_pkg::*;

`timescale 1ns / 1ps
module axis_width_converter_test();

Expand All @@ -16,8 +18,8 @@ localparam int UP = 8;
localparam int COMB_UP = 4;
localparam int COMB_DOWN = 3;

`define MAX(A,B) (A > B) ? A : B
localparam int DWIDTH = `MAX(`MAX(`MAX(DWIDTH_DOWN_IN, DWIDTH_UP_IN*UP), (DWIDTH_COMB_IN*COMB_UP)/COMB_DOWN), DWIDTH_COMB_IN);
sim_util_pkg::sample_discriminator_util util;
localparam int DWIDTH = util.max(util.max(util.max(DWIDTH_DOWN_IN, DWIDTH_UP_IN*UP), (DWIDTH_COMB_IN*COMB_UP)/COMB_DOWN), DWIDTH_COMB_IN);

Axis_If #(.DWIDTH(DWIDTH_DOWN_IN)) downsizer_in ();
Axis_If #(.DWIDTH(DWIDTH_DOWN_IN/DOWN)) downsizer_out ();
Expand Down Expand Up @@ -107,7 +109,7 @@ localparam [2:0][31:0] WORD_SIZE = '{
DWIDTH_DOWN_IN/DOWN // downsizer
};

localparam MAX_WORD_SIZE = `MAX(`MAX(WORD_SIZE[0],WORD_SIZE[1]),WORD_SIZE[2]);
localparam MAX_WORD_SIZE = util.max(util.max(WORD_SIZE[0],WORD_SIZE[1]),WORD_SIZE[2]);
logic [MAX_WORD_SIZE-1:0] sent_word, received_word;

// update data and track sent/received samples
Expand Down
9 changes: 4 additions & 5 deletions dds_test.srcs/sim_1/new/axis_x2_test.sv
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
`timescale 1ns / 1ps
module axis_x2_test();


int error_count = 0;

logic reset;
Expand All @@ -21,6 +22,8 @@ real d_in;
int_t received[$];
int_t expected[$];

sim_util_pkg::generic #(int_t) util;

always @(posedge clk) begin
if (reset) begin
data_in_if.data <= '0;
Expand All @@ -42,10 +45,6 @@ always @(posedge clk) begin
end
end

function int_t abs(input int_t x);
return (x < 0) ? -x : x;
endfunction

task check_results();
$display("received.size() = %0d", received.size());
$display("expected.size() = %0d", expected.size());
Expand All @@ -56,7 +55,7 @@ task check_results();
// check the values match
// casting to uint_t seems to perform a rounding operation, so the test data may be slightly too large
while (received.size() > 0 && expected.size() > 0) begin
if (abs(expected[$] - received[$]) > 1) begin
if (util.abs(expected[$] - received[$]) > 1) begin
$warning("mismatch: got %x, expected %x", received[$], expected[$]);
error_count = error_count + 1;
end
Expand Down
3 changes: 2 additions & 1 deletion dds_test.srcs/sim_1/new/sample_discriminator_test.sv
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ localparam int PARALLEL_SAMPLES = 4;
localparam int SAMPLE_INDEX_WIDTH = 14;
localparam int CLOCK_WIDTH = 50;

sim_util_pkg::sample_discriminator_util #(.SAMPLE_WIDTH(SAMPLE_WIDTH), .PARALLEL_SAMPLES(PARALLEL_SAMPLES)) util;

Axis_If #(.DWIDTH(N_CHANNELS*SAMPLE_WIDTH*2)) config_in();
Axis_Parallel_If #(.DWIDTH(SAMPLE_WIDTH*PARALLEL_SAMPLES), .PARALLEL_CHANNELS(N_CHANNELS)) data_in();
Axis_Parallel_If #(.DWIDTH(SAMPLE_WIDTH*PARALLEL_SAMPLES), .PARALLEL_CHANNELS(N_CHANNELS)) data_out();
Expand Down Expand Up @@ -87,7 +89,6 @@ task check_results (
inout logic [N_CHANNELS-1:0][SAMPLE_INDEX_WIDTH-1:0] sample_index,
inout logic [N_CHANNELS-1:0] is_high
);
sim_util_pkg::sample_discriminator_util #(.SAMPLE_WIDTH(SAMPLE_WIDTH), .PARALLEL_SAMPLES(PARALLEL_SAMPLES)) util;
for (int i = 0; i < N_CHANNELS; i++) begin
// process each channel, first check that we received an appropriate amount of data
$display("data_sent[%0d].size() = %0d", i, data_sent[i].size());
Expand Down
12 changes: 12 additions & 0 deletions dds_test.srcs/sim_1/new/sim_util_pkg.sv
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ package sim_util_pkg;

endclass

class generic #(type T=int);

function T max(input T A, input T B);
return (A > B) ? A : B;
endfunction

function T abs(input T x);
return (x < 0) ? -x : x;
endfunction

endclass

endpackage

0 comments on commit 8642f99

Please sign in to comment.