-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo_memory.v
More file actions
81 lines (68 loc) · 1.64 KB
/
fifo_memory.v
File metadata and controls
81 lines (68 loc) · 1.64 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
80
81
// Code your design here
module fifo(data_out,fifo_full,fifo_empty,clk,wr_en,rd_en,rst,data_in);
input clk,wr_en,rd_en,rst;
input [7:0] data_in;
output reg fifo_full,fifo_empty;
reg [7:0] fifo_count;
reg [7:0] mem[63:0];
output [7:0] data_out;
reg [5:0] wr_ptr,rd_ptr;
// status of fifo
always@(fifo_count)
begin
fifo_empty = (fifo_count == 0);
fifo_full = (fifo_count == 64);
end
//counter
always@(posedge clk or posedge rst)
begin
if(rst)
fifo_count <= 0;
else if((!fifo_full && wr_en) &&(!fifo_empty && rd_en))
//both happenning at the same time
fifo_count <= fifo_count;
else if(!fifo_empty && rd_en)
fifo_count <= fifo_count - 1;
else if(!fifo_full && wr_en)
fifo_count <= fifo_count + 1;
else
fifo_count <= fifo_count;
end
//read
always@(posedge clk or posedge rst)
begin
if(rst)
data_out <= 0;
else if(!fifo_empty && rd_en)
data_out <= mem[rd_ptr];
else
data_out <= data_out;
end
//write
always@(posedge clk)
begin
if(!fifo_full && wr_en)
mem[wr_ptr] <= data_in;
else
mem[wr_ptr] <= mem[wr_ptr];
end
//memory pointers
always@(posedge clk or posedge rst)
begin
if(rst)
begin
wr_ptr <= 0;
rd_ptr <= 0;
end
else begin
if(!fifo_full && wr_en)
wr_ptr <= wr_ptr + 1;
else
wr_ptr <= wr_ptr;
if(!fifo_empty && rd_en)
rd_ptr <= rd_ptr + 1;
else
rd_ptr <= rd_ptr;
end
end
endmodule