\ ram_tools.fs -- standalone version of xram access tools 18Aug07rjn
\ 
\ code needed to access extended on board RAM
\ 16bit addresses used to get/put data in ext ram.
\
\ --- extended ram access macros
:m rxram $E0 , m;  \ looks equivalent to @x in misc8051.fs
\ to use above must do p! first.
\ :m wxram $F0 , m;  \ same as (!x) in misc8051.fs
\ use !x instead as it has drop...

\ need just the header line
: .hrow     \ print the header row numbers
   cr space space space space space
   0 #  16 # 6 #for dup h. 1+ 6 #next drop
   ;

\ working version of .pxram -- doesn't hang
: .xram  ( da -)  p!
   cr space space space space space
   0 #  16 # 6 #for dup h. 1+ 6 #next drop
   16 # 6 #for
      cr p dh. 
      16 # 5 #for @x+ h. 5 #next
   6 #next ;

\ special test
: ?char ( n ) \ is it a printable character..
 \        dup
         -if  \ if negative nothing to print
            drop $2e # emit ;  \ print "."
         then
         0=if  \ if zero nothing to print
            drop $2e # emit ;  \ print "."
         then dup       \ going to trash char
         $1f # negate + -if  \ if not in range
            drop drop $2e # emit ; \ print "."
         then  
            drop emit \ char is printable
       ;

\ like above but with ascii displayed as well
: .axram  ( da -)  p!
   cr space space space space space
   0 #  16 # 6 #for dup h. 1+ 6 #next drop
   16 # 6 #for
      cr p dh. 
      [ $82 push DPH push ]  \ save dptr
      16 # 5 #for @x+ h. 5 #next
      [ DPH pop $82 pop ]    \ get dptr back
      cr space space space space space
      16 # 5 #for @x+ ?char space space 5 #next
   6 #next ;

\ version without header of .pxram -- doesn't hang
: .xxram  ( da -)  p!
   16 # 6 #for
      cr p dh. 
      16 # 5 #for @x+ h. 5 #next
   6 #next ;

