4.3.1. Writing the Custom Instruction HDL File
4.3.2. Opening the Component Editor
4.3.3. Specifying the Custom Instruction Component Type
4.3.4. Displaying the Custom Instruction Block Symbol
4.3.5. Adding the Custom Instruction HDL File
4.3.6. Configuring the Custom Instruction Parameter Type
4.3.7. Setting Up the Custom Instruction Interfaces
4.3.8. Saving and Adding the Custom Instruction
4.3.9. Generating and Compiling the Processor System
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;
}