commit 88fb08c2b22eaf407d251bcd48372f4d33fb5480
parent 2e90428b0c3baacc456dd4e53cf91401a56df49c
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date: Tue, 23 Mar 2021 19:40:03 +0000
Add offset and append switch to file device.
Diffstat:
2 files changed, 8 insertions(+), 6 deletions(-)
diff --git 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
@@ -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);
}