| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "cpu/irq_ctx.h" |
| 15 |
|
| 16 |
#include <stdbool.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
static int failures = 0; |
| 21 |
|
| 22 |
static void |
| 23 |
check(bool cond, const char *what) |
| 24 |
{ |
| 25 |
if (!cond) { |
| 26 |
failures++; |
| 27 |
printf("FAIL: %s\n", what); |
| 28 |
} else { |
| 29 |
printf("ok : %s\n", what); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define VEC_IRQ 0 |
| 36 |
#define VEC_NMI 1 |
| 37 |
#define VEC_BRK 2 |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
int |
| 44 |
main(void) |
| 45 |
{ |
| 46 |
|
| 47 |
{ |
| 48 |
cpu_irq_ctx_reset(); |
| 49 |
check(!cpu_in_interrupt(), "starts outside any interrupt"); |
| 50 |
check(cpu_irq_depth() == 0, "at depth zero"); |
| 51 |
check(cpu_irq_count() == 0, "with nothing counted"); |
| 52 |
check(cpu_irq_return_pc() == 0, "and no return address to report"); |
| 53 |
check(!cpu_irq_take_entered_flag(), "and nothing just entered"); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
{ |
| 58 |
cpu_irq_ctx_reset(); |
| 59 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1234, 0x01FD); |
| 60 |
|
| 61 |
check(cpu_in_interrupt(), "reports being inside a handler"); |
| 62 |
check(cpu_irq_depth() == 1, "at depth one"); |
| 63 |
check(cpu_irq_count() == 1, "counts the interrupt"); |
| 64 |
check(cpu_irq_last_vector() == VEC_IRQ, "remembers which vector"); |
| 65 |
check(cpu_irq_return_pc() == 0x1234, "reports where it will return to"); |
| 66 |
|
| 67 |
|
| 68 |
cpu_irq_ctx_leave(0x01FD); |
| 69 |
check(!cpu_in_interrupt(), "reports leaving the handler"); |
| 70 |
check(cpu_irq_depth() == 0, "back to depth zero"); |
| 71 |
check(cpu_irq_count() == 1, "the count survives the return"); |
| 72 |
check(cpu_irq_return_pc() == 0, "and there is no return address again"); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
{ |
| 80 |
cpu_irq_ctx_reset(); |
| 81 |
check(!cpu_irq_take_entered_flag(), "no entry reported before one happens"); |
| 82 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 83 |
check(cpu_irq_take_entered_flag(), "reports the entry once"); |
| 84 |
check(!cpu_irq_take_entered_flag(), "and only once"); |
| 85 |
|
| 86 |
|
| 87 |
cpu_irq_ctx_enter(VEC_NMI, 0x2000, 0x01FA); |
| 88 |
check(cpu_irq_take_entered_flag(), "a second entry is reported too"); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
{ |
| 95 |
cpu_irq_ctx_reset(); |
| 96 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 97 |
cpu_irq_ctx_enter(VEC_NMI, 0x2000, 0x01FA); |
| 98 |
|
| 99 |
check(cpu_irq_depth() == 2, "counts nested handlers"); |
| 100 |
check(cpu_irq_count() == 2, "counts both interrupts"); |
| 101 |
check(cpu_irq_last_vector() == VEC_NMI, "reports the innermost vector"); |
| 102 |
check(cpu_irq_return_pc() == 0x2000, "reports the innermost return address"); |
| 103 |
|
| 104 |
cpu_irq_ctx_leave(0x01FA); |
| 105 |
check(cpu_irq_depth() == 1, "unwinds one level per return"); |
| 106 |
check(cpu_in_interrupt(), "still inside the outer handler"); |
| 107 |
check(cpu_irq_return_pc() == 0x1000, "and reports its return address again"); |
| 108 |
|
| 109 |
cpu_irq_ctx_leave(0x01FD); |
| 110 |
check(cpu_irq_depth() == 0, "unwinds the outer handler too"); |
| 111 |
check(!cpu_in_interrupt(), "and is outside any interrupt"); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
{ |
| 118 |
cpu_irq_ctx_reset(); |
| 119 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 120 |
cpu_irq_ctx_enter(VEC_IRQ, 0x2000, 0x01FA); |
| 121 |
cpu_irq_ctx_enter(VEC_IRQ, 0x3000, 0x01F7); |
| 122 |
check(cpu_irq_depth() == 3, "three handlers open"); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
cpu_irq_ctx_leave(0x01FD); |
| 127 |
check(cpu_irq_depth() == 0, "a return past every frame unwinds them all"); |
| 128 |
check(!cpu_in_interrupt(), "and leaves the machine outside any interrupt"); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
{ |
| 136 |
cpu_irq_ctx_reset(); |
| 137 |
cpu_irq_ctx_leave(0x01FD); |
| 138 |
check(cpu_irq_depth() == 0, "an unmatched return leaves depth at zero"); |
| 139 |
check(!cpu_in_interrupt(), "and does not report being in a handler"); |
| 140 |
|
| 141 |
cpu_irq_ctx_leave(0x01FD); |
| 142 |
cpu_irq_ctx_leave(0x01FD); |
| 143 |
check(cpu_irq_depth() == 0, "repeated unmatched returns stay at zero"); |
| 144 |
|
| 145 |
|
| 146 |
cpu_irq_ctx_enter(VEC_IRQ, 0x4000, 0x01FD); |
| 147 |
check(cpu_irq_depth() == 1 && cpu_irq_return_pc() == 0x4000, |
| 148 |
"tracking still works after an unmatched return"); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
{ |
| 153 |
cpu_irq_ctx_reset(); |
| 154 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 155 |
cpu_irq_ctx_enter(VEC_NMI, 0x2000, 0x01FA); |
| 156 |
cpu_irq_ctx_reset(); |
| 157 |
|
| 158 |
check(cpu_irq_depth() == 0, "reset abandons open handlers"); |
| 159 |
check(!cpu_in_interrupt(), "and reports being outside one"); |
| 160 |
check(cpu_irq_count() == 0, "and clears the count"); |
| 161 |
check(cpu_irq_return_pc() == 0, "and the return address"); |
| 162 |
check(!cpu_irq_take_entered_flag(), "and the just-entered flag"); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
{ |
| 169 |
cpu_irq_ctx_reset(); |
| 170 |
uint16_t sp = 0x01FD; |
| 171 |
for (int i = 0; i < 40; i++) { |
| 172 |
cpu_irq_ctx_enter(VEC_IRQ, (uint16_t)(0x1000 + i), sp); |
| 173 |
sp = (uint16_t)(sp - 3); |
| 174 |
} |
| 175 |
check(cpu_irq_depth() == 40, "counts deeper nesting than it records"); |
| 176 |
check(cpu_irq_count() == 40, "and counts every interrupt"); |
| 177 |
|
| 178 |
|
| 179 |
for (int i = 39; i >= 0; i--) { |
| 180 |
sp = (uint16_t)(sp + 3); |
| 181 |
cpu_irq_ctx_leave(sp); |
| 182 |
} |
| 183 |
check(cpu_irq_depth() == 0, "and unwinds all the way back out"); |
| 184 |
check(!cpu_in_interrupt(), "ending outside any interrupt"); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
cpu_irq_ctx_reset(); |
| 189 |
sp = 0x01FD; |
| 190 |
for (int i = 0; i < CPU_IRQ_CTX_MAX + 1; i++) { |
| 191 |
cpu_irq_ctx_enter(VEC_IRQ, (uint16_t)(0x1000 + i), sp); |
| 192 |
sp = (uint16_t)(sp - 3); |
| 193 |
} |
| 194 |
check(cpu_irq_depth() == CPU_IRQ_CTX_MAX + 1, "opens one more than it records"); |
| 195 |
|
| 196 |
|
| 197 |
cpu_irq_ctx_leave(0x01FD); |
| 198 |
check(cpu_irq_depth() == CPU_IRQ_CTX_MAX, |
| 199 |
"an unrecorded level pops exactly one, without cascading"); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
{ |
| 217 |
cpu_irq_ctx_reset(); |
| 218 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x0101); |
| 219 |
cpu_irq_ctx_enter(VEC_NMI, 0x2000, 0x01FE); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
check(cpu_irq_depth() == 1, "a wrapped outer frame is retired, not kept"); |
| 224 |
|
| 225 |
cpu_irq_ctx_leave(0x01FE); |
| 226 |
check(cpu_irq_depth() == 0, "and the inner return closes what is left"); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
cpu_irq_ctx_enter(VEC_IRQ, 0x3000, 0x01FD); |
| 232 |
check(cpu_irq_depth() == 1, "the next clean interrupt is tracked normally"); |
| 233 |
check(cpu_irq_return_pc() == 0x3000, "with its own return address"); |
| 234 |
cpu_irq_ctx_leave(0x01FD); |
| 235 |
check(cpu_irq_depth() == 0, "and closes cleanly"); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
{ |
| 244 |
cpu_irq_ctx_reset(); |
| 245 |
|
| 246 |
|
| 247 |
uint16_t sp = 0x01FD; |
| 248 |
for (int i = 0; i < CPU_IRQ_CTX_MAX + 6; i++) |
| 249 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, sp--); |
| 250 |
check(cpu_irq_depth() == CPU_IRQ_CTX_MAX + 6, "nesting can pass the array"); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
cpu_irq_ctx_enter(VEC_IRQ, 0x2000, 0x01F5); |
| 259 |
check(cpu_irq_depth() == 9, |
| 260 |
"levels above the array collapse onto the deepest surviving frame"); |
| 261 |
check(cpu_irq_return_pc() == 0x2000, "and the new frame is the innermost"); |
| 262 |
|
| 263 |
|
| 264 |
cpu_irq_ctx_leave(0x01F5); |
| 265 |
check(cpu_irq_depth() == 8, "leaving it uncovers the frame beneath"); |
| 266 |
|
| 267 |
|
| 268 |
cpu_irq_ctx_enter(VEC_IRQ, 0x3000, 0x01FD); |
| 269 |
check(cpu_irq_depth() == 1, "an interrupt above them all retires the rest"); |
| 270 |
cpu_irq_ctx_leave(0x01FD); |
| 271 |
check(cpu_irq_depth() == 0, "so the depth comes back to zero"); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
{ |
| 277 |
cpu_irq_ctx_reset(); |
| 278 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 279 |
cpu_irq_ctx_enter(VEC_NMI, 0x2000, 0x01F9); |
| 280 |
check(cpu_irq_depth() == 2, "nested handlers count two"); |
| 281 |
|
| 282 |
cpu_irq_ctx_leave(0x01F9); |
| 283 |
check(cpu_irq_depth() == 1, "a balanced inner return unwinds only its own"); |
| 284 |
check(cpu_irq_return_pc() == 0x1000, "and the outer frame is reported again"); |
| 285 |
|
| 286 |
cpu_irq_ctx_leave(0x01FD); |
| 287 |
check(cpu_irq_depth() == 0, "and the outer return closes it"); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
{ |
| 292 |
cpu_irq_ctx_reset(); |
| 293 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 294 |
cpu_irq_ctx_enter(VEC_NMI, 0x2000, 0x01F9); |
| 295 |
cpu_irq_ctx_enter(VEC_BRK, 0x3000, 0x01F5); |
| 296 |
cpu_irq_ctx_leave(0x01FD); |
| 297 |
check(cpu_irq_depth() == 0, |
| 298 |
"a return to an outer frame's own entry unwinds everything inside it"); |
| 299 |
} |
| 300 |
|
| 301 |
{ |
| 302 |
cpu_irq_ctx_reset(); |
| 303 |
for (int i = 0; i < 100; i++) { |
| 304 |
cpu_irq_ctx_enter(VEC_IRQ, 0x1000, 0x01FD); |
| 305 |
cpu_irq_ctx_leave(0x01FD); |
| 306 |
} |
| 307 |
check(cpu_irq_count() == 100, "counts every interrupt taken, not just open ones"); |
| 308 |
check(cpu_irq_depth() == 0, "while ending balanced"); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
{ |
| 313 |
cpu_irq_ctx_reset(); |
| 314 |
const int vectors[] = { VEC_IRQ, VEC_NMI, VEC_BRK }; |
| 315 |
bool ok = true; |
| 316 |
for (unsigned i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) { |
| 317 |
cpu_irq_ctx_enter(vectors[i], 0x1000, 0x01FD); |
| 318 |
if (cpu_irq_last_vector() != vectors[i]) |
| 319 |
ok = false; |
| 320 |
cpu_irq_ctx_leave(0x01FD); |
| 321 |
} |
| 322 |
check(ok, "reports back whichever vector was taken"); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
{ |
| 332 |
cpu_irq_ctx_reset(); |
| 333 |
|
| 334 |
cpu_irq_ctx_enter(VEC_BRK, 0x4000, 0x01F0); |
| 335 |
|
| 336 |
cpu_irq_ctx_enter(VEC_IRQ, 0x5000, 0x01FD); |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
check(cpu_irq_depth() == 1, "an abandoned frame is retired, not stacked"); |
| 341 |
|
| 342 |
cpu_irq_ctx_leave(0x01FD); |
| 343 |
check(cpu_irq_depth() == 0, |
| 344 |
"so the depth comes back to zero once the IRQ returns"); |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
cpu_irq_ctx_enter(VEC_BRK, 0x4100, 0x01F0); |
| 349 |
cpu_irq_ctx_enter(VEC_IRQ, 0x5100, 0x01FD); |
| 350 |
cpu_irq_ctx_leave(0x01FD); |
| 351 |
check(cpu_irq_depth() == 0, "however many times it happens"); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
{ |
| 359 |
cpu_irq_ctx_reset(); |
| 360 |
|
| 361 |
cpu_irq_ctx_enter(VEC_IRQ, 0x123456, 0x01FD); |
| 362 |
check(cpu_irq_return_pc() == 0x123456, |
| 363 |
"the return address keeps its program bank"); |
| 364 |
|
| 365 |
cpu_irq_ctx_enter(VEC_NMI, 0x008000, 0x01F9); |
| 366 |
check(cpu_irq_return_pc() == 0x008000, |
| 367 |
"and a bank-zero address is unchanged"); |
| 368 |
|
| 369 |
cpu_irq_ctx_leave(0x01F9); |
| 370 |
check(cpu_irq_return_pc() == 0x123456, |
| 371 |
"and the outer one is restored when the inner returns"); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
{ |
| 381 |
cpu_irq_ctx_reset(); |
| 382 |
|
| 383 |
|
| 384 |
for (int sp = 0xFFFF; sp >= 0; sp--) |
| 385 |
cpu_irq_ctx_enter(VEC_BRK, 0x6000, (uint16_t)sp); |
| 386 |
|
| 387 |
const int deep = cpu_irq_depth(); |
| 388 |
check(deep > CPU_IRQ_CTX_MAX, |
| 389 |
"nesting can exceed the frames it records detail for"); |
| 390 |
check(deep == 0x10000, "up to the cap"); |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
cpu_irq_ctx_enter(VEC_BRK, 0x6000, 0x0000); |
| 396 |
check(cpu_irq_depth() == 0x10000, "and stays there when it is passed"); |
| 397 |
check(cpu_in_interrupt(), "and still reports an interrupt"); |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
for (int i = 0; i < deep - CPU_IRQ_CTX_MAX; i++) |
| 402 |
cpu_irq_ctx_leave(0x0000); |
| 403 |
check(cpu_irq_depth() == CPU_IRQ_CTX_MAX, |
| 404 |
"undetailed levels come off one at a time"); |
| 405 |
|
| 406 |
cpu_irq_ctx_leave(0xFFFF); |
| 407 |
check(cpu_irq_depth() == 0, "and the outermost return closes the rest"); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
{ |
| 414 |
cpu_irq_ctx_reset(); |
| 415 |
|
| 416 |
cpu_irq_ctx_enter(VEC_BRK, 0x7000, 0x01FD); |
| 417 |
cpu_irq_ctx_enter(VEC_IRQ, 0x7100, 0x01FD); |
| 418 |
check(cpu_irq_depth() == 1, |
| 419 |
"an interrupt at the same level retires the one before it"); |
| 420 |
|
| 421 |
cpu_irq_ctx_leave(0x01FD); |
| 422 |
check(cpu_irq_depth() == 0, "and returns cleanly to zero"); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
{ |
| 433 |
cpu_irq_ctx_reset(); |
| 434 |
|
| 435 |
cpu_irq_ctx_enter(VEC_IRQ, 0x8000, 0x01FD); |
| 436 |
cpu_irq_ctx_leave(0x01FA); |
| 437 |
check(cpu_irq_depth() == 1, |
| 438 |
"a return matching no frame leaves the depth alone"); |
| 439 |
check(cpu_in_interrupt(), |
| 440 |
"so a live handler is never reported as finished"); |
| 441 |
|
| 442 |
cpu_irq_ctx_leave(0x01FD); |
| 443 |
check(cpu_irq_depth() == 0, "and the real return still closes it"); |
| 444 |
} |
| 445 |
|
| 446 |
if (failures) { |
| 447 |
printf("\nFAILED (%d failures)\n", failures); |
| 448 |
return 1; |
| 449 |
} |
| 450 |
printf("\nPASSED (0 failures)\n"); |
| 451 |
return 0; |
| 452 |
} |