b16.asm 2.34 KB
Newer Older
bp's avatar
bp committed
1 2
#! /usr/bin/gforth b16-asm.fs
: reset			\ label reset
bp's avatar
bp committed
3
   ' boot jmp		\ jump forward to boot
bp's avatar
bp committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
: stack			\ stack test
    1 #c 2 #c 3 #	\ fill stack with 1 2 3 4 5 6 7 8 9 A B
    4 # 5 #c 6 #c
    7 #c 8 #c 9 #
    $A # $B # nop
    drop drop drop	\ drop values from stack
    drop drop drop
    drop drop drop
    drop drop
;			\ return to caller
: alu			\ test ALU
    $1234 # $5678 # +	\ add two literals
    $1234 # com +c	\ complement and add with carry = subtract
    $1234 # $5678 # or	\ OR two literals
    $1234 # $5678 # and	\ AND two literals
    xor drop drop	\ XOR the two results from above, and drop results
;			\ return to caller
: mul ( u1 u2 -- ud )	\ unsigned expanding multiplication
    >A			\ move multiplicant to register A
    0 # dup +		\ put zero on top of stack and clear carry flag
    *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+ *+
    			\ 17 mul-step instructions
    >r drop a r>	\ drop second multiplicant, reorder results
;			\ return to caller
: div ( ud udiv -- uqout umod )	\ unsigned division with remainder
    com			\ invert divider
    >r >r >a r> r>	\ move low part of divident to A
    over 0 # +		\ copy low part of divider to top, clear carry
    /-  /- /- /- /-  /- /- /- /-  /- /- /- /-  /- /- /- /-
    			\ 17 div-step instructions
    nip nip a >r	\ reorder results
    -cIF		\ branch if carry set
        *+ r>		\ divide correction if carry was clear
    ;			\ return to caller
    THEN		\ resolve -cIF branch
    0 # +		\ clear carry
    *+			\ mul-step divides by two
    $8000 # + r>	\ insert carry
;			\ return to caller
: jumps			\ test a few jumps
    0 #c 1 #c    -IF  drop 3 # + THEN  drop \ jump if non-zero
    0 #c 0 #c     IF  drop 4 # + THEN  drop \ jump if zero
    0 # com     -cIF  drop 5 # + THEN  drop \ jump if carry
    0 #c 0 #c +  cIF  drop 6 # + THEN  drop \ jump if no carry
;			\ return to caller
: -jumps		\ test a few jumps the other way round
    0 #c 1 #c     IF  drop 3 # + THEN
    0 #c 0 #c    -IF  drop 4 # + THEN
    0 # com      cIF  drop 5 # + THEN
    0 #c 0 #c + -cIF  drop 6 # + THEN
;			\ return to caller
bp's avatar
bp committed
55
: boot			\ resolve forward jump from reset
bp's avatar
bp committed
56 57 58 59 60
    stack		\ call stack test
    alu			\ call ALU test
    $1234 # $5678 # mul	\ multiply 1234 with 5678 (hex)
    $789a # div drop drop \ divide by 789A, drop results
    jumps -jumps	\ call jump tests
bp's avatar
bp committed
61
;			\ endless loop
bp's avatar
bp committed
62
.end			\ end of test program
bp's avatar
bp committed
63
.hex