38
Tutorial Nios Custom Instructions 101 Innovation Drive San Jose, CA 95134 (408) 544-7000 Document Version: 1.1 http://www.altera.com Document Date: September 2002

Nios Custom Instructions - · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Embed Size (px)

Citation preview

Page 1: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Tutorial

Nios Custom Instructions

101 Innovation DriveSan Jose, CA 95134(408) 544-7000 Document Version: 1.1http://www.altera.com Document Date: September 2002

Page 2: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

ii Altera Corporation

Copyright Nios Custom Instructions Tutorial

Copyright © 2002 Altera Corporation. All rights reserved. Altera, The Programmable Solutions Company, the stylized Altera logo,specific device designations, and all other words and logos that are identified as trademarks and/or service marks are, unlessnoted otherwise, the trademarks and service marks of Altera Corporation in the U.S. and other countries. All other product orservice names are the property of their respective holders. Altera products are protected under numerous U.S. and foreign patentsand pending applications, mask work rights, and copyrights. Altera warrants performance of its semiconductorproducts to current specifications in accordance with Altera’s standard warranty, but reserves the right to makechanges to any products and services at any time without notice. Altera assumes no responsibility or liabilityarising out of the application or use of any information, product, or service described herein except as expresslyagreed to in writing by Altera Corporation. Altera customers are advised to obtain the latest version of devicespecifications before relying on any published information and before placing orders for products or services.

TU-NIOSCITTRL-1.1

Page 3: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Altera Corporation

About this Document

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 1 shows the tutorial revision history.

How to Find Information

� 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.� Thumbnail icons, which provide miniature previews of each page,

provide a link to the pages.� Numerous links, shown in green text, allow you to jump to related

information.

Table 1. Tutorial Revision History

Date Description

September Edits made for Nios 2.2 release

June 2002 First publication. The tutorial introduces the custom instruction feature of the Nios embedded processor version 2.1 and SOPC Builder 2.52.

iii

Page 4: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

About this Document Nios Custom Instructions Tutorial

How to Contact Altera

For the most up-to-date information about Altera products, go to the Altera world-wide web site at http://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 in Table 2.

Note:(1) You can also contact your local Altera sales office or sales representative.

Documentation Feedback

Altera values your feedback. If you would like to provide feedback on this document—e.g., clarification requests, inaccuracies, or inconsistencies—send e-mail to [email protected].

Table 2. How to Contact Altera

Information Type USA & Canada All Other Locations

Technical support http://www.altera.com/mysupport http://www.altera.com/mysupport

(800) 800-EPLD (3753)(7:30 a.m. to 5:30 p.m. Pacific Time)

(408) 544-7000 (1)(7:30 a.m. to 5:30 p.m. Pacific Time)

Product literature http://www.altera.com http://www.altera.com

Altera literature services [email protected] (1) [email protected] (1)

Non-technical customer service

(800) 767-3753 (408) 544-7000 (7:30 a.m. to 5:30 p.m. Pacific Time)

FTP site ftp.altera.com ftp.altera.com

iv Altera Corporation

Page 5: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial About this Document

Typographic Conventions

The Nios Custom Instructions Tutorial uses the typographic conventions shown in Table 3.

Table 3. Conventions

Visual Cue Meaning

Bold Type with Initial Capital Letters

Command names, dialog box titles, checkbox options, and dialog box options are shown in bold, initial capital letters. Example: Save As dialog box.

bold type External timing parameters, directory names, project names, disk drive names, filenames, filename extensions, and software utility names are shown in bold type. Examples: fMAX, \QuartusII directory, d: drive, chiptrip.gdf file.

Italic Type with Initial Capital Letters

Document titles are shown in italic type with initial capital letters. Example: AN 75 (High-Speed Board Design).

Italic type Internal 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>.pof file.

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 type Signal and port names are shown in lowercase Courier type. Examples: data1, tdi, input. Active-low signals are denoted by suffix n, e.g., resetn.

Anything that must be typed exactly as it appears is shown in Courier type. For example: c:\quartusII\qdesigns\tutorial\chiptrip.gdf. Also, sections of an actual file, such as a Report File, references to parts of files (e.g., the AHDL keyword SUBDESIGN), 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.

