m68k.inc 9.5 KB

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