Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/BACKLOG.md

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions docs/saves.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ Deleting a state with Triangle in the Savestate menu also deletes its `.bak`,
When a `.sav` exists but cannot be read, the emulator asks "The save file
cannot be read. Try again?". Yes reads it again. No asks "Play without
saving? The file stays as it is.": yes starts the game and writes nothing to
the `.sav` until another game is loaded, and no goes back to the game list.
While you play without saving, no pre-link copy is made or restored. The file
itself is never written over.
the `.sav` until another game is loaded, and no goes back to the game list
(at startup it opens the menu, and a game started from a launcher closes the
emulator instead). While you play without saving, no pre-link copy is made
or restored. The file itself is never written over.

## Restoring a file by hand

Expand Down
6 changes: 3 additions & 3 deletions docs/wip/save-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
- **Branch:** `features/state-of-the-art-development/wip/save-store`, staccato da `features/state-of-the-art-development/main`.
- **Ticket:** EXT-1, SAV-1, SAV-2 (tranne SAV-2.6), SAV-3, SAV-5 (26 storie, circa 21 gg).
- **File principali:** backup in `source/src/memory.c`, `save_store.c` e `save_format.c` (nuovi), avvisi in `gui.c`.
- **Dipende da:** nessuna attesa su altri branch: SAV-2.6 è in main (b5cffbf), ENG-1.1 ed ENG-1.2 (`wip/release-and-ci`) sono in main 8ace097, unito qui da 89769bd.
- **Dipende da:** nessuna attesa su altri branch: SAV-2.6 è in main (dbab178), ENG-1.1 ed ENG-1.2 (`wip/release-and-ci`) sono in main 8ace097, unito qui da 89769bd.
- **Nota:** Primo lavoro della 1.4.4, che per la decisione (l) resta una tappa interna di lavoro: il rilascio è la 1.5.0. `features/save-read-fixes` (SAV-1 e SAV-2.8) è unito qui da 5dde920.
- **Regole:** quelle di «Branch di lavoro dell'epica» in `docs/BACKLOG.md`. Il testo completo di ogni ticket (cosa, file, test, accettazione, licenza) è in `docs/BACKLOG.md` sotto il suo ID. Si spunta un ticket quando la sua accettazione è verificata; questo file si cancella nella PR che chiude il branch.
- **Legenda:** «subito» = nessuna dipendenza, si può iniziare oggi; «aspetta» = dipendenze in altri branch, si parte quando sono in main.

## 1.4.4

