AY-3-8910 / YM2149 emulation

Discussion of development of software for any "obsolete" computer or video game system. See the WSdev wiki and ObscureDev wiki for more information on certain platforms.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by rainwarrior »

From the other thread:
Pokun wrote:I have some stupid 5B questions I could not find answers for in the wiki or these threads.

1)
The Sunsoft 5B ties the /SEL pin low so that the internal divider is used and the input clock is halved. Does this mean that the clock is half of the CPU clock (NTSC: 1.789773 MHz, PAL: 1.662607 MHz), in other words 0.8948865 MHz and 0.8313035 MHz for NTSC and PAL respectively? So the formula to calculate the period value for a given frequency for an NTSC system is: "Period = 894886.5 / (32 * Frequency)" and not "Period = 1789773 / (32 * Frequency)" correct?

I also read somewhere that the NTSC clock is 1.7897725 MHz or even other values (might not affect the final period values though).


2)
Is it verified that the YM2149F SSG's smoother envelope ramping is because the internal divider is used? I've read about the different envelope levels in the SSG and AY before (here, there are some other interesting info here too like the unused bits that can store values) but there it didn't say that it only applies when using the divider, confusing me.
I know that some MSX computers that uses an SSG instead of an AY for its PSG uses the divider but also uses a faster clock (3.579545 MHz) so to maintain the MSX standard of 1.7897725 MHz for the PSG. I guess these systems benefits from the smoother envelope while still running at the proper speed for the PSG?


3)
What is the difference between YM2149 and YM2149F? I think I read somewhere that the 5B is an YM2149F but not an YM2149.
1/2. The formulas on the wiki are for the Famicom CPU clock of 1789772 Hz. The 32 given in those formulae are dividing that clock. The Famicom CPU clock is the clock that drives the chip, so it would never be any other value unless the Famicom's clock was modified.

The other day I tried to revise the description on the Wiki, since 5B doesn't have an option not to use SEL, I don't think the wiki's description of 5B should be encumbered with too much explanation of what SEL does (which I tried to pare back in my revision, hoping to be less confusing).

As I understand it, leaving SEL high or disconnect makes the YM into a backward-compatible AY. I believed it would switch the envelope into a 16-step mode compatible with the AY, but I don't have a YM to verify this with (yet). I may have been completely mistaken about that. Hypothetically, if SEL was high, this would mean the formulae on the wiki for tone and noise would use 16 instead of 32, and either the envelope formula would look the same (but only have 16 steps), or it would use 8 clocks per step instead of 16 and still have 32 total steps. I don't know which is true. For the actual 5B, the tone/noise frequency factor is 32, and 16 clocks per step for the envelope, as given.

So for the 5B, all I can say for certain is that it is in SEL-low mode, because the division by 2 is required for the pitch of the tone and noise. The YM2149 datasheet is a little bit vague about SEL and the envelope. It's possible that the envelope will tick every 8 clocks with SEL high, instead of every 16 like on the 5B (SEL low), and not what I had long assumed. If anyone knows the truth of that please let me know. Maybe you're right about it being a concession for later computers which were a lot faster than the first generation that was using AY and would rather drive the whole chip at half speed.

This is a case where looking back, I don't know where the source of my belief about what SEL high means for the envelope is. It's possible it was based on something I'd read or tried, or it's possible I made it up by accident or misunderstanding. So... all I can do is retract it for now, pending some more authorative information. (I am trying to be better about documenting my sources in the present.)

I can't comment on being able to read the registers, since the 5B has no read access to it.

3. I think YM2149F is just the full name for the "YM2149". I've not heard of any other versions (other than later clones with different numbering, YMZ294 etc.) The Yamaha datasheet just calls it YM2149 though.

E.g. if I search eBay for YM2149 all of them have an F.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: AY-3-8910 / YM2149 emulation

Post by Pokun »

