Best way to detect NTSC or PAL

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Best way to detect NTSC or PAL

Post by DRW »

What is the best way to check whether the console is NTSC or PAL?

I'd prefer a method that is used once at startup and then it's done. If it's possible, I'd like to have something that you don't need to put into the NMI where it would takes away CPU time at any frame.

Also, about that Dendy stuff: Do I have to set the music to PAL or to NTSC for that?
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Best way to detect NTSC or PAL

Post by dougeff »

Check the startup code that comes with Famitone2. I don't remember the details... maybe it counts something between several NMIs.
But, it's able to tell the difference between NTSC and PAL.
nesdoug.com -- blog/tutorial on programming for the NES
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Best way to detect NTSC or PAL

Post by lidnariq »

Here's the run-once routine I wrote for the NROM Driar port; no NMI intrusion necessary.

Code: Select all

;;; use the power-on wait to detect video system-
	ldx #0
	ldy #0
@vwait1:
	bit $2002
	bpl @vwait1  ; at this point, about 27384 cycles have passed
@vwait2:
	inx
	bne @noincy
	iny
@noincy:
	bit $2002
	bpl @vwait2  ; at this point, about 57165 cycles have passed

;;; BUT because of a hardware oversight, we might have missed a vblank flag.
;;;  so we need to both check for 1Vbl and 2Vbl
;;; NTSC NES: 29780 cycles / 12.005 -> $9B0 or $1361 (Y:X)
;;; PAL NES:  33247 cycles / 12.005 -> $AD1 or $15A2
;;; Dendy:    35464 cycles / 12.005 -> $B8A or $1714

	tya
	cmp #16
	bcc @nodiv2
	lsr
@nodiv2:
	clc
	adc #<-9
	cmp #3
	bcc @noclip3
	lda #3
@noclip3:
;;; Right now, A contains 0,1,2,3 for NTSC,PAL,Dendy,Bad
Last edited by lidnariq on Tue Jan 26, 2016 5:03 pm, edited 1 time in total.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Best way to detect NTSC or PAL

Post by tepples »

It can be done by counting cycles from one NMI to the next. This may mess up on an overclocked NES, so you can either use a more complicated method to detect overclocked systems or just write off overclocking as undefined behavior.

Ninja'd: lidnariq has an interesting way to correct for the $2002 race.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Best way to detect NTSC or PAL

Post by thefox »

Alternative way:
- Poll for VBL
- Wait in timed loop for ~30k cycles
- Check VBL flag in $2002, if set => NTSC
- Wait in timed loop for ~4k cycles
- Check VBL flag in $2002, if set => PAL
- Otherwise => Dendy
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Best way to detect NTSC or PAL

Post by dougeff »

Does anyone know the music difference of the Dendy, as asked in OP?
nesdoug.com -- blog/tutorial on programming for the NES
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Best way to detect NTSC or PAL

Post by lidnariq »

Dendy sound wants NTSC tuning (at least if you're using DPCM) and PAL speeds.

I have to admit that I like thefox's algorithm better than my "brute force" implementation.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Best way to detect NTSC or PAL

Post by tokumaru »

thefox wrote:Alternative way:
- Poll for VBL
- Wait in timed loop for ~30k cycles
- Check VBL flag in $2002, if set => NTSC
- Wait in timed loop for ~4k cycles
- Check VBL flag in $2002, if set => PAL
- Otherwise => Dendy
That's how I do it (it's a good way to do the detection when booting up without interfering with the rest of the program), except I don't have one variable indicate 1 of 3 types of consoles. Instead I have 2 flags, one indicating whether the frame rate is 60Hz (NTSC) or 50Hz (PAL and Dendy), and another indicating whether the PPU vs. CPU clock ratio is 3:1 (NTSC and Dendy) or 3.2:1 (PAL). I do this because of the Dendy, which has some things in common with NTSC consoles and some in common with PAL consoles, and his way I can make decisions by checking a single flag, instead of checking whether the console is this OR that.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Best way to detect NTSC or PAL

Post by tepples »

0=NTSC, 1=PAL, 2=Dendy gives the mapping nonzero=change speed, bit 0 set=change pitch.

In game code and music tempo code:

Code: Select all

  lda tvSystem
  beq @noSpeedCorrection
    ; fixes for PAL NES/Dendy here
  @noSpeedCorrection:
In music pitch code and raster effect code:

