Advanced Digital Design with the Verilog HDL

Similar documents
Synthesis of Language Constructs. 5/10/04 & 5/13/04 Hardware Description Languages and Synthesis

Register Transfer Level in Verilog: Part I

Writing Circuit Descriptions 8

ECE 551: Digital System *

CSE140L: Components and Design Techniques for Digital Systems Lab

CSE140L: Components and Design

EEL 4783: HDL in Digital System Design

Why Should I Learn This Language? VLSI HDL. Verilog-2

Synthesis vs. Compilation Descriptions mapped to hardware Verilog design patterns for best synthesis. Spring 2007 Lec #8 -- HW Synthesis 1

Synthesis of Combinational and Sequential Circuits with Verilog

Advanced Digital Design with the Verilog HDL

FPGA Design Challenge :Techkriti 14 Digital Design using Verilog Part 1

ECE 4514 Digital Design II. Spring Lecture 15: FSM-based Control

ECEN 468 Advanced Logic Design

RTL Design (Using ASM/SM Chart)

Logic Synthesis. EECS150 - Digital Design Lecture 6 - Synthesis

Verilog introduction. Embedded and Ambient Systems Lab

Verilog for High Performance

Verilog for Synthesis Ing. Pullini Antonio

MCMASTER UNIVERSITY EMBEDDED SYSTEMS

Digital Design with FPGAs. By Neeraj Kulkarni

Lecture 32: SystemVerilog

Verilog Tutorial. Introduction. T. A.: Hsueh-Yi Lin. 2008/3/12 VLSI Digital Signal Processing 2

ECE 2300 Digital Logic & Computer Organization. More Verilog Finite State Machines

EECS150 - Digital Design Lecture 10 Logic Synthesis

EECS150 - Digital Design Lecture 10 Logic Synthesis

ECE 551 Digital System Design and Synthesis. Instructor: Kewal K. Saluja. Midterm Exam

ECE 2300 Digital Logic & Computer Organization. More Verilog Finite State Machines

Chapter 15: Design Examples. CPU Design Example. Prof. Soo-Ik Chae 15-1

Chapter 9: Sequential Logic Modules

Lecture 15: System Modeling and Verilog

Verilog Behavioral Modeling

Digital Circuit Design and Language. Datapath Design. Chang, Ik Joon Kyunghee University

ECE 2300 Digital Logic & Computer Organization. More Sequential Logic Verilog

Introduction to Digital Design with Verilog HDL

Quick Introduction to SystemVerilog: Sequental Logic

Sequential Logic Design

EN164: Design of Computing Systems Lecture 06: Lab Foundations / Verilog 2

Laboratory Exercise 3 Davide Rossi DEI University of Bologna AA

Programmable Logic Devices Verilog VII CMPE 415

Synthesizable Verilog

Chapter-5. EE 335 : Advanced Microprocessor. Logic Design with Behavioral Models of Combinational and Sequential Logic

Chap 6 Introduction to HDL (d)

VHDL: RTL Synthesis Basics. 1 of 59

ECE 4514 Digital Design II. Spring Lecture 13: Logic Synthesis

Digital Design (VIMIAA01) Introduction to the Verilog HDL

FSM and Efficient Synthesizable FSM Design using Verilog

Lecture 12 VHDL Synthesis

Lecture 3. Behavioral Modeling Sequential Circuits. Registers Counters Finite State Machines

a, b sum module add32 sum vector bus sum[31:0] sum[0] sum[31]. sum[7:0] sum sum overflow module add32_carry assign

ECEN 468 Advanced Digital System Design

The Verilog Language COMS W Prof. Stephen A. Edwards Fall 2002 Columbia University Department of Computer Science

EE178 Lecture Verilog FSM Examples. Eric Crabill SJSU / Xilinx Fall 2007

ECE 2300 Digital Logic & Computer Organization. More Finite State Machines

EECS150 - Digital Design Lecture 5 - Verilog Logic Synthesis

Homework deadline extended to next friday

Verilog Module 1 Introduction and Combinational Logic

EPC6055 Digital Integrated Circuits EXAM 1 Fall Semester 2013

Chapter 9: Sequential Logic Modules

ECE 574: Modeling and Synthesis of Digital Systems using Verilog and VHDL. Fall 2017 Final Exam (6.00 to 8.30pm) Verilog SOLUTIONS

Graduate Institute of Electronics Engineering, NTU. Lecturer: Chihhao Chao Date:

EECS Components and Design Techniques for Digital Systems. Lec 20 RTL Design Optimization 11/6/2007

CAD for VLSI Design - I. Lecture 21 V. Kamakoti and Shankar Balachandran

CS6710 Tool Suite. Verilog is the Key Tool

Verilog. Verilog for Synthesis

Introduction to Verilog

Modeling Sequential Circuits in Verilog

Blocking(=) vs Nonblocking (<=) Assignment. Lecture 3: Modeling Sequential Logic in Verilog HDL. Procedural assignments

Problem Set 3 Solutions

