237 lines
5.7 KiB
Tal
237 lines
5.7 KiB
Tal
( math32.tal )
|
|
( )
|
|
( 32-bit integers are represented by two 16-bit integers )
|
|
( x** means xhi* xlo* )
|
|
|
|
%DEBUG { #ff #0e DEO }
|
|
%RTN { JMP2r }
|
|
%EMIT { #18 DEO }
|
|
%DIGIT { #00 SWP ;digits ADD2 LDA EMIT }
|
|
%SPACE { #20 EMIT }
|
|
%NEWLINE { #0a EMIT }
|
|
|
|
%TOR2 { ROT2 ROT2 }
|
|
%POP4 { POP2 POP2 }
|
|
|
|
%X { #0000 #0001 }
|
|
%Y { #1234 #ffff }
|
|
%Z { #fedc #ba98 }
|
|
|
|
|0100
|
|
( run a bunch of test cases )
|
|
|
|
#ffff #ffff #2a ;mul16 ;emit-short ;test16 JSR2
|
|
#0001 #ffff #2a ;mul16 ;emit-short ;test16 JSR2
|
|
#ffff #0001 #2a ;mul16 ;emit-short ;test16 JSR2
|
|
#00ff #0001 #2a ;mul16 ;emit-short ;test16 JSR2
|
|
NEWLINE
|
|
|
|
#ffff #ffff #ffff #ffff #2a ;mul32 ;emit-long ;test32 JSR2
|
|
#0000 #0001 #ffff #ffff #2a ;mul32 ;emit-long ;test32 JSR2
|
|
#0000 #0002 #0000 #ffff #2a ;mul32 ;emit-long ;test32 JSR2
|
|
#0001 #2345 #0034 #5678 #2a ;mul32 ;emit-long ;test32 JSR2
|
|
NEWLINE
|
|
|
|
X Y #2b ;add32 ;emit-long ;test32 JSR2
|
|
Z Y #2b ;add32 ;emit-long ;test32 JSR2
|
|
#fedc #0000 #1234 #0000 #2b ;add32 ;emit-long ;test32 JSR2
|
|
#fedc #0000 #0000 #0000 #2b ;add32 ;emit-long ;test32 JSR2
|
|
#0000 #0000 #0000 #0000 #2b ;add32 ;emit-long ;test32 JSR2
|
|
NEWLINE
|
|
|
|
#1234 #1234 #0000 #0001 #2d ;sub32 ;emit-long ;test32 JSR2
|
|
#1234 #1234 #0000 #1234 #2d ;sub32 ;emit-long ;test32 JSR2
|
|
#1234 #1234 #1234 #0001 #2d ;sub32 ;emit-long ;test32 JSR2
|
|
#1234 #1234 #1000 #0000 #2d ;sub32 ;emit-long ;test32 JSR2
|
|
#1234 #1234 #ffff #ffff #2d ;sub32 ;emit-long ;test32 JSR2
|
|
NEWLINE
|
|
|
|
X Y #26 ;and32 ;emit-long ;test32 JSR2
|
|
X Y #7c ;or32 ;emit-long ;test32 JSR2
|
|
X Y #5e ;xor32 ;emit-long ;test32 JSR2
|
|
X Y #3d ;eq32 ;emit-byte ;test32 JSR2
|
|
Y Y #3d ;eq32 ;emit-byte ;test32 JSR2
|
|
X Y #7e ;ne32 ;emit-byte ;test32 JSR2
|
|
X X #7e ;ne32 ;emit-byte ;test32 JSR2
|
|
BRK
|
|
|
|
@test16 ( x* y* symbol^ test-addr* emit-addr* -> )
|
|
,&emitaddr STR2
|
|
,&testaddr STR2
|
|
,&testsym STR
|
|
,&y STR2
|
|
,&x STR2
|
|
,&x LDR2 ;emit-short JSR2
|
|
SPACE ,&testsym LDR EMIT SPACE
|
|
,&y LDR2 ;emit-short JSR2
|
|
SPACE #3d EMIT SPACE
|
|
,&x LDR2 ,&y LDR2
|
|
,&testaddr LDR2 JSR2
|
|
,&emitaddr LDR2 JSR2 NEWLINE
|
|
RTN
|
|
&testsym $1
|
|
&testaddr $2
|
|
&emitaddr $2
|
|
&x $2 &y $2
|
|
|
|
@test32 ( x** y** symbol^ test-addr* emit-addr* -> )
|
|
,&emitaddr STR2
|
|
,&testaddr STR2
|
|
,&testsym STR
|
|
,&ylo STR2 ,&yhi STR2
|
|
,&xlo STR2 ,&xhi STR2
|
|
,&xhi LDR2 ,&xlo LDR2 ;emit-long JSR2
|
|
SPACE ,&testsym LDR EMIT SPACE
|
|
,&yhi LDR2 ,&ylo LDR2 ;emit-long JSR2
|
|
SPACE #3d EMIT SPACE
|
|
,&xhi LDR2 ,&xlo LDR2
|
|
,&yhi LDR2 ,&ylo LDR2
|
|
,&testaddr LDR2 JSR2
|
|
,&emitaddr LDR2 JSR2 NEWLINE
|
|
RTN
|
|
&testsym $1
|
|
&testaddr $2
|
|
&emitaddr $2
|
|
&xhi $2 &xlo $2
|
|
&yhi $2 &ylo $2
|
|
|
|
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ )
|
|
ROT2 EQU2 ,&maybe JCN
|
|
POP4 #00 RTN
|
|
&maybe EQU2
|
|
RTN
|
|
|
|
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ )
|
|
ROT2 EQU2 ,&maybe JCN
|
|
POP4 #01 RTN
|
|
&maybe NEQ2
|
|
RTN
|
|
|
|
@and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
|
ROT2 AND2 TOR2 AND2 SWP2
|
|
RTN
|
|
|
|
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
|
ROT2 ORA2 TOR2 ORA2 SWP2
|
|
RTN
|
|
|
|
@xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
|
ROT2 EOR2 TOR2 EOR2 SWP2
|
|
RTN
|
|
|
|
%COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 }
|
|
|
|
@complement32 ( x** -> ~x** )
|
|
COMPLEMENT32
|
|
RTN
|
|
|
|
@negate32 ( x** -> -x** )
|
|
COMPLEMENT32
|
|
INC2 ( ~xhi -xlo )
|
|
DUP2 #0000 NEQ2 ( ~xhi -xlo non-zero? )
|
|
,&done JCN ( xlo non-zero => don't inc hi )
|
|
SWP2 INC2 SWP2 ( -xhi -xlo )
|
|
&done
|
|
RTN
|
|
|
|
@left-by-16 ( xhi* xlo* -> xlo* 0000 )
|
|
SWP2 POP2 #0000
|
|
RTN
|
|
|
|
@add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
|
|
,&y2 STR2 ,&y0 STR2 ( save ylo, yhi )
|
|
,&x2 STR2 ,&x0 STR2 ( save xlo, xhi )
|
|
#0000 #0000 ,&z0 STR2 ,&z2 STR2 ( reset zhi, zlo )
|
|
|
|
( x3 + y3 => z2z3 )
|
|
#00 ,&x3 LDR #00 ,&y3 LDR ADD2 ,&z2 STR2
|
|
|
|
( x2 + y2 + z2 => z1z2 )
|
|
#00 ,&x2 LDR ,&z1 LDR2 ADD2 ,&z1 STR2
|
|
#00 ,&y2 LDR ,&z1 LDR2 ADD2 ,&z1 STR2
|
|
|
|
( x1 + y1 + z1 => z0z1 )
|
|
#00 ,&x1 LDR ,&z0 LDR2 ADD2 ,&z0 STR2
|
|
#00 ,&y1 LDR ,&z0 LDR2 ADD2 ,&z0 STR2
|
|
|
|
( x0 + y0 + z0 => z0 )
|
|
,&x0 LDR ,&z0 LDR ADD ,&z0 STR
|
|
,&y0 LDR ,&z0 LDR ADD ,&z0 STR
|
|
|
|
( load zhi,zlo )
|
|
,&z0 LDR2 ,&z2 LDR2
|
|
RTN
|
|
( registers for add32 )
|
|
[ &x0 $1 &x1 $1 &x2 $1 &x3 $1 ]
|
|
[ &y0 $1 &y1 $1 &y2 $1 &y3 $1 ]
|
|
[ &z0 $1 &z1 $1 &z2 $2 ]
|
|
|
|
@sub32 ( x** y** -> z** )
|
|
;negate32 JSR2 ;add32 JSR2
|
|
RTN
|
|
|
|
@mul16 ( x* y* -> z** )
|
|
,&y1 STR ,&y0 STR ( save ylo, yhi )
|
|
,&x1 STR ,&x0 STR ( save xlo, xhi )
|
|
#0000 #00 ,&z0 STR2 ,&z2 STR ( reset z0,z1,z2 )
|
|
#0000 #00 ,&a0 STR2 ,&a2 STR ( reset a0,a1,a2 )
|
|
|
|
( x1 * y1 => z1z2 )
|
|
#00 ,&x1 LDR #00 ,&y1 LDR MUL2 ,&z1 STR2
|
|
|
|
( x0 * y1 => z0z1 )
|
|
#00 ,&x0 LDR #00 ,&y1 LDR MUL2 ,&z0 LDR2 ADD2 ,&z0 STR2
|
|
|
|
( x1 * y0 => a1a2 )
|
|
#00 ,&x1 LDR #00 ,&y0 LDR MUL2 ,&a1 STR2
|
|
|
|
( x0 * y0 => a0a1 )
|
|
#00 ,&x0 LDR #00 ,&y0 LDR MUL2 ,&a0 LDR2 ADD2 ,&a0 STR2
|
|
|
|
( add z and a<<8 )
|
|
#00 ,&z0 LDR2 ,&z2 LDR
|
|
,&a0 LDR2 ,&a2 LDR #00
|
|
;add32 JSR2
|
|
RTN
|
|
[ &x0 $1 &x1 $1 ]
|
|
[ &y0 $1 &y1 $1 ]
|
|
[ &z0 $1 &z1 $1 &z2 $1 ]
|
|
[ &a0 $1 &a1 $1 &a2 $1 ]
|
|
|
|
@mul32 ( x** y** -> z** )
|
|
,&y1 STR2 ,&y0 STR2 ( save ylo, yhi )
|
|
,&x1 STR2 ,&x0 STR2 ( save xlo, xhi )
|
|
,&y1 LDR2 ,&x1 LDR2 ;mul16 JSR2 ( [x1*y1] )
|
|
,&z1 STR2 ,&z0 STR2 ( sum = x1*y1, save zlo, zhi )
|
|
|
|
,&y1 LDR2 ,&x0 LDR2 MUL2 ( [x0*y1]<<16 )
|
|
,&y0 LDR2 ,&x1 LDR2 MUL2 ( [x1*y0]<<16 )
|
|
( [x0*y0]<<32 will completely overflow )
|
|
ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 )
|
|
,&z1 LDR2
|
|
RTN
|
|
[ &x0 $2 &x1 $2 ]
|
|
[ &y0 $2 &y1 $2 ]
|
|
[ &z0 $2 &z1 $2 ]
|
|
|
|
@emit-long ( hi* lo* -> )
|
|
SWP2 ( lo* hi* )
|
|
;emit-short JSR2
|
|
;emit-short JSR2
|
|
RTN
|
|
|
|
%EMIT-BYTE { DUP #04 SFT DIGIT #0f AND DIGIT }
|
|
|
|
@emit-short ( x* -> )
|
|
SWP ( lo^ hi^ )
|
|
EMIT-BYTE EMIT-BYTE
|
|
RTN
|
|
|
|
@emit-byte ( x^ -> )
|
|
EMIT-BYTE
|
|
RTN
|
|
|
|
@digits
|
|
30 31 32 33 34 35 36 37
|
|
38 39 61 62 63 64 65 66
|