From 2774e1706d329ecbdf5f56fa0210a46d81624974 Mon Sep 17 00:00:00 2001 From: neauoire Date: Mon, 8 Feb 2021 11:44:37 -0800 Subject: [PATCH] CPU Cleanup --- build.sh | 2 +- examples/arithmetic.usm | 6 -- examples/benchmark.usm | 2 - examples/blank.usm | 36 +++--------- examples/jump.usm | 7 --- examples/loop.usm | 28 +++++++-- examples/subcond.usm | 2 - examples/subroutines.usm | 9 --- examples/{core.usm => test.usm} | 4 +- examples/vectors.usm | 11 ---- uxn.c | 100 ++++++++++++++++---------------- 11 files changed, 83 insertions(+), 124 deletions(-) delete mode 100644 examples/arithmetic.usm delete mode 100644 examples/jump.usm delete mode 100644 examples/subcond.usm delete mode 100644 examples/subroutines.usm rename examples/{core.usm => test.usm} (74%) delete mode 100644 examples/vectors.usm diff --git a/build.sh b/build.sh index 7cec28b..a6813e9 100755 --- a/build.sh +++ b/build.sh @@ -14,5 +14,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr 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 # run -./uxnasm examples/condjump.usm boot.rom +./uxnasm examples/test.usm boot.rom ./uxn boot.rom diff --git a/examples/arithmetic.usm b/examples/arithmetic.usm deleted file mode 100644 index 957df5f..0000000 --- a/examples/arithmetic.usm +++ /dev/null @@ -1,6 +0,0 @@ -< arithmetic > - -0203 LTH .true JMC - -:false ee BRK -:true ff BRK diff --git a/examples/benchmark.usm b/examples/benchmark.usm index 816181a..a7bc3d9 100644 --- a/examples/benchmark.usm +++ b/examples/benchmark.usm @@ -26,8 +26,6 @@ ,1234 ,1233 GTH^ #0e STR ,1234 ,1235 LTH^ #0f STR -,ef ,ee EQU ,12 ,01 ROT ADD? - |c000 @FRAME BRK |d000 @ERROR BRK |FFFA .RESET .FRAME .ERROR diff --git a/examples/blank.usm b/examples/blank.usm index 19da348..a2cbd95 100644 --- a/examples/blank.usm +++ b/examples/blank.usm @@ -1,30 +1,10 @@ -( blank project ) +( blank ) -;variable1 -:constant1 9abc +;iterator +:dev1r FFF0 +:dev1w FFF1 -|0100 ( -------------------------------- ) - -@RESET - -,abcd - -BRK ( RESET-END ) - -|c000 ( -------------------------------- ) - -@FRAME ( FRAME-START ) - -,abcd - -BRK ( FRAME-END ) - -|d000 ( -------------------------------- ) - -@ERROR BRK - -|FFFA ( -------------------------------- ) - -.RESET -.FRAME -.ERROR +|0100 @RESET BRK +|c000 @FRAME BRK +|d000 @ERROR BRK +|FFFA .RESET .FRAME .ERROR diff --git a/examples/jump.usm b/examples/jump.usm deleted file mode 100644 index d878b9b..0000000 --- a/examples/jump.usm +++ /dev/null @@ -1,7 +0,0 @@ -< jump > - -.end JMI BRK - -:end - ff - BRK diff --git a/examples/loop.usm b/examples/loop.usm index 2ed308e..bf26317 100644 --- a/examples/loop.usm +++ b/examples/loop.usm @@ -1,8 +1,24 @@ -< loop > +( loop ) -01 .loop JSI ffff BRK +|0100 @RESET -:loop - 01 ADD - 0f NEQ .loop JMC - RTS +( increment value on stack ) + +,00 + +@loop1 + ,01 ADD DUP + ,ff NEQ ,loop1 ROT JMP? POP^ + +( increment value in memory ) + +@loop2 + #00 LDR ,01 ADD #00 STR + #00 LDR + ,ff NEQ ,loop2 ROT JMP? POP^ + +BRK + +|c000 @FRAME BRK +|d000 @ERROR BRK +|FFFA .RESET .FRAME .ERROR diff --git a/examples/subcond.usm b/examples/subcond.usm deleted file mode 100644 index eca9ed0..0000000 --- a/examples/subcond.usm +++ /dev/null @@ -1,2 +0,0 @@ -< subcond > - diff --git a/examples/subroutines.usm b/examples/subroutines.usm deleted file mode 100644 index 4a450e7..0000000 --- a/examples/subroutines.usm +++ /dev/null @@ -1,9 +0,0 @@ -< subroutines > - -.part1 JSI 44 BRK - -:part1 11 .part2 JSI RTS - -:part2 22 .part3 JSI RTS - -:part3 33 RTS \ No newline at end of file diff --git a/examples/core.usm b/examples/test.usm similarity index 74% rename from examples/core.usm rename to examples/test.usm index f446a73..0efe381 100644 --- a/examples/core.usm +++ b/examples/test.usm @@ -1,4 +1,4 @@ -( hello world ) +( my default test file ) ;iterator :dev1r FFF0 @@ -6,6 +6,8 @@ |0100 @RESET +,01 ,02 ADD + |c000 @FRAME BRK |d000 @ERROR BRK |FFFA .RESET .FRAME .ERROR diff --git a/examples/vectors.usm b/examples/vectors.usm deleted file mode 100644 index 182d2a2..0000000 --- a/examples/vectors.usm +++ /dev/null @@ -1,11 +0,0 @@ -< vectors > - -:RESET BRK -:FRAME BRK -:ERROR BRK - -@FFFA < vectors > - -.RESET -.FRAME -.ERROR \ No newline at end of file diff --git a/uxn.c b/uxn.c index 389f93b..3364bfa 100644 --- a/uxn.c +++ b/uxn.c @@ -158,13 +158,13 @@ void (*ops[])() = { op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16 }; -Uint8 opr[][2] = { /* TODO */ - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, - {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0} +Uint8 opr[][2] = { + {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {2,1}, {3,0}, + {2,0}, {2,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, + {1,0}, {1,2}, {2,2}, {3,3}, {3,3}, {2,1}, {2,1}, {2,1}, + {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, + {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, /* TODO */ + {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0} /* TODO */ }; /* clang-format on */ @@ -177,54 +177,52 @@ error(char *name, int id) } int -device1(Uint8 *read, Uint8 *write) +doliteral(Uint8 instr) { - printf("%c", *write); - *write = 0; - (void)read; - return 0; + if(cpu.wst.ptr >= 255) + return error("Stack overflow", instr); + wspush8(instr); + cpu.literal--; + return 1; } -void -opc(Uint8 src, Uint8 *op) +int +dodevices(void) /* experimental */ { - *op = src; - *op &= ~(1 << 5); - *op &= ~(1 << 6); - *op &= ~(1 << 7); + if(cpu.ram.dat[0xfff1]) { + printf("%c", cpu.ram.dat[0xfff1]); + cpu.ram.dat[0xfff1] = 0x00; + } + return 1; +} + +int +doopcode(Uint8 instr) +{ + Uint8 op = instr & 0x1f; + setflag(FLAG_SHORT, (instr >> 5) & 1); + setflag(FLAG_SIGN, (instr >> 6) & 1); /* usused */ + setflag(FLAG_COND, (instr >> 7) & 1); + if(getflag(FLAG_SHORT)) + op += 16; + if(cpu.wst.ptr < opr[op][0]) + return error("Stack underflow", op); + if(cpu.wst.ptr + opr[op][1] - opr[op][0] >= 255) + return error("Stack overflow", instr); + if(!getflag(FLAG_COND) || (getflag(FLAG_COND) && wspop8())) + (*ops[op])(); + dodevices(); + return 1; } int eval(void) { - Uint8 op, instr = cpu.ram.dat[cpu.ram.ptr++]; - /* when literal */ - if(cpu.literal > 0) { - wspush8(instr); - cpu.literal--; - return 1; - } - /* when opcode */ - opc(instr, &op); - setflag(FLAG_SHORT, (instr >> 5) & 1); - setflag(FLAG_SIGN, (instr >> 6) & 1); /* TODO: Implement */ - setflag(FLAG_COND, (instr >> 7) & 1); - /* TODO: overflow */ - if(cpu.wst.ptr < opr[op][0]) - return error("Stack underflow", op); - /* short mode */ - if(getflag(FLAG_SHORT)) - op += 16; - /* cond mode */ - if(getflag(FLAG_COND)) { - if(wspop8()) - (*ops[op])(); - } else - (*ops[op])(); - /* devices: experimental */ - if(cpu.ram.dat[0xfff1]) - device1(&cpu.ram.dat[0xfff0], &cpu.ram.dat[0xfff1]); - cpu.counter++; + Uint8 instr = cpu.ram.dat[cpu.ram.ptr++]; + if(cpu.literal > 0) + return doliteral(instr); + else + return doopcode(instr); return 1; } @@ -236,9 +234,9 @@ load(FILE *f) } void -debug(void) +echof(void) { - printf("ended @ %d steps | hf: %x sf: %x cf: %x tf: %x\n", + printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n", cpu.counter, getflag(FLAG_HALT) != 0, getflag(FLAG_SHORT) != 0, @@ -256,12 +254,12 @@ boot(void) cpu.ram.ptr = cpu.vreset; setflag(FLAG_HALT, 0); while(!(cpu.status & FLAG_HALT) && eval()) - ; + cpu.counter++; /*eval frame */ cpu.ram.ptr = cpu.vframe; setflag(FLAG_HALT, 0); while(!(cpu.status & FLAG_HALT) && eval()) - ; + cpu.counter++; return 1; } @@ -280,6 +278,6 @@ main(int argc, char *argv[]) /* print result */ echos(&cpu.wst, 0x40, "stack"); echom(&cpu.ram, 0x40, "ram"); - debug(); + echof(); return 0; }