Sega Master System / Mark III / Game Gear
by andete. Original documents available at: https://github.com/andete/ym2413/tree/master/results
Last post we looked at the YM2413 decay rates. Now it's time for the attack rates. Unfortunately I wasn't yet able to figure it out completely. But I'll already post what I've got now. Maybe one of you can help fill in some details.
Let's start with the 0%-100% attack-rate table from the YM2413 datasheet. The first table below is copy/pasted from the datasheet (but rearranged in 4 columns). The second table is calculated using the C-program listed below.
Attack rates 0..3 take infinitely long (envelope doesn't change). Rates 60..63 are instantaneous.
Similar to the decay-rate tables, each row is half the row above, and there's also a fixed ratio between the values in the columns (factors are "21 17 14 12"). Though for the higher rates (48 and above) this isn't 100% correct anymore.
When expressed in YM2413 samples (49716Hz) instead of milliseconds, and rounded to the nearest integer, the values listed in the datasheet match exactly with the predicted values. At least up-to rate=48, for higher rates the values start to deviate.
Though it's not clear what the predictions in the above table actually mean. E.g. where do these column-ratios "21 17 14 12" come from? And how does the envelope actually change over that time period?
Let's first look at some pictures:
These measurements are similar to the ones in the previous post: I played a sine wave but now varied channel.block and car.AR (instead of car.DR) to measure the different attack-rates (remember the effective rate is a combination of the selected AR parameter and the frequency of the channel). Here are the details of the settings I've used:
|reg#0x20 = 0x00||key-off|
|reg#0x10 = 0x**||fnum-low|
|reg#0x30 = 0x00||max volume / custom instrument|
|reg#0x20 = 0x1*||key-on / block=* / fnum=*|
I varied car.AR from 0 to 15 and block between 1, 3, 5 and 7 (this gives effective rates 0..63). I mostly used fnum=256, but near the end I switched to fnum=511. I started with car.ML=0, but gradually ramped it up to car.ML=15.
Justified by the above measurements/observations we can now use almost exactly the same algorithm as we used for decay to explain the attack rate timing:
As mentioned above this algorithm likely doesn't apply for rates 52..59. It probably needs similar changes as were done for the decay-rate, though I don't have data yet to verify this.
To obtain data for these very high rates we cannot use the same measurement methodology (look at amplitude peaks). Instead I'm thinking about numerically matching all the sampled values with predictions from an emulation model. Though for this to work we first need to figure out the exact content of the internal sine and exponential table. That's something I'd like to do in one of the following posts.
We now know the timing of the attack-envelope transitions, but we do not yet know to what level we must go at each transition. (In case of decay it was simple: always add 1 (or 2) to the 7-bit EG counter).
In the above table we saw that (almost) all graphs used the same 12 amplitude levels. If we match those with levels of the decay envelope we get this:
So for each of the 12 attack-rate levels (1st column) I measured the amplitude of the peaks (2nd column). Then I looked up the corresponding decay-EG level with the same amplitude (3rd column). Note that for low amplitudes there are multiple EG-levels that correspond with the same amplitude. So for the first few attack-steps I could only match a range of possible levels (I hope to also refine this measurement in the future).
I couldn't see any pattern in this sequence (3rd column), so I looked at earlier OPL3 reverse engineering work by 'carbon14' and 'opl3' for inspiration: http://forums.submarine.org.uk/phpBB/viewtopic.php?f=9&t=16
OPL3 has 512 EG steps (instead of 128 for YM2413). The formula for the OPL3 attack envelope levels is:
I tried to modify this for YM2413. The following comes close, but doesn't match exactly:
So initially it matches pretty well, but near the end it goes too slow. I haven't been able yet to find a better formula.
Above I mentioned that 'almost' all rates used these same 12 levels. But rate=4 is different (visible in the pictures if you look closely). Instead for rate=4 I got this sequence:
So also 12 steps, but slightly different levels.
But when I repeated the measurement for rate=4 I got the same sequence as for all the other measurements. So likely there's nothing special about rate=4. The special thing is that it's the very first measurement I took.
When I start another measurement, I do not fully restart the YM2413 hardware, instead I briefly set the channel to key-off and then to key-on again. Because car.DR=0 and car.RR=15 this very quickly goes from EG-level=0 to EG=127 before starting the attack-phase. I don't remember what I was doing before the very first attack-measurement but possibly the EG-level was in a different state at that time.
So next I started experimenting with different settings for car.SL, car.DR, car.RR, ... I tried to get into a different EG-level before starting the attack-measurement. Only very occasionally I could trigger an alternative attack sequence. (Only) once I could reproduce the sequence
A few more times I got
But, by far, in the majority of the cases I got
I'm not sure yet what determines which sequence the attack-envelope follows. My best guess is that for some reason the initial EG level is not 127 but another value, and because of this the formula generates a different sequence. But I don't know yet why the initial value is different and I also don't know yet what the exact formula is that generates the sequence.
In this and in the previous post we've looked at the attack and decay (and release) states in isolation. We should still investigate in more detail what happens on the transition between the different states. But that's also something for later.
Above I already mentioned possible improvements to the attack-rate measurements: instead of looking for peak-amplitude, try to numerically match the measured values with predicted values. This will allow to also measure rates 44..59 and distinguish between levels with the same amplitude.
I also mentioned that to be able to construct an emulation model to make predictions we first need to know the content of the internal sine and exponential tables. My plan is to investigate that in one of the next posts.
However I can already give a program that correctly predicts the maximum amplitudes for all 128 EG levels. This program is based on earlier OPL3 reverse engineering work done by Matthew Gambrell and Olli Niemitalo: https://docs.google.com/document/d/18IGx18NQY_Q1PJVZ-bHywao9bhsDoAqoIn1rIm42nwo
I'll explain in more detail in a later post.
This is the program:
And below is the output it produces. This matches 100% with the measured amplitude of all 128 EG levels, including the same level-ranges that share the same maximum amplitude.