Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user maak ! (Registered 2024-04-18) 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
2003-01-29 20:02
Perff
Administrator

Posts: 1665
I know it's not in the 'hardcore' coding-spirit, but isn't it easier just to ask the system?

One thing you could do is check the IRQ-timer which is different for PAL & NTSC even though I can't remember the values.
This ofcourse only work is you come straight from basic, and/or nothing has tampered with the IRQ-timing.

Another thing is to check the PAL/NTSC variable.
The address $02A6 is 0 for NTSC and 1 for PAL set by the ROM but this again could be overwritten by something.
But the ROM can't. In the ROM somwhere I think it says something like:
lda #$01
sta $02A6
for PAL and lda #$00 for NTSC. Why not just check that? (yes. It might not be at the same location in each ROM-version)

I'm not 100% sure of this information though. Just something I think I remember. Please correct me if I'm wrong. :)

/Perff
2003-01-30 11:38
Ninja

Registered: Jan 2002
Posts: 404
Perff: There is no PAL or NTSC-specific ROM. So any ROM checks for PAL/NTSC (counting rasterlines, see $FF5E...) and sets $02A6 depending on the result. And while we are just at it: Checking $02A6 is a poor way of detecting IMHO, because this location can easily be messed up. Those few bytes for a true PAL/NTSC-detection do not hurt, I think.

Dr Jay: What about this?

...
bne synch1
lda $d011
cli
rts

Bit 7 is set when PAL and cleared when NTSC. And I guess the LDY #$00 can be removed, too.
2003-01-30 11:48
Dr. Jay
Account closed

Registered: Jan 2003
Posts: 32
LOL. I saw the LDY after I posted it ... artifact from an earlier attempt. Yes, I like the LDA $D011 ... much more elegant. And as for the ROM issue ... everything I've come to understand says that checking the $02a6 etc is unreliable as different versions, mods, carts, etc. may change this ... but I'm stuck in NTSC-land with a 64C so I haven't had the opportunity to try this on many other stock machines.

2003-01-30 13:21
Ninja

Registered: Jan 2002
Posts: 404
I think the main problem with $02A6 is, that it is set after a reset only. So, load some scene-production which doesn't care about KERNAL-variables and maybe exits with a plain RTS, and $02A6 is as reliable as your house-number :)
Also, the KERNAL-method does not work with accelerators.

False detections because of $02A6 are really annoying; especially as PAL/NTSC-checks are public and easy to implement.

So much for the preaching... ;)
2003-01-30 13:29
Perff
Administrator

Posts: 1665
ninjadrm: Ok sorry for my "lame" idea. I just thought I remembered that somewhere in the ROM it said lda #$X sta $02A6 and thought of that as a 100% way to determin PAL/NTSC

Well.. I was wrong. You're right. Sorry. :)
2003-01-30 13:47
Ninja

Registered: Jan 2002
Posts: 404
Perff: No need to be sorry or to excuse for a "lame" idea; it was just meant to be constructive criticism :)
2003-02-02 22:34
Graham
Account closed

Registered: Dec 2002
Posts: 990
do NOT use the pal/ntsc flag since it is unreliable, even without any kind of extensions. the best way is really to check if certain rasterlines exist or not.
2009-02-01 13:02
AlexC

Registered: Jan 2008
Posts: 293
Quote: do NOT use the pal/ntsc flag since it is unreliable, even without any kind of extensions. the best way is really to check if certain rasterlines exist or not.


Does it mean that original ROM can set this flag wrongly? Haven't studied this part of ROM but I remember that the detection routine used is quite short.
2009-02-01 15:10
chatGPZ

Registered: Dec 2001
Posts: 11092
yes, the rom routine has a flaw that sometimes makes it report ntsc when the machine is infact pal. (atleast thats how i remember it =D)
2009-02-03 20:57
AlexC

Registered: Jan 2008
Posts: 293
Quote: yes, the rom routine has a flaw that sometimes makes it report ntsc when the machine is infact pal. (atleast thats how i remember it =D)