Thanks, wasn't sure which of the two threads to ask this in myself.
rainwarrior wrote:The formulas on the wiki are for the Famicom CPU clock of 1789772 Hz. The 32 given in those formulae are dividing that clock.
Ah right, I forgot that the original formula in the data sheet was multiplying with 16 instead of 32. I guess I have to recalculate the MSX PSG note pitch table to use for the Famicom and PAL NES.
rainwarrior wrote:The other day I tried to revise the description on the Wiki, since 5B doesn't have an option not to use SEL, I don't think the wiki's description of 5B should be encumbered with too much explanation of what SEL does (which I tried to pare back in my revision, hoping to be less confusing).
I think this is an important bit of information and should remain being mentioned somewhere on that wiki page though. Maybe in a section with additional notes if you think it unnecessarily encumbers the wiki page. It explains why the pitch formulae is different from the data sheet, and it's not something you can just check on pinout diagrams either.
rainwarrior wrote:As I understand it, leaving SEL high or disconnect makes the YM into a backward-compatible AY.
I understood it as that the internal pull-up pulls it high even if it's disconnected so that it can replace an AY where the same pin (TEST 2 pin) is normally required to be disconnected, making it pin-compatible but not necessarily compatible on every little quirk. According to the article I linked, it sounds like the /SEL pin is just for making it possible to use a faster clock without changing anything else (maybe the faster clocks was cheaper?). The AY is rated for 2 MHz maximum while the SSG can use up to a 4 MHz clock.
byuu wrote:Even if you clock the YM externally twice as often to compensate, that's still going to be twice as slow, not twice as fast. You'd have to clock it eight times faster to actually make the envelopes run compatibly (save for the smoother steps) to the AY.
Ah! If this is true, doubling the clock would not be enough, and the MSX computers that uses a 3.579545 MHz clock would run too slow and play the wrong pitch and envelope. Is this really right?
rainwarrior wrote:I can't comment on being able to read the registers, since the 5B has no read access to it.
Oh I totally forgot about that! I guess the same can be said about the I/O ports that the MSX uses for joystick I/O and that the 5B doesn't seem to connect if they are even still in there at all.
Near
Founder of higan project
Posts: 1553
Joined: Mon Mar 27, 2006 5:23 pm

Re: AY-3-8910 / YM2149 emulation

Post by Near »

Here's a YM2149 C. I know of no differences with the various revisions, though.
https://imgur.com/a/8h9n7m3
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by rainwarrior »

Pokun wrote:
rainwarrior wrote:The formulas on the wiki are for the Famicom CPU clock of 1789772 Hz. The 32 given in those formulae are dividing that clock.
Ah right, I forgot that the original formula in the data sheet was multiplying with 16 instead of 32. I guess I have to recalculate the MSX PSG note pitch table to use for the Famicom and PAL NES.
The x32 is for the whole period, by the way. The actual operation needs to flip the square wave every 16 clocks. (The noise is every 32 clocks. The envelope is every 16.)
Pokun wrote:
rainwarrior wrote:The other day I tried to revise the description on the Wiki, since 5B doesn't have an option not to use SEL, I don't think the wiki's description of 5B should be encumbered with too much explanation of what SEL does (which I tried to pare back in my revision, hoping to be less confusing).
I think this is an important bit of information and should remain being mentioned somewhere on that wiki page though. Maybe in a section with additional notes if you think it unnecessarily encumbers the wiki page. It explains why the pitch formulae is different from the data sheet, and it's not something you can just check on pinout diagrams either.
It's still mentioned, but not as many times. Previously there was a bunch of references to the AY and the clock being divided etc. and I think it was making it really hard to read. (Sorta like the Tau/Pi thing. Having to parse 2x in every formula probably doesn't help comprehension.)
Pokun wrote:I understood it as that the internal pull-up pulls it high
Yes, high and NC are equivalent. That's described in the datasheet.
byuu wrote:Here's a YM2149 C. I know of no differences with the various revisions, though.
https://imgur.com/a/8h9n7m3
Ah! Well that's interesting.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: AY-3-8910 / YM2149 emulation

Post by lidnariq »

rainwarrior wrote:So for the 5B, all I can say for certain is that it is in SEL-low mode, because the division by 2 is required for the pitch of the tone and noise. The YM2149 datasheet is a little bit vague about SEL and the envelope. It's possible that the envelope will tick every 8 clocks with SEL high, instead of every 16 like on the 5B (SEL low), and not what I had long assumed. If anyone knows the truth of that please let me know. Maybe you're right about it being a concession for later computers which were a lot faster than the first generation that was using AY and would rather drive the whole chip at half speed.
I mean, no-where in the PDF does it claim that SEL does anything other than add a prescaler.

But I do think it's interesting that the "normalized voltage" graph in the datasheet strongly insinuates that the envelope has a nonzero magnitude when it's at 1/31, while your measurement on the 5B has found that to not be true.
Attachments
ym2149-007-crop.png
ym2149-007-crop.png (1.88 KiB) Viewed 13994 times
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by rainwarrior »

