2015. 8. 11. 21:45ㆍFPGA/코드
구글링 자료인데.. 출처는 모르겠고..그림 파일로 되어 있어서
타이핑만 그대로 해서 TB 한번 돌려봄 검증 필요함 ....!!!!
File : circular_queue.v
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
`timescale 1ns / 1ps
module circular_queue
(
rstb ,
clk ,
rd ,
wr ,
w_data ,
empty ,
full ,
r_data
);
parameter B = 8 ; // number of bits in a word
parameter W = 5 ; // number of address bits
input rstb ;
input clk ;
input rd ;
input wr ;
input [B-1:0] w_data ;
output empty ;
output full ;
output [B-1:0] r_data ;
//------------------------------------------------------------------------------
// signal declaration
//------------------------------------------------------------------------------
reg [B-1:0] array_reg[2**W-1:0] ; // register array
reg [W-1:0] w_ptr_reg ;
reg [W-1:0] w_ptr_next ;
reg [W-1:0] w_ptr_succ ;
reg [W-1:0] r_ptr_reg ;
reg [W-1:0] r_ptr_next ;
reg [W-1:0] r_ptr_succ ;
reg full_reg ;
reg full_next ;
reg empty_reg ;
reg empty_next ;
//------------------------------------------------------------------------------
// register file write operation
//------------------------------------------------------------------------------
wire wr_en ;
always @ ( posedge clk )
if ( wr_en )
array_reg [ w_ptr_reg ] = w_data ;
// register file read operation
assign r_data = array_reg [ r_ptr_reg ] ;
// write enabled only when FIFO is not full
assign wr_en = wr & ~full_reg ;
//------------------------------------------------------------------------------
// fifo control logic
// register for read and write pointers
//------------------------------------------------------------------------------
always @ ( negedge rstb or posedge clk ) begin
if ( !rstb ) begin
w_ptr_reg <= {{ W } { 1'b0 }} ;
r_ptr_reg <= {{ W } { 1'b0 }} ;
full_reg <= 1'b0 ;
empty_reg <= 1'b1 ;
end else begin
w_ptr_reg <= w_ptr_next ;
r_ptr_reg <= r_ptr_next ;
full_reg <= full_next ;
empty_reg <= empty_next ;
end
end
//------------------------------------------------------------------------------
// next state logic for read and write pointers
//------------------------------------------------------------------------------
always @ ( * ) begin
// successive pointer values
w_ptr_succ = w_ptr_reg + 1'b1 ;
r_ptr_succ = r_ptr_reg + 1'b1 ;
// default : keep old values
w_ptr_next = w_ptr_reg ;
r_ptr_next = r_ptr_reg ;
full_next = full_reg ;
empty_next = empty_reg ;
case ( { wr, rd } )
2'b01 : begin // read
if ( ~empty_reg ) begin // not empty
r_ptr_next = r_ptr_succ ;
full_next = 1'b0 ;
if ( r_ptr_succ == w_ptr_reg )
empty_next = 1'b1 ;
end
end
2'b10 : begin // write
if ( ~full_reg ) begin // not full
w_ptr_next = w_ptr_succ ;
empty_next = 1'b0 ;
if ( w_ptr_succ == r_ptr_reg )
full_next = 1'b1 ;
end
end
2'b11 : begin // write and read
w_ptr_next = w_ptr_succ ;
w_ptr_next = r_ptr_succ ;
end
2'b00 : begin
end
endcase
end
//------------------------------------------------------------------------------
// output
//------------------------------------------------------------------------------
assign full = full_reg ;
assign empty = empty_reg ;
endmodule
'FPGA > 코드' 카테고리의 다른 글
Verilog_Memory Initialization (0) | 2015.08.11 |
---|