| 1 |
typedef __UINT8_TYPE__ u8; |
| 2 |
typedef __UINT16_TYPE__ u16; |
| 3 |
__extension__ typedef __uint24 u24; |
| 4 |
typedef __UINT32_TYPE__ u32; |
| 5 |
typedef __UINT64_TYPE__ u64; |
| 6 |
|
| 7 |
__attribute__((__used__)) |
| 8 |
u8 gprs[32]; |
| 9 |
|
| 10 |
// USE_VALUE = 1: Copy constant to value[] and pass that to test_gprs(). |
| 11 |
// USE_VALUE = 0: Pass Lval label from .macro run_test_gprs to test_gprs(). |
| 12 |
#ifndef USE_VALUE |
| 13 |
#error define USE_VALUE to 0 or 1 |
| 14 |
#endif |
| 15 |
|
| 16 |
#if USE_VALUE |
| 17 |
__attribute__((__used__)) |
| 18 |
u8 value[8]; |
| 19 |
#endif |
| 20 |
|
| 21 |
#ifdef __AVR_HAVE_JMP_CALL__ |
| 22 |
#define XCALL "call" |
| 23 |
#else |
| 24 |
#define XCALL "rcall" |
| 25 |
#endif |
| 26 |
|
| 27 |
#define GPRS_16_29 "16,17,18,19,20,21,22,23,24,25,26,27,28,29" |
| 28 |
|
| 29 |
#ifdef __AVR_TINY__ |
| 30 |
#define FIRST_GPR 16 |
| 31 |
#define GPRs_29 GPRS_16_29 |
| 32 |
#else |
| 33 |
#define FIRST_GPR 0 |
| 34 |
#define GPRs_29 "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15," GPRS_16_29 |
| 35 |
#endif |
| 36 |
|
| 37 |
#if __AVR_RODATA_IN_RAM__ |
| 38 |
#define PGM __attribute__((progmem)) |
| 39 |
#else |
| 40 |
#define PGM |
| 41 |
#endif |
| 42 |
|
| 43 |
// Save GPRs to gprs[]. |
| 44 |
__attribute__((naked, used)) |
| 45 |
void save_gprs (void) |
| 46 |
{ |
| 47 |
__asm ( |
| 48 |
#ifdef __AVR_TINY__ |
| 49 |
"push r31" "\n\t" |
| 50 |
"push r30" "\n\t" |
| 51 |
"ldi r30, lo8(gprs+%0)" "\n\t" |
| 52 |
"ldi r31, hi8(gprs+%0)" "\n\t" |
| 53 |
".irp n," GPRs_29 "\n\t" |
| 54 |
" st z+, \\n" "\n\t" |
| 55 |
".endr" "\n\t" |
| 56 |
"pop r16 $ st z+, r16" "\n\t" |
| 57 |
"pop r16 $ st z+, r16" "\n\t" |
| 58 |
#else |
| 59 |
"sts gprs+31, r31" "\n\t" |
| 60 |
"sts gprs+30, r30" "\n\t" |
| 61 |
"ldi r30, lo8(gprs+%0)" "\n\t" |
| 62 |
"ldi r31, hi8(gprs+%0)" "\n\t" |
| 63 |
".irp n," GPRs_29 "\n\t" |
| 64 |
" st z+, \\n" "\n\t" |
| 65 |
".endr" "\n\t" |
| 66 |
#endif |
| 67 |
"ret" |
| 68 |
:: "n" (FIRST_GPR)); |
| 69 |
} |
| 70 |
|
| 71 |
// Restore GPRs from gprs[]. |
| 72 |
__attribute__((naked, used)) |
| 73 |
void restore_gprs (void) |
| 74 |
{ |
| 75 |
__asm ("ldi r30, lo8(gprs+%0)" "\n\t" |
| 76 |
"ldi r31, hi8(gprs+%0)" "\n\t" |
| 77 |
".irp n," GPRs_29 "\n\t" |
| 78 |
" ld \\n, z+" "\n\t" |
| 79 |
".endr" "\n\t" |
| 80 |
#ifdef __AVR_TINY__ |
| 81 |
"push r29" "\n\t" |
| 82 |
"ld r29, z+" "\n\t" |
| 83 |
"push r29" "\n\t" |
| 84 |
"ld r31, z" "\n\t" |
| 85 |
"pop r30" "\n\t" |
| 86 |
"pop r29" "\n\t" |
| 87 |
#else |
| 88 |
"ld r30, z" "\n\t" |
| 89 |
"lds r31, gprs+31" "\n\t" |
| 90 |
#endif |
| 91 |
"ret" |
| 92 |
:: "n" (FIRST_GPR)); |
| 93 |
} |
| 94 |
|
| 95 |
// Write N-byte const value VAL: *Z++ = VAL. |
| 96 |
__asm (".macro write_value n, val" "\n\t" |
| 97 |
" ldi r24, lo8(\\val)" "\n\t" |
| 98 |
" st z+, r24" "\n\t" |
| 99 |
" .if \\n > 1" "\n\t" |
| 100 |
" write_value \"(\\n - 1)\", \"(\\val >> 8)\"" "\n\t" |
| 101 |
" .endif" "\n\t" |
| 102 |
".endm"); |
| 103 |
|
| 104 |
// 1) Save all gprs |
| 105 |
// 2) Call test_gprs (n, regno, Lval, line) |
| 106 |
// 3) Restore all gprs |
| 107 |
__asm (".macro run_test_gprs n, regno, val, Lval, line" "\n\t" |
| 108 |
"TestForLine\\line\\().L\\@:" "\n\t" |
| 109 |
" " XCALL " save_gprs" "\n\t" |
| 110 |
# if USE_VALUE |
| 111 |
// Write VAL to value[] |
| 112 |
" ldi r30, lo8(\\Lval)" "\n\t" |
| 113 |
" ldi r31, hi8(\\Lval)" "\n\t" |
| 114 |
" write_value \\n, \\val" "\n\t" |
| 115 |
#endif |
| 116 |
// Call test_gprs (u8 n, u8 regno, const void *Lval, int line) |
| 117 |
" ldi r24, \\n" "\n\t" |
| 118 |
" ldi r22, \\regno" "\n\t" |
| 119 |
" ldi r21, hi8(\\Lval)" "\n\t" |
| 120 |
" ldi r20, lo8(\\Lval)" "\n\t" |
| 121 |
" ldi r19, hi8(\\line)" "\n\t" |
| 122 |
" ldi r18, lo8(\\line)" "\n\t" |
| 123 |
" " XCALL " test_gprs" "\n\t" |
| 124 |
" " XCALL " restore_gprs" "\n\t" |
| 125 |
"DoneTestLine\\line\\().L\\@:" "\n\t" |
| 126 |
".endm"); |
| 127 |
|
| 128 |
// Test if reg[REG] ... reg[REG + BITS/8 - 1] are holding VAL. |
| 129 |
#define rtest(BITS, REG, VAL) \ |
| 130 |
do { \ |
| 131 |
PGM static const u##BITS Lval = VAL; \ |
| 132 |
register u##BITS r##REG __asm (#REG) = VAL; \ |
| 133 |
__asm ("run_test_gprs %[size], %[reg], %[val], %[Lval], %[line] " \ |
| 134 |
"; u" #BITS " r" #REG "=" #VAL ", line %[line];" \ |
| 135 |
:: \ |
| 136 |
[size] "n" (BITS / 8), [reg] "n" (REG), \ |
| 137 |
[val] "n" ((u##BITS) VAL), [Lval] "i" (& Lval), \ |
| 138 |
[line] "n" (__LINE__), "r" (r##REG)); \ |
| 139 |
} while (0) |
| 140 |
|
| 141 |
#define rtest_nowhile0(BITS, REG, VAL) \ |
| 142 |
PGM static const u##BITS Lval = VAL; \ |
| 143 |
register u##BITS r##REG __asm (#REG) = VAL; \ |
| 144 |
__asm ("run_test_gprs %[size], %[reg], %[val], %[Lval], %[line] " \ |
| 145 |
"; u" #BITS " r" #REG "=" #VAL ", line %[line];" \ |
| 146 |
:: \ |
| 147 |
[size] "n" (BITS / 8), [reg] "n" (REG), \ |
| 148 |
[val] "n" ((u##BITS) VAL), [Lval] "i" (& Lval), \ |
| 149 |
[line] "n" (__LINE__), "r" (r##REG)) |
| 150 |
|
| 151 |
|
| 152 |
#if ! __AVR_RODATA_IN_RAM__ || USE_VALUE |
| 153 |
#define LOAD_INCZ "ld %0,%a1+" |
| 154 |
#elif defined (__AVR_HAVE_LPMX__) |
| 155 |
#define LOAD_INCZ "lpm %0,%a1+" |
| 156 |
#else |
| 157 |
#define LOAD_INCZ "lpm $ mov %0,r0 $ adiw r30,1" |
| 158 |
#endif |
| 159 |
|
| 160 |
// Called by .macro run_test_gprs which is invoked by rtest() |
| 161 |
// resp. rtest_nowhile(). |
| 162 |
__attribute__((__used__)) |
| 163 |
void test_gprs (u8 n, u8 regno, const void *pval, int line) |
| 164 |
{ |
| 165 |
const u8 *r = gprs + regno; |
| 166 |
const u8 *pv = pval; |
| 167 |
for (u8 i = 0; i < n; ++i) |
| 168 |
{ |
| 169 |
u8 vi; |
| 170 |
__asm (LOAD_INCZ : "=r" (vi), "+z" (pv)); |
| 171 |
if (*r++ != vi) |
| 172 |
__builtin_exit (line); |
| 173 |
} |
| 174 |
} |