From 79c09d9793033fc4c89dc405c8f24e445cf139f3 Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Fri, 9 Aug 2019 18:40:09 +0200 Subject: [PATCH] set IRQ frequency depending on video system --- STATUS.md | 5 +- c64/kernal/cint.s | 26 +------- c64/kernal/fda3.ioinit.s | 61 ++++++++++++------- c64/kernal/iec/iec_wait20us.s | 2 +- c64/kernal/iec/iec_wait60us.s | 2 +- .../interrupts/fe47.default_nmi_handler.s | 2 + c64/kernal/setup_pal_ntsc.s | 31 ++++++++++ 7 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 c64/kernal/setup_pal_ntsc.s diff --git a/STATUS.md b/STATUS.md index 6c7cdc45..83831a1f 100644 --- a/STATUS.md +++ b/STATUS.md @@ -27,8 +27,7 @@ The following ROM features are currently missing: * tape support * RS-232 support * breaking Kernal routines with RUN/STOP key -* BRK handling -* PAL/NTSC is detected, but for now only startup banner uses the result +* BRK handling; NMI/IRQ handling is incomplete Features currently being worked on: @@ -73,7 +72,7 @@ NOTE: Even the 'DONE' routines won't support features described as missing in on | Address | Name | Status | Remarks | | --------- | :------- | :------: | :------------------------------------------------: | | `$FF81` | `CINT` | DONE | | -| `$FF84` | `IOINIT` | DONE | | +| `$FF84` | `IOINIT` | PARTIAL | CIA initialization incomplete | | `$FF87` | `RAMTAS` | DONE | | | `$FF8A` | `RESTOR` | DONE | | | `$FF8D` | `VECTOR` | DONE | | diff --git a/c64/kernal/cint.s b/c64/kernal/cint.s index 142005c0..3d2ae602 100644 --- a/c64/kernal/cint.s +++ b/c64/kernal/cint.s @@ -14,31 +14,7 @@ cint: cint_brk: ; entry for BRK and STOP+RESTORE - XXX, where should it start? jsr setup_vicii - - ;; Detect video system (PAL/NTSC), use Graham's method, as it's short and reliable - ;; see here: https://codebase64.org/doku.php?id=base:detect_pal_ntsc - -cint_w0: - lda VIC_RASTER -cint_w1: - cmp VIC_RASTER - beq cint_w1 - bmi cint_w0 - - ;; Result in A, if no interrupt happened during the test: - ;; #$37 -> 312 rasterlines, PAL, VIC 6569 - ;; #$06 -> 263 rasterlines, NTSC, VIC 6567R8 - ;; #$05 -> 262 rasterlines, NTSC, VIC 6567R56A - - cmp #$07 - bcs cint_pal -cint_ntsc: - lda #$00 - beq + -cint_pal: - lda #$01 -* - sta PALNTSC + jsr setup_pal_ntsc ;; Initialise cursor blink flags (Compute's Mapping the 64 p215) lda #$00 diff --git a/c64/kernal/fda3.ioinit.s b/c64/kernal/fda3.ioinit.s index 76ec2ada..fb4a159c 100644 --- a/c64/kernal/fda3.ioinit.s +++ b/c64/kernal/fda3.ioinit.s @@ -34,35 +34,54 @@ ioinit: cpy #$80 ; $20 * amount of chips to silence, $80 to silence 4 chips bne - - ;; Enable CIA1 IRQ and ~50Hz timer - ;; (https://csdb.dk/forums/?roomid=11&topicid=69037) - lda #$7f - sta $DC0D ; First disable all + ;; XXX: calibrate TOD for both CIA's, see here: https://codebase64.org/doku.php?id=base:efficient_tod_initialisation + + ;; Enable CIA1 IRQ and ~50Hz timer (https://csdb.dk/forums/?roomid=11&topicid=69037) + lda #$7F + sta CIA1_ICR ; disable all ;; Set timer interval to ~1/60th of a second - ;; (This value was calculated by running a custom IRQ haandler on a C64 + jsr setup_pal_ntsc + + ;; (This value was calculated by running a custom IRQ handler on a C64 ;; with original KERNAL, and writing the values of $DC04/5 to the screen ;; in the IRQ handler to see roughly what value the timers must be set to) - lda #<16380 - sta $dc04 - lda #>16380 - sta $dc05 + ;; ldy #<16380 + ;; ldx #>16380 + + ;; PAL C64 (https://codebase64.org/doku.php?id=base:cpu_clocking), + ;; is clocked at 0.985248 MHz, so that 1/60s is 16421 CPU cycles + + ldy #<16421 + ldx #>16421 + + lda PALNTSC + bne + + + ;; NTSC C64 (https://codebase64.org/doku.php?id=base:cpu_clocking), + ;; is clocked at 1.022727 MHz, so that 1/60s is 17045 CPU cycles + + ldy #<17045 + ldx #>17045 + + ;; Store the IRQ timing constant to CIA chip +* + sty CIA1_TIMALO + stx CIA1_TIMAHI ;; Enable timer interrupt lda #$81 - sta $dc0d + sta CIA1_ICR - ;; Enable timer A to run continuously - ;; (http://codebase64.org/doku.php?id=base:timerinterrupts) + ;; Enable timer A to run continuously (http://codebase64.org/doku.php?id=base:timerinterrupts) lda #$11 - sta $dc0e - lda $dc0d + sta CIA1_CRA + lda CIA1_ICR ;; Set DDR on CIA2 for IEC bus, VIC-II banking - lda #$3b - sta $dd02 - - ;; Set IEC bus oto its initial idle state - jsr iec_set_idle - - rts + lda #$3B + sta CIA2_DDRA + + ;; Set IEC bus to its initial idle state + jmp iec_set_idle + diff --git a/c64/kernal/iec/iec_wait20us.s b/c64/kernal/iec/iec_wait20us.s index 3ea1349f..819830cc 100644 --- a/c64/kernal/iec/iec_wait20us.s +++ b/c64/kernal/iec/iec_wait20us.s @@ -6,7 +6,7 @@ iec_wait20us: ;; PAL: ;; - CPU frequency 0.985248 MHz, we need to waste at least 20 cycles ;; NTSC: - ;; - CPU frequency 1.022727 MHZ, we need to waste at least 21 cycles + ;; - CPU frequency 1.022727 MHz, we need to waste at least 21 cycles ;; Waste cycles by reading some unimportant memory location ;; in order not to clutter VICE debug logs diff --git a/c64/kernal/iec/iec_wait60us.s b/c64/kernal/iec/iec_wait60us.s index 0f6cd957..90501a5e 100644 --- a/c64/kernal/iec/iec_wait60us.s +++ b/c64/kernal/iec/iec_wait60us.s @@ -6,7 +6,7 @@ iec_wait60us: ;; PAL: ;; - CPU frequency 0.985248 MHz, we need to waste at least 60 cycles ;; NTSC: - ;; - CPU frequency 1.022727 MHZ, we need to waste at least 62 cycles + ;; - CPU frequency 1.022727 MHz, we need to waste at least 62 cycles ;; Waste cycles in a loop diff --git a/c64/kernal/interrupts/fe47.default_nmi_handler.s b/c64/kernal/interrupts/fe47.default_nmi_handler.s index 0d0a0ea1..8814ac2c 100644 --- a/c64/kernal/interrupts/fe47.default_nmi_handler.s +++ b/c64/kernal/interrupts/fe47.default_nmi_handler.s @@ -13,6 +13,8 @@ default_nmi_handler: ;; XXX: RS-232 support is not implemented + ;; XXX confirm NMIs from CIA, or no other NMI will arrive! + jsr cartridge_check bcc + jmp (ICART_WARM_START) diff --git a/c64/kernal/setup_pal_ntsc.s b/c64/kernal/setup_pal_ntsc.s new file mode 100644 index 00000000..f0cdd3a0 --- /dev/null +++ b/c64/kernal/setup_pal_ntsc.s @@ -0,0 +1,31 @@ + +;; +;; Detect video system (PAL/NTSC), use Graham's method, as it's short and reliable +;; see here: https://codebase64.org/doku.php?id=base:detect_pal_ntsc +;; + +setup_pal_ntsc: + + lda VIC_RASTER +setup_pal_ntsc_w1: + cmp VIC_RASTER + beq setup_pal_ntsc_w1 + bmi setup_pal_ntsc + + ;; Result in A, if no interrupt happened during the test: + ;; #$37 -> 312 rasterlines, PAL, VIC 6569 + ;; #$06 -> 263 rasterlines, NTSC, VIC 6567R8 + ;; #$05 -> 262 rasterlines, NTSC, VIC 6567R56A + + cmp #$07 + bcs setup_pal + +setup_ntsc: + lda #$00 + beq + + +setup_pal: + lda #$01 +* + sta PALNTSC + rts