FDS disk eject/insert loop bug?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

FDS disk eject/insert loop bug?

Post by Sour »

I stumbled upon a couple of FDS games that do something that seems weird while waiting for a disk to be inserted:

Code: Select all

loop:                    
  LDA $4032                
  AND #$01                 
  BEQ loop                 ;if a disk is currently inserted, loop (e.g wait for eject)
  LDA $4032                
  AND #$01                 
  BNE loop                 ;if no disk is currently inserted, loop (e.g wait for insert, but the loop goes back to the first check!)
And then others do this instead (which makes a lot more sense to me):

Code: Select all

wait_eject_loop:         
  LDA $4032                
  AND #$01                 
  BEQ wait_eject_loop      
  LDY #$FF                 
  JSR Delayms              
wait_insert_loop:        
  LDA $4032                
  AND #$01                 
  BNE wait_insert_loop
As far as I can tell, the first version can fail to recognize the newly inserted disk pretty often (which would force the user to eject the disk and then reinsert it). This can happen if you the "drive is empty" bit gets cleared while the code is in the first part of the loop (which loops until the disk is ejected).

My 6502 assembly skills are pretty terrible, so I could be understanding this wrong, but I can actually get this behavior several times in a row in the games (Aspic, Ao no Senritsu) that use the first version (e.g doing eject disk -> wait -> insert disk causes the execution to go back to the first loop and the game doesn't react at all)

The only way the first version would "reliably" be able to exit its loop was if the value of the "drive is empty" bit quickly toggled between 0 and 1 a number of times when physically ejecting or inserting a disk?
User avatar
*Spitfire_NES*
Posts: 306
Joined: Fri May 21, 2010 4:10 pm

Re: FDS disk eject/insert loop bug?

Post by *Spitfire_NES* »

Which games are these Sour? Would Aspic-Majaou no Noroi be one of them? This was something that was fixed in nestopia before by martin, under better disc drive emulation. It takes multiple times to get this game to load as a lot of the time ejecting/inserting the disk does nothing.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: FDS disk eject/insert loop bug?

Post by tepples »

Sour wrote:The only way the first version would "reliably" be able to exit its loop was if the value of the "drive is empty" bit quickly toggled between 0 and 1 a number of times when physically ejecting or inserting a disk?
You mean like switch bounce?
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: FDS disk eject/insert loop bug?

Post by Sour »

*Spitfire_NES* wrote:Would Aspic-Majaou no Noroi be one of them?
Yes, Aspic is one of the 2 games I found that have this problem. Thanks - I'll try to hunt down the change that was done in Nestopia's code for this.
tepples wrote:You mean like switch bounce?
Exactly what I was hoping to hear! I expected this might be a thing, but my knowledge of electronics is rather limited. If this happens on the FDS, it would make a lot of sense.
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: FDS disk eject/insert loop bug?

Post by lidnariq »

Switch bounce usually only happens in one direction (the "making contact" direction)..
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: FDS disk eject/insert loop bug?

Post by Sour »

lidnariq wrote:Switch bounce usually only happens in one direction (the "making contact" direction)..
It could probably still explain this, since the buggy loop occurs while waiting for the disk to be inserted, which would presumably push on a switch?

Also, I took a look at Nestopia's code, but it didn't look like anything special was being done for that flag. And Nestopia itself doesn't work properly with Aspic, either (need to switch the disk a bunch of times before it finally works)
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: FDS disk eject/insert loop bug?

Post by rainwarrior »

Sour wrote:
lidnariq wrote:Switch bounce usually only happens in one direction (the "making contact" direction)..
It could probably still explain this, since the buggy loop occurs while waiting for the disk to be inserted, which would presumably push on a switch?
I think the switch could be either... push to connect or push to break (my gut says the latter), but there's trivial ways that could either be a high or low signal by the time you read it. The Wiki's pinout says it's low when "media set" at least when coming in to the RAM adapter.
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: FDS disk eject/insert loop bug?

Post by lidnariq »

Briefly looking around for videos of people disassembling the drive itself, I think I see a big normally-closed switch on the side with yellow wires (that is opened by inserting a disk). But the people who uploaded the videos also comment on there being multiple switches inside (there's obviously also the limit switches for seeking to end and beginning), so I'm not certain if I'm both seeing that big one correctly and if it's also the one that detects insertion.
User avatar
*Spitfire_NES*
Posts: 306
Joined: Fri May 21, 2010 4:10 pm

Re: FDS disk eject/insert loop bug?

Post by *Spitfire_NES* »

Hey Sour, sorry i prob should have clarified, this fix was seemingly implemented into nestopia 1.33. I think in subsequent versions martin may have overwrote these fixes in fds since he obviously made many fixes to fds.cpp since then.

Here's a snippet from the changelog for that version:

* More accurate FDS disk drive emulation. Fixes "Aspic-Majaou no Noroi", "Hao Kun no Fushigina Tabi" and possibly others.
User avatar
*Spitfire_NES*
Posts: 306
Joined: Fri May 21, 2010 4:10 pm

Re: FDS disk eject/insert loop bug?

Post by *Spitfire_NES* »

Have you had any luck with this Sour? I tested out nestopia 1.33 and this bug is not in there. I also downloaded the source as well if you are interested in that version. :beer:
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: FDS disk eject/insert loop bug?

Post by Sour »

*Spitfire_NES* wrote:Have you had any luck with this Sour? I tested out nestopia 1.33 and this bug is not in there. I also downloaded the source as well if you are interested in that version. :beer:
I took a look at the differences between 1.33 and 1.34 before my previous post - but there was a pretty large amount of changes in the FDS code between both versions, which makes it hard to figure out what change might have altered the behavior (esp. since Nestopia's FDS implementation is huge, ~2.5k lines)

Taking a bit of a break from Mesen at the moment, since I've spent far too much time on it in the past weeks between releasing 0.9.4 & porting it to libretro - I'll probably take another look at this at some point, though.
Post Reply