Thanks for clearing it up for me. I need to get NTSC machine to experiment a bit.
2009-02-14 12:15
Devia

Registered: Oct 2004
Posts: 401
So the shortest/fastes routine for reliable detection would be something like:

PALNTSC
	sei
	lda	$01
	pha
	lda	#<INT_NMI
	sta	$fffa
	lda	#>INT_NMI
	sta	$fffb
	lda	#$35
	sta	$01

;---important stuff start
:	bit	$d011
	bpl	:-
:	lda	$d012
	cmp	#<263
	bcs	:+	;Result >= 0 then PAL
	bit	$d011
	bmi	:-
	clc		;NTSC	
:
;---important stuff end

	pla
	sta	$01
	rts

INT_NMI
	rti


2009-02-14 12:25
tlr

Registered: Sep 2003
Posts: 1702
I came to basically the same conclusion:
http://codebase64.org/doku.php?id=bae:dtv_detect (scroll down)
2009-02-14 13:54
Devia

Registered: Oct 2004
Posts: 401
ahh... The first lda $d011, bmi *-3 seems obsolete, though - unless I'm missing something?
Is there any particular reason for waiting for line 288?

2009-02-14 14:24
TNT
Account closed

Registered: Oct 2004
Posts: 189
Quote: ahh... The first lda $d011, bmi *-3 seems obsolete, though - unless I'm missing something?
Is there any particular reason for waiting for line 288?



2009-02-14 17:15
Frantic

Registered: Mar 2003
Posts: 1627
Just wanted to say that TLR missed one letter in his link to codebase... the link is:

http://codebase64.org/doku.php?id=base:dtv_detect

Also, don't hesitate to update the current NTSC/PAL detection routine if you come up something better:

http://codebase64.org/doku.php?id=base:detect_pal_ntsc
2009-02-14 17:37
tlr

Registered: Sep 2003
Posts: 1702
Quote: ahh... The first lda $d011, bmi *-3 seems obsolete, though - unless I'm missing something?
Is there any particular reason for waiting for line 288?



So what happens if your version starts within 6 cycles of the raster counter wrapping to 0? ;)

288 is just about halfway between the max number of raster lines on PAL and NTSC respectively.
2009-02-14 21:22
Devia

Registered: Oct 2004
Posts: 401
Quote: So what happens if your version starts within 6 cycles of the raster counter wrapping to 0? ;)

288 is just about halfway between the max number of raster lines on PAL and NTSC respectively.


Well in that case it's apparently an NTSC machine ;-)

So, yes..

:	bit	$d011
	bmi	:-
:	bit	$d011
	bpl	:-
:	lda	$d012
	cmp	#<263
	bcs	:+	;Result >= 0 then PAL
	bit	$d011
	bmi	:-
	clc		;NTSC	
:


Any more pitfalls then?
2009-02-15 13:41
TNT
Account closed

Registered: Oct 2004
Posts: 189
WTF happened to my post above? Well, tlr pointed out the need for extra check.

Can anyone find something wrong with this approach:
.w	ldx	$d012
	cpx	$d012
	beq	*-3
	lda	$d012
	bne	.w
	inx
	beq	.w
	cpx	#20

2009-02-15 16:35
tlr

Registered: Sep 2003
Posts: 1702
Quote: WTF happened to my post above? Well, tlr pointed out the need for extra check.

Can anyone find something wrong with this approach:
.w	ldx	$d012
	cpx	$d012
	beq	*-3
	lda	$d012
	bne	.w
	inx
	beq	.w
	cpx	#20



Nice approach using just $d012 to avoid the race condition!
I think that will work.
2009-02-15 17:03
j0x

Registered: Mar 2004
Posts: 215
l1 lda $d012
l2 cmp $d012
   beq l2
   bmi l1
   cmp #$20
   bcc ntsc


2009-02-15 17:16
tlr

Registered: Sep 2003
Posts: 1702
Quote:
l1 lda $d012
l2 cmp $d012
   beq l2
   bmi l1
   cmp #$20
   bcc ntsc




