From 76fae9da82fb1ccad09d7a599ee5d56ec2ef8dc7 Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 20:02:46 +0200 Subject: [PATCH] Add irq handling, PAL/NTSC detection and clock() --- libsrc/atari7800/clock.s | 89 ++++++++++++++++++++++++++++++++++++++++ libsrc/atari7800/irq.s | 36 ++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 libsrc/atari7800/clock.s create mode 100644 libsrc/atari7800/irq.s diff --git a/libsrc/atari7800/clock.s b/libsrc/atari7800/clock.s new file mode 100644 index 000000000..814defceb --- /dev/null +++ b/libsrc/atari7800/clock.s @@ -0,0 +1,89 @@ +; +; 2022-03-15, Karri Kaksonen +; +; clock_t clock (void); +; clock_t _clocks_per_sec (void); +; + + .export _clock, __clocks_per_sec, clock_count + .interruptor update_clock, 2 ; (low priority) + .constructor init_clock + + .import sreg: zp + .import _zonecounter + .import _paldetected + .include "atari7800.inc" + + .macpack generic + + .code + +;----------------------------------------------------------------------------- +; Read the clock counter. +; + .proc _clock + + lda #0 + sta sreg+1 ; Promote 24 bits up to 32 bits + lda clock_count+2 + sta sreg + ldx clock_count+1 + lda clock_count + + rts + .endproc + +;----------------------------------------------------------------------------- +; Return the number of clock ticks in one second. +; + .proc __clocks_per_sec + + lda #0 + tax + sta sreg ; return 32 bits + sta sreg+1 + lda _paldetected + bne pal + lda #60 ; NTSC - 60Hz + rts +pal: + lda #50 ; PAL - 50Hz + rts + .endproc + +;----------------------------------------------------------------------------- +; This interrupt handler increments a 24-bit counter at every video +; vertical-blanking time. +; Update the clock only on interrupt while the drawing on screen is on +; _zonecounter == 1 (from 1st visible scanline to last visible scanline) +; +update_clock: + lda _zonecounter + and #01 + beq @L1 + inc clock_count + bne @L1 + inc clock_count+1 + bne @L1 + inc clock_count+2 +@L1: ;clc ; General interrupt was not reset + rts + +;----------------------------------------------------------------------------- +; Set time to zero at startup +; + .segment "ONCE" +init_clock: + lda #0 + sta clock_count+2 + sta clock_count+1 + sta clock_count + rts + +;----------------------------------------------------------------------------- +; Store time in 3 bytes +; + .bss +clock_count: + .res 3 + diff --git a/libsrc/atari7800/irq.s b/libsrc/atari7800/irq.s new file mode 100644 index 000000000..ed315a1b7 --- /dev/null +++ b/libsrc/atari7800/irq.s @@ -0,0 +1,36 @@ +; +; IRQ handling (Atari 7800 version) +; + + .export initirq, doneirq, IRQStub + + .import __INTERRUPTOR_COUNT__, callirq + + .include "atari7800.inc" + + .code +; ------------------------------------------------------------------------ + +initirq: +doneirq: + rts + +; ------------------------------------------------------------------------ + +IRQStub: + cld ; Just to be sure + pha + lda #<(__INTERRUPTOR_COUNT__ * 2) + beq @L1 + txa + pha + tya + pha + jsr callirq ; Call the functions + pla + tay + pla + tax +@L1: pla + rti +