aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-08-27 21:55:15 +0200
committerBad Diode <bd@badd10de.dev>2023-08-27 21:55:15 +0200
commit941ad8e28efb685e910b38b06f98bc79fb29677f (patch)
tree6a208ff821ba707f6eb9bd405a7dde4edfa11c7e
parent9fc5ff0b00e1ca72c75721d3bdc4ff2287b48fc2 (diff)
downloaduxngba-941ad8e28efb685e910b38b06f98bc79fb29677f.tar.gz
uxngba-941ad8e28efb685e910b38b06f98bc79fb29677f.zip
Add the `add2, sub2, litr, lit2r` ops
-rw-r--r--src/main.c49
-rw-r--r--src/uxn-core.s212
2 files changed, 136 insertions, 125 deletions
diff --git a/src/main.c b/src/main.c
index 21c8e0e..e10c8f0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -517,12 +517,38 @@ init_uxn() {
517 // uxn_boot(u, uxn_ram, uxn_dei, uxn_deo); 517 // uxn_boot(u, uxn_ram, uxn_dei, uxn_deo);
518 518
519 // Copy rom to VM. 519 // Copy rom to VM.
520 u8 uxn_rom[] = { 520 u8 uxn_rom[32] = {
521 // Hello world (h) 521 // Hello world (h)
522 // 0x80, 0x68, 0x80, 0x18, 0x17, 522 // 0x80, 0x68, 0x80, 0x18, 0x17,
523 // 523 //
524 // ADD test 524 // ADD test
525 0x80, 0x04, 0x80, 0x03, 0x80, 0x02, 0x80, 0x01, 0x18 525 // 0x80, 0x04, 0x80, 0x03, 0x80, 0x02, 0x80, 0x01, 0x18, 0x18
526 // SUB test
527 // 0x80, 0x04, 0x80, 0x03, 0x80, 0x02, 0x80, 0x01, 0x19, 0x19, 0x19
528 // LIT2 04 03 LIT2 02 01 ADD ADD ADD BRK
529 // 0xa0, 0x04, 0x03,
530 // 0xa0, 0x04, 0x03, 0xa0, 0x02, 0x01,
531 // 0xa0, 0x04, 0x03, 0xa0, 0x02, 0x01, 0x18, 0x18, 0x00
532 // #0004 #0008 ADD
533 // 0xa0, 0x00, 0x04, 0xa0, 0x00, 0x08, 0x18,
534 // #0004 #0008 ADD2
535 0x80, 0xFF, 0xff,
536 0xa0, 0x00, 0x08, 0xa0, 0x00, 0x04, 0x38,
537 // 0xa0, 0x00, 0x08, 0xa0, 0x00, 0x03, 0x39,
538 // 0xa0, 0x00, 0x01, 0x38,
539 //
540 // 0x80, 0xFF, 0xff,
541 // 0xa0, 0xba, 0xdd,
542 // 0xa0, 0x10, 0xde,
543 // 0xa0, 0xfe, 0xed,
544 // 0xa0, 0xde, 0xaf,
545 // 0xa0, 0xba, 0xdd,
546 // 0xa0, 0x10, 0xde,
547 // 0xa0, 0xfe, 0xed,
548 // 0xa0, 0xde, 0xaf,
549 //
550 // #0008 #0003 SUB2
551 // 0xa0, 0x00, 0x08, 0xa0, 0x00, 0x03,
526 }; 552 };
527 memcpy(uxn_ram + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom)); 553 memcpy(uxn_ram + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));
528} 554}
@@ -715,7 +741,6 @@ handle_input(Uxn *u) {
715 741
716#define STACK_SIZE 16 742#define STACK_SIZE 16
717u8 stack[STACK_SIZE] = {0}; 743u8 stack[STACK_SIZE] = {0};
718extern void uxn_init_asm(void);
719extern void uxn_eval_asm(u16 pc); 744extern void uxn_eval_asm(u16 pc);
720 745
721// TODO: This should be on the IWRAM for maximum speed, as these are operations 746// TODO: This should be on the IWRAM for maximum speed, as these are operations
@@ -766,29 +791,21 @@ main(void) {
766 rst[i] = 0; 791 rst[i] = 0;
767 io_ports[i] = 0; 792 io_ports[i] = 0;
768 } 793 }
769 uxn_init_asm();
770 794
771 txt_printf("ROM\n\n"); 795 txt_printf("ROM\n\n");
772 for (size_t i = 0; i < 8; i++) { 796 for (size_t i = 0; i < 32; i++) {
773 txt_printf("%02x ", uxn_ram[i + PAGE_PROGRAM]); 797 if (i % 8 == 0) {
774 }
775 txt_printf("\n\n");
776
777 txt_printf("STACK (BEFORE)\n");
778 txt_printf("PTR: %d\n", wst_ptr - (uintptr_t)wst);
779 for (size_t i = 0; i < STACK_SIZE; i++) {
780 if (i % 4 == 0) {
781 txt_printf("\n"); 798 txt_printf("\n");
782 } 799 }
783 txt_printf("%02x ", wst[i]); 800 txt_printf("%02x ", uxn_ram[i + PAGE_PROGRAM]);
784 } 801 }
785 txt_printf("\n\n"); 802 txt_printf("\n\n");
786 803
787 uxn_eval_asm(PAGE_PROGRAM); 804 uxn_eval_asm(PAGE_PROGRAM);
788 txt_printf("STACK (AFTER)\n"); 805 txt_printf("STACK\n");
789 txt_printf("PTR: %d\n", wst_ptr - (uintptr_t)wst); 806 txt_printf("PTR: %d\n", wst_ptr - (uintptr_t)wst);
790 for (size_t i = 0; i < STACK_SIZE; i++) { 807 for (size_t i = 0; i < STACK_SIZE; i++) {
791 if (i % 4 == 0) { 808 if (i % 8 == 0) {
792 txt_printf("\n"); 809 txt_printf("\n");
793 } 810 }
794 txt_printf("%02x ", wst[i]); 811 txt_printf("%02x ", wst[i]);
diff --git a/src/uxn-core.s b/src/uxn-core.s
index 2e7a165..a9fc3f9 100644
--- a/src/uxn-core.s
+++ b/src/uxn-core.s
@@ -20,47 +20,13 @@ rst_ptr: .word rst
20.global io_ports 20.global io_ports
21io_ports: .space 256 21io_ports: .space 256
22 22
23@
24@ r0 is our custom stack pointer
25@ r1 is the value we want to insert/extract
26@
27@ When the stack grows downwards:
28@ push {r1} == str r1, [sp, #-4]!
29@ pop {r1} == ldr r1, [sp], #4
30@
31@ In our case the stack grow upwards:
32@
33@ 8bit:
34@ push {r1} == strb r1, [sp], #1
35@ pop {r1} == ldrb r1, [sp, #-1]!
36@
37@ 32bit:
38@ push {r1} == str r1, [sp], #4
39@ pop {r1} == ldr r1, [sp, #-4]!
40@
41
42.global uxn_init_asm
43uxn_init_asm:
44 @ DEBUG: Initialize the stack for testing purposes.
45 ldr r0, wst_ptr
46 mov r1, #4
47 strb r1, [r0], #1
48 sub r1, r1, #1
49 strb r1, [r0], #1
50 sub r1, r1, #1
51 strb r1, [r0], #1
52 sub r1, r1, #1
53 strb r1, [r0], #1
54 adr r1, wst_ptr
55 str r0, [r1]
56 bx lr
57
58@ UXN evaluation function. 23@ UXN evaluation function.
59@ 24@
60@ r0: PC pointer (argument is offset from RAM). 25@ r0: PC pointer (argument is offset from RAM).
61@ r1: Stack pointer (wst/rst). 26@ r1: Stack pointer (wst/rst).
62@ r2: Ram ptr. 27@ r2: Ram ptr.
63@ r3-r7: Scratch registers. 28@ r3-r7: Scratch registers.
29@
64.global uxn_eval_asm 30.global uxn_eval_asm
65uxn_eval_asm: 31uxn_eval_asm:
66 @ Initialization. 32 @ Initialization.
@@ -95,13 +61,13 @@ brk:
95 b uxn_ret 61 b uxn_ret
96 62
97jci: 63jci:
98 b uxn_ret 64 b uxn_decode
99 65
100jmi: 66jmi:
101 b uxn_ret 67 b uxn_decode
102 68
103jsi: 69jsi:
104 b uxn_ret 70 b uxn_decode
105 71
106litr: 72litr:
107 ldrb r3, [r0], #1 73 ldrb r3, [r0], #1
@@ -109,201 +75,229 @@ litr:
109 b uxn_decode 75 b uxn_decode
110 76
111lit2r: 77lit2r:
112 b uxn_ret 78 ldrb r3, [r0], #1
79 ldrb r4, [r0], #1
80 strb r3, [r1], #1
81 strb r4, [r1], #1
82 b uxn_decode
113 83
114inc: 84inc:
115 b uxn_ret 85 b uxn_decode
116 86
117inc2: 87inc2:
118 b uxn_ret 88 b uxn_decode
119 89
120pop: 90pop:
121 b uxn_ret 91 b uxn_decode
122 92
123pop2: 93pop2:
124 b uxn_ret 94 b uxn_decode
125 95
126nip: 96nip:
127 b uxn_ret 97 b uxn_decode
128 98
129nip2: 99nip2:
130 b uxn_ret 100 b uxn_decode
131 101
132swp: 102swp:
133 b uxn_ret 103 b uxn_decode
134 104
135swp2: 105swp2:
136 b uxn_ret 106 b uxn_decode
137 107
138rot: 108rot:
139 b uxn_ret 109 b uxn_decode
140 110
141rot2: 111rot2:
142 b uxn_ret 112 b uxn_decode
143 113
144dup: 114dup:
145 b uxn_ret 115 b uxn_decode
146 116
147dup2: 117dup2:
148 b uxn_ret 118 b uxn_decode
149 119
150ovr: 120ovr:
151 b uxn_ret 121 b uxn_decode
152 122
153ovr2: 123ovr2:
154 b uxn_ret 124 b uxn_decode
155 125
156equ: 126equ:
157 b uxn_ret 127 b uxn_decode
158 128
159equ2: 129equ2:
160 b uxn_ret 130 b uxn_decode
161 131
162neq: 132neq:
163 b uxn_ret 133 b uxn_decode
164 134
165neq2: 135neq2:
166 b uxn_ret 136 b uxn_decode
167 137
168gth: 138gth:
169 b uxn_ret 139 b uxn_decode
170 140
171gth2: 141gth2:
172 b uxn_ret 142 b uxn_decode
173 143
174lth: 144lth:
175 b uxn_ret 145 b uxn_decode
176 146
177lth2: 147lth2:
178 b uxn_ret 148 b uxn_decode
179 149
180jmp: 150jmp:
181 b uxn_ret 151 b uxn_decode
182 152
183jmp2: 153jmp2:
184 b uxn_ret 154 b uxn_decode
185 155
186jcn: 156jcn:
187 b uxn_ret 157 b uxn_decode
188 158
189jcn2: 159jcn2:
190 b uxn_ret 160 b uxn_decode
191 161
192jsr: 162jsr:
193 b uxn_ret 163 b uxn_decode
194 164
195jsr2: 165jsr2:
196 b uxn_ret 166 b uxn_decode
197 167
198sth: 168sth:
199 b uxn_ret 169 b uxn_decode
200 170
201sth2: 171sth2:
202 b uxn_ret 172 b uxn_decode
203 173
204ldz: 174ldz:
205 b uxn_ret 175 b uxn_decode
206 176
207ldz2: 177ldz2:
208 b uxn_ret 178 b uxn_decode
209 179
210stz: 180stz:
211 b uxn_ret 181 b uxn_decode
212 182
213stz2: 183stz2:
214 b uxn_ret 184 b uxn_decode
215 185
216ldr: 186ldr:
217 b uxn_ret 187 b uxn_decode
218 188
219ldr2: 189ldr2:
220 b uxn_ret 190 b uxn_decode
221 191
222str: 192str:
223 b uxn_ret 193 b uxn_decode
224 194
225str2: 195str2:
226 b uxn_ret 196 b uxn_decode
227 197
228lda: 198lda:
229 b uxn_ret 199 b uxn_decode
230 200
231lda2: 201lda2:
232 b uxn_ret 202 b uxn_decode
233 203
234sta: 204sta:
235 b uxn_ret 205 b uxn_decode
236 206
237sta2: 207sta2:
238 b uxn_ret 208 b uxn_decode
239 209
240dei: 210dei:
241 b uxn_ret 211 b uxn_decode
242 212
243dei2: 213dei2:
244 b uxn_ret 214 b uxn_decode
245 215
246deo: 216deo:
247 b uxn_ret 217 b uxn_decode
248 218
249deo2: 219deo2:
250 b uxn_ret 220 b uxn_decode
251 221
252add: 222add:
253 ldr r3, [r1, #-1]! 223 ldr r3, [r1, #-1]!
254 ldr r4, [r1, #-1]! 224 ldr r4, [r1, #-1]!
255 add r3, r3, r4 225 add r3, r3, r4
256 strb r3, [r1], #1 226 strb r3, [r1], #1
257 b uxn_ret @ NOTE: This should return to the next loop iteration. 227 b uxn_decode
258 228
259add2: 229add2:
260 b uxn_ret 230 ldrb r3, [r1, #-1]!
231 lsl r3, #8
232 ldrb r5, [r1, #-1]!
233 add r3, r5
234 ldrb r4, [r1, #-1]!
235 ldrb r5, [r1, #-1]!
236 lsl r4, #8
237 add r4, r5
238 add r3, r3, r4
239 strb r3, [r1], #1
240 lsr r3, #8
241 strb r3, [r1], #1
242 b uxn_decode
261 243
262sub: 244sub:
263 @ ldr r2, [r0, #-1]! 245 ldr r3, [r1, #-1]!
264 @ ldr r1, [r0, #-1]! 246 ldr r4, [r1, #-1]!
265 @ sub r1, r1, r2 247 sub r4, r4, r3
266 @ strb r1, [r0], #1 248 strb r4, [r1], #1
267 b uxn_ret @ NOTE: This should return to the next loop iteration. 249 b uxn_decode
268 250
269sub2: 251sub2:
270 b uxn_ret 252 ldrb r3, [r1, #-1]!
253 ldrb r5, [r1, #-1]!
254 lsl r3, #8
255 add r3, r5
256 ldrb r4, [r1, #-1]!
257 ldrb r5, [r1, #-1]!
258 lsl r4, #8
259 add r4, r5
260 sub r3, r4, r3
261 strb r3, [r1], #1
262 lsr r3, #8
263 strb r3, [r1], #1
264 b uxn_decode
271 265
272mul: 266mul:
273 b uxn_ret 267 b uxn_decode
274 268
275mul2: 269mul2:
276 b uxn_ret 270 b uxn_decode
277 271
278div: 272div:
279 b uxn_ret 273 b uxn_decode
280 274
281div2: 275div2:
282 b uxn_ret 276 b uxn_decode
283 277
284and: 278and:
285 b uxn_ret 279 b uxn_decode
286 280
287and2: 281and2:
288 b uxn_ret 282 b uxn_decode
289 283
290ora: 284ora:
291 b uxn_ret 285 b uxn_decode
292 286
293ora2: 287ora2:
294 b uxn_ret 288 b uxn_decode
295 289
296eor: 290eor:
297 b uxn_ret 291 b uxn_decode
298 292
299eor2: 293eor2:
300 b uxn_ret 294 b uxn_decode
301 295
302sft: 296sft:
303 b uxn_ret 297 b uxn_decode
304 298
305sft2: 299sft2:
306 b uxn_ret 300 b uxn_decode
307 301
308@ OP table 302@ OP table
309op_table: 303op_table:
@@ -468,7 +462,7 @@ op_table:
468 .word brk @ 0x9d 462 .word brk @ 0x9d
469 .word brk @ 0x9e 463 .word brk @ 0x9e
470 .word brk @ 0x9f 464 .word brk @ 0x9f
471 .word brk @ 0xa0 465 .word lit2r @ 0xa0
472 .word brk @ 0xa1 466 .word brk @ 0xa1
473 .word brk @ 0xa2 467 .word brk @ 0xa2
474 .word brk @ 0xa3 468 .word brk @ 0xa3