jotego / jtcores

FPGA cores compatible with multiple arcade game machines and KiCAD schematics of arcade games. Working on MiSTer FPGA/Analogue Pocket
https://patreon.com/jotego
GNU General Public License v3.0
240 stars 41 forks source link

cps: cps1 frequency response #556

Closed jotego closed 8 months ago

jotego commented 9 months ago

Artemio kindly ported his test suite to CPS1 and made some measurements using real boards. I have some questions about the measurements:

Pin 7 of the MSM6295 (PCM chip) selects the sampling frequency. Games do toggle this bit. This will have an effect on the aliasing as the Nyquist filter after the PCM chip is fixed. Thus for high speed setting, the filter will be poorer and there will be more aliasing. I wonder which setting you used for the test.

ArtemioUrbina commented 9 months ago

My pleasure. Yes, MDFourier has been ported to CPS1 using ccps, the current source code is here:

https://github.com/ArtemioUrbina/cps1mdfourier

Regarding Pin 7 of the MSM629, I will re-record the whole thing from all my PCBs again to compare having it set to both values, in order to confirm how much it changes and give you a proper comparison. I usually go with the high setting since I assume (maybe incorrectly) that since that covers the highest range and the analog path is the same, the widest range is being compared. I already coded this variant of the test and will be able to record tomorrow if you deem this to be useful.

jotego commented 9 months ago

There is no explicit low-pass filter for the FM sound on the board:

imagen

But I saw recently schematics for a Yamaha DAC, not sure the model, and I think the output was not buffered. So a resistor chain would be in series with the decoupling capacitors, forming a natural low pass filter. I think it was ikamusume, in Twitter, who published the schematics so I am going to ask him.

That would make this messy because the effective filter frequency could be signal-dependent, and hencen non-linear. I need to see how that resistor divider at the output of YM3012 was done. Some configurations can have a constant output impedance.

jotego commented 9 months ago

Another artifact that complicates things is that due to the short length of the quarter sine ROM inside the YM2151, for some high frequency settings you get less amplitude at the output. I can see this in simulation where the digital values have no filtering:

imagen

This means that you are measuring two things: the FM ROM table aliasing and the DAC output impedance. It would be nice to separate them. Maybe using a different waveform as the output of the YM2151 could provide a more consistent amplitude level across frequency. A high feedback value for the 1st operator can produce a triangular shape if I remember well. I would need to think about how to overcome this limitation.

jotego commented 8 months ago

There are schematics available for YM3014. From them, the output impedance for S[2:0] codes from 7 to 1 is 0.5R, 0.375R, 0.344R, 0.336R and then 0.333R. It looks like the schematics are missing a resistor, by adding it, the output impedance becomes exactly R/3 for all codes. I will assume the output impedance is constant on YM3012 too.

ArtemioUrbina commented 8 months ago

This means that you are measuring two things: the FM ROM table aliasing and the DAC output impedance. It would be nice to separate them. Maybe using a different waveform as the output of the YM2151 could provide a more consistent amplitude level across frequency. A high feedback value for the 1st operator can produce a triangular shape if I remember well. I would need to think about how to overcome this limitation.

I'll be waiting for suggestions on that area. Right now I can only compare the whole thing after the DAC since I am recording from the PCBs, but will follow any instructions to help.

Pin 7 of the MSM6295 (PCM chip) selects the sampling frequency. Games do toggle this bit. This will have an effect on the aliasing as the Nyquist filter after the PCM chip is fixed. Thus for high speed setting, the filter will be poorer and there will be more aliasing. I wonder which setting you used for the test.

Regarding the Above, I coded and tested a more complete version that runs the sweep in both pin7 settings:

DA__ALL_CPS1-89626A-4-SF2Board-A_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

Yellow is oin 7 in high, and orange is pin 7 in low. Filtering is indeed different in MisterFPGA with both, I'll need to record the other PCBs to check if filtering varies between PCBs between both settings. But it follows the same filtering in hardware and MAME in both settings when comparing a PCB and MAME as you can see below, since both difference lines trace above each other:

DA__ALL_CPS1-89626A-4-SF2Board-A_vs_mamewin_0000

