MSU1 A/V synchronization

Discussion of hardware and software development for Super NES and Super Famicom.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: MSU1 A/V synchronization

Post by tepples »

byuu wrote:We could take a nuclear option for now and declare that MSU1 perfect-sync is only for NTSC titles, and worry about PAL perfect-sync support only if and when a strong desire for it emerges.
That's what Konami ended up deciding for VRC IRQs.

Another option is to have the MSU1 default to NTSC and snoop $213F reads to see whether to switch to PAL.
byuu wrote:What I really love about the NTSC / 487 option is that it doesn't matter whether you output at 44100hz (your own oscillator) or at NTSC / 487 = 44101hz (reuse SNES oscillator) ... the time to drift by a noticeable degree is longer than entire movies, so it doesn't matter at all in practice.
That and a feature-length sprite animation could use one .pcm per chapter.

But I'm inclined to just stick with /487, much as PlayStation sample rates are slightly off (44056 Hz, accounting for the 1000/1001 factor in NTSC frame rates).
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

Hmmm...
59378938/122500 = 484.72602448979591836734

somewhere inbetween, lol.
Near
Founder of higan project
Posts: 1553
Joined: Mon Mar 27, 2006 5:23 pm

Re: MSU1 A/V synchronization

Post by Near »

(315/88*6000000)/44100 = 487.012987013
21281370/44100 = 482.570748299

487.012987013*65536 = 31916883.116883968 ~= 31916883
482.570748299*65536 = 31625756.560523264 ~= 31625757

(315/88*6000000)*65536 = 1407534545454.545454545 ~= 1407534545455
21281370*65536 = 1394695864320

1407534545455 / 31916883 = 44100.000161513
1394695864320 / 31625757 = 44099.999387208

I'd say that's pretty damned close :D

Now if we have to fit it into a 32-bit number ...

487.012987013*128 = 62337.662337664 ~= 62338
482.570748299*128 = 61769.055782272 ~= 61769

(315/88*6000000)*128 = 2749090909.090909091 ~= 2749090909
21281370*128 = 2724015360

2749090909 / 62338 = 44099.761124836
2724015360 / 61769 = 44100.039825803

Pseudo-algorithm:

Code: Select all

//the number is big enough it won't exceed max twice, so you don't have to initialize it at reset if you don't want to
uint32_t counter = 0;

void pin1_strobe() {  //21.47mhz or 21.28mhz
  counter += (NTSC ? 62338 : 61769);
  if(counter >= (NTSC ? 2749090909 : 2724015360)) {
    counter -= (NTSC ? 2749090909 : 2724015360);
    sd2snes_output_one_audio_sample();
  }
}
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

Sweet. =) I can imagine using this (487.x for $213F.4 == 0, 482.x for $213F.4 == 1). Breaks the edge case where the user switches 50/60Hz mid-game but I'd say we can safely ignore that. It could lead to problems with additional hardware that patches $213F.4 though, like the Ultra16 and the SuperCIC with additional optional circuitry.
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

BTW you dun goofed somewhere. 21477272.7272 * 62238 / 2749090909 = 486.23, not 44099.7... :P

21477272.727272 * 5644800 / 2749090909 = 44100.0000015 though which is super sexy.

That said, with a 32 bit phase accumulator I guess I can just as well have it wrap without additional arithmetic.
21477272.727272 / (2^32) * 8819000 = 44100.00242800725806706000
21281370 / (2^32) * 8900182 = 44100.00196875538672957300
lidnariq
Posts: 10677
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: MSU1 A/V synchronization

Post by lidnariq »

Isn't the NTSC ratio exactly 77÷ 37500 ? There has to be some other constraint in here that I'm missing...
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

HOW DID YOU DO THAT D:
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

Nevermind, Wolfram Alpha can solve for it, and it gives an exact ratio for PAL too.
I feel like math doge.
Near
Founder of higan project
Posts: 1553
Joined: Mon Mar 27, 2006 5:23 pm

Re: MSU1 A/V synchronization

Post by Near »

Oh wow ... we're gonna have matching volume and frequency ...!! That means the exact same files will be compatible with every hack. That's totally awesome.

Now if we can just devise a good resume scheme, we'll have an MSU1 trifecta! :D

ikari, thank you so much for your patience and keeping up with all of this. I know it's just a small portion of your overall project (supporting several coprocessors and lots of other features), yet you've put a lot of work into this and I really appreciate it. MSU1 would still be dead if not for you, Conn, Darkshock, et al.

Even I'm getting a lot more excited about it lately.

> Nevermind, Wolfram Alpha can solve for it, and it gives an exact ratio for PAL too.

And for NTSC color subcarrier ratio, I like 315/88 (MHz). Multiply by 6 for SNES crystal.
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

:)

Regarding resume someone has come up with something: https://github.com/mlarouche/sd2snes/co ... b0cb74d1d2
Maybe it's a bit feature specific but in the spirit of MSU1 it presents a very high level interface to the SNES. It introduces a new "resume" bit at It is supposed to work like this:

1. Request & play "Overworld" track as usual
2. During playback, write to $2007 with the new "resume" bit set ($2007.2). "play" may be either 0 or 1. This saves the current track and playback position to temporary variables. This action is not repeatable so choose wisely when to save your resume point. :>
3. Request & play "Battle" track as usual, with "resume" bit CLEARED. (setting it will overwrite the saved track and position at this point)
4. Request "Overworld" track again, play with "resume" bit SET. This will recognize the track number saved previously and seek to the saved position.

Unfortunately he cannot test it because he is having some trouble with the build toolchain. Not quite sure why, ld is attempting to link stuff that it shouldn't. :/
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: MSU1 A/V synchronization

Post by tepples »

ikari_01 wrote:HOW DID YOU DO THAT D:
Start from the definition of NTSC, then count master clocks.
  • The audio subcarrier is 4.5 MHz.
  • The horizontal scan rate (of a standard signal, not the NES or Super NES signal) is 1/286 of the audio subcarrier, or 4500/286 kHz = about 15.734 kHz.
  • The NTSC chroma subcarrier is 227.5 times the horizontal scan rate, or 315/88 MHz = about 3.5795 MHz.
  • The NES and Super NES master clock is six times the chroma subcarrier, or 945/44 MHz = about 21.4773 MHz.
What does it give for the PAL ratio? I know the PAL chroma subcarrier is 4433618.75 MHz, but I haven't been able to factor it well because of that big, prime 64489 in the way.
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

1470 / 709379.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: MSU1 A/V synchronization

Post by tepples »

I figured that much. Now factor 709379 to see where I ran into problems. My point is that memorizing the fraction isn't much better than memorizing the exact value, unlike NTSC where 315/88 is snappy enough to derive all other rates.

Bresenham for NTSC fits in a 16-bit adder and comparator; PAL needs a 20.
User avatar
ikari_01
Posts: 141
Joined: Sat Jul 04, 2009 2:28 pm
Location: Wunstorf, Germany

Re: MSU1 A/V synchronization

Post by ikari_01 »

I get the NTSC -> master clock derivation but I still fail to see how you get to 44100 from there. ^^;
lidnariq
Posts: 10677
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: MSU1 A/V synchronization

Post by lidnariq »

39375000 × 6 ÷ 11 = 21477272+8⁄11

44100 ÷ (39375000 × 6 ÷ 11) = (44100 × 11) ÷ ( 39375000 × 6) = (77 × 6300) ÷ (37500 × 6300) = 77 ÷ 37500
Post Reply