一乐电子

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

微信扫码登录

手机号码,快捷登录

手机号码,快捷登录

搜索
查看: 6892|回复: 2

FIFO 代码

[复制链接]
发表于 2009-11-14 20:46 | 显示全部楼层 |阅读模式
////////////////////////////////////////////////////////////////////
. {, q" H) u9 Z  B! }" i// FileName:  "Fifo.v"9 x* p' h  \! R% u: u8 }
// Author  :  Venkata Ramana Kalapatapu
  y* t9 z* e+ \( l% N7 p, f// Company :  Sand Microelectronics Inc.1 {# G7 F$ e" d7 u$ R
//           (now a part of Synopsys, Inc.),
% \  ^5 ]2 ^3 P' l// Profile :  Sand develops Simulation Models, Synthesizable Cores and& L+ V8 A, E; t, q+ N
//           Performance Analysis Tools for Processors, buses and3 R) ]" t9 q& M- ]( K
//           memory products.  Sand's products include models for. R3 F2 y; P, ^0 E
//           industry-standard components and custom-developed models' k& u9 [% g( u2 D8 N
//           for specific simulation environments.
& O' F: \  u# Q; N3 b$ ]9 i  x//4 O# A0 S- ?% ^% u& t
////////////////////////////////////////////////////////////////////
" K2 K; t- o* P8 e8 W) U5 F; `+ O: w) e7 S* w7 Q1 \; B* L
`define  FWIDTH     32           // Width of the FIFO.
/ U4 P1 ~* \$ d- u# R' B`define  FDEPTH     4            // Depth of the FIFO.
* c1 e! B" n3 X/ o. p( c5 ?4 H% Y`define  FCWIDTH    2            // Counter Width of the FIFO 2 to power
# h2 ]+ a) A8 k0 e                                 // FCWIDTH = FDEPTH.
- b* ?  A  @* j: Rmodule   fifo(  Clk,: f2 R+ b# f; n  N
              RstN,& \( `: `# O+ n9 U
              Data_In,
5 @; n+ G; a: w1 A1 j* Y  o/ U/ {& u              FClrN,, p' d8 q5 L& P" I* F
              FInN,
' n+ a" V- B/ G4 ?# o. M% p5 i+ x              FOutN,/ I6 V7 E  J7 h8 f3 P3 h4 r
              F_Data,
# W9 }- N1 t* K! x: E9 m              F_FullN,' T: W" w5 P# y5 z
              F_LastN,
3 x2 ~- V# |0 q( D, n2 f+ B6 ]              F_SLastN,
2 d  ~1 {/ l! O9 }              F_FirstN,3 d  p# F. ]9 n! c
              F_EmptyN* N1 x  ?* h6 j
           );3 ]( r+ E9 I5 y$ `9 T

( S" Y; d  _3 t* J6 R. W" E3 u/ R, y+ z& Tinput                       Clk;      // CLK signal.; C5 G" p( u! }& C
input                       RstN;     // Low Asserted Reset signal.( ?0 Y+ I; v' s! i  n6 V
input [(`FWIDTH-1):0]       Data_In;  // Data into FIFO.) [& T- @5 L' ?
input                       FInN;     // Write into FIFO Signal.
7 p# g. @2 T( N9 _input                       FClrN;    // Clear signal to FIFO.
' e: g9 f+ ?; ~& `8 rinput                       FOutN;    // Read from FIFO signal." c0 P0 V; j6 E" A
output [(`FWIDTH-1):0]      F_Data;   // FIFO data out.
6 j% g; @4 e" I1 Goutput                      F_FullN;  // FIFO full indicating signal.
1 n- Q5 f1 ]4 H# q4 Y; eoutput                      F_EmptyN; // FIFO empty indicating signal.9 r7 M3 z$ w2 F7 E4 @# X- t/ d0 L
output                      F_LastN;  // FIFO Last but one signal.9 m$ e$ x, @4 Y& ], P! F
output                      F_SLastN; // FIFO SLast but one signal.( ~, i7 _  o8 v: _3 W% w2 P
output                      F_FirstN; // Signal indicating only one- M1 t2 H! M5 |7 {3 f3 Z
                                      // word in FIFO.* [: t3 J8 E( `) E" F

- N% i8 z- P- C  Z% ?3 ureg                F_FullN;  s: ^, D$ i/ y2 i, [8 V, [. e
reg                F_EmptyN;
- v; R  z% e+ k9 m8 }) w4 R# ~reg                F_LastN;- D, |& X& H& @) g( W2 g/ m
reg                F_SLastN;
* B- l8 v6 ?" Greg                F_FirstN;3 u; k6 o( y* ]- X0 q4 g
reg    [`FCWIDTH:0]      fcounter; //counter indicates num of data in FIFO9 v( P* s  u7 I9 P* h: O
reg    [(`FCWIDTH-1):0]   rd_ptr;      // Current read pointer.
0 A8 L. I# Q- r1 preg    [(`FCWIDTH-1):0]   wr_ptr;      // Current write pointer.
6 d+ q- i1 q$ ?- v+ q; w  \wire   [(`FWIDTH-1):0]    FIFODataOut; // Data out from FIFO MemBlk% q5 E5 |" j$ k. c8 ~2 V2 Z
wire   [(`FWIDTH-1):0]    FIFODataIn;  // Data into FIFO MemBlk
* E% O) T4 f9 V& c" [! S0 Rwire   ReadN  = FOutN;( q9 P& h8 Y9 o( z
wire   WriteN = FInN;
: }: L, g& G4 N: Uassign F_Data     = FIFODataOut;
, z6 W: s1 K& t0 yassign FIFODataIn = Data_In;
' C4 F& z& R% N* A3 \4 z
. Q, n5 f! j! `    FIFO_MEM_BLK memblk(.clk(Clk),
6 x* e: e' o  C) _" g6 f                        .writeN(WriteN),8 d5 Z7 j( q7 @/ e
                        .rd_addr(rd_ptr),  c2 U: w* _- t/ e
                        .wr_addr(wr_ptr),
- i' G) y0 F- f2 d/ g3 o                        .data_in(FIFODataIn),
0 M: A+ F1 v( c' r                        .data_out(FIFODataOut)
0 |! _+ O% o$ @2 {4 ^& \! F                       );
1 K& C9 w9 O5 |7 W9 _    // Control circuitry for FIFO. If reset or clr signal is asserted,' ?0 I" Z; r( j  Y3 C
    // all the counters are set to 0. If write only the write counter$ r# y( P  b& z9 x0 W4 s* g( \
    // is incremented else if read only read counter is incremented
1 F( \- q* K1 t. R' w" G8 Q    // else if both, read and write counters are incremented.
. s/ x: S: J$ V5 |$ f) P6 E; a    // fcounter indicates the num of items in the FIFO. Write only) P7 @5 C/ h( U
    // increments the fcounter, read only decrements the counter, and
. Y0 C  b* {2 p9 N- q    // read && write doesn't change the counter value./ A6 ^( Q; o  V2 r
    always @(posedge Clk or negedge RstN)
& G: U" ?+ e4 N; K# L    begin
# F' r, n5 W6 E2 _$ I       if(!RstN) begin
- q8 t5 l2 L1 L! C+ o7 n           fcounter    <= 0;& k! r8 S; {- r. F2 \
           rd_ptr      <= 0;( ~4 S3 O* f( P0 G* T. O8 Z
           wr_ptr      <= 0;
) j* r* `+ a& U' v  q3 k! |       end
) T1 z) `- }# V/ Y, q( D       else begin
) l$ z. G% r% Q$ p3 A0 q           if(!FClrN ) begin
1 P0 ?8 R7 _7 y* B: F4 ^! t               fcounter    <= 0;2 t5 z4 R' {* C! ~, N- l
               rd_ptr      <= 0;
2 W. D! i$ |! r  q6 w               wr_ptr      <= 0;! W" J: `/ x- G8 L2 K$ U
           end) {; X: k5 c- ?; {! K* h* `
           else begin1 r4 z& d! `+ f# ]( L
               if(!WriteN && F_FullN)
& X. E1 N! E& ~                   wr_ptr <= wr_ptr + 1;9 W8 P8 y% T4 E" l( Y: c$ H
               if(!ReadN && F_EmptyN)" R& s- v% I- O7 q/ w
                   rd_ptr <= rd_ptr + 1;
. P" o' Q9 _$ h! K               if(!WriteN && ReadN && F_FullN); j1 S8 Y' H% N
                   fcounter <= fcounter + 1;; E( e  @9 F) d
               else if(WriteN && !ReadN && F_EmptyN)
% _$ _' W# X( z9 k3 Y) c/ n                   fcounter <= fcounter - 1;
$ s- x! _, f$ T6 B          end- N, y) [0 O: h6 ~5 M
       end5 B# n( P3 g' O8 Y5 e- d
    end
0 Y  [$ D1 {! h# }3 C0 Y    // All the FIFO status signals depends on the value of fcounter.
+ V/ u; W& Q/ K, v7 ^    // If the fcounter is equal to fdepth, indicates FIFO is full.
2 Z9 ~5 w( Q: f8 _' w0 E2 {    // If the fcounter is equal to zero, indicates the FIFO is empty.
/ y! E3 Y2 q5 v/ r% D    // F_EmptyN signal indicates FIFO Empty Status. By default it is$ o# o! ?* I+ U
    // asserted, indicating the FIFO is empty. After the First Data is
+ {& Q& b& \3 u    // put into the FIFO the signal is deasserted.
' G4 o' F* |8 E; y$ z    always @(posedge Clk or negedge RstN)
; `! Q" c2 b5 S' P) l    begin
- J' p& q8 v1 C, a& l6 Z% R       if(!RstN)5 E7 O/ ~) D: G/ a9 O& F: n* a
          F_EmptyN <= 1'b0;
& z" |& |9 a: T" ^" h       else begin
2 L! m. U& E0 C          if(FClrN==1'b1) begin" Z  `( \: ~! A* \" S% ?# i& x
             if(F_EmptyN==1'b0 && WriteN==1'b0)$ S6 I1 w( o% H5 p6 D
                 F_EmptyN <= 1'b1;& y& i. ]3 R! ^( p
             else if(F_FirstN==1'b0 && ReadN==1'b0 && WriteN==1'b1)
' D4 k; Y% T- @$ T# B" U% W                 F_EmptyN <= 1'b0;
! t, G4 i) S- n& z+ [+ n7 f+ g' G          end% k8 a5 p9 U9 }% Y  m
          else) ]. x% |: S6 i
             F_EmptyN <= 1'b0;
# l6 i. Z4 {5 k5 D       end* W  Q1 x5 \% y; N+ y
    end
: _7 N* L( e- `6 q, O$ M6 [  x/ h    // F_FirstN signal indicates that there is only one datum sitting
8 T$ U8 F$ I6 N8 T$ H  u    // in the FIFO. When the FIFO is empty and a write to FIFO occurs,% z& V  p5 _3 p2 n. H8 J; z- h$ F
    // this signal gets asserted.
% l, U/ L& F' [4 b    always @(posedge Clk or negedge RstN)
: u1 L# d# W9 \2 ]9 M$ ]" D' g! M    begin+ G0 `# @  j* V' \0 D0 {  B
       if(!RstN)# ?' f3 q) D7 y  W: j. L1 v
          F_FirstN <= 1'b1;
; |; U! ~; a) n" }; B       else begin1 ?: f: P' O' Y/ ]: m+ n
          if(FClrN==1'b1) begin* D7 V& w2 m* ]$ w
             if((F_EmptyN==1'b0 && WriteN==1'b0) ||
; q% ^7 m, t3 Q& n3 x                (fcounter==2 && ReadN==1'b0 && WriteN==1'b1))
9 r4 O. [! a/ q                 F_FirstN <= 1'b0;
, S: @' u4 @6 ]* x$ }: i$ K             else if (F_FirstN==1'b0 && (WriteN ^ ReadN))
. n# v2 d1 @$ S9 k- Q" h                 F_FirstN <= 1'b1;
4 {, v' f& z- ?+ r3 B$ d8 [; t4 E          end
( t7 G7 L( E' i, J          else begin/ F7 ]$ @/ v% y6 F! f4 i: m+ i. w1 I
             F_FirstN <= 1'b1;
5 `# R9 D; s- |  z& h% V' [- ]          end# H4 x6 L4 t( Y! g/ {1 n5 a: n( \
       end
: u2 b! C/ @! _# o; P% }$ t, x- n    end2 a8 w0 I( r7 S- g8 a
# t8 \3 I1 p( d+ r: n& F
    // F_SLastN indicates that there is space for only two data words
  p4 I% p0 w+ Q    //in the FIFO.
! v. t& K; P7 T5 J( D( I$ a" n" T    always @(posedge Clk or negedge RstN)
1 B2 o* m* n" p/ p2 W9 `    begin* t, M! ?2 O3 _+ E: p
       if(!RstN)
# l7 z+ u+ i+ j* x. p! f9 K          F_SLastN <= 1'b1;
. ^( w( D) S2 p4 y# b       else begin
+ A8 V6 B5 Y) X2 }* X; E          if(FClrN==1'b1) begin! T" o! o4 M9 f1 B) c4 j1 C" I
             if( (F_LastN==1'b0 && ReadN==1'b0 && WriteN==1'b1) ||
& p% a' L7 R# s) `9 q                 (fcounter == (`FDEPTH-3) && WriteN==1'b0 && ReadN==1'b1))- q+ K, b" Y6 N- C/ H, o, [4 b
                 F_SLastN <= 1'b0;9 \- R; x: f- L: j

( j! U6 J8 T3 X" S& S7 \, K, i# c$ B             else if(F_SLastN==1'b0 && (ReadN ^ WriteN) )
) h/ {6 l; }$ g; ^" s+ ~& i8 P                 F_SLastN <= 1'b1;
& g0 Y8 A$ N& j3 A" ?4 y          end
* Y/ c5 H! q4 _9 A; R& _          else# h2 P2 x! ?- C9 S9 T& S1 U9 t
             F_SLastN <= 1'b1;
3 E9 p7 l4 j. ~, x) \3 x3 g7 k2 \       end: t0 W  s+ H5 p4 t7 E6 r
    end( P" |$ \% }7 S, L6 J2 Q+ K
    // F_LastN indicates that there is one space for only one data
/ R1 |( \6 b4 w* y    // word in the FIFO.
7 R# G  s% h8 i4 j    always @(posedge Clk or negedge RstN)+ T0 V* o' ]( x2 ?" I
    begin
% X7 J9 L( ~/ N8 j       if(!RstN)
$ W+ c9 D1 t$ Z6 ?3 M( ]8 W          F_LastN <= 1'b1;( w7 c9 ?; B" u! b( W7 m6 V3 K  S
       else begin
; _; W7 S" E; M0 A          if(FClrN==1'b1) begin
4 u, g. N8 `' V             if ((F_FullN==1'b0 && ReadN==1'b0)  ||
) ~9 J% p. g9 V  ]' l  l                 (fcounter == (`FDEPTH-2) && WriteN==1'b0 && ReadN==1'b1))
" C1 [* p# V9 s! e5 W+ W/ I                 F_LastN <= 1'b0;/ }% a9 H; m* L; c8 {
             else if(F_LastN==1'b0 && (ReadN ^ WriteN) ): T( Q& T) z* I
                 F_LastN <= 1'b1;
) U; Z, L' ~  B9 x; e6 H          end# ^$ w3 |4 n0 P- |: o6 t
          else
# _& Y: r! _3 Y% t" O$ @; @             F_LastN <= 1'b1;( y  X+ S5 Z+ P. X4 f
       end
) ~. `8 S, k, o6 l8 |1 M    end6 {6 l. V2 N' I  P# ?
. m7 H* n9 c6 \6 j
    // F_FullN indicates that the FIFO is full.1 _5 D/ q' x- \2 P, C/ q. p; J
    always @(posedge Clk or negedge RstN)
, {( E8 L6 g4 C  a+ Z+ H    begin
7 a% S* U# z3 @2 }6 @1 z       if(!RstN)
" M) K' ]8 T# X" _4 D           F_FullN <= 1'b1;
6 A5 |5 I  d) C9 [       else begin" A' y9 p: T) M& u
           if(FClrN==1'b1)  begin9 O2 }! g  \7 _( A0 w  A7 K* {
               if (F_LastN==1'b0 && WriteN==1'b0 && ReadN==1'b1)
) d! z8 n' E8 c8 L                    F_FullN <= 1'b0;# S+ f0 r; f2 E9 b! \, a" L
               else if(F_FullN==1'b0 && ReadN==1'b0)( M3 p  Q/ z7 l. o* b: v
                    F_FullN <= 1'b1;
8 J5 q% b+ C) x           end
. B: x  {- R3 v7 m, y/ A4 _! K5 G' M" m           else
( K4 T% f# w3 i' ^- R4 g. n               F_FullN <= 1'b1;
4 D- u4 _4 w9 \/ n! r( Y/ n       end% J( t# O- b+ ~1 E
    end% W; n+ K; [0 k; C
endmodule
8 i. w* ~1 n! }6 t; j& Z% P- p1 h% Q" C& l6 }0 p7 s+ f& M6 R

! }2 T" p0 R7 y- w///////////////////////////////////////////////////////////////////
! z* _* |  S% G* ~//
1 [; Q- a9 P; @2 V//
% y% q/ S- i5 a//   Configurable memory block for fifo. The width of the mem" T! U: r: B% s! T2 t9 b" U
//   block is configured via FWIDTH. All the data into fifo is done% q9 t9 t* Q9 ]9 P2 U: e
//   synchronous to block.
+ ~- M9 [/ ?9 G6 ]8 j" |2 w//# x2 O: ?  r) z6 c
//   Author : Venkata Ramana Kalapatapu
* Q* ?1 E: P4 n6 v/ |//
! S+ i! K( v' c3 c( m; w///////////////////////////////////////////////////////////////////1 t1 p2 i6 n6 O3 e# L/ @
module FIFO_MEM_BLK( clk,
9 c: S8 {, |! {$ y                     writeN,
7 R+ p1 H; w0 e- c                     wr_addr,: j4 B$ z' F0 e" h4 Q' I- k
                     rd_addr,6 U/ v; Q5 a7 d
                     data_in,/ Y- y( D( k+ ^, D; X9 T7 @: k
                     data_out' R) O: A# ^! k9 {) b; F5 N
                   );
7 {* Z" q; t/ B. w7 o$ N/ R( [; ~& q# W
input                    clk;       // input clk.
1 {$ \7 y! ?7 {, I; Iinput  writeN;  // Write Signal to put data into fifo.
0 d) p2 W. c- y. ^. R8 a  qinput  [(`FCWIDTH-1):0]  wr_addr;   // Write Address." O2 X% z; _' a. C+ T; i
input  [(`FCWIDTH-1):0]  rd_addr;   // Read Address.
3 f% |! i% j6 A) o/ p+ d* ~input  [(`FWIDTH-1):0]   data_in;   // DataIn in to Memory Block
5 E0 S- q2 P' m8 c+ Houtput [(`FWIDTH-1):0]   data_out;  // Data Out from the Memory; Z6 ]# y" f/ R6 G+ c' t; V9 B
                                    // Block(FIFO)5 p: a3 [/ G" R% \
wire   [(`FWIDTH-1):0] data_out;
$ X. d! @2 a% H% Lreg    [(`FWIDTH-1):0] FIFO[0:(`FDEPTH-1)];
2 L2 m$ _, Y6 x- Wassign data_out  = FIFO[rd_addr];
4 O% S# `4 K3 ]1 z. o$ w4 Ialways @(posedge clk)' v9 w7 L+ m; O' F9 b9 U
begin
! o9 o) t* ]5 D3 |& C/ ^+ E, N   if(writeN==1'b0). V0 h) A* j. G$ h( z( f
      FIFO[wr_addr] <= data_in;5 v# U  M; M% @8 ?. t! k; l0 h8 n
end, \8 d1 T9 V* j
endmodule
 楼主| 发表于 2009-11-14 20:47 | 显示全部楼层
如果各位能看得明的话“吱”一声吧!谢谢
回复

使用道具 举报

 楼主| 发表于 2009-11-14 20:52 | 显示全部楼层
module fifo(data_out,fifo_full,fifo_he,fifo_hf,fifo_empty,clk,reset,write,read,data_in); 1 Z7 q2 S, e3 k3 U  I' {
parameter FIFO_WIDTH=8; 7 S1 A+ w( k+ }+ C7 S* W. z0 G
parameter FIFO_DEPTH=8;
6 v, ~& o' d1 R1 Y: l5 Yparameter FIFO_PTR_WDTH=3;
: d4 }$ l6 l4 ~" Y+ A( f   w' c- r: e5 G2 B! D. Y$ d
output    [FIFO_WIDTH-1:0]      data_out;//The output data
2 X1 C0 Y) y, q; d# ]! K8 x4 T+ Aoutput                          fifo_full;//The fifo full flag & D3 {  \% E8 n: g
output                          fifo_he;//The fifo half empty flag
/ a. w/ U! R" V) \. V7 Houtput                          fifo_hf;//The fifo half full flag 5 }( }2 k( K8 ]8 p" K
output                          fifo_empty;//The fifo empty flag
' ^* i8 e8 O9 N4 y3 z* g7 \ 7 l/ N7 P& d$ a) s7 S7 t- k
input                           clk;//The input flag
( l9 Y' `. ]6 V$ l! z- Ainput                           reset;//The fifo reset
/ K# ~* J6 S5 {. _$ W8 dinput                           write;//The syncronous write strobe
/ r6 l& a/ Q( a+ i& xinput                           read;//The syncronous read strobe : F! U4 v. a* z+ l1 A# [" H
input     [FIFO_WIDTH-1:0]      data_in;//The input data : b  U  w. h3 L5 Y

6 y! v! L8 u) M# ]& oreg       [FIFO_WIDTH-1:0]      fifo_ram       [0:FIFO_DEPTH-1]; # k# U) Y* s  ?# e. ?
reg       [FIFO_PTR_WDTH-1:0]   wr_ptr,rd_ptr; - [' ~7 X, l" {6 o
reg       [FIFO_PTR_WDTH-1:0]   fifo_count;
: f, q5 O9 `# o* ^* lwire                            fifo_full,fifo_enpty; * L# Z4 y; R$ [
wire                            fifo_he,fifo_hf; 0 S4 A8 k$ W$ S& m/ Q* _/ [0 j
reg       [FIFO_WIDTH-1:0]      data_out; 6 [4 |: U% O5 P5 w! G+ J# w5 x

+ q* W7 s4 x; D& l" G/***************************************************************************
! [$ u. z3 K1 K9 O$ gIf this is a read get the data that is in the location pointed to by the
; @' x& k( V$ C# e tead pointer,and put it onto the output bus
6 K' \' {5 j( [****************************************************************************/ 6 N2 a$ L8 N0 A; N5 {% L2 [
always@(posedge clk)
1 t& A* A8 s: g' \   if(read)
! K5 P2 D. J! S; i      data_out<=fifo_ram[rd_ptr];
/ a) g+ j8 P8 w3 i% k   else if(write)
/ Y4 _7 m# a- w, {) i% u      fifo_ram[wr_ptr]=data_in;
. D4 \, v$ M4 O( A+ x) Y7 E
, w, u. b/ y) q% C$ h/ ]/**************************************************************************** , N9 r" S4 j0 b/ V: M
Increment the write pointer on every write and the read pointer on every read 8 q/ U6 Q9 w6 _2 _9 k/ a/ R- L$ B) C8 N
*****************************************************************************/
( g" G2 x! C# e+ r1 A         ; P, `( r6 n+ f
always@(posedge clk)
3 C* k2 W' t# P, a0 L* O   if(reset) ' a; i0 y  V; s) v% x) M8 m4 b0 i
      wr_ptr<=0; 0 ?) L$ P9 K7 o' q+ _+ t# x
   else
$ X- J5 Z: H1 F. Z" L- ~      wr_ptr<=(write)?wr_ptr+1:wr_ptr; 1 O/ p4 `8 z. I$ V: g
    ' {7 q# p6 X( l' o: D& n& D
+ v0 f. x2 e4 O9 ?% M% t
always@(posedge clk) # N8 x, ^! \1 c- J; Y! j" m
   if(reset) " M( ]0 W* c8 D) b
      rd_ptr<=0; 7 S: J5 s0 G7 b
   else
2 z" D8 @. U( t5 b. M! L9 T& i3 D% m      rd_ptr<=(read)?rd_ptr+1:rd_ptr; ; P$ j+ E" m: v: M# u
       * n4 o  b0 X% h+ \
/***************************************************************************** ( v% v7 t" w2 u' T5 h2 K$ X9 B. D
The fifo counter increment on every write and decerment on every read .those  
" T# \5 K$ E& d8 q. ?: B& qcode is used to provide flags to the other module,the other module check  7 O5 ~% f& }+ j, V
those flags,then know the state of fifo and decide whether write or read fifo
0 X. W6 h! ?; N2 T, F*****************************************************************************/
1 D8 W; Q. o0 z8 I9 _ 8 v1 w& i# d" y1 \/ g7 \5 Y! R
always@(posedge clk)
  D5 X% P9 M$ ubegin
% d, T1 I8 r( {7 x$ x/ v. c3 [3 ^     if(reset)
8 ]  }4 c$ T$ P& d4 M9 ^7 w7 e" e           begin & k9 R5 P' @. q: ]1 @6 K
               fifo_count<=0; 5 G# w+ d) N% k. H/ d" Y4 P: L( g
           end ) B! ~; M7 E. y3 _7 d4 O* w
      else begin
2 Y7 V8 h- O& }! `0 }3 T$ K            case({write,read})  
( n/ V" B; }1 |; o  `  V& Y* w            2'b00:fifo_count<=fifo_count;  5 _: d9 K. N2 Y! l
            2'b01:fifo_count<=(fifo_count==0)?FIFO_DEPTH:fifo_count-1;
8 q2 K5 B! X/ y; i: E9 z: |5 T            2'b10:fifo_count<=(fifo_count==FIFO_DEPTH)?0:fifo_count+1; - w( b' \1 ?) }( Q& L3 V
            2'b11:fifo_count<=fifo_count;
; d8 e9 c0 `+ L$ E# E, I6 h            endcase 8 a9 d' o( ]4 z) p* C6 `: p$ M$ {
            end   J  m: w  K3 y4 P2 O; @
end
( O! M% C' t2 i/ n$ c / ^6 h( f4 y! e3 B0 S' B- R
assign fifo_hf=(fifo_count>=4);
: ]5 q$ a" N2 I0 k/ r1 Qassign fifo_he=(fifo_count<=4);
9 T5 I; n$ M2 o# \assign fifo_empty=(fifo_count==0);
7 Z  i. z& X$ dassign fifo_full=(fifo_count>=FIFO_DEPTH);
6 ^; @/ a" d9 W- g/ J : ]3 l% B/ P& N6 H" o! ~2 b
endmodule
回复

使用道具 举报

本版积分规则

QQ|一淘宝店|手机版|商店|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2025-10-28 06:22 , Processed in 0.033337 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表