Another thing that we can do is keep track of the outputs of the simulation.
So in particular we can augment our basic
test scripts with something like the following commands.
We can at the beginning of the of the test when we initialize things, we can.
Instruct the simulator to create an output file,
which in this example, we happen to call Xor.out.
And as we go through the through the testing.
At the end of each test,
we tell the simulator to output to the output file a set of values,
which is determined in the preamble of the script.
So, as you see the third line with script says, the output placed is a, b, and out.
And so whenever the simulator would see the directive output,
it will write to the output file, the current values of a, b and out.
And at the end of the simulation,
we can simply inspect the result of the output file and convince our self that
the chip has actually behaved according to expectations.
In fact, in this particular example,
if you will inspect the output file that emerged from the simulation,
you will notice that it is completely identical to the truth table of Xor gate.
So it looks like the gate is well behaving.
So here's a snapshot of the hardware
simulation in action using a test script.
And once again, we have the HDL code as,
as we did before at the bottom left of, of the screen.
But we, but we now have something new.
We have a test script, which can be seen on the right pane.
Which is some sort of a multipurpose GUI area,
which we use for for different purposes.
And after we load the script,
we can instruct the simulator to actually execute the script.
We do it by clicking this control and the simulator the simulator goes to work.
And at the end, if we want, we can inspect the resulting output file.
And here is also a demo of of the same thing with,
with an actual how do simulations session.
We will now show how to use the how to simulator in
order to test an HDL program using a test script.
So the first thing that we can do is load the HDL program into the simulator.
So let us choose the same Xor.hdl chip that we used before and
then instead of playing with the input pins interactively.
We can use this icon here to open a test script and
we see that in the project zero folder,
we have one test script called Xor.tst.
So let us load it into the simulator environment.
Once we do it this pane in the interface shows us the contents of
the currently loaded test script and we see that it begins with a few comments.
And then following that, we see the actual testing commands.
And the first command or the next command to be executed,
is highlighted by a yellow bar,
we can call it the cursor of the of the test script.
Now we see that each command ends either with a comma or
a semicolon and when we click the play icon,
the simulator is going to execute all the testing
commands until the next semicolon is encountered.
Now the first four commands in this test strip set up the simulation so to speak.
And so let us, let us execute them.
So I click this icon here.
And we see that the cursor moved to the next batch of commands.
And this batch says, set a to 0, set b to 0.
These are the input pins of this particular chip.
And then evaluate the chip logic and output the result into the output file.
So let us execute this batch of commands and
we see that indeed, the a and b inputs are now 0 and
the out of the chip happens to be 0, which seems to be right.
Because as we know, Xor of and zero and zero should be zero.
So let us execute the next batch of test script commands.
This batch sets a to 0 and b to 1, evaluates the shapes and
outputs the result and indeed, we see that Xor of 0 and
0 ended up being 1, which seems to be correct.
So executing the next batch of commands sets a to 0, I'm sorry.
Sets a to 1 and b to 0 giving the result 1, which also seems to be correct.
And then moving along, we execute the next and
final batch of commands, which set both a and b to one.
And we see that the Xor response by emitting the value zero,
which once again seems to be correct.
Now the test script for Xor gate is relatively simple,
because basically, it simply executes every possible input
combination as stated by the chip truth table.
In more complex shapes such an exhaustive testing is is less feasible.
And therefore, the test, testing scripts will likely be more complex.
Now, as we tested this script the script has also written
the results of every test script batch into the output file.
You know, this is the the result of saying output in, in the I'm sorry.
This is the result of, of the output command.
Now, if we want to inspect the output file, we can do so
by going to this control here and selecting Output.
So let's do that.
And once we do it, we see that the output file,
indeed looks exactly, like the truth table of Xor and this gives us yet
another indication that the chip has operated to a specification.
Before we go on with this step by step working example,
I would like to stop for a minute or two and
say some general things about the general concept of hardware simulators.
So first of all, there are many of them.
Some of them are free and open source.
Others are proprietary and very sophisticated and very expensive.
And in this course, we use a simple hardware simulator, which at the same
time, gives you everything that you need in order to build the head computer and
any other computer that you want to build using the techniques
described in the course.
So the supplied hardware simulator, which you have on your computer now is a,
is a tool that once again, gives you everything you need.
And if you want to learn how to use it,
there are several places to consult with available documentation.
If you go to the nand2tetris .org website you can find a full chapter,
I think it's appendix a and appen, actually appendix and appendix b.
In our book describe how to use the hardware simulator and
these, these chapters are also available freely on the website and
another thing that you should do is go for the hardware simulator tutorial,
which is a stack of interacting slides that that explain how to use the simulator
as well as all of the examples that you saw in this particular lecture.
So that's the end of of the commercial break about hardware simulators and
we return to our ongoing example.
So let us revisit what we have done so far.
We looked at a particular HDL file,
which records the logic of Xor.
We have supplied the simulator with
a test script designed to test this particular code.
And we also looked at the resulting output file and
convinced ourself that the HDL file is actually correct.
Now this privilege of looking at an output file and saying, yeah,
it works is something which is rarely available in real life testing.
Why?
Because the chips that we normally build are far more complex than Xor gate.
And there is no way to look at an output file of let's
say an ALU or, or a CPU chip or something like that.
And, and say, yeah, it works correctly.
You know, it's we, we have to do it in,
in a far more systematic and planned way.
So is there a way to do it?
Well, you bet.
And that's, that's what we're going to do next.
In particular, we can carry out the simulation with yet
another tool which is called a compare file.
So here's an example of a compare file.
It looks exactly like an output file.
And in fact, in its previous incarnation, the compare file was a,
a, an output file of a well-behaving Xor chips.
So someone, and we'll talk about this someone momentarily,
someone has written an Xor chip correctly.
Ran it thru the simulator, generated an output file.
Renamed it compare file and gave it us.
And said, now you go and build your chip and, and compare it to what I did.
So basically, we can take the compare file and load it into the simulator and
then start to do the simulator as the simulation as we did before.
And now, in each step of the simulation, whenever we output a set
of selected pin values, the simulator will compare the output
set of values to the respective line in the compare file.
And if the two lines will not agree with each other.
The simulator will not throw a comparison error.
So this gives us that ability to not only record the outputs of the chip,
but also compare them to desired results.
So you probably asked yourself once again,
how do we generate these compare files to begin with?
Right?
Well, we, we could have left it as as a mystery, but
there is no need to to leave anything unexplained.
So here's how we do it.
We do it using something called behavioral simulation.
In particular, consider this Xor gate that we saw before.
And let us assume, once again that this implementation is correct and
we put it into the simulator along with the script.
We run it, we generate results and then we simply rename it to be Xor.cmp and
it becomes the official compare file of this shape.
So what is special about this slide, didn't, didn't you say,
everything that we said here before.
Well, we did, but there's one detail which is, which is