m68k.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. {$IFNDEF FPU_SOFT}
  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. function InterLockedDecrement (var Target: longint) : longint;
  344. begin
  345. {$warning FIX ME}
  346. Dec(Target);
  347. Result := Target;
  348. end;
  349. function InterLockedIncrement (var Target: longint) : longint;
  350. begin
  351. {$warning FIX ME}
  352. Inc(Target);
  353. Result := Target;
  354. end;
  355. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  356. begin
  357. {$warning FIX ME}
  358. Result := Target;
  359. Target := Source;
  360. end;
  361. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  362. begin
  363. {$warning FIX ME}
  364. Result := Target;
  365. Target := Target + Source;
  366. end;
  367. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  368. begin
  369. {$warning FIX ME}
  370. Result := Target;
  371. if Target = Comperand then
  372. Target := NewValue;
  373. end;
  374. {$if defined(CPUM68K_HAS_BYTEREV) or defined(CPUM68K_HAS_ROLROR)}
  375. { Disabled for now, because not all cases below were tested. (KB) }
  376. {.$define FPC_SYSTEM_HAS_SWAPENDIAN}
  377. {$endif}
  378. {$if defined(FPC_SYSTEM_HAS_SWAPENDIAN)}
  379. function SwapEndian(const AValue: SmallInt): SmallInt; assembler; nostackframe;
  380. asm
  381. {$if defined(CPUM68K_HAS_ROLROR)}
  382. move.w avalue, d0
  383. ror.w #8, d0
  384. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  385. move.w avalue, d0
  386. byterev d0
  387. swap d0
  388. {$else}
  389. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  390. clr.l d0
  391. move.w avalue, d0
  392. move.w d0, d1
  393. lsr.l #8, d0
  394. lsl.l #8, d1
  395. or.l d1, d0
  396. {$endif}
  397. end;
  398. function SwapEndian(const AValue: Word): Word; assembler; nostackframe;
  399. asm
  400. {$if defined(CPUM68K_HAS_ROLROR)}
  401. move.w avalue, d0
  402. ror.w #8, d0
  403. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  404. move.w avalue, d0
  405. byterev d0
  406. swap d0
  407. {$else}
  408. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  409. clr.l d0
  410. move.w avalue, d0
  411. move.w d0, d1
  412. lsr.l #8, d0
  413. lsl.l #8, d1
  414. or.l d1, d0
  415. {$endif}
  416. end;
  417. function SwapEndian(const AValue: LongInt): LongInt; assembler; nostackframe;
  418. asm
  419. {$if defined(CPUM68K_HAS_ROLROR)}
  420. move.l avalue, d0
  421. ror.w #8, d0
  422. swap d0
  423. ror.w #8, d0
  424. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  425. move.l avalue, d0
  426. byterev d0
  427. {$else}
  428. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  429. move.l avalue, d0
  430. move.l d0, d1
  431. andi.l #$ff00ff00, d0
  432. andi.l #$00ff00ff, d1
  433. lsr.l #8, d0
  434. lsl.l #8, d1
  435. or.l d1, d0
  436. swap d0
  437. {$endif}
  438. end;
  439. function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe;
  440. asm
  441. {$if defined(CPUM68K_HAS_ROLROR)}
  442. move.l avalue, d0
  443. ror.w #8, d0
  444. swap d0
  445. ror.w #8, d0
  446. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  447. move.l avalue, d0
  448. byterev d0
  449. {$else}
  450. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  451. move.l avalue, d0
  452. move.l d0, d1
  453. andi.l #$ff00ff00, d0
  454. andi.l #$00ff00ff, d1
  455. lsr.l #8, d0
  456. lsl.l #8, d1
  457. or.l d1, d0
  458. swap d0
  459. {$endif}
  460. end;
  461. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  462. asm
  463. {$if defined(CPUM68K_HAS_ROLROR)}
  464. move.l avalue+4, d0
  465. ror.w #8, d0
  466. swap d0
  467. ror.w #8, d0
  468. move.l avalue, d1
  469. ror.w #8, d1
  470. swap d1
  471. ror.w #8, d1
  472. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  473. move.l avalue+4, d0
  474. move.l avalue, d1
  475. byterev d0
  476. byterev d1
  477. {$else}
  478. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  479. move.l d2, -(sp)
  480. move.l avalue+4, d0
  481. move.l d0, d1
  482. andi.l #$ff00ff00, d0
  483. andi.l #$00ff00ff, d1
  484. lsr.l #8, d0
  485. lsl.l #8, d1
  486. or.l d1, d0
  487. swap d0
  488. move.l avalue, d1
  489. move.l d1, d2
  490. andi.l #$ff00ff00, d1
  491. andi.l #$00ff00ff, d2
  492. lsr.l #8, d1
  493. lsl.l #8, d2
  494. or.l d2, d1
  495. swap d1
  496. move.l (sp)+, d2
  497. {$endif}
  498. end;
  499. function SwapEndian(const AValue: QWord): QWord; assembler; nostackframe;
  500. asm
  501. {$if defined(CPUM68K_HAS_ROLROR)}
  502. move.l avalue+4, d0
  503. ror.w #8, d0
  504. swap d0
  505. ror.w #8, d0
  506. move.l avalue, d1
  507. ror.w #8, d1
  508. swap d1
  509. ror.w #8, d1
  510. {$elseif defined(CPUM68K_HAS_BYTEREV)}
  511. move.l avalue+4, d0
  512. move.l avalue, d1
  513. byterev d0
  514. byterev d1
  515. {$else}
  516. // only ISA A/B ColdFire can end in this branch, so use long ops everywhere
  517. move.l d2, -(sp)
  518. move.l avalue+4, d0
  519. move.l d0, d1
  520. andi.l #$ff00ff00, d0
  521. andi.l #$00ff00ff, d1
  522. lsr.l #8, d0
  523. lsl.l #8, d1
  524. or.l d1, d0
  525. swap d0
  526. move.l avalue, d1
  527. move.l d1, d2
  528. andi.l #$ff00ff00, d1
  529. andi.l #$00ff00ff, d2
  530. lsr.l #8, d1
  531. lsl.l #8, d2
  532. or.l d2, d1
  533. swap d1
  534. move.l (sp)+, d2
  535. {$endif}
  536. end;
  537. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}