~vdupras/duskos

e1b82824ffa14ad6fcf345bd292579e062eb2cf4 — Virgil Dupras a month ago 9217305
lib/crc: rewrite in HAL

The HAL version is much more efficient than the C one. It's also a good HAL
showcase.
4 files changed, 37 insertions(+), 43 deletions(-)

M fs/doc/hal.txt
D fs/lib/crc.c
M fs/lib/crc.fs
M posix/vm.c
M fs/doc/hal.txt => fs/doc/hal.txt +27 -27
@@ 202,24 202,24 @@ branchA,   --

Instructions:

@,       op --   Read source into dest
!,       op --   Write dest to source
@!,      op --   Swap dest and source
+,       op --   *Z* Add source to dest
[@],     op --   Read indirect source into dest
[!],     op --   Write indirect source into dest
compare, op --   Compare source to dest
[+n],    n op -- *Z* Add n to source without affecting dest
addr,    op --   Store the effective address of the operand in dest

ps+,    n --   Add n to PSP
rs+,    n --   Add n to RSP
W+n,    n --   *Z* Add n to W
A+n,    n --   *Z* Add n to A
W>A,    --     Copy W to A
A>W,    --     Copy A to W
W<>A,   --     Swap W and A
-W,     --     W = -W
@,       op --      Read source into dest
!,       op --      Write dest to source
@!,      op --      Swap dest and source
+,       op --   Z  Add source to dest
[@],     op --      Read indirect source into dest
[!],     op --      Write indirect source into dest
compare, op --      Compare source to dest
[+n],    n op -- Z  Add n to source without affecting dest
addr,    op --      Store the effective address of the operand in dest

ps+,    n --      Add n to PSP
rs+,    n --      Add n to RSP
W+n,    n --   Z  Add n to W
A+n,    n --   Z  Add n to A
W>A,    --        Copy W to A
A>W,    --        Copy A to W
W<>A,   --        Swap W and A
-W,     --        W = -W

## High HAL



@@ 245,12 245,12 @@ s>=)

Instructions:

-,    op --  dest - operand
*,    op --  dest * operand
/,    op --  dest / operand
%,    op --  dest modulo operand
<<,   op --  dest lshift operand
>>,   op --  dest rshift operand
&,    op --  dest and operand
|,    op --  dest or operand
^,    op --  dest xor operand
-,    op --     dest - operand
*,    op --     dest * operand
/,    op --     dest / operand
%,    op --     dest modulo operand
<<,   op --     dest lshift operand
>>,   op --     dest rshift operand
&,    op --  Z  dest and operand
|,    op --  Z  dest or operand
^,    op --  Z  dest xor operand

D fs/lib/crc.c => fs/lib/crc.c +0 -13
@@ 1,13 0,0 @@
unsigned int crc32(unsigned int crc, int c) {
    unsigned int i, b;

    crc = crc ^ c;
    for (i=0; i<8; i++) {
        b = crc & 1;
        crc >>= 1;
        if (b) {
            crc = crc ^ $EDB88320;
        }
    }
    return crc;
}

M fs/lib/crc.fs => fs/lib/crc.fs +8 -2
@@ 1,7 1,13 @@
\ CRC implementations
?f<< /asm/hal.fs

?f<< comp/c/cc.fs
cc<< lib/crc.c
code crc32 ( crc c -- crc )
  PSP) ^,
  8 i) A>) @, PSP) A>) !, begin \ counter in PSP+0
    W>A, 1 i) >>,
    1 i) A>) &, 0 Z) branchC, $edb88320 i) ^, then
   -1 PSP) [+n], NZ) branchC, drop
  nip, exit,

\ Computes CRC32 over range "a u".
: crc32[] ( a u -- crc )

M posix/vm.c => posix/vm.c +2 -1
@@ 563,7 563,8 @@ static BinOp binops[BINOPCNT] = {
};
static void _binop() {
	byte binopidx = gpcb(); readop();
	opdset(binops[binopidx](opdget(), opsget())); }
	dword n = binops[binopidx](opdget(), opsget());
	opdset(n); vm.Z = n == 0; }
static void WBINOP() { M32B; _binop(); } // 0x48
static void WBINOP16() { M16B; _binop(); }
static void WBINOP8() { M8B; _binop(); }