A Brief Introduction to Verilog Hardware Definition Language (HDL)

Introduction To Verilog Design. Chun-Hung Chou

University of Toronto Faculty of Applied Science and Engineering Edward S. Rogers Sr. Department of Electrical and Computer Engineering

In this lecture, we will focus on two very important digital building blocks: counters which can either count events or keep time information, and

Recommended Design Techniques for ECE241 Project Franjo Plavec Department of Electrical and Computer Engineering University of Toronto

VHDL. VHDL History. Why VHDL? Introduction to Structured VLSI Design. Very High Speed Integrated Circuit (VHSIC) Hardware Description Language

Speaker: Kayting Adviser: Prof. An-Yeu Wu Date: 2009/11/23

Register Transfer Level Design. Topics. Register Transfer Level Notation. Chapter 8 Steve Oldridge Dr. Sidney Fels. A Circuit is described as:

Nikhil Gupta. FPGA Challenge Takneek 2012

Lab 7 (Sections 300, 301 and 302) Prelab: Introduction to Verilog

Introduction to Verilog HDL. Verilog 1

Using Library Modules in Verilog Designs

Two HDLs used today VHDL. Why VHDL? Introduction to Structured VLSI Design

Digital Design with SystemVerilog

Chapter 5 Registers & Counters

Hardware Design Environments. Dr. Mahdi Abbasi Computer Engineering Department Bu-Ali Sina University

Verilog Fundamentals. Shubham Singh. Junior Undergrad. Electrical Engineering

EN2911X: Reconfigurable Computing Lecture 05: Verilog (2)

VHDL for Synthesis. Course Description. Course Duration. Goals

Verilog Lecture Gandhi Puvvada, USC always statements, Coding a Flip-Flop. blocking and non-blocking assignments. Copyright 2008 Gandhi Puvvada 1

CSE140L: Components and Design Techniques for Digital Systems Lab. Verilog HDL. Instructor: Mohsen Imani UC San Diego. Source: Eric Crabill, Xilinx

Date Performed: Marks Obtained: /10. Group Members (ID):. Experiment # 11. Introduction to Verilog II Sequential Circuits

Introduction To HDL. Verilog HDL. Debdeep Mukhopadhyay Dept of CSE, IIT Madras 1

FSM Components. FSM Description. HDL Coding Methods. Chapter 7: HDL Coding Techniques

Verilog. What is Verilog? VHDL vs. Verilog. Hardware description language: Two major languages. Many EDA tools support HDL-based design

N-input EX-NOR gate. N-output inverter. N-input NOR gate

Chapter 2 Using Hardware Description Language Verilog. Overview

EN2911X: Reconfigurable Computing Topic 02: Hardware Definition Languages

Lecture #2: Verilog HDL

In this lecture, we will go beyond the basic Verilog syntax and examine how flipflops and other clocked circuits are specified.

Digital Design Using Verilog EE Final Examination

Transcription:

Copyright 2001, 2003 MD Ciletti 1 Advanced Digital Design with the Verilog HDL M. D. Ciletti Department of Electrical and Computer Engineering University of Colorado Colorado Springs, Colorado ciletti@vlsic.uccs.edu Draft: Chap 6c: Synthesis of Combinational and Sequential Logic (Rev 9/17/2003) Copyright 2000, 2002, 2003. These notes are solely for classroom use by the instructor. No part of these notes may be copied, reproduced, or distributed to a third party, including students, in any form without the written permission of the author.

Copyright 2001, 2003 MD Ciletti 2 Note to the instructor: These slides are provided solely for classroom use in academic institutions by the instructor using the text, Advance Digital Design with the Verilog HDL by Michael Ciletti, published by Prentice Hall. This material may not be used in off-campus instruction, resold, reproduced or generally distributed in the original or modified format for any purpose without the permission of the Author. This material may not be placed on any server or network, and is protected under all copyright laws, as they currently exist. I am providing these slides to you subject to your agreeing that you will not provide them to your students in hardcopy or electronic format or use them for off-campus instruction of any kind. Please email to me your agreement to these conditions. I will greatly appreciate your assisting me by calling to my attention any errors or any other revisions that would enhance the utility of these slides for classroom use.

Copyright 2001, 2003 MD Ciletti 3 Synthesis of Implicit State Machines, Registers and Counters Implicit state machines are described by one or more edge-sensitive eventcontrol expressions in the same behavior Clock edges define the boundaries of the states Machine remains in a fixed state between clock edges All transitions must be synchronized to the same edge of the clock Do not declare a state register it is implicit Limitation: Each state of an implicit state machine may be entered from only one other state

Copyright 2001, 2003 MD Ciletti 4 Example: Implicit State Machines Synthesis tools infer the existence of an implicit FSM when a cyclic (always) behavior has more than one embedded, clock-synchronized, event control expression The multiple event control expressions within an implicit finite state machine separate the activity of the behavior into distinct clock cycles of the machine always @ (posedge clk) // Synch event before first assignment begin reg_a <= reg_b; // Executes in first clock cycle reg_c <= reg_d; // Executes in first clock cycle. @ (posedge clk) // Begins second clock cycle. begin reg_g <= reg_f; // Executes in second clock cycle. end end reg_m <= reg_r; // Executes in second clock cycle.

