Shop OBEX P1 Docs P2 Docs Learn Events
Re-writing code using counters — Parallax Forums

Re-writing code using counters

average joeaverage joe Posts: 795
edited 2012-09-03 01:23 in Propeller 1
Hey guys, I've been trying to re-write some code using the counters to control clock lines. I have been able to 2 out of 3 portions re-written but having trouble with the last one. The original code is as follows:
pasmhubtoram            call    #get_values             ' get hubaddr,ramaddr,len and set control pins
                        call    #set161
                        or      outa,maskP16P20         ' set control pins high
                        mov     latchvalue,#%11111101    ' group 2, displays all off
                        call    #set373                 ' send out to the latch
                                                                                                                       
hubtoram_loop   and     outa,maskP16P31         '%11111111_11111111_00000000_00000000  clear for output                    
                        rdword  data_16,hubaddr         ' get the word from hub                                                                         
                        or      outa,data_16            ' send out the byte to P0-P15                                                                   
                        andn    outa,maskP17            ' set mem write low                                                                             
                        add     hubaddr,#2              ' increment by 2 bytes = 1 word. Put this here for small delay while writes                     
                        or      outa,maskP17            ' mem write high                                                                             
                        andn    outa,maskP20            ' clock 161 low
                        or      outa,maskP20            ' clock 161 high
                        djnz    len,#hubtoram_loop      ' loop this many times                                                                                                                         
                                                                                                                                                      
                        or      outa,maskP20            ' stop clock                                                                                    
                        
                        jmp     #done                   ' tristate pins and listen for commands

This transfers data from hub to external sram. It works but the main loop takes 3 hub slots and I suspect using the counters is the key to get it down to 2. Here's my last effort:
'-----------------------------------------------------------------
' setups needed for burst write   Thanks to Jazzed (Steve Denson)
'-----------------------------------------------------------------
wr_phsa                  long    $0800_0000       ' phsa offset for adjusting clock start
wr_frqa                  long    $0800_0000
wr_ctra                  long    4<<26 | 20      ' NCO mode on P20

fastpasmhubtoram        call    #get_values             ' get hubaddr,ramaddr,len and set control pins
                        sub      len, #1
                        sub      ramaddr,#1
                        call    #set161
                        or      outa,maskP16P20         ' set control pins high
                        mov     latchvalue,#%11111101    ' group 2, displays all off
                        call    #set373                 ' send out to the latch

                        'insert burst read here
                        or      outa,maskP20           ' get clock line?
                        mov     phsa, wr_phsa             ' init counters phsa
                        mov     frqa, wr_frqa         ' setup NCO freq
                        
                        andn    outa,maskP20            ' start counters

                        rdword  data_16,hubaddr           ' sync up only                  
                        mov     ctra, wr_ctra             ' enable address counter clk                                                                                    
                        
                                                                                                                       
fasthubtoram_loop       and     outa,maskP16P31         '%11111111_11111111_00000000_00000000  clear for output                                         A0 A8 B0 B8             
                        rdword  data_16,hubaddr         ' get the word from hub                                                                         C0 C8 D0 D8  E0 E8 F0 F8 
                        or      outa,data_16            ' send out the byte to P0-P15                                                                   00 08 10 18 
                        andn    outa,maskP17            ' set mem write low                                                                             20 28 30 38 
                        add     hubaddr,#2              ' increment by 2 bytes = 1 word. Put this here for small delay while writes                     40 48 50 58 
                        or      outa,maskP17            ' mem write high                                                                                60 68 70 78
                        djnz    len,#fasthubtoram_loop   ' loop this many times                                                                         80 88 90 98
                                                                                                                                                      ' A0 A8 B0 B8
                        or      outa,maskP20            ' stop clock                                                                                    
                                                                                                                                                      ' 
                        mov     ctra, #0                ' stop counter                                                                                  
                                                                                                                                                     
                        jmp     #done                   ' tristate pins and listen for commands

For reference I'm posting the code I have been able to translate.
pasmramtohub            call    #get_values             ' get hubaddr,ramaddr,len and set control pins
                        call    #set161

                        or      outa,maskP16P20         ' set control pins high
                        mov     latchvalue,#%11111101    ' group 2, displays all off
                        call    #set373                 ' send out to the latch
                     
                        and     dira,maskP16P31         '%11111111_11111111_00000000_00000000 inputs
                        andn    outa,maskP16            ' memory /rd low
ramtohub_loop           mov     data_16,ina             ' get the data
                        wrword  data_16,hubaddr         ' move data to hub
                        andn    outa,maskP20            ' clock 161 low
                        or      outa,maskP20            ' clock 161 high
                        add     hubaddr,#2              ' increment the hub address 
                        djnz    len,#ramtohub_loop
                        or      outa,maskP16            ' memory /rd high  
                        jmp     #done                   ' ' tristate pins and listen for commands
