English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 19 June 2022, 14:52   #1
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Floppy keeps spinning after fread

Hello all,

I'm developing a game in C, in my code I load assets from floppy with fread/fclose functions, it works, but the floppy keeps spinning even after the load is completed.

How can I solve it?

Thank you in advance,

Lorenzo
Csoft is offline  
Old 19 June 2022, 15:41   #2
Galahad/FLT
Going nowhere
 
Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 8,986
Quote:
Originally Posted by Csoft View Post
Hello all,

I'm developing a game in C, in my code I load assets from floppy with fread/fclose functions, it works, but the floppy keeps spinning even after the load is completed.

How can I solve it?

Thank you in advance,

Lorenzo
Are you shutting down the system after loading files or stopping multitasking with Forbid?
Galahad/FLT is offline  
Old 19 June 2022, 15:55   #3
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by Galahad/FLT View Post
Are you shutting down the system after loading files or stopping multitasking with Forbid?
No, I'm not shutting down the system at all. I'm trying to keep everything OS friendly.
Csoft is offline  
Old 19 June 2022, 19:28   #4
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
what's the flow?
fopen/fread/fclose? (c library)
Open/Read/Close? (OS)
alkis is offline  
Old 21 June 2022, 17:36   #5
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by Csoft View Post
No, I'm not shutting down the system at all. I'm trying to keep everything OS friendly.
Then there is no reason it could ever happen. And it should be easy to isolate the problem with some test code doing fopen/fread/fclose.

Is it some strange file system? Or standard FFS?
Note, that a running motor for a few seconds after the last access is normal.
phx is offline  
Old 21 June 2022, 21:52   #6
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by phx View Post
Then there is no reason it could ever happen. And it should be easy to isolate the problem with some test code doing fopen/fread/fclose.

Is it some strange file system? Or standard FFS?
Note, that a running motor for a few seconds after the last access is normal.
Simply this, with standard Amiga FFS, it keeps spinning forever:

Code:
FILE *fp = fopen(filename, "rb");
    if (fp) {
        fread(dest, sizeof(unsigned char), size, fp);
    } else {
        printf("error: file '%s' not found\n", filename);
    }
    fclose(fp);
Csoft is offline  
Old 21 June 2022, 22:31   #7
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
Please post a complete source. For example, you could run into a buffer overrun here, overwriting some Os variables if "dest" is smaller than "size" bytes. Thus, what is "dest", and "size", and how are they allocated? A standard "fread" (or an underlying Read()) will not cause this.
Thomas Richter is offline  
Old 21 June 2022, 22:41   #8
Asman
68k
 
Asman's Avatar
 
Join Date: Sep 2005
Location: Somewhere
Posts: 828
Quote:
Originally Posted by Csoft View Post
Simply this, with standard Amiga FFS, it keeps spinning forever:

Code:
FILE *fp = fopen(filename, "rb");
    if (fp) {
        fread(dest, sizeof(unsigned char), size, fp);
    } else {
        printf("error: file '%s' not found\n", filename);
    }
    fclose(fp);
What about such code, it still keeps spinning forever ?

Code:
FILE *fp = fopen(filename, "rb");

if (fp)
{
    fread(dest, sizeof(unsigned char), size, fp);
    fclose(fp);
}
else
{
    printf("error: file '%s' not found\n", filename);
}
Furthermore you can also print value from fclose, should be 0.
Asman is offline  
Old 22 June 2022, 15:09   #9
Olaf Barthel
Registered User
 
Join Date: Aug 2010
Location: Germany
Posts: 532
Quote:
Originally Posted by Csoft View Post
Hello all,

I'm developing a game in C, in my code I load assets from floppy with fread/fclose functions, it works, but the floppy keeps spinning even after the load is completed.
Which operating system version are you developing and testing your game on? Are you using timer.device I/O requests in your game? Any chance you might be using a vertical blanking interrupt handler in your game?

