|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// fwft_read_ahead_buf.sv |
| 3 | +// published as part of https://github.com/pConst/basic_verilog |
| 4 | +// Konstantin Pavlov, [email protected] |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +// INFO ------------------------------------------------------------------------ |
| 8 | +// Read ahead buffer |
| 9 | +// |
| 10 | + |
| 11 | + |
| 12 | +/* --- INSTANTIATION TEMPLATE BEGIN --- |
| 13 | +
|
| 14 | +read_ahead_buf #( |
| 15 | + .DATA_W( 32 ) |
| 16 | +)( |
| 17 | + .clk( ), |
| 18 | + .anrst( ), |
| 19 | +
|
| 20 | + // input fifo interface |
| 21 | + .fifo_r_req( ), |
| 22 | + .fifo_r_data( ), |
| 23 | + .fifo_empty( ), |
| 24 | +
|
| 25 | + // output fifo interface |
| 26 | + .r_req( ), |
| 27 | + .r_data( ), |
| 28 | + .empty( ) |
| 29 | +); |
| 30 | +
|
| 31 | +--- INSTANTIATION TEMPLATE END ---*/ |
| 32 | + |
| 33 | + |
| 34 | +module read_ahead_buf #( parameter |
| 35 | + DATA_W = 32 |
| 36 | +)( |
| 37 | + input clk, // clock |
| 38 | + input anrst, // inverse reset |
| 39 | + |
| 40 | + // input fifo interface |
| 41 | + output fifo_r_req, |
| 42 | + input [DATA_W-1:0] fifo_r_data, |
| 43 | + input fifo_empty, |
| 44 | + |
| 45 | + // output fifo interface |
| 46 | + input r_req, |
| 47 | + output logic [DATA_W-1:0] r_data, |
| 48 | + output logic empty |
| 49 | +); |
| 50 | + |
| 51 | + // buffer initialization flags |
| 52 | + logic buf_empty = 1'b1; |
| 53 | + |
| 54 | + // buffer fill request |
| 55 | + logic buf_fill_req; |
| 56 | + assign buf_fill_req = ~fifo_empty && |
| 57 | + buf_empty && |
| 58 | + ~buf_fill_req_d1; |
| 59 | + |
| 60 | + // buffer fill and re-fill cycle |
| 61 | + logic buf_fill_req_d1 = 1'b0; |
| 62 | + always_ff @(posedge clk or negedge anrst) begin |
| 63 | + if( ~anrst ) begin |
| 64 | + buf_fill_req_d1 <= 1'b0; |
| 65 | + end else begin |
| 66 | + buf_fill_req_d1 <= buf_fill_req; |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + // filtering read requests |
| 71 | + logic r_req_filt; |
| 72 | + assign r_req_filt = anrst && |
| 73 | + ~fifo_empty && |
| 74 | + ~buf_empty && |
| 75 | + ~buf_fill_req && r_req; |
| 76 | + |
| 77 | + logic r_req_rise; |
| 78 | + logic r_req_fall; |
| 79 | + edge_detect r_req_ed ( |
| 80 | + .clk( clk ), |
| 81 | + .nrst( anrst ), |
| 82 | + .in( r_req_filt ), |
| 83 | + .rising( r_req_rise ), |
| 84 | + .falling( r_req_fall ), |
| 85 | + .both( ) |
| 86 | + ); |
| 87 | + |
| 88 | + assign fifo_r_req = r_req_filt; |
| 89 | + assign empty = anrst && fifo_empty && buf_empty; |
| 90 | + |
| 91 | + // buffer itself |
| 92 | + logic [DATA_W-1:0] r_data_buf = '0; |
| 93 | + always_ff @(posedge clk or negedge anrst) begin |
| 94 | + if( ~anrst ) begin |
| 95 | + buf_empty = 1'b1; |
| 96 | + end else begin |
| 97 | + |
| 98 | + if( buf_fill_req_d1 ) begin |
| 99 | + r_data_buf[DATA_W-1:0] <= fifo_r_data[DATA_W-1:0]; |
| 100 | + buf_empty = 1'b0; |
| 101 | + end else if( ~r_req_filt && r_req_fall ) begin |
| 102 | + r_data_buf[DATA_W-1:0] <= fifo_r_data[DATA_W-1:0]; |
| 103 | + end |
| 104 | + |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + soft_latch #( |
| 109 | + .WIDTH( DATA_W ) |
| 110 | + ) r_data_latch ( |
| 111 | + .clk( clk ), |
| 112 | + .anrst( anrst ), |
| 113 | + .latch( r_req_filt ), |
| 114 | + .in( (r_req_rise)?(r_data_buf[DATA_W-1:0]):(fifo_r_data[DATA_W-1:0]) ), |
| 115 | + .out( r_data[DATA_W-1:0] ) |
| 116 | + ); |
| 117 | + |
| 118 | +endmodule |
0 commit comments