To create input vectors for our simulations, we need only a very small subset of Verilog. These pages give some examples
initial begin clk = 1'b0; globalReset = 1'b1; in = 1'b1; end
The expression: 1'b0 indicates a binary number of value 0. In fact, for setting the values zero and one, you need only type 0 and 1.
There are two main sections:
initial begin ----- end always begin ----- end
Notice that "blocks" of code in Verilog are "bracketed" by the keywords begin and end - in C this is done by "{" and "}".
The initial block is started at the beginning of the simulation. Thus it is the place to initialize signals and to define a sequence of signal changes.
The always clock is a sequence of signals which is repeated throughout the simulation.
The delay between signal changes (or events) is specified by a number following a # sign. Thus:
#160 globalReset = 0; #160 in = 0; #160 in = 1; #320 in = 0;
Notice that the delay is measured from the last event rather than from the beginning of the simulation.
The most common us of the always block is to define the clock signal. The following:
always begin #10 clk = ~clk; end
Thus the general approach is to setup the clock and other regularly alternating signals in always blocks and to define the sequence of changing signals in the initial block.
Warning: it is wise to end the initial block with a statement:
#<delay> $finish;
The repeat command may be useful. If you have a sequence of signal changes with is repeated for a given number (say 10) times then this can be coded as follows:
repeat(10) begin #40 in = 0; #20 in = 1; end
One further command may be useful: a random number generator. The following produces numbers in the range [0, n-1]:
{$random} % n
The exception is random ones and zeros because some random number generators are not quite random in the least significant bit (lsb). For this it is necessary to divide the first term by an even number to remove the lsb before the modulus operation.
{$random} / 16 % 2
repeat(14) begin #20 in = {$random} / 16 % 2; end
Note: