This tutorial introduces you to custom instructions using the Altera® Nios®embedded processor version 2.1. It shows you how to use SOPC Builder version 2.5 and higher to implement a custom instruction into your Nios system module. Table 1shows the tutorial revision history.
Table 1. Tutorial Revision History Date Description June 2002 First publication. The tutorial introduces the custom instruction feature of the Nios embedded processor version 2.1 and SOPC Builder 2.52. The Adobe Acrobat Find feature allows you to search the contents of a PDF file. Click the binoculars toolbar icon to open the Find dialog box. Bookmarks serve as an additional table of contents. which provide miniature previews of each page,Thumbnail icons, provide a link to the pages. Numerous links, shown in green text, allow you to jump to related information.
iii
About this Document
Nios Custom Instructions Tutorial
How to ContactFor the most up-to-date information about Altera products, go to the AlteraAltera world-wide web site athttp://www.altera.com. For technical support on this product, go to http://www.altera.com/mysupport. For additional information about Altera products, consult the sources shown inTable 2.
Table 2. How to Contact Altera Information Type USA & Canada All Other Locations Technical supporthttp://www.altera.com/mysupport http://www.altera.com/mysupport (800) 800-EPLD (3753) (408) 544-7000(1) (7:30 a.m. to 5:30 p.m. (7:30 a.m. to 5:30 p.m. Pacific Time) Pacific Time) Product literaturehttp://www.altera.com http://www.altera.com Altera literature serviceslit_req@altera.com(1)lit_req@altera.com(1) Non-technical customer (800) 767-3753 (408) 544-7000 service (7:30 a.m. to 5:30 p.m. Pacific Time) FTP siteftp.altera.com ftp.altera.com Note: (1) You can also contact your local Altera sales office or sales representative. DocumentationAltera values your feedback. If you would like to provide feedback on this Feedbackotliagm-.ee—dnescudontmen.,ioclsardifoiccsat@iaolnterreaq.cuoesmo.rei,srucaanccs,its—iencteisnscoin _
iv
Altera Corporation
Nios Custom Instructions Tutorial
About this Document
TypographicThewhosNnioisnCTuasbtolem3I.nstructions Tutorialuses the typographic conventions Conventions
Table 3. Conventions Visual Cue Meaning Bold Type with Initialbox titles, checkbox options, and dialog box options areCommand names, dialog Capital Lettersshown in bold, initial capital letters. Example:Save Asdialog box. bold typetiming parameters, directory names, project names, disk drive names,External filenames, filename extensions, and software utility names are shown in bold type. Examples:fMAX, \QuartusIIdirectory,d:drive,chiptrip.gdffile. Italic Type with InitialDocument titles are shown in italic type with initial capital letters. Example:AN 75 Capital Letters (High-Speed Board Design). Italic typeInternal timing parameters and variables are shown in italic type. Examples:tPIA,n+ 1. Variable names are enclosed in angle brackets (< >) and shown in italic type. Example: <file name>, <project name>.poffile. Initial Capital Letters Keyboard keys and menu names are shown with initial capital letters. Examples: Delete key, the Options menu. “Subheading Title References to sections within a document and titles of on-line help topics are shown in quotation marks. Example: “Typographical Conventions. Courier typeand port names are shown in lowercase Courier type. Examples:Signal data1,tdi, input.Active-low signals are denoted by suffixn, e.g.,resetn. Anything that must be typed exactly as it appears is shown in Courier type. For example:.epddigstIpIiqr\cu\tiohtinrgl\asc:rtusf\qua. Also, sections of an actual file, such as a Report File, references to parts of files (e.g., the AHDL keywordSUBDESIGN), as well as logic function names (e.g.,TRI) are shown in Courier. 1., 2., 3., and a., b., c.,... Numbered steps are used in a list of items when the sequence of the items is important, such as the steps listed in a procedure. Bullets are used in a list of items when the sequence of the items is not important. va procedure that consists of one step only.The checkmark indicates 1The hand points to information that requires special attention. rThe angled arrow indicates you should press the Enter key. fdirect you to more information on a particular topic.The feet
Altera Corporation
v
Contents
About this Document .............................................................................................................................. ... iii How to Find Information ....................................................................................................... ....... iii How to Contact Altera ......................................................................................................... ......... iv Documentation Feedback ........................................................................................................ ..... iv Typographic Conventions ....................................................................................................... .......v Tutorial Overview .............................................................................................................................. ..........9 Introduction .................................................................................................................. .................... 9 Example 1: Floating Point Unit ..............................................................................................9 Example 2: Binary to BCD Converter ................................................................................... 9 Hardware & Software Requirements ............................................................................................ 9 Tutorial Files ................................................................................................................ ...................10 More Information .............................................................................................................. ............. 10 Example 1: Floating Point Unit ..............................................................................................................11 Introduction .................................................................................................................. .................. 11 Open the Quartus II Project ................................................................................................... ....... 11 Add & Instantiate the fpu.vqm Module ..................................................................................... 12 Generate the Nios System Module ...................................................................................... 19 Compile the Quartus II Project ............................................................................................20 Program the Nios Development Board ..............................................................................20 Edit the Macros ............................................................................................................... ................21 Build & Download Executable Files to the Nios System .........................................................23 Example 2: Binary to BCD Converter ...................................................................................................27 Introduction .................................................................................................................. .................. 27 Open the Quartus Project ...................................................................................................... .......27 Open the Nios System Module ................................................................................................... .28 Add Custom Instructions to the Nios CPU ................................................................................29 Add the binary_to_bcd.v Custom Instruction File ...........................................................31 Add the display.v Custom Instruction File ........................................................................ 32 Compile & Download the Files to the Board ............................................................................. 35 Compile the Quartus II Project ............................................................................................35 Create & Download Executable Files to the Nios System ............................................... 37
Altera Corporation
vii
Introduction
Hardware & Software Requirements
Altera Corporation
Tutorial Overview
This tutorial introduces you to custom instructions using the Altera Nios embedded processor version 2.1 and shows you how to use SOPC Builder version 2.5 and higher to add a custom instruction to your Nios microprocessor. This tutorial guides you through the steps for implementing two example custom instructions in a Nios system module and describes how to access these custom instructions through software. This tutorial is for Nios novices or users who are new to using embedded systems in PLDs. 1This tutorial assumes you have already completed theNios Tutorial.TheNios Tutorialshows you how to use the SOPC Builder and the Quartus®II software to create your own Nios system module that interfaces with the Nios development board. This tutorial is divided into the following sections:
Example 1: Floating Point Unit “Example 1: Floating Point Unit” on page 11implements a multi-cycle custom instruction with a prefix. It also illustrates how to edit the software macro for use with non-integer variables.
Example 2: Binary to BCD Converter “Example 2: Binary to BCD Converter” on page 27implements multiple custom instructions. One of these custom instructions has export ports that are accessible from outside the Nios system module. This tutorial requires: A PC running the Windows NT or 2000 operating system Nios embedded processor version 2.1/SOPC Builder version 2.5 and higher GNUPro®Nios software development tools version 2.1 II software version 1.1 or higherQuartus A Nios development board, set up as described in theNios Embedded Processor Getting Started User Guide The ByteBlaster™ driver, installed as described in theQuartus II Installation & Licensing for PCsmanual
9
1
Tutorial Overview
Tutorial Files
More Information
Altera Corporation
Nios Custom Instructions Tutorial
1When you install the Nios embedded processor, the installation program also installs the LeonardoSpectrum™ software. The SOPC Builder uses this version of the LeonardoSpectrum software when synthesizing a Nios system module. You can request a free license file for this software from the Nios Development Software Licenses page on the Altera web site at http://www.altera.com.Your license file also contains a license for the Quartus II software. The required files to complete this tutorial successfully are in thetutorialsfolder in the SOPC Builder 2.5 installation directory. The default installation directory is:
C:/altera/excalibur/sopc builder 2 5/tutorials/Cust Inst Example1 _ _ _ _ _ C:/altera/excalibur/sopc builder 2 5/tutorials/Cust Inst Example2 _ _ _ _ _ You can also find these files bundled with the Custom Instructions Tutorial atp:tthalw.ww//moc.arettaretil/lit-ure/htmlnio.. SeeAN 188: Custom Instructions for the Nios Embedded Processorfor mo
er
information about the Nios custom instruction feature and further details about implementing custom instructions.
10
Introduction
Open the Quartus II Project
Altera Corporation
Example 1: Floating Point Unit
This tutorial section guides you through the steps required to implement a multi-cycle custom instruction that uses a prefix port. It also guides you through the steps required to edit the macro for non-integer variables. The custom instruction you will add is contained in thefpu.vqmmodule. This Verilog Quartus Mapping (.vqm) file contains a pre-synthesized netlist of a floating-point custom instruction design. The custom instruction design module is namedfpu. This instruction performs four basic floating-point unit operations: absolute, multiply, multiply with negate, and negate value. The unit takes two 32-bitfloatvalues as inputs, and generates one 32-bit float value as output. The prefix value as shown inTable 4controls the operation that is performed.
Table 4. Custom Instruction Floating-Point Unit Operation Function Prefix Floating-Point Unit Operation Absolute 0 result =|dataa| Negate 1 result =−dataa Multiply 2 result =dataa×datab Multiply with negate 3 result =−(dataa×datab) 1Altera has not testedfpu.vqmfor IEEE compliance. The use of this floating-point unit is for demonstration purposes only. To start the Quartus II software and open the custom instruction project, follow these steps: 1. ChoosePrograms > Altera> Quartus II<version> (Windows Start menu) to start the Quartus II software. 2. ChooseOpen Project(File menu) to open the project. 3. Browse to the working directory of your project. This tutorial uses the following default directory: c:\altera\excalibur\sopc builder 2 5\tutorials\Cust Inst Exa _ _ _ _ _ mple1