From 88fb08c2b22eaf407d251bcd48372f4d33fb5480 Mon Sep 17 00:00:00 2001 From: Andrew Alderwick Date: Tue, 23 Mar 2021 19:40:03 +0000 Subject: [PATCH] Add offset and append switch to file device. --- src/debugger.c | 7 ++++--- src/emulator.c | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/debugger.c b/src/debugger.c index 6790fd9..f6fed1e 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -59,17 +59,18 @@ file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) Uint8 *m = u->ram.dat; char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]]; Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3]; + Uint16 offset = (m[ptr + 0] << 8) + m[ptr + 1]; if(b0 == 0x0d) { Uint16 addr = (m[ptr + 8 + 4] << 8) + b1; FILE *f = fopen(name, "r"); - if(f && fread(&m[addr], length, 1, f)) { + if(f && fseek(f, offset, SEEK_SET) != -1 && fread(&m[addr], length, 1, f)) { fclose(f); printf("Loaded %d bytes, at %04x from %s\n", length, addr, name); } } else if(b0 == 0x0f) { Uint16 addr = (m[ptr + 8 + 6] << 8) + b1; - FILE *f = fopen(name, "w"); - if(fwrite(&m[addr], length, 1, f)) { + FILE *f = fopen(name, (m[ptr + 2] & 0x1) ? "a" : "w"); + if(f && fseek(f, offset, SEEK_SET) != -1 && fwrite(&m[addr], length, 1, f)) { fclose(f); printf("Saved %d bytes, at %04x from %s\n", length, addr, name); } diff --git a/src/emulator.c b/src/emulator.c index 994f166..1c007d2 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -365,17 +365,18 @@ file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) Uint8 *m = u->ram.dat; char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]]; Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3]; + Uint16 offset = (m[ptr + 0] << 8) + m[ptr + 1]; if(b0 == 0x0d) { Uint16 addr = (m[ptr + 8 + 4] << 8) + b1; FILE *f = fopen(name, "r"); - if(f && fread(&m[addr], length, 1, f)) { + if(f && fseek(f, offset, SEEK_SET) != -1 && fread(&m[addr], length, 1, f)) { fclose(f); printf("Loaded %d bytes, at %04x from %s\n", length, addr, name); } } else if(b0 == 0x0f) { Uint16 addr = (m[ptr + 8 + 6] << 8) + b1; - FILE *f = fopen(name, "w"); - if(fwrite(&m[addr], length, 1, f)) { + FILE *f = fopen(name, (m[ptr + 2] & 0x1) ? "a" : "w"); + if(f && fseek(f, offset, SEEK_SET) != -1 && fwrite(&m[addr], length, 1, f)) { fclose(f); printf("Saved %d bytes, at %04x from %s\n", length, addr, name); }