hdb3_enc.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * hdb3_enc.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * HDB3 bit ->symbols encoding as described in G.703
  7. *
  8. *
  9. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  10. * All rights reserved.
  11. *
  12. * LGPL v3+, see LICENSE.lgpl3
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 3 of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  27. */
  28. `default_nettype none
  29. module hdb3_enc (
  30. // Input
  31. input wire in_data,
  32. input wire in_valid,
  33. // Output
  34. output wire out_pos,
  35. output wire out_neg,
  36. output reg out_valid,
  37. // Common
  38. input wire clk,
  39. input wire rst
  40. );
  41. // Signals
  42. reg [3:0] d_pos;
  43. reg [3:0] d_neg;
  44. reg [1:0] zcnt; // Zero-Count
  45. reg pstate; // Pulse state
  46. reg vstate; // Violation state
  47. // Output
  48. assign out_pos = d_pos[3];
  49. assign out_neg = d_neg[3];
  50. always @(posedge clk)
  51. out_valid <= in_valid;
  52. // Main logic
  53. always @(posedge clk)
  54. begin
  55. if (rst) begin
  56. // Reset state
  57. d_pos <= 4'h0;
  58. d_neg <= 4'h0;
  59. zcnt <= 2'b00;
  60. pstate <= 1'b0;
  61. vstate <= 1'b0;
  62. end else if (in_valid) begin
  63. // Check for 4 zeros
  64. if ((zcnt == 2'b11) && (in_data == 1'b0)) begin
  65. // This is a run, handle special case
  66. // But need to check if it's 000V or B00V
  67. if (pstate == vstate) begin
  68. // Pulse State is the same state as the last violation
  69. // state. So this next violation state is going to be
  70. // opposite polarity, so no DC to compensate -> 000V
  71. // New data: Violation bit
  72. d_pos[0] <= pstate;
  73. d_neg[0] <= ~pstate;
  74. // Shift reg
  75. d_pos[3:1] <= d_pos[2:0];
  76. d_neg[3:1] <= d_neg[2:0];
  77. // Zero count: Reset
  78. zcnt <= 2'b00;
  79. // Pulse state tracking
  80. pstate <= pstate;
  81. // Violation state tracking
  82. vstate <= vstate ^ 1;
  83. end else begin
  84. // Pulse State is the opposite state as the last violation
  85. // state. So this next violation would be the same
  86. // polarity ... need to use B00V to avoid DC
  87. // New data: Violation bit
  88. d_pos[0] <= ~pstate;
  89. d_neg[0] <= pstate;
  90. // Shift reg
  91. d_pos[2:1] <= d_pos[1:0];
  92. d_neg[2:1] <= d_neg[1:0];
  93. // Balancing bit
  94. d_pos[3] <= ~pstate;
  95. d_neg[3] <= pstate;
  96. // Zero count: Reset
  97. zcnt <= 2'b00;
  98. // Pulse state tracking
  99. pstate <= pstate ^ 1;
  100. // Violation state tracking
  101. vstate <= vstate ^ 1;
  102. end
  103. end else begin
  104. // Normal case
  105. // New data
  106. d_pos[0] <= in_data & ~pstate;
  107. d_neg[0] <= in_data & pstate;
  108. // Shift reg
  109. d_pos[3:1] <= d_pos[2:0];
  110. d_neg[3:1] <= d_neg[2:0];
  111. // Zero count
  112. if (in_data == 1'b0)
  113. zcnt <= zcnt + 1;
  114. else
  115. zcnt <= 2'b00;
  116. // Pulse state tracking
  117. pstate <= pstate ^ in_data;
  118. // Violation state tracking
  119. vstate <= vstate;
  120. end
  121. end
  122. end
  123. endmodule // hdb3_enc