\ examples.fs

\ ----- putting a 16-bit number on the stack -----
: tadr  $0123 ## ;

\ ----- fetch and store from/to a direct cell -----
: set5  $23 #  5 #! ;   \ store $23 in direct cell 5
: get5  ( - n) 5 #@ ;   \ fetch contents of direct cell 5

\ ----- using the address register to fetch and store bytes -----
: put3  5 # a!  5 # 4 # 3 # !+ !+ !+ ;
: get3  ( -- n1 n2 n3)   5 # a!  @+ @+ @+ ;

\ ----- variables and constants -----
$0a constant con1
$0B constant con2
$05 constant cell5
: test1  cell5 # a!  con1 # ! ;  \ indirectly load cell 5 using a
: test2  con2 # cell5 #! ;  \ directly load cell5 with con2
: .cell5  cell5 #@ h. ;     \ display contents of cell5 in hex

: myhead [ $01 $02 ] , , ;
: five 5 # ;
:m fiver 5 # m;
: mf  fiver ;
:m SCLK 0 .P2 m;
:m (+P2.0)  SCLK set m;
: +P2.0 [ SCLK set ] ;
: init7   5 # 7 #for  0 .P2 toggle  7 #next ;
: iamhere here [ dup $FF00 AND 8 RSHIFT ] # # ;
: bloop  $0A # begin dup . 1- 0=until drop ;
: bloop1  0 # begin dup . 1+ $0A # =until drop ; 
: bloop2  0 # begin dup . 1- -10 # <until drop ;
: bloop3  1 # begin dup . 2*' 5 # until drop ;    

: bloop5  1 # begin dup . 2* 5 .T until. drop ;
: bloop6  1 # begin dup . 2* -until drop ; 

\ ----- using various conditional operators ----- 
: iffy1  if drop $0A # ; then drop $0B # ;
: iffy2  1 .T if. $0A # ; then $0B # ;
: iffy3  1 .P0 if. $0A # ; then $0B # ;
: iffy3  1 .P0 dup if. drop $0A # ; then drop $0B # ;
: iffy4  $80 # 2* drop if' $0A # ; then $0B # ;
: iffy5  $80 # 2*' drop 0=if' $0A # ; then $0B # ;
: iffy6  ( n -- n)  -if drop $0A # ; then drop $0B # ; 

\ ----- nested #for ... #next loops -----  
: delay 0 # 7 #for 0 # 6 #for 50 # 5 #for 5 #next 6 #next 7 #next ;

\ ----- arithmetic and logic -----
: ior1  $aa #  $55 # ior ;
: ior2  $aa #  8 # !  $55 # 8 and! ;
: addem  ( n1 n2 - n3)   4 # 5 # + ;
: negate-example  ( - n)   44 #  5 # negate + ;
: invert-example  ( - n1 n2)  5 ~#  5 # invert ;
: leftwith  [ setc ] $c0 # 2*' ;

\ incrementing and decrementing
: incunder  ( n1 n2 - n3 n2)   u1+ ;
: decunder  ( n1 n2 - n3 n2)   u1- ;

\ ----- Multiplication & Division -----
: *  ( n1 n2 - n3)   3 # 5 # |* ;
: um*  ( n1 n2 - n1 )   |um* ;

$a4 constant P0MDOUT
:m dirs  P0MDOUT m;
: logic2  $fc # dirs ior! ;
:m pins  P0 m;
: logic3 ( c - ) $f0 # pins and!  pins ior! ;
: swaper   $a5 #  [ $c4 ] , ;

\ ----- data pointer -----
: p  ( - n1 n2)  |p ;
: pmore ( n1 n2 -- n3 n4)   p! p+ ;

\ ----- ASSEMBLER EXAMPLES -----
\ set bit in accumulator with set
: set-example1  ( - n)   5 #  [ 1 .T set ] ;
: set-example2  ( - n)   5 #  1 .T set ;

\ Move data in a direct cell to a register
:m direct-to-register  [ $15 3 mov ] m;
: testdr  ( - n)   
   $15 # a!  22 # ! 	\ move 22 into direct cell $15
   direct-to-register 	\ move contents of $15 to R3
   3 # a!  @ ;		\ put contents of R3 on the stack

\ Move data in a register to a direct cell
:m register-to-direct  [ 3 $15 mov ] m;
: testrd  ( - n)   
   3 # a!  33 # ! 	\ move 33 into R3
   register-to-direct 	\ move contents of R3 to direct cell $15
   $15 # a!  @ ;	\ put contents of $15 on the stack

\ Move data in a direct cell to another direct cell
:m direct-to-direct  [ $15 $16 mov ] m;
: testdd  ( - n)   
   $15 # a!  $a5 # ! 	\ move $a5 into direct cell $15
   direct-to-direct 	\ move contents of $15 to direct cell $16
   $16 # a!  @ ;	\ put contents of $16 on the stack
: increment-R7  ( - n)   $aa # 7 #!  7 inc  7 #@ ;
