Commit 6eef2d29 authored by mjlyons's avatar mjlyons

Adding testbench for spiloop, and a library to easily write tests with vSPI commands

parent 0060d193
//
// spiloop-tb.v
//
// Tests the vspi loopback (spiloop) module
//
// (c) Michael Lyons, 2012
//
`include "vspi-defines.vh"
module spiloop_tb;
reg SysClk;
wire spi_miso;
reg spi_mosi;
reg spi_clk;
reg spi_ss;
`include "vspi-tasks.vh"
spiloop dutSpiloop(
.Reset(1'b0),
.SysClk(SysClk),
.spi_miso(spi_miso),
.spi_mosi(spi_mosi),
.spi_clk(spi_clk),
.spi_ss(spi_ss)
);
always begin
#5 SysClk = !SysClk;
end
reg [7:0] recvByte;
reg [31:0] recvWord;
integer i;
initial begin
SysClk = 1'b0;
spi_mosi = 1'b0;
spi_clk = 1'b0;
spi_ss = 1'b1;
#100
// Write 0xNNNNNNNN to RegN and read back
$display("Testing each register");
for (i = 0; i < 16; i = i + 1) begin
spiWriteReg(i, {8{i[3:0]}});
spiReadReg(i, recvWord);
`expect_h("SPI.r0", {8{i[3:0]}}, recvWord);
end
$display("");
// Write over entire memory, read it back.
$display("Testing memory");
spiWriteMemStart();
for (i = 0; i < 4096; i = i + 1) begin
spiWriteMemNextByte({i[3:0], i[7:4]});
end
spiWriteMemStop();
spiReadMemStart();
for (i = 0; i < 4096; i = i + 1) begin
spiReadMemNextByte(recvWord[7:0]);
`expect_h("SPI.mem[]", {i[3:0], i[7:4]}, recvWord[7:0]);
end
spiReadMemStop();
$display("\nTestbench passed without error!\n");
$finish;
end
endmodule
//
// verifies that a variable matches its expected value
// and displays it in hex
//
`define expect_h(name,expectedVal,actualVal) if ((expectedVal) !== (actualVal)) begin $display("[ERROR] %s expected='h%X actual='h%X", (name), (expectedVal), (actualVal)); $finish; end else begin $display("[PASS] %s='h%X", (name), (actualVal)); end
//
// Sends a byte to logic over spi, gets byte back
//
//
// Call before doing any multi-byte SPI transmission
//
task spiTxStart;
begin
spi_ss = 0;
#20;
end
endtask
//
// Call after doing a multi-byte SPI transmission
task spiTxStop;
begin
spi_ss = 1;
#20;
end
endtask
task spiExchByte;
input [7:0] byteToSend;
output [7:0] recvByte;
reg [7:0] recvByte;
integer bitIndex;
begin
spi_clk = 1'b0;
//$display("Sending byte via spi: 8'h%02X", byteToSend);
bitIndex = 8;
while (bitIndex > 0) begin
#20
bitIndex = bitIndex - 1;
//$display("Sending bit[%0d] via spi: %b",
// bitIndex, byteToSend[bitIndex]);
spi_mosi = byteToSend[bitIndex];
recvByte[bitIndex] = spi_miso;
spi_clk = 1'b1;
#20
spi_clk = 1'b0;
end
end
endtask
//
// Sends a byte to vSPI, but not receive anything
//
task spiSendByte;
input [7:0] byteToSend;
reg [7:0] recvByte;
begin
spiExchByte(byteToSend, recvByte);
end
endtask
//
// Receives a byte from vSPI, but does not send anything
//
task spiRecvByte;
output [7:0] recvByte;
reg [7:0] recvByte;
begin
spiExchByte(8'hFF, recvByte);
end
endtask
//
// Write register on vspi
//
task spiWriteReg;
input [ 3:0] regId;
input [31:0] regValue;
integer byteId;
begin
spiTxStart();
spiSendByte({4'b1100, regId}); // Write spi.reg
byteId = 4;
while (byteId > 0) begin
byteId = byteId - 1;
spiSendByte(regValue[byteId*8 +: 8]);
end
spiTxStop();
end
endtask
//
// Read spi register
//
task spiReadReg;
input [ 3:0] regId;
output [31:0] regValue;
reg [31:0] regValue;
integer byteId;
begin
spiTxStart();
spiSendByte({4'h8, regId});
byteId = 4;
while (byteId > 0) begin
byteId = byteId - 1;
spiRecvByte(regValue[byteId*8 +: 8]);
end
spiTxStop();
end
endtask
//
// Initializes in preparation to write to spi memory buffer
//
task spiWriteMemStart;
begin
spiTxStart();
spiSendByte(8'h01);
end
endtask
//
// Writes a byte to a memory transfer in progress
//
task spiWriteMemNextByte;
input [7:0] writeByte;
begin
spiSendByte(writeByte);
end
endtask
//
// Finishes writing to memory over spi
//
task spiWriteMemStop;
begin
spiTxStop();
end
endtask
//
// Prepares to read a byte from spi memory
//
task spiReadMemStart;
begin
spiTxStart();
spiSendByte(8'h03);
end
endtask
//
// Read a byte from a memory transfer in progress
//
task spiReadMemNextByte;
output [7:0] recvByte;
reg [7:0] recvByte;
begin
spiRecvByte(recvByte);
end
endtask
//
// Finishes reading from spi memory
//
task spiReadMemStop;
begin
spiTxStop();
end
endtask
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment