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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/glulxe/glulxe.h"
namespace Glk {
namespace Glulxe {
void Glulxe::stream_get_iosys(uint *mode, uint *rock) {
*mode = iosys_mode;
*rock = iosys_rock;
}
void Glulxe::stream_setup_unichar() {
#ifdef GLK_MODULE_UNICODE
if (glk_gestalt(gestalt_Unicode, 0))
glkio_unichar_han_ptr = &Glulxe::glk_put_char_uni;
else
glkio_unichar_han_ptr = &Glulxe::glkio_unichar_nouni_han;
#else /* GLK_MODULE_UNICODE */
glkio_unichar_han_ptr = glkio_unichar_nouni_han;
#endif /* GLK_MODULE_UNICODE */
}
void Glulxe::stream_set_iosys(uint mode, uint rock) {
switch (mode) {
default:
mode = 0;
// fall through
case iosys_None:
rock = 0;
stream_char_handler = &Glulxe::nopio_char_han;
stream_unichar_handler = &Glulxe::nopio_unichar_han;
break;
case iosys_Filter:
stream_char_handler = &Glulxe::filio_char_han;
stream_unichar_handler = &Glulxe::filio_unichar_han;
break;
case iosys_Glk:
if (!glkio_unichar_han_ptr)
stream_setup_unichar();
rock = 0;
stream_char_handler = &Glulxe::glk_put_char;
stream_unichar_handler = glkio_unichar_han_ptr;
break;
}
iosys_mode = mode;
iosys_rock = rock;
}
void Glulxe::nopio_char_han(unsigned char ch) {
}
void Glulxe::nopio_unichar_han(uint32 ch) {
}
void Glulxe::filio_char_han(unsigned char ch) {
uint val = ch;
push_callstub(0, 0);
enter_function(iosys_rock, 1, &val);
}
void Glulxe::filio_unichar_han(uint32 val) {
uint v = val;
push_callstub(0, 0);
enter_function(iosys_rock, 1, &v);
}
void Glulxe::glkio_unichar_nouni_han(uint32 val) {
/* Only used if the Glk library has no Unicode functions */
if (val > 0xFF)
val = '?';
glk_put_char(val);
}
void Glulxe::stream_num(int val, int inmiddle, int charnum) {
int ix = 0;
int res, jx;
char buf[16];
uint ival;
if (val == 0) {
buf[ix] = '0';
ix++;
} else {
if (val < 0)
ival = -val;
else
ival = val;
while (ival != 0) {
buf[ix] = (ival % 10) + '0';
ix++;
ival /= 10;
}
if (val < 0) {
buf[ix] = '-';
ix++;
}
}
switch (iosys_mode) {
case iosys_Glk:
ix -= charnum;
while (ix > 0) {
ix--;
glk_put_char(buf[ix]);
}
break;
case iosys_Filter:
if (!inmiddle) {
push_callstub(0x11, 0);
inmiddle = true;
}
if (charnum < ix) {
ival = buf[(ix - 1) - charnum] & 0xFF;
pc = val;
push_callstub(0x12, charnum + 1);
enter_function(iosys_rock, 1, &ival);
return;
}
break;
default:
break;
}
if (inmiddle) {
res = pop_callstub_string(&jx);
if (res)
fatal_error("String-on-string call stub while printing number.");
}
}
void Glulxe::stream_string(uint addr, int inmiddle, int bitnum) {
int ch;
int type;
int alldone = false;
int substring = (inmiddle != 0);
uint ival;
if (!addr)
fatal_error("Called stream_string with null address.");
while (!alldone) {
if (inmiddle == 0) {
type = Mem1(addr);
if (type == 0xE2)
addr += 4;
else
addr++;
bitnum = 0;
} else {
type = inmiddle;
}
if (type == 0xE1) {
if (tablecache_valid) {
int bits, numbits;
int readahead;
uint tmpaddr;
cacheblock_t *cablist;
int done = 0;
/* bitnum is already set right */
bits = Mem1(addr);
if (bitnum)
bits >>= bitnum;
numbits = (8 - bitnum);
readahead = false;
if (tablecache.type != 0) {
/* This is a bit of a cheat. If the top-level block is not
a branch, then it must be a string-terminator -- otherwise
the string would be an infinite repetition of that block.
We check for this case and bail immediately. */
done = 1;
}
cablist = tablecache.u.branches;
while (!done) {
cacheblock_t *cab;
if (numbits < CACHEBITS) {
/* readahead is certainly false */
int newbyte = Mem1(addr + 1);
bits |= (newbyte << numbits);
numbits += 8;
readahead = true;
}
cab = &(cablist[bits & CACHEMASK]);
numbits -= cab->depth;
bits >>= cab->depth;
bitnum += cab->depth;
if (bitnum >= 8) {
addr += 1;
bitnum -= 8;
if (readahead) {
readahead = false;
} else {
int newbyte = Mem1(addr);
bits |= (newbyte << numbits);
numbits += 8;
}
}
switch (cab->type) {
case 0x00: /* non-leaf node */
cablist = cab->u.branches;
break;
case 0x01: /* string terminator */
done = 1;
break;
case 0x02: /* single character */
switch (iosys_mode) {
case iosys_Glk:
glk_put_char(cab->u.ch);
break;
case iosys_Filter:
ival = cab->u.ch & 0xFF;
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
enter_function(iosys_rock, 1, &ival);
return;
}
cablist = tablecache.u.branches;
break;
case 0x04: /* single Unicode character */
switch (iosys_mode) {
case iosys_Glk:
(this->*glkio_unichar_han_ptr)(cab->u.uch);
break;
case iosys_Filter:
ival = cab->u.uch;
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
enter_function(iosys_rock, 1, &ival);
return;
}
cablist = tablecache.u.branches;
break;
case 0x03: /* C string */
switch (iosys_mode) {
case iosys_Glk:
for (tmpaddr = cab->u.addr; (ch = Mem1(tmpaddr)) != '\0'; tmpaddr++)
glk_put_char(ch);
cablist = tablecache.u.branches;
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
inmiddle = 0xE0;
addr = cab->u.addr;
done = 2;
break;
default:
cablist = tablecache.u.branches;
break;
}
break;
case 0x05: /* C Unicode string */
switch (iosys_mode) {
case iosys_Glk:
for (tmpaddr = cab->u.addr; (ival = Mem4(tmpaddr)) != 0; tmpaddr += 4)
(this->*glkio_unichar_han_ptr)(ival);
cablist = tablecache.u.branches;
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
inmiddle = 0xE2;
addr = cab->u.addr;
done = 2;
break;
default:
cablist = tablecache.u.branches;
break;
}
break;
case 0x08:
case 0x09:
case 0x0A:
case 0x0B: {
uint oaddr;
int otype;
oaddr = cab->u.addr;
if (cab->type >= 0x09)
oaddr = Mem4(oaddr);
if (cab->type == 0x0B)
oaddr = Mem4(oaddr);
otype = Mem1(oaddr);
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
if (otype >= 0xE0 && otype <= 0xFF) {
pc = addr;
push_callstub(0x10, bitnum);
inmiddle = 0;
addr = oaddr;
done = 2;
} else if (otype >= 0xC0 && otype <= 0xDF) {
uint argc;
uint *argv;
if (cab->type == 0x0A || cab->type == 0x0B) {
argc = Mem4(cab->u.addr + 4);
argv = pop_arguments(argc, cab->u.addr + 8);
} else {
argc = 0;
argv = nullptr;
}
pc = addr;
push_callstub(0x10, bitnum);
enter_function(oaddr, argc, argv);
return;
} else {
fatal_error("Unknown object while decoding string indirect reference.");
}
}
break;
default:
fatal_error("Unknown entity in string decoding (cached).");
break;
}
}
if (done > 1) {
continue; /* restart the top-level loop */
}
} else { /* tablecache not valid */
uint node;
int byte1;
int nodetype;
int done = 0;
if (!stringtable)
fatal_error("Attempted to print a compressed string with no table set.");
/* bitnum is already set right */
byte1 = Mem1(addr);
if (bitnum)
byte1 >>= bitnum;
node = Mem4(stringtable + 8);
while (!done) {
nodetype = Mem1(node);
node++;
switch (nodetype) {
case 0x00: /* non-leaf node */
if (byte1 & 1)
node = Mem4(node + 4);
else
node = Mem4(node + 0);
if (bitnum == 7) {
bitnum = 0;
addr++;
byte1 = Mem1(addr);
} else {
bitnum++;
byte1 >>= 1;
}
break;
case 0x01: /* string terminator */
done = 1;
break;
case 0x02: /* single character */
ch = Mem1(node);
switch (iosys_mode) {
case iosys_Glk:
glk_put_char(ch);
break;
case iosys_Filter:
ival = ch & 0xFF;
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
enter_function(iosys_rock, 1, &ival);
return;
}
node = Mem4(stringtable + 8);
break;
case 0x04: /* single Unicode character */
ival = Mem4(node);
switch (iosys_mode) {
case iosys_Glk:
(this->*glkio_unichar_han_ptr)(ival);
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
enter_function(iosys_rock, 1, &ival);
return;
}
node = Mem4(stringtable + 8);
break;
case 0x03: /* C string */
switch (iosys_mode) {
case iosys_Glk:
for (; (ch = Mem1(node)) != '\0'; node++)
glk_put_char(ch);
node = Mem4(stringtable + 8);
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
inmiddle = 0xE0;
addr = node;
done = 2;
break;
default:
node = Mem4(stringtable + 8);
break;
}
break;
case 0x05: /* C Unicode string */
switch (iosys_mode) {
case iosys_Glk:
for (; (ival = Mem4(node)) != 0; node += 4)
(this->*glkio_unichar_han_ptr)(ival);
node = Mem4(stringtable + 8);
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
pc = addr;
push_callstub(0x10, bitnum);
inmiddle = 0xE2;
addr = node;
done = 2;
break;
default:
node = Mem4(stringtable + 8);
break;
}
break;
case 0x08:
case 0x09:
case 0x0A:
case 0x0B: {
uint oaddr;
int otype;
oaddr = Mem4(node);
if (nodetype == 0x09 || nodetype == 0x0B)
oaddr = Mem4(oaddr);
otype = Mem1(oaddr);
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
if (otype >= 0xE0 && otype <= 0xFF) {
pc = addr;
push_callstub(0x10, bitnum);
inmiddle = 0;
addr = oaddr;
done = 2;
} else if (otype >= 0xC0 && otype <= 0xDF) {
uint argc;
uint *argv;
if (nodetype == 0x0A || nodetype == 0x0B) {
argc = Mem4(node + 4);
argv = pop_arguments(argc, node + 8);
} else {
argc = 0;
argv = nullptr;
}
pc = addr;
push_callstub(0x10, bitnum);
enter_function(oaddr, argc, argv);
return;
} else {
fatal_error("Unknown object while decoding string indirect reference.");
}
}
break;
default:
fatal_error("Unknown entity in string decoding.");
break;
}
}
if (done > 1) {
continue; /* restart the top-level loop */
}
}
} else if (type == 0xE0) {
switch (iosys_mode) {
case iosys_Glk:
while (1) {
ch = Mem1(addr);
addr++;
if (ch == '\0')
break;
glk_put_char(ch);
}
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
ch = Mem1(addr);
addr++;
if (ch != '\0') {
ival = ch & 0xFF;
pc = addr;
push_callstub(0x13, 0);
enter_function(iosys_rock, 1, &ival);
return;
}
break;
}
} else if (type == 0xE2) {
switch (iosys_mode) {
case iosys_Glk:
while (1) {
ival = Mem4(addr);
addr += 4;
if (ival == 0)
break;
(this->*glkio_unichar_han_ptr)(ival);
}
break;
case iosys_Filter:
if (!substring) {
push_callstub(0x11, 0);
substring = true;
}
ival = Mem4(addr);
addr += 4;
if (ival != 0) {
pc = addr;
push_callstub(0x14, 0);
enter_function(iosys_rock, 1, &ival);
return;
}
break;
}
} else if (type >= 0xE0 && type <= 0xFF) {
fatal_error("Attempt to print unknown type of string.");
} else {
fatal_error("Attempt to print non-string.");
}
if (!substring) {
/* Just get straight out. */
alldone = true;
} else {
/* Pop a stub and see what's to be done. */
addr = pop_callstub_string(&bitnum);
if (addr == 0) {
alldone = true;
} else {
inmiddle = 0xE1;
}
}
}
}
uint Glulxe::stream_get_table() {
return stringtable;
}
void Glulxe::stream_set_table(uint addr) {
if (stringtable == addr)
return;
/* Drop cache. */
if (tablecache_valid) {
if (tablecache.type == 0)
dropcache(tablecache.u.branches);
tablecache.u.branches = nullptr;
tablecache_valid = false;
}
stringtable = addr;
if (stringtable) {
/* Build cache. We can only do this if the table is entirely in ROM. */
uint tablelen = Mem4(stringtable);
uint rootaddr = Mem4(stringtable + 8);
int cache_stringtable = (stringtable + tablelen <= ramstart);
/* cache_stringtable = true; ...for testing only */
/* cache_stringtable = false; ...for testing only */
if (cache_stringtable) {
buildcache(&tablecache, rootaddr, CACHEBITS, 0);
/* dumpcache(&tablecache, 1, 0); */
tablecache_valid = true;
}
}
}
void Glulxe::buildcache(cacheblock_t *cablist, uint nodeaddr, int depth, int mask) {
int ix, type;
type = Mem1(nodeaddr);
if (type == 0 && depth == CACHEBITS) {
cacheblock_t *list, *cab;
list = (cacheblock_t *)glulx_malloc(sizeof(cacheblock_t) * CACHESIZE);
buildcache(list, nodeaddr, 0, 0);
cab = &(cablist[mask]);
cab->type = 0;
cab->depth = CACHEBITS;
cab->u.branches = list;
return;
}
if (type == 0) {
uint leftaddr = Mem4(nodeaddr + 1);
uint rightaddr = Mem4(nodeaddr + 5);
buildcache(cablist, leftaddr, depth + 1, mask);
buildcache(cablist, rightaddr, depth + 1, (mask | (1 << depth)));
return;
}
/* Leaf node. */
nodeaddr++;
for (ix = mask; ix < CACHESIZE; ix += (1 << depth)) {
cacheblock_t *cab = &(cablist[ix]);
cab->type = type;
cab->depth = depth;
switch (type) {
case 0x02:
cab->u.ch = Mem1(nodeaddr);
break;
case 0x04:
cab->u.uch = Mem4(nodeaddr);
break;
case 0x03:
case 0x05:
case 0x0A:
case 0x0B:
cab->u.addr = nodeaddr;
break;
case 0x08:
case 0x09:
cab->u.addr = Mem4(nodeaddr);
break;
}
}
}
#if 0
#include <stdio.h>
void Glulxe::dumpcache(cacheblock_t *cablist, int count, int indent) {
int ix, jx;
for (ix = 0; ix < count; ix++) {
cacheblock_t *cab = &(cablist[ix]);
for (jx = 0; jx < indent; jx++)
printf(" ");
printf("%X: ", ix);
switch (cab->type) {
case 0:
printf("...\n");
dumpcache(cab->u.branches, CACHESIZE, indent + 1);
break;
case 1:
printf("<EOS>\n");
break;
case 2:
printf("0x%02X", cab->u.ch);
if (cab->u.ch < 32)
printf(" ''\n");
else
printf(" '%c'\n", cab->u.ch);
break;
default:
printf("type %02X, address %06lX\n", cab->type, cab->u.addr);
break;
}
}
}
#endif /* 0 */
void Glulxe::dropcache(cacheblock_t *cablist) {
int ix;
for (ix = 0; ix < CACHESIZE; ix++) {
cacheblock_t *cab = &(cablist[ix]);
if (cab->type == 0) {
dropcache(cab->u.branches);
cab->u.branches = nullptr;
}
}
glulx_free(cablist);
}
char *Glulxe::make_temp_string(uint addr) {
int ix, len;
uint addr2;
char *res;
if (Mem1(addr) != 0xE0)
fatal_error("String argument to a Glk call must be unencoded.");
addr++;
for (addr2 = addr; Mem1(addr2); addr2++) { };
len = (addr2 - addr);
if (len < STATIC_TEMP_BUFSIZE) {
res = temp_buf;
} else {
res = (char *)glulx_malloc(len + 1);
if (!res)
fatal_error("Unable to allocate space for string argument to Glk call.");
}
for (ix = 0, addr2 = addr; ix < len; ix++, addr2++) {
res[ix] = Mem1(addr2);
}
res[len] = '\0';
return res;
}
uint32 *Glulxe::make_temp_ustring(uint addr) {
int ix, len;
uint addr2;
uint32 *res;
if (Mem1(addr) != 0xE2)
fatal_error("Ustring argument to a Glk call must be unencoded.");
addr += 4;
for (addr2 = addr; Mem4(addr2); addr2 += 4) { };
len = (addr2 - addr) / 4;
if ((len + 1) * 4 < STATIC_TEMP_BUFSIZE) {
res = (uint32 *)temp_buf;
} else {
res = (uint32 *)glulx_malloc((len + 1) * 4);
if (!res)
fatal_error("Unable to allocate space for ustring argument to Glk call.");
}
for (ix = 0, addr2 = addr; ix < len; ix++, addr2 += 4) {
res[ix] = Mem4(addr2);
}
res[len] = 0;
return res;
}
void Glulxe::free_temp_string(char *str) {
if (str && str != temp_buf)
glulx_free(str);
}
void Glulxe::free_temp_ustring(uint32 *str) {
if (str && str != (uint32 *)temp_buf)
glulx_free(str);
}
} // End of namespace Glulxe
} // End of namespace Glk
|