v The checkmark indicates a procedure that consists of one step only.

1 The hand points to information that requires special attention.

r The angled arrow indicates you should press the Enter key.

f The feet direct you to more information on a particular topic.

Altera Corporation v

Page 6: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter
Page 7: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Contents

About this Document ................................................................................................................................. iiiHow to Find Information .............................................................................................................. iiiHow to Contact Altera .................................................................................................................. ivDocumentation Feedback ............................................................................................................. ivTypographic Conventions ..............................................................................................................v

Tutorial Overview ........................................................................................................................................9Introduction ......................................................................................................................................9

Example 1: Floating Point Unit ..............................................................................................9Example 2: Binary to BCD Converter ...................................................................................9

Hardware & Software Requirements ............................................................................................9Tutorial Files ...................................................................................................................................10More Information ...........................................................................................................................10

Example 1: Floating Point Unit ..............................................................................................................11Introduction ....................................................................................................................................11Open the Quartus II Project ..........................................................................................................11Add & Instantiate the fpu.vqm Module .....................................................................................12

Generate the Nios System Module ......................................................................................19Compile the Quartus II Project ............................................................................................20Program the Nios Development Board ..............................................................................20

Edit the Macros ...............................................................................................................................21Build & Download Executable Files to the Nios System .........................................................23

Example 2: Binary to BCD Converter ...................................................................................................27Introduction ....................................................................................................................................27Open the Quartus Project .............................................................................................................27Open the Nios System Module ....................................................................................................28Add Custom Instructions to the Nios CPU ................................................................................29

Add the binary_to_bcd.v Custom Instruction File ...........................................................31Add the display.v Custom Instruction File ........................................................................32

Compile & Download the Files to the Board .............................................................................35Compile the Quartus II Project ............................................................................................35Create & Download Executable Files to the Nios System ...............................................37

Altera Corporation vii

Page 8: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter
Page 9: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Altera Corporation

Tuto

1

Tutorial Overview

rial Overview Introduction This tutorial introduces you to custom instructions using the Altera Nios

embedded processor and shows you how to use SOPC Builder 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.

1 This tutorial assumes you have already completed the Nios Tutorial. The Nios Tutorial shows 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 11 implements 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 27 implements multiple custom instructions. One of these custom instructions has export ports that are accessible from outside the Nios system module.

Hardware & Software Requirements

This tutorial requires:

� A PC running the Windows NT or 2000 operating system� Nios embedded processor version 2.1 or higher/SOPC Builder

version 2.5 and higher� GNUPro® Nios software development tools version 2.1� Quartus II software version 1.1 or higher� A Nios development board, set up as described in the Nios Embedded

Processor Getting Started User Guide� The ByteBlaster™ driver, installed as described in the Quartus II

Installation & Licensing for PCs manual

9

Page 10: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Tutorial Overview Nios Custom Instructions Tutorial

Tutorial Files The required files to complete this tutorial successfully are in the tutorials folder in the SOPC Builder installation directory. The default installation directory is:

C:/altera/excalibur/sopc_builder/tutorials/CI_Tutorial

You can also find these files bundled with the Custom Instructions Tutorial at http://www.altera.com/literature/lit-nio.html.

More Information

See AN 188: Custom Instructions for the Nios Embedded Processor for more information about the Nios custom instruction feature and further details about implementing custom instructions.

Altera Corporation 10

Page 11: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Altera Corporation

Example 1: FloatingPoint Unit

Example 1

2

Introduction 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 the fpu.vqm module. This Verilog Quartus Mapping (.vqm) file contains a pre-synthesized netlist of a floating-point custom instruction design. The custom instruction design module is named fpu. This instruction performs four basic floating-point unit operations: absolute, multiply, multiply with negate, and negate value. The unit takes two 32-bit float values as inputs, and generates one 32-bit float value as output. The prefix value as shown in Table 4 controls the operation that is performed.

1 Altera has not tested fpu.vqm for IEEE compliance. The use of this floating-point unit is for demonstration purposes only.

