add reset pin

This commit is contained in:
Peter McGoron 2023-04-20 19:10:04 +00:00
parent 2119ec275b
commit 1c6672e618
5 changed files with 112 additions and 40 deletions

View File

@ -30,6 +30,7 @@ spi_master
) )
( (
input clk, input clk,
input rst_L,
`ifndef SPI_MASTER_NO_READ `ifndef SPI_MASTER_NO_READ
output reg [WID-1:0] from_slave, output reg [WID-1:0] from_slave,
input miso, input miso,
@ -138,7 +139,18 @@ endtask
initial ready_to_arm = 1; initial ready_to_arm = 1;
always @ (posedge clk) begin always @ (posedge clk) begin
case (state) if (!rst_L) begin
idle_state();
finished <= 0;
state <= WAIT_ON_ARM;
ready_to_arm <= 1;
`ifndef SPI_MASTER_NO_READ
from_slave <= 0;
`endif
`ifndef SPI_MASTER_NO_WRITE
send_buf <= 0;
`endif
end else case (state)
WAIT_ON_ARM: begin WAIT_ON_ARM: begin
`ifdef SIMULATION `ifdef SIMULATION
if (!ready_to_arm) if (!ready_to_arm)

View File

@ -17,6 +17,7 @@ module `SPI_MASTER_SS_NAME
) )
( (
input clk, input clk,
input rst_L,
`ifndef SPI_MASTER_NO_READ `ifndef SPI_MASTER_NO_READ
output [WID-1:0] from_slave, output [WID-1:0] from_slave,
input miso, input miso,
@ -45,6 +46,7 @@ assign ss_L = !ss;
.PHASE(PHASE) .PHASE(PHASE)
) master ( ) master (
.clk(clk), .clk(clk),
.rst_L(rst_L),
`ifndef SPI_MASTER_NO_READ `ifndef SPI_MASTER_NO_READ
.from_slave(from_slave), .from_slave(from_slave),
.miso(miso), .miso(miso),
@ -72,7 +74,12 @@ task master_arm();
endtask endtask
always @ (posedge clk) begin always @ (posedge clk) begin
case (state) if (!rst_L) begin
state <= WAIT_ON_ARM;
timer <= 0;
arm_master <= 0;
ss <= 0;
end else case (state)
WAIT_ON_ARM: begin WAIT_ON_ARM: begin
if (arm) begin if (arm) begin
timer <= 1; timer <= 1;

View File

@ -20,6 +20,7 @@ spi_slave
) )
( (
input clk, input clk,
input rst_L,
input sck, input sck,
input ss_L, input ss_L,
`ifndef SPI_SLAVE_NO_READ `ifndef SPI_SLAVE_NO_READ
@ -96,6 +97,21 @@ task check_counter();
endtask endtask
always @ (posedge clk) begin always @ (posedge clk) begin
if (!rst_L) begin
sck_delay <= 0;
bit_counter <= 0;
ss_delay <= 0;
ready_at_start <= 0;
`ifndef SPI_SLAVE_NO_READ
from_master <= 0;
`endif
`ifndef SPI_SLAVE_NO_WRITE
miso <= 0;
send_buf <= 0;
`endif
finished <= 0;
err <= 0;
end else begin
sck_delay <= sck; sck_delay <= sck;
ss_delay <= ss; ss_delay <= ss;
@ -144,5 +160,6 @@ always @ (posedge clk) begin
end end
endcase endcase
end end
end
endmodule endmodule

View File

@ -12,6 +12,7 @@ module simtop
parameter WID_LEN = 5 parameter WID_LEN = 5
) ( ) (
input clk, input clk,
input rst_L,
`ifndef SPI_MASTER_NO_WRITE `ifndef SPI_MASTER_NO_WRITE
input [WID-1:0] master_to_slave, input [WID-1:0] master_to_slave,
output [WID-1:0] from_master, output [WID-1:0] from_master,
@ -62,6 +63,7 @@ reg slave_error;
.WID_LEN(WID_LEN) .WID_LEN(WID_LEN)
) master ( ) master (
.clk(clk), .clk(clk),
.rst_L(rst_L),
`ifndef SPI_MASTER_NO_WRITE `ifndef SPI_MASTER_NO_WRITE
.to_slave(master_to_slave), .to_slave(master_to_slave),
.mosi(mosi), .mosi(mosi),
@ -86,6 +88,7 @@ reg slave_error;
.WID_LEN(WID_LEN) .WID_LEN(WID_LEN)
) slave ( ) slave (
.clk(clk), .clk(clk),
.rst_L(rst_L),
.sck(sck), .sck(sck),
.ss_L(ss_L), .ss_L(ss_L),
`ifndef SPI_MASTER_NO_WRITE `ifndef SPI_MASTER_NO_WRITE

View File

@ -31,6 +31,17 @@ static void progress_n(int f) {
progress(); progress();
} }
static void test_reset_pin(void) {
sim->rst_L = 0;
progress();
sim->rdy = 1;
sim->activate = 1;
progress_n(200);
assert(!sim->master_finished);
sim->rst_L = 1;
}
static void test_cross_transfer(unsigned m2s, unsigned s2m) { static void test_cross_transfer(unsigned m2s, unsigned s2m) {
#ifndef SPI_MASTER_NO_WRITE #ifndef SPI_MASTER_NO_WRITE
sim->master_to_slave = m2s; sim->master_to_slave = m2s;
@ -39,6 +50,7 @@ static void test_cross_transfer(unsigned m2s, unsigned s2m) {
sim->slave_to_master = s2m; sim->slave_to_master = s2m;
#endif #endif
sim->rst_L = 1;
progress(); progress();
SET_SS(sim, 1); SET_SS(sim, 1);
sim->rdy = 1; sim->rdy = 1;
@ -74,6 +86,19 @@ static void test_cross_transfer(unsigned m2s, unsigned s2m) {
#endif #endif
} }
static void test_interrupted(unsigned m2s, unsigned s2m) {
sim->rst_L = 1;
progress();
sim->rdy = 1;
sim->activate = 1;
progress_n(6);
sim->rst_L = 0;
progress_n(100);
sim->rst_L = 1;
test_cross_transfer(m2s, s2m);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
Verilated::commandArgs(argc, argv); Verilated::commandArgs(argc, argv);
Verilated::traceEverOn(true); Verilated::traceEverOn(true);
@ -84,13 +109,21 @@ int main(int argc, char **argv) {
sim->activate = 0; sim->activate = 0;
sim->rdy = 0; sim->rdy = 0;
test_reset_pin();
test_cross_transfer(0b101010101010101010101010, 0b010101010101010101010101); test_cross_transfer(0b101010101010101010101010, 0b010101010101010101010101);
test_cross_transfer(0b110011001100110011001100, 0b001100110011001100110011); test_cross_transfer(0b110011001100110011001100, 0b001100110011001100110011);
test_reset_pin();
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
unsigned m2s = rand() & ((1 << WID) - 1); unsigned m2s = rand() & ((1 << WID) - 1);
unsigned s2m = rand() & ((1 << WID) - 1); unsigned s2m = rand() & ((1 << WID) - 1);
if (i % (((rand() + 1) % 32) + 1) == 0)
test_interrupted(m2s, s2m);
else
test_cross_transfer(m2s, s2m); test_cross_transfer(m2s, s2m);
if (i % (((rand() + 1) % 64) + 1) == 0)
test_reset_pin();
} }
sim->final(); sim->final();