Search code examples
tclmodelsim

ModelSim freezes when it executes [gets stdin]


I have a TCL stript steering my ModelSim simulation. I would like to be able to break the simulation, ask user for some input and continue. vsim_break is not exactly what I want. I've tried set user_input [gets stdin], but the simulator freezes immediately. Are there other ways?


Solution

  • When you do gets stdin, Tcl (normally) waits until it receives a line from standard input. If you want to keep a GUI (or other background-processing system) going but still wait for a line, you need a slightly different approach.

    proc getStdinLine {} {
        set ::user_input [gets stdin]
    }
    fileevent stdin readable getStdinLine
    vwait ::user_input
    # Unregister the callback
    fileevent stdin readable {}
    

    BEWARE! This code can have problems with reentrancy; if your script manages to get back to the same point while it is waiting, it can very easily get itself in a real mess. This is because vwait keeps a record of what is being waited for on the C stack; this remains true even in Tcl 8.6 (though that has coroutines which you can use to create a workaround, if ModelSim is cooperative enough).