Open the Quartus II Project

To start the Quartus II software and open the custom instruction project, follow these steps:

1. Choose Programs > Altera> Quartus II <version> (Windows Start menu) to start the Quartus II software.

2. Choose Open 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\tutorials\CI_Tutorial\ Cust_Inst_Example1

Table 4. Custom Instruction Floating-Point Unit Operation

Function Prefix Floating-Point Unit Operation

Absolute 0 result = |dataa|

Negate 1 result = − dataaMultiply 2 result = dataa × databMultiply with negate 3 ���������−(dataa × datab)

11

Page 12: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

4. Select standard_32.quartus and click Open. The top-level block diagram file for this project, standard _32.bdf, appears. See Figure 1.

Figure 1. Standard_32.bdf

Add & Instantiate the fpu.vqm Module

You are going to use SOPC Builder version 2.5 to add the fpu.vqm module to the Nios CPU. Using SOPC Builder, this section guides you through the steps to connect the required ports, add the macro name, and set the required CPU clock cycles for fpu.vqm. To add the fpu.vqm module, perform the following steps:

1. Double-click the ref_32_system symbol in standard_32.bdf to launch SOPC Builder. The SOPC Builder System Contents Page for the Nios ref_32_system appears. See Figure 2.

12 Altera Corporation

Page 13: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

Figure 2. SOPC Builder 2.5 System Contents Page

2. Edit the Nios CPU in the SOPC Builder using one of the following methods:

Right–click cpu and choose Edit in the pop-up menu or double-click cpu. The Nios configuration wizard displays.

3. Turn on the Enable advanced configuration controls option in the Architecture tab of the Nios configuration wizard to add custom instructions. See Figure 3.

Altera Corporation 13

Page 14: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

Figure 3. Nios Configuration Wizard

4. Click the Custom Instructions tab. See Figure 4.

Figure 4. Selected Custom Instruction Opcode

1 The Opcode row for USR0 is highlighted to indicate which Opcode you are adding to the CPU.

5. Click Import.

USR0 Highlighted

14 Altera Corporation

Page 15: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

The Interface to User Logic appears. See Figure 5. The Interface to User Logic adds all necessary files and scans the top-level module for the ports required for this design. Next, the Interface to User Logic connects these ports to the associated ports of the Nios arithmetic logic unit (ALU).

Figure 5. Interface to User Logic

6. Click Add.

7. Select the Custom_instructions folder.

8. Click fpu.vqm.

9. Click Open.

The Interface to User Logic automatically enters the top-level module name fpu, which is the correct top-level module name for this example. The Interface to User Logic inserts the module name of the first file opened as the top-level module.

10. Click Populate Port Table.

Altera Corporation 15

Page 16: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

The Interface to User Logic scans all ports in the top-level module. Any ports matching names defined for custom instructions will be connected to the Nios ALU automatically. Any other ports will be routed to the top of the Nios system module and labeled as export.

Figure 6 shows the Interface to User Logic when it has finished processing.

1 The fpu.vqm file may take 15-30 seconds to scan.

Figure 6. Importing Custom Instructions

11. Click Add to System. You return to the Custom Instructions tab. See Figure 7.

You will now add the macro name and the required number of CPU clock cycles. By default, the macro name is the first four characters of the top-level module name. The macro name of this custom instruction becomes fpu and will be the name used to access your custom instruction in software.

f See the SOPC Builder Data Sheet for more system module information.

16 Altera Corporation

Page 17: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

Figure 7. Custom Instructions Tab

13. Enter 5 r under Cycle Count. See Figure 7. Use 5 as the CPU clock cycle for fpu to return the correct output value to the CPU.

Figure 8 shows a simulation of fpu.vqm and enumerates the clock cycle count used for the longest running portion of this custom instruction.

Highlighted

Clock Cycles

Altera Corporation 17

Page 18: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

Figure 8. fpu Simulation with 5 Count Cycle

14. Click the Software Configuration tab. See Figure 9.

15. Turn off Use small printf. This setting allows you to print floating- point values to the Nios SDK Shell later in this example.

