From 5ac5168a6d979146987bf393201da37537afe027 Mon Sep 17 00:00:00 2001 From: neauoire Date: Fri, 5 Mar 2021 10:06:09 -0800 Subject: [PATCH] Completed load/save impl --- emulator.c | 57 +++++++------- examples/app.nasu.usm | 177 +++++++++++++++++++++++++++++++++--------- examples/blank.usm | 4 + uxn.c | 6 +- uxn.h | 3 +- 5 files changed, 179 insertions(+), 68 deletions(-) diff --git a/emulator.c b/emulator.c index 054f60f..b343d27 100644 --- a/emulator.c +++ b/emulator.c @@ -383,17 +383,22 @@ sprite_poke(Uint8 *m, Uint16 ptr, Uint8 b0, Uint8 b1) Uint8 file_poke(Uint8 *m, Uint16 ptr, Uint8 b0, Uint8 b1) { - FILE *f; - ptr += 8; + char *name = &m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]]; + Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3]; if(b0 == 0x0d) { - char *name = &m[(m[ptr] << 8) + m[ptr + 1]]; - Uint16 length = (m[ptr + 2] << 8) + m[ptr + 3]; - Uint16 addr = (m[ptr + 4] << 8) + b1; - f = fopen(name, "w"); - if(!fwrite(&m[addr], length, 1, f)) - return error("Save", "Failure"); - fclose(f); - printf("Exported %s[%d bytes]\n", name, length); + Uint16 addr = (m[ptr + 8 + 4] << 8) + b1; + FILE *f = fopen(name, "r"); + if(f && 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)) { + fclose(f); + printf("Saved %d bytes, at %04x from %s\n", length, addr, name); + } } return b1; } @@ -467,22 +472,22 @@ main(int argc, char **argv) if(!init()) return error("Init", "Failed"); - portuxn(&u, "console", ppnil, console_poke); - devscreen = portuxn(&u, "screen", ppnil, screen_poke); - portuxn(&u, "sprite", ppnil, sprite_poke); - devctrl = portuxn(&u, "controller", ppnil, ppnil); - devkey = portuxn(&u, "key", ppnil, ppnil); - devmouse = portuxn(&u, "mouse", ppnil, ppnil); - portuxn(&u, "file", ppnil, file_poke); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "empty", ppnil, ppnil); - portuxn(&u, "system", ppnil, system_poke); + portuxn(&u, "console", console_poke); + devscreen = portuxn(&u, "screen", screen_poke); + portuxn(&u, "sprite", sprite_poke); + devctrl = portuxn(&u, "controller", ppnil); + devkey = portuxn(&u, "key", ppnil); + devmouse = portuxn(&u, "mouse", ppnil); + portuxn(&u, "file", file_poke); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "empty", ppnil); + portuxn(&u, "system", system_poke); /* Write screen size to dev/screen */ u.ram.dat[devscreen->addr + 0] = (HOR * 8 >> 8) & 0xff; diff --git a/examples/app.nasu.usm b/examples/app.nasu.usm index e3cdb78..f3bcbba 100644 --- a/examples/app.nasu.usm +++ b/examples/app.nasu.usm @@ -4,7 +4,7 @@ &Sprite { pad 8 x 2 y 2 addr 2 color 1 } &Controller { buttons 1 } &Mouse { x 2 y 2 state 1 chord 1 change 1 } -&File { pad 8 name 2 length 2 binary 2 } +&File { pad 8 name 2 length 2 load 2 save 2 } &Rect2d { x1 2 y1 2 x2 2 y2 2 } &Point2d { x 2 y 2 } @@ -74,11 +74,20 @@ BRK ,redraw JSR ,click-end JMP @no-toolbar-click + ( load button ) + + ,no-load-click ~dev/mouse.y ~bankview.y #0010 SUB2 SUB2 #0008 DIV2 #0000 NEQ2 JMP? POP2 + ,no-load-click ~dev/mouse.x ~tileview.x SUB2 #0008 DIV2 #000e NEQU2 JMP? POP2 + ( load ) ,filename =dev/file.name #0800 =dev/file.length ~bankview.addr =dev/file.load + ( release ) #00 =dev/mouse.state + ,redraw JSR ,click-end JMP + @no-load-click + ( save button ) ,no-save-click ~dev/mouse.y ~bankview.y #0010 SUB2 SUB2 #0008 DIV2 #0000 NEQ2 JMP? POP2 ,no-save-click ~dev/mouse.x ~tileview.x SUB2 #0008 DIV2 #000f NEQU2 JMP? POP2 - ( save ) ,save_name =dev/file.name #0800 =dev/file.length ~bankview.addr =dev/file.binary + ( save ) ,filename =dev/file.name #0800 =dev/file.length ~bankview.addr =dev/file.save ( release ) #00 =dev/mouse.state ,redraw JSR ,click-end JMP @no-save-click @@ -397,7 +406,7 @@ RTS @blank_icn [ 0000 0000 0000 0000 ] @cell1_icn [ 7cfe fefe fefe 7c00 ] @save_icn [ fe82 8282 848e f400 ] -@save_name [ nasu_export.chr 00 ] +@filename [ nasu_project.chr 00 ] @font_hex ( 0-F ) [ @@ -545,38 +554,134 @@ RTS |E800 @bank2 [ - 0000 0000 0000 0000 0000 2400 7e3c 0000 0000 2400 3c42 0000 0000 6c7c 7c38 1000 - 0010 387c 7c38 1000 0038 387c 6c10 3800 0010 387c 7c10 3800 0000 0018 1800 0000 - 007e 4242 4242 7e00 0000 1824 2418 0000 0018 2442 4224 1800 001e 063a 4a48 3000 - 0038 446c 107c 1000 000c 0808 0838 3800 003e 2222 2266 6600 0000 0822 0022 0800 - 0000 1018 1c18 1000 0000 0818 3818 0800 0008 1c00 001c 0800 0028 2828 2800 2800 - 003e 4a4a 3a0a 0a00 000c 3046 620c 3000 0000 0000 0000 ffff 0010 3800 3810 0038 - 0008 1c2a 0808 0800 0008 0808 2a1c 0800 0000 0804 7e04 0800 0000 1020 7e20 1000 - 0000 4040 7e00 0000 0000 0024 6624 0000 0000 1038 7c00 0000 0000 007c 3810 0000 - 0000 0000 0000 0000 0008 0808 0800 0800 0014 1400 0000 0000 0024 7e24 247e 2400 - 0008 1e28 1c0a 3c08 0042 0408 1020 4200 0030 4832 4c44 3a00 0008 1000 0000 0000 - 0004 0808 0808 0400 0010 0808 0808 1000 0000 1408 3e08 1400 0000 0808 3e08 0800 - 0000 0000 0008 0810 0000 0000 3c00 0000 0000 0000 0000 0800 0000 0204 0810 2000 - 003c 464a 5262 3c00 0018 2808 0808 3e00 003c 4202 3c40 7e00 003c 421c 0242 3c00 - 0008 1828 487e 0800 007e 407c 0242 3c00 003c 407c 4242 3c00 007e 0204 0810 1000 - 003c 423c 4242 3c00 003c 4242 3e02 3c00 0000 0008 0000 0800 0000 0800 0008 0810 - 0000 0810 2010 0800 0000 003e 003e 0000 0000 1008 0408 1000 003c 4202 0c00 0800 - 003c 425a 5442 3c00 0018 2442 7e42 4200 007c 427c 4242 7c00 003c 4240 4042 3c00 - 0078 4442 4244 7800 007e 407c 4040 7e00 003e 4040 7c40 4000 003c 4240 4e42 3c00 - 0042 427e 4242 4200 003e 0808 0808 3e00 0002 0202 4242 3c00 0044 4870 4844 4200 - 0040 4040 4040 7e00 0042 665a 4242 4200 0042 6252 4a46 4200 003c 4242 4242 3c00 - 007c 4242 7c40 4000 003c 4242 524a 3c00 007c 4242 7c44 4200 003c 403c 0242 3c00 - 00fe 1010 1010 1000 0042 4242 4242 3c00 0042 4242 4224 1800 0042 4242 5a66 4200 - 0042 2418 1824 4200 0082 4428 1010 1000 007e 0408 1020 7e00 000c 0808 0808 0c00 - 0040 2010 0804 0200 0018 0808 0808 1800 0008 1422 0000 0000 0000 0000 0000 7e00 - 0008 0400 0000 0000 0000 1c02 1e22 1e00 0020 203c 2222 3c00 0000 1e20 2020 1e00 - 0002 021e 2222 1e00 0000 1c22 3c20 1e00 000c 101c 1010 1000 0000 1c22 221e 021c - 0020 202c 3222 2200 0008 0018 0808 0400 0008 0008 0808 4830 0020 2428 3028 2400 - 0010 1010 1010 0c00 0000 6854 5454 5400 0000 5864 4444 4400 0000 3844 4444 3800 - 0000 7844 4478 4040 0000 3c44 443c 0406 0000 2c30 2020 2000 0000 3840 3804 7800 - 0010 103c 1010 0c00 0000 4444 4444 3800 0000 4444 2828 1000 0000 4454 5454 2800 - 0000 4428 1028 4400 0000 4444 443c 0438 0000 7c08 1020 7c00 000c 0810 1008 0c00 - 0008 0808 0808 0800 0030 1008 0810 3000 0000 0032 4c00 0000 3c42 99a1 a199 423c + 0001 0303 0303 0307 c0c0 c0c0 c000 4000 + 1010 1030 3018 3030 0000 0000 0000 0000 + 0000 0800 1000 0010 0000 0000 0000 0101 + 0409 0840 5090 1230 0000 0040 0080 0000 + 0000 0000 0000 0000 0000 0010 0000 2000 + 0000 0000 0000 0000 0000 0000 0100 0000 + 0080 0000 0000 0000 0000 0000 0000 0000 + 0707 0303 0301 0101 dfdf dfdf ffff fff7 + 0606 0606 060c 0c04 4080 8040 0000 4040 + 1030 3030 3020 6030 0000 0000 0000 0000 + 0020 0020 8020 80c0 0102 0200 0004 0400 + 2030 7060 6460 e0e1 4080 8000 0080 0000 + 0400 0808 0010 1101 4040 4040 8080 8080 + 0204 040c 0c08 0808 0002 0000 0400 0004 + 2020 2040 4040 40a0 0000 0002 0004 0000 + 0000 0000 0000 0001 ffff ffff ff7f 7f7f + 0c0c 1818 0818 1810 0040 0000 4000 0040 + 2060 5050 4050 5048 0101 0101 0101 0303 + 8080 2080 8020 0000 0808 0109 1111 0332 + a1a0 8222 2062 2220 0000 0000 0000 0000 + 0123 0303 0745 058d 8000 0000 0000 0000 + 1818 1210 1030 3004 0009 0901 0312 120a + a080 c0c0 00c0 8080 0402 020c 0504 1c0d + 8000 8200 0002 4000 7f7f 7f7f 7f7f 7f7f + 1010 3030 0020 2060 0000 1000 2000 0000 + 4050 5848 0898 4848 0303 0302 0206 0206 + 0020 0040 2000 4000 2222 0224 2444 400c + 4444 4444 4048 4044 0000 0001 0100 0202 + 091b 1202 2222 0242 0000 0000 0004 0009 + 5040 4020 a8a0 a020 0614 0c29 2910 5150 + 8080 8000 0000 8000 2018 481a 1090 3231 + 0000 8004 0000 0802 7f7f 7f7f 7f7f 7f7f + 6040 cc03 c000 e407 0000 0010 c030 1806 + 4848 0848 4808 2848 0404 0504 0408 0808 + 4040 0040 4040 0040 4848 8010 9090 30a0 + 4800 4048 10d0 0020 0204 0404 0109 282a + 4206 8084 0404 0404 0101 1202 0212 0424 + 4050 4040 4141 42c2 22a3 a042 42c6 8484 + 0001 0002 0204 0400 2420 6248 48c2 9090 + 0004 0010 0400 0008 7f7f 7fff 7fff ffff + c07e 0f0b 0480 8060 e31d c63b 1c0f 1930 + 0824 c4c4 f07c bcdd 0809 0808 0019 0811 + 0020 0181 4100 2202 a020 2040 4000 4000 + 9080 2020 a0a0 0041 1250 4424 a0a8 8850 + 0000 0404 0008 0800 0448 0800 1111 1101 + 8185 8782 0a08 1404 8404 0808 0808 1010 + 0809 0212 1224 2526 8484 1080 2820 0060 + 2001 0101 2301 1343 ffff ffff ffff ffff + f038 cf70 0000 0000 e000 ff00 0000 0000 + 5d2e e6f1 0800 0400 1014 d010 61c8 3010 + 4282 9240 0490 9000 0000 0080 0000 8000 + 4140 4202 8241 0502 5040 a080 4000 008d + 0108 0000 121f 7037 2222 06c4 54c0 00fd + 0810 1030 2020 0000 1011 2020 2125 4142 + 4a4a 9a96 1434 2c2c 1040 4000 8080 0081 + 4707 078f 0f4f 1f9f ffff ffff ffff ffff + 0080 f07e 8ff1 feff 0000 0000 e07c 0feb + 0000 0000 0000 8080 0000 0000 0000 0000 + 0010 0000 0800 0000 0000 0000 0001 0000 + 8284 0400 0900 0610 1942 2722 8800 0000 + bcef bee0 3008 0300 07fc ff11 1008 8864 + e03c c7f8 df03 0000 0614 84f8 18c0 f88f + 495b 1a1a 5ab6 b4b4 0000 0102 0200 0404 + 0f1c 1830 3030 6061 ff7f 3f3f 3f3f 3f7f + ff3f 0700 0000 0000 ffff ff7f 0600 0000 + c0c0 c080 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 300e 0300 0000 0000 + 063c c03b 07ff 0000 0307 fefc f080 0001 + ece8 6878 d0d0 f0a0 0008 0801 1111 0323 + e7cf dfd3 a1c1 c3e3 ffff ffff ffff ffff + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0001 0303 0301 0000 + 00c0 f83f c3f8 ff7f 0000 00c0 fe3f 81fe + 0000 0000 00ff ff00 0101 0103 0ff7 df1f + e0e0 c0c0 c0c0 8181 2307 474f 4fcf 9f9f + a3e3 c7c7 c7cf 8f9f ffff ffff ffff ffff + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 1f07 0000 0000 0000 ffff 7f0f 0000 0000 + f8ff ffff 7f00 0000 3fff ffff c60e 0e0c + 8383 0306 060e 0e0c 3e3e 3c39 7163 67cf + 3f7f ffff ffff ffff ffff ffff ffff ffff + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 8080 4040 6030 3838 + 0000 0000 0003 071f 0000 0020 e0c0 8000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0003 0206 + 0000 0000 4000 0000 1c1c 1838 3870 71f1 + 1c5d 1db8 bb33 73f3 df9f bfff ef3f 7fff + ffff ffff ffff ffff ffff ffff ffff ffff + 8080 c0e0 f0f8 fcfc 0000 0000 0000 0000 + 0000 0000 000e 0300 1f1f 0500 0000 8070 + ff78 3204 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0101 0307 0f1f 3f7f e0e2 e3c1 c5c7 838b + f3e5 e5e3 ebc7 d7c7 ffff ffff ffff ffff + ffff ffff ffff ffff ffff ffff ffff ffff + feff bfdf dfff f7fb 0000 80c0 c0e0 f0f0 + 0000 0000 0301 0000 1c07 0100 00c0 e070 + 0020 ff01 0000 0000 0000 80fc f070 70f0 + 0000 0000 0000 0000 0000 0000 0000 0000 + 0000 0000 0000 0103 0001 030f 1fff ffff + ffff fffe feff fefe 0717 1f1f 3f3f 7f7f + cfcf cf9f df9f bebd ffef dfbf bf7b f7ef + ffff ffff ffff ffff fffe fcfd f9f3 f9e3 + fbef efff ffff ffff f8fc fefe ffff ffff + 0000 0000 0080 80c0 7e0f 0100 0000 0000 + 04f8 e000 0000 0000 6060 2020 3010 0010 + 0000 0000 0000 0000 0000 0001 071f 7fff + 0f1f 7fff ffff ffff ffff fdfd fbf7 ffff + fefc fdf9 fbf9 e7f3 7fff ffff ffff fffe + bf7f 7f7f 7ffe dff7 efdf bfbf 7fff efdf + ffff ffff ffff fffe d7f7 e7cf 9fbf 3ffe + ffff dfe7 f3f9 fcfe ffff ffff ffff ffff + e0e0 f0f8 fcfe feff 0000 0000 0000 0080 + 0000 0000 0000 0003 1010 1001 171f 7fff + 030f 3fff ffff ffff ffff ffff ffff ffff + fdfb f7ff ffff fdfb ffff ffff efff ffff + d7e7 afcf dfbf bfff feff fbff ffff ffff + f7ef dfff be7e fdef df9f 3f7f 7fff ffff + fefd ffff ffff ffff ffff fbfe fcfb faf9 ] @@ -589,4 +694,4 @@ RTS |FF60 ;dev/file File |FFF0 .RESET .FRAME .ERROR ( vectors ) -|FFF8 [ 35ac c03c afac ] ( palette ) \ No newline at end of file +|FFF8 [ e0fc 30cc 30ac ] ( palette ) \ No newline at end of file diff --git a/examples/blank.usm b/examples/blank.usm index ff75e50..e5df554 100644 --- a/examples/blank.usm +++ b/examples/blank.usm @@ -4,7 +4,9 @@ &Screen { width 2 height 2 pad 4 y 2 x 2 color 1 } &Sprite { pad 8 y 2 x 2 addr 2 color 1 } &Controller { buttons 1 } +&Keyboard { key 1 } &Mouse { x 2 y 2 state 1 chord 1 } +&File { rname 2 rlen 2 rbin 2 rtxt 2 wname 2 wlen 2 wbin 2 wtxt 2 } &Label2d { x 2 y 2 color 1 addr 2 } &Picture2d { x 2 y 2 width 2 height 2 color 1 addr 2 } @@ -19,7 +21,9 @@ |FF10 ;dev/screen Screen |FF20 ;dev/sprite Sprite |FF30 ;dev/ctrl Controller +|FF40 ;dev/key Keyboard |FF50 ;dev/mouse Mouse +|FF60 ;dev/file File |FFF0 .RESET .FRAME .ERROR ( vectors ) |FFF8 [ f2ac 35bb 2b53 ] ( palette ) diff --git a/uxn.c b/uxn.c index 7caa5cb..e5b3701 100644 --- a/uxn.c +++ b/uxn.c @@ -19,9 +19,8 @@ WITH REGARD TO THIS SOFTWARE. void setflag(Uint8 *a, char flag, int b) { if(b) *a |= flag; else *a &= (~flag); } int getflag(Uint8 *a, char flag) { return *a & flag; } Uint8 devpoke8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].poke(u->ram.dat, 0xff00 + id * 0x10, b0, b1) : b1; } -Uint8 devpeek8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].peek(u->ram.dat, 0xff00 + id * 0x10, b0, b1) : b1; } void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = a >= 0xff00 ? devpoke8(u, (a & 0xff) >> 4, a & 0xf, b) : b; } -Uint8 mempeek8(Uxn *u, Uint16 a) { return a >= 0xff00 ? devpeek8(u, (a & 0xff) >> 4, a & 0xf, u->ram.dat[a]) : u->ram.dat[a]; } +Uint8 mempeek8(Uxn *u, Uint16 a) { return u->ram.dat[a]; } void mempoke16(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); } Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, a + 1); } void push8(Stack *s, Uint8 a) { s->dat[s->ptr++] = a; } @@ -205,11 +204,10 @@ loaduxn(Uxn *u, char *filepath) } Device * -portuxn(Uxn *u, char *name, Uint8 (*pefn)(Uint8 *m, Uint16 ptr, Uint8 b0, Uint8 b1), Uint8 (*pofn)(Uint8 *m, Uint16 ptr, Uint8 b0, Uint8 b1)) +portuxn(Uxn *u, char *name, Uint8 (*pofn)(Uint8 *m, Uint16 ptr, Uint8 b0, Uint8 b1)) { Device *d = &u->dev[u->devices++]; d->addr = 0xff00 + (u->devices - 1) * 0x10; - d->peek = pefn; d->poke = pofn; printf("Device #%d: %s, at 0x%04x \n", u->devices - 1, name, d->addr); return d; diff --git a/uxn.h b/uxn.h index c4f9e94..549528c 100644 --- a/uxn.h +++ b/uxn.h @@ -33,7 +33,6 @@ typedef struct { typedef struct Device { Uint16 addr; - Uint8 (*peek)(Uint8 *, Uint16, Uint8, Uint8); Uint8 (*poke)(Uint8 *, Uint16, Uint8, Uint8); } Device; @@ -50,4 +49,4 @@ int getflag(Uint8 *status, char flag); int loaduxn(Uxn *c, char *filepath); int bootuxn(Uxn *c); int evaluxn(Uxn *u, Uint16 vec); -Device *portuxn(Uxn *u, char *name, Uint8 (*pefn)(Uint8 *, Uint16, Uint8, Uint8), Uint8 (*pofn)(Uint8 *, Uint16, Uint8, Uint8)); +Device *portuxn(Uxn *u, char *name, Uint8 (*pofn)(Uint8 *, Uint16, Uint8, Uint8));