Simulate with ModelSimModelSim - simulation softwareModelSim can be used to simulate VHDL-code, to determine whether it is "right" thinking.The Altera version of ModelSim is also integrated with a "database" with facts about Alterachips, eg. MAX-chips, so one can also do simulations that take into account the "time delay"and other phenomena within the intended target circuit. ( As long as the target circuit is ofAltera's brand . ).Select the correct software version - in school there are severalversions installed in the Start menu!Altera Web edition\ModelSim-Altera Starter Edition\ModelSim-Altera 10.1d(Quartus II 13.0sp1)Start ModelSim.In the window "important information" you click on Jumpstart to get help with setting up aproject.1

Then you click on "Create a Project" in the welcome window.Create a project.Project NameMAXsim can be a suitable nameProject locationH:/MAXwork browse to the same folder you used for Quartus.Default Library Namework keep the suggested name, it's the standard at VHDL-simulationKlicka på OK.2

We choose "Add Existing File" to add a VHDL-file to the project."Browse" to the file codelock.vhd that we created earlier with Quartus.Clic on OK. Then clic on Close.Codelock code in ModelSimModelSim has a own compiler to produce the code for simulation. Though we havecompiled the VHDL code in Quartus we must now compile it again for ModelSim.Choose Compile menu, alternative Compile All.3

Now the VHDL-code is also compiled for Modelsim.Status symbol changes from a blue question mark to a green check!Simulate codelock-template!We simulate by giving different commands in the Transcript-window, and then follow someselected signals in the window Wave.Transcript-window is a terminal window where you enter commands, but you can also givemost commands by menu selection, or by clicking on buttons. Commands are always writtenin theTranscript-window, regardless of how they are given.Load the Design to simulator.Choose the tab Library, and open the folder work. Doubleclick on "Entity" codelock. Aseries of commands are now executed resulting in that the design is loaded into the simulator.In the Transcript-window you can follow the commands executed.4

Prepare simulationWe need to have a number of windows open in order to follow the simulation.Give thees commands in Transcript-windowor check in the View-menu.VSIM VSIM VSIM VSIM viewviewviewviewobjectslocalssourcewave -undockModelsim consists of "windows". It can be hard to see everything at the same time. With thebutton Zoom/Unzoom you can enlarge the window. With the button Dock/Undock thewindow can be moved to any location, it is that alternative we choose for Wave-window.With the button Close those windows not needed for the time can be closed.Signals in Wave windowIf you have many signals, it is a good idea to select the signals you are interested to followin Wave-window, but this time we choose to follow them all:add wave *There are several ways to add signals to the Wave-window: Select signals in Object-window (Shift Left Button) and "drag and drop" theselection to Wave-window.Right-click in the Object-window and choose Add to Wave.A Add to Wave dialoge-window is reachable from the menu, Add.5

Format, Radix, HexadecimalThe state variable q has 32 different states, such a variable is easier to follow if it is presentedas a hexa-decimal number, 00 . 1F instead of a binary number. We therefore suggestthat you check the variable and change the presentation to hexadecimal. UUUUU is exchangedto XX in the Wave-window. Other variables are best suited to be presented as binary numbers.Create stimuliThe default time resolution in Wave is nanoseconds, ns. A suitable clock frequency for acode lock may however be as low as 5 Hz, or a period time of 0.2 sec.The easiest way, of not having to make extensive adjustments of the program, is to "scale"6

our problem to a higher clock speed with a period of 20 ns. We then has to imagine that thereare fast fingers that press the keys!Stimuli, Inputsignals as clock pulses or key-presses, are created with the command forcein the Transcript-window.force codelock/clk 1 0ns, 0 10ns -repeat 20nsGenerates clockpulses for ever.force codelock/k 000force codelock/r 0000Initiates variables r and 100nsRuns the simulation in 100 ns, which is five clock cycles.Simulate keypressesforce codelock/kforce codelock/rrun 30nsforce codelock/kforce codelock/rrun 800ns0010001000000030 ns (20 10) means that the keypress is certain be in the gap between the clock edges. Thefull simulationtime 100 ns 30ns 800 ns 930 ns corresponds to 46.5 clock pulse periods.This is enough to show the lock's entire opening sequence.7

Do-fileTranscript-window, you can run many commands in sequence from a Do-file.Alternatively, you can copy the text in Windows (Ctrl-C) and paste it (Ctrl-V) in Transcript.delete wave *add wave codelock/clkadd wave codelock/kadd wave codelock/radd wave codelock/qadd wave codelock/unlockforce codelock/clk 1 0ns, 0 10ns -repeat 20nsforce codelock/k 000force codelock/r 0000run 100nsforce codelock/k 001force codelock/r 0001run 30nsforce codelock/k 000force codelock/r 0000run 800nsThis is how to create a Do-file. Paste text commands above in the file. Then save it among theother files (in MAXwork) with extension .do.You run a Do-file with these commands (in Transcript):restart -fdo lock.doFind in the Wave windowIt can be difficult to find what you are looking for in the Wave window. Therefore, there is awhole series of tools like Zoom, Expanded time, Cursors .Add, Cursor.8

A Cursor can be used together with the function Edit, Wave Signal Search.Now the Cursor points what happens (this time nothing special!) when q has the state 07.Spend a little time now to try different tools available for orientation in Wave Window!The simulation has shown that the lock opens to the intended keypress, but this is not enough - There is need for more "testing"before one can trust the construction!William Sandqvist [email protected]