What am I doing wrong with my sweeps?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

What am I doing wrong with my sweeps?

Post by miker00lz »

I gave up on sweeps a few years ago, and now I'm trying to finally resolve the problems I have always had with them. Either they sweep way too fast, the pitch is way too low, or other similar glitchy behavior occurs. This is what happens every time I clock the sweep unit:

Code: Select all

Private Sub sweepclock()
    Dim s As Long, wl As Long, chan As Long
    
    If allowsweeps = 0 Then Exit Sub

    For chan = 0 To 1
        If (square(chan).sweep > 0) Then square(chan).sweep = square(chan).sweep - 1
        If ((square(chan).sweepenable <> 0) And (square(chan).sweep > 0)) Then
            wl = square(chan).period
            s = wl \ (2 ^ square(chan).sweep)
            If square(chan).negate Then s = (Not s) + (chan Xor 1)
            wl = square(chan).period + s
            If ((wl < &H800&) And (wl > 7)) Then square(chan).sweepresult = wl Else square(chan).sweepresult = 0
        Else
            square(chan).sweepresult = 0
        End If
    Next chan
End Sub
In another part of the APU code, when generating the current sample to be output, if the sweep unit is enabled and .sweepresult calculated in the above code is greater than zero I use that value as the effective period. If sweep is enabled and .sweepresult is zero, the channel is silenced. Does anybody see at a glance what it is that I'm doing wrong? :|
User avatar
oRBIT2002
Posts: 687
Joined: Sun Mar 19, 2006 3:06 am
Location: Gothenburg/Sweden

Re: What am I doing wrong with my sweeps?

Post by oRBIT2002 »

Check out the psuedocode by Disch, it's helped me alot. Found in this thread:
viewtopic.php?f=3&t=13767
User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: What am I doing wrong with my sweeps?

Post by miker00lz »

I've been comparing, but I'm still having no luck. I've got to be missing something.
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Re: What am I doing wrong with my sweeps?

Post by Disch »

VB is barf and I have a hard time reading it.... so I might be misreading some of this... but from what I can see:


1)

Code: Select all

        If (square(chan).sweep > 0) Then square(chan).sweep = square(chan).sweep - 1
        If ((square(chan).sweepenable <> 0) And (square(chan).sweep > 0)) Then
That 2nd line looks like it is happening always. It should have an 'else' because you should only do it if the sweep counter was zero at the time of the clock.


2) 'sweepresult' probably should not be a separate variable. The sweep unit directly modifies 'period'

3) You might want to consider a separate approach to indicate whether or not the sweep unit is forcing silence on the channel. Your current approach will only silence the channel if sweep is enabled -- but it should be silencing it even when disabled. See my pseudocode in the thread oRBIT2002 linked for a recommended way to handle this.
User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: What am I doing wrong with my sweeps?

Post by miker00lz »

Disch wrote:VB is barf and I have a hard time reading it.... so I might be misreading some of this... but from what I can see:


1)

Code: Select all

        If (square(chan).sweep > 0) Then square(chan).sweep = square(chan).sweep - 1
        If ((square(chan).sweepenable <> 0) And (square(chan).sweep > 0)) Then
That 2nd line looks like it is happening always. It should have an 'else' because you should only do it if the sweep counter was zero at the time of the clock.


2) 'sweepresult' probably should not be a separate variable. The sweep unit directly modifies 'period'

3) You might want to consider a separate approach to indicate whether or not the sweep unit is forcing silence on the channel. Your current approach will only silence the channel if sweep is enabled -- but it should be silencing it even when disabled. See my pseudocode in the thread oRBIT2002 linked for a recommended way to handle this.
Agreed VB is terrible, especially for emulators. That's why I'm doing it. I wrote a NES emulator and a PC emulator in C a few years ago, but have a soft spot for VB since I learned how to code using that and QB... just wanted to see how good of an emu I could make with it.

I'm going to try your suggestions and see what I come up with, thanks!

BTW, if you think NES in VB6 is a waste of time... check this out: http://www.vbforums.com/showthread.php? ... C-emulator!
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What am I doing wrong with my sweeps?

Post by tepples »

miker00lz wrote:Agreed VB is terrible, especially for emulators.
VB6 I'm inclined to agree. But what makes VB.NET particularly more "terrible" than C#? I thought the "Visual Fred" camp said VB.NET was C# semantics with different syntax.
User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: What am I doing wrong with my sweeps?

Post by miker00lz »

tepples wrote:
miker00lz wrote:Agreed VB is terrible, especially for emulators.
VB6 I'm inclined to agree. But what makes VB.NET particularly more "terrible" than C#? I thought the "Visual Fred" camp said VB.NET was C# semantics with different syntax.
Nothing makes it worse than C#, you're right. When I say VB I only refer to VB6.

VB6 is a great language for rapid development of business apps and small utilities, but that's about it. Emulators? Fuhgeddaboutit... doesn't even have unsigned integers!!
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Re: What am I doing wrong with my sweeps?

Post by Disch »

miker00lz wrote:Fuhgeddaboutit... doesn't even have unsigned integers!!

Neither does Java =P
User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: What am I doing wrong with my sweeps?

Post by miker00lz »

I'm not particularly fond of Java either, lol. Took a few classes for it in college. It's good for easy cross-platform compatibility.

Anyway, I got the sweeps more or less working. Still some bugs, but I'll work them out. Thanks Disch!

Here is an mp3 of me playing through the SMB world 1-1: http://proliant.rubbermallet.org/vbnes-smb.mp3

Pipes sound pretty good, everything else is still off.

And just for fun, here is vbNES playing a cool chiptune by zanzan: http://proliant.rubbermallet.org/vbnes- ... _over.flac

:)
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Re: What am I doing wrong with my sweeps?

Post by Disch »

Sounds like you are letting your linear counter wrap from 0->Load instead of stopping it at zero.

It also sounds like you are clocking all subunits way to fast -- which suggests your Frame Counter is being clocked too fast.

Let's see some tracelogs! Tracelogs tracelogs tracelogs. Make some logs that show when $4017 is being written to, Frame Counter clocks, and what the Linear counter is doing on each of its clocks.

These problems are so easy to solve with tracelogs!
Post Reply