- [x] **EXT-1.1** — Rilevamento sui DMA da 17 o 81 unità, con test host del protocollo · S, 1 gg · dipende da: — · PSP: no · **subito** · _CI verde su 214c50a (run 42803695637): test_eeprom sul vero memory.c passa in `make check` e sotto ASan e UBSan in `make test-sanitize`, e memory.c compila nei due profili PSP_
- [ ] **EXT-1.2** — Caso "scrittura prima della lettura" nella ROM di test dei salvataggi · S, 0,5 gg · dipende da: SAV-2.6, EXT-1.1 · PSP: sì · _codice pronto (a58d9dc, 8f4729c) e prova `save-eeprom8k-wfirst` nel catalogo hardware (cc76d87, 4796757), mancano `make test-roms` con un compilatore ARM (build della ROM, core New e mGBA; la CI non lo esegue) e le prove su 1.4.2 e sul candidato, su due PSP_
- [ ] **EXT-1.2** — Caso "scrittura prima della lettura" nella ROM di test dei salvataggi · S, 0,5 gg · dipende da: SAV-2.6, EXT-1.1 · PSP: sì · _codice pronto (a58d9dc, 8f4729c), che a ogni avvio scrive un solo blocco (3Fh) e non una generazione (testo del ticket corretto il 2026-11-16), e prova `save-eeprom8k-wfirst` nel catalogo hardware (cc76d87, 4796757), mancano `make test-roms` con un compilatore ARM (build della ROM, core New e mGBA; la CI non lo esegue) e le prove su 1.4.2 e sul candidato, su due PSP_
- [x] **SAV-1.1** — Modulo puro save_format: tipo dal tag, taglia nativa, clamp · S, 0,5 gg · dipende da: — · PSP: no · **subito**
- [x] **SAV-1.2** — Interfaccia SaveStoreIo (exists, read, write) e file system finto con guasti iniettabili · S, 0,5 gg · dipende da: — · PSP: no · **subito**
- [x] **SAV-1.3** — save_store_load: assente, letto o illeggibile · S, 0,5 gg · dipende da: SAV-1.2 · PSP: no
- [x] **SAV-1.4** — load_backup ricablato, binding sceIo condiviso e blocco delle scritture · M, 1,5 gg · dipende da: SAV-1.1, SAV-1.3 · PSP: no · _CI verde su 214c50a (run 42803695637): test_memory_backup sul vero memory.c passa in `make check` e sotto ASan e UBSan in `make test-sanitize`, `check-lifecycle.sh` è verde e gui.c e main.c compilano nei due profili PSP_
- [ ] **SAV-1.5** — Avviso di salvataggio illeggibile: riprova o gioca senza salvare · S, 0,5 gg · dipende da: SAV-1.4 · PSP: sì · _codice pronto (27c6538) con una differenza dal ticket da confermare nella PR: all'avvio da launcher (ROM passata in argv) due no chiudono l'emulatore invece di aprire il menu (`main.c`, provato da `tests/test_startup.py`); il caso della directory `<rom>.sav` è nella prova `save-read-matrix` (ee6873e), la CI è verde su 214c50a (run 42803695637), manca la prova su PSP_
- [ ] **SAV-1.5** — Avviso di salvataggio illeggibile: riprova o gioca senza salvare · S, 0,5 gg · dipende da: SAV-1.4 · PSP: sì · _codice pronto (27c6538); all'avvio da launcher (ROM passata in argv) due no chiudono l'emulatore invece di aprire il menu, come già un caricamento da launcher fallito (`main.c`, provato da `tests/test_startup.py`): differenza accettata il 2026-11-16 e scritta nel ticket; il caso della directory `<rom>.sav` è nella prova `save-read-matrix` (ee6873e), la CI è verde su 214c50a (run 42803695637), manca la prova su PSP_
- [x] **SAV-1.6** — .sav.orig conservato dallo store ed evento SAVE_LOAD nel runtime log · S, 0,5 gg · dipende da: SAV-1.4, SAV-2.1 · PSP: no · _CI verde su 214c50a (run 42803695637): test_memory_backup, che prova che il vero memory.c registra SAVE_LOAD, passa in `make check` e sotto ASan e UBSan in `make test-sanitize`_
- [x] **SAV-1.7** — Argine immediato per gli stati che indicizzano fuori dal buffer · S, 0,5 gg · dipende da: SAV-1.1 · PSP: no · _CI verde su 214c50a (run 42803695637): memory.c compila con gcc e nei due profili PSP, test_save_format e test_memory_backup passano sotto ASan e UBSan in `make test-sanitize`_
- [ ] **SAV-1.8** — Validazione hardware del percorso di lettura · S, 1 gg · dipende da: SAV-1.4, SAV-1.5, SAV-1.6, SAV-1.7 · PSP: sì · _in parte: docs/saves.md (650222a, f9d4407) e la prova `save-read-matrix` nel catalogo hardware (ee6873e, 41bcb7e), mancano la sessione hardware e le note della 1.5.0 generate da `make release-gate` (ENG-1.2), che leggono `docs/releases/1.5.0.txt`: l'id `save-read-matrix` oggi è solo in `docs/releases/1.4.4.txt` (file di wip/release-and-ci)_
Expand Down
2 changes: 1 addition & 1 deletion scripts/check-menu-layout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ status_body=$(awk '
' source/src/gui.c)
if [ -z "$status_body" ] ||
printf '%s\n' "$status_body" |
grep -qE 'sceIo|savestate_file_exists|get_savestate_info' ||
grep -qE 'sceIo|savestate_file_(exists|stat)|get_savestate_info' ||
! printf '%s\n' "$status_body" | grep -q 'menu_layout_status_columns' ||
! grep -q 'menu_draw_main_status(display_option->display_msg' \
source/src/gui.c; then
Expand Down
30 changes: 20 additions & 10 deletions source/src/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -3123,26 +3123,36 @@ void auto_savestate_sleep(void)
action_savestate_slot(AUTO_SAVESTATE_SLOT);
}

/* Nonzero if the slot's .svs is in dir_state; the slot menu backs up to the
auto slot only when there is a state to load. A save cut between the
store's two renames left only a .bak and .tmp: recovered first, as the
load will. */
static u32 savestate_file_exists(u32 slot)
/* Nonzero if the slot's .svs is in dir_state: one stat, no recovery. */
static u32 savestate_file_stat(u32 slot)
{
char savestate_filename[MAX_FILE];
char savestate_path[MAX_PATH];
SceIoStat stat;

get_savestate_filename(slot, savestate_filename);
save_store_recover(backup_io, dir_state, savestate_filename);
snprintf(savestate_path, sizeof(savestate_path), "%s%s", dir_state, savestate_filename);
return (sceIoGetstat(savestate_path, &stat) >= 0);
}

