996
|
1 /*
|
|
2 * pick2.h - Runtime library for INTERCAL, for use by PIC C compilers
|
|
3 *
|
|
4 * Some of the code here was originally in fiddle.c, some was originally
|
|
5 * in cesspool.c. The rest is a translation into C of syslib.i.
|
|
6 *
|
|
7 LICENSE TERMS
|
|
8 Copyright (C) 1996 Eric S. Raymond
|
|
9 Copyright (C) 2006 Alex Smith
|
|
10
|
|
11 This program is free software; you can redistribute it and/or modify
|
|
12 it under the terms of the GNU General Public License as published by
|
|
13 the Free Software Foundation; either version 2 of the License, or
|
|
14 (at your option) any later version.
|
|
15
|
|
16 This program is distributed in the hope that it will be useful,
|
|
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 GNU General Public License for more details.
|
|
20
|
|
21 You should have received a copy of the GNU General Public License
|
|
22 along with this program; if not, write to the Free Software
|
|
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 */
|
|
25
|
|
26 #define ick_resume ick_popnext
|
|
27
|
|
28 ICK_INT32 ick_mingle(ICK_INT16 r, ICK_INT16 s)
|
|
29 {
|
|
30 r = ((r & 0x0000ff00) << 8) | (r & 0x000000ff);
|
|
31 r = ((r & 0x00f000f0) << 4) | (r & 0x000f000f);
|
|
32 r = ((r & 0x0c0c0c0c) << 2) | (r & 0x03030303);
|
|
33 r = ((r & 0x22222222) << 1) | (r & 0x11111111);
|
|
34 s = ((s & 0x0000ff00) << 8) | (s & 0x000000ff);
|
|
35 s = ((s & 0x00f000f0) << 4) | (s & 0x000f000f);
|
|
36 s = ((s & 0x0c0c0c0c) << 2) | (s & 0x03030303);
|
|
37 s = ((s & 0x22222222) << 1) | (s & 0x11111111);
|
|
38 return (r << 1) | s;
|
|
39 }
|
|
40
|
|
41 ICK_INT32 ick_iselect(ICK_INT32 r, ICK_INT32 s)
|
|
42 {
|
|
43 ICK_INT32 i = 1, t = 0;
|
|
44 while (s) {
|
|
45 if (s & i) {
|
|
46 t |= r & i;
|
|
47 s ^= i;
|
|
48 i <<= 1;
|
|
49 }
|
|
50 else {
|
|
51 s >>= 1;
|
|
52 r >>= 1;
|
|
53 }
|
|
54 }
|
|
55 return t;
|
|
56 }
|
|
57
|
|
58 ICK_INT16 ick_and16(ICK_INT16 n)
|
|
59 {
|
|
60 ICK_INT16 m;
|
|
61 m = (n >> 1);
|
|
62 if (n & 1)
|
|
63 m |= 0x8000;
|
|
64 return(m & n);
|
|
65 }
|
|
66
|
|
67 ICK_INT16 ick_or16(ICK_INT16 n)
|
|
68 {
|
|
69 ICK_INT16 m;
|
|
70 m = (n >> 1);
|
|
71 if (n & 1)
|
|
72 m |= 0x8000;
|
|
73 return(m | n);
|
|
74 }
|
|
75
|
|
76 ICK_INT32 ick_and32(ICK_INT32 n)
|
|
77 {
|
|
78 ICK_INT32 m;
|
|
79 m = (n >> 1);
|
|
80 if (n & 1)
|
|
81 m |= 0x80000000;
|
|
82 return(m & n);
|
|
83 }
|
|
84
|
|
85 ICK_INT32 ick_or32(ICK_INT32 n)
|
|
86 {
|
|
87 ICK_INT32 m;
|
|
88 m = (n >> 1);
|
|
89 if (n & 1)
|
|
90 m |= 0x80000000;
|
|
91 return(m | n);
|
|
92 }
|
|
93
|
|
94 ICK_INT16 ick_xor16(ICK_INT16 n)
|
|
95 {
|
|
96 ICK_INT16 m;
|
|
97 m = (n >> 1);
|
|
98 if (n & 1)
|
|
99 m |= 0x8000;
|
|
100 return(m ^ n);
|
|
101 }
|
|
102
|
|
103 ICK_INT32 ick_xor32(ICK_INT32 n)
|
|
104 {
|
|
105 ICK_INT32 m;
|
|
106 m = (n >> 1);
|
|
107 if (n & 1)
|
|
108 m |= 0x80000000;
|
|
109 return(m ^ n);
|
|
110 }
|
|
111
|
|
112 ICK_INT8 ick_nextindex = 0;
|
|
113 ICK_INT16 ick_next[16];
|
|
114
|
|
115 void ick_pushnext(ICK_INT16 n)
|
|
116 {
|
|
117 ick_next[ick_nextindex++] = n;
|
|
118 }
|
|
119
|
|
120 ICK_INT16 ick_popnext(ICK_INT8 n)
|
|
121 {
|
|
122 if (ick_nextindex < n) {
|
|
123 ick_nextindex = 0;
|
|
124 return (ICK_INT16)-1;
|
|
125 }
|
|
126 else
|
|
127 ick_nextindex -= n;
|
|
128 return(ick_next[ick_nextindex]);
|
|
129 }
|
|
130
|
|
131 /* It's hard to do randomization on a PIC. */
|
|
132 ICK_INT1 ick_roll(ICK_INT8 n)
|
|
133 {
|
|
134 return n>50;
|
|
135 }
|
|
136
|
|
137 /* Syslib, implemented in C to save ROM space. */
|
|
138 #if defined(ONESPOT1)&&defined(ONESPOT2)&&defined(TWOSPOT1)
|
|
139 void syslibopt1050()
|
|
140 {
|
|
141 ONESPOT2=0;
|
|
142 if(ONESPOT1) ONESPOT2=TWOSPOT1/ONESPOT1;
|
|
143 }
|
|
144 void syslibopt1520()
|
|
145 {
|
|
146 TWOSPOT1=((ICK_INT32)ONESPOT1<<16)|ONESPOT2;
|
|
147 }
|
|
148 #endif /* .1 .2 :1 */
|
|
149 #if defined(TWOSPOT1)&&defined(TWOSPOT2)&&defined(TWOSPOT3)
|
|
150 void syslibopt1550()
|
|
151 {
|
|
152 TWOSPOT3=0;
|
|
153 if(TWOSPOT2) TWOSPOT3=TWOSPOT1/TWOSPOT2;
|
|
154 }
|
|
155 void syslipopt1500()
|
|
156 {
|
|
157 TWOSPOT3=TWOSPOT2+TWOSPOT1;
|
|
158 }
|
|
159 void syslibopt1510()
|
|
160 {
|
|
161 TWOSPOT3=TWOSPOT1-TWOSPOT2;
|
|
162 }
|
|
163 void syslibopt1530()
|
|
164 {
|
|
165 TWOSPOT3=TWOSPOT1*TWOSPOT2;
|
|
166 }
|
|
167 void syslibopt1540()
|
|
168 {
|
|
169 TWOSPOT3=0;
|
|
170 if(TWOSPOT2) TWOSPOT3=TWOSPOT1/TWOSPOT2;
|
|
171 }
|
|
172 #ifdef TWOSPOT4
|
|
173 void syslibopt1509()
|
|
174 {
|
|
175 TWOSPOT3=TWOSPOT2+TWOSPOT1;
|
|
176 TWOSPOT4=1;
|
|
177 if(TWOSPOT3<TWOSPOT2) TWOSPOT4=2; /* overflow */
|
|
178 }
|
|
179 void syslibopt1539()
|
|
180 {
|
|
181 TWOSPOT4=1;
|
|
182 if(TWOSPOT2&&TWOSPOT1>0xffffffff/TWOSPOT2) TWOSPOT4=2;
|
|
183 TWOSPOT3=TWOSPOT1*TWOSPOT2;
|
|
184 }
|
|
185 #endif /* :4 */
|
|
186 #endif /* :1 :2 :3 */
|
|
187 #if defined(ONESPOT1)&&defined(ONESPOT2)&&defined(ONESPOT3)
|
|
188 void syslipopt1000()
|
|
189 {
|
|
190 ONESPOT3=ONESPOT2+ONESPOT1;
|
|
191 }
|
|
192 void syslibopt1010()
|
|
193 {
|
|
194 ONESPOT3=ONESPOT1-ONESPOT2;
|
|
195 }
|
|
196 void syslibopt1030()
|
|
197 {
|
|
198 ONESPOT3=ONESPOT1*ONESPOT2;
|
|
199 }
|
|
200 void syslibopt1040()
|
|
201 {
|
|
202 ONESPOT3=0;
|
|
203 if(ONESPOT2) ONESPOT3=ONESPOT1/ONESPOT2;
|
|
204 }
|
|
205 #ifdef ONESPOT4
|
|
206 void syslibopt1009()
|
|
207 {
|
|
208 ONESPOT3=ONESPOT2+ONESPOT1;
|
|
209 ONESPOT4=1;
|
|
210 if(ONESPOT3<ONESPOT2) ONESPOT4=2; /* overflow */
|
|
211 }
|
|
212 void syslibopt1039()
|
|
213 {
|
|
214 ONESPOT4=1;
|
|
215 if(ONESPOT2&&ONESPOT1>0xffff/ONESPOT2) ONESPOT4=2;
|
|
216 ONESPOT3=ONESPOT1*ONESPOT2;
|
|
217 }
|
|
218 #endif /* .4 */
|
|
219 #endif /* .1 .2 .3 */
|
|
220 #ifdef ONESPOT1
|
|
221 void syslibopt1020()
|
|
222 {
|
|
223 ONESPOT1++;
|
|
224 }
|
|
225 #endif
|