aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-05-16 19:01:52 +0200
committerBad Diode <bd@badd10de.dev>2022-05-16 19:01:52 +0200
commite7cd6e1598c8fa0b11b4ddc23b9037be7749e920 (patch)
treebebf41fc3df5ff2f335772cc0d658d7d63341dc0
parentaea3279d8730b9e921c7c23387ee3a803d91a7cc (diff)
downloaduxngba-main.tar.gz
uxngba-main.zip
Update file device to support multiple file devsHEADmain
-rw-r--r--src/file.c69
-rw-r--r--src/main.c10
2 files changed, 42 insertions, 37 deletions
diff --git a/src/file.c b/src/file.c
index a45d59f..6dba2f9 100644
--- a/src/file.c
+++ b/src/file.c
@@ -13,65 +13,68 @@ WITH REGARD TO THIS SOFTWARE.
13 13
14#include "posprintf.h" 14#include "posprintf.h"
15 15
16static File f = {0}; 16typedef struct {
17static char *current_filename = ""; 17 File f;
18 char *filename;
19 enum {
20 IDLE,
21 FILE_READ,
22 FILE_WRITE,
23 } state;
24} UxnFile;
18 25
19static enum { 26static UxnFile uxn_file[2];
20 IDLE,
21 FILE_READ,
22 FILE_WRITE,
23} file_state;
24 27
25void 28void
26file_reset(void) { 29file_reset(UxnFile *f) {
27 f = (File){0}; 30 f->f = (File){0};
28 file_state = IDLE; 31 f->state = IDLE;
29} 32}
30 33
31u16 34u16
32file_init(void *filename) { 35file_init(UxnFile *f, void *filename) {
33 file_reset(); 36 file_reset(f);
34 current_filename = filename; 37 f->filename = filename;
35 return 0; 38 return 0;
36} 39}
37 40
38u16 41u16
39file_read(void *dest, u16 len) { 42file_read(UxnFile *f, void *dest, u16 len) {
40 if(file_state != FILE_READ) { 43 if(f->state != FILE_READ) {
41 file_reset(); 44 file_reset(f);
42 f = fs_open_file(current_filename, FS_OPEN_READ); 45 f->f = fs_open_file(f->filename, FS_OPEN_READ);
43 file_state = FILE_READ; 46 f->state = FILE_READ;
44 } 47 }
45 if (f.index == FS_NULL) { 48 if (f->f.index == FS_NULL) {
46 return 0; 49 return 0;
47 } 50 }
48 return fs_read(dest, len, &f); 51 return fs_read(dest, len, &f->f);
49} 52}
50 53
51u16 54u16
52file_write(void *src, u16 len, u8 flags) { 55file_write(UxnFile *f, void *src, u16 len, u8 flags) {
53 if(file_state != FILE_WRITE) { 56 if(f->state != FILE_WRITE) {
54 file_reset(); 57 file_reset(f);
55 OpenMode mode = (flags & 0x01) ? FS_OPEN_APPEND : FS_OPEN_WRITE; 58 OpenMode mode = (flags & 0x01) ? FS_OPEN_APPEND : FS_OPEN_WRITE;
56 f = fs_open_file(current_filename, mode); 59 f->f = fs_open_file(f->filename, mode);
57 file_state = FILE_WRITE; 60 f->state = FILE_WRITE;
58 } 61 }
59 if (f.index == FS_NULL) { 62 if (f->f.index == FS_NULL) {
60 return 0; 63 return 0;
61 } 64 }
62 return fs_write(src, len, &f); 65 return fs_write(src, len, &f->f);
63} 66}
64 67
65u16 68u16
66file_stat(void *dest, u16 len) { 69file_stat(UxnFile *f, void *dest, u16 len) {
67 if(len < strlen(current_filename) + 7) { 70 if(len < strlen(f->filename) + 7) {
68 return 0; 71 return 0;
69 } 72 }
70 if (f.index == FS_NULL) { 73 if (f->f.index == FS_NULL) {
71 posprintf(dest, "!!!! %s\n", current_filename); 74 posprintf(dest, "!!!! %s\n", f->filename);
72 } else { 75 } else {
73 u16 file_size = fs_file_size(&f); 76 u16 file_size = fs_file_size(&f->f);
74 posprintf(dest, "%04x %s\n", (unsigned int)file_size, current_filename); 77 posprintf(dest, "%04x %s\n", (unsigned int)file_size, f->filename);
75 } 78 }
76 return 0; 79 return 0;
77} 80}
diff --git a/src/main.c b/src/main.c
index 1acae1b..21bc2a7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -223,6 +223,7 @@ void
223file_talk(Device *d, u8 b0, u8 w) { 223file_talk(Device *d, u8 b0, u8 w) {
224 if (w) { 224 if (w) {
225 u16 a, b, res; 225 u16 a, b, res;
226 UxnFile *f = &uxn_file[d - &d->u->dev[0xa]];
226 switch(b0) { 227 switch(b0) {
227 case 0x5: { 228 case 0x5: {
228 DEVPEEK16(a, 0x4); 229 DEVPEEK16(a, 0x4);
@@ -230,16 +231,17 @@ file_talk(Device *d, u8 b0, u8 w) {
230 if(b > 0x10000 - a) { 231 if(b > 0x10000 - a) {
231 b = 0x10000 - a; 232 b = 0x10000 - a;
232 } 233 }
233 res = file_stat(&d->mem[a], b); 234 res = file_stat(f, &d->mem[a], b);
234 DEVPOKE16(0x2, res); 235 DEVPOKE16(0x2, res);
235 } break; 236 } break;
236 case 0x6: { 237 case 0x6: {
238 // TODO: no file deletion for now
237 // res = file_delete(); 239 // res = file_delete();
238 // DEVPOKE16(0x2, res); 240 // DEVPOKE16(0x2, res);
239 } break; 241 } break;
240 case 0x9: { 242 case 0x9: {
241 DEVPEEK16(a, 0x8); 243 DEVPEEK16(a, 0x8);
242 res = file_init(&d->mem[a]); 244 res = file_init(f, &d->mem[a]);
243 DEVPOKE16(0x2, res); 245 DEVPOKE16(0x2, res);
244 } break; 246 } break;
245 case 0xd: { 247 case 0xd: {
@@ -248,7 +250,7 @@ file_talk(Device *d, u8 b0, u8 w) {
248 if(b > 0x10000 - a) { 250 if(b > 0x10000 - a) {
249 b = 0x10000 - a; 251 b = 0x10000 - a;
250 } 252 }
251 res = file_read(&d->mem[a], b); 253 res = file_read(f, &d->mem[a], b);
252 DEVPOKE16(0x2, res); 254 DEVPOKE16(0x2, res);
253 } break; 255 } break;
254 case 0xf: { 256 case 0xf: {
@@ -257,7 +259,7 @@ file_talk(Device *d, u8 b0, u8 w) {
257 if(b > 0x10000 - a) { 259 if(b > 0x10000 - a) {
258 b = 0x10000 - a; 260 b = 0x10000 - a;
259 } 261 }
260 res = file_write(&d->mem[a], b, d->dat[0x7]); 262 res = file_write(f, &d->mem[a], b, d->dat[0x7]);
261 DEVPOKE16(0x2, res); 263 DEVPOKE16(0x2, res);
262 } break; 264 } break;
263 } 265 }