diff options
author | Bad Diode <bd@badd10de.dev> | 2023-08-28 19:13:29 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2023-08-28 19:13:29 +0200 |
commit | d1f218fdaa8ab08508e565b8169a904d47dda1b3 (patch) | |
tree | a98c3a28643407763cec551f331f208cefa50f5b | |
parent | 4a98636f412b5bbcafdbc04e4780b9118170f201 (diff) | |
download | uxngba-d1f218fdaa8ab08508e565b8169a904d47dda1b3.tar.gz uxngba-d1f218fdaa8ab08508e565b8169a904d47dda1b3.zip |
Add zero-page load/store LDZ/STZ ops
-rw-r--r-- | src/main.c | 66 | ||||
-rw-r--r-- | src/uxn-core.s | 99 |
2 files changed, 131 insertions, 34 deletions
@@ -116,16 +116,31 @@ init_uxn() { | |||
116 | // 0xa0, 0x0f, 0x88, 0x1e, // -> ( 87 ) | 116 | // 0xa0, 0x0f, 0x88, 0x1e, // -> ( 87 ) |
117 | // 0xa0, 0xf0, 0x08, 0x1e, // -> ( f8 ) | 117 | // 0xa0, 0xf0, 0x08, 0x1e, // -> ( f8 ) |
118 | // 0xa0, 0xf0, 0x0f, 0xa0, 0xaa, 0xaa, 0x3e, // -> ( 5a a5 ) | 118 | // 0xa0, 0xf0, 0x0f, 0xa0, 0xaa, 0xaa, 0x3e, // -> ( 5a a5 ) |
119 | // SFT. | 119 | // // SFT. |
120 | 0xa0, 0x34, 0x10, 0x1f, // -> ( 68 ) | 120 | // 0xa0, 0x34, 0x10, 0x1f, // -> ( 68 ) |
121 | 0xa0, 0x34, 0x01, 0x1f, // -> ( 1a ) | 121 | // 0xa0, 0x34, 0x01, 0x1f, // -> ( 1a ) |
122 | 0xa0, 0x34, 0x33, 0x1f, // -> ( 30 ) | 122 | // 0xa0, 0x34, 0x33, 0x1f, // -> ( 30 ) |
123 | 0xa0, 0x12, 0x48, 0x80, 0x34, 0x3f, // -> ( 09 20 ) | 123 | // 0xa0, 0x12, 0x48, 0x80, 0x34, 0x3f, // -> ( 09 20 ) |
124 | 124 | // STZ / LDZ. | |
125 | 0xa0, 0x34, 0x00, 0x11, // STZ (u8*)uxn_ram[1] = 0x34 | ||
126 | 0xa0, 0xaa, 0xbb, 0x80, 0x01, 0x31, // STZ2 (u16*)uxn_ram[1] = 0x34 | ||
127 | 0x80, 0x00, 0x10, // LDZ -> ( 34 ) | ||
128 | 0x80, 0x01, 0x30, // LDZ2 -> ( aa bb ) | ||
125 | }; | 129 | }; |
126 | memcpy(uxn_ram + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom)); | 130 | memcpy(uxn_ram + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom)); |
127 | } | 131 | } |
128 | 132 | ||
133 | // TODO: | ||
134 | // - Division | ||
135 | // - DEI/DEO | ||
136 | // - Relative load/store | ||
137 | // - Absolute load/store | ||
138 | // - Jump instructions (JMP/JCN/JCI/JMI/JSI/JSR) | ||
139 | // - OPr modes | ||
140 | // - OPk modes | ||
141 | // - OPrk modes | ||
142 | // | ||
143 | |||
129 | int | 144 | int |
130 | main(void) { | 145 | main(void) { |
131 | // Adjust system wait times. | 146 | // Adjust system wait times. |
@@ -167,23 +182,48 @@ main(void) { | |||
167 | io_ports[i] = 0; | 182 | io_ports[i] = 0; |
168 | } | 183 | } |
169 | 184 | ||
170 | txt_printf("ROM"); | 185 | // txt_printf("ROM"); |
186 | // for (size_t i = 0; i < 64; i++) { | ||
187 | // if (i % 8 == 0) { | ||
188 | // txt_printf("\n"); | ||
189 | // } | ||
190 | // txt_printf("%02x ", uxn_ram[i + PAGE_PROGRAM]); | ||
191 | // } | ||
192 | // txt_printf("\n\n"); | ||
193 | |||
194 | uxn_eval_asm(PAGE_PROGRAM); | ||
195 | txt_printf("WST ("); | ||
196 | txt_printf("SIZE: %d)", wst_ptr - (uintptr_t)wst); | ||
171 | for (size_t i = 0; i < 64; i++) { | 197 | for (size_t i = 0; i < 64; i++) { |
172 | if (i % 8 == 0) { | 198 | if (i % 8 == 0) { |
173 | txt_printf("\n"); | 199 | txt_printf("\n"); |
174 | } | 200 | } |
175 | txt_printf("%02x ", uxn_ram[i + PAGE_PROGRAM]); | 201 | if (i >= (wst_ptr - (uintptr_t)wst)) { |
202 | txt_printf("%02x ", 0); | ||
203 | } else { | ||
204 | txt_printf("%02x ", wst[i]); | ||
205 | } | ||
176 | } | 206 | } |
207 | // txt_printf("\n\n"); | ||
208 | // txt_printf("RST ("); | ||
209 | // txt_printf("SIZE: %d)", rst_ptr - (uintptr_t)rst); | ||
210 | // for (size_t i = 0; i < 64; i++) { | ||
211 | // if (i % 8 == 0) { | ||
212 | // txt_printf("\n"); | ||
213 | // } | ||
214 | // if (i >= (rst_ptr - (uintptr_t)rst)) { | ||
215 | // txt_printf("%02x ", 0); | ||
216 | // } else { | ||
217 | // txt_printf("%02x ", rst[i]); | ||
218 | // } | ||
219 | // } | ||
177 | txt_printf("\n\n"); | 220 | txt_printf("\n\n"); |
178 | 221 | txt_printf("RAM (ZP)\n"); | |
179 | uxn_eval_asm(PAGE_PROGRAM); | ||
180 | txt_printf("STACK ("); | ||
181 | txt_printf("PTR: %d)", wst_ptr - (uintptr_t)wst); | ||
182 | for (size_t i = 0; i < 64; i++) { | 222 | for (size_t i = 0; i < 64; i++) { |
183 | if (i % 8 == 0) { | 223 | if (i % 8 == 0) { |
184 | txt_printf("\n"); | 224 | txt_printf("\n"); |
185 | } | 225 | } |
186 | txt_printf("%02x ", wst[i]); | 226 | txt_printf("%02x ", uxn_ram[i]); |
187 | } | 227 | } |
188 | 228 | ||
189 | uintptr_t stack_size = wst_ptr - (uintptr_t)wst; | 229 | uintptr_t stack_size = wst_ptr - (uintptr_t)wst; |
diff --git a/src/uxn-core.s b/src/uxn-core.s index 9288ccf..ed860d6 100644 --- a/src/uxn-core.s +++ b/src/uxn-core.s | |||
@@ -23,17 +23,19 @@ io_ports: .space 256 | |||
23 | @ UXN evaluation function. | 23 | @ UXN evaluation function. |
24 | @ | 24 | @ |
25 | @ r0: PC pointer (argument is offset from RAM). | 25 | @ r0: PC pointer (argument is offset from RAM). |
26 | @ r1: Stack pointer (wst/rst). | 26 | @ r1: Stack pointer (wst). |
27 | @ r2: Ram ptr. | 27 | @ r2: Stack pointer (rst). |
28 | @ r3-r7: Scratch registers. | 28 | @ r7: Ram ptr. |
29 | @ r3-r6: Scratch registers. | ||
29 | @ | 30 | @ |
30 | .global uxn_eval_asm | 31 | .global uxn_eval_asm |
31 | uxn_eval_asm: | 32 | uxn_eval_asm: |
32 | @ Initialization. | 33 | @ Initialization. |
33 | push {r4-r7} | 34 | push {r4-r7} |
34 | ldr r1, wst_ptr | 35 | ldr r1, wst_ptr |
35 | ldr r2, =uxn_ram | 36 | ldr r2, rst_ptr |
36 | add r0, r0, r2 | 37 | ldr r7, =uxn_ram |
38 | add r0, r0, r7 | ||
37 | 39 | ||
38 | uxn_decode: | 40 | uxn_decode: |
39 | ldrb r3, [r0], #1 @ current OP value / table index | 41 | ldrb r3, [r0], #1 @ current OP value / table index |
@@ -44,9 +46,11 @@ uxn_decode: | |||
44 | bx r4 @ op_table[idx * 4]() | 46 | bx r4 @ op_table[idx * 4]() |
45 | 47 | ||
46 | uxn_ret: | 48 | uxn_ret: |
47 | @ Update stack pointer and return. | 49 | @ Update stack pointers and return. |
48 | adr r0, wst_ptr | 50 | adr r0, wst_ptr |
49 | str r1, [r0] | 51 | str r1, [r0] |
52 | adr r0, rst_ptr | ||
53 | str r2, [r0] | ||
50 | pop {r4-r7} | 54 | pop {r4-r7} |
51 | bx lr | 55 | bx lr |
52 | 56 | ||
@@ -54,6 +58,10 @@ uxn_ret: | |||
54 | @ Macros. | 58 | @ Macros. |
55 | @ | 59 | @ |
56 | 60 | ||
61 | .macro next a | ||
62 | ldrb \a, [r0], #1 | ||
63 | .endm | ||
64 | |||
57 | .macro wpop8 a | 65 | .macro wpop8 a |
58 | ldrb \a, [r1, #-1]! | 66 | ldrb \a, [r1, #-1]! |
59 | .endm | 67 | .endm |
@@ -78,6 +86,31 @@ uxn_ret: | |||
78 | ldrb \a, [r1, \b] | 86 | ldrb \a, [r1, \b] |
79 | .endm | 87 | .endm |
80 | 88 | ||
89 | .macro rpush8 a | ||
90 | strb \a, [r2], #1 | ||
91 | .endm | ||
92 | |||
93 | .macro rpush16 a | ||
94 | strb \a, [r2, #1] | ||
95 | lsr \a, #8 | ||
96 | strb \a, [r2], #2 | ||
97 | .endm | ||
98 | |||
99 | .macro zsave8 val, off | ||
100 | strb \val, [r7, \off] | ||
101 | .endm | ||
102 | |||
103 | .macro zsave16 val, off | ||
104 | add \off, r7 | ||
105 | strb \val, [\off, #1] | ||
106 | lsr \val, #8 | ||
107 | strb \val, [\off] | ||
108 | .endm | ||
109 | |||
110 | .macro zload8 dst, off | ||
111 | ldrb \dst, [r7, \off] | ||
112 | .endm | ||
113 | |||
81 | @ OP table | 114 | @ OP table |
82 | op_table: | 115 | op_table: |
83 | .word brk @ 0x00 | 116 | .word brk @ 0x00 |
@@ -96,7 +129,7 @@ op_table: | |||
96 | .word jcn @ 0x0d | 129 | .word jcn @ 0x0d |
97 | .word jsr @ 0x0e | 130 | .word jsr @ 0x0e |
98 | .word sth @ 0x0f | 131 | .word sth @ 0x0f |
99 | .word ldz @ 0x00 | 132 | .word ldz @ 0x10 |
100 | .word stz @ 0x11 | 133 | .word stz @ 0x11 |
101 | .word ldr @ 0x12 | 134 | .word ldr @ 0x12 |
102 | .word str @ 0x13 | 135 | .word str @ 0x13 |
@@ -209,7 +242,7 @@ op_table: | |||
209 | .word brk @ 0x7d | 242 | .word brk @ 0x7d |
210 | .word brk @ 0x7e | 243 | .word brk @ 0x7e |
211 | .word brk @ 0x7f | 244 | .word brk @ 0x7f |
212 | .word litr @ 0x80 | 245 | .word lit @ 0x80 |
213 | .word brk @ 0x81 | 246 | .word brk @ 0x81 |
214 | .word brk @ 0x82 | 247 | .word brk @ 0x82 |
215 | .word brk @ 0x83 | 248 | .word brk @ 0x83 |
@@ -241,7 +274,7 @@ op_table: | |||
241 | .word brk @ 0x9d | 274 | .word brk @ 0x9d |
242 | .word brk @ 0x9e | 275 | .word brk @ 0x9e |
243 | .word brk @ 0x9f | 276 | .word brk @ 0x9f |
244 | .word lit2r @ 0xa0 | 277 | .word lit2 @ 0xa0 |
245 | .word brk @ 0xa1 | 278 | .word brk @ 0xa1 |
246 | .word brk @ 0xa2 | 279 | .word brk @ 0xa2 |
247 | .word brk @ 0xa3 | 280 | .word brk @ 0xa3 |
@@ -331,10 +364,10 @@ op_table: | |||
331 | .word brk @ 0xf7 | 364 | .word brk @ 0xf7 |
332 | .word brk @ 0xf8 | 365 | .word brk @ 0xf8 |
333 | @ TODO: Can we mask this instead of having empty space? | 366 | @ TODO: Can we mask this instead of having empty space? |
334 | .word lit2r @ 0xf9 | 367 | .word lit2 @ 0xf9 |
335 | .word litr @ 0xfa | 368 | .word lit @ 0xfa |
336 | .word lit2r @ 0xfb | 369 | .word lit2 @ 0xfb |
337 | .word litr @ 0xfc | 370 | .word lit @ 0xfc |
338 | .word jsi @ 0xfd | 371 | .word jsi @ 0xfd |
339 | .word jmi @ 0xfe | 372 | .word jmi @ 0xfe |
340 | .word jci @ 0xff | 373 | .word jci @ 0xff |
@@ -347,24 +380,29 @@ brk: | |||
347 | b uxn_ret | 380 | b uxn_ret |
348 | 381 | ||
349 | jci: | 382 | jci: |
383 | wpop8 r3 | ||
384 | cmp r3, #0 | ||
385 | @ TODO: What to do here? Reference not fully clear. | ||
350 | b uxn_decode | 386 | b uxn_decode |
351 | 387 | ||
352 | jmi: | 388 | jmi: |
389 | @ TODO: What to do here? Reference not fully clear. | ||
353 | b uxn_decode | 390 | b uxn_decode |
354 | 391 | ||
355 | jsi: | 392 | jsi: |
393 | @ TODO: What to do here? Reference not fully clear. | ||
356 | b uxn_decode | 394 | b uxn_decode |
357 | 395 | ||
358 | litr: | 396 | lit: |
359 | ldrb r3, [r0], #1 | 397 | next r3 |
360 | strb r3, [r1], #1 | 398 | wpush8 r3 |
361 | b uxn_decode | 399 | b uxn_decode |
362 | 400 | ||
363 | lit2r: | 401 | lit2: |
364 | ldrb r3, [r0], #1 | 402 | next r3 |
365 | ldrb r4, [r0], #1 | 403 | next r4 |
366 | strb r3, [r1], #1 | 404 | wpush8 r3 |
367 | strb r4, [r1], #1 | 405 | wpush8 r4 |
368 | b uxn_decode | 406 | b uxn_decode |
369 | 407 | ||
370 | inc: | 408 | inc: |
@@ -544,21 +582,40 @@ jsr2: | |||
544 | b uxn_decode | 582 | b uxn_decode |
545 | 583 | ||
546 | sth: | 584 | sth: |
585 | wpop8 r3 | ||
586 | rpush8 r3 | ||
547 | b uxn_decode | 587 | b uxn_decode |
548 | 588 | ||
549 | sth2: | 589 | sth2: |
590 | wpop16 r3, r5 | ||
591 | rpush16 r3 | ||
550 | b uxn_decode | 592 | b uxn_decode |
551 | 593 | ||
552 | ldz: | 594 | ldz: |
595 | wpop8 r3 | ||
596 | zload8 r4, r3 | ||
597 | wpush8 r4 | ||
553 | b uxn_decode | 598 | b uxn_decode |
554 | 599 | ||
555 | ldz2: | 600 | ldz2: |
601 | wpop8 r3 | ||
602 | zload8 r4, r3 | ||
603 | wpush8 r4 | ||
604 | add r3, #1 | ||
605 | zload8 r4, r3 | ||
606 | wpush8 r4 | ||
556 | b uxn_decode | 607 | b uxn_decode |
557 | 608 | ||
558 | stz: | 609 | stz: |
610 | wpop8 r3 | ||
611 | wpop8 r4 | ||
612 | zsave8 r4, r3 | ||
559 | b uxn_decode | 613 | b uxn_decode |
560 | 614 | ||
561 | stz2: | 615 | stz2: |
616 | wpop8 r3 | ||
617 | wpop16 r4, r5 | ||
618 | zsave16 r4, r3 | ||
562 | b uxn_decode | 619 | b uxn_decode |
563 | 620 | ||
564 | ldr: | 621 | ldr: |