Quartus® II Tcl Example: Version Number in a VHDL Register Bank



This example procedure generates a VHDL file with a hexadecimal value stored in a bank of registers. You can use this procedure to automate writing small amounts of data (such as a revision number) to a register bank in your design.

The generated VHDL file is named version_reg.vhd. Call the procedure with the hexadecimal number you want stored in the register bank. There is an example of how to call the procedure at the bottom of this page.

When you call the procedure in a Tcl script, you should wrap the procedure call in a catch statement because the procedure returns an error if there are problems creating the VHDL file. You can catch the error and display it.

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

Using a Catch Statement

The following is an example of how to call the procedure above and catch any errors.

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