~vdupras/duskos

bf3a586ffb37f93776a2b024b2b5e90ac5d31530 — Virgil Dupras 1 year, 4 months ago e5726d6
hal: move all condition flags to Low HAL

This allows us to remove the kernel < word and make basic comparison words much
faster.

We also remove C) and NC) which aren't used at the moment and have ambiguous
meaning if used with compare, because under ARM, the C flag is inverted.

These flags would have a correct meaning if they were used with shift
instructions, but semantics for this haven't been developed yet, so for now I'm
just removing them.
5 files changed, 32 insertions(+), 50 deletions(-)

M fs/asm/i386.fs
M fs/doc/hal.txt
M fs/xcomp/bootlo.fs
M fs/xcomp/i386/kernel.fs
M posix/vm.c
M fs/asm/i386.fs => fs/asm/i386.fs +0 -4
@@ 315,7 315,3 @@ op <<, shl,        op >>, shr,
: dup, -4 ps+, PSP) !, ;
: nip, 4 ps+, ;
: drop, PSP) @, nip, ;

$2 const C)        $3 const NC)        $4 const Z)         $5 const NZ)
$2 const <)        $3 const >=)        $6 const <=)        $7 const >)
$c const s<)       $d const s>=)       $e const s<=)       $f const s>)

M fs/doc/hal.txt => fs/doc/hal.txt +9 -14
@@ 229,6 229,14 @@ Branching and conditions:

Z)
NZ)
<)
<=)
>)
>=)
s<)   Signed comparison
s<=)
s>)
s>=)

W=0>Z,     --
  Sets Z according to whether W is zero.


@@ 262,7 270,7 @@ Instructions:
+,       op --   Z  Add source to dest
[@],     op --      Read indirect source into dest
[!],     op --      Write indirect source into dest
compare, op --      Compare source to dest
compare, op --   A  Compare source to dest (all flags set)
[+n],    n op -- Z  Add n to source without affecting dest
addr,    op --      Store the effective address of the operand in dest



@@ 285,19 293,6 @@ A>)   A register is the destination instead of W
<>)   Direction of the operation is inverted (see above)
&)    Reference to operand (see above)

Branching and conditions:

C)    Carry flag
NC)
<)
<=)
>)
>=)
s<)   Signed comparison
s<=)
s>)
s>=)

Instructions:

-,    op --     dest - operand

M fs/xcomp/bootlo.fs => fs/xcomp/bootlo.fs +3 -5
@@ 86,7 86,9 @@ code ?dup W=0>Z, 0 Z) branchC, dup, then exit,
: begin HERE @ ; immediate
: again branch, drop ; immediate
: until W>A, drop, A=0>Z, Z) branchC, drop ; immediate
: = - not ;
: _ code PSP) compare, C>W, nip, exit, ;
Z) _ =   NZ) _ <>  >) _ <    <) _ >    >=) _ <=  <=) _ >=

: \ begin in< $0a = until ; immediate
\ hello, this is a comment!
: exit exit, ; immediate


@@ 97,12 99,8 @@ code ?dup W=0>Z, 0 Z) branchC, dup, then exit,
( hello, another comment! )

\ Arithmetic
: > swap < ;
: 0>= $80000000 < ;
: 0< 0>= not ;
: >= < not ;
: <= > not ;
: <> = not ;
: / /mod nip ;
: mod /mod drop ;
: ?swap ( n n -- l h ) 2dup > if swap then ;

M fs/xcomp/i386/kernel.fs => fs/xcomp/i386/kernel.fs +2 -7
@@ 114,6 114,8 @@ $106 xconst PSP)
$104 xconst RSP)
$4 xconst Z)
$5 xconst NZ)
$2 xconst <)       $3 xconst >=)       $6 xconst <=)       $7 xconst >)
$c xconst s<)      $d xconst s>=)      $e xconst s<=)      $f xconst s>)

pc to lblhbank 0 ,
xcode m) ( a -- operand )


@@ 356,13 358,6 @@ xcode xor
  ax si 0 d) xor,
  xnip, ret,

xcode <
  bx ax mov,
  ax ax xor,
  si 0 d) bx cmp,
  al setc,
  xnip, ret,

xcode pc@ ( port -- n8 )
  dx ax mov,
  ax ax xor,

