From ad88fefae53a45da679c293072c48576e7c57101 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Fri, 24 May 2024 23:25:13 +0200 Subject: [PATCH] small cleanup --- sw/bootloader/src/boot.c | 2 +- sw/bootloader/src/error.c | 2 +- sw/bootloader/src/exception.S | 4 ++ sw/bootloader/src/exception.c | 2 +- sw/bootloader/src/interrupt.c | 70 ++++++++++++++++++++++------------- sw/bootloader/src/vr4300.h | 3 ++ 6 files changed, 54 insertions(+), 29 deletions(-) diff --git a/sw/bootloader/src/boot.c b/sw/bootloader/src/boot.c index b4f6346..b62608a 100644 --- a/sw/bootloader/src/boot.c +++ b/sw/bootloader/src/boot.c @@ -146,5 +146,5 @@ void boot (boot_params_t *params) { "t3" ); - while (1); + while (true); } diff --git a/sw/bootloader/src/error.c b/sw/bootloader/src/error.c index a739f01..5914acb 100644 --- a/sw/bootloader/src/error.c +++ b/sw/bootloader/src/error.c @@ -19,5 +19,5 @@ void error_display (const char *fmt, ...) { va_end(args); display_printf("\n"); - while (1); + while (true); } diff --git a/sw/bootloader/src/exception.S b/sw/bootloader/src/exception.S index 0276cab..639458d 100644 --- a/sw/bootloader/src/exception.S +++ b/sw/bootloader/src/exception.S @@ -120,6 +120,10 @@ exception_fatal: exception_interrupt: andi $a0, $t0, C0_CR_IP_MASK srl $a0, C0_CR_IP_BIT + mfc0 $t0, C0_STATUS + andi $t0, C0_SR_IM_MASK + srl $t0, C0_SR_IM_BIT + and $a0, $t0 jal exception_interrupt_handler exception_restore: diff --git a/sw/bootloader/src/exception.c b/sw/bootloader/src/exception.c index 52083b7..6bd1988 100644 --- a/sw/bootloader/src/exception.c +++ b/sw/bootloader/src/exception.c @@ -48,5 +48,5 @@ void exception_fatal_handler (uint32_t exception_code, exception_t *e) { display_printf(" t8: 0x%08lX t9: 0x%08lX k0: 0x%08lX k1: 0x%08lX\n", e->t8.u32, e->t9.u32, e->k0.u32, e->k1.u32); display_printf(" gp: 0x%08lX sp: 0x%08lX s8: 0x%08lX ra: 0x%08lX\n\n", e->gp.u32, e->sp.u32, e->s8.u32, e->ra.u32); - while (1); + while (true); } diff --git a/sw/bootloader/src/interrupt.c b/sw/bootloader/src/interrupt.c index d214f5b..15de90d 100644 --- a/sw/bootloader/src/interrupt.c +++ b/sw/bootloader/src/interrupt.c @@ -5,6 +5,7 @@ typedef enum { + INTERRUPT_NONE = 0, INTERRUPT_SW_0 = (1 << 0), INTERRUPT_SW_1 = (1 << 1), INTERRUPT_RCP = (1 << 2), @@ -16,54 +17,71 @@ typedef enum { } interrupt_t; -static void exception_interrupt_unhandled (uint8_t interrupt) { - display_init((uint32_t *) (&assets_sc64_logo_640_240_dimmed)); +void exception_interrupt_handler (uint8_t interrupt) { + if (interrupt == INTERRUPT_NONE) { + display_init((uint32_t *) (&assets_sc64_logo_640_240_dimmed)); - version_print(); - display_printf("[ Unhandled interrupt ]\n"); - display_printf("Pending (0x%02X):\n", interrupt); - for (int i = 0; i < 8; i++) { - switch (interrupt & (1 << i)) { - case INTERRUPT_SW_0: display_printf(" Software interrupt (0)\n"); break; - case INTERRUPT_SW_1: display_printf(" Software interrupt (1)\n"); break; - case INTERRUPT_RCP: display_printf(" RCP interrupt (2)\n"); break; - case INTERRUPT_CART: display_printf(" CART interrupt (3)\n"); break; - case INTERRUPT_PRENMI: display_printf(" Pre NMI interrupt (4)\n"); break; - case INTERRUPT_HW_5: display_printf(" Hardware interrupt (5)\n"); break; - case INTERRUPT_HW_6: display_printf(" Hardware interrupt (6)\n"); break; - case INTERRUPT_TIMER: display_printf(" Timer interrupt (7)\n"); break; - default: break; - } + version_print(); + display_printf("[ Empty interrupt ]\n"); + display_printf("There is no interrupt to handle\n"); + + while (true); } - while (1); -} - - -void exception_interrupt_handler (uint8_t interrupt) { if (interrupt & INTERRUPT_CART) { + interrupt &= ~(INTERRUPT_CART); + sc64_irq_t irq = sc64_irq_pending(); if (irq != SC64_IRQ_NONE) { - return sc64_irq_callback(irq); + sc64_irq_callback(irq); } } if (interrupt & INTERRUPT_PRENMI) { + interrupt &= ~(INTERRUPT_PRENMI); + if (display_ready()) { display_init(NULL); + display_printf("Resetting...\n"); } - while (1); + + while (true); } if (interrupt & INTERRUPT_TIMER) { + interrupt &= ~(INTERRUPT_TIMER); + display_init((uint32_t *) (&assets_sc64_logo_640_240_dimmed)); + version_print(); display_printf("[ Watchdog ]\n"); display_printf("SC64 bootloader did not finish loading in 5 seconds\n"); - while (1); + + while (true); } - exception_interrupt_unhandled(interrupt); + if (interrupt != INTERRUPT_NONE) { + display_init((uint32_t *) (&assets_sc64_logo_640_240_dimmed)); + + version_print(); + display_printf("[ Unhandled interrupt ]\n"); + display_printf("Pending (0x%02X):\n", interrupt); + for (int i = 0; i < 8; i++) { + switch (interrupt & (1 << i)) { + case INTERRUPT_SW_0: display_printf(" (0) Software interrupt\n"); break; + case INTERRUPT_SW_1: display_printf(" (1) Software interrupt\n"); break; + case INTERRUPT_RCP: display_printf(" (2) RCP interrupt\n"); break; + case INTERRUPT_CART: display_printf(" (3) CART interrupt\n"); break; + case INTERRUPT_PRENMI: display_printf(" (4) Pre NMI interrupt\n"); break; + case INTERRUPT_HW_5: display_printf(" (5) Hardware interrupt\n"); break; + case INTERRUPT_HW_6: display_printf(" (6) Hardware interrupt\n"); break; + case INTERRUPT_TIMER: display_printf(" (7) Timer interrupt\n"); break; + default: break; + } + } + + while (true); + } } diff --git a/sw/bootloader/src/vr4300.h b/sw/bootloader/src/vr4300.h index 58b3617..ec932aa 100644 --- a/sw/bootloader/src/vr4300.h +++ b/sw/bootloader/src/vr4300.h @@ -48,6 +48,9 @@ #define C0_SR_CU2 (1 << 30) #define C0_SR_CU3 (1 << 31) +#define C0_SR_IM_MASK (C0_SR_IM7 | C0_SR_IM6 | C0_SR_IM5 | C0_SR_IM4 | C0_SR_IM3 | C0_SR_IM2 | C0_SR_IM1 | C0_SR_IM0) +#define C0_SR_IM_BIT (8) + #define C0_CR_EC0 (1 << 2) #define C0_CR_EC1 (1 << 3)