It's the job of the file system to tell the floppy disk drive to spin down and give it a rest. This should happen within 3-4 seconds of no file system activity taking place, such as the last read or write operation. Because the file system uses timer.device to schedule periodic tests for activity, accidentally knocking out timer.device processing may cause the spindown never to take place (trackdisk.device does not spin down on its own accord).

Question is whether your game code might be responsible for that.
Olaf Barthel is offline  
Old 22 June 2022, 17:03   #10
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by Olaf Barthel View Post
Which operating system version are you developing and testing your game on? Are you using timer.device I/O requests in your game? Any chance you might be using a vertical blanking interrupt handler in your game?

It's the job of the file system to tell the floppy disk drive to spin down and give it a rest. This should happen within 3-4 seconds of no file system activity taking place, such as the last read or write operation. Because the file system uses timer.device to schedule periodic tests for activity, accidentally knocking out timer.device processing may cause the spindown never to take place (trackdisk.device does not spin down on its own accord).

Question is whether your game code might be responsible for that.
I'm working on 3.0. No use of timer.device, but I DO use a vblank interrupt, yes. Could be this the source of the problem?
Csoft is offline  
Old 22 June 2022, 20:09   #11
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
If the system timer is not run, yes. What is the return code of your code? Note, in particular, that your code shall return with Z flag set as otherwise the exec interrupt handler assumes that your code handled the interrupt completely, and will not call the remaining system interrupt handlers, including the one of the timer.device.
Thomas Richter is offline  
Old 30 August 2022, 23:11   #12
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by Thomas Richter View Post
If the system timer is not run, yes. What is the return code of your code? Note, in particular, that your code shall return with Z flag set as otherwise the exec interrupt handler assumes that your code handled the interrupt completely, and will not call the remaining system interrupt handlers, including the one of the timer.device.
I'm puzzled. I tried to not stop the system with Permit(), I disabled the vblank interrupt but I have a mod playroutine that starts a CIA interrupt. I do the following:

Read everything I must read from disk.
Wait for 5 seconds.
The floppy stops to spin.
After 5 seconds I install the CIA interrupt and the floppy restarts to spin!!!

I'm using FS-Uae.
Csoft is offline  
Old 31 August 2022, 10:04   #13
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
Quote:
Originally Posted by Csoft View Post
Read everything I must read from disk.
Wait for 5 seconds.
The floppy stops to spin.
After 5 seconds I install the CIA interrupt and the floppy restarts to spin!!!

I'm using FS-Uae.
Assuming the emulator isn't faulty, I'd say your CIA ISR setup is (wrongly) messing with some CIA registers which then causes the floppy to turn on.
hooverphonique is offline  
Old 31 August 2022, 10:21   #14
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
Quote:
Originally Posted by Csoft View Post
I'm puzzled. I tried to not stop the system with Permit(), I disabled the vblank interrupt but I have a mod playroutine that starts a CIA interrupt.
If you remove the system VBLank, then the timer.device will stop working, and by that the file system lost all its time information, including the timerequest it generated to stop the floppy motor.


In short: You cannot "partially" stop AmigaOs. Use the Os, dude!
Thomas Richter is offline  
Old 31 August 2022, 11:47   #15
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by Thomas Richter View Post
If you remove the system VBLank, then the timer.device will stop working, and by that the file system lost all its time information, including the timerequest it generated to stop the floppy motor.


In short: You cannot "partially" stop AmigaOs. Use the Os, dude!
I didn't disable the system VBlank, I removed completely my VBlank interrupt installation by commenting it.

This problem persists even if I don't disable the OS at all.

Probabily is the ptplayer routine which do some mess with CIA, I'll check it.
Csoft is offline  
Old 31 August 2022, 13:02   #16
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by Csoft View Post
I didn't disable the system VBlank, I removed completely my VBlank interrupt installation by commenting it.

This problem persists even if I don't disable the OS at all.

