User Tools

Site Tools


base:detect_pal_ntsc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
base:detect_pal_ntsc [2016-09-27 17:49] sokratesbase:detect_pal_ntsc [2020-11-10 21:59] copyfault
Line 1: Line 1:
-====== Detect PAL/NTSC ====== +====== Detect NTSC/PAL ======
- +
-Since you cannot rely on $02a6 to detect PAL/NTSC, you better do the check yourself. The theory behind these checks is simply that PAL and NTSC systems have different ammounts of rasterlines, which can thus serve as the basis for a detection check.+
  
 +Since you cannot rely on $02a6 to detect NTSC/PAL, you better do the check yourself. The theory behind these checks is simply that PAL and NTSC systems have different amounts of rasterlines, which can thus serve as the basis for a detection check. For alternative approach, especially useful if you also need to use TOD, check [[efficient_tod_initialisation|Efficient TOD initialisation]] page.
 ===== J0x variant ===== ===== J0x variant =====
  
Line 67: Line 66:
  
 <code> <code>
 +    SEI
     LDX #$00     LDX #$00
 w0  LDA $D012 w0  LDA $D012
Line 611: Line 611:
 ; eof ; eof
 </code> </code>
 +
 +
 +===== TWW's Variant =====
 +
 +Count's number of cycles on one scan with CIA timer and uses the 2 LSBs from the high byte of the CIA Timer to determine model. This reliably detects PAL, NTSC, NTSC2 and DREAN. routine exits with result in A. Make sure no interrupts occur during the runtime of the routine.
 +
 +<code>
 +    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 +    // Detect PAL/NTSC
 +    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 +    // 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
 +
 +DetectC64Model:
 +
 +    // Use CIA #1 Timer B to count cycled in a frame
 +    lda #$ff
 +    sta $dc06
 +    sta $dc07  // Latch #$ffff to Timer B
 +
 +    bit $d011
 +    bpl *-3    // Wait untill Raster > 256
 +    bit $d011
 +    bmi *-3    // Wait untill Raster = 0
 +
 +    ldx #%00011001
 +    stx $dc0f  // Start Timer B (One shot mode (Timer stops automatically when underflow))
 +
 +    bit $d011
 +    bpl *-3    // Wait untill Raster > 256
 +    bit $d011
 +    bmi *-3    // Wait untill Raster = 0
 +
 +    sec
 +    sbc $dc07  // Hibyte number of cycles used
 +    and #%00000011
 +    rts
 +</code>
 +
  
 ===== Older routine ===== ===== Older routine =====
Line 785: Line 826:
 </code> </code>
  
 +===== When size really matters =====
 +
 +by Copyfault/The Solution/The Obsessed Maniacs
 +
 +In the following I'm going to present two routines for detecting PAL/NTSC: the first one is used for telling a EU-PAL-chip (with 63 cycles per line) apart from a "new NTSC"-chip (with 65cycles/line). This suffices in most cases since the mentioned systems were the most common in EU or US, resp.
 +
 +The 2nd routine is capable of detecting any of the four VIC-types that have been mentioned earlier on this page (EU-PAL, NTSC old _and_ new and the one for the Drean-PAL-system).
 +
 +==== Short PAL/NTSC detection ====
 +
 +The basic idea is to constantly read the raster beam position and keeping it in a backup register until rasterline==0 (or rasterline==$100) is reached. Then the backup register holds the value of the last line that was read before reaching line 0 (or $100 resp.).
 +
 +In case the last line was $ff, it does not tell much about the system at hand. Thus, the rasterline-read-and-backup-procedure is repeated until the backup value is not $ff afterwards.
 +
 +The well-known table of VIC-specs now reveals that the backup value must be one of the following:
 +<code>
 +#$37 -> 312 rasterlines -> PAL
 +#$06 -> 263 rasterlines -> new NTSC 
 +#$05 -> 262 rasterlines -> old NTSC
 +</code>
 +
 +It's even possible to tell new and old NTSC apart this way (not just PAL vs. NTSC as stated in the preface). The approach is NOT capable to distinguish between the different PAL-variants (PAL N as used in the Drean-systems and EU-PAL) since they have the same number of rasterlines per frame.
 +
 +
 +=== Source Code ===
 +<code>
 +chk:          
 +              ldx #$aa   //$aa = TAX
 +              lda $d012
 +              bne chk+1
 +              txa
 +              bmi chk
 +</code>
 +This routine continuously checks $D012==0 while saving the last read $D012-value in X and repeats the whole procedure when the value stored in X has the MSB set. This luckily suffices to distinguish the case "last line was $ff" from "last line was the last one of the frame", since the highest possible value is $37 (on PAL).
 +
 +Why that [''ldx #$aa'']? There's a small probability that the routine starts when we accidently **are** on line $0 (or $100). The [''bne chk+1''] would not branch, i.e. the check-for-line$0-loop would end promptly, without any backup value stored in X yet. Thus, we need an init value for X that forces the line$0-check to be repeated in this rare case. It could by any value with MSB set, but using $aa (=TAX as opcode) contributes to making the whole routine as short as possbile.
 +
 +
 +===== Very short full VIC-type detection =====
 +
 +Count cycles galore
 +
 +=== Source Code ===
 +<code>
 +yet
 +to
 +come
 +</code>
base/detect_pal_ntsc.txt · Last modified: 2020-11-11 01:49 by copyfault