m68k.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. {$IF DEFINED(FPU68881) OR DEFINED(FPUCOLDFIRE)}
  25. function GetFPCR: DWord; assembler; nostackframe;
  26. asm
  27. fmove.l fpcr,d0
  28. end;
  29. function GetFPSR: DWord; assembler; nostackframe;
  30. asm
  31. fmove.l fpsr, d0
  32. end;
  33. procedure SetFPCR(x: DWord);
  34. begin
  35. Default68kFPCR:=x;
  36. asm
  37. fmove.l x, fpcr
  38. end;
  39. end;
  40. procedure SetFPSR(x: DWord); assembler; nostackframe;
  41. asm
  42. fmove.l x, fpsr
  43. end;
  44. function GetNativeFPUControlWord: TNativeFPUControlWord;
  45. begin
  46. result:=GetFPCR;
  47. end;
  48. procedure SetNativeFPUControlWord(const cw: TNativeFPUControlWord);
  49. begin
  50. SetFPCR(cw);
  51. end;
  52. {$DEFINE FPC_SYSTEM_HAS_SYSRESETFPU}
  53. procedure SysResetFPU;
  54. begin
  55. softfloat_exception_flags:=[];
  56. SetFPCR(Default68KFPCR);
  57. SetFPSR(0);
  58. end;
  59. {$DEFINE FPC_SYSTEM_HAS_SYSINITFPU}
  60. procedure SysInitFPU;
  61. begin
  62. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  63. softfloat_exception_flags:=[];
  64. SetFPSR(0);
  65. end;
  66. {$endif}
  67. {$ifndef INTERNAL_BACKTRACE}
  68. {$define FPC_SYSTEM_HAS_GET_FRAME}
  69. function get_frame : pointer; assembler;nostackframe;
  70. asm
  71. move.l fp,d0
  72. end;
  73. {$endif not INTERNAL_BACKTRACE}
  74. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  75. function get_caller_addr(framebp : pointer;addr:pointer=nil) : pointer; assembler;
  76. asm
  77. move.l framebp,d0
  78. tst.l d0
  79. beq @Lnul_address
  80. move.l d0,a0
  81. move.l 4(a0),d0
  82. @Lnul_address:
  83. end;
  84. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  85. function get_caller_frame(framebp : pointer;addr:pointer=nil) : pointer; assembler;
  86. asm
  87. move.l framebp,d0
  88. tst.l d0
  89. beq @Lnul_frame
  90. move.l d0,a0
  91. move.l (a0),d0
  92. @Lnul_frame:
  93. end;
  94. {$define FPC_SYSTEM_HAS_SPTR}
  95. function Sptr : pointer; assembler;nostackframe;
  96. asm
  97. move.l sp,d0
  98. end;
  99. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  100. function get_pc_addr : pointer;assembler;nostackframe;
  101. asm
  102. move.l (sp),d0
  103. end;
  104. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  105. {$define FPC_SYSTEM_HAS_FILLCHAR}
  106. procedure FillChar(var x; count : longint; value : byte); assembler; register; nostackframe;
  107. asm
  108. { a0 is x, d0 is count, d1 is value }
  109. tst.l d0 { anything to fill at all? }
  110. ble @Lquit
  111. cmp.l #32,d0 { limits were tested against real hardware on various CPU }
  112. blt @LfillByte
  113. cmp.l #128,d0 { limits were tested against real hardware on various CPU }
  114. blt @LfillWord
  115. bra @LfillDWord
  116. {$ifndef CPUM68K_HAS_DBRA}
  117. @LfillByte:
  118. {$endif}
  119. @LfillByteLoop:
  120. move.b d1,(a0)+
  121. {$ifdef CPUM68K_HAS_DBRA}
  122. @LfillByte:
  123. dbra d0,@LfillByteLoop
  124. {$else}
  125. subq.l #1,d0
  126. bne @LfillByteLoop
  127. {$endif}
  128. rts
  129. @LfillWord:
  130. move.l d2,-(sp)
  131. move.l a0,d2
  132. btst #0,d2
  133. beq @Leven
  134. subq.l #1,d0
  135. move.b d1,(a0)+
  136. @Leven:
  137. move.b d1,d2 // copy value to upper byte
  138. {$ifdef CPUCOLDFIRE}
  139. lsl.l #8,d1
  140. {$else}
  141. lsl.w #8,d1
  142. {$endif}
  143. move.b d2,d1
  144. move.l d0,d2 // adjust d0 for leftover copy
  145. bclr #0,d2
  146. sub.l d2,d0
  147. lsr.l #1,d2
  148. {$ifdef CPUM68K_HAS_DBRA}
  149. subq.l #1,d2
  150. {$endif}
  151. @LfillWordLoop:
  152. move.w d1,(a0)+
  153. {$ifdef CPUM68K_HAS_DBRA}
  154. dbra d2,@LFillWordLoop
  155. {$else}
  156. subq.l #1,d2
  157. bne @LfillWordLoop
  158. {$endif}
  159. move.l (sp)+,d2
  160. tst.l d0
  161. bne @LfillByte
  162. rts
  163. @LfillDWord:
  164. move.l d2,-(sp)
  165. move.b d1,d2 // copy value to upper bytes
  166. {$ifdef CPUCOLDFIRE}
  167. lsl.l #8,d1
  168. {$else}
  169. lsl.w #8,d1
  170. {$endif}
  171. move.b d2,d1
  172. move.w d1,d2
  173. swap d1
  174. move.w d2,d1
  175. move.l a0,d2 // do initial byte and word fill, if the address is unaligned
  176. btst #0,d2
  177. beq @Ldeven
  178. subq.l #1,d0
  179. move.b d1,(a0)+
  180. @Ldeven:
  181. move.l a0,d2
  182. btst #1,d2
  183. beq @Ldquad
  184. subq.l #2,d0
  185. move.w d1,(a0)+
  186. @Ldquad:
  187. move.l d0,d2 // adjust d0 for leftover copy
  188. {$ifdef CPUCOLDFIRE}
  189. and.l #$fffffffc,d2
  190. {$else}
  191. and.b #$fc,d2
  192. {$endif}
  193. sub.l d2,d0
  194. lsr.l #2,d2
  195. bra @LfillLongLoopStart
  196. @LfillLongLoop:
  197. move.l d1,(a0)+
  198. move.l d1,(a0)+
  199. move.l d1,(a0)+
  200. move.l d1,(a0)+
  201. subq.l #4,d2
  202. @LfillLongLoopStart:
  203. cmp.l #4,d2
  204. bgt @LfillLongLoop
  205. {$ifdef CPUM68K_HAS_DBRA}
  206. subq.l #1,d2
  207. {$endif}
  208. @LfillDWordLoop:
  209. move.l d1,(a0)+
  210. {$ifdef CPUM68K_HAS_DBRA}
  211. dbra d2,@LFillDWordLoop
  212. {$else}
  213. subq.l #1,d2
  214. bne @LfillDWordLoop
  215. {$endif}
  216. move.l (sp)+,d2
  217. tst.l d0
  218. bne @LfillByte
  219. @Lquit:
  220. end;
  221. {$endif ndef FPC_SYSTEM_HAS_FILLCHAR}
  222. {$ifdef dummy}
  223. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  224. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  225. {---------------------------------------------------}
  226. { Low-level routine to copy a string to another }
  227. { string with maximum length. Never call directly! }
  228. { On Entry: }
  229. { a1.l = string to copy to }
  230. { a0.l = source string }
  231. { d0.l = maximum length of copy }
  232. { registers destroyed: a0,a1,d0,d1 }
  233. {---------------------------------------------------}
  234. asm
  235. { move.l sstr,a0
  236. move.l dstr,a1
  237. move.l len,d1 }
  238. move.l d0,d1
  239. move.b (a0)+,d0 { Get source length }
  240. and.w #$ff,d0
  241. cmp.w d1,d0 { This is a signed comparison! }
  242. ble @LM4
  243. move.b d1,d0 { If longer than maximum size of target, cut
  244. source length }
  245. @LM4:
  246. andi.l #$ff,d0 { zero extend d0-byte }
  247. move.l d0,d1 { save length to copy }
  248. move.b d0,(a1)+ { save new length }
  249. { Check if copying length is zero - if so then }
  250. { exit without copying anything. }
  251. tst.b d1
  252. beq @Lend
  253. bra @LMSTRCOPY55
  254. @LMSTRCOPY56: { 68010 Fast loop mode }
  255. move.b (a0)+,(a1)+
  256. @LMSTRCOPY55:
  257. {$ifndef CPUM68K_HAS_DBRA}
  258. sub.l #1,d1
  259. bpl @LMSTRCOPY56
  260. {$else CPUM68K_HAS_DBRA}
  261. dbra d1,@LMSTRCOPY56
  262. {$endif CPUM68K_HAS_DBRA}
  263. @Lend:
  264. end;
  265. { Concatenate Strings }
  266. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  267. { therefore online assembler may not parse the params as normal }
  268. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  269. begin
  270. asm
  271. move.b #255,d0
  272. move.l s1,a0 { a0 = destination }
  273. move.l s2,a1 { a1 = source }
  274. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  275. move.b (a1),d6
  276. and.w #$ff,d0 { Sign flags are checked! }
  277. and.w #$ff,d6
  278. cmp.w d6,d0 { if copyl > length(s2) then }
  279. ble @Lcontinue
  280. move.b (a1),d0 { copyl:=length(s2) }
  281. @Lcontinue:
  282. move.b (a0),d6
  283. and.l #$ff,d6
  284. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  285. add.l #1,a1 { s2[1] }
  286. move.b d0,d6
  287. { Check if copying length is zero - if so then }
  288. { exit without copying anything. }
  289. tst.b d6
  290. beq @Lend
  291. bra @ALoop
  292. @Loop:
  293. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  294. @ALoop:
  295. {$ifndef CPUM68K_HAS_DBRA}
  296. sub.l #1,d6
  297. bpl @Loop
  298. {$else CPUM68K_HAS_DBRA}
  299. dbra d6,@Loop
  300. {$endif CPUM68K_HAS_DBRA}
  301. move.l s1,a0
  302. add.b d0,(a0) { change to new string length }
  303. @Lend:
  304. end ['d0','d1','a0','a1','d6'];
  305. end;
  306. { Compares strings }
  307. { DO NOT CALL directly. }
  308. { a0 = pointer to first string to compare }
  309. { a1 = pointer to second string to compare }
  310. { ALL FLAGS are set appropriately. }
  311. { ZF = strings are equal }
  312. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  313. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  314. asm
  315. move.b (a0)+,d0 { Get length of first string }
  316. move.b (a1)+,d6 { Get length of 2nd string }
  317. move.b d6,d1 { Save length of string for final compare }
  318. cmp.b d0,d6 { Get shortest string length }
  319. ble @LSTRCONCAT1
  320. move.b d0,d6 { Set length to shortest string }
  321. @LSTRCONCAT1:
  322. tst.b d6 { Both strings have a length of zero, exit }
  323. beq @LSTRCONCAT2
  324. andi.l #$ff,d6
  325. subq.l #1,d6 { subtract first attempt }
  326. { if value is -1 then don't loop and just compare lengths of }
  327. { both strings before exiting. }
  328. bmi @LSTRCONCAT2
  329. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  330. @LSTRCONCAT5:
  331. { Workaroung for GAS v.134 bug }
  332. { old: cmp.b (a1)+,(a0)+ }
  333. cmpm.b (a1)+,(a0)+
  334. @LSTRCONCAT4:
  335. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  336. bne @LSTRCONCAT3
  337. @LSTRCONCAT2:
  338. { If length of both string are equal }
  339. { Then set zero flag }
  340. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  341. @LSTRCONCAT3:
  342. end;
  343. {$endif dummy}
  344. {$ifndef FPC_SYSTEM_HAS_MOVE}
  345. {$define FPC_SYSTEM_HAS_MOVE}
  346. procedure move(const source;var dest;count : longint); assembler;
  347. { base pointer+8 = source }
  348. { base pointer+12 = destination }
  349. { base pointer+16 = number of bytes to move}
  350. asm
  351. move.l count, d0 { number of bytes }
  352. ble @LMOVE5 { anything to copy at all? }
  353. move.l dest, a1 { destination }
  354. move.l source, a0 { source }
  355. {$ifdef CPUM68K_HAS_DBRA}
  356. cmpi.l #65535, d0 { check, if this is a word move }
  357. ble @LMEMSET00 { use fast dbra mode 68010+ }
  358. {$endif CPUM68K_HAS_DBRA}
  359. cmp.l a0,a1 { check copy direction }
  360. bls @LMOVE3
  361. add.l d0,a0 { move pointers to end }
  362. add.l d0,a1
  363. @LMOVE1:
  364. move.b -(a0),-(a1) { (s < d) copy loop }
  365. subq.l #1,d0
  366. bne @LMOVE1
  367. bra @LMOVE5
  368. @LMOVE3:
  369. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  370. subq.l #1,d0
  371. bne @LMOVE3
  372. bra @LMOVE5
  373. {$ifdef CPUM68K_HAS_DBRA}
  374. @LMEMSET00: { use fast loop mode 68010+ }
  375. cmp.l a0,a1 { check copy direction }
  376. bls @LMOVE04
  377. add.l d0,a0 { move pointers to end }
  378. add.l d0,a1
  379. bra @LMOVE02
  380. @LMOVE01:
  381. move.b -(a0),-(a1) { (s < d) copy loop }
  382. @LMOVE02:
  383. dbra d0,@LMOVE01
  384. bra @LMOVE5
  385. @LMOVE03:
  386. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  387. @LMOVE04:
  388. dbra d0,@LMOVE03
  389. {$endif CPUM68K_HAS_DBRA}
  390. { end fast loop mode }
  391. @LMOVE5:
  392. end;
  393. {$endif ndef FPC_SYSTEM_HAS_MOVE}
  394. {$ifdef CPUM68K_HAS_UNALIGNED}
  395. {$define FPC_SYSTEM_HAS_FILLWORD}
  396. procedure FillWord(var x; count : longint; value : word); assembler;
  397. asm
  398. move.l x, a0 { destination }
  399. move.w value, d1 { fill data }
  400. move.l count, d0 { number of bytes to fill }
  401. ble @LMEMSET3 { anything to fill at all? }
  402. bra @LMEMSET21
  403. @LMEMSET11:
  404. move.w d1,(a0)+
  405. @LMEMSET21:
  406. subq.l #1,d0
  407. bpl @LMEMSET11
  408. @LMEMSET3:
  409. end;
  410. {$endif}
  411. {$IFNDEF FPC_SYSTEM_HAS_INTERLOCKEDFUNCS}
  412. {$IFNDEF CPUM68K_HAS_CAS}
  413. var
  414. spinLock: byte;
  415. procedure getSpinLock; assembler; nostackframe;
  416. asm
  417. {$IFDEF CPUM68K_HAS_TAS}
  418. lea.l spinlock,a0
  419. @loop:
  420. tas (a0)
  421. bne @loop
  422. {$ENDIF}
  423. end;
  424. procedure releaseSpinLock; assembler; nostackframe;
  425. asm
  426. moveq.l #0,d0
  427. move.b d0,spinlock
  428. end;
  429. {$ENDIF}
  430. function InterLockedDecrement (var Target: longint) : longint;
  431. {$IFDEF CPUM68K_HAS_CAS}
  432. register; assembler;
  433. asm
  434. move.l (a0), d0
  435. @loop:
  436. move.l d0, d1
  437. subq.l #1, d1
  438. cas.l d0, d1, (a0)
  439. bne @loop
  440. move.l d1, d0
  441. end;
  442. {$ELSE}
  443. begin
  444. getSpinLock;
  445. Dec(Target);
  446. Result := Target;
  447. releaseSpinLock;
  448. end;
  449. {$ENDIF}
  450. function InterLockedIncrement (var Target: longint) : longint;
  451. {$IFDEF CPUM68K_HAS_CAS}
  452. register; assembler;
  453. asm
  454. move.l (a0), d0
  455. @loop:
  456. move.l d0, d1
  457. addq.l #1, d1
  458. cas.l d0, d1, (a0)
  459. bne @loop
  460. move.l d1, d0
  461. end;
  462. {$ELSE}
  463. begin
  464. getSpinLock;
  465. Inc(Target);
  466. Result := Target;
  467. releaseSpinLock;
  468. end;
  469. {$ENDIF}
  470. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  471. {$IFDEF CPUM68K_HAS_CAS}
  472. register; assembler;
  473. asm
  474. move.l Source, d1
  475. move.l (a0), d0
  476. @loop:
  477. cas.l d0, d1, (a0)
  478. bne @loop
  479. end;
  480. {$ELSE}
  481. begin
  482. getSpinLock;
  483. Result := Target;
  484. Target := Source;
  485. releaseSpinLock;
  486. end;
  487. {$ENDIF}
  488. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  489. {$IFDEF CPUM68K_HAS_CAS}
  490. register; assembler;
  491. asm
  492. move.l Source, a1
  493. move.l (a0), d0
  494. @loop:
  495. move.l a1, d1
  496. add.l d0, d1
  497. cas.l d0, d1, (a0)
  498. bne @loop
  499. end;
  500. {$ELSE}
  501. begin
  502. getSpinLock;
  503. Result := Target;
  504. Target := Target + Source;
  505. releaseSpinLock;
  506. end;
  507. {$ENDIF}
  508. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  509. {$IFDEF CPUM68K_HAS_CAS}
  510. register; assembler;
  511. asm
  512. // Target = a0, NewValue = d0, Comperand = d1
  513. exg.l d0, d1
  514. cas.l d0, d1, (a0)
  515. end;
  516. {$ELSE}
  517. begin
  518. getSpinLock;
  519. Result := Target;
  520. if Target = Comperand then
  521. Target := NewValue;
  522. releaseSpinLock;
  523. end;
  524. {$ENDIF}
  525. {$ENDIF FPC_SYSTEM_HAS_INTERLOCKEDFUNCS}
  526. {$ifndef FPC_SYSTEM_HAS_TEST68K}
  527. procedure Test68k(var CPU: byte; var FPU: byte);
  528. begin
  529. {$warning Implement me!}
  530. CPU:=0;
  531. FPU:=0;
  532. end;
  533. {$endif}
  534. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  535. {$define FPC_SYSTEM_HAS_SAR_QWORD}
  536. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc; assembler; nostackframe;
  537. asm
  538. // d0 = shift
  539. lea.l 4(sp),a0
  540. move.l d2,-(sp)
  541. move.l d0,d2
  542. {$ifndef CPUCOLDFIRE}
  543. and.w #63,d2
  544. cmp.w #32,d2
  545. {$else}
  546. and.l #63,d2
  547. cmp.l #32,d2
  548. {$endif}
  549. bge.s @longshift
  550. move.l (a0)+,d0
  551. move.l (a0),d1
  552. {$ifdef CPUM68K_HAS_ROLROR}
  553. cmp.w #1,d2
  554. beq.s @oneshift
  555. {$endif}
  556. {$ifdef CPU68000}
  557. cmp.w #16,d2
  558. beq.s @sixteenshift
  559. {$endif}
  560. move.l d3,a0
  561. move.l d4,a1
  562. move.l d0,d3
  563. moveq.l #32,d4
  564. {$ifndef CPUCOLDFIRE}
  565. sub.w d2,d4
  566. {$else}
  567. sub.l d2,d4
  568. {$endif}
  569. asr.l d2,d0
  570. lsl.l d4,d3
  571. lsr.l d2,d1
  572. or.l d3,d1
  573. move.l a0,d3
  574. move.l a1,d4
  575. bra.s @quit
  576. {$ifdef CPU68000}
  577. @sixteenshift:
  578. move.w d0,d1
  579. swap d1
  580. swap d0
  581. ext.l d0
  582. bra.s @quit
  583. {$endif}
  584. {$ifdef CPUM68K_HAS_ROLROR}
  585. @oneshift:
  586. asr.l #1,d0
  587. roxr.l #1,d1
  588. bra.s @quit
  589. {$endif}
  590. @longshift:
  591. move.l (a0),d0
  592. move.l d0,d1
  593. smi d0
  594. {$if defined(CPU68020) or defined(CPUCOLDFIRE)}
  595. extb.l d0
  596. {$else}
  597. ext.w d0
  598. ext.l d0
  599. {$endif}
  600. {$ifndef CPUCOLDFIRE}
  601. sub.w #32,d2
  602. {$else}
  603. sub.l #32,d2
  604. {$endif}
  605. asr.l d2,d1
  606. @quit:
  607. move.l (sp)+,d2
  608. end;
  609. {$endif}
  610. {$if defined(CPUM68K_HAS_BYTEREV) or defined(CPUM68K_HAS_ROLROR)}
  611. { Disabled for now, because not all cases below were tested. (KB) }
  612. {.$define FPC_SYSTEM_HAS_SWAPENDIAN}
  613. {$endif}
  614. {$if defined(FPC_SYSTEM_HAS_SWAPENDIAN)}
  615. function SwapEndian(const AValue: SmallInt): SmallInt; assembler; nostackframe;
  616. asm
  617. {$if defined(CPUM68K_HAS_ROLROR)}
  618. move.w avalue, d0
  619. ror.w #8, d0
  620. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  621. move.w avalue, d0
  622. byterev d0
  623. swap d0
  624. {$else}
  625. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  626. clr.l d0
  627. move.w avalue, d0
  628. move.w d0, d1
  629. lsr.l #8, d0
  630. lsl.l #8, d1
  631. or.l d1, d0
  632. {$endif}
  633. end;
  634. function SwapEndian(const AValue: Word): Word; assembler; nostackframe;
  635. asm
  636. {$if defined(CPUM68K_HAS_ROLROR)}
  637. move.w avalue, d0
  638. ror.w #8, d0
  639. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  640. move.w avalue, d0
  641. byterev d0
  642. swap d0
  643. {$else}
  644. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  645. clr.l d0
  646. move.w avalue, d0
  647. move.w d0, d1
  648. lsr.l #8, d0
  649. lsl.l #8, d1
  650. or.l d1, d0
  651. {$endif}
  652. end;
  653. function SwapEndian(const AValue: LongInt): LongInt; assembler; nostackframe;
  654. asm
  655. {$if defined(CPUM68K_HAS_ROLROR)}
  656. move.l avalue, d0
  657. ror.w #8, d0
  658. swap d0
  659. ror.w #8, d0
  660. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  661. move.l avalue, d0
  662. byterev d0
  663. {$else}
  664. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  665. move.l avalue, d0
  666. move.l d0, d1
  667. andi.l #$ff00ff00, d0
  668. andi.l #$00ff00ff, d1
  669. lsr.l #8, d0
  670. lsl.l #8, d1
  671. or.l d1, d0
  672. swap d0
  673. {$endif}
  674. end;
  675. function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe;
  676. asm
  677. {$if defined(CPUM68K_HAS_ROLROR)}
  678. move.l avalue, d0
  679. ror.w #8, d0
  680. swap d0
  681. ror.w #8, d0
  682. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  683. move.l avalue, d0
  684. byterev d0
  685. {$else}
  686. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  687. move.l avalue, d0
  688. move.l d0, d1
  689. andi.l #$ff00ff00, d0
  690. andi.l #$00ff00ff, d1
  691. lsr.l #8, d0
  692. lsl.l #8, d1
  693. or.l d1, d0
  694. swap d0
  695. {$endif}
  696. end;
  697. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  698. asm
  699. {$if defined(CPUM68K_HAS_ROLROR)}
  700. move.l avalue+4, d0
  701. ror.w #8, d0
  702. swap d0
  703. ror.w #8, d0
  704. move.l avalue, d1
  705. ror.w #8, d1
  706. swap d1
  707. ror.w #8, d1
  708. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  709. move.l avalue+4, d0
  710. move.l avalue, d1
  711. byterev d0
  712. byterev d1
  713. {$else}
  714. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  715. move.l d2, -(sp)
  716. move.l avalue+4, d0
  717. move.l d0, d1
  718. andi.l #$ff00ff00, d0
  719. andi.l #$00ff00ff, d1
  720. lsr.l #8, d0
  721. lsl.l #8, d1
  722. or.l d1, d0
  723. swap d0
  724. move.l avalue, d1
  725. move.l d1, d2
  726. andi.l #$ff00ff00, d1
  727. andi.l #$00ff00ff, d2
  728. lsr.l #8, d1
  729. lsl.l #8, d2
  730. or.l d2, d1
  731. swap d1
  732. move.l (sp)+, d2
  733. {$endif}
  734. end;
  735. function SwapEndian(const AValue: QWord): QWord; assembler; nostackframe;
  736. asm
  737. {$if defined(CPUM68K_HAS_ROLROR)}
  738. move.l avalue+4, d0
  739. ror.w #8, d0
  740. swap d0
  741. ror.w #8, d0
  742. move.l avalue, d1
  743. ror.w #8, d1
  744. swap d1
  745. ror.w #8, d1
  746. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  747. move.l avalue+4, d0
  748. move.l avalue, d1
  749. byterev d0
  750. byterev d1
  751. {$else}
  752. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  753. move.l d2, -(sp)
  754. move.l avalue+4, d0
  755. move.l d0, d1
  756. andi.l #$ff00ff00, d0
  757. andi.l #$00ff00ff, d1
  758. lsr.l #8, d0
  759. lsl.l #8, d1
  760. or.l d1, d0
  761. swap d0
  762. move.l avalue, d1
  763. move.l d1, d2
  764. andi.l #$ff00ff00, d1
  765. andi.l #$00ff00ff, d2
  766. lsr.l #8, d1
  767. lsl.l #8, d2
  768. or.l d2, d1
  769. swap d1
  770. move.l (sp)+, d2
  771. {$endif}
  772. end;
  773. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  774. procedure fpc_cpucodeinit;
  775. begin
  776. Test68k(Test68000,Test68881);
  777. end;