372 68 5MB
English Pages 512 Year 2005
Digital Logic and Microprocessor Design With VHDL
Enoch O. Hwang La Sierra University, Riverside
%URRNV&ROH ,6%1 ſ7HDP(/ (&7521L;
To my wife and children, Windy, Jonathan and Michelle
Contents Contents .................................................................................................................................................................... Preface ...................................................................................................................................................................
Chapter 1 Designing Microprocessors...................................................................................... 1.1 Overview of a Microprocessor ....................................................................................................................... 1.2 Design Abstraction Levels.............................................................................................................................. 1.3 Examples of a 2-to-1 Multiplexer ................................................................................................................... 1.3.1 Behavioral Level.................................................................................................................................... 1.3.2 Gate Level.............................................................................................................................................. 1.3.3 Transistor Level ..................................................................................................................................... 1.4 Introduction to VHDL .................................................................................................................................... 1.5 Synthesis....................................................................................................................................................... 1.6 Going Forward.............................................................................................................................................. 1.7 Summary Checklist....................................................................................................................................... 1.8 Problems .......................................................................................................................................................
Chapter 2 Digital Circuits.......................................................................................................... 2 2.1 Binary Numbers.............................................................................................................................................. 3 2.2 Binary Switch ................................................................................................................................................. 2.3 Basic Logic Operators and Logic Expressions ............................................................................................... 2.4 Truth Tables.................................................................................................................................................... 2.5 Boolean Algebra and Boolean Function ......................................................................................................... 2.5.1 Boolean Algebra .................................................................................................................................... 2.5.2 * Duality Principle ............................................................................................................................... 2.5.3 Boolean Function and the Inverse........................................................................................................ 2.6 Minterms and Maxterms............................................................................................................................... 2.6.1 Minterms.............................................................................................................................................. 2.6.2 * Maxterms .......................................................................................................................................... 2.7 Canonical, Standard, and non-Standard Forms............................................................................................. 2.8 Logic Gates and Circuit Diagrams................................................................................................................ 2.9 Example: Designing a Car Security System ................................................................................................. 2.10 VHDL for Digital Circuits............................................................................................................................ 2.10.1 VHDL code for a 2-input NAND gate................................................................................................. 2.10.2 VHDL code for a 3-input NOR gate.................................................................................................... 2.10.3 VHDL code for a function ................................................................................................................... 2.11 Summary Checklist....................................................................................................................................... 2.12 Problems .......................................................................................................................................................
Chapter 3 Combinational Circuits............................................................................................ 3.1 Analysis of Combinational Circuits................................................................................................................ 3.1.1 Using a Truth Table ............................................................................................................................... 3.1.2 Using a Boolean Function...................................................................................................................... 3.2 Synthesis of Combinational Circuits .............................................................................................................. 3.3 * Technology Mapping................................................................................................................................... 3.4 Minimization of Combinational Circuits ...................................................................................................... 3.4.1 Karnaugh Maps.................................................................................................................................... 3.4.2 Don’t-cares .......................................................................................................................................... 3.4.3 * Tabulation Method............................................................................................................................ 3.5 * Timing Hazards and Glitches ....................................................................................................................
5
3.5.1 Using Glitches ..................................................................................................................................... 3.6 BCD to 7-Segment Decoder ......................................................................................................................... 3.7 VHDL for Combinational Circuits ............................................................................................................... 3.7.1 Structural BCD to 7-Segment Decoder................................................................................................ 3.7.2 Dataflow BCD to 7-Segment Decoder ................................................................................................ 3.7.3 Behavioral BCD to 7-Segment Decoder.............................................................................................. 3.8 Summary Checklist....................................................................................................................................... 3.9 Problems .......................................................................................................................................................
Chapter 4 Standard Combinational Components................................................................... 4.1 Signal Naming Conventions ........................................................................................................................... 4.2 Adder .............................................................................................................................................................. 4.2.1 Full Adder.............................................................................................................................................. 4.2.2 Ripple-carry Adder ................................................................................................................................ 4.2.3 * Carry-lookahead Adder....................................................................................................................... 4.3 Two’s Complement Binary Numbers ............................................................................................................. 4.4 Subtractor........................................................................................................................................................ 4.5 Adder-Subtractor Combination..................................................................................................................... 4.6 Arithmetic Logic Unit................................................................................................................................... 4.7 Decoder......................................................................................................................................................... 4.8 Encoder......................................................................................................................................................... 4.8.1 * Priority Encoder................................................................................................................................ 4.9 Multiplexer ................................................................................................................................................... 4.9.1 * Using Multiplexers to Implement a Function ................................................................................... 4.10 Tri-state Buffer ............................................................................................................................................. 4.11 Comparator ................................................................................................................................................... 4.12 Shifter ........................................................................................................................................................... 4.12.1 * Barrel Shifter .................................................................................................................................... 4.13 * Multiplier ................................................................................................................................................... 4.14 Summary Checklist....................................................................................................................................... 4.15 Problems .......................................................................................................................................................
Chapter 5 * Implementation Technologies ............................................................................. 5.1 Physical Abstraction ....................................................................................................................................... 5.2 Metal-Oxide-Semiconductor Field-Effect Transistor (MOSFET).................................................................. 5.3 CMOS Logic................................................................................................................................................... 5.4 CMOS Circuits ............................................................................................................................................... 5.4.1 CMOS Inverter ...................................................................................................................................... 5.4.2 CMOS NAND gate................................................................................................................................ 5.4.3 CMOS AND gate................................................................................................................................... 5.4.4 CMOS NOR and OR Gates ................................................................................................................ 1 5.4.5 Transmission Gate ............................................................................................................................... 5.4.6 2-input Multiplexer CMOS Circuit.....................................................................................................1 5.4.7 CMOS XOR and XNOR Gates............................................................................................................1 5.5 Analysis of CMOS Circuits .......................................................................................................................... 1 5.6 Using ROMs to Implement a Function ......................................................................................................... 15 5.7 Using PLAs to Implement a Function .......................................................................................................... 1 5.8 Using PALs to Implement a Function ..........................................................................................................1 5.9 Complex Programmable Logic Device (CPLD) ........................................................................................... 5.10 Field Programmable Gate Array (FPGA) ..................................................................................................... 5.11 Summary Checklist....................................................................................................................................... 5.12 Problems .......................................................................................................................................................
6
Chapter 6 Latches and Flip-Flops ............................................................................................ 6.1 Bistable Element............................................................................................................................................. 6.2 SR Latch ......................................................................................................................................................... 6.3 SR Latch with Enable ..................................................................................................................................... 6.4 D Latch ........................................................................................................................................................... 6.5 D Latch with Enable ....................................................................................................................................... 6.6 Clock............................................................................................................................................................... 6.7 D Flip-Flop ................................................................................................................................................... 1 6.7.1 * Alternative Smaller Circuit ............................................................................................................... 1 6.8 D Flip-Flop with Enable ............................................................................................................................... 1 6.9 Asynchronous Inputs .................................................................................................................................... 1 6.10 Description of a Flip-Flop ............................................................................................................................ 6.10.1 Characteristic Table ............................................................................................................................. 1 6.10.2 Characteristic Equation........................................................................................................................ 1 6.10.3 State Diagram ...................................................................................................................................... 1 6.10.4 Excitation Table................................................................................................................................... 1 6.11 Timing Issues................................................................................................................................................ 1 6.12 Example: Car Security System – Version 2.................................................................................................. 6.13 VHDL for Latches and Flip-Flops................................................................................................................ 1 6.13.1 Implied Memory Element.................................................................................................................... 1 6.13.2 VHDL Code for a D Latch with Enable .............................................................................................. 6.13.3 VHDL Code for a D Flip-Flop ............................................................................................................ 19 6.13.4 VHDL Code for a D Flip-Flop with Enable and Asynchronous Set and Clear ................................... 6.14 * Flip-Flop Types ......................................................................................................................................... 6.14.1 SR Flip-Flop ........................................................................................................................................ 6.14.2 JK Flip-Flop......................................................................................................................................... 6.14.3 T Flip-Flop........................................................................................................................................... 6.15 Summary Checklist....................................................................................................................................... 6.16 Problems .......................................................................................................................................................
Chapter 7 Sequential Circuits ................................................................................................... 2 7.1 Finite-State-Machine (FSM) Models.............................................................................................................. 7.2 State Diagrams................................................................................................................................................ 7.3 Analysis of Sequential Circuits....................................................................................................................... 7.3.1 Excitation Equation ............................................................................................................................... 7.3.2 Next-state Equation ............................................................................................................................... 7.3.3 Next-state Table..................................................................................................................................... 7.3.4 Output Equation................................................................................................................................... 7.3.5 Output Table ........................................................................................................................................ 7.3.6 State Diagram ......................................................................................................................................0 7.3.7 Example: Analysis of a Moore FSM ................................................................................................... 7.3.8 Example: Analysis of a Mealy FSM.................................................................................................... 7.4 Synthesis of Sequential Circuits ................................................................................................................... 7.4.1 State Diagram ...................................................................................................................................... 7.4.2 Next-state Table................................................................................................................................... 7.4.3 Implementation Table.......................................................................................................................... 7.4.4 Excitation Equation and Next-state Circuit ......................................................................................... 7.4.5 Output Table and Equation .................................................................................................................. 1 7.4.6 FSM Circuit ......................................................................................................................................... 7.4.7 Examples: Synthesis of Moore FSMs.................................................................................................. 7.4.8 Example: Synthesis of a Mealy FSM................................................................................................... 7.5 Unused State Encodings and the Encoding of States.................................................................................... 7.6 Example: Car Security System – Version 3.................................................................................................. 7.7 VHDL for Sequential Circuits ......................................................................................................................
7
7.8 * Optimization for Sequential Circuits ......................................................................................................... 7.8.1 State Reduction.................................................................................................................................... 3 7.8.2 State Encoding ..................................................................................................................................... 7.8.3 Choice of Flip-Flops ............................................................................................................................ 7.9 Summary Checklist....................................................................................................................................... 7.10 Problems .......................................................................................................................................................
Chapter 8 Standard Sequential Components .......................................................................... 2 8.1 Registers ......................................................................................................................................................... 8.2 Shift Registers................................................................................................................................................. 8.2.1 Serial-to-Parallel Shift Register ............................................................................................................. 8.2.2 Serial-to-Parallel and Parallel-to-Serial Shift Register .......................................................................... 8.3 Counters.......................................................................................................................................................... 8.3.1 Binary Up Counter................................................................................................................................. 8.3.2 Binary Up-Down Counter.................................................................................................................... 8.3.3 Binary Up-Down Counter with Parallel Load ..................................................................................... 8.3.4 BCD Up Counter ................................................................................................................................. 8.3.5 BCD Up-Down Counter ...................................................................................................................... 8.4 Register Files ................................................................................................................................................ 8.5 Static Random Access Memory.................................................................................................................... 2 8.6 * Larger Memories ....................................................................................................................................... 8.6.1 More Memory Locations ..................................................................................................................... 2 8.6.2 Wider Bit Width .................................................................................................................................. 2 8.7 Summary Checklist....................................................................................................................................... 2 8.8 Problems ....................................................................................................................................................... 2
Chapter 9 Datapaths .................................................................................................................. 2 9.1 Designing Dedicated Datapaths...................................................................................................................... 9.1.1 Selecting Registers................................................................................................................................. 9.1.2 Selecting Functional Units..................................................................................................................... 9.1.3 Data Transfer Methods .......................................................................................................................... 9.1.4 Generating Status Signals .................................................................................................................... 9.2 Using Dedicated Datapaths........................................................................................................................... 9.3 Examples of Dedicated Datapaths ................................................................................................................ 9.3.1 Simple IF-THEN-ELSE....................................................................................................................... 9.3.2 Counting 1 to 10 .................................................................................................................................. 9.3.3 Summation of n down to 1................................................................................................................... 9.3.4 Factorial ............................................................................................................................................... 9.3.5 Count Zero-One ................................................................................................................................... 9.4 General Datapaths......................................................................................................................................... 9.5 Using General Datapaths .............................................................................................................................. 9.6 A More Complex General Datapath ............................................................................................................. 9.7 Timing Issues................................................................................................................................................ 9.8 VHDL for Datapaths..................................................................................................................................... 9.8.1 Dedicated Datapath.............................................................................................................................. 9.8.2 General Datapath ................................................................................................................................. 9.9 Summary Checklist....................................................................................................................................... 9.10 Problems .......................................................................................................................................................
Chapter 10 Control Units ............................................................................................................ 10.1 Constructing the Control Unit......................................................................................................................... 10.2 Examples ........................................................................................................................................................
8
10.2.1 Count 1 to 10 ......................................................................................................................................... 10.2.2 Summation of 1 to n .............................................................................................................................. 10.3 Generating Status Signals ............................................................................................................................. 10.4 Timing Issues................................................................................................................................................ 10.5 Standalone Controllers.................................................................................................................................. 10.5.1 Rotating Lights .................................................................................................................................... 10.5.2 PS/2 Keyboard Controller.................................................................................................................... 10.5.3 VGA Monitor Controller .....................................................................................................................6 10.6 * ASM Charts and State Action Tables ........................................................................................................ 37 10.6.1 ASM Charts ........................................................................................................................................37 10.6.2 State Action Tables..............................................................................................................................0 10.7 VHDL for Control Units...............................................................................................................................1 10.8 Summary Checklist.......................................................................................................................................2 10.9 Problems .......................................................................................................................................................4
Chapter 11 Dedicated Microprocessors ..................................................................................... 11.1 Manual Construction of a Dedicated Microprocessor .................................................................................... 11.2 Examples ........................................................................................................................................................ 11.2.1 Greatest Common Divisor ..................................................................................................................... 11.2.2 Summing Input Numbers..................................................................................................................... 11.2.3 High-Low Guessing Game .................................................................................................................. 11.2.4 Finding Largest Number ...................................................................................................................... 11.3 VHDL for Dedicated Microprocessors......................................................................................................... 11.3.1 FSM + D Model................................................................................................................................... 11.3.2 FSMD Model ....................................................................................................................................... 11.3.3 Behavioral Model ................................................................................................................................ 11.4 Summary Checklist....................................................................................................................................... 11.5 Problems .......................................................................................................................................................
Chapter 12 General-Purpose Microprocessors ......................................................................... 12.1 Overview of the CPU Design ......................................................................................................................... 12.2 The EC-1 General-Purpose Microprocessor ................................................................................................... 12.2.1 Instruction Set........................................................................................................................................ 12.2.2 Datapath................................................................................................................................................. 5 12.2.3 Control Unit ........................................................................................................................................... 12.2.4 Complete Circuit.................................................................................................................................... 12.2.5 Sample Program...................................................................................................................................0 12.2.6 Simulation............................................................................................................................................ 12.2.7 Hardware Implementation ...................................................................................................................2 12.3 The EC-2 General-Purpose Microprocessor .................................................................................................3 12.3.1 Instruction Set...................................................................................................................................... 12.3.2 Datapath...............................................................................................................................................4 12.3.3 Control Unit .........................................................................................................................................5 12.3.4 Complete Circuit..................................................................................................................................8 12.3.5 Sample Program...................................................................................................................................9 12.3.6 Hardware Implementation ...................................................................................................................1 12.4 VHDL for General-Purpose Microprocessors ..............................................................................................2 12.4.1 Structural FSM+D ...............................................................................................................................2 12.4.2 Behavioral FSMD ................................................................................................................................9 12.5 Summary Checklist.......................................................................................................................................2 12.6 Problems .......................................................................................................................................................2
9
Appendix A Schematic Entry Tutorial 1 .................................................................................... A.1 Getting Started ................................................................................................................................................ A.1.1 Preparing a Folder for the Project.......................................................................................................... A.1.2 Starting MAX+plus II............................................................................................................................ A.1.3 Starting the Graphic Editor .................................................................................................................... A.2 Using the Graphic Editor ................................................................................................................................ 4 A.2.1 Drawing Tools ....................................................................................................................................... 4 A.2.2 Inserting Logic Symbols........................................................................................................................ 4 A.2.3 Selecting, Moving, Copying, and Deleting Logic Symbols................................................................... A.2.4 Making and Naming Connections ......................................................................................................... 6 A.2.5 Selecting, Moving and Deleting Connection Lines ............................................................................... A.3 Specifying the Top-Level File and Project ..................................................................................................... A.3.1 Saving the Schematic Drawing.............................................................................................................. A.3.2 Specifying the Project............................................................................................................................ A.4 Synthesis for Functional Simulation............................................................................................................... A.5 Circuit Simulation........................................................................................................................................... A.5.1 Selecting Input Test Signals .................................................................................................................. A.5.2 Customizing the Waveform Editor ...................................................................................................... A.5.3 Assigning Values to the Input Signals ................................................................................................. A.5.4 Saving the Waveform File ................................................................................................................... A.5.5 Starting the Simulator .......................................................................................................................... A.6 Creating and Using the Logic Symbol..........................................................................................................
Appendix B VHDL Entry Tutorial 2........................................................................................... B.1 Getting Started ................................................................................................................................................ B.1.1 Preparing a Folder for the Project.......................................................................................................... B.1.2 Starting MAX+plus II............................................................................................................................ B.1.3 Creating a Project .................................................................................................................................. B.1.4 Editing the VHDL Source Code ............................................................................................................ B.2 Synthesis for Functional Simulation............................................................................................................... B.3 Circuit Simulation........................................................................................................................................... B.3.1 Selecting Input Test Signals .................................................................................................................. B.3.2 Customizing the Waveform Editor ........................................................................................................ B.3.3 Assigning Values to the Input Signals ................................................................................................... B.3.4 Saving the Waveform File ..................................................................................................................... B.3.5 Starting the Simulator ............................................................................................................................
Appendix C UP2 Programming Tutorial 3................................................................................. C.1 Getting Started ................................................................................................................................................ C.1.1 Preparing a Folder for the Project.......................................................................................................... C.1.2 Creating a Project .................................................................................................................................. C.1.3 Viewing the Source File ........................................................................................................................ C.2 Synthesis for Programming the PLD .............................................................................................................. C.3 Circuit Simulation........................................................................................................................................... C.4 Using the Floorplan Editor ............................................................................................................................. C.4.1 Selecting the Target Device ................................................................................................................... C.4.2 Maping the I/O Pins with the Floorplan Editor...................................................................................... C.5 Fitting the Netlist and Pins to the PLD ........................................................................................................... C.6 Hardware Setup ............................................................................................................................................ C.6.1 Installing the ByteBlaster Driver ......................................................................................................... C.6.2 Jumper Settings.................................................................................................................................... C.6.3 Hardware Connections........................................................................................................................ C.7 Programming the PLD .................................................................................................................................. C.8 Testing the Hardware.................................................................................................................................... C.9 MAX7000S EPM7128SLC84-7 Summary...................................................................................................
10
C.9.1 JTAG Jumper Settings ......................................................................................................................... C.9.2 Prototyping Resources for Use ............................................................................................................ C.9.3 General Pin Assignments..................................................................................................................... C.9.4 Two Pushbutton Switches.................................................................................................................... C.9.5 16 DIP Switches .................................................................................................................................. C.9.6 16 LEDs............................................................................................................................................... C.9.7 7-Segment LEDs.................................................................................................................................. C.9.8 Clock.................................................................................................................................................... C.10 FLEX10K EPF10K70RC240-4 Summary.................................................................................................... C.10.1 JTAG Jumper Settings ......................................................................................................................... C.10.2 Prototyping Resources for Use ............................................................................................................ C.10.3 Two Pushbutton Switches.................................................................................................................... C.10.4 8 DIP Switches .................................................................................................................................... C.10.5 7-Segment LEDs.................................................................................................................................. C.10.6 Clock.................................................................................................................................................... C.10.7 PS/2 Port .............................................................................................................................................. C.10.8 VGA Port.............................................................................................................................................
Appendix D VHDL Summary...................................................................................................... D.1 Basic Language Elements............................................................................................................................... D.1.1 Comments .............................................................................................................................................. D.1.2 Identifiers............................................................................................................................................... D.1.3 Data Objects .......................................................................................................................................... D.1.4 Data Types ............................................................................................................................................. D.1.5 Data Operators ....................................................................................................................................... D.1.6 ENTITY................................................................................................................................................. D.1.7 ARCHITECTURE................................................................................................................................. D.1.8 GENERIC .............................................................................................................................................. D.1.9 PACKAGE ............................................................................................................................................ D.2 Dataflow Model Concurrent Statements....................................................................................................... D.2.1 Concurrent Signal Assignment ............................................................................................................0 D.2.2 Conditional Signal Assignment ........................................................................................................... D.2.3 Selected Signal Assignment................................................................................................................. D.2.4 Dataflow Model Example....................................................................................................................2 D.3 Behavioral Model Sequential Statements ..................................................................................................... D.3.1 PROCESS ............................................................................................................................................ D.3.2 Sequential Signal Assignment ............................................................................................................. D.3.3 Variable Assignment ........................................................................................................................... D.3.4 WAIT................................................................................................................................................... D.3.5 IF THEN ELSE.................................................................................................................................... D.3.6 CASE ...................................................................................................................................................4 D.3.7 NULL................................................................................................................................................... D.3.8 FOR ..................................................................................................................................................... D.3.9 WHILE ................................................................................................................................................ D.3.10 LOOP................................................................................................................................................... D.3.11 EXIT .................................................................................................................................................... D.3.12 NEXT................................................................................................................................................... D.3.13 FUNCTION ......................................................................................................................................... D.3.14 PROCEDURE...................................................................................................................................... D.3.15 Behavioral Model Example ................................................................................................................. D.4 Structural Model Statements......................................................................................................................... D.4.1 COMPONENT Declaration................................................................................................................. D.4.2 PORT MAP ......................................................................................................................................... D.4.3 OPEN................................................................................................................................................... D.4.4 GENERATE ........................................................................................................................................ D.4.5 Structural Model Example ...................................................................................................................
11
D.5 Conversion Routines.....................................................................................................................................1 D.5.1 CONV_INTEGER() ............................................................................................................................1 D.5.2 CONV_STD_LOGIC_VECTOR(,).....................................................................................................
12
Digital Logic and Microprocessor Design with VHDL
Preface
Preface This book is about the digital logic design of microprocessors. It is intended to provide both an understanding of the basic principles of digital logic design, and how these fundamental principles are applied in the building of complex microprocessor circuits using current technologies. Although the basic principles of digital logic design have not changed, the design process, and the implementation of the circuits have changed. With the advances in fully integrated modern computer aided design (CAD) tools for logic synthesis, simulation, and the implementation of circuits in programmable logic devices (PLDs) such as field programmable gate arrays (FPGAs), it is now possible to design and implement complex digital circuits very easily and quickly. Many excellent books on digital logic design have followed the traditional approach of introducing the basic principles and theories of logic design, and the building of separate combinational and sequential components. However, students are left to wonder about the purpose of these individual components, and how they are used in the building of microprocessors – the ultimate in digital circuits. One primary goal of this book is to fill in this gap by going beyond the logic principles, and the building of individual components. The use of these principles and the individual components are combined together to create datapaths and control units, and finally the building of real dedicated custom microprocessors and general-purpose microprocessors. Previous logic design and implementation techniques mainly focus on the logic gate level. At this low level, it is difficult to discuss larger and more complex circuits beyond the standard combinational and sequential circuits. However, with the introduction of the register-transfer technique for designing datapaths, and the concept of a finitestate machine for control units, we can easily implement an arbitrary algorithm as a dedicated microprocessor in hardware. The construction of a general-purpose microprocessor then comes naturally as a generalization of a dedicated microprocessor. With the provided CAD tool, and the optional FPGA hardware development kit, students can actually implement these microprocessor circuits, and see them execute, both in software simulation, and in hardware. The book contains many interesting examples with complete circuit schematic diagrams, and VHDL codes for both simulation and implementation in hardware. With the hands-on exercises, the student will learn not only the principles of digital logic design, but also in practice, how circuits are implemented using current technologies. To actually see your own microprocessor comes to life in real hardware is an exciting experience. Hopefully, this will help the students to not only remember what they have learned, but will also get them interested in the world of digital circuit design.
Advanced and Historical Topics Sections that are designated with an asterisk ( * ) are either advanced topics, or topics for a historical perspective. These sections may be skipped without any loss of continuity in learning how to design a microprocessor.
Summary Checklist There is a chapter summary checklist at the end of each chapter. These checklists provide a quick way for students to evaluate whether they have understood the materials presented in the chapter. The items in the checklists are divided into two categories. The first set of items deal with new concepts, ideas, and definitions, while the second set deals with practical how to do something types.
Design of Circuits Using VHDL Although this book provides coverage on VHDL for all the circuits, it can be omitted entirely for the understanding and designing of digital circuits. For an introductory course in digital logic design, learning the basic principles is more important than learning how to use a hardware description language. In fact, instructors may find that students may get lost in learning the principles while trying to learn the language at the same time. With this in mind, the VHDL code in the text is totally independent of the presentation of each topic, and may be skipped without any loss of continuity.
13
Digital Logic and Microprocessor Design with VHDL
Preface
On the other hand, by studying the VHDL codes, the student can not only learn the use of a hardware description language, but also learn how digital circuits can be designed automatically using a synthesizer. This book provides a basic introduction to VHDL, and uses the learn-by-examples approach. In writing VHDL code at the dataflow and behavioral levels, the student will see the power and usefulness of a state-of-the-art CAD synthesis tool.
Using this Book This book can be used in either an introductory, or a more advanced course in digital logic design. For an introductory course with no previous background in logic, Chapters 1 to 4 are intended to provide the fundamental concepts in designing combinational circuits, and Chapters 6 to 8 cover the basic sequential circuits. Chapters 9 to 12 on microprocessor design can be introduced and covered lightly. For an advanced course where students already have an exposure to logic gates and simple digital circuits, Chapters 1 to 4 will serve as a review. The focus should be on the register-transfer design of datapaths and control units, and the building of dedicated and general-purpose microprocessors as covered in Chapters 9 to 12. A lab component should complement the course where students can have a hands-on experience in implementing the circuits presented using the included CAD software, and the optional development kit. A brief summary of the topics covered in each chapter follows. Chapter 1 – Designing a Microprocessor gives an overview of the various components of a microprocessor circuit, and the different abstraction levels in which a circuit can be designed. Chapter 2 – Digital Circuits provides the basic principles and theories for designing digital logic circuits by introducing the use of truth tables and Boolean algebra, and how the theories get translated into logic gates, and circuit diagrams. A brief introduction to VHDL is also given. Chapter 3 – Combinational Circuits shows how combinational circuits are analyzed, synthesized and reduced. Chapter 4 – Combinational Components discusses the standard combinational components that are used as building blocks for larger digital circuits. These components include adder, subtractor, arithmetic logic unit, decoder, encoder, multiplexer, tri-state buffer, comparator, shifter, and multiplier. In a hierarchical design, these components will be used to build larger circuits such as the microprocessor. Chapter 5 – Implementation Technologies digresses a little by looking at how logic gates are implemented at the transistor level, and the various programmable logic devices available for implementing digital circuits. Chapter 6 – Latches and Flip-Flops introduces the basic storage elements, specifically, the latch and the flipflop. Chapter 7 – Sequential Circuits shows how sequential circuits in the form of finite-state machines, are analyzed, and synthesized. This chapter also shows how the operation of sequential circuits can be precisely described using state diagrams. Chapter 8 – Sequential Components discusses the standard sequential components that are used as building blocks for larger digital circuits. These components include register, shift register, counter, register file, and memory. Similar to the combinational components, these sequential components will be used in a hierarchical fashion to build larger circuits. Chapter 9 – Datapaths introduces the register-transfer design methodology, and shows how an arbitrary algorithm can be performed by a datapath. Chapter 10 – Control Units shows how a finite-state machine (introduced in Chapter 7) is used to control the operations of a datapath so that the algorithm can be executed automatically. Chapter 11 – Dedicated Microprocessors ties the separate datapath and control unit together to form one coherent circuit – the custom dedicated microprocessor. Several complete dedicated microprocessor examples are provided. Chapter 12 – General-Purpose Microprocessors continues on from Chapter 11 to suggest that a generalpurpose microprocessor is really a dedicated microprocessor that is dedicated to only read, decode, and execute instructions. A simple general-purpose microprocessor is designed and implemented, and programs written in machine language can be executed on it.
14
Digital Logic and Microprocessor Design with VHDL
Preface
Software and Hardware Packages The newest student edition of Altera’s MAX+Plus II CAD software is included with this book on the accompanying CD-ROM. The optional UP2 hardware development kit is available from Altera at a special student price. An order form for the kit can be obtained from Altera’s website at www.altera.com. Source files for all the circuit drawings and VHDL codes presented in this book can also be found on the accompanying CD-ROM.
Website for the Book The website for this book is located at the following URL: www.cs.lasierra.edu/~ehwang The website provides many resources for both faculty and students.
Enoch O. Hwang Riverside, California
15
Chapter 1
Designing Microprocessors
Control Inputs
Data Inputs
Microprocessor Control Unit
8
'0'
Datapath
MUX ff Nextstate Logic
State Memory
Output Logic
Register
Control Signals
Status Signals Control Outputs
ALU
8
ff
Register
8 Data Outputs
Digital Logic and Microprocessor Design with VHDL
Chapter 1 - Designing Microprocessors
Being a computer science or electrical engineering student, you probably have assembled a PC before. You may have gone out to purchase the motherboard, CPU (central processing unit), memory, disk drive, video card, sound card, and other necessary parts, assembled them together, and have made yourself a state-of-the-art working computer. But have you ever wondered how the circuits inside those IC (integrated circuit) chips are designed? You know how the PC works at the system level by installing the operating system and seeing your machine come to life. But have you thought about how your PC works at the circuit level, how the memory is designed, or how the CPU circuit is designed? In this book, I will show you from the ground up, how to design the digital circuits for microprocessors, also known as CPUs. When we hear the word “microprocessor,” the first thing that probably comes to many of our minds is the Intel Pentium® CPU, which is found in most PCs. However, there are many more microprocessors that are not Pentiums, and many more microprocessors that are used in areas other than the PCs. Microprocessors are the heart of all “smart” devices, whether they be electronic devices or otherwise. Their smartness comes as a direct result of the decisions and controls that microprocessors make. For example, we usually do not consider a car to be an electronic device. However, it certainly has many complex, smart electronic systems, such as the anti-lock brakes and the fuel-injection system. Each of these systems is controlled by a microprocessor. Yes, even the black, hardened blob that looks like a dried-up and pressed-down piece of gum inside a musical greeting card is a microprocessor. There are generally two types of microprocessors: general-purpose microprocessors and dedicated microprocessors. General-purpose microprocessors, such as the Pentium CPU, can perform different tasks under the control of software instructions. General-purpose microprocessors are used in all personal computers. Dedicated microprocessors, also known as application-specific integrated circuits (ASICs), on the other hand, are designed to perform just one specific task. For example, inside your cell phone, there is a dedicated microprocessor that controls its entire operation. The embedded microprocessor inside the cell phone does nothing else but control the operation of the phone. Dedicated microprocessors are, therefore, usually much smaller and not as complex as general-purpose microprocessors. However, they are used in every smart electronic device, such as the musical greeting cards, electronic toys, TVs, cell phones, microwave ovens, and anti-lock break systems in your car. From this short list, I’m sure that you can think of many more devices that have a dedicated microprocessor inside them. Although the small dedicated microprocessors are not as powerful as the general-purpose microprocessors, they are being sold and used in a lot more places than the powerful general-purpose microprocessors that are used in personal computers. Designing and building microprocessors may sound very complicated, but don’t let that scare you, because it is not really all that difficult to understand the basic principles of how microprocessors are designed. We are not trying to design a Pentium microprocessor here, but after you have learned the material presented in this book, you will have the basic knowledge to understand how it is designed. This book will show you in an easily understandable approach, starting with the basics and leading you through to the building of larger components, such as the arithmetic logic unit (ALU), register, datapath, control unit, and finally to the building of the microprocessor — first dedicated microprocessors, and then general-purpose microprocessors. Along the way, there will be many sample circuits that you can try out and actually implement in hardware using the optional Altera UP2 development board. These circuits, forming the various components found inside a microprocessor, will be combined together at the end to produce real, working microprocessors. Yes, the exciting part is that at the end, you actually can implement your microprocessor in a real IC, and see that it really can execute software programs or make lights flash!
1.1
Overview of a Microprocessor
The Von Neumann model of a computer, shown in Figure 1.1, consists of four main components: the input, the output, the memory, and the microprocessor (or CPU). The parts that you purchased for your computer can all be categorized into one of these four groups. The keyboard and mouse are examples of input devices. The CRT (cathode ray tube) and speakers are examples of output devices. The different types of memory (cache, read-only memory (ROM), random-access memory (RAM), and the disk drive) are all considered part of the memory box in the model. In this book, the focus is not on the mechanical aspects of the input, output, and storage devices. Rather,
17
Digital Logic and Microprocessor Design with VHDL
Chapter 1 - Designing Microprocessors
the focus is on the design of the digital circuitry of the microprocessor, the memory, and other supporting digital logic circuits. The logic circuit for the microprocessor can be divided into two parts: the datapath and the control unit, as shown in Figure 1.1. Figure 1.2 shows the details inside the control unit and the datapath. The datapath is responsible for the actual execution of all data operations performed by the microprocessor, such as the addition of two numbers inside the arithmetic logic unit (ALU). The datapath also includes registers for the temporary storage of your data. The functional units inside the datapath, which in our example includes the ALU and the register, are connected together with multiplexers and data signal lines. The data signal lines are for transferring data between two functional units. Data signal lines in the circuit diagram are represented by lines connecting two functional units. Sometimes, several data signal lines are grouped together to form a bus. The width of the bus (that is, the number of data signal lines in the group) is annotated next to the bus line. In the example, the bus lines are thicker and are 8-bits wide. Multiplexers, also known as MUXes, are for selecting data from two or more sources to go to one destination. In the sample circuit, a 2-to-1 multiplexer is used to select between the input data and the constant ‘0’ to go to the left operand of the ALU. The output of the ALU is connected to the input of the register. The output of the register is connected to three different destinations: (1) the right operand of the ALU, (2) an OR gate used as a comparator for the test “not equal to 0,” and (3) a tri-state buffer. The tri-state buffer is used to control the output of the data from the register. Memory
Control Unit
Input
Datapath
Output
Microprocessor
Figure 1.1. Von Neumann model of a computer. Control Inputs
Data Inputs Control Unit
8
'0'
Datapath
MUX ff Nextstate Logic
State Memory
Output Logic
Register
Control Signals
Status Signals Control Outputs
ALU
8
ff
Register
8 Data Outputs
Figure 1.2. Internal parts of a microprocessor. Even though the datapath is capable of performing all of the data operations of the microprocessor, it cannot, however, do it on its own. In order for the datapath to execute the operations automatically, the control unit is required. The control unit, also known as the controller, controls all of the operations of the datapath, and therefore, the operations of the entire microprocessor. The control unit is a finite state machine (FSM) because it is a machine that executes by going from one state to another and that there are only a finite number of states for the machine to go to. The control unit is made up of three parts: the next-state logic, the state memory, and the output logic. The purpose of the state memory is to remember the current state that the FSM is in. The next-state logic is the circuit for
18
Digital Logic and Microprocessor Design with VHDL
Chapter 1 - Designing Microprocessors
determining what the next state should be for the machine. And the output logic is the circuit for generating the actual control signals for controlling the datapath. Every digital logic circuit, regardless of whether it is part of the control unit or the datapath, is categorized as either a combinational circuit or a sequential circuit. A combinational circuit is one where the output of the circuit is dependent only on the current inputs to the circuit. For example, an adder circuit is a combinational circuit. It takes two numbers as inputs. The adder evaluates the sum of these two numbers and outputs the result. A sequential circuit, on the other hand, is dependent not only on the current inputs, but also on all the previous inputs. In other words, a sequential circuit has to remember its past history. For example, the up-channel button on a TV remote is part of a sequential circuit. Pressing the up-channel button is the input to the circuit. However, just having this input is not enough for the circuit to determine what TV channel to display next. In addition to the upchannel button input, the circuit must also know the current channel that is being displayed, which is the history. If the current channel is channel 3, then pressing the up-channel button will change the channel to channel 4. Since sequential circuits are dependent on the history, they must therefore contain memory elements for remembering the history; whereas combinational circuits do not have memory elements. Examples of combinational circuits inside the microprocessor include the next-state logic and output logic in the control unit, and the ALU, multiplexers, tri-state buffers, and comparators in the datapath. Examples of sequential circuits include the register for the state memory in the controller and the registers in the datapath. The memory in the Von Neuman computer model is also a sequential circuit. Irregardless of whether a circuit is combinational or sequential, they are all made up of the three basic logic gates: AND, OR, and NOT gates. From these three basic gates, the most powerful computer can be made. Furthermore, these basic gates are built using transistors — the fundamental building blocks for all digital logic circuits. Transistors are just electronic binary switches that can be turned on or off. The on and off states of a transistor are used to represent the two binary values: 1 and 0. Figure 1.3 summarizes how the different parts and components fit together to form the microprocessor. From transistors, the basic logic gates are built. Logic gates are combined together to form either combinational circuits or sequential circuits. The difference between these two types of circuits is only in the way the logic gates are connected together. Latches and flip-flops are the simplest forms of sequential circuits, and they provide the basic building blocks for more complex sequential circuits. Certain combinational circuits and sequential circuits are used as standard building blocks for larger circuits, such as the microprocessor. These standard combinational and sequential components usually are found in standard libraries and serve as larger building blocks for the microprocessor. Different combinational components and sequential components are connected together to form either the datapath or the control unit of a microprocessor. Finally, combining the datapath and the control unit together will produce the circuit for either a dedicated or a general microprocessor.
19
Digital Logic and Microprocessor Design with VHDL
Chapter 1 - Designing Microprocessors
Transistors
Gates
5
2
Combinational Circuits 3
Flip-flops
6
Sequential Circuits 7 Combinational Components 4
Datapath
+
Sequential Components 8
Control Unit
9
10
Dedicated Microprocessor 11 General Microprocessor 12
Figure 1.3. Summary of how the parts of a microprocessor fit together. The numbers in each box denote the chapter number in which the topic is discussed.
1.2
Design Abstraction Levels
Digital circuits can be designed at any one of several abstraction levels. When designing a circuit at the transistor level, which is the lowest level, you are dealing with discrete transistors and connecting them together to form the circuit. The next level up in the abstraction is the gate level. At this level, you are working with logic gates to build the circuit. At the gate level, you also can specify the circuit using either a truth table or a Boolean equation. In using logic gates, a designer usually creates standard combinational and sequential components for building larger circuits. In this way, a very large circuit, such as a microprocessor, can be built in a hierarchical fashion. Design methodologies have shown that solving a problem hierarchically is always easier than trying to solve the entire problem as a whole from the ground up. These combinational and sequential components are used at the register-transfer level in building the datapath and the control unit in the microprocessor. At the register-transfer level, we are concerned with how the data is transferred between the various registers and functional units to realize or solve the problem at hand. Finally, at the highest level, which is the behavioral level, we construct the circuit by describing the behavior or operation of the circuit using a hardware description language. This is very similar to writing a computer program using a programming language.
1.3
Examples of a 2-to-1 Multiplexer
As an example, let us look at the design of the 2-to-1 multiplexer from the different abstraction levels. At this point, don’t worry too much if you don’t understand the details of how all of these circuits are built. This is intended just to give you an idea of what the description of the circuits look like at the different abstraction levels. We will get to the details in the rest of the book. An important point to gain from these examples is to see that there are many different ways to create the same functional circuit. Although they are all functionally equivalent, they are different in other respects such as size (how big the circuit is or how many transistors it uses), speed (how long it takes for the output result to be valid), cost
20
Digital Logic and Microprocessor Design with VHDL
Chapter 1 - Designing Microprocessors
(how much it costs to manufacture), and power usage (how much power it uses). Hence, when designing a circuit, besides being functionally correct, there will always be economic versus performance tradeoffs that we need to consider. The multiplexer is a component that is used a lot in the datapath. An analogy for the operation of the 2-to-1 multiplexer is similar in principle to a railroad switch in which two railroad tracks are to be merged onto one track. The switch controls which one of the two trains on the two separate tracks will move onto the one track. Similarly, the 2-to-1 multiplexer has two data inputs, d0 and d1, and a select input, s. The select input determines which data from the two data inputs will pass to the output, y. Figure 1.4 shows the graphical symbol also referred to as the logic symbol for the 2-to-1 multiplexer. From looking at the logic symbol, you can tell how many signal lines the 2-to-1 multiplexer has, and the name or function designated for each line. For the 2-to-1 multiplexer, there are two data input signals, d1 and d0, a select input signal, s, and an output signal, y.
s
d1
d0
1
0
y
Figure 1.4. Logic symbol for the 2-to-1 multiplexer.
1.3.1 Behavioral Level We can describe the operation of the 2-to-1 multiplexer simply, using the same names as in the logic symbol, by saying that d0 passes to y when s = 0, and d1 passes to y when s = 1 Or more precisely, the value that is at d0 passes to y when s = 0, and the value that is at d1 passes to y when s = 1. We use a hardware description language (HDL) to describe a circuit at the behavioral level. When describing a circuit at this level, you would write basically the same thing as in the description, except that you have to use the correct syntax required by the hardware description language. Figure 1.5 shows the description of the 2-to-1 multiplexer using the hardware description language called VHDL. LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY multiplexer IS PORT ( d0, d1, s: IN STD_LOGIC; y: OUT STD_LOGIC); END multiplexer; ARCHITECTURE Behavioral OF multiplexer IS BEGIN PROCESS(s, d0, d1) BEGIN y -- pass A through F -- AND F -- OR F -- NOT A F -- add F -- subtract F -- increment F -- decrement F Y Y Y Y Y Y Y Y NULL; END CASE; END IF; END PROCESS; END Behavioral; Figure 4.17 Behavioral VHDL code for a 3-to-8 decoder.
118
0
Digital Logic and Microprocessor Design with VHDL
4.8
Chapter 4 - Standard Combinational Components
Encoder
An encoder is almost like the inverse of a decoder where it encodes a 2n-bit input data into an n-bit code. The encoder has 2n input lines and n output lines, as shown by the logic symbol in Figure 4.18(c) for n = 3. The operation of the encoder is such that exactly one of the input lines should have a 1 while the remaining input lines should have 0’s. The output is the binary value of the index of the input line that has the 1. The truth table for an 8to-3 encoder is shown in Figure 4.18(a). For example, when input I3 is a 1, the three output bits Y2, Y1, and Y0, are set to 011, which is the binary number for the index 3. Entries having multiple 1’s in the truth table inputs are ignored, since we are assuming that only one input line can be a 1. Looking at the three output columns in the truth table, we obtain the three equations shown in Figure 4.18(b), and the resulting circuit in (c). The logic symbol is shown in Figure 4.18(d). Encoders are used to reduce the number of bits needed to represent some given data either in data storage or in data transmission. Encoders are also used in a system with 2n input devices, each of which may need to request for service. One input line is connected to one input device. The input device requesting for service will assert the input line that is connected to it. The corresponding n-bit output value will indicate to the system which of the 2n devices is requesting for service. For example, if device 5 requests for service, it will assert the I5 input line. The system will know that device 5 is requesting for service, since the output will be 101 = 5. However, this only works correctly if it is guaranteed that only one of the 2n devices will request for service at any one time. If two or more devices request for service at the same time, then the output will be incorrect. For example, if devices 1 and 4 of the 8-to-3 encoder request for service at the same time, then the output will also be 101, because I4 will assert the Y2 signal, and I1 will assert the Y0 signal. To resolve this problem, a priority is assigned to each of the input lines so that when multiple requests are made, the encoder outputs the index value of the input line with the highest priority. This modified encoder is known as a priority encoder. I7 0 0 0 0 0 0 0 1
I6 0 0 0 0 0 0 1 0
I5 0 0 0 0 0 1 0 0
I4 0 0 0 0 1 0 0 0
I3 0 0 0 1 0 0 0 0
I2 0 0 1 0 0 0 0 0
I1 0 1 0 0 0 0 0 0
I0 1 0 0 0 0 0 0 0
Y2 0 0 0 0 1 1 1 1
Y1 0 0 1 1 0 0 1 1
Y0 0 1 0 1 0 1 0 1
Y0 = I1 + I3 + I5 + I7 Y1 = I2 + I3 + I6 + I7 Y2 = I4 + I5 + I6 + I7
(b)
(a) I0 I1
Y0
I2 I3
I7 I6 I5 I4 I3 I2 I1 I0
Y1
I4 I5
Y2
Y2
Y1
I6 I7
(d)
(c) Figure 4.18 An 8-to-3 encoder: (a) truth table; (b) equations; (c) circuit; (d) logic symbol.
119
Y0
Digital Logic and Microprocessor Design with VHDL
Chapter 4 - Standard Combinational Components
4.8.1 * Priority Encoder The truth table for an active-high 8-to-3 priority encoder is shown in Figure 4.19. The table assumes that input I7 has the highest priority, and I0 has the lowest priority. For example, if the highest priority input asserted is I3, then it doesn’t matter whether the lower priority input lines, I2, I1 and I0, are asserted or not; the output will be for that of I3, which is 011. Since it is possible that no inputs are asserted, there is an extra output, Z, that is needed to differentiate between when no inputs are asserted and when one or more inputs are asserted. Z is set to a 1 when one or more inputs are asserted; otherwise, Z is set to 0. When Z is 0, all of the Y outputs are meaningless. I7 0 0 0 0 0 0 0 0 1
I6 0 0 0 0 0 0 0 1 ×
I5 0 0 0 0 0 0 1 × ×
I4 0 0 0 0 0 1 × × ×
I3 0 0 0 0 1 × × × ×
I2 0 0 0 1 × × × × ×
I1 0 0 1 × × × × × ×
I0 0 1 × × × × × × ×
Y2 × 0 0 0 0 1 1 1 1
Y1 × 0 0 1 1 0 0 1 1
Y0 × 0 1 0 1 0 1 0 1
Z 0 1 1 1 1 1 1 1 1
Figure 4.19 An 8-to-3 priority encoder truth table. An easy way to derive the equations for the 8-to-3 priority encoder is to define a set of eight intermediate variables, v0, …, v7, such that vk is a 1 if Ik is the highest priority 1 input. Thus, the equations for v0 to v7 are: v0 = I7' I6' I5' I4' I3' I2' I1' I0 v1 = I7' I6' I5' I4' I3' I2' I1 v2 = I7' I6' I5' I4' I3' I2 v3 = I7' I6' I5' I4' I3 v4 = I7' I6' I5' I4 v5 = I7' I6' I5 v 6 = I 7' I 6 v7 = I 7 Using these eight intermediate variables, the final equations for the priority encoder are similar to the ones for the regular encoder, namely: Y0 = v1 + v3 + v5 + v7 Y1 = v2 + v3 + v6 + v7 Y2 = v4 + v5 + v6 + v7 Finally, the equation for Z is simply Z = I7 + I6 + I5 + I4 + I3 + I2 + I1 + I0
4.9
Multiplexer
The multiplexer, or MUX for short, allows the selection of one input signal among n signals, where n > 1, and is a power of two. Select lines connected to the multiplexer determine which input signal is selected and passed to the output of the multiplexer. In general, an n-to-1 multiplexer has n data input lines, m select lines where m = log2 n, i.e. 2m = n, and one output line. For a 2-to-1 multiplexer, there is one select line, s, to select between the two inputs, d0 and d1. When s = 0, the input line d0 is selected, and the data present on d0 is passed to the output y. When s = 1, the input line d1 is selected and the data on d1 is passed to y. The truth table, equation, circuit, and logic symbol for a 2-to-1 multiplexer are shown in Figure 4.20.
120
Digital Logic and Microprocessor Design with VHDL
s 0 0 0 0 1 1 1 1
d1 0 0 1 1 0 0 1 1
d0 0 1 0 1 0 1 0 1
Chapter 4 - Standard Combinational Components
y 0 1 0 1 0 0 1 1
y = s'd1'd0 + s'd1d0 + sd1d0' + sd1d0 = s'd0(d1' + d1) + sd1(d0' + d0) = s'd0 + sd1
(b)
(a) d0 s
s
y
d1 d0 y
d1
(c)
(b)
Figure 4.20 A 2-to-1 multiplexer: (a) truth table; (b) equation; (c) circuit; (d) logic symbol. Constructing a larger size multiplexer, such as the 8-to-1 multiplexer, can be done similarly. In addition to having the eight data input lines, d0 to d7, the 8-to-1 multiplexer has three (23 = 8) select lines, s0, s1, and s2. Depending on the value of the three select lines, one of the eight input lines will be selected and the data on that input line will be passed to the output. For example, if the value of the select lines is 101, then the input line d5 is selected, and the data that is present on d5 will be passed to the output. The truth table, circuit, and logic symbol for the 8-to-1 multiplexer are shown in Figure 4.21. The truth table is written in a slightly different format. Instead of including the d’s in the input columns and enumerating all 211 = 2048 rows (the eleven variables come from the eight d’s and the three s’s), the d’s are written in the entry under the output column. For example, when the select line value is 101, the entry under the output column is d5, which means that y takes on the value of the input line d5. To understand the circuit in Figure 4.21(b), notice that each AND gate acts as a switch and is turned on by one combination of the three select lines. When a particular AND gate is turned on, the data at the corresponding d input is passed through that AND gate. The outputs of the remaining AND gates are all 0’s. d7
d6
d5
d4
d3
d2
d1
d0
s2
s2 0 0 0 0 1 1 1 1
s1 0 0 1 1 0 0 1 1
s0 0 1 0 1 0 1 0 1 (a)
y d0 d1 d2 d3 d4 d5 d6 d7
s1 s0
d7 d6 d5 d4 d3 d2 d1 d0 s2 s1 s0 y
y
(b)
(c)
Figure 4.21 An 8-to-1 multiplexer: (a) truth table; (b) circuit; (c) logic symbol. Instead of using 4-input AND gates (where three of its inputs are used by the three select lines to turn it on) we can use 2-input AND gates, as shown in Figure 4.22(a). This way the AND gate is turned on with just one line. The
121
Digital Logic and Microprocessor Design with VHDL
Chapter 4 - Standard Combinational Components
eight 2-input AND gates can be turned on individually from the eight outputs of a 3-to-8 decoder. Recall from Section 4.7 that the decoder asserts only one output line at any time. Larger multiplexers can also be constructed from smaller multiplexers. For example, an 8-to-1 multiplexer can be constructed using seven 2-to-1 multiplexers as shown in Figure 4.22(b). The four top-level 2-to-1 multiplexers provide the eight data inputs and all are switched by the same least significant select line s0. This top level selects one from each group of two data inputs. The middle level then groups the four outputs from the top level again into groups of two and selects one from each group using the select line s1. Finally, the multiplexer at the bottom level uses the most significant select line s2 to select one of the two outputs from the middle level multiplexers. The VHDL code for an 8-bit wide 4-to-1 multiplexer is shown in Figure 4.23. Two different implementations of the same multiplexer are shown. Figure 4.23(a) shows the architecture code written at the behavioral level, since it uses a PROCESS statement. Inside the PROCESS block, a CASE statement is used to select between the four choices for S. Figure 4.23(b) shows a dataflow level architecture code using a concurrent selected signal assignment statement using the keyword WITH … SELECT. In the first choice, if S is equal to 00, then the value D0 is assigned to Y. If S does not match any one of the four choices, 00, 01, 10, and 11, then the WHEN OTHERS clause is selected. The syntax (OTHERS => 'U') means to fill the entire vector with the value “U”.
s0 s1 s2
Decoder
d7
d6
d5
d4
d3
d2
d1
d0
0 1 2 3 4 5 6 7
d7 d6 1 s
d5 d4
0
1 s
y
d3 d2
0
1 s
y
d1 d0
0
1 s
y
0 y
s0 1 s
0
1 s
y
0 y
s1 1 s
s2
0 y
y
y
(a)
(b)
Figure 4.22 An 8-to-1 multiplexer implemented using: (a) a 3-to-8 decoder; (b) seven 2-to-1 multiplexers. -- A 4-to-1 8-bit wide multiplexer LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; ENTITY Multiplexer IS PORT(S: IN STD_LOGIC_VECTOR(1 DOWNTO 0); -- select lines D0, D1, D2, D3: IN STD_LOGIC_VECTOR(7 DOWNTO 0); -- data bus input Y: OUT STD_LOGIC_VECTOR(7 DOWNTO 0)); -- data bus output END Multiplexer; -- Behavioral level code ARCHITECTURE Behavioral OF Multiplexer IS BEGIN PROCESS (S,D0,D1,D2,D3) BEGIN CASE S IS WHEN "00" => Y Y Y Y Y 'U');
122
-- 8-bit vector of U
Digital Logic and Microprocessor Design with VHDL
Chapter 4 - Standard Combinational Components
END CASE; END PROCESS; END Behavioral; (a) -- Dataflow level code ARCHITECTURE Dataflow OF Multiplexer IS BEGIN WITH S SELECT Y 'U') WHEN OTHERS; END Dataflow;
-- 8-bit vector of U
(b) Figure 4.23 VHDL code for an 8-bit wide 4-to-1 multiplexer: (a) behavioral level; (b) dataflow level.
4.9.1 * Using Multiplexers to Implement a Function Multiplexers can be used to implement a Boolean function very easily. In general, for an n-variable function, a 2n-to-1 multiplexer (that is, a multiplexer with n select lines) is needed. An n-variable function has 2n minterms, and each minterm corresponds to one of the 2n multiplexer inputs. The n input variables are connected to the n select lines of the multiplexer. Depending on the values of the n variables, one data input line will be selected, and the value on that input line is passed to the output. Therefore, all we need to do is to connect all the data input lines to either a 1 or a 0, depending on whether we want that corresponding minterm to be a 1-minterm or a 0-minterm, respectively. Figure 4.24 shows the implementation of the 3-variable function, F (x, y, z) = x'y'z' + x'yz' + xy'z + xyz' + xyz. The 1-minterms for this function are m0, m2, m5, m6, and m7, so the corresponding data input lines, d0, d2, d5, d6, and d7 are connected to a 1, while the remaining data input lines are connected to a 0. For example, the 0-minterm x'yz has the value 011, which will select the d3 input, so a 0 passes to the output. On the other hand, the 1-minterm xy'z has the value 101, which will select the d5 input, so a 1 passes to the output. 1 1 1 0 0 1 0 0 x y z
d7 d6 d5 d4 d3 d2 d1 d0 s2 s1 s0 y F
Figure 4.24 Using an 8-to-1 multiplexer to implement the function F (x, y, z) = x'y'z' + x'yz' + xy'z + xyz' + xyz.
4.10 Tri-state Buffer A tri-state buffer, as the name suggests, has three states: 0, 1, and a third state denoted by Z. The value Z represents a high-impedance state, which for all practical purposes acts like a switch that is opened or a wire that is cut. Tri-state buffers are used to connect several devices to the same bus. A bus is one or more wire for transferring signals. If two or more devices are connected directly to a bus without using tri-state buffers, signals will get corrupted on the bus because the devices are always outputting either a 0 or a 1. However, with a tri-state buffer in between, devices that are not using the bus can disable the tri-state buffer so that it acts as if those devices are
123
Digital Logic and Microprocessor Design with VHDL
Chapter 4 - Standard Combinational Components
physically disconnected from the bus. At any one time, only one active device will have its tri-state buffers enabled, and thus, use the bus. The truth table and symbol for the tri-state buffer is shown in Figure 4.25(a) and (b). The active high enable line E turns the buffer on or off. When E is de-asserted with a 0, the tri-state buffer is disabled, and the output y is in its high-impedance Z state. When E is asserted with a 1, the buffer is enabled, and the output y follows the input d. A circuit consisting of only logic gates cannot produce the high impedance state required by the tri-state buffer, since logic gates can only output a 0 or a 1. To provide the high impedance state, the tri-state buffer circuit uses two transistors in conjunction with logic gates, as shown in Figure 4.25(c). Section 5.3 will discuss the operations of these two transistors in detail. For now, we will keep it simple. The top PMOS transistor is enabled with a 0 at the node labeled A, and when it is enabled, a 1 signal from Vcc passes down through the transistor to y. The bottom NMOS transistor is enabled with a 1 at the node labeled B, and when it is enabled, a 0 signal from ground passes up through the transistor to y. When the two transistors are disabled (with A = 1 and B = 0) they will both output a high impedance Z value; so y will have a Z value. Having the two transistors, we need a circuit that will control these two transistors so that together they realize the tri-state buffer function. The truth table for this control circuit is shown in Figure 4.25(d). The truth table is derived as follows. When E = 0, it does not matter what the input d is, we want both transistors to be disabled so that the output y has the Z value. The PMOS transistor is disabled when the input A = 1; whereas, the NMOS transistor is disabled when the input B = 0. When E = 1 and d = 0, we want the output y to be a 0. To get a 0 on y, we need to enable the bottom NMOS transistor and disable the top PMOS transistor so that a 0 will pass through the NMOS transistor to y. To get a 1 on y for when E = 1 and d = 1, we need to do the reverse by enabling the top PMOS transistor and disabling the bottom NMOS transistor. The resulting circuit is shown in Figure 4.25(c). When E = 0, the output of the NAND gate is a 1, regardless of what the other input is, and so the top PMOS transistor is turned off. Similarly, the output of the AND gate is a 0, so the bottom NMOS transistor is also turned off. Thus, when E = 0, both transistors are off, so the output y is in the Z state. When E = 1, the outputs of both the NAND and AND gates are equal to d'. So if d = 0, the output of the two gates are both 1, so the bottom transistor is turned on while the top transistor is turned off. Thus, y will have the value 0, which is equal to d. On the other hand, if d = 1, the top transistor is turned on while the bottom transistor is turned off, and y will have the value 1. The behavioral VHDL code for an 8-bit wide tri-state buffer is shown in Figure 4.26. E
Vcc A
E 0 1
(a)
y Z d
E
d
d
PMOS
y
y B
(b)
(c)
NMOS
E 0 0 1 1
d 0 1 0 1
A 1 1 1 0
B 0 0 1 0
y Z Z 0 1
(d)
Figure 4.25 Tri-state buffer: (a) truth table; (b) logic symbol; (c) circuit; (d) truth table for the control portion of the tri-state buffer circuit. LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; ENTITY TriState_Buffer IS PORT ( E: IN STD_LOGIC; d: IN STD_LOGIC_VECTOR(7 DOWNTO 0); y: OUT STD_LOGIC_VECTOR(7 DOWNTO 0)); END TriState_Buffer;
124
Digital Logic and Microprocessor Design with VHDL
Chapter 4 - Standard Combinational Components
ARCHITECTURE Behavioral OF TriState_Buffer IS BEGIN PROCESS (E, d) BEGIN IF (E = '1') THEN y
1
eout = ginein + einxi'yi' + einxiyi (b)
0
1
eout
x1 y1
gout
>
x0 y0
gout
>
eout
(d)
127
eout
gout
>
eout
x>y x=y
Digital Logic and Microprocessor Design with VHDL
Chapter 4 - Standard Combinational Components
Condition Invalid x=y x>y x y: (a) truth table for 1-bit slice; (b) K-maps and equations for gout and eout; (c) circuit for 1-bit slice; (d) 4-bit x > y comparator circuit; (e) operational table.
4.12 Shifter The shifter is used for shifting bits in a binary word one position either to the left or to the right. The operations for the shifter are referred to either as shifting or rotating, depending on how the end bits are shifted in or out. For a shift operation, the two end bits do not wrap around; whereas for a rotate operation, the two end bits wrap around. Figure 4.30 shows six different shift and rotate operations. For example, for the “Shift left with 0” operation, all the bits are shifted one position to the left. The original leftmost bit is shifted out (i.e. discarded) and the rightmost bit is filled with a 0. For the “Rotate left” operation, all the bits are shifted one position to the left. However, instead of discarding the leftmost bit, it is shifted in as the rightmost bit (i.e. it rotates around). For each bit position, a multiplexer is used to move a bit from either the left or right to the current bit position. The size of the multiplexer will determine the number of operations that can be implemented. For example, we can use a 4-to-1 multiplexer to implement the four operations, as specified by the table in Figure 4.31(a). Two select lines, s1 and s0, are needed to select between the four different operations. For a 4-bit operand, we will need to use four 4-to1 multiplexers as shown in Figure 4.31(b). How the inputs to the multiplexers are connected will depend on the given operations. Operation Shift left with 0
Shift left with 1
Shift right with 0
Shift right with 1
Rotate left
Rotate right
Comment
Example
Shift bits to the left one position. The leftmost bit is discarded and the rightmost bit is filled with a 0.
10110100
Same as above, except that the rightmost bit is filled with a 1.
101101000 10110100 101101001
Shift bits to the right one position. The rightmost bit is discarded and the leftmost bit is filled with a 0.
10110100
Same as above, except that the leftmost bit is filled with a 1.
10110100
Shift bits to the left one position. The leftmost bit is moved to the rightmost bit position.
10110100
Shift bits to the right one position. The rightmost bit is moved to the leftmost bit position.
10110100
Figure 4.30 Shifter and rotator operations.
128
0 1 0110100
1 1 0110100
01101001
0 1 011010
Digital Logic and Microprocessor Design with VHDL
s1 0 0 1 1
s0 0 1 0 1
Chapter 4 - Standard Combinational Components
Operation Pass through Shift left and fill with 0 Shift right and fill with 0 Rotate right (a)
in3
in2
in1
'0'
in0
'0'
3 2 1 0 s1 MUX3 s0 y
3 2 1 0 s1 MUX2 s0 y
3 2 1 0 s1 MUX1 s0 y
3 2 1 0 s1 MUX0 s0 y
out3
out2
out1
out0
s1 s0
s1 s0
in3
in2 in1 in0 4-bit Shifter out3 out2 out1 out0
(c)
(b) Figure 4.31 A 4-bit shifter: (a) operation table; (b) circuit; (c) logic symbol.
In this example, when s1 = s0 = 0, we want to pass the bit straight through without shifting, i.e. we want the value for ini to pass to outi. Given s1 = s0 = 0, d0 of the multiplexer is selected, hence, ini is connected to d0 of muxi which outputs to outi. For s1 = 0 and s0 = 1, we want to shift left, i.e. we want the value for ini to pass to outi+1. With s1 = 0 and s0 = 1, d1 of the multiplexer is selected, hence, ini is connected to d1 of MUXi+1 which outputs to outi+1. For this selection, we also want to shift in a 0 bit, so d1 of MUX0 is connected directly to a 0. The behavioral VHDL code for an 8-bit shifter having the functions as defined in Figure 4.31(a) is shown in Figure 4.32. LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL; ENTITY shifter IS PORT ( S: IN STD_LOGIC_VECTOR(1 DOWNTO 0); -- select for operations input: IN STD_LOGIC_VECTOR(7 DOWNTO 0); -- input output: OUT STD_LOGIC_VECTOR(7 DOWNTO 0)); -- output END shifter; ARCHITECTURE Behavior OF shifter IS BEGIN PROCESS(S, input) BEGIN CASE S IS WHEN "00" => -- pass through output -- shift left with 0 output -- shift right with 0 output -- rotate right output keycode(0) keycode(1) keycode(2) keycode(3) keycode(4) keycode(5) keycode(6) keycode(7) END CASE; END PROCESS; END Structural;
-- read in data bit 0 from the keyboard -- read in data bit 1 from the keyboard -- read in data bit 2 from the keyboard -- read in data bit 3 from the keyboard -- read in data bit 4 from the keyboard -- read in data bit 5 from the keyboard -- read in data bit 6 from the keyboard -- read in data bit 7 from the keyboard
Figure 10.26 VHDL code for the PS/2 keyboard controller.
10.6 Summary Checklist
Control unit Datapath Control signals
372
Digital Logic and Microprocessor Design with VHDL
Chapter 10 - Control Units
Status signals Constructing a control unit Generating control signals Generating status signals Timing issues for a control unit ASM charts State action tables Be able to derive a state diagram from an algorithm Be able to derive output signals for a control unit from control words Be able to derive status signals from the conditions in an algorithm Be able to determine how a comparator for generating a status signal connects to the datapath Be able to derive a control unit (FSM) from a state diagram
373
Digital Logic and Microprocessor Design with VHDL
Chapter 10 - Control Units
10.7 Problems 10.1 Derive the control unit for the counting problem from Example 10.1, except use the general datapath from Section 9.4. Answer Next State Current State control word 1 s0 Q1next Q0next Q1Q0 (i ≠ 10)' (i ≠ 10) (i ≠ 10) (i ≠ 10)' s0 00 s3 11 s1 01 s1 01 s2 10 s2 10 s2 10 s3 11 s1 01 control word 2 s1 s3 11 s3 11 s3 11 (i ≠ 10)
(b) s2
control word 3 (i ≠ 10)' halt
Implementation D1 D0 (i ≠ 10)' (i ≠ 10) 11 01 10 10 11 01 11 11
Current State Q1Q0 00 01 10 11
s3
(c) (a) Q1Q0 00 01 10 11
D 0 = Q 1 + Q 0' D1 = (i ≠ 10)' + Q0
Clear 1 0 0 ×
Clear = Q1'Q0' (e)
(d)
Q 1Q 0 00 01 10 11
IE × 0 × ×
ALU2 × 1 × ×
ALU1 × 0 × ×
ALU0 × 0 × ×
Load 0 1 0 ×
Clear 1 0 0 ×
OE 0 0 1 0
IE ALU2 ALU1 ALU0 Load Clear OE
=0 = Q1'Q0 =0 =0 = Q1'Q0 = Q1'Q0' = Q1Q0' (g)
(f)
374
Digital Logic and Microprocessor Design with VHDL
status signal from the datapath
next-state logic
Chapter 10 - Control Units
state memory
output logic and control signals to the datapath
(i≠10)
'0' D1
ALU2
Q1
Clk Q'1
IE
'0'
ALU1
'0'
ALU0 Load
D0
Q0
Clk Clk
Clear OE
Q'0
(h)
10.2 Use schematic entry to implement the control unit shown in Figure 10.2(g). Simulate and test the circuit. 10.3 Implement and test the control unit for Problem 10.2 on the UP2 board. 10.4 Use schematic entry to implement the control unit shown in Figure 10.6(g). Simulate and test the circuit. 10.5 Implement and test the control unit for Problem 10.4 on the UP2 board. 10.6 Use schematic entry to implement the control unit shown in Figure 10.11(g). Simulate and test the circuit. 10.7 Implement and test the control unit for Problem 10.6 on the UP2 board. 10.8 Derive the control unit for: (a) (b) (c) (d) (e) (f)
Problem 9.23 Problem 9.24 Problem 9.25 Problem 9.26 Problem 9.27 Problem 9.28
10.9 Use schematic entry to implement the control units for Problem 10.8. Simulate and test the circuit. 10.10 Derive the control unit for: (a) (b) (c) (d) (e) (f)
Problem 9.29 Problem 9.30 Problem 9.31 Problem 9.32 Problem 9.33 Problem 9.34
375
Digital Logic and Microprocessor Design with VHDL
(g) (h)
Chapter 10 - Control Units
Problem 9.35. Problem 9.36.
10.11 Use schematic entry to implement the control units for Problem 10.10. Simulate and test the circuit. 10.12 Instead of using an FSM circuit for the keyboard controller, as shown in Section 10.3.2, design and implement a keyboard controller circuit using just a serial-to-parallel shift register. 10.13 Modify the keyboard controller circuit from Section 10.3.2 so that it will test for the correct parity bit. 10.14 Modify the keyboard controller circuit from Problem 10.12 so that it will test for the correct parity bit. 10.15 Construct and implement a circuit using the VGA controller from Section 10.3.3 to draw a blue triangle on the screen. 10.16 Construct and implement a circuit using the VGA controller from Section 10.3.3 to draw a white square in the middle of the screen. 10.17 Construct and implement a circuit using the VGA controller from Section 10.3.3 to draw a white square and a blue triangle on the screen. 10.18 Using the keyboard controller from Section 10.3.2 and the VGA controller from Section 10.3.3, construct and implement a circuit that reads in a letter from the keyboard and displays it on the screen. 10.19 Write VHDL code for the control units for Problem 10.8. Simulate and test the circuit. 10.20 Write VHDL code for the control units for Problem 10.10. Simulate and test the circuit. 10.21 Derive the ASM charts for the control units for Problem 10.8. 10.22 Derive the ASM charts for the control units for Problem 10.10. 10.23 Derive the state action tables for the control units for Problem 10.8. 10.24 Derive the state action tables for the control units for Problem 10.10.
376
Digital Logic and Microprocessor Design with VHDL
s0
Chapter 10 - Control Units
00 x=0 y=1 Start 1
0
s1
01 x=1 y=1
s2
10 x=1 y=1 n=9 1 s3
0
11 x=1 y=0
377
Chapter 11
Dedicated Microprocessors
Control Inputs
Data Inputs Control Unit
8
'0'
Datapath
MUX ff Nextstate Logic
State Memory
Output Logic
Register
Control Signals
Status Signals Control Outputs
ALU
8
ff
Register
8 Data Outputs
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
All microprocessors can be divided into two main categories: general-purpose microprocessors and dedicated microprocessors. General-purpose microprocessors are capable of performing a variety of computations. In order to achieve this goal, each computation is not hardwired into the processor, but rather is represented by a sequence of instructions in the form of a program that is stored in the memory, and executed by the microprocessor. The program in the memory can be easily changed so that another computation can be performed. Because of the general nature of the processor, it is likely that in performing a specific computation, not all of the resources available inside the general-purpose microprocessor are used. Dedicated microprocessors, also known as application specific integrated circuits (ASICs), on the other hand, are dedicated to performing only one task. The instructions for performing that one task are, therefore, hardwired into the processor itself, and once manufactured, cannot be modified again. In other words, no memory is required to store the program because the program is built right into the circuit itself. If the ASIC is completely customized, then only those resources that are required by the computation are included in the ASIC, and so no resources are wasted. Another advantage of building the program instructions directly into the microprocessor circuit itself is that the execution speed of the program is many times faster than if the instructions are stored in memory. The design of a microprocessor, whether it be a general-purpose microprocessor or a dedicated microprocessor, can be divided into two main parts, the datapath and the control unit as shown in Figure 11.1. The datapath is responsible for all the operations perform on the data. It includes (1) functional units such as adders, shifters, multipliers, ALUs, and comparators, (2) registers and other memory elements for the temporary storage of data, and (3) buses and multiplexers for the transfer of data between the different components in the datapath. External data can enter the datapath through the data input lines. Results from the computation can be returned through the data output lines. Control Inputs
Data Inputs Control unit
8
'0'
Datapath
mux ff Nextstate Logic
State Memory
Output Logic
register
Control Signals
Status Signals Control Outputs
ALU
8
ff
register
8 Data Outputs
Figure 11.1. Schematic of a microprocessor. The control unit (or controller) is responsible for controlling all the operations of the datapath by providing appropriate control signals to the datapath at the appropriate times. At any one time, the control unit is said to be in a certain state as determined by the content of the state memory. The state memory is simply a register with one or more (D) flip-flops. The control unit operates by transitioning from one state to another – one state per clock cycle, and because of this behavior, the control unit is also referred to as a finite-state machine (FSM). The next-state logic in the control unit will determine what state to go to next in the next clock cycle depending on the current state that the FSM is in, the control inputs, and the status signals. In every state, the output logic that is in the control unit generates all the appropriate control signals for controlling the datapath. The datapath, in return, provides status signals for the next-state logic. Status signals are usually from the output of comparators for testing branch conditions. Upon completion of the computation, the control output line is asserted to notify external devices that the value on the data output lines is valid. In chapters 9 and 10, you have learned how to design the datapath and the control unit separately. In this chapter, you will learn how to put them together to form a dedicated microprocessor. There are several abstraction levels at which a microprocessor can be designed. At the lowest level, you manually construct the circuit for both the control unit and the datapath separately, and then connect them together using the control and status signals. This manual process of constructing a microprocessor ties together everything that you have learned so far in this book.
379
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
However, in a real situation, this method is not too practical because the microprocessor will usually require many more states, input, and output signals. As a result, the manual construction process becomes much more complex. Furthermore, there are tools for automating this process. Nevertheless, being able to construct a microprocessor manually shows that you fully understand the theories and concepts of how a microprocessor is designed. Section 11.1 shows this complete dedicated microprocessor construction process. Section 11.2 provides several complete manual construction examples. All the circuits are on the accompanying CD-ROM, and can be downloaded onto the UP2 development board for testing. After manually designing a microprocessor, we can actually implement the circuit either by manually drawing the circuit using the Schematic Editor, or using VHDL to describe the circuit connections at the structural level. Constructing a microprocessor manually this way uses the FSM+D (FSM plus datapath) model. In this model, both the FSM and the datapath circuits are manually constructed as separate units. The FSM and the datapath are connected together in an enclosing unit using the control and status signals. The next abstraction level of microprocessor design also uses the FSM+D model. As before, you would manually construct the datapath. However, instead of manually constructing the FSM, you would use behavioral VHDL code to describe the operation of the FSM as discussed in Sections 7.6 and 10.7. There will be a next-state process and an output process in the behavioral code. The next-state process will generate the next-state logic, and the output process will generate all the control signals for driving the datapath. The final circuit for the FSM can then be automatically synthesized. The automatically synthesized FSM and the manually constructed datapath are then connected together like before in an enclosing unit using the control and status signals. In practice, this is probably the lowest level in which you would want to design a dedicated microprocessor. The advantage of using this FSM+D model is that you have full control as to how the datapath is built. Section 11.3.1 illustrates this process. The third level of microprocessor design uses the FSMD (FSM with datapath) model. Using this model, you would design the FSM using behavioral VHDL code just like in the previous level. However, instead of constructing the datapath manually as a separate unit, all the datapath operations are embedded within the FSM entity using the built-in VHDL operators. During the synthesis process, the synthesizer will automatically generate a separate FSM and datapath units, but these two units will be automatically connected together as one microprocessor by the synthesizer. The advantage of this model is that you do not have to manually design the datapath, but you still have full control as to what operation is executed in what state or in what clock cycle. In other words, you have control over the timing of the FSM circuit. Section 11.3.2 illustrates this process. Finally, a microprocessor can be described algorithmically at the behavioral level using VHDL. This process synthesizes the full microprocessor with its control unit and datapath automatically. The advantage of designing microprocessors this way is that you do not need to know how to manually design a microprocessor. In other words, you do not need to know most of the materials presented in this book. Instead, you only need to know how to write VHDL codes. The disadvantage is that you do not have control over the timing of the circuit. You can no longer specify what register-transfer operation is executed in what clock cycle. Section 11.3.3 illustrates this process.
11.1 Manual Construction of a Dedicated Microprocessor In Chapter 9, we described how a datapath is designed, and how it is used to execute a particular algorithm by specifying the control words to manipulate the datapath at each clock cycle. In that chapter, we tested the datapath by setting the control word signals manually. However, to actually have the datapath automatically operate according to the control words, a control unit is needed that will generate the control signals that correspond to the control words at each clock cycle. The construction of the control unit was described in Chapter 10. Given the algorithm and the control words, we were able to derive the state diagram, from which we get the next-state table, and finally the next-state logic circuit for the control unit. The control words also serve as the output table; from this we get the output logic circuit. Combining the next-state logic circuit, the state memory, and the output logic circuit together produces the complete control unit circuit. To form a complete dedicated microprocessor, we simply have to join the control unit and the datapath together using the control signals and the status signals. Recall that the control signals are generated by the control unit to control the operations of the datapath, while the status signals are generated by the datapath to inform the next-state logic in the control unit as to what the next state should be in the execution of the algorithm.
380
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
This method of manually constructing a dedicated microprocessor is referred to as the FSM+D (FSM plus datapath) model because the FSM and the datapath are constructed separately, and then they are joined together using the control and status signals. Example 11.1 shows this manual construction of a dedicated microprocessor. Example 11.1 In this example, we will manually construct the dedicated microprocessor for the summation algorithm of Example 9.10. Recall that the problem is to generate and add the numbers from n down to 1, where n is an input number. In Example 9.10, we derived the control words for solving this algorithm using the general datapath shown in Figure 9.32??? and repeated here in Figure 11.2(a). Example 10.6??? manually constructed the control unit circuit based on the state diagram and control words for the algorithm. The complete control unit circuit from Figure 10.4 is repeated here in Figure 11.2(b). The datapath circuit shown in Figure 11.2(a) uses 16 control signals. Correspondingly, the control unit circuit shown in Figure 11.2(b) generates the values for these 16 control signals. To combine the control unit and the datapath together, we simply connect the 16 control signals from the control unit to the corresponding 16 control signals on the datapath as shown in Figure 11.2(c). control signals from the control unit
Data Input status signal to the control unit
8 IE
15
(n ≠ 0)
8 1
mux
0
8 8 WE WA RAE RAA
14 13-12 11 10-9 Clock
4x8 RF
RBE RBA
A
B
8
8 7-6
8
A B ALU2 ALU1 ALU ALU0
5 4 3
8 SH1 SH0
2 1
Shifter 8
OE
0
8
Data Output
(a)
381
8
Digital Logic and Microprocessor Design with VHDL
next-state logic
Start
Chapter 11 - Dedicated Microprocessors
state memory
output logic and control signals to the datapath
control input
RBE
IE D2
Q2
0
Clk Clear
WA1
ALU2
WA0
ALU1
RAE
ALU0
Q1
Clk Clear
D0
Q'1
Q0
0
Clk Clear
Clk Reset
RBA0
Q'2
0 D1
0
RAA1
Q'0
RAA0 Done
status signal from the datapath
control output
(b) Start
Data Input 8
Start
IE WE WA1-0 RAE RAA1-0 Control RBE Unit RBA1-0 ALU2-0 SH1-0 OE Clock Reset
(n ≠ 0)
Control Signals
Status Signal
Done
Input IE WE WA1-0 RAE RAA1-0 RBE RBA1-0 Datapath ALU2-0 SH1-0 OE Clock (n ≠ 0) Reset Output
8 Done
Data Output
382
SH1 SH0 OE
(n ≠ 0)
Clock Reset
RBA1
WE
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
(c) Figure 11.2. Microprocessor for the summation algorithm of Example 11.1: (a) datapath; (b) control unit; (c) complete microprocessor circuit. In addition to the 16 control signals, the control unit requires one status signal from the datapath. This status signal, (n ≠ 0), is generated by the OR gate comparator in the datapath, and goes to the input of the next-state logic in the control unit. The microprocessor has two types of inputs, and two types of outputs. There is a control input signal Start that goes into the control unit. A control output signal Done that the control unit generates. Then there is the data input going into the datapath. For this example, this is where the value for n is entered in. Finally, there is the data output from which the datapath outputs the result of the computation. This complete dedicated microprocessor, as shown in Figure 11.2(c), of course does nothing more then to sum the numbers from n down to 1, and outputs the result. ♦
383
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
11.2 Examples This section provides several complete examples of manually designing and constructing dedicated microprocessors. The complete schematic drawings are available on the accompanying CD-ROM in the respective directories. All the examples’ microprocessor top-level schematic design files are named MP.GDF, and can be used for simulation. The microprocessor circuits can also be downloaded onto the UP2 development board for testing. To download the design onto the UP2 development board, use either the top-level file named UP2MAX.GDF for the MAX chip, or UP2FLEX.GDF for the FLEX chip. In each respective directory on the CD-ROM, there is a README.TXT file that describes how to use the microprocessor. Furthermore, each example also has the VHDL codes for describing it using the FSMD model on the CD-ROM. The corresponding top level VHDL files on the CD-ROM are MP.VHD, UP2MAX.VHD, and UP2FLEX.VHD.
11.2.1 Greatest Common Divisor Example 11.2 In this example, we will manually design the complete dedicated microprocessor for evaluating the greatest common divisor (GCD) of two 8-bit unsigned numbers X and Y. For example, GCD(3,5) = 1, GCD(10,4) = 2, and GCD(12,60) = 12. The algorithm for solving the GCD problem is listed in Figure 11.3. We will first design a dedicated datapath for the algorithm. Next, we will design the control unit for the datapath. We will use schematic entry to implement the complete dedicated microprocessor. Finally, we will test it using simulation, and on the UP2 board. 1 2 3 4 5 6 7 8 9 10
input input while if
X Y (X ≠ Y){ (X > Y) then X = X - Y else Y = Y - X end if } output X
Figure 11.3. Algorithm for solving the GCD problem. The algorithm shown in Figure 11.3 has five data manipulation statements in lines 1, 2, 5, 7, and 10. There are two conditional tests in lines 3 and 4. We can conclude that the datapath requires two 8-bit registers, one for variable X, and one for variable Y, and a subtractor. The dedicated datapath is shown in Figure 11.4. We need a 2-to-1 mux for the input of each register because for each register, we need to initially load it with an input number, and subsequently load it with the result from the subtractor. The two control signals, In_X and In_Y, select which of the two sources are to be loaded into the registers X and Y respectively. The two control signals, Load_X and Load_Y, load a value into the respective register. The bottom two muxes, selected by the same XY signal, determine the source to the two operands for the subtractor. When XY is asserted, then the value from register X goes to the left operand, and register Y goes to the right operand of the subtractor. When XY is de-asserted, then Y goes to the left operand, and X goes to the right operand. Thus, this allows the selection of one of the two subtraction operations, X – Y or Y – X, to perform. Finally, a tri-state buffer is used for outputting the result from register X. The Out control signal is used to enable the tri-state buffer. A greater-than and equal-to comparator is used to generate the two needed conditional status signals. The comparator inputs are directly from the two X and Y registers. There are two output signals, XeqY and XgtY, from the
384
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
comparator. XeqY is asserted if X is equal to Y, and XgtY is asserted if X is greater than Y. The operation table and circuit for this comparator was discussed in Section 4.10, and shown in Figure 4.29. This dedicated datapath for solving the GCD problem requires six control signals, In_X, In_Y, Load_X, Load_Y, XY, and Out, and two status signals, XeqY, and XgtY. Input_X
Input_Y
8
8 1
In_X
0
1
0
In_Y Load_X
D7-0 Load 8-bit register Clear X Q7-0
Load_Y Clear Clock XeqY XqtY
D7-0 Load 8-bit register Clear Y Q7-0
8
8
Comparator (X>Y), (X=Y) 1
0
1
0
8
XY 8
8 Subtractor
8 Out Output Figure 11.4. Dedicated datapath for solving the GCD problem.
The state diagram for the GCD algorithm requires five states as shown in Figure 11.5 (a). Four states are used for the five data manipulation statements, since only one state is used for performing both inputs. One “no-op” state is used for the conditional testing of the updated values of X and Y. The “no-operation” state 001 is needed since we need to test the conditions on the updated values of X and Y. From state 001, we test for the two conditions, (X=Y) and (X>Y). If (X=Y) is true, then the next state is 100. If (X=Y) is false, then the next state is either 010 or 011, depending on whether the condition (X>Y) is true of false respectively. This state diagram does not have a Start signal, so in order for the resulting microprocessor to read the inputs correctly, we must first set up the input numbers, and then assert the reset signal to clear the state memory flip-flops to 0. This way, when the FSM starts executing from state 000, the two input numbers are ready to be read in. The next-state table as derived from the state diagram is shown in Figure 11.5 (b). The table requires five variables; three to encode the six states, Q2, Q1, and Q0, and two status signals, (X=Y) and (X>Y). There are three unused state encodings, 101, 110 and 111. We have assumed that the next states from these three unused states are unconditionally back to state 000. Using D flip-flops to implement the state memory, the implementation table is the same as the next-state table except that the values in the table entries are the inputs to the flip-flops, D2, D1, and D0, instead of the flip-flops outputs, Q2next, Q1next, and Q0next. The k-maps and the excitation equations for D2, D1, and D0 are shown in Figure 11.5 (c). The control words and output truth table, having the six control signals, are shown in Figure 11.5 (d). State 000 performs both inputs of X and Y. The two mux select lines, In_X and In_Y must be asserted so that the data comes
385
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
from the two primary inputs. The two numbers are loaded into the two corresponding registers by asserting the Load_X and Load_Y lines. State 001 is for testing the two conditions, so no operations are performed. The no-op is accomplished by not loading the two registers, and not outputting a value. For states 010 and 011, the XY mux select line is used to select which of the two subtraction operations is to be performed. Asserting XY performs the operation X – Y, whereas, de-asserting XY performs the operation Y – X. The corresponding In_X or In_Y line is deasserted to route the result from the subtractor back to the input of the register. The corresponding Load_X or Load_Y line is asserted to store the result of the subtraction into the correct register. State 100 outputs the result from X by asserting the Out line. The output equations as derived from the output table are shown in Figure 11.5 (e). There is one equation for each of the six control signals. Each equation is only dependent on the current state, Q2, Q1, and Q0. We have assumed that the control signals have “don’t care” values in all the unused states. The complete control unit circuit is shown in Figure 11.5 (f). The state memory consists of three D flip-flops. The inputs to the flip-flops are the next-state circuits derived from the three excitation equations. The output circuits for the six control signals are derived from the six output equations. The two status signals, XeqY and XgtY, come from the comparator in the datapath. 000
input X input Y
(X = Y)
001 (X = Y)' & (X > Y) X = X - Y 010
100 output X
(X = Y)' & (X > Y)' 011
Y=Y-X
(a) Current State Q2Q1Q0 000 001 010 011 100
Next State Q2next Q1next Q0next (X=Y), (X>Y) 00 01 10 11 001 001 001 001 011 010 100 100 001 001 001 001 001 001 001 001 100 100 100 100 (b)
386
Digital Logic and Microprocessor Design with VHDL
D2
Q2 = 0
(X=Y),(X>Y)
Q1Q0
00
Chapter 11 - Dedicated Microprocessors
01
11
Q2 = 1 10
00 01
1
00
01
11
10
1
1
1
1
Q2Q1'Q0' Q2'Q1'Q0 (X=Y)
1
11 10
D2 = Q2Q1'Q0' + Q2'Q1'Q0 (X=Y) D1
(X=Y),(X>Y)
Q1Q0
00
01
1
1
Q2 = 0 11
Q2 = 1 10
00
01
11
10
00 01
Q2'Q1'Q0 (X=Y)'
11 10
D1 = Q2'Q1'Q0 (X=Y)' D0
(X=Y),(X>Y)
Q1Q0
Q2 = 0
Q2 = 1
00
01
11
10
00
1
1
1
1
01
1
11
1
1
1
1
10
1
1
1
1
00
01
11
10 Q2'Q0' Q2' (X=Y)' (X>Y)' Q2'Q1
D0 = Q2'Q0' + Q2' (X=Y)' (X>Y)' + Q2'Q1 (c) Control Word 0 1 2 3 4
State Q2 Q1 Q0 000 001 010 011 100
Instruction Input X, Input Y none X=X–Y Y=Y–X Output X
In_X 1 × 0 × × (d)
387
In_Y 1 × × 0 ×
Load_X 1 0 1 0 0
Load_Y 1 0 0 1 0
XY × × 1 0 ×
Out 0 0 0 0 1
Digital Logic and Microprocessor Design with VHDL
In_X = Q1' In_Y = Q0' XY = Q0'
Chapter 11 - Dedicated Microprocessors
Load_X = Q2'Q0' Load_Y = Q2'Q1'Q0' + Q2'Q1Q0 Out = Q2 (e)
In_X D2
In_Y
Q2
Clk Clr Q'2
D1
Load_X
Q1
Load_Y
Clk Clr
Q'1
XY D0
Q0
Out
Clk Clr
Clock Reset
Q'0
Done XeqY XgtY (f)
Figure 11.5. Control unit for solving the GCD problem: (a) state diagram; (b) next-state table; (c) k-maps and excitation equations; (d) control word/output truth table; (e) output equations; (f) circuit. The final microprocessor can now be easily formed by connecting the control unit and the datapath together using the designated control and status signals as shown in Figure 11.6. A sample simulation is shown in Figure 11.7. This complete microprocessor circuit is on the accompanying CD-ROM in the file MP.GDF, and can be synthesized and simulated.
388
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Input X 8 In_X In_Y Load_X Load_Y XY CU Out Clock Reset
Clock Reset
XeqY XgtY
Control Signals
Status Signals
Input Y 8
Input_X Input_Y In_X In_Y Load_X Load_Y XY DP Out Clock Reset
XeqY XgtY Output
Done
8 Done
Output
Figure 11.6. Microprocessor for solving the GCD problem.
Figure 11.7. Sample simulation for the GCD problem for the two input numbers 4 and 12. The GCD of these two numbers is 4. There is another top level file called UP2MAX.GDF in the same directory for downloading and testing this microprocessor circuit on the UP2 development board using the MAX EPM7128S PLD. You need to look at the Floorplan Editor to see how the I/O’s are mapped to the EPM7128S pins. If you prefer, you can re-map these pins. Note that the Reset signal must be mapped to pin 1 on the MAX chip, otherwise you need to remove the “Automatic Global Clear” selection checkmark from the Compiler menu Assign / Global Project Synthesis in order to compile this design file correctly. Jumper wires between the PLD pins and the appropriate switches and LED’s must be connected according to this floor plan. There are two sets of eight wires for the two input DIP switches. Another eight wires for the eight output LED’s, and one wire for the reset button. The original Done signal is mapped to segment a on digit 2. You may want to refer to Appendix C.9 for making these connections. To test this circuit, you need to first set up the two sets of eight DIP switches for the two binary unsigned input numbers. Then press the button connected to the Reset line. You should see the answer displayed as an 8-bit binary number on the eight LED’s. You should also see segment a on digit 2 light up for the Done signal. ♦
389
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
11.2.2 Summing Input Numbers Example 11.3 In this example, we will manually design the complete dedicated microprocessor for inputting many 8-bit unsigned numbers through one input port, and then output the sum of these numbers. The algorithm continues to input numbers as long as the number entered is not a zero. Each number entered is also outputted. When the number entered is a zero, the algorithm stops and outputs the sum of all the numbers entered. The algorithm for solving this problem is listed in Figure 11.8. We will first design a dedicated datapath for the algorithm. Next, we will design the control unit for the datapath. We will use schematic entry to implement the complete dedicated microprocessor. Finally, we will test it using simulation, and on the UP2 board. 1 2 3 4 5 6 7 8 9 10
Sum = 0 begin loop input X Sum = Sum + X if (X = 0) then exit loop end if output X end loop output Sum
Figure 11.8. Algorithm for summing input numbers. The algorithm shown in Figure 11.8 has five data manipulation statements in lines 1, 3, 4, 8, and 10. There is one conditional test in line 5. The algorithm requires two 8-bit registers, one for variable X, and one for variable Sum, and an adder. The dedicated datapath is shown in Figure 11.9. Line 1 in the algorithm is performed by asserting the Clear signal, line 3 is performed by asserting the Load_X signal, and line 4 is performed by asserting the Load_Sum signal. The algorithm continuously outputs either X or Sum. The Out signal selects the 2-to-1 multiplexer for one of the two sources, register X or register Sum. The output of the mux is always available at the output. The conditional test (X = 0) is generated by the 8-input NOR gate. Input_X 8
Load_Sum D7-0 Load 8-bit register Clear X Q7-0
Load_X
D7-0 Load 8-bit register Clear Sum Q7-0
Clear Clock Xeq0
8
8 8
8
Out
0
1
8
Output Figure 11.9. Dedicated datapath for the summing input numbers problem.
390
8
+
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
At first glance, this algorithm is very similar to the GCD algorithm in Example 11.2. However, because of the requirements of this problem, the actual hardware implementation of this microprocessor is slightly more difficult. Specifically, the requirement that many different numbers be inputted through one input port requires careful timing considerations, and an understanding of how mechanical switches behave. As a first try, we begin with the state diagram shown in Figure 11.10 (a). Line 1 of the algorithm is performed by the asynchronous reset, so it does not require a state to execute. Line 3 is performed in state 00, which is unconditionally followed by line 4 in state 01. The condition (X=0) is then tested. If the condition is true, the loop is exited, and the FSM goes to state 11 to output the value for Sum, and stays in that state until reset. If the condition is false, the FSM goes to state 10 to output X, and the loop repeats back to state 00. Enter'
00
input X output X
00
input X output X
Enter
01 (X = 0)' 10
Sum = Sum + X output X
01
(X = 0) output X
(X = 0)'
11
output Sum
10
(a)
Sum = Sum + X output X (X = 0) output X
11
output Sum
(b)
Figure 11.10. Incorrect state diagrams for the summing input numbers problem. One thing to notice is that in all the states except for state 11, we need to output X. This is because in the datapath, we do not have a tri-state buffer to disable the output. Instead, with the mux, either one of two numbers is always being outputted. The behavior of this, nevertheless, is correct according to the problem specifications. As an exercise, you can derive the FSM circuit based on this state diagram shown in Figure 11.10 (a), and perform a simulation to test its operation. In the simulation, the input numbers are manually assigned, so you should be able to obtain the correct simulation result. However, if you implement this circuit in hardware, it will not work correctly. The reason is that the FSM cycles through the three loop states, 00, 01, and 10, very fast because of the fast clock speed. As a result, the FSM will have gone through state 00 to input a number many times before you can even change the input to another number. Hence, the same number will be summed many times. To correct this problem, we need to add another input signal that acts like the Enter key. This way, the FSM will stay in state 00, waiting for the Enter signal to be asserted. This will give the user time to set up the input number before pressing the enter switch. When the Enter signal is asserted, the FSM will exit state 00 with the new number to be processed. This modified state diagram is shown in Figure 11.10 (b). There is still a slight timing problem with this modified state diagram because of the fast clock speed. After pressing the Enter switch, and before you have time to release it, the FSM will have cycled through the complete loop, and is back at state 00. But since you have not yet released the Enter switch, the FSM will continue on another loop with the same input number. What we need to do is to break the loop by waiting for the Enter switch to be released. This is shown in the state diagram in Figure 11.11 (a). State 10 will wait for the Enter switch to be released before continuing on, and looping back to state 00. This last state diagram is correct. However, there might be a problem with the operation of the mechanical switch used for the enter signal. When a mechanical switch is pressed, it usually goes on and off several times before settling down in the on position. This is referred to as the “debounce” problem. When the switch is
391
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
fluctuating between the on and the off positions, the FSM can again be able to go through the loop many times. What we need to do is to debounce the switch. This however, is not done in the FSM circuit itself, but in the interface circuit between the FSM and the switch. We will come back to address this problem when building the interface circuit. We will now construct the control unit circuit based on the state diagram shown in Figure 11.11 (a). Four states are used for the five data manipulation statements. All the states except for 11 will output X. State 00 inputs X and waits for the Enter signal. This allows the user to set up the input number, and then press the Enter key. When the Enter key is pressed, the FSM goes to state 01 to sum X, and test for (X = 0). If the condition is true, the FSM terminates in state 11, and outputs X, otherwise, it goes to state 10 to wait for the Enter signal to be de-asserted by the user releasing the Enter switch. After exiting state 10, the FSM continues on to repeat the loop in state 00. The next-state table as derived from the state diagram is shown in Figure 11.11 (b). The table requires four variables; two to encode the four states, Q1 and Q0, and two status signals, Enter and (X = 0). Using D flip-flops to implement the state memory, the implementation table is the same as the next-state table except that the values in the table entries are the inputs to the flip-flops, D1 and D0, instead of the flip-flops outputs, Q1next and Q0next. The k-maps and the excitation equations for D1 and D0 are shown in Figure 11.11 (c). The control words and output truth table for the three control signals, are shown in Figure 11.11 (d). State 00 performs Line 3 of the algorithm in Figure 11.8 by asserting Load_X, and Line 8 by de-asserting Out. When Out is de-asserted, X is passed to the output. State 01 performs Line 4 and Line 8. Line 4 is executed by asserting Load_Sum, and Line 8 is executed by de-asserting Out. State 10 again performs Line 8 by de-asserting Out. Finally, State 11 performs Line 10 by asserting Out. The output equations as derived from the output table are shown in Figure 11.11 (e). There is one equation for each of the three control signals. Each equation is only dependent on the current state, Q1 and Q0. The complete control unit circuit is shown in Figure 11.11 (f). The state memory consists of two D flip-flops. The inputs to the flip-flops are the next-state circuits derived from the two excitation equations. The output circuits for the three control signals are derived from the three output equations. The status signal, (Xeq0) come from the comparator in the datapath.
Enter' 00
input X output X
Enter 01 (X = 0)' Enter'
10
Sum = Sum + X output X (X = 0) output X
11
output Sum
Enter
(a) Current State Q 1Q 0 00 01 10
Next State Q2next Q1next Q0next Enter, (X=0) 00 01 10 11 00 00 01 01 10 11 10 11 00 00 10 10
392
Digital Logic and Microprocessor Design with VHDL
11
Chapter 11 - Dedicated Microprocessors
11
11
11
11
(b) D1
Enter,(X=0)
Q1Q0
00
01
11
10
01
1
1
1
1
Q0
11
1
1
1
1
Q1 Enter
1
1
00
10
D1 = Q0 + Q1 Enter D0
Enter,(X=0)
Q1Q0
00
01
00 01 11
1
11
10
1
1
1
1
1
1
Q1'Q0' Enter Q0 (X=0) Q1Q0
1
10
D0 = Q1Q0 + Q0 (X=0) + Q1'Q0' Enter (c) Control Word 0 1 2 3
State Q1 Q0 00 01 10 11
Instruction input X, output X Sum = Sum – X, output X output X output Sum
Load_X 1 0 0 0
(d) Load_X = Q1'Q0' Load_Sum = Q1'Q0 Out = Q1Q0 (e)
393
Load_Sum 0 1 0 0
Out 0 0 0 1
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Enter
D1
Q1
Load_X
Q'1
Load_Sum
Clk Clr
D0
Out
Q0
Clk Clr
Clock Reset
Q'0
Done Xeq0
(f) Figure 11.11. Control unit for the summing input numbers problem: (a) state diagram; (b) next-state table; (c) kmaps and excitation equations; (d) control word/output truth table; (e) output equations; (f) circuit. The final microprocessor can now be easily formed by connecting the control unit and the datapath together using the designated control and status signals as shown in Figure 11.12. A sample simulation is shown in Figure 11.13. This complete microprocessor circuit is on the accompanying CD-ROM in the file MP.GDF, and can be synthesized and simulated. Enter
Input X
8 Input_X
Enter Load_X Load_Sum Out
Control Signals
Load_X Load_Sum Out
CU Clock Reset
Clock Reset
DP Xeq0
Status Signal
Done
Xeq0
Clock Reset Output
8 Done
Output
Figure 11.12. Microprocessor for solving the summing input numbers problem.
394
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Figure 11.13. Sample simulation for the summing input numbers problem. In order to implement the circuit on the UP2 development board, we need to connect the microprocessor’s I/Os to the switches, LEDs, and clock source. The most important interface circuit for this problem is to debounce the Enter switch. A simple circuit to debounce a switch is to use a D flip-flop as shown in Figure 11.14. The clock frequency for the D flip-flop must be slow enough for the switch bounce to settle so that the flip-flop will latch in a single value. The exact clock frequency is not too critical. The clock divider circuit used in the example slows down the on-board 25MHz clock to approximately 4Hz. 8 DIP switches 8 Reset push button switch Enter push button switch
D
Q
Enter Clock
25MHz Clock
Input_X
Reset
25MHz 4Hz Clock Divider
Clk
MP Done
Q1 Q0
BCD decoder
7-segment LED
Output 8
LED
8 LEDs
Figure 11.14. Interface to the summing input numbers microprocessor for the UP2 development board. The top-level interface circuit is in the file UP2MAX.GDF for the MAX chip on the accompanying CD-ROM. This circuit also displays the current state value, Q1 and Q0, on the 7-segment LED. The FLEX version of this circuit is in the file UP2FLEX.GDF. It displays the sum output on the two 7-segment displays instead of using the eight discrete LEDs. ♦
11.2.3 High-Low Guessing Game Example 11.4 In this example, we will manually design the complete dedicated microprocessor for playing the high-low guessing game. The user picks a number between 0 and 99, and the computer will use the binary search algorithm to guess the number. After each guess, the user tells the computer whether the guess is high or low compared to the picked number. Two push buttons, hi_button and lo_button, are used for the user to tell the computer whether the guess is too high, too low, or correct. The hi_button is pressed if the guess is too high, and the lo_button is pressed if the guess is too low. If the guess is correct, both buttons are pressed at the same time. The algorithm for simulating this high-low guessing game is listed in Figure 11.15. The two range boundary variables, Low and High, are initialized to 0 and 100 respectively. The loop between lines 3 to 11 will keep repeating until both buttons, hi_button and lo_button, are pressed. Inside the loop, line 4 calculates the next guess by finding the middle number between the lower and upper boundaries, and then outputs this new guess in line 5. Lines 6 to 10 checks which button is pressed. If the lo_button is pressed, that means the guess is too low, so line 7 changes the
395
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Low boundary to the current Guess. Otherwise if the hi_button is pressed, that means the guess is too high, and line 9 changes the High boundary to the current Guess. The loop is then repeated with the calculation of the new Guess in line 4. When both buttons are pressed, the condition in line 11 is true, and the loop is exited. Lines 12 to 15 simply cause the display to blink the correct guess by turning it on and off. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Low = 0 // initialize Low High = 100 // initialize High repeat { Guess = (Low + High) / 2 // calculate guess using binary search output Guess if (lo_button = '1' and hi_button = '0') // low button pressed Low = Guess else if (lo_button = '0' and hi_button = '1') // hi button pressed High = Guess end if } until (lo_button = '1' and hi_button = '1') // repeat until both // buttons are pressed while (lo_button = '0' and hi_button = '0') // blink correct guess output Guess turn off display end while
Figure 11.15. Algorithm for the high-low guessing game. We will first design a dedicated datapath for the algorithm. Next, we will design the control unit for the datapath. We will use schematic entry to implement the complete dedicated microprocessor. Finally, we will test it using simulation, and on the UP2 board. The algorithm shown in Figure 11.15 has eight data manipulation operations in lines 1, 2, 4, 5, 7, 9, 13 and 14. The dedicated datapath for realizing this algorithm is shown in Figure 11.16. It requires three 8-bit registers, Low, High, and Guess, for storing the low and high range boundary values, and the guess respectively. Two 2-to-1 muxes are used for the inputs to the Low and High registers to select between the initialization values for lines 1 and 2, and setting the new Guess value for lines 7 and 9. The only arithmetic operations needed are the addition and division by 2 in line 4. Hence, the outputs from the two registers, Low and High, go to the inputs of an adder for the addition, and the output of the adder goes to a shifter. The division by 2 is performed by doing a right shift of 1 bit. The result from the shifter is stored in the register Guess. Depending on the condition in line 6, the value in Guess is loaded into either the Low or the High register by asserting the corresponding load signal for that register. A 2-input by 8-bit AND gate is used to control the output of the Guess number. One 8-bit set of input is connected to the output of the Guess register. The other 8-bit set of input is connected together in common to the output enable OutE signal. By asserting OutE, the data from Guess is passed to the output port. To blink the output display in lines 13 and 14, we just toggle the OutE line. The datapath shown in Figure 11.16 requires five control signals, Init, LOload, HIload, GUESSload, and OutE. The Init signal controls the two muxes to determine whether to load in the initialization values or the new guess. The three load signals, LOload, HIload, and GUESSload control the writing of the three respective registers. Finally, OutE controls the output of the guess value.
396
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
"00000000"
"01100100"
8
8
8 1
0
1
0
Init 8 LOload
8
D7-0 Load 8-bit register Clear Low Q7-0
D7-0 Load 8-bit register Clear High Q7-0
HIload Clear Clock 8
8
8
+
>> 1
8 D7-0 Load 8-bit register Clear Guess Q7-0
GUESSload
8 OutE 8 Output
Figure 11.16. Dedicated datapath for the high-low guessing game. The state diagram for this algorithm requires six states as shown in Figure 11.17 (a). State 000 is the starting initialization state. State 001 executes lines 4 and 5 by calculating the new guess, and outputting it. State 001 also waits for the user key press. If only the lo_button is pressed, then the FSM goes to state 010 to assign the guess as the new low value. If only the hi_button is pressed, then the FSM goes to state 011 to assign the guess as the new high value. If both buttons are pressed, then the FSM goes to state 100 go output the guess. From state 100, the FSM turns on and off the output by cycling between states 100 and 101 until a button is pressed. When a button is pressed from either state 100 or 101, the FSM goes back to the initialization state for a new game. The output table showing the five output signals, Init, LOload, HIload, GUESSload, and OutE, to be generated in each state are shown in Figure 11.17 (d). The corresponding output equations derived from the output table are shown in (e). Again, we will use D flip-flops to implement the state memory. Having six states, three flip-flops are needed, with two unused states. Both the next-state table and the implementation table are shown in Figure 11.17 (b). Recall that when D flip-flops are used for the implementation, the next-state table and the implementation table are the same because the characteristic equation for the D flip-flop is Qnext = D. The implementation table is simply the truth table for the three variables D2, D1, and D0. Hence, the excitation equations for the three flip-flops, D2, D1, and D0, are derived directly from the implementation table. The K-maps and equations for these three variables are shown in Figure 11.17 (c).
397
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Using the three excitation equations for deriving the next-state logic circuit, the three D flip-flops for the state memory, and the five output equations for deriving the output logic circuit, we get the complete control unit circuit for the high-low guessing game as shown in Figure 11.17 (f).
000 Low = 0 High = 100 Guess = (Low + High) / 2 output Guess
hi' & lo' 001
hi' & lo
hi & lo' hi & lo
010
011
Low = Guess
High = Guess
hi or lo
100
hi' & lo'
hi' & lo' hi or lo
Output Guess
101 Turn off lights (a)
Current State Q2Q1Q0 000 001 010 011 100 101 110 (not used) 111 (not used)
Next State Q2next Q1next Q0next (Implementation D2 D1 D0) hi, lo 00 01 10 11 001 001 001 001 001 010 011 100 001 001 001 001 001 001 001 001 101 000 000 000 100 000 000 000 000 000 000 000 000 000 000 000 (b)
D2
hi, lo Q1Q0 00
Q2 = 0 01
11
00 01
1
Q2 = 1 10
00
01
11
10
1
Q2Q1' hi' lo'
1
Q2'Q1'Q0 hi lo
11 10
D2 = Q2Q1' hi' lo' + Q2'Q1'Q0 hi lo
398
Digital Logic and Microprocessor Design with VHDL
D1
hi, lo Q1Q0 00
Chapter 11 - Dedicated Microprocessors
Q2 = 0 01
11
Q2 = 1 10
00
01
11
10
00 01
1
Q2'Q1'Q0 hi lo'
1
11 Q2'Q1'Q0 hi' lo
10
D1 = Q2'Q1'Q0 hi lo' + Q2'Q1'Q0 hi' lo
D0
hi, lo Q1Q0 00
Q2 = 0
Q2 = 1
01
11
10
00
1
1
1
1
00
1
01
1
11
1
1
1
1
10
1
1
1
1
01
11
1
10
Q1'Q0' hi' lo' Q2'Q0' Q2'Q1 Q2' lo'
D0 = Q1'Q0' hi' lo' + Q2'Q0' + Q2'Q1 + Q2' lo' (c) Control Word 0 1 2 3 4 5
State Q2 Q1 Q0 000 001 010 011 100 101
Instruction Initialize Guess Low = Guess High = Guess Correct Flash output
Init 1 0 0 0 0 0 (d)
Init = Q2'Q1'Q0' HIload = Q2'Q1'Q0' + Q2'Q1Q0 LOload = Q2'Q1'Q0' + Q2'Q1Q0' GUESSload = Q2'Q1'Q0 OutE = Q2' + Q0' (e)
399
HIload 1 0 0 1 0 0
LOload 1 0 1 0 0 0
GUESSload 0 1 0 0 0 0
OutE 1 1 1 1 1 0
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Init D2
Q2
Clk
HIload
Clr Q'2
D1
Q1
LOload
Clk Clr
Q'1
GUESSload D0
Q0
Clk Clr
OutE Q'0
Clock Reset lo hi
(f) Figure 11.17. Control unit for the high-low guessing game: (a) state diagram; (b) next-state table; (c) k-maps and excitation equations; (d) control word/output truth table; (e) output equations; (f) circuit. Connecting the control unit circuit shown in Figure 11.17 (f) and the datapath circuit shown in Figure 11.16 together using the control and status signals produces the final microprocessor as shown in Figure 11.18. This complete microprocessor circuit is on the accompanying CD-ROM in the file MP.GDF, and can be synthesized and simulated.
Init HIload LOload GUESSload CU OutE Clock Reset
Clock Reset
lo hi
Control Signals
Status Signals
Init HIload LOload GUESSload OutE DP Clock Reset
lo hi Output
8 Output
Figure 11.18. Microprocessor for the high-low guessing game.
400
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Figure 11.19 shows the interface needed to test the high-low guessing game dedicated microprocessor on the UP2 development board. The top-level file UP2FLEX.GDF in the implementation directory can be used for downloading and testing this circuit on the UP2 development board using the FLEX PLD. ♦ High push button switch
Low push button switch
In Out One-shot Reset Clock In Out One-shot Reset Clock
Hi Lo Reset Clock
MP
Output
binary to decimal
two 7-segment LEDs
Reset dip switch 25MHz Clock
25MHz 4Hz Clock Divider
Figure 11.19. Interface to the high-low guessing game microprocessor for the UP2 development board.
11.2.4 Finding Largest Number Example 11.5 In this example, we will implement the classic computer program of inputting several numbers, and then output the largest of these numbers. We will assume that the numbers, entered through one input port, are 8-bit unsigned numbers, and a zero will terminate the inputs. The current largest number is always displayed. The algorithm for solving this problem is listed in Figure 11.20. 1 2 3 4 5 6 7 8 9
Largest = 0 // for storing the current largest number input X // enter first number while (X ≠ 0){ if (X > Largest) then // if new number greater? Largest = X // yes, remember new largest number end if output Largest input X // get next number }
Figure 11.20. Algorithm for the finding largest number problem. We will first design a dedicated datapath for the algorithm. Next, we will design the control unit for the datapath. We will use schematic entry to implement the complete dedicated microprocessor. Finally, we will test it using simulation, and on the UP2 board. The algorithm shown in Figure 11.20 has five data manipulation operations in lines 1, 2, 5, 7, and 8. It requires two registers: an 8-bit register for storing X, and an 8-bit register for storing Largest. No functional unit for performing arithmetic is needed. The dedicated datapath is shown in Figure 11.21.
401
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Input_X 8 D Load 8-bit register Clear X
Load_X Clear Clock
8 Xeq0 8
Load_Largest
XgtLargest
Load 8-bit register Clear Largest
>
8
8 Output
Figure 11.21. Dedicated datapath for the finding largest number problem. Line 1 in the algorithm is performed by asserting the Clear signal, line 2 and 8 are performed by asserting the Load_X signal, and line 5 is performed by asserting the Load_Largest signal. The algorithm continuously outputs Largest. The conditional test (X = 0) is generated by the 8-input NOR gate, and the conditional test (X > Largest) is generated by the greater-than comparator. This algorithm is very similar to the summing input numbers problem in Example 11.3, especially the situation for handling the Enter key for inputting each number through the same input port. The state diagram is shown in Figure 11.22 (a). State 00 inputs X and waits for the Enter signal. This allows the user to set up the input number, and then press the Enter key. When the Enter key is pressed, the FSM goes to state 01 to make two tests. If (X = 0) is true, the FSM terminates in state 11. If (X > Largest) is true, then the FSM goes to state 10 to assign X as the new largest number. If X is not 0 and not the largest, then the FSM will go back to state 00 to wait for another input number. Before going back to state 00 from both states 01 and 10, we need to wait for the release of the Enter key as explained in Example 11.3. The next-state table as derived from the state diagram is shown in Figure 11.22 (b). The table requires five variables; two to encode the four states, Q1 and Q0, and three status signals, Enter, (X = 0), and (X > Largest). Using D flip-flops to implement the state memory, the implementation table is the same as the next-state table except that the values in the table entries are for the inputs to the flip-flops, D1 and D0. The k-maps and the excitation equations for D1 and D0 are shown in Figure 11.22 (c). The control words and output truth table for the three control signals, are shown in Figure 11.22 (d). State 00 performs Lines 2 and 8 of the algorithm in Figure 11.20 by asserting the Load_X signal. All the states output Largest, and this action does not require any control signals. State 10 performs Line 5 by asserting Load_Largest. State 11 outputs a Done signal to inform the user that the FSM has stopped. The output equations as derived from the output table are shown in Figure 11.22 (e). There is one equation for each of the three control signals. Each equation is only dependent on the current state, Q1 and Q0. The complete control unit circuit is shown in Figure 11.22 (f). The state memory consists of two D flip-flops. The inputs to the flip-flops are the next-state circuits derived from the two excitation equations. The output circuits for the three control signals are derived from the three output equations. The status signal (Xeq0) come from the NOR gate comparator in the datapath, and the status signal (XgtLargest) come from the greater-than comparator in the datapath.
402
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Enter' 00
input X output Largest
Enter Enter (X > Largest)' & Enter'
01
output Largest (X = 0)
(X > Largest) Enter'
11
output Largest
10
output Largest
Enter (a) Current State Q1Q0 00 01 10 11
000 00 00 00 11
Next State Q2next Q1next Q0next Enter, (X=0), (X>Largest) 001 010 011 100 101 110 00 00 00 01 01 01 10 11 11 01 10 11 00 00 00 10 10 10 11 11 11 11 11 11
111 01 11 10 11
(b) D1
Enter = 0 (X=0), (X>Largest) Q1Q0 00 01 11 10
Enter = 1 00
01
11
10
1
1
1
1
1
1
1
1
1
1
1
00
Q0(X=0)
01 11
Q0(X>Largest)
1
1
1
1
1
1
1
10
Q1Enter
Q1Q0
D1 = Q1Q0 + Q1Enter + Q0 (X=0) + Q0 (X>Largest) D0
Enter = 0 (X=0), (X>Largest) Q1Q0 00 01 11 10 00
Q1'Enter(X>Largest)'
00
01
11
10
1
1
1
1
1
1
1
1
Q1'Q0'Enter
01 11
Enter = 1
1
1
1
1
1
1
1
1
10 Q1Q0
403
1
Q0(X=0)
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
D0 = Q1Q0 + Q0 (X=0) + Q1'Q0' Enter + Q1' Enter(X>Largest)' (c) Control Word 0 1 2 3
State Q1 Q0 00 01 10 11
Instruction input X, output Largest output Largest Largest = X, output Largest output Largest
Load_X 1 0 0 0
Load_Largest 0 0 1 0
Done 0 0 0 1
(d) Load_X = Q1'Q0' Load_Largest = Q1Q0' Done = Q1Q0 (e) Enter
Load_X D1
Q1
Load_Largest
Clk Clr
D0
Q'1
Done
Q0
Clk
Clock Reset
Clr
Q'0
Xeq0 XqtLargest (f) Figure 11.22. Control unit for the finding largest number problem: (a) state diagram; (b) next-state table; (c) k-maps and excitation equations; (d) control word/output truth table; (e) output equations; (f) circuit. Connecting the control unit and the datapath together using the control and status signals produces the final microprocessor as shown in Figure 11.23. A sample simulation is shown in Figure 11.24. This complete microprocessor circuit is on the accompanying CD-ROM in the file MP.GDF, and can be synthesized and simulated. The top-level interface circuit for testing this microprocessor on the UP2 development board using the MAX chip is shown in Figure 11.25, and is in the file UP2MAX.GDF on the accompanying CD-ROM. Like the circuit for Example 11.2, this circuit displays the largest number on the eight LEDs in binary, and the current state value, Q1 and Q0, on the 7-segment LED. Again, a debounce circuit is needed for the Enter switch. The 8-bit number inputs are through the eight DIP switches. The FLEX version of this circuit is in the file UP2FLEX.GDF. Instead of displaying the largest number on the eight discrete LEDs, it displays the number on the two 7-segment displays. ♦
404
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Input X
Enter
8 Enter
Input_X
Load_X Load_Largest Out
Control Signals
Load_X Load_Largest Out
CU Clock Reset
DP
Clock Xeq0 Reset XgtLargest
Status Signal
Clock Reset
Xeq0 XgtLargest
Done
Output
8 Done
Output
Figure 11.23. Microprocessor for solving the finding largest number problem.
Figure 11.24. Sample simulation trace for the finding largest number problem. The last output number 15 when Done is asserted is the largest of the six numbers entered 7, 4, 12, 8, 15, and 6. 8 DIP switches 8
Reset push button switch Enter push button switch
D
Q
Enter Clock
25MHz Clock
25MHz 4Hz Clock Divider
Input_X
Reset
Clk
MP Done
Q1 Q0
BCD decoder
Output 8
LED
8 LEDs
Figure 11.25. Interface to the finding largest number microprocessor for the UP2 development board.
405
7-segment LED
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
11.3 VHDL for Dedicated Microprocessors In the introduction to this chapter, we mentioned that we can use VHDL based on different models to automatically synthesize a microprocessor. The FSM+D model requires the manual construction of the datapath, but the FSM is described using VHDL, and is synthesized automatically. The FSMD model also uses VHDL to describe the FSM, but in addition, the datapath operations are imbedded within the FSM VHDL code using built-in VHDL operators. Finally, the behavioral model uses VHDL to describe the behavior of the complete microprocessor algorithmically, and so both the control unit and the datapath are synthesized automatically.
11.3.1 FSM + D Model In Section 11.1, we constructed a dedicated microprocessor by manually constructing the circuits for a control unit and a datapath. These two components are then connected together to form the microprocessor. Instead of manually constructing the control unit circuit, an alternative way is to automatically synthesize it from behavioral VHDL code. A separate datapath circuit is still manually constructed like before. The two components are then joined together to form the complete microprocessor. Writing the behavioral VHDL code for the control unit was discussed in Chapter 10. The method is again illustrated in Example 11.6. The final dedicated microprocessor is constructed using an enclosing VHDL entity written at the structural level that combines the VHDL code for both the control unit entity and the datapath entity. Example 11.6 In this example, we will again construct the dedicated microprocessor for the summation algorithm of Example 9.3 similar to Example 11.1. However, unlike Example 11.1 where the circuit for the control unit is manually derived, we will use VHDL to automatically synthesize the control unit circuit. In order to write the behavioral VHDL code for the control unit, we need to use the information from the state diagram and the control words derived for the algorithm. The control words were manually derived in Example 9.3, and the state diagram was manually derived in Example 10.2. The state diagram and control words from these two examples are repeated here in Figure 11.26. The complete behavioral VHDL code for the control unit is shown in Figure 11.27. In the entity section, the output signals include the 16 control signals for controlling the datapath. There is one input signal neq0 in the entity section, which is the status signal (n ≠ 0) from the datapath. In addition to the control and status signals, there is the control input signal Start and the control output signal Done. Finally, there are the two global input signals Clock and Reset. The Reset signal is to reset all the flip-flops in the state memory to zero. The architecture section starts out with using the TYPE statement to define all the states used in the state diagram. The SIGNAL statement declares the signal state to remember the current state of the FSM. There are two processes in the architecture section, the next-state logic process and the output logic process that execute concurrently. As the name suggests, the next-state process defines the next-state logic circuit that is inside the control unit, and the output logic process defines the output logic circuit inside the control unit. The main statement within these two processes is the CASE statement that determines what the current state is. For the next-state process, the CASE statement is executed only at the rising clock edge because of the test (clock'EVENT AND clock = '1') in the IF statement. Hence, the State signal is assigned a new state value only at the rising clock edge. The new state value is, of course, dependent on the current state and input signals, if any. For example, in state s0, the next state is dependent on the input signal Start, whereas, in state s1, the next state is unconditionally s2. In the output process, all control signals are generated for every case, i.e., all the control signals must be assigned a value in every case. Recall from Section 6.13.1 that VHDL synthesizes a signal using a memory element if the signal is not assigned a value in all possible cases. However, the output circuit is a combinational circuit, and it should not contain any memory elements. For each state in the CASE statement in the output process, the values assigned to each of the output signal are taken directly from the control word/output table. For the datapath, we are using the same general datapath as discussed in Section 8.5. The VHDL code for this datapath is listed in Figure 8.12 and Figure 8.13. Figure 8.12 describes the individual components used in the datapath and Figure 8.13 combines these components into the desired datapath.
406
Digital Logic and Microprocessor Design with VHDL
Chapter 11 - Dedicated Microprocessors
Finally, Figure 11.28 combines the datapath and the control unit together using structural VHDL coding to produce the top-level entity sum for the microprocessor. The entity section specifies the primary I/O signals for the microprocessor. In addition to the global Clock and Reset signals, there are the four I/O signals shown in Figure 11.2. These are the control input Start signal, the control output Done signal, the datapath input, and the datapath output. The architecture section declares the two components, fsm and datapath, used in this module. These two components are connected together using the 16 control signals and one status signal, and are declared as internal signals using the SIGNAL declaration statement. Finally, two PORT MAP statements are used to actually connect the control unit and the datapath together. A sample simulation trace is shown in Figure 11.29 for the input n = 10. After asserting the Start signal in state s0, the input value 10 is read in during state s2. The value 10 is written to RF1 at the next rising clock edge, which also brings the FSM to state s3. RF1 is for storing n that counts from 10 down to 0. RF0 is for storing sum and is updated after each count of n. When RF1 reaches zero, the sum 55 from RF0 is sent to the output, and the Done ♦ signal is asserted. At this point, the FSM stays in state s0 indefinitely waiting for another start signal.
Start' s0 Start
(n ≠ 0)
s1
control word 1 sum = 0
s2
control word 2 input n
s3
control word 3 sum = sum + n
s4
control word 4 n=n-1
(n ≠ 0)' s5
(a)
407
control word 5 output sum
Digital Logic and Microprocessor Design with VHDL
Control Word 1 2 3 4 5
Instruction sum = 0 input n sum = sum + n n=n–1 output sum
IE 15 0 1 0 0 ×
WE 14 1 1 1 1 0
WA1,0 13-12 00 01 00 01 ××
Chapter 11 - Dedicated Microprocessors
RAE 11 1 0 1 1 1
RAA1,0 10-9 00 ×× 00 01 00
RBE 8 1 0 1 0 0
RBA1,0 7-6 00 ×× 01 ×× ××
ALU2,1,0 5-3 101 (subtract) ××× 100 (add) 111 (decrement) 000 (pass)
SH1,0 2-1 00 00 00 00 00
(b) Figure 11.26. For Example 11.6: (a) state diagram; (b) control words. LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; ENTITY fsm IS PORT ( clock, reset, start: IN STD_LOGIC; IE: OUT STD_LOGIC; WE: OUT STD_LOGIC; WA: OUT STD_LOGIC_VECTOR(1 DOWNTO 0); RAE: OUT STD_LOGIC; RAA: OUT STD_LOGIC_VECTOR(1 DOWNTO 0); RBE: OUT STD_LOGIC; RBA: OUT STD_LOGIC_VECTOR(1 DOWNTO 0); aluSel : OUT STD_LOGIC_VECTOR(2 DOWNTO 0); shSel: OUT STD_LOGIC_VECTOR(1 DOWNTO 0); OE: OUT STD_LOGIC; done: OUT STD_LOGIC; neq0: IN STD_LOGIC); END fsm; ARCHITECTURE fsm_arc OF fsm IS TYPE state_type IS (s0, s1, s2, s3, s4, s5); SIGNAL state: state_type; BEGIN next_state_logic: PROCESS(reset, clock) BEGIN IF(reset = '1') THEN state IF (start = '1') THEN state -- reset state state CASE IR IS WHEN "000" => state state state state state state state state state state state state state IF (Enter = '0') THEN -- wait for the Enter key for inputs state