Usage:
In all the projects that appear in the book, the files Xxx.tst, Xxx.out, and Xxx.cmp are supplied by us. These files are designed to test Xxx.yyy, whose development is the essence of the project. In some cases, we also supply a skeletal version of Xxx.yyy, for example, an HDL interface with a missing implementation part. All the files in all the projects are plain text files that can be viewed and edited using plain text editors.
Typically, one starts a simulation session by loading the supplied Xxx.tst script file into the relevant simulator. Typically, the first commands in the script instruct the simulator to load the code stored in Xxx.yyy and then, optionally, initialize an output file and a compare file. The remaining commands in the script run the actual tests, as we elaborate below.
B.2 Testing Chips on the Hardware Simulator
The hardware simulator supplied with the book is designed for testing and simulating chip definitions written in the Hardware Description Language (HDL) described in appendix A. Chapter 1 provides essential background on chip development and testing, and thus it is recommended to read it first.
B.2.1 Example
The script shown in figure B.1 is designed to test the EQ3 chip defined in figure A.1. A test script normally starts with some initialization commands, followed by a series of simulation steps, each ending with a semicolon. A simulation step typically instructs the simulator to bind the chip’s input pins to some test values, evaluate the chip logic, and write selected variable values into a designated output file. Figure B.2 illustrates the EQ3.tst script in action.
B.2.2 Data Types and Variables
Data Types
Test scripts support two data types: integers and strings. Integer constants can be expressed in hexadecimal (%X prefix), binary (%B prefix), or decimal (%D prefix) format, which is the default. These values are always translated into their equivalent 2’s complement binary values. For example, the commands set al %B1111111111111111, set a2 %XFFFF, set a3 %D-1, set a4 -1 will set the four variables to the same value: a series of sixteen 1’s, representing “minus one” in decimal. String values (%S prefix) are used strictly for printing purposes and cannot be assigned to variables. String constants must be enclosed by “”.
Figure B.1
Testing a chip on the hardware simulator.
The simulator clock (used in testing sequential chips only) emits a series of values denoted 0, 0+, 1, 1+, 2, 2+, 3, 3+, and so forth. The progression of these clock cycles (also called time units) is controlled by two script commands called tick and tock. A tick moves the clock value from
t
to
t
+, and a tock from
t
+ to
t
+ 1, bringing upon the next time unit. The current time unit is stored in a system variable called time.
Script commands can access three types of variables: pins, variables of built-in chips, and the system variable time.
Pins:
Input, output, and internal pins of the simulated chip. For example, the command set in 0 sets the value of the pin whose name is in to 0.
Variables of built-in chips:
Exposed by the chip’s external implementation. See section B.2.4 for more details.
Time:
The number of time units that elapsed since the simulation started running (read-only).
Figure B.2
Typical hardware simulation session, shown at the script’s end. The loaded script is identical to EQ3.tst from figure B.1, except that some white space was added to improve readability.
B.2.3 Script Commands
Command Syntax
A script is a sequence of commands. Each command is terminated by a comma, a semicolon, or an exclamation mark. These terminators have the following semantics:
■ Comma (,): terminates a script command.
■ Semicolon (;): terminates a script command and a simulation step. A simulation step consists of one or more script commands. When the user instructs the simulator to “single-step” via the simulator’s GUI, the simulator executes the script from the current command until a semicolon is reached, at which point the simulation is paused.
■ Exclamation mark (!): terminates a script command and stops the script execution. The user can later resume the script execution from that point onward. This option is typically used to facilitate interactive debugging.
It is convenient to organize the script commands in two conceptual sections. “Set up commands” are used to load files and initialize global settings. “Simulation commands” walk the simulator through a series of tests.
Setup Commands
load
Xxx.hdl: Loads the HDL program stored in Xxx.hdl into the simulator. The file name must include the .hdl extension and must not include a path specification. The simulator will try to load the file from the current directory, and, failing that, from the simulator’s builtIn directory, as described in section A.3.
output-file
Xxx.out: Instructs the simulator to write further output to the named file, which must include an .out extension. The output file will be created in the current directory.
output-list
υ
1,
υ
2,...: Instructs the simulator what to write to the output file in every subsequent output command in this script (until the next output-list command, if any). Each value in the list is a variable name followed by a formatting specification. The command also produces a single header line consisting of the variable names. Each item v in the output-list has the syntax variable format
padL.len.padR.
This directive instructs the simulator to write
padL
spaces, then the current variable value in the specified format using len columns, then
padR
spaces, then the divider symbol “|”. Format can be either %B (binary), %X (hexa), %D (decimal) or %S (string). The default format specification is %B1.1.1.
For example, the CPU.hdl chip of the Hack platform has an input pin named reset, an output pin named pc (among others), and a chip part named DRegister (among others). If we want to track the values of these variables during the chip’s execution, we can use something like the following command:
(Sate variables of built-in chips are explained here.) This command may produce the following output (after two subsequent output commands):
compare-to
Xxx.cmp: Instructs the simulator that each subsequent output line should be compared to its corresponding line in the specified comparison file (which must include the .cmp extension). If any two lines are not the same, the simulator displays an error message and halts the script execution. The compare file is assumed to be present in the current directory.
Simulation Commands
set
variable value:
Assigns the value to the variable. The variable is either a pin or an internal variable of the simulated chip or one of its chip parts. The widths of the value and the variable must be compatible. For example, if x is a 16-bit pin and y is a 1-bit pin, then set x 153 is valid whereas set y 153 will yield an error and halt the simulation.
eval:
Instructs the simulator to apply the chip logic to the current values of the input pins and compute the resulting output values.
output:
This command causes the simulator to go through the following logic:
1. Get the current values of all the variables listed in the last output-list command.
2. Create an output line using the format specified in the last output-list command.
3.
Write the output line to the output file.
4. (if a compare file has been previously declared via the compare-to command): If the output line differs from the current line of the compare file, display an error message and stop the script’s execution.
5.
Advance the line cursors of the output file and the compare file.
tick:
Ends the first phase of the current time unit (clock cycle).
tock:
Ends the second phase of the current time unit and embarks on the first phase of the next time unit.
repeat
num
{
commands
}: Instructs the simulator to repeat the commands enclosed by the curly brackets num times. If num is omitted, the simulator repeats the commands until the simulation has been stopped for some reason.
while
Boolean-condition
{
commands
}: Instructs the simulator to repeat the commands enclosed in the curly brackets as long as the Boolean-condition is true. The condition is of the form
x
op
y
where
x
and
y
are either constants or variable names and op is one of the following: =, >, <, >=, <=, <>. If
x
and
y
are strings, op can be either = or <>.
echo
text
: Instructs the simulator to display the text string in the status line (which is part of the simulator GUI). The text must be enclosed by “ ”.
clear-echo:
Instructs the simulator to clear the status line.
breakpoint
variable value:
Instructs the simulator to compare the value of the specified
variable
to the specified value. The comparison is performed after the execution of each script command. If the variable contains the specified value, the execution halts and a message is displayed. Otherwise, the execution continues normally.