| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- `timescale 1ns/1ps
- module binary921_frame_tx #(
- parameter integer MAX_PAYLOAD_BYTES = 64,
- parameter integer LATCH_PAYLOAD = 1,
- parameter [7:0] VERSION = 8'h01
- ) (
- input wire clk,
- input wire rst_n,
- input wire start,
- input wire [7:0] profile_id,
- input wire [7:0] frame_type,
- input wire [31:0] frame_seq,
- input wire [31:0] timestamp_tick,
- input wire [7:0] payload_len,
- input wire [(MAX_PAYLOAD_BYTES*8)-1:0] payload,
- output reg busy,
- output reg done,
- output reg tx_valid,
- output reg [7:0] tx_data,
- input wire tx_ready
- );
- localparam [7:0] MAGIC0 = 8'ha5;
- localparam [7:0] MAGIC1 = 8'h5a;
- reg [7:0] byte_index;
- reg [7:0] total_bytes;
- reg [15:0] crc;
- wire [15:0] crc_next;
- reg [15:0] crc_after_accept;
- reg [7:0] next_byte_index;
- reg [7:0] next_send_byte;
- reg [7:0] latched_profile_id;
- reg [7:0] latched_frame_type;
- reg [31:0] latched_frame_seq;
- reg [31:0] latched_timestamp_tick;
- reg [7:0] latched_payload_len;
- reg [(MAX_PAYLOAD_BYTES*8)-1:0] latched_payload;
- binary921_crc16 u_crc (
- .crc_in(crc),
- .data(tx_data),
- .crc_out(crc_next)
- );
- function [7:0] payload_byte;
- input [7:0] index;
- begin
- if (LATCH_PAYLOAD != 0) begin
- payload_byte = latched_payload[(index * 8) +: 8];
- end else begin
- payload_byte = payload[(index * 8) +: 8];
- end
- end
- endfunction
- always @* begin
- crc_after_accept = crc;
- if (byte_index >= 8'd2 && byte_index < (8'd14 + latched_payload_len)) begin
- crc_after_accept = crc_next;
- end
- next_byte_index = byte_index + 8'd1;
- case (next_byte_index)
- 8'd0: next_send_byte = MAGIC0;
- 8'd1: next_send_byte = MAGIC1;
- 8'd2: next_send_byte = VERSION;
- 8'd3: next_send_byte = latched_profile_id;
- 8'd4: next_send_byte = latched_frame_type;
- 8'd5: next_send_byte = latched_frame_seq[7:0];
- 8'd6: next_send_byte = latched_frame_seq[15:8];
- 8'd7: next_send_byte = latched_frame_seq[23:16];
- 8'd8: next_send_byte = latched_frame_seq[31:24];
- 8'd9: next_send_byte = latched_timestamp_tick[7:0];
- 8'd10: next_send_byte = latched_timestamp_tick[15:8];
- 8'd11: next_send_byte = latched_timestamp_tick[23:16];
- 8'd12: next_send_byte = latched_timestamp_tick[31:24];
- 8'd13: next_send_byte = latched_payload_len;
- default: begin
- if (next_byte_index < (8'd14 + latched_payload_len)) begin
- next_send_byte = payload_byte(next_byte_index - 8'd14);
- end else if (next_byte_index == (8'd14 + latched_payload_len)) begin
- next_send_byte = crc_after_accept[7:0];
- end else begin
- next_send_byte = crc_after_accept[15:8];
- end
- end
- endcase
- end
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- busy <= 1'b0;
- done <= 1'b0;
- tx_valid <= 1'b0;
- tx_data <= 8'd0;
- byte_index <= 8'd0;
- total_bytes <= 8'd0;
- crc <= 16'hffff;
- latched_profile_id <= 8'd0;
- latched_frame_type <= 8'd0;
- latched_frame_seq <= 32'd0;
- latched_timestamp_tick <= 32'd0;
- latched_payload_len <= 8'd0;
- latched_payload <= {(MAX_PAYLOAD_BYTES*8){1'b0}};
- end else begin
- done <= 1'b0;
- if (start && !busy) begin
- busy <= 1'b1;
- byte_index <= 8'd0;
- total_bytes <= 8'd16 + payload_len;
- crc <= 16'hffff;
- tx_valid <= 1'b1;
- tx_data <= MAGIC0;
- latched_profile_id <= profile_id;
- latched_frame_type <= frame_type;
- latched_frame_seq <= frame_seq;
- latched_timestamp_tick <= timestamp_tick;
- latched_payload_len <= payload_len;
- latched_payload <= payload;
- end else if (busy && tx_valid && tx_ready) begin
- if (byte_index >= 8'd2 && byte_index < (8'd14 + latched_payload_len)) begin
- crc <= crc_next;
- end
- if (byte_index == total_bytes - 8'd1) begin
- busy <= 1'b0;
- done <= 1'b1;
- tx_valid <= 1'b0;
- byte_index <= 8'd0;
- end else begin
- byte_index <= byte_index + 8'd1;
- tx_valid <= 1'b1;
- tx_data <= next_send_byte;
- end
- end else if (busy) begin
- tx_valid <= 1'b1;
- end else begin
- tx_valid <= 1'b0;
- end
- end
- end
- endmodule
|