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-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 :-)
2019-05-17 18:22
Krill

Registered: Apr 2002
Posts: 2847
Sorry, brainfart, scratch the "of course". But i'm still not so sure about PAL SX-64, as
$7f4a for 60Hz TOD clock and 985248.444 CPU/CIA clock
$70a6 for 60Hz TOD clock and 1022727.14 CPU/CIA clock
the two values are pretty close. What are the cycle count ranges assuming a mains frequency tolerance of 4 Hz, i.e., with 56-64 Hz? Too lazy to do the maths now. :)
2019-05-17 18:34
Krill

Registered: Apr 2002
Posts: 2847
Okay, 4 Hz is way too much. Usual deviations in Europe are 0.2 Hz, i guess it's similar in NTSC land. I haven't found any number yet regarding the March 2018 low point in Europe due to some conflict in the Balkans, though. :)
2019-05-18 07:49
Krill

Registered: Apr 2002
Posts: 2847
Quoting Silver Dream !
I currently don't account for Drean so that's something to verify.
Maybe, you should also account for no AC at all. AFAIK, that's a thing with broken or otherwise weird PSUs. TOD is frozen but C-64 runs normally otherwise. Your current implementation would block and never return.
2019-05-18 13:06
soci

Registered: Sep 2003
Posts: 474
That routine is not safe, as Krill mentioned.

I've already encountered such "lockups" with some productions which prompted for the 555 installations in my C64/C128s. But at least I bothered to measure and set them close to 50 Hz ;)

So to set up the TOD 50/60 Hz prescaler I'd do the following instead:
tod_calibrate	php
                sei
		lda $dc0e
		ora #$80  ; set 50 Hz
		sta $dc0e
		lda $dc08 ; start TOD
		sta $dc08

		jsr measure
		jsr measure
		cpy #211  ; decision value
		bcs pal
		lda $dc0e
		eor #$80  ; change to 60Hz
		sta $dc0e
pal		plp
		rts

		.page        ; must not cross page   
measure		ldy #0
		lda $dc08
lp2		ldx #37
lp		cmp $dc08
		bne done
		dex
		bne lp
		iny
		bne lp2
done		rts
		.endp

This works both in case the screen is off without sprites (best case) and with screen on 8 y expanded sprites (worst case).

Video system detection is out of scope, there's too much noise.

This works by measuring cycle counts between TOD ticks. The calibration exits even if no ticks are present.

Best case estimated cycle numbers:
 82104  cycle - PAL/60
 85227  cycle - NTSC/60
 85286  cycle - DREAN/60

 98524  cycle - PAL/50
102273  cycle - NTSC/50
102344  cycle - DREAN/50

Worst case estimated cycle numbers (effective):
74280 PAL/60
75707 NTSC2/60
75889 NTSC/60
77409 DREAN/60

89136 PAL/50
90849 NTSC2/50
91067 NTSC/50
92891 DREAN/50

The decision point was chosen to be 87211 (between DREAN/60 and PAL/50), which is 211 after division.

It is assumed that the TOD frequency is within 1 Hz of 50/60 Hz for this to work.

The frequency margin can be widened if it's ensured that the measurement is always done in the best case scenario (screen and sprites off). Then 222 can be used as a decision point which allows for a 3 Hz frequency deviation.
2019-05-18 13:10
Oswald

Registered: Apr 2002
Posts: 5020
thats interesting so you can basicly measure the AC freq with the help of the TOD clock ?
2019-05-18 13:30
Krill

Registered: Apr 2002
Posts: 2847
Yes, you determine TOD update rate using the system's crystal clock frequency as reference.
2019-05-18 22:18
soci

Registered: Sep 2003
Posts: 474
It's better to use the horizontal retrace frequency as reference clock as it's more consistent than cycle counts.

This has the advantage that it works on SuperCPU and that it can handle a 3 Hz frequency deviation even with screen on.
tod_calibrate   php
		sei
		lda $dc0e
		ora #$80		; set 50 Hz
		sta $dc0e
		lda $dc08		; start TOD
		sta $dc08

		jsr measure
		jsr measure
		cpy #204		; decision value
		bcs pal
		lda $dc0e
		eor #$80		; change to 60Hz
		sta $dc0e
pal		plp
		rts

measure		ldy #0
		lda $dc08
lp2		pha
		lda #64
lp		cpx $d012
		beq lp
		ldx $d012
		lsr a
		bne lp
		pla
		cmp $dc08
		bne done
		iny
		bne lp2
done		rts
2019-05-19 08:13
soci

Registered: Sep 2003
Posts: 474
Shortened version:
http://codebase64.org/doku.php?id=base:tod_calibration

The other 2 solutions on codebase hang on DC only, but whatever.

Btw. this is one of the releases which fails to start without TOD ticks:
VSP Lab V1.1
Previous - 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 - 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
Pushead/Hokuto Force
syntaxerror
Thunder.Bird/HF/MYD!..
sachy
csabanw
Henne
A3/AFL
Guests online: 145
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 Musicians
1 Vincenzo  (9.8)
2 Rob Hubbard  (9.7)
3 Stinsen  (9.7)
4 Jeroen Tel  (9.6)
5 Linus  (9.6)

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