aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-24 20:58:14 +0200
committerBad Diode <bd@badd10de.dev>2021-05-24 20:58:14 +0200
commit8e90cbd6fd790c16acd86cba878a69160fa4f17b (patch)
tree7bdeee7a0079a5aa8a0710583d879a99f9283505
parentabe30b526dc5017d6c9adb61472ebe3cfc727482 (diff)
downloaduxngba-8e90cbd6fd790c16acd86cba878a69160fa4f17b.tar.gz
uxngba-8e90cbd6fd790c16acd86cba878a69160fa4f17b.zip
Finish first prototype of new filesystem
-rw-r--r--src/filesystem.c61
-rw-r--r--src/main.c49
2 files changed, 58 insertions, 52 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index 327b613..ee55dcf 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -117,7 +117,7 @@ fs_init(void) {
117void 117void
118_fs_update_filesystem_header(void) { 118_fs_update_filesystem_header(void) {
119 _fs_write(&filesystem, 0, offsetof(FileSystem, files)); 119 _fs_write(&filesystem, 0, offsetof(FileSystem, files));
120}; 120}
121 121
122void 122void
123_fs_update_file_index(u16 index) { 123_fs_update_file_index(u16 index) {
@@ -238,7 +238,6 @@ _fs_free_blocks(u8 blk_id) {
238 _fs_free_blocks(next_block); 238 _fs_free_blocks(next_block);
239} 239}
240 240
241// Write to block as a new file.
242void 241void
243_fs_write_to_block(u8 *src, u16 n_bytes, u16 blk_offset, 242_fs_write_to_block(u8 *src, u16 n_bytes, u16 blk_offset,
244 u8 blk_id, u8 prev_blk, bool append) { 243 u8 blk_id, u8 prev_blk, bool append) {
@@ -295,7 +294,7 @@ fs_seek(File *file, int offset, SeekMode mode) {
295 new_offset = MAX((int)file_size - 1 + offset, 0); 294 new_offset = MAX((int)file_size - 1 + offset, 0);
296 } break; 295 } break;
297 } 296 }
298 if (new_offset >= file_size) { 297 if (new_offset != 0 && new_offset >= file_size) {
299 return -1; 298 return -1;
300 } 299 }
301 file->offset = new_offset; 300 file->offset = new_offset;
@@ -365,22 +364,54 @@ fs_write(u8 *src, u16 n_bytes, File *file) {
365 return n_bytes; 364 return n_bytes;
366} 365}
367 366
367void
368_fs_read_from_block(u8 *dst, u16 n_bytes, u16 blk_offset, u8 blk_id) {
369 // Read initial block.
370 FileBlock block;
371 u16 block_pos = FILE_DATA_START + FILE_BLOCK_SIZE * blk_id;
372 _fs_read(&block, block_pos, sizeof(FileBlock));
373
374 u16 read_bytes = MIN(block.size - blk_offset, n_bytes);
375 _fs_read(dst, block_pos + blk_offset + sizeof(FileBlock), read_bytes);
376
377 u16 remaining_bytes = n_bytes - read_bytes;
378 if (block.next_block != FS_NULL && remaining_bytes > 0) {
379 _fs_read_from_block(dst + read_bytes, remaining_bytes, 0, block.next_block);
380 }
381}
382
368u16 383u16
369fs_read(u8 *dst, u16 n_bytes, u16 offset, File *file) { 384fs_read(u8 *dst, u16 n_bytes, File *file) {
370 // File *file = &filesystem.files[file_index]; 385 if ((file->mode & FS_OPEN_READ) == 0) {
386 return 0;
387 }
388
389 // If there is an offset find the block index and relative offset.
390 u8 blk_id = filesystem.files[file->index].first_block;
391 u16 offset = file->offset;
371 392
372 // // Check if the offset is within limits. 393 // Read as much as we can from the file after the offset.
373 // if (file->size == 0 || offset >= file->size - 1) { 394 u16 file_size = fs_file_size(file);
374 // return 0; 395 if (offset + n_bytes >= file_size) {
375 // } 396 n_bytes = file_size - offset;
397 }
376 398
377 // // Read as much as we can. 399 if (offset >= FILE_BLOCK_CAPACITY) {
378 // if (offset + n_bytes > file->size) { 400 u16 n_blocks_offset = offset / FILE_BLOCK_CAPACITY;
379 // n_bytes = file->size - offset; 401 for (size_t i = 0; i < n_blocks_offset; ++i) {
380 // } 402 FileBlock block;
403 u16 block_pos = FILE_DATA_START + FILE_BLOCK_SIZE * blk_id;
404 _fs_read(&block, block_pos, sizeof(FileBlock));
405 blk_id = block.next_block;
406 if (blk_id == FS_NULL) {
407 return 0;
408 }
409 }
410 offset = offset % FILE_BLOCK_CAPACITY;
411 }
381 412
382 // // Copy n_bytes to destination. 413 // Copy n_bytes to destination.
383 // _fs_read(dst, FILE_DATA_OFFSET + FILE_MAX_SIZE * file_index + offset, n_bytes); 414 _fs_read_from_block(dst, n_bytes, offset, blk_id);
384 415
385 return n_bytes; 416 return n_bytes;
386} 417}
diff --git a/src/main.c b/src/main.c
index 62030e5..ea8d459 100644
--- a/src/main.c
+++ b/src/main.c
@@ -124,20 +124,19 @@ file_talk(Device *d, u8 b0, u8 w) {
124 u16 result = 0, length = mempeek16(d->dat, 0xa); 124 u16 result = 0, length = mempeek16(d->dat, 0xa);
125 u16 offset = mempeek16(d->dat, 0x4); 125 u16 offset = mempeek16(d->dat, 0x4);
126 u16 addr = mempeek16(d->dat, b0 - 1); 126 u16 addr = mempeek16(d->dat, b0 - 1);
127 File file = fs_open_file(name, read ? FS_OPEN_READ : FS_OPEN_WRITE); 127 OpenMode mode = FS_OPEN_READ;
128 if (!read) {
129 mode = offset ? FS_OPEN_APPEND : FS_OPEN_WRITE;
130 }
131 File file = fs_open_file(name, mode);
128 if (file.index != FS_NULL) { 132 if (file.index != FS_NULL) {
129 txt_position(9, 9); 133 if(fs_seek(&file, offset, SEEK_SET) != -1) {
130 // TODO: Use file.cur pointer and fs_seek instead of offset. 134 if (read) {
131 // TODO: Remove append, that should be a write mode. 135 result = fs_read(&d->mem[addr], length, &file);
132 if (read) { 136 } else {
133 result = fs_read(&d->mem[addr], length, offset, &file); 137 result = fs_write(&d->mem[addr], length, &file);
134 } else { 138 }
135 result = fs_write(&d->mem[addr], length, offset, offset > 0, &file);
136 txt_printf("WROTE: %d\n", result);
137 } 139 }
138 } else {
139 // txt_position(9, 9);
140 // txt_printf("NOT FOUND");
141 } 140 }
142 mempoke16(d->dat, 0x2, result); 141 mempoke16(d->dat, 0x2, result);
143 } 142 }
@@ -197,7 +196,7 @@ handle_input(Uxn *u) {
197 } 196 }
198 197
199 // Update ctrl_idx. 198 // Update ctrl_idx.
200 ctrl_idx = ctrl_idx + 1 > LEN(ctrl_methods) - 1 ? 0 : ctrl_idx + 1; 199 ctrl_idx = (ctrl_idx + 1 > (int)LEN(ctrl_methods) - 1) ? 0 : ctrl_idx + 1;
201 200
202 // Initialize controller variables here. 201 // Initialize controller variables here.
203 if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) { 202 if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) {
@@ -360,38 +359,14 @@ int main(void) {
360 txt_init(1, TEXT_LAYER); 359 txt_init(1, TEXT_LAYER);
361 txt_position(0,0); 360 txt_position(0,0);
362 361
363 u8 test_data_a[1020];
364 u8 test_data_b[2038];
365 memset(&test_data_a, 0xAA, sizeof(test_data_a));
366 memset(&test_data_b, 0xbb, sizeof(test_data_b));
367
368 txt_position(0, 8);
369 File file_a = fs_open_file("file_a", FS_OPEN_WRITE);
370 File file_b = fs_open_file("file_b", FS_OPEN_WRITE);
371 fs_write(&test_data_b, sizeof(test_data_b), 0, 0, &file_a);
372 fs_write(&test_data_a, sizeof(test_data_a), 0, 0, &file_a);
373 // fs_write(&test_data_a, sizeof(test_data_a), 0, 0, &file_a);
374 // fs_write(&test_data_b, sizeof(test_data_b), 0, 0, &file_b);
375
376 // Main loop. 362 // Main loop.
377 int frame_counter = 0; 363 int frame_counter = 0;
378 evaluxn(&u, 0x0100); 364 evaluxn(&u, 0x0100);
379 u32 flip_cycles = 0;
380 while(true) { 365 while(true) {
381 bios_vblank_wait(); 366 bios_vblank_wait();
382 profile_start();
383 handle_input(&u); 367 handle_input(&u);
384 u32 input_cycles = profile_stop();
385 profile_start();
386 evaluxn(&u, mempeek16(devscreen->dat, 0)); 368 evaluxn(&u, mempeek16(devscreen->dat, 0));
387 u32 eval_cycles = profile_stop();
388 txt_position(0, 8);
389 // txt_printf("INPUT: %lu \n", input_cycles);
390 // txt_printf("EVAL: %lu \n", eval_cycles);
391 // txt_printf("FLIP: %lu \n", flip_cycles);
392 profile_start();
393 flipbuf(&ppu); 369 flipbuf(&ppu);
394 flip_cycles = profile_stop();
395 frame_counter++; 370 frame_counter++;
396 } 371 }
397 372