user1405165
user1405165

Reputation: 21

Tcl binary loop. Binary increment

I am trying to write a tcl script for my decoder module (verilog in modelsim) I need to loop the 'din' input value from from 000 to 111
Thats what i've come up with by now.

vsim work.decode_shift
add wave -noupdate -format Logic -radix binary  /decode_shift/din
add wave -noupdate -format Logic -radix binary  /decode_shift/dout
for { set i 0 } { $i==0111 } { incr i } {
    force din $i
    run 100
}
run @500ns

It doesn't work because of some type issue that i din't know how to bypass. What am i doing wrong and what is the proper way to increment binary digits in tcl?

Upvotes: 2

Views: 1956

Answers (2)

Julian
Julian

Reputation: 1542

Don't know if this will help you http://codepad.org/YX4nfMIS (reproduced below) It makes an ascending list of strings representing numbers in binary. But that may not be how Verilog wants your data.

set l { "000" "001" "010" "011" "100" "101" "110" "111"}
for { set i 0} { $i<8 } { incr i } {
 puts [lindex $l $i]
}

Or as Donal points out

set l { "000" "001" "010" "011" "100" "101" "110" "111"}
foreach i $l {
  puts $i
}

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137637

With Tcl, you don't increment binary digits. You format the number as binary. Prior to 8.6, you use a combination of binary format and binary scan to do the conversion, as here:

vsim work.decode_shift
add wave -noupdate -format Logic -radix binary  /decode_shift/din
add wave -noupdate -format Logic -radix binary  /decode_shift/dout
for { set i 0 } { $i<=7 } { incr i } {      # Need non-binary literal
    # Convert to 8 binary digits, store result in “i_bin” variable
    binary scan [binary format c $i] B8 i_bin

    force din $i_bin; # Assume takes 8 bits; [string range] to trim otherwise
    run 100
}
run @500ns

If you've got 8.6, you can do this instead:

vsim work.decode_shift
add wave -noupdate -format Logic -radix binary  /decode_shift/din
add wave -noupdate -format Logic -radix binary  /decode_shift/dout
for { set i 0 } { $i<=0b111 } { incr i } {   # Binary literal...
    force din [format %04b $i]; # width 4 field, zero padded on left
    run 100
}
run @500ns

Upvotes: 2

Related Questions