m68k.inc 20 KB

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