-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
1628 lines (1560 loc) · 43 KB
/
interpreter.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 <stdio.h>
const char *compressed_disk=
"AArrO5Bta2Rvc2ZzgAAAAgADAgEBgAAAAQACAuCAAAABAARAC/AJgAAAAQABEoAAAAEAAQKAAAAP"
"ACQgICAgICAgICAgIEZBVDEyICAgMcCM2L5YfIoEhMB0CrQOuw+AAAABABfNEEbr8Pr06/1oZWxs"
"bywgd29ybGQNCoAAAZgABVWq8P//gAAR/QAD8P//gBZr/Q=="
;
/*
This interpreter is released under the MIT License.
Copyright (c) 2015 MikeCAT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
int base64_char2id(char c) {
static const char *table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i;
for(i=0;table[i]!='\0';i++) {
if(table[i]==c)return i;
}
return -1;
}
int base64_get_next_char(void) {
static int pos=0;
for(;;) {
char input=compressed_disk[pos];
int id;
if(input=='\0')return -1;
pos++;
id=base64_char2id(input);
if(id>=0)return id;
}
}
int base64_get_next_byte(void) {
static int buffer;
static int buffer_pos=0;
int ret=-1;
int next=base64_get_next_char();
if(next<0)return -1;
if(buffer_pos==0) {
ret=(next<<2)&0xfc;
if((next=base64_get_next_char())<0)return -1;
ret|=(next>>4)&0x03;
buffer=(next<<4)&0xf0;
buffer_pos=1;
} else if(buffer_pos==1) {
ret=buffer|((next>>2)&0x0f);
buffer=(next<<6)&0xc0;
buffer_pos=2;
} else if(buffer_pos==2) {
ret=buffer|(next&0x3f);
buffer_pos=0;
}
return ret;
}
unsigned char disk[512*18*80*2];
void decompress_disk(void) {
int input;
int num,j;
int out_pos=0;
while((input=base64_get_next_byte())>=0) {
if(input & 0x80) {
num=input&0x7f;
num=(num<<8)|base64_get_next_byte();
num=(num<<8)|base64_get_next_byte();
num=(num<<8)|base64_get_next_byte();
for(j=0;j<num;j++)disk[out_pos++]=0;
} else {
num=input&0x7f;
num=(num<<8)|base64_get_next_byte();
for(j=0;j<num;j++)disk[out_pos++]=base64_get_next_byte();
}
}
}
int read_disk(unsigned char *out, int head, int cylinder, int sector) {
if(0<=head && head<2 && 0<=cylinder && cylinder<80 && 1<=sector && sector<=18) {
unsigned char *begin=disk+512*(18*(2*cylinder+head)+(sector-1));
int i;
for(i=0;i<512;i++)out[i]=begin[i];
return 1;
} else {
return 0;
}
}
#define RAM_SIZE 0xa0000
unsigned char RAM[RAM_SIZE];
unsigned int REGS[12];
unsigned int IP;
unsigned int FLAGS;
#define CF_BIT 0x0001
#define PF_BIT 0x0004
#define AF_BIT 0x0010
#define ZF_BIT 0x0040
#define SF_BIT 0x0080
#define TF_BIT 0x0100
#define IF_BIT 0x0200
#define DF_BIT 0x0400
#define OF_BIT 0x0800
#define AX REGS[0]
#define CX REGS[1]
#define DX REGS[2]
#define BX REGS[3]
#define SP REGS[4]
#define BP REGS[5]
#define SI REGS[6]
#define DI REGS[7]
#define ES REGS[8]
#define CS REGS[9]
#define SS REGS[10]
#define DS REGS[11]
typedef struct {
enum {
TYPE_8BIT_MEM,
TYPE_8BIT_REG,
TYPE_16BIT_MEM,
TYPE_16BIT_REG
} type;
int index;
} storage_t;
int read_data(storage_t where) {
int i=where.index;
int t=where.type;
int ret=0;
if(t==TYPE_8BIT_MEM) {
if(0<=i && i<RAM_SIZE)ret=RAM[i]; else ret=0xff;
} else if(t==TYPE_8BIT_REG) {
if(0<=i && i<4)ret=REGS[i] & 0xff;
else if(4<=i && i<8)ret=(REGS[i-4]>>8) & 0xff;
} else if(t==TYPE_16BIT_MEM) {
if(0<=i && i+1<RAM_SIZE)ret=RAM[i]|(RAM[i+1]<<8);
else if(i==RAM_SIZE-1)ret=RAM[i]|(0xff<<8);
else ret=0xffff;
} else if(t==TYPE_16BIT_REG) {
if(0<=i && i<12)ret=REGS[i] & 0xffff;
}
return ret;
}
void write_data(storage_t where,int data) {
int i=where.index;
int t=where.type;
if(t==TYPE_8BIT_MEM) {
if(0<=i && i<RAM_SIZE)RAM[i]=data;
} else if(t==TYPE_8BIT_REG) {
if(0<=i && i<4)REGS[i]=(REGS[i]&0xff00)|(data&0xff);
else if(4<=i && i<8)REGS[i-4]=(REGS[i-4]&0x00ff)|((data&0xff)<<8);
} else if(t==TYPE_16BIT_MEM) {
if(0<=i && i<RAM_SIZE)RAM[i]=data&0xff;
if(i+1<RAM_SIZE)RAM[i+1]=(data>>8)&0xff;
} else if(t==TYPE_16BIT_REG) {
if(0<=i && i<12)REGS[i]=data&0xffff;
}
}
int fetch_inst(int offset) {
storage_t pos;
pos.type=TYPE_8BIT_MEM;
pos.index=((CS<<4)+((IP+offset)&0xffff))&0xfffff;
return read_data(pos);
}
int read_stack(int offset) {
storage_t pos;
pos.type=TYPE_16BIT_MEM;
pos.index=((SS<<4)+((SP+offset)&0xffff))&0xfffff;
return read_data(pos);
}
void write_stack(int offset,int data) {
storage_t pos;
pos.type=TYPE_16BIT_MEM;
pos.index=((SS<<4)+((SP+offset)&0xffff))&0xfffff;
write_data(pos,data);
}
int run_in(int port,int is_word) {
printf("unimplemented IN port=0x%04X is_word=%d at CS=%04X IP=%04X\n",port,is_word,CS,IP);
return 0;
}
int run_out(int port,int is_word) {
printf("unimplemented OUT port=0x%04X is_word=%d at CS=%04X IP=%04X\n",port,is_word,CS,IP);
return 0;
}
int run_int(int type) {
int int_cs,int_ip;
storage_t reg;
reg.type=TYPE_16BIT_MEM;
reg.index=4*type;
int_ip=read_data(reg);
reg.index+=2;
int_cs=read_data(reg);
if(int_ip!=0 || int_cs!=0) {
write_stack(-2,FLAGS);
write_stack(-4,CS);
write_stack(-6,IP);
SP=(SP-6)&0xffff;
CS=int_cs;
IP=int_ip;
return 1;
} else {
int service;
if(type==0x00) {
printf("#DE (int 0x00) detected at CS=%04X, IP=%04X\n",CS,IP);
return 0;
} else if(type==0x06) {
printf("#UD (int 0x06) detected at CS=%04X, IP=%04X\n",CS,IP);
return 0;
} else if(type==0x10) {
reg.type=TYPE_8BIT_REG;
reg.index=4;
service=read_data(reg);
if(service==0x0e) { /* write a character */
static int is_prev_cr=0;
int data;
reg.index=0;
data=read_data(reg);
if(data==0x0d) {
putchar('\n');
is_prev_cr=1;
} else if(data==0x0a) {
if(!is_prev_cr)putchar('\n');
is_prev_cr=0;
} else {
putchar(data);
is_prev_cr=0;
}
return 1;
}
} else if(type==0x11) { /* equipment determination */
/* see HTTP stanislavs.org/helppc/int_11.html */
AX=0x0131;
return 1;
} else if(type==0x12) { /* get low memory size */
AX=RAM_SIZE/1024-1;
return 1;
} else if(type==0x13) {
reg.type=TYPE_8BIT_REG;
reg.index=4;
service=read_data(reg);
reg.index=2;
if(read_data(reg)!=0x00) {
/* invalid disk number*/
reg.index=4;
write_data(reg,0x01);
FLAGS|=CF_BIT;
return 1;
} else {
if(service==0x00) { /* reset disk */
FLAGS&=~CF_BIT;
return 1;
} else if(service==0x02) { /* read disk */
int sector_num,cylinder_idx,sector_idx,head_idx;
reg.index=0;
sector_num=read_data(reg);
reg.index=5;
cylinder_idx=read_data(reg);
reg.index=1;
cylinder_idx|=(read_data(reg)&0xc0)<<2;
sector_idx=read_data(reg)&0x3f;
reg.index=6;
head_idx=read_data(reg);
if(sector_num!=1) {
/* multi-secctor access is not supported */
reg.index=4;
write_data(reg,0x01);
FLAGS|=CF_BIT;
} else {
unsigned char buf[512];
if(read_disk(buf,head_idx,cylinder_idx,sector_idx)) {
int i;
reg.type=TYPE_8BIT_MEM;
for(i=0;i<512;i++) {
reg.index=(ES<<4)+((BX+i)&0xffff);
write_data(reg,buf[i]);
}
reg.type=TYPE_8BIT_REG;
reg.index=4;
write_data(reg,0x00);
FLAGS&=~CF_BIT;
} else {
reg.index=4;
write_data(reg,0x04);
FLAGS|=CF_BIT;
}
}
return 1;
}
}
} else if(type==0x16) {
reg.type=TYPE_8BIT_REG;
reg.index=4;
service=read_data(reg);
if(service==0x00) { /* read a char */
/* scan code not supported (return save value as the ASCII code) */
int input=getchar()&0xff;
write_data(reg,input);
reg.index=0;
write_data(reg,input);
return 1;
} else if(service==0x01) { /* check the buffer */
/* not supported well */
int input=getchar(); /* assume that there are some characters */
ungetc(input,stdin);
write_data(reg,input);
reg.index=0;
write_data(reg,input);
FLAGS&=~ZF_BIT; /* return that there are some characters */
return 1;
}
}
printf("unimplemented INT type=0x%02X at CS=%04X, IP=%04X\n",type,CS,IP);
return 0;
}
}
void vm_init(void) {
read_disk(&RAM[0x7c00],0,0,1);
AX=0xaa55;SP=0x7c00;
ES=CS=SS=DS=0x0000;
IP=0x7c00;
FLAGS=2;
}
typedef int (*p_calc_func)(int,int,int);
int mov_func(int src,int dest,int is_word) {
(void)dest;
(void)is_word;
return src;
}
int get_signed_value(int value,int is_word) {
if(is_word && (value&0x8000)!=0) {
return -(((~value)+1)&0xffff);
} else if(!is_word && (value&0x80)!=0) {
return -(((~value)+1)&0xff);
} else {
return value;
}
}
int cut_value(int value,int is_word) {
if(is_word) {
return value&0xffff;
} else {
return value&0xff;
}
}
void set_sf(int value,int is_word) {
if((is_word && (value&0x8000)!=0) || (!is_word && (value&0x80)!=0))FLAGS|=SF_BIT; else FLAGS&=~SF_BIT;
}
void set_zf(int value) {
if(value==0)FLAGS|=ZF_BIT; else FLAGS&=~ZF_BIT;
}
void set_pf(int value) {
int count=value&0xff;
count=((count&0xaa)>>1)+(count&0x55);
count=((count&0xcc)>>2)+(count&0x33);
count=((count&0xf0)>>4)+(count&0x0f);
if(count%2==0)FLAGS|=PF_BIT; else FLAGS&=~PF_BIT;
}
int addition_func(int src,int dest,int is_word,int do_set_cf,int do_add_carry) {
int ret=dest+src;
int cut_ret;
int ret2=get_signed_value(dest,is_word)+get_signed_value(src,is_word);
if(do_add_carry && (FLAGS&CF_BIT)!=0) {
ret++;
ret2++;
}
cut_ret=cut_value(ret,is_word);
if((is_word && (ret2<-0x8000 || 0x8000<=ret2)) || (!is_word && (ret2<-0x80 || 0x80<=ret2))) {
FLAGS|=OF_BIT;
} else {
FLAGS&=~OF_BIT;
}
set_sf(cut_ret,is_word);
set_zf(cut_ret);
if((dest&0xf)+(src&0xf)>0xf)FLAGS|=AF_BIT; else FLAGS&=~AF_BIT;
set_pf(ret);
if(do_set_cf) {
if((is_word && ret>0xffff) || (!is_word && ret>0xff))FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
}
return cut_ret;
}
int subtract_func(int src,int dest,int is_word,int do_set_cf,int do_sub_carry) {
int src_cmp,ret,cut_ret,ret2;
if(is_word)src_cmp=((~src)&0xffff)+1; else src_cmp=((~src)&0xff)+1;
ret=dest+src_cmp;
ret2=get_signed_value(dest,is_word)-get_signed_value(src,is_word);
if(do_sub_carry && (FLAGS&CF_BIT)!=0) {
ret--;
ret2--;
}
cut_ret=cut_value(ret,is_word);
if((is_word && (ret2<-0x8000 || 0x8000<=ret2)) || (!is_word && (ret2<-0x80 || 0x80<=ret2))) {
FLAGS|=OF_BIT;
} else {
FLAGS&=~OF_BIT;
}
set_sf(cut_ret,is_word);
set_zf(cut_ret);
if((dest&0xf)+(((~src)&0xf)+1)>0xf)FLAGS&=~AF_BIT; else FLAGS|=AF_BIT;
set_pf(ret);
if(do_set_cf) {
if((is_word && ret>0xffff) || (!is_word && ret>0xff))FLAGS&=~CF_BIT; else FLAGS|=CF_BIT;
}
return cut_ret;
}
int add_func(int src,int dest,int is_word) {
return addition_func(src,dest,is_word,1,0);
}
int or_func(int src,int dest,int is_word) {
int ret=src|dest;
(void)is_word;
FLAGS&=~(OF_BIT | CF_BIT);
set_sf(ret,is_word);
set_zf(ret);
set_pf(ret);
return ret;
}
int adc_func(int src,int dest,int is_word) {
return addition_func(src,dest,is_word,1,1);
}
int ssb_func(int src,int dest,int is_word) {
return subtract_func(src,dest,is_word,1,1);
}
int and_func(int src,int dest,int is_word) {
int ret=src&dest;
(void)is_word;
FLAGS&=~(OF_BIT | CF_BIT);
set_sf(ret,is_word);
set_zf(ret);
set_pf(ret);
return ret;
}
int sub_func(int src,int dest,int is_word) {
return subtract_func(src,dest,is_word,1,0);
}
int sub_func_for_scas(int src,int dest,int is_word) {
return subtract_func(dest,src,is_word,1,0);
}
int xor_func(int src,int dest,int is_word) {
int ret=src^dest;
(void)is_word;
FLAGS&=~(OF_BIT | CF_BIT);
set_sf(ret,is_word);
set_zf(ret);
set_pf(ret);
return ret;
}
int not_func(int src,int dest,int is_word) {
(void)src;
return cut_value(~dest,is_word);
}
int neg_func(int src,int dest,int is_word) {
(void)src;
return subtract_func(dest,0,is_word,1,0);
}
int rol_func(int src,int dest,int is_word) {
int ret=dest;
int last_value=0;
int i;
if(src!=0) {
for(i=0;i<src;i++) {
if(is_word) {
ret=((ret<<1)|(ret>>15))&0xffff;
} else {
ret=((ret<<1)|(ret>>7))&0xff;
}
last_value=ret&1;
}
if(src==1) {
if((is_word && (ret&0x8000)!=0) || (!is_word && (ret&0x80)!=0)) {
if(last_value)FLAGS&=~OF_BIT; else FLAGS|=OF_BIT;
} else {
if(last_value)FLAGS|=OF_BIT; else FLAGS&=~OF_BIT;
}
}
if(last_value)FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
}
return ret;
}
int ror_func(int src,int dest,int is_word) {
int ret=dest;
int last_value=0;
int i;
if(src!=0) {
for(i=0;i<src;i++) {
last_value=ret&1;
if(is_word) {
ret=((ret>>1)|(ret<<15))&0xffff;
} else {
ret=((ret>>1)|(ret<<7))&0xff;
}
}
if(src==1) {
if((is_word && ((ret&0xc000)==0xc000 || (ret&0xc000)==0x0000)) ||
(!is_word && ((ret&0xc0)==0xc0 || (ret&0xc0)==0x00))) {
FLAGS&=~OF_BIT;
} else {
FLAGS|=OF_BIT;
}
}
if(last_value)FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
}
return ret;
}
int rcl_func(int src,int dest,int is_word) {
int ret=dest;
int last_value;
int i;
if(FLAGS&CF_BIT)last_value=1; else last_value=0;
if(src!=0) {
for(i=0;i<src;i++) {
if(is_word) {
ret=(ret<<1)|last_value;
last_value=(ret>>16)&1;
ret&=0xffff;
} else {
ret=(ret<<1)|last_value;
last_value=(ret>>8)&1;
ret&=0xff;
}
}
if(src==1) {
if((is_word && (ret&0x8000)!=0) || (!is_word && (ret&0x80)!=0)) {
if(last_value)FLAGS&=~OF_BIT; else FLAGS|=OF_BIT;
} else {
if(last_value)FLAGS|=OF_BIT; else FLAGS&=~OF_BIT;
}
}
if(last_value)FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
}
return ret;
}
int rcr_func(int src,int dest,int is_word) {
int ret=dest;
int last_value;
int i;
if(FLAGS&CF_BIT)last_value=1; else last_value=0;
if(src!=0) {
for(i=0;i<src;i++) {
int next_last_value=ret&1;
if(is_word) {
ret=((ret>>1)|(last_value<<15))&0xffff;
} else {
ret=((ret>>1)|(last_value<<7))&0xff;
}
last_value=next_last_value;
}
if(src==1) {
if((is_word && ((ret&0xc000)==0xc000 || (ret&0xc000)==0x0000)) ||
(!is_word && ((ret&0xc0)==0xc0 || (ret&0xc0)==0x00))) {
FLAGS&=~OF_BIT;
} else {
FLAGS|=OF_BIT;
}
}
if(last_value)FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
}
return ret;
}
int shl_func(int src,int dest,int is_word) {
int ret=dest<<(src&0x1f);
int cut_ret=cut_value(ret,is_word);
if(src!=0) {
if(src==1) {
if((is_word && ((src&0xc000) == 0xc000 || (src&0xc000) == 0x0000)) ||
(!is_word && ((src&0xc0) == 0xc0 || (src&0x0) == 0x00)))FLAGS&=~OF_BIT;
else FLAGS|=OF_BIT;
}
set_sf(cut_ret,is_word);
set_zf(cut_ret);
set_pf(cut_ret);
}
return cut_ret;
}
int shr_func(int src,int dest,int is_word) {
int ret=dest>>(src&0x1f);
if(src!=0) {
if(src==1) {
if((is_word && (src&0x8000)!=0) || (!is_word && (src&0x80)!=0))FLAGS|=OF_BIT; else FLAGS&=~OF_BIT;
}
if((dest>>((src&0x1f)-1))&1)FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
set_sf(ret,is_word);
set_zf(ret);
set_pf(ret);
}
return ret;
}
int sar_func(int src,int dest,int is_word) {
int ret=dest;
int i;
int last_data=0;
for(i=0;i<(src&0x1f);i++) {
last_data=ret&1;
ret>>=1;
if(is_word) {
if(dest&0x8000)ret|=0x8000;
} else {
if(dest&0x80)ret|=0x80;
}
}
if(src!=0) {
if(src==1)FLAGS&=~OF_BIT;
if(last_data)FLAGS|=CF_BIT; else FLAGS&=~CF_BIT;
set_sf(ret,is_word);
set_zf(ret);
set_pf(ret);
}
return ret;
}
int inc_func(int src,int dest,int is_word) {
(void)src;
return addition_func(1,dest,is_word,0,0);
}
int dec_func(int src,int dest,int is_word) {
(void)src;
return subtract_func(1,dest,is_word,0,0);
}
/* segment<0 -> default, segment>=4 -> no segment(for LEA) */
storage_t fetch_mod_rm(int *size,int offset,int segment,int w) {
storage_t ret;
int mod_rm=fetch_inst(offset);
int mod=(mod_rm>>6)&3;
int rm=mod_rm&7;
int disp=0;
if(mod==0) {
if(rm==6) {
*size=3;
disp=fetch_inst(offset+1)|(fetch_inst(offset+2)<<8);
} else {
*size=1;
disp=0;
}
} else if(mod==1) {
*size=2;
disp=fetch_inst(offset+1);
if(disp&0x80)disp|=0xff00;
} else if(mod==2) {
*size=3;
disp=fetch_inst(offset+1)|(fetch_inst(offset+2)<<8);
} else {
*size=1;
if(w)ret.type=TYPE_16BIT_REG; else ret.type=TYPE_8BIT_REG;
ret.index=rm;
return ret;
}
if(w)ret.type=TYPE_16BIT_MEM; else ret.type=TYPE_8BIT_MEM;
if(rm==0)ret.index=BX+SI+disp;
else if(rm==1)ret.index=BX+DI+disp;
else if(rm==2)ret.index=BP+SI+disp;
else if(rm==3)ret.index=BP+DI+disp;
else if(rm==4)ret.index=SI+disp;
else if(rm==5)ret.index=DI+disp;
else if(rm==6) {
if(mod==0)ret.index=disp; else ret.index=BP+disp;
} else ret.index=BX+disp;
ret.index&=0xffff;
if(0<=segment && segment<4) {
ret.index+=(REGS[8+segment]<<4);
} else if(segment<0) {
if(rm==2 || rm==3 || (rm==6 && mod!=0)) {
ret.index+=(SS<<4);
} else {
ret.index+=(DS<<4);
}
}
return ret;
}
storage_t fetch_reg(int offset,int w) {
storage_t reg;
if(w)reg.type=TYPE_16BIT_REG; else reg.type=TYPE_8BIT_REG;
reg.index=(fetch_inst(offset)>>3)&7;
return reg;
}
void do_calc_rm_r(p_calc_func func,int segment,int is_test) {
int inst=fetch_inst(-1);
int w=inst&1;
int modrm_size=1;
storage_t rm=fetch_mod_rm(&modrm_size,0,segment,w);
storage_t reg=fetch_reg(0,w);
int calc_res;
IP=(IP+modrm_size)&0xffff;
if(inst&2) {
calc_res=func(read_data(rm),read_data(reg),w);
if(!is_test)write_data(reg,calc_res);
} else {
calc_res=func(read_data(reg),read_data(rm),w);
if(!is_test)write_data(rm,calc_res);
}
}
void do_calc_imm_rm(p_calc_func func,int segment,int is_test,int enable_s) {
int inst=fetch_inst(-1);
int w=inst&1;
int modrm_size=1;
storage_t rm=fetch_mod_rm(&modrm_size,0,segment,w);
int calc_res;
int imm;
IP=(IP+modrm_size)&0xffff;
imm=fetch_inst(0);
IP=(IP+1)&0xffff;
if(w) {
if(enable_s && (inst&2)!=0) {
if(imm&0x80)imm|=0xff00;
} else {
imm|=fetch_inst(0)<<8;
IP=(IP+1)&0xffff;
}
}
calc_res=func(imm,read_data(rm),w);
if(!is_test)write_data(rm,calc_res);
}
void do_calc_imm_acc(p_calc_func func,int is_test) {
int w=fetch_inst(-1)&1;
storage_t acc;
int calc_res;
int imm;
if(w)acc.type=TYPE_16BIT_REG; else acc.type=TYPE_8BIT_REG;
acc.index=0;
imm=fetch_inst(0);
IP=(IP+1)&0xffff;
if(w) {
imm|=fetch_inst(0)<<8;
IP=(IP+1)&0xffff;
}
calc_res=func(imm,read_data(acc),w);
if(!is_test)write_data(acc,calc_res);
}
void do_logic(p_calc_func func,int segment) {
int inst=fetch_inst(-1);
int w=inst&1;
int v=inst&2;
int modrm_size=1;
int src;
storage_t rm=fetch_mod_rm(&modrm_size,0,segment,w);
storage_t reg;
IP=(IP+modrm_size)&0xffff;
reg.type=TYPE_8BIT_REG;
reg.index=1;
if(v)src=read_data(reg); else src=1;
write_data(rm,func(src,read_data(rm),w));
}
/* useax_flag&1 -> use AX instead of [SI], useax_flag&2 -> use AX instead of [DI] */
/* ignore ZF if zf_for_continue<0 */
void do_string(p_calc_func func,int rep_flag,int zf_for_continue,int useax_flag,int segment,int is_test,int is_word) {
int use_segment=segment;
if(use_segment<0)use_segment=3;
while(CX!=0 || !rep_flag) {
/* execute the instruction */
storage_t src,dest;
int calc_ret;
int diff;
if(is_word) {
if(useax_flag&1)src.type=TYPE_16BIT_REG; else src.type=TYPE_16BIT_MEM;
if(useax_flag&2)dest.type=TYPE_16BIT_REG; else dest.type=TYPE_16BIT_MEM;
} else {
if(useax_flag&1)src.type=TYPE_8BIT_REG; else src.type=TYPE_8BIT_MEM;
if(useax_flag&2)dest.type=TYPE_8BIT_REG; else dest.type=TYPE_8BIT_MEM;
}
if(useax_flag&1)src.index=0; else src.index=(REGS[8+use_segment]<<4)+SI;
if(useax_flag&2)dest.index=0; else dest.index=(ES<<4)+DI;
calc_ret=func(read_data(src),read_data(dest),is_word);
if(!is_test)write_data(dest,calc_ret);
/* move the pointer */
if(is_word)diff=2; else diff=1;
if(FLAGS&DF_BIT) {
if((useax_flag&1)==0)SI=(SI-diff)&0xffff;
if((useax_flag&2)==0)DI=(DI-diff)&0xffff;
} else {
if((useax_flag&1)==0)SI=(SI+diff)&0xffff;
if((useax_flag&2)==0)DI=(DI+diff)&0xffff;
}
if(!rep_flag)break;
/* check the condition */
CX=(CX-1)&0xffff;
if(CX==0)break;
if(zf_for_continue>=0 && ((zf_for_continue && (FLAGS&ZF_BIT)==0) || (!zf_for_continue && (FLAGS&ZF_BIT)!=0)))break;
}
}
void do_jcc(int condition) {
int disp=fetch_inst(0);
IP=(IP+1)&0xffff;
if(disp&0x80)disp|=0xff00;
if(condition)IP=(IP+disp)&0xffff;
}
void print_unimplemented(const char *name,int print_second_byte) {
if(name==NULL) {
if(print_second_byte) {
printf("unimplemented instruction %02X /%d at CS=%04X, IP=%04X\n",fetch_inst(-1),(fetch_inst(0)>>3)&7,CS,(IP-1)&0xffff);
} else {
printf("unimplemented instruction %02X at CS=%04X, IP=%04X\n",fetch_inst(-1),CS,(IP-1)&0xffff);
}
} else {
if(print_second_byte) {
printf("unimplemented instruction %s(%02X %02X) at CS=%04X, IP=%04X\n",name,fetch_inst(-1),fetch_inst(0),CS,(IP-1)&0xffff);
} else {
printf("unimplemented instruction %s(%02X) at CS=%04X, IP=%04X\n",name,fetch_inst(-1),CS,(IP-1)&0xffff);
}
}
}
/* based on http://www.datasheetarchive.com/dlmain/Datasheets-14/DSA-279540.pdf */
int vm_step(void) {
int segment=-1;
int rep_flag=0;
int rep_zf_for_continue=0;
int inst=fetch_inst(0);
while((inst&0xe7)==0x26 || (inst&0xfe)==0xf2) {
if((inst&0xe7)==0x26) { /* segment override prefix */
segment=(inst>>3)&3;
} else { /* REP */
rep_flag=1;
rep_zf_for_continue=inst&1;
}
IP=(IP+1)&0xffff;
inst=fetch_inst(0);
}
IP=(IP+1)&0xffff;
if((inst&0xfc)==0x88) { /* MOV Register/Memory to/from Register */
do_calc_rm_r(mov_func,segment,0);
} else if((inst&0xfe)==0xc6) { /* MOV Immediate to Register/Memory */
if(((fetch_inst(0)>>3)&7)==0) {
do_calc_imm_rm(mov_func,segment,0,0);
} else {
print_unimplemented(NULL,1);
return 0;
}
} else if((inst&0xf0)==0xb0) { /* MOV Immediate to Register */
storage_t reg;
int imm=fetch_inst(0);
reg.index=inst&7;
IP=(IP+1)&0xffff;
if(inst&0x08) {
reg.type=TYPE_16BIT_REG;
imm|=fetch_inst(0)<<8;
IP=(IP+1)&0xffff;
} else {
reg.type=TYPE_8BIT_REG;
}
write_data(reg,imm);
} else if((inst&0xfc)==0xa0) {
storage_t mem;
storage_t acc;
mem.index=fetch_inst(0)|(fetch_inst(1)<<8);
acc.index=0;
if(0<=segment && segment<4) {
mem.index+=REGS[8+segment]<<4;
} else {
mem.index+=DS<<4;
}
IP=(IP+2)&0xffff;
if(inst&0x01) {
mem.type=TYPE_16BIT_MEM;
acc.type=TYPE_16BIT_REG;
} else {
mem.type=TYPE_8BIT_MEM;
acc.type=TYPE_8BIT_REG;
}
if((inst&0xfe)==0xa0) { /* MOV Memory to Accumulator */
write_data(acc,read_data(mem));
} else { /* MOV Accumulator to Memory */
write_data(mem,read_data(acc));
}
} else if((inst&0xfd)==0x8c) {
if(((fetch_inst(0)>>5)&1)==0) {
int modrm_size=1;
storage_t mem=fetch_mod_rm(&modrm_size,0,segment,1);
storage_t reg;
reg.type=TYPE_16BIT_REG;
reg.index=8+((fetch_inst(0)>>3)&3);
IP=(IP+modrm_size)&0xffff;
if(inst==0x8e) { /* MOV Register/Memory to Segment Register */
write_data(reg,read_data(mem));
} else { /* MOV Segment Register to Register/Memory */
write_data(mem,read_data(reg));
}
} else {
print_unimplemented(NULL,1);
return 0;
}
} else if((inst&0xf8)==0x50) { /* PUSH Register */
storage_t mem;
mem.type=TYPE_16BIT_REG;
mem.index=inst&7;
SP=(SP-2)&0xffff;
write_stack(0,read_data(mem));
} else if((inst&0xe7)==0x06) { /* PUSH Segment Register */
storage_t mem;
mem.type=TYPE_16BIT_REG;
mem.index=8+((inst>>3)&3);
SP=(SP-2)&0xffff;
write_stack(0,read_data(mem));
} else if(inst==0x8f) { /* POP Register/Memory */
if(((fetch_inst(0)>>3)&7)==0) {
int modrm_size=1;
storage_t mem=fetch_mod_rm(&modrm_size,0,segment,1);
write_data(mem,read_stack(0));
SP=(SP+2)&0xffff;
IP=(IP+modrm_size)&0xffff;
} else {
print_unimplemented(NULL,1);
return 0;
}
} else if((inst&0xf8)==0x58) { /* POP Register */
storage_t mem;
mem.type=TYPE_16BIT_REG;
mem.index=inst&7;
write_data(mem,read_stack(0));
SP=(SP+2)&0xffff;
} else if((inst&0xe7)==0x07) { /* POP Segment Register */
storage_t mem;
mem.type=TYPE_16BIT_REG;
mem.index=8+((inst>>3)&3);
write_data(mem,read_stack(0));
SP=(SP+2)&0xffff;
} else if((inst&0xfe)==0x86) { /* XCHG Register/Memory with Register */
int modrm_size=1;
storage_t rm=fetch_mod_rm(&modrm_size,0,segment,inst&1);
storage_t reg=fetch_reg(0,inst&1);
int temp;
IP=(IP+modrm_size)&0xffff;
temp=read_data(rm);
write_data(rm,read_data(reg));
write_data(reg,temp);
} else if((inst&0xf8)==0x90) { /* XCHG Register with Accumulator */
storage_t acc;
storage_t reg;
int temp;
acc.type=reg.type=TYPE_16BIT_REG;
acc.index=0;
reg.index=inst&7;
temp=read_data(acc);