Becomes
'-----------------------------------------------------------------------
' setups needed for burst read from ram  Thanks to Jazzed (Steve Denson)
'-----------------------------------------------------------------------
rd_frqa                  long     $1000_0000
rd_ctra                  long     4<<26 | 20     ' NCO mode on P20
rd_ctrb                  long     $A<<26| 20      ' Edge Accumulate mode on P20


fastpasmramtohub        call    #get_values             ' get hubaddr,ramaddr,len and set control pins
                        call    #set161
                        or      outa,maskP16P20         ' set control pins high
                        mov     latchvalue,#%11111101   ' group 2, displays all off
                        call    #set373                 ' send out to the latch
                           
                        and     dira,maskP16P31         ' %11111111_11111111_00000000_00000000 inputs
                        andn    outa,maskP16            ' memory /rd low

                        mov     phsa, #0                ' init counters phsa
                        mov     frqa, rd_frqa           ' setup NCO freq
                       
                        mov     phsb, hubaddr           ' save hub ptr to phsb
                        mov     frqb, #2                ' setup EDGE freq
                        'ctra setup for burst read
                        
                        or      outa,maskP20            ' get clock line?
                        andn    outa,maskP20            ' start counters

                        mov     ctrb, rd_ctrb           ' set ctr be mode
                           
                        rdword  data_16,phsb            ' sync up only                  
                        mov     ctra, rd_ctra           ' enable address counter clk                    
                        
fastramtohub_loop           ' 10MB/s read loop uses phsb for hub pointer
                        mov     data_16,ina             ' get first data                   1 2 3 4 
                        wrword  data_16,phsb            ' move data to hub                 5 6 7 8 9 A B C
                        djnz    len,#fastramtohub_loop                                '    D E F 0

                        or      outa,maskP20            ' stop clock
                        
                        mov      ctra, #0               ' stop counter
                        mov      ctrb, #0               ' stop counter

                        or      outa,maskP16            ' memory /rd high

                        jmp #done

And
pasmramtodisplay        call    #get_values             ' get hubaddr,ramaddr,len (only uses len) and set control pins
                        call    #set161
                        or      outa,maskP16P20         ' set control pins high
                        mov     latchvalue, dirb
                        call    #set373

                        andn    outa,maskP19            ' CS low
                        and     dira,maskP16P31         ' %11111111_11111111_00000000_00000000 so prop pins 0-15 HiZ
                        andn    outa,maskP16            ' ram /rd low

ramtodisplay_loop       andn    outa,maskP18            ' ILI write low
                        or      outa,maskP18            ' ILI write high
                        andn    outa,maskP20            ' clock 161 low
                        or      outa,maskP20            ' clock 161 high
                        djnz    len,#ramtodisplay_loop
                        or      outa,maskP16            ' mem /rd high
                        or      outa,maskP19            ' CS high 
                        jmp     #done

becomes
'--------------------------------------------------------------------------
' setups needed for burst write to display  Thanks to Jazzed (Steve Denson)
'--------------------------------------------------------------------------
ds_ctra                  long    4<<26 | 20      ' NCO mode on P20         
ds_frq                   long    $2000_0000      ' phsb accumulates twice per edge
ds_ctrb                  long    4<<26 | 18      ' NCO mode on P18 
                                                
fastpasmramtodisplay    call    #get_values             ' get hubaddr,ramaddr,len (only uses len) and set control pins
                        sub     len, #2 
                        add     ramaddr, #1   
                        call    #set161
                        or      outa,maskP16P20         ' set control pins high
                        mov     latchvalue, dirb
                        call    #set373
                        and     dira,maskP16P31         ' %11111111_11111111_00000000_00000000 so prop pins 0-15 HiZ
                        andn    outa,maskP19            ' CS low
                        andn    outa,maskP16            ' ram /rd low
            'ctra setup for burst read
                        mov     phsa, #0                  ' init counters phsa        P20
                        mov     phsb, #0                  ' init         to phsb   P18
                        mov     frqa, ds_frq              ' setup NCO freq             P20
                        mov     frqb, ds_frq              ' setup NCO freq        P18
                        andn    outa,maskP20              ' start counters             P20
                        andn    outa,maskP18              ' start counters         P18
                                                                                                '   P20   Count                 P18  Write
                        mov     ctrb, ds_ctrb             ' enable display clk           P18         
                        mov     ctra, ds_ctra             ' enable address counter clk   P20         0                           2   4   6   8
                        
