1 module packet_builder #(parameter NUM_CHAN = 2)(
5 input [31:0] timestamp_clock,
8 input [15:0]chan_fifodata,
9 input [NUM_CHAN:0]chan_empty,
10 input [9:0]chan_usedw,
11 output reg [3:0]rd_select,
12 output reg chan_rdreq,
15 output reg [15:0]fifodata,
17 input wire [31:0]rssi_0, input wire [31:0]rssi_1, input wire [31:0]rssi_2,
18 input wire [31:0]rssi_3, output wire [7:0] debugbus,
19 input [NUM_CHAN:0] underrun);
26 `define TIMESTAMP 3'd3
29 `define MAXPAYLOAD 504
31 `define PAYLOAD_LEN 8:0
42 reg [NUM_CHAN:0] overrun;
44 reg [8:0] read_length;
45 reg [8:0] payload_len;
46 reg timestamp_complete;
49 wire [31:0] true_rssi;
50 wire [4:0] true_channel;
53 assign debugbus = {chan_empty[0], rd_select[0], have_space,
54 (chan_usedw >= 10'd504), (chan_usedw ==0),
55 ready_to_send, state[1:0]};
57 assign true_rssi = (rd_select[1]) ? ((rd_select[0]) ? rssi_3:rssi_2) :
58 ((rd_select[0]) ? rssi_1:rssi_0);
59 assign true_channel = (check_next == 4'd0 ? 5'h1f : {1'd0, check_next - 4'd1});
60 assign ready_to_send = (chan_usedw >= 10'd504) || (chan_usedw == 0) ||
61 ((rd_select == NUM_CHAN)&&(chan_usedw > 0));
63 always @(posedge rxclk)
71 timestamp_complete <= 0;
78 //check if the channel is full
79 if(~chan_empty[check_next])
83 //transmit if the usb buffer have space
84 //check if we should send
88 overrun[check_next] <= 0;
93 overrun[check_next] <= 1;
95 rd_select <= #1 check_next;
97 check_next <= #1 (check_next == channels ? 4'd0 : check_next + 4'd1);
101 fifodata[`PAYLOAD_LEN] <= #1 9'd504;
102 payload_len <= #1 9'd504;
103 fifodata[`TAG] <= #1 0;
104 fifodata[`MBZ] <= #1 0;
107 state <= #1 `HEADER2;
112 fifodata[`CHAN] <= #1 true_channel;
113 fifodata[`RSSI] <= #1 true_rssi[5:0];
114 fifodata[`BURST] <= #1 0;
115 fifodata[`DROPPED] <= #1 0;
116 fifodata[`UNDERRUN] <= #1 (check_next == 0) ? 1'b0 : underrun[true_channel];
117 fifodata[`OVERRUN] <= #1 (check_next == 0) ? 1'b0 : overrun[true_channel];
118 state <= #1 `TIMESTAMP;
122 fifodata <= #1 (timestamp_complete ? timestamp_clock[31:16] : timestamp_clock[15:0]);
123 timestamp_complete <= #1 ~timestamp_complete;
125 if (~timestamp_complete)
128 state <= #1 (timestamp_complete ? `FORWARD : `TIMESTAMP);
132 read_length <= #1 read_length + 9'd2;
133 fifodata <= #1 (read_length >= payload_len ? 16'hDEAD : chan_fifodata);
135 if (read_length >= `MAXPAYLOAD)
141 else if (read_length == payload_len - 4)
146 //handling error state