Quartus® II Tcl Verilog 寄存器库中的版本号

author-image

作者

此示例程序生成一个 Verilog 文件,其中包含存储在寄存器库中的十六进制值。可以使用此程序自动将少量数据(如修订号)写入设计中的寄存器库。

生成的 Verilog 文件名为 version_reg.v。使用要存储在寄存器库中的十六进制数调用程序。此页面底部有一个展示如何调用此程序的示例。

由于如果创建 Verilog 文件时出现问题,此程序将返回错误,因此在 Tcl 脚本中调用此程序时,应将程序调用封装在 catch 语句中。可以捕获并显示错误。

proc generate_verilog { hex_value } {

    set num_digits [string length $hex_value]
    set bit_width [expr { 4 * $num_digits } ]
    set high_index [expr { $bit_width - 1 } ]
    set reset_value [string repeat "0" $num_digits]

    if { [catch {
        set fh [open "version_reg.v" w ]
        puts $fh "module version_reg (clock, reset, data_out);"
        puts $fh "    input clock;"
        puts $fh "    input reset;"
        puts $fh "    output \[$high_index:0\] data_out;"
        puts $fh "    reg \[$high_index:0\] data_out;"
        puts $fh "    always @ (posedge clock or negedge reset) begin"
        puts $fh "        if (!reset)"
        puts $fh "            data_out <= ${bit_width}'h${reset_value};"
        puts $fh "        else"
        puts $fh "            data_out <= ${bit_width}'h${hex_value};"
        puts $fh "    end"
        puts $fh "endmodule"
        close $fh
    } res ] } {
        return -code error $res
    } else {
        return 1
    }
}

使用 catch 语句

下面是如何调用上述程序并捕获任何错误的示例:

set my_hex_number "A5"
if { [catch { generate_verilog $my_hex_number } res] } {
    post_message -type error "Couldn't generate Verilog file\n$res"
}
# If the script gets here, there were no errors.