Dan Strother is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | //
// Copyright (c) 2011, Daniel Strother < http://danstrother.com/ >
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// - The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Module Description:
// An asynchronous FIFO with ready/valid handshaking for use on interfaces that
// are row/block-oriented (e.g. image processing). Can input a single row at a
// time, and split them into multiple parallel output ROWS.
module dlsc_rowbuffer_splitter #(
parameter ROW_WIDTH = 20, // width of each input row (ROW_WIDTH >= 16)
parameter BUF_DEPTH = ROW_WIDTH, // depth of buffer (BUF_DEPTH >= ROW_WIDTH)
parameter ROWS = 3, // number of rows to output in parallel
parameter DATA = 16, // width of each piece of data
// derived; don't touch
parameter DATA_R = (DATA*ROWS)
) (
// ** input **
// system
input wire in_clk,
input wire in_rst,
// handshake
output reg in_ready,
input wire in_valid,
// data
input wire [DATA -1:0] in_data,
// ** output **
// system
input wire out_clk,
input wire out_rst,
// handshake
input wire out_ready,
output wire out_valid,
// data
output wire [DATA_R-1:0] out_data
);
`include "dlsc_clog2.vh"
localparam ADDR = `dlsc_clog2(BUF_DEPTH);
// ** input **
// output status synchronized to input domain
wire [ADDR-1:0] in_o_addr;
wire in_o_phase;
// input status
reg [ADDR-1:0] in_addr;
reg [ADDR-1:0] in_addr_next;
reg in_addr_next_last; // in_addr_next == (BUF_DEPTH-1)
reg in_phase;
reg in_phase_next;
reg [ADDR-1:0] in_addr_base;
reg in_addr_base_last;
reg in_phase_base;
reg [ADDR-1:0] in_cnt;
reg in_cnt_last; // in_cnt == (ROW_WIDTH-1)
reg in_cnt_next_last; // in_cnt == (ROW_WIDTH-2)
reg [ROWS :0] in_row; // 1 more than needed, for ROWS=1 case
wire in_row_last = in_row[ROWS-1];
wire in_en = (in_ready && in_valid);
/* verilator lint_off WIDTH */
always @(posedge in_clk) begin
if(in_rst) begin
in_addr <= 0;
in_addr_next <= 1;
in_addr_next_last <= 1'b0;
in_phase <= 1'b0;
in_phase_next <= 1'b0;
in_addr_base <= 0;
in_addr_base_last <= 1'b0;
in_phase_base <= 1'b0;
in_cnt <= 0;
in_cnt_last <= 1'b0;
in_cnt_next_last <= 1'b0;
in_row <= 1;
end else if(in_en) begin
// increment count
in_cnt_next_last <= (in_cnt == (ROW_WIDTH-3));
in_cnt_last <= in_cnt_next_last;
if(in_cnt_last) begin
in_cnt <= 0;
end else begin
in_cnt <= in_cnt + 1;
end
// shift row
if(in_cnt_last) begin
in_row <= { in_row[ROWS-1:0], in_row_last };
end
// increment address
in_addr <= in_addr_next;
in_phase <= in_phase_next;
// increment next-address
in_addr_next_last <= (in_addr_next == (BUF_DEPTH-2));
if(in_addr_next_last) begin
in_addr_next <= 0;
in_phase_next <= !in_phase_next;
end else begin
in_addr_next <= in_addr_next + 1;
end
// set next-address for upcoming count overflow
// (overrides increment operation above)
if(!in_row_last && in_cnt_next_last) begin
// continuing pass; must start row from same address as previous rows
in_addr_next_last <= in_addr_base_last;
in_addr_next <= in_addr_base;
in_phase_next <= in_phase_base;
end
// save base on final count overflow
if(in_row_last && in_cnt_last) begin
// starting new pass; can start row after end of previous row
in_addr_base_last <= in_addr_next_last;
in_addr_base <= in_addr_next;
in_phase_base <= in_phase_next;
end
end
end
/* verilator lint_on WIDTH */
always @(posedge in_clk) begin
if(in_rst) begin
in_ready <= 1'b0;
end else begin
// if we're on the same phase, output is waiting on us, so we can write freely
// if not, we must wait for output to consume data before we overwrite it
if(in_en) begin
in_ready <= (in_phase_next == in_o_phase || in_addr_next != in_o_addr);
end else begin
in_ready <= (in_phase == in_o_phase || in_addr != in_o_addr);
end
end
end
// address/phase to report to output domain..
// as far as the output domain is concerned, we never advance beyond the first
// datum until we're on the final row
reg [ADDR-1:0] in_i_addr;
reg in_i_phase;
always @(posedge in_clk) begin
if(in_rst) begin
in_i_addr <= 0;
in_i_phase <= 0;
end else begin
in_i_addr <= in_row_last ? in_addr : in_addr_base;
in_i_phase <= in_row_last ? in_phase : in_phase_base;
end
end
// ** output **
// input status synchronized to output domain
wire [ADDR-1:0] out_i_addr;
wire out_i_phase;
// output status
reg [ADDR-1:0] out_addr;
reg [ADDR-1:0] out_addr_next;
reg out_addr_next_last; // out_addr_next == (BUF_DEPTH-1)
reg out_phase;
reg out_phase_next;
// handshake to rvh_decoupler
wire d_out_ready;
reg d_out_valid;
wire out_en = (d_out_ready && d_out_valid);
/* verilator lint_off WIDTH */
always @(posedge out_clk) begin
if(out_rst) begin
out_addr <= 0;
out_addr_next <= 1;
out_addr_next_last <= 1'b0;
out_phase <= 1'b0;
out_phase_next <= 1'b0;
end else if(out_en) begin
// increment address
out_addr <= out_addr_next;
out_phase <= out_phase_next;
// increment next-address
out_addr_next_last <= (out_addr_next == (BUF_DEPTH-2));
if(out_addr_next_last) begin
out_addr_next <= 0;
out_phase_next <= !out_phase_next;
end else begin
out_addr_next <= out_addr_next + 1;
end
end
end
/* verilator lint_on WIDTH */
always @(posedge out_clk) begin
if(out_rst) begin
d_out_valid <= 1'b0;
end else begin
// if we're on a different phase, input is waiting for us to consume data, so we're free to read
// if not, we must wait for input to write data
d_out_valid <= out_en ? ( out_phase_next != out_i_phase || out_addr_next != out_i_addr ) :
( out_phase != out_i_phase || out_addr != out_i_addr );
end
end
// ** synchronizers **
dlsc_domaincross #(
.DATA ( ADDR + 1 ),
.RESET ( {(ADDR+1){1'b0}} )
) dlsc_domaincross_inst_in (
.in_clk ( out_clk ),
.in_rst ( out_rst ),
.in_data ( { out_addr, out_phase } ),
.out_clk ( in_clk ),
.out_rst ( in_rst ),
.out_data ( { in_o_addr, in_o_phase } )
);
dlsc_domaincross #(
.DATA ( ADDR + 1 ),
.RESET ( {(ADDR+1){1'b0}} )
) dlsc_domaincross_inst_out (
.in_clk ( in_clk ),
.in_rst ( in_rst ),
.in_data ( { in_i_addr, in_i_phase } ),
.out_clk ( out_clk ),
.out_rst ( out_rst ),
.out_data ( { out_i_addr, out_i_phase } )
);
// ** buffers **
wire [DATA_R-1:0] d_out_data;
generate
genvar j;
for(j=0;j<ROWS;j=j+1) begin:GEN_BUFFERS
dlsc_ram_dp #(
.DATA ( DATA ),
.ADDR ( ADDR ),
.DEPTH ( BUF_DEPTH ),
.PIPELINE_WR ( 1 ),
.PIPELINE_WR_DATA ( 1 ),
.PIPELINE_RD ( 1 ),
.WARNINGS ( 0 )
) dlsc_ram_dp_inst (
.write_clk ( in_clk ),
.write_en ( in_en && in_row[j] ),
.write_addr ( in_addr ),
.write_data ( in_data ),
.read_clk ( out_clk ),
.read_en ( !d_out_valid || d_out_ready ),
.read_addr ( out_en ? out_addr_next : out_addr ),
.read_data ( d_out_data[ (j*DATA) +: DATA ] )
);
end
endgenerate
// ** output decoupler **
dlsc_rvh_decoupler #(
.WIDTH ( DATA_R )
) dlsc_rvh_decoupler_inst (
.clk ( out_clk ),
.rst ( out_rst ),
.in_en ( 1'b1 ),
.in_ready ( d_out_ready ),
.in_valid ( d_out_valid ),
.in_data ( d_out_data ),
.out_en ( 1'b1 ),
.out_ready ( out_ready ),
.out_valid ( out_valid ),
.out_data ( out_data )
);
`ifdef DLSC_SIMULATION
`include "dlsc_sim_top.vh"
reg in_rst_prev = 0;
always @(posedge in_clk) begin
in_rst_prev <= in_rst;
if(!in_rst && in_rst_prev) begin
if(in_o_addr != 0 || in_o_phase != 0) begin
`dlsc_warn("in_rst deasserted with synchronized out_* values not in reset state (reset pulse may have been too short)");
end
end
end
reg out_rst_prev = 0;
always @(posedge out_clk) begin
out_rst_prev <= out_rst;
if(!out_rst && out_rst_prev) begin
if(out_i_addr != 0 || out_i_phase != 0) begin
`dlsc_warn("out_rst deasserted with synchronized in_* values not in reset state (reset pulse may have been too short)");
end
end
end
`include "dlsc_sim_bot.vh"
`endif
endmodule
|