lidnariq wrote:I mean, no-where in the PDF does it claim that SEL does anything other than add a prescaler.
Yeah, I couldn't find anything either. I don't know where I got the notion that it only applied to the tone/noise, and the more I look for it the more I'm thinking it's unlikely that it's anything more than my own fabrication/misunderstanding.
lidnariq wrote:But I do think it's interesting that the "normalized voltage" graph in the datasheet strongly insinuates that the envelope has a nonzero magnitude when it's at 1/31, while your measurement on the 5B has found that to not be true.
I don't think the insinuation is strong. There's a lot of noise, and it's scanned at an angle, putting an unfortunate 1 pixel step up right in the middle of the value you're trying to interpret. (At any rate even if it were clear, it was still worth verifying.)
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by FrankenGraphics »

Hmm. An AY can be bought for much cheaper ($1.45 @ ≥100pcs) than an YM249F ($2.49 @ ≥100pcs). A d-flip flop to halve the clock could be had for a 0.1-0.3 cents*.

32 envelope steps notwithstanding, that might be a good approach to make sunsoft 5b-like boards available for homebrew at a decent price.


*Actually, a quad d-flop (or octal for that matter) would cost about 0.3 cents so you might even have a cpu clock divider and get an octave divided pseudo-channel from each of the analog outputs for free. Well, i'd need a cheap way to gate them. Probably fixed R values to mix them lower than the source, too.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by rainwarrior »

Well with a CPLD at your disposal I don't think you'd even need a separate component for a divider?

InfiniteNESLives has offered a 5B reproduction board for a long time now:
http://www.infiniteneslives.com/nessupplies.php#FME7

I think they're using an AY. Info here:
https://forums.nesdev.com/viewtopic.php ... 75#p100622
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by FrankenGraphics »

I was maybe thinking more an UxROM-style mapper (ie lower in price to begin with). The chip/no chip margin seems to be closer together, too, from my comparison at chips available from utsource. Purpose being putting out a music cartridge to begin with, sort of to demo it. But i'm completely new to digital circuits so i might be missing something that wouldn't make it worthwhile.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: AY-3-8910 / YM2149 emulation

Post by lidnariq »

Years ago, Zzo38 invented a mapper concept for just using a AY-3-8910 (with the 16 GPIO pins) - nesdevwiki:User:Zzo38/Mapper A. GNROM class, but its extreme minimalism - it's hidden behind the PPU - cripples it.

MAME's documentation on the AY-3-8910 says that the "TEST 2" pin - same as the divider select on the YM2149 - is apparently another active-high chip enable. Two active-high chip enables isn't quite enough to map it to a place that doesn't overlap with ROM, though.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: AY-3-8910 / YM2149 emulation

Post by Pokun »

rainwarrior wrote:
Pokun wrote:
rainwarrior wrote:The formulas on the wiki are for the Famicom CPU clock of 1789772 Hz. The 32 given in those formulae are dividing that clock.
Ah right, I forgot that the original formula in the data sheet was multiplying with 16 instead of 32. I guess I have to recalculate the MSX PSG note pitch table to use for the Famicom and PAL NES.
The x32 is for the whole period, by the way. The actual operation needs to flip the square wave every 16 clocks. (The noise is every 32 clocks. The envelope is every 16.)
What do you mean flip every 16 clocks? "Table 5.1" here is not enough for square (except that it is for the MSX clock)? Do I need to do something else than just make a table of note pitches to feed it with?
rainwarrior wrote:It's still mentioned, but not as many times. Previously there was a bunch of references to the AY and the clock being divided etc. and I think it was making it really hard to read. (Sorta like the Tau/Pi thing. Having to parse 2x in every formula probably doesn't help comprehension.)
I see, fair enough.
byuu wrote:Here's a YM2149 C. I know of no differences with the various revisions, though.
https://imgur.com/a/8h9n7m3
So the F is a revision number after all. I found the post that made me think that it was an important point that the 5B contained an F version. Reading it again I see that it was not about the F but about the YM. It was an important point that it's an YM rather than an AY.
FrankenGraphics wrote:Hmm. An AY can be bought for much cheaper ($1.45 @ ≥100pcs) than an YM249F ($2.49 @ ≥100pcs). A d-flip flop to halve the clock could be had for a 0.1-0.3 cents*.

