-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_tv_setting.asm
More file actions
73 lines (69 loc) · 2.16 KB
/
detect_tv_setting.asm
File metadata and controls
73 lines (69 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
detect_tv_setting:
;
; NES TV system detection code
; Copyright 2011 Damian Yerrick
;
; Copying and distribution of this file, with or without
; modification, are permitted in any medium without royalty provided
; the copyright notice and this notice are preserved in all source
; code copies. This file is offered as-is, without any warranty.
;
;.export getTVSystem
;.importzp nmis
;.align 32 ; ensure that branches do not cross a page boundary
;;
; Detects which of NTSC, PAL, or Dendy is in use by counting cycles
; between NMIs.
;
; NTSC NES produces 262 scanlines, with 341/3 CPU cycles per line.
; PAL NES produces 312 scanlines, with 341/3.2 CPU cycles per line.
; Its vblank is longer than NTSC, and its CPU is slower.
; Dendy is a Russian famiclone distributed by Steepler that uses the
; PAL signal with a CPU as fast as the NTSC CPU. Its vblank is as
; long as PAL's, but its NMI occurs toward the end of vblank (line
; 291 instead of 241) so that cycle offsets from NMI remain the same
; as NTSC, keeping Balloon Fight and any game using a CPU cycle-
; counting mapper (e.g. FDS, Konami VRC) working.
;
; nmis is a variable that the NMI handler modifies every frame.
; Make sure your NMI handler finishes within 1500 or so cycles (not
; taking the whole NMI or waiting for sprite 0) while calling this,
; or the result in A will be wrong.
;
; @return A: TV system (0: NTSC, 1: PAL, 2: Dendy; 3: unknown
; Y: high byte of iterations used (1 iteration = 11 cycles)
; X: low byte of iterations used
LDA #$01
STA detecting_tv
ldx #0
ldy #0
lda nmis
nmiwait1:
cmp nmis
beq nmiwait1
lda nmis
nmiwait2:
; Each iteration takes 11 cycles.
; NTSC NES: 29780 cycles or 2707 = $A93 iterations
; PAL NES: 33247 cycles or 3022 = $BCE iterations
; Dendy: 35464 cycles or 3224 = $C98 iterations
; so we can divide by $100 (rounding down), subtract ten,
; and end up with 0=ntsc, 1=pal, 2=dendy, 3=unknown
inx
bne .exit_loop
iny
.exit_loop:
cmp nmis
beq nmiwait2
STX debug+1
STY debug+2
tya
sec
sbc #09
cmp #3
bcc notAbove3
lda #3
notAbove3:
LDX #$00
STX detecting_tv
rts