Figure 9. Software Configuration Tab

0 1 2 3 4 5

Start asserted data valid Custom instruction executed Result valid from customer instruction

Five count cycle

18 Altera Corporation

Page 19: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

16. Click Finish. This returns you to the SOPC Builder System Contents Page.

You have instantiated the fpu.vqm module as a custom instruction in the Nios CPU. Next, you will generate the Nios system module and compile the design in the Quartus II software.

Generate the Nios System Module

To generate the Nios system module, perform the following steps:

1. Click the System Generation tab in SOPC Builder. See Figure 10.

2. Click Generate.

The SOPC Builder re-creates the Nios cpu with the custom instruction added. It also creates the C/C++ and Assembly macros in excalibur.h and excalibur.s, respectively. Figure 10 shows the SOPC Builder when it has finished generating.

Figure 10. System Generation Completed Message

System generation completed message

Altera Corporation 19

Page 20: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

3. When system generation is complete, click Exit to return to the Quartus II software.

Compile the Quartus II Project

To compile the Quartus II project, perform the following steps:

3. Choose Start Compilation (Processing menu).

4. After the compilation is successful, click OK as shown in Figure 11.

Figure 11. Compilation Complete Message

Program the Nios Development Board

You must properly connect and set up the ByteBlasterMVTM cable to transmit configuration data over the JTAG port to download the hardware image and configure the APEXTM device on the Nios development board. See the Nios Embedded Processor Development Board Data Sheet for JTAG connector information.

1. Click Open. (File menu).

2. In Files of type, choose Programming Files (*.cdf, *.sof, *pof).

3. Select standard_32.cdf and click Open. The programmer window appears with the chain description file, standard_32.cdf.

The standard_32.cdf includes the programming file standard_32.sof, JTAG as the programming mode, and ByteBlasterMV as the programming hardware type.

1 Verify that the Programming Hardware Type is ByteBlasterMV and the Hardware Mode is JTAG.

20 Altera Corporation

Page 21: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

Figure 12. JTAG Chain Description File

4. Click Start.

1 The progress bar displays 0% to 100% as it downloads the configuration. When the device is configured, the microprocessor begins running and messages transmit over the communication serial port if you have a terminal connected to it.

Edit the Macros In the last part of this example, you create and download the executable to the Nios system. First, you must edit the macros created by the SOPC Builder because the fpu.vqm module uses type float as the inputs and output. By default, SOPC Builder builds the macros to use type int as the inputs and output.

To edit the macro, perform the following steps:

1. Choose Open (File menu).

2. In Files of type, choose Software Files (*.c, *.cpp, *h, *.s, *.asm).

3. Select the cpu_sdk\inc directory.

4. Select excalibur.h and click Open.

5. In excalibur.h, scroll to the Structure and Routines For Each Peripheral section, which defines macros nm_fpu and nm_fpu_pfx. See Figure 13.

Programming Hardware Type - ByteBlasterMV

Programming Mode - JTAG

Altera Corporation 21

Page 22: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

6. Replace the int with float for each macro in the line under the #define statement.

Figure 13. excalibur.h – nm_fpu and nm_fpu_pfx Macros

This change lets you input float values to the custom instruction. The output of the macro is also type float, because (_x) is returned as the output.

1 excalibur.h and the macros are regenerated whenever you generate the Nios system module. If you are not using type int as your inputs and output to the macro, you must edit the macro every time you generate the Nios system. To work around this issue, cut and paste your macro into a different header file or include it in your source. Rename the macro to avoid a conflict with the macro produced by the SOPC Builder. Another solution is to use typecasting within your C/C++ source code.

f See AN 188 Custom Instructions for the Nios Embedded Processor for detailed custom instruction information.

7. Choose Save (File menu) to save the changes to excalibur.h.

Change int to float

Change int to float

22 Altera Corporation

Page 23: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

Build & Download Executable Files to the Nios System

To build and download the software, perform the following steps:.

1. Choose Programs > Altera> Nios <version> > Nios SDK Shell (Windows Start menu).

