m68k.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. { 68881/2 FPCR Encodings
  25. Rounding Mode Rounding Precision
  26. (RND Field) Encoding (PREC Field)
  27. To Nearest (RN) 0 0 Extend (X)
  28. To Zero (RZ) 0 1 Single (S)
  29. To Minus Infinity (RM) 1 0 Double (D)
  30. To Plus Infinity (RP) 1 1 Undefined
  31. }
  32. { 68881/2 FPCR layout }
  33. { Exception Enable Byte: }
  34. { 15 - BSUN - Branch/Set on Unordered }
  35. { 14 - SNAN - Signal Not A Number }
  36. { 13 - OPERR - Operand Error }
  37. { 12 - OVFL - Overflow }
  38. { 11 - UNFL - Underflow }
  39. { 10 - DZ - Divide by Zero }
  40. { 09 - INEX2 - Inexact Operation }
  41. { 08 - INEX1 - Inexact Decimal Input }
  42. { Mode Control Byte: }
  43. { 07 - PREC - Rounding Precision }
  44. { 06 - PREC - Rounding Precision }
  45. { 05 - RND - Rounding Mode }
  46. { 04 - RND - Rounding Mode }
  47. { 03 - 0 - Reserved, Set to zero }
  48. { 02 - 0 - Reserved, Set to zero }
  49. { 01 - 0 - Reserved, Set to zero }
  50. { 00 - 0 - Reserved, Set to zero }
  51. {$IFDEF FPU68881}
  52. {$DEFINE FPC_SYSTEM_HAS_SYSRESETFPU}
  53. procedure SysResetFPU; assembler;
  54. asm
  55. clr.l d0
  56. fmove.l d0,fpcr
  57. end;
  58. {$DEFINE FPC_SYSTEM_HAS_SYSINITFPU}
  59. procedure SysInitFPU; assembler;
  60. asm
  61. clr.l d0
  62. // FIX ME:
  63. // move.w 0,d0 // enable a sane set of exception flags here
  64. fmove.l d0,fpcr
  65. end;
  66. {$ENDIF}
  67. procedure fpc_cpuinit;
  68. begin
  69. SysResetFPU;
  70. if (not IsLibrary) then
  71. SysInitFPU;
  72. end;
  73. {$define FPC_SYSTEM_HAS_GET_FRAME}
  74. function get_frame : pointer; assembler;nostackframe;
  75. asm
  76. {$if defined(amiga)}
  77. move.l a5,d0
  78. {$else}
  79. move.l a6,d0
  80. {$endif}
  81. end;
  82. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  83. function get_caller_addr(framebp : pointer;addr:pointer=nil) : pointer;
  84. begin
  85. asm
  86. move.l framebp,a0
  87. cmp.l #0,a0
  88. beq @Lnul_address
  89. move.l 4(a0),a0
  90. @Lnul_address:
  91. move.l a0,@RESULT
  92. end ['a0'];
  93. end;
  94. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  95. function get_caller_frame(framebp : pointer;addr:pointer=nil) : pointer;
  96. begin
  97. asm
  98. move.l FRAMEBP,a0
  99. cmp.l #0,a0
  100. beq @Lnul_frame
  101. move.l (a0),a0
  102. @Lnul_frame:
  103. move.l a0,@RESULT
  104. end ['a0'];
  105. end;
  106. {$define FPC_SYSTEM_HAS_SPTR}
  107. function Sptr : pointer; assembler;nostackframe;
  108. asm
  109. move.l sp,d0
  110. end;
  111. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  112. function get_pc_addr : pointer;assembler;nostackframe;
  113. asm
  114. move.l (sp),d0
  115. end;
  116. {$define FPC_SYSTEM_HAS_FILLCHAR}
  117. procedure FillChar(var x; count : longint; value : byte); assembler;
  118. asm
  119. move.l x, a0 { destination }
  120. move.l count, d1 { number of bytes to fill }
  121. move.b value, d0 { fill data }
  122. tst.l d1 { anything to fill at all? }
  123. ble @LMEMSET5
  124. {$ifdef CPUM68K_HAS_DBRA}
  125. { FIXME: Any reason why not always just use DBRA mode on
  126. CPUs which support it? (KB)
  127. - DBRA does only 16-bit decrements, so handling more than 65535 bytes
  128. requires additional code anyway (Sergei) }
  129. cmpi.l #65535, d1 { check, if this is a word move }
  130. ble @LMEMSET3 { use fast dbra mode }
  131. {$endif CPUM68K_HAS_DBRA}
  132. bra @LMEMSET2
  133. @LMEMSET1:
  134. move.b d0,(a0)+
  135. @LMEMSET2:
  136. subq.l #1,d1
  137. bpl @LMEMSET1
  138. bra @LMEMSET5 { finished slow mode , exit }
  139. {$ifdef CPUM68K_HAS_DBRA}
  140. @LMEMSET4: { fast loop mode section 68010+ }
  141. move.b d0,(a0)+
  142. @LMEMSET3:
  143. dbra d1,@LMEMSET4
  144. {$endif CPUM68K_HAS_DBRA}
  145. @LMEMSET5:
  146. end;
  147. {$ifdef dummy}
  148. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  149. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  150. {---------------------------------------------------}
  151. { Low-level routine to copy a string to another }
  152. { string with maximum length. Never call directly! }
  153. { On Entry: }
  154. { a1.l = string to copy to }
  155. { a0.l = source string }
  156. { d0.l = maximum length of copy }
  157. { registers destroyed: a0,a1,d0,d1 }
  158. {---------------------------------------------------}
  159. asm
  160. { move.l sstr,a0
  161. move.l dstr,a1
  162. move.l len,d1 }
  163. move.l d0,d1
  164. move.b (a0)+,d0 { Get source length }
  165. and.w #$ff,d0
  166. cmp.w d1,d0 { This is a signed comparison! }
  167. ble @LM4
  168. move.b d1,d0 { If longer than maximum size of target, cut
  169. source length }
  170. @LM4:
  171. andi.l #$ff,d0 { zero extend d0-byte }
  172. move.l d0,d1 { save length to copy }
  173. move.b d0,(a1)+ { save new length }
  174. { Check if copying length is zero - if so then }
  175. { exit without copying anything. }
  176. tst.b d1
  177. beq @Lend
  178. bra @LMSTRCOPY55
  179. @LMSTRCOPY56: { 68010 Fast loop mode }
  180. move.b (a0)+,(a1)+
  181. @LMSTRCOPY55:
  182. {$ifndef CPUM68K_HAS_DBRA}
  183. sub.l #1,d1
  184. bpl @LMSTRCOPY56
  185. {$else CPUM68K_HAS_DBRA}
  186. dbra d1,@LMSTRCOPY56
  187. {$endif CPUM68K_HAS_DBRA}
  188. @Lend:
  189. end;
  190. { Concatenate Strings }
  191. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  192. { therefore online assembler may not parse the params as normal }
  193. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  194. begin
  195. asm
  196. move.b #255,d0
  197. move.l s1,a0 { a0 = destination }
  198. move.l s2,a1 { a1 = source }
  199. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  200. move.b (a1),d6
  201. and.w #$ff,d0 { Sign flags are checked! }
  202. and.w #$ff,d6
  203. cmp.w d6,d0 { if copyl > length(s2) then }
  204. ble @Lcontinue
  205. move.b (a1),d0 { copyl:=length(s2) }
  206. @Lcontinue:
  207. move.b (a0),d6
  208. and.l #$ff,d6
  209. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  210. add.l #1,a1 { s2[1] }
  211. move.b d0,d6
  212. { Check if copying length is zero - if so then }
  213. { exit without copying anything. }
  214. tst.b d6
  215. beq @Lend
  216. bra @ALoop
  217. @Loop:
  218. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  219. @ALoop:
  220. {$ifndef CPUM68K_HAS_DBRA}
  221. sub.l #1,d6
  222. bpl @Loop
  223. {$else CPUM68K_HAS_DBRA}
  224. dbra d6,@Loop
  225. {$endif CPUM68K_HAS_DBRA}
  226. move.l s1,a0
  227. add.b d0,(a0) { change to new string length }
  228. @Lend:
  229. end ['d0','d1','a0','a1','d6'];
  230. end;
  231. { Compares strings }
  232. { DO NOT CALL directly. }
  233. { a0 = pointer to first string to compare }
  234. { a1 = pointer to second string to compare }
  235. { ALL FLAGS are set appropriately. }
  236. { ZF = strings are equal }
  237. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  238. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  239. asm
  240. move.b (a0)+,d0 { Get length of first string }
  241. move.b (a1)+,d6 { Get length of 2nd string }
  242. move.b d6,d1 { Save length of string for final compare }
  243. cmp.b d0,d6 { Get shortest string length }
  244. ble @LSTRCONCAT1
  245. move.b d0,d6 { Set length to shortest string }
  246. @LSTRCONCAT1:
  247. tst.b d6 { Both strings have a length of zero, exit }
  248. beq @LSTRCONCAT2
  249. andi.l #$ff,d6
  250. subq.l #1,d6 { subtract first attempt }
  251. { if value is -1 then don't loop and just compare lengths of }
  252. { both strings before exiting. }
  253. bmi @LSTRCONCAT2
  254. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  255. @LSTRCONCAT5:
  256. { Workaroung for GAS v.134 bug }
  257. { old: cmp.b (a1)+,(a0)+ }
  258. cmpm.b (a1)+,(a0)+
  259. @LSTRCONCAT4:
  260. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  261. bne @LSTRCONCAT3
  262. @LSTRCONCAT2:
  263. { If length of both string are equal }
  264. { Then set zero flag }
  265. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  266. @LSTRCONCAT3:
  267. end;
  268. {$endif dummy}
  269. {$define FPC_SYSTEM_HAS_MOVE}
  270. procedure move(const source;var dest;count : longint);
  271. { base pointer+8 = source }
  272. { base pointer+12 = destination }
  273. { base pointer+16 = number of bytes to move}
  274. begin
  275. asm
  276. clr.l d0
  277. move.l count, d0 { number of bytes }
  278. tst.l d0 { anything to copy at all? }
  279. ble @LMOVE5
  280. @LMOVE0:
  281. move.l dest, a1 { destination }
  282. move.l source, a0 { source }
  283. {$ifdef CPUM68K_HAS_DBRA}
  284. cmpi.l #65535, d0 { check, if this is a word move }
  285. ble @LMEMSET00 { use fast dbra mode 68010+ }
  286. {$endif CPUM68K_HAS_DBRA}
  287. cmp.l a0,a1 { check copy direction }
  288. bls @LMOVE4
  289. add.l d0,a0 { move pointers to end }
  290. add.l d0,a1
  291. bra @LMOVE2
  292. @LMOVE1:
  293. move.b -(a0),-(a1) { (s < d) copy loop }
  294. @LMOVE2:
  295. subq.l #1,d0
  296. cmpi.l #-1,d0
  297. bne @LMOVE1
  298. bra @LMOVE5
  299. @LMOVE3:
  300. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  301. @LMOVE4:
  302. subq.l #1,d0
  303. cmpi.l #-1,d0
  304. bne @LMOVE3
  305. bra @LMOVE5
  306. {$ifdef CPUM68K_HAS_DBRA}
  307. @LMEMSET00: { use fast loop mode 68010+ }
  308. cmp.l a0,a1 { check copy direction }
  309. bls @LMOVE04
  310. add.l d0,a0 { move pointers to end }
  311. add.l d0,a1
  312. bra @LMOVE02
  313. @LMOVE01:
  314. move.b -(a0),-(a1) { (s < d) copy loop }
  315. @LMOVE02:
  316. dbra d0,@LMOVE01
  317. bra @LMOVE5
  318. @LMOVE03:
  319. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  320. @LMOVE04:
  321. dbra d0,@LMOVE03
  322. {$endif CPUM68K_HAS_DBRA}
  323. { end fast loop mode }
  324. @LMOVE5:
  325. end ['d0','a0','a1'];
  326. end;
  327. {$define FPC_SYSTEM_HAS_FILLWORD}
  328. procedure FillWord(var x; count : longint; value : word); assembler;
  329. asm
  330. move.l x, a0 { destination }
  331. move.l count, d1 { number of bytes to fill }
  332. move.w value, d0 { fill data }
  333. tst.l d1 { anything to fill at all? }
  334. ble @LMEMSET3
  335. bra @LMEMSET21
  336. @LMEMSET11:
  337. move.w d0,(a0)+
  338. @LMEMSET21:
  339. subq.l #1,d1
  340. bpl @LMEMSET11
  341. @LMEMSET3:
  342. end;
  343. {$IFNDEF HASAMIGA}
  344. function InterLockedDecrement (var Target: longint) : longint;
  345. begin
  346. {$warning FIX ME}
  347. Dec(Target);
  348. Result := Target;
  349. end;
  350. function InterLockedIncrement (var Target: longint) : longint;
  351. begin
  352. {$warning FIX ME}
  353. Inc(Target);
  354. Result := Target;
  355. end;
  356. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  357. begin
  358. {$warning FIX ME}
  359. Result := Target;
  360. Target := Source;
  361. end;
  362. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  363. begin
  364. {$warning FIX ME}
  365. Result := Target;
  366. Target := Target + Source;
  367. end;
  368. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  369. begin
  370. {$warning FIX ME}
  371. Result := Target;
  372. if Target = Comperand then
  373. Target := NewValue;
  374. end;
  375. {$ENDIF HASAMIGA}
  376. {$if defined(CPUM68K_HAS_BYTEREV) or defined(CPUM68K_HAS_ROLROR)}
  377. { Disabled for now, because not all cases below were tested. (KB) }
  378. {.$define FPC_SYSTEM_HAS_SWAPENDIAN}
  379. {$endif}
  380. {$if defined(FPC_SYSTEM_HAS_SWAPENDIAN)}
  381. function SwapEndian(const AValue: SmallInt): SmallInt; assembler; nostackframe;
  382. asm
  383. {$if defined(CPUM68K_HAS_ROLROR)}
  384. move.w avalue, d0
  385. ror.w #8, d0
  386. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  387. move.w avalue, d0
  388. byterev d0
  389. swap d0
  390. {$else}
  391. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  392. clr.l d0
  393. move.w avalue, d0
  394. move.w d0, d1
  395. lsr.l #8, d0
  396. lsl.l #8, d1
  397. or.l d1, d0
  398. {$endif}
  399. end;
  400. function SwapEndian(const AValue: Word): Word; assembler; nostackframe;
  401. asm
  402. {$if defined(CPUM68K_HAS_ROLROR)}
  403. move.w avalue, d0
  404. ror.w #8, d0
  405. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  406. move.w avalue, d0
  407. byterev d0
  408. swap d0
  409. {$else}
  410. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  411. clr.l d0
  412. move.w avalue, d0
  413. move.w d0, d1
  414. lsr.l #8, d0
  415. lsl.l #8, d1
  416. or.l d1, d0
  417. {$endif}
  418. end;
  419. function SwapEndian(const AValue: LongInt): LongInt; assembler; nostackframe;
  420. asm
  421. {$if defined(CPUM68K_HAS_ROLROR)}
  422. move.l avalue, d0
  423. ror.w #8, d0
  424. swap d0
  425. ror.w #8, d0
  426. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  427. move.l avalue, d0
  428. byterev d0
  429. {$else}
  430. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  431. move.l avalue, d0
  432. move.l d0, d1
  433. andi.l #$ff00ff00, d0
  434. andi.l #$00ff00ff, d1
  435. lsr.l #8, d0
  436. lsl.l #8, d1
  437. or.l d1, d0
  438. swap d0
  439. {$endif}
  440. end;
  441. function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe;
  442. asm
  443. {$if defined(CPUM68K_HAS_ROLROR)}
  444. move.l avalue, d0
  445. ror.w #8, d0
  446. swap d0
  447. ror.w #8, d0
  448. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  449. move.l avalue, d0
  450. byterev d0
  451. {$else}
  452. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  453. move.l avalue, d0
  454. move.l d0, d1
  455. andi.l #$ff00ff00, d0
  456. andi.l #$00ff00ff, d1
  457. lsr.l #8, d0
  458. lsl.l #8, d1
  459. or.l d1, d0
  460. swap d0
  461. {$endif}
  462. end;
  463. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  464. asm
  465. {$if defined(CPUM68K_HAS_ROLROR)}
  466. move.l avalue+4, d0
  467. ror.w #8, d0
  468. swap d0
  469. ror.w #8, d0
  470. move.l avalue, d1
  471. ror.w #8, d1
  472. swap d1
  473. ror.w #8, d1
  474. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  475. move.l avalue+4, d0
  476. move.l avalue, d1
  477. byterev d0
  478. byterev d1
  479. {$else}
  480. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  481. move.l d2, -(sp)
  482. move.l avalue+4, d0
  483. move.l d0, d1
  484. andi.l #$ff00ff00, d0
  485. andi.l #$00ff00ff, d1
  486. lsr.l #8, d0
  487. lsl.l #8, d1
  488. or.l d1, d0
  489. swap d0
  490. move.l avalue, d1
  491. move.l d1, d2
  492. andi.l #$ff00ff00, d1
  493. andi.l #$00ff00ff, d2
  494. lsr.l #8, d1
  495. lsl.l #8, d2
  496. or.l d2, d1
  497. swap d1
  498. move.l (sp)+, d2
  499. {$endif}
  500. end;
  501. function SwapEndian(const AValue: QWord): QWord; assembler; nostackframe;
  502. asm
  503. {$if defined(CPUM68K_HAS_ROLROR)}
  504. move.l avalue+4, d0
  505. ror.w #8, d0
  506. swap d0
  507. ror.w #8, d0
  508. move.l avalue, d1
  509. ror.w #8, d1
  510. swap d1
  511. ror.w #8, d1
  512. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  513. move.l avalue+4, d0
  514. move.l avalue, d1
  515. byterev d0
  516. byterev d1
  517. {$else}
  518. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  519. move.l d2, -(sp)
  520. move.l avalue+4, d0
  521. move.l d0, d1
  522. andi.l #$ff00ff00, d0
  523. andi.l #$00ff00ff, d1
  524. lsr.l #8, d0
  525. lsl.l #8, d1
  526. or.l d1, d0
  527. swap d0
  528. move.l avalue, d1
  529. move.l d1, d2
  530. andi.l #$ff00ff00, d1
  531. andi.l #$00ff00ff, d2
  532. lsr.l #8, d1
  533. lsl.l #8, d2
  534. or.l d2, d1
  535. swap d1
  536. move.l (sp)+, d2
  537. {$endif}
  538. end;
  539. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}