Progress on branching

This commit is contained in:
neauoire 2021-02-01 11:58:47 -08:00
parent 1482e5662d
commit 2782586f78
8 changed files with 61 additions and 35 deletions

View File

@ -34,6 +34,17 @@ $01 < pointer8 >
:label ADD RTS
```
## Mission
- Loop
- Conditional example
- Pointers/Literals
- Print word to stdout
- Draw pixel to screen
- Detect mouse click
- 16 bits addressing
- jumping to subroutine should be relative
## TODOs
- Implement addressing
@ -44,6 +55,7 @@ $01 < pointer8 >
- Audo-detect literals length.
- SDL Layer Emulator
- Build PPU
- Interrupts
## Refs

View File

@ -13,12 +13,6 @@ rm -f ./boot.rom
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 uxnasm.c -o uxnasm
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
# build(fast)
# cc uxn.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -o uxn
# Size
# echo "Size: $(du -sk ./uxn)"
# run
./uxnasm example.usm boot.rom
./uxnasm examples/cond.usm boot.rom
./uxn boot.rom

16
examples/cond.usm Normal file
View File

@ -0,0 +1,16 @@
< conditionals >
+03 +02 ADD
+05 EQU
.there JMQ
:here
< when not equal >
+ee
BRK
:there
< when is equal >
+ff
BRK

3
examples/core.usm Normal file
View File

@ -0,0 +1,3 @@
< core >
+12 -34

7
examples/jump.usm Normal file
View File

@ -0,0 +1,7 @@
< conditionals >
.end JMP
:end
+ff
BRK

12
uxn.c
View File

@ -91,8 +91,8 @@ rspop(void)
/* clang-format off */
void op_brk() { setflag(FLAG_HALT, 1); }
void op_lit() { cpu.literal += cpu.memory[cpu.mptr++]; }
void op_nop() { }
void op_rts() { cpu.mptr = rspop(); }
void op_lit() { cpu.literal += 1;}
void op_drp() { spop(); }
void op_dup() { spush(cpu.stack[cpu.sptr - 1]); }
void op_swp() { Uint8 b = spop(), a = spop(); spush(b); spush(a); }
@ -100,8 +100,8 @@ void op_ovr() { spush(cpu.stack[cpu.sptr - 2]); }
void op_rot() { Uint8 c = spop(),b = spop(),a = spop(); spush(b); spush(c); spush(a); }
void op_jmp() { cpu.mptr = spop(); }
void op_jsr() { rspush(cpu.mptr); cpu.mptr = spop(); }
void op_jeq() { if(getflag(FLAG_ZERO)) cpu.mptr = spop(); }
void op_rts() { cpu.mptr = rspop(); }
void op_jmq() { if(getflag(FLAG_ZERO)) op_jmp(); }
void op_jsq() { if(getflag(FLAG_ZERO)) op_jsr(); }
void op_equ() { setflag(FLAG_ZERO, spop() == spop()); }
void op_neq() { setflag(FLAG_ZERO, spop() != spop()); }
void op_lth() { setflag(FLAG_ZERO, spop() < spop()); }
@ -116,8 +116,8 @@ void op_mul() { spush(spop() * spop()); }
void op_div() { spush(spop() / spop()); }
void (*ops[])(void) = {
op_brk, op_lit, op_nop, op_drp, op_dup, op_swp, op_ovr, op_rot,
op_jmp, op_jsr, op_jeq, op_rts, op_equ, op_neq, op_gth, op_lth,
op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot,
op_jmp, op_jsr, op_jmq, op_jsq, op_equ, op_neq, op_gth, op_lth,
op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div};
/* clang-format on */

View File

@ -31,8 +31,8 @@ Label labels[256];
char opcodes[][4] = {
"BRK",
"RTS",
"LIT",
"---",
"POP",
"DUP",
"SWP",
@ -41,8 +41,8 @@ char opcodes[][4] = {
/* */
"JMP",
"JSR",
"JEQ",
"RTS",
"JMQ",
"JSQ",
"EQU",
"NEQ",
"LTH",
@ -164,11 +164,10 @@ int
getlength(char *w)
{
if(findop(w) || scmp(w, "BRK")) return 1;
if(w[0] == '.') return 3;
if(w[0] == '.') return 2;
if(w[0] == ':') return 0;
if(w[0] == '[') return 2;
if(sihx(w)) return 1;
if(w[0] == ']') return 0;
if(w[0] == '+') return 2;
if(w[0] == '-') return 2;
printf("Unknown length %s\n", w);
return 0;
}
@ -205,28 +204,23 @@ pass2(FILE *f)
int skip = 0;
char word[64];
while(fscanf(f, "%s", word) == 1) {
Uint8 op;
Uint8 op = 0;
Label *l;
if(word[0] == ':') continue;
suca(word);
if(comment(word, &skip)) continue;
if(word[0] == ']') continue;
if(word[0] == '+') {
addprg(0x01);
addprg(1);
/* literals */
if(word[0] == '+' || word[0] == '-')
addprg(0x02);
if(word[0] == '+')
addprg(shex(word + 1));
} else if(word[0] == '[') {
addprg(0x01);
addprg(shex(word + 1));
} else if((op = findop(word)))
else if(word[0] == '-')
addprg((Uint8)(-1 * shex(word + 1)));
/* opcodes */
else if((op = findop(word)) || scmp(word, "BRK"))
addprg(op);
else if(sihx(word))
addprg(shex(word));
else if(scmp(word, "BRK"))
addprg(0x00);
else if((l = findlabel(word + 1))) {
addprg(0x01);
addprg(1);
addprg(0x02);
addprg(l->addr);
} else
printf("unknown: %s\n", word);