avr.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. function InterLockedDecrement (var Target: longint) : longint;
  106. var
  107. temp_sreg : byte;
  108. begin
  109. { block interrupts }
  110. temp_sreg:=avr_save();
  111. Result:=Target-1;
  112. Target:=Result;
  113. { release interrupts }
  114. avr_restore(temp_sreg);
  115. end;
  116. function InterLockedIncrement (var Target: longint) : longint;
  117. var
  118. temp_sreg : byte;
  119. begin
  120. { block interrupts }
  121. temp_sreg:=avr_save();
  122. Result:=Target+1;
  123. Target:=Result;
  124. { release interrupts }
  125. avr_restore(temp_sreg);
  126. end;
  127. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  128. var
  129. temp_sreg : byte;
  130. begin
  131. { block interrupts }
  132. temp_sreg:=avr_save();
  133. Result:=Target;
  134. Target:=Source;
  135. { release interrupts }
  136. avr_restore(temp_sreg);
  137. end;
  138. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  139. var
  140. temp_sreg : byte;
  141. begin
  142. { block interrupts }
  143. temp_sreg:=avr_save();
  144. Result:=Target;
  145. if Result=Comperand then
  146. Target:=NewValue;
  147. { release interrupts }
  148. avr_restore(temp_sreg);
  149. end;
  150. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  151. var
  152. temp_sreg : byte;
  153. begin
  154. { block interrupts }
  155. temp_sreg:=avr_save();
  156. Result:=Target;
  157. Target:=Result+Source;
  158. { release interrupts }
  159. avr_restore(temp_sreg);
  160. end;
  161. function InterLockedDecrement (var Target: smallint) : smallint;
  162. var
  163. temp_sreg : byte;
  164. begin
  165. { block interrupts }
  166. temp_sreg:=avr_save();
  167. Result:=Target-1;
  168. Target:=Result;
  169. { release interrupts }
  170. avr_restore(temp_sreg);
  171. end;
  172. function InterLockedIncrement (var Target: smallint) : smallint;
  173. var
  174. temp_sreg : byte;
  175. begin
  176. { block interrupts }
  177. temp_sreg:=avr_save();
  178. Result:=Target+1;
  179. Target:=Result;
  180. { release interrupts }
  181. avr_restore(temp_sreg);
  182. end;
  183. function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
  184. var
  185. temp_sreg : byte;
  186. begin
  187. { block interrupts }
  188. temp_sreg:=avr_save();
  189. Result:=Target;
  190. Target:=Source;
  191. { release interrupts }
  192. avr_restore(temp_sreg);
  193. end;
  194. function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
  195. var
  196. temp_sreg : byte;
  197. begin
  198. { block interrupts }
  199. temp_sreg:=avr_save();
  200. Result:=Target;
  201. if Result=Comperand then
  202. Target:=NewValue;
  203. { release interrupts }
  204. avr_restore(temp_sreg);
  205. end;
  206. function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
  207. var
  208. temp_sreg : byte;
  209. begin
  210. { block interrupts }
  211. temp_sreg:=avr_save();
  212. Result:=Target;
  213. Target:=Result+Source;
  214. { release interrupts }
  215. avr_restore(temp_sreg);
  216. end;
  217. {$define FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  218. procedure fpc_shortstr_shortstr_intern_charmove(const src: shortstring; const srcindex: byte; var dst: shortstring; const dstindex, len: byte); {$ifdef SYSTEMINLINE}inline;{$endif}
  219. begin
  220. move(src[srcindex],dst[dstindex],len);
  221. end;
  222. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT_MULTI}
  223. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  224. var
  225. s2l, tmpindex : byte;
  226. i,
  227. Len : ObjpasInt;
  228. relocatedstringindex: byte;
  229. p : pshortstring;
  230. begin
  231. if high(sarr)=0 then
  232. begin
  233. DestS:='';
  234. exit;
  235. end;
  236. { for s := s1 + s + ..., relocate s to the correct first position in dests
  237. and remember this position for possible subsequent occurences of s }
  238. Len:=1;
  239. i:=low(sarr);
  240. while (i<=high(sarr)) and (@dests<>sarr[i]) and (Len<=high(dests)) do
  241. begin
  242. Len:=Len+length(sarr[i]^);
  243. inc(i);
  244. end;
  245. if Len<=high(dests) then
  246. begin
  247. relocatedstringindex:=Len;
  248. s2l:=length(dests);
  249. if uint16(s2l)+uint16(relocatedstringindex) > high(dests) then
  250. s2l:=high(dests)-relocatedstringindex+1;
  251. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,relocatedstringindex,s2l);
  252. end;
  253. Len:=0;
  254. for i:=low(sarr) to high(sarr) do
  255. begin
  256. p:=sarr[i];
  257. if assigned(p) then
  258. begin
  259. s2l:=length(p^);
  260. if Len+s2l>high(dests) then
  261. s2l:=high(dests)-Len;
  262. { Use relocated string position if src = dests }
  263. if (p=@dests) then
  264. tmpindex:=relocatedstringindex
  265. else
  266. tmpindex:=1;
  267. fpc_shortstr_shortstr_intern_charmove(p^,tmpindex,dests,Len+1,s2l);
  268. inc(Len,s2l);
  269. end;
  270. end;
  271. dests[0]:=Chr(Len);
  272. end;
  273. {include hand-optimized assembler code}
  274. {$i math.inc}