Using fopen() in cc65

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
8bit-Dude
Posts: 13
Joined: 03 Oct 2017

Using fopen() in cc65

Post by 8bit-Dude »

Hey guyz,

I am coding a game for Atari 8bit, and want to use fopen() in cc65 to read some game related data.

I used mkatr.exe to create a bootable ATR, containing only my prog and data file.
However, I cannot seem open the file (I tried both upper and lower case filename).
fopen("myfile.dat") always return 0.

Does anyone know if a specific ATR format (SpartaDOS...) is required for fopen() to work?

Cheers,
Tony
whartung
Posts: 1004
Joined: 13 Dec 2003

Re: Using fopen() in cc65

Post by whartung »

No reason it should. The Ataris CIO is the moderator between your code and the actual disk OS, and it standardized all of the conventional I/O operations. (Extended stuff would have to be done via the equivalent of the XIO call, which is mostly just a raw call to CIO).

I don't know what a "bootable ATR" is, but if you're loading your code straight from the boot segment (or, say, a ROM Cartridge), vs having DOS load it via a command or whatever the equivalent of "autorun" your DOS uses, you probably don't have DOS loaded at all. That would explain the failure as well.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Using fopen() in cc65

Post by barrym95838 »

8bit-Dude wrote:
fopen("myfile.dat") always return 0.
Doesn't fopen() typically require two arguments?

FILE * fopen ( const char * filename, const char * mode );

Mike B.
User avatar
8bit-Dude
Posts: 13
Joined: 03 Oct 2017

Re: Using fopen() in cc65

Post by 8bit-Dude »

Thanks for the replies guys!

Here is a sample of my code:

Code: Select all

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <unistd.h>

void LoadBitmap(char *filename) 
{
	FILE* fp;
	
	// Open Map File
	fp = fopen(filename, "rb");	
    
    if (fp>0) {
        printf("Loading...\n");
        fread((char*)(0xA100), 1, 0x1E00, fp);
        fclose(fp);
    } else {
        printf("File not found...\n");
    }
}

int main (void)
{
    // Try loading file
    LoadBitmap("freeway.dat");
    printf("Ready to start MGM\n");
    cgetc (); 
        
    // Done
    return EXIT_SUCCESS;
}
When loading the resulting ATR file (with compiled prog and data file), I get the message file not found.
I have attached the ATR file (loadable in Altirra) and Sources.
Attachments
freeway.zip
(31.5 KiB) Downloaded 110 times
rwiker
Posts: 294
Joined: 03 Mar 2011

Re: Using fopen() in cc65

Post by rwiker »

Note that fp is a pointer; the check should be "fp != NULL" (or possibly "fp != 0"), not "fp > 0"...

... assuming Unix-like (or POSIX-like) semantics of fopen.
EdwardianDuck
Posts: 4
Joined: 04 Oct 2017

Re: Using fopen() in cc65

Post by EdwardianDuck »

As alluded to above, you need some form of DOS on the disk as well (or at least the A8 needs to have a DOS loaded). While the ATR is in SpartaDOS/X format, it doesn't actually contain SDX itself. Of course, you might be booting SDX from cartridge.

Attached (hopefully) is an ATR with Atari DOS 2.5 on it, with your program (renamed to AUTORUN.SYS) and data file on it. When booting this in Altirra, I no longer get the error message.

I've left the whole Atari DOS 2.5 on the image, but some of this can be deleted.

Jeremy
Attachments
freeway-dos25.zip
ATR Image
(40.8 KiB) Downloaded 121 times
User avatar
8bit-Dude
Posts: 13
Joined: 03 Oct 2017

Re: Using fopen() in cc65

Post by 8bit-Dude »

Ed, that's a fantastic reply, thank you so much!

Could you kindly tell me how I can compile such disk myself?
Did you use mkatr, or some other program?
EdwardianDuck
Posts: 4
Joined: 04 Oct 2017

Re: Using fopen() in cc65

Post by EdwardianDuck »

I'm at work without access to hobby stuff, so I cheated a bit by taking an existing ATR from a program I developed, deleted my program using Tools > Disk Explorer in Altirra then dragged and dropped your files into the image. I then renamed FREEWAY.COM to AUTORUN.SYS (again in Disk Explorer).

I can't remember exactly where I got the Atari DOS 2.5 ATR from, but it was easy enough to find online at the time I was writing the program.

Disk images are available here for various DOSes.

https://atariwiki.org/wiki/Wiki.jsp?pag ... %20DOS%202

Hopefully this helps.

Jeremy
User avatar
8bit-Dude
Posts: 13
Joined: 03 Oct 2017

Re: Using fopen() in cc65

Post by 8bit-Dude »

SUP, I got the same idea working with "atadim.exe"!

If you may have a little extra time, could you please look into my next problem here: viewtopic.php?f=2&t=4933

Thank you in advance!!
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Using fopen() in cc65

Post by barrym95838 »

Wow, Jeremy, you jumped right in out of nowhere and provided some excellent help! Thank you. You should Introduce yourself and tell us a bit about your background.

Mike B.
Post Reply