ram_fifo: add empty and full ports

This commit is contained in:
Peter McGoron 2022-12-18 06:06:44 +00:00
parent 1be89f314c
commit 15480f11da
3 changed files with 63 additions and 20 deletions

View File

@ -1,7 +1,7 @@
module ram_fifo #( module ram_fifo #(
parameter DAT_WID = 24, parameter DAT_WID = 24,
parameter FIFO_DEPTH = 1500, parameter FIFO_DEPTH_WID = 11,
parameter FIFO_DEPTH_WID = 11 parameter [FIFO_DEPTH_WID-1:0] FIFO_DEPTH = 1500
) ( ) (
input clk, input clk,
input rst, input rst,
@ -10,10 +10,16 @@ module ram_fifo #(
input write_enable, input write_enable,
input signed [DAT_WID-1:0] write_dat, input signed [DAT_WID-1:0] write_dat,
output reg [FIFO_DEPTH_WID-1:0] fifo_size, output signed [DAT_WID-1:0] read_dat,
output signed [DAT_WID-1:0] read_dat output empty,
output full
); );
reg [FIFO_DEPTH_WID-1:0] fifo_size;
initial fifo_size = 0;
assign empty = fifo_size == 0;
assign full = fifo_size == FIFO_DEPTH;
ram_fifo_dual_port #( ram_fifo_dual_port #(
.DAT_WID(DAT_WID), .DAT_WID(DAT_WID),
.FIFO_DEPTH(FIFO_DEPTH), .FIFO_DEPTH(FIFO_DEPTH),
@ -48,4 +54,13 @@ always @ (posedge clk) begin
end end
end end
/*
`ifdef VERILATOR
initial begin
$dumpfile("ram_fifo.vcd");
$dumpvars;
end
`endif
*/
endmodule endmodule

View File

@ -34,6 +34,9 @@ always @ (posedge RCLK) begin
read_ptr <= 0; read_ptr <= 0;
end else if (read_enable) begin end else if (read_enable) begin
read_dat <= memory[read_ptr]; read_dat <= memory[read_ptr];
if (read_ptr == FIFO_DEPTH-1)
read_ptr <= 0;
else
read_ptr <= read_ptr + 1; read_ptr <= read_ptr + 1;
end end
end end
@ -45,6 +48,9 @@ always @ (posedge WCLK) begin
write_ptr <= 0; write_ptr <= 0;
end else if (write_enable) begin end else if (write_enable) begin
memory[write_ptr] <= write_dat; memory[write_ptr] <= write_dat;
if (write_ptr == FIFO_DEPTH-1)
write_ptr <= 0;
else
write_ptr <= write_ptr + 1; write_ptr <= write_ptr + 1;
end end
end end

View File

@ -1,4 +1,5 @@
#include <memory> #include <memory>
#include <cassert>
#include <limits> #include <limits>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
@ -6,7 +7,6 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
#include <unistd.h> #include <unistd.h>
#include <verilated.h> #include <verilated.h>
#include "Vram_fifo.h" #include "Vram_fifo.h"
using ModType = Vram_fifo; using ModType = Vram_fifo;
@ -14,6 +14,10 @@ ModType *mod;
uint32_t main_time = 0; uint32_t main_time = 0;
double sc_time_stamp() {
return main_time;
}
static void run_clock() { static void run_clock() {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
mod->clk = !mod->clk; mod->clk = !mod->clk;
@ -42,10 +46,11 @@ static void init_values() {
mod->write_dat = 0; mod->write_dat = 0;
} }
#define MAX_VALS 32000/24 #define MAX_VALS 1500
uint32_t vals[MAX_VALS]; uint32_t vals[MAX_VALS];
static void push(uint32_t v) { static void push(uint32_t v) {
assert(!mod->full);
mod->write_dat = v; mod->write_dat = v;
mod->write_enable = 1; mod->write_enable = 1;
run_clock(); run_clock();
@ -54,30 +59,47 @@ static void push(uint32_t v) {
} }
static void pop(int i) { static void pop(int i) {
assert(!mod->empty);
mod->read_enable = 1; mod->read_enable = 1;
run_clock(); run_clock();
mod->read_enable = 0; mod->read_enable = 0;
run_clock(); run_clock();
} }
static void push_random(int start, int end) {
for (int i = start; i < end; i++) {
vals[i] = rand() & 0xFFFFFFFFFFFF;
printf("%d\n", i);
push(vals[i]);
}
}
static void pop_random(int start, int end) {
for (int i = start; i < end; i++) {
pop(i);
if (mod->read_dat != vals[i]) {
fprintf(stderr, "expect %u, %u\n", vals[i], mod->read_dat);
exit(1);
}
}
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
init(argc, argv); init(argc, argv);
init_values(); init_values();
run_clock(); run_clock();
assert(mod->empty);
/* Simple test */ push_random(0, MAX_VALS);
for (int i = 0; i < MAX_VALS; i++) { assert(mod->full);
vals[i] = rand() & 0xFFFFFFFFFFFF;
push(vals[i]);
}
for (int i = 0; i < MAX_VALS; i++) { pop_random(0, MAX_VALS);
pop(i); assert(mod->empty);
if (mod->read_dat != vals[i]) {
fprintf(stderr, "expect %u, %u\n", vals[i], mod->read_dat); push_random(0, 50);
return 1; pop_random(0, 20);
} push_random(50, 100);
} pop_random(20, 100);
return 0; return 0;
} }