upsilon/firmware/rtl/raster/ram_fifo_sim.cpp

106 lines
1.8 KiB
C++
Raw Normal View History

2022-12-16 19:46:04 -05:00
#include <memory>
2022-12-18 01:06:44 -05:00
#include <cassert>
2022-12-16 19:46:04 -05:00
#include <limits>
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <random>
#include <unistd.h>
#include <verilated.h>
#include "Vram_fifo.h"
2022-12-16 19:46:04 -05:00
using ModType = Vram_fifo;
ModType *mod;
uint32_t main_time = 0;
2022-12-18 01:06:44 -05:00
double sc_time_stamp() {
return main_time;
}
2022-12-16 19:46:04 -05:00
static void run_clock() {
for (int i = 0; i < 2; i++) {
mod->clk = !mod->clk;
mod->eval();
main_time++;
}
}
static void cleanup_exit() {
mod->final();
delete mod;
}
static void init(int argc, char **argv) {
Verilated::commandArgs(argc, argv);
Verilated::traceEverOn(true);
mod = new ModType;
mod->clk = 0;
atexit(cleanup_exit);
}
static void init_values() {
mod->rst = 0;
mod->read_enable = 0;
mod->write_enable = 0;
mod->write_dat = 0;
}
2022-12-18 01:06:44 -05:00
#define MAX_VALS 1500
uint32_t vals[MAX_VALS];
2022-12-16 19:46:04 -05:00
static void push(uint32_t v) {
2022-12-18 01:06:44 -05:00
assert(!mod->full);
mod->write_dat = v;
2022-12-16 19:46:04 -05:00
mod->write_enable = 1;
run_clock();
2022-12-16 19:46:04 -05:00
mod->write_enable = 0;
run_clock();
}
static void pop(int i) {
2022-12-18 01:06:44 -05:00
assert(!mod->empty);
2022-12-16 19:46:04 -05:00
mod->read_enable = 1;
run_clock();
2022-12-16 19:46:04 -05:00
mod->read_enable = 0;
run_clock();
}
2022-12-18 01:06:44 -05:00
static void push_random(int start, int end) {
for (int i = start; i < end; i++) {
2022-12-16 19:46:04 -05:00
vals[i] = rand() & 0xFFFFFFFFFFFF;
2022-12-18 01:06:44 -05:00
printf("%d\n", i);
push(vals[i]);
2022-12-16 19:46:04 -05:00
}
2022-12-18 01:06:44 -05:00
}
2022-12-16 19:46:04 -05:00
2022-12-18 01:06:44 -05:00
static void pop_random(int start, int end) {
for (int i = start; i < end; i++) {
2022-12-16 19:46:04 -05:00
pop(i);
if (mod->read_dat != vals[i]) {
fprintf(stderr, "expect %u, %u\n", vals[i], mod->read_dat);
2022-12-18 01:06:44 -05:00
exit(1);
2022-12-16 19:46:04 -05:00
}
}
2022-12-18 01:06:44 -05:00
}
int main(int argc, char **argv) {
init(argc, argv);
init_values();
run_clock();
assert(mod->empty);
push_random(0, MAX_VALS);
assert(mod->full);
pop_random(0, MAX_VALS);
assert(mod->empty);
push_random(0, 50);
pop_random(0, 20);
push_random(50, 100);
pop_random(20, 100);
2022-12-16 19:46:04 -05:00
return 0;
}