代码拉取完成,页面将自动刷新
//------------------------------------------------------------------------------
// lifo.sv
// Konstantin Pavlov, pavlovconst@gmail.com
//------------------------------------------------------------------------------
// INFO ------------------------------------------------------------------------
// Simple single-clock LIFO buffer implementation, also known as "stack"
// Features one write- and one read- port in FWFT mode
// See also "fifo.sv" module for similar FIFO buffer implementation
/* --- INSTANTIATION TEMPLATE BEGIN ---
lifo #(
.DEPTH( 8 ),
.DATA_W( 32 )
) FF1 (
.clk( clk ),
.rst( 1'b0 ),
.w_req( ),
.w_data( ),
.r_req( ),
.r_data( ),
.cnt( ),
.empty( ),
.full( )
);
--- INSTANTIATION TEMPLATE END ---*/
module lifo #( parameter
DEPTH = 4, // max elements count == DEPTH, DEPTH MUST be power of 2
DEPTH_W = $clog2(DEPTH)+1, // elements counter width, extra bit to store
// "fifo full" state, see cnt[] variable comments
DATA_W = 32 // data field width
)(
input clk,
input rst, // non-inverted reset
// input port
input w_req,
input [DATA_W-1:0] w_data,
// output port
input r_req,
output logic [DATA_W-1:0] r_data,
// helper ports
output logic [DATA_W-1:0] cnt = 0,
output logic empty,
output logic full,
output logic fail
);
// lifo data
logic [DEPTH-1:0][DATA_W-1:0] data = 0;
// cnt[] vector always holds lifo elements count
// data[cnt[]] points to the first empty lifo slot
// when lifo is full data[cnt[]] points "outside" of data[]
// please take attention to the case when cnt[]==0 && r_req==1'b1 && w_req==1'b1
// this case makes no read/write to the lifo and should be handled externally
always_ff @(posedge clk) begin
if ( rst ) begin
data <= 0;
cnt <= 0;
end else begin
case ({w_req, r_req})
2'b01 : begin // reading out
if ( cnt[DATA_W-1:0] > 1'b0 ) begin
cnt[DATA_W-1:0] <= cnt[DATA_W-1:0] - 1'b1;
end
end
2'b10 : begin // writing in
if ( ~full ) begin
data[cnt[DATA_W-1:0]] <= w_data;
cnt[DATA_W-1:0] <= cnt[DATA_W-1:0] + 1'b1;
end
end
2'b11 : begin // simultaneously reading and writing
if ( cnt[DATA_W-1:0] > 1'b0 ) begin
data[cnt[DATA_W-1:0]-1] <= w_data;
end
// cnt[DATA_W-1:0] <= cnt[DATA_W-1:0]; // data counter does not change
end
default: ;
endcase
end
end
always_comb begin
empty = ( cnt[DATA_W-1:0] == 0 );
full = ( cnt[DATA_W-1:0] == DEPTH );
if (~empty) begin
r_data[DATA_W-1:0] = data[cnt[DATA_W-1:0]]; // first-word fall-through mode
end else begin
r_data[DATA_W-1:0] = 0;
end
fail = ( empty && r_req ) ||
( full && w_req );
end
endmodule
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。