Code: Select all

  lda tvSystem
  lsr a
  bcs @noPitchCorrection
    ; fixes for PAL NES here
  @noPitchCorrection:
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Best way to detect NTSC or PAL

Post by rainwarrior »

I've used tepples' method (from the wiki) in several releases at this point, so I can say that it works well in my experience.

The other two approaches should work, too, though.
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Best way to detect NTSC or PAL

Post by DRW »

I'll check the rest of the answers later.

But now, I'd like to elaborate on the following:
dougeff wrote:Check the startup code that comes with Famitone2. I don't remember the details... maybe it counts something between several NMIs.
But, it's able to tell the difference between NTSC and PAL.
There is something like that in FamiTone? Because in that library, you have to specify yourself whether FamiTone shall use PAL or NTSC.


Another question about FamiTone: Is that program able to use the correct music for Dendy? Because I can only pass PAL or NTSC to the initialization:

Code: Select all

;------------------------------------------------------------------------------
; reset APU, initialize FamiTone
; in: A   0 for PAL, not 0 for NTSC
;     X,Y pointer to music data
;------------------------------------------------------------------------------

FamiToneInit:
And then there is the FT_PITCH_FIX constant that is set to true when FT_PAL_SUPPORT and FT_NTSC_SUPPORT are both enabled.

So, will FamiTone adjust the music correctly for Dendy when NTSC and PAL support are both enabled?
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: Best way to detect NTSC or PAL

Post by GradualGames »

I wonder how many Dendy systems there are out there relative to PAL and NTSC systems? I never see it as a selection in emulators. ...I just started using tepples method from the wiki as well, baked it into the GGSound demos (the library itself, like famitone, doesn't have the detection itself) and used it to correct raster effects in my current game project---works great for me!

*edit* Another thing I had wanted to say was---feedback from beta testers suggests that once a player is used to the pitch and speed of a game on NTSC or PAL, that feels like the "normal version" of the game to them---and the opposite region either feels too fast or too slow. This phenomenon suggests to me it is not necessarily always worth it to go to the trouble to make a game play the same on each region. *edit* I only used it to make the raster effects work correctly in both, on my current project. All other speed and pitch concerns were left alone. I might wind up doing it on the next project though, just "because."
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Best way to detect NTSC or PAL

Post by tepples »

As far as I can tell, most systems using a Dendy-style chipset (6527P/6538) were sold in Asia.

There is a patch to enable Dendy emulation in FCEUX.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Best way to detect NTSC or PAL

Post by tokumaru »

GradualGames wrote:I wonder how many Dendy systems there are out there relative to PAL and NTSC systems? I never see it as a selection in emulators.
I think at least one emulator calls it "hybrid mode".
feedback from beta testers suggests that once a player is used to the pitch and speed of a game on NTSC or PAL, that feels like the "normal version" of the game to them---and the opposite region either feels too fast or too slow.
I guess it was very much like this in the 80's and 90's, when you had absolutely no contact with the other version, but now that we have YouTube and emulators it's very easy to be exposed to alternate versions, and you can immediately tell that something is off and not everyone is having the same experience.
This phenomenon suggests to me it is not necessarily always worth it to go to the trouble to make a game play the same on each region.
It really is a lot of trouble. Somethings have to be adjusted up, others down, and the low precision of fixed-point numbers doesn't really allow everything to remain synced properly.
I only used it to make the raster effects work correctly in both, on my current project. All other speed and pitch concerns were left alone. I might wind up doing it on the next project though, just "because."
I'll probably just handle raster effects and the music's tempo and pitch, which are simple. I definitely don't feel like messing with physics and animations, because I'm sure I won't be able to perfectly match the gameplay for both refresh rates.
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: Best way to detect NTSC or PAL

Post by GradualGames »

Unfortunately I made so many updates to GGSound it didn't make it realistic to integrate into my current project, so the pitch will be different, too, but apparently people get used to that as well. I'll surely correct for pitch in my next project though, it is indeed easy. But this brings me to a question that I haven't yet found an answer to, and that is: How is the speed of envelope execution handled for NTSC versus PAL? Since these require frame by frame fidelity to sound smooth, I would imagine that envelopes remain unchanged on NTSC or PAL for most music engines, and thus will sound subtly different, and maybe even change the affect of certain instruments. Can anyone comment on this? GGSound right now does not do any adjustment for envelopes, only the tempo of playing notes. Does famitone?
Post Reply