32 envelope steps notwithstanding, that might be a good approach to make sunsoft 5b-like boards available for homebrew at a decent price.


*Actually, a quad d-flop (or octal for that matter) would cost about 0.3 cents so you might even have a cpu clock divider and get an octave divided pseudo-channel from each of the analog outputs for free. Well, i'd need a cheap way to gate them. Probably fixed R values to mix them lower than the source, too.
I didn't know these were in production anymore? I know you can find them on ebay and whatnot but I assumed they are NOS or knockoffs. Someone also said they can be bad chips as well.
rainwarrior wrote:Well with a CPLD at your disposal I don't think you'd even need a separate component for a divider?

InfiniteNESLives has offered a 5B reproduction board for a long time now:
http://www.infiniteneslives.com/nessupplies.php#FME7

I think they're using an AY. Info here:
https://forums.nesdev.com/viewtopic.php ... 75#p100622
I just wish there was an 60-pin option though so that it can actually be used with a Famicom.

There's also this thing.
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by FrankenGraphics »

pokun wrote:I didn't know these were in production anymore?
They may be from time to time. the ay-3 machine was popular in various applications, including casino slot machines. You may use it in anything that beeps. While the envelopes aren't too musical seen as volume envelopes, they come in handy when you want a beeper have a couple of different signals.

The vendor i'm looking at (utsource) has 134.500 units in stock (!!!) which means they've likely picked up the rest of a big factory order. But utsource simply buy up stocks of components from multiple sources when they can be had for cheap, so you can't really source it. Just guess at the label and if you can spot solder marks, i guess.

I don't think they're knockoffs, and if they were, there's no difference either way. Someone in an FB group bought the same item and they worked just fine. :)
Someone also said they can be bad chips as well.
They're not bad per definition, but some NOS parts may have the factory set identifier be non-zero, which you'll want to verify before soldering a whole batch onto boards. The preset is 0 unless requested by some customer (usually for keeping several devices on the same address).


Actually, it seems you can get 100 pcs of AY-3 for $0.975 / piece and YM:s for $1.53, via some fluctuating wholesale price option through the same vendor.
At under a dollar, that would make a lot of difference for homebrew viability. :O
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: AY-3-8910 / YM2149 emulation

Post by rainwarrior »

Pokun wrote:
rainwarrior wrote:The x32 is for the whole period, by the way. The actual operation needs to flip the square wave every 16 clocks. (The noise is every 32 clocks. The envelope is every 16.)
What do you mean flip every 16 clocks?
I mean the 5B's square generator operates by incrementing a counter every 16 clocks. After increment, if the counter is >= the period register value, it toggles between high and low.

That means the signal changes every 16 clocks x the period register value. An entire square wave, on the other hand, requires two changes to make a complete wave. Half the wave will be up, half will be down. So the entire wave will take 32 clocks x the period register for the whole wave.

It's a question of whether you are trying to calculate the frequency of the waveform produced (probably what you want to know if trying to make music with it), or trying to understand how often it needs to update the signal (probably what you want to know if trying to write an emulator).

For the AY (or YM with SEL high/nc) it's every 8 clocks instead.
Pokun wrote:"Table 5.1" here is not enough for square (except that it is for the MSX clock)? Do I need to do something else than just make a table of note pitches to feed it with?
What you need depends on what you want to do.

That table of pitches is relative to the clock of the MSX. If the clock is faster or slower than your target, the pitches will be higher or lower accordingly, but as a musical scale they will still be relatively correct.

From earlier...
Pokun wrote:Ah! If this is true, doubling the clock would not be enough, and the MSX computers that uses a 3.579545 MHz clock would run too slow and play the wrong pitch and envelope. Is this really right?
If the clock is 2x the NES clock, and it's an AY or SEL high/nc YM (another 2x relative to 5B), then its frequencies will be 4x what the 5B does. Two octaves up.

If you want to play some music on both devices at the same pitch, you could just shift the table over by 2 octaves I guess? One will be able to go 2 octaves lower, though, and the other will have more precision in its high frequencies.

For more minor changes in frequency, you could probably usually just leave the playback as-is, like 2MHz vs 1.8MHz will be a different pitch but all the tones will still be correct relative to each other. A change in octave is pretty big, but a smaller change in pitch doesn't normally affect anything about the music negatively, unless you're trying to stay in tune with a second audio source.


