generator.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. # vim: ts=8 sw=8 noexpandtab
  2. #
  3. # CRC code generator
  4. #
  5. # Copyright (c) 2020-2023 Michael Büsch <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 dataclasses import dataclass
  22. from libcrcgen.util import int2poly
  23. __all__ = [
  24. "CrcGen",
  25. "CrcGenError",
  26. ]
  27. @dataclass(frozen=True)
  28. class AbstractBit:
  29. def flatten(self):
  30. return [self, ]
  31. def optimize(self, sortLex):
  32. pass
  33. @dataclass(frozen=True)
  34. class Bit(AbstractBit):
  35. name: str
  36. index: int
  37. def gen_python(self, level=0):
  38. return f"{self.name}[{self.index}]"
  39. def gen_c(self, level=0):
  40. return f"b({self.name}, {self.index})"
  41. def gen_verilog(self, level=0):
  42. return f"{self.name}[{self.index}]"
  43. def gen_vhdl(self, level=0):
  44. return f"{self.name}({self.index})"
  45. def gen_myhdl(self, level=0):
  46. return f"{self.name}[{self.index}]"
  47. def sortKey(self):
  48. return f"{self.name}_{self.index:07}"
  49. @dataclass(frozen=True)
  50. class ConstBit(AbstractBit):
  51. value: int
  52. def gen_python(self, level=0):
  53. return "1" if self.value else "0"
  54. def gen_c(self, level=0):
  55. return "1u" if self.value else "0u"
  56. def gen_verilog(self, level=0):
  57. return "1'b1" if self.value else "1'b0"
  58. def gen_vhdl(self, level=0):
  59. return 'b"1"' if self.value else 'b"0"'
  60. def gen_myhdl(self, level=0):
  61. return "1" if self.value else "0"
  62. def sortKey(self):
  63. return "1" if self.value else "0"
  64. class XOR:
  65. __slots__ = (
  66. "__items",
  67. )
  68. def __init__(self, *items):
  69. self.__items = items
  70. def flatten(self):
  71. newItems = [ item
  72. for subItem in self.__items
  73. for item in subItem.flatten() ]
  74. self.__items = newItems
  75. return newItems
  76. def optimize(self, sortLex):
  77. newItems = []
  78. haveBits = {}
  79. constOnes = []
  80. for item in self.__items:
  81. if isinstance(item, Bit):
  82. # Store bit for even/uneven count analysis.
  83. haveBits[item] = haveBits.get(item, 0) + 1
  84. elif isinstance(item, ConstBit):
  85. # Constant 0 does not change the XOR result. Remove it.
  86. if item.value:
  87. constOnes.append(item)
  88. else:
  89. # This is something else. Keep it.
  90. newItems.append(item)
  91. # An even count of the same bit is equal to zero. Remove them.
  92. # An uneven count of the same bit is equal to one of them. Keep one.
  93. newItems.extend(bit for bit, count in haveBits.items()
  94. if count % 2)
  95. # If there's an uneven amount of constant ones, keep one of them.
  96. if len(constOnes) % 2:
  97. newItems.append(constOnes[0])
  98. if sortLex:
  99. # XOR can be arranged in any order.
  100. newItems.sort(key=lambda item: item.sortKey())
  101. if not newItems:
  102. # All items have been optimized out.
  103. # This term shall be zero.
  104. newItems.append(ConstBit(0))
  105. self.__items = newItems
  106. def gen_python(self, level=0):
  107. return self.__gen("(", ")", level, " ^ ", lambda item: item.gen_python(level + 1))
  108. def gen_c(self, level=0):
  109. return self.__gen("(", ")", level, " ^ ", lambda item: item.gen_c(level + 1))
  110. def gen_verilog(self, level=0):
  111. return self.__gen("(", ")", level, " ^ ", lambda item: item.gen_verilog(level + 1))
  112. def gen_vhdl(self, level=0):
  113. return self.__gen("(", ")", level, " xor ", lambda item: item.gen_vhdl(level + 1))
  114. def gen_myhdl(self, level=0):
  115. return self.__gen("(", ")", level, " ^ ", lambda item: item.gen_myhdl(level + 1))
  116. def __gen(self, prefix, suffix, level, oper, itemGen):
  117. assert self.__items, "Empty XOR."
  118. if level == 0:
  119. prefix = suffix = ""
  120. return prefix + (oper.join(itemGen(item) for item in self.__items)) + suffix
  121. def sortKey(self):
  122. return "__".join(item.sortKey() for item in self.__items)
  123. class Word:
  124. __slots__ = (
  125. "__items",
  126. )
  127. def __init__(self, *items):
  128. # items must be LSB first.
  129. self.__items = list(items)
  130. def __getitem__(self, index):
  131. return self.__items[index]
  132. def flatten(self):
  133. for item in self.__items:
  134. item.flatten()
  135. def optimize(self, sortLex):
  136. for item in self.__items:
  137. item.optimize(sortLex)
  138. class CrcGenError(Exception):
  139. pass
  140. class CrcGen:
  141. """Combinatorial CRC algorithm generator.
  142. """
  143. OPT_FLATTEN = 1 << 0 # Flatten the bit operation tree
  144. OPT_ELIMINATE = 1 << 1 # Eliminate redundant operations
  145. OPT_LEX = 1 << 2 # Sort the operands in lexicographical order where possible
  146. OPT_NONE = 0
  147. OPT_ALL = OPT_FLATTEN | OPT_ELIMINATE | OPT_LEX
  148. def __init__(self,
  149. P,
  150. nrCrcBits,
  151. nrDataBits=8,
  152. shiftRight=False,
  153. optimize=OPT_ALL):
  154. self._P = P
  155. self._nrCrcBits = nrCrcBits
  156. self._nrDataBits = nrDataBits
  157. self._shiftRight = shiftRight
  158. self._optimize = optimize
  159. def __gen(self, dataVarName, crcVarName):
  160. nrCrcBits = self._nrCrcBits
  161. nrDataBits = self._nrDataBits
  162. P = self._P
  163. if nrCrcBits < 1 or nrDataBits < 1:
  164. raise CrcGenError("Invalid number of bits.")
  165. # Construct the function input data word.
  166. inData = Word(*(
  167. Bit(dataVarName, i)
  168. for i in range(nrDataBits)
  169. ))
  170. # Construct the function input CRC word.
  171. inCrc = Word(*(
  172. Bit(crcVarName, i)
  173. for i in range(nrCrcBits)
  174. ))
  175. # Helper function to XOR a polynomial bit with the data bit 'dataBit',
  176. # if the decision bit 'queryBit' is set.
  177. # This is done reversed, because the polynomial is constant.
  178. def xor_P(dataBit, queryBit, bitNr):
  179. if (P >> bitNr) & 1:
  180. return XOR(dataBit, queryBit)
  181. return dataBit
  182. # Helper function to optimize the algorithm.
  183. # This removes unnecessary operations.
  184. def optimize(word, sort=False):
  185. if self._optimize & self.OPT_FLATTEN:
  186. word.flatten()
  187. if self._optimize & self.OPT_ELIMINATE:
  188. word.optimize(sortLex=(sort and (self._optimize & self.OPT_LEX)))
  189. return word
  190. # Run the shift register for each input data bit.
  191. word = inCrc
  192. if self._shiftRight:
  193. for i in range(nrDataBits):
  194. # Run the shift register once.
  195. bits = []
  196. for j in range(nrCrcBits):
  197. # Shift to the right: j + 1
  198. stateBit = word[j + 1] if j < nrCrcBits - 1 else ConstBit(0)
  199. # XOR the input bit with LSB.
  200. queryBit = XOR(word[0], inData[i])
  201. # XOR the polynomial coefficient, if the query bit is set.
  202. stateBit = xor_P(stateBit, queryBit, j)
  203. bits.append(stateBit)
  204. word = optimize(Word(*bits))
  205. else:
  206. for i in reversed(range(nrDataBits)):
  207. # Run the shift register once.
  208. bits = []
  209. for j in range(nrCrcBits):
  210. # Shift to the left: j - 1
  211. stateBit = word[j - 1] if j > 0 else ConstBit(0)
  212. # XOR the input bit with MSB.
  213. queryBit = XOR(word[nrCrcBits - 1], inData[i])
  214. # XOR the polynomial coefficient, if the query bit is set.
  215. stateBit = xor_P(stateBit, queryBit, j)
  216. bits.append(stateBit)
  217. word = optimize(Word(*bits))
  218. word = optimize(word, sort=True)
  219. return word
  220. def __header(self, language):
  221. return f"""\
  222. THIS IS GENERATED {language.upper()} CODE.
  223. https://bues.ch/h/crcgen
  224. This code is Public Domain.
  225. Permission to use, copy, modify, and/or distribute this software for any
  226. purpose with or without fee is hereby granted.
  227. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  228. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  229. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  230. SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  231. RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  232. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
  233. USE OR PERFORMANCE OF THIS SOFTWARE."""
  234. def __algDescription(self):
  235. pstr = int2poly(self._P, self._nrCrcBits, self._shiftRight)
  236. shift = "right (little endian)" if self._shiftRight else "left (big endian)"
  237. return (f"CRC polynomial coefficients: {pstr}\n"
  238. f" 0x{self._P:X} (hex)\n"
  239. f"CRC width: {self._nrCrcBits} bits\n"
  240. f"CRC shift direction: {shift}\n"
  241. f"Input word width: {self._nrDataBits} bits\n")
  242. def genPython(self,
  243. funcName="crc",
  244. crcVarName="crc",
  245. dataVarName="data"):
  246. word = self.__gen(dataVarName, crcVarName)
  247. ret = []
  248. ret.append("# vim: ts=4 sw=4 expandtab")
  249. ret.append("")
  250. ret.extend("# " + l for l in self.__header("Python").splitlines())
  251. ret.append("")
  252. ret.extend("# " + l for l in self.__algDescription().splitlines())
  253. ret.append("")
  254. ret.append(f"def {funcName}({crcVarName}, {dataVarName}):")
  255. ret.append(f" class bitwrapper:")
  256. ret.append(f" def __init__(self, x):")
  257. ret.append(f" self.x = x")
  258. ret.append(f" def __getitem__(self, i):")
  259. ret.append(f" return (self.x >> i) & 1")
  260. ret.append(f" def __setitem__(self, i, x):")
  261. ret.append(f" self.x = (self.x | (1 << i)) if x else (self.x & ~(1 << i))")
  262. ret.append(f" {crcVarName} = bitwrapper({crcVarName})")
  263. ret.append(f" {dataVarName} = bitwrapper({dataVarName})")
  264. ret.append(f" ret = bitwrapper(0)")
  265. for i, bit in enumerate(word):
  266. ret.append(f" ret[{i}] = {bit.gen_python()}")
  267. ret.append(" return ret.x")
  268. return "\n".join(ret)
  269. def genVerilog(self,
  270. genFunction=True,
  271. name="crc",
  272. inDataName="inData",
  273. inCrcName="inCrc",
  274. outCrcName="outCrc"):
  275. word = self.__gen(inDataName, inCrcName)
  276. ret = []
  277. ret.append("// vim: ts=4 sw=4 expandtab")
  278. ret.append("")
  279. ret.extend("// " + l for l in self.__header("Verilog").splitlines())
  280. ret.append("")
  281. if not genFunction:
  282. ret.append(f"`ifndef {name.upper()}_V_")
  283. ret.append(f"`define {name.upper()}_V_")
  284. ret.append("")
  285. ret.extend("// " + l for l in self.__algDescription().splitlines())
  286. ret.append("")
  287. if genFunction:
  288. ret.append(f"function automatic [{self._nrCrcBits - 1}:0] {name};")
  289. else:
  290. ret.append(f"module {name} (")
  291. end = ";" if genFunction else ","
  292. ret.append(f" input [{self._nrCrcBits - 1}:0] {inCrcName}{end}")
  293. ret.append(f" input [{self._nrDataBits - 1}:0] {inDataName}{end}")
  294. if genFunction:
  295. ret.append("begin")
  296. else:
  297. ret.append(f" output [{self._nrCrcBits - 1}:0] {outCrcName}")
  298. ret.append(");")
  299. for i, bit in enumerate(word):
  300. assign = "" if genFunction else "assign "
  301. assignName = name if genFunction else outCrcName
  302. ret.append(f" {assign}{assignName}[{i}] = {bit.gen_verilog()};")
  303. if genFunction:
  304. ret.append("end")
  305. ret.append("endfunction")
  306. else:
  307. ret.append("endmodule")
  308. ret.append("")
  309. ret.append(f"`endif // {name.upper()}_V_")
  310. return "\n".join(ret)
  311. def genVHDL(self,
  312. name="crc",
  313. inDataName="inData",
  314. inCrcName="inCrc",
  315. outCrcName="outCrc"):
  316. word = self.__gen(inDataName, inCrcName)
  317. ret = []
  318. ret.append(f"-- vim: ts=4 sw=4 expandtab")
  319. ret.append(f"")
  320. ret.extend(f"-- " + l for l in self.__header("VHDL").splitlines())
  321. ret.append(f"")
  322. ret.extend(f"-- " + l for l in self.__algDescription().splitlines())
  323. ret.append(f"")
  324. ret.append(f"library IEEE;")
  325. ret.append(f"use IEEE.std_logic_1164.all;")
  326. ret.append(f"")
  327. ret.append(f"entity {name} is")
  328. ret.append(f" port (")
  329. ret.append(f" {inCrcName}: in std_logic_vector({self._nrCrcBits - 1} downto 0);")
  330. ret.append(f" {inDataName}: in std_logic_vector({self._nrDataBits - 1} downto 0);")
  331. ret.append(f" {outCrcName}: out std_logic_vector({self._nrCrcBits - 1} downto 0)")
  332. ret.append(f" );")
  333. ret.append(f"end entity {name};")
  334. ret.append(f"")
  335. ret.append(f"architecture Behavioral of {name} is")
  336. ret.append(f"begin")
  337. for i, bit in enumerate(word):
  338. ret.append(f" {outCrcName}({i}) <= {bit.gen_vhdl()};")
  339. ret.append(f"end architecture Behavioral;")
  340. return "\n".join(ret)
  341. def genMyHDL(self,
  342. blockName="crc",
  343. inDataName="inData",
  344. inCrcName="inCrc",
  345. outCrcName="outCrc"):
  346. word = self.__gen(inDataName, inCrcName)
  347. ret = []
  348. ret.append("# vim: ts=4 sw=4 expandtab")
  349. ret.append("")
  350. ret.extend("# " + l for l in self.__header("MyHDL").splitlines())
  351. ret.append("")
  352. ret.extend("# " + l for l in self.__algDescription().splitlines())
  353. ret.append("")
  354. ret.append("from myhdl import *")
  355. ret.append("")
  356. ret.append("@block")
  357. ret.append(f"def {blockName}({inCrcName}, {inDataName}, {outCrcName}):")
  358. ret.append(" @always_comb")
  359. ret.append(" def logic():")
  360. for i, bit in enumerate(word):
  361. ret.append(f" {outCrcName}.next[{i}] = {bit.gen_myhdl()}")
  362. ret.append(" return logic")
  363. ret.append("")
  364. ret.append("if __name__ == '__main__':")
  365. ret.append(f" instance = {blockName}(")
  366. ret.append(f" {inCrcName}=Signal(intbv(0)[{self._nrCrcBits}:]),")
  367. ret.append(f" {inDataName}=Signal(intbv(0)[{self._nrDataBits}:]),")
  368. ret.append(f" {outCrcName}=Signal(intbv(0)[{self._nrCrcBits}:])")
  369. ret.append(f" )")
  370. ret.append(f" instance.convert(hdl='Verilog')")
  371. ret.append(f" instance.convert(hdl='VHDL')")
  372. return "\n".join(ret)
  373. def genC(self,
  374. funcName="crc",
  375. crcVarName="crc",
  376. dataVarName="data",
  377. static=False,
  378. inline=False,
  379. declOnly=False,
  380. includeGuards=True,
  381. includes=True):
  382. word = self.__gen(dataVarName, crcVarName)
  383. def makeCType(nrBits, name):
  384. if nrBits <= 8:
  385. cBits = 8
  386. elif nrBits <= 16:
  387. cBits = 16
  388. elif nrBits <= 32:
  389. cBits = 32
  390. elif nrBits <= 64:
  391. cBits = 64
  392. else:
  393. raise CrcGenError("C code generator: " + name + " sizes "
  394. "bigger than 64 bit "
  395. "are not supported.")
  396. return f"uint{cBits}_t"
  397. cCrcType = makeCType(self._nrCrcBits, "CRC")
  398. cDataType = makeCType(self._nrDataBits, "Input data")
  399. ret = []
  400. ret.append("// vim: ts=4 sw=4 expandtab")
  401. ret.append("")
  402. ret.extend("// " + l for l in self.__header("C").splitlines())
  403. ret.append("")
  404. if includeGuards:
  405. ret.append(f"#ifndef {funcName.upper()}_H_")
  406. ret.append(f"#define {funcName.upper()}_H_")
  407. if includes:
  408. ret.append("")
  409. ret.append("#include <stdint.h>")
  410. ret.append("")
  411. ret.extend("// " + l for l in self.__algDescription().splitlines())
  412. ret.append("")
  413. if not declOnly:
  414. ret.append("#ifdef b")
  415. ret.append("# undef b")
  416. ret.append("#endif")
  417. ret.append("#define b(x, b) (((x) >> (b)) & 1u)")
  418. ret.append("")
  419. extern = "extern " if declOnly else ""
  420. static = "static " if static and not declOnly else ""
  421. inline = "inline " if inline and not declOnly else ""
  422. end = ";" if declOnly else ""
  423. ret.append(f"{extern}{static}{inline}{cCrcType} "
  424. f"{funcName}({cCrcType} {crcVarName}, {cDataType} {dataVarName}){end}")
  425. if not declOnly:
  426. ret.append("{")
  427. ret.append(f" {cCrcType} ret;")
  428. for i, bit in enumerate(word):
  429. operator = "|=" if i > 0 else " ="
  430. ret.append(f" ret {operator} ({cCrcType})({bit.gen_c()}) << {i};")
  431. ret.append(" return ret;")
  432. ret.append("}")
  433. ret.append("#undef b")
  434. if includeGuards:
  435. ret.append("")
  436. ret.append(f"#endif /* {funcName.upper()}_H_ */")
  437. return "\n".join(ret)