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 Coding > detecting pal, drean, ntsc or old ntsc
2012-11-26 14:21
Peiselulli

Registered: Oct 2006
Posts: 81
detecting pal, drean, ntsc or old ntsc

Hello,
I want to know if a test like this is reliable for the four different machine types PAL, NTSC, NTSC(old) and DREAN (ACME source code):

OLD_NTSC = 1
NTSC = 0
PAL = 2
DREAN = 3

        !to "detect.prg",cbm
	!cpu 6510

	* = $0801
	!by $0b,$08,$00,$00,$9e,$32,$30,$36,$31,$00,$00,$00
		
detect:	sei
w0:  	lda $d012
w1:  	cmp $d012
	beq w1
	bmi w0
	and #$03
	cmp #$01
	bne w2
	lda #OLD_NTSC
	bne detected ; unconditional
w2:	cmp #$03
	lda #$00
	rol
	beq detected	;ntsc

	;check for drean or pal
	ldx #$00
	lda #$10
l	inx
	cmp $d012
	bne l
	lda #PAL
	cpx #$70
	bcc detected
	;is a drean
	lda #DREAN
detected:
	tay
	lax offset,y
l2:	lda base_text,x
	beq end
	jsr $ffd2
	inx
	bne l2
end:	lda #$0d
	jmp $ffd2

base_text:
old_ntsc_text:	!text "OLD "
ntsc_text:	!text "NTSC",0
pal_text:	!text "PAL",0
drean_text:	!text "DREAN",0

offset:		
	!byte ntsc_text-base_text
	!byte old_ntsc_text-base_text
	!byte pal_text-base_text
	!byte drean_text-base_text


 
... 63 posts hidden. Click here to view all posts....
 
2014-01-19 12:35
Burglar

Registered: Dec 2004
Posts: 1033
I can understand why ppl want to know how to manually detect pal/ntsc, but for real releases, what's wrong with good old $02a6?

lda $02a6
beq ntsc
bne pal

its really that simple.

and with micro64 tdwtnht also detected ntsc while I was running pal. then restarted it and it was detected as pal... so, obviously ur check is seriously broken.

again, use $02a6, it's why it's there!
2014-01-19 13:02
tlr

Registered: Sep 2003
Posts: 1717
IIRC $02a6 is far from reliable.
2014-01-19 13:40
Flavioweb

Registered: Nov 2011
Posts: 447
You forgot a SEI.
If an irq is fired in the middle of the loop... all fucks.
2014-01-19 13:59
Ninja

Registered: Jan 2002
Posts: 406
Burglar: Two reasons given here: http://www.the-dreams.de/articles/pal-ntsc-detector.txt
2014-01-19 21:21
Fungus

Registered: Sep 2002
Posts: 621
This works :P

I made it fix jiffyclock timing so things would run at the same speed on different systems (basic etc). But it detects the different systems too reliably. If it bugs in an emulator then the emulator needs to be fixed.

;-----------------------------------------
;PAL/NTSC detector
;
;Fungus/Nostalgia
;
;2010
;-----------------------------------------

;ntsc/pal-n jiffy clock =
;pal-m jiffyclock = 16420/$4024 $4000 rounded for 60 times/sec
;pal-m jiffyclock = 19656/$4cc8 $4c00 rounded for 50 times/sec
;ntsc-m (old) jiffyclock = 16832/$41c0 $4100 rounded for 60 times/sec
;ntsc-m (old) jiffyclock = 20198/$4ee6 $4e00 rounded for 50 times/sec
;ntsc-m jiffyclock = 17095/$42c7 $4200 rounded for 60 times/sec
;ntsc-m jiffyclock = 20514/$5022 $5000 rounded for 50 times/sec
;pal-n jiffyclock = 16900/$4204 $4200 rounded for 60 times/sec
;pal-n jiffyclock = 20280/$4f38 $4f00 rounded for 50 times/sec
;
;pal-m cycles per line is 63
;ntsc-m old cycles per line is 64
;ntsc-m standard and pal-n cycles per line is 65


* = $1000

sei
lda #$00
sta $d012
lda #$01
sta $d01a
lda #$1b
sta $d011
lda #$1f
sta $dc0d
sta $dd0d
lda #<irq1
sta $0314
lda #>irq1
sta $0315
lda $dc0d
lda $dd0d
inc $d019
cli
loop jmp *

lda result
eor #$ff
sta result
lda result+$01
eor #$ff
sta result+$01
cmp #$41
beq ntscold
cmp #$42
beq ntscnew
cmp #$4c
beq pal
bcs drean
unknown jmp *

ntscold
lda #$80
sta $02a6
lda ntscoldjiffy
sta $dc05
bne done
ntscnew
lda #$00
sta $02a6
ntscnewdone
lda ntscjiffy
sta $dc05
bne done

pal
lda #$01
sta $02a6
paldone
lda paljiffy
sta $dc05
bne done

drean
lda #$81
sta $02a6
dreandone
lda dreanjiffy
sta $dc05
done
lda #$00
sta $dc04

sei ;use this to reset to normal after check
lda #$31
sta $0314
lda #$ea
sta $0315
lda #$81
sta $dc0d
lda $dc0e
and #$80
ora #$11
sta $dc0e
lda $dc0d
cli
rts
irq1
ldx #$10
dex
bpl *-1
lda #<irq2
sta $0314
lda #>irq2
sta $0315
lda #$ff
sta $dc04
sta $dc05
lda #$19
sta $dc0e
inc $d019
jmp $ea81
irq2
lda $dc05
sta result+$01
lda $dc04
sta result
lda #$00
sta $d01a
lda #$2c
sta loop
inc $d019
jmp $ea81

