main.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # vim: ts=8 sw=8 noexpandtab
  2. #
  3. # CRC code generator
  4. #
  5. # Copyright (c) 2019-2021 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. from libcrcgen import *
  22. import sys
  23. import argparse
  24. __all__ = [
  25. "main",
  26. ]
  27. def main():
  28. try:
  29. def argInt(string):
  30. if string.startswith("0x"):
  31. return int(string[2:], 16)
  32. return int(string)
  33. p = argparse.ArgumentParser()
  34. g = p.add_mutually_exclusive_group(required=True)
  35. g.add_argument("-v", "--verilog-function", action="store_true", help="Generate Verilog function")
  36. g.add_argument("-m", "--verilog-module", action="store_true", help="Generate Verilog module")
  37. g.add_argument("-V", "--vhdl", action="store_true", help="Generate VHDL module")
  38. g.add_argument("-M", "--myhdl", action="store_true", help="Generate MyHDL block")
  39. g.add_argument("-p", "--python", action="store_true", help="Generate Python code")
  40. g.add_argument("-c", "--c", action="store_true", help="Generate C code")
  41. g.add_argument("-T", "--polynomial-convert", type=str, help="Convert a polynomial from string to int or vice versa.")
  42. g.add_argument("-t", "--test", action="store_true", help="Run unit tests for the specified algorithm")
  43. p.add_argument("-a", "--algorithm", type=str,
  44. choices=CRC_PARAMETERS.keys(), default="CRC-32",
  45. help="Select the CRC algorithm. "
  46. "Individual algorithm parameters (e.g. polynomial) can be overridden with the options below.")
  47. p.add_argument("-P", "--polynomial", type=str, help="CRC polynomial")
  48. p.add_argument("-B", "--nr-crc-bits", type=argInt, help="Number of CRC bits.")
  49. p.add_argument("-b", "--nr-data-bits", type=argInt, default="8", help="Number of input data word bits.")
  50. g = p.add_mutually_exclusive_group()
  51. g.add_argument("-R", "--shift-right", action="store_true", help="CRC algorithm shift direction: right shift")
  52. g.add_argument("-L", "--shift-left", action="store_true", help="CRC algorithm shift direction: left shift")
  53. p.add_argument("-n", "--name", type=str, default="crc", help="Generated function/module name")
  54. p.add_argument("-D", "--data-param", type=str, default="data", help="Generated function/module data parameter name")
  55. p.add_argument("-C", "--crc-in-param", type=str, default="crcIn", help="Generated function/module crc input parameter name")
  56. p.add_argument("-o", "--crc-out-param", type=str, default="crcOut", help="Generated module crc output parameter name")
  57. p.add_argument("-S", "--static", action="store_true", help="Generate static C function")
  58. p.add_argument("-I", "--inline", action="store_true", help="Generate inline C function")
  59. p.add_argument("-O", "--optimize", type=argInt, default=CrcGen.OPT_ALL,
  60. help=f"Select individual algorithm optimizer steps. "
  61. f"The argument to the -O option can be any sum of the following integers: "
  62. f"-O{CrcGen.OPT_FLATTEN} (Flatten the bit operation tree), "
  63. f"-O{CrcGen.OPT_ELIMINATE} (Eliminate redundant operations), "
  64. f"-O{CrcGen.OPT_LEX} (Sort the operands in lexicographical order where possible). "
  65. f"-O{CrcGen.OPT_NONE} disables all optimizer steps. "
  66. f"If this option is not given, then by default all optimizer steps are enabled (-O{CrcGen.OPT_ALL}).")
  67. args = p.parse_args()
  68. if (args.nr_crc_bits is not None and
  69. args.nr_crc_bits < 1):
  70. raise CrcGenError("Invalid -B|--nr-crc-bits argument.")
  71. if args.nr_data_bits < 1:
  72. raise CrcGenError("Invalid -b|--nr-data-bits argument.")
  73. if args.polynomial_convert is not None:
  74. if args.nr_crc_bits is None:
  75. raise CrcGenError("-B|--nr-crc-bits is required for -T|--polynomial-convert")
  76. try:
  77. if "^" in args.polynomial_convert.lower():
  78. p = poly2int(args.polynomial_convert,
  79. args.nr_crc_bits,
  80. args.shift_right)
  81. print(f"0x{p:X}")
  82. else:
  83. print(int2poly(int(args.polynomial_convert, 0),
  84. args.nr_crc_bits,
  85. args.shift_right))
  86. except ValueError as e:
  87. raise CrcGenError("-T|--polynomial-convert error: " + str(e))
  88. return 0
  89. crcParameters = CRC_PARAMETERS[args.algorithm].copy()
  90. if args.nr_crc_bits is not None:
  91. crcParameters["nrBits"] = args.nr_crc_bits
  92. if args.shift_right:
  93. crcParameters["shiftRight"] = True
  94. if args.shift_left:
  95. crcParameters["shiftRight"] = False
  96. if args.polynomial is not None:
  97. try:
  98. if "^" in args.polynomial:
  99. polynomial = poly2int(args.polynomial,
  100. crcParameters["nrBits"],
  101. crcParameters["shiftRight"])
  102. else:
  103. polynomial = argInt(args.polynomial)
  104. except ValueError as e:
  105. raise CrcGenError("Polynomial error: " + str(e))
  106. crcParameters["polynomial"] = polynomial
  107. polynomial = crcParameters["polynomial"]
  108. nrCrcBits = crcParameters["nrBits"]
  109. shiftRight = crcParameters["shiftRight"]
  110. if polynomial > ((1 << nrCrcBits) - 1):
  111. raise CrcGenError(f"Invalid polynomial. "
  112. f"It is bigger than the CRC width "
  113. f"of (2**{nrCrcBits})-1.")
  114. gen = CrcGen(P=polynomial,
  115. nrCrcBits=nrCrcBits,
  116. nrDataBits=args.nr_data_bits,
  117. shiftRight=shiftRight,
  118. optimize=args.optimize)
  119. if args.test:
  120. gen.runTests()
  121. else:
  122. if args.python:
  123. print(gen.genPython(funcName=args.name,
  124. crcVarName=args.crc_in_param,
  125. dataVarName=args.data_param))
  126. elif args.verilog_function:
  127. print(gen.genVerilog(genFunction=True,
  128. name=args.name,
  129. inDataName=args.data_param,
  130. inCrcName=args.crc_in_param,
  131. outCrcName=args.crc_out_param))
  132. elif args.verilog_module:
  133. print(gen.genVerilog(genFunction=False,
  134. name=args.name,
  135. inDataName=args.data_param,
  136. inCrcName=args.crc_in_param,
  137. outCrcName=args.crc_out_param))
  138. elif args.vhdl:
  139. print(gen.genVHDL(name=args.name,
  140. inDataName=args.data_param,
  141. inCrcName=args.crc_in_param,
  142. outCrcName=args.crc_out_param))
  143. elif args.myhdl:
  144. print(gen.genMyHDL(blockName=args.name,
  145. inDataName=args.data_param,
  146. inCrcName=args.crc_in_param,
  147. outCrcName=args.crc_out_param))
  148. elif args.c:
  149. print(gen.genC(funcName=args.name,
  150. crcVarName=args.crc_in_param,
  151. dataVarName=args.data_param,
  152. static=args.static,
  153. inline=args.inline))
  154. return 0
  155. except CrcGenError as e:
  156. print("ERROR: " + str(e), file=sys.stderr)
  157. return 1