VICE White Noise Bug?

You need an actual VIC.

Moderator: Moderators

Post Reply
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

VICE White Noise Bug?

Post by SparkyNZ »

I've spent most of the morning trying to get the latest white noise generation code to work from VICE (I have a Windows SDL app and I've borrowed the sound generation code from the VICE source).

I noticed the old code I'd borrowed from VICE 2.2 didn't sound the same when doing POKE36877,220 with VICE3.8. I compared WinVice 2.2 emulator with WinVice 3.8 emulator and sure enough - the code has changed between these two versions:

Image

When I compiled the latest 3.8 code I was getting no sound output at all for 36877. The other 3 channels were fine.

It was only when I started looking at the FabGL ESP32 library that I noticed a difference in the initial value for this:

VICE 3.8:

Code: Select all

static uint16_t noise_LFSR = 0x0000;
FABGL:

Code: Select all

static const uint16_t LFSRINIT = 0x0202;
As soon as I changed my VICE snippet as per below, I started to get sounds from 36877:

Code: Select all

static uint16_t noise_LFSR = 0x0202;
Is this a bug or something I should be reporting to the VICE team? See below - I can't figure out how it could possibly become non-zero:
Image
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: VICE White Noise Bug?

Post by groepaz »

What doesn't work though? (That it sounds different is quite expected - there should be a thread somewhere around here about the new reverse engineered LFSR)

also the quoted code is missing some lines? it should be:

Code: Select all

                        int bit3  = (noise_LFSR >> 3) & 1;
                        int bit12 = (noise_LFSR >> 12) & 1;
                        int bit14 = (noise_LFSR >> 14) & 1;
                        int bit15 = (noise_LFSR >> 15) & 1;
                        int gate1 = bit3 ^ bit12;
                        int gate2 = bit14 ^ bit15;
                        int gate3 = (gate1 ^ gate2) ^ 1;
                        int gate4 = (gate3 & enabled) ^ 1;
                        noise_LFSR0_old = noise_LFSR & 1;
                        noise_LFSR = (noise_LFSR << 1) | gate4;
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

Re: VICE White Noise Bug?

Post by SparkyNZ »

groepaz wrote: Mon Feb 05, 2024 1:11 pm What doesn't work though? (That it sounds different is quite expected - there should be a thread somewhere around here about the new reverse engineered LFSR)

also the quoted code is missing some lines? it should be:

Code: Select all

                        int bit3  = (noise_LFSR >> 3) & 1;
                        int bit12 = (noise_LFSR >> 12) & 1;
                        int bit14 = (noise_LFSR >> 14) & 1;
                        int bit15 = (noise_LFSR >> 15) & 1;
                        int gate1 = bit3 ^ bit12;
                        int gate2 = bit14 ^ bit15;
                        int gate3 = (gate1 ^ gate2) ^ 1;
                        int gate4 = (gate3 & enabled) ^ 1;
                        noise_LFSR0_old = noise_LFSR & 1;
                        noise_LFSR = (noise_LFSR << 1) | gate4;
"What doesn't work" is that I get no sound at all (ie silence) from 38677 when it is set to 220 and 36878 is set to 15.

I only copy and pasted search results for noise_LFSR because I was looking for assignments to noise_LFSR.

I get no sound at all on channel 3 unless I set the initial value of noise_LFSR to 0x0202. VICE sets the initial value of noise_LFSR to 0x0000. Obviously the version of VICE that's downloadable works and has sound. There may be something in the above code that I'm not understanding.

If initial noise_LFSR = 0x0000, then after this:

Code: Select all

                        int bit3  = (noise_LFSR >> 3) & 1;
                        int bit12 = (noise_LFSR >> 12) & 1;
                        int bit14 = (noise_LFSR >> 14) & 1;
                        int bit15 = (noise_LFSR >> 15) & 1;
noise_LFSR is still zero. bit3, bit12, bit14 and bit15 are all 0 too.

Code: Select all

                        int gate1 = bit3 ^ bit12;
                        int gate2 = bit14 ^ bit15;
gate1 and gate 2 are still 0.

Code: Select all

                       int gate3 = (gate1 ^ gate2) ^ 1;
                        int gate4 = (gate3 & enabled) ^ 1;
gate3 would be 0 ^ 1 which is 1.
gate4 would be (1 & enabled) ^ 1 which is 0 if enabled is 1

Code: Select all

                        noise_LFSR0_old = noise_LFSR & 1;
                        noise_LFSR = (noise_LFSR << 1) | gate4;
Both noise_LFSR and noise_LFSR_old remain as 0, so subsequent iterations are the same - noise_LFSR is always zero and edge_trigger never gets set below:

