x393  1.0
FPGAcodeforElphelNC393camera
huff_fifo393.v
Go to the documentation of this file.
1 
39 
40 module huff_fifo393 (
41  input xclk, // pixel clock, posedge
42  input xclk2x, // twice frequency - uses negedge inside
43  input en, // will reset if ==0 (sync to xclk)
44  input [15:0] di, // data in (sync to xclk)
45  input ds, // din valid (sync to xclk)
46  input want_read, // will be and-ed with dav
47  output dav, // FIFO output latch has data (fifo_or_full)
48  output reg [15:0] q
49  ); // output data
50 
51  reg [9:0] wa;
52  reg [9:0] ra_r;
53  wire [15:0] fifo_o;
54  reg ds1; // ds delayed by one xclk to give time to block ram to write data. Not needed likely.
55  reg synci;
56  reg [2:0] synco;
57  reg sync_we; // single xclk2x period pulse for each ds@xclk
58  reg en2x; // en sync to xclk2x;
59 
60  reg [9:0] diff_a;
61 
62  wire [3:0] re;
63  reg [2:0] nempty_r; // output register and RAM registers not empty
64  wire [3:0] nempty; // output register and RAM register and RAM internal are not empty
65  wire many;
66 
67  assign dav = nempty[0];
68  assign nempty = {(|diff_a), nempty_r};
69  assign many = &(nempty); // memory and all the register chain are full
70  assign re = {4{many & want_read}} | {nempty[3] & ~nempty[2], // read memory location
71  nempty[2] & ~nempty[1], // regen
72  nempty[1] & ~nempty[0], // copy to q- register
73  nempty[0] & want_read}; // external read when data is available
74 
75  always @ (posedge xclk) begin // input stage, no overrun detection. TODO: propagate half-full?
76  if (!en) wa <= 0;
77  else if (ds) wa <= wa+1;
78 
79  ds1 <= ds && en;
80  if (!en) synci <= 1'b0;
81  else if (ds1) synci <= ~synci;
82  end
83 
84  always @ (negedge xclk2x) begin
85  en2x <= en;
86  synco <= {synco[1:0],synci};
87  sync_we <= en2x && (synco[1] != synco[2]);
88  end
89 
90  always @ (negedge xclk2x) begin
91  if (!en2x) nempty_r[0] <= 0;
92  else if (re[1] ^ re[0]) nempty_r[0] <=re[1];
93 
94  if (!en2x) nempty_r[1] <= 0;
95  else if (re[2] ^ re[1]) nempty_r[1] <=re[2];
96 
97  if (!en2x) nempty_r[2] <= 0;
98  else if (re[3] ^ re[2]) nempty_r[2] <=re[3];
99 
100  if (!en2x) ra_r <= 0;
101  else if (re[3]) ra_r <= ra_r + 1;
102 
103  if (!en2x) diff_a <= 0;
104  else if ( sync_we && !re[3]) diff_a <= diff_a + 1;
105  else if (!sync_we && re[3]) diff_a <= diff_a - 1;
106 
107  if (!en2x) q <= 0;
108  else if (re[1]) q <= fifo_o;
109 
110  end
111 
113  .REGISTERS (1),
114  .LOG2WIDTH_WR (4),
115  .LOG2WIDTH_RD (4),
116  .DUMMY (0)
117  ) i_fifo (
118  .rclk (xclk2x), // input
119  .raddr (ra_r[9:0]), // input[9:0]
120  .ren (re[3]), // input
121  .regen (re[2]), // input
122  .data_out (fifo_o[15:0]), // output[15:0]
123  .wclk (xclk), // input
124  .waddr (wa[9:0]), // input[9:0]
125  .we (ds), // input
126  .web (4'hf), // input[3:0]
127  .data_in (di[15:0]) // input[15:0]
128  );
129 
130 endmodule
reg [15:0] 2442q
Definition: huff_fifo393.v:48
2444ra_rreg[9:0]
Definition: huff_fifo393.v:52
2451diff_areg[9:0]
Definition: huff_fifo393.v:60
2448syncoreg[2:0]
Definition: huff_fifo393.v:56
[1 << LOG2WIDTH_WR-1:0] 11597data_in
[1 << LOG2WIDTH_RD-1:0] 11592data_out
[13-LOG2WIDTH_RD:0] 11589raddr
i_fifo ram18_var_w_var_r
Definition: huff_fifo393.v:112
2453nempty_rreg[2:0]
Definition: huff_fifo393.v:63
[15:0] 2438di
Definition: huff_fifo393.v:44
2445fifo_owire[15:0]
Definition: huff_fifo393.v:53
2454nemptywire[3:0]
Definition: huff_fifo393.v:64
2443wareg[9:0]
Definition: huff_fifo393.v:51
[13-LOG2WIDTH_WR:0] 11594waddr
2452rewire[3:0]
Definition: huff_fifo393.v:62