spi/tests/write_read.cpp

133 lines
2.4 KiB
C++
Raw Normal View History

#include <stdio.h>
#include <verilated.h>
2022-10-22 18:34:54 -04:00
#include "Vsimtop.h"
2022-10-22 18:34:54 -04:00
Vsimtop *sim;
2022-10-23 14:03:29 -04:00
int return_value = 0;
2022-10-22 18:34:54 -04:00
2022-10-23 12:37:07 -04:00
#ifdef SPI_MASTER_SS
# define SET_SS(mod, v)
#else
# define SET_SS(mod,v) ((mod)->ss = (v))
#endif
2022-10-22 18:34:54 -04:00
uint32_t main_time = 0;
double sc_time_stamp() {
return main_time;
}
static void progress() {
sim->eval();
2022-10-22 18:34:54 -04:00
main_time++;
sim->clk = !sim->clk;
sim->eval();
main_time++;
sim->clk = !sim->clk;
}
static void progress_n(int f) {
for (int i = 0; i < f; i++)
progress();
}
2023-04-20 15:10:04 -04:00
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;
}
2022-10-22 18:34:54 -04:00
static void test_cross_transfer(unsigned m2s, unsigned s2m) {
#ifndef SPI_MASTER_NO_WRITE
sim->master_to_slave = m2s;
#endif
#ifndef SPI_MASTER_NO_READ
sim->slave_to_master = s2m;
#endif
2023-04-20 15:10:04 -04:00
sim->rst_L = 1;
2022-10-22 18:34:54 -04:00
progress();
2022-10-23 12:37:07 -04:00
SET_SS(sim, 1);
2022-07-21 01:53:38 -04:00
sim->rdy = 1;
sim->activate = 1;
2022-10-22 18:34:54 -04:00
progress();
while (!sim->master_finished)
progress();
2022-10-22 18:34:54 -04:00
progress_n(5);
2022-07-21 01:53:38 -04:00
sim->activate = 0;
2022-10-23 12:37:07 -04:00
SET_SS(sim, 0);
2022-07-21 01:53:38 -04:00
sim->rdy = 0;
progress_n(5);
2022-10-22 18:34:54 -04:00
if (sim->err) {
printf("slave error\n");
2022-10-23 14:03:29 -04:00
return_value = 1;
2022-10-22 18:34:54 -04:00
}
#ifndef SPI_MASTER_NO_WRITE
if (sim->master_to_slave != sim->from_master) {
printf("(m2s) %lx != %lx\n", sim->master_to_slave, sim->from_master);
2022-10-23 14:03:29 -04:00
return_value = 1;
2022-10-22 18:34:54 -04:00
}
#endif
#ifndef SPI_MASTER_NO_READ
if (sim->slave_to_master != sim->from_slave) {
printf("(m2s) %lx != %lx\n", sim->slave_to_master, sim->from_slave);
2022-10-23 14:03:29 -04:00
return_value = 1;
2022-10-22 18:34:54 -04:00
}
#endif
}
2023-04-20 15:10:04 -04:00
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);
}
2022-10-22 18:34:54 -04:00
int main(int argc, char **argv) {
Verilated::commandArgs(argc, argv);
Verilated::traceEverOn(true);
sim = new Vsimtop;
2022-10-23 12:37:07 -04:00
SET_SS(sim, 0);
2022-10-22 18:34:54 -04:00
sim->clk = 0;
sim->activate = 0;
2022-07-21 01:53:38 -04:00
sim->rdy = 0;
2022-10-22 18:34:54 -04:00
2023-04-20 15:10:04 -04:00
test_reset_pin();
2022-10-23 12:37:07 -04:00
test_cross_transfer(0b101010101010101010101010, 0b010101010101010101010101);
test_cross_transfer(0b110011001100110011001100, 0b001100110011001100110011);
2023-04-20 15:10:04 -04:00
test_reset_pin();
2022-10-23 12:37:07 -04:00
2022-10-22 18:34:54 -04:00
for (int i = 0; i < 10000; i++) {
unsigned m2s = rand() & ((1 << WID) - 1);
unsigned s2m = rand() & ((1 << WID) - 1);
2023-04-20 15:10:04 -04:00
if (i % (((rand() + 1) % 32) + 1) == 0)
test_interrupted(m2s, s2m);
else
test_cross_transfer(m2s, s2m);
if (i % (((rand() + 1) % 64) + 1) == 0)
test_reset_pin();
2022-10-22 18:34:54 -04:00
}
sim->final();
delete sim;
2022-10-23 14:03:29 -04:00
return return_value;
}