Shop OBEX P1 Docs P2 Docs Learn Events
Pre-Emptive Multi-Threading / Tasking Demo - Can Switch at 1MHz ! — Parallax Forums

Pre-Emptive Multi-Threading / Tasking Demo - Can Switch at 1MHz !

78rpm78rpm Posts: 264
edited 2015-10-31 22:48 in Propeller 2
*** EDITED 30th Nov Oct *** See bottom of post
*** EDITED 30th Nov Oct Again*** See bottom of post
*** EDITED 31st Oct *** See bottom of post + new files

This is a small demonstration of multi-threading / tasking on a Propeller 2.
Thanks to Seairth and Electrodude for some assistance.
Test program to demonstrate a simple pre-emptive multi-threading / tasking.
by 78rpm

Output can be directed to the OUTA or the OUTB ports, change the DIRECTION_OUT
register and the TASKS_OUT in the CON section below.

Changing SWITCHING_FREQUENCY allows the interrupt frequency to be defined.
Currently set to 1KHz. Even a switching frequency of 1MHz is possible with this 
small demonstration.

There are four task which have conditional execution directed by the state of
the Z (Zero) flag and the C (Carry) flag, of a WAITX period and SETB in the
output register.

  Task    Z    C
    0     1    1
    1     1    0
    2     0    1
    3     0    0

This task switching demonstration is written not to emphasise the conditional 
execution of instructions, but to prove that the Z and C flags are preserved
in the return address save at special function register IRET1. The tasks will
only exercise their outputs with the specific Z and C states.


***EDIT 30th Nov Oct ***

I have just realised WAITX blocks interrupts!!! :blush:

I am now in the process of recoding the WAITX to GETCT1 and CMP sequences. I will post the new version
when I have finished the edit.

***EDIT 30th Nov Oct Again***

New file attached 5KB. The first one, 4KB, is still valid if you DO NOT use the WAITX.
The new file uses software counters. The new speed tops out at a little above 900KHz for switching and the LEDs are noticably slow.

*** EDIT 31st Oct ***

The bugs have been sorted and new files are attached. The maximum switching frequency is back up to 1MHz, but the switching overhead is too much.

First file, 78rpm_task_switcher.spin is the basic framework.
Second file, 78rpm_task_switcher_sftcnt.spin has four software counters to show something useful and that the tasks do return from interrupt with their C and Z flags in the correct state.