fastramtodisplay_loop   nop                                  '                                       2    4   6   8               10  12  14 0  2   4   6  8 
                        djnz    len,#fastramtodisplay_loop   '                                       10  12  14   0               2   4   6  8
                                                                                                   ' 2    4   6   8               10  12  14 0
                        or      outa,maskP18                 ' stop clock
                                                                                                                               
                        mov     ctrb, #0                ' stop counter                               10  12  14   0               
                        mov     ctra, #0                ' stop counter                               2    4   6   8               10  12  14 0
                                                   
                        or      outa,maskP20            ' stop clock                                 2    4   6   8
                        
                        or      outa,maskP19            ' CS high
                        or      outa,maskP16            ' mem /rd high

                        jmp     #done

I think I'm doing things right, hopefully someone can help!

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-08-25 03:29
    I have been able to 2 out of 3 portions re-written but having trouble with the last one.
    What exactly is the trouble? I assume it's the location of the rising edge of the P20 clock? Right now it will rise during or outa,data_16, is that the problem?
  • average joeaverage joe Posts: 795
    edited 2012-08-25 03:38
    Sorry for the bad posting. The problem is the rising edge of P20. It needs to be after or outa,maskP17 ' mem write high. As you can see I'm no expert with the counters, hence why I was trying to figure it out myself. Steve provided the ram2hub for a previous board and I was able to modify for ram2display by figuring out what phs is for each instruction. Couldn't get it so I thought I'd ask for some help. Also, do you see any room for improvement in ram2display or ram2hub? Thanks!
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-25 03:56
    OK, you use the 5 top bits of the counter and effectively initialise them with 1 (%0000_1xxxx_...). The cycle following counter activation (1st public cycle of and) does the first increment of phsx, IOW 15 cycles later you'll see the rising edge (3rd public cycle of or). This also means that the falling edge is during djnz. So for now you could simply invert the signal (shift the phase) by initialising phsx with $88000000 instead. This means we start with P20 being high, it then goes low during or outa,data_16 and high again during djnz.

    attachment.php?attachmentid=95144&d=1345892568
    635 x 92 - 5K
  • average joeaverage joe Posts: 795
    edited 2012-08-25 04:29
    Okay, that worked! The sub len, #1 was the only thing wrong. That was an artifact of previous attempts to get things working. Thanks for your help! The timing diagram actually explains it quite well. I actually started with 8800_0000 then somehow worked my way backwards, lol.
  • average joeaverage joe Posts: 795
    edited 2012-09-03 01:23
    I'm posting here again because I'm a bit confused and hoping someone can help me out. I kept working and working on the burst-transfers using the propeller's counters. Seems I commented out too much from something that WAS working, and broke everything. I THINK it's fixed, but I still can't account for a few instructions, see code:
    pasmhubtoram            sub     ramaddr,#1                                      '??????
    
                            call    #set161and373                                   ' set up the 161 counter and then change to group 2
    
                            'or      dira,maskP0P15                                 ' %00000000_00000000_11111111_11111111         ' data bus outputs                              ''Pins set as output by Set161
                                                    'ctr setup for burst read
                            mov     phsa, wr_phsa                                   ' init counters phsa
                            mov     frqa, wr_frqa                                   ' setup NCO freq
                            andn    outa,maskP20                                    ' start counters
                            rdword  data_16,hubaddr                                 ' sync up only
                            mov     ctra, _ctra                                     ' enable address counter clk                                                              
                                                    ' 5MB/s write loop uses ctra for address clock
    hubtoram_loop           and     outa,maskP16P31                                 '%11111111_11111111_00000000_00000000 clear for output                                          
                            rdword  data_16,hubaddr                                 ' get the word from hub                                                 
                            or      outa,data_16                                    ' send out the byte to P0-P15                                           
                            andn    outa,maskP17                                    ' set mem write low                                                     
                            add     hubaddr,#2                                      ' increment by 2 bytes = 1 word. Put this here for small delay while writes 
                            or      outa,maskP17                                    ' mem write high                                                        
                            djnz    len,#hubtoram_loop                              ' loop this many times                                                  
                                                                                                                                                          
                            or      outa,maskP20                                    ' stop clock                                                                                                                                 ' 
                            mov     ctra, #0                                        ' stop counter                                                                                                                              
                            jmp     #done                                           ' tristate pins and listen for commands
    
    pasmramtodisplay        sub     len, #2                                         ' offset 1 extra clock at start and 1 at end
                            add     ramaddr, #1                                     ' ???not sure???
    
                            call    #set161                                         ' Set start address
                            mov     latchvalue, dirb                                ' restore latchvalue saved at get_values
                            call    #set373                                         ' and set
    
                            and     dira,maskP16P31         ' %11111111_11111111_00000000_00000000 so prop pins 0-15 HiZ
                            andn    outa,maskP19            ' CS low
                            andn    outa,maskP16            ' ram /rd low
                                                    'ctr setup for burst read
                            mov     phsa, #0                                        ' init counters phsa P20
                            mov     phsb, #0                                        ' init counters phsb P18
                            mov     frqa, ds_frq                                    ' setup NCO freq P20
                            mov     frqb, ds_frq                                    ' setup NCO freq P18
                            andn    outa,maskP20                                    ' start counters P20
                            andn    outa,maskP18                                    ' start counters P18                                                                                 
                            mov     ctrb, ds_ctrb                                   ' enable display clk P18
                            mov     ctra, _ctra                                     ' enable address counter clk P20
                                                    ' 20MB/s read loop uses ctra for address clock and ctrb for display clock
    ramtodisplay_loop       nop                                                     ' idle               
                            djnz    len,#ramtodisplay_loop                      '                             
    
                            or      outa,maskP18                                    ' stop clock
                            mov     ctrb, #0                                        ' stop counter                                    
                            mov     ctra, #0                                        ' stop counter       
                            or      outa,maskP20                                    ' stop clock            
                            or      outa,maskP19                                    ' CS high
                            or      outa,maskP16                                    ' mem /rd high
    
                            jmp     #done
    
    The ???? are what I'm wondering. The other burst-function seems to be okay? I'm hoping someone can help me figure out WHY these are needed, or how to correct if NOT needed. For clarification
    pasmramtohub            call    #set161and373                                   ' set up the 161 counter and change to group 2
    
                            and     dira,maskP16P31                                 '%11111111_11111111_00000000_00000000 inputs
                            andn    outa,maskP16                                    ' memory /rd low
    
                                                    'ctr setup for burst read
                            mov     phsa, #0                                        ' init counters phsa
                            mov     frqa, rd_frqa                                   ' setup NCO freq
                            mov     phsb, hubaddr                                   ' save hub ptr to phsb
                            mov     frqb, #2                                        ' setup EDGE freq
                            andn    outa,maskP20                                    ' start counters
                            mov     ctrb, rd_ctrb                                   ' set ctr be mode
                               
                            rdword  data_16,phsb                                    ' sync up only
                            mov     ctra, _ctra                                     ' enable address counter clk
                                                    ' 10MB/s read loop uses phsb for hub pointer
    ramtohub_loop           mov     data_16,ina                                     ' get first data                     
                            wrword  data_16,phsb                                    ' move data to hub
                            djnz    len,#ramtohub_loop                                
    
                            or      outa,maskP20                                    ' stop clock
                            mov     ctra, #0                                        ' stop counter
                            mov     ctrb, #0                                        ' stop counter
                            or      outa,maskP16                                    ' memory /rd high
    
                            jmp #done
    
    This needs no offset adjusts. The "overhead"
    set373                  or      outa,maskP22            ' pin 22 high 
                            'or      dira,maskP22            ' and now set as an output                                             'P22 always controlled by cog              
                            or      dira,#%1_11111111       ' enable pins 0-7 and 8 as outputs
                            'andn    outa,maskP8             ' P8 low 
                            and     outa,maskP0P8low        ' P0-P8 low                                                             ''P8 is made low at the same time as P0-P7
                            or      outa,latchvalue         ' send out the data 
                            or      outa,maskP8             ' P8 high, clocks out data
                            'andn    outa,maskP8             ' P8 low                                                               ''P8 low not needed
                            andn    outa,maskP22            ' pin 22 low
    set373_ret              ret
    
    set161                  mov     latchvalue,#%11111110    ' group 1, displays all off
                            call    #set373                 ' send out to the latch
                            and     outa,maskP0P20low       '%11111111_11100000_00000000_00000000   
                            or      dira,maskP0P20          '%00000000_00011111_11111111_11111111
                            or      outa,ramaddr            ' send out ramaddr
                            or      outa,maskP20            ' P20 clock high
                            or      outa,maskP19            ' p19 load high
                            andn    outa,maskP20            ' clock low                                                            
                            andn     outa,maskP19            ' load low
                            'or      outa,maskP20            ' clock high                                                          ''Not needed since    
                            'or      outa,maskP19            ' load high
                            or      outa,maskP16P20         ' P16-P20 high in case the next thing is a group change and P16/P17 are low and hence upset the ram chip
    set161_ret              ret
    
    set161and373            call    #set161                 ' pass ramaddr
                            mov     latchvalue,#%11111101   ' group 2
                            call    #set373                 ' change to group 2
    set161and373_ret        ret                                                
    
    The rest shouldn't matter.... This is supposed to transfer data to and from hub, external SRAM and display. Transfers are in words, p0-15, p16-20 are control. Group1 is address-bus, parallel load, Group2 is data transfer. Counters are 74hc161... Thoughts?
Sign In or Register to comment.