/
la_asyncfifo.v
194 lines (165 loc) · 6.85 KB
/
la_asyncfifo.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*****************************************************************************
* Function: Dual Clock Asynchronous FIFO
* Copyright: Lambda Project Authors. All rights Reserved.
* License: MIT (see LICENSE file in Lambda repository)
*
* Docs:
*
* This is a wrapper for selecting from a set of hardened memory macros.
*
* A synthesizable reference model is used when the TYPE is DEFAULT. The
* synthesizable model does not implement the cfg and test interface and should
* only be used for basic testing and for synthesizing for FPGA devices.
* Advanced ASIC development should rely on complete functional models
* supplied on a per macro basis.
*
* Technologoy specific implementations of "la_dpram" would generally include
* one ore more hardcoded instantiations of RAM modules with a generate
* statement relying on the "TYPE" to select between the list of modules
* at build time.
*
****************************************************************************/
module la_asyncfifo #(
parameter DW = 32, // Memory width
parameter DEPTH = 4, // FIFO depth
parameter NS = 1, // Number of power supplies
parameter CTRLW = 1, // width of asic ctrl interface
parameter TESTW = 1, // width of asic teset interface
parameter CHAOS = 0, // generates random full logic when set
parameter TYPE = "DEFAULT" // Pass through variable for hard macro
) ( // write port
input wr_clk,
input wr_nreset,
input [ DW-1:0] wr_din, // data to write
input wr_en, // write fifo
input wr_chaosmode, // randomly assert fifo full when set
output reg wr_full, // fifo full
// read port
input rd_clk,
input rd_nreset,
output [ DW-1:0] rd_dout, // output data (next cycle)
input rd_en, // read fifo
output reg rd_empty, // fifo is empty
// Power signals
input vss, // ground signal
input [ NS-1:0] vdd, // supplies
// Generic interfaces
input [CTRLW-1:0] ctrl, // pass through ASIC control interface
input [TESTW-1:0] test // pass through ASIC test interface
);
// local params
localparam AW = (DEPTH == 1) ? 1 : $clog2(DEPTH);
// local wires
reg [AW:0] wr_grayptr;
reg [AW:0] wr_binptr;
reg [AW:0] wr_binptr_mem;
wire [AW:0] wr_grayptr_nxt;
wire [AW:0] wr_binptr_nxt;
wire [AW:0] wr_binptr_mem_nxt;
wire [AW:0] wr_grayptr_sync;
wire wr_chaosfull;
reg [AW:0] rd_grayptr;
reg [AW:0] rd_binptr;
reg [AW:0] rd_binptr_mem;
wire [AW:0] rd_grayptr_nxt;
wire [AW:0] rd_binptr_nxt;
wire [AW:0] rd_binptr_mem_nxt;
wire [AW:0] rd_grayptr_sync;
genvar i;
//###########################
//# WRITE SIDE LOGIC
//###########################
always @(posedge wr_clk or negedge wr_nreset)
if (~wr_nreset) begin
wr_binptr_mem[AW:0] <= 'b0;
wr_binptr[AW:0] <= 'b0;
wr_grayptr[AW:0] <= 'b0;
end else begin
wr_binptr_mem[AW:0] <= (wr_binptr_mem_nxt[AW:0] == DEPTH) ? 'b0 : wr_binptr_mem_nxt[AW:0];
wr_binptr[AW:0] <= wr_binptr_nxt[AW:0];
wr_grayptr[AW:0] <= wr_grayptr_nxt[AW:0];
end
// Update binary pointer on write and not full
assign wr_binptr_mem_nxt[AW:0] = wr_binptr_mem[AW-1:0] + {{(AW-1){1'b0}}, (wr_en && ~wr_full)};
assign wr_binptr_nxt[AW:0] = wr_binptr[AW:0] + {{AW{1'b0}}, (wr_en && ~wr_full)};
// Convert binary point to gray pointer for sync
assign wr_grayptr_nxt[AW:0] = wr_binptr_nxt[AW:0] ^ {1'b0, wr_binptr_nxt[AW:1]};
// Full comparison (gray pointer based)
// Amir - add support for fifo DEPTH that is not power of 2
// Note - the previous logic also had a bug that full was high one entry to soon
reg [AW:0] rd_binptr_sync;
wire [AW:0] fifo_used;
integer j;
always @(*) begin
rd_binptr_sync[AW] = rd_grayptr_sync[AW];
for (j = AW; j > 0; j = j - 1)
rd_binptr_sync[j-1] = rd_binptr_sync[j] ^ rd_grayptr_sync[j-1];
end
assign fifo_used = wr_binptr - rd_binptr_sync;
always @(posedge wr_clk or negedge wr_nreset)
if (~wr_nreset) wr_full <= 1'b0;
else
wr_full <= (wr_chaosfull & wr_chaosmode) |
(fifo_used + {{AW{1'b0}}, (wr_en && ~wr_full)}) == DEPTH;
// Write --> Read clock synchronizer
for (i = 0; i < (AW + 1); i = i + 1) begin
la_drsync wrsync (
.out(wr_grayptr_sync[i]),
.clk(rd_clk),
.nreset(rd_nreset),
.in(wr_grayptr[i])
);
end
//###########################
//# READ SIDE LOGIC
//###########################
always @(posedge rd_clk or negedge rd_nreset)
if (~rd_nreset) begin
rd_binptr_mem[AW:0] <= 'b0;
rd_binptr[AW:0] <= 'b0;
rd_grayptr[AW:0] <= 'b0;
end else begin
rd_binptr_mem[AW:0] <= (rd_binptr_mem_nxt[AW:0] == DEPTH) ? 'b0 : rd_binptr_mem_nxt[AW:0];
rd_binptr[AW:0] <= rd_binptr_nxt[AW:0];
rd_grayptr[AW:0] <= rd_grayptr_nxt[AW:0];
end
assign rd_binptr_mem_nxt[AW:0] = rd_binptr_mem[AW-1:0] + {{(AW-1){1'b0}}, (rd_en && ~rd_empty)};
assign rd_binptr_nxt[AW:0] = rd_binptr[AW:0] + {{AW{1'b0}}, (rd_en && ~rd_empty)};
assign rd_grayptr_nxt[AW:0] = rd_binptr_nxt[AW:0] ^ {1'b0, rd_binptr_nxt[AW:1]};
// Full comparison (gray pointer based)
always @(posedge rd_clk or negedge rd_nreset)
if (~rd_nreset) rd_empty <= 1'b1;
else rd_empty <= (rd_grayptr_nxt[AW:0] == wr_grayptr_sync[AW:0]);
// Read --> write clock synchronizer
for (i = 0; i < (AW + 1); i = i + 1) begin
la_drsync rdsync (
.out(rd_grayptr_sync[i]),
.clk(wr_clk),
.nreset(wr_nreset),
.in(rd_grayptr[i])
);
end
//###########################
//# Dual Port Memory
//###########################
reg [DW-1:0] ram[DEPTH-1:0];
// Write port (FIFO input)
always @(posedge wr_clk) if (wr_en & ~wr_full) ram[wr_binptr_mem[AW-1:0]] <= wr_din[DW-1:0];
// Read port (FIFO output)
assign rd_dout[DW-1:0] = ram[rd_binptr_mem[AW-1:0]];
//############################
// Randomly Asserts FIFO full
//############################
generate
if (CHAOS) begin
// TODO: implement LFSR
reg chaosreg;
always @(posedge wr_clk or negedge wr_nreset)
if (~wr_nreset) chaosreg <= 1'b0;
else chaosreg <= ~chaosreg;
assign wr_chaosfull = chaosreg;
end else begin
assign wr_chaosfull = 1'b0;
end
endgenerate
endmodule