Synthesizable Verilog: Dr. Paul D. Franzon
Synthesizable Verilog: Dr. Paul D. Franzon
Synthesizable Verilog
Dr. Paul D. Franzon
Outline
1. Combinational Logic Examples.
2. Sequential Logic
3. Finite State Machines
4. Datapath Design
References
1. Smith and Franzon, Chapters 5, 8, Appendix A (on this web site)
2. M. Smith, Chapter 10, 11
3. D. Smith, Chapters 4, 6, 8.
The //synopsys full_case tells Synopsys that all alternatives have been specified,
thus preventing latches. It is an example of a Synopsys Directive. The ‘default’ and
pre-assignment works to prevent unintentional latches also.
…Design Steps
3. Write your Verilog:
l All register outputs are assigned within always@(posedge clock) blocks
u Also place register input logic within these blocks
u EVERY VARIABLE ASSIGNED WITHIN SUCH A BLOCK BECOMES THE
OUTPUT OF A FLIP-FLOP
l Combinational logic can be designed:
u As input logic to the registers, or,
u as its own Procedural block, or,
u using continuous assignment, or,
u structurally (see later)
4. Write your verification test fixture
l Make sure your verification is as complete as possible (see later section)
5. Synthesize your design
l Fix all Errors, understand or fix (as appropriate) all Warnings
l Make sure timing is met (‘slack (met)’)
6. Conduct post-synthesis verification
l Especially, timing and function
©2000, Dr. Paul D. Franzon, www.ece.ncsu.edu/erl/faculty/paulf.html 5
Priority Encoder
Truth Table: A2 A1 A0 Y1 Y0
0 0 0 0 0
0 0 1 F 0
0 1 X 0 F
1 X X 1 1
Capture in Verilog using if-then-else or a casex statement:
input [2:0] A;
input F;
reg [1:0] Y;
always@(A or F)
begin
if (A[2]) Y = 2’b11;
else if (A[1]) Y = {1’b0,F};
else if (A[0]) Y = {F,1’b0};
else Y = 2’b00;
end
…Priority Encoder
begin
casex (A) //synopsys full_case
3’b1xx : Y = 2’b11;
3’b01x : Y = {1’b0,F};
3’b001 : Y = {F,1’b0};
default : Y = 2’b00;
endcase
end
case and casex statements are much like the C ‘switch’ statement
l Only one alternative is selected though
casex statement allows some of the alternatives to contain ‘don’t cares’
Non-Priority Encoder
Truth Table: A2 A1 A0 Y1 Y0
all others 0 0
0 0 1 F 0
0 1 0 0 F
1 0 0 1 1
...Procedural Blocks
//synopsys parallel_case tells Synopsys we only ever expect one of the case
alternatives to be true, leading to non-priority logic which is usually smaller.
Q. Why do the examples on the previous page describe priority logic while the example on
this page describes non-priority logic?
Exercise
Implement the following logic: OP[1:0] Fu nct[4:0] Sel[1:0] B
01 x 11 0
11 xxx11 01 1
11 xxx01 10 1
integer i;
reg [7:0] A;
always@(A)
begin
OddParity = 1’b0;
for (i=0; i<=7; i=i+1)
if (A[i]) OddParity = ~OddParity;
end
Is the following code fragment synthesizable?
integer i, N;
parameter N=7;
reg [N:0] A;
always@(A)
begin
OddParity = 1’b0;
for (i=0; i<=N; i=i+1)
if (A[i]) OddParity = ~OddParity;
end
©2000, Dr. Paul D. Franzon, www.ece.ncsu.edu/erl/faculty/paulf.html 12
ECE 520 Class Notes
Notes:
l Multiplexor-based design is often smaller and faster than logicgate based design in
CMOS. Often structural code has to be used to force this implementation style.
l Explicit coding of width and msb of bc in final = codes[bc[1:0]];
2.
wire [7:0] mush;
tri [3:0] bus;
• A continuous assignment logic style must be used in Synthesis for tri-state logic.
Registers
Some Flip Flop Types:
reg Q0, Q1, Q2, Q3, Q4; Note:
// D Flip Flop Registers with asynchronous
always@(posedge clock) reset are smaller than
Q0 <= D;
// D Flip Flop with asynchronous reset
those with synchronous
always@(posedge clock or negedge reset)
if (!reset) Q1 <= 0;
reset
else Q1 <= D; + don’t need clock to reset
// D Flip Flop with synchronous reset BUT it is a good idea to
always@(posedge clock)
if (!reset) Q2 <= 0; sychronize reset at the block
else Q2 <= D;
level to reduce impact of noise.
// D Flip Flop with enable
always@(posedge clock)
if (enable) Q3 <= D;
// D Flip Flop with synchronous clear and preset
always@(posedge clock)
if (!clear) Q4 <= 0;
else if (!preset) Q4 <= 1;
else Q4 <= D;
… FSM Types
• State Vector Encoding
l Minimal encoding
u Minimum number of bits
l Minimum, sequential encoding
u Minimum number of bits and states in sequence
Ð Does not necessarily optimize ‘next state logic’ size
l Gray encoding
u state bit changes by only one bit between sequential states
Ð Minimizes switching activity in state vector register
l One-hot encoding
u one bit per state
Ð usually gives fastest ‘next state’ logic
Example: 7-state FSM, states S0 … S7:
… FSM Types
• Resets:
l Reset usually occurs only on power-up and when someone hits the ‘reset’ button
l Asynchronous reset:
u FSM goes to reset state whenever reset occurs
l Synchronous reset:
u FSM goes to reset state on the next clock edge after reset occurs
l Asynchronous reset leads to smaller flip-flops while synchronous reset is ‘safer’
(noise on the reset line is less likely to accidently cause a reset).
• Fail-Safe Behavior:
l If the FSM enters an ‘illegal’ state due to noise is it guaranteed to then enter a
legal state?
u ‘Yes’ is generally desirable
… FSM Types
• Sequential Next state or output logic
l Usually, these blocks are combinational logic only
l However, can place sequential logic (e.g. a counter, or a toggle-flip-flop) in these
blocks if it is advantageous
l AVOID DOING THIS AS MUCH AS YOU CAN UNLESS YOU ARE REALLY
SURE ABOUT WHAT YOU ARE DOING
u Sequential next state or output logic can get very confusing to design and debug
• Registered or Unregistered Outputs
l Do not register the outputs unless you need to ‘deglitch’ the outputs (for example, for
asynchronous handshaking - combinational logic has to be assumed to be glitchy) or
are pipelining the control
l e.g. Next Output
State Logic reg_out
Logic State
Reg.
State Vector
Final Exercise
Linear Feedback Shift Register
• Used to generate a pseudo-random sequence of numbers
l ‘pseudo-random’ because the number sequence cycles
• Next number obtained by ‘hashing’ the previous number with a bunch of XOR
gates
l With the right design, no number is ever repeated (up to 2N, where N = # bits)
Applications
• Random number generators
• Built In Self Test (BIST)
l Generates test vector sequence for production test of a block of logic
• Used in data encryption, checksum generation, and data compression techniques
Final Exercise
What is the following Verilog building? (D. Smith, Example 7.10)
module LFSR_8BIT (Clock, Reset, Y);
input Clock, Reset;
output [7:0] Y;
integer N [1:7];
parameter [7:0] Taps = 8’b10001110;
reg Bits0_6_Zero, Feedback;
reg [7:0] LFSR_Reg, Next_LFSR_Reg;
always@(posedge clock or negedge Reset)
begin: LFSR_Reg
if (!Reset)
LFSR_Reg <= 8’b0;
else
LFSR_Reg <= Next_LFSR_Reg;
end
…exercise
always@(LFSR_Reg)
begin: LFSR_Feedback
Bits0_6_Zero = ~| LFSR_Reg[6:0];
Feedback = LFSR_Reg[7] ^ Bits0_6_Zero;
for (N=7; N>=1; N=N-1)
if (Taps[N-1] == 1)
Next_LFSR_Reg[N] = LFSR_Reg[N-1] ^ Feedback;
else
Next_LFSR_Reg[N] = LFSR_Reg[N-1];
Next_LFSR_Reg[0] = Feedback;
end
assign Y = LFSR_Reg;
endmodule
Features of Design
• Parameterized Design:
l Details of design can be changed by changing Taps (see example)
• always@(LFSR_Reg) block
l Note ‘chain’ of logic in this block
u Intermediate results used on road to final result
Summary
l Combinational Logic implied by always@(x ...) or assign statements
u Try to build unprioritized logic in a case statement when possible
u Use structural Verilog (assign) to build muxes for smallest fastest design