
DevelopmentSega Master System / Mark III / Game Gear 
Home  Forums  Games  Scans  Maps  Cheats  Credits 
by andete. Original documents available at: https://github.com/andete/ym2413/tree/master/results
<< YM2413 Reverse Engineering Notes 20150311  YM2413 Reverse Engineering Notes  YM2413 Reverse Engineering Notes 20150320 >>
In the previous post we figured out the resolution of the sine table. To do this we choose 'special' values (powersof2) for the channel frequency (fnum+block+ML) and looked at 'flat' regions in the output. E.g. near zero where the sine wave is rising/falling the fastest, we still saw multiple successive equal output values (because the sine table only has 1024 entries and the chosen frequency was low enough to require more than 1024 steps per period).
In this post we'll use the same technique to figure out more details about the internal phase calculations. Phase or phasecounter here means the 'position' in the sine wave.
When setting nonpoweroftwo fnum values we see that the stepsize through the sine table is not always the same for successive samples. E.g. the same entry might sometimes repeat 5x sometimes 6x (for lower frequencies) or for each sample we skip ahead sometimes 7 sometimes 8 entries in the table (for higher frequencies). This can easily be explained by assuming fixedpoint arithmetic. So instead of only having a 10 bit phase counter (for 1024 entries) we have some additional fractional bits.
Fixedpoint calculations are very cheap, both in software and (even more) in hardware. Most (all?) YM2413 emulators also perform the phase calculations in fixedpoint. But not all emulation code agrees on the number of bits before and after the decimal point. From the previous post we know there are 10 bits before, but how many bits are there after the decimal point?
The upper (integral) bits determine the entry in the sine table, the lower (fractional) bits track the position between entries. In theory the fractional bits could be used to interpolate between two table entries, this could increase the accuracy of the result. Though interpolation typically requires multiplication and that's likely too expensive for the YM2413 hardware implementation. So for now I'll assume the YM2413 doesn't interpolate. Instead it directly uses the upper 10 bits as the entry position (actually the upper 2 bits 'mirror' the table lookup and only the next 8 bits are used as the actual table index).
So again, how many fractional bits are there? To figure this out I did the following experiment: play a sine wave (exact shape doesn't matter) at various different settings for:
For each combination capture the output and measure the length of 'flat' segments (the number of consecutive equalvalue outputs). This gives the following table:
ML  block  fnum  #repeats  stepsize  remarks 

0  0  0  inf  0  #1 
0  0  1  inf  0  #2 
0  0  2  512  1  
0  0  3  512  1  #4 
0  0  4  256  2  
0  0  8  128  4  
0  1  0  inf  0  #1 
0  1  1  512  1  
0  1  2  256  2  
0  1  3  170.67  3  #3 
0  1  4  128  4  
0  1  8  64  8  
0  2  0  inf  0  #1 
0  2  1  256  2  
0  2  2  128  4  
0  2  3  85.33  6  #3 
0  2  4  64  8  
0  2  8  32  16  
1  0  0  inf  0  #1 
1  0  1  512  1  
1  0  2  256  2  
1  0  3  170.67  3  #3 
1  0  4  128  4  
1  0  8  64  8  
1  1  0  inf  0  #1 
1  1  1  256  2  
1  1  2  128  4  
1  1  3  85.33  6  #3 
1  1  4  64  8  
1  1  8  32  16 
The first 3 columns are the input parameters of the experiments. The #repeats column is the measured result. And the stepsize column is a calculated value (more on this column below).
The table shows the following 'interesting' data points:
If we ignore ML=0,block=0 for now, the pattern seems to be
is equal to
The highest value in the #repeats column is 512. All other values in that column can be expressed as 512/n with n an integer. This suggests the fixedpoint phasecounter has 9 fractional bits.
Let's assume the formula
stepsize = ((fnum * mlTab[ML]) << block) >> 1
with
mlTab = {1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30}
(same table as listed in the datasheet, but each value doubled)
The result of this formula is shown in the 'stepsize' column. This value must be interpreted as a fixedpoint value with 9 fractional bits (roughly speaking, you get the equivalent mathematical value by dividing the number by 512).
To check this formula let's use it to predict the measured values:
#repeats should be the reciprocal of the stepsize. Or expressed in 9bit fixedpoint:
This matches exactly the measurements.
Let's revisit the earlier remarks:
What about overflow? If we pick the values ML=15,block=7,fnum=511 (the maximum values) our formula predicts stepsize = 981120 (0xEF880). But that doesn't fit in 19 bits.
Let's repeat the above experiment, but now for high frequency settings. Instead of measuring the equalvaluesegmentlength, we now measure the length of one full sine period:
ML  block  fnum  periodlen  stepsize  remarks 

0  0  256  4096  0x00080  
0  1  256  2048  0x00100  
0  2  256  1024  0x00200  
0  3  256  512  0x00400  
0  4  256  256  0x00800  
0  5  256  128  0x01000  
0  6  256  64  0x02000  
0  7  256  32  0x04000  
1  7  256  16  0x08000  
2  7  256  8  0x10000  
3  7  256  5.33  0x18000  
4  7  256  4  0x20000  
5  7  256  3.2  0x28000  
6  7  256  2.67  0x30000  
7  7  256  2.29  0x38000  
8  7  256  2  0x40000  #1 
9  7  256    0x48000  #2 
15  7  511  +/ 7.77  0xEF880  #3 
=0x6F880 
As before the first 3 columns are the input parameters, the periodlen column is the measured result and the stepsize column contains our predicted value.
Only the really high values are interesting:
This all means we can ignore overflow in the stepsize calculation.
So the conclusion (so far):
Though the phase is also influenced by:
In this experiment I disabled these 3 influences (or as much as possible). Later we'll possibly have to extend the stepsize formula to take these effects into account.
From timetotime I look at the YM2413^{[1]} dieshot: http://siliconpr0n.org/map/yamaha/fhb013/mz_ns50xu/
This time I was actually able to find a feature in the dieshot that supports the conclusion of this post: there really seem to be 19 bits allocated to the phase counter(s).
In the large rectangle in the topright corner you can see 19 horizontal bands. In the left part of that rectangle you see a region that has much less (vertical) wires going across the bands. If you look very closely to the band(s) in that region you see features that (horizontally) repeat 18 times. (Look at the link above for a zoomable image).
That region contains 19 chains of 18 flipflops. Each flipflop stores a single bit. There are 19 bands next to each other, combined they form a 19bit value. There are 18 flipflops in a chain, that's one phasecounter for each of the 18 operators (9 modulators and 9 carriers). We also see that (only) the upper 10 of the 19 flipflops are routed further to a (relatively small) block of logic (that performs mirroring depending on the upper two bits?) and the output of that logic is further routed to a ROM (the sinetable).
The YM2413 only has the hardware to do one 'operatorcalculation' (modulator or carrier) at a time. This means that the data required for such a calculation somehow has to be routed to that operator logic. A chain of flipflops is one such possibility: (only) the bits at the head of the chain are used, the other bits are waiting in line (literally). You can also see some wires that connect the head of each chain back to the tail. Those connections close the circle so that the same value comes back every 18 'cycles'. Actually there still is a bit of extra logic present in the loop, this is likely the stepsize adder.
A couple of days ago (after I had already finished most experiments for this post) I discovered this forum:
http://forums.submarine.org.uk/phpBB/
The main focus in that forum is the OPL3, but there's a little info on the OPL2 as well. (YM2413 (OPLL) is an OPL2 derivative, so closer to OPL2 than to OPL3). For this post the following forumtopic is the most relevant:
Yamaha OPL3 research > Phase Generator
The information there seems to match, more or less, with my findings. There's one important difference:
Though also
So from that point of view the results match again.
<< YM2413 Reverse Engineering Notes 20150311  YM2413 Reverse Engineering Notes  YM2413 Reverse Engineering Notes 20150320 >>