I'll add more results tomorrow after recording from more hardware.

I added the option to test the "Reduction Specification" (amplitude) in ADPCM, and made MDfourier compare that in the time domain only. Here it is with zero reduction, above is the PCB and below is MiSTerFPGA:

TD_00202_1_OKI6295_AMP_HI_00000_CPS1-89626A-4-SF2Board-A_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

TD_00202_2_OKI6295_AMP_HI_00000_CPS1-89626A-4-SF2Board-A_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

Here it is at a different Reduction level:

TD_00206_1_OKI6295_AMP_HI_00004_CPS1-89626A-4-SF2Board-A_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

TD_00205_2_OKI6295_AMP_HI_00003_CPS1-89626A-4-SF2Board-A_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

I made these since I thought that maybe reduction levels were wrong and that made a perceived difference, but no, they match perfectly in amplitude, but you can clearly see the filtering difference even in the time domain:

jotego commented 8 months ago

Just pasting results before going through your latest reply.

The bounce back effect seeing in the reference waveform after the filter effect, cannot be seen with and without filter in the core, and using both truncated and non-truncated outputs of JT51. Could the bounce back be aliasing from your signal chain, @ArtemioUrbina ?

Reference imagen

JT51 with truncated and non-truncated outputs, no filter imagen

jotego commented 8 months ago

