m68k.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. end;
  27. {$define FPC_SYSTEM_HAS_GET_FRAME}
  28. function get_frame : pointer; assembler;nostackframe;
  29. asm
  30. {$if defined(amiga)}
  31. move.l a5,d0
  32. {$else}
  33. move.l a6,d0
  34. {$endif}
  35. end;
  36. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  37. function get_caller_addr(framebp : pointer;addr:pointer=nil) : pointer;
  38. begin
  39. asm
  40. move.l framebp,a0
  41. cmp.l #0,a0
  42. beq @Lnul_address
  43. move.l 4(a0),a0
  44. @Lnul_address:
  45. move.l a0,@RESULT
  46. end ['a0'];
  47. end;
  48. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  49. function get_caller_frame(framebp : pointer;addr:pointer=nil) : pointer;
  50. begin
  51. asm
  52. move.l FRAMEBP,a0
  53. cmp.l #0,a0
  54. beq @Lnul_frame
  55. move.l (a0),a0
  56. @Lnul_frame:
  57. move.l a0,@RESULT
  58. end ['a0'];
  59. end;
  60. {$define FPC_SYSTEM_HAS_SPTR}
  61. function Sptr : pointer; assembler;nostackframe;
  62. asm
  63. move.l sp,d0
  64. end;
  65. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  66. function get_pc_addr : pointer;assembler;nostackframe;
  67. asm
  68. move.l (sp),d0
  69. end;
  70. {$define FPC_SYSTEM_HAS_FILLCHAR}
  71. procedure FillChar(var x;count:longint;value:byte); assembler;
  72. asm
  73. move.l x, a0 { destination }
  74. move.l count, d1 { number of bytes to fill }
  75. move.b value, d0 { fill data }
  76. tst.l d1 { anything to fill at all? }
  77. beq @LMEMSET5
  78. cmpi.l #65535, d1 { check, if this is a word move }
  79. ble @LMEMSET3 { use fast dbra mode }
  80. bra @LMEMSET2
  81. @LMEMSET1:
  82. move.b d0,(a0)+
  83. @LMEMSET2:
  84. subq.l #1,d1
  85. cmp.l #-1,d1
  86. bne @LMEMSET1
  87. bra @LMEMSET5 { finished slow mode , exit }
  88. @LMEMSET4: { fast loop mode section 68010+ }
  89. move.b d0,(a0)+
  90. @LMEMSET3:
  91. {$ifdef CPUCOLDFIRE}
  92. sub.l #1,d1
  93. bmi @LMEMSET4
  94. {$else}
  95. dbra d1,@LMEMSET4
  96. {$endif}
  97. @LMEMSET5:
  98. end ['d0','d1','a0'];
  99. {$ifdef dummy}
  100. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  101. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  102. {---------------------------------------------------}
  103. { Low-level routine to copy a string to another }
  104. { string with maximum length. Never call directly! }
  105. { On Entry: }
  106. { a1.l = string to copy to }
  107. { a0.l = source string }
  108. { d0.l = maximum length of copy }
  109. { registers destroyed: a0,a1,d0,d1 }
  110. {---------------------------------------------------}
  111. asm
  112. { move.l sstr,a0
  113. move.l dstr,a1
  114. move.l len,d1 }
  115. move.l d0,d1
  116. move.b (a0)+,d0 { Get source length }
  117. and.w #$ff,d0
  118. cmp.w d1,d0 { This is a signed comparison! }
  119. ble @LM4
  120. move.b d1,d0 { If longer than maximum size of target, cut
  121. source length }
  122. @LM4:
  123. andi.l #$ff,d0 { zero extend d0-byte }
  124. move.l d0,d1 { save length to copy }
  125. move.b d0,(a1)+ { save new length }
  126. { Check if copying length is zero - if so then }
  127. { exit without copying anything. }
  128. tst.b d1
  129. beq @Lend
  130. bra @LMSTRCOPY55
  131. @LMSTRCOPY56: { 68010 Fast loop mode }
  132. move.b (a0)+,(a1)+
  133. @LMSTRCOPY55:
  134. {$ifdef CPUCOLDFIRE}
  135. sub.l #1,d1
  136. bmi @LMSTRCOPY56
  137. {$else}
  138. dbra d1,@LMSTRCOPY56
  139. {$endif}
  140. @Lend:
  141. end;
  142. { Concatenate Strings }
  143. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  144. { therefore online assembler may not parse the params as normal }
  145. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  146. begin
  147. asm
  148. move.b #255,d0
  149. move.l s1,a0 { a0 = destination }
  150. move.l s2,a1 { a1 = source }
  151. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  152. move.b (a1),d6
  153. and.w #$ff,d0 { Sign flags are checked! }
  154. and.w #$ff,d6
  155. cmp.w d6,d0 { if copyl > length(s2) then }
  156. ble @Lcontinue
  157. move.b (a1),d0 { copyl:=length(s2) }
  158. @Lcontinue:
  159. move.b (a0),d6
  160. and.l #$ff,d6
  161. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  162. add.l #1,a1 { s2[1] }
  163. move.b d0,d6
  164. { Check if copying length is zero - if so then }
  165. { exit without copying anything. }
  166. tst.b d6
  167. beq @Lend
  168. bra @ALoop
  169. @Loop:
  170. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  171. @ALoop:
  172. {$ifdef CPUCOLDFIRE}
  173. sub.l #1,d6
  174. bmi @Loop
  175. {$else}
  176. dbra d6,@Loop
  177. {$endif}
  178. move.l s1,a0
  179. add.b d0,(a0) { change to new string length }
  180. @Lend:
  181. end ['d0','d1','a0','a1','d6'];
  182. end;
  183. { Compares strings }
  184. { DO NOT CALL directly. }
  185. { a0 = pointer to first string to compare }
  186. { a1 = pointer to second string to compare }
  187. { ALL FLAGS are set appropriately. }
  188. { ZF = strings are equal }
  189. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  190. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  191. asm
  192. move.b (a0)+,d0 { Get length of first string }
  193. move.b (a1)+,d6 { Get length of 2nd string }
  194. move.b d6,d1 { Save length of string for final compare }
  195. cmp.b d0,d6 { Get shortest string length }
  196. ble @LSTRCONCAT1
  197. move.b d0,d6 { Set length to shortest string }
  198. @LSTRCONCAT1:
  199. tst.b d6 { Both strings have a length of zero, exit }
  200. beq @LSTRCONCAT2
  201. andi.l #$ff,d6
  202. subq.l #1,d6 { subtract first attempt }
  203. { if value is -1 then don't loop and just compare lengths of }
  204. { both strings before exiting. }
  205. bmi @LSTRCONCAT2
  206. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  207. @LSTRCONCAT5:
  208. { Workaroung for GAS v.134 bug }
  209. { old: cmp.b (a1)+,(a0)+ }
  210. cmpm.b (a1)+,(a0)+
  211. @LSTRCONCAT4:
  212. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  213. bne @LSTRCONCAT3
  214. @LSTRCONCAT2:
  215. { If length of both string are equal }
  216. { Then set zero flag }
  217. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  218. @LSTRCONCAT3:
  219. end;
  220. {$endif dummy}
  221. {$define FPC_SYSTEM_HAS_MOVE}
  222. procedure move(const source;var dest;count : longint);
  223. { base pointer+8 = source }
  224. { base pointer+12 = destination }
  225. { base pointer+16 = number of bytes to move}
  226. begin
  227. asm
  228. clr.l d0
  229. move.l count, d0 { number of bytes }
  230. tst.l d0 { anything to copy at all? }
  231. beq @LMOVE5
  232. @LMOVE0:
  233. move.l dest, a1 { destination }
  234. move.l source, a0 { source }
  235. {$ifndef CPUCOLDFIRE}
  236. cmpi.l #65535, d0 { check, if this is a word move }
  237. ble @LMEMSET00 { use fast dbra mode 68010+ }
  238. {$endif CPUCOLDFIRE}
  239. cmp.l a0,a1 { check copy direction }
  240. bls @LMOVE4
  241. add.l d0,a0 { move pointers to end }
  242. add.l d0,a1
  243. bra @LMOVE2
  244. @LMOVE1:
  245. move.b -(a0),-(a1) { (s < d) copy loop }
  246. @LMOVE2:
  247. subq.l #1,d0
  248. cmpi.l #-1,d0
  249. bne @LMOVE1
  250. bra @LMOVE5
  251. @LMOVE3:
  252. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  253. @LMOVE4:
  254. subq.l #1,d0
  255. cmpi.l #-1,d0
  256. bne @LMOVE3
  257. bra @LMOVE5
  258. {$ifndef CPUCOLDFIRE}
  259. @LMEMSET00: { use fast loop mode 68010+ }
  260. cmp.l a0,a1 { check copy direction }
  261. bls @LMOVE04
  262. add.l d0,a0 { move pointers to end }
  263. add.l d0,a1
  264. bra @LMOVE02
  265. @LMOVE01:
  266. move.b -(a0),-(a1) { (s < d) copy loop }
  267. @LMOVE02:
  268. dbra d0,@LMOVE01
  269. bra @LMOVE5
  270. @LMOVE03:
  271. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  272. @LMOVE04:
  273. dbra d0,@LMOVE03
  274. {$endif CPUCOLDFIRE}
  275. { end fast loop mode }
  276. @LMOVE5:
  277. end ['d0','a0','a1'];
  278. end;
  279. {$define FPC_SYSTEM_HAS_FILLWORD}
  280. procedure fillword(var x;count : longint;value : word);
  281. begin
  282. asm
  283. move.l x, a0 { destination }
  284. move.l count, d1 { number of bytes to fill }
  285. move.w value, d0 { fill data }
  286. tst.l d1 { anything to fill at all? }
  287. beq @LMEMSET3
  288. bra @LMEMSET21
  289. @LMEMSET11:
  290. move.w d0,(a0)+
  291. @LMEMSET21:
  292. subq.l #1,d1
  293. cmp.b #-1,d1
  294. bne @LMEMSET11
  295. @LMEMSET3:
  296. end ['d0','d1','a0'];
  297. end;
  298. {$define FPC_SYSTEM_HAS_ABS_LONGINT}
  299. function abs(l : longint) : longint;
  300. begin
  301. asm
  302. move.l l,d0
  303. tst.l d0
  304. bpl @LMABS1
  305. neg.l d0
  306. @LMABS1:
  307. move.l d0,@RESULT
  308. end ['d0'];
  309. end;
  310. function InterLockedDecrement (var Target: longint) : longint;
  311. begin
  312. {$warning FIX ME}
  313. Dec(Target);
  314. Result := Target;
  315. end;
  316. function InterLockedIncrement (var Target: longint) : longint;
  317. begin
  318. {$warning FIX ME}
  319. Inc(Target);
  320. Result := Target;
  321. end;
  322. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  323. begin
  324. {$warning FIX ME}
  325. Result := Target;
  326. Target := Source;
  327. end;
  328. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  329. begin
  330. {$warning FIX ME}
  331. Result := Target;
  332. Target := Target + Source;
  333. end;
  334. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  335. begin
  336. {$warning FIX ME}
  337. Result := Target;
  338. if Target = Comperand then
  339. Target := NewValue;
  340. end;