m68k.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. procedure fpc_cpuinit;
  25. begin
  26. end;
  27. {$define FPC_SYSTEM_HAS_GET_FRAME}
  28. function get_frame : pointer; assembler;
  29. asm
  30. move.l a6,d0
  31. end;
  32. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  33. function get_caller_addr(framebp : pointer) : pointer;
  34. begin
  35. asm
  36. move.l framebp,a0
  37. cmp.l #0,a0
  38. beq @Lnul_address
  39. move.l 4(a0),a0
  40. @Lnul_address:
  41. move.l a0,@RESULT
  42. end ['a0'];
  43. end;
  44. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  45. function get_caller_frame(framebp : pointer) : pointer;
  46. begin
  47. asm
  48. move.l FRAMEBP,a0
  49. cmp.l #0,a0
  50. beq @Lnul_frame
  51. move.l (a0),a0
  52. @Lnul_frame:
  53. move.l a0,@RESULT
  54. end ['a0'];
  55. end;
  56. {$define FPC_SYSTEM_HAS_SPTR}
  57. function Sptr : pointer; assembler;
  58. asm
  59. move.l sp,d0
  60. end ['d0'];
  61. {$define FPC_SYSTEM_HAS_FILLCHAR}
  62. procedure FillChar(var x;count:longint;value:byte); assembler;
  63. asm
  64. move.l 8(a6), a0 { destination }
  65. move.l 12(a6), d1 { number of bytes to fill }
  66. move.b 16(a6),d0 { fill data }
  67. cmpi.l #65535, d1 { check, if this is a word move }
  68. ble @LMEMSET3 { use fast dbra mode }
  69. bra @LMEMSET2
  70. @LMEMSET1:
  71. move.b d0,(a0)+
  72. @LMEMSET2:
  73. subq.l #1,d1
  74. cmp.l #-1,d1
  75. bne @LMEMSET1
  76. bra @LMEMSET5 { finished slow mode , exit }
  77. @LMEMSET4: { fast loop mode section 68010+ }
  78. move.b d0,(a0)+
  79. @LMEMSET3:
  80. dbra d1,@LMEMSET4
  81. @LMEMSET5:
  82. end ['d0','d1','a0'];
  83. {$ifdef dummy}
  84. { procedure strcopy(dstr,sstr : pointer;len : longint);[public,alias: 'STRCOPY'];}
  85. procedure strcopy; assembler;[public,alias: 'FPC_STRCOPY'];
  86. {---------------------------------------------------}
  87. { Low-level routine to copy a string to another }
  88. { string with maximum length. Never call directly! }
  89. { On Entry: }
  90. { a1.l = string to copy to }
  91. { a0.l = source string }
  92. { d0.l = maximum length of copy }
  93. { registers destroyed: a0,a1,d0,d1 }
  94. {---------------------------------------------------}
  95. asm
  96. { move.l 12(a6),a0
  97. move.l 16(a6),a1
  98. move.l 8(a6),d1 }
  99. move.l d0,d1
  100. move.b (a0)+,d0 { Get source length }
  101. and.w #$ff,d0
  102. cmp.w d1,d0 { This is a signed comparison! }
  103. ble @LM4
  104. move.b d1,d0 { If longer than maximum size of target, cut
  105. source length }
  106. @LM4:
  107. andi.l #$ff,d0 { zero extend d0-byte }
  108. move.l d0,d1 { save length to copy }
  109. move.b d0,(a1)+ { save new length }
  110. { Check if copying length is zero - if so then }
  111. { exit without copying anything. }
  112. tst.b d1
  113. beq @Lend
  114. bra @LMSTRCOPY55
  115. @LMSTRCOPY56: { 68010 Fast loop mode }
  116. move.b (a0)+,(a1)+
  117. @LMSTRCOPY55:
  118. dbra d1,@LMSTRCOPY56
  119. @Lend:
  120. end;
  121. { Concatenate Strings }
  122. { PARAMETERS ARE REVERSED COMPARED TO NORMAL! }
  123. { therefore online assembler may not parse the params as normal }
  124. procedure strconcat(s1,s2 : pointer);[public,alias: 'STRCONCAT'];
  125. begin
  126. asm
  127. move.b #255,d0
  128. move.l s1,a0 { a0 = destination }
  129. move.l s2,a1 { a1 = source }
  130. sub.b (a0),d0 { copyl:= 255 -length(s1) }
  131. move.b (a1),d6
  132. and.w #$ff,d0 { Sign flags are checked! }
  133. and.w #$ff,d6
  134. cmp.w d6,d0 { if copyl > length(s2) then }
  135. ble @Lcontinue
  136. move.b (a1),d0 { copyl:=length(s2) }
  137. @Lcontinue:
  138. move.b (a0),d6
  139. and.l #$ff,d6
  140. lea 1(a0,d6),a0 { s1[length(s1)+1] }
  141. add.l #1,a1 { s2[1] }
  142. move.b d0,d6
  143. { Check if copying length is zero - if so then }
  144. { exit without copying anything. }
  145. tst.b d6
  146. beq @Lend
  147. bra @ALoop
  148. @Loop:
  149. move.b (a1)+,(a0)+ { s1[i] := s2[i]; }
  150. @ALoop:
  151. dbra d6,@Loop
  152. move.l s1,a0
  153. add.b d0,(a0) { change to new string length }
  154. @Lend:
  155. end ['d0','d1','a0','a1','d6'];
  156. end;
  157. { Compares strings }
  158. { DO NOT CALL directly. }
  159. { a0 = pointer to first string to compare }
  160. { a1 = pointer to second string to compare }
  161. { ALL FLAGS are set appropriately. }
  162. { ZF = strings are equal }
  163. { REGISTERS DESTROYED: a0, a1, d0, d1, d6 }
  164. procedure strcmp; assembler;[public,alias:'FPC_STRCMP'];
  165. asm
  166. move.b (a0)+,d0 { Get length of first string }
  167. move.b (a1)+,d6 { Get length of 2nd string }
  168. move.b d6,d1 { Save length of string for final compare }
  169. cmp.b d0,d6 { Get shortest string length }
  170. ble @LSTRCONCAT1
  171. move.b d0,d6 { Set length to shortest string }
  172. @LSTRCONCAT1:
  173. tst.b d6 { Both strings have a length of zero, exit }
  174. beq @LSTRCONCAT2
  175. andi.l #$ff,d6
  176. subq.l #1,d6 { subtract first attempt }
  177. { if value is -1 then don't loop and just compare lengths of }
  178. { both strings before exiting. }
  179. bmi @LSTRCONCAT2
  180. or.l d0,d0 { Make sure to set Zerfo flag to 0 }
  181. @LSTRCONCAT5:
  182. { Workaroung for GAS v.134 bug }
  183. { old: cmp.b (a1)+,(a0)+ }
  184. cmpm.b (a1)+,(a0)+
  185. @LSTRCONCAT4:
  186. dbne d6,@LSTRCONCAT5 { Repeat until not equal }
  187. bne @LSTRCONCAT3
  188. @LSTRCONCAT2:
  189. { If length of both string are equal }
  190. { Then set zero flag }
  191. cmp.b d1,d0 { Compare length - set flag if equal length strings }
  192. @LSTRCONCAT3:
  193. end;
  194. {$endif dummy}
  195. {$define FPC_SYSTEM_HAS_MOVE}
  196. procedure move(const source;var dest;count : longint);
  197. { base pointer+8 = source }
  198. { base pointer+12 = destination }
  199. { base pointer+16 = number of bytes to move}
  200. begin
  201. asm
  202. clr.l d0
  203. move.l 16(a6),d0 { number of bytes }
  204. @LMOVE0:
  205. move.l 12(a6),a1 { destination }
  206. move.l 8(a6),a0 { source }
  207. cmpi.l #65535, d0 { check, if this is a word move }
  208. ble @LMEMSET00 { use fast dbra mode 68010+ }
  209. cmp.l a0,a1 { check copy direction }
  210. bls @LMOVE4
  211. add.l d0,a0 { move pointers to end }
  212. add.l d0,a1
  213. bra @LMOVE2
  214. @LMOVE1:
  215. move.b -(a0),-(a1) { (s < d) copy loop }
  216. @LMOVE2:
  217. subq.l #1,d0
  218. cmpi.l #-1,d0
  219. bne @LMOVE1
  220. bra @LMOVE5
  221. @LMOVE3:
  222. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  223. @LMOVE4:
  224. subq.l #1,d0
  225. cmpi.l #-1,d0
  226. bne @LMOVE3
  227. bra @LMOVE5
  228. @LMEMSET00: { use fast loop mode 68010+ }
  229. cmp.l a0,a1 { check copy direction }
  230. bls @LMOVE04
  231. add.l d0,a0 { move pointers to end }
  232. add.l d0,a1
  233. bra @LMOVE02
  234. @LMOVE01:
  235. move.b -(a0),-(a1) { (s < d) copy loop }
  236. @LMOVE02:
  237. dbra d0,@LMOVE01
  238. bra @LMOVE5
  239. @LMOVE03:
  240. move.b (a0)+,(a1)+ { (s >= d) copy loop }
  241. @LMOVE04:
  242. dbra d0,@LMOVE03
  243. { end fast loop mode }
  244. @LMOVE5:
  245. end ['d0','a0','a1'];
  246. end;
  247. {$define FPC_SYSTEM_HAS_FILLWORD}
  248. procedure fillword(var x;count : longint;value : word);
  249. begin
  250. asm
  251. move.l 8(a6), a0 { destination }
  252. move.l 12(a6), d1 { number of bytes to fill }
  253. move.w 16(a6),d0 { fill data }
  254. bra @LMEMSET21
  255. @LMEMSET11:
  256. move.w d0,(a0)+
  257. @LMEMSET21:
  258. subq.l #1,d1
  259. cmp.b #-1,d1
  260. bne @LMEMSET11
  261. end ['d0','d1','a0'];
  262. end;
  263. {$define FPC_SYSTEM_HAS_ABS_LONGINT}
  264. function abs(l : longint) : longint;
  265. begin
  266. asm
  267. move.l 8(a6),d0
  268. tst.l d0
  269. bpl @LMABS1
  270. neg.l d0
  271. @LMABS1:
  272. move.l d0,@RESULT
  273. end ['d0'];
  274. end;