home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 2 / AUCD2.iso / program / vista.arc / s / tasm
Text File  |  1996-01-17  |  22KB  |  778 lines

  1. ; **************************************************************************
  2. ;                     Copyright 1995 David Allison
  3. ;
  4. ;             VV    VV    IIIIII     SSSSS     TTTTTT       AA
  5. ;             VV    VV      II      SS           TT       AA  AA
  6. ;             VV    VV      II        SSSS       TT      AA    AA
  7. ;              VV  VV       II           SS      TT      AAAAAAAA
  8. ;                VV       IIIIII     SSSS        TT      AA    AA
  9. ;
  10. ;                           C++ WIMP CLASS LIBRARY
  11. ;                                for RISC OS
  12. ; **************************************************************************
  13. ;
  14. ;               E A S Y   C + +   U S E R  L I C E N C E
  15. ;               ----------------------------------------
  16. ;
  17. ; This library is copyright.  It is supplied as part of Easy C++ as
  18. ; source.  The library may NOT be copied nor placed in any publically
  19. ; accessible place (eg a Bulletin Board).  You may not sell the
  20. ; library for profit, but you may sell products which use it providing
  21. ; those products are presented as executable code and are not
  22. ; libraries themselves.  The library is supplied without any
  23. ; warranty and the copyright owner cannot be held responsible for
  24. ; damage resulting from failure of any part of this library.
  25. ;
  26. ; *************************************************************************
  27.  
  28. ;
  29. ; assembly language for thread package
  30. ;
  31.  
  32.     AREA |Threads$code|, CODE, READONLY
  33.  
  34. XEasyDebug_RegisterThreads  EQU 0x8304a + 0x20000
  35. XEasyDebug_YieldThread      EQU 0x8304b + 0x20000
  36.  
  37. R0        RN  0
  38. R1        RN  1
  39. R2        RN  2
  40. R3        RN  3
  41. R4        RN  4
  42. R5        RN  5
  43. R6        RN  6
  44. R7        RN  7
  45. R8        RN  8
  46. R9        RN  9
  47. R10       RN  10
  48. R11       RN  11
  49. R12       RN  12
  50. R13       RN  13
  51. R14       RN  14
  52. R15       RN  15
  53. PC        RN  15
  54. link      RN  14
  55. SP        RN  13
  56. vp        RN  12             ; variable pointer
  57.  
  58.  
  59. F0        FN 0
  60. F1        FN 1
  61. F2        FN 2
  62. F3        FN 3
  63. F4        FN 4
  64. F5        FN 5
  65. F6        FN 6
  66. F7        FN 7
  67.  
  68. ; SWI numbers
  69.  
  70. OS_WriteC     EQU 0
  71. OS_WriteS     EQU 1
  72. OS_WriteO     EQU 2
  73. OS_Newline    EQU 3
  74. OS_ReadC      EQU 4
  75. OS_CLI        EQU 5
  76. OS_Byte       EQU 6
  77. OS_Word       EQU 7
  78. OS_File       EQU 8
  79. OS_Args       EQU 9
  80. OS_Bget       EQU 10
  81. OS_Bput       EQU 11
  82. OS_Gbpb       EQU 12
  83. OS_Find       EQU 13
  84. OS_ReadLine   EQU 14
  85. OS_Control    EQU 15
  86. OS_GetEnv     EQU 16
  87. OS_Exit       EQU 17
  88. OS_SetEnv     EQU 18
  89. OS_IntOn      EQU 19
  90. OS_IntOff     EQU 20
  91. OS_CallBack   EQU 21
  92. OS_EnterOS    EQU 22
  93. OS_BreakPt    EQU 23
  94. OS_BreakCntrl EQU 24
  95. OS_UnusedSWI  EQU 25
  96. OS_UpdateMEMC EQU 26
  97. OS_SetCallBack    EQU 27
  98. OS_Mouse          EQU 28
  99. OS_Heap           EQU 29
  100. OS_Module         EQU 30
  101. OS_Claim          EQU 31
  102. OS_Release        EQU 32
  103. OS_ReadUnsigned   EQU 33
  104. OS_GenerateEvent  EQU 34
  105. OS_ReadVarVal     EQU 35
  106. OS_SetVarVal      EQU 36
  107. OS_GSInit         EQU 37
  108. OS_GSRead         EQU 38
  109. OS_GSTrans        EQU 39
  110. OS_BinaryToDecimal    EQU 40
  111. OS_FSControl          EQU 41
  112. OS_ChangeDynamicArea  EQU 42
  113. OS_GenerateError      EQU 43
  114. OS_ReadEscapeState    EQU 44
  115. OS_EvaluateExpression EQU 45
  116. OS_SpriteOp           EQU 46
  117. OS_ReadPalette        EQU 47
  118. OS_ServiceCall        EQU 48
  119. OS_ReadVduVariables   EQU 49
  120. OS_ReadPoint          EQU 50
  121. OS_UpCall             EQU 51
  122. OS_CallAVector        EQU 52
  123. OS_ReadModeVariable   EQU 53
  124. OS_RemoveCursors      EQU 54
  125. OS_RestoreCursors     EQU 55
  126. OS_SWINumberToString  EQU 56
  127. OS_SWINumberFromString    EQU 57
  128. OS_ValidateAddress        EQU 58
  129. OS_CallAfter              EQU 59
  130. OS_CallEvery              EQU 60
  131. OS_RemoveTickerEvent      EQU 61
  132. OS_InstallKeyHandler      EQU 62
  133. OS_CheckModeValid         EQU 63
  134. OS_ChangeEnvironment      EQU 64
  135. OS_ClaimScreenMemory      EQU 65
  136. OS_ReadMonotonicTime      EQU 66
  137. OS_PrettyPrint  EQU &44
  138. OS_Plot         EQU &45
  139. OS_WriteN       EQU &46
  140. OS_HeapSort     EQU &4F
  141. OS_CRC          EQU &5B
  142.  
  143. OS_ConvertStandardDateAndTime EQU 192
  144. OS_ConvertDateAndTime         EQU 193
  145. OS_ConvertHex1                EQU 208
  146. OS_ConvertHex2                EQU 209
  147. OS_ConvertHex4                EQU 210
  148. OS_ConvertHex6                EQU 211
  149. OS_ConvertHex8                EQU 212
  150. OS_ConvertCardinal1           EQU 213
  151. OS_ConvertCardinal2           EQU 214
  152. OS_ConvertCardinal3           EQU 215
  153. OS_ConvertCardinal4           EQU 216
  154. OS_ConvertInteger1            EQU 217
  155. OS_ConvertInteger2            EQU 218
  156. OS_ConvertInteger3            EQU 219
  157. OS_ConvertInteger4            EQU 220
  158. OS_ConvertBinary1             EQU 221
  159. OS_ConvertBinary2             EQU 222
  160. OS_ConvertBinary3             EQU 223
  161. OS_ConvertBinary4             EQU 224
  162. OS_ConvertSpacedCardinal1     EQU 225
  163. OS_ConvertSpacedCardinal2     EQU 226
  164. OS_ConvertSpacedCardinal3     EQU 227
  165. OS_ConvertSpacedCardinal4     EQU 228
  166. OS_ConvertSpacedInteger1      EQU 229
  167. OS_ConvertSpacedInteger2      EQU 230
  168. OS_ConvertSpacedInteger3      EQU 231
  169. OS_ConvertSpacedInteger4      EQU 232
  170. OS_ConvertFixedNetStation     EQU 233
  171. OS_ConvertNetStation          EQU 234
  172. OS_WriteI                     EQU 256
  173.  
  174.  
  175. XOS_WriteC     EQU 2_100000000000000000+0
  176. XOS_WriteS     EQU 2_100000000000000000+1
  177. XOS_WriteO     EQU 2_100000000000000000+2
  178. XOS_Newline    EQU 2_100000000000000000+3
  179. XOS_ReadC      EQU 2_100000000000000000+4
  180. XOS_CLI        EQU 2_100000000000000000+5
  181. XOS_Byte       EQU 2_100000000000000000+6
  182. XOS_Word       EQU 2_100000000000000000+7
  183. XOS_File       EQU 2_100000000000000000+8
  184. XOS_Args       EQU 2_100000000000000000+9
  185. XOS_Bget       EQU 2_100000000000000000+10
  186. XOS_Bput       EQU 2_100000000000000000+11
  187. XOS_Gbpb       EQU 2_100000000000000000+12
  188. XOS_Find       EQU 2_100000000000000000+13
  189. XOS_ReadLine   EQU 2_100000000000000000+14
  190. XOS_Control    EQU 2_100000000000000000+15
  191. XOS_GetEnv     EQU 2_100000000000000000+16
  192. XOS_Exit       EQU 2_100000000000000000+17
  193. XOS_SetEnv     EQU 2_100000000000000000+18
  194. XOS_IntOn      EQU 2_100000000000000000+19
  195. XOS_IntOff     EQU 2_100000000000000000+20
  196. XOS_CallBack   EQU 2_100000000000000000+21
  197. XOS_EnterOS    EQU 2_100000000000000000+22
  198. XOS_BreakPt    EQU 2_100000000000000000+23
  199. XOS_BreakCntrl EQU 2_100000000000000000+24
  200. XOS_UnusedSWI  EQU 2_100000000000000000+25
  201. XOS_UpdateMEMC EQU 2_100000000000000000+26
  202. XOS_SetCallBack    EQU 2_100000000000000000+27
  203. XOS_Mouse          EQU 2_100000000000000000+28
  204. XOS_Heap           EQU 2_100000000000000000+29
  205. XOS_Module         EQU 2_100000000000000000+30
  206. XOS_Claim          EQU 2_100000000000000000+31
  207. XOS_Release        EQU 2_100000000000000000+32
  208. XOS_ReadUnsigned   EQU 2_100000000000000000+33
  209. XOS_GenerateEvent  EQU 2_100000000000000000+34
  210. XOS_ReadVarVal     EQU 2_100000000000000000+35
  211. XOS_SetVarVal      EQU 2_100000000000000000+36
  212. XOS_GSInit         EQU 2_100000000000000000+37
  213. XOS_GSRead         EQU 2_100000000000000000+38
  214. XOS_GSTrans        EQU 2_100000000000000000+39
  215. XOS_BinaryToDecimal    EQU 2_100000000000000000+40
  216. XOS_FSControl          EQU 2_100000000000000000+41
  217. XOS_ChangeDynamicArea  EQU 2_100000000000000000+42
  218. XOS_GenerateError      EQU 2_100000000000000000+43
  219. XOS_ReadEscapeState    EQU 2_100000000000000000+44
  220. XOS_EvaluateExpression EQU 2_100000000000000000+45
  221. XOS_SpriteOp           EQU 2_100000000000000000+46
  222. XOS_ReadPalette        EQU 2_100000000000000000+47
  223. XOS_ServiceCall        EQU 2_100000000000000000+48
  224. XOS_ReadVduVariables   EQU 2_100000000000000000+49
  225. XOS_ReadPoint          EQU 2_100000000000000000+50
  226. XOS_UpCall             EQU 2_100000000000000000+51
  227. XOS_CallAVector        EQU 2_100000000000000000+52
  228. XOS_ReadModeVariable   EQU 2_100000000000000000+53
  229. XOS_RemoveCursors      EQU 2_100000000000000000+54
  230. XOS_RestoreCursors     EQU 2_100000000000000000+55
  231. XOS_SWINumberToString  EQU 2_100000000000000000+56
  232. XOS_SWINumberFromString    EQU 2_100000000000000000+57
  233. XOS_ValidateAddress        EQU 2_100000000000000000+58
  234. XOS_CallAfter              EQU 2_100000000000000000+59
  235. XOS_CallEvery              EQU 2_100000000000000000+60
  236. XOS_RemoveTickerEvent      EQU 2_100000000000000000+61
  237. XOS_InstallKeyHandler      EQU 2_100000000000000000+62
  238. XOS_CheckModeValid         EQU 2_100000000000000000+63
  239. XOS_ChangeEnvironment      EQU 2_100000000000000000+64
  240. XOS_ClaimScreenMemory      EQU 2_100000000000000000+65
  241. XOS_ReadMonotonicTime      EQU 2_100000000000000000+66
  242. XOS_PrettyPrint  EQU 2_100000000000000000+&44
  243. XOS_Plot         EQU 2_100000000000000000+&45
  244. XOS_WriteN       EQU 2_100000000000000000+&46
  245.  
  246. XOS_ConvertStandardDateAndTime EQU 2_100000000000000000+192
  247. XOS_ConvertDateAndTime         EQU 2_100000000000000000+193
  248. XOS_ConvertHex1                EQU 2_100000000000000000+208
  249. XOS_ConvertHex2                EQU 2_100000000000000000+209
  250. XOS_ConvertHex4                EQU 2_100000000000000000+210
  251. XOS_ConvertHex6                EQU 2_100000000000000000+211
  252. XOS_ConvertHex8                EQU 2_100000000000000000+212
  253. XOS_ConvertCardinal1           EQU 2_100000000000000000+213
  254. XOS_ConvertCardinal2           EQU 2_100000000000000000+214
  255. XOS_ConvertCardinal3           EQU 2_100000000000000000+215
  256. XOS_ConvertCardinal4           EQU 2_100000000000000000+216
  257. XOS_ConvertInteger1            EQU 2_100000000000000000+217
  258. XOS_ConvertInteger2            EQU 2_100000000000000000+218
  259. XOS_ConvertInteger3            EQU 2_100000000000000000+219
  260. XOS_ConvertInteger4            EQU 2_100000000000000000+220
  261. XOS_ConvertBinary1             EQU 2_100000000000000000+221
  262. XOS_ConvertBinary2             EQU 2_100000000000000000+222
  263. XOS_ConvertBinary3             EQU 2_100000000000000000+223
  264. XOS_ConvertBinary4             EQU 2_100000000000000000+224
  265. XOS_ConvertSpacedCardinal1     EQU 2_100000000000000000+225
  266. XOS_ConvertSpacedCardinal2     EQU 2_100000000000000000+226
  267. XOS_ConvertSpacedCardinal3     EQU 2_100000000000000000+227
  268. XOS_ConvertSpacedCardinal4     EQU 2_100000000000000000+228
  269. XOS_ConvertSpacedInteger1      EQU 2_100000000000000000+229
  270. XOS_ConvertSpacedInteger2      EQU 2_100000000000000000+230
  271. XOS_ConvertSpacedInteger3      EQU 2_100000000000000000+231
  272. XOS_ConvertSpacedInteger4      EQU 2_100000000000000000+232
  273. XOS_ConvertFixedNetStation     EQU 2_100000000000000000+233
  274. XOS_ConvertNetStation          EQU 2_100000000000000000+234
  275. XOS_WriteI                     EQU 2_100000000000000000+256
  276.  
  277. wimp_Initialise                EQU &400c0
  278. wimp_Poll                      EQU &400c7
  279. wimp_Closedown                 EQU &400dd
  280. wimp_ReportError               EQU &400df
  281. wimp_SendMessage               EQU &400e7
  282. wimp_BlockCopy                 EQU &400eb
  283. wimp_SlotSize                  EQU &400ec
  284. wimp_ClaimFreeMemory           EQU &400ee
  285. wimp_CommandWindow             EQU &400ef
  286. wimp_TransferBlock             EQU &400f1
  287. wimp_ReadSysInfo               EQU &400f2
  288. wimp_StartTask                 EQU &400de
  289.  
  290. Xwimp_Initialise                EQU 2_100000000000000000+&400c0
  291. Xwimp_Poll                      EQU 2_100000000000000000+&400c7
  292. Xwimp_Closedown                 EQU 2_100000000000000000+&400dd
  293. Xwimp_ReportError               EQU 2_100000000000000000+&400df
  294. Xwimp_SendMessage               EQU 2_100000000000000000+&400e7
  295. Xwimp_BlockCopy                 EQU 2_100000000000000000+&400eb
  296. Xwimp_SlotSize                  EQU 2_100000000000000000+&400ec
  297. Xwimp_ClaimFreeMemory           EQU 2_100000000000000000+&400ee
  298. Xwimp_CommandWindow             EQU 2_100000000000000000+&400ef
  299. Xwimp_TransferBlock             EQU 2_100000000000000000+&400f1
  300. Xwimp_ReadSysInfo               EQU 2_100000000000000000+&400f2
  301. Xwimp_StartTask                 EQU 2_100000000000000000+&400de
  302.  
  303.  
  304. DDEUtils_ThrowbackStart        EQU &42587
  305. DDEUtils_ThrowbackSend         EQU &42588
  306. DDEUtils_ThrowbackEnd          EQU &42589
  307.  
  308. XDDEUtils_ThrowbackStart        EQU 2_100000000000000000+&42587
  309. XDDEUtils_ThrowbackSend         EQU 2_100000000000000000+&42588
  310. XDDEUtils_ThrowbackEnd          EQU 2_100000000000000000+&42589
  311.  
  312. Throwback_ReasonProcessing EQU 0
  313. Throwback_ReasonErrorDetails EQU 1
  314. Throwback_ReasonInfoDetails EQU 2
  315.  
  316.  IMPORT context_switch__13ThreadManagerFv
  317.  IMPORT start2__6ThreadFv
  318.  
  319. ;
  320. ; call the start2 member function for a thread.  This actually starts the thread
  321. ; running
  322. ;
  323.  
  324. ; entry:
  325. ;   R0 = thread
  326.  
  327. thread_call_start2
  328.         EXPORT thread_call_start2
  329.         B start2__6ThreadFv
  330.  
  331. thread_get_time
  332.         EXPORT thread_get_time
  333.         STMFD SP!,{link}                  ; may be called in SVC mode
  334.         SWI OS_ReadMonotonicTime
  335.         LDMFD SP!,{PC}^
  336.  
  337.  
  338. Int_bit EQU (1 << 27)
  339.  
  340. USR_mode EQU 0
  341. FIQ_mode EQU 1
  342. IRQ_mode EQU 2
  343. SVC_mode EQU 3
  344.  
  345.  
  346. ;
  347. ; claim the callback vector
  348. ;
  349.  
  350. ; entry:
  351. ;   R0 = thread manager address.  This is passed to the callback in R12
  352.  
  353. thread_claim_callback
  354.     EXPORT thread_claim_callback
  355.         STMFD SP!,{R0-R3,link}
  356.         MOV R2,R0
  357.     MOV R0,#7
  358.     ADR R1,callback
  359.     LDR R3,thread_callback_regs
  360.     SWI XOS_ChangeEnvironment
  361.     STR R1,old_callback
  362.     STR R2,old_callback_r12
  363.     STR R3,old_callback_regs
  364.     LDMFD SP!,{R0-R3,PC}^
  365.  
  366. old_callback
  367.     DCD 0
  368. old_callback_r12
  369.     DCD 0
  370. old_callback_regs
  371.     DCD 0
  372.  
  373.  
  374. ;
  375. ; release the callback vector
  376. ;
  377.  
  378.  
  379. thread_release_callback
  380.     EXPORT thread_release_callback
  381.         STMFD SP!,{R0-R3,link}
  382.     MOV R0,#7
  383.     LDR R1,old_callback
  384.     LDR R2,old_callback_r12
  385.     LDR R3,old_callback_regs
  386.     SWI XOS_ChangeEnvironment
  387.     LDMFD SP!,{R0-R3,PC}^
  388.  
  389. ;
  390. ; start a ticker which will set the callback flag every clock tick
  391. ;
  392.  
  393. ; entry:
  394. ;   R0 = address of thread manager
  395.  
  396. thread_start_ticker
  397.     EXPORT thread_start_ticker
  398.         LDR R2,alien_control
  399.         CMP R2,#0
  400.         MOVNES PC,link
  401.         STMFD SP!,{R0-R2,link}
  402.         MOV R2,R0
  403.     MOV R0,#1
  404.     STR R0,thread_worksemaphore
  405.     LDR R0,thread_poll_rate
  406.     ADR R1,call_every
  407.     SWI XOS_CallEvery
  408.     LDMFD SP!,{R0-R2,PC}^
  409.  
  410. ;
  411. ; stop the ticker running
  412. ;
  413.  
  414. ; entry:
  415. ;   R0 = thread manager
  416.  
  417. thread_stop_ticker
  418.     EXPORT thread_stop_ticker
  419.         LDR R2,alien_control
  420.         CMP R2,#0
  421.         MOVNES PC,link
  422.         STMFD SP!,{R0-R1,link}
  423.         MOV R1,R0
  424.     ADR R0,call_every
  425.     SWI XOS_RemoveTickerEvent
  426.     LDMFD SP!,{R0-R1,PC}^
  427.  
  428. ;
  429. ; init the thread system for use with a debugger
  430. ;
  431.  
  432. ; entry:
  433. ;   R0 = thread manager
  434.  
  435. thread_init
  436.         EXPORT thread_init
  437.         STMFD SP!,{R0-R8,link}
  438.         MOV R5,R0
  439.         BL claim_heap_modifiers
  440.         ADR R0,thread_worksemaphore
  441.         LDR R1,ctx_swtch
  442.         ADR R2,thread_callback_regs
  443.         LDR R3,thread_poll_rate
  444.     ADR R4,thread_system_running
  445.     SWI XEasyDebug_RegisterThreads
  446.     MOVVS R1,#0                           ; no SWI: not under control
  447.     STRVS R1,alien_control
  448.     LDMVSFD SP!,{R0-R8,PC}^
  449.     CMP R0,#2                             ; module running?
  450.     MOVEQ R0,#1                           ; yes, under control
  451.     MOVNE R0,#0                           ; no, we're on our own
  452.     STR R0,alien_control
  453.     LDMFD SP!,{R0-R8,PC}^
  454.  
  455. ; this variable tells us whether we are being controlled by some outside influence
  456. ; EasyDebug controls the threads for us if so
  457.  
  458. alien_control
  459.         DCD 0
  460.  
  461. ctx_swtch
  462.         DCD context_switch__13ThreadManagerFv
  463.  
  464. thread_system_running
  465.     EXPORT thread_system_running
  466.         DCD 0
  467.  
  468. thread_worksemaphore          ; flag for callbacks
  469.         EXPORT thread_worksemaphore
  470.     DCD 0
  471.  
  472. thread_callback_regs          ; current register save area
  473.     EXPORT thread_callback_regs
  474.     DCD 0
  475.  
  476. thread_poll_rate              ; quantum poll rate
  477.         EXPORT thread_poll_rate
  478.         DCD 0
  479.  
  480. ;
  481. ; disable interrupts by setting the semaphore to non zero
  482. ;
  483.  
  484. thread_disable_ints
  485.         EXPORT thread_disable_ints
  486.     MOV R0,#1
  487.     B write_ints
  488.  
  489. ;
  490. ; enable the interrupts by setting the semaphore to 0
  491.  
  492. ;
  493. thread_enable_ints
  494.     EXPORT thread_enable_ints
  495.     MOV R0,#0
  496. write_ints
  497.     STR R0,thread_worksemaphore
  498.     MOVS PC,link
  499.  
  500. int_state
  501.     LDR R0,thread_worksemaphore
  502.     MOVS PC,link
  503. ;
  504. ; the ticker calls this every clock tick
  505. ;
  506.  
  507. call_every
  508.         STMFD SP!,{R0,R1,link}
  509.     LDR R0,thread_worksemaphore
  510.     ADD R0,R0,#1
  511.     STR R0,thread_worksemaphore
  512.     CMP R0,#1
  513.     LDMNEFD SP!,{R0,R1,PC}^
  514.     MOV R1,pc                                 ; save current processor state
  515.     TEQP pc,#Int_bit+SVC_mode                  ; disable processor interrupts
  516.     MOV R0,R0
  517.     STMFD SP!,{link}
  518.     MOV R0,R12                                ; load thread manager
  519.     BL thread_claim_callback                  ; claim the callbacks
  520.     SWI XOS_SetCallBack                        ; set the OS callback flag
  521.     LDMFD SP!,{link}
  522.     TEQP R1,#0                                ; restore processor state
  523.     MOV R0,R0
  524.     LDMFD SP!,{R0,R1,PC}^
  525.  
  526. ;
  527. ; called in usr mode by a thread wishing to give up the processor
  528. ;
  529.  
  530. ; entry:
  531. ;   R0 = thread manager
  532.  
  533. thread_yield
  534.         EXPORT thread_yield
  535.         LDR R1,alien_control
  536.         CMP R1,#0
  537.         SWINE XEasyDebug_YieldThread
  538.         MOVNES PC,link
  539.     STMFD SP!,{R0-R2,link}
  540.     MOV R1,#1
  541.     STR R1,thread_worksemaphore
  542.     BL thread_claim_callback
  543.      SWI XOS_SetCallBack
  544.      MOV R0,#0
  545.      MOV R1,#1
  546.     SWI XOS_Byte                        ; force a callback
  547.     LDMFD SP!,{R0-R2,PC}^
  548.  
  549. ;
  550. ; this is called when the OS is returning from SVC mode to USR mode
  551. ;
  552.  
  553. callback
  554.         MOV R0,#1
  555.     STR R0,thread_worksemaphore         ; disable callbacks
  556.  
  557.     TEQP PC,#SVC_mode                   ; enable ints
  558.     MOV R0,R0
  559.     MOV R4,R12                         ; save thread manager
  560.         BL thread_release_callback         ; release the callback handler
  561.  
  562. ; save floating point regs in save area
  563.  
  564.         LDR R1,thread_callback_regs
  565.         ADD R1,R1,#16*4
  566.         STFE F0,[R1,#0]
  567.         STFE F1,[R1,#12]
  568.         STFE F2,[R1,#12*2]
  569.         STFE F3,[R1,#12*3]
  570.         STFE F4,[R1,#12*4]
  571.         STFE F5,[R1,#12*5]
  572.         STFE F6,[R1,#12*6]
  573.         STFE F7,[R1,#12*7]
  574.     RFS R0                             ; read floating status
  575.     STR R0,[R1,#12*8]
  576.  
  577.     TEQP PC,#USR_mode                  ; into user mode
  578.     MOV R0,R0
  579.  
  580.     MOV fp,#0                          ; no frame pointer
  581.         MOV R0,R4                          ; get the thread manager into "this" pointer
  582.         BL context_switch__13ThreadManagerFv         ; switch to another thread
  583.  
  584.         SWI OS_EnterOS                     ; back to supervisor mode
  585.         MOV R0,R0
  586.  
  587. ; now reload the registers from the save area
  588.  
  589.         LDR R1,thread_callback_regs
  590.         ADD R1,R1,#16*4
  591.         LDFE F0,[R1,#0]
  592.         LDFE F1,[R1,#12]
  593.         LDFE F2,[R1,#12*2]
  594.         LDFE F3,[R1,#12*3]
  595.         LDFE F4,[R1,#12*4]
  596.         LDFE F5,[R1,#12*5]
  597.         LDFE F6,[R1,#12*6]
  598.         LDFE F7,[R1,#12*7]
  599.     LDR R0,[R1,#12*8]
  600.     WFS R0                             ; write floating status
  601.  
  602.     MOV R0,#0
  603.     STR R0,thread_worksemaphore        ; enable the interrupts
  604.     SUB R14,R1,#16*4                   ; get address of USR mode integer registers
  605.     LDMIA R14,{R0-R14}^                ; load USR mode regs
  606.     MOV R0,R0
  607.     TEQP PC,#Int_bit+SVC_mode          ; disable ints
  608.     MOV R0,R0
  609.     LDR R14,[R14,#15*4]                ; load the old PC value
  610.     MOVS PC,R14                        ; return
  611.  
  612. ; entry:
  613. ;   R0 = save area
  614.  
  615. thread_init_save_area
  616.         EXPORT thread_init_save_area
  617.         STMFD SP!,{R0,R1,link}
  618.         ADD R1,R0,#16*4
  619.         STFE F0,[R1,#0]
  620.         STFE F1,[R1,#12]
  621.         STFE F2,[R1,#12*2]
  622.         STFE F3,[R1,#12*3]
  623.         STFE F4,[R1,#12*4]
  624.         STFE F5,[R1,#12*5]
  625.         STFE F6,[R1,#12*6]
  626.         STFE F7,[R1,#12*7]
  627.     RFS R0                             ; read floating status
  628.     STR R0,[R1,#12*8]
  629.         LDMFD SP!,{R0,R1,PC}^
  630.  
  631.  
  632. ;
  633. ; claim the functions that modify the heap.
  634. ;
  635.  
  636. ; this claims the following functions:
  637. ;   malloc
  638. ;   free
  639. ;   calloc
  640. ;   realloc
  641.  
  642. ; and makes sure that the threads are diabled on each call
  643.  
  644. claimed_malloc
  645.     DCD 0
  646.  
  647. my_malloc
  648.     STMFD SP!,{R4-R5,link}
  649.     MOV R4,R0
  650.     BL int_state
  651.     MOV R5,R0
  652.     BL thread_disable_ints
  653.     MOV R0,R4
  654.     MOV R14,PC
  655.     LDR PC,claimed_malloc
  656.     MOV R4,R0
  657.     CMP R5,#0
  658.     BLEQ thread_enable_ints
  659.     MOV R0,R4
  660.     LDMFD SP!,{R4-R5,PC}^
  661.  
  662. claimed_free
  663.     DCD 0
  664.  
  665. my_free
  666.     STMFD SP!,{R4-R5,link}
  667.     MOV R4,R0
  668.     BL int_state
  669.     MOV R5,R0
  670.     BL thread_disable_ints
  671.     MOV R0,R4
  672.     MOV R14,PC
  673.     LDR PC,claimed_free
  674.     MOV R4,R0
  675.     CMP R5,#0
  676.     BLEQ thread_enable_ints
  677.     MOV R0,R4
  678.     LDMFD SP!,{R4-R5,PC}^
  679.  
  680. claimed_calloc
  681.     DCD 0
  682.  
  683. my_calloc
  684.     STMFD SP!,{R4-R5,link}
  685.     MOV R4,R0
  686.     BL int_state
  687.     MOV R5,R0
  688.     BL thread_disable_ints
  689.     MOV R0,R4
  690.     MOV R14,PC
  691.     LDR PC,claimed_calloc
  692.     MOV R4,R0
  693.     CMP R5,#0
  694.     BLEQ thread_enable_ints
  695.     MOV R0,R4
  696.     LDMFD SP!,{R4-R5,PC}^
  697.  
  698. claimed_realloc
  699.     DCD 0
  700.  
  701. my_realloc
  702.     STMFD SP!,{R4-R5,link}
  703.     MOV R4,R0
  704.     BL int_state
  705.     MOV R5,R0
  706.     BL thread_disable_ints
  707.     MOV R0,R4
  708.     MOV R14,PC
  709.     LDR PC,claimed_realloc
  710.     MOV R4,R0
  711.     CMP R5,#0
  712.     BLEQ thread_enable_ints
  713.     MOV R0,R4
  714.     LDMFD SP!,{R4-R5,PC}^
  715.  
  716.  
  717.     IMPORT malloc
  718.     IMPORT free
  719.     IMPORT realloc
  720.     IMPORT calloc
  721.  
  722. malloc_addr
  723.     DCD malloc
  724. free_addr
  725.     DCD free
  726. realloc_addr
  727.     DCD realloc
  728. calloc_addr
  729.     DCD calloc
  730.  
  731. claim_heap_modifiers
  732.         STMFD SP!,{link}
  733.         LDR R0,malloc_addr                  ; get address of malloc
  734.         ADR R1,claimed_malloc
  735.         ADR R2,my_malloc
  736.         BL claim_vector
  737.         LDR R0,free_addr                  ; get address of free
  738.         ADR R1,claimed_free
  739.         ADR R2,my_free
  740.         BL claim_vector
  741.         LDR R0,calloc_addr                  ; get address of calloc
  742.         ADR R1,claimed_calloc
  743.         ADR R2,my_calloc
  744.         BL claim_vector
  745.         LDR R0,realloc_addr                  ; get address of realloc
  746.         ADR R1,claimed_realloc
  747.         ADR R2,my_realloc
  748.         BL claim_vector
  749.     LDMFD SP!,{PC}^
  750.  
  751. ;
  752. ; claim a vector
  753. ;
  754.  
  755. ; entry:
  756. ;   R0 = address of vector to claim
  757. ;   R1 = address of word to hold old vector address
  758. ;   R2 = new vector address
  759.  
  760. claim_vector
  761.     STMFD SP!,{R0-R3,link}
  762.     MOV R3,R0
  763.         LDR R0,[R3,#0]                      ; get B instruction
  764.     BIC R0,R0,#0xff000000               ; offset in words
  765.     MOV R0,R0,LSL #2                    ; offset in bytes
  766.     ADD R0,R0,R3                        ; add to malloc address
  767.     ADD R0,R0,#8                        ; add for  pipelining
  768.     STR R0,[R1,#0]                      ; store in old place
  769.     SUB R0,R2,R3                        ; calc offset from branch to routine
  770.     SUB R0,R0,#8                        ; remove pipelining
  771.     MOV R0,R0,LSR #2                    ; in words
  772.     ORR R0,R0,#0xEA000000               ; make branch
  773.     STR R0,[R3,#0]                      ; store in vector position
  774.     LDMFD SP!,{R0-R3,PC}^
  775.  
  776.  
  777.  END
  778.