result
.byte $00,$00
paljiffy
.byte $40
ntscjiffy
.byte $42
ntscoldjiffy
.byte $41
dreanjiffy
.byte $42
2014-01-19 21:34
chatGPZ

Registered: Dec 2001
Posts: 11127
Quote:
Groepaz: you mentioned in the release that you encountered this problem on your pal-machine. Was that real hardware or emulator?

emu. however, even the most broken emulator has the right amount of cycles per line and frame. as others said already, your detection is broken :)
Quote:
what's wrong with good old $02a6?

the kernal is broken, thats why people started adding those checks to their releases back in 1990 you know :)
2014-01-21 04:30
TWW

Registered: Jul 2009
Posts: 541
I see a lot preferr to count raterlines. How about counting total amount of cycles between VSYNC?

Here is how I'd do it:


    sei              // Set Interrupt Flag (MPU Ignore IRQ line)
    cld              // No stupid tricks ;-)
    lda #$7f
    sta $dc0d        // No more IRQ Interrupts from CIA #1
    lda #$00
    sta $d01a        // No more IRQ Interrupts from VICII
    lda $dc0d        // Ack any latent CIA #1 Interrupts
    lda #$01
    sta $d019        // Ack any latent VICII Interrupts
    lda #$35
    sta $01          // Switch off the MPUs access to BASIC and KERNAL ROM and access the RAM instead (dec/inc $01 allows access to IO RAM).

    // Ignore NMIs
    lda #<nmivector
    sta $fffa
    lda #>nmivector
    sta $fffb        // Set NMI HW Vector to point to RTI
    jmp *+4
nmivector:
    rti
    lda #$00
    sta $dd0e  // Stop CIA #2 Timer A
    sta $dd04
    sta $dd05  // Set Timer A = 0 (NMI will trigger immediately when Timer A is restarted)
    lda #$81
    sta $dd0d  // Set Timer A as NMI source
    lda #$01
    sta $dd0e  // Start Timer A (triggers NMI and unless the NMI get's acked, any new NMI's will be ignored).

    // From this point on we will not be disturbed and can commence testing.

    lda #$00
    sta $d015  // No Sprites
    lda #$00
    sta $d011  // Turn off screen to avoid badlines


    // Detect the model
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 312 rasterlines -> 63 cycles per line PAL        => 312 * 63 = 19656 Cycles / VSYNC  => #>76  %00
    // 262 rasterlines -> 64 cycles per line NTSC V1    => 262 * 64 = 16768 Cycles / VSYNC  => #>65  %01
    // 263 rasterlines -> 65 cycles per line NTSC V2    => 263 * 65 = 17095 Cycles / VSYNC  => #>66  %10
    // 312 rasterlines -> 65 cycles per line PAL DREAN  => 312 * 65 = 20280 Cycles / VSYNC  => #>79  %11

    // Use CIA #1 Timer A to count cycled in a frame
    lda #$ff
    sta $dc06
    sta $dc07  // Latch #$ffff to Timer B

    lda $d011
    bmi *-3
    lda $d012
    bne *-3    // VSYNC

    lda #%00011001
    sta $dc0f  // Start Timer B (One shot mode (Timer stops automatically when underflow))

    lda $d011
    bpl *-3    // Wait untill we cross the 256 rasterline

    lda $d011
    bmi *-3
    lda $d012
    bne *-3    // VSYNC again

    lda #$ff
    sec
    sbc $dc07  // Hibyte number of cycles used
    and #%00000011

    sta $0400

    // Turn back on screen to see result (#$00, #$01, #$02 or #$03)
    lda #$1b
    sta $d011

    jmp *



I got some funny result on the emulator when using inject into ram etc. on the first reading but it stabilizes quickly with a small jitter ofcourse. If you want perfection you could stab and use a raster to sync and subtract the overhead to get the exact cycle number but for this purpose this would be pointless as the hi-byte of the Timer is enough to tell the different models apart.
2014-01-21 11:44
JackAsser

Registered: Jun 2002
Posts: 1989
@TWW: Clean!
2014-01-21 13:36
Norrland

Registered: Aug 2011
Posts: 14
Thanks for all suggestions, and especially to Flavioweb... Of course I missed the sei... :) actually I have it as the first opcode after the test... :P
2014-01-21 19:33
tlr

Registered: Sep 2003
Posts: 1717
Quoting TWW
I see a lot preferr to count raterlines. How about counting total amount of cycles between VSYNC?

Here is how I'd do it:
...


Nicely done. I've used something similar. Though my approach was to measure the number of cycles per line, then the number of lines and "multiply". The result is cycles/frame-1 and cycles/line-1 for direct loading into timers.
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
Dymo/G★P
Higgie/Kraze/Onslaught
Alakran_64
Apollyon/ALD
Sande/Hokuto Force
t0m3000/ibex-crew
cba
zscs
Guests online: 143
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 Dawnfall V1.1  (9.5)
8 Quadrants  (9.5)
9 Daah, Those Acid Pil..  (9.5)
10 Birth of a Flower  (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 Logo Graphicians
1 Sander  (9.9)
2 Facet  (9.6)
3 Mermaid  (9.4)
4 Pal  (9.4)
5 Shine  (9.3)

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