1 // TODO at least, use #include and #define, but #define would require me to start all over...
3 // no forward declarations
4 // Scheme-style hex literals
5 // no explicit casts (intermediate variables instead)
6 // removed functions that interact with the firmware, and with rom
7 // had some case labels that were not literals (but could have been constant folded) in the dispatch
9 /* typedef char int8; */
10 /* typedef short int16; */
11 /* typedef long int32; */
12 /* typedef unsigned char uint8; */
13 /* typedef unsigned short uint16; */
14 /* typedef unsigned long uint32; */
16 /* typedef uint8 word; */
18 /* typedef uint16 ram_addr; */
19 /* typedef uint16 rom_addr; */
40 void halt_with_error (){
45 /* typedef int16 obj; */ // TODO actually has 21 bits
46 /* int8 rom_get (int16 a){ */
47 /* return /\* *(int8*) *\/a; // TODO had rom, but caused problems */
48 /* } */ // TODO now a predefined routine
49 /* int8 ram_get_gc_tags (int16 o); */
50 /* int8 ram_get_gc_tag0 (int16 o); */
51 /* int8 ram_get_gc_tag1 (int16 o); */
52 /* void ram_set_gc_tags (int16 o, int8 tags); */
53 /* void ram_set_gc_tag0 (int16 o, int8 tag); */
54 /* void ram_set_gc_tag1 (int16 o, int8 tag); */
55 /* int8 ram_get_field0 (int16 o); */
56 /* int8 ram_get_field1 (int16 o); */
57 /* int8 ram_get_field2 (int16 o); */
58 /* int8 ram_get_field3 (int16 o); */
59 /* int8 ram_get_fieldn (int16 o, int8 n); */
60 /* void ram_set_field0 (int16 o, int8 val); */
61 /* void ram_set_field1 (int16 o, int8 val); */
62 /* void ram_set_field2 (int16 o, int8 val); */
63 /* void ram_set_field3 (int16 o, int8 val); */
64 /* void ram_set_fieldn (int16 o, int8 n, int8 val); */
65 /* int8 rom_get_field0 (int16 o); */
66 /* int8 rom_get_field1 (int16 o); */
67 /* int8 rom_get_field2 (int16 o); */
68 /* int8 rom_get_field3 (int16 o); */
69 int8
ram_get_gc_tags (int16 o
) {
71 return (*(((t2
<< 2))+#x200) & #x60); // TODO not sure these shifts really save on code space, maybe multiplications (which would be 2 additions) would be better
73 int8
ram_get_gc_tag0 (int16 o
) {
75 return (*(((t2
<< 2))+#x200) & #x20);
77 int8
ram_get_gc_tag1 (int16 o
) {
79 return (*(((t2
<< 2))+#x200) & #x40);
81 void ram_set_gc_tags (int16 o
, int8 tags
) {
82 int16 t2
= (o
- 512) << 2; // TODO optimized a couple of things
83 (*((t2
)+#x200) = ((*((t2)+#x200) & #x9f) | (tags))); // TODO if we could use bst and bcf, would be better
85 void ram_set_gc_tag0 (int16 o
, int8 tag
) {
86 int16 t2
= (o
- 512) << 2; // TODO same here
87 *(t2
+#x200) = ((*(t2+#x200) & #xdf) | (tag));
89 void ram_set_gc_tag1 (int16 o
, int8 tag
) {
90 int16 t2
= (o
- 512) << 2; // TODO same here
91 *(t2
+#x200) = ((*(t2+#x200) & #xbf) | (tag));
93 int8
ram_get_field0 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (0))+#x200); }
94 int8
ram_get_field1 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (1))+#x200); }
95 int8
ram_get_field2 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (2))+#x200); }
96 int8
ram_get_field3 (int16 o
) { int16 t2
= o
- 512; return *(((t2
<< 2) + (3))+#x200); }
97 int8
ram_get_fieldn (int16 o
, int8 n
) {
99 case 0: return ram_get_field0 (o
);
100 case 1: return ram_get_field1 (o
);
101 case 2: return ram_get_field2 (o
);
102 case 3: return ram_get_field3 (o
);
105 void ram_set_field0 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (0))+#x200) = (val); }
106 void ram_set_field1 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (1))+#x200) = (val); }
107 void ram_set_field2 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (2))+#x200) = (val); }
108 void ram_set_field3 (int16 o
, int8 val
) { int16 t2
= o
- 512; *(((t2
<< 2) + (3))+#x200) = (val); }
109 void ram_set_fieldn (int16 o
, int8 n
, int8 val
) {
111 case 0: ram_set_field0 (o
, val
); break;
112 case 1: ram_set_field1 (o
, val
); break;
113 case 2: ram_set_field2 (o
, val
); break;
114 case 3: ram_set_field3 (o
, val
); break;
117 int8
rom_get_field0 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x8000 + 4 + (0)))); }
118 int8
rom_get_field1 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x8000 + 4 + (1)))); }
119 int8
rom_get_field2 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x8000 + 4 + (2)))); }
120 int8
rom_get_field3 (int16 o
) { int16 t2
= (o
) - (3 +255 - -1 +1); return rom_get (((t2
<< 2) + (#x8000 + 4 + (3)))); }
123 /* int16 ram_get_car (int16 o); */
124 /* int16 rom_get_car (int16 o); */
125 /* int16 ram_get_cdr (int16 o); */
126 /* int16 rom_get_cdr (int16 o); */
127 /* void ram_set_car (int16 o, int16 val); */
128 /* void ram_set_cdr (int16 o, int16 val); */
130 /* int16 ram_get_entry (int16 o); */
131 /* int16 rom_get_entry (int16 o); */
133 int16
ram_get_car (int16 o
)
134 { int16 tmp
= (ram_get_field0 (o
) & #x1f); return (tmp << 8) | ram_get_field1 (o); }
135 int16
rom_get_car (int16 o
)
136 { int16 tmp
= (rom_get_field0 (o
) & #x1f); return (tmp << 8) | rom_get_field1 (o); }
137 int16
ram_get_cdr (int16 o
)
138 { int16 tmp
= (ram_get_field2 (o
) & #x1f); return (tmp << 8) | ram_get_field3 (o); }
139 int16
rom_get_cdr (int16 o
)
140 { int16 tmp
= (rom_get_field2 (o
) & #x1f); return (tmp << 8) | rom_get_field3 (o); }
142 void ram_set_car (int16 o
, int16 val
) {
143 ram_set_field0 (o
, (val
>> 8) | (ram_get_field0 (o
) & #xe0));
144 ram_set_field1 (o
, val
& #xff);
146 void ram_set_cdr (int16 o
, int16 val
) {
147 ram_set_field2 (o
, (val
>> 8) | (ram_get_field2 (o
) & #xe0));
148 ram_set_field3 (o
, val
& #xff);
152 int16
ram_get_entry (int16 o
) {
153 int16 tmp
= (ram_get_field0 (o
) & #x1f);
155 | (ram_get_field1 (o
) << 3)
156 | (ram_get_field2 (o
) >> 5));
158 int16
rom_get_entry (int16 o
){
159 int16 tmp
= (rom_get_field0 (o
) & #x1f);
161 | (rom_get_field1 (o
) << 3)
162 | (rom_get_field2 (o
) >> 5));
166 /* int16 get_global (int8 i); */
167 /* void set_global (int8 i, int16 o); */
169 int16
get_global (int8 i
) {
172 return ram_get_cdr (512 + (i
>> 1));
174 return ram_get_car (512 + (i
>> 1));
176 void set_global (int8 i
, int16 o
) {
178 ram_set_cdr (512 + (i
>> 1), o
);
180 ram_set_car (512 + (i
>> 1), o
);
186 void mark (int16 temp
) {
192 if ((!((temp
) >= 1280) && ((temp
) >= 512))) {
202 if (((((ram_get_field0 (visit
) & #x80) == #x80) || ((ram_get_field0 (visit) & #xc0) == #x40) || ((ram_get_field0 (visit) & #xc0) == 0)) && ram_get_gc_tag0 (visit))
203 || (((((ram_get_field0 (visit
) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == 0)) || (((ram_get_field0 (visit) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == #x80)))
204 && (ram_get_gc_tags (visit
) != (0<<5))))
207 if (((((ram_get_field0 (visit
) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == 0)) || (((ram_get_field0 (visit) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == #x80)))) {
212 temp
= ram_get_cdr (visit
);
214 if ((!((temp
) >= 1280) && ((temp
) >= 512))) {
216 int16 tmp
= 2; // TODO literals should be int, but that's wasteful
217 ram_set_gc_tags (visit
, (tmp
<<5));
218 ram_set_cdr (visit
, stack
);
227 if ((((ram_get_field0 (visit
) & #x80) == #x80) || ((ram_get_field0 (visit) & #xc0) == #x40) || ((ram_get_field0 (visit) & #xc0) == 0))) {
233 if (((ram_get_field0 (visit
) & #xc0) == #x40))
234 temp
= ram_get_cdr (visit
);
236 temp
= ram_get_car (visit
);
238 if ((!((temp
) >= 1280) && ((temp
) >= 512))) {
241 ram_set_gc_tag0 (visit
, (tmp
<<5));
242 if (((ram_get_field0 (visit
) & #xc0) == #x40))
243 ram_set_cdr (visit
, stack
);
245 ram_set_car (visit
, stack
);
255 ram_set_gc_tag0 (visit
, (tmp
<<5));
263 if (((((ram_get_field0 (stack
) & #x80) == #x80) && ((ram_get_field2 (stack) & #xe0) == 0)) || (((ram_get_field0 (stack) & #x80) == #x80) && ((ram_get_field2 (stack) & #xe0) == #x80))) && ram_get_gc_tag1 (stack)) {
266 temp
= ram_get_cdr (stack
);
267 ram_set_cdr (stack
, visit
);
271 ram_set_gc_tag1(visit
, (0<<5));
277 if (((ram_get_field0 (stack
) & #xc0) == #x40)) {
281 temp
= ram_get_cdr (stack
);
282 ram_set_cdr (stack
, visit
);
291 temp
= ram_get_car (stack
);
292 ram_set_car (stack
, visit
);
316 while (visit
>= (512 + ((glovars
+ 1) >> 1))) {
319 if ((((ram_get_field0 (visit
) & #x80) == #x80)
320 && (ram_get_gc_tags (visit
) == (0<<5)))
321 || !(ram_get_gc_tags (visit
) & (tmp
<<5))) {
323 if ((((ram_get_field0 (visit
) & #x80) == #x80) && ((ram_get_field2 (visit) & #xe0) == #x60))) {
325 int16 o
= ram_get_cdr (visit
);
326 int16 i
= ram_get_car (visit
);
327 ram_set_car (o
, free_list_vec
);
328 ram_set_cdr (o
, (i
+ 3) >> 2);
332 ram_set_car (visit
, free_list
);
336 if (((ram_get_field0 (visit
) & #x80) == #x80))
337 ram_set_gc_tags (visit
, (0<<5));
339 ram_set_gc_tag0 (visit
, (0<<5));
369 for (i
=0; i
<glovars
; i
++)
370 mark (get_global (i
));
376 int16
alloc_ram_cell () {
383 if (free_list
== 0) {
393 free_list
= ram_get_car (o
);
398 int16
alloc_ram_cell_init (int8 f0
, int8 f1
, int8 f2
, int8 f3
) {
399 int16 o
= alloc_ram_cell ();
401 ram_set_field0 (o
, f0
);
402 ram_set_field1 (o
, f1
);
403 ram_set_field2 (o
, f2
);
404 ram_set_field3 (o
, f3
);
409 int16
alloc_vec_cell (int16 n
) {
410 int16 o
= free_list_vec
;
419 while ((ram_get_cdr (o
) * 4) < n
) {
437 if (((ram_get_cdr(o
) * 4) - n
) < 4) {
439 ram_set_car (prec
, ram_get_car (o
));
441 free_list_vec
= ram_get_car (o
);
446 int16 new_free
= o
+ (n
+ 3) >> 2;
448 ram_set_car (prec
, new_free
);
450 free_list_vec
= new_free
;
451 ram_set_car (new_free
, ram_get_car (o
));
452 ram_set_cdr (new_free
, ram_get_cdr (o
) - (n
+ 3) >> 2);
460 /* typedef int16 integer; */
461 /* typedef int16 digit; */
462 /* typedef int32 two_digit; */
463 /* int16 make_integer (int16 lo, int16 hi); */
464 /* int16 integer_hi (int16 x); */
465 /* int16 integer_lo (int16 x); */
466 int16
make_integer (int16 lo
, int16 hi
) {
467 return alloc_ram_cell_init (0 | (hi
>> 8), hi
, lo
>> 8, lo
);
470 int16
integer_hi (int16 x
) {
471 if ((!((x
) >= 1280) && ((x
) >= 512)))
472 return ram_get_car (x
);
473 else if ((!((x
) >= 1280) && !(!((x
) >= 1280) && ((x
) >= 512)) && ((x
) >= (3 +255 - -1 +1))))
474 return rom_get_car (x
);
475 else if (x
< (3 - -1)){
476 return ((0 + (3 - -1))-1);
479 return (0 + (3 - -1));
483 int16
integer_lo (int16 x
) {
484 int16 t
= ram_get_field2 (x
);
485 if ((!((x
) >= 1280) && ((x
) >= 512)))
486 return (t
<< 8) + ram_get_field3 (x
);
487 else if ((!((x
) >= 1280) && !(!((x
) >= 1280) && ((x
) >= 512)) && ((x
) >= (3 +255 - -1 +1))))
488 return (t
<< 8) + rom_get_field3 (x
);
494 /* int16 norm (int16 prefix, int16 n); */
495 /* int8 negp (int16 x); */
496 /* int8 cmp (int16 x, int16 y); */
497 /* int16 integer_length (int16 x); */
498 /* int16 shr (int16 x); */
499 /* int16 negative_carry (int16 carry); */
500 /* int16 shl (int16 x); */
501 /* int16 shift_left (int16 x, int16 n); */
502 /* int16 add (int16 x, int16 y); */
503 /* int16 invert (int16 x); */
504 /* int16 sub (int16 x, int16 y); */
505 /* int16 neg (int16 x); */
506 /* int16 scale (int16 n, int16 x); */
507 /* int16 mulnonneg (int16 x, int16 y); */
508 /* int16 divnonneg (int16 x, int16 y); */
510 int32
decode_int (int16 o
) {
515 if (o
<= (3 + (255 - -1)))
516 return (o
- (3 - -1));
518 if ((!((o
) >= 1280) && ((o
) >= 512))) {
519 if (!((ram_get_field0 (o
) & #xc0) == 0))
521 return ram_get_field3 (o
);
523 else if ((!((o
) >= 1280) && !(!((o
) >= 1280) && ((o
) >= 512)) && ((o
) >= (3 +255 - -1 +1)))) {
524 if (!((rom_get_field0 (o
) & #xc0) == 0))
526 return rom_get_field3 (o
);
532 /* int32 decode_int (int16 o); */
533 /* int16 encode_int (int32 n); */
535 int16
norm (int16 prefix
, int16 n
) {
539 while (prefix
!= 0) {
540 int16 d
= integer_lo (prefix
);
543 prefix
= integer_hi (temp
);
545 if (((n
) == ((0 + (3 - -1))))) {
551 else if (((n
) == (((0 + (3 - -1))-1)))) {
553 if (d
>= (tmp
<<16) + -1) {
554 int16 t
= d
- (tmp
<< 16);
560 ram_set_car (temp
, n
);
567 int8
negp (int16 x
) {
572 if (((x
) == ((0 + (3 - -1))))) return 0;
573 } while (!((x
) == (((0 + (3 - -1))-1))));
578 int8
cmp (int16 x
, int16 y
) { // TODO changed. used to return -1, 0 and 1, now is 0, 1, 2
581 int8 result
= 1; // in cmp.c : cell 3
586 if (((x
) == ((0 + (3 - -1)))) // bbs 2 and 8
587 || ((x
) == (((0 + (3 - -1))-1)))) { // bbs 7 and 9
588 if (!((x
) == (y
))) // bbs 6 and 12
589 { if (negp (y
)) // bb 11
591 else result
= 0; } // bb 15
595 if (((y
) == ((0 + (3 - -1)))) // bbs 5 and 19
596 || ((y
) == (((0 + (3 - -1))-1)))) { // bbs 18 and 20
597 if (negp (x
)) // bb 17
599 else result
= 2; // bb 23
603 xlo
= integer_lo (x
); // bb 16
604 ylo
= integer_lo (y
);
607 if (xlo
!= ylo
) // bb 16 and 26
608 { if (xlo
< ylo
) // bb 25
610 else result
= 2; } // bb 29
615 int16
integer_length (int16 x
) {
623 while (!(((next
= integer_hi (x
))) == ((0 + (3 - -1))))) {
638 int16
shr (int16 x
) {
645 if (((x
) == ((0 + (3 - -1)))) || ((x
) == (((0 + (3 - -1))-1)))) {
646 result
= norm (result
, x
);
653 result
= make_integer ((d
>> 1) |
654 ((integer_lo (x
) & 1) ? (tmp
<<15) : 0), // TODO only shifting by literals is permitted, so had to change the 16 -1 to 15
661 int16
negative_carry (int16 carry
) {
663 return ((0 + (3 - -1))-1);
665 return (0 + (3 - -1));
668 int16
shl (int16 x
) {
671 int16 negc
= (0 + (3 - -1));
677 if (((x
) == (negc
))) {
678 result
= norm (result
, x
);
686 negc
= negative_carry (d
& (tmp
<<15));
687 result
= make_integer ((d
<< 1) | ((temp
) == (3)), result
); // TODO was ((0 + (3 - -1))-1)
693 int16
shift_left (int16 x
, int16 n
) {
696 if (((x
) == ((0 + (3 - -1)))))
699 while (n
& (16 -1)) {
705 x
= make_integer (0, x
);
712 int16
add (int16 x
, int16 y
) {
715 int16 negc
= (0 + (3 - -1));
721 if (((x
) == (negc
))) {
722 result
= norm (result
, y
);
726 if (((y
) == (negc
))) {
727 result
= norm (result
, x
);
735 if (((negc
) == ((0 + (3 - -1)))))
736 negc
= negative_carry (dx
< dy
);
739 negc
= negative_carry (dx
<= dy
);
745 result
= make_integer (dx
, result
);
751 int16
invert (int16 x
) {
752 if (((x
) == ((0 + (3 - -1)))))
753 return ((0 + (3 - -1))-1);
755 return (0 + (3 - -1));
758 int16
sub (int16 x
, int16 y
) {
760 int16 negc
= ((0 + (3 - -1))-1);
766 if (((x
) == (negc
)) && (((y
) == ((0 + (3 - -1)))) || ((y
) == (((0 + (3 - -1))-1))))) {
767 result
= norm (result
, invert (y
));
771 if (((y
) == (invert (negc
)))) {
772 result
= norm (result
, x
);
777 dy
= ~integer_lo (y
);
780 if (((negc
) == ((0 + (3 - -1)))))
781 negc
= negative_carry (dx
< dy
);
784 negc
= negative_carry (dx
<= dy
);
790 result
= make_integer (dx
, result
);
796 int16
neg (int16 x
) {
799 return sub ((0 + (3 - -1)), x
);
802 int16
scale (int16 n
, int16 x
) {
809 if ((n
== 0) || ((x
) == ((0 + (3 - -1)))))
810 return (0 + (3 - -1));
819 if (((x
) == ((0 + (3 - -1))))){
821 result
= norm (result
, (carry
+ (3 - -1)));
823 result
= norm (result
, make_integer (carry
, (0 + (3 - -1))));
827 if (((x
) == (((0 + (3 - -1))-1)))) {
830 if (carry
>= ((tmp
<<16) + -1))
831 result
= norm (result
, ((carry
& #xff) + (3 - -1)));
833 result
= norm (result
, make_integer (carry
, ((0 + (3 - -1))-1)));
837 int32 tmp1
= integer_lo (x
);
838 m
= tmp1
* n
+ carry
;
843 result
= make_integer (tmp2
, result
);
849 int16
mulnonneg (int16 x
, int16 y
) {
854 int16 s
= scale (integer_lo (x
), y
);
857 result
= make_integer (integer_lo (s
), result
);
861 if (((x
) == ((0 + (3 - -1)))))
864 s
= add (s
, scale (integer_lo (x
), y
));
867 return norm (result
, s
);
871 int16
divnonneg (int16 x
, int16 y
) {
875 int16 result
= (0 + (3 - -1));
876 int16 lx
= integer_length (x
);
877 int16 ly
= integer_length (y
);
882 y
= shift_left (y
, lx
);
885 result
= shl (result
);
886 if (cmp (x
, y
) >= 1) { // TODO cmp changed
888 result
= add (((0 + (3 - -1))+1), result
);
897 int16
bitwise_ior (int16 x
, int16 y
) {
903 if (((x
) == ((0 + (3 - -1)))))
904 return norm(result
, y
);
905 if (((x
) == (((0 + (3 - -1))-1))))
906 return norm(result
, x
);
907 result
= make_integer(integer_lo(x
) | integer_lo(y
),
914 int16
bitwise_xor (int16 x
, int16 y
) {
920 if (((x
) == ((0 + (3 - -1)))))
921 return norm(result
, y
);
922 if (((x
) == (((0 + (3 - -1))-1))))
923 return norm(result
, x
);
924 result
= make_integer(integer_lo(x
) ^ integer_lo(y
),
933 int16
encode_int (int32 n
) {
934 if (n
>= -1 && n
<= 255) {
936 return (tmp
+ (3 - -1));
939 return alloc_ram_cell_init (0, (0 + (3 - -1)), n
>> 8, n
);
941 void decode_2_int_args () {
942 a1
= decode_int (arg1
);
943 a2
= decode_int (arg2
);
950 void prim_numberp () {
952 && arg1
<= (3 + (255 - -1)))
955 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))){
956 arg1
= (ram_get_field0 (arg1
) & #xc0) == 0;
958 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
959 arg1
= (rom_get_field0 (arg1
) & #xc0) == 0;
966 arg1
= add (arg1
, arg2
);
976 arg1
= sub (arg1
, arg2
);
988 arg1
= mulnonneg (a1
? neg(arg1
) : arg1
,
989 a2
? neg(arg2
) : arg2
);
1001 if (((arg2
) == (((0) + (3 - -1)))))
1005 arg1
= divnonneg (a1
? neg(arg1
) : arg1
,
1006 a2
? neg(arg2
) : arg2
);
1020 if (((arg2
) == (((0) + (3 - -1)))))
1022 if (negp(arg1
) || negp(arg2
))
1026 arg3
= divnonneg (arg1
, arg2
);
1027 arg4
= mulnonneg (arg2
, arg3
);
1028 arg1
= sub(arg1
, arg4
);
1051 arg1
= ((cmp (arg1
, arg2
) == 1)); // TODO cmp changed
1061 arg1
= ((cmp (arg1
, arg2
) < 1)); // TODO cmp changed
1071 arg1
= ((cmp (arg1
, arg2
) > 1)); // TODO cmp changed
1081 arg1
= ((cmp (arg1
, arg2
) <= 1)); // TODO cmp changed
1092 arg1
= ((cmp (arg1
, arg2
) >= 1)); // TODO cmp changed
1102 arg1
= bitwise_ior(arg1
, arg2
);
1112 arg1
= bitwise_xor(arg1
, arg2
);
1126 void prim_pairp () {
1127 if ((!((arg1
) >= 1280) && ((arg1
) >= 512)))
1128 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0))));
1129 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1130 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == 0))));
1135 int16
cons (int16 car
, int16 cdr
) {
1136 return alloc_ram_cell_init (#x80 | (car >> 8),
1143 arg1
= cons (arg1
, arg2
);
1148 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1149 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1151 arg1
= ram_get_car (arg1
);
1153 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1154 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == 0)))
1156 arg1
= rom_get_car (arg1
);
1163 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1164 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1166 arg1
= ram_get_cdr (arg1
);
1168 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1169 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == 0)))
1171 arg1
= rom_get_cdr (arg1
);
1177 void prim_set_car () {
1178 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1179 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1182 ram_set_car (arg1
, arg2
);
1190 void prim_set_cdr () {
1191 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1192 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == 0)))
1195 ram_set_cdr (arg1
, arg2
);
1203 void prim_nullp () {
1204 arg1
= ((arg1
== 2));
1211 void prim_u8vectorp () {
1212 if ((!((arg1
) >= 1280) && ((arg1
) >= 512)))
1213 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60))));
1214 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1215 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60))));
1220 void prim_make_u8vector () {
1221 decode_2_int_args ();
1226 arg3
= alloc_vec_cell (a1
);
1227 arg1
= alloc_ram_cell_init (#x80 | (a1 >> 8),
1228 a1
& #xff, #x60 | (arg3 >> 8),
1233 ram_set_field0 (arg3
, a2
);
1234 ram_set_field1 (arg3
, a2
);
1235 ram_set_field2 (arg3
, a2
);
1236 ram_set_field3 (arg3
, a2
);
1241 void prim_u8vector_ref () {
1242 a2
= decode_int (arg2
);
1244 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1245 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1247 if ((ram_get_car (arg1
) <= a2
) || (a2
< 0))
1249 arg1
= ram_get_cdr (arg1
);
1251 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1252 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60)))
1254 if ((rom_get_car (arg1
) <= a2
) || (a2
< 0))
1256 arg1
= rom_get_cdr (arg1
);
1261 if (((arg1
) >= 1280)) {
1265 arg1
= encode_int (ram_get_fieldn (arg1
, a2
));
1269 arg1
= rom_get_cdr (arg1
);
1272 arg1
= rom_get_car (arg1
);
1280 void prim_u8vector_set () {
1281 a2
= decode_int (arg2
);
1282 a3
= decode_int (arg3
);
1287 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1288 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1290 if ((ram_get_car (arg1
) <= a2
) || (a2
< 0))
1292 arg1
= ram_get_cdr (arg1
);
1300 ram_set_fieldn (arg1
, a2
, a3
);
1307 void prim_u8vector_length () {
1308 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1309 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1311 arg1
= encode_int (ram_get_car (arg1
));
1313 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1314 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60)))
1316 arg1
= encode_int (rom_get_car (arg1
));
1322 void prim_u8vector_copy () {
1326 a1
= decode_int (arg2
);
1327 a2
= decode_int (arg4
);
1328 a3
= decode_int (arg5
);
1331 if ((!((arg1
) >= 1280) && ((arg1
) >= 512)) && (!((arg3
) >= 1280) && ((arg3
) >= 512))) {
1332 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)) || !(((ram_get_field0 (arg3) & #x80) == #x80) && ((ram_get_field2 (arg3) & #xe0) == #x60)))
1334 if ((ram_get_car (arg1
) < (a1
+ a3
)) || (a1
< 0) ||
1335 (ram_get_car (arg3
) < (a2
+ a3
)) || (a2
< 0))
1339 arg1
= ram_get_cdr (arg1
);
1342 arg3
= ram_get_cdr (arg3
);
1348 ram_set_fieldn (arg3
, a2
, ram_get_fieldn (arg1
, a1
));
1359 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))) && (!((arg3
) >= 1280) && ((arg3
) >= 512))) {
1360 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x60)) || !(((ram_get_field0 (arg3) & #x80) == #x80) && ((ram_get_field2 (arg3) & #xe0) == #x60)))
1362 if ((rom_get_car (arg1
) < (a1
+ a3
)) || (a1
< 0) ||
1363 (ram_get_car (arg3
) < (a2
+ a3
)) || (a2
< 0))
1366 arg1
= rom_get_cdr (arg1
);
1368 arg1
= rom_get_cdr (arg1
);
1370 arg3
= ram_get_cdr (arg3
);
1375 ram_set_fieldn (arg3
, a2
, decode_int (rom_get_car (arg1
)));
1377 arg1
= rom_get_cdr (arg1
);
1398 arg1
= ((arg1
== arg2
));
1403 arg1
= ((arg1
== 0));
1406 void prim_symbolp () {
1407 if ((!((arg1
) >= 1280) && ((arg1
) >= 512)))
1408 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x20))));
1409 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1410 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x20))));
1415 void prim_stringp () {
1416 if ((!((arg1
) >= 1280) && ((arg1
) >= 512)))
1417 arg1
= (((((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x40))));
1418 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1))))
1419 arg1
= (((((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x40))));
1424 void prim_string2list () {
1425 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1426 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x40)))
1429 arg1
= ram_get_car (arg1
);
1431 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1432 if (!(((rom_get_field0 (arg1
) & #x80) == #x80) && ((rom_get_field2 (arg1) & #xe0) == #x40)))
1435 arg1
= rom_get_car (arg1
);
1441 void prim_list2string () {
1442 arg1
= alloc_ram_cell_init (#x80 | ((arg1 & #x1f00) >> 8),
1447 void prim_booleanp () {
1448 arg1
= ((arg1
< 2));
1455 void prim_print () {
1463 int32
read_clock () {
1467 /* now = from_now( 0 ); */
1471 void prim_clock () {
1472 arg1
= encode_int (read_clock ());
1475 void prim_motor () {
1476 decode_2_int_args ();
1478 if (a1
< 1 || a1
> 2 || a2
< -100 || a2
> 100)
1482 /* MOTOR_set( a1, a2 ); */
1496 decode_2_int_args ();
1497 a3
= decode_int (arg3
);
1499 if (a1
< 1 || a1
> 3 || a2
< 0 || a3
< 0)
1503 /* LED_set( a1, a2, a3 ); */
1517 void prim_led2_color () {
1518 a1
= decode_int (arg1
);
1520 if (a1
< 0 || a1
> 1)
1524 /* LED2_color_set( a1 ); */
1536 void prim_getchar_wait () {
1537 decode_2_int_args();
1538 a1
= read_clock () + a1
;
1540 if (a1
< 0 || a2
< 1 || a2
> 3)
1546 /* serial_port_set ports; */
1547 /* ports = serial_rx_wait_with_timeout( a2, a1 ); */
1548 /* if (ports != 0) */
1549 /* arg1 = encode_int (serial_rx_read( ports )); */
1554 void prim_putchar () {
1555 decode_2_int_args ();
1557 if (a1
< 0 || a1
> 255 || a2
< 1 || a2
> 3)
1561 /* serial_tx_write( a2, a1 ); */
1575 decode_2_int_args ();
1577 if (a1
< 1 || a1
> 255 || a2
< 0)
1581 /* beep( a1, from_now( a2 ) ); */
1597 a1
= decode_int (arg1
);
1599 if (a1
< 1 || a1
> 3)
1603 /* x = adc( a1 ); */
1604 arg1
= encode_int (x
);
1607 void prim_sernum () {
1611 /* x = serial_num (); */
1618 arg1
= encode_int (x
);
1625 void prim_network_init () {
1633 void prim_network_cleanup () {
1639 void prim_receive_packet_to_u8vector () {
1641 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1645 void prim_send_packet_from_u8vector () {
1649 if (!(((ram_get_field0 (arg1
) & #x80) == #x80) && ((ram_get_field2 (arg1) & #xe0) == #x60)))
1652 a2
= decode_int (arg2
);
1656 if (ram_get_car (arg1
) < a2
)
1659 arg1
= ram_get_cdr (arg1
);
1663 /* void push_arg1 (); */
1665 /* void pop_procedure (); */
1666 /* void handle_arity_and_rest_param (); */
1667 /* void build_env (); */
1668 /* void save_cont (); */
1669 /* void interpreter (); */
1672 env
= cons (arg1
, env
);
1677 int16 o
= ram_get_car (env
);
1678 env
= ram_get_cdr (env
);
1682 void pop_procedure () {
1685 if ((!((arg1
) >= 1280) && ((arg1
) >= 512))) {
1686 if (!((ram_get_field0 (arg1
) & #xc0) == #x40))
1689 entry
= ram_get_entry (arg1
) + #x8000;
1691 else if ((!((arg1
) >= 1280) && !(!((arg1
) >= 1280) && ((arg1
) >= 512)) && ((arg1
) >= (3 +255 - -1 +1)))) {
1692 if (!((rom_get_field0 (arg1
) & #xc0) == #x40))
1695 entry
= rom_get_entry (arg1
) + #x8000;
1701 void handle_arity_and_rest_param () {
1704 np
= rom_get (entry
++);
1706 if ((np
& #x80) == 0) {
1721 arg3
= cons (arg4
, arg3
);
1727 arg1
= cons (arg3
, arg1
);
1736 arg1
= cons (arg3
, arg1
);
1746 arg3
= alloc_ram_cell_init (#x40 | (pc >> 11),
1748 ((pc
& #x0007) << 5) | (env >> 8),
1750 cont
= alloc_ram_cell_init (#x80 | (cont >> 8),
1751 cont
& #xff, #x80 | (arg3 >> 8),
1756 void init_ram_heap () {
1762 int16 tmp
= (512 + ((glovars
+ 1) >> 1)); // TODO optimization TODO parens added to solve a potential shift priority problem
1766 ram_set_gc_tags (o
, (0<<5));
1767 ram_set_car (o
, free_list
);
1772 free_list_vec
= 1280;
1773 ram_set_car (free_list_vec
, 0);
1777 ram_set_cdr (free_list_vec
, ((2047 - 1280 + 1)*4) >> 2);
1779 for (i
=0; i
<glovars
; i
++)
1791 void interpreter () {
1792 int16 tmp
= rom_get (#x8000 +2);
1793 pc
= (#x8000 + 4) + (tmp << 2);
1795 glovars
= rom_get (#x8000 +3);
1800 bytecode
= rom_get (pc
++);
1801 bytecode_hi4
= bytecode
& #xf0;
1802 bytecode_lo4
= bytecode
& #x0f;
1803 switch (bytecode_hi4
>> 4) {;
1806 case 0: // TODO used to be #x00 >> 4
1810 arg1
= bytecode_lo4
;
1820 arg1
= bytecode_lo4
+16;
1833 while (bytecode_lo4
!= 0) {
1834 arg1
= ram_get_cdr (arg1
);
1838 arg1
= ram_get_car (arg1
);
1853 while (bytecode_lo4
!= 0) {
1854 arg1
= ram_get_cdr (arg1
);
1858 arg1
= ram_get_car (arg1
);
1869 arg1
= get_global (bytecode_lo4
);
1880 set_global (bytecode_lo4
, pop());
1892 handle_arity_and_rest_param ();
1911 handle_arity_and_rest_param ();
1924 switch (bytecode_lo4
) {
1926 bytecode
= rom_get (pc
++);
1929 bytecode
= rom_get (pc
++);
1934 entry
= (arg2
<< 8) + bytecode
+ #x8000;
1937 na
= rom_get (entry
++);
1951 bytecode
= rom_get (pc
++);
1954 bytecode
= rom_get (pc
++);
1959 entry
= (arg2
<< 8) + bytecode
+ #x8000;
1962 na
= rom_get (entry
++);
1975 bytecode
= rom_get (pc
++);
1978 bytecode
= rom_get (pc
++);
1983 pc
= (arg2
<< 8) + bytecode
+ #x8000;
1988 bytecode
= rom_get (pc
++);
1991 bytecode
= rom_get (pc
++);
1997 pc
= (arg2
<< 8) + bytecode
+ #x8000;
2002 bytecode
= rom_get (pc
++);
2005 bytecode
= rom_get (pc
++);
2011 entry
= (arg2
<< 8) | bytecode
;
2013 arg1
= alloc_ram_cell_init (#x40 | (arg2 >> 3),
2014 ((arg2
& #x07) << 5) | (bytecode >> 3),
2015 ((bytecode
) << 5) |((arg3 ἀ) >>8),
2025 /* case 5: */ // TODO useless, they don't work in the regular PICOBIT
2026 /* bytecode = rom_get (pc++); */
2031 /* entry = pc + bytecode + #x8000; */
2034 /* na = rom_get (entry++); */
2047 /* bytecode = rom_get (pc++); */
2052 /* entry = pc + bytecode + #x8000; */
2055 /* na = rom_get (entry++); */
2067 /* bytecode = rom_get (pc++); */
2071 /* pc = pc + bytecode + #x8000; */
2076 /* bytecode = rom_get (pc++); */
2081 /* if (pop() == 0) */
2082 /* pc = pc + bytecode + #x8000; */
2087 /* bytecode = rom_get (pc++); */
2093 /* entry = pc + bytecode; */
2095 /* arg1 = alloc_ram_cell_init (#x40 | (arg2 >> 3), */
2096 /* ((arg2 & #x07) << 5) | (bytecode >> 3), */
2097 /* ((bytecode ) <<5) |((arg3 ἀ) >>8), */
2106 bytecode
= rom_get (pc
++);
2110 arg1
= get_global (bytecode
);
2117 bytecode
= rom_get (pc
++);
2121 set_global (bytecode
, pop());
2133 bytecode
= rom_get (pc
++);
2137 arg1
= (bytecode_lo4
<< 8) | bytecode
;
2157 switch (bytecode_lo4
) {
2159 arg1
= pop(); prim_numberp (); push_arg1 (); break;
2161 arg2
= pop(); arg1
= pop(); prim_add (); push_arg1 (); break;
2163 arg2
= pop(); arg1
= pop(); prim_sub (); push_arg1 (); break;
2165 arg2
= pop(); arg1
= pop(); prim_mul (); push_arg1 (); break;
2167 arg2
= pop(); arg1
= pop(); prim_div (); push_arg1 (); break;
2169 arg2
= pop(); arg1
= pop(); prim_rem (); push_arg1 (); break;
2171 arg1
= pop(); prim_neg (); push_arg1 (); break;
2173 arg2
= pop(); arg1
= pop(); prim_eq (); push_arg1 (); break;
2175 arg2
= pop(); arg1
= pop(); prim_lt (); push_arg1 (); break;
2177 arg2
= pop(); arg1
= pop(); prim_leq (); push_arg1 (); break;
2179 arg2
= pop(); arg1
= pop(); prim_gt (); push_arg1 (); break;
2181 arg2
= pop(); arg1
= pop(); prim_geq (); push_arg1 (); break;
2183 arg1
= pop(); prim_pairp (); push_arg1 (); break;
2185 arg2
= pop(); arg1
= pop(); prim_cons (); push_arg1 (); break;
2187 arg1
= pop(); prim_car (); push_arg1 (); break;
2189 arg1
= pop(); prim_cdr (); push_arg1 (); break;
2199 switch (bytecode_lo4
) {
2201 arg2
= pop(); arg1
= pop(); prim_set_car (); break;
2203 arg2
= pop(); arg1
= pop(); prim_set_cdr (); break;
2205 arg1
= pop(); prim_nullp (); push_arg1 (); break;
2207 arg2
= pop(); arg1
= pop(); prim_eqp (); push_arg1 (); break;
2209 arg1
= pop(); prim_not (); push_arg1 (); break;
2226 handle_arity_and_rest_param ();
2241 arg2
= ram_get_cdr (cont
);
2243 pc
= ram_get_entry (arg2
);
2245 env
= ram_get_cdr (arg2
);
2246 cont
= ram_get_car (cont
);
2257 arg1
= pop(); prim_symbolp (); push_arg1 (); break;
2260 arg1
= pop(); prim_stringp (); push_arg1 (); break;
2263 arg1
= pop(); prim_string2list (); push_arg1 (); break;
2266 arg1
= pop(); prim_list2string (); push_arg1 (); break;
2269 arg2
= pop(); arg1
= pop(); prim_make_u8vector (); push_arg1 (); break;
2272 arg2
= pop(); arg1
= pop(); prim_u8vector_ref (); push_arg1 (); break;
2275 arg3
= pop(); arg2
= pop(); arg1
= pop(); prim_u8vector_set (); break;
2285 switch (bytecode_lo4
) {
2293 prim_clock (); push_arg1 (); break;
2296 arg2
= pop(); arg1
= pop(); prim_motor (); break;
2299 arg3
= pop(); arg2
= pop(); arg1
= pop(); prim_led (); ;break;
2302 arg1
= pop(); prim_led2_color (); break;
2305 arg2
= pop(); arg1
= pop(); prim_getchar_wait (); push_arg1 (); break;
2308 arg2
= pop(); arg1
= pop(); prim_putchar (); break;
2311 arg2
= pop(); arg1
= pop(); prim_beep (); break;
2314 arg1
= pop(); prim_adc (); push_arg1 (); break;
2317 arg1
= pop(); prim_u8vectorp (); push_arg1 (); break;
2320 prim_sernum (); push_arg1 (); break;
2323 arg1
= pop(); prim_u8vector_length (); push_arg1 (); break;
2326 arg5
= pop(); arg4
= pop(); arg3
= pop(); arg2
= pop(); arg1
= pop();
2327 prim_u8vector_copy (); break;
2342 arg2
= ram_get_cdr (cont
);
2343 pc
= ram_get_entry (arg2
);
2344 env
= ram_get_cdr (arg2
);
2345 cont
= ram_get_car (cont
);
2359 switch (bytecode_lo4
) {
2362 arg1
= pop(); prim_booleanp (); push_arg1 (); break;
2365 prim_network_init (); break;
2368 prim_network_cleanup (); break;
2371 arg1
= pop(); prim_receive_packet_to_u8vector (); push_arg1 (); break;
2374 arg2
= pop(); arg1
= pop(); prim_send_packet_from_u8vector ();
2375 push_arg1 (); break;
2377 arg2
= pop(); arg1
= pop(); prim_ior (); push_arg1 (); break;
2380 arg2
= pop(); arg1
= pop(); prim_xor (); push_arg1 (); break;