e1_crc4.v 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * e1_crc4.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * E1 CRC4 computation
  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 e1_crc4 #(
  30. parameter INIT = 4'h0,
  31. parameter POLY = 4'h3
  32. )(
  33. // Input
  34. input wire in_bit,
  35. input wire in_first,
  36. input wire in_valid,
  37. // Output (updated 1 cycle after input)
  38. output wire [3:0] out_crc4,
  39. // Common
  40. input wire clk,
  41. input wire rst
  42. );
  43. reg [3:0] state;
  44. wire [3:0] state_fb_mux;
  45. wire [3:0] state_upd_mux;
  46. assign state_fb_mux = (INIT & {4{in_first}}) | (state & {4{~in_first}}); // in_first ? INIT : state
  47. assign state_upd_mux = (state_fb_mux[3] != in_bit) ? POLY : 0;
  48. always @(posedge clk)
  49. if (in_valid)
  50. state <= { state_fb_mux[2:0], 1'b0 } ^ state_upd_mux;
  51. assign out_crc4 = state;
  52. endmodule // e1_crc4