Disk file access on the CPC6128

Amstrad CPC and NC systems
Post Reply
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Disk file access on the CPC6128

Post by DarwinNE »

Hi to all,
I have a text adventure game targeting the 6128 that contains code to save its state.
Everything is written with quite standard C code (fopen, fclose, etc...)
If I compile with the -lcpcfs flag it seems to work on cassette, as specified here

https://github.com/z88dk/z88dk/wiki/Pla ... mstrad-CPC

That's cool, but my game is distributed on disk and it does not make much sense to save files only on the cassette.
Is there something that can allow accessing files on disk with the same interface?

Cheers,
D.
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

Hi to everybody,
Richard Loxley on Twitter pointed me towards this thread:

https://z88dk.org/forum/viewtopic.php?f=15&t=2718

The cited routine can thankfully be reached thanks to the Internet wayback machine:

https://web.archive.org/web/20081118194 ... oader.html
User avatar
dom
Well known member
Posts: 1310
Joined: Sun Jul 15, 2007 10:01 pm

Re: Disk file access on the CPC6128

Post by dom »

I must admit I did think that disc file access did work, and that thread does reveal why: I always run with memory/load/call.

It looks like we just need to add this (or equivalent) somewhere just before we call main in cpc_crt0.asm

Code: Select all

;;------------------------------------------------------------------------
;; store the drive number the loader was run from
ld hl,(&be7d)
ld a,(hl)                  
ld (drive+1),a

;;------------------------------------------------------------------------
ld c,&ff					;; disable all roms
ld hl,start					;; execution address for program
call mc_start_program		;; start it

;;------------------------------------------------------------------------

.start
call kl_rom_walk			;; enable all roms 

;;------------------------------------------------------------------------
;; when AMSDOS is enabled, the drive reverts back to drive 0!
;; This will restore the drive number to the drive the loader was run from
.drive ld a,0
ld hl,(&be7d)
ld (hl),a
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

Hello dom,
thank you very much!
If I understand well, the code means that when the program is launched, it stores the device that is used and directs all file I/O towards it? So, basically, if I load my game from disk, following I/O access will be done on it?

If it is the case, that would be perfect for me. Should I just wait until the code is included and then update z88dk or there is something I have to do?

Cheers,
D.
User avatar
dom
Well known member
Posts: 1310
Joined: Sun Jul 15, 2007 10:01 pm

Re: Disk file access on the CPC6128

Post by dom »

I'm taking a bit of time away from z88dk at the moment: too many things to sort out around the house, so I'm a bit slower than usual. I'll get round to this at some point this year though.

If you want to try it yourself and let me know whether it works that would be great
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

No problem at all!

I'm not an expert of the CPC platform, I may gave it a try: if I get something useful I will surely let you know.
User avatar
dom
Well known member
Posts: 1310
Joined: Sun Jul 15, 2007 10:01 pm

Re: Disk file access on the CPC6128

Post by dom »

I've just run a couple of tests:

1. Loading with memory, load, call doesn't need this code
2. Adding this code means the run & variant will work as well
3. Adding this code has no ill effects on the behaviour in case 1

So I've committed the code, the downside is that BASIC resets on program exit, but I don't think that's a massive issue for most programs.
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

Hi dom,
that's awesome! I'll give it a try in the next few days.

Thank you very much,
D.
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

Hello!

I installed the last nightly build for OSX:

Code: Select all

zcc - Frontend for the z88dk Cross-C Compiler - v17180-83d5b95-20201219
However, I still have the same behavior:
save_cpc.png
If I recompile the game and load it from a disk image, it still proposes me to save the game on the cassette.
Tomorrow, I will try to put together some simple code that may show how I use the functions, to see if I do have the same result.
You do not have the required permissions to view the files attached to this post.
User avatar
dom
Well known member
Posts: 1310
Joined: Sun Jul 15, 2007 10:01 pm

Re: Disk file access on the CPC6128

Post by dom »

Hi Davide,