/* savestate_file_stat() after recovery; the slot menu backs up to the auto
slot only when there is a state to load. A save cut between the store's
two renames left only a .bak and .tmp: recovered first, as the load
will. */
static u32 savestate_file_exists(u32 slot)
{
char savestate_filename[MAX_FILE];

get_savestate_filename(slot, savestate_filename);
save_store_recover(backup_io, dir_state, savestate_filename);
return savestate_file_stat(slot);
}

/* Numbered slots 0-9 with a file, and whether the auto slot has one, for
the Save and Load row of the main menu. Counted when the main menu is
entered and after an AUTO state write, never per frame: each check is a
file system stat. */
entered and after an AUTO state write, never per frame, with one stat per
slot: recovery would cost about ten sceIo calls per empty slot, so a save
cut mid-write counts as empty here until a load recovers it. */
static u32 menu_status_slots;
static u32 menu_status_auto;

Expand All @@ -3156,8 +3166,8 @@ static void menu_status_count_states(void)
return;

for (slot = 0; slot < AUTO_SAVESTATE_SLOT; slot++)
menu_status_slots += savestate_file_exists(slot);
menu_status_auto = savestate_file_exists(AUTO_SAVESTATE_SLOT);
menu_status_slots += savestate_file_stat(slot);
menu_status_auto = savestate_file_stat(AUTO_SAVESTATE_SLOT);
}

/* Save and Load page thumbnails: each slot's .svs preview shrunk to 60x40.
Expand Down
13 changes: 11 additions & 2 deletions tests/fake_store_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* In-memory .sav file system for the save tests; see fake_store_io.h.
*/

#include <assert.h>
#include <string.h>

#include "fake_store_io.h"
Expand Down Expand Up @@ -153,11 +154,13 @@ static int32_t fake_open_write(void *context, const char *path)
*ref.size = 0;
*ref.present = 1;
store->open_file = slot;
store->took_nothing = 0;
return slot;
}

/* Stores what fits before the device or the file is full: a short count,
then -1 as ENOSPC. short_write cuts every count short. */
then 0, as save_io_write gives on ENOSPC. short_write cuts every count
short. */
static int fake_write(void *context, int32_t file, const uint8_t *data,
size_t size)
{
Expand All @@ -177,7 +180,13 @@ static int fake_write(void *context, int32_t file, const uint8_t *data,
if(size > room)
size = room;
if(size == 0)
return -1;
{
/* A caller that writes again after a 0 would spin forever on a full
Memory Stick: fail the test instead of hanging it. */
assert(!store->took_nothing);
store->took_nothing = 1;
return 0;
}
memcpy(ref.data + *ref.size, data, size);
*ref.size += size;
store->written += size;
Expand Down
3 changes: 2 additions & 1 deletion tests/fake_store_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ typedef struct
size_t short_write; /* if nonzero, a write takes at most this many
bytes */
size_t space; /* if nonzero, the device is full once this many
bytes are written: ENOSPC */
bytes are written: ENOSPC, a write of 0 */
int cut_after; /* if nonzero, the power goes off after this many
calls of any operation: later calls fail and
change nothing, what was done stays */
Expand All @@ -81,6 +81,7 @@ typedef struct
size_t written; /* bytes the writes accepted */
size_t last_capacity; /* capacity passed to the last read */
int32_t open_file; /* the handle open_write gave, or -1 */
int took_nothing; /* the open file's last write took no byte */
} FakeStore;

/* Empties the store, clears the switches and counters, and points io at
Expand Down
29 changes: 20 additions & 9 deletions tests/test_save_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ static void test_sanitize_keeps_valid_states(void)
assert(save_format_sanitize(&state, &before) == 0);
assert(same_state(&state, &copy));
}

/* A state saved before the game's first backup access. */
{
SaveState state = valid_state();
SaveState copy;

state.type = SAVE_TYPE_NONE;
copy = state;
assert(save_format_sanitize(&state, &before) == 0);
assert(same_state(&state, &copy));
}
}

/* Each field out of range is put back and reported in its own bit. */
Expand All @@ -231,26 +242,26 @@ static void test_sanitize_repairs_each_field(void)
SaveState before = valid_state();
SaveState state;

before.type = SAVE_TYPE_SRAM;
before.sram_size = 64 * KIB;
before.flash_size = 64 * KIB;
before.eeprom_size = 512;
/* Unlike the obvious defaults (SRAM, 64 KiB, 64 KiB Flash, 512 B), so
each repair shows it took the value in use before the load. */
before.type = SAVE_TYPE_FLASH;
before.sram_size = 32 * KIB;

