-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo.v
167 lines (161 loc) · 5.46 KB
/
fifo.v
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Wissance (https://wissance.com)
// Engineer: EvilLord666 (Ushakov MV - https://github.com/EvilLord666)
//
// Create Date: 05.09.2023
// Design Name:
// Module Name: fifo
// Project Name: QuickRS232
// Target Devices: Any
// Tool Versions: Quartus Prime Lite 18.1
// Description: A module that store and manages multiple bytes store
//
// Dependencies: No
//
// Revision: 1.0
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module fifo #
(
parameter FIFO_SIZE = 8,
parameter DATA_WIDTH = 32
)
(
input wire clk,
// input wire enable,
input wire clear,
// output wire fifo_ready,
input wire push,
input wire pop,
input wire [DATA_WIDTH - 1:0] in_data,
output wire [DATA_WIDTH - 1:0] out_data,
output wire popped_last,
output wire pushed_last
);
reg [DATA_WIDTH - 1:0] fifo_data [FIFO_SIZE - 1 : 0];
reg [DATA_WIDTH - 1:0] buffer;
reg pushed_last_value;
reg popped_last_value;
reg [15:0] data_count;
reg [15:0] position;
reg [15:0] counter;
reg [2:0] fifo_state;
localparam reg [2:0] INITIAL_STATE = 1;
localparam reg [2:0] PUSH_STARTED = 2;
localparam reg [2:0] PUSH_FINISHED = 3;
localparam reg [2:0] POP_STARTED = 4;
localparam reg [2:0] POP_FINISHED = 5;
localparam reg [2:0] OPERATION_AWAITING = 6;
assign out_data = buffer;
assign pushed_last = pushed_last_value;
assign popped_last = popped_last_value;
always@ (posedge clk)
begin
if (clear == 1'b1)
begin
fifo_state = INITIAL_STATE;
for(counter = 0; counter < FIFO_SIZE; counter = counter + 16'h01)
fifo_data[counter] <= 0;
position <= 0;
data_count <= 0;
popped_last_value <= 1'b1;
pushed_last_value <= 1'b0;
buffer <= 0;
end
else
begin
case (fifo_state)
INITIAL_STATE:
begin
fifo_state = OPERATION_AWAITING;
end
OPERATION_AWAITING:
begin
// if position == 0, nothing to pop
if (position == 0)
begin
popped_last_value <= 1'b1;
end
else
begin
popped_last_value <= 1'b0;
end
// if buffer is full, nothing to push
if(data_count == FIFO_SIZE)
begin
pushed_last_value <= 1'b1;
end
else
begin
pushed_last_value <= 1'b0;
end
// push have a priority over pop
if (push == 1'b1)
begin
fifo_state <= PUSH_STARTED;
end
if (pop == 1'b1)
begin
fifo_state <= POP_STARTED;
end
end
PUSH_STARTED:
begin
if (data_count < FIFO_SIZE)
begin
fifo_data[position] <= in_data;
position <= position + 16'h01; // position is an index of next item ...
data_count <= data_count + 16'h01;
fifo_state <= PUSH_FINISHED;
popped_last_value <= 1'b0;
if (position == FIFO_SIZE - 1)
begin
pushed_last_value <= 1'b1;
end
end
else
begin
fifo_state <= OPERATION_AWAITING;
end
end
PUSH_FINISHED:
begin
if (push == 1'b0)
begin
fifo_state <= OPERATION_AWAITING;
end
end
POP_STARTED:
begin
if (data_count > 0)
begin
buffer <= fifo_data[0];
data_count <= data_count - 16'h01;
pushed_last_value <= 0;
for(counter = 0; counter < FIFO_SIZE - 1; counter = counter + 16'h01)
fifo_data[counter] <= fifo_data[counter + 1];
fifo_data[FIFO_SIZE - 1] <= 0;
position <= position - 16'h01;
fifo_state <= POP_FINISHED;
pushed_last_value <= 1'b0;
if(position == 1)
popped_last_value <= 1'b1;
end
else
begin
fifo_state <= OPERATION_AWAITING;
end
end
POP_FINISHED:
begin
if (pop == 1'b0)
begin
fifo_state <= OPERATION_AWAITING;
end
end
endcase
end
end
endmodule