Chapter 1: Operators
Chapter 1: Operators
4) -4’d2 is allowed, but it is illegal to have a – between base format and number as in
4’d-2
6) \t (tab), \n (new line) and \b (blank space) are the 3 possible spaces in Verilog.
The space in the string (anything between “ “) is anyway a space
7) Nets – wire, wand, wor, tri, triand, trior, trireg. Except trireg, nets have a default
value of ‘z’. ‘trireg’ defaults to ‘x’
8) Registers – default to ‘x’. It stores bits as an unsigned value (logic can interpret
sign)
reg [3:0] a;
reg [0:3] b;
12) Verilog arrays are allowed for reg, integer, time, real, realtime.
A verilog array has multiple elements which are 1-bit or n-bit wide
15) $monitor – displays the string whenever any of the signals to be displayed
changes its value.
If there are multiple $monitor statements, the last one takes the preference
(overrides the previous ones)
$monitoron and $monitoroff are used to control $monitor statement (let’s say in a
particular time frame in the simulation).
$monitor is on by default.
16) $stop – used to suspend the simulation (let’s say at a time after which the designer
may want to debug the design)
19) $write is similar to $display except that it doesn’t have an in-built new line
character.
20) $display is invoked as many times as it is called. Each time it is called (or
invoked), the value of the variables at that instance are printed out.
21) $monitor is invoked once per time unit in which it is called. It can be present
more than one time per time unit. But, it is invoked only once per time unit and it
prints the value of the variables at the end of the time step.
22) $strobe is invoked as many times as it is called per time unit. Each time, it prints
the value of the variables at the end of the time step. It is to be understood that
there could be time units where none of the $display, $monitor, $strobe directives
are scheduled
23) Example
************************
reg [3:0] a;
initial begin
a = i;
end
end
************************
$display -> It is printed 10 times from 0 to 9. Same for $monitor and $strobe
24) Sample code
Output:
25)
Chapter 2: Gate level modeling
1) Basic gates/primitives – and, or, nand, nor, xor, xnor, buf, not, etc
3) If the gate needs more than 2 inputs, keep extending the port list
6) buf/not with additional controls (ctrl0 and ctrl1) are also available
bufif0, bufif1, notif0, notif1.
Note: If ctrl is not there (0, in case of *1 primitives and vice versa), output
is ‘z’.
9)
Chapter 3: Data flow modeling
1) Continuous assignments
Inertial delay: Any pulse of width < the regular assignment delay is not
propagated.
wire #10 c;
4) Equality operators
5) Reduction operators – and, or, nand, nor, xor, xnor. Can’t be not (will be bit-wise)
//x = 4’b1010
&x => 0
| x => 1 // 1 | 0 | 1 | 0
Chapter 4: Behavioral modeling
2) initial
3) always
4) Procedural assignment
Step 3:
a) Non – blocking assignment (evaluation done in Step 1)
Step 4:
a) $monitor events
b) $strobe events
A verilog race condition occurs if two or more statements scheduled in the same
time step produce different end results by changing the order of execution of the
events as permitted by the Verilog standard
In this example, if the first always block is scheduled before the second one, y2 =
1 and y1 = 1 at the end of the simulation. If it is the other way round, y2 = 0 and
y1 = 0.
If in the above code, the blocking assignments are converted into non-blocking
assignments, it is not a race condition and y2 = 0 and y1 = 1 at the end of the
simulation. When reset is removed, and in the next clock cycle (time step), in the
active events queue, y1 is evaluated as 0 and y2 as 1. These values are stored in
some temporary variables. During step-3 of the event queue, the values are
assigned. Thus, the values get exchanged irrespective of the order in which the
two always blocks are scheduled
9) Sample Verilog codes and comments.
Code 1:
Code 2:
Comments: Will work both w.r.t simulation and synthesis. But not a suggested
style (gets cumbersome with more assignments)
Code 3:
Comments: Will not work. Verilog can schedule any always block before any
always block. So, will fail in Simulation. It will synthesize correctly.
11) Verilog – 2001 supports always @ (*), which is used to include all the variables
on the RHS in the always block into the sensitivity list
Verilog simulator doesn’t move without any timing control. 3 types of timing
control in Verilog
#0 a = b + 1; // 0- delay
******************
//Named event
if (a)
-> abc; //Trigger the event
end
c = d;
end
******************
13) while loop (based on a logical expression), for loop, repeat loop ( a numerical
count or a signal. If a signal, its value is evaluated when the loop starts off),
forever loop (infinite loop. Use $finish, disable to stop the loop), if-else are
supported
A full case is a case statement for which it is not possible to find a binary string
that will not match any case item
A parallel case is a case statement for which it is possible to find one and only one
case item corresponding to each possible binary string.
These blocks can be named (fork: abc). They can be disabled by using disable
*****************
fork
a = 1;
b = 0;
c = a + b;
join
*****************
*****************
genvar i; //variable used inside a generate block
generate
L l0 (.wren_in_n(we_n[i]),
.wsel_n(wsel_n),
.wren_out_n(we_gated_n[i]));
end
endgenerate
*****************
17)
Chapter 5: Logic Synthesis
3) The following code example where the output ‘f’ is assigned to the value of ‘c’
up-front is a good practice to avoid latch-inferences
4) The following code segment infers a gated latch (A level sensitive latch that
passes the input onto the output when the gate value is 1. It holds the value when
the latches’ gate input is ‘0’)
always @ (*) begin
if(a) b = c;
end
5) Case statements (without edge control like posedge or negedge in the sensitivity
lists of the always blocks) will infer a latch if all the cases are not specified. It’s a
good practice to have defaults
Simulation will give ‘X’ when sel = 2’b11. Synthesis will optimize the ‘X’.
NOTE: to prevent simulation-synthesis mismatches, it is suggested that default
case item will not have X’s assigned to the outputs.
11) Synopsys has the full case directive “// synopsys full_case” which interprets a
full case statement. For simulation, it is nothing more than a comment. So, it is
ignored. It has the following impact on Synthesis. If the case statement is full,
there’s no impact of the directive. If the case statement is not full (not all cases
are covered), the outputs are assigned the value of X’s for all the uncovered case
items, thereby causing a simulation-synthesis mismatch.
This code infers priority logic when simulated, but will infer a non-priority
logic when synthesized (because of the parallel_case directive)
14) The parallel case directive will infer non-priority logic (typically) from the code.
If the case statement already has no overlapping case-items (parallel), the
directive doesn’t create any simulation-synthesis mismatch. If, on the other
hand, the case statement is not full, it causes a simulation-synthesis mismatch.
16) If the sensitivity list contains an edge-sensitive specification, a latch will not be
inferred. It is inferred if the sensitivity list doesn’t contain any edge-sensitive
specification and if there exists at least one control path that doesn’t assign a
value to the left hand side variable (reg)
Chapter6: Functions and Tasks
1) Tasks
a) Definition
*********************
task abc;
input a;
input b;
output c;
#10 c = a & b;
endtask
*********************
b) Call to a task
*********************
module xyz;
reg a, b, c;
always @ (a or b) begin
abc(a,b,c); //no instance name
end
endmodule
*********************
2) Functions
a) Definition
********************
function abc;
input a;
abc = a + 1;
endfunction
*********************
b) Call to a function
**********************
module xyz;
input x;
output y;
reg y;
end
endmodule
**********************
d) Constant functions
4) Parameters can also be overridden by passing the values from top while
instantiation
5) Conditional compilation
- `ifdef, `ifndef, `elsif, `else, `endif
6) Conditional execution
- $test$plusargs
Looks if DISPLAY_VAR is set @ run time
- $value$plusargs
7) File I/O
******************
integer a;
a = $fopen(“abc.h”);
******************
Continuous assignments don’t queue up. They only keep track of the next
output change and the time it has to change. So, continuous assignments
properly model inertial delay
Examples
Transport delay models propagate all the signals to the output after every
input signal change. These models essentially queue up the changes
Example
always @ (a or b or ci) begin
end
end
This will eat away all inputs changes if there are changes inside the delay
Here, changes @ 24 get reflected @ the output @ 27. Changes @ 15, etc
are not reflected
All input changes between the first change and the delay units from the first
change are lost. Output reflects the first change after the delay units and will
ignore all the changes in between.
14) Tt
Q and A
1) always @ (a)
begin
if(a)
$display(“Hello”);
end
If in the above code, ‘a’ goes to ‘X’ at some point in simulation, will the message
get printed?
No. ‘If’ treats ‘X’ and ‘Z’ as false. ‘repeat’ also treats ‘X’ and ‘Z’ as zeros.
If the case expression is a constant, it is called a reverse case. The care variables
are compared against that constant in priority, in that case
4) What would happen if a signal not included in the sensitivity list of an always block is
used on the R.H.S inside the always block?
The simulation will not toggle even if that signal changes (as it is not included in the
sensitivity list), but the synthesis tool will generate a net-list that will consider that. That
way there will be simulation-synthesis mismatches.
Signal
SPECPARAM – can be used both in specify block and outside it and inside
regular modules.
PARAMETR – Only in modules, not in specify blocks.
7) What happens to the combinational logic whose output is not connected to any
port during synthesis?
Will be optimized out (removed) by the synthesis tool. Should be taken care of
during LINT phase
8) What value is sampled during simulation on an unconnected input port?
The Most significant extra bits of the wider port are left floating (High Z is
driven).
10) What happens if multiple assignments are made to the same signal either through
blocking or through non-blocking assignments with out any conditional
qualifiers?
Although it is not suggested to write code that way, coding in such a way will
take the last assignment as final in simulation. It neglects the previous
assignments
12) What happens w.r.t Synthesis if an integer is used in place of a reg variable?
13) When do you go for a case statement and when an IF-ELSE-IF kind of a coding?
If the control is based on one variable, then go for case. If the over all flow is
dependent on lot of conditions and variables, then go for IF-ELSE-IF although it
is not a strict rule, that’s the way it generally is.
14) How do you avoid a priority encoder in an IF-ELSE-IF structure
15) In casex, both X and Z are treated as don’t cares and in casez, only Z is treated as
don’t care. What happens to X’s in casez?
If both ‘force’ and ‘assign’ are used on the same variable, ‘force’ overwrites the
‘assign’ till it gets the corresponding ‘release’.
It would get synthesized with all possible selects (non-constant indices possible)
taken as input of a multiplexer.
No. The synthesis tool doesn’t understand how many times the internal variable
needs to be repeated.
No. Simulator will not have a clue to which instance of the task the user wants to
refer
There are 2 system tasks used to probe through the command line
a) $test$plusarg
b) $value$plusarg
23) Draw the state diagram and the corresponding verilog code for a state machine
that detects a pair of 1’s or 0’s in a serial input. The circuit should essentially
output a 1 for one cycle, whenever the input is 1 or 0 for 2 successive cycles.
Answer for both Mealy and Moore state machines
MOORE FSM
Verilog code
---------------------------------
module fsm( clk, rst, inp, outp);
2'b01:
begin
if( inp ) state <= 2'b11;
else state <= 2'b10;
end
2'b10:
begin
if( inp ) state <= 2'b01;
else state <= 2'b11;
end
2'b11:
begin
if( inp ) state <= 2'b01;
else state <= 2'b10;
end
endcase
end
end
end
endmodule
--------------------------------
MEALY FSM
Verilog Code
2'b01: begin
if( inp ) begin
state <= 2'b00;
outp <= 1;
end
else begin
state <= 2'b10;
outp <= 0;
end
end
2'b10: begin
if( inp ) begin
state <= 2'b01;
outp <= 0;
end
else begin
state <= 2'b00;
outp <= 1;
end
end
default: begin
state <= 2'b00;
outp <= 0;
end
endcase
end
end
endmodule
16
26) What is the functionality of &&&?
27) How do we get all the data printed on the standard output into a file?
$log(“filename”);
It has got to do with the messages printed at the very end of the simulation (when
finish kills the simulation)
Combinational logic
33) You have 2 clocks of same frequency, but with different phase. How do you find
out which clock is leading over the other?
Use a single D-FLOP. Feed one of the clocks (CLK1) to the D-Input and the other
(CLK2) to the clock input. If the output is high, CLK1 is leading else, CLK2 is
leading.
34) If your verilog function needs to return 2 values to the caller, what will you do?
reg [3:0] k;
for(k=0; k<=15; k=k+1)
---------
Infinite times
36) Is it possible to disable a function using named blocks and ‘disable’ construct?
37)