Then, I’m using Tcl string and list commands to extract the time value and time units.
In the code below, I’m using the ModelSim-specific examine command to read VHDL signal and constant values in Tcl. First, I want to read the clock_period constant and passcode generic into the Tcl environment. Now that we have loaded the simulation, we can start interacting with the VHDL code. I also like to include an If statement that will load the waveform, if it exists. No simulation time passes until we use the run command later in the script. That will load the simulation, but it won’t run it. We do that with the vsim command, followed by the library and entity name of the VHDL testbench. Inside the namespace, we have to begin by starting the simulation, as shown below. We will put all the Tcl code we write from now on inside of the codelocktb namespace, as shown below. By wrapping all of your code in the namespace, you avoid that potential mess. That’s a good idea because otherwise, you may unintentionally overwrite global variables from your Tcl script. The first thing I recommend is to create a Tcl namespace.
If you have ModelSim installed, you can download the example project using the form below.
It’s a drawback of using Tcl that your code gets locked to a particular simulator vendor.įor reference, I recommend the Tcl Developer Xchange, which covers the Tcl language in general, and the ModelSim Command Reference Manual, which describes all the ModelSim-specific commands.
That’s because it uses a few commands that are specific to this simulator. If you want to use it in Vivado, for instance, you have to make some changes to it. The Tcl code in this example only works with the ModelSim VHDL simulator. The code below is combinational, but since the pins signal is clocked, the unlock signal will only change on the rising edge of the clock. The implementationĪt the top of the architecture region, I’ve added a concurrent statement that unlocks the vault when the pins signal matches the generic constants. With this scheme, the user will explicitly have to enter four 0’s. If we had used the range 0 to 9 for the pins array, setting the secret passcode to 0000 would have initially opened the vault. We have to use a reset value which isn’t a digit that the user can enter, and that’s what the -1 is for. Type pins_type is array (0 to 3) of integer range -1 to 9 But instead of using the BCD range of 0 to 9, we let the numbers go from -1 to 9. This module has only one internal signal: a shift register that contains the four last digits that the user has typed. The other inputs and outputs are std_logics. Just like the passcode, the input_digit signal is also a BCD type. Generic (pin0, pin1, pin2, pin3 : integer range 0 to 9) The four generic constants are binary coded decimals (BCDs) realized as integers with a restricted range. Because this module’s purpose is to be a simple example DUT for our TCL-based testbench, I’m hard-coding the secret passcode using generics. The code below shows the entity of the code lock module. In this article, we will use 1234 as the passcode. The unlock signal shall only be ‘1’ only when the user has entered four consecutive digits that match the correct PIN. Imagine that it controls the locking mechanism of a safe or a vault of some kind. There is only one output from this module: the unlock signal. The module shall sample the input digit when the enable is ‘1’ on a rising clock edge. Apart from the clock and reset, there are two input signals: input_digit and input_enable. The waveform above shows how the code lock module is going to work. Thus, we need to create a sequence detector in VHDL. To relock it, we can enter another, incorrect number. Our module will start in the locked position, and if we enter four digits in a row that matches the secret PIN code, it will unlock the safe. For simplicity, our example will only use the number keys, and not the “CLEAR” and “LOCK” buttons. The image above shows such a code lock in the form of a hotel safe.
Strictly speaking, a combination lock is really a permutation lock, but let’s call it a code lock. You also have to enter them in the right order. It’s not enough to enter the correct combination of digits to unlock it. However, I find this term to be inaccurate. People often refer to a code lock as a combination lock. It will be a code lock module that will unlock a vault when we enter the correct number sequence on a PIN pad. Before we start on the testbench, I will present the device under test (DUT).