12 bytes. Very impressive!
2009-02-15 22:00
Devia

Registered: Oct 2004
Posts: 401
Quote:
l1 lda $d012
l2 cmp $d012
   beq l2
   bmi l1
   cmp #$20
   bcc ntsc




That's brilliant.
2009-02-16 09:02
Frantic

Registered: Mar 2003
Posts: 1627
I put it on codebase:

http://codebase64.org/doku.php?id=base:detect_pal_ntsc

I also wonder... does Ninja's older routine have any benefits over and above this short 12 byte routine? (Didn't have any time/energy to look into it myself now, sorry.) If not, I guess it could just as well be removed.

//FTC
2009-02-16 12:54
Devia

Registered: Oct 2004
Posts: 401
You still need to catch NMI and disable IRQ for it to be completely reliable - and the 12 bytes is without the bcc in the end, which may be obsolete depending on how you implement it.


2009-02-16 14:28
Ninja

Registered: Jan 2002
Posts: 404
Frantic:

Technically, my routine does not have an advantage. From an educational point of view, it has (it was supplement code to an article after all) :) If you don't know the topic, then it surely helps to read my routine first, before you use the other (probably with disabling interrupts there, too).
2009-02-16 20:32
Frantic

Registered: Mar 2003
Posts: 1627
Okok... Thanks for the information. In fact, I added the whole article to the page, since I guess it might add something of value to someone out there. (Just tell me if you mind...)
2009-02-16 20:49
Kickback

Registered: Apr 2004
Posts: 97
Actually this helps me BIGTIME. Because I always thought that the PAL/NTSC register was the way to go.

Thanks guys for the information, helps me with NTSc/PAL intros ;-)
2009-02-17 07:58
chatGPZ

Registered: Dec 2001
Posts: 11092
Quote:
PAL/NTSC register


hu? there is no such thing :) the kernal uses a similar (but buggy) routine to detect pal/ntsc =)
2009-04-27 19:53
Mix256
Account closed

Registered: Dec 2008
Posts: 26
But...but, there're two ntsc.
Any smart solution to this as well?

Or do I just wait for line 262, if the next is 263 strike old_ntsc. If the one after that one is 264, we got pal otherwise it's new_ntsc.
2009-04-27 20:18
chatGPZ

Registered: Dec 2001
Posts: 11092
dont bother about the 64 cycle vic. they are very rare, and nothing works right on them anyway. noone gives a damn really =)
2009-04-27 20:29
Mix256
Account closed

Registered: Dec 2008
Posts: 26
Great! Thanks a lot!
2009-04-28 13:16
Skate

Registered: Jul 2003
Posts: 490
Warning! This message contains just for fun material. Don't take my comments too seriously.

I always thought 64 cycles per line is the most suitable number of cycles for c64. It's a pity that only very rare NTSC machines used it in the past.

1) 64 cycles covers all 512 pixels. Some of them are not visible pixels maybe but no 8 pixels gap is the best alternative for me. Actually more than 64 cycles is ok too.

2) Second reason is simple. It's a Commodore "64" god dammit! Why would it have 63 or 65 cycles per line? :D

In PAL mode, I could live with loosing 5 raster lines or so. Just a thought...

Let's get back to the topic. A funny (just for fun) alternative way of detecting PAL/NTSC would be to show two different stabilized rasterbar blocks using 63 and 65 cycles per line. User could select the correct looking block before the demo starts ;) Showing raster splits would be even nicer.

Yeah yeah, complete comment is pointless I know :)
2009-04-28 13:42
enthusi

Registered: May 2004
Posts: 674
Quote: Warning! This message contains just for fun material. Don't take my comments too seriously.

I always thought 64 cycles per line is the most suitable number of cycles for c64. It's a pity that only very rare NTSC machines used it in the past.

1) 64 cycles covers all 512 pixels. Some of them are not visible pixels maybe but no 8 pixels gap is the best alternative for me. Actually more than 64 cycles is ok too.

2) Second reason is simple. It's a Commodore "64" god dammit! Why would it have 63 or 65 cycles per line? :D

