Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make testbenches selftesting #302

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion hw/application_fpga/core/tk1/tb/tb_tk1.v
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,22 @@ module tb_tk1 ();
end
endtask // test10

//----------------------------------------------------------------
// exit_with_error_code()
//
// Exit with the right error code
//----------------------------------------------------------------
task exit_with_error_code;
begin
if (error_ctr == 0) begin
$finish(0);
end
else begin
$fatal(1);
end
end
endtask // exit_with_error_code


//----------------------------------------------------------------
// tk1_test
Expand Down Expand Up @@ -744,7 +760,7 @@ module tb_tk1 ();
$display(" -= Testbench for tk1 completed =-");
$display(" =============================");
$display("");
$finish;
exit_with_error_code();
end // tk1_test
endmodule // tb_tk1

Expand Down
73 changes: 69 additions & 4 deletions hw/application_fpga/core/tk1/tb/tb_tk1_spi_master.v
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ module tb_tk1_spi_master ();
end
endtask // read_status

//----------------------------------------------------------------
// check_byte()
//
// The function checks that the input_data byte matches with the expected.
//----------------------------------------------------------------
task check_byte(input [7 : 0] input_data, input [7 : 0] expected);
begin : check_byte
if (input_data != expected) begin
$display("--- Error: Got 0x%02x, expected 0x%02x", input_data, expected);
error_ctr = error_ctr + 1;
end
end
endtask // check_byte



