Quartus® II Tcl 示例:VHDL 寄存器库中的版本号

author-image

作者

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

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

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

proc generate_vhdl { 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.vhd" w ]
        puts $fh "LIBRARY ieee;\nUSE ieee.std_logic_1164.ALL;"
        puts $fh "ENTITY version_reg IS"
        puts $fh "    PORT ("
        puts $fh "        clock: IN STD_LOGIC;"
        puts $fh "        reset: IN STD_LOGIC;"
        puts $fh "        data_out: OUT STD_LOGIC_VECTOR(${high_index} \
             downto 0)"
        puts $fh "    );"
        puts $fh "END version_reg;"
        puts $fh "ARCHITECTURE rtl OF version_reg IS"
        puts $fh "BEGIN"
        puts $fh "PROCESS (clock,reset)"
        puts $fh "    BEGIN"
        puts $fh "    IF (reset='0') THEN"
        puts $fh "        data_out <=X\"${reset_value}\""
        puts $fh "    ELSIF rising_edge (clock) THEN"
        puts $fh "        data_out <= X\"${hex_value}\""
        puts $fh "    END IF;"
        puts $fh "END PROCESS;"
        puts $fh "END rtl;"
        close $fh
    } res ] } {
        return -code error $res
    } else {
        return 1
    }
}

使用 catch 语句

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

set my_hex_number "A5"
if { [catch { generate_vhdl $my_hex_number } res] } {
    post_message -type error "Couldn't generate VHDL file\n$res"
}
# 如果脚本显示此内容,表明没有错误。