-
Notifications
You must be signed in to change notification settings - Fork 12
/
TIVar.cpp
executable file
·543 lines (487 loc) · 12.8 KB
/
TIVar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/***************************************************
* tivar.cpp - Library for converting TI-OS var *
* types to/from POSIX var types. *
* Part of the ArTICL linking library. *
* Created by Christopher Mitchell, *
* 2011-2015, all rights reserved. *
***************************************************/
#include "TIVar.h"
// Convert a TI real variable into a long long int
long long int TIVar::realToLong8x(uint8_t* real, enum Endpoint model) {
long long int rval = 0;
int32_t dec_exp;
// Figure out what type it is
enum RealType type = modelToType(model);
if (type == REAL_89) {
return NAN; // TI-89/TI-92 not yet implemented! TODO
}
// Convert the exponent
dec_exp = TIVar::extractExponent(real, type) + 14;
// Now extract the number
const uint8_t mantissa_offset = (type == REAL_82)?2:3;
for(int i = 0; i < dec_exp; i++) {
rval *= 10;
rval += 0x0f & (real[mantissa_offset + (i >> 1)] >> (4 - (4 * (i % 2))));
}
// Negate the number, if necessary
if (real[0] & 0x80) {
rval = 0 - rval;
}
return rval;
}
// Convert a TI real variable into a double
double TIVar::realToFloat8x(uint8_t* real, enum Endpoint model) {
const double ieee_lut[10] = {0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f};
int32_t dec_exp;
double ieee_acc = 0;
// Figure out what type it is
enum RealType type = modelToType(model);
if (type == REAL_89) {
return NAN; // TI-89/TI-92 not yet implemented! TODO
}
// Convert the exponent
dec_exp = TIVar::extractExponent(real, type);
// Convert the mantissa
const uint8_t mantissa_offset = (type == REAL_82)?2:3;
for(uint8_t i = 0; i < 14; i++) {
float digit = ieee_lut[0x0f & (real[mantissa_offset + (i >> 1)] >> ((i & 0x01)?0:4))];
ieee_acc = (10 * ieee_acc) + digit;
}
// Raise mantissa to a positive exponent
while(dec_exp > 0) {
ieee_acc *= 10;
dec_exp--;
}
// Lower mantissa to a negative exponent
while(dec_exp < 0) {
ieee_acc *= 0.1f;
dec_exp++;
}
// Negate the number, if necessary
if (real[0] & 0x80) {
ieee_acc *= -1;
}
return ieee_acc;
}
// Convert a long long signed integer into a TI real variable
int TIVar::longToReal8x(long long int n, uint8_t* real, enum Endpoint model) {
int16_t exp = 13;
// Figure out what type it is
enum RealType type = modelToType(model);
if (type == REAL_89)
return -1; // TI-89/TI-92 not yet implemented! TODO
// Set sign bit and get absolute value
real[0] = (n >= 0)?0x00:0x80;
n = (n > 0)?n:-n;
// Bring large numbers down
while(n != 0 && n >= 10e13) {
n /= 10;
exp += 1;
}
// Bring small numbers up
while(n != 0 && n < 1e13) {
n *= 10;
exp -= 1;
}
// Extract the digits
const uint8_t mantissa_offset = (type == REAL_82)?2:3;
for(int8_t i=13; i >= 0; i--) {
uint8_t cdigit = (uint8_t)(n % 10);
if ((i & 0x01) == 1) {
real[mantissa_offset + (i >> 1)] = cdigit;
} else {
real[mantissa_offset + (i >> 1)] |= (cdigit << 4);
}
n /= 10;
}
// Set the exponent
if (type == REAL_82) {
exp += 0x80;
real[1] = (uint8_t)exp;
} else if (type == REAL_85) {
int32_t temp_exp = (int32_t)exp;
temp_exp += 0x00fc00;
real[1] = (uint8_t)(temp_exp & 0x00ff);
real[2] = (uint8_t)((temp_exp >> 8) & 0x00ff);
}
return TIVar::sizeOfReal(model); // Success: inserted data length
}
// Convert a double into a TI real variable
int TIVar::floatToReal8x(double f, uint8_t* real, enum Endpoint model) {
int16_t exp = 13;
// Figure out what type it is
enum RealType type = modelToType(model);
if (type == REAL_89) {
return -1; // TI-89/TI-92 not yet implemented! TODO
}
// Set sign bit and get absolute value
real[0] = (f >= 0)?0x00:0x80;
f = (f > 0)?f:-f;
// Bring large numbers down
while(f != 0 && f >= 10.e13) {
f *= 0.1f;
exp += 1;
}
// Bring small numbers up
while(f != 0 && f < 1.e13) {
f *= 10.f;
exp -= 1;
}
// Extract the digits
const uint8_t mantissa_offset = (type == REAL_82)?2:3;
for(int8_t i=13; i >= 0; i--) {
double digit, odigit;
digit = odigit = fmod(f, 10.);
uint8_t cdigit = 0;
while(digit > 0.5) {
cdigit++;
digit -= 1.f;
}
if ((i & 0x01) == 1) {
real[mantissa_offset + (i >> 1)] = cdigit;
} else {
real[mantissa_offset + (i >> 1)] |= (cdigit << 4);
}
f = (f - odigit) / 10.f;
}
// Set the exponent
if (type == REAL_82) {
exp += 0x80;
real[1] = (uint8_t)exp;
} else if (type == REAL_85) {
int32_t temp_exp = (int32_t)exp;
temp_exp += 0x00fc00;
real[1] = (uint8_t)(temp_exp & 0x00ff);
real[2] = (uint8_t)((temp_exp >> 8) & 0x00ff);
}
return TIVar::sizeOfReal(model); // Success: inserted data length
}
// Convert a printable 7-bit ASCII String into a TI string variable
int TIVar::stringToStrVar8x(String s, uint8_t* strVar, enum Endpoint model) {
uint16_t tokenlen = 0;
enum StringType type = modelToTypeStr(model);
int pos = 2; // Leave room for the length word prefix
if (type == STR_89) {
pos = 1;
} else if (type == STR_92) {
pos = 3;
}
for (int i = 0; i < s.length(); i++) {
uint8_t c = s[i];
uint16_t t;
if (c < 0x20 || c >= 0x7f) {
// Ignore control characters and 8-bit codes
continue;
}
if (type == STR_83) {
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z')) {
// Map basic characters (0-9, A-Z) directly
t = c;
} else if (c >= 'a' && c <= 'k') {
// Map lowercase letters (group 1)
t = (uint16_t)(c - 'a') + 0xbbb0;
} else if (c >= 'l' && c <= 'z') {
// Map lowercase letters (group 2)
t = (uint16_t)(c - 'l') + 0xbbbc;
} else {
// Map punctuation
switch (c) {
case ' ': t = 0x29; break;
case '!': t = 0x2d; break;
case '\"': t = 0x2a; break;
case '#': t = 0xbbd2; break;
case '$': t = 0xbbd3; break;
case '%': t = 0xbbda; break;
case '&': t = 0xbbd4; break;
case '\'': t = 0xae; break;
case '(': t = 0x10; break;
case ')': t = 0x11; break;
case '*': t = 0x82; break;
case '+': t = 0x70; break;
case ',': t = 0x2b; break;
case '-': t = 0x71; break;
case '.': t = 0x3a; break;
case '/': t = 0x83; break;
case ':': t = 0x3e; break;
case ';': t = 0xbbd6; break;
case '<': t = 0x6b; break;
case '=': t = 0x6a; break;
case '>': t = 0x6c; break;
case '?': t = 0xaf; break;
case '@': t = 0xbbd1; break;
case '[': t = 0x06; break;
case '\\': t = 0xbbd7; break;
case ']': t = 0x07; break;
case '^': t = 0xf0; break;
case '_': t = 0xbbd9; break;
case '`': t = 0xbbd5; break;
case '{': t = 0x08; break;
case '|': t = 0xbbd8; break;
case '}': t = 0x09; break;
case '~': t = 0xbbcf; break;
}
}
} else if (type == STR_82) {
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z')) {
// Map basic characters (0-9, A-Z) directly
t = c;
} else if (c >= 'a' && c <= 'z') {
// Turn lowercase letters into uppercase letters
t = (c - ('a' - 'A'));
} else {
// Map punctuation
switch (c) {
case ' ': t = 0x29; break;
case '!': t = 0x2d; break;
case '\"': t = 0x2a; break;
case '\'': t = 0xae; break;
case '(': t = 0x10; break;
case ')': t = 0x11; break;
case '*': t = 0x82; break;
case '+': t = 0x70; break;
case ',': t = 0x2b; break;
case '-': t = 0x71; break;
case '.': t = 0x3a; break;
case '/': t = 0x83; break;
case ':': t = 0x3e; break;
case '<': t = 0x6b; break;
case '=': t = 0x6a; break;
case '>': t = 0x6c; break;
case '?': t = 0xaf; break;
case '[': t = 0x06; break;
case ']': t = 0x07; break;
case '^': t = 0xf0; break;
case '{': t = 0x08; break;
case '}': t = 0x09; break;
}
}
} else { // Non-83-type mapping
// Map all printable characters directly
t = c;
}
// Append the token
if (t & 0xff00) {
strVar[pos++] = (t & 0xff00) >> 8;
}
strVar[pos++] = (t & 0xff);
tokenlen++;
}
if (type == STR_89) {
strVar[0] = '\0';
strVar[pos++] = '\0';
strVar[pos++] = 0x2d;
} else if (type == STR_92) {
TIVar::intToSizeWord(tokenlen + 2, strVar);
strVar[2] = '\0';
strVar[pos++] = '\0';
strVar[pos++] = 0x2d;
} else {
TIVar::intToSizeWord(tokenlen, strVar);
}
return pos; // Equivalent to the variable's length in bytes
}
// Convert a TI string variable into a printable 7-bit ASCII String
String TIVar::strVarToString8x(uint8_t* strVar, enum Endpoint model) {
String s;
enum StringType type = modelToTypeStr(model);
if (type == STR_89 || type == STR_92) {
int i = (type == STR_89) ? 1 : 3;
while (strVar[i]) {
s.concat((char)strVar[i]);
i++;
}
return s;
}
uint16_t tokenlen = sizeWordToInt(strVar);
int pos = 2;
for (int i = 0; i < tokenlen; i++) {
uint8_t c;
if (type == STR_85 || type == STR_86) {
c = s[i];
} else {
uint16_t t;
if (isA2ByteTok(strVar[pos])) {
t = strVar[pos++] << 8;
t |= strVar[pos++];
} else {
t = strVar[pos++];
}
if ((t >= 0x30 && t <= 0x39) ||
(t >= 0x41 && t <= 0x5a)) {
// Map basic tokens (0-9, A-Z) directly
c = t;
} else if (t >= 0xbbb0 && t <= 0xbbba) {
// Map lowercase letters (group 1)
c = t + 'a' - 0xbbb0;
} else if (t >= 0xbbbc && t <= 0xbbca) {
// Map lowercase letters (group 2)
c = t + 'l' - 0xbbbc;
} else {
// Map punctuation
switch (t) {
case 0x29: c = ' '; break;
case 0x2d: c = '!'; break;
case 0x2a: c = '\"'; break;
case 0xbbd2: c = '#'; break;
case 0xbbd3: c = '$'; break;
case 0xbbda: c = '%'; break;
case 0xbbd4: c = '&'; break;
case 0xae: c = '\''; break;
case 0x10: c = '('; break;
case 0x11: c = ')'; break;
case 0x82: c = '*'; break;
case 0x70: c = '+'; break;
case 0x2b: c = ','; break;
case 0x71: c = '-'; break;
case 0x3a: c = '.'; break;
case 0x83: c = '/'; break;
case 0x3e: c = ':'; break;
case 0xbbd6: c = ';'; break;
case 0x6b: c = '<'; break;
case 0x6a: c = '='; break;
case 0x6c: c = '>'; break;
case 0xaf: c = '?'; break;
case 0xbbd1: c = '@'; break;
case 0x06: c = '['; break;
case 0xbbd7: c = '\\'; break;
case 0x07: c = ']'; break;
case 0xf0: c = '^'; break;
case 0xbbd9: c = '_'; break;
case 0xbbd5: c = '`'; break;
case 0x08: c = '{'; break;
case 0xbbd8: c = '|'; break;
case 0x09: c = '}'; break;
case 0xbbcf: c = '~'; break;
default: c = '?'; break; // Non-ASCII tokens
}
}
}
s.concat((char)c);
}
return s;
}
bool TIVar::isA2ByteTok(uint8_t a) {
return (
a == 0x5c ||
a == 0x5d ||
a == 0x5e ||
a == 0x60 ||
a == 0x61 ||
a == 0x62 ||
a == 0x63 ||
a == 0x7e ||
a == 0xaa ||
a == 0xbb ||
a == 0xef);
}
// Return the type of real variable used on each model
enum RealType TIVar::modelToType(enum Endpoint model) {
switch(model) {
case COMP82:
case CBL82:
case CALC82:
return REAL_82;
break;
case COMP83:
case COMP83P:
case CALC83P:
case CALC83:
return REAL_83;
break;
case COMP85:
case CBL85:
case CALC85a:
case CALC85b:
return REAL_85;
break;
case COMP86:
return REAL_86;
break;
case COMP89:
case CBL89:
case CALC89:
return REAL_89;
break;
default:
return REAL_INVALID;
break;
}
}
// Return the type of string variable used on each model
enum StringType TIVar::modelToTypeStr(enum Endpoint model) {
switch(model) {
case COMP83:
case COMP83P:
case CALC83P:
case CALC83:
return STR_83;
break;
case COMP85:
case CBL85:
case CALC85a:
case CALC85b:
return STR_85;
break;
case COMP86:
return STR_86;
break;
case COMP89:
case CBL89:
case CALC89:
return STR_89;
break;
case CALC82:
case COMP82:
return STR_82;
break;
// TODO: The machine ID bytes for 89 are incorrect
// and causing these cases to not compile.
// case COMP92:
// case CBL92:
// case CALC92:
// return STR_92;
// break;
default:
return STR_INVALID;
break;
}
}
// Extract the exponent from a real
int32_t TIVar::extractExponent(uint8_t* real, enum RealType type) {
if (type == REAL_82) {
return ((int16_t)real[1] - 0x80) - 13; // decimal point is followed by 13 digits
} else if (type == REAL_85) {
int32_t raw_exp = (int32_t)TIVar::sizeWordToInt(&real[1]);
raw_exp -= 0x00fc00;
return (int16_t)raw_exp;
}
return 0;
}
uint16_t TIVar::sizeWordToInt(uint8_t* ptr) {
return ((uint16_t)ptr[0]) | (((uint16_t)ptr[1]) << 8);
}
void TIVar::intToSizeWord(uint16_t size, uint8_t* ptr) {
ptr[0] = (uint8_t)(size & 0x00ff);
ptr[1] = (uint8_t)(size >> 8);
return;
}
// Real variable length on each model
// REAL_89 is currently not supported by ArTICL.
int TIVar::sizeOfReal(enum Endpoint model) {
enum RealType type = modelToType(model);
switch(type) {
case REAL_82:
return 9;
break;
case REAL_85:
return 10;
break;
case REAL_89:
case REAL_INVALID:
return -1;
break;
}
return -1;
}