commit 2774e1706d329ecbdf5f56fa0210a46d81624974
parent c2d075958834acb9654f2fd873e62209e87828e0
Author: neauoire <aliceffekt@gmail.com>
Date: Mon, 8 Feb 2021 11:44:37 -0800
CPU Cleanup
Diffstat:
12 files changed, 89 insertions(+), 132 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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/core.usm b/examples/core.usm
@@ -1,11 +0,0 @@
-( hello world )
-
-;iterator
-:dev1r FFF0
-:dev1w FFF1
-
-|0100 @RESET
-
-|c000 @FRAME BRK
-|d000 @ERROR BRK
-|FFFA .RESET .FRAME .ERROR
diff --git a/examples/jump.usm b/examples/jump.usm
@@ -1,7 +0,0 @@
-< jump >
-
-.end JMI BRK
-
-:end
- ff
- BRK
diff --git 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
@@ -1,2 +0,0 @@
-< subcond >
-
diff --git a/examples/subroutines.usm b/examples/subroutines.usm
@@ -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/test.usm b/examples/test.usm
@@ -0,0 +1,13 @@
+( my default test file )
+
+;iterator
+:dev1r FFF0
+:dev1w FFF1
+
+|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
@@ -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
@@ -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
-eval(void)
+doopcode(Uint8 instr)
{
- 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);
+ Uint8 op = instr & 0x1f;
setflag(FLAG_SHORT, (instr >> 5) & 1);
- setflag(FLAG_SIGN, (instr >> 6) & 1); /* TODO: Implement */
+ setflag(FLAG_SIGN, (instr >> 6) & 1); /* usused */
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
+ 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])();
- /* devices: experimental */
- if(cpu.ram.dat[0xfff1])
- device1(&cpu.ram.dat[0xfff0], &cpu.ram.dat[0xfff1]);
- cpu.counter++;
+ dodevices();
+ return 1;
+}
+
+int
+eval(void)
+{
+ 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;
}