06 Verilog Synth
06 Verilog Synth
POKL.04.01.02-00-209/11
assign Y =
(~SEL[1] & ~SEL[0] & I[0]) |
(~SEL[1] & SEL[0] & I[1]) |
( SEL[1] & ~SEL[0] & I[2]) |
( SEL[1] & SEL[0] & I[3]);
endmodule
Continues assignment - MUX 4 to 1
Conditional operator <sel>?<expr_1>:<expr_0>
assign Y = SEL[1] ?
(SEL[0] ? I[3] : I[2]) :
(SEL[0] ? I[1] : I[0]);
endmodule
Procedural assignment - MUX 4 to 1
Procedural assignment is triggered by all signals that are
arguments of expressions
module MUX(Y, SEL, I);
output Y;
reg Y; //Overwrite output type
input [1:0] SEL;
input [3:0] I;
initial
$monitor(“%t : %d : %b : %b”, $time, SEL, I, Y);
endmodule
Continues assignment - MUX 4 to 1
Parameterized size of multiplexed bus
module MUX(Y, SEL, I0, I1, I2, I3);
parameter W = 4;
output [W-1:0] Y;
input [1:0] SEL;
Input [W-1:0] I0, I1, I2, I3;
assign Y = SEL[1] ?
(SEL[0] ? I_3 : I_2) :
(SEL[0] ? I_1 : I_0);
endmodule
7 segment decoder
Complex function description
We do not know an analytic form of a function
We know a functional description – a tabular form
A case statement
Tabular function representation
endmodule
7 segment decoder – cont.
always @(DATA or BL)
if(BL) LED = 7'b0000000;
else
case(DATA)
4'b0000:LED = 7'b1111110; //0
4'b0001:LED = 7'b0110000;
4'b0010:LED = 7'b1101101;
4'b0011:LED = 7'b1111001;
4'b0100:LED = 7'b0110011;
4'b0101:LED = 7'b1011011;
4'b0110:LED = 7'b1011111;
4'b0111:LED = 7'b1110000;
4'b1000:LED = 7'b1111111;
4'b1001:LED = 7'b1111011; //9
4'b1100:LED = 7'b0111000; //L
4'b1101:LED = 7'b1100111; //P
4'b1110:LED = 7'b0000001; //Minus
default:LED = 7'b0000000; //Blank
endcase
endmodule
Parameterized full adder
Parameterized size of inputs A, B and result S
Concatenation used as LHS to split result into sum and carry
out
Result aware description – better synthesis
endmodule
Parameterized full adder - testbench
module ADD_TEST;
parameter W = 4;
reg [W-1:0] A,B;
wire [W-1:0] S;
wire Co;
integer i;
initial begin
A = {W{1’b0}}; B = {W{1’b0}};
for(i=0; i < w; i = i + 1) begin
repeat(4) {B[i],A[i]} = {B[i],A[i]} + 1;
end
end
initial
$monitor(“%t:\n %b\n %b\n%b:%b”,$time,A,B,Co,S);
endmodule
Parameterized ALU
module ALU(OP, A, B, Ci, Y, Co, Z);
parameter W = 4;
input [W-1:0] A,B;
input Ci;
input [1:0] OP;
output [W-1:0] Y; reg [W-1:0] Y;
output reg Co; //Carry out
output Z; //Zero result
ALU #(W)A1(.A(A),.B(B),.Ci(Ci),.Y(Y),.Co(Co),.Z(Z),.OP(OP));
initial begin
A = 16'd0; B = 16'd0; OP = 2'b00; Ci = 1'b0;
#10; A = 16'h05FA; B = 16'hF505;
repeat(4) begin //Cycle through all operations
#10 Ci = 1'b1; #10 Ci = 1'b0;
OP = OP + 1;
end
$finish;
end
endmodule
3-state buffer
3-state buffer enables creating bidirectional bus lines
CTRL signal when deasserted place output of the buffer in
high impedance state
Bus contention appears when more than one buffer is active
at the same time
module BUFT(Y, A, CTRL);
parameter W = 4;
input [W-1:0] A;
output [W-1:0] Y;
input CTRL;
endmodule
3-state buffer – testbench structure
`timescale 1ns/100ps
module BUFT_TEST;
parameter W = 8; //Bus width
wire [W-1:0] y; //Bus lines
reg [W-1:0] a1, a2; //Input signals
reg c1, c2; //control signals
initial begin
c1 = 1'b0; c2 = 1'b0;
a1 = {W{1'b0}}; a2 = {W{1'b0}}; task BUFT_DIAG;
#5; integer i;
assign c1 = CTRL; assign a1 = DATA; begin
BUFT_DIAG; CTRL = 1'b1;
deassign c1; deassign a1; DATA = {W{1'b0}};
#5; #5 DATA[0] = 1'b1;
assign c2 = CTRL; assign a2 = DATA; for(i=0; i < W; i = i + 1)
BUFT_DIAG; #5 DATA = DATA << 1;
deassign c2; deassign a2; CTRL = 1'b0;
$display("Bus contention test."); DATA[0] = 1'b1;
a1 = {W{1'b1}}; for(i=0; i < W; i = i + 1)
a2 = {W{1'b0}}; #5 DATA = DATA << 1;
#5 c1 = 1'b1; end
#5 c2 = 1'b1; endtask
#5 c1 = 1'b0;
#5 c2 = 1'b0;
#5 $finish;
end
3-state buffer – contention detection
integer i;
always @(y) begin
for(i=0; i < W; i= i + 1)
if(y[i] === 1'bx)
$display("Warning!!! At %t Bus contention detected on
Y[%d] line!", $time,i);
end
3-state buffer – simulation results
Sequential circuits
D type flip-flop
Triggered with rising/falling edge of clock signal
Synchronous set/reset signals
Asynchronous set/reset signals
Counters
Binary up counter
up/down counters
Counters cascade connection
Loadable counter
Finite State Machines
Single always block description
Dual always block description
Avoiding latches in description
sr-latch flip-flop
module NAND(Y,A,B);
output Y;
input A,B;
module SR_LATCH(Q,Qn,C,S,R);
assign Y = ~(A & B) output Q,Qn;
input C,S,R;
endmodule wire SG,RG;
NAND G_S(SG,C,S);
NAND G_R(SR,C,R);
module SR_FF(Q,Qn,Sn,Rn); SR_FF SR(Q,Qn,SG,RG);
output Q,Qn;
input Sn,Rn; endmodule
NAND G1(Q,Qn,Sn);
NAND G2(Qn,Q,Rn);
endmodule
Different D-type flip-flops
endmodule endmodule
D flip-flop with preset inputs
endmodule endmodule
Shift register
endmodule
Binary counter
module CNT(CLK, CLR, Q);
input CLK, CLR;
output [3:0] Q;
reg [3:0] Q;
endmodule
Binary Up-Down Counter
Binary up-down counter
CE - Clock enable input
CEO – Clock enable output
DIR – Counting direction input
Generation of CEO
CE dependant
Counting up – max value
Counting down – min value
module CNT(CLK, CLR, CE, DIR, Q, CEO);
parameter W = 8
input CLK, CLR, LD;
input [W-1:0] D;
output [W-1:0] Q;
reg [W-1:0] Q;
...
Binary Up-Down Counter
...
always @(posedge CLK or posedge CLR)
if(CLR)
Q <= {W{1’d0}}; //Reset
else begin
if(CE) begin
if(DIR)
Q <= Q + 1; //Up Counting
else
Q <= Q - 1; //Down Counting
end
end
endmodule
BCD counter
module CNT(CLK, CLR, CE, Q, CEO);
input CLK, CLR, CE;
output reg [3:0] Q;
output CEO;
endmodule
BCD counter – cascade connection
Connecting 3 instances of BCD counter into modulo 1000
BCD counter
Instances are linked by CE and CEO signals
//Input signals
wire CLK, CLR, CE;
//Internal Clock Enable signal
wire CE_U, CE_D, CE_H;
// Counter outputs
wire [3:0] Q_U, Q_D, Q_H;
endmodule
Digital Frequency Synthesis
Synthesis based on fractional generation of clock pulses
f = f0 (A/2n)
n – bit width of DDS accumulator
A – programmed value in range 1 – 2n-1
f0 – reference frequency
module DDS(CLK, CE, A, CEO);
parameter W = 16; //Accumulator width
input CLK, CE;
input [W-1:0] A;
output reg CEO;
reg [W-1:0] ACC;
STOP
M=0
KB_PLAY
KB_PAUSE
KB_STOP
KB_STOP
KB_PLAY
PAUSE PLAY
M=1 M=1
KB_PAUSE
@else @else
FSM implementation – state register
module TapeCTRL(CLK, CLR, KB_STOP, KB_PAUSE, KB_PLAY, M);
input CLK, CLR, KB_STOP, KB_PAUSE, KB_PLAY;
output M;
reg [1:0] Q, NextQ;
//State register
always @(posedge CLK or posedge CLR)
begin
if(CLR)
Q <= STOP;
else
Q <= NextQ;
end
FSM implementation – next state
//Next state function
always @(Q or KB_STOP or KB_PAUSE or KB_PLAY) begin
NextQ = Q; //Safe encoding -> @else
case(Q)
STOP : begin
if(KB_PAUSE) NextQ = PAUSE;
else if(KB_PLAY) NextQ = PLAY;
end
PAUSE: begin
if(KB_STOP) NextQ = STOP;
else if(KB_PLAY) NextQ = PLAY;
end
PLAY : begin
if(KB_STOP) NextQ = STOP;
else if(KB_PAUSE) NextQ = PAUSE;
end
endcase
end
...
FSM implementation – output
endmodule
Memory implementation
Declaration of synchronous memory (synthesizable)
Write data
Read data
module RAM(CLK, WE, A, D, Q);
parameter DW = 8; //Data word size
Parameter AW = 4; //Address word size
input CLK, WE /*Write Enable*/;
input [AW-1:0] A
input [DW-1:0] D;
output [DW-1:0] Q;
reg [W-1:0] MEM [(1 << AW)-1:0];
assign Q = MEM[A];
endmodule
Memory implementation
Dual pointer memory
FIFO, Multiport addressable register file, etc.
module RAM(CLK, WE, A1, A2, D, Q1, Q2);
parameter DW = 8; //Data word size
Parameter AW = 4; //Address word size
input CLK, WE /*Write Enable*/;
input [AW-1:0] A1, A2;
input [DW-1:0] D;
output [DW-1:0] Q1, Q2;
reg [W-1:0] MEM [(1 << AW)-1:0];
assign Q1 = MEM[A1];
assign Q2 = MEM[A2];
endmodule
Block RAM – specific part
module RAMB(CLK, RST, EN, WE, ADDR, D, Q);
output reg [7:0] Q;
input [11:0] ADDR;
input [7:0] D;
input EN, CLK, WE, RST;
reg [7:0] MEM [4095:0];
always@(posedge CLK)
if(EN)
if(RST == 1)
Q <= 1'b0;
else begin
if(WE == 1) Q <= D;
else Q <= MEM[ADDR];
end
endmodule
Program mentorski receptą na efektywne kształcenie na makrokierunku
automatyka i robotyka, elektronika i telekomunikacja, informatyka
na Politechnice Śląskiej
Mentoring program - a recipe for efficient education at the Macrocourse on Automatic Control and Robotics, Electronics and
Telecommunication, and Computer Science offered by the Silesian University of Technology
POKL.04.01.02-00-209/11