Week2-Slides Watermark
Week2-Slides Watermark
1
22/08/17
– Strength levels are typically used to resolve conflicts between signal drivers of pull Driving driven on a wire, the stronger signal
different strengths in real circuits. will prevail.
large Storage
Value Level Represents • These are particularly useful for MOS
Initialization: weak Driving level circuits, e.g. dynamic MOS.
0 Logic 0 state • All unconnected nets are set medium Storage
1 Logic 1 state to “z”. small Storage
x Unknown logic state • All register variables set to
highz High impedance
“x”.
z High impedance state
2
22/08/17
module simple_counter (clk, rst, count); module simple_counter (clk, rst, count);
input clk, input clk,
rst; output [31:0] rst; output [31:0]
32-bit counter with
count; count;
synchronous reset. 32-bit counter with
reg [31:0] count; reg [31:0] count;
• Count value increases at asynchronous reset.
always @(posedge clk) the positive edge of the always @(posedge clk or posedge rst) • Here reset occurs
begin clock. begin whenever “rst” goes high.
if (rst) • If “rst” is high, the counter if (rst) • Does not synchronize with
count = 32’b0; is reset at the positive edge count = 32’b0; clock.
else of the next clock. else
count = count + count = count + 1;
1; end
end endmodule
endmodule
Hardware Modeling Using Verilog 17
Hardware Modeling Using Verilog 18
3
22/08/17
4
22/08/17
5
22/08/17
6
22/08/17
7
22/08/17
Relational operators operate on numbers, and Bitwise operators operate on bits, and return a value that is also a bit.
return a Boolean value (true or false).
8
22/08/17
Reduction operators accepts a single word operand and produce a single bit as
Examples:
output. Shift Operators:
• Operates on all the bits within the word. wire [15:0] data, target;
>> shift right
<< shift left assign target = data >> 3;
Examples:
Reduction Operators: >>> arithmetic shift right assign target = data >>> 2;
wire [3:0] a, b, c; wire f1, f2, f3;
& bitwise AND
assign a = 4’b0111;
| bitwise OR Examples:
~& bitwise NAND assign b = 4’b1100;
Conditional Operator: wire a, b, c;
~| bitwise NOR assign c = 4’b0100;
cond_expr ? true_expr : false_expr; wire [7:0] x, y, z;
^ bitwise exclusive-OR assign f1 = ^a;
~^ bitwise exclusive-NOR assign a = (b > c) ? b : c;
// gives a 1 assign z = (x == y) ? x+2 : x-2;
assign f2 = & (a ^ b); // gives a 0
assign f3Modeling
Hardware = ^a & ~^b; // gives a 1
Using Verilog 49
Hardware Modeling Using Verilog 50
Concatenation Operator: Joins together bits from two or more module operator_example (x, y, f1, f2);
comma-separated expressions. input x, y;
{…, …, …}
output f1, f2;
Replication Operator: Joins together n copies of an expression wire [9:0] x, y; wire [4:0] f1; wire
{n{m}} m, where n is a constant. f2;
assign f1 = x[4:0] & y[4:0];
Examples: f2 = x[2] | ~f1[3];
assign f = {a, b}; assign f2 = ~& x;
assign f = {a, 3’b101, b}; f1 = f2 ? x[9:5] :
assign f = {x[2], y[0], a}; assign x[4:0];
assign f = {2’b10,
3{2’b01}, x}; assign
Hardware Modeling Using Verilog endmod Hardware Modeling Using Verilog
51 52
ule
+ – ! ~ (unary)
// An 8-bit adder description
Operator **
module parallel_adder (sum, cout, in1, in2, cin); Precedence * / %
<< >> >>>
Precedence increases
9
22/08/17
Some Points
• The presence of a ‘z’ or ‘x’ in a reg or wire being used in an arithmetic
expression results in the whole expression being unknown (‘x’).
• The logical operators (!, &&, | |) all evaluate to a 1-bit result (0, 1 or
x).
END OF LECTURE 09
• The relational operators (>, <, <=, >=, ~=, ==) also evaluate to a 1-bit
result (0 or 1).
• Boolean false is equivalent to 1’b0.
Boolean true is equivalent to 1’b1.
Example 1
• The structural hierarchical description of a 16-to-1 multiplexer.
a) Using pure behavioral modeling.
b) Structural modeling using 4-to-1 multiplexer specified using behavioral
Lec ture 10: VERILOG MODELING model.
EXAMPLES c) Make structural modeling of 4-to-1 multiplexer, using behavioral
modeling of 2-to-1 multiplexer.
PROF. INDRANIL SENGUPTA
DEPARTMENT OF COMPUTER SCIENCE AND d) Make structural gate-level modeling of 2-to-1 multiplexer, to have a
ENGINEERING
complete structural hierarchical description.
module muxtest;
reg [15:0] A; reg [3:0] S; wire
Version 1: Using pure behavioral modeling F;
10
22/08/17
11
22/08/17
~Y
[1
5]
&
Z[
15
])
;
endmodule
module alutest;
reg [15:0] X, Y; Simulation Output
wire [15:0] Z; wire S, ZR, CY, P, V;
ALU DUT (X, Y, Z, S, ZR, CY, P, V);
0 X=xxxx, Y=xxxx, Z=xxxx, S=x, Z=x, CY=x, P=x, V=x
initial 5 X=8fff, Y=8000, Z=0fff, S=0, Z=0, CY=1, P=1,
V=1 10 X=fffe, Y=0002, Z=0000, S=0, Z=1, CY=1,
begin
P=1, V=0 15 X=aaaa, Y=5555, Z=ffff, S=1, Z=0,
$dump
fil CY=0, P=1, V=0
e
("a
lu.
vcd
");
$du
mpv
ars
(0, Hardware Modeling Using Verilog Hardware Modeling Using Verilog
71 72
alu
tes
t);
$monitor ($time," X=%h, Y=%h, Z=%h, S=%b, Z=%b, CY=%b, P=%b,
V=%b", X, Y, Z, S, ZR, CY, P, V);
#5 X = 16'h8fff; Y = 16'h8000; #5
X = 16'hfffe; Y = 16'h0002;
#5 X = 16'hAAAA; Y = 16'h5555;
#5 $finish;
end
endmodule
12
22/08/17
adder4 A0
adder4 A1
(Z[3:0], c[1], X[3:0], Y[3:0], 1’b0);
(Z[7:4], c[2], X[7:4], Y[7:4], c[1]);
Version 3: Structural Modeling of Ripple Carry Adder
adder4 A2 (Z[11:8], c[3], X[11:8], Y[11:8], c[2]); module adder4 (S, cout, A, B, cin);
adder4 A3 (Z[15:12], Carry, X[15:12], Y[15:12], c[3]); input [3:0] A, B; input cin;
endmodule output [3:0] S; output
cout; wire c1,c2,c3;
Behavioral description of a 4-bit adder
fulladder FA0
module adder4 (S, cout, A, B, cin); (S[0],c1,A[0],B[0],cin);
input [3:0] A, B; input cin;
fulladder FA1
output [3:0] S; output
(S[1],c2,A[1],B[1],c1);
cout;
fulladder FA2
(S[2],c3,A[2],B[2],c2);
assign {cout,S} = A + B + fulladder FA3
cin; endmodule (S[3],cout,A[3],B[3],c3);
Hardware Modeling Using Verilog 75
Hardware Modeling Using Verilog 76
endmodule
Contd…
77
13
22/08/17
B3 A 3 B2 A 2 B1 A 1 B0
Generation of the Carry and Sum bits A0
c4 = g3 + g2p3 + g1p2p3 + g0p1p2p3 + c0p0p1p2p3 4 AND2 gates
3 AND3 gates gi and pi Generator
c3 = g2 + g1p2 + g0p1p2 + c0p0p1p2 2 AND4 gates
g3 p3 g2 p2 g1 p1 g0
c2 = g1 + g0p1 + c0p0p1 1 AND5 gate
p0
1 OR2, 1 OR3, 1 OR4
c1 = g0 + c0p0 4-bit Carry Look Ahead Circuit
and 1 OR5 gate
S0 = A0 Ⓒ B0 Ⓒ c0 = p0 Ⓒ c0 c3 c2 c1 c0
S1 = p1 Ⓒ c1 4 XOR2 gates
S2 = p2 Ⓒ c2 xor xor xor xor
S3 = p3 Ⓒ c3 c4
S3 S2 S1 S0
14
22/08/17
END OF LECTURE 11
15