<p>不,C/C++中的enum和python中的enum.enum是两个完全不同的东西。然而,有一个强大的解决方案。我建议<em>从python代码中编写C/C++/Arduino头<strong>。借助python强大的内省功能,可以轻松地使用<code>__dict__</code>扫描python枚举/类,并编写Arduino代码可以使用的<code>.h</code>文件。这就是我如何生成与相关python项目中的枚举和常量匹配的Verilog和SystemVerilog头的方法。运行python应用程序会生成一个始终同步的新头文件</p>
<p>编辑:一个更明确的例子</p>
<p>我已经为基于FPGA的微处理器构建了一个汇编程序。汇编程序是用python编写的,而处理器是用Verilog编写的。因此,我创建了一个Verilog头文件,如下所示:</p>
<pre><code># Compute the 'after' content.
content = '// opcodes.vh\n'
content += '`ifndef _opcodes_vh\n'
content += '`define _opcodes_vh\n'
content += '// DO NOT EDIT FILE GENERATED BY ASSEMBLER\n'
content += '// \n'
content += '// OPCODES\n'
content += '// \n'
A = Arch
for i in A.__dict__:
if i.startswith('OPC_'):
o = i.replace('OPC_', 'OPCODE_')
s = '`define ' + o
while len(s) < 40:
s = s + ' '
hexval = str(hex(A.__dict__[i])).replace('0x', '')
decval = str(A.__dict__[i])
s = s + "7'h" + hexval + '\t\t// ' + str(decval) + '\n'
content += s
content += '// END OF GENERATED FILE.\n'
content += '`endif'
# Write to very specific location for Vivado to see it.
file = open(self.opcodes_filename, 'w', encoding='utf-8')
file.write(content)
file.close()
</code></pre>
<p>最终输出如下所示:</p>
<pre><code>// opcodes.vh
`ifndef _opcodes_vh
`define _opcodes_vh
// DO NOT EDIT FILE GENERATED BY ASSEMBLER
//
// OPCODES
//
`define OPCODE_LD_GPR_EXPR 7'h0 // 0
`define OPCODE_LD_GPR_GPTR 7'h1 // 1
`define OPCODE_SV_EXPR_GPR 7'h2 // 2
...
`define OPCODE_IO_T 7'h4a // 74
`define OPCODE_TX_T 7'h4b // 75
// END OF GENERATED FILE.
`endif
</code></pre>