1 |
54 |
zero_gravi |
// #################################################################################################
|
2 |
55 |
zero_gravi |
// # << NEORV32: neorv32_intrinsics.h - Helper functions/macros for (custom) "intrinsics" >> #
|
3 |
54 |
zero_gravi |
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2021, 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 neorv32_intrinsics.h
|
38 |
|
|
* @author Stephan Nolting
|
39 |
|
|
* @brief Helper functions and macros for custom "intrinsics" / instructions.
|
40 |
|
|
**************************************************************************/
|
41 |
|
|
|
42 |
|
|
#ifndef neorv32_intrinsics_h
|
43 |
|
|
#define neorv32_intrinsics_h
|
44 |
|
|
|
45 |
|
|
/**********************************************************************//**
|
46 |
|
|
* @name Custom instructions / intrinsics helper macros
|
47 |
|
|
**************************************************************************/
|
48 |
|
|
/**@{*/
|
49 |
|
|
|
50 |
|
|
//** Selection helper macro */
|
51 |
|
|
#define STR1(x) #x
|
52 |
|
|
//** Selection helper macro 2 */
|
53 |
|
|
#define STR(x) STR1(x)
|
54 |
|
|
|
55 |
|
|
//** Register address converter */
|
56 |
|
|
#define GET_REG_ADDR(x) REG_ADDR_##x
|
57 |
|
|
|
58 |
|
|
#define REG_ADDR_x0 0 /**< register 0 */
|
59 |
|
|
#define REG_ADDR_x1 1 /**< register 1 */
|
60 |
|
|
#define REG_ADDR_x2 2 /**< register 2 */
|
61 |
|
|
#define REG_ADDR_x3 3 /**< register 3 */
|
62 |
|
|
#define REG_ADDR_x4 4 /**< register 4 */
|
63 |
|
|
#define REG_ADDR_x5 5 /**< register 5 */
|
64 |
|
|
#define REG_ADDR_x6 6 /**< register 6 */
|
65 |
|
|
#define REG_ADDR_x7 7 /**< register 7 */
|
66 |
|
|
#define REG_ADDR_x8 8 /**< register 8 */
|
67 |
|
|
#define REG_ADDR_x9 9 /**< register 9 */
|
68 |
|
|
#define REG_ADDR_x10 10 /**< register 10 */
|
69 |
|
|
#define REG_ADDR_x11 11 /**< register 11 */
|
70 |
|
|
#define REG_ADDR_x12 12 /**< register 12 */
|
71 |
|
|
#define REG_ADDR_x13 13 /**< register 13 */
|
72 |
|
|
#define REG_ADDR_x14 14 /**< register 14 */
|
73 |
|
|
#define REG_ADDR_x15 15 /**< register 15 */
|
74 |
|
|
#define REG_ADDR_x16 16 /**< register 16 */
|
75 |
|
|
#define REG_ADDR_x17 17 /**< register 17 */
|
76 |
|
|
#define REG_ADDR_x18 18 /**< register 18 */
|
77 |
|
|
#define REG_ADDR_x19 19 /**< register 19 */
|
78 |
|
|
#define REG_ADDR_x20 20 /**< register 20 */
|
79 |
|
|
#define REG_ADDR_x21 21 /**< register 21 */
|
80 |
|
|
#define REG_ADDR_x22 22 /**< register 22 */
|
81 |
|
|
#define REG_ADDR_x23 23 /**< register 23 */
|
82 |
|
|
#define REG_ADDR_x24 24 /**< register 24 */
|
83 |
|
|
#define REG_ADDR_x25 25 /**< register 25 */
|
84 |
|
|
#define REG_ADDR_x26 26 /**< register 26 */
|
85 |
|
|
#define REG_ADDR_x27 27 /**< register 27 */
|
86 |
|
|
#define REG_ADDR_x28 28 /**< register 28 */
|
87 |
|
|
#define REG_ADDR_x29 29 /**< register 29 */
|
88 |
|
|
#define REG_ADDR_x30 30 /**< register 30 */
|
89 |
|
|
#define REG_ADDR_x31 31 /**< register 31 */
|
90 |
|
|
#define REG_ADDR_zero 0 /**< register 0 - according to calling convention */
|
91 |
|
|
#define REG_ADDR_ra 1 /**< register 1 - according to calling convention */
|
92 |
|
|
#define REG_ADDR_sp 2 /**< register 2 - according to calling convention */
|
93 |
|
|
#define REG_ADDR_gp 3 /**< register 3 - according to calling convention */
|
94 |
|
|
#define REG_ADDR_tp 4 /**< register 4 - according to calling convention */
|
95 |
|
|
#define REG_ADDR_t0 5 /**< register 5 - according to calling convention */
|
96 |
|
|
#define REG_ADDR_t1 6 /**< register 6 - according to calling convention */
|
97 |
|
|
#define REG_ADDR_t2 7 /**< register 7 - according to calling convention */
|
98 |
|
|
#define REG_ADDR_s0 8 /**< register 8 - according to calling convention */
|
99 |
|
|
#define REG_ADDR_s1 9 /**< register 9 - according to calling convention */
|
100 |
|
|
#define REG_ADDR_a0 10 /**< register 10 - according to calling convention */
|
101 |
|
|
#define REG_ADDR_a1 11 /**< register 11 - according to calling convention */
|
102 |
|
|
#define REG_ADDR_a2 12 /**< register 12 - according to calling convention */
|
103 |
|
|
#define REG_ADDR_a3 13 /**< register 13 - according to calling convention */
|
104 |
|
|
#define REG_ADDR_a4 14 /**< register 14 - according to calling convention */
|
105 |
|
|
#define REG_ADDR_a5 15 /**< register 15 - according to calling convention */
|
106 |
|
|
#define REG_ADDR_a6 16 /**< register 16 - according to calling convention */
|
107 |
|
|
#define REG_ADDR_a7 17 /**< register 17 - according to calling convention */
|
108 |
|
|
#define REG_ADDR_s2 18 /**< register 18 - according to calling convention */
|
109 |
|
|
#define REG_ADDR_s3 19 /**< register 19 - according to calling convention */
|
110 |
|
|
#define REG_ADDR_s4 20 /**< register 20 - according to calling convention */
|
111 |
|
|
#define REG_ADDR_s5 21 /**< register 21 - according to calling convention */
|
112 |
|
|
#define REG_ADDR_s6 22 /**< register 22 - according to calling convention */
|
113 |
|
|
#define REG_ADDR_s7 23 /**< register 23 - according to calling convention */
|
114 |
|
|
#define REG_ADDR_s8 24 /**< register 24 - according to calling convention */
|
115 |
|
|
#define REG_ADDR_s9 25 /**< register 25 - according to calling convention */
|
116 |
|
|
#define REG_ADDR_s10 26 /**< register 26 - according to calling convention */
|
117 |
|
|
#define REG_ADDR_s11 27 /**< register 27 - according to calling convention */
|
118 |
|
|
#define REG_ADDR_t3 28 /**< register 28 - according to calling convention */
|
119 |
|
|
#define REG_ADDR_t4 29 /**< register 29 - according to calling convention */
|
120 |
|
|
#define REG_ADDR_t5 30 /**< register 30 - according to calling convention */
|
121 |
|
|
#define REG_ADDR_t6 31 /**< register 31 - according to calling convention */
|
122 |
|
|
|
123 |
55 |
zero_gravi |
//** Construct instruction word (32-bit) for R2-type instruction */
|
124 |
|
|
#define CMD_WORD_R2_TYPE(funct7, rs2, rs1, funct3, rd, opcode) \
|
125 |
54 |
zero_gravi |
( (opcode & 0x7f) << 0 ) + \
|
126 |
|
|
( (rd & 0x1f) << 7 ) + \
|
127 |
55 |
zero_gravi |
( (funct3 & 0x1f) << 12 ) + \
|
128 |
54 |
zero_gravi |
( (rs1 & 0x1f) << 15 ) + \
|
129 |
|
|
( (rs2 & 0x1f) << 20 ) + \
|
130 |
55 |
zero_gravi |
( (funct7 & 0x7f) << 25 )
|
131 |
54 |
zero_gravi |
|
132 |
55 |
zero_gravi |
//** Construct instruction word (32-bit) for R3-type instruction */
|
133 |
|
|
#define CMD_WORD_R3_TYPE(rs3, rs2, rs1, funct3, rd, opcode) \
|
134 |
|
|
( (opcode & 0x7f) << 0 ) + \
|
135 |
|
|
( (rd & 0x1f) << 7 ) + \
|
136 |
|
|
( (funct3 & 0x1f) << 12 ) + \
|
137 |
|
|
( (rs1 & 0x1f) << 15 ) + \
|
138 |
|
|
( (rs2 & 0x1f) << 20 ) + \
|
139 |
|
|
( (rs3 & 0x1f) << 27 )
|
140 |
|
|
|
141 |
54 |
zero_gravi |
//** Construct instruction word (32-bit) for I-type instruction */
|
142 |
|
|
#define CMD_WORD_I_TYPE(imm12, rs1_f5, funct3, rd, opcode) \
|
143 |
|
|
( (opcode & 0x7f) << 0 ) + \
|
144 |
|
|
( (rd & 0x1f) << 7 ) + \
|
145 |
55 |
zero_gravi |
( (funct3 & 0x1f) << 12 ) + \
|
146 |
54 |
zero_gravi |
( (rs1_f5 & 0x1f) << 15 ) + \
|
147 |
55 |
zero_gravi |
( (imm12 & 0xfff) << 20 )
|
148 |
54 |
zero_gravi |
|
149 |
55 |
zero_gravi |
//** Construct custom R3-type instruction (4 registers, funct3, opcode) */
|
150 |
|
|
#define CUSTOM_INSTR_R3_TYPE(rs3, rs2, rs1, funct3, rd, opcode) \
|
151 |
61 |
zero_gravi |
asm volatile (".word " STR(CMD_WORD_R3_TYPE(GET_REG_ADDR(rs3), GET_REG_ADDR(rs2), GET_REG_ADDR(rs1), funct3, GET_REG_ADDR(rd), opcode))"\n");
|
152 |
54 |
zero_gravi |
|
153 |
55 |
zero_gravi |
//** Construct custom R2-type instruction (3 registers, funct3, funct7, opcode) */
|
154 |
|
|
#define CUSTOM_INSTR_R2_TYPE(funct7, rs2, rs1, funct3, rd, opcode) \
|
155 |
61 |
zero_gravi |
asm volatile (".word " STR(CMD_WORD_R2_TYPE(funct7, GET_REG_ADDR(rs2), GET_REG_ADDR(rs1), funct3, GET_REG_ADDR(rd), opcode))"\n");
|
156 |
55 |
zero_gravi |
|
157 |
|
|
//** Construct custom R1-type instruction (2 registers, funct3, funct7, funct5, opcode) */
|
158 |
54 |
zero_gravi |
#define CUSTOM_INSTR_R1_TYPE(funct7, funct5, rs1, funct3, rd, opcode) \
|
159 |
61 |
zero_gravi |
asm volatile (".word " STR(CMD_WORD_R2_TYPE(funct7, funct5, GET_REG_ADDR(rs1), funct3, GET_REG_ADDR(rd), opcode))"\n");
|
160 |
54 |
zero_gravi |
|
161 |
55 |
zero_gravi |
//** Construct custom I-type instruction (2 registers, funct3, imm12, opcode) */
|
162 |
54 |
zero_gravi |
#define CUSTOM_INSTR_I_TYPE(imm12, rs1, funct3, rd, opcode) \
|
163 |
61 |
zero_gravi |
asm volatile (".word " STR(CMD_WORD_I_TYPE(imm12, GET_REG_ADDR(rs1), funct3, GET_REG_ADDR(rd), opcode))"\n");
|
164 |
54 |
zero_gravi |
/**@}*/
|
165 |
|
|
|
166 |
|
|
#endif // neorv32_intrinsics_h
|