Menu

How to read a loop variable

2023-04-24
2023-07-02
  • Jan Kromhout

    Jan Kromhout - 2023-04-24

    I need in the second loop the loopcounter of the first loop.
    As example I call it counter1.
    Is this posible in FF?

    Cheers
    Jan

    : test_7SEG ( --- )
      i2c.init
      7 for \ counter1
             7SEG i2c.addr.write drop
            1 i2c.c! drop 
            3 for \ counter2
                   1 counter1 lshift  i2c.c! drop 
             next
            i2c.stop
      next
    ;
    
     
    👍
    1
  • Mikael Nordman

    Mikael Nordman - 2023-04-25

    On the PIC24 you can do it like this.

    : for-test
      5 for
        5 for
          rp@ 2- @ .
          rp@ 4 - @ .
        next
      next
    ;
    

    Or like this.

    : i rp@ #6 - @ ;
    : j rp@ #8 - @ ;
    : k rp@ #10 - @ ;
    : for-test
      2 for
        3 for
          4 for
            i .
            j .
            k .
          next
        next
      next
    ;
    

    For PIC18 and the ATMEGA different offsets must be used.

     
    👍
    1
  • Tristan Williams

    I too am trying to read inner and outer loop variables in nested for..next but am unable to find rp@ on a PIC18. rp@ does not seem to be a builtin word and I can't locate it in any .fs files. All help gratefully received.

    Best wishes,
    Tristan

     
    • Mikael Nordman

      Mikael Nordman - 2023-05-30

      I think rp@ has never existed for PIC18 because it is not possible to index into the HW return stack. So it was rather pointless to read the return stack pointer.
      BUT, some years ago I changed the return stack handling so that call/return uses the hardware stack and R> >R FOR NEXT uses a software stack where FSR2 is the stack pointer.

      Assuming the Q71 family the return stack pointer can be fetched by by reading C4D9.

      : rp@ $c4d9 @ $c000 or ;
      K42, K83 and similar
      : rp@ $ffd9 @  $c000 or ;
      Older chips
      : rp@ $ffd9 @  $f000 or ;
      

      The cells on the return stack has the bytes swapped so some extra code is needed to fetch the indexes. Did some trials but messed it up.

       

      Last edit: Mikael Nordman 2023-05-30
  • Tristan Williams

    Thank you. Just what I needed. Below seems to work with the Q71.
    Best wishes,
    Tristan

    _ijk_                                                                                                                      
    marker _ijk_                                                                                                               
    
    $c4d9 constant fsr2                                                                                                        
    
    : rp@ fsr2 @ $c000 or ;                                                                                                    
    
    : i r@ ; \ or use synonym                                                                                                  
    : j rp@    2- dup c@ swap 1- c@ 8 lshift or ;                                                                              
    : k rp@ 2- 2- dup c@ swap 1- c@ 8 lshift or ;                                                                              
    
    : ex                                                                                                                       
        cr                                                                                                                     
        2 for                                                                                                                  
            3 for                                                                                                              
                4 for                                                                                                          
                    k . j . i . cr                                                                                             
                next                                                                                                           
                cr                                                                                                             
            next                                                                                                               
            cr                                                                                                                 
        next                                                                                                                   
        cr                                                                                                                     
    ;        
    
    1 2 3 
    1 2 2 
    1 2 1 
    1 2 0 
    
    1 1 3 
    1 1 2 
    1 1 1 
    1 1 0 
    
    1 0 3 
    1 0 2 
    1 0 1 
    1 0 0 
    
    
    0 2 3 
    0 2 2 
    0 2 1 
    0 2 0 
    
    0 1 3 
    0 1 2 
    0 1 1 
    0 1 0 
    
    0 0 3 
    0 0 2 
    0 0 1 
    0 0 0 
    
     
  • Jan Kromhout

    Jan Kromhout - 2023-07-01

    normaly I can copy the top item of the return stack with r@ , how to make a copy of the second item? or in geneneral copy the n-th element?

    cheers, Jan

     
  • Jan Kromhout

    Jan Kromhout - 2023-07-02

    found solutinin, second item on retrnstack is rp@ 2- @

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.