m68k.inc 16 KB

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