\ specify number of 16 byte lines to display -- re-added 18Aug07rjn
: #.xram  ( n da -)  p!
   cr space space space space space
   0 #  16 # 6 #for dup h. 1+ 6 #next drop
   ( 16 #) 6 #for
      cr p dh. 
      16 # 5 #for @x+ h. 5 #next
   6 #next ;

\ without the header
: #.xxram  ( n da -)  p!
   ( 16 #) 6 #for
      cr p dh. 
      16 # 5 #for @x+ h. 5 #next
   6 #next ;

\ old code but still seeing corrupted returns
\  !!!!!!! old code
\ $3100 ## .pram = print ram using movx a,@dptr
\ has a problem - hangs - must be something in h.
: .pxram ( da) p! 
        cr p dh.
        16 # 7 #for
         rxram p+ h. 
        7 #next cr 
       ;

\ this one seems to work ok - bdk...
\ macro to emit xram as ascii
:m emxram 
         $E0 ,              \ read xram to a
         $03 , $03 , $03 , $03 , \ hi nibble to low
         $54 , $0F ,        \ anl $0f - hide upper nibble
         $24 , $90 ,        \ add to a $90
         $D4 ,              \ decimal adjust
         $34 , $40 ,        \ addc $40
         $D4 ,              \ decimal adjust
         emit
         $E0 , $A3 ,        \ read xram data, inc dptr
         $54 , $0F ,        \ anl $0f - hide upper nibble
         $24 , $90 ,        \ add to a $90
         $D4 ,              \ decimal adjust
         $34 , $40 ,        \ addc $40
         $D4 ,              \ decimal adjust
         emit
       m;

\ lets dump one byte of xram
\ $3000 ## .bxram
: .bxram ( da) p! 
         emxram
       ;

\ lets dump one line of xram
\ $3000 ## .sxram
: .sxram ( da) p! 
        cr p dh.
        16 # 7 #for
         emxram
        7 #next cr 
       ;

\ lets dump $100 of xram
\ $3000 ## .dxram
: .dxram ( da) p!
       $10 # 6 #for
        cr p dh.
        16 # 7 #for
         emxram
        7 #next
       6 #next
      ;

\ need some xram routines
\ $A5 # $3123 ## !xram = byte to xram using movx @dptr,a
: !xram  ( n da -)   p! !x ;  \ consumes byte

\ $3123 ## @xram = byte
: @xram  ( da - n)   p! @x ;  \ returns byte

\ $A5 # $3100 ## fxram = fill xram using movx @dptr,a
: fxram ( da) p!
        $7F # 7 #for
          (!x) p+  
        7 #next 
       drop
       ;
      
\ $3100 ## ifxram = fill xram using movx @dptr with index
\ will get $7F to $00 in ram
: ifxram ( d) p!
        $7F # 7 #for
         $EF , !x p+  \ mov a,r7 so drop a...
        7 #next 
       ;
      
: clr_xram 
        $90 , $00 , $00 , \ base address in dptr $0000
        $7F , $FF ,       \ r7 low end
        $7E , $20 ,       \ r6 hi end $1fff+1
        $E4 ,             \ clr a
: clr-loop
        $F0 ,             \ movx @DPTR,a
        $A3 ,             \ inc dptr
        $DF , $FC ,       \ djnz r7 to clr-loop
        $DE , $FA ,       \ djnz r6 to clr-loop
        ;

\ need to increment counter in xram - need read - write it
\ $3190 ## inc_xram 
: inc_xram ( da-) 
           @xram 1+ !x
           ;

\ need to decrement counter in xram - need read - write it
\ $3190 ## inc_xram 
: dec_xram ( da-) 
           @xram 1- !x
           ;

\ need to clear counter in xram - write only
\ $3190 ## zip_xram 
: zip_xram ( da-)
           p! 0 # !x
           ;


\ routines to move data between buffers
\ getput - get from given and put in already set p
\ call by doing $1234 ## p! then $4321 ## getput
\ this is single byte move.
: xgetput ( da -) \ p already set
      [ $82 push DPH push ]  \ save dptr
      p! @x                  \ get the byte
      [ DPH pop $82 pop ]    \ get dptr back
      !x+                    \ store and incr
      ;

\ this is double byte move.
: xgetputd ( da -) \ p already set
      [ $82 push DPH push ]  \ save dptr
      p! @x+ @x              \ get the byte
      swap                   \ swap order
      [ DPH pop $82 pop ]    \ get dptr back
      !x+ !x+                \ store and incr
      ;

\ macro to move data between buffers
\ mgetput - get from given and put in given
\ call by doing $1234 ## $4321 ## mgetput
\ $src $dest -- dptr not saved between 
\ this is single byte move.
:m mxgetput ( da da -) p!    \ set dptr
      [ $82 push DPH push ]  \ save dptr
      p! @x                  \ get the byte
      [ DPH pop $82 pop ]    \ get dptr back
      p! !x                  \ store and incr
      m;

\ this is double byte move.
:m mxgetputd ( da da -) p!   \ set dptr
      [ $82 push DPH push ]  \ save dptr
      p! @x+ @x              \ get the byte
      swap                   \ swap order
      [ DPH pop $82 pop ]    \ get dptr back
      p! !x+ !x              \ store and incr
      m;

\ some string handling for xram...
\ defined elsewhere = :m " 34 parse here there place here there [ c@ 1 + ] allot m;
: $xtype ( da )  \ top is source ptr R5/R6 is destination
      p! @p+       \ set source ptr and get count + up pointer
       begin       \ start counted loop
        @p+        \ get byte & add one to dptr
        [ $82 push DPH push ] \ save off source ptr
        $8D , $82 , $8E , $83 , \ get dest ptr R5>DPL R6>DPH
        !x+
        $AD , $82 , $AE , $83 , \ save dest ptr DPL>R5 DPH>R6
        [ DPH pop $82 pop ]    \ get source dptr back
        1-         \ decrament count
       0=until     \ loop until done
      drop ;       \ drop count

: xstring ( da ) \ pass destination get source
      pop pop swap $xtype 
      ;

: xsetptr ( da ) \ address into r5/r6 for $xtype to use
        [ $82 push DPH push ] \ save dptr just in case
        p!
        $AD , $82 , $AE , $83 , \ save dest ptr DPL>R5 DPH>R6
        [ DPH pop $82 pop ]    \ get dptr back
        ;

: !xbyptr ( n ) \ store byte using r5/r6 as dptr 
        $8D , $82 , $8E , $83 , \ get dest ptr R5>DPL R6>DPH
        !x+
        $AD , $82 , $AE , $83 , \ save dest ptr DPL>R5 DPH>R6
        ;
 
     
