fpga: Add testcase for SPI access control

Add testcase that checks that access control
      is enabled and disabled as expected.

Signed-off-by: Joachim Strömbergson <joachim@assured.se>
This commit is contained in:
Joachim Strömbergson 2024-07-11 13:21:20 +02:00 committed by Daniel Jobson
parent 8c476e19c3
commit 148a8f0996
No known key found for this signature in database
GPG Key ID: 3707A9DBF4BB8F1A

View File

@ -61,6 +61,7 @@ module tb_tk1();
localparam ADDR_SPI_EN = 8'h80;
localparam ADDR_SPI_XFER = 8'h81;
localparam ADDR_SPI_DATA = 8'h82;
localparam ADDR_SPI_CMD = 8'h83;
//----------------------------------------------------------------
@ -86,9 +87,6 @@ module tb_tk1();
wire [14 : 0] tb_ram_aslr;
wire [31 : 0] tb_ram_scramble;
reg tb_ram_access;
reg tb_rom_access;
wire tb_led_r;
wire tb_led_g;
wire tb_led_b;
@ -132,9 +130,6 @@ module tb_tk1();
.cpu_valid(tb_cpu_valid),
.force_trap(tb_force_trap),
.ram_access(tb_ram_access),
.rom_access(tb_rom_access),
.ram_aslr(tb_ram_aslr),
.ram_scramble(tb_ram_scramble),
@ -283,9 +278,6 @@ module tb_tk1();
tb_cpu_valid = 1'h0;
tb_cpu_trap = 1'h0;
tb_ram_access = 1'h0;
tb_rom_access = 1'h0;
tb_gpio1 = 1'h0;
tb_gpio2 = 1'h0;
@ -307,8 +299,7 @@ module tb_tk1();
begin
if (DEBUG)
begin
$display("--- Writing 0x%08x to 0x%02x.", word, address);
$display("");
$display("--- Write word: 0x%08x to 0x%02x.", word, address);
end
tb_address = address;
@ -371,13 +362,11 @@ module tb_tk1();
if (DEBUG)
begin
if (read_data == expected) begin
$display("--- Reading 0x%08x from 0x%02x.", read_data, address);
$display("--- Read word: 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);
$display("--- Read word: Error. Got 0x%08x, expected 0x%08x", read_data, address, expected);
error_ctr = error_ctr + 1;
end
$display("");
end
end
endtask // read_check_word
@ -693,6 +682,13 @@ module tb_tk1();
tb_monitor = 0;
tb_spi_monitor = 0;
// We need to enable SPI access before testing.
dut.spi_access_ctrl_new = 1'h1;
dut.spi_access_ctrl_we = 1'h1;
#(CLK_PERIOD);
dut.spi_access_ctrl_new = 1'h0;
dut.spi_access_ctrl_we = 1'h0;
$display("");
$display("--- test10: Loopback in SPI Master started.");
@ -711,7 +707,7 @@ module tb_tk1();
end
$display("--- test10: Byte should have been sent.");
// 0x58 is the inverse of 0xa7.
// Read back data. 0x58 is the inverse of 0xa7.
#(2 * CLK_PERIOD);
read_check_word(ADDR_SPI_DATA, 32'h58);
write_word(ADDR_SPI_EN, 32'h0);
@ -737,32 +733,76 @@ module tb_tk1();
$display("");
$display("--- test11: SPI access control started.");
// Read SPI ready. Access should be blocked after reset.
$display("Status of access_ok_reg after reset: 0x%1x", dut.access_ok_reg);
// Start by resetting and check if access is enabled.
$display("--- test11: Check that SPI access is disabled after reset.");
reset_dut();
read_word(ADDR_SPI_XFER, 32'h0);
$display("");
// Signal that we are performing access from ROM.
// Then try to read SPI ready. This should be granted.
tb_rom_access = 1'h1;
// Set the SPI command adress API to an address in ROM space.
// Read it back to check that it has been set.
$display("--- test11: Set and read SPI command address.");
write_word(ADDR_SPI_CMD, 32'h0000dead);
read_word(ADDR_SPI_CMD, 32'h0000dead);
$display("");
// Simulate CPU instruction read from the SPI command adress.
// And check that access has been enabled.
$display("--- test11: Perform CPU instruction read from SPI command address.");
$display("--- test11: Then check that SPI access is enabled.");
tb_cpu_addr = 32'h0000dead;
tb_cpu_instr = 1'h1;
tb_cpu_valid = 1'h1;
#(CLK_PERIOD);
tb_rom_access = 1'h0;
$display("Status of access_ok_reg after ROM access: 0x%1x", dut.access_ok_reg);
tb_cpu_instr = 1'h0;
tb_cpu_valid = 1'h0;
read_word(ADDR_SPI_XFER, 32'h1);
$display("");
// Signal that we are performing access from RAM.
// Then try to read SPI ready. This should be blocked.
tb_ram_access = 1'h1;
// Simulate CPU instruction read from a RAM address.
// And check that access has been disabled.
$display("--- test11: Perform CPU instruction read from a RAM address.");
$display("--- test11: Then check that SPI access has been disabled.");
tb_cpu_addr = 32'h4000dead;
tb_cpu_instr = 1'h1;
tb_cpu_valid = 1'h1;
#(CLK_PERIOD);
tb_ram_access = 1'h0;
$display("Status of access_ok_reg after RAM access: 0x%1x", dut.access_ok_reg);
tb_cpu_instr = 1'h0;
tb_cpu_valid = 1'h0;
read_word(ADDR_SPI_XFER, 32'h0);
$display("");
// Signal that we are performing access from ROM again.
// Then try to read SPI ready. This should be granted.
tb_rom_access = 1'h1;
// Move the SPI command adress API to a new address in ROM space.
// Read it back to check that it has been set.
$display("--- test11: Set SPI command address to a new address and read again.");
write_word(ADDR_SPI_CMD, 32'h0000cafe);
read_word(ADDR_SPI_CMD, 32'h0000cafe);
$display("");
// Simulate CPU instruction read from the old SPI command adress.
// And check that access is still been disabled.
$display("--- test11: Perform CPU instruction read from old SPI command address.");
$display("--- test11: Then check that SPI access is still disabled.");
tb_cpu_addr = 32'h0000dead;
tb_cpu_instr = 1'h1;
tb_cpu_valid = 1'h1;
#(CLK_PERIOD);
tb_rom_access = 1'h0;
$display("Status of access_ok_reg after ROM access: 0x%1x", dut.access_ok_reg);
tb_cpu_instr = 1'h0;
tb_cpu_valid = 1'h0;
read_word(ADDR_SPI_XFER, 32'h0);
$display("");
// Simulate CPU instruction read from the new SPI command adress.
// And check that access is now enabled.
$display("--- test11: Perform CPU instruction read from new SPI command address.");
$display("--- test11: Then check that SPI access is now enabled.");
tb_cpu_addr = 32'h0000cafe;
tb_cpu_instr = 1'h1;
tb_cpu_valid = 1'h1;
#(CLK_PERIOD);
tb_cpu_instr = 1'h0;
tb_cpu_valid = 1'h0;
read_word(ADDR_SPI_XFER, 32'h1);
$display("--- test11: completed.");