Thank you for the new measurements. I think I need to split this in two different issues, one for FM (just continue here) and one for ADPCM (I'll create a new one) as differences will have complete different causes in both of them. As far as I know, MAME does zero filtering on this driver. I think they do not truncate the FM signal either. This truncation comes from the YM3012, which drops a few lower bits of the signal when the amplitude is large. I can optionally drop them in the core, but I have never really noticed any difference. I leave them as they likely help prevent aliasing due to digital resampling in the FPGA signal path to HDMI.

ArtemioUrbina commented 8 months ago

I used toslink for MiSTer and the chain has been validated with stricter tests. I attach the new pattern generator in case you want to verify it, it is a new ADPCM tone at 1khz that is played with both Hi and Lo, and the 8 reduction levels to compare.

sf2ud.zip

And the flac files that generated these plots for both the PCB and MiSTer:

NewFullADPCM.zip

ArtemioUrbina commented 8 months ago

Just for reference, here is the difference comparison using the toslink capture chain:

DA__ALL_mdfourier-dac-48000_vs_BD-HDFury-Toslink2Coax-M_0000

jotego commented 8 months ago

Thank you. I can see the bouncing indeed. Apparently I had been recording a filtered version. The effect comes from sine wave ROM aliasing. This is a new record from the core. Excuse me for not looking at the spectra just yet. I want to get the easy parts aligned first.

imagen

jotego commented 8 months ago

By the way, @ArtemioUrbina, if you zoom in, you will see that the positive side of the sine wave is distorted in the PCB recording:

imagen

This effect does not show in the core. It may be related to some electrical problem in the board. I do not think it indicates true performance.

ArtemioUrbina commented 8 months ago

Thank you. I can see the bouncing indeed. Apparently I had been recording a filtered version. The effect comes from sine wave ROM aliasing. This is a new record from the core. Excuse me for not looking at the spectra just yet. I want to get the easy parts aligned first.

imagen

Ii was afraid I was doing the same and checked just after you mentioned it.

Just recorded the older CPS1 88617A-7b (GNG) with ADPCM and compared it to the newer CPS18 9626A-4 (Sf2) with ADPCM, and the filters in hardware for ADPCM follow the exact same curve in hi and low for pin 7.

DA__ALL_CPS1-88617A-7b-GNG-ADPCM_vs_CPS1-89626A-4-SF2Board-A_0000

As you can see yellow and orange trace the same difference curve.

Comparing CPS1 88617A-7b to Mister we can see the bouncing on the frequency domain, and also the difference in filtering for ADPCM:

DA__ALL_AVG_CPS1-88617A-7b-GNG-ADPCM_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

ArtemioUrbina commented 8 months ago

By the way, @ArtemioUrbina, if you zoom in, you will see that the positive side of the sine wave is distorted in the PCB recording:

imagen

This effect does not show in the core. It may be related to some electrical problem in the board. I do not think it indicates true performance.

Yeah, that's true. But it matches across all my boards and across all recordings as you can see in the FM comparisons and in all the recordings you have.

DA_CPS1-88617A-7b-GNG-ADPCM_vs_CPS1-89626A-4-SF2Board-A_0000_01YM2151

And there is no missing or extra frequency information when you compare them. The match in FM is basically perfect across PCBs from different games and revisions:

MISSING-EXTRA_T_SP_CPS1-89626A-4-SF2Board-A_S MISSING-A-T_SP_CPS1-88617A-7b-GNG-ADPCM_S

ArtemioUrbina commented 8 months ago

Here is the CPS1-88617A-7b-GNG-ADPCMFull recording: CPS1-88617A-7b-GNG-ADPCMFull.zip

Edit: I checked the other YM2151 systems for which I have MDfourier, and this is not present in the X68000 or IREM boards, but it is present in all CPS1 PCBs I own

Edit2: I found two boards that don't have those bumps: Magic Sword and Strider, both use 88617A-b7 and 88617b-5. Could be a capacitor issue? But common to all later revisions? I can recap one of the ones we have and check if it goes away, or I can use either board for all recordings if you prefer.

jotego commented 8 months ago

Comparing CPS1 88617A-7b to Mister we can see the bouncing on the frequency domain, and also the difference in filtering for ADPCM:

DA__ALL_AVG_CPS1-88617A-7b-GNG-ADPCM_vs_MisterFPGA-CPS1-ADPCM-Fu_0000

If I understand this correctly, this plot is the spectrum difference between the MiSTer core and the board. If the dB are negative, who is small? the core or the board? Should I read it as FM is the same in the 100-1000Hz band? But then PCM is about 3-6dB louder in the core around 200Hz? Then the core has a low-pass filter so the board eventually gets 6dB louder PCM than the core at 3kHz?

The board being 18dB+ louder than the core at 2.3kHz sounds unreasonable. The difference is too large and at a very hearable frequency. It is also a frequency at which the YM2151 will produce nice sine waves. It does not make sense unless the wave distortion is creating those HF harmonics.

Speaking of the distortion, supply noise is a likely culprit. Yes, capacitors will likely help. But we can just forget about the broken boards and use the Strider one as reference, if that one produces a clean sine wave.

I have recompiled the core without any filter on the ADPCM side. jtcps1.rbf.zip. Could you produce the spectrum difference again of this core vs Strider?

By the way, if you could produce a longer ADPCM burst, it would be easier to compare the waveforms visually. The spectrum is very useful but plain transient waveforms are also kind to the eye. The current frequency sweep is too fast for that and the results look really different.

Thank for all your help

ArtemioUrbina commented 8 months ago

If I understand this correctly, this plot is the spectrum difference between the MiSTer core and the board. If the dB are negative, who is small? the core or the board?

Yes it is the spectrum difference between the Board (as reference) and the MiSTer (as comparision). MdFourier uses the reference as the "truth" in a relative per comparison exercise. Thus, the plot is of spectral differences of the comparision signal (MiSTer in this case), in other words if the plot is negative it shows that MiSTer has that difference againt the PCB.

Should I read it as FM is the same in the 100-1000Hz band? But then PCM is about 3-6dB louder in the core around 200Hz? Then the core has a low-pass filter so the board eventually gets 6dB louder PCM than the core at 3kHz?

So yes, all the above are correct.

The board being 18dB+ louder than the core at 2.3kHz sounds unreasonable. The difference is too large and at a very hearable frequency. It is also a frequency at which the YM2151 will produce nice sine waves. It does not make sense unless the wave distortion is creating those HF harmonics.

I agree, but that's what the numbers say. All the process is open so we can audit any part of it if you like.

Speaking of the distortion, supply noise is a likely culprit.

Just as I read you I was about to tell you I had found the cause, and yes it is the power supply. I was using a new one, ofr those recordings, I went back to an era appropriate arcade power supply and the issue is gone.

Yes, capacitors will likely help. But we can just forget about the broken boards and use the Strider one as reference, if that one produces a clean sine wave.

With the power supply out of the equation, we now have clean form all of them, but I went with the Magic Sword one since it is a newer revision (88617A-b7) closer to the schematics we have.

I have recompiled the core without any filter on the ADPCM side. jtcps1.rbf.zip. Could you produce the spectrum difference again of this core vs Strider?

I will and will post results soon here, with the current build of MDfourier.

By the way, if you could produce a longer ADPCM burst, it would be easier to compare the waveforms visually. The spectrum is very useful but plain transient waveforms are also kind to the eye. The current frequency sweep is too fast for that and the results look really different.

I can and will to the largest possible size on the OKI, thank you. MDfourier can automatically produce the time domain plots of each "note" as the above comparing aligned segments (with the -u option), let me know if that helps you.

Thank for all your help

Thank you for caring.

ArtemioUrbina commented 8 months ago

I have recompiled the core without any filter on the ADPCM side. jtcps1.rbf.zip. Could you produce the spectrum difference again of this core vs Strider?

Here are the results comparing the unfiltered ADPCM:

DA__ALL_CPS1-88617A-7b-MagicSwor_vs_MisterFPGA-CPS1-MDF-unfi_0000

DA_CPS1-88617A-7b-MagicSwor_vs_MisterFPGA-CPS1-MDF-unfi_0000_02OKI6295_HI

DA_CPS1-88617A-7b-MagicSwor_vs_MisterFPGA-CPS1-MDF-unfi_0000_03OKI6295_LO

The board being 18dB+ louder than the core at 2.3kHz sounds unreasonable. The difference is too large and at a very hearable frequency. It is also a frequency at which the YM2151 will produce nice sine waves. It does not make sense unless the wave distortion is creating those HF harmonics.

That is due to the results in the PCB, check these out:

PCB: SP_A_CPS1-88617A-7b-MagicSwor_01YM2151_r

MiSTer:

SP_B_MisterFPGA-CPS1-MDF-unfi_01YM2151_r

Since the PCB is lower by that much at that frequency, MiSTer is that much higher when compared relative to it.

ArtemioUrbina commented 8 months ago

I must add that that filtering at 2.3khz is inherent to CPS1, the X68000 doesn't show it with its YM2151:

SP_A_CZ-600C-Artemio-Headphon_01FM

ArtemioUrbina commented 8 months ago

Here is the Unfiltered ADPCM MiSTer FPGA recording: MisterFPGA-CPS1-MDF-unfilteredADPCM.zip

ArtemioUrbina commented 8 months ago

By the way, if you could produce a longer ADPCM burst, it would be easier to compare the waveforms visually. The spectrum is very useful but plain transient waveforms are also kind to the eye. The current frequency sweep is too fast for that and the results look really different.

remnade a version that plays a 40 second ADPCM sweep, here are the recordings;

Magic Sword CPS1 88617A-7b CPS1-88617A-7b-MagicSword-ADPCM-40s.zip

MiSTerFPGA non filtered code via toslink:

MisterFPGA-CPS1-MDF-unfilteredADPCM-40s.zip

And here's MDFourier difference plot:

DA__ALL_CPS1-88617A-7b-MagicSwor_vs_misterfull40s_0000

Spectrograms:

PCB: SP__ALL_A_CPS1-88617A-7b-MagicSwor

MiSTer:

SP__ALL_B_misterfull40s

And the sweeps:

PCB: T_SP_A_CPS1-88617A-7b-MagicSwor

MiSTer:

T_SP_B_misterfull40s

jotego commented 8 months ago

Thank you very much for all the new measurements and plots. Let me comment on the FM side. The summary is:

My takeaway is that the FM channel in MiSTer is good as it is. Differences below 10Hz and 18kHz are hard to control because each user has a different signal chain and filters around those frequencies will always be present. In the mid frequencies, I do not see differences large enough to justify going after them, at least for now because the PCM channel does have a large mismatch.

About the distortion seen in the PCB recordings, one way around it would be to use a lower amplitude setting in the YM2151. We do not know what an out of the box PCB would be like but I would assume that the distortion was not present in fresh boards. Debugging the problem in the PCB can be quite a quest of its own as the number of possible culprits is quite large (power amplifier, opamps, DAC, digital noise coupling, capacitors, power supply...)

This spread sheet contains the frequency comparison data I have used for the above judgement. comp.ods.zip

jotego commented 8 months ago

I must add that that filtering at 2.3khz is inherent to CPS1, the X68000 doesn't show it with its YM2151:

SP_A_CZ-600C-Artemio-Headphon_01FM

Let me insist on this one: I think this is due to aliasing in the quarter-sine ROM table. It should always be present. For high frequency settings only a handful of values are read from the table, and this may skip the largest amplitude ones, so you get the effect of having a different gain.

For this to be repetitive, the phase counter must be reset at the beginning of each note. The key on command should reset it. It will also be sensitive to anything affecting the phase, like DT and LFO settings.

If you want to test this, you can set a very slow note, like a 10Hz tone and then just set the frequency setting above 2kHz at different positions of the output waveform. That will make the operator read the ROM table starting from a different position. Depending on when you trigger the frequency change relative to the current output, you will get a different amplitude for the HF sine wave. But, the important take away is that this does not represent real filtering due to external devices outside the YM2151 chip.

jotego commented 8 months ago

@ArtemioUrbina it looks like the latest mdf zip does not run the long ADPCM sweep but the short 11 second version. I do see the volume steps (nice addition!) Could you post again the .zip with the modified files for mdf.zip? There's no need to include other files that do not change.

ArtemioUrbina commented 8 months ago

@ArtemioUrbina it looks like the latest mdf zip does not run the long ADPCM sweep but the short 11 second version. I do see the volume steps (nice addition!) Could you post again the .zip with the modified files for mdf.zip? There's no need to include other files that do not change.

Sure thing, sorry about that.

mdf_40sADPCM.zip

jotego commented 8 months ago

First spectrum comparison for ADPCM:

I will work on a new low-pass filter to match the PCB spectrum better. I will look at the relative FM/ADPCM balance after that.

imagen

ArtemioUrbina commented 8 months ago

I will work on a new low-pass filter to match the PCB spectrum better. I will look at the relative FM/ADPCM balance after that.

This is great news, thank you very much for your impressive work.

Let me insist on this one: I think this is due to aliasing in the quarter-sine ROM table. It should always be present. For high frequency settings only a handful of values are read from the table, and this may skip the largest amplitude ones, so you get the effect of having a different gain.

You are right, my inexperience lead me to jump to conclusions. I am sorry about that. This is indeed not filtering, but it leaves me with a few questions that I still need to figure out:

1) I need to figure out why this does not happen in the x68000, since I basically ported the source code to CPS1 (only difference is that the z80 handles the YM2151 and not the 68k directly). I'll keep investigating this.