Probabily is the ptplayer routine which do some mess with CIA, I'll check it.
No. It's not. It seems pure madness. I tried the following:

1) I do NOT call Forbid() anymore.
2) I do NOT install my VBlank
3) I do NOT install CIA interrupt (ptplayer)
3) After all disk read I wait 10 seconds and the spin stops.

I found the source of the problem:

4) The first time I play a sound the spin sound restarts!!!!!!!

This happens on both FS-Uae and Winuae, so I think it's not an emulator related issue.
Csoft is offline  
Old 31 August 2022, 14:32   #17
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,214
Paula is unrelated to the floppy motor control, which is in the hands of CIA, resp. the trackdisk.device controlling CIA. Thus, if this is related to playing sounds, probably something is wrong with your player function.
Thomas Richter is offline  
Old 31 August 2022, 14:44   #18
Csoft
Registered User
 
Join Date: Apr 2021
Location: Italy
Posts: 21
Quote:
Originally Posted by Thomas Richter View Post
Paula is unrelated to the floppy motor control, which is in the hands of CIA, resp. the trackdisk.device controlling CIA. Thus, if this is related to playing sounds, probably something is wrong with your player function.
I'm not doing anything special with my sound playing function, here it is

Code:
UWORD audioDmaBits[] = {
    DMAF_AUD0,
    DMAF_AUD1,
    DMAF_AUD2,
    DMAF_AUD3
};

/**
 * @brief Play the sound
 * 
 * Period = 1 / (samplerate * 2,81937 * 10^-7)
 *          
 * @param sound 
 * @param period 
 * @param volume 
 * @param audiochannel 
 */
void playsound(sound* sound,UWORD period,UWORD volume,USHORT audiochannel) {

    if (!mt_Enable) {
        
        // Stop audiochannel's DMA in case there is an already playing sound in the audiochannel
        custom.dmacon = audioDmaBits[audiochannel];    
        custom.aud[audiochannel].ac_ptr = sound->sampledata;
        custom.aud[audiochannel].ac_len = sound->size;
        custom.aud[audiochannel].ac_per = period;
        custom.aud[audiochannel].ac_vol = volume;
        custom.dmacon = DMAF_SETCLR | audioDmaBits[audiochannel];    // Start sound playback

        
    } 
    // The following is called if I'm using ptplayer, which have its own playback facilities
    // the problem IS present even in this case
    else {
        SfxStructure pt_sound;
        pt_sound.sfx_ptr = sound->sampledata;
        pt_sound.sfx_len = sound->size;
        pt_sound.sfx_per = period;
        pt_sound.sfx_vol = volume;
        pt_sound.sfx_cha = audiochannel;
        pt_sound.sfx_pri = 127;
        mt_playfx(&custom,&pt_sound);
    }
}
Cannot understand why the spin stops after n seconds and then restarts if I play a sound...
Csoft is offline  
Old 31 August 2022, 15:26   #19
Galahad/FLT
Going nowhere
 
Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 8,986
When the floppy spin sound restarts, does the floppy light onscreen turn on with the sound or does it stay off?
Galahad/FLT is offline  
Old 31 August 2022, 19:09   #20
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,038
audioChannel is 0-3 and audioDmaBits is { 1, 2, 4, 8 }?
a/b is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
(A1200) Chinon FZ-354 Floppy Drive is spinning randomly Sim085 support.Hardware 3 07 January 2022 08:54
CD Spinning Problems spawnerbr support.Hardware 0 21 August 2020 06:46
Standard C libraries? aka fopen,fread,fwrite FirstNE Coders. C/C++ 8 07 August 2020 22:05
Problem with fread while booting game from adf MacSpain Coders. General 5 16 May 2020 11:34
Amiga 500 Panasonic floppy drive only spinning sometimes. h4tt3n support.Hardware 5 24 January 2016 16:20

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:18.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.22638 seconds with 15 queries