OpenCores
URL https://opencores.org/ocsvn/turbo8051/turbo8051/trunk

Subversion Repositories turbo8051

[/] [turbo8051/] [trunk/] [rtl/] [lib/] [async_fifo.v] - Blame information for rev 76

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 dinesha
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Tubo 8051 cores common library Module                       ////
4
////                                                              ////
5
////  This file is part of the Turbo 8051 cores project           ////
6
////  http://www.opencores.org/cores/turbo8051/                   ////
7
////                                                              ////
8
////  Description                                                 ////
9
////  Turbo 8051 definitions.                                     ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////    nothing                                                   ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Dinesh Annayya, dinesha@opencores.org                 ////
16
////                                                              ////
17 76 dinesha
////  Revision : Mar 2, 2011                                      //// 
18
////                                                              ////
19 11 dinesha
//////////////////////////////////////////////////////////////////////
20
////                                                              ////
21
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
22
////                                                              ////
23
//// This source file may be used and distributed without         ////
24
//// restriction provided that this copyright statement is not    ////
25
//// removed from the file and that any derivative work contains  ////
26
//// the original copyright notice and the associated disclaimer. ////
27
////                                                              ////
28
//// This source file is free software; you can redistribute it   ////
29
//// and/or modify it under the terms of the GNU Lesser General   ////
30
//// Public License as published by the Free Software Foundation; ////
31
//// either version 2.1 of the License, or (at your option) any   ////
32
//// later version.                                               ////
33
////                                                              ////
34
//// This source is distributed in the hope that it will be       ////
35
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
36
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
37
//// PURPOSE.  See the GNU Lesser General Public License for more ////
38
//// details.                                                     ////
39
////                                                              ////
40
//// You should have received a copy of the GNU Lesser General    ////
41
//// Public License along with this source; if not, download it   ////
42
//// from http://www.opencores.org/lgpl.shtml                     ////
43
////                                                              ////
44
//////////////////////////////////////////////////////////////////////
45
 
46
//-------------------------------------------
47
// async_fifo:: async FIFO
48
//    Following two ports are newly added
49
//        1. At write clock domain:
50
//           wr_total_free_space -->  Indicate total free transfer available 
51
//        2. At read clock domain:
52
//           rd_total_aval       -->  Indicate total no of transfer available
53
//-----------------------------------------------
54
 
55
module async_fifo (wr_clk,
56
                   wr_reset_n,
57
                   wr_en,
58
                   wr_data,
59
                   full,                 // sync'ed to wr_clk
60
                   afull,                 // sync'ed to wr_clk
61
                   wr_total_free_space,
62
                   rd_clk,
63
                   rd_reset_n,
64
                   rd_en,
65
                   empty,                // sync'ed to rd_clk
66
                   aempty,                // sync'ed to rd_clk
67
                   rd_total_aval,
68
                   rd_data);
69
 
70
   parameter W = 4'd8;
71
   parameter DP = 3'd4;
72
   parameter WR_FAST = 1'b1;
73
   parameter RD_FAST = 1'b1;
74
   parameter FULL_DP = DP;
75
   parameter EMPTY_DP = 1'b0;
76
 
77
   parameter AW = (DP == 2)   ? 1 :
78
                  (DP == 4)   ? 2 :
79
                  (DP == 8)   ? 3 :
80
                  (DP == 16)  ? 4 :
81
                  (DP == 32)  ? 5 :
82
                  (DP == 64)  ? 6 :
83
                  (DP == 128) ? 7 :
84
                  (DP == 256) ? 8 : 0;
85
 
86
   output [W-1 : 0]  rd_data;
87
   input [W-1 : 0]   wr_data;
88
   input             wr_clk, wr_reset_n, wr_en, rd_clk, rd_reset_n,
89
                     rd_en;
90
   output            full, empty;
91
   output            afull, aempty; // about full and about to empty