In PAL mode, I could live with loosing 5 raster lines or so. Just a thought...

Let's get back to the topic. A funny (just for fun) alternative way of detecting PAL/NTSC would be to show two different stabilized rasterbar blocks using 63 and 65 cycles per line. User could select the correct looking block before the demo starts ;) Showing raster splits would be even nicer.

Yeah yeah, complete comment is pointless I know :)


hehe, nice "detection".
What about input"PAL? (YES/NO)";TV$
2009-04-28 15:17
Mix256
Account closed

Registered: Dec 2008
Posts: 26
Maybe a small game that is too hard on ntsc machines due to speed. So if the player completes the game it's a PAL one and if he dies it's a ntsc one.
2009-04-28 19:15
enthusi

Registered: May 2004
Posts: 674
or you could use drive composer to align the sound of the drive to a single speed tune.
If it matches its 60Hz, otherwise 50 :)
2009-04-28 19:59
Spinball

Registered: Sep 2002
Posts: 87
Quote: hehe, nice "detection".
What about input"PAL? (YES/NO)";TV$


just like this crack? Grand Prix Circuit +F
2010-10-01 02:04
The Shadow

Registered: Oct 2007
Posts: 304
Using this method, there should be a way to distinguish NTSC from Old NTSC-M.
2010-10-01 16:46
tlr

Registered: Sep 2003
Posts: 1702
A quick way to determine the number of cycles per line is to start a timer at raster line 0 then wait until raster line 256 and check the MSB of the timer.
Correctly done (just some inversion) you'll now have the number of cycles directly.

I do this here: http://vice-emu.svn.sourceforge.net/viewvc/vice-emu/testprogs/V..
modesplit.asm, line 194, function check_time.
2010-10-02 07:04
Graham
Account closed

Registered: Dec 2002
Posts: 990
The easiest way to determine cycles/rasterline is to count the rasterlines:

312 rasterlines -> 63 cycles per line
263 rasterlines -> 65 cycles per line
262 rasterlines -> 64 cycles per line

Counting the lines:

w0  LDA $D012
w1  CMP $D012
    BEQ w1
    BCC w0


Result in Akku (low byte of rasterlinecount-1):

#$37 -> 312 rasterlines
#$06 -> 263 rasterlines
#$05 -> 262 rasterlines
2010-10-02 07:19
tlr

Registered: Sep 2003
Posts: 1702
It should be pointed out that my routine is designed for the purpose of hardware analysis and emulator evaluation.
I therefor require the number of cycles per line to be measured directly, not relying on the number of raster lines.
That routine should work with any timing encountered as long as there are more than 256 raster lines on the system.

There are no known hardware with timings different from those listed by Graham so normally you can use his method.
It will for almost all intents and purposes be equally accurate and shorter.

EDIT: nojoopa points out that the Drean PAL-N machine has 65 cycles per line and 312 raster lines so my statement about the known machines was wrong.
2010-10-02 09:36
Graham
Account closed

Registered: Dec 2002
Posts: 990
BCC should have been BMI
2010-10-02 10:05
Frantic

Registered: Mar 2003
Posts: 1627
I added TLR's and Graham's routines to codebase (the PAL/NTSC detect page) (and changed bcc to bmi).

//FTC
2010-10-02 12:13
Graham
Account closed

Registered: Dec 2002
Posts: 990
Slight improvement would be:

w0  LDA $D012
w1  CMP $D012
    BEQ w1
    BMI w0
    AND #$03

#$03 -> 312 rasterlines
#$02 -> 263 rasterlines
#$01 -> 262 rasterlines
2010-10-02 13:55
Moloch

Registered: Jan 2002
Posts: 2891
Ancient thread brought back to life, but certainly some excellent examples recently posted. Will work nicely for my NTSC/PAL detection needs, thanks!
2016-09-28 19:13
Sokrates

Registered: Jun 2014
Posts: 7
Enhanced version to differ between PAL and PAL-N. First count rasterlines, then count cycles.

    LDX #$00
