lecture3-verilog
lecture3-verilog
reg q;
wire w;
reg a;
inner module
reg b; +
wire w;
Vcc
logic-1
logical Output
Output
AND
logic-0
Vdd
Driving Levels
In reality, a driver has different strengths
Vcc
logic-1
logical Output
Output
AND
logic-0
Vdd
Driving Levels
A driving level enables to simulate the electrical effect
of different driving strengths
Can simulate the effect of signal contention
Can implement wired-AND and wired-OR
Can simulate tri-state buses
Can simulate large gates and small gates
It's a '0'
It's a '1'
It's a '1'
module name(portlist);
port declarations;
parameter declarations;
wire declarations;
reg declarations;
variable declarations;
module instantations;
dataflow statements;
always blocks;
initial blocks;
endmodule
Modules
module name(portlist);
port declarations; - Direction of ports
parameter declarations; - Module variants
(module muffin;
wire declarations; parameter banana_nut;)
reg declarations;
variable declarations;
module instantations;
dataflow statements;
always blocks;
initial blocks;
endmodule
Modules
module name(portlist);
port declarations;
parameter declarations;
module instantations;
dataflow statements; within the confines
always blocks; of this module and this
initial blocks; level of the design hierarchy
endmodule
Modules
module name(portlist);
port declarations;
parameter declarations;
wire declarations;
reg declarations;
variable declarations;
endmodule
Example - 4-bit counter in Verilog
module tflopcounter(q, clk, reset);
output [3:0] q;
input clk, reset;
always
#5 clk = ~clk;
initial
begin 3 initial blocks
reset = 1'b1;
#15 reset = 1'b0; 1 always block
#180 reset = 1'b1;
#10 reset = 1'b0;
#20 $finish;
end
initial
$monitor($time, " Output q = %d", q);
endmodule
Port/Parameter Declarations
module name(portlist);
port declarations;
parameter declarations;
wire declarations;
reg declarations;
variable declarations;
module instantations;
dataflow statements;
always blocks;
initial blocks;
endmodule
Port List
endmodule
module arbiter(
8
output reg [7:0] q,
input wire a, a
inout wire b); arbiter b
q
endmodule
outer_module
A
8
A and q MAY be a reg
a B
arbiter b
q a, b, B, Q MUST be a wire
module arbiter(
output reg [7:0] q,
input wire a,
inout wire b);
endmodule
Module Parameters
Define
and use symbolic constants in the design of a
module
The number of bits in a variable
The propagation delay of a gate
Etc ..
module arbiter
#(parameter WP = 8)
(output reg [WP-1:0] q,
input wire a,
inout wire b);
endmodule
Module Instantation and Port Matching
module outer_module;
wire [7:0] q;
wire a, b;
module outer_module;
wire [7:0] Q;
wire A, B;
module name(portlist);
port declarations;
parameter declarations;
wire declarations;
reg declarations;
variable declarations;
module instantations;
dataflow statements;
always blocks;
initial blocks;
endmodule
Initial and Always Block Statement
module initalways;
reg a, b;
endmodule;
Verilog Dataflow Modeling
Today's topic
Dataflow Modeling
module
output output
a
q
b
Behavioral Behavioral
Structural
Procedural Dataflow
module nand(q, a, b) module nand(q, a, b) module nand(q, a, b)
output q; output q; output q;
input a, b; reg q; input a, b;
input a, b;
wire n; assign q = ~(a | b);
always @(a or b)
and G1(n, a, b); q = ~(a | b); endmodule
not G2(q, n);
endmodule endmodule
Key differences with procedural code
Must assign nets (wires) instead of registers
Behavioral - Procedural Behavioral - Dataflow
assign reg assign wire
endmodule
Continuous
Procedural Assignment
Assignment
Key differences with procedural code
Must assign nets (wires) instead of registers
Behavioral - Procedural Behavioral - Dataflow
assign reg assign wire
endmodule
Any change in
Any change in
a or b will cause
a or b will cause
the expression
the always block
to re-evaluate
to re-execute
The procedural assignment evaluates
as a result of always block execution
Key differences with procedural code
Must assign nets (wires) instead of registers
Behavioral - Procedural Behavioral - Dataflow
assign reg assign wire
endmodule
endmodule
endmodule
reg a, b, c;
Useful to partition results wire c0, d;
from an expression: assign {c0, d} = a + b + c;
Verilog Operators
Operand Types
assign {c0, d} = a + b;
nets (wire)
nets (wire) variable (reg)
parameters
numbers
function call
Operand Bit-select and Part-select
wire [7:0] n;
n[3:0] Bit 3, 2, 1, 0
n[3] Bit 3
n[x] X
n[3 +:2] Bit 3, 4
Operand Memory, Memory Indexing
n[15] Element 16
n[15][3] Bit 3 of Element 16
n[15][0:3] Bits 0:3 of Element 16
n[15:0] Illegal
Operand Types
• net
• net bit-select [n] or part-select [n:m]
'net' can be • indexed net
• indexed net bit-select or part-select
• concatenated net
Bitwise
We will only discuss
Verilog-specific
Reduction
Operators
Logical
Relational
Shift
Selection
wire [7:0] d;
d wire [7:0] next_r;
reg [7:0] r;
0 1 c
next_r wire c;
assign Y = (S == 0) ? D0 :
(S == 1) ? D0 :
(S == 2) ? D1 : D2;
endmodule
Example 1
Write a 3-to-1 multiplexer S Y
Inputs D0, D1, D2 (4-bit) 0 D0
Select input S (2-bit) 1 D0
Output Y (4-bit) 2 D1
3 D2
endmodule
Example 2
Develop an 8-bit * 8-bit multiplier with 8-bit output,
reflecting the 8 msb of the multiplication
Example 2
Develop an 8-bit * 8-bit multiplier with 8-bit output,
reflecting the 8 msb of the multiplication
endmodule
Example 3
Develop a rounding module with 8-bit input and 4-bit
output. The 4-bit input captures the rounded output of
the 8 input bits. That is, if the 4 lsb of the input are
bigger than 0111, then the output should be
incremented.
Example 3
Develop a rounding module with 8-bit input and 4-bit
output. The 4-bit input captures the rounded output of
the 8 input bits. That is, if the 4 lsb of the input are
bigger than 0111, then the output should be
incremented.
wire [7:0] A;
assign A = D + 4'b1000;
assign Y = A[7:4];
endmodule
Example 4
Develop a multiply-accumulate module with 40-bit
accumulator and 2x16-bit inputs. The accumulator
register is triggered on pos clock edge and has a
negative asynchronous reset.
Example 4
Develop a multiply-accumulate module with 40-bit
accumulator and 2x16-bit inputs. The accumulator
register is triggered on pos clock edge and has a
negative asynchronous reset.
module mac(output reg [39:0] Y,
input clk, rst,
input [15:0] D0, D1);
endmodule
Example 5
Design a programmable shifter that will shift an 8-bit
word over 0 .. 3 positions to the right, inserting zeroes
at the msb side
Example 5
Design a programmable shifter that will shift an 8-bit
word over 0 .. 3 positions to the right, inserting zeroes
at the msb side
endmodule
Example 5
Design a programmable shifter that will shift an 8-bit
word over 0 .. 3 positions to the right, inserting zeroes
at the msb side
endmodule
Example 5
Design a programmable shifter that will shift an 8-bit
word over 0 .. 3 positions to the right, inserting zeroes
at the msb side
endmodule
Verilog Multiplexed Datapaths
Multiplexed Datapaths - Disclaimer
We will cover very specific Verilog modeling guidelines
to build hardware modules called 'multiplexed
datapaths'
Not the only way to build hardware in Verilog
We will see other 'design methods' later
But,
if you use the 'multiplexed datapath' method, you
MUST stick to the following guidelines
What is a multiplexed datapath ?
A module with a single clk and rst input
Zero or more inputs, one or more outputs
Edge-triggered flip-flops
Outputs depend only on registers
input input input
edge-triggered
flipflops
output output
Why single-clock ?
A module with a single clk and rst input
Zero or more inputs, one or more outputs
Edge-triggered flip-flops
Outputs depend only on registers
Dataflow
Dataflow Expressions Datapath State Expressions
Outputs
Inputs
Next
State
Previous State
How to model an edge-triggered register
module a_module(output wire q,
input wire rst,
input wire clk,
input wire d);
wire next_r1;
d
reg r1;
assign next_r1 = d; q
assign q = r1;
endmodule
How to model an edge-triggered register
module a_module(output wire q,
input wire rst,
input wire clk,
input wire d);
wire next_r1;
d
reg r1;
assign next_r1 = d; q
assign q = next_r1;
endmodule No!
How to model two edge-triggered registers
module a_module(output wire q,
input wire rst,
input wire clk,
d
input wire d);
wire next_r1, next_r2;
reg r1, r2;
next_r1
always @(posedge clk or negedge rst)rst
if (rst) begin clk
r1
r1 = next_r1;
next_r2
r2 = next_r2;
end else begin
r1 = 0; r2
r2 = 0;
end q
assign next_r1 = d;
assign next_r2 = r1;
assign q = r2;
endmodule
How to model two edge-triggered registers
module a_module(output wire q,
input wire rst,
input wire clk,
d
input wire d);
wire next_r1, next_r2;
reg r1, r2;
next_r1
always @(posedge clk or negedge rst)
if (rst) begin r1
r1 = next_r1;
next_r2
r2 = next_r2;
end else begin
r1 = 0; r2
r2 = 0;
end q
assign next_r1 = d;
assign next_r2 = r1; Will leave out drawing of clk net
assign q = r2; and rst net from now
endmodule
Add two inputs, capture in a register
d1 d2
a two-bit output
next_r1
How to get the carry bit ?
a two-bit output
next_r1
wire next_r1;
reg r1;
wire dummy;
clk
c
q d0 d1 d2
Muxed registers are key to Muxed Datapath
Overall Idea
in1 in2 in3 in1 in2 in3
c1
*
c2
*
* +
c3
*
+
q
c1
*
c2
*
* +
c3
*
+
q
Area Time
Time (3 mult) AREA (3 cycles)
Area
(1 cycle) TIME
(1 mult)
TRADEOFF
Let's try this
in1 in2 in3 module vecmul(q, rst, clk, in1, in2, in3);
output [7:0] q;
input rst, rst;
c1 input [7:0] in1, in2, in3;
* parameter c1 = 8'd2;
c2 parameter c2 = 8'd4;
parameter c3 = 8'd6;
*
c3 wire next_r1;
* reg r1;
assign aq = a2 + a1;
assign a1 = m1 * m2;
endmodule
q
Add muxes on m1, m2. Add control input.
module vecmul(q, rst, clk, in1, in2, in3, step);
output [7:0] q;
in1 in2 in3 input rst, rst;
input [7:0] in1, in2, in3;
c1 input [1:0] step;
c2
c3 parameter c1 = 8'd20;
parameter c2 = 8'd40;
parameter c3 = 8'd60;
step
m1 m2 wire next_r1;
reg r1;
assign aq = a2 + a1;
assign a1 = m1 * m2;
assign m1 = (step == 0) ? c1 :
(step == 1) ? c2 : c3;
q assign m2 = (step == 0) ? in1 :
(step == 1) ? in2 : in3;
assign a2 = (step == 0) ? 0 : aq_reg;
endmodule
System Interconnect.
module vecmul(q, rst, clk, in1, in2, in3, step);
output [7:0] q;
input rst, rst;
in1 in2 in3 input [7:0] in1, in2, in3;
c1 input [1:0] step;
c2 parameter c1 = 8'd20;
c3 parameter c2 = 8'd40;
parameter c3 = 8'd60;
step
0 wire next_r1;
m1 m2
reg r1;
assign aq = a2 + a1;
assign a1 = m1 * m2;
assign m1 = (step == 0) ? c1 :
(step == 1) ? c2 : c3;
q assign m2 = (step == 0) ? in1 :
(step == 1) ? in2 : in3;
assign a2 = (step == 0) ? 0 : aq_reg;
assign q = aq_reg;
endmodule
Simple Optimizations
module vecmul(q, rst, clk, in, step);
output [7:0] q;
input rst, rst;
in step input [7:0] in;
c1 input [1:0] step;
c2 parameter c1 = 8'd20;
c3 parameter c2 = 8'd40;
parameter c3 = 8'd60;
0 wire next_r1;
m1
reg r1;
assign aq = a2 + a1;
assign a1 = m1 * in;
assign m1 = (step == 0) ? c1 :
(step == 1) ? c2 : c3;
q assign a2 = (step == 0) ? 0 : aq_reg;
assign q = aq_reg;
endmodule
Input is now sequential: in1, in2, in3 can share an input port
Result of 'Muxed Datapath'
c1
*
c2
*
+ c3
*
cycle 1 Introduce Register
+
to transport this signal
to the next clock cycle
cycle 2 +
cycle 3
q
Verilog FSM-based Control
Finite State Machine Template
Sequential Machine defined by
Set of Inputs and Outputs
Set of States
Initial State (reset function)
State Transitions
state register
next-state output outputs
inputs function function
Designer Creates
State Transition Graph
Extract Choose
State Transition Table State Encoding
Designer Creates
State Transition Graph
Extract Choose
State Transition Table State Encoding
Designer Creates
State Transition Graph
Extract Choose
State Transition Table State Encoding
Extract Choose
State Transition Table State Encoding
Input 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1
Output 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1
Example FSM Design
How many states?
Input 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1
Output 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1
Example FSM Design
How many states?
Input 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1
Output 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1
current state
x ( = I have not seen a '1')
Input 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1
Output 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1
S0
S1
S2
Example FSM Design
How many states?
Input 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1
Output 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1
S0 0
0 1 output
S0 -> 0
S1 S1 -> 0
input 0
S2 -> 1
1
S2
1
Verilog Mapping of FSM - Generic Ideas
Use 'parameter' to express state encoding
parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10;
state register
next-state output outputs
inputs function function
Example FSM Design - Verilog Mapping II
reg q;
reg [1:0] state;
parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; S0 0
always @(posedge clk or posedge rst)
if (rst) 0
state <= s0; 1
else
case (state) S1
s0: if (i == 1'b1) 0
state <= s1;
else 1
state <= s0;
s1: ..
S2
endcase
1
always @(state)
q = 1'b0; // default assignment
case (state) output
s0: q = 1'b0; S0 -> 0
s1: q = 1'b0; S1 -> 0
s2: q = 1'b1;
endcase
S2 -> 1
endmodule
Example FSM Design - Verilog Mapping II
Two always block
Next-state logic and state update in same block
Output can be combinational
state register
next-state output outputs
inputs function function
Example FSM Design - Verilog Mapping III
reg q;
reg [1:0] state, next_state;
parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10;
S0 0
always @(posedge clk or posedge rst)
if (rst)
state <= s0; 0 1
else
state <= next_state;
S1
0
always @(state or i) begin
// next state encoding 1
next_state = s0;
case (state)
s0: if (i == 1'b1) S2
next_state = s1; 1
else
next_state = s0;
s1: .. output
endcase S0 -> 0
end
S1 -> 0
always @(state) S2 -> 1
// output encoding
endmodule
Example FSM Design - Verilog Mapping III
Three always block
Next-state logic and state update in same block
Output can be combinational
state register
next-state output outputs
inputs function function
One, two, three always block
Combinational
Output √ √
State Update
separate from √
next-state Logic