2. To change to the Cust_Inst_Example1\cpu_sdk\src directory, type the following command at the Nios SDK Shell prompt:

cd ../tutorials/CI_Tutorial/Cust_Inst_Example1/cpu_sdk/src r

Figure 14. Build and Download – cpu sdk/src

Altera provides a reference software design in the SDK directory for the tutorial design. This file ci_fpu.c, uses the custom instruction hardware that is now included in the Nios CPU.

3. To build the software, type the following command:

nios-build ci_fpu.c r

Figure 15 shows an example of the code (ci_fpu.c) using the nm_fpu_pfx macro to perform floating-point multiplication.

Altera Corporation 23

Page 24: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 1: Floating Point Unit Nios Custom Instructions Tutorial

Figure 15. ci_fpu.c Using the nm_fpu_pfx Macro to Perform Floating-Point Multiplication

/******************//* Multiplication *//******************/

dwStartTick=GetTickCount(); /* record start time*/

res_a=a*b;

lTicksUsed=GetTickCount(); /* record end time */

printf("\nFor Nios Software implementation:"); printf("\nValue of a is: %f", a); printf("\nValue of b is: %f", b); printf("\nValue of a*b is: %f", res_a);

CheckTimeStamp (dwStartTick, lTicksUsed, timer_overhead);

our_dwStartTick=GetTickCount(); /* record start time*/

res_a = nm_fpu_pfx(2, a, b); /* pfx = 2 -> floating point multiply*/

our_lTicksUsed=GetTickCount(); /* record end time */

printf("\nFor our floating point module: "); printf("\nValue of a is: %f", a); printf("\nValue of b is: %f", b); printf("\nValue of a*b is: %f", res_a);

CheckTimeStamp (our_dwStartTick, our_lTicksUsed, timer_overhead);

4. When the build finishes, download the .srec by typing the following command:

nios-run ci_fpu.srec r

Figure 16 shows an example of the command in the Nios SDK Shell.

24 Altera Corporation

Page 25: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 1: Floating Point Unit

Example 1

2

Figure 16. Nios SDK Shell with .srec Downloaded

5. Press Ctrl + C to exit terminal mode.

1 Due to compiler optimizations, the number of cycles required may change and not agree with Figure 16.

Congratulations, you have just completed Custom Instruction Example 1.

Altera Corporation 25

Page 26: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter
Page 27: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Altera Corporation

Example 2: Binary toBCD Converter

Example 2

3

Introduction This tutorial section guides you through the steps to implement multiple custom instructions. One of these custom instructions takes advantage of the user-defined port option. The base system used in this example is the standard 32-bit verilog HDL reference design, located in the directory c:\altera\excalibur\sopc_builder_2_5\examples\verilog\standard_32

In Example 2, the custom instruction modules you will add are called binary_to_bcd and display. These modules are defined in the design files named binary_to_bcd.v and display.v. binary_to_bcd converts a unsigned int (less than 100) to binary coded decimal (BCD). display takes the eight least significant bits (LSBs) of an unsigned int and shows the hexadecimal value on the dual-digit seven segment LED display of your Nios development board.

Open the Quartus Project

To start the Quartus II software, follow these steps:

1. Choose Programs > Altera> Quartus II <version> (Windows Start menu) to start the Quartus II software.

2. Choose Open Project (File menu) to open the project.

Specify the working directory for your project. This tutorial uses the following default directory:

c:\altera\excalibur\sopc_builder\tutorials\\CI_Tutorial\ Cust_Inst_Example2.

3. Select standard_32.quartus and click Open. The block diagram file standard_32.bdf appears. See Figure 17.

27

Page 28: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 2: Binary to BCD Converter Nios Custom Instructions Tutorial

Figure 17. standard_32.bdf

Open the Nios System Module

To open the Nios system module ref_32_system:

v Double-click on the ref_32_system symbol in standard_32.bdf. The SOPC Builder System Contents Page for the Nios ref_32_system appears. See Figure 18.

28 Altera Corporation

Page 29: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 2: Binary to BCD Converter

Example 2

3

Figure 18. SOPC Builder System Contents Page