2) Why does the same behavior is present in MAME but not in the X68000 and not in MiSTer? Could these be related?

DA__ALL_CPS1-88617A-7b-MagicSwor_vs_MAME40s_0000

Here are the spectrograms,

CPS1 PCB:

SP_A_CPS1-88617A-7b-MagicSwor_01YM2151

MAME:

SP_B_MAME40s_01YM2151

BTW, I recorded directly from the pre-outs and after the amplifier, and the curves are basically the same just to check if we had any differences.

jotego commented 8 months ago

ADPCM issue moved to #559 for clarity. I will continue here after solving that one.

jotego commented 8 months ago

If you are using key on and key off to control the notes during the FM test, the phase counter should get reset and you should get the same waveforms consistently. So the aliasing caused by the large sampling of the quarter sine ROM should be repetitive. But, if other register values such as the LFO, AM, PM or DT1/2 happen to be set, you will get a different sampling effect. The X68000 could have other values preloaded? What does MAME produce for X68000?

jotego commented 8 months ago

@ArtemioUrbina, could you please give me a file that only generates four tones of the same amplitude on the ADPCM chip and that plays it in an infinite loop? That way I can fine tune the filter in real time. The tones would be at:

If possible, could the Z80 just work on its own without waiting for the M68k to trigger it? That will allow for Z80-only simulations, which are faster. It will also be more portable to other arcade systems.

Excuse me for asking for so many things, but, if you do not mind, reply in #559 so the ADPCM conversation is there.

jotego commented 8 months ago

Fixed in 22b08c09