forked from skx/simple.vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-vm-opcodes.c
1168 lines (914 loc) · 27.4 KB
/
simple-vm-opcodes.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
/**
* simple-vm-opcodes.c - Implementation of our opcode-handlers.
*
* Copyright (c) 2014 by Steve Kemp. All rights reserved.
*
**
*
* 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; version 2 dated June, 1991, or (at your
* option) any later version.
*
* On Debian GNU/Linux systems, the complete text of version 2 of the GNU
* General Public License can be found in `/usr/share/common-licenses/GPL-2'
*
**
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "simple-vm.h"
#include "simple-vm-opcodes.h"
/**
* Trivial helper to test registers are not out of bounds.
*/
#define BOUNDS_TEST_REGISTER( r ) { if ( r >= REGISTER_COUNT ) \
{ \
svm_default_error_handler(svm, "Register out of bounds" ); \
} \
}
/**
* Helper to convert a two-byte value to an integer in the range 0x0000-0xffff
*/
#define BYTES_TO_ADDR(one,two) (one + ( 256 * two ))
/**
* This is a macro definition for a "math" operation.
*
* It's a little longer than I'd usually use for a macro, but it saves
* all the typing and redundency defining: add, sub, div, mod, xor, or.
*
*/
#define MATH_OPERATION(function,operator) void function(struct svm * svm) \
{ \
/* get the destination register */ \
unsigned int reg = next_byte(svm); \
BOUNDS_TEST_REGISTER(reg); \
\
/* get the source register */ \
unsigned int src1 = next_byte(svm); \
BOUNDS_TEST_REGISTER(reg); \
\
/* get the source register */\
unsigned int src2 = next_byte(svm);\
BOUNDS_TEST_REGISTER(reg);\
\
if (getenv("DEBUG") != NULL)\
printf( #function "(Register:%d = Register:%d " #operator " Register:%d)\n", reg, src1, src2); \
\
/* if the result-register stores a string .. free it */\
if ((svm->registers[reg].type == STRING) && (svm->registers[reg].content.string))\
free(svm->registers[reg].content.string);\
\
/* \
* Ensure both source registers have integer values.\
*/\
int val1 = get_int_reg(svm, src1);\
int val2 = get_int_reg(svm, src2);\
\
/** \
* Store the result.\
*/\
svm->registers[reg].content.integer = val1 operator val2; \
svm->registers[reg].type = INTEGER; \
\
/**\
* Zero result? \
*/\
if (svm->registers[reg].content.integer == 0)\
svm->flags.z = true;\
else\
svm->flags.z = false;\
\
/* handle the next instruction */ \
svm->ip += 1; \
}
/**
* Foward declarations for code in this module which is not exported.
*/
char *get_string_reg(svm_t * cpu, int reg);
int get_int_reg(svm_t * cpu, int reg);
char *string_from_stack(svm_t * svm);
unsigned char next_byte(svm_t * svm);
/**
* Helper to return the string-content of a register.
*
* NOTE: This function is not exported outside this compilation-unit.
*/
char *get_string_reg(svm_t * cpu, int reg)
{
if (cpu->registers[reg].type == STRING)
return (cpu->registers[reg].content.string);
svm_default_error_handler(cpu, "The register deesn't contain a string");
return NULL;
}
/**
* Helper to return the integer-content of a register.
*
* NOTE: This function is not exported outside this compilation-unit.
*/
int get_int_reg(svm_t * cpu, int reg)
{
if (cpu->registers[reg].type == INTEGER)
return (cpu->registers[reg].content.integer);
svm_default_error_handler(cpu, "The register doesn't contain an integer");
return 0;
}
/**
* Strings are stored inline in the program-RAM.
*
* An example script might contain something like:
*
* store #1, "Steve Kemp"
*
* This is encoded as:
*
* OP_STRING_STORE, REG1, LEN1, LEN2, "String data", ...
*
* Here we assume the IP is pointing to len1 and we read the length, then
* the string, and bump the IP as we go.
*
* The end result should be we've updated the IP to point past the end
* of the string, and we've copied it into newly allocated RAM.
*
* NOTE: This function is not exported outside this compilation-unit.
*/
char *string_from_stack(svm_t * svm)
{
/* the string length */
unsigned int len1 = next_byte(svm);
unsigned int len2 = next_byte(svm);
/* build up the length 0-64k */
int len = BYTES_TO_ADDR(len1, len2);
/* bump IP one more to point to the start of the string-data. */
svm->ip += 1;
/* allocate enough RAM to contain the string. */
char *tmp = (char *) malloc(len + 1);
if (tmp == NULL)
svm_default_error_handler(svm, "RAM allocation failure.");
/**
* Zero the allocated memory, and copy the string-contents over.
*
* The copy is inefficient - but copes with embedded NULL.
*/
memset(tmp, '\0', len + 1);
for (int i = 0; i < (int) len; i++)
{
tmp[i] = svm->code[svm->ip];
svm->ip++;
}
svm->ip--;
return tmp;
}
/**
* Read and return the next byte from the current instruction-pointer.
*
* This function ensures that reading will wrap around the address-space
* of the virtual CPU.
*/
unsigned char next_byte(svm_t * svm)
{
svm->ip += 1;
if (svm->ip >= 0xFFFF)
svm->ip = 0;
return (svm->code[svm->ip]);
}
/**
** Start implementation of virtual machine opcodes.
**
**/
void op_unknown(svm_t * svm)
{
int instruction = svm->code[svm->ip];
printf("%04X - op_unknown(%02X)\n", svm->ip, instruction);
/* handle the next instruction */
svm->ip += 1;
}
/**
* Break out of our main intepretter loop.
*/
void op_exit(struct svm *svm)
{
svm->running = false;
/* handle the next instruction - which won't happen */
svm->ip += 1;
}
/**
* No-operation / NOP.
*/
void op_nop(struct svm *svm)
{
(void) svm;
if (getenv("DEBUG") != NULL)
printf("nop()\n");
/* handle the next instruction */
svm->ip += 1;
}
/**
* Store the contents of one register in another.
*/
void op_reg_store(struct svm *svm)
{
(void) svm;
/* get the destination register */
unsigned int dst = next_byte(svm);
BOUNDS_TEST_REGISTER(dst);
/* get the source register */
unsigned int src = next_byte(svm);
BOUNDS_TEST_REGISTER(src);
if (getenv("DEBUG") != NULL)
printf("STORE(Reg%02x will be set to contents of Reg%02x)\n", dst, src);
/* Free the existing string, if present */
if ((svm->registers[dst].type == STRING) && (svm->registers[dst].content.string))
free(svm->registers[dst].content.string);
/* if storing a string - then use strdup */
if (svm->registers[src].type == STRING)
{
svm->registers[dst].type = STRING;
svm->registers[dst].content.string = strdup(svm->registers[src].content.string);
} else
{
svm->registers[dst].type = svm->registers[src].type;
svm->registers[dst].content.integer = svm->registers[src].content.integer;
}
/* handle the next instruction */
svm->ip += 1;
}
/**
* Store an integer in a register.
*/
void op_int_store(struct svm *svm)
{
/* get the register number to store in */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the value */
unsigned int val1 = next_byte(svm);
unsigned int val2 = next_byte(svm);
int value = BYTES_TO_ADDR(val1, val2);
if (getenv("DEBUG") != NULL)
printf("STORE_INT(Reg:%02x) => %04d [Hex:%04x]\n", reg, value, value);
/* if the register stores a string .. free it */
if ((svm->registers[reg].type == STRING) && (svm->registers[reg].content.string))
free(svm->registers[reg].content.string);
svm->registers[reg].content.integer = value;
svm->registers[reg].type = INTEGER;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Print the integer contents of the given register.
*/
void op_int_print(struct svm *svm)
{
/* get the register number to print */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("INT_PRINT(Register %d)\n", reg);
/* get the register contents. */
int val = get_int_reg(svm, reg);
if (getenv("DEBUG") != NULL)
printf("[STDOUT] Register R%02d => %d [Hex:%04x]\n", reg, val, val);
else
printf("%02X", val);
/* handle the next instruction */
svm->ip += 1;
}
/**
* Convert the integer contents of a register to a string
*/
void op_int_tostring(struct svm *svm)
{
/* get the register number to convert */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("INT_TOSTRING(Register %d)\n", reg);
/* get the contents of the register */
int cur = get_int_reg(svm, reg);
/* allocate a buffer. */
svm->registers[reg].type = STRING;
svm->registers[reg].content.string = malloc(10);
/* store the string-value */
memset(svm->registers[reg].content.string, '\0', 10);
sprintf(svm->registers[reg].content.string, "%d", cur);
/* handle the next instruction */
svm->ip += 1;
}
/**
* Generate a random integer and store in the specified register.
*/
void op_int_random(struct svm *svm)
{
/* get the register to save the output to */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("INT_RANDOM(Register %d)\n", reg);
/**
* If we already have a string in the register delete it.
*/
if ((svm->registers[reg].type == STRING) && (svm->registers[reg].content.string))
{
free(svm->registers[reg].content.string);
}
/* set the value. */
svm->registers[reg].type = INTEGER;
svm->registers[reg].content.integer = rand() % 0xFFFF;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Store a string in a register.
*/
void op_string_store(struct svm *svm)
{
/* get the destination register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the string to store */
char *str = string_from_stack(svm);
/**
* If we already have a string in the register delete it.
*/
if ((svm->registers[reg].type == STRING) && (svm->registers[reg].content.string))
{
free(svm->registers[reg].content.string);
}
/**
* Now store the new string.
*/
svm->registers[reg].type = STRING;
svm->registers[reg].content.string = str;
if (getenv("DEBUG") != NULL)
printf("STRING_STORE(Register %d) = '%s'\n", reg, str);
/* handle the next instruction */
svm->ip += 1;
}
/**
* Print the (string) contents of a register.
*/
void op_string_print(struct svm *svm)
{
/* get the reg number to print */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("STRING_PRINT(Register %d)\n", reg);
/* get the contents of the register */
char *str = get_string_reg(svm, reg);
/* print */
if (getenv("DEBUG") != NULL)
printf("[stdout] register R%02d => %s\n", reg, str);
else
printf("%s", str);
/* handle the next instruction */
svm->ip += 1;
}
/**
* Concatenate two strings, and store the result.
*/
void op_string_concat(struct svm *svm)
{
/* get the destination register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the source register */
unsigned int src1 = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the source register */
unsigned int src2 = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("STRING_CONCAT(Register:%d = Register:%d + Register:%d)\n",
reg, src1, src2);
/*
* Ensure both source registers have string values.
*/
char *str1 = get_string_reg(svm, src1);
char *str2 = get_string_reg(svm, src2);
/**
* Allocate RAM for two strings.
*/
int len = strlen(str1) + strlen(str2) + 1;
/**
* Zero.
*/
char *tmp = malloc(len);
memset(tmp, '\0', len);
/**
* Assign.
*/
sprintf(tmp, "%s%s", str1, str2);
/* if the destination-register currently contains a string .. free it */
if ((svm->registers[reg].type == STRING) && (svm->registers[reg].content.string))
free(svm->registers[reg].content.string);
svm->registers[reg].content.string = tmp;
svm->registers[reg].type = STRING;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Invoke the C system() function against a string register.
*/
void op_string_system(struct svm *svm)
{
/* get the reg */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("STRING_SYSTEM(Register %d)\n", reg);
char *str = get_string_reg(svm, reg);
system(str);
/* handle the next instruction */
svm->ip += 1;
}
/**
* Convert a string to an int.
*/
void op_string_toint(struct svm *svm)
{
/* get the destination register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("STRING_TOINT(Register:%d)\n", reg);
/* get the string and convert to integer */
char *str = get_string_reg(svm, reg);
int i = atoi(str);
/* free the old version */
free(svm->registers[reg].content.string);
/* set the int. */
svm->registers[reg].type = INTEGER;
svm->registers[reg].content.integer = i;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Unconditional jump
*/
void op_jump_to(struct svm *svm)
{
/**
* Read the two bytes which will build up the destination
*/
unsigned int off1 = next_byte(svm);
unsigned int off2 = next_byte(svm);
/**
* Convert to the offset in our code-segment.
*/
int offset = BYTES_TO_ADDR(off1, off2);
if (getenv("DEBUG") != NULL)
printf("JUMP_TO(Offset:%d [Hex:%04X]\n", offset, offset);
svm->ip = offset;
}
/**
* Jump to the given address if the Z-flag is set.
*/
void op_jump_z(struct svm *svm)
{
/**
* Read the two bytes which will build up the destination
*/
unsigned int off1 = next_byte(svm);
unsigned int off2 = next_byte(svm);
/**
* Convert to the offset in our code-segment.
*/
int offset = BYTES_TO_ADDR(off1, off2);
if (getenv("DEBUG") != NULL)
printf("JUMP_Z(Offset:%d [Hex:%04X]\n", offset, offset);
if (svm->flags.z)
{
svm->ip = offset;
} else
{
/* handle the next instruction */
svm->ip += 1;
}
}
/**
* Jump to the given address if the Z flag is NOT set.
*/
void op_jump_nz(struct svm *svm)
{
/**
* Read the two bytes which will build up the destination
*/
unsigned int off1 = next_byte(svm);
unsigned int off2 = next_byte(svm);
/**
* Convert to the offset in our code-segment.
*/
int offset = BYTES_TO_ADDR(off1, off2);
if (getenv("DEBUG") != NULL)
printf("JUMP_NZ(Offset:%d [Hex:%04X]\n", offset, offset);
if (!svm->flags.z)
{
svm->ip = offset;
} else
{
/* handle the next instruction */
svm->ip += 1;
}
}
MATH_OPERATION(op_add, +) // reg_result = reg1 + reg2 ;
MATH_OPERATION(op_and, &) // reg_result = reg1 & reg2 ;
MATH_OPERATION(op_sub, -) // reg_result = reg1 - reg2 ;
MATH_OPERATION(op_mul, *) // reg_result = reg1 * reg2 ;
MATH_OPERATION(op_div, /) // reg_result = reg1 / reg2 ;
MATH_OPERATION(op_xor, ^) // reg_result = reg1 ^ reg2 ;
MATH_OPERATION(op_or, |) // reg_result = reg1 | reg2 ;
/**
* Increment the given (integer) register.
*/
void op_inc(struct svm *svm)
{
/* get the register number to increment */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("INC_OP(Register %d)\n", reg);
/* get, incr, set */
int cur = get_int_reg(svm, reg);
cur += 1;
svm->registers[reg].content.integer = cur;
if (svm->registers[reg].content.integer == 0)
svm->flags.z = true;
else
svm->flags.z = false;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Decrement the given (integer) register.
*/
void op_dec(struct svm *svm)
{
/* get the register number to decrement */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("DEC_OP(Register %d)\n", reg);
/* get, decr, set */
int cur = get_int_reg(svm, reg);
cur -= 1;
svm->registers[reg].content.integer = cur;
if (svm->registers[reg].content.integer == 0)
svm->flags.z = true;
else
svm->flags.z = false;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Compare two registers. Set the Z-flag if equal.
*/
void op_cmp_reg(struct svm *svm)
{
/* get the source register */
unsigned int reg1 = next_byte(svm);
BOUNDS_TEST_REGISTER(reg1);
/* get the source register */
unsigned int reg2 = next_byte(svm);
BOUNDS_TEST_REGISTER(reg2);
if (getenv("DEBUG") != NULL)
printf("CMP(Register:%d vs Register:%d)\n", reg1, reg2);
svm->flags.z = false;
if (svm->registers[reg1].type == svm->registers[reg2].type)
{
if (svm->registers[reg1].type == STRING)
{
if (strcmp
(svm->registers[reg1].content.string,
svm->registers[reg2].content.string) == 0)
svm->flags.z = true;
} else
{
if (svm->registers[reg1].content.integer ==
svm->registers[reg2].content.integer)
svm->flags.z = true;
}
}
/* handle the next instruction */
svm->ip += 1;
}
/**
* Compare a register contents with a constant integer.
*/
void op_cmp_immediate(struct svm *svm)
{
/* get the source register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the integer to compare with */
unsigned int val1 = next_byte(svm);
unsigned int val2 = next_byte(svm);
int val = BYTES_TO_ADDR(val1, val2);
if (getenv("DEBUG") != NULL)
printf("CMP_IMMEDIATE(Register:%d vs %d [Hex:%04X])\n", reg, val, val);
svm->flags.z = false;
int cur = (int) get_int_reg(svm, reg);
if (cur == val)
svm->flags.z = true;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Compare a register contents with the given string.
*/
void op_cmp_string(struct svm *svm)
{
/* get the source register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* Now we get the string to compare against from the stack */
char *str = string_from_stack(svm);
/* get the string content from the register */
char *cur = get_string_reg(svm, reg);
if (getenv("DEBUG") != NULL)
printf("Comparing register-%d ('%s') - with string '%s'\n", reg, cur, str);
/* compare */
if (strcmp(cur, str) == 0)
svm->flags.z = true;
else
svm->flags.z = false;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Does the given register contain a string? Set the Z-flag if so.
*/
void op_is_string(struct svm *svm)
{
/* get the register to test */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("is register %02X a string?\n", reg);
if (svm->registers[reg].type == STRING)
svm->flags.z = true;
else
svm->flags.z = false;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Does the given register contain an integer? Set the Z-flag if so.
*/
void op_is_integer(struct svm *svm)
{
/* get the register to test */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
if (getenv("DEBUG") != NULL)
printf("is register %02X an integer?\n", reg);
if (svm->registers[reg].type == INTEGER)
svm->flags.z = true;
else
svm->flags.z = false;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Read from a given address into the specified register.
*/
void op_peek(struct svm *svm)
{
/* get the destination register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the address to read from the second register */
unsigned int addr = next_byte(svm);
BOUNDS_TEST_REGISTER(addr);
if (getenv("DEBUG") != NULL)
printf
("LOAD_FROM_RAM(Register:%d will contain contents of address %04X)\n",
reg, addr);
/* get the address from the register */
int adr = get_int_reg(svm, addr);
if (adr < 0 || adr > 0xffff)
svm_default_error_handler(svm, "Reading from outside RAM");
/* Read the value from RAM */
int val = svm->code[adr];
/* if the destination currently contains a string .. free it */
if ((svm->registers[reg].type == STRING) && (svm->registers[reg].content.string))
free(svm->registers[reg].content.string);
svm->registers[reg].content.integer = val;
svm->registers[reg].type = INTEGER;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Write a register-contents to memory.
*/
void op_poke(struct svm *svm)
{
/* get the destination register */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* get the address to write to from the second register */
unsigned int addr = next_byte(svm);
BOUNDS_TEST_REGISTER(addr);
/* Get the value we're to store. */
int val = get_int_reg(svm, reg);
/* Get the address we're to store it in. */
int adr = get_int_reg(svm, addr);
if (getenv("DEBUG") != NULL)
printf("STORE_IN_RAM(Address %04X set to %02X)\n", adr, val);
if (adr < 0 || adr > 0xffff)
svm_default_error_handler(svm, "Writing outside RAM");
/* do the necessary */
svm->code[adr] = val;
/* handle the next instruction */
svm->ip += 1;
}
/**
* Copy a chunk of memory.
*/
void op_memcpy(struct svm *svm)
{
/* get the register number to store to */
unsigned int dest_reg = next_byte(svm);
BOUNDS_TEST_REGISTER(dest_reg);
/* get the register number to copy from */
unsigned int src_reg = next_byte(svm);
BOUNDS_TEST_REGISTER(src_reg);
/* get the register number with the size */
unsigned int size_reg = next_byte(svm);
BOUNDS_TEST_REGISTER(size_reg);
/**
* Now handle the copy.
*/
int src = get_int_reg(svm, src_reg);
int dest = get_int_reg(svm, dest_reg);
int size = get_int_reg(svm, size_reg);
if (getenv("DEBUG") != NULL)
{
printf("Copying %4x bytes from %04x to %04X\n", size, src, dest);
}
/** Slow, but copes with nulls and allows debugging. */
for (int i = 0; i < size; i++)
{
svm->code[dest + i] = svm->code[src + i];
}
/* handle the next instruction */
svm->ip += 1;
}
/**
* Push the contents of a given register onto the stack.
*/
void op_stack_push(struct svm *svm)
{
/* get the register to push */
unsigned int reg = next_byte(svm);
BOUNDS_TEST_REGISTER(reg);
/* Get the value we're to store. */
int val = get_int_reg(svm, reg);
if (getenv("DEBUG") != NULL)
printf("PUSH(Register %d [=%04x])\n", reg, val);
/* store it */
svm->SP += 1;
svm->stack[svm->SP] = val;
/**
* Ensure the stack hasn't overflown.
*/
int sp_size = sizeof(svm->stack) / sizeof(svm->stack[0]);
if (svm->SP > sp_size)
svm_default_error_handler(svm, "stack overflow - stack is full");
/* handle the next instruction */
svm->ip += 1;