some more refactoring
This commit is contained in:
parent
497f297877
commit
7253569f7e
91
math32.tal
91
math32.tal
|
@ -19,27 +19,30 @@
|
||||||
( )
|
( )
|
||||||
( Operations supported: )
|
( Operations supported: )
|
||||||
( )
|
( )
|
||||||
( - add32 x** y** -> z** [x + y] )
|
( NAME STACK EFFECT DEFINITION )
|
||||||
( - sub32 x** y** -> z** [x - y] )
|
( add32 x** y** -> z** x + y )
|
||||||
( - mul16 x* y* -> z** [x * y] )
|
( sub32 x** y** -> z** x - y )
|
||||||
( - mul32 x** y** -> z** [x * y] )
|
( mul16 x* y* -> z** x * y )
|
||||||
( - div32 x** y** -> z** [x / y] )
|
( mul32 x** y** -> z** x * y )
|
||||||
( - negate32 x** -> z** [-x] )
|
( div32 x** y** -> z** x / y )
|
||||||
( - left-shift x** n^ -> z** [x<<n] )
|
( negate32 x** -> z** -x )
|
||||||
( - right-shift x** n^ -> z** [x>>n] )
|
( lshift32 x** n^ -> z** x<<n )
|
||||||
( - and32 x** y** -> z** [x & y] )
|
( rshift32 x** n^ -> z** x>>n )
|
||||||
( - or32 x** y** -> z** [x | y] )
|
( and32 x** y** -> z** x & y )
|
||||||
( - xor32 x** y** -> z** [x ^ y] )
|
( or32 x** y** -> z** x | y )
|
||||||
( - complement32 x** -> z** [~x] )
|
( xor32 x** y** -> z** x ^ y )
|
||||||
( - eq32 x** y** -> bool^ [x == y] )
|
( complement32 x** -> z** ~x )
|
||||||
( - ne32 x** y** -> bool^ [x != y] )
|
( eq32 x** y** -> bool^ x == y )
|
||||||
( - is-zero32 x** -> bool^ [x == 0] )
|
( ne32 x** y** -> bool^ x != y )
|
||||||
( - non-zero32 x** -> bool^ [x != 0] )
|
( is-zero32 x** -> bool^ x == 0 )
|
||||||
( - lt32 x** y** -> bool^ [x < y] )
|
( non-zero32 x** -> bool^ x != 0 )
|
||||||
( - gt32 x** y** -> bool^ [x > y] )
|
( lt32 x** y** -> bool^ x < y )
|
||||||
( - lteq32 x** y** -> bool^ [x <= y] )
|
( gt32 x** y** -> bool^ x > y )
|
||||||
( - gteq32 x** y** -> bool^ [x >= y] )
|
( lteq32 x** y** -> bool^ x <= y )
|
||||||
( - bitcount x** -> bool^ log2+1 )
|
( gteq32 x** y** -> bool^ x >= y )
|
||||||
|
( bitcount8 x^ -> bool^ floor(log2(x))+1 )
|
||||||
|
( bitcount16 x* -> bool^ floor(log2(x))+1 )
|
||||||
|
( bitcount32 x** -> bool^ floor(log2(x))+1 )
|
||||||
( )
|
( )
|
||||||
( In addition to the code this file uses 44 bytes of registers )
|
( In addition to the code this file uses 44 bytes of registers )
|
||||||
( to store temporary state: )
|
( to store temporary state: )
|
||||||
|
@ -51,7 +54,7 @@
|
||||||
%DEBUG { #ff #0e DEO }
|
%DEBUG { #ff #0e DEO }
|
||||||
%RTN { JMP2r }
|
%RTN { JMP2r }
|
||||||
%TOR { ROT ROT } ( a b c -> c a b )
|
%TOR { ROT ROT } ( a b c -> c a b )
|
||||||
%TOR2 { ROT2 ROT2 }
|
( %TOR2 { ROT2 ROT2 } )
|
||||||
%COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 }
|
%COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 }
|
||||||
|
|
||||||
( bitcount: number of bits needed to represent number )
|
( bitcount: number of bits needed to represent number )
|
||||||
|
@ -172,15 +175,15 @@
|
||||||
( bit shifting )
|
( bit shifting )
|
||||||
|
|
||||||
( x >> n )
|
( x >> n )
|
||||||
@right-shift ( x** n^ -> x<<n )
|
@rshift32 ( x** n^ -> x<<n )
|
||||||
DUP #08 LTH ;right-shift0 JCN2 ( x n )
|
DUP #08 LTH ;rshift32-0 JCN2 ( x n )
|
||||||
DUP #10 LTH ;right-shift1 JCN2 ( x n )
|
DUP #10 LTH ;rshift32-1 JCN2 ( x n )
|
||||||
DUP #18 LTH ;right-shift2 JCN2 ( x n )
|
DUP #18 LTH ;rshift32-2 JCN2 ( x n )
|
||||||
;right-shift3 JMP2 ( x n )
|
;rshift32-3 JMP2 ( x n )
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift right by 0-7 bits )
|
( shift right by 0-7 bits )
|
||||||
@right-shift0 ( x** n^ -> x<<n )
|
@rshift32-0 ( x** n^ -> x<<n )
|
||||||
STHk SFT ;sh/z3 STA ( write z3 )
|
STHk SFT ;sh/z3 STA ( write z3 )
|
||||||
#00 STHkr SFT2 #00 ;sh/z3 LDA ORA2 ;sh/z2 STA2 ( write z2,z3 )
|
#00 STHkr SFT2 #00 ;sh/z3 LDA ORA2 ;sh/z2 STA2 ( write z2,z3 )
|
||||||
#00 STHkr SFT2 #00 ;sh/z2 LDA ORA2 ;sh/z1 STA2 ( write z1,z2 )
|
#00 STHkr SFT2 #00 ;sh/z2 LDA ORA2 ;sh/z1 STA2 ( write z1,z2 )
|
||||||
|
@ -189,7 +192,7 @@
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift right by 8-15 bits )
|
( shift right by 8-15 bits )
|
||||||
@right-shift1 ( x** n^ -> x<<n )
|
@rshift32-1 ( x** n^ -> x<<n )
|
||||||
#08 SUB STH POP
|
#08 SUB STH POP
|
||||||
STHkr SFT ;sh/z3 STA ( write z3 )
|
STHkr SFT ;sh/z3 STA ( write z3 )
|
||||||
#00 STHkr SFT2 #00 ;sh/z3 LDA ORA2 ;sh/z2 STA2 ( write z2,z3 )
|
#00 STHkr SFT2 #00 ;sh/z3 LDA ORA2 ;sh/z2 STA2 ( write z2,z3 )
|
||||||
|
@ -198,7 +201,7 @@
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift right by 16-23 bits )
|
( shift right by 16-23 bits )
|
||||||
@right-shift2 ( x** n^ -> x<<n )
|
@rshift32-2 ( x** n^ -> x<<n )
|
||||||
#10 SUB STH POP2
|
#10 SUB STH POP2
|
||||||
STHkr SFT ;sh/z3 STA ( write z3 )
|
STHkr SFT ;sh/z3 STA ( write z3 )
|
||||||
#00 STHr SFT2 #00 ;sh/z3 LDA ORA2 ( compute z2,z3 )
|
#00 STHr SFT2 #00 ;sh/z3 LDA ORA2 ( compute z2,z3 )
|
||||||
|
@ -206,22 +209,22 @@
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift right by 16-23 bits )
|
( shift right by 16-23 bits )
|
||||||
@right-shift3 ( x** n^ -> x<<n )
|
@rshift32-3 ( x** n^ -> x<<n )
|
||||||
#18 SUB STH POP2 POP ( x0 )
|
#18 SUB STH POP2 POP ( x0 )
|
||||||
#00 SWP #0000 SWP2 ( 00 00 00 x0 )
|
#00 SWP #0000 SWP2 ( 00 00 00 x0 )
|
||||||
STHr SFT
|
STHr SFT
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( x << n )
|
( x << n )
|
||||||
@left-shift ( x** n^ -> x<<n )
|
@lshift32 ( x** n^ -> x<<n )
|
||||||
DUP #08 LTH ;left-shift0 JCN2 ( x n )
|
DUP #08 LTH ;lshift32-0 JCN2 ( x n )
|
||||||
DUP #10 LTH ;left-shift1 JCN2 ( x n )
|
DUP #10 LTH ;lshift32-1 JCN2 ( x n )
|
||||||
DUP #18 LTH ;left-shift2 JCN2 ( x n )
|
DUP #18 LTH ;lshift32-2 JCN2 ( x n )
|
||||||
;left-shift3 JMP2 ( x n )
|
;lshift32-3 JMP2 ( x n )
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift left by 0-7 bits )
|
( shift left by 0-7 bits )
|
||||||
@left-shift0 ( x** n^ -> x<<n )
|
@lshift32-0 ( x** n^ -> x<<n )
|
||||||
#40 SFT STH ( stash n<<4 )
|
#40 SFT STH ( stash n<<4 )
|
||||||
#00 SWP STHkr SFT2 ;sh/z2 STA2 ( store z2,z3 )
|
#00 SWP STHkr SFT2 ;sh/z2 STA2 ( store z2,z3 )
|
||||||
#00 SWP STHkr SFT2 #00 ;sh/z2 LDA ORA2 ;sh/z1 STA2 ( store z1,z2 )
|
#00 SWP STHkr SFT2 #00 ;sh/z2 LDA ORA2 ;sh/z1 STA2 ( store z1,z2 )
|
||||||
|
@ -231,7 +234,7 @@
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift left by 8-15 bits )
|
( shift left by 8-15 bits )
|
||||||
@left-shift1 ( x** n^ -> x<<n )
|
@lshift32-1 ( x** n^ -> x<<n )
|
||||||
#08 SUB #40 SFT STH ( stash [n-8]<<4 )
|
#08 SUB #40 SFT STH ( stash [n-8]<<4 )
|
||||||
#00 SWP STHkr SFT2 ;sh/z1 STA2 ( store z1,z2 )
|
#00 SWP STHkr SFT2 ;sh/z1 STA2 ( store z1,z2 )
|
||||||
#00 SWP STHkr SFT2 #00 ;sh/z1 LDA ORA2 ;sh/z0 STA2 ( store z0,z1 )
|
#00 SWP STHkr SFT2 #00 ;sh/z1 LDA ORA2 ;sh/z0 STA2 ( store z0,z1 )
|
||||||
|
@ -241,7 +244,7 @@
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift left by 16-23 bits )
|
( shift left by 16-23 bits )
|
||||||
@left-shift2 ( x** n^ -> x<<n )
|
@lshift32-2 ( x** n^ -> x<<n )
|
||||||
#10 SUB #40 SFT STH ( stash [n-16]<<4 )
|
#10 SUB #40 SFT STH ( stash [n-16]<<4 )
|
||||||
#00 SWP STHkr SFT2 ;sh/z0 STA2 ( store z0,z1 )
|
#00 SWP STHkr SFT2 ;sh/z0 STA2 ( store z0,z1 )
|
||||||
STHr SFT ;sh/z0 LDA ORA ( calculate z0 )
|
STHr SFT ;sh/z0 LDA ORA ( calculate z0 )
|
||||||
|
@ -250,7 +253,7 @@
|
||||||
RTN
|
RTN
|
||||||
|
|
||||||
( shift left by 24-31 bits )
|
( shift left by 24-31 bits )
|
||||||
@left-shift3 ( x** n^ -> x<<n )
|
@lshift32-3 ( x** n^ -> x<<n )
|
||||||
#18 SUB #40 SFT ( x0 x1 x2 x3 r=[n-24]<<4 )
|
#18 SUB #40 SFT ( x0 x1 x2 x3 r=[n-24]<<4 )
|
||||||
SFT ( x0 x1 x2 x3<<r )
|
SFT ( x0 x1 x2 x3<<r )
|
||||||
SWP2 POP2 SWP POP #0000 #00
|
SWP2 POP2 SWP POP #0000 #00
|
||||||
|
@ -364,11 +367,11 @@
|
||||||
|
|
||||||
( 1<<shift -> cur )
|
( 1<<shift -> cur )
|
||||||
#0000 #0001 ROT2 POP
|
#0000 #0001 ROT2 POP
|
||||||
;left-shift JSR2 ;div32/cur1 STA2 ;div32/cur0 STA2
|
;lshift32 JSR2 ;div32/cur1 STA2 ;div32/cur0 STA2
|
||||||
|
|
||||||
( div<<shift -> div )
|
( div<<shift -> div )
|
||||||
;div32/div0 LDA2 ;div32/div1 LDA2 ROT2 POP
|
;div32/div0 LDA2 ;div32/div1 LDA2 ROT2 POP
|
||||||
;left-shift JSR2 ;div32/div1 STA2 ;div32/div0 STA2
|
;lshift32 JSR2 ;div32/div1 STA2 ;div32/div0 STA2
|
||||||
|
|
||||||
&loop
|
&loop
|
||||||
( if rem >= the current divisor, we can subtract it and add to quotient )
|
( if rem >= the current divisor, we can subtract it and add to quotient )
|
||||||
|
@ -380,8 +383,8 @@
|
||||||
,&quo0 LDR2 ,&quo1 LDR2 ,&cur0 LDR2 ,&cur1 LDR2 ;add32 JSR2 ,&quo1 STR2 ,&quo0 STR2 ( quo += cur )
|
,&quo0 LDR2 ,&quo1 LDR2 ,&cur0 LDR2 ,&cur1 LDR2 ;add32 JSR2 ,&quo1 STR2 ,&quo0 STR2 ( quo += cur )
|
||||||
|
|
||||||
&rem-lt
|
&rem-lt
|
||||||
,&div0 LDR2 ,&div1 LDR2 #01 ;right-shift JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 1 )
|
,&div0 LDR2 ,&div1 LDR2 #01 ;rshift32 JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 1 )
|
||||||
,&cur0 LDR2 ,&cur1 LDR2 #01 ;right-shift JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 )
|
,&cur0 LDR2 ,&cur1 LDR2 #01 ;rshift32 JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 )
|
||||||
,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done )
|
,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done )
|
||||||
,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 )
|
,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 )
|
||||||
RTN
|
RTN
|
||||||
|
|
|
@ -52,8 +52,8 @@ RTN
|
||||||
;buf LDA LIT '* EQU ;test-mul32 JCN2
|
;buf LDA LIT '* EQU ;test-mul32 JCN2
|
||||||
;buf LDA LIT '- EQU ;test-sub32 JCN2
|
;buf LDA LIT '- EQU ;test-sub32 JCN2
|
||||||
;buf LDA LIT '/ EQU ;test-div32 JCN2
|
;buf LDA LIT '/ EQU ;test-div32 JCN2
|
||||||
;buf LDA LIT 'L EQU ;test-left-shift JCN2
|
;buf LDA LIT 'L EQU ;test-lshift32 JCN2
|
||||||
;buf LDA LIT 'R EQU ;test-right-shift JCN2
|
;buf LDA LIT 'R EQU ;test-rshift32 JCN2
|
||||||
;buf LDA LIT 'B EQU ;test-bitcount32 JCN2
|
;buf LDA LIT 'B EQU ;test-bitcount32 JCN2
|
||||||
;buf LDA LIT '& EQU ;test-and32 JCN2
|
;buf LDA LIT '& EQU ;test-and32 JCN2
|
||||||
;buf LDA LIT '| EQU ;test-or32 JCN2
|
;buf LDA LIT '| EQU ;test-or32 JCN2
|
||||||
|
@ -123,8 +123,8 @@ RTN
|
||||||
@test-mul32 ;mul32 ;binary-32-test JMP2
|
@test-mul32 ;mul32 ;binary-32-test JMP2
|
||||||
@test-sub32 ;sub32 ;binary-32-test JMP2
|
@test-sub32 ;sub32 ;binary-32-test JMP2
|
||||||
@test-div32 ;div32 ;binary-32-test JMP2
|
@test-div32 ;div32 ;binary-32-test JMP2
|
||||||
@test-left-shift ;left-shift ;binary-32-8-32-test JMP2
|
@test-lshift32 ;lshift32 ;binary-32-8-32-test JMP2
|
||||||
@test-right-shift ;right-shift ;binary-32-8-32-test JMP2
|
@test-rshift32 ;rshift32 ;binary-32-8-32-test JMP2
|
||||||
@test-bitcount32 ;bitcount32 ;unary-32-8-test JMP2
|
@test-bitcount32 ;bitcount32 ;unary-32-8-test JMP2
|
||||||
@test-and32 ;and32 ;binary-32-test JMP2
|
@test-and32 ;and32 ;binary-32-test JMP2
|
||||||
@test-or32 ;or32 ;binary-32-test JMP2
|
@test-or32 ;or32 ;binary-32-test JMP2
|
||||||
|
|
Loading…
Reference in New Issue