x393  1.0
FPGAcodeforElphelNC393camera
bit_stuffer_metadata.v
Go to the documentation of this file.
1 
41 `timescale 1ns/1ps
42 
44  input mclk,
45  input mrst, // @posedge mclk, sync reset
46  input xclk,
47  input xrst, // @posedge xclk, sync reset
48  input last_block, // use it to copy timestamp from fifo
49 
50 
51 // time stamping - will copy time at the end of color_first (later than the first hact after vact in the current frame, but before the next one
52 // and before the data is needed for output
53  input ts_pre_stb, // @mclk - 1 cycle before receiving 8 bytes of timestamp data
54  input [7:0] ts_data, // timestamp data (s0,s1,s2,s3,us0,us1,us2,us3==0)
55  input color_first, // @fradv_clk only used for timestamp
56 
57  input [31:0] din, // input data, MSB aligned
58  input [1:0] bytes_in, // number of bytes, valid @ ds (0 means 4)
59  input in_stb, // input data/bytes_in strobe
60  input flush, // end of input data
61  input abort, // @ any, extracts 0->1 and flushes
62 
63  // outputs @ negedge clk
64  output reg [31:0] data_out, // [31:0] output data
65  output reg data_out_valid,// output data valid
66  output reg done, // reset by !en, goes high after some delay after flushing
67  output reg running // from registering timestamp until done
68 `ifdef DEBUG_RING
69 , output reg [3:0] dbg_etrax_dma
70  ,output dbg_ts_rstb
71  ,output [7:0] dbg_ts_dout
72 
73 `endif
74 );
75 
76  reg [7:0] time_ram0[0:3]; // 0 - seconds, 1 - microseconds MSB in the output 32-bit word, byt LSB of the sec/usec
77  reg [7:0] time_ram1[0:3];
78  reg [7:0] time_ram2[0:3];
79  reg [7:0] time_ram3[0:3];
80  reg [3:0] ts_in=8;
81  reg last_block_d; // last_block delayed by one clock
83  reg [2:0] abort_r;
85 
86 // reg color_first_r; // registered with the same clock as color_first to extract leading edge
87 
88 // stb_time[2] - single-cycle pulse after color_first goes low
89 // reg [19:0] imgsz32; // current image size in multiples of 32-bytes
90  reg [21:0] imgsz4; // current image size in multiples of 4-bytes
91  reg last_stb_4; // last stb_in was 4 bytes
92  reg trailer;
93  reg meta_out;
94  reg [1:0] meta_word;
95  reg zeros_out; // output of 32 bytes (8 words) of zeros
96  wire trailer_done = (imgsz4[2:0] == 7) && zeros_out;
97  wire meta_last = (imgsz4[2:0] == 7) && meta_out;
98  // re-clock enable to this clock
99 
100  wire ts_rstb= last_block && !last_block_d; // enough time to have timestamp data; // one cycle before getting timestamp data from FIFO
101  wire [7:0] ts_dout; // timestamp data, byte at a time
102  wire write_size = (in_stb && (bytes_in != 0)) || (flush && last_stb_4);
104  wire stb = in_stb & !trailer && !force_flush;
105  always @ (posedge xclk) begin
106  if (xrst ||trailer_done) imgsz4 <= 0;
107  else if (stb || trailer) imgsz4 <= imgsz4 + 1;
108 
109  if (stb) last_stb_4 <= (bytes_in == 0);
110 
113 
114  if (xrst) ts_in <= 8;
115  else if (ts_rstb) ts_in <= 0;
116  else if (!ts_in[3]) ts_in <= ts_in + 1;
117 
118  if ((!ts_in[3] && (ts_in[1:0] == 0)) || write_size) time_ram0[ts_in[3:2]] <= ts_in[3]? ({imgsz4[5:0],flush?2'b0:bytes_in}):ts_dout; //ts_in[3:2] == 2'b10 when write_size
119  if ((!ts_in[3] && (ts_in[1:0] == 1)) || write_size) time_ram1[ts_in[3:2]] <= ts_in[3]? (imgsz4[13:6]):ts_dout;
120  if ((!ts_in[3] && (ts_in[1:0] == 2)) || write_size) time_ram2[ts_in[3:2]] <= ts_in[3]? (imgsz4[21:14]):ts_dout;
121  if ((!ts_in[3] && (ts_in[1:0] == 3)) || write_size) time_ram3[ts_in[3:2]] <= ts_in[3]? (8'hff):ts_dout;
122 
123  if (xrst) trailer <= 0;
124  else if (flush || force_flush) trailer <= 1;
125  else if (trailer_done) trailer <= 0;
126 
127  if (xrst) meta_out <= 0;
128  else if (trailer && (imgsz4[2:0] == 4) &&!zeros_out) meta_out <= 1;
129  else if (meta_last) meta_out <= 0;
130 
131  if (!meta_out) meta_word <= 0;
132  else meta_word <= meta_word + 1;
133 
134  if (xrst) zeros_out <= 0;
135  else if (meta_last) zeros_out <= 1;
136  else if (trailer_done) zeros_out <= 0;
137 
139  data_out_valid <= stb || trailer;
140 
141 
142  if (xrst || trailer) running <= 0;
143  else if (stb_start) running <= 1;
144 
145  done <= trailer_done;
146  // re-clock abort, extract leading edge
147  abort_r <= {abort_r[0] & ~abort_r[1], abort_r[0], abort & ~trailer};
148 
149  if (xrst || trailer) force_flush <= 0;
150  else if (abort_r) force_flush <= 1;
151 
152  end
153 
154  // just for testing
155 `ifdef DEBUG_RING
156  assign dbg_ts_rstb = ts_rstb;
157  assign dbg_ts_dout = ts_dout;
158 
159  always @ (posedge xclk) begin
160  dbg_etrax_dma <= imgsz4[3:0];
161  end
162 `endif
163 
164 //color_first && color_first_r
165  timestamp_fifo timestamp_fifo_i (
166  .sclk (mclk), // input
167  .srst (mrst), // input
168  .pre_stb (ts_pre_stb), // input
169  .din (ts_data), // input[7:0]
170  .aclk (xclk), //fradv_clk), // input
171  .arst (xrst), //fradv_clk), // input
172  .advance (stb_start), // triggers at the 0->1
173  .rclk (xclk), // input
174  .rrst (xrst), //fradv_clk), // input
175  .rstb (ts_rstb), // input
176  .dout (ts_dout) // output[7:0] reg
177  );
178 
179 endmodule
180 
timestamp_fifo_i timestamp_fifo
reg [ 7:0] 9614dout