MEGA65 / mega65-core

MEGA65 FPGA core
Other
237 stars 84 forks source link

Direct drive audio DAC has unusual stepwise behaviors in a test case #793

Closed dansanderson closed 2 months ago

dansanderson commented 4 months ago

Core: release-0.96,20240224.00,3c10488 Hardware: MEGA65 R5 Audio output: 3.5mm jack

I don't know if the direct drive audio DAC registers are intended to be useful, but I thought I'd mention this in case it's indicative of a larger problem.

Here is a short BASIC program:

10 clrbit $d711,7
15 poke $d6f8,0
20 x&=$80
30 poke $d6f9,x&
40 vsync 100
50 poke $d6f9,0
60 vsync 200
70 get a$
80 if a$=chr$(29) then x&=x&+1
90 if a$=chr$(157) then x&=x&-1
100 print chr$(147);hex$(x&)
110 goto 30

This generates a pulse wave at the video mode's refresh rate, a low buzzing noise, in the left audio DAC channel. Pressing cursor left and cursor right adjusts the MSB of one of the sample values (the other sample being 0000), which affects the volume of the noise. Assuming the DAC registers take a 16-bit signed value, the expected behavior is to produce a smooth volume gradient through the values, silent at 00/FF, loudest at 80, and a smooth transition both ways.

The actual behavior is unusual, stepwise, and appears to change under conditions I haven't identified. Messing with the audio mixer's LEFT DIGI setting before running the program appears to produce consistent but incorrect behavior:

I get different stepwise behavior immediately after a cold boot without touching the mixer, and it differs each time as if based on uninitialized register values. Just one example:

I noticed different step points, e.g. D5 to D6, on different boots, but consistent behavior during a single power-on session.

(The character of the crudely generated pulse wave sometimes changes when crossing the sign barrier but this is OK, probably just a subtle change in pulse width due to how I'm generating it.)

I have not yet tried the R3 board or HDMI audio out.

MassiveBattlebotsFan commented 3 months ago

Was able to replicate this on the 3.5mm out on my R3A machine running a development core built from 18896e0 and ROM V920395. Unable to test HDMI audio as none of my monitors like it.

Tracked it down to both the SIDs putting a nonzero value on the audio bus, thus causing an occasional overflow when the mixer tries to mix the direct audio signal with the existing nonzero output value, as well as a bug in multiply_by_volume_coefficient causing $8000 to change sign to $0000 instead of $8000. Squashed the multiplication bug and added code that sets mixed_value to $8000 if an overflow is detected, haven't taken a look at the SIDs due to not being familiar with their operation. Tested the fix with the Dual SID file on the demo disk.

markkrueg commented 3 months ago

Another way to reproduce the distortion: Consistently reproduced on R3A with Release 0.96 core: 1) Set to 8580 SID, stereo, amp OFF 2) Boot DEMO disk 1 3) Select 3-Music, 2 DUAL SID COMPO, RETURN 4) Allow X-FILES to play.

Very obvious (not subtle) distortion will be aparent at about: 1 minute 38 seconds

It will continue randomly after this point.

It occurs on both HDMI and 3.5mm analog.

I have tested @MassiveBattlebotsFan pull request and the problem is gone. I have not had any problems with this fix in any other game or demo I have tested.