www.prllxsemiondutor.om sles@prllxsemiondutor.om support@prllxsemiondutor.om phone: 916 632 4664 fx:916 624 8003 pplition Note oroutines in Propeller ssemly Lnguge strt: The multiore P8X32 does not require trditionl interrupts to mnge multiple proesses simultneously; yet multi-tsking in single ore is still hndy option. oroutines in Propeller ssemly lnguge n support pir of independent tsks in single ore (og) through the use of the JMPRET instrution. Introdution The P8X32 Propeller hs eight independent proessors, lled ogs, eh of whih n support multiple oopertive tsks. The simplest wy to implement multitsking in Propeller ssemly lnguge (PSM) for two tsks is to write the tsks s oroutines. oroutine Priniple Two progrms re sid to e oroutines when they tke turns exeuting in suh wy tht, when eh gets its turn, it resumes from the point where it left off. In the Propeller, oroutines tke turns oopertively, rther thn y mens of n interrupt, so eh must yield its turn to the other in plnned ping-pong fshion. Figure 1 illustrtes the priniple s flow hrt. Figure 1: oroutines Flowhrt Strt Flow of ontrol Result oroutine 1 oroutine 2... oroutines in Propeller ssemly Lnguge v1.0 1 of 5
Prllx Semiondutor Here, exeution ounes k nd forth etween oroutine 1 nd oroutine 2 in suh wy tht eh gets smll slie of time efore yielding to its omplement. The result is n interleved exeution tht n give the pperne tht eh is operting independently in rel time. oroutines re often employed in progrms tht do oth input nd output independently, suh s those tht perform full-duplex synhronous seril I/O, or tht monitor n enoder nd ontrol motor. Propeller oroutine Implementtion To understnd how oroutines work in the Propeller, it is neessry to hve feel for the Propeller s exeution sequene. Propeller instrutions re proessed in four steps: 1. Instrution feth 2. Write result of previous instrution 3. Soure opernd feth 4. estintion opernd feth This mens tht if the next instrution is the destintion of the urrent instrution s result, the old ontents of tht instrution slot will e red first efore the new vlue is written. Suh ehvior is wht mkes Propeller oroutine swithing oth simple nd fst. onsider the jmpret d,s instrution. This instrution trnsfers ontrol to ddress s nd stuffs the ddress of the instrution following the jmpret into the soure field of the instrution t ddress d. The ltter ddress most ommonly holds jmp instrution, so tht jmpret n e used to effet suroutine lls. In ft, the ll instrution is just jmpret in disguise. For exmple, this: '-------[ Min Progrm ]----------------------------------------- jmpret sur_ret,#sur 'll sur. '-------[ Suroutine ]------------------------------------------- sur 'Suroutine entry point. sur_ret jmp #0-0 'Return to ller. is extly equivlent to this: '-------[ Min Progrm ]----------------------------------------- ll #sur 'll sur. '-------[ Suroutine ]------------------------------------------- sur 'Suroutine entry point. sur_ret ret 'Return to ller. oroutines in Propeller ssemly Lnguge v1.0 2 of 5
Prllx Semiondutor Now, onsider wht would hppen in the sitution elow, where the trget of the ll nd the return sttement reside t the sme lotion. ll :nxt #swp swp swp_ret ret In this exmple, the following steps tke ple t the ll: 1. The proessor reds the instrution t swp (i.e. ret = jmp #ret_ddr). 2. The proessor stuffs the ddress of :nxt into the soure field of swp, repling ret_ddr. 3. The jmp #ret_ddr instrution exeutes. The next time ll issues to swp, exeution will trnsfer to :nxt, nd the ller s return ddress will get stuffed into swp s soure field. nd so it goes, k-nd-forth. This then is the essene of Propeller oroutine swithing. Progrm Exmple In this exmple progrm, two oroutines ontrol two seprte LEs, linking them t different rtes. (This progrm is designed to run on the Propeller emo ord [1] ut n e modified for other pltforms simply y hnging vlues in the ON setion s pproprite.) ON 'Spin setup ode _lkmode _xinfreq = xtl1 + pll16x = 5_000_000 PING_LE = 16 PONG_LE = 17 PU Strt ping_period := lkfreq * 11 / 16 pong_period := lkfreq * 13 / 16 'Set periods for oth LEs. ognew(@pingpong, 0) T 'PSM ode pingpong or dir,ping_msk 'Enle PING_LE output. or dir,pong_msk 'Enle PING_LE output. mov ping_time,nt 'Initilize timers. mov pong_time,ping_time '-------[ ping oroutine ]------------------------------------------------------- ping ll #swp 'Give the pong oroutine hne. 'Pong oroutine returns here. mov,nt 'Is it time to hnge stte? su,ping_time mp,ping_period w if_ jmp #ping ' No: Keep heking. dd ping_time,ping_period ' Yes: dd to get the next time. or out,ping_msk ' Turn LE on. oroutines in Propeller ssemly Lnguge v1.0 3 of 5
Prllx Semiondutor ping_on ll #swp 'Give the pong oroutine hne. 'Pong oroutine returns here. mov,nt 'Is it time gin to hnge stte? su,ping_time mp,ping_period w if_ jmp #ping_on dd ping_time,ping_period ' Yes: dd to get the next time. ndn out,ping_msk ' Turn LE off. jmp #ping 'Loop k '-------[ pong oroutine ]------------------------------------------------------- pong mov,nt 'Is it time to hnge stte? su,pong_time mp,pong_period w if_n dd pong_time,pong_period ' Yes: dd to get next time. if_n xor out,pong_msk ' Invert output pin. ll #swp 'Give the ping oroutine hne. jmp #pong 'Loop k '-------[ oroutine swpper ]----------------------------------------------------- swp swp_ret jmp #pong 'Initilize swp to point to pong. '-------[ vriles ]------------------------------------------------------------- ping_msk long 1 << PING_LE pong_msk long 1 << PONG_LE ping_period long 0-0 pong_period long 0-0 ping_time res 1 pong_time res 1 res 1 In this progrm, ping nd pong do the sme things with their respetive LEs. However, ping expliitly turns its LE on nd off, while pong simply toggles its LE. The ping oroutine, with two distint lls to swp, illustrtes tht exeution piks up from where it left off when the other oroutine returns. Here re ouple other points worth noting: 1. oth ping nd pong use the shred vrile. This is oky, so long s the vlue of shred vrile does not hve to spn ll to swp, euse the omplementry oroutine might loer it. 2. Never rely upon the sttes of the nd z flgs ross lls to swp. Without extremely reful progrmming, the omplementry oroutine will lmost ertinly loer them. If it should e neessry to retin the zero nd/or rry flg stte(s) ross ll to swp, use the flg sve nd restore instrutions illustrted elow. (The $ s in restore_z nd restore_ refer to the urrent instrution ddress.) 'Sve Z nd in their restore instrutions: sve_z muxnz restore_z,#1 'Sve NZ it in restore_z, it 0. ll #swp 'Swp to lternte oroutine. restore_z test $,#0-0 wz 'Test to restore Z flg. oroutines in Propeller ssemly Lnguge v1.0 4 of 5
Prllx Semiondutor sve_ mux restore_,#1 'Sve it in restore_, it 0. ll #swp 'Swp to lternte oroutine. restore_ test $,#0-0 w 'Test to restore flg. 'Sve Z nd in n externl long: sve_z muxnz flgs,#%10 'Sve NZ it in flgs, it 1. sve_ mux flgs,#%01 'Sve it in flgs, it 0. ll #swp 'Swp to lternte oroutine. restore_z test flgs,#%10 wz 'Restore Z from flgs, it 1. restore_ test flgs,#%01 w 'Restore from flgs, it 0. 'or restore_z shr flgs,#1 wz,w,nr 'Restore Z nd from flgs, its 1 nd 0. 'its 31..2 of flgs must e zero. flgs long 0 Resoures ownlod the oroutine exmple ode from www.prllxsemiondutor.om/n014. Referenes 1. Propeller emo ord; Prllx #32100, www.prllx.om Revision History Version 1.0: originl doument. Prllx, In., d Prllx Semiondutor, mkes no wrrnty, representtion or gurntee regrding the suitility of its produts for ny prtiulr purpose, nor does Prllx, In., d Prllx Semiondutor, ssume ny liility rising out of the pplition or use of ny produt, nd speifilly dislims ny nd ll liility, inluding without limittion onsequentil or inidentl dmges even if Prllx, In., d Prllx Semiondutor, hs een dvised of the possiility of suh dmges. Reprodution of this doument in whole or in prt is prohiited without the prior written onsent of Prllx, In., d Prllx Semiondutor. opyright 2011 Prllx, In. d Prllx Semiondutor. ll rights re reserved. Propeller nd Prllx Semiondutor re trdemrks of Prllx, In. oroutines in Propeller ssemly Lnguge v1.0 5 of 5