m68k.inc 11 KB

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