forked from vipinkmenon/tutorialsOnVerilog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.v
More file actions
79 lines (66 loc) · 1.73 KB
/
fifo.v
File metadata and controls
79 lines (66 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module fifo #(parameter FIFO_WIDTH=8, FIFO_DEPTH=8)(
input clk,
input reset,
input fifoWrEn,
input [FIFO_WIDTH-1:0] fifoWrData,
output fifoFull,
input fifoRdEn,
output [FIFO_WIDTH-1:0] fifoRdData,
output fifoEmpty,
output [$clog2(FIFO_DEPTH):0] fifoDataCount
);
reg wrEnInt;
reg [FIFO_WIDTH-1:0] fifoWrDatap;
reg [$clog2(FIFO_DEPTH):0] dataCounter;
reg [$clog2(FIFO_DEPTH)-1:0] wrPointer;
reg [$clog2(FIFO_DEPTH)-1:0] rdPointer;
assign validFifoWr = fifoWrEn & !fifoFull;
assign validFifoRd = fifoRdEn & !fifoEmpty;
assign fifoDataCount = dataCounter;
assign fifoFull = (dataCounter==FIFO_DEPTH)? 1'b1 : 1'b0;
assign fifoEmpty = (dataCounter==0)? 1'b1 : 1'b0;
//pipeline for aligning with ram wr en
always @(posedge clk)
fifoWrDatap <= fifoWrData;
//Fifo write logic
always @(posedge clk)
begin
if(reset)
wrEnInt <= 1'b0;
else if(validFifoWr)
wrEnInt <= 1'b1;
else
wrEnInt <= 1'b0;
end
always @(posedge clk)
begin
if(reset)
dataCounter <= 0;
else if(validFifoWr & !validFifoRd)
dataCounter <= dataCounter+1'b1;
else if(validFifoRd & !validFifoWr)
dataCounter <= dataCounter-1'b1;
end
always @(posedge clk)
begin
if(reset)
wrPointer <= 0;
else if(wrEnInt)
wrPointer <= wrPointer+1'b1;
end
always @(posedge clk)
begin
if(reset)
rdPointer <= 0;
else if(validFifoRd)
rdPointer <= rdPointer+1'b1;
end
ram #(.Width(FIFO_WIDTH),.Depth(FIFO_DEPTH))rm(
.clk(clk),
.wrEn(wrEnInt), //Should become high when you want to write to the memory
.wrAddr(wrPointer), //address for writing
.wrData(fifoWrDatap), //Data to be written
.rdAddr(rdPointer),
.rdData(fifoRdData)
);
endmodule