Welcome again to FPGA Design for Embedded Systems. In this video, you will learn how to start and run a simulation in ModelSim directly, including setting up the project and libraries, compiling the HDL code, and running the simulation. You'll learn helpful interactions with ModelSim, including setting breakpoints and single stepping through source code, and controlling the waveform window with zoom and cursors, and how to automate tests and debug with ModelSim by the use of DO files and the ModelSim command line. Come, simulate with us. Install ModelSim Student Edition 10.4a, which is the current one. Prepare for this exercise by copying files counter.v and tcounter.v from the Modeltech_pe_edu_10.4a directory, which is the installation directory, basically. Within that installation directory, there's an examples folder. These files are in that examples folder, under tutorials/verilog/basicSimulation. Place these files counter.v and tcounter.v in a new directory like C:/AlteraPrj/Sim1 or something like that of your choosing. To start the simulation, first, start ModelSim and then click "Jumpstart". In the next dialog box that appears, click on Create a Project. Name the project and then browse to your directory, create the project, and then add the files count.v and tcount.v into that directory. In the top menu, select Compile All, which will now compile tcount.v and count.v. Then click Simulate by selecting t.count.v in the work library and clicking on "OK". The simulation has started now. You can tell this because a bunch of signals will show up in the object window. You can click on a signal in the object window. Hit Control A and drag those signals over to the wave window. This is the easiest way in order to move the top-level signals from the object window to the wave window. Then you're going to want to simulate for 100 nanoseconds. You could do that by clicking the down run arrow, which is next to the simulation time text box that is in the top menu of ModelSim. Once you click that down arrow, it's going to run for 100 nanoseconds and you'll see a waveform that looks something like this. If you right-click in the wave window and select zoom-full, then you'll get the biggest picture of it. You should be able then to see the counter counting. We've got one, two, three, four, five and so on. There's a number of other things you can do. You can select View Files to see the files that are part of this simulated system. We're looking now at files, and we have tcounter.v and counter.v. You want to double click on the count and tcount.v files to see the source code. The source code will show up in the same space where the wave window is. You want to examine the tcount source. It only has a clock, and a reset stimulus, and a monitor as you can see on this page. Then the stimulus is just a 50 megahertz clock and a reset. The monitor causes values to be printed in the transcript window, which shows you the simulation as it works. This is a handy thing that can be done that you can include. This is not synthesizable by the way, when you include a monitor as part of the test bench. But this is a very simple test bench, but it does some nice things for us so that we can understand what's happening with the code. In the count.v window, if you click next to line 36, it'll set a break point. If you click the Run button again to run for another 100 nanoseconds, this time, it won't run for 100 nanoseconds. Why is that? Well, it hits the breakpoint. When it hits the breakpoint, it stops. In fact, it only runs for 10 nanoseconds before hitting the breakpoint. The signal values can be seen in the object or in the wave window when it stops at this point. You can see what's happening with all the signal generation at this point. The code also can be single stepped, just like C code in an IDE. Using the icons that are shown here that are in the top menu, up, down arrow and so on. You have step into, step over, step around and all those kinds of things that you would normally see in debugging C code. You can do the same thing here. It's a very powerful tool that can be used with a simulator to debug your HDL code when you see a waveform that doesn't show up the way you expect. You can set a breakpoint right before that point and then single step through it to try to find out what is happening and where you might have an error in coding that you can fix. Important debugging tool with the simulator. It's helpful sometimes to be able to control zoom. if you click on the zoom mode icon and the wave toolbar, it's going to expand to the whole page. Then you can see more details. This is only temporary though, the zoom goes to full screen, but if you click this icon again, it returns back to the window that it was originally in. You can also use icons to zoom in and out as shown here. They're the typical zoom icons. The one in the middle here is zoom-full, which is something that I use a lot to get the biggest possible picture of what's happening with all the waveforms. You can also make good use of cursors within the wave window in order to do a number of things, you can add additional cursors. You can name, lock, and delete cursors. You can use cursors to measure time intervals. This is one thing that is done most often with cursors. You can also use cursors to find transitions. You can zoom in on a cursor and so on if you've got an area where there's a number of transitions that are occurring, particularly when you start to do timing simulation, this is quite helpful. Another simulator capability that you want to make use of is DO files. You want to make do with DO. You can automate simulation by creating DO files. DO files are scripts that allow you to execute many commands at once. You can save your transcript as a DO file to repeat all the steps that you've done in a simulation session. DO files can be edited just like text files, and in fact, are TCL scripts. ModelSim can run from the transcript as a command line by entering TCL commands there, which then can be collected into a DO file and repeat it. To save a transcript file as a DO file, first, open a saved transcript file in a text editor, or you can hit control A in the transcript window and save that into an editor, and then you go through and remove all the commented lines which will have a pound sign in front of them. Then leave only the lines with commands once you get rid of all the commented lines and then you can save the file as some name.do. The script then can be run by typing the DO command in the command window. You would type DO and then the filename.do, or you can also go to the top menu tool and select tool TCL execute macro, and then it can execute the DO file that way. In this video, you have learned how to start and run a simulation in ModelSim directly, including setting up the project and libraries, compiling the HDL code and running the simulation. You've learned helpful interactions with ModelSim including setting breakpoints and single stepping through source code and controlling the waveform window with zoom and cursors, and how to automate test and debug with ModelSim by the use of DO files and the ModelSim command line.