avr.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2008 by the Free Pascal development team.
  4. Processor dependent implementation for the system unit for
  5. AVR
  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. {$asmmode gas}
  13. const
  14. {$i cpuinnr.inc}
  15. { Reads SREG and then disables interrupts, returns contents of SREG }
  16. function avr_save: byte;[INTERNPROC: in_avr_save];
  17. { Restores SREG }
  18. procedure avr_restore(old_sreg: byte); [INTERNPROC: in_avr_restore];
  19. {$define FPC_SYSTEM_HAS_FPC_CPUINIT}
  20. procedure fpc_cpuinit;{$ifdef SYSTEMINLINE}inline;{$endif}
  21. begin
  22. end;
  23. {$if not(defined(CPUAVR_16_REGS)) and defined(CPUAVR_HAS_MOVW)}
  24. {$define FPC_SYSTEM_HAS_MOVE}
  25. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE']; assembler; nostackframe;
  26. asm
  27. push r28
  28. push r29
  29. movw r26, r24 // Src=X
  30. movw r28, r22 // Dest=Y
  31. movw r30, r20 // Count=Z
  32. cp r1, r30
  33. cpc r1, r31
  34. brge .Lexit // if 0 >= Count
  35. cp r28, r26
  36. cpc r29, r27
  37. breq .Lexit // if dest = source
  38. brlo .LForwardMove // if dest < source
  39. // Add count to both pointers
  40. add r26, r30
  41. adc r27, r31
  42. add r28, r30
  43. adc r29, r31
  44. .LBackwardMove:
  45. ld r18, -X
  46. st -Y, r18
  47. sbiw r30, 1
  48. brne .LBackwardMove
  49. rjmp .Lexit
  50. .LForwardMove:
  51. ld r18, X+
  52. st Y+, r18
  53. sbiw r30, 1
  54. brne .LForwardMove
  55. .Lexit:
  56. pop r29
  57. pop r28
  58. end;
  59. {$endif not(defined(CPUAVR_16_REGS)) and defined(CPUAVR_HAS_MOVW)}
  60. {$define FPC_SYSTEM_HAS_FILLCHAR}
  61. Procedure FillChar(var x;count:SizeInt;value:byte);
  62. var
  63. pdest,pend : pbyte;
  64. v : ptruint;
  65. begin
  66. if count <= 0 then
  67. exit;
  68. pdest:=@x;
  69. pend:=pdest+count;
  70. while pdest<pend do
  71. begin
  72. pdest^:=value;
  73. inc(pdest);
  74. end;
  75. end;
  76. {$IFNDEF INTERNAL_BACKTRACE}
  77. {$define FPC_SYSTEM_HAS_GET_FRAME}
  78. { this is never going to work on avr properly this way, so inline and return nil so the compiler
  79. can optimize it }
  80. function get_frame:pointer;inline;
  81. begin
  82. result:=nil;
  83. end;
  84. {$ENDIF not INTERNAL_BACKTRACE}
  85. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  86. { this is never going to work on avr properly this way, so inline and return nil so the compiler
  87. can optimize it }
  88. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;inline;
  89. begin
  90. result:=nil;
  91. end;
  92. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  93. { this is never going to work on avr properly this way, so inline and return nil so the compiler
  94. can optimize it }
  95. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;inline;
  96. begin
  97. result:=nil;
  98. end;
  99. {$define FPC_SYSTEM_HAS_SPTR}
  100. Function Sptr : pointer;assembler;nostackframe;
  101. asm
  102. in r24, 0x3d
  103. in r25, 0x3e
  104. end;
  105. {$ifdef VER3_2}
  106. function InterLockedDecrement (var Target: longint) : longint;
  107. {$else VER3_2}
  108. {$define FPC_SYSTEM_HAS_ATOMIC_DEC_32}
  109. function fpc_atomic_dec_32 (var Target: longint) : longint;
  110. {$endif VER3_2}
  111. var
  112. temp_sreg : byte;
  113. begin
  114. { block interrupts }
  115. temp_sreg:=avr_save();
  116. Result:=Target-1;
  117. Target:=Result;
  118. { release interrupts }
  119. avr_restore(temp_sreg);
  120. end;
  121. {$ifdef VER3_2}
  122. function InterLockedIncrement (var Target: longint) : longint;
  123. {$else VER3_2}
  124. {$define FPC_SYSTEM_HAS_ATOMIC_INC_32}
  125. function fpc_atomic_inc_32 (var Target: longint) : longint;
  126. {$endif VER3_2}
  127. var
  128. temp_sreg : byte;
  129. begin
  130. { block interrupts }
  131. temp_sreg:=avr_save();
  132. Result:=Target+1;
  133. Target:=Result;
  134. { release interrupts }
  135. avr_restore(temp_sreg);
  136. end;
  137. {$ifdef VER3_2}
  138. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  139. {$else VER3_2}
  140. {$define FPC_SYSTEM_HAS_ATOMIC_XCHG_32}
  141. function fpc_atomic_xchg_32 (var Target: longint;Source : longint) : longint;
  142. {$endif VER3_2}
  143. var
  144. temp_sreg : byte;
  145. begin
  146. { block interrupts }
  147. temp_sreg:=avr_save();
  148. Result:=Target;
  149. Target:=Source;
  150. { release interrupts }
  151. avr_restore(temp_sreg);
  152. end;
  153. {$ifdef VER3_2}
  154. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  155. {$else VER3_2}
  156. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_32}
  157. function fpc_atomic_cmp_xchg_32 (var Target: longint; NewValue: longint; Comparand: longint) : longint;
  158. {$endif VER3_2}
  159. var
  160. temp_sreg : byte;
  161. begin
  162. { block interrupts }
  163. temp_sreg:=avr_save();
  164. Result:=Target;
  165. if Result={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then
  166. Target:=NewValue;
  167. { release interrupts }
  168. avr_restore(temp_sreg);
  169. end;
  170. {$ifdef VER3_2}
  171. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  172. {$else VER3_2}
  173. {$define FPC_SYSTEM_HAS_ATOMIC_ADD_32}
  174. function fpc_atomic_add_32 (var Target: longint;Value : longint) : longint;
  175. {$endif VER3_2}
  176. var
  177. temp_sreg : byte;
  178. begin
  179. { block interrupts }
  180. temp_sreg:=avr_save();
  181. Result:=Target;
  182. Target:=Result+{$ifdef VER3_2}Source{$else}Value{$endif};
  183. { release interrupts }
  184. avr_restore(temp_sreg);
  185. end;
  186. {$ifdef VER3_2}
  187. function InterLockedDecrement (var Target: smallint) : smallint;
  188. {$else VER3_2}
  189. {$define FPC_SYSTEM_HAS_ATOMIC_DEC_16}
  190. function fpc_atomic_dec_16 (var Target: smallint) : smallint;
  191. {$endif VER3_2}
  192. var
  193. temp_sreg : byte;
  194. begin
  195. { block interrupts }
  196. temp_sreg:=avr_save();
  197. Result:=Target-1;
  198. Target:=Result;
  199. { release interrupts }
  200. avr_restore(temp_sreg);
  201. end;
  202. {$ifdef VER3_2}
  203. function InterLockedIncrement (var Target: smallint) : smallint;
  204. {$else VER3_2}
  205. {$define FPC_SYSTEM_HAS_ATOMIC_INC_16}
  206. function fpc_atomic_inc_16 (var Target: smallint) : smallint;
  207. {$endif VER3_2}
  208. var
  209. temp_sreg : byte;
  210. begin
  211. { block interrupts }
  212. temp_sreg:=avr_save();
  213. Result:=Target+1;
  214. Target:=Result;
  215. { release interrupts }
  216. avr_restore(temp_sreg);
  217. end;
  218. {$ifdef VER3_2}
  219. function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
  220. {$else VER3_2}
  221. {$define FPC_SYSTEM_HAS_ATOMIC_XCHG_16}
  222. function fpc_atomic_xchg_16 (var Target: smallint;Source : smallint) : smallint;
  223. {$endif VER3_2}
  224. var
  225. temp_sreg : byte;
  226. begin
  227. { block interrupts }
  228. temp_sreg:=avr_save();
  229. Result:=Target;
  230. Target:=Source;
  231. { release interrupts }
  232. avr_restore(temp_sreg);
  233. end;
  234. {$ifdef VER3_2}
  235. function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
  236. {$else VER3_2}
  237. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_16}
  238. function fpc_atomic_cmp_xchg_16 (var Target: smallint; NewValue: smallint; Comparand: smallint) : smallint; [public,alias:'FPC_ATOMIC_CMP_XCHG_16'];
  239. {$endif VER3_2}
  240. var
  241. temp_sreg : byte;
  242. begin
  243. { block interrupts }
  244. temp_sreg:=avr_save();
  245. Result:=Target;
  246. if Result={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then
  247. Target:=NewValue;
  248. { release interrupts }
  249. avr_restore(temp_sreg);
  250. end;
  251. {$ifdef VER3_2}
  252. function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
  253. {$else VER3_2}
  254. {$define FPC_SYSTEM_HAS_ATOMIC_ADD_16}
  255. function fpc_atomic_add_16 (var Target: smallint;Value : smallint) : smallint;
  256. {$endif VER3_2}
  257. var
  258. temp_sreg : byte;
  259. begin
  260. { block interrupts }
  261. temp_sreg:=avr_save();
  262. Result:=Target;
  263. Target:=Result+{$ifdef VER3_2}Source{$else}Value{$endif};
  264. { release interrupts }
  265. avr_restore(temp_sreg);
  266. end;
  267. {$define FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  268. procedure fpc_shortstr_shortstr_intern_charmove(const src: shortstring; const srcindex: byte; var dst: shortstring; const dstindex, len: byte); {$ifdef SYSTEMINLINE}inline;{$endif}
  269. begin
  270. move(src[srcindex],dst[dstindex],len);
  271. end;
  272. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT_MULTI}
  273. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  274. var
  275. s2l, tmpindex : byte;
  276. i,
  277. Len : ObjpasInt;
  278. relocatedstringindex: byte;
  279. p : pshortstring;
  280. begin
  281. if high(sarr)=0 then
  282. begin
  283. DestS:='';
  284. exit;
  285. end;
  286. { for s := s1 + s + ..., relocate s to the correct first position in dests
  287. and remember this position for possible subsequent occurences of s }
  288. Len:=1;
  289. i:=low(sarr);
  290. while (i<=high(sarr)) and (@dests<>sarr[i]) and (Len<=high(dests)) do
  291. begin
  292. Len:=Len+length(sarr[i]^);
  293. inc(i);
  294. end;
  295. if Len<=high(dests) then
  296. begin
  297. relocatedstringindex:=Len;
  298. s2l:=length(dests);
  299. if uint16(s2l)+uint16(relocatedstringindex) > high(dests) then
  300. s2l:=high(dests)-relocatedstringindex+1;
  301. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,relocatedstringindex,s2l);
  302. end;
  303. Len:=0;
  304. for i:=low(sarr) to high(sarr) do
  305. begin
  306. p:=sarr[i];
  307. if assigned(p) then
  308. begin
  309. s2l:=length(p^);
  310. if Len+s2l>high(dests) then
  311. s2l:=high(dests)-Len;
  312. { Use relocated string position if src = dests }
  313. if (p=@dests) then
  314. tmpindex:=relocatedstringindex
  315. else
  316. tmpindex:=1;
  317. fpc_shortstr_shortstr_intern_charmove(p^,tmpindex,dests,Len+1,s2l);
  318. inc(Len,s2l);
  319. end;
  320. end;
  321. dests[0]:=Chr(Len);
  322. end;
  323. {include hand-optimized assembler code}
  324. {$i math.inc}