aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-21 19:30:08 +0200
committerBad Diode <bd@badd10de.dev>2021-05-21 19:30:08 +0200
commit3924524adf85914bbdb77f002d5443597ef154d8 (patch)
tree163a045016a19eebb71c20d661e953f8e9441ef2 /src
parent0cfa96f83d6c44b318cc0da0ec98e8e3bc97f7f9 (diff)
downloaduxngba-3924524adf85914bbdb77f002d5443597ef154d8.tar.gz
uxngba-3924524adf85914bbdb77f002d5443597ef154d8.zip
Fix filesystem read issues, load/save rom demo is working
Diffstat (limited to 'src')
-rw-r--r--src/filesystem.c33
-rw-r--r--src/main.c5
2 files changed, 28 insertions, 10 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index 1521d69..69e23f7 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -47,6 +47,12 @@ fs_init() {
47 if (filesystem.num_files == 0xFFFF 47 if (filesystem.num_files == 0xFFFF
48 && filesystem.data_capacity == 0xFFFF 48 && filesystem.data_capacity == 0xFFFF
49 && filesystem.data_size == 0xFFFF) { 49 && filesystem.data_size == 0xFFFF) {
50 // Clear SRAM.
51 for (size_t i = 0; i < KB(64); ++i) {
52 SRAM[i] = 0;
53 }
54
55 // Initialize filesystem.
50 filesystem.num_files = 0; 56 filesystem.num_files = 0;
51 filesystem.data_size = 0; 57 filesystem.data_size = 0;
52 filesystem.data_capacity = 27 * FILE_MAX_SIZE; 58 filesystem.data_capacity = 27 * FILE_MAX_SIZE;
@@ -57,8 +63,13 @@ fs_init() {
57 } 63 }
58} 64}
59 65
66typedef enum {
67 OPEN_READ,
68 OPEN_WRITE,
69} OpenMode;
70
60int 71int
61fs_open_file(char *name) { 72fs_open_file(char *name, OpenMode mode) {
62 // Try to find an existing file. 73 // Try to find an existing file.
63 for (size_t i = 0; i < filesystem.num_files; ++i) { 74 for (size_t i = 0; i < filesystem.num_files; ++i) {
64 // TODO: Replace strcmp with vectorized fixed size char comparison. 75 // TODO: Replace strcmp with vectorized fixed size char comparison.
@@ -66,6 +77,11 @@ fs_open_file(char *name) {
66 return i; 77 return i;
67 } 78 }
68 } 79 }
80
81 if (mode == OPEN_READ) {
82 return -1;
83 }
84
69 // Create a new file if there is space. 85 // Create a new file if there is space.
70 if (filesystem.num_files < FILE_CAPACITY) { 86 if (filesystem.num_files < FILE_CAPACITY) {
71 size_t index = filesystem.num_files++; 87 size_t index = filesystem.num_files++;
@@ -89,13 +105,13 @@ fs_open_file(char *name) {
89 return -1; 105 return -1;
90} 106}
91 107
92int 108size_t
93fs_write(u8 *src, size_t n_bytes, u16 file_index, u16 offset, bool append) { 109fs_write(u8 *src, size_t n_bytes, u16 file_index, u16 offset, bool append) {
94 File *file = &filesystem.files[file_index]; 110 File *file = &filesystem.files[file_index];
95 111
96 // Check if there is enough capacity for this write operation. 112 // Check if there is enough capacity for this write operation.
97 if (offset + n_bytes >= FILE_MAX_SIZE) { 113 if (offset + n_bytes >= FILE_MAX_SIZE) {
98 return -1; 114 return 0;
99 } 115 }
100 116
101 // Write data to file block. 117 // Write data to file block.
@@ -117,13 +133,18 @@ fs_write(u8 *src, size_t n_bytes, u16 file_index, u16 offset, bool append) {
117 return n_bytes; 133 return n_bytes;
118} 134}
119 135
120int 136size_t
121fs_read(u8 *dst, size_t n_bytes, u16 file_index, u16 offset) { 137fs_read(u8 *dst, size_t n_bytes, u16 file_index, u16 offset) {
122 File *file = &filesystem.files[file_index]; 138 File *file = &filesystem.files[file_index];
123 139
124 // Check if the offset is within limits. 140 // Check if the offset is within limits.
125 if (offset + n_bytes >= FILE_MAX_SIZE) { 141 if (file->size == 0 || offset >= file->size - 1) {
126 return -1; 142 return 0;
143 }
144
145 // Read as much as we can.
146 if (offset + n_bytes > file->size) {
147 n_bytes = file->size - offset;
127 } 148 }
128 149
129 // Copy n_bytes to destination. 150 // Copy n_bytes to destination.
diff --git a/src/main.c b/src/main.c
index a65b1fe..a414558 100644
--- a/src/main.c
+++ b/src/main.c
@@ -104,19 +104,16 @@ datetime_talk(Device *d, Uint8 b0, Uint8 w) {
104void 104void
105file_talk(Device *d, Uint8 b0, Uint8 w) { 105file_talk(Device *d, Uint8 b0, Uint8 w) {
106 Uint8 read = b0 == 0xd; 106 Uint8 read = b0 == 0xd;
107 txt_printf("FILE TALK\n"); // DEBUG: Remove when done
108 if(w && (read || b0 == 0xf)) { 107 if(w && (read || b0 == 0xf)) {
109 char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)]; 108 char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)];
110 Uint16 result = 0, length = mempeek16(d->dat, 0xa); 109 Uint16 result = 0, length = mempeek16(d->dat, 0xa);
111 Uint16 offset = mempeek16(d->dat, 0x4); 110 Uint16 offset = mempeek16(d->dat, 0x4);
112 Uint16 addr = mempeek16(d->dat, b0 - 1); 111 Uint16 addr = mempeek16(d->dat, b0 - 1);
113 int file_idx = fs_open_file(name); 112 int file_idx = fs_open_file(name, read ? OPEN_READ : OPEN_WRITE);
114 if (file_idx >= 0) { 113 if (file_idx >= 0) {
115 if (read) { 114 if (read) {
116 txt_printf("READING\n"); // DEBUG: Remove when done
117 result = fs_read(&d->mem[addr], length, file_idx, offset); 115 result = fs_read(&d->mem[addr], length, file_idx, offset);
118 } else { 116 } else {
119 txt_printf("WRITING\n"); // DEBUG: Remove when done
120 result = fs_write(&d->mem[addr], length, file_idx, offset, offset > 0); 117 result = fs_write(&d->mem[addr], length, file_idx, offset, offset > 0);
121 } 118 }
122 } 119 }