CPU Cleanup

This commit is contained in:
neauoire 2021-02-08 11:44:37 -08:00
parent c2d0759588
commit 2774e1706d
11 changed files with 83 additions and 124 deletions

View File

@ -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 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 # run
./uxnasm examples/condjump.usm boot.rom ./uxnasm examples/test.usm boot.rom
./uxn boot.rom ./uxn boot.rom

View File

@ -1,6 +0,0 @@
< arithmetic >
0203 LTH .true JMC
:false ee BRK
:true ff BRK

View File

@ -26,8 +26,6 @@
,1234 ,1233 GTH^ #0e STR ,1234 ,1233 GTH^ #0e STR
,1234 ,1235 LTH^ #0f STR ,1234 ,1235 LTH^ #0f STR
,ef ,ee EQU ,12 ,01 ROT ADD?
|c000 @FRAME BRK |c000 @FRAME BRK
|d000 @ERROR BRK |d000 @ERROR BRK
|FFFA .RESET .FRAME .ERROR |FFFA .RESET .FRAME .ERROR

View File

@ -1,30 +1,10 @@
( blank project ) ( blank )
;variable1 ;iterator
:constant1 9abc :dev1r FFF0
:dev1w FFF1
|0100 ( -------------------------------- ) |0100 @RESET BRK
|c000 @FRAME BRK
@RESET |d000 @ERROR BRK
|FFFA .RESET .FRAME .ERROR
,abcd
BRK ( RESET-END )
|c000 ( -------------------------------- )
@FRAME ( FRAME-START )
,abcd
BRK ( FRAME-END )
|d000 ( -------------------------------- )
@ERROR BRK
|FFFA ( -------------------------------- )
.RESET
.FRAME
.ERROR

View File

@ -1,7 +0,0 @@
< jump >
.end JMI BRK
:end
ff
BRK

View File

@ -1,8 +1,24 @@
< loop > ( loop )
01 .loop JSI ffff BRK |0100 @RESET
:loop ( increment value on stack )
01 ADD
0f NEQ .loop JMC ,00
RTS
@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

View File

@ -1,2 +0,0 @@
< subcond >

View File

@ -1,9 +0,0 @@
< subroutines >
.part1 JSI 44 BRK
:part1 11 .part2 JSI RTS
:part2 22 .part3 JSI RTS
:part3 33 RTS

View File

@ -1,4 +1,4 @@
( hello world ) ( my default test file )
;iterator ;iterator
:dev1r FFF0 :dev1r FFF0
@ -6,6 +6,8 @@
|0100 @RESET |0100 @RESET
,01 ,02 ADD
|c000 @FRAME BRK |c000 @FRAME BRK
|d000 @ERROR BRK |d000 @ERROR BRK
|FFFA .RESET .FRAME .ERROR |FFFA .RESET .FRAME .ERROR

View File

@ -1,11 +0,0 @@
< vectors >
:RESET BRK
:FRAME BRK
:ERROR BRK
@FFFA < vectors >
.RESET
.FRAME
.ERROR

100
uxn.c
View File

@ -158,13 +158,13 @@ void (*ops[])() = {
op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16 op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16
}; };
Uint8 opr[][2] = { /* TODO */ Uint8 opr[][2] = {
{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}, {2,1}, {3,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {2,0}, {2,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}, {1,0}, {1,2}, {2,2}, {3,3}, {3,3}, {2,1}, {2,1}, {2,1},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {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}, {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} {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0} /* TODO */
}; };
/* clang-format on */ /* clang-format on */
@ -177,54 +177,52 @@ error(char *name, int id)
} }
int int
device1(Uint8 *read, Uint8 *write) doliteral(Uint8 instr)
{ {
printf("%c", *write); if(cpu.wst.ptr >= 255)
*write = 0; return error("Stack overflow", instr);
(void)read; wspush8(instr);
return 0; cpu.literal--;
return 1;
} }
void int
opc(Uint8 src, Uint8 *op) dodevices(void) /* experimental */
{ {
*op = src; if(cpu.ram.dat[0xfff1]) {
*op &= ~(1 << 5); printf("%c", cpu.ram.dat[0xfff1]);
*op &= ~(1 << 6); cpu.ram.dat[0xfff1] = 0x00;
*op &= ~(1 << 7); }
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 int
eval(void) eval(void)
{ {
Uint8 op, instr = cpu.ram.dat[cpu.ram.ptr++]; Uint8 instr = cpu.ram.dat[cpu.ram.ptr++];
/* when literal */ if(cpu.literal > 0)
if(cpu.literal > 0) { return doliteral(instr);
wspush8(instr); else
cpu.literal--; return doopcode(instr);
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++;
return 1; return 1;
} }
@ -236,9 +234,9 @@ load(FILE *f)
} }
void 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, cpu.counter,
getflag(FLAG_HALT) != 0, getflag(FLAG_HALT) != 0,
getflag(FLAG_SHORT) != 0, getflag(FLAG_SHORT) != 0,
@ -256,12 +254,12 @@ boot(void)
cpu.ram.ptr = cpu.vreset; cpu.ram.ptr = cpu.vreset;
setflag(FLAG_HALT, 0); setflag(FLAG_HALT, 0);
while(!(cpu.status & FLAG_HALT) && eval()) while(!(cpu.status & FLAG_HALT) && eval())
; cpu.counter++;
/*eval frame */ /*eval frame */
cpu.ram.ptr = cpu.vframe; cpu.ram.ptr = cpu.vframe;
setflag(FLAG_HALT, 0); setflag(FLAG_HALT, 0);
while(!(cpu.status & FLAG_HALT) && eval()) while(!(cpu.status & FLAG_HALT) && eval())
; cpu.counter++;
return 1; return 1;
} }
@ -280,6 +278,6 @@ main(int argc, char *argv[])
/* print result */ /* print result */
echos(&cpu.wst, 0x40, "stack"); echos(&cpu.wst, 0x40, "stack");
echom(&cpu.ram, 0x40, "ram"); echom(&cpu.ram, 0x40, "ram");
debug(); echof();
return 0; return 0;
} }