m68k.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Carl-Eric Codere,
  5. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. m68k.inc : Processor dependent implementation of system unit
  14. For Motorola 680x0 Processor.
  15. *****************************************************************************}
  16. {****************************************************************************}
  17. { Credit where credit is due: }
  18. { -Some of the copy routines taken from the Atari dlib source code: }
  19. { Dale Schumacher (alias: Dalnefre') [email protected] }
  20. { 399 Beacon Ave. St. Paul, MN 55104,USA }
  21. { -Some of the routines taken from the freeware ATARI Sozobon C compiler }
  22. { 1988 by Sozobon, Limited. Author: Johann Ruegg (freeware) }
  23. { Thanks to all these people wherever they maybe today! }
  24. {****************************************************************************}
  25. procedure fpc_cpuinit;
  26. begin
  27. end;
  28. { Don't call the following routines directly. }
  29. Procedure Hlt;[public,alias: 'FPC_HALT_ERROR'];
  30. { called by code generator on run-time errors. }
  31. { on entry contains d0 = error code. }
  32. var
  33. b:byte; { only byte is used... }
  34. begin
  35. asm
  36. move.b d0,b
  37. end;
  38. HandleError(b);
  39. end;
  40. Procedure FillChar(var x;count:longint;value:byte);[public,alias: 'FPC_FILL_OBJECT'];
  41. begin
  42. asm
  43. move.l 8(a6), a0 { destination }
  44. move.l 12(a6), d1 { number of bytes to fill }
  45. move.b 16(a6),d0 { fill data }
  46. cmpi.l #65535, d1 { check, if this is a word move }
  47. ble @LMEMSET3 { use fast dbra mode }
  48. bra @LMEMSET2
  49. @LMEMSET1:
  50. move.b d0,(a0)+
  51. @LMEMSET2:
  52. subq.l #1,d1
  53. cmp.l #-1,d1
  54. bne @LMEMSET1
  55. bra @LMEMSET5 { finished slow mode , exit }
  56. @LMEMSET4: { fast loop mode section 68010+ }
  57. move.b d0,(a0)+
  58. @LMEMSET3:
  59. dbra d1,@LMEMSET4
  60. @LMEMSET5:
  61. end ['d0','d1','a0'];
  62. end;
  63. Procedure FillObject(var x; count: longint; value: byte);
  64. begin
  65. asm
  66. move.l 8(a6), a0 { destination }
  67. move.l 12(a6), d1 { number of bytes to fill }
  68. move.w 16(a6),d0 { fill data }
  69. cmp.l #65535, d1 { check, if this is a word move }
  70. ble @LMEMSET3 { use fast dbra mode }
  71. bra @LMEMSET2
  72. @LMEMSET1:
  73. move.b d0,(a0)+
  74. @LMEMSET2:
  75. subq.l #1,d1
  76. cmp.l #-1,d1
  77. bne @LMEMSET1
  78. bra @LMEMSET5 { finished slow mode , exit }
  79. @LMEMSET4: { fast loop mode section 68010+ }
  80. move.b d0,(a0)+
  81. @LMEMSET3:
  82. dbra d1,@LMEMSET4
  83. @LMEMSET5:
  84. end ['d0','d1','a0'];
  85. end;
  86. procedure int_help_constructor;[public,alias:'FPC_HELP_CONSTRUCTOR'];
  87. begin
  88. asm
  89. { Entry without preamble, since we need the ESP of the
  90. constructor }
  91. { Stack (relative to %ebp):
  92. 12 Self
  93. 8 VMT-Address
  94. 4 main programm-Addr
  95. 0 %ebp
  96. }
  97. { do we have to initialize self }
  98. { we just need to check for zero }
  99. move.l a5,d0
  100. tst.l d0 { set flags }
  101. bne @LHC_4
  102. { get memory, but save register first }
  103. { temporary variable }
  104. subq.l #4,sp
  105. move.l sp,a5
  106. { Save Registers }
  107. movem.l d0-a7,-(sp)
  108. { Memory size }
  109. move.l 8(a6),a0
  110. move.l (a0),-(sp)
  111. { push method pointer }
  112. move.l a5,-(sp)
  113. jsr FPC_GETMEM
  114. { Restore all registers in the correct order }
  115. movem.l (sp)+,d0-a7
  116. { Memory position to a5 }
  117. move.l (a5),a5
  118. addq.l #4,sp
  119. { If no memory available : fail() }
  120. move.l a5,d0
  121. tst.l d0 { set flags for a5 }
  122. beq @LHC_5
  123. { init self for the constructor }
  124. move.l a5,12(a6)
  125. @LHC_4:
  126. { is there a VMT address ? }
  127. move.l 8(a6),d0
  128. or.l d0,d0
  129. bne @LHC_7
  130. { In case the constructor doesn't do anything, the Zero-Flag }
  131. { can't be put, because this calls Fail() }
  132. add.l #1,d0
  133. rts
  134. @LHC_7:
  135. { set zero inside the object }
  136. { Save Registers }
  137. movem.l d0-a7,-(sp)
  138. move.w #0,-(sp)
  139. move.l 8(a6),a0
  140. move.l (a0),-(sp)
  141. move.l a5,-(sp)
  142. { }
  143. jsr FPC_FILLOBJECT
  144. { Restore all registers in the correct order }
  145. movem.l (sp)+,d0-a7
  146. { set the VMT address for the new created object }
  147. {$ifdef OBJECTVMTOFFSET}
  148. { the offset is in %edi since the calling and has not been changed !! }
  149. move.l 8(a6),d1
  150. move.l d1,(a5,d0.l)
  151. {$else OBJECTVMTOFFSET}
  152. move.l 8(a6),d0
  153. move.l d0,(a5)
  154. {$endif OBJECTVMTOFFSET}
  155. or.l d0,d0
  156. @LHC_5:
  157. rts
  158. end;
  159. end;
  160. procedure help_fail;
  161. begin
  162. asm
  163. end;
  164. end;
  165. procedure int_help_destructor;[public,alias:'FPC_HELP_DESTRUCTOR'];
  166. begin
  167. asm
  168. { Stack (relative to %ebp):
  169. 12 Self
  170. 8 VMT-Address
  171. 4 Main program-Addr
  172. 0 %ebp
  173. d0 contains vmt_offset
  174. }
  175. { temporary Variable }
  176. subq.l #4,sp
  177. move.l sp,d6
  178. { Save Registers }
  179. movem.l d0-a7,-(sp)
  180. move.l 8(a6),d1 { Get the address of the vmt }
  181. or.l d1,d1 { Check if there is a vmt }
  182. beq @LHD_3
  183. { Yes, get size from SELF! }
  184. move.l 12(a6),a0
  185. { get VMT-pointer (from Self) to %ebx }
  186. {$ifdef OBJECTVMTOFFSET}
  187. { the offset is in d0 since the calling and has not been changed !! }
  188. move.l (a0,d0.l),a1
  189. {$else OBJECTVMTOFFSET}
  190. move.l (a0),a1
  191. {$endif OBJECTVMTOFFSET}
  192. { And put size on the Stack }
  193. move.l (a1),-(sp)
  194. { SELF }
  195. { I think for precaution }
  196. { that we should clear the VMT here }
  197. clr.l (a0)
  198. { get address of local variable into }
  199. { address register }
  200. move.l d6,a1
  201. move.l a0,(a1)
  202. move.l a1,-(sp)
  203. jsr FPC_FREEMEM
  204. @LHD_3:
  205. { Restore all registers in the correct order }
  206. movem.l (sp)+,d0-a7
  207. add.l #4,sp
  208. rts
  209. end;
  210. end;
  211. procedure new_class;assembler;[public,alias:'FPC_NEW_CLASS'];
  212. asm
  213. { create class ? }
  214. move.l 8(a6), d0
  215. tst.l d0
  216. { check for nil... }
  217. beq @NEW_CLASS1
  218. { a5 contains vmt }
  219. move.l a5,-(sp)
  220. { call newinstance (class method!) }
  221. jsr 16(a5)
  222. { new instance returns a pointer to the new created }
  223. { instance in d0 }
  224. { load a5 and insert self }
  225. move.l d0,8(a6)
  226. move.l d0,a5
  227. bra @end
  228. @NEW_CLASS1:
  229. move.l a5,8(a6)
  230. @end:
  231. end;
  232. procedure dispose_class;assembler;[public,alias:'FPC_DISPOSE_CLASS'];
  233. asm
  234. { destroy class ? }
  235. move.l 8(a6),d0
  236. { save self }
  237. move.l a5,8(a6)
  238. tst.l d0
  239. beq @DISPOSE_CLASS
  240. { no inherited call }
  241. move.l (a5),d0
  242. { push self }
  243. move.l a5,-(sp)
  244. { call freeinstance }
  245. move.l d0,a0
  246. jsr 20(a0)
  247. @DISPOSE_CLASS:
  248. { load self }
  249. move.l 8(a6),a5
  250. end;
  251. { checks for a correct vmt pointer }
  252. procedure int_check_object;assembler;[public,alias:'FPC_CHECK_OBJECT'];
  253. { ON ENTRY: a0 -> Pointer to the VMT }
  254. { Nota: All registers must be preserved including }
  255. { A0 itself! }
  256. asm
  257. move.l d0,-(sp)
  258. tst.l a0
  259. { z flag set if zero }
  260. beq @co_re
  261. move.l (a0),d0
  262. add.l 4(a0),d0
  263. bne @co_re
  264. bra @end
  265. @co_re:
  266. move.l (sp)+,d0
  267. move.b #210,d0
  268. jsr FPC_HALT_ERROR
  269. @end:
  270. move.l (sp)+,d0
  271. end;
  272. function get_frame : longint; assembler;
  273. asm
  274. move.l a6,d0
  275. end;
  276. function get_caller_addr(framebp:longint):longint;
  277. begin
  278. asm
  279. move.l FRAMEBP,a0
  280. cmp.l #0,a0
  281. beq @Lnul_address
  282. move.l 4(a0),a0
  283. @Lnul_address:
  284. move.l a0,@RESULT
  285. end ['a0'];
  286. end;
  287. function get_caller_frame(framebp:longint):longint;
  288. begin
  289. asm
  290. move.l FRAMEBP,a0
  291. cmp.l #0,a0
  292. beq @Lnul_frame
  293. move.l (a0),a0
  294. @Lnul_frame:
  295. move.l a0,@RESULT
  296. end ['a0'];
  297. end;
  298. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  299. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  300. {---------------------------------------------------}
  301. { Low-level routine to copy a string to another }
  302. { string with maximum length. Never call directly! }
  303. { On Entry: }
  304. { a1.l = string to copy to }
  305. { a0.l = source string }
  306. { d0.l = maximum length of copy }
  307. { registers destroyed: a0,a1,d0,d1 }
  308. {---------------------------------------------------}
  309. asm
  310. { move.l 12(a6),a0
  311. move.l 16(a6),a1
  312. move.l 8(a6),d1 }
  313. move.l d0,d1
  314. move.b (a0)+,d0 { Get source length }
  315. and.w #$ff,d0
  316. cmp.w d1,d0 { This is a signed comparison! }
  317. ble @LM4
  318. move.b d1,d0 { If longer than maximum size of target, cut
  319. source length }
  320. @LM4:
  321. andi.l #$ff,d0 { zero extend d0-byte }
  322. move.l d0,d1 { save length to copy }
  323. move.b d0,(a1)+ { save new length }
  324. { Check if copying length is zero - if so then }
  325. { exit without copying anything. }
  326. tst.b d1
  327. beq @Lend
  328. bra @LMSTRCOPY55
  329. @LMSTRCOPY56: { 68010 Fast loop mode }
  330. move.b (a0)+,(a1)+
  331. @LMSTRCOPY55:
  332. dbra d1,@LMSTRCOPY56
  333. @Lend:
  334. end;
  335. { Concatenate Strings }
  336. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  337. { therefore online assembler may not parse the params as normal }
  338. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  339. begin
  340. asm
  341. move.b #255,d0
  342. move.l s1,a0 { a0 = destination }
  343. move.l s2,a1 { a1 = source }
  344. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  345. move.b (a1),d6
  346. and.w #$ff,d0 { Sign flags are checked! }
  347. and.w #$ff,d6
  348. cmp.w d6,d0 { if copyl > length(s2) then }
  349. ble @Lcontinue
  350. move.b (a1),d0 { copyl:=length(s2) }
  351. @Lcontinue:
  352. move.b (a0),d6
  353. and.l #$ff,d6
  354. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  355. add.l #1,a1 { s2[1] }
  356. move.b d0,d6
  357. { Check if copying length is zero - if so then }
  358. { exit without copying anything. }
  359. tst.b d6
  360. beq @Lend
  361. bra @ALoop
  362. @Loop:
  363. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  364. @ALoop:
  365. dbra d6,@Loop
  366. move.l s1,a0
  367. add.b d0,(a0) { change to new string length }
  368. @Lend:
  369. end ['d0','d1','a0','a1','d6'];
  370. end;
  371. { Compares strings }
  372. { DO NOT CALL directly. }
  373. { a0 = pointer to first string to compare }
  374. { a1 = pointer to second string to compare }
  375. { ALL FLAGS are set appropriately. }
  376. { ZF = strings are equal }
  377. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  378. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  379. asm
  380. move.b (a0)+,d0 { Get length of first string }
  381. move.b (a1)+,d6 { Get length of 2nd string }
  382. move.b d6,d1 { Save length of string for final compare }
  383. cmp.b d0,d6 { Get shortest string length }
  384. ble @LSTRCONCAT1
  385. move.b d0,d6 { Set length to shortest string }
  386. @LSTRCONCAT1:
  387. tst.b d6 { Both strings have a length of zero, exit }
  388. beq @LSTRCONCAT2
  389. andi.l #$ff,d6
  390. subq.l #1,d6 { subtract first attempt }
  391. { if value is -1 then don't loop and just compare lengths of }
  392. { both strings before exiting. }
  393. bmi @LSTRCONCAT2
  394. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  395. @LSTRCONCAT5:
  396. { Workaroung for GAS v.134 bug }
  397. { old: cmp.b (a1)+,(a0)+ }
  398. cmpm.b (a1)+,(a0)+
  399. @LSTRCONCAT4:
  400. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  401. bne @LSTRCONCAT3
  402. @LSTRCONCAT2:
  403. { If length of both string are equal }
  404. { Then set zero flag }
  405. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  406. @LSTRCONCAT3:
  407. end;
  408. Function strpas(p: pchar): string;
  409. { only 255 first characters are actually copied. }
  410. var
  411. counter : byte;
  412. str: string;
  413. Begin
  414. counter := 0;
  415. str := '';
  416. while (ord(p[counter]) <> 0) and (counter < 255) do
  417. begin
  418. counter:=counter+1;
  419. str[counter] := char(p[counter-1]);
  420. end;
  421. str[0] := char(counter);
  422. strpas := str;
  423. end;
  424. function strlen(p : pchar) : longint;
  425. var
  426. counter : longint;
  427. Begin
  428. counter := 0;
  429. repeat
  430. counter:=counter+1;
  431. until ord(p[counter]) = 0;
  432. strlen := counter;
  433. end;
  434. procedure move(var source;var dest;count : longint);
  435. { base pointer+8 = source }
  436. { base pointer+12 = destination }
  437. { base pointer+16 = number of bytes to move}
  438. begin
  439. asm
  440. clr.l d0
  441. move.l 16(a6),d0 { number of bytes }
  442. @LMOVE0:
  443. move.l 12(a6),a1 { destination }
  444. move.l 8(a6),a0 { source }
  445. cmpi.l #65535, d0 { check, if this is a word move }
  446. ble @LMEMSET00 { use fast dbra mode 68010+ }
  447. cmp.l a0,a1 { check copy direction }
  448. bls @LMOVE4
  449. add.l d0,a0 { move pointers to end }
  450. add.l d0,a1
  451. bra @LMOVE2
  452. @LMOVE1:
  453. move.b -(a0),-(a1) { (s < d) copy loop }
  454. @LMOVE2:
  455. subq.l #1,d0
  456. cmpi.l #-1,d0
  457. bne @LMOVE1
  458. bra @LMOVE5
  459. @LMOVE3:
  460. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  461. @LMOVE4:
  462. subq.l #1,d0
  463. cmpi.l #-1,d0
  464. bne @LMOVE3
  465. bra @LMOVE5
  466. @LMEMSET00: { use fast loop mode 68010+ }
  467. cmp.l a0,a1 { check copy direction }
  468. bls @LMOVE04
  469. add.l d0,a0 { move pointers to end }
  470. add.l d0,a1
  471. bra @LMOVE02
  472. @LMOVE01:
  473. move.b -(a0),-(a1) { (s < d) copy loop }
  474. @LMOVE02:
  475. dbra d0,@LMOVE01
  476. bra @LMOVE5
  477. @LMOVE03:
  478. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  479. @LMOVE04:
  480. dbra d0,@LMOVE03
  481. { end fast loop mode }
  482. @LMOVE5:
  483. end ['d0','a0','a1'];
  484. end;
  485. procedure fillword(var x;count : longint;value : word);
  486. begin
  487. asm
  488. move.l 8(a6), a0 { destination }
  489. move.l 12(a6), d1 { number of bytes to fill }
  490. move.w 16(a6),d0 { fill data }
  491. bra @LMEMSET21
  492. @LMEMSET11:
  493. move.w d0,(a0)+
  494. @LMEMSET21:
  495. subq.l #1,d1
  496. cmp.b #-1,d1
  497. bne @LMEMSET11
  498. end ['d0','d1','a0'];
  499. end;
  500. function abs(l : longint) : longint;
  501. begin
  502. asm
  503. move.l 8(a6),d0
  504. tst.l d0
  505. bpl @LMABS1
  506. neg.l d0
  507. @LMABS1:
  508. move.l d0,@RESULT
  509. end ['d0'];
  510. end;
  511. function odd(l : longint) : boolean;
  512. begin
  513. if (l and $01) = $01 then
  514. odd := TRUE
  515. else
  516. odd := FALSE;
  517. end;
  518. function sqr(l : longint) : longint;
  519. begin
  520. sqr := l*l;
  521. end;
  522. procedure int_str(l : longint;var s : string);
  523. var
  524. value: longint;
  525. negative: boolean;
  526. begin
  527. negative := false;
  528. s:='';
  529. { Workaround: }
  530. if l=$80000000 then
  531. begin
  532. s:='-2147483648';
  533. exit;
  534. end;
  535. { handle case where l = 0 }
  536. if l = 0 then
  537. begin
  538. s:='0';
  539. exit;
  540. end;
  541. If l < 0 then
  542. begin
  543. negative := true;
  544. value:=abs(l);
  545. end
  546. else
  547. value:=l;
  548. { handle non-zero case }
  549. while value>0 do
  550. begin
  551. s:=char((value mod 10)+ord('0'))+s;
  552. value := value div 10;
  553. end;
  554. if negative then
  555. s := '-' + s;
  556. end;
  557. Function Sptr : Longint;
  558. begin
  559. asm
  560. move.l sp,d0
  561. add.l #8,d0
  562. move.l d0,@RESULT
  563. end ['d0'];
  564. end;
  565. Procedure BoundsCheck;assembler;[public,alias:'FPC_RE_BOUNDS_CHECK'];
  566. { called by code generator with R+ state to }
  567. { determine if a range check occured. }
  568. { Only in 68000 mode, in 68020 mode this is }
  569. { inline. }
  570. { On Entry: }
  571. { A1 = address contaning min and max indexes }
  572. { D0 = value of current index to check. }
  573. asm
  574. cmp.l (A1),D0 { lower bound ... }
  575. bmi @rebounderr { is index lower ... }
  576. add.l #4,A1
  577. cmp.l (A1),D0
  578. bmi @reboundend
  579. beq @reboundend
  580. @rebounderr:
  581. move.l #201,d0
  582. jsr FPC_HALT_ERROR
  583. @reboundend:
  584. end;
  585. {****************************************************************************
  586. IoCheck
  587. ****************************************************************************}
  588. procedure int_iocheck(addr : longint);[public,alias: {$ifdef FPCNAMES}'FPC_'+{$endif}'IOCHECK'];
  589. var
  590. l : longint;
  591. begin
  592. asm
  593. movem.l d0-a7,-(sp)
  594. end;
  595. if InOutRes<>0 then
  596. begin
  597. l:=InOutRes;
  598. InOutRes:=0;
  599. If ErrorProc<>Nil then
  600. TErrorProc(Errorproc)(l,pointer(addr));
  601. {$ifndef RTLLITE}
  602. writeln('IO-Error ',l,' at 0x',HexStr(addr,8));
  603. {$endif}
  604. Halt(byte(l));
  605. end;
  606. asm
  607. movem.l (sp)+,d0-a7
  608. end;
  609. end;
  610. {
  611. $Log$
  612. Revision 1.4 2004-01-02 17:22:14 jonas
  613. + fpc_cpuinit procedure to allow cpu/fpu initialisation before any unit
  614. initialises
  615. + fpu exceptions for invalid operations and division by zero enabled for
  616. ppc
  617. Revision 1.3 2002/09/07 16:01:20 peter
  618. * old logs removed and tabs fixed
  619. }