L05 - Verilog - Operators Instantiation Parametrization
L05 - Verilog - Operators Instantiation Parametrization
Verilog Operators
Expressing Numbers in Verilog
Hierarchical Design – Module Instantiation
2
Verilog Operators and Bit Lengths:
For Reference
Category Examples Bit Length
3
Bitwise Operators in Behavioral Verilog
endmodule
4
Bitwise Operators: Schematic View
5
5
Reduction Operators in
Behavioral Verilog
module and8(input [7:0] a,
output y);
assign y = &a;
endmodule
6
Reduction Operators: Schematic View
[0]
[1]
[2]
[3]
[7:0]
a[7:0] [4]
AND y
[5]
[6]
[7] 8-input AND gate
7
Conditional Assignment in
Behavioral Verilog
module mux2(input [3:0] d0, d1,
input s,
output [3:0] y);
assign y = s ? d1 : d0;
// if (s) then y=d1 else y=d0;
endmodule
8
Conditional Assignment: Schematic View
9
More Complex Conditional Assignments
endmodule
10
Even More Complex Conditional
Assignments
module mux4(input [3:0] d0, d1, d2, d3
input [1:0] s,
output [3:0] y);
assign y = (s == 2’b11) ? d3 :
(s == 2’b10) ? d2 :
(s == 2’b01) ? d1 :
d0;
// if (s = “11” ) then y= d3
// else if (s = “10” ) then y= d2
// else if (s = “01” ) then y= d1
// else y= d0
endmodule
11
Verilog Operators and Bit Lengths:
For Reference
Category Examples Bit Length
13
Precedence of Operations in Verilog
Highest
Lowest
14
Expressing Numbers in Verilog
15
How to Express Numbers ?
N’Bxx
8’b0000_0001
• (N) Number of bits
– Expresses how many bits will be used to store the value
• (B) Base
– Can be b (binary), h (hexadecimal), d (decimal), o (octal)
• (xx) Number
– The value expressed in base
– Can also have X (invalid) and Z (floating), as values
– Underscore _ can be used to improve readability
16
Number Representation in Verilog
32 bits
(default)
17
Reminder: Floating Signals (Z)
assign y = en ? a : 4'bz;
endmodule
en
[3:0] [3:0] [3:0] [3:0]
a[3:0] y[3:0]
y_1[3:0]
18
Recall: Tri-State Buffer
• A tri-state buffer enables gating of different signals onto
a wire
19
Recall: Example Use of Tri-State Buffers
• Imagine a wire connecting the CPU and memory
– At any time only the CPU or the memory can place a value
on the wire, both not both
– You can have two tri-state buffers: one driven by CPU, the
other memory; and ensure at most one is enabled at any
time
20
Module Instantiation
21
Instantiating a Module
i_first
i_second
22
Instantiating a Module
output Y;
wire n1;
endmodule
23
Instantiating a Module
output Y;
wire n1;
endmodule
24
Instantiating a Module
output Y;
wire n1;
endmodule
25
Instantiating a Module
output Y;
wire n1;
26
Instantiating a Module
output Y;
wire n1;
// alternative
small i_first ( A, SEL, n1 );
/* Shorter instantiation,
pin order very important */ module small (A, B, Y);
input A;
// any pin order, safer choice input B;
small i_second ( .B(C), output Y;
.Y(Y),
.A(n1) ); // description of small
endmodule endmodule
28
Writing More Reusable Verilog Code
• We have a module that can compare two 4-bit numbers
29
Parameterized Modules
module mux2
#(parameter width = 8) // name and default value
(input [width-1:0] d0, d1,
input s,
output [width-1:0] y);
assign y = s ? d1 : d0;
endmodule
30
Instantiating Parameterized Modules
module mux2
#(parameter width = 8) // name and default value
(input [width-1:0] d0, d1,
input s,
output [width-1:0] y);
assign y = s ? d1 : d0;
endmodule
31