#include #include "fs.h" #include "ata.h" #include "fat.h" #include #include "panic.h" #define MAX_HANDLES 32 #define FILE_BUFFER(h) ((void *)(0x0002be00 + (h << 9))) #define HANDLE(h) ((handles - 1)[h]) #define HEAD(h) (FILE_BUFFER(h) + HANDLE(h).seek % 512) struct handle_info { uint16_t start_cluster; uint16_t seek; uint16_t loaded_cluster; bool dirty; }; struct handle_info handles[MAX_HANDLES]; void clear_fs_handles() { struct handle_info *f = handles; while (f < handles + MAX_HANDLES) (f++)->start_cluster = 0; } fs_handle next_handle() { fs_handle r = 0; while (HANDLE(++r).start_cluster) if (r == MAX_HANDLES) return 0; return r; } void write_buffer(fs_handle handle) { HANDLE(handle).dirty = false; panic("Not implemented (write_buffer)."); } void read_buffer(fs_handle handle) { if (HANDLE(handle).dirty) write_buffer(handle); HANDLE(handle).loaded_cluster = HANDLE(handle).seek >> 9; panic("Not implemented (read_buffer)."); } fs_handle fs_open(uint8_t *path) { fs_handle next = next_handle(); if (!next) return 0; uint16_t s = get_start_cluster(path); if (!s) return 0; HANDLE(next).start_cluster = s; HANDLE(next).seek = 0; HANDLE(next).dirty = false; read_buffer(next); return next; } int16_t fs_seek(fs_handle handle, int16_t by) { uint16_t old = HANDLE(handle).seek; uint16_t to = -by > old ? 0 : old + by; HANDLE(handle).seek = to; if ((to ^ old) & 0xfe00) read_buffer(handle); } uint16_t fs_read(fs_handle handle, uint16_t max, void *buffer) { panic("Not implemented (fs_read)."); } uint16_t fs_write(fs_handle handle, uint16_t max, void *buffer) { panic("Not implemented (fs_write)."); } void fs_close(fs_handle handle) { if (HANDLE(handle).dirty) write_buffer(handle); HANDLE(handle).start_cluster = 0; }