Shop OBEX P1 Docs P2 Docs Learn Events
Copying array into a Cog — Parallax Forums

Copying array into a Cog

rezzrezz Posts: 1
edited 2008-10-14 23:05 in Propeller 1
Hello Everyone,

This question is related to the "Passing PAR to assembly" question recently asked by Hack Saw. I'm trying to set values for an array in Spin, then copy array into a new Cog and use the values in the array as delay times to control toggling a pin on/off. I've tried the following code, but I'm doing something wrong. It may be a very simple error, such as not copying an address into a pointer correctly, or not incrementing my pointers correctly.

I've searched the forums and haven't found a topic exactly on point. I'd appreciate any assistance.

(I realize that Spin would be fast enough to handle the delays I'm trying to achieve (2 - 3 seconds), but these are just for testing. In my actual application I'll need to use delays that are small fractions of a second, and Spin won't be fast enough.)

'' File: AssemblyTest.spin
'' Trying out assembly language

CON

  _clkmode        = xtal1 + pll16x           ' Feedback and PLL multiplier
                                             ' xtal1 - means that you have an external
                                               ' low speed crystal supplying clock oscillations
                                             ' pll16x - means you're multiplying external
                                               ' crystal frequency by 16 
  _xinfreq        = 5_000_000                ' External oscillator = 5 MHz

                                             ' BELOW:  clkfreq is the clock frequency after
                                               ' it has been set by above

                                             ' cnt is the system counter register - basically
                                             '  it's elapsed clock cycles

                                             'clkfreq is the clock frequency - cycles/second

  LED_PIN         = 3                        ' Pin that sends on/off signal to LED
  ARRAY_SIZE      = 4


VAR

  long delay[noparse][[/noparse]ARRAY_SIZE]
  long IdCog


PUB LED                             ' Main method

  IdCog := 0

  'Set delays:
  delay[noparse][[/noparse]0] := 2 * clkfreq
  delay := 3 * clkfreq
  delay := 2 * clkfreq
  delay := 3 * clkfreq

  IdCog := cognew(@AssyTest, @delay)

  'Shut down cog after 20 seconds - should be more than
  'enough time to run through delay array
  waitcnt(cnt + clkfreq * 20)

  cogstop(IdCog)  
  
DAT

                        ORG     0
AssyTest                mov     ptr_global, par
                        mov     ptr_local, @array
                        
' Loop through delay[noparse][[/noparse]] array by way of ptr_global, and read it into local array
                        mov     Count, #0 
loop
                        rdlong  ptr_local, ptr_global
                        add     ptr_local, #4
                        add     ptr_global, #4
                        add     Count, #1
                        cmp     Count, #ARRAY_SIZE       wz
             if_nz      jmp     #loop


' Reset Count to zero, ptr_local to start of array, set pin to output
                        mov     Count, #0
                        mov     ptr_local, @array
                        mov     dira, pin
                         
' Toggle LED based on values from local copy of delay[noparse][[/noparse]] array
loop2
                        mov     Time, cnt
                        add     Time, ptr_local     
                        waitcnt Time, ptr_local                        
                        xor     outa, pin              'Toggle LED on/off
                        add     Count, #1
                        add     ptr_local, #4
                        cmp     Count, #ARRAY_SIZE       wz
             if_nz      jmp     #loop2                        

pin                     long    1<< LED_PIN
Count                   long    0
array                   res     ARRAY_SIZE             'reserve array for ARRAY_SIZE longs
Time                    res     1
ptr_local               res     1
ptr_global              res     1




Thanks

Comments

  • mparkmpark Posts: 1,305
    edited 2008-10-14 22:13
    I think you want to say
    mov ptr_local, #array
  • hippyhippy Posts: 1,981
    edited 2008-10-14 23:05
    Also "rdlong ptr_local, ptr_global" will load what ptr_global points to into ptr_local, not into what ptr_local points to.

    Additionally, "add ptr_local, #4" should be #1; every location in Cog is one long and addressed as longs rather than as four bytes.
Sign In or Register to comment.