On a different line of thinking though... and this is kind of beyond the scope of what I'd want to try and emulate but I wonder if the chip updates all channels at once in a synchronized way, or if they're staggered.

The AY updates tone every 8 clocks, noise every 16 clocks, and envelope every 16 clocks. I wonder if that indicates a cycle of 2 clocks per tone, then 2 clocks per noise/envelope alternating? A repeating pattern of Tone 0, Tone 1, Tone 2, Noise, Tone 0, Tone 1, Tone 2, Envelope?

On the YM, though, the envelope ticks twice as often, so this idea breaks down there. I guess instead of alternating it might also tick the envelope Envelope a second time when the Noise is updated...

Anyway, that's just speculation. I wouldn't really want to put that knowledge to use in an emulator. Since phase can't be read back from the chip (whether or not its registers are readable, it's not on any register), software couldn't try to get in sync with it anyway, and the difference is entirely above audio samplerates.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: AY-3-8910 / YM2149 emulation

Post by Pokun »

FrankenGraphics wrote: They may be from time to time. the ay-3 machine was popular in various applications, including casino slot machines. You may use it in anything that beeps. While the envelopes aren't too musical seen as volume envelopes, they come in handy when you want a beeper have a couple of different signals.
I see, I guess they are useful if you need something a bit more advanced than a buzzer and the hardware envelopes lets you get around having to program a sophisticated driver for it.
FrankenGraphics wrote: I don't think they're knockoffs, and if they were, there's no difference either way. Someone in an FB group bought the same item and they worked just fine. :)
I hope you are right. I might order an SSG (or maybe an AY) just to play around with. Not sure what I need besides a microcontroller and a speaker though. An amplifier I guess.
rainwarrior wrote:I mean the 5B's square generator operates by incrementing a counter every 16 clocks. After increment, if the counter is >= the period register value, it toggles between high and low.

That means the signal changes every 16 clocks x the period register value. An entire square wave, on the other hand, requires two changes to make a complete wave. Half the wave will be up, half will be down. So the entire wave will take 32 clocks x the period register for the whole wave.

It's a question of whether you are trying to calculate the frequency of the waveform produced (probably what you want to know if trying to make music with it), or trying to understand how often it needs to update the signal (probably what you want to know if trying to write an emulator).

For the AY (or YM with SEL high/nc) it's every 8 clocks instead.
Oh I see. My goal for now is to program it and make sounds and music come out of it and eventually include it in my Famicom sound driver. So I'm calculating the period values needed for each frequency in a musical note system to use as a lookup table in the sound driver.
rainwarrior wrote:That table of pitches is relative to the clock of the MSX. If the clock is faster or slower than your target, the pitches will be higher or lower accordingly, but as a musical scale they will still be relatively correct.
Yeah I figured since the MSX clock is exactly the same as the NTSC Famicom, only that the clock is halved for the 5B, I can use the same note pitch table as for MSX. It's just that the square tone on the Famicom's 5B will be one octave lower than on the MSX's PSG for the same reason the Famicom's APU Square is one octave lower (as in lower pitch) than the APU Triangle.
I noticed that the note pitch table I stole from Konamiman (the above link) seems to be a bit off though so I'll just recalculate the note pitches anyway. Besides I need to recalculate them for PAL NES (PAL MSX computers use the same clock as the NTSC computers so the pitch is same on both).
rainwarrior wrote:If the clock is 2x the NES clock, and it's an AY or SEL high/nc YM (another 2x relative to 5B), then its frequencies will be 4x what the 5B does. Two octaves up.
MSX computers use the same clock as Famicom and the AY as the PSG to follow the MSX computer standard definition. Some later manufacturers used the YM as the PSG for legal reasons and also used the doubled clock and halving it by setting the /SEL pin low. So the PSG should just run at twice the speed of the 5B and therefore the PSG is one octave up if I understand everything right.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: AY-3-8910 / YM2149 emulation

Post by lidnariq »

Pokun wrote:I might order an SSG (or maybe an AY) just to play around with. Not sure what I need besides a microcontroller and a speaker though. An amplifier I guess.
The similar SN764(89/94/96) is even cheaper than the used/NOS AYs are. Only 10 bits of tuning, no hardware envelopes (but I think they're more useful as another voice instead of actually as envelope control).

Little-Scale built a simple board with a Teensy (ARM, Arduino) and eight of these.

You can just use a standard commercial amplifier.
Post Reply