版主
主题
回帖0
积分10609
阅读权限200
注册时间2008-11-22
最后登录1970-1-1
在线时间 小时
|
////////////////////////////////////////////////////////////////////2 n7 h V: Z9 [+ Q
// FileName: "Fifo.v"
3 F7 C' h3 Z4 \// Author : Venkata Ramana Kalapatapu4 t2 g f- H2 i2 l% @
// Company : Sand Microelectronics Inc.
' Z Z H* N) a4 p; R4 b// (now a part of Synopsys, Inc.),
* |5 I' s: x) q2 _! R// Profile : Sand develops Simulation Models, Synthesizable Cores and* y3 {, r: D7 Q; O& U4 \' t3 D6 j0 a
// Performance Analysis Tools for Processors, buses and8 Y! q. L% I: m- Y3 X% _
// memory products. Sand's products include models for b% N( L0 S) q) D# [3 \
// industry-standard components and custom-developed models
2 z# W' }0 [' H' k3 l# @8 T// for specific simulation environments.
+ N0 U' H. ?& K3 `7 H9 j$ t//9 D$ k7 L2 w$ Q) m: y n. E3 {7 z
////////////////////////////////////////////////////////////////////
- K0 T& ~: v" Z4 B) x& p( _- _: W9 s
% d; y; S' C, G" J`define FWIDTH 32 // Width of the FIFO.
1 i2 N, f4 ?' m: D4 e# a7 r- y- b% {' k1 i E`define FDEPTH 4 // Depth of the FIFO.) C/ v# Z, J6 Z1 z$ I
`define FCWIDTH 2 // Counter Width of the FIFO 2 to power
$ b) U9 o! R- Y' W$ A2 r" o // FCWIDTH = FDEPTH.& c& N; V, i9 @# H% A, L
module fifo( Clk,
. I$ d+ n# B; ^! r6 |7 _, O# \5 O RstN,' D2 }3 v- _9 M* s$ O
Data_In,
( Y( u' V$ V6 _. p FClrN, d$ ~9 h+ D& [ S' O N z( P/ N
FInN,
0 G/ e+ J9 @5 o5 t FOutN,
3 ?, e% v3 @( }: u( I* @ F_Data,
3 {- T [) v/ a0 s+ K' \9 T, U F_FullN,$ ^! c" C1 ?1 R( D6 m$ }- h
F_LastN,6 D* G8 A" I9 @5 r
F_SLastN,) Y# \- X) ?6 m! R; V1 q; R2 ]1 q
F_FirstN,) b8 g" J8 S' I% @# j
F_EmptyN5 D1 l- ^. H* G2 v
);
/ x2 I9 n8 V4 d) Z3 f" D+ J7 b5 u) n1 ]' n
input Clk; // CLK signal.0 U2 i$ g, s) H
input RstN; // Low Asserted Reset signal. c w7 [2 ?5 X1 t
input [(`FWIDTH-1):0] Data_In; // Data into FIFO.
( ]# e+ t1 I4 C' S! Ainput FInN; // Write into FIFO Signal.
8 t$ J! |6 ]6 t. p: e1 M% rinput FClrN; // Clear signal to FIFO.
6 }9 O9 _$ T7 Vinput FOutN; // Read from FIFO signal.
: c1 n6 V4 I1 }' C4 R- m2 Ioutput [(`FWIDTH-1):0] F_Data; // FIFO data out.
! k5 g8 S& s1 G0 s+ moutput F_FullN; // FIFO full indicating signal.
' ^8 A2 j7 z5 L1 xoutput F_EmptyN; // FIFO empty indicating signal.
$ z) M, i8 m7 d- Routput F_LastN; // FIFO Last but one signal.
4 w# a& t+ u, e8 qoutput F_SLastN; // FIFO SLast but one signal.
" n8 U0 o. ]' L8 u. _output F_FirstN; // Signal indicating only one
8 S7 _6 A4 T7 Z* C; E5 G0 f" X* a // word in FIFO.4 M+ H$ }" B! n+ t7 N5 F. d3 C% t
/ Z3 K- d7 |; areg F_FullN;& H( V- _8 K9 l5 |' K! d+ F( a! s
reg F_EmptyN;
5 T1 W* h3 F9 Hreg F_LastN;
- J2 y7 ^9 t3 areg F_SLastN;6 x' h2 I" T- e& r7 Q
reg F_FirstN;
) G6 Z2 U' S3 D8 F' C6 B. P7 t# Oreg [`FCWIDTH:0] fcounter; //counter indicates num of data in FIFO$ e3 }7 @! z$ g& `
reg [(`FCWIDTH-1):0] rd_ptr; // Current read pointer.: r: _4 x; `5 e9 S6 Z
reg [(`FCWIDTH-1):0] wr_ptr; // Current write pointer.+ [9 V6 `" t* X- h! D
wire [(`FWIDTH-1):0] FIFODataOut; // Data out from FIFO MemBlk
3 d2 V2 R9 K' U) bwire [(`FWIDTH-1):0] FIFODataIn; // Data into FIFO MemBlk
' y* r' ?' B, K' z- Y }wire ReadN = FOutN;# ~+ {( k- d. P( @3 ~
wire WriteN = FInN;
" |# N* Q( |" p# {! @. ]2 Y' Aassign F_Data = FIFODataOut;
2 z4 ^9 r6 q& massign FIFODataIn = Data_In;
8 P9 H; E/ {9 d( s$ t. {% M
% D1 B# J; G6 Q* [; U \$ o FIFO_MEM_BLK memblk(.clk(Clk),
, g% d2 G% Q: H .writeN(WriteN),
0 A4 o& \* |# L+ H4 K, i .rd_addr(rd_ptr),
0 l" s8 _4 n3 Q% r% D' m .wr_addr(wr_ptr),* p, S3 N7 E. D) c3 m- r
.data_in(FIFODataIn),0 k5 Q1 ]0 p4 W6 V) s
.data_out(FIFODataOut) ^( u% ^( k, G3 y6 y
);3 U! u" Y V5 n6 y7 P8 K
// Control circuitry for FIFO. If reset or clr signal is asserted,
7 W5 x- M" T' ?# s1 n) Y5 y2 ? // all the counters are set to 0. If write only the write counter7 N% W A; e9 R6 S+ E0 _
// is incremented else if read only read counter is incremented- \9 Y L+ ~& ~" o, g0 s
// else if both, read and write counters are incremented.' X% {6 H. L' u0 y
// fcounter indicates the num of items in the FIFO. Write only& e, _8 \5 P% e- h2 z$ u) H2 r: y; f
// increments the fcounter, read only decrements the counter, and
1 {2 Q8 B( x( q* X7 D // read && write doesn't change the counter value." e* n" k- f( ]! ]7 k
always @(posedge Clk or negedge RstN)0 T: E3 U% o! y" T2 `6 G
begin. m: s! f9 J, l. U
if(!RstN) begin$ E9 \! Q- n% `" A2 \* r
fcounter <= 0;
5 W0 F( a( O3 ?1 U. }5 p" V7 o rd_ptr <= 0;
) i- @* @. j+ c7 L2 z" E' ^9 O wr_ptr <= 0;
; u" b/ ]" u+ ~) |* O/ w; j& r% w end* G* r1 {' L8 p* ]* C. Y% S9 W
else begin" |+ q$ k5 N% e4 |- y* V+ _3 w! N
if(!FClrN ) begin% ~: ^! ~$ d6 i
fcounter <= 0;
6 K; C& b" G1 G( f7 y" H rd_ptr <= 0;
1 [4 |: i% s# e1 m, B. n wr_ptr <= 0;% M5 F1 I: {3 C- k- P" T8 e
end" H6 ]1 i# q- ^ H4 F7 ^# t2 ?
else begin
( c& i4 X* h0 L6 }5 T if(!WriteN && F_FullN) p9 A8 A: m, N) z# }* o" J8 f) q
wr_ptr <= wr_ptr + 1;/ k" Y1 `% ^3 K5 }. ~2 B
if(!ReadN && F_EmptyN)( f. ^) {3 @- }2 r8 [! P
rd_ptr <= rd_ptr + 1;1 _! e( \( Q4 k4 s. H# M- S
if(!WriteN && ReadN && F_FullN). g; u* ~6 Q$ |# K4 ^
fcounter <= fcounter + 1;
9 a$ q5 O. @- t else if(WriteN && !ReadN && F_EmptyN)
5 l/ W; u% H; X5 J$ _; }1 v) v fcounter <= fcounter - 1;
4 L. f9 e' K7 f5 f' {% A! L# Z, x end$ I4 w1 x% e2 d& S
end
3 [4 _+ L" q4 D3 v end
2 h1 J! w- v, i1 ^) H // All the FIFO status signals depends on the value of fcounter.
6 i; \' R$ r ]1 i% z3 Y // If the fcounter is equal to fdepth, indicates FIFO is full.: {9 Q' D$ y4 y
// If the fcounter is equal to zero, indicates the FIFO is empty.
, _' N/ F; c3 z: Y // F_EmptyN signal indicates FIFO Empty Status. By default it is
" z" o9 D% p. a+ P4 r" U8 t // asserted, indicating the FIFO is empty. After the First Data is& h( M/ V' h) | g7 X
// put into the FIFO the signal is deasserted.
. @: i0 @3 j% q8 U always @(posedge Clk or negedge RstN)
9 i2 ~) u5 E) r5 q/ Y begin
8 @: r4 j/ l. o if(!RstN)
# u- S5 h# |6 S7 y F_EmptyN <= 1'b0;
% n/ C' U! `9 |) f/ k2 ^ else begin* g8 Q+ T# |! Q' v9 D$ G" u
if(FClrN==1'b1) begin: }% \. Q- x% i$ s q
if(F_EmptyN==1'b0 && WriteN==1'b0)6 P; U; ?5 h0 I- R
F_EmptyN <= 1'b1;/ ~ T5 ]1 P$ }) v( S! K
else if(F_FirstN==1'b0 && ReadN==1'b0 && WriteN==1'b1)% A7 }8 ?% ^0 i7 b$ R) Q
F_EmptyN <= 1'b0;
& ~1 e, K. L ?8 f0 ^: } end2 K' b8 K% o4 \" I4 ^& i
else
+ ?8 \# f1 Z% m8 ] F_EmptyN <= 1'b0;
, n J* b/ A4 G" N$ R end
' e- J2 _' Z! ^2 }) ?" [3 Y end
, W% s: L4 M6 g: _ // F_FirstN signal indicates that there is only one datum sitting; o- k" H( J$ Q( \, g
// in the FIFO. When the FIFO is empty and a write to FIFO occurs,& z. I2 r* Z( T: H0 ~% e
// this signal gets asserted.
) a, V% W7 o6 F4 E always @(posedge Clk or negedge RstN)& f" b- w+ @( R) i/ d# w
begin" [9 V6 H2 [( M7 K
if(!RstN)
% }" ?$ ^, r2 ?* F, x F_FirstN <= 1'b1;6 [" `5 r# Z. i% r
else begin% f! |- R' u; j+ }
if(FClrN==1'b1) begin$ |" E# |4 \! E5 Y% v% d: b
if((F_EmptyN==1'b0 && WriteN==1'b0) ||
- B5 \8 c6 w4 H. q* W2 o (fcounter==2 && ReadN==1'b0 && WriteN==1'b1))( {9 r1 M; y R* @" B
F_FirstN <= 1'b0;* V# C/ r7 X( @: q, p% \4 L# S
else if (F_FirstN==1'b0 && (WriteN ^ ReadN))- e' ^( G" p* w. f) N
F_FirstN <= 1'b1;2 V/ Y5 v6 w4 t9 R3 l3 n7 X8 t8 Q% @
end5 n, |, t5 J3 H' X5 Z: T8 ]) d
else begin
# O8 X) N, @# O& h- f F_FirstN <= 1'b1;# n9 q+ Y* c2 J5 r* N G3 C2 N' H
end
. @7 J( h+ {2 K/ O* b5 h8 B end
0 _3 ?, ]: c$ } Y. }* B/ k end
; j% t, y7 K3 u3 l5 g
" X8 T; ?" Y# l( c$ s // F_SLastN indicates that there is space for only two data words
2 F* ]: f/ q( d; _ n //in the FIFO.
) B! n U3 Y n! @! T8 d* x always @(posedge Clk or negedge RstN)
( ?7 W" c! g% a) T begin( N- W4 _# i) Z" A8 }
if(!RstN)- H1 _4 g' F; D/ Y1 ]) I4 U# h( D0 Z
F_SLastN <= 1'b1;0 Y4 x1 _& d9 @2 o7 j
else begin4 h' a! s" r& o B) L
if(FClrN==1'b1) begin! F6 A9 j$ y0 S2 U) T1 D
if( (F_LastN==1'b0 && ReadN==1'b0 && WriteN==1'b1) ||
- }8 w7 ?2 X. v (fcounter == (`FDEPTH-3) && WriteN==1'b0 && ReadN==1'b1))
7 T# r2 n& m7 ?0 ~1 e! M F_SLastN <= 1'b0;1 ?8 Y9 \# ~) C$ N! z! o
, o8 q$ r% o* N0 |+ L" r
else if(F_SLastN==1'b0 && (ReadN ^ WriteN) )3 P% `: j2 A+ v5 `
F_SLastN <= 1'b1;' K+ \; V8 M# J X \6 t
end
" U' ~0 V! c: e% U) A8 @. i# a$ S G else
' Q4 K( ^7 Z6 x# X F_SLastN <= 1'b1;
) V: {& D' c4 d; Z& f( T end
/ j3 l+ R+ g; T4 w# u' Z end. r( r$ G9 ?1 \' l( }& I
// F_LastN indicates that there is one space for only one data$ N3 s" s; \% E \- Y, p
// word in the FIFO.4 X( C9 V8 T0 X4 i% {/ C
always @(posedge Clk or negedge RstN)
8 \! a* `; U/ x& L, [3 r begin- r; Z/ {! k- e$ p
if(!RstN)
' r6 r7 i5 _% O8 U3 L: ] F_LastN <= 1'b1;) t5 f. p) s7 n; y
else begin
" t6 J9 ~- ~* x$ [1 v! Y I/ ~" z; [, o if(FClrN==1'b1) begin
1 O; Z- Z5 d, [ if ((F_FullN==1'b0 && ReadN==1'b0) ||
5 o" P/ V9 h3 M4 w (fcounter == (`FDEPTH-2) && WriteN==1'b0 && ReadN==1'b1))
2 Y# d% q2 L" k) }2 }0 J! P F_LastN <= 1'b0;
) S! _. k+ n2 X* ^* C& K else if(F_LastN==1'b0 && (ReadN ^ WriteN) )+ v" j1 F) n) y- z0 s/ k
F_LastN <= 1'b1;
4 T' }- f q1 R6 w end: {, Z" P" A7 a$ z/ y# x
else
0 |1 [1 ^3 S* u$ `" L% U$ {, T F_LastN <= 1'b1;7 ~& }' O- R8 J$ N
end; A* k: i) k1 q2 ?( N X; q
end# R8 d" n) Y* z/ k! C: E" z( Q, S
( U) g4 h# L; c' G* k/ B6 q
// F_FullN indicates that the FIFO is full.$ y$ Y: O3 N; j$ e: f/ ?% [& P. H
always @(posedge Clk or negedge RstN)
( N# k% D; g& Z/ B begin4 x1 q( W# E5 _' n+ ~8 n2 t
if(!RstN)
7 L) `9 I1 {& y4 S3 B6 E4 G( B) l F_FullN <= 1'b1;
9 t' V6 S; c2 v, k7 [- S& r else begin
' W, @( e6 K' o4 k8 ^ if(FClrN==1'b1) begin! D6 D8 O! h" m5 z" c
if (F_LastN==1'b0 && WriteN==1'b0 && ReadN==1'b1)
) j' `& }2 D, m F_FullN <= 1'b0;
1 L& X3 ^$ D- f* g else if(F_FullN==1'b0 && ReadN==1'b0)
) }( A7 M+ X$ G# U& _ ? F_FullN <= 1'b1;, i0 N. }% \2 d
end; |5 l! x/ \! Y9 q( A
else3 H7 o' ]: a- _; _) Q
F_FullN <= 1'b1;
5 y' h6 G/ R" c8 h8 Q9 V# i7 A end
6 A( k! v6 B# R/ W5 P; b7 @ end
3 \7 H- _! j0 R0 J5 Z' Aendmodule- A# r- P9 F7 U4 d" h; U
7 _& |9 N3 t! z. p4 Z
: W! D. j" B" e3 w& W* f/ a///////////////////////////////////////////////////////////////////! J' o7 a3 E0 L- K( O
//1 n6 T2 S5 m' L$ X, M
//+ u- c- `7 G5 x) n" J! D
// Configurable memory block for fifo. The width of the mem2 W& S1 n! ^- |% f# y* R; k
// block is configured via FWIDTH. All the data into fifo is done O: P8 H7 K9 A8 s* x7 O/ U9 M
// synchronous to block.6 v' k0 m) x4 \5 @1 _2 D
//# s6 ^: G0 s# d
// Author : Venkata Ramana Kalapatapu
( K% o# `! m$ l3 j& a: {0 m//
; x4 u6 K1 @9 T, ^2 w///////////////////////////////////////////////////////////////////3 R h! F) b2 }
module FIFO_MEM_BLK( clk,
1 N: Y. T7 z, w4 Y5 _7 R writeN,) j* B' \) l6 H& D. @+ O8 U* I0 a
wr_addr,
6 I; Z7 K4 i/ g rd_addr,: F+ H. t2 \9 h2 E, o7 ~# _
data_in,+ e3 W7 u' A6 t- a' B- w1 l
data_out
- E2 X! c8 @ \4 `( v1 t3 E );
+ f9 U+ ^1 @) K1 }
; k5 r5 T, M* ginput clk; // input clk. K( ^% l5 ]: w% ~8 Z! n
input writeN; // Write Signal to put data into fifo.
- T# W8 [- ~0 u. j' dinput [(`FCWIDTH-1):0] wr_addr; // Write Address.
9 A6 T6 C6 X! f+ g. f. _8 ginput [(`FCWIDTH-1):0] rd_addr; // Read Address.$ A# n4 y6 Z+ h- O, `, ^
input [(`FWIDTH-1):0] data_in; // DataIn in to Memory Block
% E( O7 k2 j; J5 P% Routput [(`FWIDTH-1):0] data_out; // Data Out from the Memory
4 n; l W# H0 _( _% z7 e: K // Block(FIFO)+ T$ G5 l. v+ B4 ]# `3 K+ j* u
wire [(`FWIDTH-1):0] data_out;
! _& {9 c5 a6 x X k1 xreg [(`FWIDTH-1):0] FIFO[0:(`FDEPTH-1)];
! m2 W/ Z2 G7 n/ Y* g& G$ fassign data_out = FIFO[rd_addr];0 x d0 \; j( C+ V; F1 u
always @(posedge clk)7 u8 K5 o I2 ~' L9 N
begin. `: m+ ~8 C/ H
if(writeN==1'b0)4 h. q& H% A- ~& Q* t0 Q; I
FIFO[wr_addr] <= data_in;
& l* _; z7 F9 f, i2 Yend
) a) p% t& M2 ?/ _8 Pendmodule |
|