1 /*
2  * Copyright (c) 2007-2013 Michael Mondy
3  * Copyright (c) 2012-2016 Harry Reed
4  * Copyright (c) 2013-2018 Charles Anthony
5  * Copyright (c) 2021 The DPS8M Development Team
6  *
7  * All rights reserved.
8  *
9  * This software is made available under the terms of the ICU
10  * License, version 1.8.1 or later.  For more details, see the
11  * LICENSE.md file at the top-level directory of this distribution.
12  */
13 
14 // Interface for cfg_parse
15 
16 typedef struct config_value_list_s
17   {
18     const char * value_name;
19     int64_t value;
20   } config_value_list_t;
21 
22 typedef struct config_list_s
23   {
24     const char * name;  // opt name
25     int64_t min, max;   // value limits
26     config_value_list_t * value_list;
27   } config_list_t;
28 
29 typedef struct config_state_s
30   {
31     char * copy;
32     char * statement_save;
33   } config_state_t;
34 
35 int cfg_parse (const char * tag, const char * cptr, config_list_t * clist, config_state_t * state, int64_t * result);
36 void cfg_parse_done (config_state_t * state);
37 
38 struct opcode_s * get_iwb_info (DCDstruct *i);
39 char * dump_flags(char * buffer, word18 flags);
40 char *disassemble(char * result, word36 instruction);
41 char *get_mod_string(char * msg, word6 tag);
42 word72 convert_to_word72 (word36 even, word36 odd);
43 void convert_to_word36 (word72 src, word36 *even, word36 *odd);
44 
45 word36 compl36(word36 op1, word18 *flags, bool * ovf);
46 word18 compl18(word18 op1, word18 *flags, bool * ovf);
47 
48 void copyBytes(int posn, word36 src, word36 *dst);
49 void copyChars(int posn, word36 src, word36 *dst);
50 
51 void putByte(word36 *dst, word9 data, int posn);
52 void putChar(word36 *dst, word6 data, int posn);
53 
54 void cmp36(word36 op1, word36 op2, word18 *flags);
55 void cmp36wl(word36 A, word36 Y, word36 Q, word18 *flags);
56 void cmp18(word18 op1, word18 op2, word18 *flags);
57 void cmp72(word72 op1, word72 op2, word18 *flags);
58 
59 char *strlower(char *q);
60 int strmask(char *str, char *mask);
61 char *Strtok(char *, char *);
62 char *stripquotes(char *s);
63 char *trim(char *s);
64 char *ltrim(char *s);
65 char *rtrim(char *s);
66 
67 //
68 // getbitsNN/setbitsNN/putbitsNN
69 //
70 //   Manipluate bitfields.
71 //     NN      the word size (18, 36, 72).
72 //     data    the incoming word
73 //     i       the starting bit number (DPS8 notation, 0 is the MSB)
74 //     n       the number of bits, starting at i
75 //     val     the bits
76 //
77 //   val = getbitsNN (data, i, n)
78 //
79 //       extract n bits from data, starting at i.
80 //
81 //           val = getbits36 (data, 0, 1) --> the sign bit
82 //           val = getbits36 (data, 18, 18) --> the low eighteen bits
83 //
84 //   newdata = setbitsNN (data, i, n, val)
85 //
86 //       return 'data' with n bits of val inserted.
87 //
88 //           newdata = setbits36 (data, 0, 18, 1) --> move '1' into the high
89 //                                                    18 bits.
90 //
91 //   putbitsNN (& data, i, n, val)
92 //
93 //        put val into data (equivalent to 'data = setbitsNN (data, ....)'
94 //
95 //           putbits (& data, 0, 18, 1) --> set the high 18 bits to '1'.
96 //
97 
98 #if 0
99 static inline word72 getbits72 (word72 x, uint i, uint n)
100   {
101     // bit 71 is right end, bit zero is 72nd from the right
102     int shift = 71 - (int) i - (int) n + 1;
103     if (shift < 0 || shift > 71)
104       {
105         sim_printf ("getbits72: bad args (i=%d,n=%d)\n", i, n);
106         return 0;
107       }
108      else
109       return (x >> (unsigned) shift) & ~ (~0U << n);
110   }
111 #endif
112 
getbits36(word36 x,uint i,uint n)113 static inline word36 getbits36(word36 x, uint i, uint n) {
114     // bit 35 is right end, bit zero is 36th from the right
115     int shift = 35-(int)i-(int)n+1;
116     if (shift < 0 || shift > 35) {
117         sim_printf ("getbits36: bad args (%012"PRIo64",i=%d,n=%d)\n", x, i, n);
118         return 0;
119     } else
120         return (x >> (unsigned) shift) & ~ (~0U << n);
121 }
122 
getbits36_1(word36 x,uint i)123 static inline word1 getbits36_1 (word36 x, uint i)
124   {
125     // bit 35 is right end, bit zero is 36th from the right
126     int shift = 35-(int)i;
127     if (shift < 0 || shift > 35) {
128         sim_printf ("getbits36_1: bad args (%012"PRIo64",i=%d)\n", x, i);
129         return 0;
130     } else
131         return (x >> (unsigned) shift) & 01;
132   }
133 
getbits36_2(word36 x,uint i)134 static inline word2 getbits36_2 (word36 x, uint i)
135   {
136     // bit 35 is right end, bit zero is 36th from the right
137     int shift = 35-(int)i-(int)2+1;
138     if (shift < 0 || shift > 35) {
139         sim_printf ("getbits36_2: bad args (%012"PRIo64",i=%d)\n", x, i);
140         return 0;
141     } else
142         return (x >> (unsigned) shift) & 03;
143   }
144 
getbits36_3(word36 x,uint i)145 static inline word3 getbits36_3 (word36 x, uint i)
146   {
147     // bit 35 is right end, bit zero is 36th from the right
148     int shift = 35-(int)i-(int)3+1;
149     if (shift < 0 || shift > 35) {
150         sim_printf ("getbits36_3: bad args (%012"PRIo64",i=%d)\n", x, i);
151         return 0;
152     } else
153         return (x >> (unsigned) shift) & 07;
154   }
155 
getbits36_4(word36 x,uint i)156 static inline word4 getbits36_4 (word36 x, uint i)
157   {
158     // bit 35 is right end, bit zero is 36th from the right
159     int shift = 35-(int)i-(int)4+1;
160     if (shift < 0 || shift > 35) {
161         sim_printf ("getbits36_4: bad args (%012"PRIo64",i=%d)\n", x, i);
162         return 0;
163     } else
164         return (x >> (unsigned) shift) & 017;
165   }
166 
getbits36_5(word36 x,uint i)167 static inline word5 getbits36_5 (word36 x, uint i)
168   {
169     // bit 35 is right end, bit zero is 36th from the right
170     int shift = 35-(int)i-(int)5+1;
171     if (shift < 0 || shift > 35) {
172         sim_printf ("getbits36_5: bad args (%012"PRIo64",i=%d)\n", x, i);
173         return 0;
174     } else
175         return (x >> (unsigned) shift) & 037;
176   }
177 
getbits36_6(word36 x,uint i)178 static inline word6 getbits36_6 (word36 x, uint i)
179   {
180     // bit 35 is right end, bit zero is 36th from the right
181     int shift = 35-(int)i-(int)6+1;
182     if (shift < 0 || shift > 35) {
183         sim_printf ("getbits36_6: bad args (%012"PRIo64",i=%d)\n", x, i);
184         return 0;
185     } else
186         return (x >> (unsigned) shift) & 077;
187   }
188 
getbits36_7(word36 x,uint i)189 static inline word7 getbits36_7 (word36 x, uint i)
190   {
191     // bit 35 is right end, bit zero is 36th from the right
192     int shift = 35-(int)i-(int)7+1;
193     if (shift < 0 || shift > 35) {
194         sim_printf ("getbits36_7: bad args (%012"PRIo64",i=%d)\n", x, i);
195         return 0;
196     } else
197         return (x >> (unsigned) shift) & 0177;
198   }
199 
getbits36_8(word36 x,uint i)200 static inline word8 getbits36_8 (word36 x, uint i)
201   {
202     // bit 35 is right end, bit zero is 36th from the right
203     int shift = 35-(int)i-(int)8+1;
204     if (shift < 0 || shift > 35) {
205         sim_printf ("getbits36_8: bad args (%012"PRIo64",i=%d)\n", x, i);
206         return 0;
207     } else
208         return (x >> (unsigned) shift) & 0377;
209   }
210 
getbits36_9(word36 x,uint i)211 static inline word9 getbits36_9 (word36 x, uint i)
212   {
213     // bit 35 is right end, bit zero is 36th from the right
214     int shift = 35-(int)i-(int)9+1;
215     if (shift < 0 || shift > 35) {
216         sim_printf ("getbits36_9: bad args (%012"PRIo64",i=%d)\n", x, i);
217         return 0;
218     } else
219         return (x >> (unsigned) shift) & 0777;
220   }
221 
getbits36_10(word36 x,uint i)222 static inline word10 getbits36_10 (word36 x, uint i)
223   {
224     // bit 35 is right end, bit zero is 36th from the right
225     int shift = 35-(int)i-(int)10+1;
226     if (shift < 0 || shift > 35) {
227         sim_printf ("getbits36_10: bad args (%012"PRIo64",i=%d)\n", x, i);
228         return 0;
229     } else
230         return (x >> (unsigned) shift) & 01777;
231   }
232 
getbits36_12(word36 x,uint i)233 static inline word12 getbits36_12 (word36 x, uint i)
234   {
235     // bit 35 is right end, bit zero is 36th from the right
236     int shift = 35-(int)i-(int)12+1;
237     if (shift < 0 || shift > 35) {
238         sim_printf ("getbits36_12: bad args (%012"PRIo64",i=%d)\n", x, i);
239         return 0;
240     } else
241         return (x >> (unsigned) shift) & 07777;
242   }
243 
getbits36_14(word36 x,uint i)244 static inline word14 getbits36_14 (word36 x, uint i)
245   {
246     // bit 35 is right end, bit zero is 36th from the right
247     int shift = 35-(int)i-(int)14+1;
248     if (shift < 0 || shift > 35) {
249         sim_printf ("getbits36_14: bad args (%012"PRIo64",i=%d)\n", x, i);
250         return 0;
251     } else
252         return (x >> (unsigned) shift) & 037777;
253   }
254 
getbits36_15(word36 x,uint i)255 static inline word15 getbits36_15 (word36 x, uint i)
256   {
257     // bit 35 is right end, bit zero is 36th from the right
258     int shift = 35-(int)i-(int)15+1;
259     if (shift < 0 || shift > 35) {
260         sim_printf ("getbits36_15: bad args (%012"PRIo64",i=%d)\n", x, i);
261         return 0;
262     } else
263         return (x >> (unsigned) shift) & 077777;
264   }
265 
getbits36_16(word36 x,uint i)266 static inline word16 getbits36_16 (word36 x, uint i)
267   {
268     // bit 35 is right end, bit zero is 36th from the right
269     int shift = 35-(int)i-(int)16+1;
270     if (shift < 0 || shift > 35) {
271         sim_printf ("getbits36_16: bad args (%012"PRIo64",i=%d)\n", x, i);
272         return 0;
273     } else
274         return (x >> (unsigned) shift) & 0177777;
275   }
276 
getbits36_18(word36 x,uint i)277 static inline word18 getbits36_18 (word36 x, uint i)
278   {
279     // bit 35 is right end, bit zero is 36th from the right
280     int shift = 35-(int)i-(int)18+1;
281     if (shift < 0 || shift > 35) {
282         sim_printf ("getbits36_18: bad args (%012"PRIo64",i=%d)\n", x, i);
283         return 0;
284     } else
285         return (x >> (unsigned) shift) & 0777777;
286   }
287 
getbits36_24(word36 x,uint i)288 static inline word24 getbits36_24 (word36 x, uint i)
289   {
290     // bit 35 is right end, bit zero is 36th from the right
291     int shift = 35-(int)i-(int)24+1;
292     if (shift < 0 || shift > 35) {
293         sim_printf ("getbits36_24: bad args (%012"PRIo64",i=%d)\n", x, i);
294         return 0;
295     } else
296         return (x >> (unsigned) shift) & MASK24;
297   }
298 
getbits36_28(word36 x,uint i)299 static inline word28 getbits36_28 (word36 x, uint i)
300   {
301     // bit 35 is right end, bit zero is 36th from the right
302     int shift = 35-(int)i-(int)28+1;
303     if (shift < 0 || shift > 35) {
304         sim_printf ("getbits36_28: bad args (%012"PRIo64",i=%d)\n", x, i);
305         return 0;
306     } else
307         return (x >> (unsigned) shift) & 01777777777;
308   }
309 
setbits36(word36 x,uint p,uint n,word36 val)310 static inline word36 setbits36(word36 x, uint p, uint n, word36 val)
311 {
312     int shift = 36 - (int) p - (int) n;
313     if (shift < 0 || shift > 35) {
314         sim_printf ("setbits36: bad args (%012"PRIo64",pos=%d,n=%d)\n", x, p, n);
315         return 0;
316     }
317     word36 mask = ~ (~0U<<n);  // n low bits on
318     mask <<= (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
319     // caller may provide val that is too big, e.g., a word with all bits
320     // set to one, so we mask val
321     word36 result = (x & ~ mask) | ((val&MASKBITS(n)) << (36 - p - n));
322     return result;
323 }
324 
setbits36_1(word36 x,uint p,word1 val)325 static inline word36 setbits36_1 (word36 x, uint p, word1 val)
326 {
327     const int n = 1;
328     int shift = 36 - (int) p - (int) n;
329     if (shift < 0 || shift > 35) {
330         sim_printf ("setbits36_1: bad args (%012"PRIo64",pos=%d)\n", x, p);
331         return 0;
332     }
333     word36 mask = ~ (~0U<<n);  // n low bits on
334     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
335     // caller may provide val that is too big, e.g., a word with all bits
336     // set to one, so we mask val
337     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
338     return result;
339 }
340 
setbits36_4(word36 x,uint p,word4 val)341 static inline word36 setbits36_4 (word36 x, uint p, word4 val)
342 {
343     const int n = 4;
344     int shift = 36 - (int) p - (int) n;
345     if (shift < 0 || shift > 35) {
346         sim_printf ("setbits36_4: bad args (%012"PRIo64",pos=%d)\n", x, p);
347         return 0;
348     }
349     word36 mask = ~ (~0U<<n);  // n low bits on
350     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
351     // caller may provide val that is too big, e.g., a word with all bits
352     // set to one, so we mask val
353     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
354     return result;
355 }
356 
setbits36_5(word36 x,uint p,word5 val)357 static inline word36 setbits36_5 (word36 x, uint p, word5 val)
358 {
359     const int n = 5;
360     int shift = 36 - (int) p - (int) n;
361     if (shift < 0 || shift > 35) {
362         sim_printf ("setbits36_5: bad args (%012"PRIo64",pos=%d)\n", x, p);
363         return 0;
364     }
365     word36 mask = ~ (~0U<<n);  // n low bits on
366     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
367     // caller may provide val that is too big, e.g., a word with all bits
368     // set to one, so we mask val
369     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
370     return result;
371 }
372 
setbits36_6(word36 x,uint p,word6 val)373 static inline word36 setbits36_6 (word36 x, uint p, word6 val)
374 {
375     const int n = 6;
376     int shift = 36 - (int) p - (int) n;
377     if (shift < 0 || shift > 35) {
378         sim_printf ("setbits36_6: bad args (%012"PRIo64",pos=%d)\n", x, p);
379         return 0;
380     }
381     word36 mask = ~ (~0U<<n);  // n low bits on
382     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
383     // caller may provide val that is too big, e.g., a word with all bits
384     // set to one, so we mask val
385     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
386     return result;
387 }
388 
setbits36_8(word36 x,uint p,word8 val)389 static inline word36 setbits36_8 (word36 x, uint p, word8 val)
390 {
391     const int n = 8;
392     int shift = 36 - (int) p - (int) n;
393     if (shift < 0 || shift > 35) {
394         sim_printf ("setbits36_8: bad args (%012"PRIo64",pos=%d)\n", x, p);
395         return 0;
396     }
397     word36 mask = ~ (~0U<<n);  // n low bits on
398     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
399     // caller may provide val that is too big, e.g., a word with all bits
400     // set to one, so we mask val
401     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
402     return result;
403 }
404 
setbits36_9(word36 x,uint p,word9 val)405 static inline word36 setbits36_9 (word36 x, uint p, word9 val)
406 {
407     const int n = 9;
408     int shift = 36 - (int) p - (int) n;
409     if (shift < 0 || shift > 35) {
410         sim_printf ("setbits36_9: bad args (%012"PRIo64",pos=%d)\n", x, p);
411         return 0;
412     }
413     word36 mask = ~ (~0U<<n);  // n low bits on
414     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
415     // caller may provide val that is too big, e.g., a word with all bits
416     // set to one, so we mask val
417     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
418     return result;
419 }
420 
setbits36_16(word36 x,uint p,word16 val)421 static inline word36 setbits36_16 (word36 x, uint p, word16 val)
422 {
423     const int n = 16;
424     int shift = 36 - (int) p - (int) n;
425     if (shift < 0 || shift > 35) {
426         sim_printf ("setbits36_16: bad args (%012"PRIo64",pos=%d)\n", x, p);
427         return 0;
428     }
429     word36 mask = ~ (~0U<<n);  // n low bits on
430     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
431     // caller may provide val that is too big, e.g., a word with all bits
432     // set to one, so we mask val
433     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
434     return result;
435 }
436 
setbits36_24(word36 x,uint p,word24 val)437 static inline word36 setbits36_24 (word36 x, uint p, word24 val)
438 {
439     const int n = 24;
440     int shift = 36 - (int) p - (int) n;
441     if (shift < 0 || shift > 35) {
442         sim_printf ("setbits36_24: bad args (%012"PRIo64",pos=%d)\n", x, p);
443         return 0;
444     }
445     word36 mask = ~ (~0U<<n);  // n low bits on
446     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
447     // caller may provide val that is too big, e.g., a word with all bits
448     // set to one, so we mask val
449     word36 result = (x & ~ smask) | (((word36) val & mask) << shift);
450     return result;
451 }
452 
putbits36(word36 * x,uint p,uint n,word36 val)453 static inline void putbits36 (word36 * x, uint p, uint n, word36 val)
454   {
455     int shift = 36 - (int) p - (int) n;
456     if (shift < 0 || shift > 35)
457       {
458         sim_printf ("putbits36: bad args (%012"PRIo64",pos=%d,n=%d)\n", * x, p, n);
459         return;
460       }
461     word36 mask = ~ (~0U << n);  // n low bits on
462     mask <<= (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
463     // caller may provide val that is too big, e.g., a word with all bits
464     // set to one, so we mask val
465     * x = (* x & ~mask) | ((val & MASKBITS (n)) << (36 - p - n));
466     return;
467   }
468 
putbits36_1(word36 * x,uint p,word1 val)469 static inline void putbits36_1 (word36 * x, uint p, word1 val)
470 {
471     const int n = 1;
472     int shift = 36 - (int) p - (int) n;
473     if (shift < 0 || shift > 35) {
474         sim_printf ("putbits36_1: bad args (%012"PRIo64",pos=%d)\n", *x, p);
475         return;
476     }
477     word36 mask = ~ (~0U<<n);  // n low bits on
478     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
479     // caller may provide val that is too big, e.g., a word with all bits
480     // set to one, so we mask val
481     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
482 }
483 
putbits36_2(word36 * x,uint p,word2 val)484 static inline void putbits36_2 (word36 * x, uint p, word2 val)
485 {
486     const int n = 2;
487     int shift = 36 - (int) p - (int) n;
488     if (shift < 0 || shift > 35) {
489         sim_printf ("putbits36_2: bad args (%012"PRIo64",pos=%d)\n", *x, p);
490         return;
491     }
492     word36 mask = ~ (~0U<<n);  // n low bits on
493     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
494     // caller may provide val that is too big, e.g., a word with all bits
495     // set to one, so we mask val
496     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
497 }
498 
putbits36_3(word36 * x,uint p,word3 val)499 static inline void putbits36_3 (word36 * x, uint p, word3 val)
500 {
501     const int n = 3;
502     int shift = 36 - (int) p - (int) n;
503     if (shift < 0 || shift > 35) {
504         sim_printf ("putbits36_3: bad args (%012"PRIo64",pos=%d)\n", *x, p);
505         return;
506     }
507     word36 mask = ~ (~0U<<n);  // n low bits on
508     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
509     // caller may provide val that is too big, e.g., a word with all bits
510     // set to one, so we mask val
511     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
512 }
513 
putbits36_4(word36 * x,uint p,word4 val)514 static inline void putbits36_4 (word36 * x, uint p, word4 val)
515 {
516     const int n = 4;
517     int shift = 36 - (int) p - (int) n;
518     if (shift < 0 || shift > 35) {
519         sim_printf ("putbits36_4: bad args (%012"PRIo64",pos=%d)\n", *x, p);
520         return;
521     }
522     word36 mask = ~ (~0U<<n);  // n low bits on
523     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
524     // caller may provide val that is too big, e.g., a word with all bits
525     // set to one, so we mask val
526     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
527 }
528 
putbits36_5(word36 * x,uint p,word5 val)529 static inline void putbits36_5 (word36 * x, uint p, word5 val)
530 {
531     const int n = 5;
532     int shift = 36 - (int) p - (int) n;
533     if (shift < 0 || shift > 35) {
534         sim_printf ("putbits36_5: bad args (%012"PRIo64",pos=%d)\n", *x, p);
535         return;
536     }
537     word36 mask = ~ (~0U<<n);  // n low bits on
538     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
539     // caller may provide val that is too big, e.g., a word with all bits
540     // set to one, so we mask val
541     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
542 }
543 
putbits36_6(word36 * x,uint p,word6 val)544 static inline void putbits36_6 (word36 * x, uint p, word6 val)
545 {
546     const int n = 6;
547     int shift = 36 - (int) p - (int) n;
548     if (shift < 0 || shift > 35) {
549         sim_printf ("putbits36_6: bad args (%012"PRIo64",pos=%d)\n", *x, p);
550         return;
551     }
552     word36 mask = ~ (~0U<<n);  // n low bits on
553     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
554     // caller may provide val that is too big, e.g., a word with all bits
555     // set to one, so we mask val
556     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
557 }
558 
putbits36_7(word36 * x,uint p,word7 val)559 static inline void putbits36_7 (word36 * x, uint p, word7 val)
560 {
561     const int n = 7;
562     int shift = 36 - (int) p - (int) n;
563     if (shift < 0 || shift > 35) {
564         sim_printf ("putbits36_7: bad args (%012"PRIo64",pos=%d)\n", *x, p);
565         return;
566     }
567     word36 mask = ~ (~0U<<n);  // n low bits on
568     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
569     // caller may provide val that is too big, e.g., a word with all bits
570     // set to one, so we mask val
571     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
572 }
573 
putbits36_8(word36 * x,uint p,word8 val)574 static inline void putbits36_8 (word36 * x, uint p, word8 val)
575 {
576     const int n = 8;
577     int shift = 36 - (int) p - (int) n;
578     if (shift < 0 || shift > 35) {
579         sim_printf ("putbits36_8: bad args (%012"PRIo64",pos=%d)\n", *x, p);
580         return;
581     }
582     word36 mask = ~ (~0U<<n);  // n low bits on
583     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
584     // caller may provide val that is too big, e.g., a word with all bits
585     // set to one, so we mask val
586     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
587 }
588 
putbits36_9(word36 * x,uint p,word9 val)589 static inline void putbits36_9 (word36 * x, uint p, word9 val)
590 {
591     const int n = 9;
592     int shift = 36 - (int) p - (int) n;
593     if (shift < 0 || shift > 35) {
594         sim_printf ("putbits36_9: bad args (%012"PRIo64",pos=%d)\n", *x, p);
595         return;
596     }
597     word36 mask = ~ (~0U<<n);  // n low bits on
598     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
599     // caller may provide val that is too big, e.g., a word with all bits
600     // set to one, so we mask val
601 #ifndef __OPEN64__
602 # ifdef __GNUC__
603 #  ifndef __clang_version__
604 #   if __GNUC__ > 3
605 #    pragma GCC diagnostic push
606 #    pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
607 #   endif /* if __GNUC__ > 3 */
608 #  endif /* ifndef __clang_version__ */
609 # endif /* ifdef __GNUC__ */
610 #endif /* ifndef __OPEN64__ */
611     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
612 #ifndef __OPEN64__
613 # ifdef __GNUC__
614 #  ifndef __clang_version__
615 #   if __GNUC__ > 3
616 #    pragma GCC diagnostic pop
617 #   endif /* if __GNUC__ > 3 */
618 #  endif /* ifndef __clang_version__ */
619 # endif /* ifdef __GNUC__ */
620 #endif /* ifndef __OPEN64__ */
621 }
622 
putbits36_10(word36 * x,uint p,word10 val)623 static inline void putbits36_10 (word36 * x, uint p, word10 val)
624 {
625     const int n = 10;
626     int shift = 36 - (int) p - (int) n;
627     if (shift < 0 || shift > 35) {
628         sim_printf ("putbits36_10: bad args (%012"PRIo64",pos=%d)\n", *x, p);
629         return;
630     }
631     word36 mask = ~ (~0U<<n);  // n low bits on
632     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
633     // caller may provide val that is too big, e.g., a word with all bits
634     // set to one, so we mask val
635     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
636 }
637 
putbits36_12(word36 * x,uint p,word12 val)638 static inline void putbits36_12 (word36 * x, uint p, word12 val)
639 {
640     const int n = 12;
641     int shift = 36 - (int) p - (int) n;
642     if (shift < 0 || shift > 35) {
643         sim_printf ("putbits36_12: bad args (%012"PRIo64",pos=%d)\n", *x, p);
644         return;
645     }
646     word36 mask = ~ (~0U<<n);  // n low bits on
647     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
648     // caller may provide val that is too big, e.g., a word with all bits
649     // set to one, so we mask val
650     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
651 }
652 
putbits36_13(word36 * x,uint p,word13 val)653 static inline void putbits36_13 (word36 * x, uint p, word13 val)
654 {
655     const int n = 13;
656     int shift = 36 - (int) p - (int) n;
657     if (shift < 0 || shift > 35) {
658         sim_printf ("putbits36_13: bad args (%012"PRIo64",pos=%d)\n", *x, p);
659         return;
660     }
661     word36 mask = ~ (~0U<<n);  // n low bits on
662     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
663     // caller may provide val that is too big, e.g., a word with all bits
664     // set to one, so we mask val
665     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
666 }
667 
putbits36_14(word36 * x,uint p,word14 val)668 static inline void putbits36_14 (word36 * x, uint p, word14 val)
669 {
670     const int n = 14;
671     int shift = 36 - (int) p - (int) n;
672     if (shift < 0 || shift > 35) {
673         sim_printf ("putbits36_14: bad args (%012"PRIo64",pos=%d)\n", *x, p);
674         return;
675     }
676     word36 mask = ~ (~0U<<n);  // n low bits on
677     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
678     // caller may provide val that is too big, e.g., a word with all bits
679     // set to one, so we mask val
680     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
681 }
682 
putbits36_15(word36 * x,uint p,word15 val)683 static inline void putbits36_15 (word36 * x, uint p, word15 val)
684 {
685     const int n = 15;
686     int shift = 36 - (int) p - (int) n;
687     if (shift < 0 || shift > 35) {
688         sim_printf ("putbits36_15: bad args (%012"PRIo64",pos=%d)\n", *x, p);
689         return;
690     }
691     word36 mask = ~ (~0U<<n);  // n low bits on
692     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
693     // caller may provide val that is too big, e.g., a word with all bits
694     // set to one, so we mask val
695     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
696 }
697 
putbits36_16(word36 * x,uint p,word16 val)698 static inline void putbits36_16 (word36 * x, uint p, word16 val)
699 {
700     const int n = 16;
701     int shift = 36 - (int) p - (int) n;
702     if (shift < 0 || shift > 35) {
703         sim_printf ("putbits36_16: bad args (%012"PRIo64",pos=%d)\n", *x, p);
704         return;
705     }
706     word36 mask = ~ (~0U<<n);  // n low bits on
707     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
708     // caller may provide val that is too big, e.g., a word with all bits
709     // set to one, so we mask val
710     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
711 }
712 
putbits36_17(word36 * x,uint p,word17 val)713 static inline void putbits36_17 (word36 * x, uint p, word17 val)
714 {
715     const int n = 17;
716     int shift = 36 - (int) p - (int) n;
717     if (shift < 0 || shift > 35) {
718         sim_printf ("putbits36_17: bad args (%012"PRIo64",pos=%d)\n", *x, p);
719         return;
720     }
721     word36 mask = ~ (~0U<<n);  // n low bits on
722     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
723     // caller may provide val that is too big, e.g., a word with all bits
724     // set to one, so we mask val
725     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
726 }
727 
putbits36_18(word36 * x,uint p,word18 val)728 static inline void putbits36_18 (word36 * x, uint p, word18 val)
729 {
730     const int n = 18;
731     int shift = 36 - (int) p - (int) n;
732     if (shift < 0 || shift > 35) {
733         sim_printf ("putbits36_18: bad args (%012"PRIo64",pos=%d)\n", *x, p);
734         return;
735     }
736     word36 mask = ~ (~0U<<n);  // n low bits on
737     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
738     // caller may provide val that is too big, e.g., a word with all bits
739     // set to one, so we mask val
740     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
741 }
742 
putbits36_23(word36 * x,uint p,word23 val)743 static inline void putbits36_23 (word36 * x, uint p, word23 val)
744 {
745     const int n = 23;
746     int shift = 36 - (int) p - (int) n;
747     if (shift < 0 || shift > 35) {
748         sim_printf ("putbits36_23: bad args (%012"PRIo64",pos=%d)\n", *x, p);
749         return;
750     }
751     word36 mask = ~ (~0U<<n);  // n low bits on
752     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
753     // caller may provide val that is too big, e.g., a word with all bits
754     // set to one, so we mask val
755     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
756 }
757 
putbits36_24(word36 * x,uint p,word24 val)758 static inline void putbits36_24 (word36 * x, uint p, word24 val)
759 {
760     const int n = 24;
761     int shift = 36 - (int) p - (int) n;
762     if (shift < 0 || shift > 35) {
763         sim_printf ("putbits36_24: bad args (%012"PRIo64",pos=%d)\n", *x, p);
764         return;
765     }
766     word36 mask = ~ (~0U<<n);  // n low bits on
767     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
768     // caller may provide val that is too big, e.g., a word with all bits
769     // set to one, so we mask val
770     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
771 }
772 
putbits36_28(word36 * x,uint p,word28 val)773 static inline void putbits36_28 (word36 * x, uint p, word28 val)
774 {
775     const int n = 28;
776     int shift = 36 - (int) p - (int) n;
777     if (shift < 0 || shift > 35) {
778         sim_printf ("putbits36_28: bad args (%012"PRIo64",pos=%d)\n", *x, p);
779         return;
780     }
781     word36 mask = ~ (~0U<<n);  // n low bits on
782     word36 smask = mask << (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
783     // caller may provide val that is too big, e.g., a word with all bits
784     // set to one, so we mask val
785     * x = (* x & ~ smask) | (((word36) val & mask) << shift);
786 }
787 
putbits72(word72 * x,uint p,uint n,word72 val)788 static inline void putbits72 (word72 * x, uint p, uint n, word72 val)
789   {
790     int shift = 72 - (int) p - (int) n;
791     if (shift < 0 || shift > 71)
792       {
793         sim_printf ("putbits72: bad args (pos=%d,n=%d)\n", p, n);
794         return;
795       }
796 #ifdef NEED_128
797 // n low bits on
798     uint64_t lowmask = 0;
799     uint64_t highmask = 0;
800     if (n >= 64)
801       {
802         lowmask = MASK64;
803         highmask = ~ ((~(uint64_t)0) << n);
804         highmask &= 0377U;
805       }
806     else
807       {
808         lowmask = ~ ((~(uint64_t)0) << n);
809       }
810     word72 mask = construct_128 (highmask, lowmask);
811     mask = lshift_128 (mask, (uint) shift);
812     word72 notmask = complement_128 (mask);
813     * x = or_128 (and_128 (* x, notmask), and_128 (lshift_128 (val, 72 - p - n), mask));
814 #else
815     word72 mask = ~ ((~(word72)0) << n);  // n low bits on
816     mask <<= (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
817     // caller may provide val that is too big, e.g., a word with all bits
818     // set to one, so we mask val
819     * x = (* x & ~mask) | ((val & MASKBITS72 (n)) << (72 - p - n));
820 #endif
821     return;
822   }
823 
824 
825 //  getbits18 (data, starting bit, number of bits)
826 
getbits18(word18 x,uint i,uint n)827 static inline word18 getbits18 (word18 x, uint i, uint n)
828   {
829     // bit 17 is right end, bit zero is 18th from the right
830     int shift = 17 - (int) i - (int) n + 1;
831     if (shift < 0 || shift > 17)
832       {
833         sim_printf ("getbits18: bad args (%06o,i=%d,n=%d)\n", x, i, n);
834         return 0;
835       }
836     else
837       return (x >> (unsigned) shift) & ~ (~0U << n);
838   }
839 
840 //  putbits18 (data, starting bit, number of bits, bits)
841 
putbits18(word18 * x,uint p,uint n,word18 val)842 static inline void putbits18 (word18 * x, uint p, uint n, word18 val)
843   {
844     int shift = 18 - (int) p - (int) n;
845     if (shift < 0 || shift > 17)
846       {
847         sim_printf ("putbits18: bad args (%06o,pos=%d,n=%d)\n", * x, p, n);
848         return;
849       }
850     word18 mask = ~ (~0U << n);  // n low bits on
851     mask <<= (unsigned) shift;  // shift 1s to proper position; result 0*1{n}0*
852     // caller may provide val that is too big, e.g., a word with all bits
853     // set to one, so we mask val
854     * x = (* x & ~mask) | ((val & MASKBITS18 (n)) << (18 - p - n));
855     return;
856   }
857 
858 //
859 // setmask -- set bits from mask
860 //
861 
setmask(word36 * v,word36 mask)862 static inline void setmask (word36 * v, word36 mask)
863   {
864     * v |= mask;
865   }
866 
867 //
868 // clrmask -- clear bits from mask
869 //
870 
clrmask(word36 * v,word36 mask)871 static inline void clrmask (word36 * v, word36 mask)
872   {
873     * v &= ~mask;
874   }
875 
876 char * strdupesc (const char * str);
877 word36 extr36 (uint8 * bits, uint woffset);
878 void put36 (word36 val, uint8 * bits, uint woffset);
879 int extractASCII36FromBuffer (uint8 * bufp, t_mtrlnt tbc, uint * words_processed, word36 *wordp);
880 int extractWord36FromBuffer (uint8 * bufp, t_mtrlnt tbc, uint * words_processed, uint64 *wordp);
881 int insertASCII36toBuffer (uint8 * bufp, t_mtrlnt tbc, uint * words_processed, word36 word);
882 int insertWord36toBuffer (uint8 * bufp, t_mtrlnt tbc, uint * words_processed, word36 word);
883 void print_int128 (int128 n, char * p);
884 word36 Add36b (word36 op1, word36 op2, word1 carryin, word18 flagsToSet, word18 * flags, bool * ovf);
885 word36 Sub36b (word36 op1, word36 op2, word1 carryin, word18 flagsToSet, word18 * flags, bool * ovf);
886 word18 Add18b (word18 op1, word18 op2, word1 carryin, word18 flagsToSet, word18 * flags, bool * ovf);
887 word18 Sub18b (word18 op1, word18 op2, word1 carryin, word18 flagsToSet, word18 * flags, bool * ovf);
888 word72 Add72b (word72 op1, word72 op2, word1 carryin, word18 flagsToSet, word18 * flags, bool * ovf);
889 word72 Sub72b (word72 op1, word72 op2, word1 carryin, word18 flagsToSet, word18 * flags, bool * ovf);
890 
891 void timespec_diff(struct timespec *start, struct timespec *stop,
892                    struct timespec *result);
893 
894 #if defined(THREADZ) || defined(LOCKLESS)
895 void currentTR (word27 * trunits, bool * ovf);
896 #endif
897