From 220171d0da0b6c43933994be264853a7f2a9fd3d Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 20:02:46 +0200 Subject: [PATCH 1/9] 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 + From d6009721d42036eeb3ffad610c3e8db28307303f Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 20:02:54 +0200 Subject: [PATCH 2/9] Add irq handling, PAL/NTSC detection and clock() --- libsrc/atari7800/crt0.s | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/libsrc/atari7800/crt0.s b/libsrc/atari7800/crt0.s index 3d53abfa8..bd8a57525 100644 --- a/libsrc/atari7800/crt0.s +++ b/libsrc/atari7800/crt0.s @@ -1,16 +1,17 @@ .export _zonecounter + .export _paldetected .export __STARTUP__ : absolute = 1 .export _exit .import __ROM_START__ .import __RAM3_START__, __RAM3_SIZE__ .import initlib, donelib .import zerobss, copydata + .import IRQStub .import push0, _main + .include "atari7800.inc" .include "zeropage.inc" INPTCTRL = $01 -OFFSET = $38 -CTRL = $3c .segment "STARTUP" start: @@ -38,6 +39,29 @@ start: jsr zerobss jsr initlib + ; Examine machine type as we need the info early +vboff: lda MSTAT + bmi vboff +vbon: lda MSTAT + bpl vbon +vboff2: lda MSTAT + bmi vboff2 + lda #$00 + sta _paldetected + jmp mtsta +count: sta WSYNC + sta WSYNC + dec _paldetected +mtsta: lda MSTAT + bpl count + lda _paldetected + cmp #$78 + bcc mtntsc + lda #$01 + jmp mtpal +mtntsc: lda #$00 +mtpal: sta _paldetected + ; Call main program (pass empty command line) jsr push0 ; argc jsr push0 ; argv @@ -50,7 +74,7 @@ _exit: NMIHandler: inc _zonecounter - rti + jmp IRQStub IRQHandler: rti @@ -58,6 +82,9 @@ IRQHandler: .segment "DATA" _zonecounter: .byte 0 +_paldetected: + .byte 0 ; 0 = NTSC, 1 = PAL + .segment "ENCRYPTION" .res 126, $ff ; Reserved for encryption From c577ec1383e16fa0e178f6b7d1e302ed2eeb7fea Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 21:16:56 +0200 Subject: [PATCH 3/9] Add more space to init segmen --- cfg/atari7800.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cfg/atari7800.cfg b/cfg/atari7800.cfg index 562dd3857..b27353307 100644 --- a/cfg/atari7800.cfg +++ b/cfg/atari7800.cfg @@ -10,7 +10,7 @@ SYMBOLS { __ENCRYPT_BOTTOM__: value = $ff7a, type = export; __ENCRYPT_SIZE__: value = $80, type = export; __MEMORY_TOP__: value = __ENCRYPT_BOTTOM__, type = export; - __INIT_SIZE__: value = 69, type = export; + __INIT_SIZE__: value = 107, type = export; __MEMORY_INIT__: value = __MEMORY_TOP__ - __INIT_SIZE__, type = export; __MEMORY_BOTTOM__: value = $10000 - __CARTSIZE__, type = weak; __FREE_ROM_SIZE__: value = __MEMORY_INIT__ - __MEMORY_BOTTOM__, type = export; From d0d0941a649e392e57fabde0310f2d176ed4224b Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 21:28:35 +0200 Subject: [PATCH 4/9] Add get_tv.s --- libsrc/atari7800/get_tv.s | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libsrc/atari7800/get_tv.s diff --git a/libsrc/atari7800/get_tv.s b/libsrc/atari7800/get_tv.s new file mode 100644 index 000000000..307a5c58d --- /dev/null +++ b/libsrc/atari7800/get_tv.s @@ -0,0 +1,26 @@ +; +; Stefan Haubenthal, 2007-01-21 +; +; unsigned char get_tv (void); +; /* Return the video mode the machine is using */ +; + + .include "atari7800.inc" + .include "get_tv.inc" + .importzp tmp1, tmp2 + .import _paldetected + +;-------------------------------------------------------------------------- +; _get_tv + +.proc _get_tv + + lda _paldetected + beq ntsc + lda #TV::PAL + rts +ntsc: + lda #TV::NTSC + rts + +.endproc From 865be55e66af59aab3366618f9f2bb19439e6ea1 Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 21:29:06 +0200 Subject: [PATCH 5/9] Split clock file to components --- libsrc/atari7800/clock.s | 22 +------------------- libsrc/atari7800/clocks_per_sec.s | 34 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 libsrc/atari7800/clocks_per_sec.s diff --git a/libsrc/atari7800/clock.s b/libsrc/atari7800/clock.s index 814defceb..3b259cfcf 100644 --- a/libsrc/atari7800/clock.s +++ b/libsrc/atari7800/clock.s @@ -2,16 +2,14 @@ ; 2022-03-15, Karri Kaksonen ; ; clock_t clock (void); -; clock_t _clocks_per_sec (void); ; - .export _clock, __clocks_per_sec, clock_count + .export _clock, clock_count .interruptor update_clock, 2 ; (low priority) .constructor init_clock .import sreg: zp .import _zonecounter - .import _paldetected .include "atari7800.inc" .macpack generic @@ -33,24 +31,6 @@ 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. diff --git a/libsrc/atari7800/clocks_per_sec.s b/libsrc/atari7800/clocks_per_sec.s new file mode 100644 index 000000000..dc09c4396 --- /dev/null +++ b/libsrc/atari7800/clocks_per_sec.s @@ -0,0 +1,34 @@ +; +; 2022-03-15, Karri Kaksonen +; +; clock_t _clocks_per_sec (void); +; + + .export __clocks_per_sec + + .import sreg: zp + .import _paldetected + .include "atari7800.inc" + + .macpack generic + + .code + +;----------------------------------------------------------------------------- +; 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 + From 4c3120b93536f1b6020695d7922fc3c797356b65 Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Tue, 22 Mar 2022 21:41:12 +0200 Subject: [PATCH 6/9] Make STARTUP segment larger --- cfg/atari7800.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cfg/atari7800.cfg b/cfg/atari7800.cfg index b27353307..4d20ab0d9 100644 --- a/cfg/atari7800.cfg +++ b/cfg/atari7800.cfg @@ -10,7 +10,7 @@ SYMBOLS { __ENCRYPT_BOTTOM__: value = $ff7a, type = export; __ENCRYPT_SIZE__: value = $80, type = export; __MEMORY_TOP__: value = __ENCRYPT_BOTTOM__, type = export; - __INIT_SIZE__: value = 107, type = export; + __INIT_SIZE__: value = 121, type = export; __MEMORY_INIT__: value = __MEMORY_TOP__ - __INIT_SIZE__, type = export; __MEMORY_BOTTOM__: value = $10000 - __CARTSIZE__, type = weak; __FREE_ROM_SIZE__: value = __MEMORY_INIT__ - __MEMORY_BOTTOM__, type = export; From 8d0b0e6ffcad7d6d63ccfc212e230d4cab484fa1 Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Fri, 25 Mar 2022 14:44:54 +0200 Subject: [PATCH 7/9] Separate machine detection from crt0 and put it in get_tv --- libsrc/atari7800/crt0.s | 27 --------------- libsrc/atari7800/get_tv.s | 69 ++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/libsrc/atari7800/crt0.s b/libsrc/atari7800/crt0.s index bd8a57525..d7cf307ee 100644 --- a/libsrc/atari7800/crt0.s +++ b/libsrc/atari7800/crt0.s @@ -1,5 +1,4 @@ .export _zonecounter - .export _paldetected .export __STARTUP__ : absolute = 1 .export _exit .import __ROM_START__ @@ -39,29 +38,6 @@ start: jsr zerobss jsr initlib - ; Examine machine type as we need the info early -vboff: lda MSTAT - bmi vboff -vbon: lda MSTAT - bpl vbon -vboff2: lda MSTAT - bmi vboff2 - lda #$00 - sta _paldetected - jmp mtsta -count: sta WSYNC - sta WSYNC - dec _paldetected -mtsta: lda MSTAT - bpl count - lda _paldetected - cmp #$78 - bcc mtntsc - lda #$01 - jmp mtpal -mtntsc: lda #$00 -mtpal: sta _paldetected - ; Call main program (pass empty command line) jsr push0 ; argc jsr push0 ; argv @@ -82,9 +58,6 @@ IRQHandler: .segment "DATA" _zonecounter: .byte 0 -_paldetected: - .byte 0 ; 0 = NTSC, 1 = PAL - .segment "ENCRYPTION" .res 126, $ff ; Reserved for encryption diff --git a/libsrc/atari7800/get_tv.s b/libsrc/atari7800/get_tv.s index 307a5c58d..5438b2b95 100644 --- a/libsrc/atari7800/get_tv.s +++ b/libsrc/atari7800/get_tv.s @@ -1,26 +1,63 @@ ; -; Stefan Haubenthal, 2007-01-21 +; Karri Kaksonen, 2022-03-25 ; -; unsigned char get_tv (void); -; /* Return the video mode the machine is using */ +; unsigned char get_tv (void) ; + .include "atari7800.inc" + .export _get_tv - .include "atari7800.inc" - .include "get_tv.inc" - .importzp tmp1, tmp2 - .import _paldetected +.segment "DATA" -;-------------------------------------------------------------------------- -; _get_tv +_paldetected: + .byte $FF -.proc _get_tv +; --------------------------------------------------------------- +; unsigned char get_tv (void) +; --------------------------------------------------------------- - lda _paldetected - beq ntsc - lda #TV::PAL - rts -ntsc: - lda #TV::NTSC +.segment "CODE" + +.proc _get_tv: near + +.segment "CODE" + + ldx #$00 + lda #$FF + cmp _paldetected + bne L8 +L1: lda $0028 + and #$80 + bne L1 +L2: lda $0028 + and #$80 + beq L2 +L3: lda $0028 + and #$80 + bne L3 + lda #$00 + sta M0001 + jmp L5 +L4: sta $0024 + sta $0024 + dec M0001 +L5: lda $0028 + and #$80 + beq L4 + lda M0001 + cmp #$78 + bcc L6 + lda #$00 + jmp L7 +L6: lda #$01 +L7: sta _paldetected + ldx #$00 +L8: lda _paldetected rts +.segment "BSS" + +M0001: + .res 1,$00 + .endproc + From 4a71a1fbb20117b6ad438481d01c228f1943350a Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Fri, 25 Mar 2022 15:02:43 +0200 Subject: [PATCH 8/9] Use proper mnemonics --- libsrc/atari7800/get_tv.s | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/libsrc/atari7800/get_tv.s b/libsrc/atari7800/get_tv.s index 5438b2b95..995b5c652 100644 --- a/libsrc/atari7800/get_tv.s +++ b/libsrc/atari7800/get_tv.s @@ -4,6 +4,7 @@ ; unsigned char get_tv (void) ; .include "atari7800.inc" + .include "get_tv.inc" .export _get_tv .segment "DATA" @@ -25,30 +26,30 @@ _paldetected: lda #$FF cmp _paldetected bne L8 -L1: lda $0028 +L1: lda MSTAT and #$80 bne L1 -L2: lda $0028 +L2: lda MSTAT and #$80 beq L2 -L3: lda $0028 +L3: lda MSTAT and #$80 bne L3 lda #$00 sta M0001 jmp L5 -L4: sta $0024 - sta $0024 +L4: sta MWSYNC + sta MWSYNC dec M0001 -L5: lda $0028 +L5: lda MSTAT and #$80 beq L4 lda M0001 cmp #$78 bcc L6 - lda #$00 + lda #TV::NTSC jmp L7 -L6: lda #$01 +L6: lda #TV::PAL L7: sta _paldetected ldx #$00 L8: lda _paldetected From 5115a1b50f1578fe51b06e66f46d1ec72c364052 Mon Sep 17 00:00:00 2001 From: Karri Kaksonen Date: Fri, 25 Mar 2022 15:08:14 +0200 Subject: [PATCH 9/9] Export paldetected --- libsrc/atari7800/get_tv.s | 1 + 1 file changed, 1 insertion(+) diff --git a/libsrc/atari7800/get_tv.s b/libsrc/atari7800/get_tv.s index 995b5c652..3eb94d54d 100644 --- a/libsrc/atari7800/get_tv.s +++ b/libsrc/atari7800/get_tv.s @@ -6,6 +6,7 @@ .include "atari7800.inc" .include "get_tv.inc" .export _get_tv + .export _paldetected .segment "DATA"