Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user Harvey ! (Registered 2024-11-25) 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 07:40
soci

Registered: Sep 2003
Posts: 479
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: 108
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: 108
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: 2969
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: 108
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: 2969
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: 2969
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: 2969
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: 479
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: 5086
thats interesting so you can basicly measure the AC freq with the help of the TOD clock ?
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
Marq/Fit^Lieves!Tuor..
Scooby/G★P/Light
Alakran_64
JackAsser/Booze Design
kbs/Pht/Lxt
sachy/BOOM!
grass/LETHARGY
CopAss/Leader
Mike
celticdesign/G★P/M..
Guests online: 124
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Mojo  (9.6)
6 The Demo Coder  (9.6)
7 What Is The Matrix 2  (9.6)
8 Uncensored  (9.6)
9 Wonderland XIV  (9.6)
10 Comaland 100%  (9.6)
Top onefile Demos
1 Layers  (9.6)
2 Party Elk 2  (9.6)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.6)
5 Libertongo  (9.5)
6 Rainbow Connection  (9.5)
7 Onscreen 5k  (9.5)
8 Morph  (9.5)
9 Dawnfall V1.1  (9.5)
10 It's More Fun to Com..  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Nostalgia  (9.3)
5 Triad  (9.2)
Top Crackers
1 Mr. Z  (9.9)
2 Antitrack  (9.8)
3 OTD  (9.8)
4 Fungus  (9.8)
5 S!R  (9.8)

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