Nios II Custom Instruction User Guide

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

3.1. Custom Instruction Software Examples

These examples illustrate how the Nios II custom instruction software interface fits into your software code.

The following example 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.

#define ALT_CI_BITSWAP_N 0x00
#define ALT_CI_BITSWAP(A) __builtin_custom_ini(ALT_CI_BITSWAP_N,(A))

In this example, ALT_CI_BITWSWAP_N is defined to be 0x0, which is the custom instruction’s selection index. The ALT_CI_BITSWAP(A) macro accepts a single argument, abstracting out the selection index ALT_CI_BITWSWAP_N. The macro maps to a GNU Compiler Collection (GCC) Nios II built-in function.

The next example illustrates application code that uses the bit-swap custom instruction.


#include "system.h"

int main (void)
{
	int a = 0x12345678;
	int a_swap = 0;
	
	a_swap = ALT_CI_BITSWAP(a);
	return 0;
}			

The code in this example includes the system.h file to enable the application software to use the custom instruction macro definition. The example code declares two integers, a and a_swap. Integer a is passed as input to the bit swap custom instruction and the results are loaded in a_swap.

The example above 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.

Note: You can define custom macros for Nios II custom instructions that allow other 32 bit input types to interface with custom instructions.