Add missing files
[bcusdk.git] / bcu / include / bcu_funcs.h
blob5874c8aebb940ad3af6a08b62b897294ecb222e4
1 /*
2 BCU SDK bcu development enviroment
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 In addition to the permissions in the GNU General Public License,
11 you may link the compiled version of this file into combinations
12 with other programs, and distribute those combinations without any
13 restriction coming from the use of this file. (The General Public
14 License restrictions do apply in other respects; for example, they
15 cover modification of the file, and distribution when not linked into
16 a combine executable.)
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #ifndef _BCU_FUNCS_H
29 #define _BCU_FUNCS_H
31 #include <stdbool.h>
33 typedef struct
35 bool newstate;
36 uchar stateok;
37 } FT12_GetStatus_Result;
39 typedef struct
41 unsigned short quotient;
42 unsigned short remainder;
43 bool error;
44 } U_Div_Result;
46 typedef struct
48 unsigned short product;
49 bool overflow;
50 } U_Mul_Result;
52 typedef struct
54 uchar_loptr pointer;
55 bool found;
56 } PopBuf_Result;
58 typedef struct
60 uchar_loptr pointer;
61 bool valid;
62 } AllocBuf_Result;
64 typedef struct
66 bool expired;
67 uchar time;
68 } TM_GetFlg_Result;
70 typedef struct
72 uchar octet;
73 bool error;
74 } U_SerialShift_Result;
76 typedef struct
78 uchar pointer;
79 bool error;
80 } S_xxShift_Result;
82 typedef struct
84 signed short value;
85 bool error;
86 } U_map_Result;
88 #define SETAONCARRY "\n\tclra\n\tbcc _L_%=\n\tinca\n_L_%=:\n\t"
89 #define SETAONZERO "\n\tclra\n\tbne _L_%=\n\tinca\n_L_%=:\n\t"
90 #define SETAONNZERO "\n\tclra\n\tbeq _L_%=\n\tinca\n_L_%=:\n\t"
92 static void inline
93 reset_watchdog ()
95 asm volatile ("bset.0 0x00C");
98 extern const uchar OR_TAB[8];
99 extern const uchar AND_TAB[8];
101 static uchar inline
102 _U_flagsGet (uchar no)
104 uchar ret;
105 if (!__builtin_constant_p (no))
106 asm volatile ("lda %1\n\tjsr U_flagsGet":"=z" (ret):"r" (no):"A", "X",
107 "RegC", "RegJ");
108 else
109 asm volatile ("lda $%1\n\tjsr U_flagsGet":"=z" (ret):"i" (no):"A", "X",
110 "RegC", "RegJ");
111 return ret;
114 static void inline
115 _U_flagsSet (uchar no, uchar flag)
117 if (!__builtin_constant_p (no))
118 asm volatile ("lda %0\n\tjsr U_flagsSet"::"r" (no), "z" (flag):"A", "X",
119 "RegC", "RegJ");
120 else
121 asm volatile ("lda $%0\n\tjsr U_flagsSet"::"i" (no), "z" (flag):"A", "X",
122 "RegC", "RegJ");
125 static void inline
126 _U_transRequest (uchar no)
128 if (!__builtin_constant_p (no))
129 asm volatile ("lda %0\n\tjsr U_transRequest"::"r" (no):"A", "X", "RegB",
130 "RegC", "RegJ");
131 else
132 asm volatile ("lda $%0\n\tjsr U_transRequest"::"i" (no):"A", "X", "RegB",
133 "RegC", "RegJ");
136 static uchar inline
137 _U_testObj (uchar no)
139 uchar ret;
140 if (!__builtin_constant_p (no))
141 asm volatile ("lda %1\n\tjsr U_testObj":"=c" (ret):"r" (no):"A", "X",
142 "RegB", "RegJ");
143 else
144 asm volatile ("lda $%1\n\tjsr U_testObj":"=c" (ret):"i" (no):"A", "X",
145 "RegB", "RegJ");
146 return ret;
149 static void inline
150 _EEwrite (uchar offset, uchar value)
152 if (!__builtin_constant_p (offset))
154 if (!__builtin_constant_p (value))
155 asm volatile ("lda %0\n\tldx %1\n\tjsr EEwrite"::"r" (value),
156 "r" (offset):"A", "X", "RegB", "RegC", "RegH");
157 else
158 asm volatile ("lda %0\n\tldx $%1\n\tjsr EEwrite"::"r" (value),
159 "i" (offset):"A", "X", "RegB", "RegC", "RegH");
161 else
163 if (!__builtin_constant_p (value))
164 asm volatile ("lda $%0\n\tldx %1\n\tjsr EEwrite"::"i" (value),
165 "r" (offset):"A", "X", "RegB", "RegC", "RegH");
166 else
167 asm volatile ("lda $%0\n\tldx $%1\n\tjsr EEwrite"::"i" (value),
168 "i" (offset):"A", "X", "RegB", "RegC", "RegH");
172 static void inline
173 _EEsetChecksum ()
175 asm volatile ("jsr EEsetChecksum":::"A", "X", "RegB", "RegC", "RegH");
178 static uchar inline
179 _U_debounce (uchar value, uchar time)
181 uchar ret;
182 if (!__builtin_constant_p (time))
184 volatile ("lda %1\n\tldx %2\n\tjsr U_debounce\n\tsta %0":"=r" (ret):"r"
185 (value), "r" (time):"X", "A", "RegB", "RegC", "RegD", "RegE",
186 "RegF", "RegG");
187 else
189 volatile ("lda %1\n\tldx $%2\n\tjsr U_debounce\n\tsta %0":"=r" (ret):"r"
190 (value), "i" (time):"X", "A", "RegB", "RegC", "RegD", "RegE",
191 "RegF", "RegG");
192 return ret;
195 static uchar inline
196 _U_deb10 (uchar value)
198 uchar ret;
199 asm volatile ("lda %1\n\tjsr U_deb10\n\tsta %0":"=r" (ret):"r" (value):"X",
200 "A", "RegB", "RegC", "RegD", "RegE", "RegF", "RegG");
201 return ret;
204 static uchar inline
205 _U_deb30 (uchar value)
207 uchar ret;
208 asm volatile ("lda %1\n\tjsr U_deb30\n\tsta %0":"=r" (ret):"r" (value):"X",
209 "A", "RegB", "RegC", "RegD", "RegE", "RegF", "RegG");
210 return ret;
213 static void inline
214 _U_delMsgs ()
216 asm volatile ("jsr U_delMsgs":::"A", "X", "RegB");
219 static short inline
220 _U_readAD (uchar channel, uchar count)
222 short ret;
223 if (!__builtin_constant_p (count))
225 if (!__builtin_constant_p (channel))
227 volatile ("lda %1\n\tldx %2\n\tjsr U_readAD":"=t" (ret):"r"
228 (channel), "r" (count):"A", "X");
229 else
231 volatile ("lda %1\n\tldx $%2\n\tjsr U_readAD":"=t" (ret):"r"
232 (channel), "i" (count):"A", "X");
234 else
236 if (!__builtin_constant_p (channel))
238 volatile ("lda $%1\n\tldx %2\n\tjsr U_readAD":"=t" (ret):"i"
239 (channel), "r" (count):"A", "X");
240 else
242 volatile ("lda $%1\n\tldx $%2\n\tjsr U_readAD":"=t" (ret):"i"
243 (channel), "i" (count):"A", "X");
245 return ret;
248 static U_map_Result inline
249 _U_map (signed short value, uchar ptr)
251 U_map_Result ret;
252 if (!__builtin_constant_p (ptr))
254 volatile
255 ("ldx %2\n\tjsr U_map" SETAONCARRY "sta %1":"=q"
256 (ret.value), "=r" (ret.error):"r" (ptr), "q" (value):"A", "X", "RegD",
257 "RegE", "RegF", "RegG", "RegH", "RegI");
258 else
260 volatile
261 ("ldx $%2\n\tjsr U_map" SETAONCARRY "sta %1":"=q"
262 (ret.value), "=r" (ret.error):"i" (ptr), "q" (value):"A", "X", "RegD",
263 "RegE", "RegF", "RegG", "RegH", "RegI");
264 return ret;
267 static signed short inline
268 _U_map_NE (signed short value, uchar ptr)
270 signed short ret;
271 if (!__builtin_constant_p (ptr))
273 volatile
274 ("ldx %1\n\tjsr U_map":"=q"
275 (ret):"r" (ptr), "q" (value):"A", "X", "RegD",
276 "RegE", "RegF", "RegG", "RegH", "RegI");
277 else
279 volatile
280 ("ldx $%1\n\tjsr U_map":"=q"
281 (ret):"i" (ptr), "q" (value):"A", "X", "RegD",
282 "RegE", "RegF", "RegG", "RegH", "RegI");
283 return ret;
286 static uchar inline
287 _U_ioAST (uchar val)
289 uchar ret;
290 if (!__builtin_constant_p (val))
291 asm volatile ("lda %1\n\tjsr U_ioAST":"=z" (ret):"r" (val):"A", "X",
292 "RegC", "RegD");
293 else
294 asm volatile ("lda $%1\n\tjsr U_ioAST":"=z" (ret):"i" (val):"A", "X",
295 "RegC", "RegD");
296 return ret;
299 static S_xxShift_Result inline
300 _S_AstShift (uchar ptr)
302 S_xxShift_Result ret;
303 if (!__builtin_constant_p (ptr))
305 volatile
306 ("ldx %2\n\tjsr S_AstShift\n\tstx %0" SETAONCARRY "sta %1":"=r"
307 (ret.pointer), "=r" (ret.error):"r" (ptr):"A", "X", "RegB", "RegC",
308 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
309 else
311 volatile
312 ("ldx $%2\n\tjsr S_AstShift\n\tstx %0" SETAONCARRY "sta %1":"=r"
313 (ret.pointer), "=r" (ret.error):"i" (ptr):"A", "X", "RegB", "RegC",
314 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
315 return ret;
318 static uchar inline
319 _S_AstShift_NE (uchar ptr)
321 uchar ret;
322 if (!__builtin_constant_p (ptr))
324 volatile
325 ("ldx %1\n\tjsr S_AstShift\n\tstx %0":"=r"
326 (ret):"r" (ptr):"A", "X", "RegB", "RegC",
327 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
328 else
330 volatile
331 ("ldx $%1\n\tjsr S_AstShift\n\tstx %0":"=r"
332 (ret):"i" (ptr):"A", "X", "RegB", "RegC",
333 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
334 return ret;
337 static S_xxShift_Result inline
338 _S_LastShift (uchar ptr)
340 S_xxShift_Result ret;
341 if (!__builtin_constant_p (ptr))
343 volatile
344 ("ldx %2\n\tjsr S_LastShift\n\tstx %0" SETAONCARRY "sta %1":"=r"
345 (ret.pointer), "=r" (ret.error):"r" (ptr):"A", "X", "RegB", "RegC",
346 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
347 else
349 volatile
350 ("ldx $%2\n\tjsr S_LastShift\n\tstx %0" SETAONCARRY "sta %1":"=r"
351 (ret.pointer), "=r" (ret.error):"i" (ptr):"A", "X", "RegB", "RegC",
352 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
353 return ret;
356 static uchar inline
357 _S_LastShift_NE (uchar ptr)
359 uchar ret;
360 if (!__builtin_constant_p (ptr))
362 volatile
363 ("ldx %1\n\tjsr S_LastShift\n\tstx %0":"=r"
364 (ret):"r" (ptr):"A", "X", "RegB", "RegC",
365 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
366 else
368 volatile
369 ("ldx $%1\n\tjsr S_LastShift\n\tstx %0":"=r"
370 (ret):"i" (ptr):"A", "X", "RegB", "RegC",
371 "RegD", "RegE", "RegF", "RegG", "RegH", "RegI");
372 return ret;
375 static U_SerialShift_Result inline
376 _U_SerialShift (uchar octet)
378 U_SerialShift_Result ret;
379 if (!__builtin_constant_p (octet))
381 volatile
382 ("lda %2\n\tjsr U_SerialShift\n\tsta %0" SETAONCARRY "sta %1":"=r"
383 (ret.octet), "=r" (ret.error):"r" (octet):"A", "X", "RegB", "RegC",
384 "RegD", "RegE", "RegF", "RegG", "RegI");
385 else
387 volatile
388 ("lda $%2\n\tjsr U_SerialShift\n\tsta %0" SETAONCARRY "sta %1":"=r"
389 (ret.octet), "=r" (ret.error):"i" (octet):"A", "X", "RegB", "RegC",
390 "RegD", "RegE", "RegF", "RegG", "RegI");
391 return ret;
394 static uchar inline
395 _U_SerialShift_NE (uchar octet)
397 uchar ret;
398 if (!__builtin_constant_p (octet))
400 volatile
401 ("lda %1\n\tjsr U_SerialShift\n\tsta %0":"=r"
402 (ret):"r" (octet):"A", "X", "RegB", "RegC",
403 "RegD", "RegE", "RegF", "RegG", "RegI");
404 else
406 volatile
407 ("lda $%1\n\tjsr U_SerialShift\n\tsta %0":"=r"
408 (ret):"i" (octet):"A", "X", "RegB", "RegC",
409 "RegD", "RegE", "RegF", "RegG", "RegI");
410 return ret;
413 static void inline
414 _TM_Load (uchar setup, uchar runtime)
416 if (!__builtin_constant_p (runtime))
418 if (!__builtin_constant_p (setup))
419 asm volatile ("lda %0\n\tldx %1\n\tjsr TM_Load"::"r" (setup),
420 "r" (runtime):"A", "X", "RegB", "RegC", "RegD", "RegE",
421 "RegF");
422 else
423 asm volatile ("lda %0\n\tldx $%1\n\tjsr TM_Load"::"r" (setup),
424 "i" (runtime):"A", "X", "RegB", "RegC", "RegD", "RegE",
425 "RegF");
427 else
429 if (!__builtin_constant_p (setup))
430 asm volatile ("lda $%0\n\tldx %1\n\tjsr TM_Load"::"i" (setup),
431 "r" (runtime):"A", "X", "RegB", "RegC", "RegD", "RegE",
432 "RegF");
433 else
434 asm volatile ("lda $%0\n\tldx $%1\n\tjsr TM_Load"::"i" (setup),
435 "i" (runtime):"A", "X", "RegB", "RegC", "RegD", "RegE",
436 "RegF");
440 static TM_GetFlg_Result inline
441 _TM_GetFlg (uchar timer)
443 TM_GetFlg_Result ret;
444 if (!__builtin_constant_p (timer))
446 volatile
447 ("lda %2\n\tjsr TM_GetFlg\n\tsta %0" SETAONCARRY "sta %1":"=r"
448 (ret.time), "=r" (ret.expired):"r" (timer):"A", "X", "RegB", "RegC",
449 "RegD", "RegE");
450 else
452 volatile
453 ("lda $%2\n\tjsr TM_GetFlg\n\tsta %0" SETAONCARRY "sta %1":"=r"
454 (ret.time), "=r" (ret.expired):"i" (timer):"A", "X", "RegB", "RegC",
455 "RegD", "RegE");
456 return ret;
459 static bool inline
460 _TM_GetFlg_M0 (uchar timer)
462 bool ret;
463 if (!__builtin_constant_p (timer))
465 volatile
466 ("lda %1\n\tjsr TM_GetFlg" SETAONCARRY "sta %0":"=r"
467 (ret):"r" (timer):"A", "X", "RegB", "RegC", "RegD", "RegE");
468 else
470 volatile
471 ("lda $%1\n\tjsr TM_GetFlg" SETAONCARRY "sta %1":"=r"
472 (ret):"i" (timer):"A", "X", "RegB", "RegC", "RegD", "RegE");
473 return ret;
476 static uchar inline
477 _TM_GetFlg_M1 (uchar timer)
479 uchar ret;
480 if (!__builtin_constant_p (timer))
482 volatile
483 ("lda %1\n\tjsr TM_GetFlg\n\tsta %0":"=r"
484 (ret):"r" (timer):"A", "X", "RegB", "RegC", "RegD", "RegE");
485 else
487 volatile
488 ("lda $%1\n\tjsr TM_GetFlg\n\tsta %0":"=r"
489 (ret):"i" (timer):"A", "X", "RegB", "RegC", "RegD", "RegE");
490 return ret;
493 static void inline
494 _U_SetTM (uchar timer, uchar pointer, uchar time)
496 if (!__builtin_constant_p (pointer))
498 if (!__builtin_constant_p (timer))
500 volatile ("lda %1\n\tldx %2\n\tjsr U_SetTM"::"e" (time),
501 "r" (timer), "r" (pointer):"A", "X", "RegB", "RegC",
502 "RegD");
503 else
504 asm volatile ("lda %1\n\tldx $%2\n\tjsr U_SetTM"::"e" (time),
505 "r" (timer), "i" (pointer):"A", "X", "RegB", "RegC",
506 "RegD");
508 else
510 if (!__builtin_constant_p (timer))
512 volatile ("lda $%1\n\tldx %2\n\tjsr U_SetTM"::"e" (time),
513 "i" (timer), "r" (pointer):"A", "X", "RegB", "RegC",
514 "RegD");
515 else
517 volatile ("lda $%1\n\tldx $%2\n\tjsr U_SetTM"::"e" (time),
518 "i" (timer), "i" (pointer):"A", "X", "RegB", "RegC",
519 "RegD");
523 static void inline
524 _U_SetTMx (uchar timer, uchar time)
526 if (!__builtin_constant_p (timer))
527 asm volatile ("lda %1\n\tjsr U_SetTMx"::"e" (time), "r" (timer):"A", "X",
528 "RegB", "RegC", "RegD");
529 else
530 asm volatile ("lda %1\n\tjsr U_SetTMx"::"e" (time), "r" (timer):"A", "X",
531 "RegB", "RegC", "RegD");
534 static bool inline
535 _U_GetTM (uchar timer, uchar pointer)
537 uchar ret;
538 if (!__builtin_constant_p (pointer))
540 if (!__builtin_constant_p (timer))
542 volatile
543 ("lda %1\n\tldx %2\n\tjsr U_GetTM" SETAONZERO "sta %0":"=r"
544 (ret):"r" (timer), "r" (pointer):"A", "X", "RegB", "RegC", "RegD");
545 else
547 volatile
548 ("lda $%1\n\tldx %2\n\tjsr U_GetTM" SETAONZERO "sta %0":"=r"
549 (ret):"i" (timer), "r" (pointer):"A", "X", "RegB", "RegC", "RegD");
551 else
553 if (!__builtin_constant_p (timer))
555 volatile
556 ("lda %1\n\tldx $%2\n\tjsr U_GetTM" SETAONZERO "sta %0":"=r"
557 (ret):"r" (timer), "i" (pointer):"A", "X", "RegB", "RegC", "RegD");
558 else
560 volatile
561 ("lda $%1\n\tldx $%2\n\tjsr U_GetTM" SETAONZERO "sta %0":"=r"
562 (ret):"i" (timer), "i" (pointer):"A", "X", "RegB", "RegC", "RegD");
564 return ret;
567 static bool inline
568 _U_GetTMx (uchar timer)
570 uchar ret;
571 if (!__builtin_constant_p (timer))
573 volatile
574 ("lda %1\n\tjsr U_GetTM" SETAONZERO "sta %0":"=r"
575 (ret):"r" (timer):"A", "X", "RegB", "RegC", "RegD");
576 else
578 volatile
579 ("lda $%1\n\tjsr U_GetTM" SETAONZERO "sta %0":"=r"
580 (ret):"i" (timer):"A", "X", "RegB", "RegC", "RegD");
581 return ret;
584 static void inline
585 _U_Delay (uchar delay)
587 if (!__builtin_constant_p (delay))
588 asm volatile ("lda %0\n\tjsr U_Delay"::"r" (delay):"A", "X", "RegB");
589 else
590 asm volatile ("lda $%0\n\tjsr U_Delay"::"i" (delay):"A", "X", "RegB");
593 static AllocBuf_Result inline
594 _AllocBuf (bool longbuf)
596 AllocBuf_Result ret;
597 uchar e;
598 if (longbuf)
600 volatile
601 ("sec\n\tjsr AllocBuf\n\tstx %1" SETAONCARRY "sta %0":"=r"
602 (ret.valid), "=r" (e)::"A", "X");
603 else
605 volatile
606 ("clc\n\tjsr AllocBuf\n\tstx %1" SETAONCARRY "sta %0":"=r"
607 (ret.valid), "=r" (e)::"A", "X");
608 ret.pointer = (uchar_loptr) (uint1) e;
609 return ret;
612 static uchar_loptr inline
613 _AllocBuf_NE (bool longbuf)
615 uchar ret;
616 if (longbuf)
617 asm volatile ("sec\n\tjsr AllocBuf\n\tstx %0":"=r" (ret)::"A", "X");
618 else
619 asm volatile ("clc\n\tjsr AllocBuf\n\tstx %0":"=r" (ret)::"A", "X");
620 return (uchar_loptr) (uint1) ret;
623 static void inline
624 _FreeBuf (uchar_loptr pointer)
626 uchar x = ((uint1) pointer);
627 if (!__builtin_constant_p (x))
628 asm volatile ("ldx %0\n\tjsr FreeBuf"::"r" (x):"A", "X", "RegB");
629 else
630 asm volatile ("ldx $%0\n\tjsr FreeBuf"::"i" (x):"A", "X", "RegB");
633 static PopBuf_Result inline
634 _PopBuf (uchar msg)
636 PopBuf_Result ret;
637 uchar e;
638 if (!__builtin_constant_p (msg))
640 volatile
641 ("lda %2\n\tjsr PopBuf\n\tstx %1" SETAONCARRY "sta %0":"=r"
642 (ret.found), "=r" (e):"r" (msg):"A", "X", "RegB");
643 else
645 volatile
646 ("lda $%2\n\tjsr PopBuf\n\tstx %1" SETAONCARRY "sta %0":"=r"
647 (ret.found), "=r" (e):"i" (msg):"A", "X", "RegB");
648 ret.pointer = (uchar_loptr) (uint1) e;
649 return ret;
652 static uchar_loptr inline
653 _PopBuf_NE (uchar msg)
655 uchar ret;
656 if (!__builtin_constant_p (msg))
658 volatile
659 ("lda %1\n\tjsr PopBuf\n\tstx %0":"=r"
660 (ret):"r" (msg):"A", "X", "RegB");
661 else
663 volatile
664 ("lda $%1\n\tjsr PopBuf\n\tstx %1":"=r" (ret):"i" (msg):"A", "X", "RegB");
665 return (uchar_loptr) (uint1) ret;
668 static U_Mul_Result inline
669 _multDE_FG (unsigned short v1, unsigned short v2)
671 U_Mul_Result ret;
673 volatile
674 ("jsr multDE_FG" SETAONCARRY "sta %0":"=r"
675 (ret.overflow), "=q" (ret.product):"t" (v1), "u" (v2):"A", "X");
676 return ret;
678 static unsigned short inline
679 _multDE_FG_NE (unsigned short v1, unsigned short v2)
681 unsigned short ret;
682 asm volatile ("jsr multDE_FG":"=q" (ret):"t" (v1), "u" (v2):"A", "X");
683 return ret;
686 static U_Div_Result inline
687 _divDE_BC (unsigned short dividend, unsigned short divisor)
689 U_Div_Result ret;
691 volatile
692 ("jsr divDE_BC" SETAONCARRY "sta %0":"=r"
693 (ret.error), "=u" (ret.quotient), "=t" (ret.remainder):"q" (divisor),
694 "t" (dividend):"A", "X");
695 return ret;
697 static unsigned short inline
698 _divDE_BC_quotient (unsigned short dividend, unsigned short divisor)
700 U_Div_Result ret;
702 volatile
703 ("jsr divDE_BC":"=u" (ret.quotient), "=t" (ret.remainder):"q" (divisor),
704 "t" (dividend):"A", "X");
705 return ret.quotient;
708 static unsigned short inline
709 _divDE_BC_remainder (unsigned short dividend, unsigned short divisor)
711 U_Div_Result ret;
713 volatile
714 ("jsr divDE_BC":"=u" (ret.quotient), "=t" (ret.remainder):"q" (divisor),
715 "t" (dividend):"A", "X");
716 return ret.remainder;
719 #define DEF_SHIFTROT(NAME) static uchar inline _##NAME(uchar val){uchar ret;asm volatile ("lda %1\n\t jsr " #NAME "\n\tsta %1":"=r"(ret):"r"(val):"A");return ret; }
721 DEF_SHIFTROT (shlA4)
722 DEF_SHIFTROT (shlA5)
723 DEF_SHIFTROT (shlA6)
724 DEF_SHIFTROT (shlA7)
725 DEF_SHIFTROT (shrA4)
726 DEF_SHIFTROT (shrA5)
727 DEF_SHIFTROT (shrA6)
728 DEF_SHIFTROT (shrA7)
729 DEF_SHIFTROT (rolA1)
730 DEF_SHIFTROT (rolA2)
731 DEF_SHIFTROT (rolA3) DEF_SHIFTROT (rolA4) DEF_SHIFTROT (rolA7)
732 static
733 uchar
734 inline
735 _U_SetBit (uchar octet, uchar bit, bool set)
737 if (!__builtin_constant_p (bit))
739 if (set)
740 asm volatile ("sec\n\tlda %1\n\tjsr U_SetBit":"=h" (octet):"r" (bit),
741 "0" (octet):"RegB", "A");
742 else
743 asm volatile ("clc\n\tlda %1\n\tjsr U_SetBit":"=h" (octet):"r" (bit),
744 "0" (octet):"RegB", "A");
746 else
748 if (set)
749 asm volatile ("sec\n\tlda $%1\n\tjsr U_SetBit":"=h" (octet):"i" (bit),
750 "0" (octet):"RegB", "A");
751 else
752 asm volatile ("clc\n\tlda $%1\n\tjsr U_SetBit":"=h" (octet):"i" (bit),
753 "0" (octet):"RegB", "A");
755 return octet;
758 static bool inline
759 _U_GetBit (uchar octet, uchar bit)
761 uchar ret;
762 if (!__builtin_constant_p (bit))
764 volatile
765 ("lda %1\n\tjsr U_GetBit" SETAONNZERO "sta %0":"=r"
766 (ret):"r" (bit), "h" (octet):"A", "X", "RegB");
767 else
769 volatile
770 ("lda $%1\n\tjsr U_GetBit" SETAONNZERO "sta %0":"=r"
771 (ret):"i" (bit), "h" (octet):"A", "X", "RegB");
772 return ret;
775 #ifdef BCU_0020_H
777 static void inline
778 _U_EE_WriteBlock (void *ptr, long data)
780 asm volatile ("jsr U_EE_WriteBlock"::"v" (ptr), "y" (data):"A", "X", "RegB",
781 "RegC", "RegJ");
784 static uchar inline
785 _U_GetAccess ()
787 uchar ret;
788 asm volatile ("jsr U_GetAccess\n\tsta %0":"=r" (ret)::"A");
789 return ret;
792 static void inline
793 _U_SetPollingRsp (uchar val)
795 if (!__builtin_constant_p (val))
796 asm volatile ("lda %0\n\tjsr U_SetPollingRsp"::"r" (val):"A");
797 else
798 asm volatile ("lda $%0\n\tjsr U_SetPollingRsp"::"i" (val):"A");
801 static void inline
802 _U_Char_Out (uchar val)
804 if (!__builtin_constant_p (val))
805 asm volatile ("lda %0\n\tjsr U_Char_Out"::"r" (val):"A", "X");
806 else
807 asm volatile ("lda $%0\n\tjsr U_Char_Out"::"i" (val):"A", "X");
810 static void inline
811 _U_TS_Set (uchar timer, uchar mode, uchar scale, uchar value, uchar param)
813 if (!__builtin_constant_p (timer))
815 if (!__builtin_constant_p (mode))
816 asm volatile ("lda %1\n\tldx %2\n\tjsr U_TS_Set"::"z" (scale),
817 "r" (mode), "r" (timer), "c" (value), "d" (param):"A",
818 "X", "RegB", "RegC", "RegD", "RegE", "RegF");
819 else
820 asm volatile ("lda %1\n\tldx $%2\n\tjsr U_TS_Set"::"z" (scale),
821 "r" (mode), "i" (timer), "c" (value), "d" (param):"A",
822 "X", "RegB", "RegC", "RegD", "RegE", "RegF");
824 else
826 if (!__builtin_constant_p (mode))
827 asm volatile ("lda $%1\n\tldx %2\n\tjsr U_TS_Set"::"z" (scale),
828 "i" (mode), "r" (timer), "c" (value), "d" (param):"A",
829 "X", "RegB", "RegC", "RegD", "RegE", "RegF");
830 else
831 asm volatile ("lda $%1\n\tldx $%2\n\tjsr U_TS_Set"::"z" (scale),
832 "i" (mode), "i" (timer), "c" (value), "d" (param):"A",
833 "X", "RegB", "RegC", "RegD", "RegE", "RegF");
837 static void inline
838 _U_TS_Del (uchar val)
840 if (!__builtin_constant_p (val))
841 asm volatile ("ldx %0\n\tjsr U_TS_Del"::"r" (val):"X");
842 else
843 asm volatile ("ldx $%0\n\tjsr U_TS_Del"::"i" (val):"X");
846 static void inline
847 _U_MS_Post (uchar msgid, uchar_loptr pointer)
849 if (!__builtin_constant_p (msgid))
851 if (!__builtin_constant_p (pointer))
852 asm volatile ("lda %0\n\tldx %1\n\tjsr U_MS_Post"::"r" (msgid),
853 "r" (pointer):"A", "X", "RegB", "RegL", "RegM", "RegN");
854 else
855 asm volatile ("lda %0\n\tldx $%1\n\tjsr U_MS_Post"::"r" (msgid),
856 "i" (pointer):"A", "X", "RegB", "RegL", "RegM", "RegN");
858 else
860 if (!__builtin_constant_p (pointer))
861 asm volatile ("lda $%0\n\tldx %1\n\tjsr U_MS_Post"::"i" (msgid),
862 "r" (pointer):"A", "X", "RegB", "RegL", "RegM", "RegN");
863 else
864 asm volatile ("lda $%0\n\tldx $%1\n\tjsr U_MS_Post"::"i" (msgid),
865 "i" (pointer):"A", "X", "RegB", "RegL", "RegM", "RegN");
869 static void inline
870 _U_MS_Switch (uchar msgid, uchar destination)
872 if (!__builtin_constant_p (msgid))
874 if (!__builtin_constant_p (destination))
875 asm volatile ("lda %0\n\tldx %1\n\tjsr U_MS_Switch"::
876 "r" (destination), "r" (msgid):"A", "X", "RegB", "RegL",
877 "RegM", "RegN");
878 else
879 asm volatile ("lda %0\n\tldx $%1\n\tjsr U_MS_Switch"::"r" (destination),
880 "i" (msgid):"A", "X", "RegB", "RegL", "RegM", "RegN");
882 else
884 if (!__builtin_constant_p (destination))
885 asm volatile ("lda $%0\n\tldx %1\n\tjsr U_MS_Switch"::
886 "i" (destination), "r" (msgid):"A", "X", "RegB", "RegL",
887 "RegM", "RegN");
888 else
889 asm volatile ("lda $%0\n\tldx $%1\n\tjsr U_MS_Switch"::
890 "i" (destination), "i" (msgid):"A", "X", "RegB", "RegL",
891 "RegM", "RegN");
895 static short inline
896 _FP_Flt2Int (uchar ptr, uchar exponent)
898 short ret;
899 if (!__builtin_constant_p (ptr))
900 asm volatile ("ldx %1\n\tjsr FP_Flt2Int":"=q" (ret):"r" (ptr),
901 "f" (exponent):"A", "X", "RegD", "RegE");
902 else
903 asm volatile ("ldx $%1\n\tjsr FP_Flt2Int":"=q" (ret):"i" (ptr),
904 "f" (exponent):"A", "X", "RegD", "RegE");
905 return ret;
908 static void inline
909 _FP_Int2Flt (short val, uchar ptr, uchar exponent)
911 if (!__builtin_constant_p (ptr))
912 asm volatile ("ldx %0\n\tjsr FP_Int2Flt"::"r" (ptr), "f" (exponent),
913 "q" (val));
914 else
915 asm volatile ("ldx $%0\n\tjsr FP_Int2Flt"::"i" (ptr), "f" (exponent),
916 "q" (val));
919 static void inline
920 _U_FT12_Reset (uchar baudrate)
922 if (!__builtin_constant_p (baudrate))
923 asm volatile ("lda %0\n\tjsr U_FT12_Reset"::"r" (baudrate):"A", "X",
924 "RegB", "RegC", "RegD", "RegE", "RegF", "RegG", "RegH",
925 "RegI", "RegJ", "RegK", "RegL", "RegM", "RegN");
926 else
927 asm volatile ("lda $%0\n\tjsr U_FT12_Reset"::"i" (baudrate):"A", "X",
928 "RegB", "RegC", "RegD", "RegE", "RegF", "RegG", "RegH",
929 "RegI", "RegJ", "RegK", "RegL", "RegM", "RegN");
932 static FT12_GetStatus_Result inline
933 _U_FT12_GetStatus (bool force_reset)
935 FT12_GetStatus_Result ret;
936 if (force_reset)
938 volatile
939 ("sec\n\tjsr U_FT12_GetStatus\n\tsta %0" SETAONCARRY "sta %1":"=r"
940 (ret.stateok), "=r" (ret.newstate)::"A");
941 else
943 volatile
944 ("clc\n\tjsr U_FT12_GetStatus\n\tsta %0" SETAONCARRY "sta %1":"=r"
945 (ret.stateok), "=r" (ret.newstate)::"A");
946 return ret;
949 static void inline
950 _U_SCI_Init (uchar baudrate)
952 if (!__builtin_constant_p (baudrate))
953 asm volatile ("lda %0\n\tjsr U_SCI_Init"::"r" (baudrate):"A", "X");
954 else
955 asm volatile ("lda $%0\n\tjsr U_SCI_Init"::"i" (baudrate):"A", "X");
958 static void inline
959 _U_SPI_Init ()
961 asm volatile ("jsr U_SPI_Init":::"A", "X");
964 #undef SETAONCARRY
965 #undef SETAONZERO
966 #undef SETAONNZERO
968 #endif
970 static inline void
971 __U_flag_Set (uchar no, uchar bit)
973 if (__builtin_constant_p (no) && __builtin_constant_p (bit))
974 asm volatile ("bset.%1 %0+ramflag_pointer"::"i" (no / 2),
975 "i" ((bit % 4) + (no % 2) * 4));
976 else
977 _U_flagsSet (no, _U_flagsGet (no) | (1 << bit));
980 static inline void
981 __U_flag_Clear (uchar no, uchar bit)
983 if (__builtin_constant_p (no) && __builtin_constant_p (bit))
984 asm volatile ("bclr.%1 %0+ramflag_pointer"::"i" (no / 2),
985 "i" ((bit % 4) + (no % 2) * 4));
986 else
987 _U_flagsSet (no, _U_flagsGet (no) & ~(1 << bit));
990 static inline void
991 __U_transRequest (uchar no)
993 if (__builtin_constant_p (no))
995 __U_flag_Set (no, 0);
996 __U_flag_Set (no, 1);
998 else
999 _U_transRequest (no);
1003 /* not implemented:
1004 AL_SAPcallback
1005 U_EE_WriteHI
1007 not supported:
1008 U_TS_Seti
1011 #endif