92
   output   [AW:0]   wr_total_free_space; // Total Number of free space aval 
93
                                               // w.r.t write clk
94
                                               // note: Without accounting byte enables
95
   output   [AW:0]   rd_total_aval;       // Total Number of words avaialble 
96
                                               // w.r.t rd clock, 
97
                                              // note: Without accounting byte enables
98
   // synopsys translate_off
99
 
100
   initial begin
101
      if (AW == 0) begin
102
         $display ("%m : ERROR!!! Fifo depth %d not in range 2 to 256", DP);
103
      end // if (AW == 0)
104
   end // initial begin
105
 
106
   // synopsys translate_on
107
   reg [W-1 : 0]    mem[DP-1 : 0];
108
 
109
   /*********************** write side ************************/
110
   reg [AW:0] sync_rd_ptr_0, sync_rd_ptr_1;
111
   wire [AW:0] sync_rd_ptr;
112
   reg [AW:0] wr_ptr, grey_wr_ptr;
113
   reg [AW:0] grey_rd_ptr;
114
   reg full_q;
115
   wire full_c;
116
   wire afull_c;
117
   wire [AW:0] wr_ptr_inc = wr_ptr + 1'b1;
118
   wire [AW:0] wr_cnt = get_cnt(wr_ptr, sync_rd_ptr);
119
 
120
   assign full_c  = (wr_cnt == FULL_DP) ? 1'b1 : 1'b0;
121
   assign afull_c = (wr_cnt == FULL_DP-1) ? 1'b1 : 1'b0;
122
 
123
   //--------------------------
124
   // Shows total number of words 
125
   // of free space available w.r.t write clock
126
   //--------------------------- 
127
   assign wr_total_free_space = FULL_DP - wr_cnt;
128
 
129
   always @(posedge wr_clk or negedge wr_reset_n) begin
130
        if (!wr_reset_n) begin
131
                wr_ptr <= 0;
132
                grey_wr_ptr <= 0;
133
                full_q <= 0;
134
        end
135
        else if (wr_en) begin
136
                wr_ptr <= wr_ptr_inc;
137
                grey_wr_ptr <= bin2grey(wr_ptr_inc);
138
                if (wr_cnt == (FULL_DP-1)) begin
139
                        full_q <= 1'b1;
140
                end
141
        end
142
        else begin
143
                if (full_q && (wr_cnt<FULL_DP)) begin
144
                        full_q <= 1'b0;
145
                end
146
        end
147
    end
148
 
149
    assign full  = (WR_FAST == 1) ? full_c : full_q;
150 20 dinesha
    assign afull = afull_c;
151 11 dinesha
 
152
    always @(posedge wr_clk) begin
153
        if (wr_en) begin
154
                mem[wr_ptr[AW-1:0]] <= wr_data;
155
        end
156
    end
157
 
158
    wire [AW:0] grey_rd_ptr_dly ;
159
    assign #1 grey_rd_ptr_dly = grey_rd_ptr;
160
 
161
    // read pointer synchronizer
162
    always @(posedge wr_clk or negedge wr_reset_n) begin
163
        if (!wr_reset_n) begin
164
                sync_rd_ptr_0 <= 0;
165
                sync_rd_ptr_1 <= 0;
166
        end
167
        else begin
168
                sync_rd_ptr_0 <= grey_rd_ptr_dly;
169
                sync_rd_ptr_1 <= sync_rd_ptr_0;
170
        end
171
    end
172
 
173
    assign sync_rd_ptr = grey2bin(sync_rd_ptr_1);
174
 
175
   /************************ read side *****************************/
176
   reg [AW:0] sync_wr_ptr_0, sync_wr_ptr_1;
177
   wire [AW:0] sync_wr_ptr;
178
   reg [AW:0] rd_ptr;
179
   reg empty_q;
180
   wire empty_c;
181
   wire aempty_c;
