Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Productions > PAL/NTSC detect
2003-01-29 18:35
Dr. Jay
Account closed

Registered: Jan 2003
Posts: 32
PAL/NTSC detect

Just wanted to share a quick routine that detects PAL/NTSC WITHOUT using interrupts or latches.

;pal/NTSC detect - 0 = PAL, non-zero = NTSC

palntsc
sei ; disable interrupts
wait
lda $d012
bne wait ; wait for rasterline 0 or 256
wait1
lda $d011 ; Is rasterbeam in the area
bpl wait1 ; 0-255? if yes, wait
wait2
ldy #$00
synch1 lda $d012
cmp #$37 ; top PAL rasterline
bne synch1
lda $d012 ; if next is 0, then PAL
synch2 cmp $d012
beq synch2
lda $d012
cli ; enable interrupts
rts ; return
 
... 67 posts hidden. Click here to view all posts....
 
2019-05-15 20:13
Silver Dream !

Registered: Nov 2005
Posts: 107
FYI - added a different approach to codebase64 a few days ago. Can save some bytes in specific situations.

http://codebase64.org/doku.php?id=base:efficient_tod_initialisa..
2019-05-15 21:42
TWW

Registered: Jul 2009
Posts: 541
Interesting.

This is the way I detect the frequency (after determining the model):

    // Detect AC Frequency
    // ===================
    // PAL TEST 50Hz:   #$7f
    // PAL TEST 60Hz:   #$32
    // NTSC TEST 50Hz:  #$b3
    // NTSC TEST 60Hz:  #$70
    // NTSC2 TEST 50Hz: #$b3
    // NTSC2 TEST 60Hz: #$70
    // DREAN TEST 50Hz: #$70
    // DREAN TEST 60Hz: #$20

DetectACFrequency:
    lda #$ff
    sta $dc04
    sta $dc05              // CIA #1 Timer A = #$ffff
    sta $dc06
    sta $dc07              // CIA #1 Timer B = #$ffff
    lda #$00
    sta $dc0b
    sta $dc0a
    sta $dc09
    sta $dc08              // CIA #1 TOD set to 00:00:00:00
    ldx #%10000001         // CIA #1 Timer A Start + TOD = 50 Hz
    lda VideoSystem
    beq !+                 // Branch if PAL (use 50Hz)
    cmp #$03
    beq !+                 // Branch if DREAN (use 50Hz)
    ldx #%00000001         // CIA #1 Timer A Start + TOD = 60 Hz (NTSC/NTSC2)
!:  ldy #%01000001         // CIA #1 Timer B Start + Sync with Timer A
    lda $dc08
!:  cmp $dc08
    beq !-                 // Wait untill TOD changes with 1/10 second
    stx $dc0e              // CIA #1 Timer A Start
    sty $dc0f              // CIA #1 Timer B Start
    lda $dc08
!:  cmp $dc08
    beq !-                 // Wait untill TOD changes with 1/10 second
    lda $dc05


    ldx VideoSystem
    beq PALorDREAN
    cpx #$03
    beq PALorDREAN
NTSCorNTSC2:
    cmp Table50Hz,x
    bne TOD50Hz
    jmp TOD60Hz

PALorDREAN:
    cmp Table50Hz,x
    bne TOD60Hz
TOD50Hz:
    lda #$81
    ldx #$80
    ldy #50
    jmp !+
TOD60Hz:
    lda #$01
    ldx #$00
    ldy #60
!:  sta $dc0e
    stx $dd0e  // Set correct frequency on TOD Clocks and initialize CIA #1 & #2 Control Register A
    sty ACMains
    rts

    //---------------------------------------------------------------------
    // DATA: 50/60 Hz Detection Table
    //
    // PAL/50   - #$fffe 7f 28 -  98.519 cycles => MPU Clock = 0,985 MHz
    // PAL/60   - #$fffe 32 24 - 118.235 cycles
    //
    // NTSC/50  - #$fffe b3 0f -  85.232 cycles
    // NTSC/60  - #$fffe 70 7d - 102.274 cycles => MPU Clock = 1,023 MHz
    //
    // NTSC2/50 - #$fffe b3 12 -  85.229 cycles
    // NTSC2/60 - #$fffe 70 6b - 102.292 cycles => MPU Clock = 1,023 MHz
    //
    // DREAN/50 - #$fffe 70 23 - 102.326 cycles => MPU Clock = 1,023 MHz
    // DREAN/60 - #$fffe 20 32 - 122.829 cycles

