Prepare for more than one File device.

This commit is contained in:
Andrew Alderwick 2022-03-17 18:31:33 +00:00
parent bca73244f0
commit 27089a77e4
4 changed files with 100 additions and 61 deletions

View File

@ -19,29 +19,32 @@ WITH REGARD TO THIS SOFTWARE.
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
static FILE *f; typedef struct {
static DIR *dir; FILE *f;
static char *current_filename = ""; DIR *dir;
static struct dirent *de; char *current_filename;
struct dirent *de;
enum { IDLE,
FILE_READ,
FILE_WRITE,
DIR_READ } state;
} UxnFile;
static enum { IDLE, static UxnFile uxn_file[POLYFILEY];
FILE_READ,
FILE_WRITE,
DIR_READ } state;
static void static void
reset(void) reset(UxnFile *c)
{ {
if(f != NULL) { if(c->f != NULL) {
fclose(f); fclose(c->f);
f = NULL; c->f = NULL;
} }
if(dir != NULL) { if(c->dir != NULL) {
closedir(dir); closedir(c->dir);
dir = NULL; c->dir = NULL;
} }
de = NULL; c->de = NULL;
state = IDLE; c->state = IDLE;
} }
static Uint16 static Uint16
@ -61,20 +64,20 @@ get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int f
} }
static Uint16 static Uint16
file_read_dir(char *dest, Uint16 len) file_read_dir(UxnFile *c, char *dest, Uint16 len)
{ {
static char pathname[4096]; static char pathname[4096];
char *p = dest; char *p = dest;
if(de == NULL) de = readdir(dir); if(c->de == NULL) c->de = readdir(c->dir);
for(; de != NULL; de = readdir(dir)) { for(; c->de != NULL; c->de = readdir(c->dir)) {
Uint16 n; Uint16 n;
if(de->d_name[0] == '.' && de->d_name[1] == '\0') if(c->de->d_name[0] == '.' && c->de->d_name[1] == '\0')
continue; continue;
if(strlen(current_filename) + 1 + strlen(de->d_name) < sizeof(pathname)) if(strlen(c->current_filename) + 1 + strlen(c->de->d_name) < sizeof(pathname))
sprintf(pathname, "%s/%s", current_filename, de->d_name); sprintf(pathname, "%s/%s", c->current_filename, c->de->d_name);
else else
pathname[0] = '\0'; pathname[0] = '\0';
n = get_entry(p, len, pathname, de->d_name, 1); n = get_entry(p, len, pathname, c->de->d_name, 1);
if(!n) break; if(!n) break;
p += n; p += n;
len -= n; len -= n;
@ -83,68 +86,69 @@ file_read_dir(char *dest, Uint16 len)
} }
static Uint16 static Uint16
file_init(void *filename) file_init(UxnFile *c, void *filename)
{ {
reset(); reset(c);
current_filename = filename; c->current_filename = filename;
return 0; return 0;
} }
static Uint16 static Uint16
file_read(void *dest, Uint16 len) file_read(UxnFile *c, void *dest, Uint16 len)
{ {
if(state != FILE_READ && state != DIR_READ) { if(c->state != FILE_READ && c->state != DIR_READ) {
reset(); reset(c);
if((dir = opendir(current_filename)) != NULL) if((c->dir = opendir(c->current_filename)) != NULL)
state = DIR_READ; c->state = DIR_READ;
else if((f = fopen(current_filename, "rb")) != NULL) else if((c->f = fopen(c->current_filename, "rb")) != NULL)
state = FILE_READ; c->state = FILE_READ;
} }
if(state == FILE_READ) if(c->state == FILE_READ)
return fread(dest, 1, len, f); return fread(dest, 1, len, c->f);
if(state == DIR_READ) if(c->state == DIR_READ)
return file_read_dir(dest, len); return file_read_dir(c, dest, len);
return 0; return 0;
} }
static Uint16 static Uint16
file_write(void *src, Uint16 len, Uint8 flags) file_write(UxnFile *c, void *src, Uint16 len, Uint8 flags)
{ {
Uint16 ret = 0; Uint16 ret = 0;
if(state != FILE_WRITE) { if(c->state != FILE_WRITE) {
reset(); reset(c);
if((f = fopen(current_filename, (flags & 0x01) ? "ab" : "wb")) != NULL) if((c->f = fopen(c->current_filename, (flags & 0x01) ? "ab" : "wb")) != NULL)
state = FILE_WRITE; c->state = FILE_WRITE;
} }
if(state == FILE_WRITE) { if(c->state == FILE_WRITE) {
if((ret = fwrite(src, 1, len, f)) > 0 && fflush(f) != 0) if((ret = fwrite(src, 1, len, c->f)) > 0 && fflush(c->f) != 0)
ret = 0; ret = 0;
} }
return ret; return ret;
} }
static Uint16 static Uint16
file_stat(void *dest, Uint16 len) file_stat(UxnFile *c, void *dest, Uint16 len)
{ {
char *basename = strrchr(current_filename, '/'); char *basename = strrchr(c->current_filename, '/');
if(basename != NULL) if(basename != NULL)
basename++; basename++;
else else
basename = current_filename; basename = c->current_filename;
return get_entry(dest, len, current_filename, basename, 0); return get_entry(dest, len, c->current_filename, basename, 0);
} }
static Uint16 static Uint16
file_delete(void) file_delete(UxnFile *c)
{ {
return unlink(current_filename); return unlink(c->current_filename);
} }
/* IO */ /* IO */
void void
file_deo(Device *d, Uint8 port) file_i_deo(int instance, Device *d, Uint8 port)
{ {
UxnFile *c = &uxn_file[instance];
Uint16 addr, len, res; Uint16 addr, len, res;
switch(port) { switch(port) {
case 0x5: case 0x5:
@ -152,16 +156,16 @@ file_deo(Device *d, Uint8 port)
DEVPEEK16(len, 0xa); DEVPEEK16(len, 0xa);
if(len > 0x10000 - addr) if(len > 0x10000 - addr)
len = 0x10000 - addr; len = 0x10000 - addr;
res = file_stat(&d->u->ram[addr], len); res = file_stat(c, &d->u->ram[addr], len);
DEVPOKE16(0x2, res); DEVPOKE16(0x2, res);
break; break;
case 0x6: case 0x6:
res = file_delete(); res = file_delete(c);
DEVPOKE16(0x2, res); DEVPOKE16(0x2, res);
break; break;
case 0x9: case 0x9:
DEVPEEK16(addr, 0x8); DEVPEEK16(addr, 0x8);
res = file_init(&d->u->ram[addr]); res = file_init(c, (char *)&d->u->ram[addr]);
DEVPOKE16(0x2, res); DEVPOKE16(0x2, res);
break; break;
case 0xd: case 0xd:
@ -169,7 +173,7 @@ file_deo(Device *d, Uint8 port)
DEVPEEK16(len, 0xa); DEVPEEK16(len, 0xa);
if(len > 0x10000 - addr) if(len > 0x10000 - addr)
len = 0x10000 - addr; len = 0x10000 - addr;
res = file_read(&d->u->ram[addr], len); res = file_read(c, &d->u->ram[addr], len);
DEVPOKE16(0x2, res); DEVPOKE16(0x2, res);
break; break;
case 0xf: case 0xf:
@ -177,8 +181,14 @@ file_deo(Device *d, Uint8 port)
DEVPEEK16(len, 0xa); DEVPEEK16(len, 0xa);
if(len > 0x10000 - addr) if(len > 0x10000 - addr)
len = 0x10000 - addr; len = 0x10000 - addr;
res = file_write(&d->u->ram[addr], len, d->dat[0x7]); res = file_write(c, &d->u->ram[addr], len, d->dat[0x7]);
DEVPOKE16(0x2, res); DEVPOKE16(0x2, res);
break; break;
} }
} }
Uint8
file_i_dei(int instance, Device *d, Uint8 port)
{
return d->dat[port];
}

View File

@ -10,4 +10,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE. WITH REGARD TO THIS SOFTWARE.
*/ */
void file_deo(Device *d, Uint8 port); #define POLYFILEY 1
void file_i_deo(int instance, Device *d, Uint8 port);
Uint8 file_i_dei(int instance, Device *d, Uint8 port);

View File

@ -17,6 +17,8 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE. WITH REGARD TO THIS SOFTWARE.
*/ */
static Device *devfile0;
static int static int
error(char *msg, const char *err) error(char *msg, const char *err)
{ {
@ -42,6 +44,18 @@ console_deo(Device *d, Uint8 port)
} }
} }
static void
file_deo(Device *d, Uint8 port)
{
file_i_deo(d - devfile0, d, port);
}
static Uint8
file_dei(Device *d, Uint8 port)
{
return file_i_dei(d - devfile0, d, port);
}
static Uint8 static Uint8
nil_dei(Device *d, Uint8 port) nil_dei(Device *d, Uint8 port)
{ {
@ -102,7 +116,7 @@ start(Uxn *u)
/* empty */ uxn_port(u, 0x7, nil_dei, nil_deo); /* empty */ uxn_port(u, 0x7, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x8, nil_dei, nil_deo); /* empty */ uxn_port(u, 0x8, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x9, nil_dei, nil_deo); /* empty */ uxn_port(u, 0x9, nil_dei, nil_deo);
/* file */ uxn_port(u, 0xa, nil_dei, file_deo); /* file */ devfile0 = uxn_port(u, 0xa, file_dei, file_deo);
/* datetime */ uxn_port(u, 0xb, datetime_dei, nil_deo); /* datetime */ uxn_port(u, 0xb, datetime_dei, nil_deo);
/* empty */ uxn_port(u, 0xc, nil_dei, nil_deo); /* empty */ uxn_port(u, 0xc, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0xd, nil_dei, nil_deo); /* empty */ uxn_port(u, 0xd, nil_dei, nil_deo);

View File

@ -42,7 +42,7 @@ static SDL_Rect gRect;
/* devices */ /* devices */
static Device *devscreen, *devmouse, *devctrl, *devaudio0; static Device *devscreen, *devmouse, *devctrl, *devaudio0, *devfile0;
static Uint8 zoom = 1; static Uint8 zoom = 1;
static Uint32 stdin_event, audio0_event; static Uint32 stdin_event, audio0_event;
@ -207,6 +207,18 @@ audio_deo(Device *d, Uint8 port)
} }
} }
static void
file_deo(Device *d, Uint8 port)
{
file_i_deo(d - devfile0, d, port);
}
static Uint8
file_dei(Device *d, Uint8 port)
{
return file_i_dei(d - devfile0, d, port);
}
static Uint8 static Uint8
nil_dei(Device *d, Uint8 port) nil_dei(Device *d, Uint8 port)
{ {
@ -253,7 +265,7 @@ start(Uxn *u, char *rom)
/* unused */ uxn_port(u, 0x7, nil_dei, nil_deo); /* unused */ uxn_port(u, 0x7, nil_dei, nil_deo);
/* control */ devctrl = uxn_port(u, 0x8, nil_dei, nil_deo); /* control */ devctrl = uxn_port(u, 0x8, nil_dei, nil_deo);
/* mouse */ devmouse = uxn_port(u, 0x9, nil_dei, nil_deo); /* mouse */ devmouse = uxn_port(u, 0x9, nil_dei, nil_deo);
/* file */ uxn_port(u, 0xa, nil_dei, file_deo); /* file */ devfile0 = uxn_port(u, 0xa, file_dei, file_deo);
/* datetime */ uxn_port(u, 0xb, datetime_dei, nil_deo); /* datetime */ uxn_port(u, 0xb, datetime_dei, nil_deo);
/* unused */ uxn_port(u, 0xc, nil_dei, nil_deo); /* unused */ uxn_port(u, 0xc, nil_dei, nil_deo);
/* unused */ uxn_port(u, 0xd, nil_dei, nil_deo); /* unused */ uxn_port(u, 0xd, nil_dei, nil_deo);