//----------------------------------------------------------------
// tc_get_device_id()
Expand Down Expand Up @@ -474,10 +489,15 @@ module tb_tk1_spi_master ();
// Send dummy bytes and get response back.
xfer_byte(8'h00, rx_byte);
$display("--- tc_get_jedec_id: Got manufacture ID 0x%02x", rx_byte);
check_byte(rx_byte, 8'hef);

xfer_byte(8'h00, rx_byte);
$display("--- tc_get_jedec_id: Got memory type 0x%02x", rx_byte);
check_byte(rx_byte, 8'h40);

xfer_byte(8'h00, rx_byte);
$display("--- tc_get_jedec_id: Got memory capacity 0x%02x", rx_byte);
check_byte(rx_byte, 8'h14);

disable_spi();
#(2 * CLK_PERIOD);
Expand All @@ -498,12 +518,22 @@ module tb_tk1_spi_master ();
//----------------------------------------------------------------
task tc_get_unique_device_id;
begin : tc_get_id
reg [7 : 0] expected[0 : 7];
reg [7 : 0] rx_byte;
integer i;
tc_ctr = tc_ctr + 1;
tc_ctr = tc_ctr + 1;
monitor = 0;
verbose = 0;

expected[0] = 8'hdc;
expected[1] = 8'h02;
expected[2] = 8'h03;
expected[3] = 8'h04;
expected[4] = 8'h05;
expected[5] = 8'h06;
expected[6] = 8'h07;
expected[7] = 8'h08;

$display("");
$display("--- tc_get_unique_device_id: Read out unique id from the memory");
$display("--- tc_get_unique_device_id: Expected result: 0xdc02030405060708");
Expand All @@ -527,6 +557,7 @@ module tb_tk1_spi_master ();
for (i = 0; i < 8; i = i + 1) begin
xfer_byte(8'h00, rx_byte);
$display("--- tc_get_unique_device_id: 0x%02x", rx_byte);
check_byte(rx_byte, expected[i]);
end

disable_spi();
Expand Down Expand Up @@ -592,12 +623,30 @@ module tb_tk1_spi_master ();
//----------------------------------------------------------------
task tc_read_mem;
begin : tc_get_id
reg [7 : 0] expected[0 : 15];
reg [7 : 0] rx_byte;
integer i;
tc_ctr = tc_ctr + 1;
tc_ctr = tc_ctr + 1;
monitor = 0;
verbose = 0;

expected[0] = 8'hde;
expected[1] = 8'had;
expected[2] = 8'hbe;
expected[3] = 8'hef;
expected[4] = 8'hde;
expected[5] = 8'had;
expected[6] = 8'hbe;
expected[7] = 8'hef;
expected[8] = 8'hde;
expected[9] = 8'had;
expected[10] = 8'hbe;
expected[11] = 8'hef;
expected[12] = 8'hde;
expected[13] = 8'had;
expected[14] = 8'hbe;
expected[15] = 8'hef;

$display("");
$display("--- tc_read_mem: Read out the first 16 bytes from the memory.");

Expand All @@ -609,7 +658,7 @@ module tb_tk1_spi_master ();
$display("--- tc_read_mem: Sending 0x03 command.");
xfer_byte(8'h03, rx_byte);

// Send adress 0x000000.
// Send address 0x000000.
$display("--- tc_read_mem: Sending 24 bit address 0x000000.");
xfer_byte(8'h00, rx_byte);
xfer_byte(8'h00, rx_byte);
Expand All @@ -620,6 +669,7 @@ module tb_tk1_spi_master ();
for (i = 1; i < 17; i = i + 1) begin
xfer_byte(8'h00, rx_byte);
$display("--- tc_read_mem: Byte %d: 0x%02x", i, rx_byte);
check_byte(rx_byte, expected[i-1]);
end

disable_spi();
Expand Down Expand Up @@ -684,6 +734,21 @@ module tb_tk1_spi_master ();
end
endtask // tc_rmr_mem

//----------------------------------------------------------------
// exit_with_error_code()
//
// Exit with the right error code
//----------------------------------------------------------------
task exit_with_error_code;
begin
if (error_ctr == 0) begin
$finish(0);
end
else begin
$fatal(1);
end
end
endtask // exit_with_error_code

//----------------------------------------------------------------
// tk1_spi_master_test
Expand Down Expand Up @@ -712,7 +777,7 @@ module tb_tk1_spi_master ();
$display(" -= Testbench for tk1_spi_master completed =-");
$display(" =========================================");
$display("");
$finish;
exit_with_error_code();
end // tk1_spi_master_test
endmodule // tb_tk1_spi_master

Expand Down
66 changes: 60 additions & 6 deletions hw/application_fpga/core/touch_sense/tb/tb_touch_sense.v
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,43 @@ module tb_touch_sense ();
endtask // read_word


//----------------------------------------------------------------
// read_check_word()
//
// Read a data word from the given address in the DUT.
// the word read will be available in the global variable
// read_data.
//
// The function also checks that the data read matches
// the expected value or not.
//----------------------------------------------------------------
task read_check_word(input [7 : 0] address, input [31 : 0] expected);
begin : read_check_word

tb_address = address;
tb_cs = 1'h1;

#(CLK_PERIOD);
read_data = tb_read_data;

#(CLK_PERIOD);
tb_cs = 1'h0;

if (DEBUG) begin
if (read_data == expected) begin
$display("--- Reading 0x%08x from 0x%02x.", read_data, address);
end
else begin
$display("--- Error: Got 0x%08x when reading from 0x%02x, expected 0x%08x", read_data,
address, expected);
error_ctr = error_ctr + 1;
end
$display("");
end
end
endtask // read_check_word


//----------------------------------------------------------------
// wait_ready()
//
Expand Down Expand Up @@ -242,29 +279,29 @@ module tb_touch_sense ();

// Check status.
#(CLK_PERIOD);
read_word(8'h09);
read_check_word(ADDR_STATUS, 32'h00);

// Set touch event input to high.
$display("--- test1: Creating a touch event.");
tb_touch_event = 1'h1;

$display("--- test1: Waiting for the event to be caught.");
wait_ready();
read_check_word(ADDR_STATUS, 32'h01);

$display("--- test1: Event has been seen.");

$display("--- test1: Dropping the event input.");
tb_touch_event = 1'h0;
#(CLK_PERIOD);
$display("--- test1: Clearing the event.");
write_word(8'h09, 32'h0);
write_word(ADDR_STATUS, 32'h0);
#(CLK_PERIOD);

// Check that the event is now removed.
read_word(8'h09);
read_check_word(ADDR_STATUS, 32'h00);
#(CLK_PERIOD);
$display("--- test1: Event has been cleared.");
read_word(8'h09);
read_check_word(ADDR_STATUS, 32'h00);
#(CLK_PERIOD);

$display("--- test1: completed.");
Expand All @@ -273,6 +310,23 @@ module tb_touch_sense ();
endtask // test1


//----------------------------------------------------------------
// exit_with_error_code()
//
// Exit with the right error code
//----------------------------------------------------------------
task exit_with_error_code;
begin
if (error_ctr == 0) begin
$finish(0);
end
else begin
$fatal(1);
end
end
endtask // exit_with_error_code


//----------------------------------------------------------------
// touch_sense_test
//----------------------------------------------------------------
Expand All @@ -292,7 +346,7 @@ module tb_touch_sense ();
$display(" -= Testbench for touch_sense completed =-");
$display(" ======================================");
$display("");
$finish;
exit_with_error_code();
end // touch_sense_test
endmodule // tb_touch_sense

Expand Down
20 changes: 18 additions & 2 deletions hw/application_fpga/core/trng/tb/tb_trng.v
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ module tb_trng ();
// the word read will be available in the global variable
// read_data.
//----------------------------------------------------------------
task read_word(input [ 7 : 0] address, input [31 : 0] expected);
task read_word(input [7 : 0] address, input [31 : 0] expected);
begin : read_word
reg [31 : 0] read_data;

Expand Down Expand Up @@ -221,6 +221,22 @@ module tb_trng ();
end
endtask // test1

//----------------------------------------------------------------
// exit_with_error_code()
//
// Exit with the right error code
//----------------------------------------------------------------
task exit_with_error_code;
begin
if (error_ctr == 0) begin
$finish(0);
end
else begin
$fatal(1);
end
end
endtask // exit_with_error_code


//----------------------------------------------------------------
// trng_test
Expand All @@ -240,7 +256,7 @@ module tb_trng ();
$display(" -= Testbench for trng completed =-");
$display(" ==============================");
$display("");
$finish;
exit_with_error_code();
end // trng_test
endmodule // tb_trng

Expand Down
19 changes: 18 additions & 1 deletion hw/application_fpga/core/uart/tb/tb_uart.v
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,23 @@ module tb_uart ();
endtask // display_test_result


//----------------------------------------------------------------
// exit_with_error_code()
//
// Exit with the right error code
//----------------------------------------------------------------
task exit_with_error_code;
begin
if (error_ctr == 0) begin
$finish(0);
end
else begin
$fatal(1);
end
end
endtask // exit_with_error_code


//----------------------------------------------------------------
// uart_test
// The main test functionality.
Expand All @@ -340,7 +357,7 @@ module tb_uart ();

display_test_result();
$display("*** Simulation done.");
$finish;
exit_with_error_code();
end // uart_test
endmodule // tb_uart

Expand Down
Loading