w0  LDA $D012
w1  CMP $D012
    BEQ w1
    BMI w0
    AND #$03
    CMP #$03
    BNE detectionDone ; done for NTSC
    TAY
countCycles
    INX
    LDA $D012
    BPL countCycles
    CPX #$5E  ; VICE values: PAL-N=$6C PAL=$50
	      ; so choose middle value $5E for check 
    BCC isPAL
    INY ; is PAL-N
isPAL
    TYA
detectionDone
    ...

Results in the accumulator:
#$01: 262 rasterlines and 64 cycles per line [NTSC: 6567R56A VIC] (OLD NTSC)
#$02: 263 rasterlines and 65 cycles per line [NTSC: 6567R8 VIC]
#$03: 312 rasterlines and 63 cycles per line [PAL: 6569 VIC]
#$04: 312 rasterlines and 65 cycles per line [Drean PAL-N: 6572 VIC]
2016-09-28 21:09
Count Zero

Registered: Jan 2003
Posts: 1820
Saw your edit on codebase - thanks for that!

Wonder whenever some special fix becomes drean-only or such. Those few more cycles likely only add advantage on calculations for some games, hm?
2016-09-29 02:08
TWW

Registered: Jul 2009
Posts: 541
Another version:

    .pseudocommand VSync {
        bit $d011
        bpl *-3
        bit $d011
        bmi *-3
    }

    lda #$ff
    sta $dc04
    sta $dc05  // Latch #$ffff to Timer A
    :VSync
    ldx #%00011001
    stx $dc0e  // Start Timer A (One shot mode (Timer stops automatically when underflow))
    :VSync
    sec
    sbc $dc05  // Hibyte number of counter
    and #%00000011


A now contains the video system as follows:

#0 - NTSC1
#1 - DREAN
#2 - PAL
#3 - NTSC2

Edit: Rectified bug.
2016-09-29 19:58
Sokrates

Registered: Jun 2014
Posts: 7
Quote: Saw your edit on codebase - thanks for that!

Wonder whenever some special fix becomes drean-only or such. Those few more cycles likely only add advantage on calculations for some games, hm?


Does anyone know about games/demos using the extra cycles of the drean?

My interest was only to have stable rasterlines for all VIC types. Just to be complete.

@TWW: I found your webpage about this topic after you post :-)
2016-09-30 23:13
Peiselulli

Registered: Oct 2006
Posts: 81
The module version of Spike+Minestorm have a drean detection and support it, also Jars Revenge ...
2016-10-01 01:02
Fungus

Registered: Sep 2002
Posts: 608
already discussed here a couple years ago detecting pal, drean, ntsc or old ntsc
2016-10-01 21:22
Sokrates

Registered: Jun 2014
Posts: 7
Quote: The module version of Spike+Minestorm have a drean detection and support it, also Jars Revenge ...

Could you elaborate on that please? Maybe in a new thread "extra features in games/demos for drean" or so?
2016-10-04 10:14
Peiselulli

Registered: Oct 2006
Posts: 81
"support" only means that my (typical) CIA timers are adjusted if the different machines are detected, so the games will work. No more, no less.
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: 2822
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: 473
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: 2822
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: 2822
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: 2822
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: 2822
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: 473
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: 5017
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: 2822
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: 473
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: 473
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
2019-05-19 20:44
Krill

Registered: Apr 2002
Posts: 2822
Quoting Krill
Yes, you determine TOD update rate using the system's crystal clock frequency as reference.
Quoting soci
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 [...]
Pretty sure the CIA timers run at ~1 MHz regardless of any accelerator cartridge. I wasn't suggesting to count CPU cycles. =)