Code: Select all

edge_trigger = (noise_LFSR & 1) & !noise_LFSR0_old;
I don't understand how this code is working in VICE. What am I missing?

I doesn't really bother me all that much because I have sound in my own program when I set the initial value to 0x0202 and I've moved on.
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: VICE White Noise Bug?

Post by groepaz »

bit 0 of gate4 is either 0 or 1 on each iteration, and ORed into the LFSR ... so thats how 1 bits go into the LFSR
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

Re: VICE White Noise Bug?

Post by SparkyNZ »

groepaz wrote: Mon Feb 05, 2024 3:10 pm bit 0 of gate4 is either 0 or 1 on each iteration, and ORed into the LFSR ... so thats how 1 bits go into the LFSR

Code: Select all

int gate3 = (gate1 ^ gate2) ^ 1;    // Always 1
int gate4 = (gate3 & enabled) ^ 1; // ( 1 & 1 ) ^ 1 -> 0
I can only see this working if enabled is 0. I wonder if I'm setting enabled to 1 before the VIC is actually clocked.
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: VICE White Noise Bug?

Post by groepaz »

1 AND 1 is 1, not 0
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

Re: VICE White Noise Bug?

Post by SparkyNZ »

groepaz wrote: Mon Feb 05, 2024 3:16 pm 1 AND 1 is 1, not 0
Correct but 1 ^ 1 is 0
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: VICE White Noise Bug?

Post by groepaz »

Sorry, i interpreted wrong what you wrote...

yes it requires "enabled" being zero (for some cycles) to start. which it is after reset.
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

Re: VICE White Noise Bug?

Post by SparkyNZ »

groepaz wrote: Mon Feb 05, 2024 3:23 pm Sorry, i interpreted wrong what you wrote...

yes it requires "enabled" being zero (for some cycles) to start. which it is after reset.
I'll load it up and give that a try.. stay tuned..
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

Re: VICE White Noise Bug?

Post by SparkyNZ »

groepaz wrote: Mon Feb 05, 2024 3:23 pm yes it requires "enabled" being zero (for some cycles) to start. which it is after reset.

Code: Select all

  vic_sound_machine_init( 44100, VIC20_PAL_CYCLES_PER_SEC );

  //POKE( 36874, 230 );
  //POKE( 36875, 220 );
  //POKE( 36876, 0 );
  POKE( 36877, 220 );
  POKE( 36878, 15 );
Yup. If I wait 2 seconds and then do the last 2 "POKEs" it works.. so that's me jumping the gun and setting registers before the reset has been done :-)

Maybe FabGL did the same thing and had to hack it to work. :-)

So no bug - user error (ie. me). Mystery solved.
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: VICE White Noise Bug?

Post by groepaz »

you shouldnt have to wait, it takes 16 machine cycles to fill the lfsr with 1 bits:

Code: Select all

enabled:0 bit3:0 bit12:0 bit14:0 bit15:0 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:0001
enabled:0 bit3:0 bit12:0 bit14:0 bit15:0 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:0003
enabled:0 bit3:0 bit12:0 bit14:0 bit15:0 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:0007
enabled:0 bit3:0 bit12:0 bit14:0 bit15:0 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:000f
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:001f
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:003f
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:007f
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:00ff
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:01ff
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:03ff
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:07ff
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:0fff
enabled:0 bit3:1 bit12:0 bit14:0 bit15:0 gate1:1 gate2:0 gate3:0 gate4:1 lfsr:1fff
enabled:0 bit3:1 bit12:1 bit14:0 bit15:0 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:3fff
enabled:0 bit3:1 bit12:1 bit14:0 bit15:0 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:7fff
enabled:0 bit3:1 bit12:1 bit14:1 bit15:0 gate1:0 gate2:1 gate3:0 gate4:1 lfsr:ffff
enabled:0 bit3:1 bit12:1 bit14:1 bit15:1 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:ffff
enabled:0 bit3:1 bit12:1 bit14:1 bit15:1 gate1:0 gate2:0 gate3:1 gate4:1 lfsr:ffff
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 153
Joined: Tue Jan 18, 2011 2:23 am

Re: VICE White Noise Bug?

Post by SparkyNZ »

groepaz wrote: Mon Feb 05, 2024 3:30 pm you shouldnt have to wait, it takes 16 machine cycles to fill the lfsr with 1 bits:
I think I need to clock the Vic at least 1 cycle first though. The way my code was working, it was setting register values before the very first cycle - and that's why it would never work.
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: VICE White Noise Bug?

Post by groepaz »

you should clock it for at least 16 cycles first, yes :)
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
Post Reply