add interrupt init code (and increase rtc tick rate)

This commit is contained in:
Byron Lathi
2023-11-20 22:23:18 -08:00
parent 5e36d0824a
commit 7089b663ca
7 changed files with 244 additions and 230 deletions

View File

@@ -1,51 +0,0 @@
; ---------------------------------------------------------------------------
; interrupt.s
; ---------------------------------------------------------------------------
;
; Interrupt handler.
;
; Checks for a BRK instruction and returns from all valid interrupts.
.import _handle_irq
.import _cputc, _clrscr
.export _irq_int, _nmi_int
.include "io.inc65"
.segment "CODE"
.PC02 ; Force 65C02 assembly mode
; ---------------------------------------------------------------------------
; Non-maskable interrupt (NMI) service routine
_nmi_int: RTI ; Return from all NMI interrupts
; ---------------------------------------------------------------------------
; Maskable interrupt (IRQ) service routine
_irq_int: PHX ; Save X register contents to stack
TSX ; Transfer stack pointer to X
PHA ; Save accumulator contents to stack
INX ; Increment X so it points to the status
INX ; register value saved on the stack
LDA $100,X ; Load status register contents
AND #$10 ; Isolate B status bit
BNE break ; If B = 1, BRK detected
; ---------------------------------------------------------------------------
; IRQ detected, return
irq: PLA ; Restore accumulator contents
PLX ; Restore X register contents
jsr _handle_irq ; Handle the IRQ
RTI ; Return from all IRQ interrupts
; ---------------------------------------------------------------------------
; BRK detected, stop
break:
pla
plx
rti

View File

@@ -14,6 +14,18 @@ RTC_IRQ_THRESHOLD = $20
RTC_OUTPUT = $30
RTC_CONTROL = $30
THRESHOLD_0 = $a0
; THRESHOLD_1 = $0f
THRESHOLD_1 = $00
THRESHOLD_2 = $00
THRESHOLD_3 = $00
; IRQ_THRESHOLD_0 = $32
IRQ_THRESHOLD_0 = $10
IRQ_THRESHOLD_1 = $00
IRQ_THRESHOLD_2 = $00
IRQ_THRESHOLD_3 = $00
; void init_rtc(void);
; Initialize rtc and generate 50ms interrupts
.proc _init_rtc
@@ -36,36 +48,36 @@ RTC_CONTROL = $30
lda #RTC_THRESHOLD+0 ; Set threshold to 4000 ($fa0)
sta RTC_CMD
lda #$a0
lda #THRESHOLD_0
sta RTC_DAT
lda #RTC_THRESHOLD+1
sta RTC_CMD
lda #$0f
lda #THRESHOLD_1
sta RTC_DAT
lda #RTC_THRESHOLD+2
sta RTC_CMD
lda #$00
lda #THRESHOLD_2
sta RTC_DAT
lda #RTC_THRESHOLD+3
sta RTC_CMD
lda #$00
lda #THRESHOLD_3
sta RTC_DAT
lda #RTC_IRQ_THRESHOLD+0 ; Set irq threshold to 50 ($32)
sta RTC_CMD
lda #$32
lda #IRQ_THRESHOLD_0
sta RTC_DAT
lda #RTC_IRQ_THRESHOLD+1
sta RTC_CMD
lda #$00
lda #IRQ_THRESHOLD_1
sta RTC_DAT
lda #RTC_IRQ_THRESHOLD+2
sta RTC_CMD
lda #$00
lda #IRQ_THRESHOLD_2
sta RTC_DAT
lda #RTC_IRQ_THRESHOLD+3
sta RTC_CMD
lda #$00
lda #IRQ_THRESHOLD_3
sta RTC_DAT
lda #$30

View File

@@ -9,6 +9,8 @@
void irq_int();
void nmi_int();
void register_irq(void* addr, uint8_t irqn);
uint8_t irq_get_status();
void irq_set_status(uint8_t);

View File

@@ -0,0 +1,43 @@
.MACPACK generic
.autoimport
.export _irq_int, _nmi_int
.export _register_irq
IRQ_CMD_ADDR = $effc
IRQ_DAT_ADDR = $effd
IRQ_CMD_READIRQ = $00
.proc _nmi_int
rti
.endproc
; _irq_int
.proc _irq_int
; Load IRQ number
lda #IRQ_CMD_READIRQ
sta IRQ_CMD_ADDR
lda IRQ_DAT_ADDR
; shift by 2 (oh so only 128 interrupts are supported lol)
lsr
tax
jmp (irq_table,x)
; use that to index jump table
.endproc
; void register_irq(void* addr, uint8_t irqn);
.proc _register_irq
tax
jsr popa
sta irq_table,x
jsr popa
sta irq_table+1,x
rts
.endproc
.data
; interrupt handler jump table
irq_table: .res 256

View File

@@ -2,7 +2,7 @@
#include <stdint.h>
#include <conio.h>
#include "devices/interrupt.h"
#include "interrupts/interrupt.h"
#include "devices/uart.h"

View File

@@ -1,8 +1,14 @@
#include <conio.h>
#include "devices/interrupt_controller.h"
#include "interrupts/interrupt.h"
#include "devices/rtc.h"
void handle_rtc_interrupt() {
cputs("In IRQ interrupt!\n");
asm volatile ("rti");
}
int main() {
cputs("Kernel\n");
@@ -19,6 +25,8 @@ int main() {
cputs("Initialize RTC\n");
init_rtc();
register_irq(&handle_rtc_interrupt, 0);
// cputs("Initialize Serial\n");
// // init_serial();
// enable_irq(2, IRQ_EDGE);
@@ -26,4 +34,4 @@ int main() {
while(1);
return 0;
}
}