24 output reg tx_data_get = 0,
32 always @(posedge clk) begin
38 reg [7:0] data_shift_reg = 0;
39 reg [7:0] oe_shift_reg = 0;
40 reg [7:0] se0_shift_reg = 0;
43 wire serial_tx_data = data_shift_reg[0];
44 wire serial_tx_oe = oe_shift_reg[0];
45 wire serial_tx_se0 = se0_shift_reg[0];
50 reg [2:0] bit_count = 0;
52 reg [4:0] bit_history_q = 0;
53 wire [5:0] bit_history = {serial_tx_data, bit_history_q};
54 wire bitstuff = bit_history == 6'b111111;
58 reg bitstuff_qqqq = 0;
61 always @(posedge clk) begin
62 bitstuff_q <= bitstuff;
63 bitstuff_qq <= bitstuff_q;
64 bitstuff_qqq <= bitstuff_qq;
65 bitstuff_qqqq <= bitstuff_qqq;
68 assign pkt_end = bit_strobe && se0_shift_reg[1:0] == 2'b01;
72 reg [31:0] pkt_state = 0;
76 localparam DATA_OR_CRC16_0 = 3;
77 localparam CRC16_1 = 4;
82 always @(posedge clk) begin
91 if (byte_strobe) begin
93 data_shift_reg <= 8'b10000000;
94 oe_shift_reg <= 8'b11111111;
95 se0_shift_reg <= 8'b00000000;
100 if (byte_strobe) begin
101 if (pidq[1:0] == 2'b11) begin
102 pkt_state <= DATA_OR_CRC16_0;
107 data_shift_reg <= {~pidq, pidq};
108 oe_shift_reg <= 8'b11111111;
109 se0_shift_reg <= 8'b00000000;
113 DATA_OR_CRC16_0 : begin
114 if (byte_strobe) begin
115 if (tx_data_avail) begin
116 pkt_state <= DATA_OR_CRC16_0;
119 data_shift_reg <= tx_data;
120 oe_shift_reg <= 8'b11111111;
121 se0_shift_reg <= 8'b00000000;
123 pkt_state <= CRC16_1;
126 data_shift_reg <= ~{crc16[8], crc16[9], crc16[10], crc16[11], crc16[12], crc16[13], crc16[14], crc16[15]};
127 oe_shift_reg <= 8'b11111111;
128 se0_shift_reg <= 8'b00000000;
136 if (byte_strobe) begin
138 data_shift_reg <= ~{crc16[0], crc16[1], crc16[2], crc16[3], crc16[4], crc16[5], crc16[6], crc16[7]};
139 oe_shift_reg <= 8'b11111111;
140 se0_shift_reg <= 8'b00000000;
145 if (byte_strobe) begin
147 oe_shift_reg <= 8'b00000111;
148 se0_shift_reg <= 8'b00000111;
153 if (bit_strobe && !bitstuff) begin
154 byte_strobe <= (bit_count == 3'b000);
163 end
else if (bit_strobe) begin
166 bit_history_q <= bit_history[5:1];
167 data_shift_reg[0] <= 0;
171 bit_count <= bit_count + 1;
173 data_shift_reg <= (data_shift_reg >> 1);
174 oe_shift_reg <= (oe_shift_reg >> 1);
175 se0_shift_reg <= (se0_shift_reg >> 1);
177 bit_history_q <= bit_history[5:1];
185 wire crc16_invert = serial_tx_data ^ crc16[15];
187 always @(posedge clk) begin
189 crc16 <= 16'b1111111111111111;
192 if (bit_strobe && data_payload && !bitstuff_qqqq && !pkt_start) begin
193 crc16[15] <= crc16[14] ^ crc16_invert;
194 crc16[14] <= crc16[13];
195 crc16[13] <= crc16[12];
196 crc16[12] <= crc16[11];
197 crc16[11] <= crc16[10];
198 crc16[10] <= crc16[9];
199 crc16[9] <= crc16[8];
200 crc16[8] <= crc16[7];
201 crc16[7] <= crc16[6];
202 crc16[6] <= crc16[5];
203 crc16[5] <= crc16[4];
204 crc16[4] <= crc16[3];
205 crc16[3] <= crc16[2];
206 crc16[2] <= crc16[1] ^ crc16_invert;
207 crc16[1] <= crc16[0];
208 crc16[0] <= crc16_invert;
212 reg [2:0] dp_eop = 0;
216 always @(posedge clk) begin
224 end
else if (bit_strobe) begin
227 if (serial_tx_se0) begin
231 dp_eop <= dp_eop >> 1;
233 end
else if (serial_tx_data) begin
module usb_fs_tx(input clk_48mhz, input reset, input bit_strobe, output reg oe=0, output reg dp=0, output reg dn=0, input pkt_start, output pkt_end, input[4] pid, input tx_data_avail, output reg tx_data_get=0, input[8] tx_data)