From c8bb36e7d7174a1294d64d7b5807b9e4b5697741 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:46:58 +0100 Subject: [PATCH 01/11] Record that two No answers on a launcher start quit the emulator SAV-1.5 said two No answers at startup open the menu. From game.gba or the browser they do; from a launcher (ROM in argv) they quit, as a failed launcher load already did. Accepted on 2026-11-16: the ticket, the SAV-1 goal and docs/saves.md now say so. --- docs/BACKLOG.md | 4 ++-- docs/saves.md | 5 +++-- docs/wip/save-store.md | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index 5c5e0a8..3487cf8 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -882,7 +882,7 @@ Il gruppo copre lettura e scrittura del `.sav`, i savestate, i guasti di emulazi | in corso | 1.4.4 | 5,5 gg | — (solo SAV-1.6 usa SAV-2.1) | **Copre:** F-001 lettura del `.sav` limitata alla capacità (una lettura fallita non diventa un salvataggio vuoto), F-002 tipo di salvataggio dal tag ROM (`.sav` da 64 KiB alla dimensione nativa), F-004 prove di persistenza con guasti di I/O iniettabili, F-081 avviso di avvio fallito con il motivo (con EXT-2) -**Obiettivo:** Nessuna sequenza di guasti di lettura scrive sul `.sav` un'immagine vuota o parziale: dopo un esito FAILED ogni scrittura di quel `.sav` è bloccata, con un test host sul vero `memory.c` e un controllo di sorgente. Il `.sav` resta alla dimensione nativa del chip (EEPROM 512 B o 8 KiB, SRAM 32 o 64 KiB, Flash 64 o 128 KiB); i file da 128 KiB, come quelli che scrive libretro gpSP, si leggono per prefisso e l'originale incoerente resta come `.sav.orig`. Un salvataggio illeggibile apre due domande nelle 5 lingue (riprovare; giocare senza salvare, con il file intatto) e due no tornano alla lista. Matrice tag × dimensione da 45 casi e fuzz da 200.000 passi verdi in `make test` e `make test-sanitize`; matrice di SAV-1.8 su PSP-1000 e su un secondo modello; ogni decisione su tipo, taglia ed esito nel runtime log delle build di release. +**Obiettivo:** Nessuna sequenza di guasti di lettura scrive sul `.sav` un'immagine vuota o parziale: dopo un esito FAILED ogni scrittura di quel `.sav` è bloccata, con un test host sul vero `memory.c` e un controllo di sorgente. Il `.sav` resta alla dimensione nativa del chip (EEPROM 512 B o 8 KiB, SRAM 32 o 64 KiB, Flash 64 o 128 KiB); i file da 128 KiB, come quelli che scrive libretro gpSP, si leggono per prefisso e l'originale incoerente resta come `.sav.orig`. Un salvataggio illeggibile apre due domande nelle 5 lingue (riprovare; giocare senza salvare, con il file intatto) e due no tornano alla lista (all'avvio da launcher chiudono l'emulatore). Matrice tag × dimensione da 45 casi e fuzz da 200.000 passi verdi in `make test` e `make test-sanitize`; matrice di SAV-1.8 su PSP-1000 e su un secondo modello; ogni decisione su tipo, taglia ed esito nel runtime log delle build di release. **Da noi oggi:** `load_backup` (`source/src/memory.c:3160-3220`) riempie di 0xFF il buffer statico da 128 KiB (`:185`, `:3165`) e legge `file_length` byte senza limite (`:3173-3175`). Lo switch sulla dimensione (`:3179-3213`) scavalca il tipo trovato da `load_backup_id` (`:3055`): nel caso 0x10000 assegna `sram_size = BACKUP_FLASH` (`:3200`) e `save_backup` scrive `sram_size` byte (`:3244-3262`), quindi un gioco SRAM con `.sav` da 64 KiB torna su disco come file da 1 byte. Se apertura o lettura falliscono resta l'immagine 0xFF, `load_gamepak` ignora l'esito (`:3687`) e il primo flush (`update_backup` `:3274-3284`, chiamato da `main.c:1140-1141`) la scrive sopra il file vero. Il pattern di I/O iniettabile esiste già per le impostazioni (`config_store.h:12-23`, finto in `tests/test_config_store.c`). Il lavoro è in corso nel branch `features/save-read-fixes` (da d6ef51f, non committato, da ribasare sulla 1.4.3, che ha cambiato `gui.c` e `main.c`): SAV-1.1-1.5 e SAV-1.7 sono implementate, SAV-1.2 in forma ridotta; `tests/test_save_format.c`, `tests/test_save_store.c` (con `tests/fake_store_io.c/.h`), `tests/test_memory_backup.c` e lo scenario di annullamento di `tests/test_startup.py` sono registrati nel `Makefile`, regole normale e sanitize. Verificato solo con clang e uno shim freestanding (UBSan in trap), con 26 mutanti del codice e 8 delle regole di `check-lifecycle.sh` uccisi; non verificati `make check`, `make test-sanitize` con ASan, la build PSP e l'hardware (SAV-1.8). Mancano SAV-1.6, SAV-1.8 e gli eventi del runtime log di SAV-1.6 e SAV-1.7. **Come ci arriviamo:** - Il file resta alla dimensione nativa del chip (EEPROM 512 B o 8 KiB, SRAM 32 o 64 KiB, Flash 64 o 128 KiB), portabile su flash cart e su PC. @@ -922,7 +922,7 @@ Il gruppo copre lettura e scrittura del `.sav`, i savestate, i guasti di emulazi - **Accettazione:** il `.sav` da 64 KiB di un gioco SRAM resta di 65536 byte; un `.sav` da 200 KiB non sfora il buffer; dopo una lettura fallita nessun percorso tocca il file; in `memory.c` non restano chiamate `FILE_*` sul `.sav`. #### SAV-1.5 — Avviso di salvataggio illeggibile: riprova o gioca senza salvare · S, 0,5 gg · dipende da: SAV-1.4 · PSP: sì -- **Cosa:** Implementata nel branch. Il controllo sta in `load_gamepak`, unico punto comune a tutti i caricamenti di ROM (menu a `gui.c:3952`, ROM più stato a `gui.c:4052`, ROM recenti, avvio), ma dopo `scePowerUnlock` (`memory.c:3694`): il lock è preso a `:3639` e un dialogo lì dentro rimanderebbe lo standby finché resta aperto. Un esito FAILED apre due domande con `yesno_dialog` (`main.c:2337`): «Il salvataggio non si legge. Riprovare?» e, se no, «Giocare senza salvare? Il file resta com'è.». Un nuovo tentativo che non trova il file conta come illeggibile. Due no annullano il caricamento (`LOAD_GAMEPAK_CANCELLED` = -3, distinto da -1 e dal -2 di `load_file_zip`) e tornano alla lista delle ROM; all'avvio due no aprono il menu. Mentre le domande sono aperte il nome della ROM è vuoto, così una sospensione non scrive lo stato AUTO. Le nuove voci `MSG_*` vanno nell'enum `MESSAGE_ITEM` prima di `MSG_END` e nei cinque `message_*.h`, nello stesso ordine. +- **Cosa:** Implementata nel branch. Il controllo sta in `load_gamepak`, unico punto comune a tutti i caricamenti di ROM (menu a `gui.c:3952`, ROM più stato a `gui.c:4052`, ROM recenti, avvio), ma dopo `scePowerUnlock` (`memory.c:3694`): il lock è preso a `:3639` e un dialogo lì dentro rimanderebbe lo standby finché resta aperto. Un esito FAILED apre due domande con `yesno_dialog` (`main.c:2337`): «Il salvataggio non si legge. Riprovare?» e, se no, «Giocare senza salvare? Il file resta com'è.». Un nuovo tentativo che non trova il file conta come illeggibile. Due no annullano il caricamento (`LOAD_GAMEPAK_CANCELLED` = -3, distinto da -1 e dal -2 di `load_file_zip`) e tornano alla lista delle ROM; all'avvio da `game.gba` o dal browser due no aprono il menu, mentre all'avvio da launcher (ROM passata in argv) chiudono l'emulatore, come già fa un caricamento da launcher fallito (decisione del 2026-11-16). Mentre le domande sono aperte il nome della ROM è vuoto, così una sospensione non scrive lo stato AUTO. Le nuove voci `MSG_*` vanno nell'enum `MESSAGE_ITEM` prima di `MSG_END` e nei cinque `message_*.h`, nello stesso ordine. - **File:** `source/src/memory.c`, `source/src/message.h`, `source/src/text/message_jpn.h`, `source/src/text/message_eng.h`, `source/src/text/message_chs.h`, `source/src/text/message_cht.h`, `source/src/text/message_ita.h`, `tests/test_memory_backup.c`, `tests/test_startup.py`, `scripts/check-lifecycle.sh` - **Test:** `scripts/check-messages.sh`; `scripts/check-lifecycle.sh`: in `load_gamepak` nessun dialogo fra `scePowerLock` e `scePowerUnlock`; `test_memory_backup.c`: Riprova rilegge, Gioca senza salvare mantiene il blocco; `tests/test_startup.py`: lo scenario di annullamento; PSP: una directory chiamata `.sav` nella cartella dei salvataggi (`sceIoGetstat` la trova, `sceIoOpen` dovrebbe rifiutarla: da confermare alla prima prova, altrimenti guasto iniettato in una build diagnostica F-137). - **Accettazione:** Riprova rilegge davvero il file; con Gioca senza salvare la data di modifica del `.sav` resta invariata dopo l'uscita; `check-messages.sh` verde. diff --git a/docs/saves.md b/docs/saves.md index 128b2e9..18cdb88 100644 --- a/docs/saves.md +++ b/docs/saves.md @@ -114,8 +114,9 @@ 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 +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 diff --git a/docs/wip/save-store.md b/docs/wip/save-store.md index 453101e..5fe96ed 100644 --- a/docs/wip/save-store.md +++ b/docs/wip/save-store.md @@ -16,7 +16,7 @@ - [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 `.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 `.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)_ From 845d44505520f34f9ec5e2bce019708a345deaf7 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:48:32 +0100 Subject: [PATCH 02/11] Count the main menu's state slots without recovering each one Since savestate_file_exists() recovers a cut save first, the Save and Load row cost about ten sceIo calls per empty slot, some 121 each time the main menu opened. The count now stats each slot once; the Load page, the benchmark and every load still recover first. --- source/src/gui.c | 29 ++++++++++++------ tests/test_save_io.py | 70 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/source/src/gui.c b/source/src/gui.c index f89da17..66a842a 100644 --- a/source/src/gui.c +++ b/source/src/gui.c @@ -3123,25 +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, never per frame: each check is a file system stat. */ + entered, 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; @@ -3155,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. diff --git a/tests/test_save_io.py b/tests/test_save_io.py index 7f796b5..4957134 100644 --- a/tests/test_save_io.py +++ b/tests/test_save_io.py @@ -268,15 +268,83 @@ "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 +#include +#include +#include +#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)", +] + class SaveIoTests(unittest.TestCase): + 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) From e7212666e3cf9dee7cac1cc0cd79536ee8a0d729 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:50:34 +0100 Subject: [PATCH 03/11] Make the fake store's full device return 0, as the sceIo binding does save_io_write returns 0 on ENOSPC, but the fake returned -1, so no test fed save_store a 0 and the guard against spinning on a full Memory Stick could go unnoticed. The fake now returns 0 and asserts that nothing writes again after it, so a regression fails instead of hanging. --- tests/fake_store_io.c | 13 +++++++++++-- tests/fake_store_io.h | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/fake_store_io.c b/tests/fake_store_io.c index efda830..ecf2d2f 100644 --- a/tests/fake_store_io.c +++ b/tests/fake_store_io.c @@ -2,6 +2,7 @@ * In-memory .sav file system for the save tests; see fake_store_io.h. */ +#include #include #include "fake_store_io.h" @@ -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) { @@ -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; diff --git a/tests/fake_store_io.h b/tests/fake_store_io.h index 7a50570..2aad815 100644 --- a/tests/fake_store_io.h +++ b/tests/fake_store_io.h @@ -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 */ @@ -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 From 97790f3137b73679dd22608a5293c14d7adc829d Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:51:33 +0100 Subject: [PATCH 04/11] Test that a savestate repair takes the values in use before the load The repair test's fallback values matched the obvious defaults, so a sanitize that ignored them passed, and no valid state had type NONE. The fallback now differs from every default, and a NONE state must load unchanged. --- tests/test_save_format.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/tests/test_save_format.c b/tests/test_save_format.c index 0652518..c774c71 100644 --- a/tests/test_save_format.c +++ b/tests/test_save_format.c @@ -223,6 +223,17 @@ static void test_sanitize_keeps_valid_states(void) assert(save_format_sanitize(&state, &before) == 0); assert(same_state(&state, ©)); } + + /* 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, ©)); + } } /* Each field out of range is put back and reported in its own bit. */ @@ -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; @@ -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(); From 560700bf1c80f0112effed86331de50812a09397 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:51:59 +0100 Subject: [PATCH 05/11] Say that the save events keep runtime log v6 in their tickets SAV-1.6, SAV-1.7 and SAV-2.4 still said the format stays v5, and SAV-1.7 that its event was missing. The tree logs SAVE_LOAD, SAVESTATE_REPAIR and SAVE_WRITE in format 6, ENG-1.6's version. --- docs/BACKLOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index 3487cf8..edadcbc 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -928,13 +928,13 @@ Il gruppo copre lettura e scrittura del `.sav`, i savestate, i guasti di emulazi - **Accettazione:** Riprova rilegge davvero il file; con Gioca senza salvare la data di modifica del `.sav` resta invariata dopo l'uscita; `check-messages.sh` verde. #### 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 -- **Cosa:** Quando `save_format` segnala incoerenza (taglia diversa da quella nativa, file oltre 128 KiB, file troncato da versioni precedenti), la prima transazione di SAV-2.1 conserva il vecchio primario come `.sav.orig`: il `.bak` appena creato viene spostato, senza copie né spazio in più, se `.orig` non esiste già. Finché `.orig` manca lo store non cancella mai quel `.bak`; se lo spostamento fallisce, la scrittura successiva fallisce in modo visibile (SAV-2.3) invece di perdere l'originale. Un nuovo evento `RUNTIME_LOG_EVENT_SAVE_LOAD`, in coda all'enum (`runtime_log.h:51-75`), registra tipo, taglia, dimensione del file, esito e incoerenza. Per la regola R1 (principio 6 della panoramica) un valore in coda all'enum non cambia `RUNTIME_LOG_VERSION`: il formato resta v5 (il decoder mostra già gli eventi ignoti come `event-N`, `tools/decode_runtime_log.py:162`) e il decoder impara nome e valori. È il primo tipo di evento nuovo dalla v5: la regola sui valori del decoder l'hanno già usata i gruppi di LINK_COUNTERS delle 1.4.1-1.4.3. Per questo aggiorna anche il commento d'intestazione di `runtime_log.h:16`, che oggi dice che i valori degli enum fanno parte del formato: dopo dirà che un valore aggiunto in coda a un enum mantiene la versione, mentre il layout dei record e il significato dei valori esistenti fanno parte del formato. Se la regola viene rifiutata, l'evento passa nel bump di ENG-1.6 nella 1.5. +- **Cosa:** Quando `save_format` segnala incoerenza (taglia diversa da quella nativa, file oltre 128 KiB, file troncato da versioni precedenti), la prima transazione di SAV-2.1 conserva il vecchio primario come `.sav.orig`: il `.bak` appena creato viene spostato, senza copie né spazio in più, se `.orig` non esiste già. Finché `.orig` manca lo store non cancella mai quel `.bak`; se lo spostamento fallisce, la scrittura successiva fallisce in modo visibile (SAV-2.3) invece di perdere l'originale. Un nuovo evento `RUNTIME_LOG_EVENT_SAVE_LOAD`, in coda all'enum (`runtime_log.h:51-75`), registra tipo, taglia, dimensione del file, esito e incoerenza. Per la regola R1 (principio 6 della panoramica) un valore in coda all'enum non cambia `RUNTIME_LOG_VERSION`: il formato resta v6, quello di ENG-1.6 (il decoder mostra già gli eventi ignoti come `event-N`, `tools/decode_runtime_log.py:162`) e il decoder impara nome e valori. È il primo tipo di evento nuovo dalla v5: la regola sui valori del decoder l'hanno già usata i gruppi di LINK_COUNTERS delle 1.4.1-1.4.3. Per questo aggiorna anche il commento d'intestazione di `runtime_log.h:16`, che oggi dice che i valori degli enum fanno parte del formato: dopo dirà che un valore aggiunto in coda a un enum mantiene la versione, mentre il layout dei record e il significato dei valori esistenti fanno parte del formato. Se la regola viene rifiutata, l'evento passa nel bump di ENG-1.6 nella 1.5. - **File:** `source/src/save_store.c`, `source/src/memory.c`, `source/src/runtime_log.h` (evento e commento d'intestazione), `tools/decode_runtime_log.py`, `tests/test_save_store.c`, `tests/test_decode_runtime_log.py` - **Test:** `test_save_store.c`: `.orig` creato una sola volta e mai sovrascritto, taglio di corrente a ogni passo, move fallita che lascia il `.bak`; `tests/test_decode_runtime_log.py`: decodifica di SAVE_LOAD; i gruppi 7 e 8 di LINK_COUNTERS si decodificano per nome, e il gruppo dopo l'ultimo noto (il massimo di LINK_COUNTERS più 1) come counters-N. - **Accettazione:** ogni decisione su tipo e taglia si legge nel runtime log di una build di release; `.orig` esiste solo dopo un'incoerenza e poi non cambia più; nessuna sequenza di guasti perde il file originale. #### SAV-1.7 — Argine immediato per gli stati che indicizzano fuori dal buffer · S, 0,5 gg · dipende da: SAV-1.1 · PSP: no -- **Cosa:** Implementata nel branch (`save_format_sanitize`, con ripiego sui valori in uso prima del caricamento); manca l'evento del runtime log. Dopo `SAVESTATE_BLOCK(read)` (`memory.c:3801` e `:3842`; nel branch del BIOS aperto `load_state` passa già da `load_state_silent`, quindi il punto è uno) si riportano in range i campi che indicizzano `gamepak_backup` o ne fissano la lunghezza: `flash_bank` (`:2039`, `:2211`), `eeprom_address` ed `eeprom_counter` (`:1935`, `:1941`, `:1981`), `sram_size`, `flash_size` ed `eeprom_size` (lunghezza scritta da `save_backup`, `:3244-3262`) e `backup_type`. Lo fa una funzione pura `save_format_sanitize`, che restituisce la maschera dei campi corretti; la maschera va nel runtime log con un evento in coda all'enum, che per la stessa regola di SAV-1.6 (R1) lascia il formato a v5. Non è la validazione completa di SAV-4: chiude solo letture e scritture fuori buffer. +- **Cosa:** Implementata nel branch (`save_format_sanitize`, con ripiego sui valori in uso prima del caricamento), con l'evento `RUNTIME_LOG_EVENT_SAVESTATE_REPAIR` nel runtime log. Dopo `SAVESTATE_BLOCK(read)` (`memory.c:3801` e `:3842`; nel branch del BIOS aperto `load_state` passa già da `load_state_silent`, quindi il punto è uno) si riportano in range i campi che indicizzano `gamepak_backup` o ne fissano la lunghezza: `flash_bank` (`:2039`, `:2211`), `eeprom_address` ed `eeprom_counter` (`:1935`, `:1941`, `:1981`), `sram_size`, `flash_size` ed `eeprom_size` (lunghezza scritta da `save_backup`, `:3244-3262`) e `backup_type`. Lo fa una funzione pura `save_format_sanitize`, che restituisce la maschera dei campi corretti; la maschera va nel runtime log con un evento in coda all'enum, che per la stessa regola di SAV-1.6 (R1) lascia il formato a v6. Non è la validazione completa di SAV-4: chiude solo letture e scritture fuori buffer. - **File:** `source/src/save_format.c`, `source/src/save_format.h`, `source/src/memory.c`, `source/src/runtime_log.h`, `tools/decode_runtime_log.py`, `tests/test_save_format.c`, `tests/test_decode_runtime_log.py` - **Test:** ogni campo fuori range torna al valore sicuro ed è segnato nella maschera; i valori validi restano invariati; decodifica del nuovo evento; ASan. - **Accettazione:** nessun valore letto da un `.svs` fa leggere o scrivere fuori da `gamepak_backup`; gli stati validi si caricano identici. @@ -987,7 +987,7 @@ Il gruppo copre lettura e scrittura del `.sav`, i savestate, i guasti di emulazi - **Accettazione:** un errore di scrittura non perde mai il salvataggio in RAM finché il gioco resta aperto; il `.sav` su disco è sempre il vecchio o il nuovo. #### SAV-2.4 — Misura di ogni flush nel runtime log · S, 0,5 gg · dipende da: SAV-2.3 · PSP: sì -- **Cosa:** Un nuovo evento `RUNTIME_LOG_EVENT_SAVE_WRITE`, in coda all'enum, registra durata in µs, byte, esito e numero di tentativi. Il formato resta v5 per la stessa regola di SAV-1.6 (R1); decoder e test vengono aggiornati. Tre sessioni su PSP-1000 (un gioco Flash 128 KiB, uno SRAM e una sessione link con scambio) formano la baseline del gate di SAV-5. +- **Cosa:** Un nuovo evento `RUNTIME_LOG_EVENT_SAVE_WRITE`, in coda all'enum, registra durata in µs, byte, esito e numero di tentativi. Il formato resta v6 per la stessa regola di SAV-1.6 (R1); decoder e test vengono aggiornati. Tre sessioni su PSP-1000 (un gioco Flash 128 KiB, uno SRAM e una sessione link con scambio) formano la baseline del gate di SAV-5. - **File:** `source/src/runtime_log.h`, `source/src/memory.c`, `tools/decode_runtime_log.py`, `tests/test_decode_runtime_log.py` - **Test:** `tests/test_decode_runtime_log.py`: decodifica di SAVE_WRITE; PSP-1000: tre runtime log con p50, p95 e massimo della durata. - **Accettazione:** ogni flush lascia un record; dall'ultima scrittura del gioco alla fine del flush passa al massimo 1 s al p99 (10 frame più la durata registrata) nelle tre sessioni; la baseline accompagna la decisione su SAV-5. From ce49a51c27724a1506f3fc66f4f2bb061b69bf37 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:52:12 +0100 Subject: [PATCH 06/11] Cite the main merge that brought the save test ROM The note gave b5cffbf, the ROM link check fix; the save type test ROM (SAV-2.6) reached main with dbab178. --- docs/wip/save-store.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/wip/save-store.md b/docs/wip/save-store.md index 5fe96ed..aa07a6f 100644 --- a/docs/wip/save-store.md +++ b/docs/wip/save-store.md @@ -3,7 +3,7 @@ - **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. From 39613565e1b73233e2b7631dde577c83b9a63d5c Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:52:35 +0100 Subject: [PATCH 07/11] Describe the write-first save ROM target as the code runs it EXT-1.2 said the target writes a whole generation before any read. It writes one block (3Fh) on every boot, since a generation would destroy what read_back checks, and after the first boot it runs as eeprom8k plus that write. --- docs/BACKLOG.md | 2 +- docs/wip/save-store.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index edadcbc..62924fb 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -6694,7 +6694,7 @@ Il gruppo raccoglie quello che le altre sezioni avevano lasciato fuori: tre dife - **Licenza:** codice nuovo GPL-2.0-or-later; la regola sulle scritture viene da libretro/gpsp a commit fissato 5819380 (`gba_memory.c:987-989`, GPL-2.0-or-later), la condizione esatta è nostra. #### 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ì -- **Cosa:** Nuovo target `eeprom8k_wfirst` nella ROM dei salvataggi di SAV-2.6 (`tests/save-rom`), costruito con il suo include `tests/gba-rom.mk`. Con il `.sav` assente scrive una generazione prima di qualunque lettura e la rilegge. L'esito segue il protocollo di SAV-2.6: PASS o FAIL su schermo e firma nel blocco riservato della EEPROM. Il codice del protocollo non legge la EEPROM prima di quella scrittura: la firma si scrive dopo. Ai riavvii successivi si comporta come `eeprom8k`. Si esegue nella sessione hardware di SAV-2.7. +- **Cosa:** Nuovo target `eeprom8k_wfirst` nella ROM dei salvataggi di SAV-2.6 (`tests/save-rom`), costruito con il suo include `tests/gba-rom.mk`. Prima di qualunque lettura scrive un blocco, l'ultimo dello spazio della firma (3Fh), con un DMA da 81 unità come il primo salvataggio di un gioco, e lo rilegge; col `.sav` assente l'emulatore deve dedurre la taglia da quella scrittura. L'esito segue il protocollo di SAV-2.6: PASS o FAIL su schermo e firma nel blocco riservato della EEPROM. Il codice del protocollo non legge la EEPROM prima di quella scrittura: la firma si scrive dopo. La ROM non distingue un chip vuoto senza leggerlo, quindi scrive quel blocco a ogni avvio: una generazione intera, come diceva questo testo fino al 2026-11-16, distruggerebbe quella che `read_back` controlla. Ai riavvii successivi si comporta come `eeprom8k`, più quella scrittura. Si esegue nella sessione hardware di SAV-2.7. - **File:** `tests/save-rom/source/main.c`, `tests/save-rom/Makefile`, `docs/HARDWARE-RESULTS.md` - **Test:** PSP-1000 e il secondo modello di SAV-2.7, con la build 1.4.2 (FAIL atteso) e con il candidato 1.4.4 (PASS); controllo della dimensione del `.sav`; firma letta con `tools/decode_test_rom.py`. - **Accettazione:** PASS sul candidato con entrambi i modelli; `.sav` di 8192 byte; esito registrato con lo sha256 di EBOOT.PBP. diff --git a/docs/wip/save-store.md b/docs/wip/save-store.md index aa07a6f..36a45b4 100644 --- a/docs/wip/save-store.md +++ b/docs/wip/save-store.md @@ -11,7 +11,7 @@ ## 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 From a59a3e4d917d50a45ff09f07807a0fabc13b19d7 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:52:48 +0100 Subject: [PATCH 08/11] Name the Link Cable menu as the menu does in the changelog --- CHANGELOG.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3b0265..1c54118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,11 +65,11 @@ - Before a link session starts, the game's save is written and a copy of it is kept as `.sav.link1`; the two copies before it move to `.link2` and `.link3`. If the save cannot be written or copied, the session does - not start and the Link menu says why. -- Link > Restore pre-link save puts one of those copies back, chosen by its - date and time. The current save is kept as `.sav.bak`, and the game - restarts on the restored save. The item is not available during a - session. + not start and the Link Cable menu says why. +- Link Cable > Restore pre-link save puts one of those copies back, chosen + by its date and time. The current save is kept as `.sav.bak`, and + the game restarts on the restored save. The item is not available during + a session. - The runtime log records how every `.sav` was read (its size, the save chip chosen and whether they disagree), how long every save write took, whether it worked and how many tries it needed, how long the save copy From 53a4fc1935e4adf895d00fd25431506da4aa8e62 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:14:07 +0100 Subject: [PATCH 09/11] Catch a slot stat in the per-frame main menu status The main menu now counts its state slots with savestate_file_stat(), so the guard that keeps file system calls out of menu_draw_main_status() looks for that name too. --- scripts/check-menu-layout.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/check-menu-layout.sh b/scripts/check-menu-layout.sh index 1ae6bb5..78d496f 100644 --- a/scripts/check-menu-layout.sh +++ b/scripts/check-menu-layout.sh @@ -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 From c184a7723e267275adb74fe0581b3160548b46a1 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:14:08 +0100 Subject: [PATCH 10/11] Rewrap the unreadable save paragraph in docs/saves.md --- docs/saves.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/saves.md b/docs/saves.md index 18cdb88..e9cadbb 100644 --- a/docs/saves.md +++ b/docs/saves.md @@ -116,8 +116,8 @@ 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 (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. +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 From 3db2bcf7b54ee507ae531215fe75951508d57eb8 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:14:08 +0100 Subject: [PATCH 11/11] Drop the fallback for a refused rule from SAV-1.6 The ticket already says the format stays at v6, the version ENG-1.6 bumped to, so the fallback to that bump no longer applies. --- docs/BACKLOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index 62924fb..03bf332 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -928,7 +928,7 @@ Il gruppo copre lettura e scrittura del `.sav`, i savestate, i guasti di emulazi - **Accettazione:** Riprova rilegge davvero il file; con Gioca senza salvare la data di modifica del `.sav` resta invariata dopo l'uscita; `check-messages.sh` verde. #### 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 -- **Cosa:** Quando `save_format` segnala incoerenza (taglia diversa da quella nativa, file oltre 128 KiB, file troncato da versioni precedenti), la prima transazione di SAV-2.1 conserva il vecchio primario come `.sav.orig`: il `.bak` appena creato viene spostato, senza copie né spazio in più, se `.orig` non esiste già. Finché `.orig` manca lo store non cancella mai quel `.bak`; se lo spostamento fallisce, la scrittura successiva fallisce in modo visibile (SAV-2.3) invece di perdere l'originale. Un nuovo evento `RUNTIME_LOG_EVENT_SAVE_LOAD`, in coda all'enum (`runtime_log.h:51-75`), registra tipo, taglia, dimensione del file, esito e incoerenza. Per la regola R1 (principio 6 della panoramica) un valore in coda all'enum non cambia `RUNTIME_LOG_VERSION`: il formato resta v6, quello di ENG-1.6 (il decoder mostra già gli eventi ignoti come `event-N`, `tools/decode_runtime_log.py:162`) e il decoder impara nome e valori. È il primo tipo di evento nuovo dalla v5: la regola sui valori del decoder l'hanno già usata i gruppi di LINK_COUNTERS delle 1.4.1-1.4.3. Per questo aggiorna anche il commento d'intestazione di `runtime_log.h:16`, che oggi dice che i valori degli enum fanno parte del formato: dopo dirà che un valore aggiunto in coda a un enum mantiene la versione, mentre il layout dei record e il significato dei valori esistenti fanno parte del formato. Se la regola viene rifiutata, l'evento passa nel bump di ENG-1.6 nella 1.5. +- **Cosa:** Quando `save_format` segnala incoerenza (taglia diversa da quella nativa, file oltre 128 KiB, file troncato da versioni precedenti), la prima transazione di SAV-2.1 conserva il vecchio primario come `.sav.orig`: il `.bak` appena creato viene spostato, senza copie né spazio in più, se `.orig` non esiste già. Finché `.orig` manca lo store non cancella mai quel `.bak`; se lo spostamento fallisce, la scrittura successiva fallisce in modo visibile (SAV-2.3) invece di perdere l'originale. Un nuovo evento `RUNTIME_LOG_EVENT_SAVE_LOAD`, in coda all'enum (`runtime_log.h:51-75`), registra tipo, taglia, dimensione del file, esito e incoerenza. Per la regola R1 (principio 6 della panoramica) un valore in coda all'enum non cambia `RUNTIME_LOG_VERSION`: il formato resta v6, quello di ENG-1.6 (il decoder mostra già gli eventi ignoti come `event-N`, `tools/decode_runtime_log.py:162`) e il decoder impara nome e valori. È il primo tipo di evento nuovo dalla v5: la regola sui valori del decoder l'hanno già usata i gruppi di LINK_COUNTERS delle 1.4.1-1.4.3. Per questo aggiorna anche il commento d'intestazione di `runtime_log.h:16`, che oggi dice che i valori degli enum fanno parte del formato: dopo dirà che un valore aggiunto in coda a un enum mantiene la versione, mentre il layout dei record e il significato dei valori esistenti fanno parte del formato. - **File:** `source/src/save_store.c`, `source/src/memory.c`, `source/src/runtime_log.h` (evento e commento d'intestazione), `tools/decode_runtime_log.py`, `tests/test_save_store.c`, `tests/test_decode_runtime_log.py` - **Test:** `test_save_store.c`: `.orig` creato una sola volta e mai sovrascritto, taglio di corrente a ogni passo, move fallita che lascia il `.bak`; `tests/test_decode_runtime_log.py`: decodifica di SAVE_LOAD; i gruppi 7 e 8 di LINK_COUNTERS si decodificano per nome, e il gruppo dopo l'ultimo noto (il massimo di LINK_COUNTERS più 1) come counters-N. - **Accettazione:** ogni decisione su tipo e taglia si legge nel runtime log di una build di release; `.orig` esiste solo dopo un'incoerenza e poi non cambia più; nessuna sequenza di guasti perde il file originale.