Copyright 2001, 2003 MD Ciletti 5 Synthesis of Implicit State Machines Synthesis tool infers the size of the state Registers are allocated to hold "multiple wait states" Synthesis tool infers the logic governing state transitions

Copyright 2001, 2003 MD Ciletti 6 Example: Ripple Counter module ripple_counter (count, toggle, clock, reset,); output [3: 0] count; input toggle, clock, reset; reg [3: 0] count; wire c0, c1, c2; assign c0 = count[0]; assign c1 = count[1]; assign c2 = count[2]; always @ (posedge reset or posedge clock) if (reset == 1'b1) count[0] <= 1'b0; else if (toggle == 1'b1) count[0] <= ~count[0]; always @ (posedge reset or negedge c0) if (reset == 1'b1) count[1] <= 1'b0; else if (toggle == 1'b1) count[1] <= ~count[1];

Copyright 2001, 2003 MD Ciletti 7 always @ (posedge reset or negedge c1) if (reset == 1'b1) count[2] <= 1'b0; else if (toggle == 1'b1) count[2] <= ~count[2]; always @ (posedge reset or negedge c2) if (reset == 1'b1) count[3] <= 1'b0; else if (toggle == 1'b1) count[3] <= ~count[3]; endmodule count[0] count[1] count[2] count[3] toggle T Q T Q T Q T Q R R R R clock reset

Copyright 2001, 2003 MD Ciletti 8 Synthesized circuit: count[3:0] count[3] count[2] toggle count[1] clock count[0] reset

Copyright 2001, 2003 MD Ciletti 9 Example: Ring Counter A sequential machine having an identical activity flow in every cycle is a onecycle implicit state machine One state: "running" Revisit Example 5.40 reset_ S_idle 2 0,3 up_dwn S_decr 2 up_dwn 1 S_incr 1 0,3 up_dwn 1 0,3 2

Copyright 2001, 2003 MD Ciletti 10

Copyright 2001, 2003 MD Ciletti 11 reset_ count <= 0 count <= 0 S_running 0,3 1 up_dwn count <= count + 1 count <= count - 1 S_running reset_ 1 count <= count + 1 count <= count - 1 2 0,3 1 up_dwn 2 Note: modeling the machine with a single implicit state is simpler than modeling a machine whose state is the count. We'll see later that this problem can be partitioned into a datapath and a controller.

