binary921_frame_tx.v 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. `timescale 1ns/1ps
  2. module binary921_frame_tx #(
  3. parameter integer MAX_PAYLOAD_BYTES = 64,
  4. parameter integer LATCH_PAYLOAD = 1,
  5. parameter [7:0] VERSION = 8'h01
  6. ) (
  7. input wire clk,
  8. input wire rst_n,
  9. input wire start,
  10. input wire [7:0] profile_id,
  11. input wire [7:0] frame_type,
  12. input wire [31:0] frame_seq,
  13. input wire [31:0] timestamp_tick,
  14. input wire [7:0] payload_len,
  15. input wire [(MAX_PAYLOAD_BYTES*8)-1:0] payload,
  16. output reg busy,
  17. output reg done,
  18. output reg tx_valid,
  19. output reg [7:0] tx_data,
  20. input wire tx_ready
  21. );
  22. localparam [7:0] MAGIC0 = 8'ha5;
  23. localparam [7:0] MAGIC1 = 8'h5a;
  24. reg [7:0] byte_index;
  25. reg [7:0] total_bytes;
  26. reg [15:0] crc;
  27. wire [15:0] crc_next;
  28. reg [15:0] crc_after_accept;
  29. reg [7:0] next_byte_index;
  30. reg [7:0] next_send_byte;
  31. reg [7:0] latched_profile_id;
  32. reg [7:0] latched_frame_type;
  33. reg [31:0] latched_frame_seq;
  34. reg [31:0] latched_timestamp_tick;
  35. reg [7:0] latched_payload_len;
  36. reg [(MAX_PAYLOAD_BYTES*8)-1:0] latched_payload;
  37. binary921_crc16 u_crc (
  38. .crc_in(crc),
  39. .data(tx_data),
  40. .crc_out(crc_next)
  41. );
  42. function [7:0] payload_byte;
  43. input [7:0] index;
  44. begin
  45. if (LATCH_PAYLOAD != 0) begin
  46. payload_byte = latched_payload[(index * 8) +: 8];
  47. end else begin
  48. payload_byte = payload[(index * 8) +: 8];
  49. end
  50. end
  51. endfunction
  52. always @* begin
  53. crc_after_accept = crc;
  54. if (byte_index >= 8'd2 && byte_index < (8'd14 + latched_payload_len)) begin
  55. crc_after_accept = crc_next;
  56. end
  57. next_byte_index = byte_index + 8'd1;
  58. case (next_byte_index)
  59. 8'd0: next_send_byte = MAGIC0;
  60. 8'd1: next_send_byte = MAGIC1;
  61. 8'd2: next_send_byte = VERSION;
  62. 8'd3: next_send_byte = latched_profile_id;
  63. 8'd4: next_send_byte = latched_frame_type;
  64. 8'd5: next_send_byte = latched_frame_seq[7:0];
  65. 8'd6: next_send_byte = latched_frame_seq[15:8];
  66. 8'd7: next_send_byte = latched_frame_seq[23:16];
  67. 8'd8: next_send_byte = latched_frame_seq[31:24];
  68. 8'd9: next_send_byte = latched_timestamp_tick[7:0];
  69. 8'd10: next_send_byte = latched_timestamp_tick[15:8];
  70. 8'd11: next_send_byte = latched_timestamp_tick[23:16];
  71. 8'd12: next_send_byte = latched_timestamp_tick[31:24];
  72. 8'd13: next_send_byte = latched_payload_len;
  73. default: begin
  74. if (next_byte_index < (8'd14 + latched_payload_len)) begin
  75. next_send_byte = payload_byte(next_byte_index - 8'd14);
  76. end else if (next_byte_index == (8'd14 + latched_payload_len)) begin
  77. next_send_byte = crc_after_accept[7:0];
  78. end else begin
  79. next_send_byte = crc_after_accept[15:8];
  80. end
  81. end
  82. endcase
  83. end
  84. always @(posedge clk or negedge rst_n) begin
  85. if (!rst_n) begin
  86. busy <= 1'b0;
  87. done <= 1'b0;
  88. tx_valid <= 1'b0;
  89. tx_data <= 8'd0;
  90. byte_index <= 8'd0;
  91. total_bytes <= 8'd0;
  92. crc <= 16'hffff;
  93. latched_profile_id <= 8'd0;
  94. latched_frame_type <= 8'd0;
  95. latched_frame_seq <= 32'd0;
  96. latched_timestamp_tick <= 32'd0;
  97. latched_payload_len <= 8'd0;
  98. latched_payload <= {(MAX_PAYLOAD_BYTES*8){1'b0}};
  99. end else begin
  100. done <= 1'b0;
  101. if (start && !busy) begin
  102. busy <= 1'b1;
  103. byte_index <= 8'd0;
  104. total_bytes <= 8'd16 + payload_len;
  105. crc <= 16'hffff;
  106. tx_valid <= 1'b1;
  107. tx_data <= MAGIC0;
  108. latched_profile_id <= profile_id;
  109. latched_frame_type <= frame_type;
  110. latched_frame_seq <= frame_seq;
  111. latched_timestamp_tick <= timestamp_tick;
  112. latched_payload_len <= payload_len;
  113. latched_payload <= payload;
  114. end else if (busy && tx_valid && tx_ready) begin
  115. if (byte_index >= 8'd2 && byte_index < (8'd14 + latched_payload_len)) begin
  116. crc <= crc_next;
  117. end
  118. if (byte_index == total_bytes - 8'd1) begin
  119. busy <= 1'b0;
  120. done <= 1'b1;
  121. tx_valid <= 1'b0;
  122. byte_index <= 8'd0;
  123. end else begin
  124. byte_index <= byte_index + 8'd1;
  125. tx_valid <= 1'b1;
  126. tx_data <= next_send_byte;
  127. end
  128. end else if (busy) begin
  129. tx_valid <= 1'b1;
  130. end else begin
  131. tx_valid <= 1'b0;
  132. end
  133. end
  134. end
  135. endmodule