Table50Hz:  // Values to detect that 
    .byte $7f  // PAL
    .byte $70  // NTSC_OLD
    .byte $70  // NTSC
    .byte $70  // DREAN


Works in all configs of vice using a regular C64 but that 'could' be missguiding ,-)
2019-05-16 16:00
Silver Dream !

Registered: Nov 2005
Posts: 107
I basically needed simple way to properly initialise TOD. Being lazy I had a look at what was available on codebase but found only something I wouldn't want to use for a few reasons. Therefore I fired up a spreadsheet, calculated what was needed and wrote my own. Once done, adding only two CMPs looked like a no-brainer.
2019-05-17 00:12
TWW

Registered: Jul 2009
Posts: 541
Do please share 8-)
2019-05-17 00:26
Krill

Registered: Apr 2002
Posts: 2847
I'm not sure i understand. The aim is to detect the mains frequency regardless of the computer's native video mode? E.g.., be able to detect a PAL C-64 running in NTSC land and vice versa, for some reason? Why? :)
2019-05-17 07:40
soci

Registered: Sep 2003
Posts: 474
Put there a large tolerance (like 4Hz) if it's used for anything important.

It's not like the mains frequency is always the same. The machine could run off generator/ups or some refurbished AT/ATX supply with a 12V inverter. Or someone just stuck in a 555 internally instead of bothering to build that inverter and set the trimmer so that the TOD speed "feels about right".
2019-05-17 11:39
Silver Dream !

Registered: Nov 2005
Posts: 107
Quoting Krill
I'm not sure i understand. The aim is to detect the mains frequency regardless of the computer's native video mode? E.g.., be able to detect a PAL C-64 running in NTSC land and vice versa, for some reason? Why? :)

The aim was neither to detect mains frequency (mains frequency is not necessarily the same as TOD ticks) nor the video norm. It was to detect TOD ticks frequency so that TOD can be set correctly up before being used. An old problem, to which I felt like better approach was needed. Only as a side-effect it can also detect the video norm. Therefore – if your PRG needs both TOD and PAL/NTSC – it's probably the most time/byte efficient approach.
2019-05-17 11:48
Silver Dream !

Registered: Nov 2005
Posts: 107
Quoting soci
Put there a large tolerance (like 4Hz) if it's used for anything important.

It's not like the mains frequency is always the same. The machine could run off generator/ups or some refurbished AT/ATX supply with a 12V inverter. Or someone just stuck in a 555 internally instead of bothering to build that inverter and set the trimmer so that the TOD speed "feels about right".

Mains frequency is expected to be long-term stable. So yes, you are generally right. For something short-term important TOD's neither really accurate nor granular enough. As for what it runs off is a different thing. Surely you can't account for all possible modifications/deviations when you have only binary choice between expected 50 and 60Hz in the chip.
2019-05-17 16:28
Krill

Registered: Apr 2002
Posts: 2847
Quoting Silver Dream !
It was to detect TOD ticks frequency so that TOD can be set correctly up before being used. [...]Only as a side-effect it can also detect the video norm. Therefore – if your PRG needs both TOD and PAL/NTSC – it's probably the most time/byte efficient approach.
I see. Not that it matters much, but i guess with the large tolerances for the mains frequency, your approach would likely misdetect a Drean C-64 as an NTSC machine (see also TWW's implementation). And of course a PAL SX-64 as well.

But i know now that i need to change my TOD setup code to not use the result of the video mode detection. :)
2019-05-17 17:05
Silver Dream !

Registered: Nov 2005
Posts: 107
Quoting Krill
[...] i guess with the large tolerances for the mains frequency, your approach would likely misdetect a Drean C-64 as an NTSC machine (see also TWW's implementation). And of course a PAL SX-64 as well.

I currently don't account for Drean so that's something to verify. Unfortunately I don't have any to test on real h/w. Why OTOH do you think would I misdetect PAL SX-64 (which I tested not to be the case)? The test PRG is here:

https://dl.dropboxusercontent.com/s/6wwli9ghhqvtjqh/test.c64

If somebody wants to take it for a spin then please run it for a prolonged period of time and see if only one line changes. I ran it on all tested machines for many hours (overnight the shortest). If someone has SCPU - please test it there too :-)
Previous - 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
eryngi
Guests online: 141
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Wafer Demo  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Nostalgia  (9.3)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Graphicians
1 Sulevi  (10)
2 Mirage  (9.8)
3 Lobo  (9.7)
4 Mikael  (9.7)
5 Archmage  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.053 sec.