# -*- tcl -*-

# List all available programming hardwares, and select the USBBlaster.
# (Note: this example assumes only one USBBlaster connected.)
puts "Programming Hardwares:"
foreach hardware_name [get_hardware_names] {
        puts $hardware_name
        if { [string match "USB-Blaster*" $hardware_name] } {
                set usbblaster_name $hardware_name
        }
}
puts "\nSelect JTAG chain connected to $usbblaster_name.\n";

# List all devices on the chain, and select the first device on the chain.
puts "\nDevices on the JTAG chain:"
foreach device_name [get_device_names -hardware_name $usbblaster_name] {
        puts $device_name
        if { [string match "@1*" $device_name] } {
                set test_device $device_name
        }
}
puts "\nSelect device: $test_device.\n";

# Virtual JTAG commands
set vjtag_index 0
proc vdr {length {value ""}} {
    upvar vjtag_index ix
    set opad [string repeat 0 [expr ($length+3)/4 - [string length $value]]]
    return [device_virtual_dr_shift -instance_index $ix \
		-show_equivalent_device_ir_dr_shift \
		-value_in_hex -length $length -dr_value "$opad$value"]
}
proc vir {cmd} {
    upvar vjtag_index ix
    return [device_virtual_ir_shift -instance_index $ix \
		-show_equivalent_device_ir_dr_shift \
		-ir_value $cmd -no_captured_ir_value]
}

# Device JTAG commands
proc ddr {length {value ""}} {
    set opad [string repeat 0 [expr ($length+3)/4 - [string length $value]]]
    return [device_dr_shift -value_in_hex -length $length \
		-dr_value "$opad$value"]
}
proc dir {cmd} {
    return [device_ir_shift -ir_value $cmd -no_captured_ir_value]
}

# Open device 
open_device -hardware_name $usbblaster_name -device_name $test_device

# Lock device
device_lock -timeout 100000