If you could create a test program that would useful - maybe you are doing something different to the test program I'm using. I've put mine below (apologies for the state of it, it's been hacked a bit over the years!). I'm compiling with:

zcc +cpc test.c -create-app -subtype=dsk -lcpcfs

Code: Select all

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

unsigned char c[2048];

int main()
{
  FILE *file;
  int i1,finished =0;

  strcpy(c, "This is a test");
  while(finished == 0) {
  switch(fgetc_cons())
    {
      case 'o' : printf("open - ");
                 file=fopen("test.dat","rb");
                 if (file==NULL)
                   printf("fail\n");
                 else printf("ok\n");
                 break;

      case 'a' : printf("rdwr - ");
                 file=fopen("test.dat","r+b");
                 if (file==NULL)
                   printf("fail\n");
                 else printf("ok\n");
                 break;

#if 0
      case 's': printf("Seek start\n");
		if ( fseek(file, 0, SEEK_SET) ) {
			printf("Fail\n");
		} else printf("OK\n");
		break;
#endif

      case 'O' : printf("create - ");
                 file=fopen("test.dat","wb");
                 if (file==NULL)
                   printf("fail\n");
                 else printf("ok\n");
                 break;

      case 'p': fprintf(file, "Hello %d\n",12);
		printf("Printf\n");
		break;

      case 'c' : printf("close\n");
                 fclose(file);
                 break;

      case 'r' : printf("read - ");
                 i1=fread(c,2048,1,file);
                 printf("result %d\n",i1);
                 break;

      case 'w' : printf("write - ");
                 i1=fwrite(c,2048,1,file);
                 printf("result %d\n",i1);
                 break;

      case '0' : printf("c=0\n");
                 memset(c,0,2048);
                 break;

      case '1' : printf("c=0\n");
                 memset(c,1,2048);
                 break;
      case '.':
		printf("Quitting\n");
		finished = 1;
		break;

    }
	}

  return 0;
}

DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

Hello and thank you so much!

Here is some simple code that shows the issues I'm having:

Code: Select all

#include<stdio.h>

int main(void)
{
    FILE *f;
    f=fopen("test.txt","w");
    if(f==NULL) {
        printf("Error\n");
        return 1;
    }
    fprintf(f, "This is a test\n");
    fclose(f);
    return 0;
}
here is the makefile:

Code: Select all

all: cpc

cpc: test_cpc.c
	zcc +cpc test_cpc.c -lcpcfs -lm -subtype=dsk -create-app -o testcpc
I still have the access through the cassette.
I tried with your code and I got the same result:
issue_cpc.png
If everything works for you, I think that the most probable thing is that I could not update the z88dk properly in my system. I will have a deeper look today.
You do not have the required permissions to view the files attached to this post.
User avatar
dom
Well known member
Posts: 1310
Joined: Sun Jul 15, 2007 10:01 pm

Re: Disk file access on the CPC6128

Post by dom »

Thanks, I've just tried out your example and I get a file created on disc.

To summarise:

1. zcc +cpc file2.c -create-app -subtype=dsk -lcpcfs -m
2. mame cpc6128 -flop1 a.dsk
3. run "a.cpc",&1200
[CPC resets]
4. cat

And I can see file.txt on disk.

So, I think it might be something up with your install, if you add the -m option to the compilation you should then be able to disassemble the binary using z88dk-dis, eg for my compilation above:

z88dk-dis -o 0x1200 -x a.map a.bin

yields:

Code: Select all

start:
                    ld      hl,($be7d)                      ;[1200] 2a 7d be
                    ld      a,(hl)                          ;[1203] 7e
                    ld      ($1213),a                       ;[1204] 32 13 12
                    ld      c,$ff                           ;[1207] 0e ff
                    ld      hl,$120f                        ;[1209] 21 0f 12
                    call    $bd16                           ;[120c] cd 16 bd
start2:
                    call    $bccb                           ;[120f] cd cb bc
drive:
                    ld      a,$00                           ;[1212] 3e 00
                    ld      hl,($be7d)                      ;[1214] 2a 7d be
 
Which is the new code inserted to fix this issue.
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Disk file access on the CPC6128

Post by DarwinNE »

Thank you! I could check and saw I did not obtain the same code.
I realized I updated the compiler, but that I still had ZCCCFG pointing towards a set of libraries that were out of date...

Now everything seems to work properly.

Many thanks again for the amazing work on z88dk and the patience, it's wonderful to have a tool so flexible targeting so many different systems.
User avatar
dom
Well known member
Posts: 1310
Joined: Sun Jul 15, 2007 10:01 pm

Re: Disk file access on the CPC6128

Post by dom »

Excellent that’s great to hear.

Thank you for your kind words here and elsewhere.
Post Reply