state = valid_state();
state.type = 9;
assert(save_format_sanitize(&state, &before) == SAVE_FIX_TYPE);
assert(state.type == SAVE_TYPE_SRAM);
assert(state.type == SAVE_TYPE_FLASH);

/* States saved by older builds hold sram_size 1 for a 64 KiB file. */
/* States saved by older builds hold sram_size 1. */
state = valid_state();
state.sram_size = 1;
assert(save_format_sanitize(&state, &before) == SAVE_FIX_SRAM_SIZE);
assert(state.sram_size == 64 * KIB);
assert(state.sram_size == 32 * KIB);

state = valid_state();
state.flash_size = 0x30000;
assert(save_format_sanitize(&state, &before) == SAVE_FIX_FLASH_SIZE);
assert(state.flash_size == 64 * KIB);
assert(state.flash_size == 128 * KIB);

state = valid_state();
state.flash_bank = 5;
Expand All @@ -262,7 +273,7 @@ static void test_sanitize_repairs_each_field(void)
state.eeprom_counter = 0;
state.eeprom_size = 0x4000;
assert(save_format_sanitize(&state, &before) == SAVE_FIX_EEPROM_SIZE);
assert(state.eeprom_size == 512);
assert(state.eeprom_size == 8 * KIB);

/* The address width matters only in an address phase. */
state = valid_state();
Expand Down
70 changes: 69 additions & 1 deletion tests/test_save_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,66 @@
"const SaveStoreIo gui_save_store_io =",
]

# The main menu's slot count, with a counting stat and recovery. Slot 3
# and the auto slot (10) have a state.
SLOT_STUBS = r'''
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "save_store.h"
typedef uint32_t u32;
typedef struct { int st_mode; } SceIoStat;
#define MAX_FILE 256
#define MAX_PATH 512
#define AUTO_SAVESTATE_SLOT 10
static const SaveStoreIo *backup_io;
static char dir_state[] = "ms0:/s/";
static char gamepak_filename[] = "game.gba";
static int stats, recovers;
static u32 menu_status_slots;
static u32 menu_status_auto;
static void get_savestate_filename(u32 slot, char *name)
{
sprintf(name, "game_%d.svs", (int)slot);
}
SaveStoreResult save_store_recover(const SaveStoreIo *io, const char *dir,
const char *name)
{
(void)io; (void)dir; (void)name;
recovers++;
return SAVE_STORE_OK;
}
static int sceIoGetstat(const char *path, SceIoStat *stat)
{
(void)stat;
stats++;
return strcmp(path, "ms0:/s/game_3.svs") == 0 ||
strcmp(path, "ms0:/s/game_10.svs") == 0 ? 0 : -1;
}
'''

SLOT_SCENARIOS = r'''
int main(void)
{
menu_status_count_states();
assert(menu_status_slots == 1 && menu_status_auto == 1);
/* One stat per slot: recovery would cost about ten calls per empty slot
each time the main menu opens. */
assert(stats == 11 && recovers == 0);
stats = 0;
assert(savestate_file_exists(3) && stats == 1 && recovers == 1);
assert(!savestate_file_exists(4) && stats == 2 && recovers == 2);
return 0;
}
'''

SLOT_FUNCTIONS = [
"static u32 savestate_file_stat(u32 slot)",
"static u32 savestate_file_exists(u32 slot)",
"static void menu_status_count_states(void)",
]


# A memory card of named files behind the sceIo calls of the trace menu's
# "Finish and save": each call can be made to fail, and a rename fails
Expand Down Expand Up @@ -477,13 +537,21 @@ def test_trace_save_keeps_the_saved_pair(self):
source += function("source/src/gui.c", signature) + "\n"
build_and_run(source + TRACE_SAVE_SCENARIOS, "trace_save")

def test_main_menu_counts_slots_without_recovery(self):
source = SLOT_STUBS
for signature in SLOT_FUNCTIONS:
source += function("source/src/gui.c", signature) + "\n"
self.build_and_run(source + SLOT_SCENARIOS)

def test_production_sceio_binding(self):
# The real gui.c functions and table, between the stubs and scenarios.
source = (f"#define FILE_SIZE {FILE_SIZE}\n"
f"#define EXPECTED_CRC 0x{zlib.crc32(CONTENT):08X}u\n" + STUBS)
for signature in FUNCTIONS:
source += function("source/src/gui.c", signature) + "\n"
source += ";\n" + SCENARIOS
self.build_and_run(source + ";\n" + SCENARIOS)

def build_and_run(self, source):
with tempfile.TemporaryDirectory(prefix="gbarush-save-io-") as directory:
path = Path(directory)
(path / "save_io.c").write_text(source)
Expand Down