RealChipDesign Preface
RealChipDesign Preface
RealChipDesign Preface
Verification
Using Verilog and VHDL
Ben Cohen
VhdlCohen Publishing
Los Angeles, California
http://www.vhdlcohen.com/
ii Real Chip Design and Verification
Using Verilog and VHDL
Published by:
VhdlCohen Publishing
P.O. 2362
Palos Verdes Peninsula CA 90274-2362
vhdlcohen@aol.com
http://www.vhdlcohen.com
All rights reserved. No part of this publication may be reproduced or transmitted in any
form or by any means, electronic or mechanical, including photocopying, recording, or
by any information storage and retrieval system, without the prior written permission
from the author, except for the inclusion of brief quotations in a review.
PREFACE ……………………………………………………………………XIII
ACKNOWLEDGEMENTS…………………………………………………..XXV
DISCLAIMER…………….…………………………………………………...XXVIII
1. OVERVIEW 1
2. FUNDAMENTALS 7
2.1 Flip-Flop 8
2.1.1 Synchronous Flip-FLop 8
2.2 Latch 9
2.5 Registers 20
2.5.1 Serial to Parallel 20
iv Real Chip Design and Verification
Using Verilog and VHDL
2.5.2 Parallel to Serial 22
2.5.3 Register File 24
2.6 Counters 28
2.6.1 Up/Down Binary Counter 28
2.6.2 Loadable Down-Counter 32
2.6.3 Building a free-running downcounter 35
2.6.4 Simple 5-bit Up-Counter with Terminal Count 37
2.6.5 LFSR Terminal Counters 39
2.6.6 One-Hot / One Cold Counters 46
2.6.7 Gray-Code 51
2.6.7.1 Designing Gray-Code Counters 52
2.6.7.2 Gray To Binary Conversion 53
2.6.7.3 Binary To Gray Conversion 56
2.6.8 Gray-Code Counter Implementation 57
2.6.9 Johnson 64
2.7 Memories 68
2.7.1 ROM 68
2.7.1.1 Application of Trigonometric Functions 72
2.7.2 RAM 76
2.7.2.1 Inferring Virtex or Virtex-E Block SelectRAM+ 78
2.7.3 EDAC 79
2.7.4 First-In-First-Out Memories (FIFO) 102
4. VERIFICATION 187
Verilog and VHDL have been with us now for almost 20 years. During that time, we have
watched them grow from esoteric new ways of specifying testbench and design functionality
to the mainstay in any complex design methodology. Despite the wide usage and proliferation
of HDL-based design methodologies, there is still much to be learned in this area for many
people, from new designers just starting-out with HDLs to experienced engineers looking for
ways to improve their productivity.
This new book by Ben Cohen is an invaluable addition to the existing literature on chip design
using the Verilog and VHDL hardware description languages. As Ben notes in his Preface,
the purpose of his book is not to teach either HDL, as there are already several books on the
market that do an excellent job of describing and teaching the languages. However, as Ben
also notes, a sound understanding of the HDL, though a requirement, is not sufficient.
Understanding the HDL alone will not make you an expert logic designer, any more than
learning C or C++ will make you an expert computer programmer.
One of the things that make this book particularly important is that it doesn't focus on just
Verilog or VHDL, but rather on actual design and simulation using examples from both
languages. No one actually designs using two languages at the same time, but more and more
designers find themselves using IP in a different language or integrating their design with
another one written in different language. In addition to driving the need for tools that support
both languages, this is also driving the need for designers to understand both Verilog and
VHDL at least well enough to be able to debug modules written in either language.
This book concentrates on common classes of hardware architectures and design problems,
and focuses on the process of transitioning design requirements into synthesizable HDL code.
Using his extensive, wide-ranging experience in computer architecture and hardware design,
as well as in his training and consulting work, Ben provides numerous examples of real-life
designs illustrated with VHDL and Verilog code. This code is shown in a way that makes it
easy for the reader to gain a greater understanding of the languages and how they compare.
All code presented in the book is included on the companion CD, along with other
information, such as application notes.
x Real Chip Design and Verification
Using Verilog and VHDL
Ben also covers a critical aspect for any real-life testbench creation: the use of transaction-
based verification techniques. Designs are too complicated to continue to validate them
exclusively at the individual signal level. In order to both improve performance and ensure
that the tests actually check intended behavior, designers need to create tests and verify results
at the transaction level. The book includes a chapter covering on Verilog and VHDL
transaction level testing while also referring to C++ based transaction level test tools such as
the open-source Testbuilder (available at www.testbuilder.net).
Cadence Design Systems is proud that some of its leading digital verification products were
used in the creation of this book. HAL was used for HDL analysis and lint checking, which
provides a perfect static verification complement to the dynamic simulation featured in the
rest of the book. The high-performance, mixed-language simulator NC-Sim was used for
verifying all of the examples in the book and the results were shown using the SimVision GUI
and debug environment.
This book is one of the best investments that a logic designer can make. We are certain that it
will be of enormous value to all those involved in HDL-based chip design for years to come.
Rahul Razdan
Corporate Vice President - Systems and Functional Verification.
Cadence Design Systems, Inc.
Preface xi
For those eager to learn the practical aspects of design, they often are left 'on their own'.
There is a void in mentoring in engineering today. The designer must seek out information
from books, from colleagues, from poking and probing through existing code to see what is
typically done in the industry.
In Real Chip Design and Verification Using Verilog and VHDL, Ben Cohen bridges gaps. He
bridges the gaps in a designer's knowledge, he covers the gaps left by other texts. The focus
on this book is to learn by example. The readers starting point is the macro elements and their
implementation. Ben takes these elements, simulates them, synthesizes them, and leaves the
reader with the ability to do the same. He defers giving bias to Verilog or VHDL, but rather
acknowledges that both are in use, and thus the reader may utilize either or both in their
career.
xii Real Chip Design and Verification
Using Verilog and VHDL
Ben bridges simulation and synthesis, and this acknowledges that implementation and
verification must both be done in design. He then extends the discussion to more complex
design objects, and discusses architectural tradeoffs in these components. These discussions
extend to their impact for targeting FPGAs and ASICs, their final destination.
Ben continues to be a pragmatic author. His topics and writing are very accessible and
pertinent to the engineering community he reaches. At Synplicity, we have benefited from
Ben's knowledge of language and tools in his role as customer and author. Now you can too.
Andrew Dauman
Vice President, Corporate Applications Engineering
Sunnyvale, CA
October, 2001
Preface xiii
PREFACE
Now that you know a hardware description language (HDL), where do you go from
here? As a VHDL trainer and consultant, I experienced that many engineers understand the
HDL from a software viewpoint, but do not know how to approach design problems. There is
a fallacy that HDL is the panacea to all design issues, and that synthesis tools will perform the
magic of translating the HDL into hardware. The reality is that synthesis is just a tool to help
in the implementation of what is described. It is necessary that the authors of the HDL code
understand the hardware architecture and implications of what is described. A proper HDL
description of an incorrect or improper architecture does not necessarily yield a correct or
optimum design. The following quotation, from a contributor in comp.lang.vhdl newsgroup,
expresses this point in an interesting manner: HDL (e.g., VHDL or Verilog), like any other EE
design tool, assumes that the user has a working knowledge of electronics and digital design.
I know many who are poor designers before learning HDL and remain poor designers after
taking an HDL class.
Real Chip Design and Verification Using Verilog and VHDL addresses the practical and real
aspects of logic design, processes, and verification. It incorporates a collection of FPGA and
ASIC design practices, and uses Verilog and VHDL as a tool for expression of the desired
architectures. This book is not intended to teach either HDL, as there are several books
specifically geared toward teaching the languages. However, it provides various architectural
design primitives, applications, and verification techniques, along with design methodologies
and common practices.
Logic design is an art that is learned, and often relearned by designers. There are several
common classes of design problems and several common classes of hardware architectures
including synchronization logic, counters, controllers, arithmetic elements, and storage
elements. This book addresses those classes of designs with practical examples to expose the
reader to variations in styles and approaches. The architectural issues, design decomposition,
and HDL code in both VHDL and Verilog are discussed and demonstrated. Transaction-
based testbenches with error injection methodologies demonstrate, by example, design
verification techniques. Models used for this verification task include a counter and an EDAC
(error detection and correction) logic with a RAM.
This book is intended as a training book in conjunction with an HDL class as a means to
demonstrate the transition of design requirements into an HDL design. Specifically, it
demonstrates by example the following:
xiv Real Chip Design and Verification
Using Verilog and VHDL
Cadence NC-Sim simulator and HAL analysis and lint checking tools were used because of
their levels of efficiency, accuracy, and maturity. Cadence represents a vendor that is a leader
in the EDA industry. Synplicity Synplify Pro® FPGA synthesis tool was also extensively
used because Synplicity is recognized as a vendor of advanced, efficient, and easy to use
synthesis tools targeted for FPGAs, and now ASICs. Even though these specific tools were
used, almost all of the information is tool independent.
Chapter 1 provides an overview of the architectural decomposition process, and presents the
classes of hardware designs. Chapter 2 presents fundamental architectural elements used in
the construction of designs. These include flip-flops, latches, synchronous edge detector,
application of both edges of the clock, registers, counter styles (e.g., Binary, One-Hot, Gray,
LFSR, Johnson), memories including ROM, RAM, FIFO, and Error Detection and Correction
(EDAC) logic. A trigonometric function defined in C, but implemented in HDL as a ROM,
is also demonstrated. This chapter also addresses the importance of understanding the cell
primitives and FPGA architecture including the clocking features of ASICs and FPGAs.
Topics on clocking schemes and phase lock loops are discussed. Chapter 3 addresses the
synchronous/asynchronous aspects of the real world, and methods to resolve those issues.
Metastability is explained, MTBF calculations are defined, and solutions in the handling of
metastability are presented. The design of an asynchronous FIFO is demonstrated. The topic
of crossing clock domains is also presented. Chapter 4 addresses the verification issue and
presents through two examples the transaction-based verification methodology. The topic of
forcing design errors is also demonstrated in those examples, including the verification of a
loadable counter and an EDAC model for a thirty-two-bit wide memory. Chapter 5 focuses
on control machines and uses a very simple CPU design to demonstrate implementation
methodologies with FSM and microprogrammed solutions. Chapter 6 addresses arithmetic
intensive machines. It explains the application of SIGNED and UNSIGNED types in HDL.
Verilog 1995/2001 type issues are demonstrated. Chapter 7 explains and demonstrates
mixed mode simulations and synthesis. Chapter 8 presents a discussion on minimizing
design errors and addresses miscellaneous design issues. Chapter 9 compares Verilog to
VHDL to enable users of one discipline to understand the language differences and nuances of
the other discipline. It also provides Verilog coding style guidelines for VHDL and Verilog
users.
Preface xv
All HDL code described in the book is on a companion CD. All code was verified and
simulated with NC-Sim version v03.30.11. All synthesizable code was synthesized with
Synplify Pro® version 6.2.42. The CD also includes application notes and files of practical
use that were collected over a period of several years. EMACS editor for Windows, along
with VHDL and Verilog modes is on CD. The CD includes data sheets and additional
information on Synplicity’s product line, and excellent Cadence’s Verilog reference and HDL
simulation documentation.
This book is intended for:
1. Engineers. Book provides classes of architectural examples and decomposition into
HDLs. Engineers are better at copying and improving upon what is done, than from
starting from scratch. This book will provide a head start in these processes.
2. Trainers. This book provides the focus of an advanced hardware design class using
HDLs. Emphasis is on architecture, processes, methodologies, and style.
3. College students. Book demonstrates the hardware architectural processes.
A list of Verilog books that are often recommended includes:
Verilog HDL : A Guide to Digital Design and Synthesis, Samir Palnitkar, 396 pages, Prentice
Hall 1996, ISBN: 0134516753
Verilog HDL Synthesis, A Practical Primer, J. Bhasker, 236 pages, Star Galaxy Publishing,
ISBN 0-9650391-5-3
A Verilog HDL Primer, Second Edition, J. Bhasker, 1999, Star Galaxy Publishing, ISBN 0-
9650391-7-X.
For information about the new features of Verilog, I recommend the book VERILOG 2001, A
Guide to the New Features of the Verilog Hardware Description Language, Stuart
Sutherland, 2002, Kluwer Academic Publishers, ISBN 0-7923-7568-8
For VHDL, I second Janick Bergeron's recommendation3 for the book VHDL Coding Styles
and Methodologies, 2nd Edition, 1999, Kluwer Academic Publishers, ISBN 0-7923-8474-1.
Another highly recommended book for VHDL is The Designer's Guide to VHDL, 2nd
Edition, Peter J. Ashenden, 740 pages, Morgan Kaufmann Publishers, ISBN 1558606912
3Writing testbenches, Functional verification of HDL models, Janick Bergeron, Kluwer Academic
Publishers 2000
4 http://janick.bergeron.com/
About The CD
Table 1 summarizes the contents of the enclosed CD.
Chapter 2
MODEL FIGURE DESCRIPTION
ch2/ff_reset.v 2.1-1 Verilog Resettable Flip-Flop
ch2/ff_reset.vhd 2.1-2 VHDL Resettable Flip-Flop
ch2/latch.v 2.2-1 Verilog Latch
ch2/latch.vhd 2.2-2 VHDL Latch
ch2/oneshot.v 2.3-2 Verilog Code for Edge-Detect and Testbench
ch2/oneshot.vhd 2.3-2 VHDL Code for Edge-Detect and Testbench
Using Negative Edge of Flip-flop to Clock Data,
ch2/negposff.v 2.4.2-1 and using Positive edge of Clock to Output Data to
System
Simple Serial to Parallel Converter with External
ch2/ser2parallel.v 2.5.1-1
Synchronization
Simple Serial to Parallel Converter with External
ch2/ser2parallel.vhd 2.5.1-2
Synchronization
ch2/parallel2ser.v 2.5.2-1 Verilog Parallel to Serial Converter
ch2/parallel2ser.vhd 2.5.2-2 VHDL Parallel to Serial Converter
ch2/regfile.v 2.5.3-1 Register File Inference in Verilog
ch2/regfile.vhd 2.5.3-2 Register File Inference in VHDL
ch2/counter.v 2.6.1-1 Binary Verilog Counter
ch2/counter.vhd 2.6.1-2 Binary VHDL Counter
Counter Example with Numeric_Unsigned
ch2/counterun.vhd 2.6.1-4
Package
ch2/dncounter.v 2.6.2-1 Verilog Model of a Loadable Down-Counter
ch2/dncounter.vhd 2.6.2-2 VHDL Model of a Loadable Down-Counter
Down-Counter Configured as a Free-Running
ch2/freerundncntr.v 2.6.3-2
Counter
Down-Counter Configured as a Free-Running
ch2/ freerundncntr.vhd 2.6.3-3
Counter
ch2/counter5.v 2.6.4-1 Simple 5-Bit Up-Counter with Terminal Count
xviii Real Chip Design and Verification
Using Verilog and VHDL
ch2/counter5.vhd 2.6.4-2 Simple 5-Bit Up-Counter with Terminal Count
ch2/lfsr4.vhd &
2.6.5-1 Simulation of Four-bit LFSR
ch2/TestLFSR_tb.vhd
ch2/lfsr4count.vhd 2.6.5-1 Four-Bit LFSR counter that counts to 16 states
Model of the testbench for the LFSR terminal
ch2/TestLFSR4_tb.vhd 2.6.5-3
counter
ch2/lfsr4count.v 2.6.5-5 Verilog Model for 4-bit LFSR Counter
ch2/lfsr4count_tb.v 2.6.5-5 Verilog Testbench for 4-bit LFSR Counter
ch2/counter1hot.v 2.6.6-1 Verilog Model of One-Hot Counter
ch2/counter1hot.vhd 2.6.6-2 VHDL Model of One-Hot Counter
ch2/counter1hotsafe.v 2.6.6-4 Verilog Model of Safe One-Hot Counter
Verilog Model testbench for the safe One-Hot
ch2/counter1hotsafetb.v 2.6.6-6
Counter
ch2/counter1hotsafe.vhd 2.6.6-7 VHDL Model of Safe One-Hot Counter
Non-Working but Conceptually Correct Gray-to-
ch2/gray2bin_bad.v 2.6.7.2-2
Binary Verilog Model
ch2/gray2bin.vhd 2.6.7.2-3 Working Gray-to-Binary VHDL Model
Parameterized and correct gray-to-binary Verilog
ch2/gray2bin.v 2.6.7.2-4
model
ch2/bin2gray.v 2.6.7.3-1 Verilog Binay to Gray Converter
ch2/bin2gray.vhd 2.6.7.3-2 VHDL Binay to Gray Converter
ch2/graycntr.v 2.6.8-1 Verilog Gray Code Counter with binary output
ch2/graycntr_lookup.v 2.6.8-10 Verilog Gray Code Counter lookup style
ch2/graycntr.vhd 2.6.8-1 VHDL Gray Code Counter with binary output
ch2/graycntr_lookup.vhd 2.6.8-9 VHDL Gray Code Counter lookup style
ch2/graycntr_tb.v 2.6.8-5 Verilog Testbench for the Graycode Counter
ch2/graycntr_tb.vhd 2.6.8-7 VHDL Testbench for the Graycode Counter
ch2/Johnson.v 2.6.9-1 Verilog Johnson Counter with terminal count
ch2/Johnson.vhd 2.6.9-4 VHDL Johnson Counter
ch2/rom2.v 2.7.1-1 Verilog ROM Model
ch2/rom6.vhd 2.7.1-5 VHDL ROM Model
ch2/rom6const.vhd 2.7.1-6 VHDL ROM Implication with Constants
ch2/romgen/vhdlsin.c 2.7.1.1-1 C program to generate the Verilog SINE ROM
ch2/romgen/vhdlsin.h 2.7.1.1-2 C support file: vhdlsin.h
Preface xix
CC See http://www.cadence.com/
http://www.cadence.com/products/index.html
vlogrefTOC.html Verilog-XL Reference, Product Version 3.3
ncvlogTOC.html Cadence NC-Verilog Simulator Help, Product Version 3.3
ncvlogPN Cadence NC-Verilog Simulator Product Notes, Version 3.3
ncvhdlTOC.html Cadence NC-VHDL Simulator Help, Product Version 3.3
Cadence ncvhdlPN Cadence NC-VHDL Simulator Product Notes, Version 3.3
nccoexPNTOC. Cadence NC-Sim Mixed Language Simulator Notes, V 3.3
5 http://www.synplicity.com/
6 http://www.cadence.com/
7 http://www.sunburst-design.com/ (Expert Verilog, Synthesis and Verification Training)
8 http://www.memecdesign.com/
9 http://rk.gsfc.nasa.gov/
xxvi Real Chip Design and Verification
Using Verilog and VHDL
VhdlCohen Publishing
Email: VhdlCohen@aol.com
Web page: http://www.vhdlcohen.com/
xxviii Real Chip Design and Verification
Using Verilog and VHDL
DISCLAIMER
Every attempt was made to ensure accuracy in the specifications and implementation of the
models. However, All code provided in this book and in the accompanied CD is distributed
with *ABSOLUTELY NO SUPPORT* and *NO WARRANTY* from the author. The author
shall not be liable for damage in connection with, or arising out of, the furnishing,
performance or use of the models provided in the book and CD.
Use or reproduction of the information provided in this book and on the enclosed CD for
commercial gain is strictly prohibited.