AN 977: Nios® V Processor Custom Instruction

ID 773194
Date 4/14/2023
Public
Document Table of Contents

3.1. Custom Instruction Software C Macro

The following examples illustrate how the Nios® V processor custom instruction software interface fits into the software code.

The example below shows a portion of the system.h header file that defines a macro for a custom instruction. The macros defined by the Board Support Package Editor use C integer types only.

Software C Macros with assigned funct3 and funct7 fields

#define CUSTOM_OPERATION(VAL_1, VAL_2) ({ \ 
 int output; \ 
 asm volatile (".insn r  0x0B, 0x1, 0x0, %[out], %[input1], %[input2]" \ 
: [out] "=r" (output) \ 
: [input1] "r" (VAL_1), [input2] "r" (VAL_2)); \ 
output;  \ 
}) 
Note: The example above assigns funct3 as 0x1 and funct7 as 0x0.

The example below shows an application code that uses the custom instruction and includes the system.h file. This example enables the application software to use the custom instruction macro definition. You are required to apply uint32_t variable type, along with inttypes.h header.

Software C Macros that includes system.h

#include "system.h"
#include <inttypes.h>
int main (void)

{
uint32_t val_1 = 0x12345678;      //max 32-bit
uint32_t val_2 = 0x98765432;      //max 32-bit
uint32_t result;
	
	result = CUSTOM_OPERATION_1(val_1, val_2);
	return 0;
}