| 1 |
72 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # << NEORV32 - CFU Custom Instructions Example Program >> #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # BSD 3-Clause License #
|
| 5 |
|
|
// # #
|
| 6 |
|
|
// # Copyright (c) 2022, Stephan Nolting. All rights reserved. #
|
| 7 |
|
|
// # #
|
| 8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
| 9 |
|
|
// # permitted provided that the following conditions are met: #
|
| 10 |
|
|
// # #
|
| 11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
| 12 |
|
|
// # conditions and the following disclaimer. #
|
| 13 |
|
|
// # #
|
| 14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
| 15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
| 16 |
|
|
// # provided with the distribution. #
|
| 17 |
|
|
// # #
|
| 18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
| 19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
| 20 |
|
|
// # permission. #
|
| 21 |
|
|
// # #
|
| 22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
| 23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
| 24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
| 25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
| 26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
| 27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
| 28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
| 29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
| 30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
| 31 |
|
|
// # ********************************************************************************************* #
|
| 32 |
|
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
| 33 |
|
|
// #################################################################################################
|
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
/**********************************************************************//**
|
| 37 |
|
|
* @file demo_cfu/main.c
|
| 38 |
|
|
* @author Stephan Nolting
|
| 39 |
|
|
* @brief Example program showing how to use the CFU's custom instructions.
|
| 40 |
|
|
**************************************************************************/
|
| 41 |
|
|
#include <neorv32.h>
|
| 42 |
|
|
|
| 43 |
|
|
|
| 44 |
|
|
/**********************************************************************//**
|
| 45 |
|
|
* @name User configuration
|
| 46 |
|
|
**************************************************************************/
|
| 47 |
|
|
/**@{*/
|
| 48 |
|
|
/** UART BAUD rate */
|
| 49 |
|
|
#define BAUD_RATE 19200
|
| 50 |
|
|
/** Number of test cases per CFU instruction */
|
| 51 |
|
|
#define TESTCASES 4
|
| 52 |
|
|
/**@}*/
|
| 53 |
|
|
|
| 54 |
|
|
|
| 55 |
|
|
/**********************************************************************//**
|
| 56 |
|
|
* @name Prototypes
|
| 57 |
|
|
**************************************************************************/
|
| 58 |
|
|
uint32_t xorshift32(void);
|
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
|
|
/**********************************************************************//**
|
| 62 |
|
|
* Main function
|
| 63 |
|
|
*
|
| 64 |
|
|
* @note This program requires the CFU and UART0.
|
| 65 |
|
|
*
|
| 66 |
|
|
* @return 0 if execution was successful
|
| 67 |
|
|
**************************************************************************/
|
| 68 |
|
|
int main() {
|
| 69 |
|
|
|
| 70 |
|
|
// initialize NEORV32 run-time environment
|
| 71 |
|
|
neorv32_rte_setup();
|
| 72 |
|
|
|
| 73 |
|
|
// setup UART0 at default baud rate, no parity bits, no HW flow control
|
| 74 |
|
|
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
| 75 |
|
|
|
| 76 |
|
|
// check if UART0 is implemented
|
| 77 |
|
|
if (neorv32_uart0_available() == 0) {
|
| 78 |
|
|
return 1; // UART0 not available, exit
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
// check if the CFU is implemented at all
|
| 82 |
|
|
// note that the CFU is wrapped in the core's "Zxcfu" ISA extension
|
| 83 |
|
|
if (neorv32_cpu_cfu_available() == 0) {
|
| 84 |
|
|
neorv32_uart0_printf("ERROR! CFU ('Zxcfu' ISA extensions) not implemented!\n");
|
| 85 |
|
|
return 1;
|
| 86 |
|
|
}
|
| 87 |
|
|
|
| 88 |
|
|
|
| 89 |
|
|
// intro
|
| 90 |
|
|
neorv32_uart0_printf("\n<<< NEORV32 Custom Functions Unit (CFU) 'Custom Instructions' Example Program >>>\n\n");
|
| 91 |
|
|
|
| 92 |
|
|
neorv32_uart0_printf("NOTE: This program assumes the _default_ CFU hardware module, which implements\n"
|
| 93 |
|
|
" four simple data conversion instructions.\n\n");
|
| 94 |
|
|
|
| 95 |
|
|
neorv32_uart0_printf("NOTE: This program (and it's comments) just shows how to USE the CFU's custom\n"
|
| 96 |
|
|
" instructions. The actual implementation of these instructions is done\n"
|
| 97 |
|
|
" in the CFU hardware module (-> rtl/core/neorv32_cpu_cp_cfu.vhd).\n\n");
|
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
// custom instructions usage examples
|
| 101 |
|
|
uint32_t i, opa, opb;
|
| 102 |
|
|
|
| 103 |
|
|
neorv32_uart0_printf("\n--- CFU 'binary to gray' instruction (funct3 = 000) ---\n");
|
| 104 |
|
|
for (i=0; i<TESTCASES; i++) {
|
| 105 |
|
|
opa = xorshift32(); // get random test data
|
| 106 |
|
|
opb = 0;
|
| 107 |
|
|
neorv32_uart0_printf("%u: neorv32_cfu_cmd0 - OPA = 0x%x, OPB = 0x%x, ", i, opa, opb);
|
| 108 |
|
|
|
| 109 |
|
|
// The CFU custom instruction can be used as plain C functions!
|
| 110 |
|
|
//
|
| 111 |
|
|
// There are 8 "prototypes" for the CFU instructions:
|
| 112 |
|
|
// - neorv32_cfu_cmd0(funct7, rs1, rs2) - sets the instruction's "funct3" bit field to 000
|
| 113 |
|
|
// - neorv32_cfu_cmd1(funct7, rs1, rs2) - sets the instruction's "funct3" bit field to 001
|
| 114 |
|
|
// - ...
|
| 115 |
|
|
// - neorv32_cfu_cmd7(funct7, rs1, rs2) - sets the instruction's "funct3" bit field to 111
|
| 116 |
|
|
//
|
| 117 |
|
|
// These functions are turned into 32-bit instruction words resembling a R2-type RISC-V instruction
|
| 118 |
|
|
// (=> "intrinsics").
|
| 119 |
|
|
//
|
| 120 |
|
|
// Each neorv32_cfu_cmd* function requires three arguments:
|
| 121 |
|
|
// - funct7: a compile-time static 7-bit immediate (put in the instruction's "funct7" bit field)
|
| 122 |
|
|
// - rs1: a 32-bit operand A (this is the first register file source rs1)
|
| 123 |
|
|
// - rs2: a 32-bit operand B (this is the first register second source rs2)
|
| 124 |
|
|
//
|
| 125 |
|
|
// The operands can be literals, variables, function return values, ... you name it.
|
| 126 |
|
|
//
|
| 127 |
|
|
// Each neorv32_cfu_cmd* function returns a 32-bit uint32_t data word, which represents
|
| 128 |
|
|
// the result of the according instruction.
|
| 129 |
|
|
//
|
| 130 |
|
|
// The 7-bit immediate ("funct7") can be used to pass small _static_ literals to the CFU
|
| 131 |
|
|
// or to do a more fine-grained function selection - it all depends on your hardware implementation! ;)
|
| 132 |
|
|
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd0(0b0000000, opa, opb));
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
|
|
neorv32_uart0_printf("\n--- CFU 'gray to binary' instruction (funct3 = 001) ---\n");
|
| 136 |
|
|
for (i=0; i<TESTCASES; i++) {
|
| 137 |
|
|
opa = xorshift32();
|
| 138 |
|
|
neorv32_uart0_printf("%u: neorv32_cfu_cmd1 - OPA = 0x%x, OPB = 0x%x, ", i, opa, 0);
|
| 139 |
|
|
// you can also pass literals instead of variables to the intrinsics (0 instead of opb):
|
| 140 |
|
|
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd1(0b0000000, opa, 0));
|
| 141 |
|
|
}
|
| 142 |
|
|
|
| 143 |
|
|
neorv32_uart0_printf("\n--- CFU 'bit reversal' instruction (funct3 = 010) ---\n");
|
| 144 |
|
|
for (i=0; i<TESTCASES; i++) {
|
| 145 |
|
|
opa = xorshift32();
|
| 146 |
|
|
neorv32_uart0_printf("%u: neorv32_cfu_cmd2 - OPA = 0x%x, OPB = 0x%x, ", i, opa, 0);
|
| 147 |
|
|
// here we are setting the funct7 bit-field to all-one; however, this is not
|
| 148 |
|
|
// used at all by the default CFU hardware module
|
| 149 |
|
|
// note that all funct3/funct7 combinations are treated as "valid" by the CPU
|
| 150 |
|
|
// - so there is no chance of causing an illegal instruction exception by using the CFU intrinsics
|
| 151 |
|
|
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd2(0b1111111, opa, 0));
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
neorv32_uart0_printf("\n--- CFU 'XNOR' instruction (funct3 = 011) ---\n");
|
| 155 |
|
|
for (i=0; i<TESTCASES; i++) {
|
| 156 |
|
|
opa = xorshift32();
|
| 157 |
|
|
opb = xorshift32();
|
| 158 |
|
|
neorv32_uart0_printf("%u: neorv32_cfu_cmd3 - OPA = 0x%x, OPB = 0x%x, ", i, opa, opb);
|
| 159 |
|
|
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd3(0b0000000, opa, opb));
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
|
| 163 |
|
|
neorv32_uart0_printf("\nCFU demo program completed.\n");
|
| 164 |
|
|
|
| 165 |
|
|
return 0;
|
| 166 |
|
|
}
|
| 167 |
|
|
|
| 168 |
|
|
|
| 169 |
|
|
/**********************************************************************//**
|
| 170 |
|
|
* Pseudo-random number generator (to generate deterministic test data).
|
| 171 |
|
|
*
|
| 172 |
|
|
* @return Random data (32-bit).
|
| 173 |
|
|
**************************************************************************/
|
| 174 |
|
|
uint32_t xorshift32(void) {
|
| 175 |
|
|
|
| 176 |
|
|
static uint32_t x32 = 314159265;
|
| 177 |
|
|
|
| 178 |
|
|
x32 ^= x32 << 13;
|
| 179 |
|
|
x32 ^= x32 >> 17;
|
| 180 |
|
|
x32 ^= x32 << 5;
|
| 181 |
|
|
|
| 182 |
|
|
return x32;
|
| 183 |
|
|
}
|