z80.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2017 by the Free Pascal development team.
  4. Processor dependent implementation for the system unit for
  5. Z80
  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. var
  13. z80_save_hl: Word; public name 'FPC_Z80_SAVE_HL';
  14. {$define FPC_SYSTEM_HAS_FPC_CPUINIT}
  15. procedure fpc_cpuinit;{$ifdef SYSTEMINLINE}inline;{$endif}
  16. begin
  17. end;
  18. {$define FPC_SYSTEM_HAS_MOVE}
  19. procedure Move(const source;var dest;count:SizeInt);assembler;[public, alias: 'FPC_MOVE'];
  20. label
  21. skip, forward_move;
  22. asm
  23. ld c, (count)
  24. ld b, (count+1)
  25. bit 7, b
  26. jp NZ, skip
  27. ld a, b
  28. or a, c
  29. jp Z, skip
  30. ld l, (source)
  31. ld h, (source+1)
  32. ld e, (dest)
  33. ld d, (dest+1)
  34. ld a, d
  35. cp a, h
  36. jp C, forward_move
  37. ld a, e
  38. cp a, l
  39. jp C, forward_move
  40. { backward move }
  41. add hl, bc
  42. dec hl
  43. ex de, hl
  44. add hl, bc
  45. dec hl
  46. ex de, hl
  47. lddr
  48. jp skip
  49. forward_move:
  50. ldir
  51. skip:
  52. end;
  53. {$define FPC_SYSTEM_HAS_FILLCHAR}
  54. Procedure FillChar(var x;count:SizeInt;value:byte);assembler;
  55. label
  56. skip, loop;
  57. asm
  58. ld c, (count)
  59. ld b, (count+1)
  60. bit 7, b
  61. jp NZ, skip
  62. ld a, b
  63. or a, c
  64. jp Z, skip
  65. ld e, (value)
  66. ld a, 0
  67. ld l, (x)
  68. ld h, (x+1)
  69. loop:
  70. ld (hl), e
  71. inc hl
  72. dec bc
  73. cp a, c
  74. jp NZ, loop
  75. cp a, b
  76. jp NZ, loop
  77. skip:
  78. end;
  79. {$IFNDEF INTERNAL_BACKTRACE}
  80. {$define FPC_SYSTEM_HAS_GET_FRAME}
  81. function get_frame:pointer;assembler;nostackframe;
  82. asm
  83. push ix
  84. pop hl
  85. end;
  86. {$ENDIF not INTERNAL_BACKTRACE}
  87. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  88. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler;
  89. label
  90. framebp_null;
  91. asm
  92. ld l, (framebp)
  93. ld h, (framebp+1)
  94. ld a, l
  95. or a, h
  96. jp Z, framebp_null
  97. inc hl
  98. inc hl
  99. ld e, (hl)
  100. inc hl
  101. ld d, (hl)
  102. ex de, hl
  103. framebp_null:
  104. end;
  105. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  106. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler;
  107. label
  108. framebp_null;
  109. asm
  110. ld l, (framebp)
  111. ld h, (framebp+1)
  112. ld a, l
  113. or a, h
  114. jp Z, framebp_null
  115. ld e, (hl)
  116. inc hl
  117. ld d, (hl)
  118. ex de, hl
  119. framebp_null:
  120. end;
  121. {$define FPC_SYSTEM_HAS_SPTR}
  122. Function Sptr : pointer;assembler;nostackframe;
  123. asm
  124. ld hl, 0
  125. add hl, sp
  126. end;
  127. {$ifdef VER3_2}
  128. function InterLockedDecrement (var Target: longint) : longint;
  129. {$else VER3_2}
  130. {$define FPC_SYSTEM_HAS_ATOMIC_DEC_32}
  131. function fpc_atomic_dec_32 (var Target: longint): longint;
  132. {$endif VER3_2}
  133. begin
  134. { block interrupts }
  135. asm
  136. di
  137. end;
  138. dec(Target);
  139. Result:=Target;
  140. { release interrupts }
  141. asm
  142. ei
  143. end;
  144. end;
  145. {$ifdef VER3_2}
  146. function InterLockedIncrement (var Target: longint) : longint;
  147. {$else VER3_2}
  148. {$define FPC_SYSTEM_HAS_ATOMIC_INC_32}
  149. function fpc_atomic_inc_32 (var Target: longint): longint;
  150. {$endif VER3_2}
  151. begin
  152. { block interrupts }
  153. asm
  154. di
  155. end;
  156. inc(Target);
  157. Result:=Target;
  158. { release interrupts }
  159. asm
  160. ei
  161. end;
  162. end;
  163. {$ifdef VER3_2}
  164. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  165. {$else VER3_2}
  166. {$define FPC_SYSTEM_HAS_ATOMIC_XCHG_32}
  167. function fpc_atomic_xchg_32 (var Target: longint;Source : longint): longint;
  168. {$endif VER3_2}
  169. begin
  170. { block interrupts }
  171. asm
  172. di
  173. end;
  174. Result:=Target;
  175. Target:=Source;
  176. { release interrupts }
  177. asm
  178. ei
  179. end;
  180. end;
  181. {$ifdef VER3_2}
  182. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  183. {$else VER3_2}
  184. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_32}
  185. function fpc_atomic_cmp_xchg_32 (var Target: longint; NewValue: longint; Comparand: longint): longint;
  186. {$endif VER3_2}
  187. begin
  188. { block interrupts }
  189. asm
  190. di
  191. end;
  192. Result:=Target;
  193. if Target={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then
  194. Target:=NewValue;
  195. { release interrupts }
  196. asm
  197. ei
  198. end;
  199. end;
  200. {$ifdef VER3_2}
  201. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  202. {$else VER3_2}
  203. {$define FPC_SYSTEM_HAS_ATOMIC_ADD_32}
  204. function fpc_atomic_add_32 (var Target: longint;Value : longint): longint;
  205. {$endif VER3_2}
  206. begin
  207. { block interrupts }
  208. asm
  209. di
  210. end;
  211. Result:=Target;
  212. inc(Target,{$ifdef VER3_2}Source{$else}Value{$endif});
  213. { release interrupts }
  214. asm
  215. ei
  216. end;
  217. end;
  218. {$ifdef VER3_2}
  219. function InterLockedDecrement (var Target: smallint) : smallint;
  220. {$else VER3_2}
  221. {$define FPC_SYSTEM_HAS_ATOMIC_DEC_16}
  222. function fpc_atomic_dec_16 (var Target: smallint): smallint;
  223. {$endif VER3_2}
  224. begin
  225. { block interrupts }
  226. asm
  227. di
  228. end;
  229. dec(Target);
  230. Result:=Target;
  231. { release interrupts }
  232. asm
  233. ei
  234. end;
  235. end;
  236. {$ifdef VER3_2}
  237. function InterLockedIncrement (var Target: smallint) : smallint;
  238. {$else VER3_2}
  239. {$define FPC_SYSTEM_HAS_ATOMIC_INC_16}
  240. function fpc_atomic_inc_16 (var Target: smallint): smallint;
  241. {$endif VER3_2}
  242. begin
  243. { block interrupts }
  244. asm
  245. di
  246. end;
  247. inc(Target);
  248. Result:=Target;
  249. { release interrupts }
  250. asm
  251. ei
  252. end;
  253. end;
  254. {$ifdef VER3_2}
  255. function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
  256. {$else VER3_2}
  257. {$define FPC_SYSTEM_HAS_ATOMIC_XCHG_16}
  258. function fpc_atomic_xchg_16 (var Target: smallint;Source : smallint): smallint;
  259. {$endif VER3_2}
  260. begin
  261. { block interrupts }
  262. asm
  263. di
  264. end;
  265. Result:=Target;
  266. Target:=Source;
  267. { release interrupts }
  268. asm
  269. ei
  270. end;
  271. end;
  272. {$ifdef VER3_2}
  273. function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
  274. {$else VER3_2}
  275. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_16}
  276. function fpc_atomic_cmp_xchg_16 (var Target: smallint; NewValue: smallint; Comparand: smallint): smallint; [public,alias:'FPC_ATOMIC_CMP_XCHG_16'];
  277. {$endif VER3_2}
  278. begin
  279. { block interrupts }
  280. asm
  281. di
  282. end;
  283. Result:=Target;
  284. if Target={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then
  285. Target:=NewValue;
  286. { release interrupts }
  287. asm
  288. ei
  289. end;
  290. end;
  291. {$ifdef VER3_2}
  292. function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
  293. {$else VER3_2}
  294. {$define FPC_SYSTEM_HAS_ATOMIC_ADD_16}
  295. function fpc_atomic_add_16 (var Target: smallint;Value : smallint): smallint;
  296. {$endif VER3_2}
  297. begin
  298. { block interrupts }
  299. asm
  300. di
  301. end;
  302. Result:=Target;
  303. inc(Target,{$ifdef VER3_2}Source{$else}Value{$endif});
  304. { release interrupts }
  305. asm
  306. ei
  307. end;
  308. end;