06 Verilog Combinational
06 Verilog Combinational
http://safari.ethz.ch/ddca
1
Adapted from Digital Design and Computer Architecture, David Money Harris & Sarah L. Harris ©2007 Elsevier
Carnegie Mellon
¢ Structural Modeling
2
Carnegie Mellon
3
Carnegie Mellon
4
Carnegie Mellon
¢ It is not proprietary
§ HDLs are not tool specific
¢ It is machine readable
§ It is easier for computers to understand the circuit
5
Carnegie Mellon
6
Carnegie Mellon
Defining a module
¢ A module is the main building block in Verilog
a
Verilog
b y
Module
c
7
Carnegie Mellon
Defining a module
module example (a, b, c, y);
input a;
input b;
input c;
output y;
endmodule
a
Verilog
b y
Module
c
8
Carnegie Mellon
A question of style
The following two codes are identical
module test ( a, b, y ); module test ( input a,
input a; input b,
input b; output y );
output y;
endmodule
endmodule
9
Carnegie Mellon
¢ Example:
10
Carnegie Mellon
Basic Syntax
¢ Verilog is case sensitive:
§ SomeName and somename are not the same!
¢ Whitespace is ignored
// Single line comments start with a //
/* Multiline comments
are defined like this */
11
Carnegie Mellon
Good Practices
¢ Develop/use a consistent naming style
12
Carnegie Mellon
¢ Behavioral
§ The module body contains functional description of the circuit
§ Contains logical and mathematical operators
13
Carnegie Mellon
14
Carnegie Mellon
endmodule
15
Carnegie Mellon
endmodule
16
Carnegie Mellon
endmodule
17
Carnegie Mellon
// description of small
endmodule
endmodule
18
Carnegie Mellon
19
Carnegie Mellon
// 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 i2 ( .B(C), output Y;
.Y(Y),
.A(n1) ); // description of small
endmodule endmodule
20
Carnegie Mellon
¢ Simulation
§ Allows the behavior of the circuit to be verified without actually
manufacturing the circuit
§ Simulators can work on behavioral or gate-level schematics
21
Carnegie Mellon
endmodule
22
Carnegie Mellon
b
c y
un5_y
y
un8_y
23
Carnegie Mellon
24
Carnegie Mellon
Bitwise Operators
module gates(input [3:0] a, b,
output [3:0] y1, y2, y3, y4, y5);
endmodule
25
Carnegie Mellon
26
Carnegie Mellon
Reduction Operators
module and8(input [7:0] a,
output y);
assign y = &a;
endmodule
27
Carnegie Mellon
28
Carnegie Mellon
Conditional Assignment
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
29
Carnegie Mellon
30
Carnegie Mellon
endmodule
31
Carnegie Mellon
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
32
Carnegie Mellon
¢ (B) Base
§ Can be b (binary), h (hexadecimal), d (decimal), o (octal)
¢ (xx) Number
§ The value expressed in base, apart from numbers it can also have X
and Z as values.
§ Underscore _ can be used to improve readability
33
Carnegie Mellon
34
Carnegie Mellon
¢ Multiplexer functionality
§ If … then … else
35
Carnegie Mellon
36
Carnegie Mellon
endmodule endmodule
37
Carnegie Mellon
endmodule
38
Carnegie Mellon
endmodule
39
Carnegie Mellon
endmodule
40
Carnegie Mellon
endmodule
41
Carnegie Mellon
endmodule
42
Carnegie Mellon
endmodule
43
Carnegie Mellon
endmodule
44
Carnegie Mellon
45
Carnegie Mellon
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
46
Carnegie Mellon
assign y = s ? d1 : d0;
endmodule
Manipulating Bits
// You can assign partial busses
wire [15:0] longbus;
wire [7:0] shortbus;
assign shortbus = longbus[12:5];
// Concatenating is by {}
assign y = {a[2],a[1],a[0],a[0]};
48
Carnegie Mellon
Z floating output
module tristate(input [3:0] a,
input en,
output [3:0] y);
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]
49
Carnegie Mellon
A
&
0 1 Z X
0 0 0 0 0
1 0 1 X X
B
Z 0 X X X
X 0 X X X
50
Carnegie Mellon
‘timescale 1ns/1ps
module simple (input a, output z1, z2);
endmodule
51
Carnegie Mellon
Next Steps
¢ We have seen an overview of Verilog
52