-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1476 lines (1225 loc) · 36.1 KB
/
main.c
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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "header.h"
#include "stack.h"
#include "zip.h"
#include "callStack.h"
#include "data.h"
#include "object.h"
#include "../foenixLibrary/mytypes.h"
#include "../foenixLibrary/vicky.h"
#include "../foenixLibrary/timer.h"
#include "../foenixLibrary/interrupt.h"
#define DEBUG
void *heap_start = (void * )0x190000, *heap_end = (void * )0x220000;
#define UART1 (*(unsigned char *)0xAF13F8)
#define UART1X (*(unsigned char *)0xAF13FD)
char debugStringBuffer[200];
int debugBufferLength = 0;
//http://jiten-thakkar.com/posts/writing-generic-stack-in-c
//https://www.techiedelight.com/introduction-linked-lists/
//https://www.codesdope.com/blog/article/making-a-stack-using-linked-list-in-c/
void IRQHandler(void)
{
// int reg = 0;
// if (reg = (INT_PENDING_REG0 & FNX0_INT02_TMR0))
// {
// textScreen[6] = spinner[spinnerState];
// if (spinnerState < 7)
// spinnerState++;
// else
// {
// spinnerState = 0;
// }
// //disk_timerproc();
// reg = INT_PENDING_REG0 & FNX0_INT02_TMR0;
// INT_PENDING_REG0 = reg;
// }
// if (reg = (INT_PENDING_REG3 & FNX3_INT02_IDE))
// {
// textScreen[10] = spinner[spinnerStateDisk];
// if (spinnerStateDisk < 7)
// spinnerStateDisk++;
// else
// {
// spinnerStateDisk = 0;
// }
// printf("interrupt %d", IDE_CMD_STAT);
// reg = INT_PENDING_REG3 & INT_PENDING_REG3;
// INT_PENDING_REG3 = reg;
// }
printf("interrupt");
}
void COPHandler(void)
{
printf("COP");
}
void BRKHandler(void)
{
printf("break");
while(1) {};
}
void initialize(void);
void gameLoop(void);
void ReadInstruction(void);
short loadVariable(short number);
void storeVariable(short location, short value);
void StoreResult(short value);
void z_get_prop_len(void);
byte opcode;
byte opcodeExtended;
short operands[8];
short operandType[8];
short storeLocation;
int instructionCount = -1;
//byte *zorkData;
extern struct header* zorkHeader;
struct stack *stack;
//u_int8_t currentInstruction;
ushort programCounter;
short globals[240];
int main()
{
// Emulator workarround for screen
//set the display size - 128 x 64
COLS_PER_LINE = 80;
LINES_MAX = 60;
//set the visible display size - 80 x 60
COLS_VISIBLE = 80;
LINES_VISIBLE = 60;
//INT_MASK_REG0 = 0xFB; // unmask harddisk;
// enable interrupts
//enableInterrupts();
setEGATextPalette();
clearTextScreen(' ', 0xD, 0xE);
VKY_TXT_CURSOR_X_REG = 0;
VKY_TXT_CURSOR_Y_REG = 0;
BORDER_X_SIZE = 0;
BORDER_Y_SIZE = 0;
// Create the stack
stack = stack_new(1024);
// Clear variables
//memset(variableStack, 0, 512 * 15);
//create the callstack
// fix in later version we start in "main" function, might need to push a functionData struct on the callstack for that.
callStack_initialize();
/*
u_int16_t a = getInitialProgramCounter();
u_int16_t b = getRelease();
u_int16_t c = getObjectTableLocation();
u_int16_t d = getGlobalVariableLocation();
u_int16_t e = getStaticMemoryLocation();
u_int16_t f = getHighMemoryStart();
u_int16_t g = getAbbreviationsLocation();
u_int16_t h = getAlphabetTableAddress();
*/
printf("Initialising....\n\n");
// initialize stuff
initialize();
printf("Start main game loop.\n\n");
// play game
gameLoop();
printf("Exiting");
return 0;
}
void initialize(void)
{
byte i = 0;
ushort address = 0;
// header_initialise("/home/bart/ZIP/ZORK1.DAT");
// data_initialise(getHighMemoryStart(), "/home/bart/ZIP/ZORK1.DAT");
printf("Reading header\n");
header_initialise("1:ZORK1.DAT");
printf("Reading data file\n");
data_initialise(getHighMemoryStart(), "1:ZORK1.DAT");
//data_initialise(getStaticMemoryLocation(), "1:ZORK1.DAT");
// free data from initial load and retarget pointer to main data array
free(zorkHeader);
zorkHeader = (struct header *)zData;
address = getGlobalVariableLocation();
/* stick header to array */
//zorkHeader = zData;
// load initial values for globals
for(i = 0; i < 240; i++)
{
globals[i] = data_loadWord(address);
address += 2;
}
objecttable_initialize(getObjectTableLocation(), zorkHeader->version);
text_initialize(zorkHeader->version, getAbbreviationsLocation());
}
void gameLoop()
{
byte tempByte;
#ifdef DEBUG
int i = 0;
#endif
programCounter = getInitialProgramCounter();
do
{
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "\nPC: %hu,", programCounter);
write(3, debugStringBuffer, debugBufferLength);
#endif
instructionCount++;
ReadInstruction();
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, " OPCODE: 0x%X (%d) instruction: %d\n", opcode, opcode, instructionCount);
write(3, debugStringBuffer, debugBufferLength);
i = 0;
while (operandType[i] != 3)
{
debugBufferLength = sprintf(debugStringBuffer, "\tOperand %d value: %d(%hu) type: %d\n", i, operands[i], (ushort)operands[i], operandType[i]);
write(3, debugStringBuffer, debugBufferLength);
i++;
}
#endif
//printf("%d", instructionCount);
/* VAR instruction */
if ((opcode & 0xC0) == 0xC0)
{
if (opcode >= 0xE0)
{
switch (opcode)
{
case 0xE0:
z_call_vs();
break;
case 0xE1:
z_storew();
break;
case 0xE3:
z_put_prop();
break;
case 0xE4:
z_read();
break;
case 0xE5:
z_print_char();
break;
case 0xE6:
z_print_num();
break;
case 0xE8:
z_push();
break;
case 0xE9:
z_pull();
break;
default:
printf("unimplemented opcode A: %X (%d)", opcode, opcode);
exit(1);
break;
}
}
else
{
switch(opcode)
{
case 0xC1:
z_je();
break;
case 0xC9:
z_and();
break;
default:
printf("unimplemented opcode B: %d", opcode);
exit(1);
break;
}
}
//continue;
goto endloop;
}
/* SHORT instruction */
if ((opcode & 0x80) == 0x80)
{
// 0x80 - 0x8F Large const
// 0x90 - 0x9F small const
// 0xA0 - 0xAF variable (locals / globals)
switch(opcode)
{
case 0x80:
break;
case 0x81:
break;
case 0xA0:
z_jz();
break;
case 0x84:
case 0x94:
case 0xA4:
z_get_prop_len();
break;
case 0x8C:
z_jump();
break;
case 0x95:
z_inc();
break;
case 0xA1:
z_get_sibling();
break;
case 0xA2:
z_get_child();
break;
case 0xA3:
z_get_parent();
break;
case 0xAA:
z_print_obj();
break;
case 0xAB:
z_ret();
break;
case 0xB0:
z_rtrue();
break;
case 0xB1:
z_rfalse();
break;
case 0xB2:
z_printf();
break;
case 0xB8:
z_ret_popped();
break;
case 0xBB:
z_new_line();
break;
default:
printf("unimplemented opcode C: %d\n", opcode);
exit(1);
break;
}
//continue;
goto endloop;
}
/* Extended instructions for level 5 and above */
if (opcode == 0xBE && zorkHeader->version >= 5)
{
printf("unimplemented opcode D: %d\n", opcode);
exit(1);
break;
//continue;
goto endloop;
}
/* LONG instruction */
switch(opcode)
{
case 0x41:
case 0x61:
z_je();
break;
case 0x42:
z_jl();
break;
case 0x04:
z_dec_chk();
break;
case 0x05:
z_inc_chk();
break;
case 0x10:
case 0x30:
z_loadb();
break;
case 0x0D:
case 0x2D:
z_store();
break;
case 0x46:
z_jin();
break;
case 0x49:
z_and();
break;
case 0x4A:
z_test_attr();
break;
case 0x4B:
z_set_attr();
break;
case 0x0F:
case 0x4F:
z_loadw();
break;
case 0x54:
case 0x74:
z_add();
break;
case 0x51:
z_get_prop();
break;
case 0x55:
z_sub();
break;
case 0x6E:
z_insert_obj();
break;
default:
printf("unimplemented opcode E: %d\n", opcode);
exit(1);
break;
}
endloop:
//dummy printf to attach breakpoint
printf("");
}
while (1);
}
void ReadInstruction()
{
short i = 0;
byte tempByte = 0;
// Clear operandtypes and operands
memset(operandType, 0, 8);
memset(operands, 0, 8);
opcode = data_loadByte(programCounter++);
// 2 operand
if (opcode <= 0x7F)
{
switch ((opcode >> 5))
{
case 0: // smallconstant smallconstant
operands[0] = data_loadByte(programCounter++);
operands[1] = data_loadByte(programCounter++);
operandType[0] = 0x01;
operandType[1] = 0x01;
operandType[2] = 0x03;
break;
case 1: // smallconstant variablenumber
operands[0] = data_loadByte(programCounter++);
operands[1] = loadVariable(data_loadByte(programCounter++));
operandType[0] = 0x01;
operandType[1] = 0x02;
operandType[2] = 0x03;
break;
case 2: // variable small
operands[0] = loadVariable(data_loadByte(programCounter++));
operands[1] = data_loadByte(programCounter++);
operandType[0] = 0x02;
operandType[1] = 0x01;
operandType[2] = 0x03;
break;
case 3: // variable variable
operands[0] = loadVariable(data_loadByte(programCounter++));
operands[1] = loadVariable(data_loadByte(programCounter++));
operandType[0] = 0x02;
operandType[1] = 0x02;
operandType[2] = 0x03;
break;
default:
// Impossible do nothing
break;
}
}
// 1 operand and 0 operand
if (opcode >= 0x80 && opcode < 0xBF)
{
switch ((opcode >> 4) & 0x03)
{
case 0: // largeconstant
operands[0] = data_loadWord(programCounter);
programCounter +=2;
operandType[0] = 0x00;
operandType[1] = 0x03;
break;
case 1: // smallconstant
operands[0] = data_loadByte(programCounter++);
operandType[0] = 0x01;
operandType[1] = 0x03;
break;
case 2: // variable
operands[0] = loadVariable(data_loadByte(programCounter++));
operandType[0] = 0x02;
operandType[1] = 0x03;
break;
case 3: // zero op or extended op
if (opcode == 0xBE)
{
opcodeExtended = data_loadByte(programCounter++);
printf("Extended opcode, need to implement operand loading!");
// fix load operands
}
else
{
operandType[0] = 0x03;
}
break;
default:
// Impossible do nothing
break;
}
}
// variable operand count opcodes
if (opcode >= 0xC0 && opcode < 0xFF)
{
tempByte = data_loadByte(programCounter++);
// get operand types
for (i = 3; i >= 0; i--)
{
operandType[i] = tempByte & 0x03;
tempByte = tempByte >> 2;
}
// 4.5.1 Note that only call_vs2 and call_vn2 can have more than 4 operands, and no instruction can have more than 8.
//if (operandType[3] != 0x03 && operandType[2] != 0x03 && operandType[1] != 0x03 && operandType[0] != 0x03 && opcode != 0xE0)
if (opcode == 0xEC || opcode == 0xFA)
{
// Load second operand byte
tempByte = data_loadByte(programCounter++);
for (i = 7; i >= 4; i--)
{
operandType[i] = tempByte & 0x03;
tempByte = tempByte >> 2;
}
}
else
{
// signal "end of operands" in case of 4 operand calls. Does nothing when less than 4 operands are used.
operandType[4] = 0x03;
}
// Read operands
for(i = 0; operandType[i] != 0x3 && i < 8; i++)
{
switch(operandType[i])
{
case 0x0:
operands[i] = data_loadWord(programCounter);
programCounter += 2;
break;
case 0x1:
operands[i] = data_loadByte(programCounter++);
break;
case 0x2:
// get variable number
operands[i] = loadVariable(data_loadByte(programCounter++));
break;
}
}
}
}
short loadVariable(short number)
{
// change variable number to variable value for processing
if (number == 0)
{
return stack_pop(stack);
}
else if(number <= 0x0F)
{
// 0 is stack so 1 is first local giving an array offset of 1
return callStack_top()->locals[number - 1];
}
else
{
// globals start after 16 (stack at 0 plus 15 locals) so offset 0x10
return globals[number - 0x10];
}
}
/* 4.6 "Store" instructions return a value: e.g., mul multiplies its two operands together.
Such instructions must be followed by a single byte giving the variable number of where to put the result.
*/
void storeResult(short value)
{
ushort storeLocation = data_loadByte(programCounter++);
if (storeLocation == 0)
{
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "Storing %d (%hu) to stack\n", value, (ushort)value, storeLocation);
write(3, debugStringBuffer, debugBufferLength);
#endif
stack_push(stack, value);
}
else if (storeLocation <= 15)
{
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "Storing local %d (%hu) to %d\n", value, (ushort)value, storeLocation);
write(3, debugStringBuffer, debugBufferLength);
#endif
callStack_top()->locals[storeLocation - 1] = value;
}
else
{
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "Storing global %d (%d) to %d\n", value, (ushort)value, storeLocation - 0x10);
write(3, debugStringBuffer, debugBufferLength);
#endif
globals[storeLocation - 0x10] = value;
}
}
void storeVariable(short location, short value)
{
if (location == 0)
{
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "Storing %d (%d) to stack\n", value, (ushort)value);
write(3, debugStringBuffer, debugBufferLength);
#endif
stack_push(stack, value);
}
else if (location <= 15)
{
#ifdef DEBUG
sprintf(debugStringBuffer, "Storing local %d (%d) to %d\n", value, (ushort)value, location);
write(3, debugStringBuffer, debugBufferLength);
#endif
callStack_top()->locals[location - 1] = value;
}
else
{
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "Storing global %d (%d) to %d\n", value, (ushort)value, location - 0x10);
write(3, debugStringBuffer, debugBufferLength);
#endif
globals[location - 0x10] = value;
}
}
/* 4.7
Instructions which test a condition are called "branch" instructions. The branch information is stored in one or two bytes,
indicating what to do with the result of the test. If bit 7 of the first byte is 0, a branch occurs when the condition was false;
if 1, then branch is on true. If bit 6 is set, then the branch occupies 1 byte only, and the "offset" is in the range 0 to 63,
given in the bottom 6 bits. If bit 6 is clear, then the offset is a signed 14-bit number given in bits 0 to 5 of the first byte
followed by all 8 of the second.
4.7.1
An offset of 0 means "return false from the current routine", and 1 means "return true from the current routine".
4.7.2
Otherwise, a branch moves execution to the instruction at address
Address after branch data + Offset - 2.
*/
void branchTo(int value)
{
short branchOffset = 0;
byte branchByte1 = 0;
branchByte1 = data_loadByte(programCounter++);
if ((branchByte1 & 0x40) == 0)
{
// 2 byte offset
branchOffset = ((branchByte1 & 0x3F) << 8) + data_loadByte(programCounter++);
}
else
{
// 1 byte offset
branchOffset = (branchByte1 & 0x3F);
}
if ((branchByte1 >> 7 && value == 1) || ((branchByte1 >> 7) == 0 && value == 0))
{
if (branchOffset == 0 || branchOffset == 1)
{
returnfromRoutine(branchOffset);
}
else
{
programCounter = programCounter + branchOffset - 2;
}
}
}
void returnfromRoutine(short returnValue)
{
functionData returnFrom;
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "return from: %hu\r\n", callStack_Size());
write(3, debugStringBuffer, debugBufferLength);
#endif
// return to previous routing by popping the current one from the stack
returnFrom = callStack_pop();
// return program counter to previous value;
programCounter = returnFrom.returnAddress;
// free the locals. Is this needed?
//free(returnFrom.locals);
// pop all unused data from current routine the stack
while (stack_size(stack) > returnFrom.returnStackSize)
{
stack_pop(stack);
}
// return the function result.
storeResult(returnValue);
}
void z_add(void)
{
ushort result = 0;
result = operands[0] + operands[1];
storeResult(result);
}
void z_sub(void)
{
ushort result = 0;
result = operands[0] - operands[1];
storeResult(result);
}
/*
VAR:224 0 1 call routine ...up to 3 args... -> (result)
The only call instruction in Version 3, Inform reads this as call_vs in higher versions: it calls the routine with 0, 1, 2 or 3 arguments
as supplied and stores the resulting return value. (When the address 0 is called as a routine, nothing happens and the return value is false.)
*/
void z_call_vs(void)
{
short i = 0;
ushort x;
if(operands[0] == 0)
{
storeResult(0);
return;
}
// #ifdef DEBUG
// debugBufferLength = sprintf(debugStringBuffer, "callx %d", callStack_Size());
// write(3, debugStringBuffer, debugBufferLength);
// #endif
// New functiondata object to push on the callstack
callStack_push();
//callStack_top()->locals = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Read storage location and push on stack;
// do not read here, read after return
//fd.returnValueStorage = storeLocation; //zorkData[programCounter++];
// #ifdef DEBUG
// debugBufferLength = sprintf(debugStringBuffer, "call %d", callStack_Size());
// write(3, debugStringBuffer, debugBufferLength);
// #endif
// Push return program counter to stack
callStack_top()->returnAddress = programCounter;
// Push current stackDepth to stack and set to zero
callStack_top()->returnStackSize = stack_size(stack);
// read number of locals in routine
// fix packed addressess implement 1.2.3 here
x = data_loadByte(operands[0] << 1); // (left shift 1 gives *2)
//fd.locals = malloc(x * 2); // times two as short is two bytes per item
// Clear and load locals from routine header
//memset(fd.locals, 0, x * 2);
memset(callStack_top()->locals, 0, 15 * 2);
for (i = 0; (i / 2) < x; i += 2)
{
//callStack_top()->locals[i / 2] = (data_loadByte((operands[0] << 1) + 1 + i) << 8) + data_loadByte((operands[0] << 1) + 2 + i);
callStack_top()->locals[i / 2] = data_loadWord((operands[0] << 1) + 1 + i);
}
// set locals from function operands
// first operand is routine address so skip that.
for (i = 1; operandType[i] != 0x03; i++)
{
callStack_top()->locals[i - 1] = operands[i];
}
// call routine
// counter + number of locals * 2 + 1 for the byte with local count.
//callStack_push(fd);
programCounter = (operands[0] << 1) + (x * 2) + 1;
#ifdef DEBUG
debugBufferLength = sprintf(debugStringBuffer, "Added frame: %hd returnAddress: %hu \n", callStack_Size(), callStack_top()->returnAddress);
write(3, debugStringBuffer, debugBufferLength);
#endif
}
void z_je(void)
{
short i = 0;
ushort result = 0;
if (opcode != 0xC1)
{
branchTo(operands[0] == operands[1]);
}
else
{
for (i = 1; operandType[i] != 3; i++)
{
if (operands[0] == operands[i])
result = 1;
}
branchTo(result);
}
}
void z_jz(void)
{
branchTo(operands[0] == 0);
}
void z_get_prop_len(void)
{
ushort location = 0;
ushort store = 0;
short value;
printf("needs validation/completion");
if (opcode = 0x94)
{
// get location of property
// apperenty location is location of actual property data, not location of preceding size byte
// fix unsure what location represents, might be a object number in the object table.
location = data_loadByte(programCounter++) - 1;
// get location to store result
store = data_loadByte(programCounter++);
// get actual value
//addr = zargs[0] - 1;
value = data_loadByte(location);
/* Calculate length of property */
//if (h_version <= V3)
value = (value >> 5) + 1;
//else if (!(value & 0x80))
// value = (value >> 6) + 1;
/*else {
value &= 0x3f;
if (value == 0) value = 64; /* demanded by Spec 1.0 */
//}
}
storeResult(value);
}
/*
2OP:13 D store (variable) value
Set the VARiable referenced by the operand to value.
*/
void z_store(void)
{
storeVariable(operands[0], operands[1]);
}
/*
storew array word-index value
array-->word-index = value, i.e. stores the given value in the word at address array+2*word-index (which must lie in dynamic memory). (See loadw.)
*/
void z_storew(void)
{
ushort array = operands[0];
short wordIndex = operands[1];
short value = operands[2];
//zorkData[array + 2 * wordIndex] = (value >> 8);
//zorkData[(array + 2 * wordIndex) + 1] = (value & 0xFF);
data_saveWord(array + 2 * wordIndex, value);
}
// 2OP:4 4 dec_chk (variable) value ?(label)
// Decrement variable, and branch if it is now less than the given value.
void z_dec_chk(void)
{
short variableValue = 0;
short variableNumber = 0;
// constant gives variable to load
variableNumber = operands[0];
variableValue = loadVariable(variableNumber);
// decrement value and store back;
storeVariable(variableNumber, --variableValue);
branchTo(variableValue < operands[1]);
}
/*
2OP:5 5 inc_chk (variable) value ?(label)
Increment variable, and branch if now greater than value.
*/
void z_inc_chk(void)
{
short variableValue = 0;
short variableNumber = 0;
// constant gives variable to load
variableNumber = operands[0];
variableValue = loadVariable(variableNumber);
// increment value and store back;
storeVariable(variableNumber, ++variableValue);
branchTo(variableValue > operands[1]);
}
/*
2OP:16 10 loadb array byte-index -> (result)
Stores array->byte-index (i.e., the byte at address array+byte-index, which must lie in static or dynamic memory).
*/
void z_loadb(void)
{
ushort array = 0;
short byteIndex = 0;
byte value = 0;
array = operands[0];
byteIndex = operands[1];
value = data_loadByte(array + byteIndex);
storeResult(value);
}
/*
2OP:15 F loadw array word-index -> (result)
Stores array-->word-index (i.e., the word at address array+2*word-index, which must lie in static or dynamic memory).
*/
void z_loadw(void)
{
ushort array = 0;
short wordIndex = 0;
short value = 0;
array = operands[0];
wordIndex = operands[1];
value = data_loadWord(array + 2 * wordIndex);
storeResult(value);
}
/*
VAR:227 3 put_prop object property value