Comments

  • cgraceycgracey Posts: 14,152
    Use CMPM, as it compares and puts the MSB of the result into C. That is what you after if you are trying to determine if some GETCT target has passed.

    On the Prop2-Hot, there was a 'PASSCNT D/#' instruction which would loop to itself until some GETCNT target was passed. That is what we need in your case, to make code as simple as possible.
  • cgracey wrote: »
    Use CMPM, as it compares and puts the MSB of the result into C. That is what you after if you are trying to determine if some GETCT target has passed.

    On the Prop2-Hot, there was a 'PASSCNT D/#' instruction which would loop to itself until some GETCNT target was passed. That is what we need in your case, to make code as simple as possible.
    Thank you for the help. I'll have an attempt with that instruction later or tomorrow.

    For the moment I have substituted software counters to keep the demonstration code small.
    Test program to demonstrate a simple pre-emptive multi-threading / tasking.
    By  : 78rpm
    Name: 78rpm_task_switcher.spin
    
    This program can task switch at 900KHz on 50MHz FPGA, but at that speed little
    time is available for processing and led blinking runs slow. LEDs b3:b0 on the
    output port are inverted every time a software counter reaches 0.
    
    Output can be directed to the OUTA or the OUTB ports, change the DIRECTION_OUT
    register and the TASKS_OUT in the CON section below. 
    
    Changing SWITCHING_FREQUENCY allows the interrupt frequency to be defined.
    Currently set to 1KHz. Even a switching frequency of 900KHz is possible with this 
    small demonstration.
    
    There are four task which have conditional branching in a tight loop when the 
    tasks Z (Zero) flag and C (Carry) flag are not in the correct state.
    
      Task    Z    C
        0     1    1
        1     1    0
        2     0    1
        3     0    0
    
    This task switching demonstration is written not to emphasise the conditional 
    execution of instructions, but to prove that the Z and C flags are preserved
    in the return address save at special function register IRET1. The tasks will
    only exercise their outputs with the specific Z and C states.
    

    See top post for new file.
  • 78rpm wrote: »
    *** EDITED 30th Nov *** See bottom of post
    *** EDITED 30th Nov Again*** See bottom of post

    This is a small demonstration of multi-threading / tasking on a Propeller 2.
    Thanks to Seairth and Electrodude for some assistance.
    Test program to demonstrate a simple pre-emptive multi-threading / tasking.
    by 78rpm
    
    Output can be directed to the OUTA or the OUTB ports, change the DIRECTION_OUT
    register and the TASKS_OUT in the CON section below.
    
    Changing SWITCHING_FREQUENCY allows the interrupt frequency to be defined.
    Currently set to 1KHz. Even a switching frequency of 1MHz is possible with this 
    small demonstration.
    
    There are four task which have conditional execution directed by the state of
    the Z (Zero) flag and the C (Carry) flag, of a WAITX period and SETB in the
    output register.
    
      Task    Z    C
        0     1    1
        1     1    0
        2     0    1
        3     0    0
    
    This task switching demonstration is written not to emphasise the conditional 
    execution of instructions, but to prove that the Z and C flags are preserved
    in the return address save at special function register IRET1. The tasks will
    only exercise their outputs with the specific Z and C states.
    


    ***EDIT 30th Nov ***

    I have just realised WAITX blocks interrupts!!! :blush:

    I am now in the process of recoding the WAITX to GETCT1 and CMP sequences. I will post the new version
    when I have finished the edit.

    ***EDIT 30th Nov Again***

    New file attached 5KB. The first one, 4KB, is still valid if you DO NOT use the WAITX.
    The new file uses software counters. The new speed tops out at a little above 900KHz for switching and the LEDs are noticably slow.
    After a short while task_0 stops updating. After quite a while, task_2 stops updating. Both tasks rely on Carry being set. Tasks 1 and 3 are still updating after a long time, these require Carry clear.
  • 78rpm wrote: »
    78rpm wrote: »
    *** EDITED 30th Nov *** See bottom of post
    *** EDITED 30th Nov Again*** See bottom of post

    This is a small demonstration of multi-threading / tasking on a Propeller 2.
    Thanks to Seairth and Electrodude for some assistance.
    Test program to demonstrate a simple pre-emptive multi-threading / tasking.
    by 78rpm
    
    Output can be directed to the OUTA or the OUTB ports, change the DIRECTION_OUT
    register and the TASKS_OUT in the CON section below.
    
    Changing SWITCHING_FREQUENCY allows the interrupt frequency to be defined.
    Currently set to 1KHz. Even a switching frequency of 1MHz is possible with this 
    small demonstration.
    
    There are four task which have conditional execution directed by the state of
    the Z (Zero) flag and the C (Carry) flag, of a WAITX period and SETB in the
    output register.
    
      Task    Z    C
        0     1    1
        1     1    0
        2     0    1
        3     0    0
    
    This task switching demonstration is written not to emphasise the conditional 
    execution of instructions, but to prove that the Z and C flags are preserved
    in the return address save at special function register IRET1. The tasks will
    only exercise their outputs with the specific Z and C states.
    


    ***EDIT 30th Nov ***

    I have just realised WAITX blocks interrupts!!! :blush:

    I am now in the process of recoding the WAITX to GETCT1 and CMP sequences. I will post the new version
    when I have finished the edit.

    ***EDIT 30th Nov Again***

    New file attached 5KB. The first one, 4KB, is still valid if you DO NOT use the WAITX.
    The new file uses software counters. The new speed tops out at a little above 900KHz for switching and the LEDs are noticably slow.
    After a short while task_0 stops updating. After quite a while, task_2 stops updating. Both tasks rely on Carry being set. Tasks 1 and 3 are still updating after a long time, these require Carry clear.

    For some reason I though it was November! I will change that in my top post.

    Additionally, the bugs have been ironed out with input from Seairth and Chip. The switching speed is now back up to a maximum of 1MHz, but it is really too slow to use.

    New files are posted in the top post.
Sign In or Register to comment.