| ... | ... |
@@ -35,18 +35,16 @@ cc uxn.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -o uxn |
| 35 | 35 |
|
| 36 | 36 |
## Mission |
| 37 | 37 |
|
| 38 |
+- Catch overflow/underflow |
|
| 39 |
+- Jumps should be relative |
|
| 38 | 40 |
- constants |
| 39 | 41 |
- variables |
| 42 |
+- Pointers/Literals |
|
| 40 | 43 |
- A Three-Way Decision Routine(http://www.6502.org/tutorials/compare_instructions.html) |
| 41 | 44 |
- Carry flag? |
| 42 |
-- Loop |
|
| 43 |
-- Pointers/Literals |
|
| 44 | 45 |
- Print word to stdout |
| 45 | 46 |
- Draw pixel to screen |
| 46 | 47 |
- Detect mouse click |
| 47 |
-- Jumps should be relative |
|
| 48 |
-- Catch overflow/underflow |
|
| 49 |
-- Audo-detect literals length. |
|
| 50 | 48 |
- SDL Layer Emulator |
| 51 | 49 |
- Build PPU |
| 52 | 50 |
- Interrupts |
| ... | ... |
@@ -14,5 +14,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr |
| 14 | 14 |
cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn |
| 15 | 15 |
|
| 16 | 16 |
# run |
| 17 |
-./uxnasm examples/loop.usm boot.rom |
|
| 17 |
+./uxnasm examples/arithmetic.usm boot.rom |
|
| 18 | 18 |
./uxn boot.rom |
| ... | ... |
@@ -68,39 +68,21 @@ echo(Stack *s, Uint8 len, char *name) |
| 68 | 68 |
printf("\n\n");
|
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 |
-void |
|
| 72 |
-wspush(Uint8 v) |
|
| 73 |
-{
|
|
| 74 |
- cpu.wst.dat[cpu.wst.ptr++] = v; |
|
| 75 |
-} |
|
| 76 |
- |
|
| 77 |
-Uint8 |
|
| 78 |
-wspop(void) |
|
| 79 |
-{
|
|
| 80 |
- return cpu.wst.dat[--cpu.wst.ptr]; |
|
| 81 |
-} |
|
| 82 |
- |
|
| 83 |
-void |
|
| 84 |
-rspush(Uint8 v) |
|
| 85 |
-{
|
|
| 86 |
- cpu.rst.dat[cpu.rst.ptr++] = v; |
|
| 87 |
-} |
|
| 88 |
- |
|
| 89 |
-Uint8 |
|
| 90 |
-rspop(void) |
|
| 91 |
-{
|
|
| 92 |
- return cpu.rst.dat[--cpu.rst.ptr]; |
|
| 93 |
-} |
|
| 94 |
- |
|
| 95 | 71 |
#pragma mark - Operations |
| 96 | 72 |
|
| 97 | 73 |
/* clang-format off */ |
| 98 | 74 |
|
| 75 |
+void wspush(Uint8 v) { cpu.wst.dat[cpu.wst.ptr++] = v; }
|
|
| 76 |
+Uint8 wspop(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
|
|
| 77 |
+Uint8 wspeek(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; }
|
|
| 78 |
+void rspush(Uint8 v) { cpu.rst.dat[cpu.rst.ptr++] = v; }
|
|
| 79 |
+Uint8 rspop(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
|
|
| 80 |
+ |
|
| 99 | 81 |
void op_brk() { setflag(FLAG_HALT, 1); }
|
| 100 | 82 |
void op_rts() { cpu.rom.ptr = rspop(); }
|
| 101 | 83 |
void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
|
| 102 | 84 |
void op_drp() { wspop(); }
|
| 103 |
-void op_dup() { wspush(cpu.wst.dat[cpu.wst.ptr - 1]); }
|
|
| 85 |
+void op_dup() { wspush(wspeek()); }
|
|
| 104 | 86 |
void op_swp() { Uint8 b = wspop(), a = wspop(); wspush(b); wspush(a); }
|
| 105 | 87 |
void op_ovr() { wspush(cpu.wst.dat[cpu.wst.ptr - 2]); }
|
| 106 | 88 |
void op_rot() { Uint8 c = wspop(),b = wspop(),a = wspop(); wspush(b); wspush(c); wspush(a); }
|
| ... | ... |
@@ -108,10 +90,10 @@ void op_jmi() { cpu.rom.ptr = wspop(); }
|
| 108 | 90 |
void op_jsi() { rspush(cpu.rom.ptr); cpu.rom.ptr = wspop(); }
|
| 109 | 91 |
void op_jmz() { Uint8 a = wspop(); if(getflag(FLAG_ZERO)){ cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
|
| 110 | 92 |
void op_jsz() { Uint8 a = wspop(); if(getflag(FLAG_ZERO)){ rspush(cpu.rom.ptr); cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
|
| 111 |
-void op_equ() { Uint8 a = wspop(); Uint8 b = wspop(); setflag(FLAG_ZERO, a == b); wspush(b); }
|
|
| 112 |
-void op_neq() { Uint8 a = wspop(); Uint8 b = wspop(); setflag(FLAG_ZERO, a != b); wspush(b); }
|
|
| 113 |
-void op_lth() { setflag(FLAG_ZERO, wspop() < cpu.wst.dat[cpu.wst.ptr]); }
|
|
| 114 |
-void op_gth() { setflag(FLAG_ZERO, wspop() > cpu.wst.dat[cpu.wst.ptr]); }
|
|
| 93 |
+void op_equ() { setflag(FLAG_ZERO, wspop() == wspeek()); }
|
|
| 94 |
+void op_neq() { setflag(FLAG_ZERO, wspop() != wspeek()); }
|
|
| 95 |
+void op_gth() { setflag(FLAG_ZERO, wspop() < wspeek()); }
|
|
| 96 |
+void op_lth() { setflag(FLAG_ZERO, wspop() > wspeek()); }
|
|
| 115 | 97 |
void op_and() { wspush(wspop() & wspop()); }
|
| 116 | 98 |
void op_ora() { wspush(wspop() | wspop()); }
|
| 117 | 99 |
void op_rol() { wspush(wspop() << 1); }
|
| ... | ... |
@@ -127,10 +109,10 @@ void (*ops[])(void) = {
|
| 127 | 109 |
op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div}; |
| 128 | 110 |
|
| 129 | 111 |
Uint8 opr[][2] = {
|
| 130 |
- {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
|
|
| 131 |
- {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
|
|
| 112 |
+ {0,0}, {0,0}, {0,0}, {1,0}, {0,1}, {1,1}, {0,1}, {3,3},
|
|
| 113 |
+ {1,0}, {1,0}, {1,0}, {1,0}, {1,0}, {1,0}, {1,0}, {1,0},
|
|
| 132 | 114 |
{1,0}, {1,0}, {1,0}, {1,0}, {2,1}, {0,0}, {0,0}, {0,0},
|
| 133 |
- {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
|
|
| 115 |
+ {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}
|
|
| 134 | 116 |
}; |
| 135 | 117 |
|
| 136 | 118 |
/* clang-format on */ |
| ... | ... |
@@ -33,7 +33,7 @@ Label labels[256]; |
| 33 | 33 |
|
| 34 | 34 |
char opcodes[][4] = {
|
| 35 | 35 |
"BRK", "RTS", "LIT", "POP", "DUP", "SWP", "OVR", "ROT", |
| 36 |
- "JMI", "JSI", "JMZ", "JSZ", "EQU", "NEQ", "LTH", "GTH", |
|
| 36 |
+ "JMI", "JSI", "JMZ", "JSZ", "EQU", "NEQ", "GTH", "LTH", |
|
| 37 | 37 |
"AND", "ORA", "ROL", "ROR", "ADD", "SUB", "MUL", "DIV"}; |
| 38 | 38 |
|
| 39 | 39 |
/* clang-format on */ |