M posix/vm.c => posix/vm.c +18 -20
@@ 69,14 69,14 @@ no assembler to complete the HAL to "full" level later. It's all in there.
// Condition structure: b6:0 condition ID b7 invert
#define CONDZ 0x00
#define CONDNZ 0x80
#define CONDC 0x01
#define CONDNC 0x81
#define CONDA 0x02 // above (unsigned gt)
#define CONDNA 0x82
#define CONDLT 0x03 // signed
#define CONDNLT 0x83
#define CONDGT 0x04 // signed
#define CONDNGT 0x84
#define CONDLT 0x01 // unsigned lesser than
#define CONDNLT 0x81
#define CONDGT 0x02
#define CONDNGT 0x82
#define CONDSLT 0x03 // signed
#define CONDNSLT 0x83
#define CONDSGT 0x04 // signed
#define CONSNSGT 0x84
#define EMETA_8B 0x10
#define EMETA_16B 0x11



@@ 276,10 276,10 @@ static int checkcond(byte cond) {
	int r = 0;
	switch (cond&0x7f) {
		case CONDZ: r = vm.Z; break;
		case CONDC: r = vm.C; break;
		case CONDA: r = !vm.C && !vm.Z; break;
		case CONDLT: r = vm.SC; break;
		case CONDGT: r = !vm.SC && !vm.Z; break;
		case CONDLT: r = vm.C; break;
		case CONDGT: r = !vm.C && !vm.Z; break;
		case CONDSLT: r = vm.SC; break;
		case CONDSGT: r = !vm.SC && !vm.Z; break;
	}
	if (cond&0x80) r = !r;
	return r;


@@ 576,7 576,6 @@ static void WBINOP() { M32B; _binop(); } // 0x48
static void WBINOP16() { M16B; _binop(); }
static void WBINOP8() { M8B; _binop(); }
static void DIVMOD() { dword b = vm.W; dword a = pnip(); vm.W = a % b; ppush(a / b); }
static void LT() { vm.W = pnip() < vm.W; }
static void NEG() { vm.W = -vm.W; }

static void BYE() { vm.PC = MEMSZ; } // 0x50


@@ 900,7 899,7 @@ static void (*ops[OPCNT])() = {
	MAYBEWORD, WORD, PARSE, FIND, WNF, FINDMOD, NULL, NULL,
	STACKCHK, COMPWORD, RUNWORD, COMPILING, STARTCOMP, STOPCOMP, RSADDWR, COMPOP,
	ALIGN4, ENTRY, CODE, CODE16, CODE8, COMPBINOP, NULL, NULL,
	WBINOP, WBINOP16, WBINOP8, DIVMOD, NULL, LT, NEG, NULL,
	WBINOP, WBINOP16, WBINOP8, DIVMOD, NULL, NULL, NEG, NULL,
	BYE, BYEFAIL, QUIT, ABORT_, DBG, USLEEP, MPROTECT, NULL,
	NULL, NULL, NULL, NULL, NULL, WCHECKZ, STOREC, ACHECKZ,
	FCHILD, FOPEN, FREADBUF, FCLOSE, FINFO, FITER, NULL, FSEEK,


@@ 943,7 942,7 @@ static char *opnames[OPCNT-0x28] = {
	"maybeword", "word", "parse", "find", "(wnf)", "findmod", NULL, NULL,
	"stack?", "compword", "runword", "compiling", "]", NULL, "rs+,", NULL,
	"align4", "entry", "code", "code16b", "code8b", NULL, NULL, NULL,
	NULL, NULL, NULL, "/mod", NULL, "<", NULL, NULL,
	NULL, NULL, NULL, "/mod", NULL, NULL, NULL, NULL,
	"bye", "byefail", "quit", "(abort)", "dbg", "_usleep", "mprotect", NULL,
	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
	"_fchild", "_fopen", "_freadbuf", "_fclose", "_finfo", "_fiter", NULL, "_fseek",


@@ 972,11 971,10 @@ static void buildsysdict() {
	sysconst("W)", OPW); sysconst("A)", OPA);
	sysconst("PSP)", OPPSP); sysconst("RSP)", OPRSP);
	sysconst("Z)", CONDZ); sysconst("NZ)", CONDNZ);
	sysconst("C)", CONDC); sysconst("NC)", CONDNC);
	sysconst("<)", CONDC); sysconst(">=)", CONDNC);
	sysconst(">)", CONDA); sysconst("<=)", CONDNA);
	sysconst("s<)", CONDLT); sysconst("s>=)", CONDNLT);
	sysconst("s>)", CONDGT); sysconst("s<=)", CONDNGT);
	sysconst("<)", CONDLT); sysconst(">=)", CONDNLT);
	sysconst(">)", CONDGT); sysconst("<=)", CONDNGT);
	sysconst("s<)", CONDSLT); sysconst("s>=)", CONDNSLT);
	sysconst("s>)", CONDSGT); sysconst("s<=)", CONSNSGT);
	for (int i=0; i<OPCNT-0x28; i++) {
		if (opnames[i]) wentry(opnames[i], i+0x28);
	}