Add Custom Instructions to the Nios CPU

To add custom instructions, perform the following steps:

1. Edit the Nios CPU in the SOPC Builder using one of the following methods:

Right–click cpu and choose Edit in the pop-up menu or double-click cpu. The Nios configuration wizard displays.

2. Turn on the Enable advanced configuration controls option in the Architecture tab of the Nios configuration wizard. See Figure 19.

Altera Corporation 29

Page 30: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 2: Binary to BCD Converter Nios Custom Instructions Tutorial

Figure 19. Nios Configuration Wizard

3. Click the Custom Instructions tab. See Figure 20. SOPC Builder highlights the selected row. Make sure that the USR0 opcode row is the default selection.

Figure 20. Custom Instructions Tab

4. Click Import.

30 Altera Corporation

Page 31: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 2: Binary to BCD Converter

Example 2

3

The Interface to User Logic appears. See Figure 21. The Interface to User Logic associates the necessary design files to each custom instruction opcode. It then scans all ports defined in the top-level module for the custom instruction of the system for all required ports and automatically connects them to the Nios ALU.

f See the SOPC Builder Data Sheet for more system module information.

Figure 21. Interface to User Logic

Add the binary_to_bcd.v Custom Instruction File

1. Click Add.

2. Select the Custom_instructions folder.

3. Click binary_to_bcd.v.

4. Click Open.

Altera Corporation 31

Page 32: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 2: Binary to BCD Converter Nios Custom Instructions Tutorial

The Interface to User Logic automatically enters the top-level module name binary_to_bcd, which is the correct top-level module name for this example. The Interface to User Logic inserts the module name of the first file opened as the top-level module.

5. Click Populate Port Table.

The Interface to User Logic scans the top-level module for any ports. Any ports matching names defined for custom instructions will be connected to the Nios ALU automatically. Any other ports will be routed to the top of the Nios system module and labeled as export.

6. Click Add to System. You return to the Custom Instructions tab. See Figure 22.

7. Add the macro name and the required number of CPU clock cycles.

Figure 22. Custom Instructions Tab with USR0 Name

The Name cell entry for USR0 is bina. By default, the Name cell contains the first four characters of the top-level module name. This macro name is used to access your custom instruction in software.

8. Enter 3 r in the Cycle Count cell. Three is the number of CPU clock cycles needed for the binary_to_bcd custom logic block.

Add the display.v Custom Instruction File

1. To add the next custom instruction, select USR1. USR1 is highlighted.

bina

32 Altera Corporation

Page 33: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 2: Binary to BCD Converter

Example 2

3

2. Click Import.

3. Click Add.

4. Browse to the Custom_instructions folder. Select and open display.v.

5. Click Populate Port Table.

The user-defined ports seven_seg are labeled as export. During generation, these ports are routed to the top-level of the Nios system module. The custom instruction module can access logic outside the Nios system module through these ports. See Figure 23.

Figure 23. Scan Files

6. Click Add to System. You are returned to the Custom Instructions tab.

The Name cell entry for USR1 is disp. You will use the default as the macro name. See Figure 24.

Altera Corporation 33

Page 34: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 2: Binary to BCD Converter Nios Custom Instructions Tutorial

Figure 24. Custom Instructions Tab with USR1 Name

Leave the default value of 1 for the Cycle Count. The warning message “Custom instruction disp is sequential logic, but is set to take 1 cycle” appears. You can ignore this message and accept the default Cycle Count value of 1. The default value is used because:

– display requires the multi-cycle mode because it uses the clock signal to register the output ports seven_seg. The output port provides the inputs to the dual seven segment display

– Only one clock cycle is needed because display always returns zero to the CPU.

7. Click Finish. You are returned to the SOPC Builder.

You have instantiated binary_to_bcd and display as custom instructions in the Nios CPU. Next you generate the Nios system module and compile the design in the Quartus II software.

Generate the Nios System Module

To generate the Nios system module, perform the following steps:

1. Click the System Generation tab in SOPC Builder.

2. Click Generate.

