-
Notifications
You must be signed in to change notification settings - Fork 217
/
808x.c
3992 lines (3839 loc) · 177 KB
/
808x.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
// 1B64 - Vid_SetMode (Vid_Vesa.c)
// 6689c - CONS_Printf
/*SHR AX,1
4 clocks - fetch opcode
4 clocks - fetch mod/rm
2 clocks - execute 2 clocks - fetch opcode 1
2 clocks - fetch opcode 2
4 clocks - fetch mod/rm
2 clocks - fetch opcode 1 2 clocks - execute
2 clocks - fetch opcode 2 etc*/
#if defined(__linux__) || defined(__APPLE__)
#include <unistd.h>
#endif
#include <stdio.h>
#include "ibm.h"
#include "x86_ops.h"
#include "codegen.h"
#include "cpu.h"
#include "keyboard.h"
#include "mem.h"
#include "nmi.h"
#include "pic.h"
#include "timer.h"
#include "x86.h"
#include "x87.h"
#include "paths.h"
uint64_t xt_cpu_multi;
int nmi = 0;
int nmi_auto_clear = 0;
int oldcpl;
int tempc;
static int noint = 0;
int output = 0;
int timetolive = 0;
int ins = 0;
int is8086 = 0;
static uint32_t oldds;
uint32_t oldss;
static int nextcyc = 0;
static int memcycs;
static int cycdiff;
static void FETCHCOMPLETE();
#define IRQTEST ((cpu_state.flags & I_FLAG) && (pic.pend & ~pic.mask) && !noint)
static uint8_t readmemb(uint32_t a) {
if (a != (cs + cpu_state.pc))
memcycs += 4;
if (readlookup2[(a) >> 12] == -1)
return readmembl(a);
else
return *(uint8_t *)(readlookup2[(a) >> 12] + (a));
}
static uint8_t readmembf(uint32_t a) {
if (readlookup2[(a) >> 12] == -1)
return readmembl(a);
else
return *(uint8_t *)(readlookup2[(a) >> 12] + (a));
}
static uint16_t readmemw(uint32_t s, uint16_t a) {
if (a != (cs + cpu_state.pc))
memcycs += (8 >> is8086);
if ((readlookup2[((s) + (a)) >> 12] == -1 || (s) == 0xFFFFFFFF))
return readmemwl(s + a);
else
return *(uint16_t *)(readlookup2[(s + a) >> 12] + s + a);
}
void refreshread() { /*pclog("Refreshread\n"); */
FETCHCOMPLETE();
memcycs += 4;
}
#undef fetchea
#define fetchea() \
{ \
rmdat = FETCH(); \
cpu_reg = (rmdat >> 3) & 7; \
cpu_mod = (rmdat >> 6) & 3; \
cpu_rm = rmdat & 7; \
if (cpu_mod != 3) \
fetcheal(); \
}
static void writememb(uint32_t a, uint8_t v) {
memcycs += 4;
if (writelookup2[(a) >> 12] == -1)
writemembl(a, v);
else
*(uint8_t *)(writelookup2[a >> 12] + a) = v;
}
static void writememw(uint32_t s, uint32_t a, uint16_t v) {
memcycs += (8 >> is8086);
if (writelookup2[((s) + (a)) >> 12] == -1 || (s) == 0xFFFFFFFF)
writememwl(s + a, v);
else
*(uint16_t *)(writelookup2[(s + a) >> 12] + s + a) = v;
}
//#define readmemb(a) (((a)<0xA0000)?ram[a]:readmembl(a))
static int fetchcycles = 0, fetchclocks;
static uint8_t prefetchqueue[6];
static uint16_t prefetchpc;
static int prefetchw = 0;
static inline uint8_t FETCH() {
uint8_t temp;
/* temp=prefetchqueue[0];
prefetchqueue[0]=prefetchqueue[1];
prefetchqueue[1]=prefetchqueue[2];
prefetchqueue[2]=prefetchqueue[3];
prefetchqueue[3]=prefetchqueue[4];
prefetchqueue[4]=prefetchqueue[5];
if (prefetchw<=((is8086)?4:3))
{
prefetchqueue[prefetchw++]=readmembf(cs+prefetchpc); prefetchpc++;
if (is8086 && (prefetchpc&1))
{
prefetchqueue[prefetchw++]=readmembf(cs+prefetchpc); prefetchpc++;
}
}*/
// uint8_t temp=readmemb(cs+pc);
// if (output) printf("FETCH %04X %i\n",pc,fetchcycles);
if (prefetchw == 0) //(fetchcycles<4)
{
cycles -= (4 - (fetchcycles & 3));
fetchclocks += (4 - (fetchcycles & 3));
fetchcycles = 4;
temp = readmembf(cs + cpu_state.pc);
prefetchpc = cpu_state.pc = cpu_state.pc + 1;
// if (output) printf(" FETCH %04X:%04X %02X %04X %04X
// %i\n",CS,pc-1,temp,pc,prefetchpc,prefetchw);
if (is8086 && (cpu_state.pc & 1)) {
prefetchqueue[0] = readmembf(cs + cpu_state.pc);
// if (output) printf(" PREFETCHED from %04X:%04X %02X
// 8086\n",CS,prefetchpc,prefetchqueue[prefetchw]);
prefetchpc++;
prefetchw++;
}
} else {
temp = prefetchqueue[0];
prefetchqueue[0] = prefetchqueue[1];
prefetchqueue[1] = prefetchqueue[2];
prefetchqueue[2] = prefetchqueue[3];
prefetchqueue[3] = prefetchqueue[4];
prefetchqueue[4] = prefetchqueue[5];
prefetchw--;
// if (output) printf("PREFETCH %04X:%04X %02X %04X %04X %i\n",CS,pc,temp,pc,prefetchpc,prefetchw);
fetchcycles -= 4;
// fetchclocks+=4;
cpu_state.pc++;
}
// if (output) printf("%i\n",fetchcycles);
return temp;
}
static inline void FETCHADD(int c) {
int d;
// if (output) printf("FETCHADD %i\n",c);
if (c < 0)
return;
if (prefetchw > ((is8086) ? 4 : 3))
return;
d = c + (fetchcycles & 3);
while (d > 3 && prefetchw < ((is8086) ? 6 : 4)) {
d -= 4;
if (is8086 && !(prefetchpc & 1)) {
prefetchqueue[prefetchw] = readmembf(cs + prefetchpc);
// printf("PREFETCHED from %04X:%04X %02X
// 8086\n",CS,prefetchpc,prefetchqueue[prefetchw]);
prefetchpc++;
prefetchw++;
}
if (prefetchw < 6) {
prefetchqueue[prefetchw] = readmembf(cs + prefetchpc);
// printf("PREFETCHED from %04X:%04X
// %02X\n",CS,prefetchpc,prefetchqueue[prefetchw]);
prefetchpc++;
prefetchw++;
}
}
fetchcycles += c;
if (fetchcycles > 16)
fetchcycles = 16;
// if (fetchcycles>24) fetchcycles=24;
}
static void FETCHCOMPLETE() {
// pclog("Fetchcomplete %i %i %i\n",fetchcycles&3,fetchcycles,prefetchw);
if (!(fetchcycles & 3))
return;
if (prefetchw > ((is8086) ? 4 : 3))
return;
if (!prefetchw)
nextcyc = (4 - (fetchcycles & 3));
cycles -= (4 - (fetchcycles & 3));
fetchclocks += (4 - (fetchcycles & 3));
if (is8086 && !(prefetchpc & 1)) {
prefetchqueue[prefetchw] = readmembf(cs + prefetchpc);
// printf("PREFETCHEDc from %04X:%04X %02X 8086\n",CS,prefetchpc,prefetchqueue[prefetchw]);
prefetchpc++;
prefetchw++;
}
if (prefetchw < 6) {
prefetchqueue[prefetchw] = readmembf(cs + prefetchpc);
// printf("PREFETCHEDc from %04X:%04X %02X\n",CS,prefetchpc,prefetchqueue[prefetchw]);
prefetchpc++;
prefetchw++;
}
fetchcycles += (4 - (fetchcycles & 3));
}
static inline void FETCHCLEAR() {
/* int c;
fetchcycles=0;
prefetchpc=pc;
if (is8086 && (prefetchpc&1)) cycles-=4;
for (c=0;c<((is8086)?6:4);c++)
{
prefetchqueue[c]=readmembf(cs+prefetchpc);
if (!is8086 || !(prefetchpc&1)) cycles-=4;
prefetchpc++;
}
prefetchw=(is8086)?6:4;*/
// fetchcycles=0;
prefetchpc = cpu_state.pc;
prefetchw = 0;
memcycs = cycdiff - cycles;
fetchclocks = 0;
// memcycs=cycles;
/* prefetchqueue[0]=readmembf(cs+prefetchpc);
prefetchpc++;
prefetchw=1;
if (is8086 && prefetchpc&1)
{
prefetchqueue[1]=readmembf(cs+prefetchpc);
prefetchpc++;
}*/
}
static uint16_t getword() {
uint8_t temp = FETCH();
return temp | (FETCH() << 8);
}
/*EA calculation*/
/*R/M - bits 0-2 - R/M bits 3-5 - Reg bits 6-7 - mod
From 386 programmers manual :
r8(/r) AL CL DL BL AH CH DH BH
r16(/r) AX CX DX BX SP BP SI DI
r32(/r) EAX ECX EDX EBX ESP EBP ESI EDI
/digit (Opcode) 0 1 2 3 4 5 6 7
REG = 000 001 010 011 100 101 110 111
����Address
disp8 denotes an 8-bit displacement following the ModR/M byte, to be
sign-extended and added to the index. disp16 denotes a 16-bit displacement
following the ModR/M byte, to be added to the index. Default segment
register is SS for the effective addresses containing a BP index, DS for
other effective addresses.
�Ŀ �Mod R/M� ���������ModR/M Values in Hexadecimal�������Ŀ
[BX + SI] 000 00 08 10 18 20 28 30 38
[BX + DI] 001 01 09 11 19 21 29 31 39
[BP + SI] 010 02 0A 12 1A 22 2A 32 3A
[BP + DI] 011 03 0B 13 1B 23 2B 33 3B
[SI] 00 100 04 0C 14 1C 24 2C 34 3C
[DI] 101 05 0D 15 1D 25 2D 35 3D
disp16 110 06 0E 16 1E 26 2E 36 3E
[BX] 111 07 0F 17 1F 27 2F 37 3F
[BX+SI]+disp8 000 40 48 50 58 60 68 70 78
[BX+DI]+disp8 001 41 49 51 59 61 69 71 79
[BP+SI]+disp8 010 42 4A 52 5A 62 6A 72 7A
[BP+DI]+disp8 011 43 4B 53 5B 63 6B 73 7B
[SI]+disp8 01 100 44 4C 54 5C 64 6C 74 7C
[DI]+disp8 101 45 4D 55 5D 65 6D 75 7D
[BP]+disp8 110 46 4E 56 5E 66 6E 76 7E
[BX]+disp8 111 47 4F 57 5F 67 6F 77 7F
[BX+SI]+disp16 000 80 88 90 98 A0 A8 B0 B8
[BX+DI]+disp16 001 81 89 91 99 A1 A9 B1 B9
[BX+SI]+disp16 010 82 8A 92 9A A2 AA B2 BA
[BX+DI]+disp16 011 83 8B 93 9B A3 AB B3 BB
[SI]+disp16 10 100 84 8C 94 9C A4 AC B4 BC
[DI]+disp16 101 85 8D 95 9D A5 AD B5 BD
[BP]+disp16 110 86 8E 96 9E A6 AE B6 BE
[BX]+disp16 111 87 8F 97 9F A7 AF B7 BF
EAX/AX/AL 000 C0 C8 D0 D8 E0 E8 F0 F8
ECX/CX/CL 001 C1 C9 D1 D9 E1 E9 F1 F9
EDX/DX/DL 010 C2 CA D2 DA E2 EA F2 FA
EBX/BX/BL 011 C3 CB D3 DB E3 EB F3 FB
ESP/SP/AH 11 100 C4 CC D4 DC E4 EC F4 FC
EBP/BP/CH 101 C5 CD D5 DD E5 ED F5 FD
ESI/SI/DH 110 C6 CE D6 DE E6 EE F6 FE
EDI/DI/BH 111 C7 CF D7 DF E7 EF F7 FF
mod = 11 - register
10 - address + 16 bit displacement
01 - address + 8 bit displacement
00 - address
reg = If mod=11, (depending on data size, 16 bits/8 bits, 32 bits=extend 16 bit registers)
0=AX/AL 1=CX/CL 2=DX/DL 3=BX/BL
4=SP/AH 5=BP/CH 6=SI/DH 7=DI/BH
Otherwise, LSB selects SI/DI (0=SI), NMSB selects BX/BP (0=BX), and MSB
selects whether BX/BP are used at all (0=used).
mod=00 is an exception though
6=16 bit displacement only
7=[BX]
Usage varies with instructions.
MOV AL,BL has ModR/M as C3, for example.
mod=11, reg=0, r/m=3
MOV uses reg as dest, and r/m as src.
reg 0 is AL, reg 3 is BL
If BP or SP are in address calc, seg is SS, else DS
*/
uint32_t easeg;
uint32_t rmdat;
static uint16_t zero = 0;
uint16_t *mod1add[2][8];
uint32_t *mod1seg[8];
int slowrm[8];
void makemod1table() {
mod1add[0][0] = &BX;
mod1add[0][1] = &BX;
mod1add[0][2] = &BP;
mod1add[0][3] = &BP;
mod1add[0][4] = &SI;
mod1add[0][5] = &DI;
mod1add[0][6] = &BP;
mod1add[0][7] = &BX;
mod1add[1][0] = &SI;
mod1add[1][1] = &DI;
mod1add[1][2] = &SI;
mod1add[1][3] = &DI;
mod1add[1][4] = &zero;
mod1add[1][5] = &zero;
mod1add[1][6] = &zero;
mod1add[1][7] = &zero;
slowrm[0] = 0;
slowrm[1] = 1;
slowrm[2] = 1;
slowrm[3] = 0;
mod1seg[0] = &ds;
mod1seg[1] = &ds;
mod1seg[2] = &ss;
mod1seg[3] = &ss;
mod1seg[4] = &ds;
mod1seg[5] = &ds;
mod1seg[6] = &ss;
mod1seg[7] = &ds;
}
static void fetcheal() {
if (!cpu_mod && cpu_rm == 6) {
cpu_state.eaaddr = getword();
easeg = ds;
FETCHADD(6);
} else {
switch (cpu_mod) {
case 0:
cpu_state.eaaddr = 0;
if (cpu_rm & 4)
FETCHADD(5);
else
FETCHADD(7 + slowrm[cpu_rm]);
break;
case 1:
cpu_state.eaaddr = (uint16_t)(int8_t)FETCH();
if (cpu_rm & 4)
FETCHADD(9);
else
FETCHADD(11 + slowrm[cpu_rm]);
break;
case 2:
cpu_state.eaaddr = getword();
if (cpu_rm & 4)
FETCHADD(9);
else
FETCHADD(11 + slowrm[cpu_rm]);
break;
}
cpu_state.eaaddr += (*mod1add[0][cpu_rm]) + (*mod1add[1][cpu_rm]);
easeg = *mod1seg[cpu_rm];
cpu_state.eaaddr &= 0xFFFF;
}
}
static inline uint8_t geteab() {
if (cpu_mod == 3)
return (cpu_rm & 4) ? cpu_state.regs[cpu_rm & 3].b.h : cpu_state.regs[cpu_rm & 3].b.l;
return readmemb(easeg + cpu_state.eaaddr);
}
static inline uint16_t geteaw() {
if (cpu_mod == 3)
return cpu_state.regs[cpu_rm].w;
// if (output==3) printf("GETEAW %04X:%08X\n",easeg,eaaddr);
return readmemw(easeg, cpu_state.eaaddr);
}
static inline uint16_t geteaw2() {
if (cpu_mod == 3)
return cpu_state.regs[cpu_rm].w;
// printf("Getting addr from %04X:%04X %05X\n",easeg,eaaddr+2,easeg+eaaddr+2);
return readmemw(easeg, (cpu_state.eaaddr + 2) & 0xFFFF);
}
static inline void seteab(uint8_t val) {
if (cpu_mod == 3) {
if (cpu_rm & 4)
cpu_state.regs[cpu_rm & 3].b.h = val;
else
cpu_state.regs[cpu_rm & 3].b.l = val;
} else {
writememb(easeg + cpu_state.eaaddr, val);
}
}
static inline void seteaw(uint16_t val) {
if (cpu_mod == 3)
cpu_state.regs[cpu_rm].w = val;
else {
writememw(easeg, cpu_state.eaaddr, val);
// writememb(easeg+eaaddr+1,val>>8);
}
}
/*Flags*/
uint8_t znptable8[256];
static uint16_t znptable16[65536];
void makeznptable() {
int c, d;
for (c = 0; c < 256; c++) {
d = 0;
if (c & 1)
d++;
if (c & 2)
d++;
if (c & 4)
d++;
if (c & 8)
d++;
if (c & 16)
d++;
if (c & 32)
d++;
if (c & 64)
d++;
if (c & 128)
d++;
if (d & 1)
znptable8[c] = 0;
else
znptable8[c] = P_FLAG;
if (c == 0xb1)
pclog("znp8 b1 = %i %02X\n", d, znptable8[c]);
if (!c)
znptable8[c] |= Z_FLAG;
if (c & 0x80)
znptable8[c] |= N_FLAG;
}
for (c = 0; c < 65536; c++) {
d = 0;
if (c & 1)
d++;
if (c & 2)
d++;
if (c & 4)
d++;
if (c & 8)
d++;
if (c & 16)
d++;
if (c & 32)
d++;
if (c & 64)
d++;
if (c & 128)
d++;
if (d & 1)
znptable16[c] = 0;
else
znptable16[c] = P_FLAG;
if (c == 0xb1)
pclog("znp16 b1 = %i %02X\n", d, znptable16[c]);
if (c == 0x65b1)
pclog("znp16 65b1 = %i %02X\n", d, znptable16[c]);
if (!c)
znptable16[c] |= Z_FLAG;
if (c & 0x8000)
znptable16[c] |= N_FLAG;
}
// makemod1table();
}
int indump = 0;
FILE* dofopen(const char *filepath, const char* filename, const char * mode) {
int c = strlen(filepath) - 1;
char* sep = (filepath[c] == '/' || filepath[c] == '\\') ? "" : "/";
char buf[1024];
sprintf(buf, "%s%s%s", filepath, sep, filename);
return fopen(buf, mode);
}
void dumpregs() {
int c, d = 0, e = 0;
#ifndef RELEASE_BUILD
FILE *f;
if (indump)
return;
indump = 1;
output = 0;
/* f=dofopen(logs_path, "rram3.dmp","wb");
for (c=0;c<0x8000000;c++) putc(readmemb(c+0x10000000),f);
fclose(f);*/
f = dofopen(logs_path, "ram.dmp", "wb");
fwrite(ram, mem_size * 1024, 1, f);
fclose(f);
/* pclog("Dumping rram5.dmp\n");
f=dofopen(logs_path, "rram5.dmp","wb");
for (c=0;c<0x1000000;c++) putc(readmemb(c+0x10150000),f);
fclose(f);*/
pclog("Dumping rram.dmp\n");
f = dofopen(logs_path, "rram.dmp", "wb");
for (c = 0; c < 0x1000000; c++)
putc(readmemb(c), f);
fclose(f);
/* f=dofopen(logs_path, "rram2.dmp","wb");
for (c=0;c<0x100000;c++) putc(readmemb(c+0xbff00000),f);
fclose(f);
f = dofopen(logs_path, "stack.dmp","wb");
for (c = 0; c < 0x6000; c++) putc(readmemb(c+0xFFDFA000), f);
fclose(f);
f = dofopen(logs_path, "tempx.dmp","wb");
for (c = 0; c < 0x10000; c++) putc(readmemb(c+0xFC816000), f);
fclose(f);
f = dofopen(logs_path, "tempx2.dmp","wb");
for (c = 0; c < 0x10000; c++) putc(readmemb(c+0xFDEF5000), f);
fclose(f);*/
pclog("Dumping rram4.dmp\n");
f = dofopen(logs_path, "rram4.dmp", "wb");
for (c = 0; c < 0x0050000; c++) {
cpu_state.abrt = 0;
putc(readmembl(c + 0x80000000), f);
}
fclose(f);
pclog("Dumping done\n");
/* f=dofopen(logs_path, "rram6.dmp","wb");
for (c=0;c<0x1000000;c++) putc(readmemb(c+0xBF000000),f);
fclose(f);*/
/* f=dofopen(logs_path, "ram6.bin","wb");
fwrite(ram+0x10100,0xA000,1,f);
fclose(f);
f=dofopen(logs_path, "boot.bin","wb");
fwrite(ram+0x7C00,0x200,1,f);
fclose(f);
f=dofopen(logs_path, "ram7.bin","wb");
fwrite(ram+0x11100,0x2000,1,f);
fclose(f);
f=dofopen(logs_path, "ram8.bin","wb");
fwrite(ram+0x3D210,0x200,1,f);
fclose(f); */
/* f=dofopen(logs_path, "bios.dmp","wb");
fwrite(rom,0x20000,1,f);
fclose(f);*/
/* f=dofopen(logs_path, "kernel.dmp","wb");
for (c=0;c<0x200000;c++) putc(readmemb(c+0xC0000000),f);
fclose(f);*/
/* f=dofopen(logs_path, "rram.dmp","wb");
for (c=0;c<0x1500000;c++) putc(readmemb(c),f);
fclose(f);
if (!times)
{
f=dofopen(logs_path, "thing.dmp","wb");
fwrite(ram+0x11E50,0x1000,1,f);
fclose(f);
}*/
#endif
if (is386)
printf("EAX=%08X EBX=%08X ECX=%08X EDX=%08X\nEDI=%08X ESI=%08X EBP=%08X ESP=%08X\n", EAX, EBX, ECX, EDX, EDI, ESI,
EBP, ESP);
else
printf("AX=%04X BX=%04X CX=%04X DX=%04X DI=%04X SI=%04X BP=%04X SP=%04X\n", AX, BX, CX, DX, DI, SI, BP, SP);
printf("PC=%04X CS=%04X DS=%04X ES=%04X SS=%04X FLAGS=%04X\n", cpu_state.pc, CS, DS, ES, SS, cpu_state.flags);
printf("%04X\n", cpu_state.oldpc);
printf("%i ins\n", ins);
if (is386)
printf("In %s mode\n", (msw & 1) ? ((cpu_state.eflags & VM_FLAG) ? "V86" : "protected") : "real");
else
printf("In %s mode\n", (msw & 1) ? "protected" : "real");
printf("CS : base=%06X limit=%08X access=%02X limit_low=%08X limit_high=%08X\n", cs, cpu_state.seg_cs.limit,
cpu_state.seg_cs.access, cpu_state.seg_cs.limit_low, cpu_state.seg_cs.limit_high);
printf("DS : base=%06X limit=%08X access=%02X limit_low=%08X limit_high=%08X\n", ds, cpu_state.seg_ds.limit,
cpu_state.seg_ds.access, cpu_state.seg_ds.limit_low, cpu_state.seg_ds.limit_high);
printf("ES : base=%06X limit=%08X access=%02X limit_low=%08X limit_high=%08X\n", es, cpu_state.seg_es.limit,
cpu_state.seg_es.access, cpu_state.seg_es.limit_low, cpu_state.seg_es.limit_high);
if (is386) {
printf("FS : base=%06X limit=%08X access=%02X limit_low=%08X limit_high=%08X\n", cpu_state.seg_fs.base,
cpu_state.seg_fs.limit, cpu_state.seg_fs.access, cpu_state.seg_fs.limit_low, cpu_state.seg_fs.limit_high);
printf("GS : base=%06X limit=%08X access=%02X limit_low=%08X limit_high=%08X\n", gs, cpu_state.seg_gs.limit,
cpu_state.seg_gs.access, cpu_state.seg_gs.limit_low, cpu_state.seg_gs.limit_high);
}
printf("SS : base=%06X limit=%08X access=%02X limit_low=%08X limit_high=%08X\n", ss, cpu_state.seg_ss.limit,
cpu_state.seg_ss.access, cpu_state.seg_ss.limit_low, cpu_state.seg_ss.limit_high);
printf("GDT : base=%06X limit=%04X\n", gdt.base, gdt.limit);
printf("LDT : base=%06X limit=%04X\n", ldt.base, ldt.limit);
printf("IDT : base=%06X limit=%04X\n", idt.base, idt.limit);
printf("TR : base=%06X limit=%04X\n", tr.base, tr.limit);
if (is386) {
printf("386 in %s mode stack in %s mode\n", (use32) ? "32-bit" : "16-bit", (stack32) ? "32-bit" : "16-bit");
printf("CR0=%08X CR2=%08X CR3=%08X CR4=%08x\n", cr0, cr2, cr3, cr4);
pclog("SMBASE=%08x\n", cpu_state.smbase);
}
printf("Entries in readlookup : %i writelookup : %i\n", readlnum, writelnum);
for (c = 0; c < 1024 * 1024; c++) {
if (readlookup2[c] != 0xFFFFFFFF)
d++;
if (writelookup2[c] != 0xFFFFFFFF)
e++;
}
printf("Entries in readlookup : %i writelookup : %i\n", d, e);
x87_dumpregs();
indump = 0;
}
int x86_was_reset = 0;
void resetx86() {
pclog("x86 reset\n");
ins = 0;
use32 = 0;
cpu_cur_status = 0;
stack32 = 0;
// i86_Reset();
// cs=0xFFFF0;
msw = 0;
if (is486)
cr0 = 1 << 30;
else
cr0 = 0;
cpu_cache_int_enabled = 0;
cpu_update_waitstates();
cr4 = 0;
cpu_state.eflags = 0;
cgate32 = 0;
if (AT) {
loadcs(0xF000);
cpu_state.pc = 0xFFF0;
rammask = cpu_16bitbus ? 0xFFFFFF : 0xFFFFFFFF;
} else {
loadcs(0xFFFF);
cpu_state.pc = 0;
rammask = 0xfffff;
}
idt.base = 0;
idt.limit = is386 ? 0x03FF : 0xFFFF;
cpu_state.flags = 2;
EAX = EBX = ECX = EDX = ESI = EDI = EBP = ESP = 0;
makeznptable();
resetreadlookup();
makemod1table();
FETCHCLEAR();
x87_reset();
cpu_set_edx();
mmu_perm = 4;
x86seg_reset();
codegen_reset();
x86_was_reset = 1;
cpu_state.smbase = 0x30000;
}
void softresetx86() {
// dumpregs();
// exit(-1);
use32 = 0;
stack32 = 0;
cpu_cur_status = 0;
// i86_Reset();
// cs=0xFFFF0;
msw = 0;
if (is486)
cr0 = 1 << 30;
else
cr0 = 0;
cpu_cache_int_enabled = 0;
cpu_update_waitstates();
cr4 = 0;
cpu_state.eflags = 0;
cgate32 = 0;
if (AT) {
loadcs(0xF000);
cpu_state.pc = 0xFFF0;
rammask = cpu_16bitbus ? 0xFFFFFF : 0xFFFFFFFF;
} else {
loadcs(0xFFFF);
cpu_state.pc = 0;
rammask = 0xfffff;
}
// rammask=0xFFFFFFFF;
cpu_state.flags = 2;
idt.base = 0;
if (is386) {
idt.limit = 0x03FF;
EAX = EBX = ECX = EDX = ESI = EDI = EBP = ESP = 0;
} else {
idt.limit = 0xFFFF;
}
x86seg_reset();
flushmmucache();
x86_was_reset = 1;
FETCHCLEAR();
}
static void setznp8(uint8_t val) {
cpu_state.flags &= ~0xC4;
cpu_state.flags |= znptable8[val];
}
static void setznp16(uint16_t val) {
cpu_state.flags &= ~0xC4;
cpu_state.flags |= znptable16[val];
}
static void setadd8(uint8_t a, uint8_t b) {
uint16_t c = (uint16_t)a + (uint16_t)b;
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable8[c & 0xFF];
if (c & 0x100)
cpu_state.flags |= C_FLAG;
if (!((a ^ b) & 0x80) && ((a ^ c) & 0x80))
cpu_state.flags |= V_FLAG;
if (((a & 0xF) + (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setadd8nc(uint8_t a, uint8_t b) {
uint16_t c = (uint16_t)a + (uint16_t)b;
cpu_state.flags &= ~0x8D4;
cpu_state.flags |= znptable8[c & 0xFF];
if (!((a ^ b) & 0x80) && ((a ^ c) & 0x80))
cpu_state.flags |= V_FLAG;
if (((a & 0xF) + (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setadc8(uint8_t a, uint8_t b) {
uint16_t c = (uint16_t)a + (uint16_t)b + tempc;
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable8[c & 0xFF];
if (c & 0x100)
cpu_state.flags |= C_FLAG;
if (!((a ^ b) & 0x80) && ((a ^ c) & 0x80))
cpu_state.flags |= V_FLAG;
if (((a & 0xF) + (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setadd16(uint16_t a, uint16_t b) {
uint32_t c = (uint32_t)a + (uint32_t)b;
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable16[c & 0xFFFF];
if (c & 0x10000)
cpu_state.flags |= C_FLAG;
if (!((a ^ b) & 0x8000) && ((a ^ c) & 0x8000))
cpu_state.flags |= V_FLAG;
if (((a & 0xF) + (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setadd16nc(uint16_t a, uint16_t b) {
uint32_t c = (uint32_t)a + (uint32_t)b;
cpu_state.flags &= ~0x8D4;
cpu_state.flags |= znptable16[c & 0xFFFF];
if (!((a ^ b) & 0x8000) && ((a ^ c) & 0x8000))
cpu_state.flags |= V_FLAG;
if (((a & 0xF) + (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setadc16(uint16_t a, uint16_t b) {
uint32_t c = (uint32_t)a + (uint32_t)b + tempc;
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable16[c & 0xFFFF];
if (c & 0x10000)
cpu_state.flags |= C_FLAG;
if (!((a ^ b) & 0x8000) && ((a ^ c) & 0x8000))
cpu_state.flags |= V_FLAG;
if (((a & 0xF) + (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setsub8(uint8_t a, uint8_t b) {
uint16_t c = (uint16_t)a - (uint16_t)b;
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable8[c & 0xFF];
if (c & 0x100)
cpu_state.flags |= C_FLAG;
if ((a ^ b) & (a ^ c) & 0x80)
cpu_state.flags |= V_FLAG;
if (((a & 0xF) - (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setsub8nc(uint8_t a, uint8_t b) {
uint16_t c = (uint16_t)a - (uint16_t)b;
cpu_state.flags &= ~0x8D4;
cpu_state.flags |= znptable8[c & 0xFF];
if ((a ^ b) & (a ^ c) & 0x80)
cpu_state.flags |= V_FLAG;
if (((a & 0xF) - (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setsbc8(uint8_t a, uint8_t b) {
uint16_t c = (uint16_t)a - (((uint16_t)b) + tempc);
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable8[c & 0xFF];
if (c & 0x100)
cpu_state.flags |= C_FLAG;
if ((a ^ b) & (a ^ c) & 0x80)
cpu_state.flags |= V_FLAG;
if (((a & 0xF) - (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setsub16(uint16_t a, uint16_t b) {
uint32_t c = (uint32_t)a - (uint32_t)b;
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= znptable16[c & 0xFFFF];
if (c & 0x10000)
cpu_state.flags |= C_FLAG;
if ((a ^ b) & (a ^ c) & 0x8000)
cpu_state.flags |= V_FLAG;
if (((a & 0xF) - (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setsub16nc(uint16_t a, uint16_t b) {
uint32_t c = (uint32_t)a - (uint32_t)b;
cpu_state.flags &= ~0x8D4;
cpu_state.flags |= (znptable16[c & 0xFFFF] & ~4);
cpu_state.flags |= (znptable8[c & 0xFF] & 4);
if ((a ^ b) & (a ^ c) & 0x8000)
cpu_state.flags |= V_FLAG;
if (((a & 0xF) - (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
static void setsbc16(uint16_t a, uint16_t b) {
uint32_t c = (uint32_t)a - (((uint32_t)b) + tempc);
cpu_state.flags &= ~0x8D5;
cpu_state.flags |= (znptable16[c & 0xFFFF] & ~4);
cpu_state.flags |= (znptable8[c & 0xFF] & 4);
if (c & 0x10000)
cpu_state.flags |= C_FLAG;
if ((a ^ b) & (a ^ c) & 0x8000)
cpu_state.flags |= V_FLAG;
if (((a & 0xF) - (b & 0xF)) & 0x10)
cpu_state.flags |= A_FLAG;
}
int current_diff = 0;
/*XT systems use the XT master oscillator (14.318 MHz) rather than the CPU clock
at the base timer frequency. Because there isn't an integer relationship
between the two frequencies, use fixed point arithmetic when updating TSC.*/
static uint64_t tsc_frac = 0;
static void clockhardware() {
int diff = cycdiff - cycles - current_diff;
current_diff += diff;
tsc_frac += (uint64_t)diff * xt_cpu_multi;
tsc += (tsc_frac >> 32);
tsc_frac &= 0xffffffff;
if (TIMER_VAL_LESS_THAN_VAL(timer_target, (uint32_t)tsc))
timer_process();
}
static int takeint = 0;
int firstrepcycle = 1;
static void rep(int fv) {
uint8_t temp;
int c = CX;
uint8_t temp2;
uint16_t tempw, tempw2;
uint16_t ipc = cpu_state.oldpc; // pc-1;
int changeds = 0;
uint32_t oldds = ds;
startrep:
temp = FETCH();
// if (firstrepcycle && temp==0xA5) printf("REP MOVSW %06X:%04X %06X:%04X\n",ds,SI,es,DI);
// if (output) printf("REP %02X %04X\n",temp,ipc);
switch (temp) {
case 0x08:
cpu_state.pc = ipc + 1;
cycles -= 2;
FETCHCLEAR();
break;
case 0x26: /*ES:*/
oldds = ds;
ds = es;
changeds = 1;
cycles -= 2;
goto startrep;
break;
case 0x2E: /*CS:*/
oldds = ds;
ds = cs;
changeds = 1;
cycles -= 2;
goto startrep;
break;
case 0x36: /*SS:*/
oldds = ds;
ds = ss;
changeds = 1;
cycles -= 2;
goto startrep;
break;
case 0x6E: /*REP OUTSB*/
if (c > 0) {
temp2 = readmemb(ds + SI);
outb(DX, temp2);
if (cpu_state.flags & D_FLAG)
SI--;
else
SI++;
c--;
cycles -= 5;
}
if (c > 0) {
firstrepcycle = 0;
cpu_state.pc = ipc;
if (cpu_state.ssegs)
cpu_state.ssegs++;
FETCHCLEAR();
} else
firstrepcycle = 1;
break;
case 0xA4: /*REP MOVSB*/
while (c > 0 && !IRQTEST) {
temp2 = readmemb(ds + SI);
writememb(es + DI, temp2);
// if (output) printf("Moved %02X from %04X:%04X to
// %04X:%04X\n",temp2,ds>>4,SI,es>>4,DI);
if (cpu_state.flags & D_FLAG) {
DI--;
SI--;
} else {
DI++;
SI++;
}
c--;
cycles -= 17;
clockhardware();
FETCHADD(17 - memcycs);
}
if (IRQTEST && c > 0)
cpu_state.pc = ipc;
// if (c>0) { firstrepcycle=0; pc=ipc; if (ssegs) ssegs++; FETCHCLEAR(); }
// else firstrepcycle=1;
// }
break;
case 0xA5: /*REP MOVSW*/
while (c > 0 && !IRQTEST) {
memcycs = 0;
tempw = readmemw(ds, SI);
writememw(es, DI, tempw);
if (cpu_state.flags & D_FLAG) {
DI -= 2;
SI -= 2;
} else {
DI += 2;
SI += 2;