Using the user-defined custom instruction macro, Using the user-defined custom instruction macro –8 – Altera Nios II Custom User Manual

Page 30

Advertising
background image

3–8

Chapter 3: Implementing a Nios II Custom Instruction in SOPC Builder

Using the User-defined Custom Instruction Macro

Nios II Custom Instruction User Guide

January 2011

Altera Corporation

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.

This macro takes a single

int

type input operand and returns an

int

type value. The

CRC custom instruction has extended type; the

n

value in the macro

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.

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

result

port.

f

For details about writing software for Nios II custom instructions, refer to

Chapter 2,

Software Interface

.

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))

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);

Advertising