Verilog For Sequential Circuits: Design of Digital Circuits 2014 Srdjan Capkun Frank K. Gürkaynak
Verilog For Sequential Circuits: Design of Digital Circuits 2014 Srdjan Capkun Frank K. Gürkaynak
http://www.syssec.ethz.ch/education/Digitaltechnik_14
1
Adapted from Digital Design and Computer Architecture, David Money Harris & Sarah L. Harris 2007 Elsevier
Carnegie Mellon
2
Carnegie Mellon
a
Verilog
b y
Module
c
3
Carnegie Mellon
a
Verilog
b y
Module
c
endmodule
4
Carnegie Mellon
5
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
6
Carnegie Mellon
endmodule
7
Carnegie Mellon
assign y = s ? d1 : d0;
// if (s) then y=d1 else y=d0;
endmodule
8
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
9
Carnegie Mellon
10
Carnegie Mellon
11
Carnegie Mellon
12
Carnegie Mellon
13
Carnegie Mellon
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
14
Carnegie Mellon
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
The posedge defines a rising edge (transition from 0 to 1).
15
Carnegie Mellon
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
assign statement is not used within always block
16
Carnegie Mellon
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
Assigned variables need to be declared as reg
The name reg does not necessarily mean that the value is
a register. (It could be, it does not have to be).
17
Carnegie Mellon
18
Carnegie Mellon
Example: D Latch
module latch (input clk,
input [3:0] d,
output reg [3:0] q);
always @ (clk, d)
if (clk) q <= d; // latch is transparent when
// clock is 1
endmodule
lat
[3:0] [3:0]
d[3:0] D[3:0] [3:0] [3:0]
Q[3:0] q[3:0]
clk C
q[3:0]
23
Carnegie Mellon
24
Carnegie Mellon
27
Carnegie Mellon
28
Carnegie Mellon
endmodule
29
Carnegie Mellon
endmodule
31
Carnegie Mellon
endmodule
32
Carnegie Mellon
endmodule
33
Carnegie Mellon
34
Carnegie Mellon
36
Carnegie Mellon
37
Carnegie Mellon
At the end, s = 1
38
Carnegie Mellon
39
Carnegie Mellon
40
Carnegie Mellon
41
Carnegie Mellon
42
Carnegie Mellon
43
Carnegie Mellon
CLK
M next
next k state k state output N
inputs state outputs
logic
logic
44
Carnegie Mellon
S2
S0
S1
45
Carnegie Mellon
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
46
Carnegie Mellon
47
Carnegie Mellon
48
Carnegie Mellon
49
Carnegie Mellon
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
50
Carnegie Mellon
Always statement
Is needed for defining memorizing elements (flip-flops, latches)
Can also be used to define combinational circuits
Writing FSMs
Next state calculation
Determining outputs
State assignment
51