Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
1. Nios II Custom Instruction Overview
Nios II Embedded Processor
+
-
&
<<
>>
Result
A
Nios II
ALU
B
Custom
Logic
When you design a system that includes an Altera Nios II embedded processor, you
can accelerate time-critical software algorithms by adding custom instructions to the
Nios II processor instruction set. Custom instructions allow you to reduce a complex
sequence of standard instructions to a single instruction implemented in hardware.
You can use this feature for a variety of applications, for example, to optimize
software inner loops for digital signal processing (DSP), packet header processing,
and computation-intensive applications. In SOPC Builder, the Nios II parameter
editor provides a GUI to add custom instructions to the Nios II processor. In Qsys,
each custom instruction is a separate component in the Qsys system. You can add as
many as 256 custom instructions to your system.
In SOPC Builder, the custom instruction logic connects directly to the Nios II
arithmetic logic unit (ALU) as shown in Figure 1–1.
Figure 1–1. Custom Instruction Logic Connects to the Nios II ALU in SOPC Builder Systems
This chapter contains the following sections:
■ “Custom Instruction Overview”
■ “Custom Instruction Types” on page 1–3
For information about the custom instruction software interface, refer to Chapter 2,
January 2011 Altera CorporationNios II Custom Instruction User Guide
Software Interface. For step-by-step instructions for implementing a custom
instruction, refer to Chapter 3, Implementing a Nios II Custom Instruction in SOPC
Builder or Chapter 4, Implementing a Nios II Custom Instruction in Qsys.
1–2Chapter 1: Nios II Custom Instruction Overview
Combinatorial
Conduit interface to external
memory, FIFO, or other logic
Multi-cycle
result
Extended
Internal
Register File
[31..0]
done
dataa[31..0]
datab[31..0]
clk
clk_en
reset
start
n[7..0]
a[4..0]
readra
b[4..0]
readrb
c[4..0]
writerc
Combinational
Custom
Logic
Custom Instruction Overview
Custom Instruction Overview
Nios II custom instructions are custom logic blocks adjacent to the ALU in the
processor’s datapath. Custom instructions give you the ability to tailor the Nios II
processor core to meet the needs of a particular application. You can accelerate time
critical software algorithms by converting them to custom hardware logic blocks.
Because it is easy to alter the design of the FPGA-based Nios II processor, custom
instructions provide an easy way to experiment with hardware-software tradeoffs at
any point in the design process.
Implementing Custom Instruction Hardware
Figure 1–2 is a hardware block diagram of a Nios II custom instruction.
Figure 1–2. Hardware Block Diagram of a Nios II Custom Instruction
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
A Nios II custom instruction logic receives input on its
datab
ports, and drives out the result on its
provides a result based on the inputs provided by the Nios II processor.
The Nios II processor supports different types of custom instructions. Figure 1–2 lists
the additional ports that accommodate different custom instruction types. Only the
ports used for the specific custom instruction implementation are required.
Figure 1–2 also shows a conduit interface to external logic. The interface to external
logic allows you to include a custom interface to system resources outside of the
Nios II processor datapath.
result
dataa
port, or on its
dataa
and
port. The custom instruction logic
Chapter 1: Nios II Custom Instruction Overview1–3
Custom Instruction Types
Implementing Custom Instruction Software
The Nios II custom instruction software interface is simple and abstracts the details of
the custom instruction from the software developer. For each custom instruction, the
Nios II Embedded Design Suite (EDS) generates a macro in the system header file,
system.h. You can use the macro directly in your C or C++ application code, and you
do not need to program assembly code to access custom instructions. Software can
also invoke custom instructions in Nios II processor assembly language.
For more information about the custom instruction software interface, refer to
Chapter 2, Software Interface.
Custom Instruction Types
Different types of custom instructions are available to meet the requirements of your
application. The type you choose determines the hardware interface for your custom
instruction.
Tab le 1– 1 shows the available custom instruction types, applications, and associated
hardware ports.
Table 1–1. Custom Instruction Types, Applications, and Hardware Ports (Part 1 of 2)
Multi-clock cycle custom logic blocks of fixed or
variable durations.
Custom logic blocks that are capable of
performing multiple operations
■
datab[31:0]
■
result[31:0]
■
dataa[31:0]
■
datab[31:0]
■
result[31:0]
■
clk
■
clk_en (1)
■
start
■
reset
■
done
■
dataa[31:0]
■
datab[31:0]
■
result[31:0]
■
clk
■
clk_en (1)
■
start
■
reset
■
done
■
n[7:0]
January 2011 Altera CorporationNios II Custom Instruction User Guide
1–4Chapter 1: Nios II Custom Instruction Overview
Custom Instruction Types
Table 1–1. Custom Instruction Types, Applications, and Hardware Ports (Part 2 of 2)
TypeApplicationHardware Ports
■
dataa[31:0]
■
datab[31:0]
■
result[31:0]
■
clk
■
clk_en
■
start
■
Internal register file
External interface
Note to Table 1–1:
(1) The
clk_en
needs to stall the custom instruction during execution.
input signal must be connected to the
Custom logic blocks that access internal register
files for input or output or both.
Custom logic blocks that interface to logic
outside of the Nios II processor’s datapath
clk_en
signals of all the registers in the custom instruction, in case the Nios II processor
reset
■
done
■
n[7:0]
■
a[4:0]
■
readra
■
b[4:0]
■
readrb
■
c[4:0]
■
writerc
Standard custom instruction ports, plus
user-defined interface to external logic.
The following sections discuss the basic functionality and hardware interface of each
of the custom instruction types listed in Table 1–1.
Combinational Custom Instructions
A combinational custom instruction is a logic block that completes its logic function in
a single clock cycle. Figure 1–3 shows a block diagram of a combinational custom
instruction.
The Nios II processor issues a combinational custom instruction speculatively; that is,
it optimizes execution by issuing the instruction before knowing whether it is
necessary, and ignores the result if it is not required. Therefore, a combinational
custom instruction must not have side effects. In particular, a combinational custom
instruction cannot have an external interface.
You can further optimize combinational custom instructions by implementing the
extended custom instruction. Refer to “Extended Custom Instructions” on page 1–7.
Multicycle Custom Instructions
Multicycle or sequential, custom instructions consist of a logic block that requires two
or more clock cycles to complete an operation. Additional control ports are required
for multicycle custom instructions, as shown in Table 1–3.
January 2011 Altera CorporationNios II Custom Instruction User Guide
1–6Chapter 1: Nios II Custom Instruction Overview
Custom Instruction Types
Figure 1–5 shows the multicycle custom instruction block diagram.
Multicycle custom instructions complete in either a fixed or variable number of clock
cycles. For a custom instruction that completes in a fixed number of clock cycles, you
specify the required number of clock cycles at system generation. For a custom
instruction that requires a variable number of clock cycles, you instantiate the
and
done
ports. These ports participate in a handshaking scheme to determine when
the custom instruction execution is complete.
Tab le 1– 3 describes the multicycle custom instruction ports.
Table 1–3. Multicycle Custom Instruction Ports
Port NameDirectionRequiredDescription
clk
clk_en
reset
start
done
dataa[31:0]
datab[31:0]
result[31:0]
InputYes System clock
InputYesClock enable
InputYesSynchronous reset
InputNoCommands custom instruction logic to start execution
OutputNo
Custom instruction logic indicates to the processor that execution is
complete
InputNoInput operand to custom instruction
InputNoInput operand to custom instruction
OutputNoResult of custom instruction
result[31..0]
done
start
As indicated in Tab le 1– 3, the
custom instructions. However, the
clk, clk_en
, and
reset
start, done, dataa, datab
ports are required for multicycle
, and
result
ports are
optional. Implement them only if the custom instruction functionality requires them.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 1: Nios II Custom Instruction Overview1–7
clk
dataa[]
datab[]
result[]
valid
valid
T0T1T3T2T4T5T6
valid
done
clk_en
start
reset
Custom Instruction Types
Figure 1–6 shows the multicycle custom instruction hardware port timing diagram.
The processor asserts the active high
instruction execution. At this time, the
remain valid throughout the duration of the custom instruction execution. The
port on the first clock cycle of the custom
dataa
and
datab
ports have valid values and
start
signal is asserted for a single clock cycle.
For a fixed length multicycle custom instruction, after the instruction starts, the
processor waits the specified number of clock cycles, and then reads the value on the
result
data on the n
signal. For an n-cycle operation, the custom logic block must present valid
th
rising edge after the custom instruction begins execution.
For a variable length multicycle custom instruction, the processor waits until the
active high
clock edge on which
result
done
signal is asserted. The processor reads the
done
is asserted. The custom logic block must present data on the
port on the same clock cycle on which it asserts the
The Nios II system clock feeds the custom logic block’s
system’s master reset feeds the active high
when the whole Nios II system is reset.
The custom logic block must treat the active high
qualifier signal, ignoring
clk
You can further optimize multicycle custom instructions by implementing the
extended internal register file, or by creating external interface custom instructions.
Refer to “Extended Custom Instructions”, “Internal Register File Custom
Instructions” on page 1–9, or “External Interface Custom Instructions” on page 1–10.
Extended Custom Instructions
Extended custom instruction allows a single custom logic block to implement several
different operations. Extended custom instructions use an index to specify which
operation the logic block performs. The index can be as many as eight bits wide,
allowing a single custom logic block to implement as many as 256 different
operations.
while
clk_en
reset
port. The
clk_en
is deasserted.
result
done
clk
port, and the Nios II
reset
port on the same
signal.
port is asserted only
port as a conventional clock
January 2011 Altera CorporationNios II Custom Instruction User Guide
1–8Chapter 1: Nios II Custom Instruction Overview
Custom Instruction Types
Figure 1–7 is a block diagram of an extended custom instruction with bit-swap,
byte-swap, and half-word swap operations.
Figure 1–7. Extended Custom Instruction with Swap Operations
Custom
dataa[31..0]
n[1..0]
bit-swap
operation
byte-swap
operation
half-word-swap
operation
Instruction
0
1
2
result[31..0]
The custom instruction in Figure 1–7 performs swap operations on data received at
the
dataa
port. It uses the two-bit-wide n port to select the output from a multiplexer,
determining which result is presented to the
result
port.
1This logic is just a simple example, using a multiplexer on the output. You can
implement function selection based on an index in any way that is appropriate for
your application.
Extended custom instructions can be combinational or multicycle custom
instructions. To implement an extended custom instruction, simply add an n port to
your custom instruction logic. The bit width of the
n
port is a function of the number
of operations the custom logic block can perform.
Extended custom instructions occupy multiple custom instruction indices. For
example, the custom instruction illustrated in Figure 1–7 occupies 4 indices, because
is two bits wide. Therefore, when this instruction is implemented in a Nios II system,
256 - 4 = 252 available indices remain.
For information about the custom instruction index, refer to “Custom Instruction
Assembly Software Interface” on page 2–3.
All extended custom instruction port operations are identical to those for the
combinational and multicycle custom instructions, with the exception of the
n
which is not present in combinational and multicycle custom instructions. The
timing is the same as that of the
multicycle custom instruction, the processor presents the index value to the
the same rising edge of the clock at which
dataa
port. For example, for an extended variable
start
is asserted, and the n port remains
n
stable during execution of the custom instruction.
port,
n
port
port on
n
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 1: Nios II Custom Instruction Overview1–9
dataa[31..0]
datab[31..0]
writerc
result[31..0]
Multiplier
Adder
DQ
CLR
Custom Instruction Types
Internal Register File Custom Instructions
The Nios II processor allows custom instruction logic to access its own internal
register file. This provides you the flexibility to specify if the custom instruction reads
its operands from the Nios II processor’s register file or from the custom instruction’s
own internal register file. In addition, a custom instruction can write its results to the
local register file rather than to the Nios II processor’s register file.
Custom instructions containing internal register files use
signals to determine if the custom instruction should use the internal register file or
the
dataa, datab
, and
result
signals. Ports a, b, and c specify the internal registers
from which to read or to which to write. For example, if
(specifying a read operation from the internal register), the
index to the internal register file. Ports
a, b
, and c are five bits each, allowing you to
address as many as 32 registers.
f For further details of Nios II custom instruction implementation, refer to the
Instruction Set Reference chapter of the Nios II Processor Reference Handbook.
Tab le 1– 4 lists the internal register file custom instruction-specific optional ports. Use
the optional ports only if the custom instruction functionality requires them.
January 2011 Altera CorporationNios II Custom Instruction User Guide
1–10Chapter 1: Nios II Custom Instruction Overview
When
writerc
result
port. The accumulated value is stored in an internal register. Alternatively, the
processor can read the value on the
is deasserted, the Nios II processor ignores the value driven on the
result
port by asserting
Custom Instruction Types
writerc
. At the same
time, the internal register is cleared so that it is ready for a new round of multiply and
accumulate operations.
The
readra, readrb, writerc, a, b
custom instruction begins, the processor presents the new values of the
readrb, writerc, a, b
, and c ports on the rising edge of the processor clock. All six of
, and c ports behave similarly to
dataa
. When the
readra
,
these ports remain stable during execution of the custom instructions.
To determine how to handle the register file, custom instruction logic reads the active
high
readra, readrb
file indexes. When
the corresponding
writerc
result
is asserted, the custom instruction logic ignores the c port and writes to the
port.
, and
writerc
readra
a
or
or b port, and receives data from the
ports. The logic uses the a, b, and c ports as register
readrb
is asserted, the custom instruction logic ignores
dataa
or
datab
port. When
All other custom instruction port operations behave the same as for combinational
and multicycle custom instructions.
External Interface Custom Instructions
Nios II custom instructions allow you to add an interface to communicate with logic
outside of the processor’s datapath. At system generation, conduits propagate out to
the top level of the SOPC Builder or Qsys system, where external logic can access the
signals.
By enabling custom instruction logic to access memory external to the processor,
external interface custom instructions extend the capabilities of the custom instruction
logic.
Figure 1–9 shows a multicycle custom instruction that has an external memory
interface.
Figure 1–9. Custom Instructions Allow the Addition of an External Interface
dataa[31..0]
datab[31..0]
clk
clk_en
reset
start
Conduit Interface
result[31..0]
done
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 1: Nios II Custom Instruction Overview1–11
Custom Instruction Types
Custom instruction logic can perform various tasks such as storing intermediate
results or reading memory to control the custom instruction operation. The conduit
interface also provides a dedicated path for data to flow into or out of the processor.
For example, custom instruction logic with an external interface can feed data directly
from the processor’s register file to an external first-in first-out (FIFO) memory buffer.
January 2011 Altera CorporationNios II Custom Instruction User Guide
1–12Chapter 1: Nios II Custom Instruction Overview
Custom Instruction Types
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
The Nios II custom instruction software interface abstracts logic implementation
details from the application code. During the build process the Nios II software build
tools generate macros that allow easy access from application code to custom
instructions.
This chapter contains the following sections:
■ “Custom Instruction Examples” on page 2–1
■ “Built-in Functions and User-defined Macros” on page 2–2
■ “Custom Instruction Assembly Software Interface” on page 2–3
Custom Instruction Examples
Example 2–1 shows a portion of the system.h header file that defines a macro for a
bit-swap custom instruction. This bit-swap example accepts one 32-bit input and
performs only one function.
Example 2–2 illustrates application code that uses the bit-swap custom instruction.
Example 2–2. Bit Swap Instruction Usage
#include "system.h"
int main (void)
{
int a = 0x12345678;
int a_swap = 0;
a_swap = ALT_CI_BITSWAP(a);
return 0;
}
The code in Example 2–2 includes the system.h file to enable the application software
to use the custom instruction macro definition. The example code declares two
integers,
and the results are loaded in
January 2011 Altera CorporationNios II Custom Instruction User Guide
a
and
a_swap
. Integer a is passed as input to the bit swap custom instruction
a_swap
.
2–2Chapter 2: Software Interface
Built-in Functions and User-defined Macros
Example 2–2 illustrates how most applications use custom instructions. The macros
defined by the Nios II software build tools use C integer types only. Occasionally,
applications require input types other than integers. In those cases, you can use a
custom instruction macro to process non-integer return values.
1You can define custom macros for Nios II custom instructions that allow other 32-bit
input types to interface with custom instructions.
Built-in Functions and User-defined Macros
The Nios II processor uses gcc built-in functions to map to custom instructions. By
default, the integer type custom instruction is defined in a system.h file. However, by
using built-in functions, software can use non-integer types with custom instructions.
Fifty-two built-in functions are available to accommodate the different combinations
of supported types.
Built-in function names have the following format:
__builtin_custom_
<return type>n<parameter types>
Tab le 2– 1 lists the 32-bit types supported by custom instructions as parameters and
return types, as well as the abbreviations used in the built-in function names.
Table 2–1. 32-bit Types Supported by Custom Instructions
32-bit TypeBuilt-in Function Abbreviation
inti
floatf
void *p
Example 2–3 shows the prototype definitions for two built-in functions.
Example 2–3. Two Example Built-in Function Prototypes
void __builtin_custom_nf (int n, float dataa);
float __builtin_custom_fnp (int n, void * dataa);
The built-in function
return a value. In contrast, the built-in function
as input, and returns a
__builtin_custom_nf
float
.
takes a
float
as an input, and does not
__builtin_custom_fnp
takes a pointer
To support non-integer input types, define macros with mnemonic names that map to
the specific built-in function required for the application.
f Refer to Appendix B, Custom Instruction Built-in Functions for detailed information
and a list of built-in functions.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 2: Software Interface2–3
Custom Instruction Assembly Software Interface
Example 2–4 shows user-defined custom instruction macros used in an application.
Example 2–4. Custom Instruction Macro Usage Example
On lines 2 through 6, the user-defined macros are declared and mapped to the
appropriate built-in functions. The macro UDEF_MACRO1 takes a float as an input
parameter and does not return anything. The macro UDEF_MACRO2 takes a pointer as
an input parameter and returns a float. Lines 14 and 15 show code that uses the two
user-defined macros.
Custom Instruction Assembly Software Interface
The Nios II custom instructions are also accessible in assembly code. This section
describes the assembly interface.
Custom instructions are R-type instructions, containing:
■ A 6-bit opcode
■ Three 5-bit register index fields
■ Three 1-bit fields for the
■ An 8-bit
N
field, used for the custom instruction index (opcode extension), and
optionally including a function select subfield
readra, readrb
, and
writerc
signals
January 2011 Altera CorporationNios II Custom Instruction User Guide
A = Register index of operand A
B = Register index of operand B
C = Register index of operand C
N = 8-bit number that selects instruction
readra = 1 i f instruction uses processor’s register rA, 0 otherwise
readrb = 1 if instruction uses processor’s register rB, 0 otherwise
writerc = 1 i f instruction provides result for processor’s register rC, 0 otherwise
Instruction Fields:
Custom Instruction Assembly Software Interface
Figure 2–1 shows a diagram of the custom instruction word.
Figure 2–1. Custom Instruction Format
Bits 5–0 are the Nios II custom instruction opcode. The opcode for a custom
instruction is 0x32.
f A list of opcodes appears in the “Instruction Opcodes” section in the Instruction Set
Reference chapter of the Nios II Processor Reference Handbook.
The
N
field, bits 13:6, is the custom instruction index. The custom instruction index
distinguishes between different custom instructions, allowing the Nios II processor to
support as many as 256 distinct custom instructions. Depending on the type of
custom instruction, the
■ A unique custom instruction index, for logic that implements a single custom
N
field has one of the following functions:
function
■ An extended custom instruction index, for logic that implements several custom
functions
Example 2–5 shows the assembly language syntax for the custom instruction.
Example 2–5. Custom Instruction Assembly Syntax
custom N, xC, xA, xB
In the assembly code instruction in Example 2–5,
is the destination for the
result[31:0]
port, xA is the
port. To access the Nios II processor’s register file, replace
register file, replace
instruction has the
x
with c. The use of r or c determines whether the custom
readra, readrb
, and
writerc
N
is the custom instruction index, xC
dataa
port, and xB is the
x
with r. To access a custom
bits held high or low. Refer to
Figure 2–1 for the location of these three bits in the custom instruction low-level
format.
Example 2–6, Example 2–7, and Example 2–8 demonstrate the syntax for custom
instruction assembler calls.
Example 2–6. Assembly Language Call to Customer Instruction I
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
custom 0, r6, r7, r8
datab
Chapter 2: Software Interface2–5
Custom Instruction Assembly Software Interface
Example 2–6 shows a call to a custom instruction with index 0. The input to the
instruction is the current contents of the Nios II processor registers
results are stored in the Nios II processor register
Example 2–7. Assembly Language Call to Customer Instruction II
custom 3, c1, r2, c4
r6
.
r7
and r8, and the
Example 2–7 shows a call to a custom instruction with index 3. The input to the
instruction is the current contents of the Nios II processor register
register
Example 2–8. Assembly Language Call to Customer Instruction III
custom 4, r6, c9, r2
c4
, and the results are stored in custom register c1.
r2
and the custom
Example 2–8 shows a call to a custom instruction with index 4. The input to the
instruction is the current contents of the custom register
register
r2
, and the results are stored in Nios II processor register r6.
c9
and the Nios II processor
f For more information about the binary format of custom instructions, refer to the
Instruction Set Reference chapter of the Nios II Processor Reference Handbook.
January 2011 Altera CorporationNios II Custom Instruction User Guide
2–6Chapter 2: Software Interface
Custom Instruction Assembly Software Interface
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
3. Implementing a Nios II Custom
This chapter describes the process of implementing a Nios II custom instruction with
the SOPC Builder component editor. The component editor enables you to create new
SOPC Builder components, including Nios II custom instructions. This chapter also
describes the process of accessing Nios II custom instructions from software.
f For detailed information about the SOPC Builder component editor, refer to the
“Component Editor” chapter of theSOPC Builder User Guide.
Design Example: Cyclic Redundancy Check
The cyclic redundancy check (CRC) algorithm detects the corruption of data during
transmission. It detects a higher percentage of errors than a simple checksum. The
CRC calculation consists of an iterative algorithm involving XOR and shift operations.
These operations are carried out concurrently in hardware and iteratively in software.
Because the operations are carried out concurrently, the execution is much faster in
hardware.
Instruction in SOPC Builder
The CRC design files demonstrate the steps to implement an extended multicycle
Nios II custom instruction. These design files are available for you to download from
the Nios II Custom Instruction Design Example web page.
Implementing Custom Instruction Hardware in SOPC Builder
This section describes the custom instruction tool-flow, and walks you through the
process of implementing a Nios II custom instruction. Implementing a Nios II custom
instruction hardware entails the following tasks:
■ “Opening the Component Editor”
■ “Adding the HDL Files”
■ “Configuring the Custom Instruction Signal Type” on page 3–3
■ “Setting Up the Custom Instruction Interfaces” on page 3–3
■ “Setting the Details” on page 3–4
■ “Saving and Adding the Custom Instruction” on page 3–5
■ “Generating the System and Compiling in the Quartus II Software” on page 3–5
The following sections detail the steps required to set up the design example
environment and to perform the list of tasks.
January 2011 Altera CorporationNios II Custom Instruction User Guide
3–2Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder
Implementing Custom Instruction Hardware in SOPC Builder
Setting up the Design Environment for the Design Example
Before you start the design example, you must set up the design environment to
accommodate the processes described in the following sections. To set up the design
example environment, follow these steps:
1. Open the ug_custom_instruction_files.zip file from the Nios II Custom
Instruction Design Example web page and extract all the files to a new directory.
2. Follow the instructions in the Quartus II Project Setup section in the
readme_sopcb.txt file in the extracted design files. The instructions direct you to
determine a <project_dir> working directory for the project, to download an
appropriate Quartus II project for your development board, and to open the
design example project in the Quartus II software.
Opening the Component Editor
After you follow the steps in the Quartus II Project Setup section of the
readme_sopcb.txt file, you have an Quartus II project located in the <project_dir>
directory and open in the Quartus II software.
To open the component editor, follow these steps:
1. To open the SOPC Builder system, on the Tools menu, click SOPC Builder.
2. On the SOPC Builder System Contents tab, double-click cpu. The Nios II
Processor parameter editor appears.
3. On the Parameter Settings page, on the Custom Instructions tab, click Import.
The component editor appears, displaying the Introduction tab.
Adding the HDL Files
To specify the synthesis HDL files for your custom instruction, follow these steps:
1. Click Next to display the HDL Files tab.
2. Click Add.
3. Browse to <project_dir>/crc_hw, the location of the HDL files for this design
example.
4. Select the CRC_Custom_Instruction.v and CRC_Component.v files and click
Open.
1The Quartus II Analysis and Synthesis program checks the design for errors
when you add the files. Confirm that no error message appears.
5. Ensure the Synth setting is turned on for both files. This setting directs the
component editor to generate models for synthesis.
6. Turn on the To p parameter for the CRC_Custom_Instruction.v file, to indicate it is
the top-level HDL file for this custom instruction.
1The Quartus II Analysis and Synthesis program checks the design for errors
when you change the To p setting. Confirm that no error message appears.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder3–3
Implementing Custom Instruction Hardware in SOPC Builder
7. Click Top Level Module and select the name of the top-level module of your
custom instruction logic. The top-level module for this design example is
CRC_Custom_Instruction.
8. To enable simulating the system in the ModelSim simulator, you can turn on Sim
for both files. In cases other than the design example, you may need to add
additional simulation files in the component editor HDL Files tab, turn on Sim
and turn off Synth for these simulation-only files, and turn off Sim for the
synthesis-only files.
Configuring the Custom Instruction Signal Type
To configure the custom instruction signal type, follow these steps:
1. Click Next to display the Signals tab.
2. For each port listed in the tab, follow these steps:
a. Select the port.
b. In the Interface column, select the name of the interface to which you want to
assign the port. For the design example, select
nios_custom_instruction_slave_0.
c. In the Signal Type column, select the appropriate signal type for the port. The
available types are described in “Custom Instruction Types” on page 1–3.
Setting Up the Custom Instruction Interfaces
To set up the custom instruction interfaces, follow these steps:
1. Click Next to display the Interfaces tab.
2. For Typ e, select Custom Instruction Slave.
3. For the design example, maintain the interface name
nios_custom_instruction_slave_0.
1If you rename an interface by changing the value in the Name field, the
Signals tab Interface column value changes automatically. The value
shown in the block diagram updates when you change tabs and return to
the Interfaces tab.
4. For Clock Cycles, type 0 for a variable multicycle type custom instruction, and
otherwise type the number of clock cycles your custom instruction logic requires.
The design example builds a variable multicycle type custom instruction.
1If the interface includes a
editor automatically infers that the interface is a variable multicycle type
custom instruction interface, and sets the value to
done
signal and a
clk
signal, the component
0
.
5. For Clock Cycle Type, type
instruction,
Combinatorial
January 2011 Altera CorporationNios II Custom Instruction User Guide
Multicycle
for a combinational type custom instruction.
Variable
for a fixed multicycle type custom instruction, or
for a variable multicycle type custom
3–4Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder
Implementing Custom Instruction Hardware in SOPC Builder
1If the interface does not include a
clk
signal, the component editor
automatically infers that the interface is a combinational type interface. If
the interface includes a
clk
signal, the component editor automatically
infers that the interface is a multicycle interface. If the interface does not
include a
fixed multicycle type interface. If the interface includes a
done
signal, the component editor infers that the interface is a
done
signal, the
component editor infers that the interface is a variable multicycle type
interface.
6. For Operands, type the number of operands for your custom instruction. For the
design example, type
1
.
7. If the warning message Interface has no signals appears, click Remove Interfaces With No Signals.
If your custom instruction logic requires additional interfaces, either to the
Avalon-MM fabric or outside the SOPC Builder system, you can specify the additional
interfaces in the Interfaces tab. The design example does not require additional
interfaces.
1Most custom instructions use some combination of standard custom instruction ports,
such as
dataa, datab
, and
result
, and do not require additional interfaces.
The following instructions provide the information you need if a custom instruction
in your own design requires additional interfaces. If you are walking through the
design example, proceed to “Setting the Details”.
To specify additional interfaces on the Interfaces tab, follow these steps:
1. Click Add Interface. The new interface has Custom Instruction Slave interface
type by default.
2. For Typ e, select the desired interface type.
3. Set the parameters for the newly created interface according to your system
requirements.
Setting the Details
To specify the final details in the custom instruction parameter editor, follow these
steps:
1. Click Next to display the HDL Parameters tab.
2. Click Next to display the Library Info tab.
3. For Name and for Display Name, type
4. For Ver s io n , type
5. Leave the Group field blank.
6. Optionally, set the Description, Created by, and Icon fields as you prefer.
7. If the bottom pane of the dialog box displays something other than the message
Info: No errors or warnings, review the other tabs to ensure you followed the
instructions correctly.
1.0
CRC
.
.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder3–5
Implementing Custom Instruction Hardware in SOPC Builder
Saving and Adding the Custom Instruction
To save the custom instruction and add it to your Nios II processor, follow these steps:
1. Click Finish. A dialog box prompts you to save your changes before exiting.
2. Click Yes, Save. In the Nios II processor parameter editor, on the Custom Instructions tab, the new custom instruction appears in the left panel.
3. If the new custom instruction does not appear on the Custom Instructions tab in
the left panel, follow these steps:
a. Click Finish to exit the Nios II processor parameter editor.
b. On the SOPC Builder System Contents tab, double-click cpu to reopen the
Nios II parameter editor.
4. In the left panel of the Custom Instructions tab, select CRC, the new custom
instruction you created in the design example.
5. Click Add to add the new instruction to the Nios II processor. Information about
the new instruction appears in the right panel, listing the name, Clock Cycles
value, N Port information, and Opcode Extension.
If the custom instruction is an extended type instruction, the N Port field specifies
the function select bits in the
N
port. Otherwise, the N Port field contains a dash.
The Opcode Extension field displays the custom instruction index (
instruction word in both binary and decimal formats. For more information about
the
N
field, refer to “Custom Instruction Assembly Software Interface” on
page 2–3.
6. Click Finish to complete the addition of the custom instruction to the SOPC
Builder system and return to SOPC Builder.
N
field) of the
Generating the System and Compiling in the Quartus II Software
After you add the custom instruction logic to the system, you can generate the system
and compile it in the Quartus II software. During system generation, SOPC Builder
connects the custom logic to the Nios II processor.
To generate the system and compile, follow these steps:
1. In SOPC Builder, on the System Generation tab, turn on Simulation. Create project simulator files if you want to simulate your system.
2. Click Generate. System generation may take several seconds to complete.
3. After system generation completes, click Exit.
4. In the Quartus II software, on the Processing menu, click Start Compilation.
f For detailed information about SOPC Builder systems and their generation, refer to
theSOPC Builder User Guide or the SOPC Builder Help system.
January 2011 Altera CorporationNios II Custom Instruction User Guide
3–6Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder
Accessing the Custom Instruction from Software
Accessing the Custom Instruction from Software
Adding a custom instruction to a Nios II processor results in a significant change to
the SOPC Builder system. In this section, you create and build a new software project
using the Nios II software build flow, and run the software that accesses the custom
instruction. The software source files are included in the downloadable design files.
Tab le 3– 1 lists the CRC application software source files and their corresponding
descriptions.
Table 3–1. CRC Application Software Source Files
File NameDescription
Main program that populates random test data, executes the CRC both in
crc_main.c
crc.cSoftware CRC algorithm run by the Nios II processor.
crc.hHeader file for crc.c.
ci_crc.cProgram that accesses CRC custom instruction.
ci_crc.hHeader file for ci_crc.c.
software and with the custom instruction, validates the output, and reports
the processing time.
To run the application software, you must create an Executable and Linking Format
File (.elf) first. To create the .elf file, follow the instructions in the Nios II Software
Build Flow section in the readme_sopcb.txt file in the extracted design files.
The application program runs three implementations of the CRC algorithm on the
same pseudo-random input data: an unoptimized software implementation, an
optimized software implementation, and the custom instruction CRC. The program
calculates the processing time and throughput for each of the versions, to demonstrate
the improved efficiency of a custom instruction compared to a software
implementation.
Example 3–1 shows the output from the application program run on a Nios II
Embedded Evaluation Kit, Cyclone III Edition (NEEK) using the Quartus II software
v10.1 and Nios II Embedded Design Suite (EDS) v10.1.
The output shows that the custom instruction CRC is 75 times faster than the
unoptimized CRC calculated purely in software and is almost 50 times faster than the
optimized version of the software CRC. The results you see using different target
device and board may vary depending on the memory characteristics of the board
and the clock speed of the device, but these ratios are representative.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder3–7
Accessing the Custom Instruction from Software
Example 3–1. Output of the CRC Design Example Software Run on a NEEK Using the Quartus II Software v10.1
******************************************************************************
Comparison between software and custom instruction CRC32
******************************************************************************
System specification
-------------------System clock speed = 62.5 MHz
Number of buffer locations = 16
Size of each buffer = 65535 bytes
Initializing all of the buffers with pseudo-random data
------------Custom instruction CRC vs software CRC = 75.1
Custom instruction CRC vs optimized software CRC = 48.9
Optimized software CRC vs software CRC = 1.5
January 2011 Altera CorporationNios II Custom Instruction User Guide
3–8Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder
Using the User-defined Custom Instruction Macro
Using the User-defined Custom Instruction Macro
The design example software uses a user-defined macro to access the CRC custom
instruction. Example 3–2 shows the macro that is defined in the ci_crc.c file.
Example 3–2. CRC Custom Instruction Macro Usage
#define CRC_CI_MACRO(n, A) __builtin_custom_ini(ALT_CI_CRC_INST_N + (n & 0x7), (A))
This macro takes a single
CRC custom instruction has extended type; the
int
type input operand and returns an
n
value in the macro
int
type value. The
CRC_CI_MACRO()
indicates the operation to be performed by the custom instruction. The custom
instruction index is added to the value of
n
. The n value is masked because the n port
of a custom instruction has only three bits.
To initialize the custom instruction, for example, you can add the initialization code in
Example 3–3 to your application software.
Example 3–3. Using the User-defined Macro to Initialize the Custom Instruction Logic
/* Initialize the custom instruction CRC to the initial remainder value: */
CRC_CI_MACRO (0,0);
For details of each operation of the CRC custom instruction and the corresponding
index value
n
, refer to the comments in the ci_crc.c file.
Example 3–2 and Example 3–3 demonstrate that you can define the macro in your
application to accommodate your requirements. For example, you can determine the
number and type of input operands, decide whether to assign a return value, and
vary the custom instruction index. However, the macro definition and usage must be
consistent with the port declarations of the custom instruction. For example, if you
define the macro to return an
int
value, the custom instruction must have a
port.
result
f For details about writing software for Nios II custom instructions, refer to Chapter 2,
Software Interface.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
4. Implementing a Nios II Custom
This chapter describes the process of implementing a Nios II custom instruction with
the Qsys component editor. The component editor enables you to create new Qsys
components, including Nios II custom instructions. This chapter also describes the
process of accessing Nios II custom instructions from software.
f For detailed information about the Qsys component editor, refer to Creating Qsys
Components in volume 1 of the Quartus II Handbook.
Design Example: Cyclic Redundancy Check
The cyclic redundancy check (CRC) algorithm detects the corruption of data during
transmission. It detects a higher percentage of errors than a simple checksum. The
CRC calculation consists of an iterative algorithm involving XOR and shift operations.
These operations are carried out concurrently in hardware and iteratively in software.
Because the operations are carried out concurrently, the execution is much faster in
hardware.
Instruction in Qsys
The CRC design files demonstrate the steps to implement an extended multicycle
Nios II custom instruction. These design files are available for you to download from
the Nios II Custom Instruction Design Example web page.
Implementing Custom Instruction Hardware in Qsys
This section describes the custom instruction tool-flow, and walks you through the
process of implementing a Nios II custom instruction. Implementing a Nios II custom
instruction hardware entails the following tasks:
■ “Opening the Component Editor”
■ “Adding the HDL Files”
■ “Configuring the Custom Instruction Signal Type” on page 4–3
■ “Setting Up the Custom Instruction Interfaces” on page 4–3
■ “Setting the Details” on page 4–4
■ “Saving and Adding the Custom Instruction” on page 4–5
■ “Generating the System and Compiling in the Quartus II Software” on page 4–5
The following sections detail the steps required to set up the design example
environment and to perform the list of tasks.
January 2011 Altera CorporationNios II Custom Instruction User Guide
4–2Chapter 4: Implementing a Nios II Custom Instruction in Qsys
Implementing Custom Instruction Hardware in Qsys
Setting up the Design Environment for the Design Example
Before you start the design example, you must set up the design environment to
accommodate the processes described in the following sections. To set up the design
example environment, follow these steps:
1. Open the ug_custom_instruction_files.zip file from the Nios II Custom
Instruction Design Example web page and extract all the files to a new directory.
2. Follow the instructions in the Quartus II Project Setup section in the
readme_qsys.txt file in the extracted design files. The instructions direct you to
determine a <project_dir> working directory for the project and to open the design
example project in the Quartus II software.
Opening the Component Editor
After you follow the steps in the Quartus II Project Setup section of the
readme_qsys.txt file, you have an Quartus II project located in the <project_dir>
directory and open in the Quartus II software.
To open the component editor, follow these steps:
1. To open Qsys, on the Tools menu, click Qsys.
2. In Qsys, on the File menu, click Open.
3. Browse to the <project_dir> directory if necessary, select the .qsys file, and click
Open.
4. On the Qsys Component Library tab, click New. The component editor appears,
displaying the Introduction tab.
Adding the HDL Files
To specify the synthesis HDL files for your custom instruction, follow these steps:
1. Click Next to display the HDL Files tab.
2. Click Add.
3. Browse to <project_dir>/crc_hw, the location of the HDL files for this design
example.
4. Select the CRC_Custom_Instruction.v and CRC_Component.v files and click
Open.
1The Quartus II Analysis and Synthesis program checks the design for errors
when you add the files. Confirm that no error message appears.
5. Ensure the Synth setting is turned on for both files. This setting directs the
component editor to generate models for synthesis..
6. Turn on the To p parameter for the CRC_Custom_Instruction.v file, to indicate it is
the top-level HDL file for this custom instruction.
1The Quartus II Analysis and Synthesis program checks the design for errors
when you change the To p setting. Confirm that no error message appears.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 4: Implementing a Nios II Custom Instruction in Qsys4–3
Implementing Custom Instruction Hardware in Qsys
7. Click Top Level Module and select the name of the top-level module of your
custom instruction logic. The top-level module for this design example is
CRC_Custom_Instruction.
8. To enable simulating the system in the ModelSim simulator, you can turn on Sim
for both files. In cases other than the design example, you may need to add
additional simulation files in the component editor HDL Files tab, turn on Sim
and turn off Synth for these simulation-only files, and turn off Sim for the
synthesis-only files.
Configuring the Custom Instruction Signal Type
To configure the custom instruction signal type, follow these steps:
1. Click Next to display the Signals tab.
2. For each port listed in the tab, follow these steps:
a. Select the port.
b. In the Interface column, select the name of the interface to which you want to
assign the port.
For the design example, for the first port you configure, select New Custom Instruction Slave. For the remaining ports, select the name created for the first
port, which is nios_custom_instruction_slave. These selections ensure that the
ports appear together on a single interface, using the name assumed by the
design example crc_hw files.
c. In the Signal Type column, select the appropriate signal type for the port. The
available signal types are described in “Custom Instruction Types” on
page 1–3. In the design example, the correct signal type for each signal has the
same name as the signal.
Setting Up the Custom Instruction Interfaces
To set up the custom instruction interfaces, follow these steps:
1. Click Next to display the Interfaces tab.
2. If the Remove Interfaces With No Signals button is active, click it.
3. Ensure that a single interface remains, with Name set to the name in the Signals
tab. For the design example, maintain the interface name
nios_custom_instruction_slave.
1If you rename an interface by changing the value in the Name field, the
Signals tab Interface column value changes automatically. The value
shown in the block diagram updates when you change tabs and return to
the Interfaces tab.
4. Ensure the Typ e for this interface is Custom Instruction Slave.
5. For Clock Cycles, type 0 for a variable multicycle type custom instruction, and
otherwise type the number of clock cycles your custom instruction logic requires.
The design example builds a variable multicycle type custom instruction.
January 2011 Altera CorporationNios II Custom Instruction User Guide
4–4Chapter 4: Implementing a Nios II Custom Instruction in Qsys
Implementing Custom Instruction Hardware in Qsys
1If the interface includes a
done
signal and a
clk
signal, the component
editor automatically infers that the interface is a variable multicycle type
custom instruction interface, and sets the value to
6. For Clock Cycle Type, type
instruction,
Combinatorial
Multicycle
for a fixed multicycle type custom instruction, or
for a combinational type custom instruction. Because the design
Variable
for a variable multicycle type custom
0
.
example builds a variable multicycle type custom instruction, Clock Cycle Type is
set to Va ri a bl e .
1If the interface does not include a
clk
signal, the component editor
automatically infers that the interface is a combinational type interface. If
the interface includes a
clk
signal, the component editor automatically
infers that the interface is a multicycle interface. If the interface does not
include a
fixed multicycle type interface. If the interface includes a
done
signal, the component editor infers that the interface is a
done
signal, the
component editor infers that the interface is a variable multicycle type
interface.
7. For Operands, type the number of operands for your custom instruction. For the
design example, type
1
.
If your custom instruction logic requires additional interfaces, either to the
Avalon-MM fabric or outside the Qsys system, you can specify the additional
interfaces in the Interfaces tab. The design example does not require additional
interfaces.
1Most custom instructions use some combination of standard custom instruction ports,
such as
dataa, datab
, and
result
, and do not require additional interfaces.
The following instructions provide the information you need if a custom instruction
in your own design requires additional interfaces. If you are walking through the
design example, proceed to “Setting the Details”.
To specify additional interfaces on the Interfaces tab, follow these steps:
1. Click Add Interface. The new interface has Custom Instruction Slave interface
type by default.
2. For Typ e, select the desired interface type.
3. Set the parameters for the newly created interface according to your system
requirements.
Setting the Details
To specify the final details in the custom instruction parameter editor, follow these
steps:
1. Click Next to display the HDL Parameters tab.
2. Click Next to display the Library Info tab.
3. For Name and for Display Name, type
CRC
.
4. For Ver s io n , type
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
1.0
.
Chapter 4: Implementing a Nios II Custom Instruction in Qsys4–5
Accessing the Custom Instruction from Software
5. Leave the Group field blank.
6. Optionally, set the Description, Created by, and Icon fields as you prefer.
7. If the bottom pane of the dialog box displays something other than the message
Info: No errors or warnings, review the other tabs to ensure you followed the
instructions correctly.
Saving and Adding the Custom Instruction
To save the custom instruction and add it to your Nios II processor, follow these steps:
1. Click Finish. A dialog box prompts you to save your changes before exiting.
2. Click Yes, Save. The new custom instruction appears in the Qsys Component
Library.
3. In the Qsys Component Library, under Library, select CRC, the new custom
instruction you created in the design example.
4. Click Add to add the new instruction to the Qsys system.
5. In the Connections panel, connect the new CRC_0 component’s
nios_custom_instruction_slave interface to the cpu component’s
custom_instruction_master interface.
Generating the System and Compiling in the Quartus II Software
After you add the custom instruction logic to the system, you can generate the Qsys
system and compile it in the Quartus II software.
To generate the system and compile, follow these steps:
1. In Qsys, on the Generation tab, turn on Create HDL design files for synthesis.
2. Click Generate. System generation may take several seconds to complete.
3. After system generation completes, on the File menu, click Exit.
4. In the Quartus II software, on the Project menu, click Add/Remove Files in Project.
5. Ensure that the .qip file in the synthesis subdirectory is added to the project.
6. On the Processing menu, click Start Compilation.
f For detailed information about Qsys systems and their generation, refer to the System
Design with Qsys section in volume 1 of the Quartus II Handbook.
Accessing the Custom Instruction from Software
Adding a custom instruction to a Nios II processor results in a significant change to
the Qsys system. In this section, you create and build a new software project using the
Nios II software build flow, and run the software that accesses the custom instruction.
The software source files are included in the downloadable design files.
January 2011 Altera CorporationNios II Custom Instruction User Guide
4–6Chapter 4: Implementing a Nios II Custom Instruction in Qsys
Accessing the Custom Instruction from Software
Tab le 4– 1 lists the CRC application software source files and their corresponding
descriptions.
Table 4–1. CRC Application Software Source Files
File NameDescription
Main program that populates random test data, executes the CRC both in
crc_main.c
crc.cSoftware CRC algorithm run by the Nios II processor.
crc.hHeader file for crc.c.
ci_crc.cProgram that accesses CRC custom instruction.
ci_crc.hHeader file for ci_crc.c.
software and with the custom instruction, validates the output, and reports
the processing time.
To run the application software, you must create an Executable and Linking Format
File (.elf) first. To create the .elf file, follow the instructions in the Nios II Software
Build Flow section in the readme_qsys.txt file in the extracted design files.
The application program runs three implementations of the CRC algorithm on the
same pseudo-random input data: an unoptimized software implementation, an
optimized software implementation, and the custom instruction CRC. The program
calculates the processing time and throughput for each of the versions, to demonstrate
the improved efficiency of a custom instruction compared to a software
implementation.
Example 4–1 shows the output from the application program run on a Nios II
Embedded Evaluation Kit, Cyclone III Edition (NEEK) using the Quartus II software
v10.1 Service Pack 1 and Nios II Embedded Design Suite (EDS) v10.1 Service Pack 1.
The output shows that the custom instruction CRC is more than 70 times faster than
the unoptimized CRC calculated purely in software and is more than 45 times faster
than the optimized version of the software CRC. The results you see using different
target device and board may vary depending on the memory characteristics of the
board and the clock speed of the device, but these ratios are representative.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Chapter 4: Implementing a Nios II Custom Instruction in Qsys4–7
Accessing the Custom Instruction from Software
Example 4–1. Output of the CRC Design Example Software Run on a NEEK Using the Quartus II Software v10.1 SP1
******************************************************************************
Comparison between software and custom instruction CRC32
******************************************************************************
System specification
-------------------System clock speed = 62.5 MHz
Number of buffer locations = 16
Size of each buffer = 65535 bytes
Initializing all of the buffers with pseudo-random data
------------Custom instruction CRC vs software CRC = 72.2
Custom instruction CRC vs optimized software CRC = 46.6
Optimized software CRC vs software CRC = 1.5
January 2011 Altera CorporationNios II Custom Instruction User Guide
4–8Chapter 4: Implementing a Nios II Custom Instruction in Qsys
Using the User-defined Custom Instruction Macro
Using the User-defined Custom Instruction Macro
The design example software uses a user-defined macro to access the CRC custom
instruction. Example 4–2 shows the macro that is defined in the ci_crc.c file.
Example 4–2. CRC Custom Instruction Macro Usage
#define CRC_CI_MACRO(n, A) __builtin_custom_ini(ALT_CI_CRC_0_N + (n & 0x7), (A))
This macro takes a single
CRC custom instruction has extended type; the
int
type input operand and returns an
n
value in the macro
int
type value. The
CRC_CI_MACRO()
indicates the operation to be performed by the custom instruction. The custom
instruction index is added to the value of
n
. The n value is masked because the n port
of a custom instruction has only three bits.
To initialize the custom instruction, for example, you can add the initialization code in
Example 4–3 to your application software.
Example 4–3. Using the User-defined Macro to Initialize the Custom Instruction Logic
/* Initialize the custom instruction CRC to the initial remainder value: */
CRC_CI_MACRO (0,0);
For details of each operation of the CRC custom instruction and the corresponding
index value
n
, refer to the comments in the ci_crc.c file.
Example 4–2 and Example 4–3 demonstrate that you can define the macro in your
application to accommodate your requirements. For example, you can determine the
number and type of input operands, decide whether to assign a return value, and
vary the custom instruction index. However, the macro definition and usage must be
consistent with the port declarations of the custom instruction. For example, if you
define the macro to return an
int
value, the custom instruction must have a
port.
result
f For details about writing software for Nios II custom instructions, refer to Chapter 2,
Software Interface.
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
A. Custom Instruction Templates
This appendix provides VHDL and Verilog HDL custom instruction wrapper file
templates that you can reference when writing custom instructions in VHDL and
Ver ilo g H DL .
f Full sets of template files are available in the <nios2eds installation
directory>/examples/verilog/custom_instruction_templates and <nios2eds installation
directory>/examples/vhdl/custom_instruction_templates directories.
January 2011 Altera CorporationNios II Custom Instruction User Guide
A–2Appendix A: Custom Instruction Templates
VHDL Custom Instruction Template
VHDL Custom Instruction Template
Example A–1 shows a VHDL custom instruction template for an internal register type
custom instruction.
Example A–1. VHDL Custom Instruction Template for Internal Register Logic Type Instruction
-- VHDL Custom Instruction Template File for Internal Register Logic
library ieee;
use ieee.std_logic_1164.all;
entity custominstruction is
port(
signal clk: in std_logic;
-- CPU system clock (required for multicycle or extended multicycle)
signal reset: in std_logic;
signal clk_en: in std_logic;
signal start: in std_logic;
signal done: out std_logic;
signal n: in std_logic_vector(7 downto 0);
signal dataa: in std_logic_vector(31 downto 0);-- Operand A (always required)
signal datab: in std_logic_vector(31 downto 0);-- Operand B (optional)
signal a: in std_logic_vector(4 downto 0);-- Internal operand A index register
signal b: in std_logic_vector(4 downto 0);-- Internal operand B index register
signal c: in std_logic_vector(4 downto 0);-- Internal result index register
signal readra: in std_logic;
signal readrb: in std_logic;
signal writerc: in std_logic;
signal result: out std_logic_vector(31 downto 0)-- result (always required)
);
end entity custominstruction;
architecture a_custominstruction of custominstruction is
-- local custom instruction signals
begin
-- custom instruction logic (note: external interfaces can be used as well)
-- Use the n[7..0] port as a select signal on a multiplexer
--to select the value to feed result[31..0]
-- CPU master asynchronous active high reset
-- (required for multicycle or extended multicycle)
-- Clock-qualifier (required for multicycle or extended multicycle)
-- Active high signal used to specify that inputs are valid
--(required for multicycle or extended multicycle)
-- Active high signal used to notify the CPU that result is valid
--(required for variable multicycle or extended variable multicycle)
-- N-field selector (required for extended);
-- Modify width to match the number of unique operations in the instruction
-- Read operand A from CPU (otherwise use internal operand A)
-- Read operand B from CPU (otherwise use internal operand B)
-- Write result to CPU (otherwise write to internal result)
end architecture a_custominstruction;
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Appendix A: Custom Instruction TemplatesA–3
Verilog HDL Custom Instruction Template Example
Verilog HDL Custom Instruction Template Example
Example A–2 shows a Verilog HDL custom instruction template for an internal
register type custom instruction.
Example A–2. Verilog HDL Custom Instruction Template for Internal Register Logic Type Instruction
// Verilog Custom Instruction Template File for Internal Register Logic
module custominstruction(
clk,// CPU system clock (required for multicycle or extended multicycle)
reset, // CPU master asynchronous active high reset
//(required for multicycle or extended multicycle)
clk_en,// Clock-qualifier (required for multicycle or extended multicycle)
start, // Active high signal used to specify that inputs are valid
//(required for multicycle or extended multicycle)
done, // Active high signal used to notify the CPU that result is valid
n,// N-field selector (required for extended)
dataa, // Operand A (always required)
datab, // Operand B (optional)
a,// Internal operand A index register
b,// Internal operand B index register
c,// Internal result index register
readra,// Read operand A from CPU (otherwise use internal operand A)
readrb, // Read operand B from CPU (otherwise use internal operand B)
writerc,// Write result to CPU (otherwise write to internal result)
result // Result (always required)
);
//(required for variable multicycle or extended variable multicycle)
//INPUTS
inputclk;
inputreset;
inputclk_en;
inputstart;
input[7:0]n;// modify width to match the number of unique operations in the instruction
input[4:0]a;
input[4:0]b;
input[4:0]c;
inputreadra;
inputreadrb;
inputwriterc;
input[31:0]dataa;
input[31:0]datab;
//OUTPUTS
outputdone;
output[31:0]result;
// custom instruction logic (note: external interfaces can be used as well)
// Use the n[7..0] port as a select signal on a multiplexer
//to select the value to feed result[31..0]
endmodule
January 2011 Altera CorporationNios II Custom Instruction User Guide
A–4Appendix A: Custom Instruction Templates
Verilog HDL Custom Instruction Template Example
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
B. Custom Instruction Built-in Functions
The Nios II
gcc
compiler,
nios2-elf-gcc
, is customized with built-in functions to
support custom instructions. This section lists the built-in functions.
f For more information about
gcc
built-in functions, refer to www.gnu.org.
Nios II custom instruction built-in functions have the following return types:
■
void
■
int
■
float
■ pointer
Built-in Functions that Return a Void Value
The following built-in functions return a void value:
■
void __builtin_custom_n (int n);
■
void __builtin_custom_ni (int n, int dataa);
■
void __builtin_custom_nf (int n, float dataa);
■
void __builtin_custom_np (int n, void *dataa);
■
void __builtin_custom_nii (int n, int dataa, int datab);
■
void __builtin_custom_nif (int n, int dataa, float datab);
■
void __builtin_custom_nip (int n, int dataa, void *datab);
■
void __builtin_custom_nfi (int n, float dataa, int datab);
■
void __builtin_custom_nff (int n, float dataa, float datab);
■
void __builtin_custom_nfp (int n, float dataa, void *datab);
■
void __builtin_custom_npi (int n, void *dataa, int datab);
■
void __builtin_custom_npf (int n, void *dataa, float datab);
■
void __builtin_custom_npp (int n, void *dataa, void *datab);
Built-in Functions that Return a Value of Type Int
The following built-in functions return a value of type
■
int __builtin_custom_in (int n);
■
int __builtin_custom_ini (int n, int dataa);
■
int __builtin_custom_inf (int n, float dataa);
■
int __builtin_custom_inp (int n, void *dataa);
int
:
January 2011 Altera CorporationNios II Custom Instruction User Guide
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
C. Floating Point Custom Instructions
If you add a floating point custom instruction to your system, conflicts could arise in
the software build process. This appendix describes how to handle these conflicts.
Both Qsys and SOPC Builder offer a predefined Floating Point Hardware component
that you can add to your system. When you add this component to your system, a
parameter editor displays, and you can turn on an option to include a floating point
divider. The parameter editor notifies you that the component generates
single-precision floating point hardware.
When you add the floating point custom instruction to your system and run the
software build process, flags are added to your
flags specify the opcode extensions of the custom instructions that are called by the
software and select the appropriate library.
One of the following two flags appears:
■
-mcustom-fpu-cfg=60-1
nios2-elf-gcc
command line. These
■
-mcustom-fpu-cfg=60-2
The flag value
floating point divider, and the flag value
60-1
indicates your system does not include a custom instruction
60-2
indicates your system includes a
custom instruction floating point divider.
The
-mcustom-fpu-cfg
double-precision constants, you must remove the
flags forces the use of single-precision constants. To allow
-mcustom-fpu-cfg
flag and replace
it with different individual compiler flags, depending on the flag value.
To enable double-precision floating point constants, follow one of these steps:
flag disables
floating point custom instruction support in your library calls, forcing the system to
use the emulated or slower version of the instruction instead.
January 2011 Altera CorporationNios II Custom Instruction User Guide
C–2Appendix C: Floating Point Custom Instructions
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
This chapter provides additional information about the document and Altera.
Document Revision History
The following table shows the revision history for this document.
DateVersionChanges
■ Updated for Quartus II software v10.1.
January 20112.0
May 20081.5
May 20071.4Add title and core version number to page footers.
May 20071.3
December 20041.2Updates for Nios II processor vresion 1.1.
September 20041.1Updates for Nios II processor version 1.01.
May 20041.0Initial release.
■ Updated for new Qsys system integration tool flow.
■ Updated with formatting changes.
■ Add new tutorial design.
■ Describe new custom instruction import flow.
■ Minor corrections to terminology and usage.
■ Describe new component editor import flow.
■ Remove tutorial design.
■ Minor corrections to terminology and usage.
Additional Information
How to Contact Altera
To locate the most up-to-date information about Altera products, refer to the
following table.
Non-technical support (General)Emailnacomp@altera.com
(Software Licensing)Emailauthorization@altera.com
Note to Table:
(1) You can also contact your local Altera sales office or sales representative.
Websitewww.altera.com/training
Emailcustrain@altera.com
January 2011 Altera CorporationNios II Custom Instruction User Guide
Info–2Additional Information
Typographic Conventions
Typographic Conventions
The following table shows the typographic conventions this document uses.
Visual CueMeaning
Bold Type with Initial Capital
Letters
bold type
Italic Type with Initial Capital LettersIndicate document titles. For example, Stratix IV Design Guidelines.
italic type
Initial Capital Letters
“Subheading Title”
Courier type
r An angled arrow instructs you to press the Enter key.
1., 2., 3., and
a., b., c., and so on
■ ■ ■Bullets indicate a list of items when the sequence of the items is not important.
1The hand points to information that requires special attention.
A question mark directs you to a software help system with related information.
f The feet direct you to another document or website with related information.
c
w
Indicate command names, dialog box titles, dialog box options, and other GUI
labels. For example, Save As dialog box. For GUI elements, capitalization matches
the GUI.
Indicates directory names, project names, disk drive names, file names, file name
extensions, software utility names, and GUI labels. For example, \qdesigns
directory, D: drive, and chiptrip.gdf file.
Indicates variables. For example, n + 1.
Variable names are enclosed in angle brackets (< >). For example, <file name> and
<project name>.pof file.
Indicate keyboard keys and menu names. For example, the Delete key and the
Options menu.
Quotation marks indicate references to sections within a document and titles of
Quartus II Help topics. For example, “Typographic Conventions.”
Indicates signal, port, register, bit, block, and primitive names. For example,
tdi
, and
input
. The suffix n denotes an active-low signal. For example,
Indicates command line commands and anything that must be typed exactly as it
appears. For example,
Also indicates sections of an actual file, such as a Report File, references to parts of
files (for example, the AHDL keyword
TRI
example,
Numbered steps indicate a list of items when the sequence of the items is important,
such as the steps listed in a procedure.
A caution calls attention to a condition or possible situation that can damage or
destroy the product or your work.
A warning calls attention to a condition or possible situation that can cause you
injury.
The envelope links to the Email Subscription Management Center page of the Altera
website, where you can sign up to receive update notifications for Altera documents.
).
c:\qdesigns\tutorial\chiptrip.gdf
SUBDESIGN
), and logic function names (for
.
data1
resetn
.
,
Nios II Custom Instruction User GuideJanuary 2011 Altera Corporation
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.