Glitch-free controller reads with DMC?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: Glitch-free controller reads with DMC?

Post by Fiskbit »

I was working on rewriting Ben's code before he posted the 1.1 patch, but dropped it after that. It should detect the Hyperkin mouse correctly, so I'll continue work on it and will post it when I get it done. The detection will also allow the mouse to be on any desired bit in $4017, picking the lowest that matches. The default mask right now is $1F.

As for the cursor occasionally jumping, do you think it needs longer delays? It could also perhaps indicate that I didn't get the DMA timing just right, so occasionally an extra read happens, which would be too quick and corrupt the data (do we understand the nature of the corruption when the mouse is read too fast?). For it to be so rare, it feels likely that would be the desync case.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Glitch-free controller reads with DMC?

Post by rainwarrior »

Fiskbit wrote: Thu Feb 24, 2022 10:05 pmAs for the cursor occasionally jumping, do you think it needs longer delays? It could also perhaps indicate that I didn't get the DMA timing just right, so occasionally an extra read happens, which would be too quick and corrupt the data (do we understand the nature of the corruption when the mouse is read too fast?). For it to be so rare, it feels likely that would be the desync case.
Found the test ROM I made a few years ago: viewtopic.php?p=236491#p236491

So, trying it now...

If the delay between 2 bytes is not long enough, it seems like the tendency is for an extra 1 bit to appear at the beginning of the 3rd byte, shifting the rest of the result over by 1 bit. If the mouse is still, this often pushes the last-known-direction bit into the high bit of displacement, so you get a 64 instead of -0. It looks like if the delay is only 26 cycles instead of 28, I get errors at roughly the same rate I was seeing in that v1.1 patch... (at 28 it's fully stable, at 23 every report is shifted, in between the frequency increases).

So it's possible a delay that's too short would look pretty similar to a DPCM collision.

For the delay between bits... harder to tell what's going on because it's a little bit more chaotic, but it looks like maybe any bit in the stream could be duplicated as a result of reading too fast. (Actually I guess in the 2-byte cases, the extra "1" bit that always appears could just be the duplication of the last signature bit from the previous byte?)

So maybe either of the 2 delays being off by a few cycles could be responsible for this.
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: Glitch-free controller reads with DMC?

Post by Fiskbit »

Oh, oops, I didn't realize my wait there was 2 cycles short; good catch! I'll toss in another NOP and then retime the desync case to make sure that's still OK.

Thanks for the info about the corruption. So reading too soon can cause the previous bit to be read again, adding a bit into the stream? This actually makes it sound like the Hyperkin mouse would naturally be DMA-safe assuming it's read first after a strobe, since the extra read(s) would come so fast that it would return the same result without deleting a bit. That caveat is because reading a joypad register during DMA can cause the other joypad to clock, so reading another controller first could clock the mouse and lose bits. Even reading the mouse first, though, those collisions could corrupt the DMA value, which may be undesirable.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Glitch-free controller reads with DMC?

Post by rainwarrior »

Fiskbit wrote: Fri Feb 25, 2022 12:01 amSo reading too soon can cause the previous bit to be read again, adding a bit into the stream? This actually makes it sound like the Hyperkin mouse would naturally be DMA-safe assuming it's read first after a strobe, since the extra read(s) would come so fast that it would return the same result without deleting a bit.
Hmm, that might be possible. I don't really know how consistent the doubled bit is. In my test that lowered the delay between bits, e.g. at 11 cycles it looked like one of the bits was duplicated most of the time, but it wasn't consistent which bit, and sometimes it was probably 2 bits or 0 bits. So, it's possible that reading it fast enough would consistently just get the same bit twice, but I don't think I've sufficiently demonstrated that at this point.

(I suspect the load of the next bit doesn't take a fixed amount of time, but instead just depends on where the read lands relative to some internal microcontroller poll loop.)

My mouse test ROM might be a good place to integrate a different read code (with some additional DPCM playback). That might be easier to investigate than as a Shanghai patch:

viewtopic.php?p=231608#p231608
User avatar
Ben Boldt
Posts: 1149
Joined: Tue Mar 22, 2016 8:27 pm
Location: Minnesota, USA

Re: Glitch-free controller reads with DMC?

Post by Ben Boldt »

My initialization code has no extra delays anywhere, so that would explain why the Hyperkin mouse isn’t detected. I didn’t think of that.

The patch is currently hard-wired for the mouse buttons to press A and B. This is why you can’t click the mouse to press start at the title screen. It wasn’t intentional to use the mouse before getting to the playfield, but it could be a nice feature to add.
User avatar
Ben Boldt
Posts: 1149
Joined: Tue Mar 22, 2016 8:27 pm
Location: Minnesota, USA

Re: Glitch-free controller reads with DMC?

Post by Ben Boldt »

Unrelated, but Shanghai II was an arcade game:

https://www.youtube.com/watch?v=ieZ1GgQ00oM

Who knew that? I had no idea. This might even be where the Sunsoft bass samples came from.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Glitch-free controller reads with DMC?

Post by rainwarrior »

I think Moviemovies1 once showed me a positive identification of Gimmick's bass samples as coming from a Roland Sound Canvas device. I think SC-55 Slap Bass 2? I don't have the sounds at hand for comparison, but it was a dead ringer.

These Shanghai II sounds on Famicom do sound very much like the Gimmick samples, though I haven't tried to extract and compare them.
User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 569
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Glitch-free controller reads with DMC?

Post by Jarhmander »

Ben Boldt wrote: Fri Feb 25, 2022 7:02 pm Unrelated, but Shanghai II was an arcade game:

https://www.youtube.com/watch?v=ieZ1GgQ00oM

Who knew that? I had no idea. This might even be where the Sunsoft bass samples came from.
That's just ... Regular FM basses?
((λ (x) (x x)) (λ (x) (x x)))
User avatar
Ben Boldt
Posts: 1149
Joined: Tue Mar 22, 2016 8:27 pm
Location: Minnesota, USA

Re: Glitch-free controller reads with DMC?

Post by Ben Boldt »

Jarhmander wrote: Sat Feb 26, 2022 3:20 pm
Ben Boldt wrote: Fri Feb 25, 2022 7:02 pm Unrelated, but Shanghai II was an arcade game:

https://www.youtube.com/watch?v=ieZ1GgQ00oM

Who knew that? I had no idea. This might even be where the Sunsoft bass samples came from.
That's just ... Regular FM basses?
Yes, Sunsoft bass (Famicom DMC) is sampled from an FM bass. Everyone’s FM bass is tweaked a little bit different though, and it is historically interesting exactly what Sunsoft used.

It could also be that the arcade’s real FM bass patch closely or exactly matches the one from Roland. It makes a lot of sense: Sunsoft tried (successfully) to emulate the bass when porting Shanghai II from arcade to Famicom. With the amazing results, bits backwards and all, they reused it in more games as well.
Post Reply