-
Notifications
You must be signed in to change notification settings - Fork 10
/
Opcodes.cs
1885 lines (1632 loc) · 56.5 KB
/
Opcodes.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
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2008, Textfyre, Inc. - All Rights Reserved
* Please read the accompanying COPYRIGHT file for licensing resstrictions.
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace FyreVM
{
/// <summary>
/// A delegate type for methods that implement Glulx opcodes.
/// </summary>
/// <param name="operands">The array of operand values, passed in and out of
/// the method.</param>
/// <remarks><para>Elements of the <paramref name="operands"/> array that correspond to
/// load operands will be filled with the loaded values before the method is called.
/// Elements corresponding to store operands must be filled in by the method;
/// after the method returns, those values will be read from the array and stored
/// in their destinations.</para>
/// <para>Note that "delayed store" operands take up two entries in the array.</para></remarks>
internal delegate void OpcodeHandler(uint[] operands);
/// <summary>
/// Describes exceptions to the typical operand order and meaning
/// for certain opcodes that don't fit the pattern.
/// </summary>
internal enum OpcodeRule : byte
{
/// <summary>
/// No special treatment.
/// </summary>
None,
/// <summary>
/// Indirect operands work with single bytes.
/// </summary>
Indirect8Bit,
/// <summary>
/// Indirect operands work with 16-bit words.
/// </summary>
Indirect16Bit,
/// <summary>
/// Has an additional operand that resembles a store, but which
/// is not actually passed out by the opcode handler. Instead, the
/// handler receives two values, DestType and DestAddr, which may
/// be written into a call stub so the result can be stored later.
/// </summary>
DelayedStore,
/// <summary>
/// Special case for op_catch. This opcode has a load operand
/// (the branch offset) and a delayed store, but the store comes first.
/// args[0] and [1] are the delayed store, and args[2] is the load.
/// </summary>
Catch,
}
internal class Opcode
{
private readonly OpcodeAttribute attr;
private readonly OpcodeHandler handler;
public Opcode(OpcodeAttribute attr, OpcodeHandler handler)
{
this.attr = attr;
this.handler = handler;
}
public OpcodeAttribute Attr
{
get { return attr; }
}
public OpcodeHandler Handler
{
get { return handler; }
}
public override string ToString()
{
return attr.Name;
}
}
/// <summary>
/// Describes a method that implements a Glulx opcode. The method must
/// fit the pattern of the <see cref="OpcodeHandler"/> delegate.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
class OpcodeAttribute : Attribute
{
private uint number;
private string name;
private byte loadArgs, storeArgs;
private OpcodeRule rule;
public OpcodeAttribute(uint num, string name, byte loadArgs)
{
this.number = num;
this.name = name;
this.loadArgs = loadArgs;
}
public OpcodeAttribute(uint num, string name, byte loadArgs, byte storeArgs)
: this(num, name, loadArgs)
{
this.storeArgs = storeArgs;
}
/// <summary>
/// Gets the opcode number.
/// </summary>
public uint Number
{
get { return number; }
}
/// <summary>
/// Gets the opcode's mnemonic name.
/// </summary>
public string Name
{
get { return name; }
}
/// <summary>
/// Gets the number of load operands, which appear before any store operands.
/// </summary>
/// <remarks>
/// If <see cref="Rule"/> is set to <see cref="OpcodeRule.Branch"/>,
/// the branch offset is not included in this count.
/// </remarks>
public byte LoadArgs
{
get { return loadArgs; }
}
/// <summary>
/// Gets the number of store operands, which appear after the load operands.
/// </summary>
public byte StoreArgs
{
get { return storeArgs; }
}
/// <summary>
/// Gets a value describing anything exceptional about this opcode.
/// </summary>
public OpcodeRule Rule
{
get { return rule; }
set { rule = value; }
}
}
public partial class Engine
{
[Opcode(0x00, "nop", 0)]
private void op_nop(uint[] args)
{
// do nothing!
}
#region Arithmetic
[Opcode(0x10, "add", 2, 1)]
private void op_add(uint[] args)
{
args[2] = args[0] + args[1];
}
[Opcode(0x11, "sub", 2, 1)]
private void op_sub(uint[] args)
{
args[2] = args[0] - args[1];
}
[Opcode(0x12, "mul", 2, 1)]
private void op_mul(uint[] args)
{
args[2] = args[0] * args[1];
}
[Opcode(0x13, "div", 2, 1)]
private void op_div(uint[] args)
{
args[2] = (uint)((int)args[0] / (int)args[1]);
}
[Opcode(0x14, "mod", 2, 1)]
private void op_mod(uint[] args)
{
args[2] = (uint)((int)args[0] % (int)args[1]);
}
[Opcode(0x15, "neg", 1, 1)]
private void op_neg(uint[] args)
{
args[1] = (uint)(-(int)args[0]);
}
[Opcode(0x18, "bitand", 2, 1)]
private void op_bitand(uint[] args)
{
args[2] = args[0] & args[1];
}
[Opcode(0x19, "bitor", 2, 1)]
private void op_bitor(uint[] args)
{
args[2] = args[0] | args[1];
}
[Opcode(0x1A, "bitxor", 2, 1)]
private void op_bitxor(uint[] args)
{
args[2] = args[0] ^ args[1];
}
[Opcode(0x1B, "bitnot", 1, 1)]
private void op_bitnot(uint[] args)
{
args[1] = ~args[0];
}
[Opcode(0x1C, "shiftl", 2, 1)]
private void op_shiftl(uint[] args)
{
if (args[1] >= 32)
args[2] = 0;
else
args[2] = args[0] << (int)args[1];
}
[Opcode(0x1D, "sshiftr", 2, 1)]
private void op_sshiftr(uint[] args)
{
if (args[1] >= 32)
args[2] = ((args[0] & 0x80000000) == 0) ? 0 : 0xFFFFFFFF;
else
args[2] = (uint)((int)args[0] >> (int)args[1]);
}
[Opcode(0x1E, "ushiftr", 2, 1)]
private void op_ushiftr(uint[] args)
{
if (args[1] >= 32)
args[2] = 0;
else
args[2] = args[0] >> (int)args[1];
}
#endregion
#region Branching
private void TakeBranch(uint target)
{
if (target == 0)
LeaveFunction(0);
else if (target == 1)
LeaveFunction(1);
else
pc += target - 2;
}
[Opcode(0x20, "jump", 1)]
private void op_jump(uint[] args)
{
TakeBranch(args[0]);
}
[Opcode(0x22, "jz", 2)]
private void op_jz(uint[] args)
{
if (args[0] == 0)
TakeBranch(args[1]);
}
[Opcode(0x23, "jnz", 2)]
private void op_jnz(uint[] args)
{
if (args[0] != 0)
TakeBranch(args[1]);
}
[Opcode(0x24, "jeq", 3)]
private void op_jeq(uint[] args)
{
if (args[0] == args[1])
TakeBranch(args[2]);
}
[Opcode(0x25, "jne", 3)]
private void op_jne(uint[] args)
{
if (args[0] != args[1])
TakeBranch(args[2]);
}
[Opcode(0x26, "jlt", 3)]
private void op_jlt(uint[] args)
{
if ((int)args[0] < (int)args[1])
TakeBranch(args[2]);
}
[Opcode(0x27, "jge", 3)]
private void op_jge(uint[] args)
{
if ((int)args[0] >= (int)args[1])
TakeBranch(args[2]);
}
[Opcode(0x28, "jgt", 3)]
private void op_jgt(uint[] args)
{
if ((int)args[0] > (int)args[1])
TakeBranch(args[2]);
}
[Opcode(0x29, "jle", 3)]
private void op_jle(uint[] args)
{
if ((int)args[0] <= (int)args[1])
TakeBranch(args[2]);
}
[Opcode(0x2A, "jltu", 3)]
private void op_jltu(uint[] args)
{
if (args[0] < args[1])
TakeBranch(args[2]);
}
[Opcode(0x2B, "jgeu", 3)]
private void op_jgeu(uint[] args)
{
if (args[0] >= args[1])
TakeBranch(args[2]);
}
[Opcode(0x2C, "jgtu", 3)]
private void op_jgtu(uint[] args)
{
if (args[0] > args[1])
TakeBranch(args[2]);
}
[Opcode(0x2D, "jleu", 3)]
private void op_jleu(uint[] args)
{
if (args[0] <= args[1])
TakeBranch(args[2]);
}
[Opcode(0x104, "jumpabs", 1)]
private void op_jumpabs(uint[] args)
{
pc = args[0];
}
#endregion
#region Functions
private uint[] funcargs1 = new uint[1];
private uint[] funcargs2 = new uint[2];
private uint[] funcargs3 = new uint[3];
[Opcode(0x30, "call", 2, Rule = OpcodeRule.DelayedStore)]
private void op_call(uint[] args)
{
int count = (int)args[1];
uint[] funcargs = new uint[count];
for (int i = 0; i < count; i++)
funcargs[i] = Pop();
PerformCall(args[0], funcargs, args[2], args[3]);
}
[Opcode(0x160, "callf", 1, Rule = OpcodeRule.DelayedStore)]
private void op_callf(uint[] args)
{
PerformCall(args[0], null, args[1], args[2]);
}
[Opcode(0x161, "callfi", 2, Rule = OpcodeRule.DelayedStore)]
private void op_callfi(uint[] args)
{
funcargs1[0] = args[1];
PerformCall(args[0], funcargs1, args[2], args[3]);
}
[Opcode(0x162, "callfii", 3, Rule = OpcodeRule.DelayedStore)]
private void op_callfii(uint[] args)
{
funcargs2[0] = args[1];
funcargs2[1] = args[2];
PerformCall(args[0], funcargs2, args[3], args[4]);
}
[Opcode(0x163, "callfiii", 4, Rule = OpcodeRule.DelayedStore)]
private void op_callfiii(uint[] args)
{
funcargs3[0] = args[1];
funcargs3[1] = args[2];
funcargs3[2] = args[3];
PerformCall(args[0], funcargs3, args[4], args[5]);
}
private void PerformCall(uint address, uint[] args, uint destType, uint destAddr)
{
PerformCall(address, args, destType, destAddr, pc);
}
private void PerformCall(uint address, uint[] args, uint destType, uint destAddr, uint stubPC)
{
PerformCall(address, args, destType, destAddr, stubPC, false);
}
/// <summary>
/// Enters a function, pushing a call stub first if necessary.
/// </summary>
/// <param name="address">The address of the function to call.</param>
/// <param name="args">The function's arguments, or <b>null</b> to call without arguments.</param>
/// <param name="destType">The DestType for the call stub. Ignored for tail calls.</param>
/// <param name="destAddr">The DestAddr for the call stub. Ignored for tail calls.</param>
/// <param name="stubPC">The PC value for the call stub. Ignored for tail calls.</param>
/// <param name="tailCall"><b>true</b> to perform a tail call, reusing the current call stub
/// and frame instead of pushing a new stub and creating a new frame.</param>
private void PerformCall(uint address, uint[] args, uint destType, uint destAddr, uint stubPC, bool tailCall)
{
uint result;
if (veneer.InterceptCall(this, address, args, out result))
{
PerformDelayedStore(destType, destAddr, result);
return;
}
if (tailCall)
{
// pop the current frame and use the call stub below it
sp = fp;
}
else
{
// use a new call stub
PushCallStub(new CallStub(destType, destAddr, stubPC, fp));
}
byte type = image.ReadByte(address);
if (type == 0xC0)
{
// arguments are passed in on the stack
EnterFunction(address);
if (args == null)
{
Push(0);
}
else
{
for (int i = args.Length - 1; i >= 0; i--)
Push(args[i]);
Push((uint)args.Length);
}
}
else if (type == 0xC1)
{
// arguments are passed in local storage
EnterFunction(address, args);
}
else
throw new VMException(string.Format("Invalid function type {0:X}h", type));
}
[Opcode(0x31, "return", 1)]
private void op_return(uint[] args)
{
LeaveFunction(args[0]);
}
[Opcode(0x32, "catch", 0, Rule = OpcodeRule.Catch)]
private void op_catch(uint[] args)
{
PushCallStub(new CallStub(args[0], args[1], pc, fp));
// the catch token is the value of sp after pushing that stub
PerformDelayedStore(args[0], args[1], sp);
TakeBranch(args[2]);
}
[Opcode(0x33, "throw", 2)]
private void op_throw(uint[] args)
{
if (args[1] > sp)
throw new VMException("Invalid catch token");
// pop the stack back down to the stub pushed by catch
sp = args[1];
// restore from the stub
CallStub stub = PopCallStub();
pc = stub.PC;
fp = stub.FramePtr;
frameLen = ReadFromStack(fp);
localsPos = ReadFromStack(fp + 4);
// store the thrown value and resume after the catch opcode
PerformDelayedStore(stub.DestType, stub.DestAddr, args[0]);
}
[Opcode(0x34, "tailcall", 2)]
private void op_tailcall(uint[] args)
{
int count = (int)args[1];
uint[] funcargs = new uint[count];
for (int i = 0; i < count; i++)
funcargs[i] = Pop();
PerformCall(args[0], funcargs, 0, 0, 0, true);
}
[Opcode(0x180, "accelfunc", 2)]
private void op_accelfunc(uint[] args)
{
veneer.SetSlotGlulx(this, false, args[0], args[1]);
}
[Opcode(0x181, "accelparam", 2)]
private void op_accelparam(uint[] args)
{
veneer.SetSlotGlulx(this, true, args[0], args[1]);
}
#endregion
#region Variables and Arrays
[Opcode(0x40, "copy", 1, 1)]
private void op_copy(uint[] args)
{
args[1] = args[0];
}
[Opcode(0x41, "copys", 1, 1, Rule = OpcodeRule.Indirect16Bit)]
private void op_copys(uint[] args)
{
args[1] = (ushort)args[0];
}
[Opcode(0x42, "copyb", 1, 1, Rule = OpcodeRule.Indirect8Bit)]
private void op_copyb(uint[] args)
{
args[1] = (byte)args[0];
}
[Opcode(0x44, "sexs", 1, 1)]
private void op_sexs(uint[] args)
{
args[1] = (uint)(int)(short)args[0];
}
[Opcode(0x45, "sexb", 1, 1)]
private void op_sexb(uint[] args)
{
args[1] = (uint)(int)(sbyte)args[0];
}
[Opcode(0x48, "aload", 2, 1)]
private void op_aload(uint[] args)
{
args[2] = image.ReadInt32(args[0] + 4 * args[1]);
}
[Opcode(0x49, "aloads", 2, 1)]
private void op_aloads(uint[] args)
{
args[2] = image.ReadInt16(args[0] + 2 * args[1]);
}
[Opcode(0x4A, "aloadb", 2, 1)]
private void op_aloadb(uint[] args)
{
args[2] = image.ReadByte(args[0] + args[1]);
}
[Opcode(0x4B, "aloadbit", 2, 1)]
private void op_aloadbit(uint[] args)
{
int bit = (int)args[1];
uint address = (uint)(args[0] + bit / 8);
bit %= 8;
if (bit < 0)
{
address--;
bit += 8;
}
byte value = image.ReadByte(address);
args[2] = (value & (1 << bit)) == 0 ? (uint)0 : (uint)1;
}
[Opcode(0x4C, "astore", 3)]
private void op_astore(uint[] args)
{
image.WriteInt32(args[0] + 4 * args[1], args[2]);
}
[Opcode(0x4D, "astores", 3)]
private void op_astores(uint[] args)
{
image.WriteInt16(args[0] + 2 * args[1], (ushort)args[2]);
}
[Opcode(0x4E, "astoreb", 3)]
private void op_astoreb(uint[] args)
{
image.WriteByte(args[0] + args[1], (byte)args[2]);
}
[Opcode(0x4F, "astorebit", 3)]
private void op_astorebit(uint[] args)
{
int bit = (int)args[1];
uint address = (uint)(args[0] + bit / 8);
bit %= 8;
if (bit < 0)
{
address--;
bit += 8;
}
byte value = image.ReadByte(address);
if (args[2] == 0)
value &= (byte)(~(1 << bit));
else
value |= (byte)(1 << bit);
image.WriteByte(address, value);
}
#endregion
#region Output
[Opcode(0x70, "streamchar", 1)]
private void op_streamchar(uint[] args)
{
StreamCharCore((byte)args[0]);
}
[Opcode(0x73, "streamunichar", 1)]
private void op_streamunichar(uint[] args)
{
StreamCharCore(args[0]);
}
private void StreamCharCore(uint value)
{
if (outputSystem == IOSystem.Filter)
{
PerformCall(filterAddress, new uint[] { value }, GLULX_STUB_STORE_NULL, 0);
}
else
{
SendCharToOutput(value);
}
}
[Opcode(0x71, "streamnum", 1)]
private void op_streamnum(uint[] args)
{
if (outputSystem == IOSystem.Filter)
{
PushCallStub(new CallStub(GLULX_STUB_RESUME_FUNC, 0, pc, fp));
string num = ((int)args[0]).ToString();
PerformCall(filterAddress, new uint[] { (uint)num[0] },
GLULX_STUB_RESUME_NUMBER, 1, args[0]);
}
else
{
string num = ((int)args[0]).ToString();
SendStringToOutput(num);
}
}
[Opcode(0x72, "streamstr", 1)]
private void op_streamstr(uint[] args)
{
if (outputSystem == IOSystem.Null)
return;
uint address = args[0];
byte type = image.ReadByte(address);
// for retrying a compressed string after we discover it needs a call stub
byte savedDigit = 0;
StrNode savedNode = null;
/* if we're not using the userland output filter, and the string is
* uncompressed (or contains no indirect references), we can just print it
* right here. */
if (outputSystem != IOSystem.Filter)
{
switch (type)
{
case 0xE0:
// C string
SendStringToOutput(ReadCString(address + 1));
return;
case 0xE2:
// Unicode string
SendStringToOutput(ReadUniString(address + 4));
return;
case 0xE1:
// compressed string
if (nativeDecodingTable != null)
{
uint oldPC = pc;
pc = address + 1;
printingDigit = 0;
StrNode node = nativeDecodingTable.GetHandlingNode(this);
while (!node.NeedsCallStub)
{
if (node.IsTerminator)
{
pc = oldPC;
return;
}
node.HandleNextChar(this);
node = nativeDecodingTable.GetHandlingNode(this);
}
savedDigit = printingDigit;
savedNode = node;
address = pc - 1;
pc = oldPC;
}
break;
}
}
// can't decompress anything without a decoding table
if (type == 0xE1 && decodingTable == 0)
throw new VMException("No string decoding table is set");
/* otherwise, we have to push a call stub and let the main
* interpreter loop take care of printing the string. */
PushCallStub(new CallStub(GLULX_STUB_RESUME_FUNC, 0, pc, fp));
switch (type)
{
case 0xE0:
execMode = ExecutionMode.CString;
pc = address + 1;
break;
case 0xE1:
execMode = ExecutionMode.CompressedString;
pc = address + 1;
printingDigit = savedDigit;
// this won't read a bit, since savedNode can't be a branch...
if (savedNode != null)
savedNode.HandleNextChar(this);
break;
case 0xE2:
execMode = ExecutionMode.UnicodeString;
pc = address + 4;
break;
default:
throw new VMException(string.Format("Invalid string type {0:X}h", type));
}
}
[Opcode(0x130, "glk", 2, 1)]
private void op_glk(uint[] args)
{
switch (glkMode)
{
case GlkMode.None:
// not really supported, just clear the stack
for (uint i = 0; i < args[1]; i++)
Pop();
args[2] = 0;
break;
case GlkMode.Wrapper:
GlkWrapperCall(args);
break;
}
}
[Opcode(0x140, "getstringtbl", 0, 1)]
private void op_getstringtbl(uint[] args)
{
args[0] = decodingTable;
}
[Opcode(0x141, "setstringtbl", 1)]
private void op_setstringtbl(uint[] args)
{
decodingTable = args[0];
CacheDecodingTable();
}
[Opcode(0x148, "getiosys", 0, 2)]
private void op_getiosys(uint[] args)
{
switch (outputSystem)
{
case IOSystem.Null:
args[0] = 0;
args[1] = 0;
break;
case IOSystem.Filter:
args[0] = 1;
args[1] = filterAddress;
break;
case IOSystem.Channels:
args[0] = 20;
args[1] = 0;
break;
}
}
[Opcode(0x149, "setiosys", 2, 0)]
private void op_setiosys(uint[] args)
{
SelectOutputSystem(args[0], args[1]);
}
#endregion
#region Memory Management
[Opcode(0x102, "getmemsize", 0, 1)]
private void op_getmemsize(uint[] args)
{
args[0] = image.EndMem;
}
[Opcode(0x103, "setmemsize", 1, 1)]
private void op_setmemsize(uint[] args)
{
if (heap != null)
throw new VMException("setmemsize is not allowed while the heap is active");
try
{
image.EndMem = args[0];
args[1] = 0;
}
catch
{
args[1] = 1;
}
}
[Opcode(0x170, "mzero", 2)]
private void op_mzero(uint[] args)
{
for (uint i = 0; i < args[0]; i++)
image.WriteByte(args[1] + i, 0);
}
[Opcode(0x171, "mcopy", 3)]
private void op_mcopy(uint[] args)
{
if (args[2] < args[1])
{
for (uint i = 0; i < args[0]; i++)
image.WriteByte(args[2] + i, image.ReadByte(args[1] + i));
}
else
{
for (uint i = args[0] - 1; i >= 0; i--)
image.WriteByte(args[2] + i, image.ReadByte(args[1] + i));
}
}
private bool HandleHeapMemoryRequest(uint newEndMem)
{
try
{
image.EndMem = newEndMem;
return true;
}
catch
{
return false;
}
}
[Opcode(0x178, "malloc", 1, 1)]
private void op_malloc(uint[] args)
{
uint size = args[0];
if ((int)size <= 0)
{
args[1] = 0;
return;
}
if (heap == null)
{
uint oldEndMem = image.EndMem;
heap = new HeapAllocator(oldEndMem, HandleHeapMemoryRequest);
heap.MaxSize = maxHeapSize;
args[1] = heap.Alloc(size);
if (args[1] == 0)
{
heap = null;
image.EndMem = oldEndMem;
}
}
else
{
args[1] = heap.Alloc(size);
}
}
[Opcode(0x179, "mfree", 1)]
private void op_mfree(uint[] args)
{
if (heap != null)
{
heap.Free(args[0]);
if (heap.BlockCount == 0)
{
image.EndMem = heap.Address;
heap = null;
}
}
}
#endregion
#region Searching
[Flags]
private enum SearchOptions
{
None = 0,
KeyIndirect = 1,
ZeroKeyTerminates = 2,
ReturnIndex = 4,
}
private bool KeyIsZero(uint address, uint size)
{
for (uint i = 0; i < size; i++)
if (image.ReadByte(address + i) != 0)
return false;
return true;
}
/// <summary>
/// Performs key comparison for the various search opcodes.
/// </summary>
/// <param name="query">The search key, if <see cref="SearchOptions.KeyIndirect"/> is
/// not set; or the address of the search key, if it is set.</param>
/// <param name="candidateAddr">The address of the candidate key which is to be
/// checked against the search key.</param>
/// <param name="keySize">The length of the keys, in bytes.</param>
/// <param name="options">The options passed into the search opcode.</param>
/// <returns>A negative value if the search key is less than the candidate key,
/// a positive value if the search key is greater than the candidate key, or
/// 0 if the keys match.</returns>
private int CompareKeys(uint query, uint candidateAddr, uint keySize, SearchOptions options)
{
if ((options & SearchOptions.KeyIndirect) == 0)
{
// KeyIndirect is *not* set
// mask query to the appropriate size and compare it against the value stored at candidateAddr
uint ckey;
switch (keySize)
{
case 1:
ckey = image.ReadByte(candidateAddr);
query &= 0xFF;
break;
case 2:
ckey = image.ReadInt16(candidateAddr);
query &= 0xFFFF;
break;
case 3:
ckey = (uint)(image.ReadByte(candidateAddr) << 24 + image.ReadInt16(candidateAddr + 1));
query &= 0xFFFFFF;
break;
default:
ckey = image.ReadInt32(candidateAddr);
break;
}