04 - Verilog Codes For Basic Components (2021f)
04 - Verilog Codes For Basic Components (2021f)
1
Outlines
• Combinational Logic
– multiplexers, de-multiplexers
– encoder, decoder, priority encoder
– ripple carry adder
– logical and arithmetic shifter
– shift registers
– ALU
– ROM, RAM
• Sequential Logic
– latch, asynchronous reset latch
– flip-flop, asynchronosu/synchronous reset flip-flop
– register file
– pipeline
– finite state machine (FSM)
– clock gating 2
Combinational Logic
3
multiplexer (1/2)
4
multiplexer (2/2)
// MUX3 with one-hot select // MUX3 with binary select
module MUX3_onehot (a2, a1, a0, s, module MUX3_onehot (a2, a1, a0,
b); s, b);
parameter k = 32; parameter k = 32;
input [k-1:0] a0, a1, a2;
input [k-1:0] a0, a1, a2; input [1:0] s; // binary select
input [2:0] s; // one-hot select output [k-1:0] b;
output [k-1:0] b; reg [k-1:0] b;
reg [k-1:0] b;
always @ (*) begin
case (s)
always @ (*) begin 0: b = a0;
case (s) 1: b = a1;
3’b001: b = a0; 2: b = a2;
3’b010: b = a1; default: b = {k{1’bx}};
3’b100: b = a2; endcase
end
default: b = {k{1’bx}}; endmodule
endcase
end
5
endmodule
demultiplexer
module demux1_4 (
output reg out0, out1, out2, out3;
input in,
input [1:0] s );
6
encoder
module encoder (
input i3, i2, i1, i0,
output reg [1:0] out);
endmodule
7
priority encoder // 8:3 priority encoder
module priority_enc83 (r, b);
module priority_enc42 input [7:0] r;
(input i3, i2, i1, i0, output [2:0] b;
output reg [1:0] out;) reg [2:0] g;
module decoder (
input [1:0] in;
output reg out0, out1, out2, out3) ;
always @(in)
case (in)
2’b00: begin out0=1; out1=0; out2=0; out3=0; end
2’b01: begin out0=0; out1=1; out2=0; out3=0; end
2’b10: begin out0=0; out1=0; out2=1; out3=0; end
2’d11: begin out0=0; out1=0; out2=0; out3=1; end
endcase
endmodule
9
decoder (2/3)
endmodule endmodule
10
decoder (3/3)
// n-to-m decoder
module Dec (a, b);
parameter n=3;
parameter m=8;
input [n-1: 0] a;
output [m-1:0] b;
assign b = 1 << a;
endmodule
11
ripple carry adder (1/2)
module ripple_adder(co, sum, a0, a1, ci); module ripple_adder(co, sum, a0, a1, ci);
parameter N=4; parameter N=4;
output [N-1:0] sum; output [N-1:0] sum;
output co; output co;
input [N-1:0] a0, a1; input [N-1:0] a0, a1;
input ci; input ci;
wire [N-1:0] carry;
wire [N-1:0] p = a0 ^ a1;
assign carry[0]=ci; wire [N-1:0] g = a0 & a1;
wire [N:0] carry = {g | ( p & carry[N-1:0] ), ci};
genvar i; // carry[0] = ci;
generate // carry[1] = g[0] | ( p[0] & carry[0] );
for (i=0; i<N; i=i+1) // carry[2] = g[1] | ( p[1] & carry[1] );
begin: r_loop // carry[3] = g[2] | ( p[2] & carry[2] );
wire t1, t2, t3; // carry[4] = g[3] | ( p[3] & carry[3] );
xor g1(t1, a0[i], a1[i]);
assign sum = p ^ carry[N-1:0];
xor g2(sum[i], t1, carry[i]); assign co = carry [N];
and g3(t2, a0[i], a1[i]);
and g4(t3, t1, carry[i]); endmodule+
or g5(carry[i+1], t2, t3);
end
endgenerate
assign co = carry[N]
12
endmodule
Ripple Carry Adder (2/2)
endmodule 13
endmodule
left/right shifter
module shifter (
input left,
input [2:0] shift_amt,
input [7:0] data_in,
output [7:0] shifted_data);
endmodule
14
wrap-around barrel shifter
15
arithmetic shift (<<<, >>>) in Verilog-2001
// Verilog-2001
integer data_value,
data_value_1995,
data_value_2001;
…
data_value = -9; // stored as 1111_…_1111_0111
…
data_value_1995 = data_value >> 3; // stored as 0001_..._1111_1110
…
data_value_2001 = data_value >>> 3; // stored as 1111_...._1111_1110
// repeat sign bit during arithmetic right shift
…
data_value_1995 = data_value << 3; // stored as 1111_..._1011_1000
…
data_value_2001 = data_value <<< 3; // stored as 1111_..._1011_1000
// repeat sign bit during arithmetic left shift
// give an example where the logical left shift << 3 is different from arithmetic shift
// e.g., data_value2 = 32’b 1110_xxx ….
16
shift register with parallel load
17
left/right/load shift register
module LRL_Shift_Register(clk, rst, left,
right, load, sin, in, out) ;
parameter n = 4 ;
input clk, rst, left, right, load, sin ;
input [n-1:0] in ;
output [n-1:0] out ;
reg [n-1:0] next ;
always @ (ALU_ctl, a, b)
case (ALU_ctl)
4’d0 : ALU_out = a & b;
4’d1 : ALU_out = a | b;
4’d 2 : ALU_out = a + b;
4’d6 : ALU_out = a – b;
4’d 7: ALU_out = (a < b) ? 1 : 0;
4’d12: ALU_out = ~(a | b);
default: ALU_out = 32’b0;
endcase
endmodule
19
Synthesis of ROM/RAM
• If synthesis tool supports pragmas to control the structure of the
synthesized netlist or to give direction to the synthesis tool, synthesis
attributes shall be used
– The first attribute within the attribute instance shall be synthesis
followed by a comma separated list of synthesis-related attributes.
20
Ref.: IEEE Std. 1364.1 Verilog Register Transfer Level Synthesis
module rom_case (a, d) ;
input [3:0] a;
Two Implementations of ROM
output [7:0] d; (using case, or using input text file)
reg [7:0] d; module rom_reg (a, d) ;
always@(*) parameter b =8;
begin
parameter w = 4;
case(a)
4'h0: d=8'h00; parameter fileName = "datafile";
4'h1: d=8'h11; input [w-1:0] a;
4'h2: d=8'h22; output [b-1:0] d;
4'h3: d=8'h33; reg [b-1:0] memory [2**w-1:0] ;
4'h4: d=8'h44; initial
4'h5: d=8'h12; begin
4'h6: d=8'h34; $readmemb (fileName, memory);
4'h7: d=8'h56;
end
4'h8: d=8'h78;
4'h9: d=8'h9a; assign d = memory[a];
4'ha: d=8'hbc; endmodule
4'hb: d=8'hde;
// content of file “datafile” memory[0]=xxxxxxxx
4'hc: d=8'hf0;
// address in hexadecimal memory[1]=xxxxxxxx
4'hd: d=8'h12;
@002 memory[2]=11111111
4'he: d=8'h34;
11111111 01010101 memory[3]=01010101
4'hf: d=8'h56;
00000000 10101010 memory[4]=00000000
default: d=8'h0;
@006 memory[5]=10101010
endcase
1111zzzz 00001111 memory[6]=1111zzzz
end
… memory[7]=00001111 21
endmodule
…
ROM
• Three types of ROM models
– one-dimensional array with data in case
statement
– two-dimensional array with data in initial
statement
– two-dimensional array with data in text file
23
ROM: 2D array using initial
• initial statement shall be supported when synthesis
attributes logic_block or rom_block is used
• without specifying attributes, a synthesis tool may opt to
implement either as random combintional logic or as a
ROM
24
ROM: 2D array with data in text file
• use $readmemb to read ROM data from a text file
25
RAM
26
Edge-sensitive RAM
• RAM shall be modeled with synthesis attribute ram_block
• If latch or register logic is desired instead of a RAM, use the
attribute logic_block instead of ram_block
• RAM could be edge- or level-sensitive
• e.g. we (write-enable) control signal is synchronized with clock
signal
27
level-sensitive RAM
• we (write-enable) is the level-sensitive control signal
– not clock signal is used
28
dual-port (one-read, one-write) RAM
• check definitions of two-port or dual-port in document
– 1R/1W, 2R, or 2W, or
– 1R/1W only module RAM(ra, wa, write, din, dout) ;
parameter b = 32;
parameter w = 4;
always@(*) begin
if(write == 1)
ram[wa] = din; // one write port
end
endmodule 29
RAM with bidirectional data bus
endmodule
30
RAM: 2D array using always
• in general, standard cell library vendors or FPGA
synthesis tools usually provide automatic generator
(compiler) of memory, such as RAM, ROM, register
file which are usually custom designs
31
Sequential Logic
32
Latches and Flip-Flops
33
register inference
• D latch
– with asynchronous set
– with asynchronous reset
– with asynchronous set/reset
• D flip flop
– with asynchronous set
– with asynchronous reset
– with asynchronous set/reset
– with synchronous set
– with synchronous reset
– with synchronous and asynchronous load
34
D Latches in TSMC 0.18um Cell Library
TLAT
active-high
TLATN
active-low
TLATNR
asynchronous
active-low
reset
TLATNSR
asynchronous
active-low
35
set/reset
DFFs in TSMC 0.18um Cell Library
DFF
DFFR
asynchronous
active-low
DFFTR
synchronous
active-low
DFFSR
asynchronous
active-low
36
set/reset
inferring simple D-latch
module d_CL (
input gate, data,
output reg q );
endmodule
37
D Latch Circuit using CMOS
• constructed from back-to-back inverters
φ Q VDD
X
D
φ A Y
φ
GND
φ
A Y 38
D-latch with asynchronous set (active-low)
39
D-latch with asynchronous reset (active-low)
• sensitive list includes control signals and input
data signals
40
D-latch with asynchronous set and reset
(active-low)
41
latch with asynchronous set and reset
module d_latch_async (
input gate, data, reset_n, set_n;
output reg q;
endmodule
42
inferring D-flipflop
43
Logic of D Flip-Flop
• composed of master and slave D-latches
• triggered at the time instance of clock edge
– unlike latch which is active (transparent) when clock is at
logic level of high
44
D Flip-Flop using CMOS
• constructed from two D latches (either dynamic or
static)
– cell library usually adopts static design
φ φ
X
Dynamic D Q
DFF
φ φ
φ φ Q
Static X
D Q
DFF φ φ
φ φ
φ φ 45
DFF with asynchronous set (active-low)
• sensitive list includes edges of both clock and
control signals
– synthesized into hardware different from normal DFF
46
DFF with asynchronous reset (active-high)
47
DFF with asynchronous set and reset (active-high)
48
DFF with asynchronous set and reset
module dff_async (
input clk, reset, set, data,
output reg q);
endmodule
49
DFF with synchronous set (active-high)
• sensitive list includes only clock signal
– synthesized into normal DFF with extra combinational
logic for the input data
50
DFF with synchronous reset (active-low)
51
DFF with synchronous and asynchronous load
52
Resettable DFF Circuits using CMOS
φ φ
Symbol
Latch
Flop
D Q D Q
reset reset
Synchronous Reset
φ Q φ φ Q
reset reset
Q
D D
φ φ
φ
φ φ φ
φ φ
φ
Q
Q φ
Asynchronous Reset
φ φ
reset
reset
D
D φ
φ φ
φ
φ φ
reset
reset
φ
φ
φ 53
register file
• register file is compose of registers
• can access many registers at the same time
• cp. single-port RAM which can access only one data
module RegisterFile (r_adr1, r_adr2, data1, data2, w_adr, w_data, w_cntl, clk)
input [5:0] r_adr1, r_adr2, w_adr;
input [31:0] w_data;
input w_cntl, clk;
output [31:0] data1, data2;
reg [31:0] RF [0:31];
endmodule
54
Two types of Sequential Logic
• Pipeline
– break long datapath into several stages, each realized in
one clock cycle
– use pipelined registers to cut the combinational logic into
several stages
– better pipelining with balanced delay for each pipeline
stage
• Finite State Machine (FSM)
– generate control signals for datapath components
– use registers to store the states
– output depends on either the current states, and/or the
current inputs
55
Pipeline
module pipelined (
input [31:0] A, B, C;
input clk;
output [63:0] out);
reg [63:0] C_pipe1;
reg [63:0] out_pipe1, out_pipe2;
always @ (posedge clk) begin
out_pipe1 <= A*B;
C_pipe1 <= C;
end
always @(posedge clk)
out_pipe2 <= out_pipe1 + C_pipe1;
assign out = out_pipe2;
endmodule
56
Moore vs. Mealy FSM
• Moore FSM
– output is a function of only present state
• Mealy FSM
– output is a function both present state and input
57
FSM (Finite State Machine)
// lower section of FSM
always @ (posedge clk, posedge rst) begin
if (rst == 1) pr_state <= … ;
else pr_state <= nx_state;
// out <= temp_out; // for stored output
end
58
Example of FSM: Vendor Machine
• Newspaper vendor machine
60
FSM Example: Vendor Machine (2/7)
generate output/next states
(combinational logic in upper FSM)
// combinational logic
function [2:0] fsm;
input [1:0] fsm_coin;
input [1:0] fsm_PRES_STATE;
reg fsm_newspaper;
reg [1:0] fsm_NEXT_STATE;
begin
case (fsm_PRES_STATE)
61
FSM Example: Vendor Machine (3/7)
generate output and next state for each
present state
s0: // state = s0
begin
if (fsm_coin == 2’b10) begin
fsm_newspaper = 1’b0;
fsm_NEXT_STATE = s10; end
else if (fsm_coint == 2’b01) begin
fsm_newspaper = 1’b0;
fsm_NEXT_STATE = s5; end
else begin
fsm_newspaper = 1’b0;
fsm_NEXT_STATE = s0; end
end
62
FSM Example: Vendor Machine (4/7)
generate output and next state for each
present state
s5: // state = s5
begin
if (fsm_coin == 2’b10) begin
fsm_newspaper = 1’b0;
fsm_NEXT_STATE = s15; end
else if (fsm_coint == 2’b01) begin
fsm_newspaper = 1’b0;
fsm_NEXT_STATE = s10; end
else begin
fsm_newspaper = 1’b0;
fsm_NEXT_STATE = s5; end
end
63
FSM Example: Vendor Machine (5/7)
generate output and next state for each
present state
endcase
end
endfunction
65
FSM Example: Vendor Machine (7/7)
store next state
(sequential logic in lower FSM)
// reevaluate combinational logic each time a coin is put or
// the present state changes
assign {newspaper, NEXT_STATE} = fsm(coin, PRES_STATE);
// clock the state FFs using synchronous reset
always @(posedge clock)
begin
if (reset == 1’b)
PRES_STSTE <= s0;
else
PRES_STATE <= NEXT_STSTE;
end
endmodule
66
Stimulus for the FSM Vendor Machine
module stimulus; // put 3 nickles
reg clock, reset #80 coin=1; #40 coin=0;
reg [1:0] coin; #80 coin=1; #40 coin=0;
wire newspaper; #80 coin=1; #40 coin=0;
vend vendY(coin, clcok, reset, // put 1 nickle and 1 dime
newspaper); #180 coin=1; #40 coin=0;
always #80 coint=2; #40 coin=0;
#20 clcok = ~clock; // put 2 dimes
#180 coin=2; #40 coin=0;
initial #80 coint=2; #40 coin=0;
begin // put 1 dime and 1 nickle
clock = 0; #180 coin=2; #40 coin=0;
coin = 0; #80 coint=1; #40 coint=0;
reset = 1;
#50 reset = 0; #80 $finish;
@(negedge clock); end
endmodule
67
Divide-by-3 counter using FSM
module divideby3FSM (input clk, input reset, output y);
reg [1:0] state, nextstate;
parameter S0 = 2’b00;
parameter S1 = 2’b01;
parameter S2 = 2’b10;
// state register
always @(posedge clk, posedge reset)
if (reset) state <= S0;
else state <= nextstate;
// output logic
assign y = (state == S0);
endmodule 68
Clock Gating
69
Register Output
• Regular register
– waste dynamic power when accessing unused data
D Q
clk reset
nrst
• Waveforms
CLK
• Clock-gated waveforms
– Controlled by ENCLK
CLK
ENCLK
gclk might have glitches !!! The clk portmight still have switching power!
cause unexpected latching not efficiently reduce dynamic power
72
Latch-based clock gating (Safe Design)
• Avoid glitches in clock signals
– Glitches incur unwanted signal edges
73
Clock gating methods
• Method 1 (unsafe gclk with possible glitches)
74
Synthesize lock gating with Synopsys
• use Synopsys synthesis script set_clock_gating_style
which automatically merge two methods into the safe clock-
gating design
en
clk
75
version 1 (AND gate at clock input)
(Manual Clock Gating)
module dff(Q, D, clk, set, rst);
input D, Clock, set, rst;
output Q;
reg gclk;
wire gclk, Enable;
// clock input is from the output of AND
assign gclk = Clock & Enable;
always @(posedge gclk)
if (rst)
Q <= 1’b0;
else if (set)
Q <= 1’b1;
else
Q <= D;
endmodule
76
version 2 (MUX at data input)
(Auto Clock Gating)
module dff(Q, D, clk, set, rst);
input D, CLK, set, rst;
output Q;
reg Q;
wire EN;
// data input from MUX controlled by en
always @(posedge CLK)
if (EN) begin
if (rst)
Q <= 1’b0;
else if (set)
Q <= 1’b1;
else
Q <= D;
end
endmodule
77
Synthesized Clock Gating
• When synthesis is done with proper commands
– Both design leads to safe clock gating circuit (with latch)
en
clk
78