spi/tests/write_read.cpp

100 lines
1.8 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 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();
}
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
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");
}
#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);
}
#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);
}
#endif
}
int main(int argc, char **argv) {
int r = 0;
(void)r;
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
2022-10-23 12:37:07 -04:00
test_cross_transfer(0b101010101010101010101010, 0b010101010101010101010101);
test_cross_transfer(0b110011001100110011001100, 0b001100110011001100110011);
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);
test_cross_transfer(m2s, s2m);
}
sim->final();
delete sim;
2022-10-22 18:34:54 -04:00
return r;
}