-
Notifications
You must be signed in to change notification settings - Fork 10
/
Output.cs
775 lines (666 loc) · 25 KB
/
Output.cs
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
/*
* Copyright © 2008, Textfyre, Inc. - All Rights Reserved
* Please read the accompanying COPYRIGHT file for licensing resstrictions.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace FyreVM
{
public partial class Engine
{
/// <summary>
/// Identifies an output system for use with @setiosys.
/// </summary>
private enum IOSystem
{
/// <summary>
/// Output is discarded.
/// </summary>
Null,
/// <summary>
/// Output is filtered through a Glulx function.
/// </summary>
Filter,
/// <summary>
/// Output is sent through FyreVM's channel system.
/// </summary>
Channels,
/// <summary>
/// Output is sent through Glk.
/// </summary>
/// <seealso cref="FyreVM.Engine.GlkMode"/>
Glk,
}
/// <summary>
/// Sends a single character to the output system (other than
/// <see cref="IOSystem.Filter"/>.
/// </summary>
/// <param name="ch">The character to send.</param>
private void SendCharToOutput(uint ch)
{
switch (outputSystem)
{
case IOSystem.Channels:
// TODO: need to handle Unicode characters larger than 16 bits?
outputBuffer.Write((char)ch);
break;
case IOSystem.Glk:
if (glkMode == GlkMode.Wrapper)
GlkWrapperWrite(ch);
break;
}
}
/// <summary>
/// Sends a string to the output system (other than
/// <see cref="IOSystem.Filter"/>.
/// </summary>
/// <param name="str">The string to send.</param>
private void SendStringToOutput(string str)
{
switch (outputSystem)
{
case IOSystem.Channels:
outputBuffer.Write(str);
break;
case IOSystem.Glk:
if (glkMode == GlkMode.Wrapper)
GlkWrapperWrite(str);
break;
}
}
/// <summary>
/// Sends the queued output to the <see cref="OutputReady"/> event handler.
/// </summary>
private void DeliverOutput()
{
if (OutputReady != null)
{
OutputReadyEventArgs args = new OutputReadyEventArgs();
args.Package = outputBuffer.Flush();
OutputReady(this, args);
}
}
private void SelectOutputSystem(uint number, uint rock)
{
switch (number)
{
case 0:
outputSystem = IOSystem.Null;
break;
case 1:
outputSystem = IOSystem.Filter;
filterAddress = rock;
break;
case 2:
if (glkMode == GlkMode.None)
throw new VMException("Glk support is not enabled");
outputSystem = IOSystem.Glk;
break;
case 20: // T is the 20th letter
outputSystem = IOSystem.Channels;
break;
default:
throw new VMException("Unrecognized output system " + number.ToString());
}
}
private void NextCStringChar()
{
byte ch = image.ReadByte(pc);
pc++;
if (ch == 0)
{
DonePrinting();
return;
}
if (outputSystem == IOSystem.Filter)
PerformCall(filterAddress, new uint[] { ch }, GLULX_STUB_RESUME_CSTR, 0, pc);
else
SendCharToOutput(ch);
}
private void NextUniStringChar()
{
uint ch = image.ReadInt32(pc);
pc += 4;
if (ch == 0)
{
DonePrinting();
return;
}
if (outputSystem == IOSystem.Filter)
PerformCall(filterAddress, new uint[] { ch }, GLULX_STUB_RESUME_UNISTR, 0, pc);
else
SendCharToOutput(ch);
}
private void NextDigit()
{
string num = pc.ToString();
if (printingDigit < num.Length)
{
if (outputSystem == IOSystem.Filter)
{
PerformCall(filterAddress, new uint[] { (uint)num[printingDigit] },
GLULX_STUB_RESUME_NUMBER, (uint)(printingDigit + 1), pc);
}
else
{
// there's no reason to be here if we're not filtering output...
System.Diagnostics.Debug.Assert(false);
SendCharToOutput(num[printingDigit]);
printingDigit++;
}
}
else
DonePrinting();
}
private bool NextCompressedStringBit()
{
bool result = (image.ReadByte(pc) & (1 << printingDigit)) != 0;
printingDigit++;
if (printingDigit == 8)
{
printingDigit = 0;
pc++;
}
return result;
}
#region Native String Decoding Table
private abstract class StrNode
{
/// <summary>
/// Performs the action associated with this string node: printing
/// a character or string, terminating output, or reading a bit and
/// delegating to another node.
/// </summary>
/// <param name="e">The <see cref="Engine"/> that is printing.</param>
/// <remarks>When called on a branch node, this will consume one or
/// more compressed string bits.</remarks>
public abstract void HandleNextChar(Engine e);
/// <summary>
/// Returns the non-branch node that will handle the next string action.
/// </summary>
/// <param name="e">The <see cref="Engine"/> that is printing.</param>
/// <returns>A non-branch string node.</returns>
/// <remarks>When called on a branch node, this will consume one or
/// more compressed string bits.</remarks>
public virtual StrNode GetHandlingNode(Engine e)
{
return this;
}
/// <summary>
/// Gets a value indicating whether this node requires a call stub to be
/// pushed.
/// </summary>
public virtual bool NeedsCallStub
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether this node terminates the string.
/// </summary>
public virtual bool IsTerminator
{
get { return false; }
}
protected void EmitChar(Engine e, char ch)
{
if (e.outputSystem == IOSystem.Filter)
{
e.PerformCall(e.filterAddress, new uint[] { (uint)ch },
GLULX_STUB_RESUME_HUFFSTR, e.printingDigit, e.pc);
}
else
{
e.SendCharToOutput(ch);
}
}
protected void EmitChar(Engine e, uint ch)
{
if (e.outputSystem == IOSystem.Filter)
{
e.PerformCall(e.filterAddress, new uint[] { ch },
GLULX_STUB_RESUME_HUFFSTR, e.printingDigit, e.pc);
}
else
{
e.SendCharToOutput(ch);
}
}
}
private class EndStrNode : StrNode
{
public override void HandleNextChar(Engine e)
{
e.DonePrinting();
}
public override bool IsTerminator
{
get { return true; }
}
}
private class BranchStrNode : StrNode
{
private readonly StrNode left, right;
public BranchStrNode(StrNode left, StrNode right)
{
this.left = left;
this.right = right;
}
public StrNode Left
{
get { return left; }
}
public StrNode Right
{
get { return right; }
}
public override void HandleNextChar(Engine e)
{
if (e.NextCompressedStringBit() == true)
right.HandleNextChar(e);
else
left.HandleNextChar(e);
}
public override StrNode GetHandlingNode(Engine e)
{
if (e.NextCompressedStringBit() == true)
return right.GetHandlingNode(e);
else
return left.GetHandlingNode(e);
}
}
private class CharStrNode : StrNode
{
private readonly char ch;
public CharStrNode(char ch)
{
this.ch = ch;
}
public char Char
{
get { return ch; }
}
public override void HandleNextChar(Engine e)
{
EmitChar(e, ch);
}
public override string ToString()
{
return "CharStrNode: '" + ch + "'";
}
}
private class UniCharStrNode : StrNode
{
private readonly uint ch;
public UniCharStrNode(uint ch)
{
this.ch = ch;
}
public uint Char
{
get { return ch; }
}
public override void HandleNextChar(Engine e)
{
EmitChar(e, ch);
}
public override string ToString()
{
return string.Format("UniCharStrNode: '{0}' ({1})", (char)ch, ch);
}
}
private class StringStrNode : StrNode
{
private readonly uint address;
private readonly ExecutionMode mode;
private readonly string str;
public StringStrNode(uint address, ExecutionMode mode, string str)
{
this.address = address;
this.mode = mode;
this.str = str;
}
public uint Address
{
get { return address; }
}
public ExecutionMode Mode
{
get { return mode; }
}
public override void HandleNextChar(Engine e)
{
if (e.outputSystem == IOSystem.Filter)
{
e.PushCallStub(
new CallStub(GLULX_STUB_RESUME_HUFFSTR, e.printingDigit, e.pc, e.fp));
e.pc = address;
e.execMode = mode;
}
else
{
e.SendStringToOutput(str);
}
}
public override string ToString()
{
return "StringStrNode: \"" + str + "\"";
}
}
private class IndirectStrNode : StrNode
{
private readonly uint address;
private readonly bool dblIndirect;
private readonly uint argCount, argsAt;
public IndirectStrNode(uint address, bool dblIndirect,
uint argCount, uint argsAt)
{
this.address = address;
this.dblIndirect = dblIndirect;
this.argCount = argCount;
this.argsAt = argsAt;
}
public uint Address
{
get { return address; }
}
public bool DoubleIndirect
{
get { return DoubleIndirect; }
}
public uint ArgCount
{
get { return argCount; }
}
public uint ArgsAt
{
get { return argsAt; }
}
public override void HandleNextChar(Engine e)
{
e.PrintIndirect(
dblIndirect ? e.image.ReadInt32(address) : address,
argCount, argsAt);
}
public override bool NeedsCallStub
{
get { return true; }
}
}
/// <summary>
/// Builds a native version of the string decoding table if the table
/// is entirely in ROM, or verifies the table's current state if the
/// table is in RAM.
/// </summary>
private void CacheDecodingTable()
{
if (decodingTable == 0)
{
nativeDecodingTable = null;
return;
}
uint size = image.ReadInt32(decodingTable + GLULX_HUFF_TABLESIZE_OFFSET);
if (decodingTable + size - 1 >= image.RamStart)
{
// if the table is in RAM, don't cache it. just verify it now
// and then process it directly from RAM when the time comes.
nativeDecodingTable = null;
VerifyDecodingTable();
return;
}
uint root = image.ReadInt32(decodingTable + GLULX_HUFF_ROOTNODE_OFFSET);
nativeDecodingTable = CacheDecodingTableNode(root);
}
private StrNode CacheDecodingTableNode(uint node)
{
if (node == 0)
return null;
byte nodeType = image.ReadByte(node++);
switch (nodeType)
{
case GLULX_HUFF_NODE_END:
return new EndStrNode();
case GLULX_HUFF_NODE_BRANCH:
return new BranchStrNode(
CacheDecodingTableNode(image.ReadInt32(node)),
CacheDecodingTableNode(image.ReadInt32(node + 4)));
case GLULX_HUFF_NODE_CHAR:
return new CharStrNode((char)image.ReadByte(node));
case GLULX_HUFF_NODE_UNICHAR:
return new UniCharStrNode(image.ReadInt32(node));
case GLULX_HUFF_NODE_CSTR:
return new StringStrNode(node, ExecutionMode.CString,
ReadCString(node));
case GLULX_HUFF_NODE_UNISTR:
return new StringStrNode(node, ExecutionMode.UnicodeString,
ReadUniString(node));
case GLULX_HUFF_NODE_INDIRECT:
return new IndirectStrNode(image.ReadInt32(node), false, 0, 0);
case GLULX_HUFF_NODE_INDIRECT_ARGS:
return new IndirectStrNode(image.ReadInt32(node), false,
image.ReadInt32(node + 4), node + 8);
case GLULX_HUFF_NODE_DBLINDIRECT:
return new IndirectStrNode(image.ReadInt32(node), true, 0, 0);
case GLULX_HUFF_NODE_DBLINDIRECT_ARGS:
return new IndirectStrNode(image.ReadInt32(node), true,
image.ReadInt32(node + 4), node + 8);
default:
throw new VMException("Unrecognized compressed string node type " + nodeType.ToString());
}
}
private string ReadCString(uint address)
{
StringBuilder sb = new StringBuilder();
byte b = image.ReadByte(address);
while (b != 0)
{
sb.Append((char)b);
b = image.ReadByte(++address);
}
return sb.ToString();
}
private string ReadUniString(uint address)
{
StringBuilder sb = new StringBuilder();
uint ch = image.ReadInt32(address);
while (ch != 0)
{
sb.Append((char)ch);
address += 4;
ch = image.ReadInt32(address);
}
return sb.ToString();
}
#endregion
/// <summary>
/// Checks that the string decoding table is well-formed, i.e., that it
/// contains at least one branch, one end marker, and no unrecognized
/// node types.
/// </summary>
/// <exception cref="VMException">
/// The string decoding table is malformed.
/// </exception>
private void VerifyDecodingTable()
{
if (decodingTable == 0)
return;
Stack<uint> nodesToCheck = new Stack<uint>();
uint rootNode = image.ReadInt32(decodingTable + GLULX_HUFF_ROOTNODE_OFFSET);
nodesToCheck.Push(rootNode);
bool foundBranch = false, foundEnd = false;
while (nodesToCheck.Count > 0)
{
uint node = nodesToCheck.Pop();
byte nodeType = image.ReadByte(node++);
switch (nodeType)
{
case GLULX_HUFF_NODE_BRANCH:
nodesToCheck.Push(image.ReadInt32(node)); // left child
nodesToCheck.Push(image.ReadInt32(node + 4)); // right child
foundBranch = true;
break;
case GLULX_HUFF_NODE_END:
foundEnd = true;
break;
case GLULX_HUFF_NODE_CHAR:
case GLULX_HUFF_NODE_UNICHAR:
case GLULX_HUFF_NODE_CSTR:
case GLULX_HUFF_NODE_UNISTR:
case GLULX_HUFF_NODE_INDIRECT:
case GLULX_HUFF_NODE_INDIRECT_ARGS:
case GLULX_HUFF_NODE_DBLINDIRECT:
case GLULX_HUFF_NODE_DBLINDIRECT_ARGS:
// OK
break;
default:
throw new VMException("Unrecognized compressed string node type " + nodeType.ToString());
}
}
if (!foundBranch)
throw new VMException("String decoding table contains no branches");
if (!foundEnd)
throw new VMException("String decoding table contains no end markers");
}
/// <summary>
/// Prints the next character of a compressed string, consuming one or
/// more bits.
/// </summary>
/// <remarks>This is only used when the string decoding table is in RAM.</remarks>
private void NextCompressedChar()
{
uint node = image.ReadInt32(decodingTable + GLULX_HUFF_ROOTNODE_OFFSET);
while (true)
{
byte nodeType = image.ReadByte(node++);
switch (nodeType)
{
case GLULX_HUFF_NODE_BRANCH:
if (NextCompressedStringBit() == true)
node = image.ReadInt32(node + 4); // go right
else
node = image.ReadInt32(node); // go left
break;
case GLULX_HUFF_NODE_END:
DonePrinting();
return;
case GLULX_HUFF_NODE_CHAR:
case GLULX_HUFF_NODE_UNICHAR:
uint singleChar = (nodeType == GLULX_HUFF_NODE_UNICHAR) ?
image.ReadInt32(node) : image.ReadByte(node);
if (outputSystem == IOSystem.Filter)
{
PerformCall(filterAddress, new uint[] { singleChar },
GLULX_STUB_RESUME_HUFFSTR, printingDigit, pc);
}
else
{
SendCharToOutput(singleChar);
}
return;
case GLULX_HUFF_NODE_CSTR:
if (outputSystem == IOSystem.Filter)
{
PushCallStub(new CallStub(GLULX_STUB_RESUME_HUFFSTR, printingDigit, pc, fp));
pc = node;
execMode = ExecutionMode.CString;
}
else
{
for (byte ch = image.ReadByte(node); ch != 0; ch = image.ReadByte(++node))
SendCharToOutput(ch);
}
return;
case GLULX_HUFF_NODE_UNISTR:
if (outputSystem == IOSystem.Filter)
{
PushCallStub(new CallStub(GLULX_STUB_RESUME_UNISTR, printingDigit, pc, fp));
pc = node;
execMode = ExecutionMode.UnicodeString;
}
else
{
for (uint ch = image.ReadInt32(node); ch != 0; node += 4, ch = image.ReadInt32(node))
SendCharToOutput(ch);
}
return;
case GLULX_HUFF_NODE_INDIRECT:
PrintIndirect(image.ReadInt32(node), 0, 0);
return;
case GLULX_HUFF_NODE_INDIRECT_ARGS:
PrintIndirect(image.ReadInt32(node), image.ReadInt32(node + 4), node + 8);
return;
case GLULX_HUFF_NODE_DBLINDIRECT:
PrintIndirect(image.ReadInt32(image.ReadInt32(node)), 0, 0);
return;
case GLULX_HUFF_NODE_DBLINDIRECT_ARGS:
PrintIndirect(image.ReadInt32(image.ReadInt32(node)), image.ReadInt32(node + 4), node + 8);
return;
default:
throw new VMException("Unrecognized compressed string node type " + nodeType.ToString());
}
}
}
/// <summary>
/// Prints a string, or calls a routine, when an indirect node is
/// encountered in a compressed string.
/// </summary>
/// <param name="address">The address of the string or routine.</param>
/// <param name="argCount">The number of arguments passed in.</param>
/// <param name="argsAt">The address where the argument array is stored.</param>
private void PrintIndirect(uint address, uint argCount, uint argsAt)
{
byte type = image.ReadByte(address);
switch (type)
{
case 0xC0:
case 0xC1:
uint[] args = new uint[argCount];
for (uint i = 0; i < argCount; i++)
args[i] = image.ReadInt32(argsAt + 4 * i);
PerformCall(address, args, GLULX_STUB_RESUME_HUFFSTR, printingDigit, pc);
break;
case 0xE0:
if (outputSystem == IOSystem.Filter)
{
PushCallStub(new CallStub(GLULX_STUB_RESUME_HUFFSTR, printingDigit, pc, fp));
execMode = ExecutionMode.CString;
pc = address + 1;
}
else
{
address++;
for (byte ch = image.ReadByte(address); ch != 0; ch = image.ReadByte(++address))
SendCharToOutput(ch);
}
break;
case 0xE1:
PushCallStub(new CallStub(GLULX_STUB_RESUME_HUFFSTR, printingDigit, pc, fp));
execMode = ExecutionMode.CompressedString;
pc = address + 1;
printingDigit = 0;
break;
case 0xE2:
if (outputSystem == IOSystem.Filter)
{
PushCallStub(new CallStub(GLULX_STUB_RESUME_HUFFSTR, printingDigit, pc, fp));
execMode = ExecutionMode.UnicodeString;
pc = address + 4;
}
else
{
address += 4;
for (uint ch = image.ReadInt32(address); ch != 0; address += 4, ch = image.ReadInt32(address))
SendCharToOutput(ch);
}
break;
default:
throw new VMException(string.Format("Invalid type for indirect printing: {0:X}h", type));
}
}
private void DonePrinting()
{
ResumeFromCallStub(0);
}
}
}