m68k.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. - DBRA does only 16-bit decrements, so handling more than 65535 bytes
  85. requires additional code anyway (Sergei) }
  86. cmpi.l #65535, d1 { check, if this is a word move }
  87. ble @LMEMSET3 { use fast dbra mode }
  88. {$endif CPUM68K_HAS_DBRA}
  89. bra @LMEMSET2
  90. @LMEMSET1:
  91. move.b d0,(a0)+
  92. @LMEMSET2:
  93. subq.l #1,d1
  94. bpl @LMEMSET1
  95. bra @LMEMSET5 { finished slow mode , exit }
  96. {$ifdef CPUM68K_HAS_DBRA}
  97. @LMEMSET4: { fast loop mode section 68010+ }
  98. move.b d0,(a0)+
  99. @LMEMSET3:
  100. dbra d1,@LMEMSET4
  101. {$endif CPUM68K_HAS_DBRA}
  102. @LMEMSET5:
  103. end;
  104. {$ifdef dummy}
  105. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  106. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  107. {---------------------------------------------------}
  108. { Low-level routine to copy a string to another }
  109. { string with maximum length. Never call directly! }
  110. { On Entry: }
  111. { a1.l = string to copy to }
  112. { a0.l = source string }
  113. { d0.l = maximum length of copy }
  114. { registers destroyed: a0,a1,d0,d1 }
  115. {---------------------------------------------------}
  116. asm
  117. { move.l sstr,a0
  118. move.l dstr,a1
  119. move.l len,d1 }
  120. move.l d0,d1
  121. move.b (a0)+,d0 { Get source length }
  122. and.w #$ff,d0
  123. cmp.w d1,d0 { This is a signed comparison! }
  124. ble @LM4
  125. move.b d1,d0 { If longer than maximum size of target, cut
  126. source length }
  127. @LM4:
  128. andi.l #$ff,d0 { zero extend d0-byte }
  129. move.l d0,d1 { save length to copy }
  130. move.b d0,(a1)+ { save new length }
  131. { Check if copying length is zero - if so then }
  132. { exit without copying anything. }
  133. tst.b d1
  134. beq @Lend
  135. bra @LMSTRCOPY55
  136. @LMSTRCOPY56: { 68010 Fast loop mode }
  137. move.b (a0)+,(a1)+
  138. @LMSTRCOPY55:
  139. {$ifndef CPUM68K_HAS_DBRA}
  140. sub.l #1,d1
  141. bpl @LMSTRCOPY56
  142. {$else CPUM68K_HAS_DBRA}
  143. dbra d1,@LMSTRCOPY56
  144. {$endif CPUM68K_HAS_DBRA}
  145. @Lend:
  146. end;
  147. { Concatenate Strings }
  148. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  149. { therefore online assembler may not parse the params as normal }
  150. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  151. begin
  152. asm
  153. move.b #255,d0
  154. move.l s1,a0 { a0 = destination }
  155. move.l s2,a1 { a1 = source }
  156. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  157. move.b (a1),d6
  158. and.w #$ff,d0 { Sign flags are checked! }
  159. and.w #$ff,d6
  160. cmp.w d6,d0 { if copyl > length(s2) then }
  161. ble @Lcontinue
  162. move.b (a1),d0 { copyl:=length(s2) }
  163. @Lcontinue:
  164. move.b (a0),d6
  165. and.l #$ff,d6
  166. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  167. add.l #1,a1 { s2[1] }
  168. move.b d0,d6
  169. { Check if copying length is zero - if so then }
  170. { exit without copying anything. }
  171. tst.b d6
  172. beq @Lend
  173. bra @ALoop
  174. @Loop:
  175. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  176. @ALoop:
  177. {$ifndef CPUM68K_HAS_DBRA}
  178. sub.l #1,d6
  179. bpl @Loop
  180. {$else CPUM68K_HAS_DBRA}
  181. dbra d6,@Loop
  182. {$endif CPUM68K_HAS_DBRA}
  183. move.l s1,a0
  184. add.b d0,(a0) { change to new string length }
  185. @Lend:
  186. end ['d0','d1','a0','a1','d6'];
  187. end;
  188. { Compares strings }
  189. { DO NOT CALL directly. }
  190. { a0 = pointer to first string to compare }
  191. { a1 = pointer to second string to compare }
  192. { ALL FLAGS are set appropriately. }
  193. { ZF = strings are equal }
  194. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  195. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  196. asm
  197. move.b (a0)+,d0 { Get length of first string }
  198. move.b (a1)+,d6 { Get length of 2nd string }
  199. move.b d6,d1 { Save length of string for final compare }
  200. cmp.b d0,d6 { Get shortest string length }
  201. ble @LSTRCONCAT1
  202. move.b d0,d6 { Set length to shortest string }
  203. @LSTRCONCAT1:
  204. tst.b d6 { Both strings have a length of zero, exit }
  205. beq @LSTRCONCAT2
  206. andi.l #$ff,d6
  207. subq.l #1,d6 { subtract first attempt }
  208. { if value is -1 then don't loop and just compare lengths of }
  209. { both strings before exiting. }
  210. bmi @LSTRCONCAT2
  211. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  212. @LSTRCONCAT5:
  213. { Workaroung for GAS v.134 bug }
  214. { old: cmp.b (a1)+,(a0)+ }
  215. cmpm.b (a1)+,(a0)+
  216. @LSTRCONCAT4:
  217. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  218. bne @LSTRCONCAT3
  219. @LSTRCONCAT2:
  220. { If length of both string are equal }
  221. { Then set zero flag }
  222. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  223. @LSTRCONCAT3:
  224. end;
  225. {$endif dummy}
  226. {$define FPC_SYSTEM_HAS_MOVE}
  227. procedure move(const source;var dest;count : longint);
  228. { base pointer+8 = source }
  229. { base pointer+12 = destination }
  230. { base pointer+16 = number of bytes to move}
  231. begin
  232. asm
  233. clr.l d0
  234. move.l count, d0 { number of bytes }
  235. tst.l d0 { anything to copy at all? }
  236. ble @LMOVE5
  237. @LMOVE0:
  238. move.l dest, a1 { destination }
  239. move.l source, a0 { source }
  240. {$ifdef CPUM68K_HAS_DBRA}
  241. cmpi.l #65535, d0 { check, if this is a word move }
  242. ble @LMEMSET00 { use fast dbra mode 68010+ }
  243. {$endif CPUM68K_HAS_DBRA}
  244. cmp.l a0,a1 { check copy direction }
  245. bls @LMOVE4
  246. add.l d0,a0 { move pointers to end }
  247. add.l d0,a1
  248. bra @LMOVE2
  249. @LMOVE1:
  250. move.b -(a0),-(a1) { (s < d) copy loop }
  251. @LMOVE2:
  252. subq.l #1,d0
  253. cmpi.l #-1,d0
  254. bne @LMOVE1
  255. bra @LMOVE5
  256. @LMOVE3:
  257. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  258. @LMOVE4:
  259. subq.l #1,d0
  260. cmpi.l #-1,d0
  261. bne @LMOVE3
  262. bra @LMOVE5
  263. {$ifdef CPUM68K_HAS_DBRA}
  264. @LMEMSET00: { use fast loop mode 68010+ }
  265. cmp.l a0,a1 { check copy direction }
  266. bls @LMOVE04
  267. add.l d0,a0 { move pointers to end }
  268. add.l d0,a1
  269. bra @LMOVE02
  270. @LMOVE01:
  271. move.b -(a0),-(a1) { (s < d) copy loop }
  272. @LMOVE02:
  273. dbra d0,@LMOVE01
  274. bra @LMOVE5
  275. @LMOVE03:
  276. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  277. @LMOVE04:
  278. dbra d0,@LMOVE03
  279. {$endif CPUM68K_HAS_DBRA}
  280. { end fast loop mode }
  281. @LMOVE5:
  282. end ['d0','a0','a1'];
  283. end;
  284. {$define FPC_SYSTEM_HAS_FILLWORD}
  285. procedure FillWord(var x; count : longint; value : word); assembler;
  286. asm
  287. move.l x, a0 { destination }
  288. move.l count, d1 { number of bytes to fill }
  289. move.w value, d0 { fill data }
  290. tst.l d1 { anything to fill at all? }
  291. ble @LMEMSET3
  292. bra @LMEMSET21
  293. @LMEMSET11:
  294. move.w d0,(a0)+
  295. @LMEMSET21:
  296. subq.l #1,d1
  297. bpl @LMEMSET11
  298. @LMEMSET3:
  299. end;
  300. function InterLockedDecrement (var Target: longint) : longint;
  301. begin
  302. {$warning FIX ME}
  303. Dec(Target);
  304. Result := Target;
  305. end;
  306. function InterLockedIncrement (var Target: longint) : longint;
  307. begin
  308. {$warning FIX ME}
  309. Inc(Target);
  310. Result := Target;
  311. end;
  312. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  313. begin
  314. {$warning FIX ME}
  315. Result := Target;
  316. Target := Source;
  317. end;
  318. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  319. begin
  320. {$warning FIX ME}
  321. Result := Target;
  322. Target := Target + Source;
  323. end;
  324. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  325. begin
  326. {$warning FIX ME}
  327. Result := Target;
  328. if Target = Comperand then
  329. Target := NewValue;
  330. end;
  331. {$if defined(CPUM68K_HAS_BYTEREV) or defined(CPUM68K_HAS_ROLROR)}
  332. { Disabled for now, because not all cases below were tested. (KB) }
  333. {.$define FPC_SYSTEM_HAS_SWAPENDIAN}
  334. {$endif}
  335. {$if defined(FPC_SYSTEM_HAS_SWAPENDIAN)}
  336. function SwapEndian(const AValue: SmallInt): SmallInt; assembler; nostackframe;
  337. asm
  338. {$if defined(CPUM68K_HAS_ROLROR)}
  339. move.w avalue, d0
  340. ror.w #8, d0
  341. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  342. move.w avalue, d0
  343. byterev d0
  344. swap d0
  345. {$else}
  346. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  347. clr.l d0
  348. move.w avalue, d0
  349. move.w d0, d1
  350. lsr.l #8, d0
  351. lsl.l #8, d1
  352. or.l d1, d0
  353. {$endif}
  354. end;
  355. function SwapEndian(const AValue: Word): Word; assembler; nostackframe;
  356. asm
  357. {$if defined(CPUM68K_HAS_ROLROR)}
  358. move.w avalue, d0
  359. ror.w #8, d0
  360. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  361. move.w avalue, d0
  362. byterev d0
  363. swap d0
  364. {$else}
  365. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  366. clr.l d0
  367. move.w avalue, d0
  368. move.w d0, d1
  369. lsr.l #8, d0
  370. lsl.l #8, d1
  371. or.l d1, d0
  372. {$endif}
  373. end;
  374. function SwapEndian(const AValue: LongInt): LongInt; assembler; nostackframe;
  375. asm
  376. {$if defined(CPUM68K_HAS_ROLROR)}
  377. move.l avalue, d0
  378. ror.w #8, d0
  379. swap d0
  380. ror.w #8, d0
  381. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  382. move.l avalue, d0
  383. byterev d0
  384. {$else}
  385. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  386. move.l avalue, d0
  387. move.l d0, d1
  388. andi.l #$ff00ff00, d0
  389. andi.l #$00ff00ff, d1
  390. lsr.l #8, d0
  391. lsl.l #8, d1
  392. or.l d1, d0
  393. swap d0
  394. {$endif}
  395. end;
  396. function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe;
  397. asm
  398. {$if defined(CPUM68K_HAS_ROLROR)}
  399. move.l avalue, d0
  400. ror.w #8, d0
  401. swap d0
  402. ror.w #8, d0
  403. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  404. move.l avalue, d0
  405. byterev d0
  406. {$else}
  407. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  408. move.l avalue, d0
  409. move.l d0, d1
  410. andi.l #$ff00ff00, d0
  411. andi.l #$00ff00ff, d1
  412. lsr.l #8, d0
  413. lsl.l #8, d1
  414. or.l d1, d0
  415. swap d0
  416. {$endif}
  417. end;
  418. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  419. asm
  420. {$if defined(CPUM68K_HAS_ROLROR)}
  421. move.l avalue+4, d0
  422. ror.w #8, d0
  423. swap d0
  424. ror.w #8, d0
  425. move.l avalue, d1
  426. ror.w #8, d1
  427. swap d1
  428. ror.w #8, d1
  429. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  430. move.l avalue+4, d0
  431. move.l avalue, d1
  432. byterev d0
  433. byterev d1
  434. {$else}
  435. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  436. move.l d2, -(sp)
  437. move.l avalue+4, d0
  438. move.l d0, d1
  439. andi.l #$ff00ff00, d0
  440. andi.l #$00ff00ff, d1
  441. lsr.l #8, d0
  442. lsl.l #8, d1
  443. or.l d1, d0
  444. swap d0
  445. move.l avalue, d1
  446. move.l d1, d2
  447. andi.l #$ff00ff00, d1
  448. andi.l #$00ff00ff, d2
  449. lsr.l #8, d1
  450. lsl.l #8, d2
  451. or.l d2, d1
  452. swap d1
  453. move.l (sp)+, d2
  454. {$endif}
  455. end;
  456. function SwapEndian(const AValue: QWord): QWord; assembler; nostackframe;
  457. asm
  458. {$if defined(CPUM68K_HAS_ROLROR)}
  459. move.l avalue+4, d0
  460. ror.w #8, d0
  461. swap d0
  462. ror.w #8, d0
  463. move.l avalue, d1
  464. ror.w #8, d1
  465. swap d1
  466. ror.w #8, d1
  467. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  468. move.l avalue+4, d0
  469. move.l avalue, d1
  470. byterev d0
  471. byterev d1
  472. {$else}
  473. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  474. move.l d2, -(sp)
  475. move.l avalue+4, d0
  476. move.l d0, d1
  477. andi.l #$ff00ff00, d0
  478. andi.l #$00ff00ff, d1
  479. lsr.l #8, d0
  480. lsl.l #8, d1
  481. or.l d1, d0
  482. swap d0
  483. move.l avalue, d1
  484. move.l d1, d2
  485. andi.l #$ff00ff00, d1
  486. andi.l #$00ff00ff, d2
  487. lsr.l #8, d1
  488. lsl.l #8, d2
  489. or.l d2, d1
  490. swap d1
  491. move.l (sp)+, d2
  492. {$endif}
  493. end;
  494. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}