Copyright 2001, 2003 MD Ciletti 12 module Up_Down_Implicit1 (count, up_dwn, clock, reset_); output [2: 0] count; input [1: 0] up_dwn; input clock, reset_; reg [2: 0] count; always @ (negedge clock or negedge reset_) if (reset_ == 0) count <= 3'b0; else if (up_dwn == 2'b00 up_dwn == 2'b11) count <= count; else if (up_dwn == 2'b01) count <= count + 1; else if (up_dwn == 2'b10) count <= count 1; endmodule

Copyright 2001, 2003 MD Ciletti 13 Synthesis of Registers Register: Array of D-type flip-flops with a common clock Example 6.31 module shifter_1 (sig_d, new_signal, Data_in, clock, reset); output sig_d, new_signal; input Data_in, clock, reset; reg sig_a, sig_b, sig_c, sig_d, new_signal;

always @ (posedge reset or posedge clock) begin if (reset == 1'b1) begin sig_a <= 0; sig_b <= 0; sig_c <= 0; sig_d <= 0; new_signal <= 0; end else begin sig_a <= Data_in; sig_b <= sig_a; sig_c <= sig_b; sig_d <= sig_c; new_signal <= (~ sig_a) & sig_b; end end endmodule Copyright 2001, 2003 MD Ciletti 14

Copyright 2001, 2003 MD Ciletti 15 Note: new_signal receives value within a synchronous behavior and appears as an output port. It will be synthesized as the output of a flip-flop. Data_in D Q sig_a sig_b sig_c sig_d D Q D Q D Q R R R R clock reset D Q new_signal R

Copyright 2001, 2003 MD Ciletti 16 Example 6.32 new_signal is formed outside of the behavior in a continuous assignment new_signal appears as an output port new_signal is synthesized as the output of combinational logic. module shifter_2 (sig_d, new_signal, Data_in, clock, reset); output sig_d, new_signal; input Data_in, clock, reset; reg sig_a, sig_b, sig_c, sig_d, new_signal;

always @ (posedge reset or posedge clock) begin if (reset == 1'b1) begin sig_a <= 0; sig_b <= 0; sig_c <= 0; sig_d <= 0; end else begin sig_a <= shift_input; sig_b <= sig_a; sig_c <= sig_b; sig_d <= sig_c; end end assign new_signal = (~ sig_a) & sig_b; endmodule Copyright 2001, 2003 MD Ciletti 17

Copyright 2001, 2003 MD Ciletti 18 Data_in D Q sig_a sig_b sig_c sig_d D Q D Q D Q R R R R clock reset new_signal

Copyright 2001, 2003 MD Ciletti 19 Example 6.33 Two versions of an accumulator forming the running sum of two samples of an input. module Add_Accum_1 (accum, overflow, data, enable, clk, reset_b); output [3: 0] accum; output overflow; input [3: 0] data; input enable, clk, reset_b; reg accum, overflow; always @ (posedge clk or negedge reset_b) if (reset_b == 0) begin accum <= 0; overflow <= 0; end else if (enable) {overflow, accum} <= accum + data; endmodule

Copyright 2001, 2003 MD Ciletti 20 module Add_Accum_2 (accum, overflow, data, enable, clk, reset_b); output [3: 0] accum; output overflow; input [3: 0] data; input enable, clk, reset_b; reg accum; wire [3:0] sum; assign {overflow, sum} = accum + data; always @ (posedge clk or negedge reset_b) if (reset_b == 0) accum <= 0; else if (enable) accum <= sum; endmodule

Copyright 2001, 2003 MD Ciletti 21 Simulation Results: Add_Accum_1 forms overflow_1 one cycle after storing the results of an overflow condition Add_Accum_2 forms overflow_2 as an unregistered Mealy output Synthesis Results: overflow_1 is formed in Add_Accum_1 as a registered version of overflow_2, which is formed in Add_Accum_2.

Copyright 2001, 2003 MD Ciletti 22

Copyright 2001, 2003 MD Ciletti 23 xor2_a clk xnor2_a data[3:0] data<1> inv_a inv_a oai211_a xor2_a dffrgpqb_a inv_a inv_a nor2_a oai2_a xor2_a dffrgpqb_a nor2_a oroai22_a xor2_a dffrgpqb_a overflow_1 oai211_a dffrgpqb_a and2_a xor2_a dffrgpqb_a accum<1> overflow_2 accum<0> xor2_a enable reset_b accum<2> accum<3> accum<3:0>

Copyright 2001, 2003 MD Ciletti 24 Resets Provide a reset signal to every sequential module that is to be synthesized o Initialize the state o Support testing Provide an external signal to reset an implicit state machine Provide reset logic to every cycle of the behavioral model o Return to the top of the multi-cycle behavior from any cycle o Return to the same state independently of when reset is asserted o Synthesis will produce extra logic to accommodate incomplete resets May have to synchronize the reset signal

Copyright 2001, 2003 MD Ciletti 25 Partial Resets Implicit state machine to asserts D_out after two successive samples of D_in are both either 1 or 0 Hold samples in a two-stage shift register Prevent premature assertion by having a state machine set flag after two samples have been received Examine consequences of partially resetting the data register

Copyright 2001, 2003 MD Ciletti 26 module Seq_Rec_Moore_imp (D_out, D_in, clock, reset); output D_out; input D_in; input clock, reset; reg last_bit, this_bit, flag; wire D_out; always begin: wrapper_for_synthesis @ (posedge clock /* or posedge reset*/) begin: machine if (reset == 1) begin last_bit <= 0; // this_bit <= 0; // flag <= 0; disable machine; end else begin // last_bit <= this_bit; this_bit <= D_in;

Copyright 2001, 2003 MD Ciletti 27 forever @ (posedge clock /* or posedge reset */) begin if (reset == 1) begin // last_bit <= 0; // this_bit <= 0; flag <= 0; disable machine; end else begin last_bit <= this_bit; this_bit <= D_in; flag <= 1; end // second edge end end end // machine end // wrapper_for_synthesis assign D_out = (flag && (this_bit == last_bit)); endmodule

Simulation Results: Copyright 2001, 2003 MD Ciletti 28

Copyright 2001, 2003 MD Ciletti 29 Note: synthesis depends on how the reset is implemented Case 1: Flush only last_bit (earliest bit received) esdpupd_ clock D_in esdpupd_ esdpupd_ esdpupd_ dffrmqb_a clk D0 D1 RB SL Q dffrgpqb_a CK D G RB Q this_bit dffrgpqb_a CK D G RB Q dffrgpqb_a CK D G RB this_bit last_bit Q flag D_out esdpupd_ multiple_wait_state reset

Copyright 2001, 2003 MD Ciletti 30 Features to note: dffrgpqb_a is a gate-input flip-flop with an internal datapath from Q to D Q is connected to D when G is low Q is connected to external datapath (D) when Q is high dffmpqb_a is a dual multiplexed input flip-flop Holds multiple_wait_state indicating two samples received RB (reset) input is disabled by espupd SL (set) is active low and wire to reset D0 and D1 are wired to power and ground respectively SL low selects D0; SL high selects D1 Assertion of reset: Causes this_bit and last_bit to hold their values Steers the external input (0) to the flag register De-assertion of reset: multiple_wait_state is 1 after first clock Steers this_bit to last_bit this_bit gets in_bit after reset is de-asserted Only flag is flushed in the loop

Copyright 2001, 2003 MD Ciletti 31 Case 2: Flush the pipeline clock esdpupd_ esdpupd_ dffrmqb_a clk D0 D1 RB SL Q dffrmqb_a clk D0 D1 RB SL Q wait_state flag D_out esdpupd_ clk D0 D1 RB SL Q last_bit esdpupd_ dffrmqb_a dffrmqb_a reset clk D0 D1 RB Q this_bit esdpupd_ SL D_in

Copyright 2001, 2003 MD Ciletti 32 When registers are not flushed on reset, additional logic is required to feed their outputs back to their inputs to retain their state under the action of the clock. Option: for simpler hardware, drive the register to a known value on reset.

Copyright 2001, 2003 MD Ciletti 33

Copyright 2001, 2003 MD Ciletti 34 Synthesis of Gated Clocks and Clock Enables Caution: Gated clocks can add skew to the clock path, and cause the clock signal to violate a flip-flop's constraint on the minimum width of the clock pulse. Recommended style: module best_gated_clock (clock, reset_, data_gate, data, Q); input clock, reset_, data, data_gate; output Q; reg Q; always @ (posedge clock or negedge reset_) if (reset_ == 0) Q <= 0; else if (data_gate) Q <= data; endmodule

Copyright 2001, 2003 MD Ciletti 35 Features: o Input data and flip-flop output are multiplexed at D input o Clock synchronizes the circuit o data_gate gates the action of the clock o Clock slivers are not a problem data data_gate muxf201 dfnf311 Q clock reset_

Copyright 2001, 2003 MD Ciletti 36 Typical model for clock enable logic: always @ (posedge clk) if (enable == 1) q_out <= Data_in;

Copyright 2001, 2003 MD Ciletti 37 Anticipating the Results of Synthesis Data types Operator grouping Expression Substitution Loops Additional details in "Modeling, Synthesis, and Rapid Prototyping with the Verilog HDL"

Copyright 2001, 2003 MD Ciletti 38 Synthesis of Data Types The identity of nets that are primary inputs or outputs are retained in synthesis Internal nets may be eliminated by synthesis Integers are stored as 32-bit words (minimum per IEEE 1364) Used sized parameters rather than integer parameters Explicit use of x or z in logical tests will not synthesize

Copyright 2001, 2003 MD Ciletti 39 Synthesis of Operators Some operators may map directly into library cells (e.g. +, 1, <, >, =) Synthesis might impose restrictions on an operator Shift operators is allowed only if the shift index is a constant Reduction, bitwise, and logical operators synthesize to equivalent gates Conditional operator synthesizes to a mux structure If both operands of * are constant the tool produces the constant result If one operand of * is a power of 2 the synthesis tool forms the result by left-shifting the other operand If one operand of / is a power of 2 the result will be formed by rightshifting the other operand If the divisor is a power of 2 the % operator will be formed as a partselect See "Modeling, Synthesis and Rapid Prototyping with the Verilog HDL for more details

Copyright 2001, 2003 MD Ciletti 40 Operator Grouping Use parentheses within expressions to influence the outcome of synthesis Example 6.35 module operator_group (sum1, sum2, a, b, c, d); output [4: 0] sum1, sum2; input [3: 0] a, b, c, d; assign sum1 = a + b + c + d; assign sum2 = (a + b) + (c + d); // 3 levels of logic // 2 levels of logic endmodule

Copyright 2001, 2003 MD Ciletti 41 Synthesis results: a b c d a b c d 4-bit adder 4-bit adder 4-bit adder 5-bit adder 5-bit adder 5-bit adder sum2 sum1 Note: Use sum1 with input d to accommodate a late signal

Copyright 2001, 2003 MD Ciletti 42 Expression Substitution Synthesis tools perform expression substitution to determine the effective logic of a sequence of procedural assignments Remember: procedural assignments have immediate effect on the value of the target variable

Copyright 2001, 2003 MD Ciletti 43 Example 6.36 module multiple_reg_assign (data_out1, data_out2, data_a, data_b, data_c, data_d, sel, clk); output [4: 0] data_out1, data_out2; input [3: 0] data_a, data_b, data_c, data_d; input clk; reg [4: 0] data_out1, data_out2; always @ (posedge clk) begin data_out1 = data_a + data_b ; data_out2 = data_out1 + data_c; if (sel == 1'b0) data_out1 = data_out2 + data_d; end endmodule

Copyright 2001, 2003 MD Ciletti 44 More readable alternative: module expression_sub (data_out1, data_out2, data_a, data_b, data_c, data_d, sel, clk); output [4: 0] data_out1, data_out2; input [3: 0] data_a, data_b, data_c, data_d; input sel, clk; reg [4: 0] data_out1, data_out2; always @ (posedge clk) begin data_out2 = data_a + data_b + data_c; if (sel == 1'b0) data_out1 = data_a + data_b + data_c + data_d; else data_out1 = data_a + data_b; end endmodule

Copyright 2001, 2003 MD Ciletti 45 Equivalent logic with nonblocking assignments module expression_sub_nb (data_out1nb, data_out2nb, data_a, data_b, data_c, data_d, sel, clk); output [4: 0] data_out1nb, data_out2nb; input [3: 0] data_a, data_b, data_c, data_d; input sel, clk; reg [4: 0] data_out1nb, data_out2nb; always @ (posedge clk) begin data_out2nb <= data_a + data_b + data_c; if (sel == 1'b0) data_out1nb <= data_a + data_b + data_c + data_d; else data_out1nb <= data_a + data_b; end endmodule

Copyright 2001, 2003 MD Ciletti 46 data_d data_c data_b sel data_a + + data_out1 clk + + data_out2 clk

Copyright 2001, 2003 MD Ciletti 47 esdpupd esdpupd clk sel a+ b + c + d mux dffspqb_a data_out1[4:0] mux dffspqb_a data_a[3:0] esdpupd data_b[3:0] 5 5 a + b + a + b + c 5 + 5 a+ b + c + d 5 + mux mux dffspqb_a dffspqb_a data_c[3:0] 5 mux dffspqb_a data_d[3:0] a + b + c dffspqb_a data_out2[4:0] esdpupd dffspqb_a esdpupd dffspqb_a esdpupd dffspqb_a esdpupd dffspqb_a esdpupd esdpupd esdpupd

Copyright 2001, 2003 MD Ciletti 48 Synthesis of Loops Classifications of loops according to data dependency and timing controls Static (data-independent) loop: the number of its iterations can be determined by the compiler before simulation Non-static ( data-dependent) loop: the number of iterations depends on some variable during operation. Note: Non-static loops that do not have internal timing controls cannot be synthesized directly.

Copyright 2001, 2003 MD Ciletti 49 Combinational Logic Static No Internal Timing Control Internal Timing Control Single-cycle Loops Sequential Logic Internal Timing Control Multi-cycle Non-Static No Internal Timing Control Not synthesizable

Copyright 2001, 2003 MD Ciletti 50 Static Loops No Internal Timing Control If a loop has no internal timing controls and no data dependencies, its computational activity is implicitly combinational.

Copyright 2001, 2003 MD Ciletti 51 Example 6.37 module for_and_loop_comb (out, a, b); output [3: 0] out; input [3: 0] a, b; reg [2: 0] i; reg [3: 0] out; wire [3: 0] a, b; always @ (a or b) begin for (i = 0; i <= 3; i = i+1) out[i] = a[i] & b[i]; end endmodule

Copyright 2001, 2003 MD Ciletti 52 Equivalent unrolled loop: out[0] = a[0] & b[0]; out[1] = a[1] & b[1]; out[2] = a[2] & b[2]; out[3] = a[3] & b[3]; a[3:0] b[3:0] out[3:0] There are no dependencies in the datapath, and the order in which the statements are evaluated does not affect the outcome of the evaluation.

Copyright 2001, 2003 MD Ciletti 53 Example 6.38 Count the 1s in a word received in parallel format module count_ones_a (bit_count, data, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count, index; reg [data_width-1: 0] temp;

Copyright 2001, 2003 MD Ciletti 54 always @ (posedge clk) if (reset) begin count = 0; bit_count = 0; end else begin count = 0; bit_count = 0; temp = data; for (index = 0; index < data_width; index = index + 1) begin count = count + temp[0]; temp = temp >> 1; end bit_count = count; end endmodule

Copyright 2001, 2003 MD Ciletti 55 Note: The loop in the Verilog model count_ones_a below has no internal timing controls and is static The order in which the statements in the cyclic behavior execute is important The model uses the blocked assignment operator (=) bit_count asserts after the loop has executed, within the same clock cycle that the loop executes

Copyright 2001, 2003 MD Ciletti 56 Simulation results: Note: the displayed values of temp, count and bit_count are final values that result after any intermediate values have been overwritten.

Copyright 2001, 2003 MD Ciletti 57 Synthesis results: esdpupd_ clk reset dffspq b_a bit_count[2:0] data[3:0] dffspq b_a esdpupd_ dffspq b_a esdpupd_

Copyright 2001, 2003 MD Ciletti 58 Static Loops with Embedded Timing Control Embedded event control expressions synchronize and distribute the computational activity of a loop over one or more cycles of the clock (implicit state machine with a cycle for each iteration) Example 6.39 module count_ones_b0 (bit_count, data, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count; reg [data_width-1: 0] temp; integer index;

always begin: wrapper_for_synthesis @ (posedge clk) begin: machine if (reset) begin bit_count = 0; disable machine; end else count = 0; bit_count = 0; index = 0; temp = data; forever @ (posedge clk) if (reset) begin bit_count = 0; disable machine; end else if (index < data_width-1) begin count = count + temp[0]; temp = temp >> 1; index = index + 1; end else begin bit_count = count + temp[0]; disable machine; end end // machine end // wrapper_for_synthesis endmodule Copyright 2001, 2003 MD Ciletti 59

module count_ones_b1 (bit_count, data, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count; reg [data_width-1: 0] temp; integer index; Copyright 2001, 2003 MD Ciletti 60

always begin: wrapper_for_synthesis @ (posedge clk) begin: machine if (reset) begin bit_count = 0; disable machine; end else begin count = 0; bit_count = 0; index = 0;temp = data; while (index < data_width) begin if (reset) begin bit_count = 0; disable machine; end else if ((index < data_width) && (temp[0] )) count = count + 1; temp = temp >> 1; index = index +1; @ (posedge clk); end if (reset) begin bit_count = 0; disable machine; end else bit_count = count; disable machine; end end // machine end // wrapper_for_synthesis endmodule Copyright 2001, 2003 MD Ciletti 61

Copyright 2001, 2003 MD Ciletti 62 module count_ones_b2 (bit_count, data, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count; reg [data_width-1: 0] temp; integer index; always begin: machine for (index = 0; index <= data_width; index = index +1) begin @ (posedge clk) if (reset) begin bit_count = 0; disable machine; end else if (index == 0) begin count = 0; bit_count = 0; temp = data; end else if (index < data_width) begin count = count + temp[0]; temp = temp >> 1; end else bit_count = count + temp[0]; end end // machine endmodule

Copyright 2001, 2003 MD Ciletti 63

Copyright 2001, 2003 MD Ciletti 64

Copyright 2001, 2003 MD Ciletti 65 Nonstatic Loops without Embedded Timing Control The number of iterations to be executed by a loop having a data dependency cannot be determined before simulation. Example 6.40 module count_ones_c (bit_count, data, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count, index; reg [data_width-1: 0] temp;

always @ (posedge clk) if (reset) begin count = 0; bit_count = 0; end else begin count = 0; temp = data; for (index = 0; temp; index = index + 1) begin if (temp[0] ) count = count + 1; temp = temp >> 1; end bit_count = count; end endmodule Copyright 2001, 2003 MD Ciletti 66

Copyright 2001, 2003 MD Ciletti 67

Copyright 2001, 2003 MD Ciletti 68 Nonstatic Loops without Embedded Timing Control For synthesis, the iterations of a non-static loop must be separated by a synchronizing edge-sensitive control expression. Example 6.41 module count_ones_d (bit_count, data, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count; reg [data_width-1: 0] temp;

always begin: wrapper_for_synthesis @ (posedge clk) if (reset) begin count = 0; bit_count = 0; end else begin: bit_counter count = 0; temp = data; while (temp) @ (posedge clk) if (reset) begin count = 2'b0; disable bit_counter; end else begin count = count + temp[0]; temp = temp >> 1; end Copyright 2001, 2003 MD Ciletti 69

@ (posedge clk); if (reset) begin count = 0; disable bit_counter; end else bit_count = count; end // bit_counter end endmodule Copyright 2001, 2003 MD Ciletti 70

Copyright 2001, 2003 MD Ciletti 71

Copyright 2001, 2003 MD Ciletti 72 module count_ones_sd (bit_count, done, data, start, clk, reset); parameter data_width = 4; parameter count_width = 3; output [count_width-1: 0] bit_count; output done; input [data_width-1: 0] data; input clk, reset; reg [count_width-1: 0] count, bit_count, index; reg [data_width-1: 0] temp; reg done, start; always @ (posedge clk) begin: bit_counter if (reset) begin count = 0; bit_count = 0; done = 0; end else if (start) begin done = 0; count = 0; bit_count = 0; temp = data;

Copyright 2001, 2003 MD Ciletti 73 for (index = 0; index < data_width; index = index + 1) @ (posedge clk) if (reset) begin count = 0; bit_count = 0; done = 0; disable bit_counter; end else begin count = count + temp[0]; temp = temp >> 1; end @ (posedge clk) // Required for final register transfer if (reset) begin count = 0; bit_count = 0; done = 0;\ disable bit_counter; end else begin bit_count = count; done = 1; end end end endmodule

Copyright 2001, 2003 MD Ciletti 74

Copyright 2001, 2003 MD Ciletti 75 State Machine Replacements for Unsynthesizable Loops Replace loop structures of non-static loops that don't have embedded timing controls by equivalent synthesizable sequential behavior.

Copyright 2001, 2003 MD Ciletti 76 Example 6.42 S_idle reset 1 1 start load_temp temp <= data bit_count <= bit_count + temp[0] temp <= temp >> 1 S_counting / busy, shift_add 1 temp_gt_1 load_temp clear bit_count <= 0 temp <= data S_waiting / done start 1

Copyright 2001, 2003 MD Ciletti 77 module count_ones_sm (bit_count, busy, done, data, start, clk, reset); parameter counter_size = 3; parameter word_size = 4; output [counter_size -1 : 0] bit_count; output busy, done; input [word_size-1: 0] data; input start, clk, reset; wire load_temp, shift_add, clear; wire temp_0, temp_gt_1; controller M0 (load_temp, shift_add, clear, busy, done, start, temp_gt_1, clk, reset); datapath M1 (temp_gt_1, temp_0, data, load_temp, shift_add, clk, reset); bit_counter_unit M2 (bit_count, temp_0, clear, clk, reset); endmodule

Copyright 2001, 2003 MD Ciletti 78 module controller (load_temp, shift_add, clear, busy, done, start, temp_gt_1, clk, reset); parameter state_size = 2; parameter S_idle = 0; parameter S_counting = 1; parameter S_waiting = 2; output load_temp, shift_add, clear, busy, done; input start, temp_gt_1, clk, reset; reg bit_count; reg [state_size-1 : 0] state, next_state; reg load_temp, shift_add, busy, done, clear;

Copyright 2001, 2003 MD Ciletti 79 always @ (state or start or temp_gt_1) begin load_temp = 0; shift_add = 0; done = 0; busy = 0; clear = 0; next_state = S_idle; case (state) S_idle: if (start) begin next_state = S_counting; load_temp = 1; end S_counting: begin busy = 1; if (temp_gt_1) begin next_state = S_counting; shift_add = 1; end else begin next_state = S_waiting; shift_add = 1; end end S_waiting: begin done = 1; if (start) begin next_state = S_counting; load_temp = 1; clear = 1; end else next_state = S_waiting; end default: begin clear = 1; next_state = S_idle; end endcase end

always @ (posedge clk) // state transitions if (reset) state <= S_idle; else state <= next_state; endmodule Copyright 2001, 2003 MD Ciletti 80

Copyright 2001, 2003 MD Ciletti 81 module datapath (temp_gt_1, temp_0, data, load_temp, shift_add, clk, reset); parameter word_size = 4; output temp_gt_1, temp_0; input [word_size-1: 0] data; input load_temp, shift_add, clk, reset; reg [word_size-1: 0] temp; wire temp_gt_1 = (temp > 1); wire temp_0 = temp[0]; always @ (posedge clk) // state and register transfers if (reset) begin temp <= 0; end else begin if (load_temp) temp <= data; if (shift_add) begin temp <= temp >> 1; end end endmodule

Copyright 2001, 2003 MD Ciletti 82 module bit_counter_unit (bit_count, temp_0, clear, clk, reset); parameter counter_size = 3; output [counter_size -1 : 0] bit_count; input temp_0; input clear, clk, reset; reg bit_count; always @ (posedge clk) // state and register transfers if (reset clear) bit_count <= 0; else bit_count <= bit_count + temp_0; endmodule

Copyright 2001, 2003 MD Ciletti 83

Copyright 2001, 2003 MD Ciletti 84 clk and2i_a data<3:0> reset shift_add load_temp data<3> nor2_a and2_a and2i_c dffrgpqb_a inv_a data<2> onnai22_a dffrgpqb_a inv_a data<1> onnai22_a dffrgpqb_a inv_a data<0> onnai22_a dffrgpqb_a temp_0 esdpupd nand3_a temp_gt_1

Copyright 2001, 2003 MD Ciletti 85 start oai21_a inv_a clear clk reset temp_gt_1 aoi21_a inv_a esdpupd inv_a dffrmpqb_a nand2_a inv_a onnai22_a dffrmpqb_a inv_a nor2_a nor2_a inv_a done load_temp busy shift_add

Copyright 2001, 2003 MD Ciletti 86 clk temp_0 xnor2_a nor2_a dffspqb_a nand2_a xor2_a nor2_a dffspqb_a and2i_a xor2_a nor2_a dffspqb_a bit_count<2:0> bit_count<2> reset clear or2_a bit_count<1> esdpupd bit_count<0>

Copyright 2001, 2003 MD Ciletti 87 Example 6.43 Implicit State Machine module count_ones_imp (bit_count, start, done, data, data_ready, clk, reset); parameter word_size = 4; parameter counter_size = 3; parameter state_size = 2; output [counter_size -1 : 0] bit_count; output start, done; input [word_size-1: 0] data; input data_ready, clk, reset; reg bit_count; reg [state_size-1 : 0] state, next_state; reg start, done, clear; reg [word_size-1: 0] temp;

Copyright 2001, 2003 MD Ciletti 88 always @ (posedge clk) if (reset) begin temp<= 0; bit_count <= 0; done <= 0; start <= 0; end else if (data_ready && data &&!temp) begin temp <= data; bit_count <= 0; done <= 0; start <= 1; end else if (data_ready && (!data) && done) begin bit_count <= 0; done <= 1; end else if (temp == 1) begin bit_count <= bit_count + temp[0]; temp <= temp >> 1; done <= 1; end else if (temp &&!done) begin start <= 0; temp <= temp >> 1; bit_count <= bit_count + temp[0]; end endmodule

Copyright 2001, 2003 MD Ciletti 89