m68k.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Carl-Eric Codere,
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. m68k.inc : Processor dependent implementation of system unit
  13. For Motorola 680x0 Processor.
  14. *****************************************************************************}
  15. {****************************************************************************}
  16. { Credit where credit is due: }
  17. { -Some of the copy routines taken from the Atari dlib source code: }
  18. { Dale Schumacher (alias: Dalnefre') [email protected] }
  19. { 399 Beacon Ave. St. Paul, MN 55104,USA }
  20. { -Some of the routines taken from the freeware ATARI Sozobon C compiler }
  21. { 1988 by Sozobon, Limited. Author: Johann Ruegg (freeware) }
  22. { Thanks to all these people wherever they maybe today! }
  23. {****************************************************************************}
  24. procedure fpc_cpuinit;
  25. begin
  26. SysResetFPU;
  27. if (not IsLibrary) then
  28. SysInitFPU;
  29. end;
  30. {$define FPC_SYSTEM_HAS_GET_FRAME}
  31. function get_frame : pointer; assembler;nostackframe;
  32. asm
  33. {$if defined(amiga)}
  34. move.l a5,d0
  35. {$else}
  36. move.l a6,d0
  37. {$endif}
  38. end;
  39. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  40. function get_caller_addr(framebp : pointer;addr:pointer=nil) : pointer;
  41. begin
  42. asm
  43. move.l framebp,a0
  44. cmp.l #0,a0
  45. beq @Lnul_address
  46. move.l 4(a0),a0
  47. @Lnul_address:
  48. move.l a0,@RESULT
  49. end ['a0'];
  50. end;
  51. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  52. function get_caller_frame(framebp : pointer;addr:pointer=nil) : pointer;
  53. begin
  54. asm
  55. move.l FRAMEBP,a0
  56. cmp.l #0,a0
  57. beq @Lnul_frame
  58. move.l (a0),a0
  59. @Lnul_frame:
  60. move.l a0,@RESULT
  61. end ['a0'];
  62. end;
  63. {$define FPC_SYSTEM_HAS_SPTR}
  64. function Sptr : pointer; assembler;nostackframe;
  65. asm
  66. move.l sp,d0
  67. end;
  68. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  69. function get_pc_addr : pointer;assembler;nostackframe;
  70. asm
  71. move.l (sp),d0
  72. end;
  73. {$define FPC_SYSTEM_HAS_FILLCHAR}
  74. procedure FillChar(var x; count : longint; value : byte); assembler;
  75. asm
  76. move.l x, a0 { destination }
  77. move.l count, d1 { number of bytes to fill }
  78. move.b value, d0 { fill data }
  79. tst.l d1 { anything to fill at all? }
  80. ble @LMEMSET5
  81. {$ifdef CPUM68K_HAS_DBRA}
  82. { FIXME: Any reason why not always just use DBRA mode on
  83. CPUs which support it? (KB) }
  84. cmpi.l #65535, d1 { check, if this is a word move }
  85. ble @LMEMSET3 { use fast dbra mode }
  86. {$endif CPUM68K_HAS_DBRA}
  87. bra @LMEMSET2
  88. @LMEMSET1:
  89. move.b d0,(a0)+
  90. @LMEMSET2:
  91. subq.l #1,d1
  92. bpl @LMEMSET1
  93. bra @LMEMSET5 { finished slow mode , exit }
  94. {$ifdef CPUM68K_HAS_DBRA}
  95. @LMEMSET4: { fast loop mode section 68010+ }
  96. move.b d0,(a0)+
  97. @LMEMSET3:
  98. dbra d1,@LMEMSET4
  99. {$endif CPUM68K_HAS_DBRA}
  100. @LMEMSET5:
  101. end;
  102. {$ifdef dummy}
  103. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  104. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  105. {---------------------------------------------------}
  106. { Low-level routine to copy a string to another }
  107. { string with maximum length. Never call directly! }
  108. { On Entry: }
  109. { a1.l = string to copy to }
  110. { a0.l = source string }
  111. { d0.l = maximum length of copy }
  112. { registers destroyed: a0,a1,d0,d1 }
  113. {---------------------------------------------------}
  114. asm
  115. { move.l sstr,a0
  116. move.l dstr,a1
  117. move.l len,d1 }
  118. move.l d0,d1
  119. move.b (a0)+,d0 { Get source length }
  120. and.w #$ff,d0
  121. cmp.w d1,d0 { This is a signed comparison! }
  122. ble @LM4
  123. move.b d1,d0 { If longer than maximum size of target, cut
  124. source length }
  125. @LM4:
  126. andi.l #$ff,d0 { zero extend d0-byte }
  127. move.l d0,d1 { save length to copy }
  128. move.b d0,(a1)+ { save new length }
  129. { Check if copying length is zero - if so then }
  130. { exit without copying anything. }
  131. tst.b d1
  132. beq @Lend
  133. bra @LMSTRCOPY55
  134. @LMSTRCOPY56: { 68010 Fast loop mode }
  135. move.b (a0)+,(a1)+
  136. @LMSTRCOPY55:
  137. {$ifndef CPUM68K_HAS_DBRA}
  138. sub.l #1,d1
  139. bpl @LMSTRCOPY56
  140. {$else CPUM68K_HAS_DBRA}
  141. dbra d1,@LMSTRCOPY56
  142. {$endif CPUM68K_HAS_DBRA}
  143. @Lend:
  144. end;
  145. { Concatenate Strings }
  146. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  147. { therefore online assembler may not parse the params as normal }
  148. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  149. begin
  150. asm
  151. move.b #255,d0
  152. move.l s1,a0 { a0 = destination }
  153. move.l s2,a1 { a1 = source }
  154. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  155. move.b (a1),d6
  156. and.w #$ff,d0 { Sign flags are checked! }
  157. and.w #$ff,d6
  158. cmp.w d6,d0 { if copyl > length(s2) then }
  159. ble @Lcontinue
  160. move.b (a1),d0 { copyl:=length(s2) }
  161. @Lcontinue:
  162. move.b (a0),d6
  163. and.l #$ff,d6
  164. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  165. add.l #1,a1 { s2[1] }
  166. move.b d0,d6
  167. { Check if copying length is zero - if so then }
  168. { exit without copying anything. }
  169. tst.b d6
  170. beq @Lend
  171. bra @ALoop
  172. @Loop:
  173. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  174. @ALoop:
  175. {$ifndef CPUM68K_HAS_DBRA}
  176. sub.l #1,d6
  177. bpl @Loop
  178. {$else CPUM68K_HAS_DBRA}
  179. dbra d6,@Loop
  180. {$endif CPUM68K_HAS_DBRA}
  181. move.l s1,a0
  182. add.b d0,(a0) { change to new string length }
  183. @Lend:
  184. end ['d0','d1','a0','a1','d6'];
  185. end;
  186. { Compares strings }
  187. { DO NOT CALL directly. }
  188. { a0 = pointer to first string to compare }
  189. { a1 = pointer to second string to compare }
  190. { ALL FLAGS are set appropriately. }
  191. { ZF = strings are equal }
  192. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  193. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  194. asm
  195. move.b (a0)+,d0 { Get length of first string }
  196. move.b (a1)+,d6 { Get length of 2nd string }
  197. move.b d6,d1 { Save length of string for final compare }
  198. cmp.b d0,d6 { Get shortest string length }
  199. ble @LSTRCONCAT1
  200. move.b d0,d6 { Set length to shortest string }
  201. @LSTRCONCAT1:
  202. tst.b d6 { Both strings have a length of zero, exit }
  203. beq @LSTRCONCAT2
  204. andi.l #$ff,d6
  205. subq.l #1,d6 { subtract first attempt }
  206. { if value is -1 then don't loop and just compare lengths of }
  207. { both strings before exiting. }
  208. bmi @LSTRCONCAT2
  209. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  210. @LSTRCONCAT5:
  211. { Workaroung for GAS v.134 bug }
  212. { old: cmp.b (a1)+,(a0)+ }
  213. cmpm.b (a1)+,(a0)+
  214. @LSTRCONCAT4:
  215. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  216. bne @LSTRCONCAT3
  217. @LSTRCONCAT2:
  218. { If length of both string are equal }
  219. { Then set zero flag }
  220. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  221. @LSTRCONCAT3:
  222. end;
  223. {$endif dummy}
  224. {$define FPC_SYSTEM_HAS_MOVE}
  225. procedure move(const source;var dest;count : longint);
  226. { base pointer+8 = source }
  227. { base pointer+12 = destination }
  228. { base pointer+16 = number of bytes to move}
  229. begin
  230. asm
  231. clr.l d0
  232. move.l count, d0 { number of bytes }
  233. tst.l d0 { anything to copy at all? }
  234. ble @LMOVE5
  235. @LMOVE0:
  236. move.l dest, a1 { destination }
  237. move.l source, a0 { source }
  238. {$ifdef CPUM68K_HAS_DBRA}
  239. cmpi.l #65535, d0 { check, if this is a word move }
  240. ble @LMEMSET00 { use fast dbra mode 68010+ }
  241. {$endif CPUM68K_HAS_DBRA}
  242. cmp.l a0,a1 { check copy direction }
  243. bls @LMOVE4
  244. add.l d0,a0 { move pointers to end }
  245. add.l d0,a1
  246. bra @LMOVE2
  247. @LMOVE1:
  248. move.b -(a0),-(a1) { (s < d) copy loop }
  249. @LMOVE2:
  250. subq.l #1,d0
  251. cmpi.l #-1,d0
  252. bne @LMOVE1
  253. bra @LMOVE5
  254. @LMOVE3:
  255. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  256. @LMOVE4:
  257. subq.l #1,d0
  258. cmpi.l #-1,d0
  259. bne @LMOVE3
  260. bra @LMOVE5
  261. {$ifdef CPUM68K_HAS_DBRA}
  262. @LMEMSET00: { use fast loop mode 68010+ }
  263. cmp.l a0,a1 { check copy direction }
  264. bls @LMOVE04
  265. add.l d0,a0 { move pointers to end }
  266. add.l d0,a1
  267. bra @LMOVE02
  268. @LMOVE01:
  269. move.b -(a0),-(a1) { (s < d) copy loop }
  270. @LMOVE02:
  271. dbra d0,@LMOVE01
  272. bra @LMOVE5
  273. @LMOVE03:
  274. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  275. @LMOVE04:
  276. dbra d0,@LMOVE03
  277. {$endif CPUM68K_HAS_DBRA}
  278. { end fast loop mode }
  279. @LMOVE5:
  280. end ['d0','a0','a1'];
  281. end;
  282. {$define FPC_SYSTEM_HAS_FILLWORD}
  283. procedure FillWord(var x; count : longint; value : word); assembler;
  284. asm
  285. move.l x, a0 { destination }
  286. move.l count, d1 { number of bytes to fill }
  287. move.w value, d0 { fill data }
  288. tst.l d1 { anything to fill at all? }
  289. ble @LMEMSET3
  290. bra @LMEMSET21
  291. @LMEMSET11:
  292. move.w d0,(a0)+
  293. @LMEMSET21:
  294. subq.l #1,d1
  295. bpl @LMEMSET11
  296. @LMEMSET3:
  297. end;
  298. function InterLockedDecrement (var Target: longint) : longint;
  299. begin
  300. {$warning FIX ME}
  301. Dec(Target);
  302. Result := Target;
  303. end;
  304. function InterLockedIncrement (var Target: longint) : longint;
  305. begin
  306. {$warning FIX ME}
  307. Inc(Target);
  308. Result := Target;
  309. end;
  310. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  311. begin
  312. {$warning FIX ME}
  313. Result := Target;
  314. Target := Source;
  315. end;
  316. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  317. begin
  318. {$warning FIX ME}
  319. Result := Target;
  320. Target := Target + Source;
  321. end;
  322. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  323. begin
  324. {$warning FIX ME}
  325. Result := Target;
  326. if Target = Comperand then
  327. Target := NewValue;
  328. end;