Nios II Custom Instruction User Guide

ID 683242
Date 4/27/2020
Public
Document Table of Contents

4.2.2. Using the User-defined Custom Instruction Macro

The design example software uses a user-defined macro to access the CRC custom instruction.

The following example shows the macro that is defined in the ci_crc.c file.

#define CRC_CI_MACRO(n, A) \
__builtin_custom_ini(ALT_CI_CRC_CUSTOM_COMPONENT_0_N + (n & 0x7), (A))

This macro accepts 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.

ALT_CI_CRC_CUSTOM_COMPONENT_0_N is the custom instruction selection index for the first instruction in the component. ALT_CI_CRC_CUSTOM_COMPONENT_0_N is added to the value of n to calculate the selection index for a specific instruction. The n value is masked because the n port of the custom instruction has only three bits.

To initialize the custom instruction, for example, you can add the initialization code in the following example to your application software.

/* 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 value of n, refer to the comments in the ci_crc.c file.

The examples above 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 extension index value, n. 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.