Verilog SAP
Verilog SAP
Verilog SAP
□ 0, 1
■ Obvious
□ Z
■ Output of an undriven tri-state driver
■ Models case where nothing is setting a wire’s
value
□ X
■ Models when the simulator can’t decide the value
■ Initial state of registers
■ When a wire is being driven to 0 and 1
simultaneously
■ Output of a gate with Z inputs
Basic Conventions
Input
Circuit
X Wire
Y Output
Z
O
Module
Input
Circuit
X Wire
Y Output
Z
O
Module
name
input X,Y,Z;
output O;
endmodule
Registers
□ Registers represent data storage elements.
Registers retain value until another value is
placed onto them.
□ In Verilog, the term register merely means
a variable that can hold a value.
□ Unlike a net, a register does not need a
driver.
Vectors
□ Arrays of Regs and Nets
Integers and Parameters
Ports
□ Ports provide interface for by which a
module can communicate with its
environment
Module
Port connection rules
Illegal Port connection
□ Module top;
//declare connection variables
reg [3:0] A, B;
reg C_in;
reg [3:0] sum;
wire C_out;
//instantiate fulladd4
fulladd4 fa0(SUM,C_OUT,A,B,C_IN);
//illegal connection
.
.
.
.
.
endmodule
Example
Connecting Ports
□ Suppose we have a module
Switch Level Modeling
□ Ability to design at MOS – transistor
(Switches) level.
■ nmos n1 (out, data, control);
■ pmos m1 (out, data, control);
■ cmos c1 (out, data, ncontrol,mcontrol);
□ Bidirectional Switches
■ tran t1 (inout1, inout2);
■ tranif0 t1 (inout1, inout2, control);
■ tranif1 t1 (inout1, inout2, control);
□ Power and Ground
■ Supply1 vdd;
■ Supply0 gnd;
Gate Level Modeling
□ A logic circuit can be designed by use of
logic gates.
□ Verilog supports basic logic gates as
predefined primitives. These primitives are
instantiated like modules except that they
are predefined in Verilog and do not need a
module definition.
Gate gate_name(out,in1,in2…)
Buf/not gates
□ Buflnot gates have one scalar input
and one or more scalar outputs.
Bufif/notif
Instantiation of bufif gates
Design of 4:1 Multiplexer
Contd..
Stimulus
4 bit full adder
Declaration:
Code contd..
4 bit adder using 1 bit adder
Stimulus
Gate Delays:
□ Rise Delay: Delay associated with a
o/p transition to 1 from any value.
Initial Statement
module stimulus
reg x,y,a,b,m;
initial
m = 1’b0;
initial
begin
#5 a=1’b1;
#25 b = 1’b0;
end
initial
begin
#10 x=1’b1;
#25 y = 1’b0;
end
initial
#50 $finish;
endmodule
always ststement
initial
#1000 $finish;
endmodule
Procedural assignments - Blocking statement
reg x, y, z
reg [15:0] reg_a, reg_b;
integer count;
initial
begin
x=0; y=1; z=1;
count = 0;
reg_a = 16’b0; reg_b = reg_a;
#15 reg_a[2] = 1’b1;
#10 reg_b[15:13] ={x,y,z};
count = count +1;
end
NON - Blocking statement
reg x, y, z
reg [15:0] reg_a, reg_b;
integer count;
initial
begin
x=0; y=1; z=1;
count = 0;
reg_a = 16’b0; reg_b = reg_a;
always@(posedge clock)
a <= b;
always@(posedge clock)
b <= a;
Case statement
casex
casez
While loop
Initial
Begin
count = 0;
while (count <128)
begin
$display (“count = %d”, count);
count = count +1;
end
end
For loop
Initial
for (count=0; count<128; count = cpunt+1)
$display(“count=%d”, count);
Repeat loop
repeat(64)
Forever loop
Initial
begin
clock = 1’b0;
forever #10 clock = ~clock;
end
Tasks and Function
□ Why??
■ To break up large behavioral designs into
smaller pieces.
□ Task
■ May have zero or more arguments of type
input, output or inout.
■ May contain delay, event or timing control
statements
■ Can enable other task and function
■ Donot return with a value, but can pass
multiple values through output and inout
argument
module top;
reg [15:0] A, B, and1,or1, xor1;
always @(A or B)
begin
bitwise_oper(and1,or1,xor1,A,B);
and
task bitwise_oper;
output [15:0] and1, or1, xor1;
input [15:0]a, b,;
begin
and1=a&b;
or1= a|b;
xor1=a^b;
end
endtask
endmodule
Function
□ Must not contain delay, event or timing control
statements
□ Always execute in 0 simuation time
□ Must have at least one input argument. Can
have more than one input.
□ Always return a single value. But can not have
output or inout arguments.
Module parity;
Reg[31:0]addr;
Reg parity;
always @(addr)
begin
parity = calc_parity(addr);
$display(“”);
end
function calc_parity;
input[31:0] address;
begin
calc_parity = ^addrress;
end
endfunction
endmodule
Automatic task & function