OXYGENHUB
◆ rrdc-perf enhanced-x16-emulator / src / sdcard_fat.h
3.9 KiB · Objective-C · last changed chore(): initial commit 25 days ago source blame history raw
1
// Commander X16 Emulator — SD card FAT index
2
// Copyright (c) 2024 X16Emu ADD contributors
3
// All rights reserved. License: 2-clause BSD
4
//
5
// Read-only FAT16/FAT32 index over an SD card image.  Borrows the caller's
6
// already-open file handle and never opens or closes it.
7
8
#ifndef SDCARD_FAT_H
9
#define SDCARD_FAT_H
10
11
#include <stdbool.h>
12
#include <stdint.h>
13
14
#ifdef __cplusplus
15
extern "C" {
16
#endif
17
18
// Forward declaration so C++ panels can include this header without pulling in
19
// files.h (which includes SDL.h).
20
struct x16file;
21
22
// ── Region tags ─────────────────────────────────────────────────────────────
23
typedef enum {
24
	SDCARD_FAT_REGION_UNKNOWN = 0,
25
	SDCARD_FAT_REGION_MBR,
26
	SDCARD_FAT_REGION_RESERVED,
27
	SDCARD_FAT_REGION_FAT,
28
	SDCARD_FAT_REGION_ROOTDIR,
29
	SDCARD_FAT_REGION_FILE,
30
	SDCARD_FAT_REGION_DIR,
31
	SDCARD_FAT_REGION_FREE,
32
} sdcard_fat_region_t;
33
34
// ── File/directory entry ─────────────────────────────────────────────────────
35
typedef struct {
36
	const char *path;          // e.g. "/GAMES/FOO.PRG", owned by the index
37
	uint32_t    size;          // file size in bytes (0 for directories)
38
	uint32_t    first_cluster;
39
	bool        is_dir;
40
	uint64_t    bytes_read;    // session counter, updated by sdcard_fat_note_access
41
	uint64_t    bytes_written;
42
} sdcard_fat_file_t;
43
44
// ── Lifecycle ────────────────────────────────────────────────────────────────
45
46
// Build or rebuild the index using the already-open image handle `img`.
47
// The handle is borrowed: sdcard_fat never closes it, and save/restores the
48
// stream position around every internal read so the caller's file position is
49
// unchanged on return.
50
// Returns false if the image cannot be parsed (not a FAT image, I/O error, etc.).
51
// On false the previous index (if any) is freed.  Safe to call repeatedly.
52
bool sdcard_fat_build(struct x16file *img);
53
54
// Release all index memory.  Safe to call when not built.
55
void sdcard_fat_free(void);
56
57
// ── Global flag ──────────────────────────────────────────────────────────────
58
59
// When false, sdcard_attach() will not build the index.  Owned by the debugger
60
// UI; defaults to true so a build without the UI behaves as before.
61
extern bool sdcard_fat_autoindex;
62
63
// ── State queries ────────────────────────────────────────────────────────────
64
65
bool        sdcard_fat_ready(void);
66
bool        sdcard_fat_is_stale(void);
67
void        sdcard_fat_mark_stale(void);
68
bool        sdcard_fat_truncated(void);
69
const char *sdcard_fat_type_name(void);   // "FAT16" / "FAT32" / ""
70
uint32_t    sdcard_fat_bytes_per_cluster(void);
71
72
// ── Lookup ───────────────────────────────────────────────────────────────────
73
74
sdcard_fat_region_t sdcard_fat_lookup(uint32_t lba,
75
                                      const sdcard_fat_file_t **out_file,
76
                                      uint64_t                 *out_offset);
77
78
void sdcard_fat_note_access(uint32_t lba, bool is_write, uint32_t bytes);
79
80
// ── Enumeration ─────────────────────────────────────────────────────────────
81
82
int                      sdcard_fat_file_count(void);
83
const sdcard_fat_file_t *sdcard_fat_file_at(int i);
84
85
#ifdef __cplusplus
86
}
87
#endif
88
89
#endif // SDCARD_FAT_H