This re-creates the Nios CPU with the custom instructions added, and creates the C/C++ and assembly macros for the custom instructions in excalibur.h and excalibur.s, respectively.

disp

34 Altera Corporation

Page 35: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 2: Binary to BCD Converter

Example 2

3

3. After system generation is complete, click Exit to return to the Quartus II software. See Figure 25.

Figure 25. System Generation Completed Message

1 The block diagram file for the standard_32 project was modified for this tutorial. Note that the export ports from the display custom instruction (seven_seg_from_the_USR1_cpu[15..0]) connects to the pins that drive the seven segment LEDs on the Nios development board.

Compile & Download the Files to the Board

Compile the Quartus II Project

In this section you will compile your Quartus II project and then download the design files to the Nios development board.

To compile the Quartus II project, perform the following steps:

1. Choose Start Compilation (Processing menu).

2. After the compilation is successful, Click OK as shown in Figure 26.

system generation completed message

Altera Corporation 35

Page 36: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 2: Binary to BCD Converter Nios Custom Instructions Tutorial

Figure 26. Compilation Complete Message

1 You must properly connect and set up the ByteBlasterMV cable to transmit configuration data over the JTAG port, download the programming file to the APEX device on the Nios development board. See the Nios Embedded Processor Development Board Data Sheet for more information.

3. Choose Open. (File menu).

4. In the Files of type box, select Programming Files (*.cdf, *.sof, *pof).

5. Select and open standard_32.cdf. The Quartus II Programmer window with the chain description file, standard_32.cdf, appears.

The standard_32.cdf includes the programming file standard_32.sof, JTAG as the programming mode, and ByteBlasterMV as the programming hardware type.

1 Verify that the programming hardware type is ByteBlasterMV and the programming mode is JTAG.

Figure 27. JTAG Programming Window

Programming Hardware Type - ByteBlasterMVProgramming Mode - JTAG

36 Altera Corporation

Page 37: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Nios Custom Instructions Tutorial Example 2: Binary to BCD Converter

Example 2

3

6. Click Start to begin downloading the hardware image to the development board.

Create & Download Executable Files to the Nios System

Altera provides a reference software design in the SDK directory for this tutorial example. These software files use the custom instruction hardware now included in the Nios CPU.

To build and download the software from the Nios SDK Shell, follow these steps:

1. Choose Programs > Altera> Nios [version] > Nios SDK Shell (Windows Start menu) to open the Nios SDK Shell.

2. To change to the Cust_Inst_Example2\cpu_sdk\src directory, type the following command at the Nios SDK Shell prompt:

cd ../tutorials/CI_Tutorial/Cust_Inst_Example2/cpu_sdk/src r

To build the software:

1. To build code that does not use the binary_to_bcd custom instruction, type the following command:

nios-build ci_disp_only.c r

2. To build code that uses the binary_to_bcd custom instruction, type the following command:

nios-build ci_hello_nios.c r

3. After the build is finished, to download the executable .srec files to the Nios development board, type the following command:

nios-run ci_disp_only.srec r

Altera Corporation 37

Page 38: Nios Custom Instructions -  · PDF fileNios Custom Instructions Tutorial About this Document ... such as a Report File, ... Binary to BCD Converter

Example 2: Binary to BCD Converter Nios Custom Instructions Tutorial

Figure 28. Downloaded srec

See Figure 28. The program ci_disp_only performs a Hexadecimal-to-BCD conversion inside a loop for 150,000 iterations. This action slows down execution time for every number that is displayed on the seven-segment LEDs.

4. To stop execution, press SW4 and SW3 (Clear) simultaneously.

5. Type Ctrl+C to exit from nios-run terminal mode.

ci_hello_nios.srec uses the binary_to_bcd custom instruction. It still performs the conversion 150,000 times for each number. However, since the conversion is done in hardware, performance is accelerated.

nios-run ci_hello_nios.srec r

You should see a speed improvement of nearly a factor of 10 for this simple example. When you convert more complex software algorithms to custom instructions, even greater speed improvements can be achieved.

Congratulations, you have just completed Custom Instruction Example 2 and the Custom Instructions Tutorial.

38 Altera Corporation