From 9751efd22cec8bc1d7f227e91d5e5172337b276d Mon Sep 17 00:00:00 2001 From: Kyle Cardoza Date: Sat, 30 Mar 2024 07:12:35 -0400 Subject: [PATCH] work on interrupts --- config/memory.scm | 2 +- config/sdk-config.mk | 1 + include/interrupts.h | 26 --------- include/macros.h | 4 ++ src/cop_handler.s | 46 ++++++++++++++++ src/interrupts.c | 113 -------------------------------------- src/interrupts.s | 107 ++++++++++++++++++++++++++++++++++++ src/interrupts2.s | 125 ------------------------------------------- src/main.c | 6 +++ 9 files changed, 165 insertions(+), 265 deletions(-) create mode 100644 src/cop_handler.s delete mode 100644 src/interrupts.c create mode 100644 src/interrupts.s delete mode 100644 src/interrupts2.s diff --git a/config/memory.scm b/config/memory.scm index d9b111a..0688c3f 100644 --- a/config/memory.scm +++ b/config/memory.scm @@ -25,7 +25,7 @@ ) (memory MidRAM - (address (#x00F000 . #x00FDFF)) + (address (#x00F000 . #x00FFFF)) (section code) (section cnear) (section reset) diff --git a/config/sdk-config.mk b/config/sdk-config.mk index 1a269ce..12b1b54 100644 --- a/config/sdk-config.mk +++ b/config/sdk-config.mk @@ -37,6 +37,7 @@ CFLAGS += --debug \ -Wno-gnu-case-range \ -Wimplicit-fallthrough \ -Wno-gnu-folding-constant + ASFLAGS += --debug endif diff --git a/include/interrupts.h b/include/interrupts.h index 640a21b..e98b21f 100644 --- a/include/interrupts.h +++ b/include/interrupts.h @@ -9,29 +9,3 @@ #include "config.h" extern void interrupts_init(void); - -void set_user_nmi_handler(void (*f)(void)); - -void set_user_brk_handler(void (*f)(void)); - -void set_user_cop_handler(void (*f)(void)); - -void set_user_irq_handler(void (*f)(void)); - -void set_user_vera_irq_handler(void (*f)(void)); - -void set_user_t7_handler(void (*f)(void)); - -void set_user_t6_handler(void (*f)(void)); - -void set_user_t5_handler(void (*f)(void)); - -void set_user_t4_handler(void (*f)(void)); - -void set_user_t3_handler(void (*f)(void)); - -void set_user_t2_handler(void (*f)(void)); - -void set_user_t1_handler(void (*f)(void)); - -void set_user_t0_handler(void (*f)(void)); diff --git a/include/macros.h b/include/macros.h index a4bece1..c0be600 100644 --- a/include/macros.h +++ b/include/macros.h @@ -7,6 +7,10 @@ #define long_a rep #0b00100000 +#define short_i sep #0b00010000 + +#define long_i rep #0b00010000 + #ifdef __CALYPSI_CODE_MODEL_SMALL__ #define libcode code diff --git a/src/cop_handler.s b/src/cop_handler.s new file mode 100644 index 0000000..25adab0 --- /dev/null +++ b/src/cop_handler.s @@ -0,0 +1,46 @@ +;;***************************************************************************** +;; Sentinel 65X Kernel +;; +;; src/cop_handler.s +;;***************************************************************************** + +#include "macros.h" + + .section code + + .public cop_handler_native + +cop_handler_native: + phb + phd + rep #0b00110000 + sta long:tmp_register_a + tsc + sta long:external_stack_pointer + lda long:internal_stack_pointer + tcs + lda long:tmp_register_a + + ; When we get here, the X register contains the function number. + ; A and Y may or may not contain parameters. None of those are + ; expected to be preserved. The job here is to ID the function + ; being called, marshall any parameters, and call it. + + tsc + sta long:internal_stack_pointer + lda long:external_stack_pointer + tcs + pld + plb + rti + + .section near + +tmp_register_a: + .space 2 + +external_stack_pointer: + .space 2 + +internal_stack_pointer: + .space 2 diff --git a/src/interrupts.c b/src/interrupts.c deleted file mode 100644 index 12dc81a..0000000 --- a/src/interrupts.c +++ /dev/null @@ -1,113 +0,0 @@ -//***************************************************************************** -// Sentinel 65X Kernel -// -// src/interrupts.c -//***************************************************************************** - -#include - -#include "config.h" - -static void (*user_nmi_handler)(void) = NULL; - -static void (*user_brk_handler)(void) = NULL; - -static void (*user_cop_handler)(void) = NULL; - -static void (*user_irq_handler)(void) = NULL; - -static void (*user_vera_irq_handler)(void) = NULL; - -static void (*user_t7_handler)(void) = NULL; - -static void (*user_t6_handler)(void) = NULL; - -static void (*user_t5_handler)(void) = NULL; - -static void (*user_t4_handler)(void) = NULL; - -static void (*user_t3_handler)(void) = NULL; - -static void (*user_t2_handler)(void) = NULL; - -static void (*user_t1_handler)(void) = NULL; - -static void (*user_t0_handler)(void) = NULL; - -void -set_user_nmi_handler(void (*f)(void)) -{ - user_nmi_handler = f; -} - -void -set_user_brk_handler(void (*f)(void)) -{ - user_brk_handler = f; -} - -void -set_user_cop_handler(void (*f)(void)) -{ - user_cop_handler = f; -} - -void -set_user_irq_handler(void (*f)(void)) -{ - user_irq_handler = f; -} - -void -set_user_vera_irq_handler(void (*f)(void)) -{ - user_vera_irq_handler = f; -} - -void -set_user_t7_handler(void (*f)(void)) -{ - user_t7_handler = f; -} - -void -set_user_t6_handler(void (*f)(void)) -{ - user_t6_handler = f; -} - -void -set_user_t5_handler(void (*f)(void)) -{ - user_t5_handler = f; -} - -void -set_user_t4_handler(void (*f)(void)) -{ - user_t4_handler = f; -} - -void -set_user_t3_handler(void (*f)(void)) -{ - user_t3_handler = f; -} - -void -set_user_t2_handler(void (*f)(void)) -{ - user_t2_handler = f; -} - -void -set_user_t1_handler(void (*f)(void)) -{ - user_t1_handler = f; -} - -void -set_user_t0_handler(void (*f)(void)) -{ - user_t0_handler = f; -} diff --git a/src/interrupts.s b/src/interrupts.s new file mode 100644 index 0000000..587b697 --- /dev/null +++ b/src/interrupts.s @@ -0,0 +1,107 @@ +;;***************************************************************************** +;; Sentinel 65X Kernel +;; +;; src/interrupts.s +;;***************************************************************************** + +#include "macros.h" + +; ----------------------------------------------------------------------------- + +irq_enter .macro + phb + phd + rep #0b00110000 + sta long:tmp_register_a + tsc + sta long:external_stack_pointer + lda long:internal_stack_pointer + tcs + lda long:tmp_register_a + .endm + +irq_exit .macro + tsc + sta long:internal_stack_pointer + lda long:external_stack_pointer + tcs + pld + plb + rti + .endm + +; ----------------------------------------------------------------------------- + + .section near + +tmp_register_a: + .space 2 + +external_stack_pointer: + .space 2 + +internal_stack_pointer: + .space 2 + +; ----------------------------------------------------------------------------- + + .section code + + .public interrupts_init + +; ----------------------------------------------------------------------------- + +interrupts_init: + long_a + sei + + lda #.near(cop_handler_native) + sta 0x00FFB4 + + rts + +; ----------------------------------------------------------------------------- + +cop_handler_native: + irq_enter + + ; When we get here, the X register contains the function number. + ; A and Y may or may not contain parameters. None of those are + ; expected to be preserved. The job here is just to jump to the + ; right handler for the particular function being called, without + ; modifying anything but the X register. + + ; First, we multiply the function number by 2, to get the jump + ; table offset. + pha + txa + asl a + tax + pla + + ; Now, we do an indirect, indexed jump through the jump table. + jsr (cop_jump_table,x) + irq_exit + +cop_jump_table: + .word .near(cop_reset) + .word .near(cop_get_version) + +; ----------------------------------------------------------------------------- + +cop_reset: + rts + +; ----------------------------------------------------------------------------- + + .extern version_major, version_minor, version_micro + +cop_get_version: + lda long:version_micro + tay + lda long:version_minor + tax + lda long:version_major + rts + +; ----------------------------------------------------------------------------- diff --git a/src/interrupts2.s b/src/interrupts2.s deleted file mode 100644 index 402a36a..0000000 --- a/src/interrupts2.s +++ /dev/null @@ -1,125 +0,0 @@ -;;***************************************************************************** -;; Sentinel 65X Kernel -;; -;; src/interrupts2.s -;;***************************************************************************** - -#include "macros.h" - - .section code - - .public interrupts_init - - .extern _Dp - .extern __program_start - -;------------------------------------------------------------------------------ - -interrupts_init: - sei - - ; 16 bit a/x - rep #0b00110000 - - ; Set the vector for the IRQ handler. - lda #.near(irq_handler) - sta 0x00FF9E - - ; Set the vector for the NMI handler. - lda #.near(nmi_handler) - sta 0x00FF9E - - ; Set the vector for the COP handler. - lda #.near(cop_handler) - sta 0x00FFB4 - - ; Set the vector for the VERA IRQ handler. - lda #.near(vera_irq_handler) - sta 0x00FF98 - - rts - -;------------------------------------------------------------------------------ - -common_irq_return: - ; 16 bit a/x - rep #0b00110000 - ply - plx - pla - pld - plb - rti - -;------------------------------------------------------------------------------ - -irq_handler: - phb - phd - ; 16 bit a/x - rep #0b00110000 - pha - phx - phy - - bra common_irq_return - -;------------------------------------------------------------------------------ - -vera_irq_handler: - phb - phd - ; 16 bit a/x - rep #0b00110000 - pha - phx - phy - - bra common_irq_return - -;------------------------------------------------------------------------------ - -nmi_handler: - phb - phd - ; 16 bit a/x - rep #0b00110000 - pha - phx - phy - - bra common_irq_return - -;------------------------------------------------------------------------------ - -cop_handler: - phb - phd - ; 16 bit a/x - rep #0b00110000 - pha - phx - phy - - ; COP dispatch here - ; Function number comes in X, gets multiplied by 2, then - ; used as an index to the jump table. - pha - txa - asl a - tax - pla - jmp (cop_dispatch_table,x) - -cop_dispatch_table: - .word .near(cop_reset) - -;------------------------------------------------------------------------------ - -cop_reset: - sei - - ; 16 bit a/x - rep #0b00110000 - - jmp long:__program_start diff --git a/src/main.c b/src/main.c index b524cf0..6c7687f 100644 --- a/src/main.c +++ b/src/main.c @@ -4,11 +4,17 @@ // src/main.c //***************************************************************************** +#include + #include "config.h" #include "interrupts.h" #include "drivers/vera.h" +const uint16_t version_major = 0; +const uint16_t version_minor = 1; +const uint16_t version_micro = 1; + void main(void) {