|
| 1 | +// RISC-V Compliance IO Test Header File |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2005-2018 Imperas Software Ltd., www.imperas.com |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 15 | + * either express or implied. |
| 16 | + * |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | + |
| 23 | +// |
| 24 | +// In general the following registers are reserved |
| 25 | +// ra, a0, t0, t1 |
| 26 | +// x1, x10 x5, x6 |
| 27 | +// new reserve x31 |
| 28 | +// |
| 29 | + |
| 30 | +#ifndef _COMPLIANCE_IO_H |
| 31 | +#define _COMPLIANCE_IO_H |
| 32 | + |
| 33 | +#define RVTEST_IO_QUIET |
| 34 | + |
| 35 | +//----------------------------------------------------------------------- |
| 36 | +// RV IO Macros (Character transfer by custom instruction) |
| 37 | +//----------------------------------------------------------------------- |
| 38 | +#define STRINGIFY(x) #x |
| 39 | +#define TOSTRING(x) STRINGIFY(x) |
| 40 | + |
| 41 | +#define RVTEST_CUSTOM1 0x0005200B |
| 42 | + |
| 43 | +#ifdef RVTEST_IO_QUIET |
| 44 | + |
| 45 | +#define RVTEST_IO_INIT |
| 46 | +#define RVTEST_IO_WRITE_STR(_SP, _STR) |
| 47 | +#define RVTEST_IO_CHECK() |
| 48 | +#define RVTEST_IO_ASSERT_GPR_EQ(_SP, _R, _I) |
| 49 | +#define RVTEST_IO_ASSERT_SFPR_EQ(_F, _R, _I) |
| 50 | +#define RVTEST_IO_ASSERT_DFPR_EQ(_D, _R, _I) |
| 51 | + |
| 52 | +#else |
| 53 | + |
| 54 | +#define RSIZE 4 |
| 55 | +// _SP = (volatile register) |
| 56 | +#define LOCAL_IO_PUSH(_SP) \ |
| 57 | + la _SP, begin_regstate; \ |
| 58 | + sw x1, (1*RSIZE)(_SP); \ |
| 59 | + sw x5, (5*RSIZE)(_SP); \ |
| 60 | + sw x6, (6*RSIZE)(_SP); \ |
| 61 | + sw x8, (8*RSIZE)(_SP); \ |
| 62 | + sw x10, (10*RSIZE)(_SP); |
| 63 | + |
| 64 | +// _SP = (volatile register) |
| 65 | +#define LOCAL_IO_POP(_SP) \ |
| 66 | + la _SP, begin_regstate; \ |
| 67 | + lw x1, (1*RSIZE)(_SP); \ |
| 68 | + lw x5, (5*RSIZE)(_SP); \ |
| 69 | + lw x6, (6*RSIZE)(_SP); \ |
| 70 | + lw x8, (8*RSIZE)(_SP); \ |
| 71 | + lw x10, (10*RSIZE)(_SP); |
| 72 | + |
| 73 | +#define LOCAL_IO_WRITE_GPR(_R) \ |
| 74 | + mv a0, _R; \ |
| 75 | + jal FN_WriteA0; |
| 76 | + |
| 77 | +#define LOCAL_IO_WRITE_FPR(_F) \ |
| 78 | + fmv.x.s a0, _F; \ |
| 79 | + jal FN_WriteA0; |
| 80 | + |
| 81 | +#define LOCAL_IO_WRITE_DFPR(_V1, _V2) \ |
| 82 | + mv a0, _V1; \ |
| 83 | + jal FN_WriteA0; \ |
| 84 | + mv a0, _V2; \ |
| 85 | + jal FN_WriteA0; \ |
| 86 | + |
| 87 | +#define LOCAL_IO_PUTC(_R) \ |
| 88 | + .word RVTEST_CUSTOM1; \ |
| 89 | + |
| 90 | +#define RVTEST_IO_INIT |
| 91 | + |
| 92 | +// Assertion violation: file file.c, line 1234: (expr) |
| 93 | +// _SP = (volatile register) |
| 94 | +// _R = GPR |
| 95 | +// _I = Immediate |
| 96 | +#define RVTEST_IO_ASSERT_GPR_EQ(_SP, _R, _I) \ |
| 97 | + LOCAL_IO_PUSH(_SP) \ |
| 98 | + mv s0, _R; \ |
| 99 | + li t0, _I; \ |
| 100 | + beq s0, t0, 20002f; \ |
| 101 | + LOCAL_IO_WRITE_STR("Assertion violation: file "); \ |
| 102 | + LOCAL_IO_WRITE_STR(__FILE__); \ |
| 103 | + LOCAL_IO_WRITE_STR(", line "); \ |
| 104 | + LOCAL_IO_WRITE_STR(TOSTRING(__LINE__)); \ |
| 105 | + LOCAL_IO_WRITE_STR(": "); \ |
| 106 | + LOCAL_IO_WRITE_STR(# _R); \ |
| 107 | + LOCAL_IO_WRITE_STR("("); \ |
| 108 | + LOCAL_IO_WRITE_GPR(s0); \ |
| 109 | + LOCAL_IO_WRITE_STR(") != "); \ |
| 110 | + LOCAL_IO_WRITE_STR(# _I); \ |
| 111 | + LOCAL_IO_WRITE_STR("\n"); \ |
| 112 | + li TESTNUM, 100; \ |
| 113 | + RVTEST_FAIL; \ |
| 114 | +20002: \ |
| 115 | + LOCAL_IO_POP(_SP) |
| 116 | + |
| 117 | +// _F = FPR |
| 118 | +// _C = GPR |
| 119 | +// _I = Immediate |
| 120 | +#define RVTEST_IO_ASSERT_SFPR_EQ(_F, _C, _I) \ |
| 121 | + fmv.x.s t0, _F; \ |
| 122 | + beq _C, t0, 20003f; \ |
| 123 | + LOCAL_IO_WRITE_STR("Assertion violation: file "); \ |
| 124 | + LOCAL_IO_WRITE_STR(__FILE__); \ |
| 125 | + LOCAL_IO_WRITE_STR(", line "); \ |
| 126 | + LOCAL_IO_WRITE_STR(TOSTRING(__LINE__)); \ |
| 127 | + LOCAL_IO_WRITE_STR(": "); \ |
| 128 | + LOCAL_IO_WRITE_STR(# _F); \ |
| 129 | + LOCAL_IO_WRITE_STR("("); \ |
| 130 | + LOCAL_IO_WRITE_FPR(_F); \ |
| 131 | + LOCAL_IO_WRITE_STR(") != "); \ |
| 132 | + LOCAL_IO_WRITE_STR(# _I); \ |
| 133 | + LOCAL_IO_WRITE_STR("\n"); \ |
| 134 | + li TESTNUM, 100; \ |
| 135 | + RVTEST_FAIL; \ |
| 136 | +20003: |
| 137 | + |
| 138 | +// _D = DFPR |
| 139 | +// _R = GPR |
| 140 | +// _I = Immediate |
| 141 | +#define RVTEST_IO_ASSERT_DFPR_EQ(_D, _R, _I) \ |
| 142 | + fmv.x.d t0, _D; \ |
| 143 | + beq _R, t0, 20005f; \ |
| 144 | + LOCAL_IO_WRITE_STR("Assertion violation: file "); \ |
| 145 | + LOCAL_IO_WRITE_STR(__FILE__); \ |
| 146 | + LOCAL_IO_WRITE_STR(", line "); \ |
| 147 | + LOCAL_IO_WRITE_STR(TOSTRING(__LINE__)); \ |
| 148 | + LOCAL_IO_WRITE_STR(": "); \ |
| 149 | + LOCAL_IO_WRITE_STR(# _D); \ |
| 150 | + LOCAL_IO_WRITE_STR("("); \ |
| 151 | + LOCAL_IO_WRITE_DFPR(_D); \ |
| 152 | + LOCAL_IO_WRITE_STR(") != "); \ |
| 153 | + LOCAL_IO_WRITE_STR(# _I); \ |
| 154 | + LOCAL_IO_WRITE_STR("\n"); \ |
| 155 | + li TESTNUM, 100; \ |
| 156 | + RVTEST_FAIL; \ |
| 157 | +20005: |
| 158 | + |
| 159 | +// _SP = (volatile register) |
| 160 | +#define LOCAL_IO_WRITE_STR(_STR) RVTEST_IO_WRITE_STR(x31, _STR) |
| 161 | +#define RVTEST_IO_WRITE_STR(_SP, _STR) \ |
| 162 | + LOCAL_IO_PUSH(_SP) \ |
| 163 | + .section .data.string; \ |
| 164 | +20001: \ |
| 165 | + .string _STR; \ |
| 166 | + .section .text.init; \ |
| 167 | + la a0, 20001b; \ |
| 168 | + jal FN_WriteStr; \ |
| 169 | + LOCAL_IO_POP(_SP) |
| 170 | + |
| 171 | +// generate assertion listing |
| 172 | +#define LOCAL_CHECK() RVTEST_IO_CHECK() |
| 173 | +#define RVTEST_IO_CHECK() \ |
| 174 | + li zero, -1; \ |
| 175 | + |
| 176 | +// |
| 177 | +// FN_WriteStr: Uses a0, t0 |
| 178 | +// |
| 179 | +FN_WriteStr: |
| 180 | + mv t0, a0; |
| 181 | +10000: |
| 182 | + lbu a0, (t0); |
| 183 | + addi t0, t0, 1; |
| 184 | + beq a0, zero, 10000f; |
| 185 | + LOCAL_IO_PUTC(a0); |
| 186 | + j 10000b; |
| 187 | +10000: |
| 188 | + ret; |
| 189 | + |
| 190 | +// |
| 191 | +// FN_WriteA0: write register a0(x10) (destroys a0(x10), t0-t2(x5-x7)) |
| 192 | +// |
| 193 | +FN_WriteA0: |
| 194 | + mv t0, a0 |
| 195 | + // determine architectural register width |
| 196 | + li a0, -1 |
| 197 | + srli a0, a0, 31 |
| 198 | + srli a0, a0, 1 |
| 199 | + bnez a0, FN_WriteA0_64 |
| 200 | + |
| 201 | +FN_WriteA0_32: |
| 202 | + // reverse register when xlen is 32 |
| 203 | + li t1, 8 |
| 204 | +10000: slli t2, t2, 4 |
| 205 | + andi a0, t0, 0xf |
| 206 | + srli t0, t0, 4 |
| 207 | + or t2, t2, a0 |
| 208 | + addi t1, t1, -1 |
| 209 | + bnez t1, 10000b |
| 210 | + li t1, 8 |
| 211 | + j FN_WriteA0_common |
| 212 | + |
| 213 | +FN_WriteA0_64: |
| 214 | + // reverse register when xlen is 64 |
| 215 | + li t1, 16 |
| 216 | +10000: slli t2, t2, 4 |
| 217 | + andi a0, t0, 0xf |
| 218 | + srli t0, t0, 4 |
| 219 | + or t2, t2, a0 |
| 220 | + addi t1, t1, -1 |
| 221 | + bnez t1, 10000b |
| 222 | + li t1, 16 |
| 223 | + |
| 224 | +FN_WriteA0_common: |
| 225 | + // write reversed characters |
| 226 | + li t0, 10 |
| 227 | +10000: andi a0, t2, 0xf |
| 228 | + blt a0, t0, 10001f |
| 229 | + addi a0, a0, 'a'-10 |
| 230 | + j 10002f |
| 231 | +10001: addi a0, a0, '0' |
| 232 | +10002: LOCAL_IO_PUTC(a0) |
| 233 | + srli t2, t2, 4 |
| 234 | + addi t1, t1, -1 |
| 235 | + bnez t1, 10000b |
| 236 | + ret |
| 237 | + |
| 238 | +#endif // RVTEST_IO_QUIET |
| 239 | + |
| 240 | +#endif // _COMPLIANCE_IO_H |
0 commit comments