Edit: Groepaz told me of this TDC-L (D1) 2/4Mhz C64 Accelerator which does indeed change CIA clocking, and not consistently depending on mode. (And a system with unreliable timers is not what i'd like to code for or consider for compatibility.)
2019-05-19 22:11
soci

Registered: Sep 2003
Posts: 473
Quoting Krill
Quoting Krill
Yes, you determine TOD update rate using the system's crystal clock frequency as reference.
Quoting soci
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 [...]
Pretty sure the CIA timers run at ~1 MHz regardless of any accelerator cartridge. I wasn't suggesting to count CPU cycles. =)


That's a reference to Devia's CPU cycle counting version.

My first attempt had the same problem as well and that was meant in the first sentence.

CIA timers are ok but if they are not changed then that's one potentially unwanted side effect is eliminated. Such a calibration routine should preferably only change the TOD pre-scaler and nothing else.
2019-05-20 10:12
Silver Dream !

Registered: Nov 2005
Posts: 107
Gents - the main point is that unless I write h/w diagnostic software, I basically initialise TOD because I need it for the PRG to serve its purpose. Otherwise I wouldn't waste bits for checking the frequency, would I? Of course if I have a very good day and mem to spare I might do a more graceful exit and maybe even tell the user "Hey - your TOD is broken. I quit." In generic case though I hardly see a point in accounting for broken hardware. TOD that does not count what it should is of no use, even if everything else works correctly. Drean is a different thing because it has a different characteristics even if working 100%. As much as I understand it (never had one in hands) it doesn't change much in terms of TOD initialisation, which should still work as expected also on Drean. It does change in terms of the video norm though, because if the CIA clock is the same as on NTSC machines then it will recognise it as NTSC. This might be worth researching and eventually accounting for (if possible) but only if we talk about the side-effect of recognising video norm next to TOD ticks freq.
2019-05-20 10:14
Silver Dream !

Registered: Nov 2005
Posts: 107
Quoting Krill
[..](And a system with unreliable timers is not what i'd like to code for or consider for compatibility.)

Precisely that.
2019-05-20 10:32
Silver Dream !

Registered: Nov 2005
Posts: 107
Quoting Krill
Edit: Groepaz told me of this TDC-L (D1) 2/4Mhz C64 Accelerator which does indeed change CIA clocking, and not consistently depending on mode.
In a C64 CIA is clocked off PHI2. Since Kisiel clocks CPU at variable rate there, that will affect.
2019-05-20 11:00
Krill

Registered: Apr 2002
Posts: 2822
Quoting Silver Dream !
Quoting Krill
Edit: Groepaz told me of this TDC-L (D1) 2/4Mhz C64 Accelerator which does indeed change CIA clocking, and not consistently depending on mode.
In a C64 CIA is clocked off PHI2. Since Kisiel clocks CPU at variable rate there, that will affect.
I know. I just wasn't aware that there are accelerators which do not come with an own external faster CPU but do the ballsy thing of speeding up operation of parts of the C-64 itself, including overclocking the CPU. Thought there were a few good reasons why Commodore didn't do this in the first place. :)
2019-05-20 11:34
Silver Dream !

Registered: Nov 2005
Posts: 107
Quoting Krill
I just wasn't aware that there are accelerators which do not come with an own external faster CPU but do the ballsy thing of speeding up operation of parts of the C-64 itself, including overclocking the CPU. Thought there were a few good reasons why Commodore didn't do this in the first place. :)

I recall there was a quasi-2MHz accelerator for the 64 once published in 64'er but AFAIR it used properly rated CPU for that. I don't recall ever seeing ICs rated higher than 2MHz in a 64. Most are 1MHz rated in fact. It doesn't mean they won't run but it means that if they do, they run (partially way) out of specs. IOW, while I truly appreciate Kisiel's ideas, I wouldn't bet on full reliability of this approach in *every* machine.
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
Dymo/G★P
psych
Mr SQL
Acidchild/Padua
Mason/Unicess
Walt/Bonzai
McMeatLoaf
Guests online: 120
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 The Ghost  (9.6)
10 Bromance  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.9)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 Rainbow Connection  (9.5)
6 TRSAC, Gabber & Pebe..  (9.5)
7 Onscreen 5k  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Coders
1 Axis  (9.8)
2 Graham  (9.8)
3 Lft  (9.8)
4 Crossbow  (9.8)
5 HCL  (9.8)

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