182
   wire [AW:0] rd_ptr_inc = rd_ptr + 1'b1;
183
   wire [AW:0] sync_wr_ptr_dec = sync_wr_ptr - 1'b1;
184
   wire [AW:0] rd_cnt = get_cnt(sync_wr_ptr, rd_ptr);
185
 
186
   assign empty_c  = (rd_cnt == 0) ? 1'b1 : 1'b0;
187
   assign aempty_c = (rd_cnt == 1) ? 1'b1 : 1'b0;
188
   //--------------------------
189
   // Shows total number of words 
190
   // space available w.r.t write clock
191
   //--------------------------- 
192
   assign rd_total_aval = rd_cnt;
193
 
194
   always @(posedge rd_clk or negedge rd_reset_n) begin
195
        if (!rd_reset_n) begin
196
                rd_ptr <= 0;
197
                grey_rd_ptr <= 0;
198
                empty_q <= 1'b1;
199
        end
200
        else begin
201
                if (rd_en) begin
202
                        rd_ptr <= rd_ptr_inc;
203
                        grey_rd_ptr <= bin2grey(rd_ptr_inc);
204
                        if (rd_cnt==(EMPTY_DP+1)) begin
205
                                empty_q <= 1'b1;
206
                        end
207
                end
208
                else begin
209
                        if (empty_q && (rd_cnt!=EMPTY_DP)) begin
210
                                empty_q <= 1'b0;
211
                        end
212
                end
213
        end
214
    end
215
 
216
    assign empty  = (RD_FAST == 1) ? empty_c : empty_q;
217
    assign aempty = aempty_c;
218
 
219
    assign rd_data = mem[rd_ptr[AW-1:0]];
220
 
221
    wire [AW:0] grey_wr_ptr_dly ;
222
    assign #1 grey_wr_ptr_dly =  grey_wr_ptr;
223
 
224
    // write pointer synchronizer
225
    always @(posedge rd_clk or negedge rd_reset_n) begin
226
        if (!rd_reset_n) begin
227
                sync_wr_ptr_0 <= 0;
228
                sync_wr_ptr_1 <= 0;
229
        end
230
        else begin
231
                sync_wr_ptr_0 <= grey_wr_ptr_dly;
232
                sync_wr_ptr_1 <= sync_wr_ptr_0;
233
        end
234
    end
235
    assign sync_wr_ptr = grey2bin(sync_wr_ptr_1);
236
 
237
 
238
/************************ functions ******************************/
239
function [AW:0] bin2grey;
240
input [AW:0] bin;
241
reg [8:0] bin_8;
242
reg [8:0] grey_8;
243
begin
244
        bin_8 = bin;
245
        grey_8[1:0] = do_grey(bin_8[2:0]);
246
        grey_8[3:2] = do_grey(bin_8[4:2]);
247
        grey_8[5:4] = do_grey(bin_8[6:4]);
248
        grey_8[7:6] = do_grey(bin_8[8:6]);
249
        grey_8[8] = bin_8[8];
250
        bin2grey = grey_8;
251
end
252
endfunction
253
 
254
function [AW:0] grey2bin;
255
input [AW:0] grey;
256
reg [8:0] grey_8;
257
reg [8:0] bin_8;
258
begin
259
        grey_8 = grey;
260
        bin_8[8] = grey_8[8];
261
        bin_8[7:6] = do_bin({bin_8[8], grey_8[7:6]});
262
        bin_8[5:4] = do_bin({bin_8[6], grey_8[5:4]});
263
        bin_8[3:2] = do_bin({bin_8[4], grey_8[3:2]});
264
        bin_8[1:0] = do_bin({bin_8[2], grey_8[1:0]});
265
        grey2bin = bin_8;
266
end
267
endfunction
268
 
269
 
270
function [1:0] do_grey;
271
input [2:0] bin;
272
begin
273
        if (bin[2]) begin  // do reverse grey
274
                case (bin[1:0])
