m68k.inc 20 KB

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