verilog
verilog
Verilog
Instance name
– Composition of primitive gates to form more complex module
Texas A&M University 7
Another Simple Circuit (in Structural Verilog)
module smpl_circuit(A,B,C,x,y);
input A,B,C;
output x,y;
wire e;
and g1(e,A,B);
not g2(y, C);
or g3(x,e,y);
endmodule
• Usage:
nand (out, in1, in2); 2-input NAND without delay
and #2 (out, in1, in2, in3); 3-input AND with 2 t.u. delay
not #1 N1(out, in); NOT with 1 t.u. delay and instance name
xor X1(out, in1, in2); 2-input XOR with instance name
x = A.B + C
y=C
A
x
B
module mux2x1_bh(A,B,select,OUT);
input A,B,select;
output OUT;
reg OUT;
always @ (select or A or B)
if (select == 1) OUT = A;
else OUT = B;
endmodule
reg Q;
wire D, Clk;
endmodule
always @(z) y
4 13 14 23
y <= #4 z;
– Its delay is called “transport” delay
– Applicable in non-blocking
assignments
A S
Half module half_adder(S, C, A, B);
Half output S, C;
B Adder
Adder C
input A, B;
wire S, C, A, B;
A
S assign S = A ^ B;
B assign C = A & B;
C
endmodule
cin
module full_adder(sum, cout, in1, in2, cin);
output sum, cout;
input in1, in2, cin;
endmodule
ha2.A
cin
Stimulus Response
Test-Bench & &
Control Signal Verification
endmodule
• If you want to synthesize your Verilog code, here are some tips
– Do not use delays in your code
– Watch for blocking and non-blocking assignments (next slide)
– Watch out for complete assignments (2 slides after next)
endmodule
• /* Multiple line
comment */
<size>’<radix> <value>
No
Noof of Binary
Binary →
→bbor
orBB Consecutive chars
bits → Consecutive chars
bits Octal
Octal →ooor
orOO 0-f,
0-f,x,x,zz
Decimal
Decimal →
→ddor
orDD
Hexadecimal→
Hexadecimal →hhor
orHH
– 8’h ax = 1010xxxx
– 12’o 3zx7 = 011zzzxxx111
• If size is ommitted it
– is inferred from the value or
– takes the simulation specific number of bits or
– takes the machine specific number of bits
wand Y; // declaration
assign Y = A;
A assign Y = B;
Y
B
wor Y; // declaration
assign Y = A;
assign Y = B;
dr
tri Y; // declaration
A Y
assign Y = (dr) ? A : z;
• Declaration
integer i, k;
real r;
• Use as registers (inside procedures)
i = 1; // assignments occur inside procedure
r = 2.9;
k = r; // k is rounded to 3
• Integers are not initialized!!
• Reals are initialized to 0.0
• No multi-dimentional arrays
reg var[1:10] [1:100]; // WRONG!!
• Escaped chars:
– \n newline
– \t tab
– %% %
– \\ \
– \“ “
but
butC&&B=0
C&&B=0
c = ~a; c = a & b;
a = 4’b1010;
b = 4’b1100;
c = a ^ b;
a = 4’b1010;
b = 2’b11;
• & → AND
• | → OR
• ^ → XOR
• ~& → NAND
• ~| → NOR
• ~^ or ^~ → XNOR
• == → logical equality
Return 0, 1 or x
• != → logical inequality
• === → case equality
• !== → case inequality Return 0 or 1
A
1
Y
B Y = (sel)? A : B;
0
sel
• +, -, *, /, %
• Negative integers:
– can be assigned negative values
– different treatment depending on base specification or not
reg [15:0] regA;
integer intA;
..
intA = -12/3; // evaluates to -4 (no base spec)
intA = -’d12/3; // evaluates to 1431655761 (base spec)
Use parentheses to
enforce your
priority
• wire
– Variable used simply to connect components together
• reg
– Variable that saves a value as part of a behavioral description
– Usually corresponds to a wire in the circuit
– Is NOT necessarily a register in the circuit
• usage:
– Don’t confuse reg assignments with the combinational
continuous assign statement!
– Reg should only be used with always blocks (sequential logic,
to be presented …)
• Same as C if statement
// Simple 4-1 mux
module mux4 (sel, A, B, C, D, Y);
input [1:0] sel; // 2-bit control signal
input A, B, C, D;
output Y;
reg Y; // target of assignment
always @(sel or A or B or C or D)
if (sel == 2’b00) Y = A;
else if (sel == 2’b01) Y = B;
else if (sel == 2’b10) Y = C;
else if (sel == 2’b11) Y = D;
endmodule
always @(sel or A or B or C or D)
if (sel[0] == 0)
if (sel[1] == 0) Y = A;
else Y = B;
else
if (sel[1] == 0) Y = C;
else Y = D;
endmodule
always @(sel or A or B or C or D)
case (sel)
2’b00: Y = A;
2’b01: Y = B; Conditions tested in
2’b10: Y = C; top to bottom order
2’b11: Y = D;
endcase
endmodule
always @(A)
case (A)
8’b00000001: Y = 0;
8’b00000010: Y = 1;
8’b00000100: Y = 2;
8’b00001000: Y = 3;
8’b00010000: Y = 4;
8’b00100000: Y = 5;
8’b01000000: Y = 6;
8’b10000000: Y = 7;
default: Y = 3’bX; // Don’t care when input is not 1-hot
endcase
endmodule
always @(A)
case (1’b1)
A[0]: Y = 0;
A[1]: Y = 1;
A[2]: Y = 2;
A[3]: Y = 3;
A[4]: Y = 4;
A[5]: Y = 5;
A[6]: Y = 6;
A[7]: Y = 7;
default: Y = 3’bX; // Don’t care when input is all 0’s
endcase
endmodule
always @(A)
case (1’b1) // synthesis parallel-case
A[0]: Y = 0;
A[1]: Y = 1;
A[2]: Y = 2;
A[3]: Y = 3;
A[4]: Y = 4;
A[5]: Y = 5;
A[6]: Y = 6;
A[7]: Y = 7;
default: Y = 3’bX; // Don’t care when input is all 0’s
endcase
endmodule
Texas A&M University 82
Verilog casex
• Like case, but cases can include ‘X’
– X bits not used when evaluating the cases
– In other words, you don’t care about those bits!
• // synthesis parallel_case
– Tells compiler that ordering of cases is not important
– That is, cases do not overlap
• e. g., state machine - can’t be in multiple states
– Gives cheaper implementation
• // synthesis full_case
– Tells compiler that cases left out can be treated as don’t cares
– Avoids incomplete specification and resulting latches