275
                        2'b00: do_grey = 2'b10;
276
                        2'b01: do_grey = 2'b11;
277
                        2'b10: do_grey = 2'b01;
278
                        2'b11: do_grey = 2'b00;
279
                endcase
280
        end
281
        else begin
282
                case (bin[1:0])
283
                        2'b00: do_grey = 2'b00;
284
                        2'b01: do_grey = 2'b01;
285
                        2'b10: do_grey = 2'b11;
286
                        2'b11: do_grey = 2'b10;
287
                endcase
288
        end
289
end
290
endfunction
291
 
292
function [1:0] do_bin;
293
input [2:0] grey;
294
begin
295
        if (grey[2]) begin      // actually bin[2]
296
                case (grey[1:0])
297
                        2'b10: do_bin = 2'b00;
298
                        2'b11: do_bin = 2'b01;
299
                        2'b01: do_bin = 2'b10;
300
                        2'b00: do_bin = 2'b11;
301
                endcase
302
        end
303
        else begin
304
                case (grey[1:0])
305
                        2'b00: do_bin = 2'b00;
306
                        2'b01: do_bin = 2'b01;
307
                        2'b11: do_bin = 2'b10;
308
                        2'b10: do_bin = 2'b11;
309
                endcase
310
        end
311
end
312
endfunction
313
 
314
function [AW:0] get_cnt;
315
input [AW:0] wr_ptr, rd_ptr;
316
begin
317
        if (wr_ptr >= rd_ptr) begin
318
                get_cnt = (wr_ptr - rd_ptr);
319
        end
320
        else begin
321
                get_cnt = DP*2 - (rd_ptr - wr_ptr);
322
        end
323
end
324
endfunction
325
 
326
// synopsys translate_off
327
always @(posedge wr_clk) begin
328
   if (wr_en && full) begin
329
      $display($time, "%m Error! afifo overflow!");
330
      $stop;
331
   end
332
end
333
 
334
always @(posedge rd_clk) begin
335
   if (rd_en && empty) begin
336
      $display($time, "%m error! afifo underflow!");
337
      $stop;
338
   end
339
end
340
 
341
// gray code monitor
342
reg [AW:0] last_gwr_ptr;
343
always @(posedge wr_clk or negedge wr_reset_n) begin
344
   if (!wr_reset_n) begin
345
      last_gwr_ptr <= #1 0;
346
   end
347
   else if (last_gwr_ptr !== grey_wr_ptr) begin
348
      check_ptr_chg(last_gwr_ptr, grey_wr_ptr);
349
      last_gwr_ptr <= #1 grey_wr_ptr;
350
   end
351
end
352
 
353
reg [AW:0] last_grd_ptr;
354
always @(posedge rd_clk or negedge rd_reset_n) begin
355
   if (!rd_reset_n) begin
356
     last_grd_ptr <= #1 0;
357
   end
358
   else if (last_grd_ptr !== grey_rd_ptr) begin
359
      check_ptr_chg(last_grd_ptr, grey_rd_ptr);
360
      last_grd_ptr <= #1 grey_rd_ptr;
361
   end
362
end
363
 
364
task check_ptr_chg;
365
input [AW:0] last_ptr;
366
input [AW:0] cur_ptr;
367
integer i;
368
integer ptr_diff;
369
begin
370
   ptr_diff = 0;
371
   for (i=0; i<= AW; i=i+ 1'b1) begin
372
      if (last_ptr[i] != cur_ptr[i]) begin
373
         ptr_diff = ptr_diff + 1'b1;
374
      end
375
   end
376
   if (ptr_diff !== 1) begin
377
      $display($time, "%m, ERROR! async fifo ptr has changed more than noe bit, last=%h, cur=%h",
378
                                last_ptr, cur_ptr);
379
      $stop;
380
   end
381
end
382
endtask
383
   // synopsys translate_on
384
 
385
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.