forked from hchunhui/tiny386
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1509 lines (1418 loc) · 36.3 KB
/
main.c
File metadata and controls
1509 lines (1418 loc) · 36.3 KB
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
#ifdef BUILD_ESP32
#define NOSDL
#else
//#define USEKVM
//#define NOSDL
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include "i386.h"
#include "i8259.h"
#include "i8254.h"
#include "ide.h"
#include "vga.h"
#include "i8042.h"
#include "misc.h"
#include "adlib.h"
#include "ne2000.h"
#include "i8257.h"
#include "sb16.h"
#include "pcspk.h"
#include "pci.h"
#include "ini.h"
#ifdef BUILD_ESP32
#include "esp_private/system_internal.h"
static uint32_t get_uticks()
{
return esp_system_get_time();
}
#else
#include <time.h>
static uint32_t get_uticks()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((uint32_t) ts.tv_sec * 1000000 +
(uint32_t) ts.tv_nsec / 1000);
}
#endif
#ifdef USEKVM
#include "kvm.h"
#include <sys/mman.h>
typedef CPUKVM CPU;
#define cpu_raise_irq cpukvm_raise_irq
#define cpu_get_cycle cpukvm_get_cycle
void *bigmalloc(size_t size)
{
return mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}
#else
typedef CPUI386 CPU;
#define cpu_raise_irq cpui386_raise_irq
#define cpu_get_cycle cpui386_get_cycle
#ifdef BUILD_ESP32
void *psmalloc(long size);
void *fbmalloc(long size);
void *bigmalloc(size_t size)
{
return psmalloc(size);
}
static char *pcram;
static long pcram_off;
static long pcram_len;
void *pcmalloc(long size)
{
void *ret = pcram + pcram_off;
size = (size + 31) / 32 * 32;
if (pcram_off + size > pcram_len) {
printf("pcram error %ld %ld %ld\n", size, pcram_off, pcram_len);
abort();
}
pcram_off += size;
return ret;
}
#else
void *bigmalloc(size_t size)
{
return malloc(size);
}
#endif
#endif
typedef struct {
CPU *cpu;
PicState2 *pic;
PITState *pit;
U8250 *serial;
CMOS *cmos;
IDEIFState *ide, *ide2;
VGAState *vga;
char *phys_mem;
long phys_mem_size;
char *vga_mem;
int vga_mem_size;
int64_t boot_start_time;
SimpleFBDrawFunc *redraw;
void *redraw_data;
void (*poll)(void *);
KBDState *i8042;
PS2KbdState *kbd;
PS2MouseState *mouse;
AdlibState *adlib;
NE2000State *ne2000;
I8257State *isa_dma, *isa_hdma;
SB16State *sb16;
PCSpkState *pcspk;
I440FXState *i440fx;
PCIBus *pcibus;
PCIDevice *pci_ide;
PCIDevice *pci_vga;
uword pci_vga_ram_addr;
EMULINK *emulink;
const char *bios;
const char *vga_bios;
u8 port92;
int shutdown_state;
int reset_request;
const char *linuxstart;
const char *kernel;
const char *initrd;
const char *cmdline;
int enable_serial;
int full_update;
} PC;
static u8 pc_io_read(void *o, int addr)
{
PC *pc = o;
u8 val;
switch(addr) {
case 0x20: case 0x21: case 0xa0: case 0xa1:
val = i8259_ioport_read(pc->pic, addr);
return val;
case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb:
case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
val = 0xff;
if (pc->enable_serial)
val = u8250_reg_read(pc->serial, addr - 0x3f8);
return val;
case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb:
case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb:
case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb:
case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
return 0;
case 0x42:
/* read delay for PIT channel 2 */
/* certain guest code needs it to drive pc speaker properly */
usleep(0);
/* fall through */
case 0x40: case 0x41: case 0x43:
val = i8254_ioport_read(pc->pit, addr);
return val;
case 0x70: case 0x71:
val = cmos_ioport_read(pc->cmos, addr);
return val;
case 0x1f0: case 0x1f1: case 0x1f2: case 0x1f3:
case 0x1f4: case 0x1f5: case 0x1f6: case 0x1f7:
val = ide_ioport_read(pc->ide, addr - 0x1f0);
return val;
case 0x170: case 0x171: case 0x172: case 0x173:
case 0x174: case 0x175: case 0x176: case 0x177:
val = ide_ioport_read(pc->ide2, addr - 0x170);
return val;
case 0x3f6:
val = ide_status_read(pc->ide);
return val;
case 0x376:
val = ide_status_read(pc->ide2);
return val;
case 0x3c0: case 0x3c1: case 0x3c2: case 0x3c3:
case 0x3c4: case 0x3c5: case 0x3c6: case 0x3c7:
case 0x3c8: case 0x3c9: case 0x3ca: case 0x3cb:
case 0x3cc: case 0x3cd: case 0x3ce: case 0x3cf:
case 0x3d0: case 0x3d1: case 0x3d2: case 0x3d3:
case 0x3d4: case 0x3d5: case 0x3d6: case 0x3d7:
case 0x3d8: case 0x3d9: case 0x3da: case 0x3db:
case 0x3dc: case 0x3dd: case 0x3de: case 0x3df:
val = vga_ioport_read(pc->vga, addr);
return val;
case 0x92:
return pc->port92;
case 0x60:
val = kbd_read_data(pc->i8042, addr);
return val;
case 0x64:
val = kbd_read_status(pc->i8042, addr);
return val;
case 0x61:
val = pcspk_ioport_read(pc->pcspk);
return val;
case 0x220: case 0x221: case 0x222: case 0x223:
case 0x228: case 0x229:
case 0x388: case 0x389: case 0x38a: case 0x38b:
return adlib_read(pc->adlib, addr);
case 0xcfc: case 0xcfd: case 0xcfe: case 0xcff:
val = i440fx_read_data(pc->i440fx, addr - 0xcfc, 0);
return val;
case 0x300: case 0x301: case 0x302: case 0x303:
case 0x304: case 0x305: case 0x306: case 0x307:
case 0x308: case 0x309: case 0x30a: case 0x30b:
case 0x30c: case 0x30d: case 0x30e: case 0x30f:
val = ne2000_ioport_read(pc->ne2000, addr);
return val;
case 0x310:
val = ne2000_asic_ioport_read(pc->ne2000, addr);
return val;
case 0x31f:
val = ne2000_reset_ioport_read(pc->ne2000, addr);
return val;
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x04: case 0x05: case 0x06: case 0x07:
val = i8257_read_chan(pc->isa_dma, addr - 0x00, 1);
return val;
case 0x08: case 0x09: case 0x0a: case 0x0b:
case 0x0c: case 0x0d: case 0x0e: case 0x0f:
val = i8257_read_cont(pc->isa_dma, addr - 0x08, 1);
return val;
case 0x81: case 0x82: case 0x83: case 0x87:
val = i8257_read_page(pc->isa_dma, addr - 0x80);
return val;
case 0x481: case 0x482: case 0x483: case 0x487:
val = i8257_read_pageh(pc->isa_dma, addr - 0x480);
return val;
case 0xc0: case 0xc2: case 0xc4: case 0xc6:
case 0xc8: case 0xca: case 0xcc: case 0xce:
val = i8257_read_chan(pc->isa_hdma, addr - 0xc0, 1);
return val;
case 0xd0: case 0xd2: case 0xd4: case 0xd6:
case 0xd8: case 0xda: case 0xdc: case 0xde:
val = i8257_read_cont(pc->isa_hdma, addr - 0xd0, 1);
return val;
case 0x89: case 0x8a: case 0x8b: case 0x8f:
val = i8257_read_page(pc->isa_hdma, addr - 0x88);
return val;
case 0x489: case 0x48a: case 0x48b: case 0x48f:
val = i8257_read_pageh(pc->isa_hdma, addr - 0x488);
return val;
case 0x225:
val = sb16_mixer_read(pc->sb16, addr);
return val;
case 0x226: case 0x22a: case 0x22c: case 0x22d: case 0x22e: case 0x22f:
val = sb16_dsp_read(pc->sb16, addr);
return val;
case 0xf1f4:
val = 0;
emulink_data_read_string(pc->emulink, &val, 1, 1);
return val;
default:
//fprintf(stderr, "in 0x%x <= 0x%x\n", addr, 0xff);
return 0xff;
}
}
static u16 pc_io_read16(void *o, int addr)
{
PC *pc = o;
u16 val;
switch(addr) {
case 0x1ce: case 0x1cf:
val = vbe_read(pc->vga, addr - 0x1ce);
return val;
case 0x1f0:
val = ide_data_readw(pc->ide);
return val;
case 0x170:
val = ide_data_readw(pc->ide2);
return val;
case 0xcf8:
val = i440fx_read_addr(pc->i440fx, 0, 1);
return val;
case 0xcfc: case 0xcfe:
val = i440fx_read_data(pc->i440fx, addr - 0xcfc, 1);
return val;
case 0x310:
val = ne2000_asic_ioport_read(pc->ne2000, addr);
return val;
case 0x220:
return adlib_read(pc->adlib, addr);
default:
fprintf(stderr, "inw 0x%x <= 0x%x\n", addr, 0xffff);
return 0xffff;
}
}
static u32 pc_io_read32(void *o, int addr)
{
PC *pc = o;
u32 val;
switch(addr) {
case 0x1f0:
val = ide_data_readl(pc->ide);
return val;
case 0x170:
val = ide_data_readl(pc->ide2);
return val;
case 0x3cc:
return (get_uticks() - pc->boot_start_time) / 1000;
case 0xcf8:
val = i440fx_read_addr(pc->i440fx, 0, 2);
return val;
case 0xcfc:
val = i440fx_read_data(pc->i440fx, 0, 2);
return val;
case 0xf1f0:
val = emulink_status_read(pc->emulink);
return val;
default:
fprintf(stderr, "ind 0x%x <= 0x%x\n", addr, 0xffffffff);
}
return 0xffffffff;
}
static int pc_io_read_string(void *o, int addr, uint8_t *buf, int size, int count)
{
PC *pc = o;
u32 val;
switch(addr) {
case 0x1f0:
return ide_data_read_string(pc->ide, buf, size, count);
case 0x170:
return ide_data_read_string(pc->ide2, buf, size, count);
case 0xf1f4:
return emulink_data_read_string(pc->emulink, buf, size, count);
}
return 0;
}
static void pc_io_write(void *o, int addr, u8 val)
{
PC *pc = o;
switch(addr) {
case 0x80: case 0xed:
/* used by linux, for io delay */
return;
case 0x20: case 0x21: case 0xa0: case 0xa1:
i8259_ioport_write(pc->pic, addr, val);
return;
case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb:
case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
u8250_reg_write(pc->serial, addr - 0x3f8, val);
return;
case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb:
case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb:
case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb:
case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
return;
case 0x40: case 0x41: case 0x42: case 0x43:
i8254_ioport_write(pc->pit, addr, val);
return;
case 0x70: case 0x71:
cmos_ioport_write(pc->cmos, addr, val);
return;
case 0x1f0: case 0x1f1: case 0x1f2: case 0x1f3:
case 0x1f4: case 0x1f5: case 0x1f6: case 0x1f7:
ide_ioport_write(pc->ide, addr - 0x1f0, val);
return;
case 0x170: case 0x171: case 0x172: case 0x173:
case 0x174: case 0x175: case 0x176: case 0x177:
ide_ioport_write(pc->ide2, addr - 0x170, val);
return;
case 0x3f6:
ide_cmd_write(pc->ide, val);
return;
case 0x376:
ide_cmd_write(pc->ide2, val);
return;
case 0x3c0: case 0x3c1: case 0x3c2: case 0x3c3:
case 0x3c4: case 0x3c5: case 0x3c6: case 0x3c7:
case 0x3c8: case 0x3c9: case 0x3ca: case 0x3cb:
case 0x3cc: case 0x3cd: case 0x3ce: case 0x3cf:
case 0x3d0: case 0x3d1: case 0x3d2: case 0x3d3:
case 0x3d4: case 0x3d5: case 0x3d6: case 0x3d7:
case 0x3d8: case 0x3d9: case 0x3da: case 0x3db:
case 0x3dc: case 0x3dd: case 0x3de: case 0x3df:
vga_ioport_write(pc->vga, addr, val);
return;
case 0x402:
putchar(val);
fflush(stdout);
return;
case 0x92:
pc->port92 = val;
return;
case 0x60:
kbd_write_data(pc->i8042, addr, val);
return;
case 0x64:
kbd_write_command(pc->i8042, addr, val);
return;
case 0x61:
pcspk_ioport_write(pc->pcspk, val);
return;
case 0x220: case 0x221: case 0x222: case 0x223:
case 0x228: case 0x229:
case 0x388: case 0x389: case 0x38a: case 0x38b:
adlib_write(pc->adlib, addr, val);
return;
case 0x8900:
switch (val) {
case 'S': if (pc->shutdown_state == 0) pc->shutdown_state = 1; break;
case 'h': if (pc->shutdown_state == 1) pc->shutdown_state = 2; break;
case 'u': if (pc->shutdown_state == 2) pc->shutdown_state = 3; break;
case 't': if (pc->shutdown_state == 3) pc->shutdown_state = 4; break;
case 'd': if (pc->shutdown_state == 4) pc->shutdown_state = 5; break;
case 'o': if (pc->shutdown_state == 5) pc->shutdown_state = 6; break;
case 'w': if (pc->shutdown_state == 6) pc->shutdown_state = 7; break;
case 'n': if (pc->shutdown_state == 7) pc->shutdown_state = 8; break;
default : pc->shutdown_state = 0; break;
}
return;
case 0xcfc: case 0xcfd: case 0xcfe: case 0xcff:
i440fx_write_data(pc->i440fx, addr - 0xcfc, val, 0);
return;
case 0x300: case 0x301: case 0x302: case 0x303:
case 0x304: case 0x305: case 0x306: case 0x307:
case 0x308: case 0x309: case 0x30a: case 0x30b:
case 0x30c: case 0x30d: case 0x30e: case 0x30f:
ne2000_ioport_write(pc->ne2000, addr, val);
return;
case 0x310:
ne2000_asic_ioport_write(pc->ne2000, addr, val);
return;
case 0x31f:
ne2000_reset_ioport_write(pc->ne2000, addr, val);
return;
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x04: case 0x05: case 0x06: case 0x07:
i8257_write_chan(pc->isa_dma, addr - 0x00, val, 1);
return;
case 0x08: case 0x09: case 0x0a: case 0x0b:
case 0x0c: case 0x0d: case 0x0e: case 0x0f:
i8257_write_cont(pc->isa_dma, addr - 0x08, val, 1);
return;
case 0x81: case 0x82: case 0x83: case 0x87:
i8257_write_page(pc->isa_dma, addr - 0x80, val);
return;
case 0x481: case 0x482: case 0x483: case 0x487:
i8257_write_pageh(pc->isa_dma, addr - 0x480, val);
return;
case 0xc0: case 0xc2: case 0xc4: case 0xc6:
case 0xc8: case 0xca: case 0xcc: case 0xce:
i8257_write_chan(pc->isa_hdma, addr - 0xc0, val, 1);
return;
case 0xd0: case 0xd2: case 0xd4: case 0xd6:
case 0xd8: case 0xda: case 0xdc: case 0xde:
i8257_write_cont(pc->isa_hdma, addr - 0xd0, val, 1);
return;
case 0x89: case 0x8a: case 0x8b: case 0x8f:
i8257_write_page(pc->isa_hdma, addr - 0x88, val);
return;
case 0x489: case 0x48a: case 0x48b: case 0x48f:
i8257_write_pageh(pc->isa_hdma, addr - 0x488, val);
return;
case 0x224:
sb16_mixer_write_indexb(pc->sb16, addr, val);
return;
case 0x225:
sb16_mixer_write_datab(pc->sb16, addr, val);
return;
case 0x226: case 0x22c:
sb16_dsp_write(pc->sb16, addr, val);
return;
case 0xf1f4:
emulink_data_write_string(pc->emulink, &val, 1, 1);
return;
default:
fprintf(stderr, "out 0x%x => 0x%x\n", val, addr);
return;
}
}
static void pc_io_write16(void *o, int addr, u16 val)
{
PC *pc = o;
switch(addr) {
case 0x1f0:
ide_data_writew(pc->ide, val);
return;
case 0x170:
ide_data_writew(pc->ide2, val);
return;
case 0x3c0: case 0x3c1: case 0x3c2: case 0x3c3:
case 0x3c4: case 0x3c5: case 0x3c6: case 0x3c7:
case 0x3c8: case 0x3c9: case 0x3ca: case 0x3cb:
case 0x3cc: case 0x3cd: case 0x3ce: case 0x3cf:
case 0x3d0: case 0x3d1: case 0x3d2: case 0x3d3:
case 0x3d4: case 0x3d5: case 0x3d6: case 0x3d7:
case 0x3d8: case 0x3d9: case 0x3da: case 0x3db:
case 0x3dc: case 0x3dd: case 0x3de:
vga_ioport_write(pc->vga, addr, val & 0xff);
vga_ioport_write(pc->vga, addr + 1, (val >> 8) & 0xff);
return;
case 0x1ce: case 0x1cf:
vbe_write(pc->vga, addr - 0x1ce, val);
return;
case 0xcfc: case 0xcfe:
i440fx_write_data(pc->i440fx, addr - 0xcfc, val, 1);
return;
case 0x310:
ne2000_asic_ioport_write(pc->ne2000, addr, val);
return;
default:
fprintf(stderr, "outw 0x%x => 0x%x\n", val, addr);
return;
}
}
static void pc_io_write32(void *o, int addr, u32 val)
{
PC *pc = o;
switch(addr) {
case 0x1f0:
ide_data_writel(pc->ide, val);
return;
case 0x170:
ide_data_writel(pc->ide2, val);
return;
case 0xcf8:
i440fx_write_addr(pc->i440fx, 0, val, 2);
return;
case 0xcfc:
i440fx_write_data(pc->i440fx, 0, val, 2);
return;
case 0xf1f0:
emulink_cmd_write(pc->emulink, val);
return;
case 0xf1f4:
emulink_data_write(pc->emulink, val);
return;
default:
fprintf(stderr, "outd 0x%x => 0x%x\n", val, addr);
return;
}
}
static int pc_io_write_string(void *o, int addr, uint8_t *buf, int size, int count)
{
PC *pc = o;
switch(addr) {
case 0x1f0:
return ide_data_write_string(pc->ide, buf, size, count);
case 0x170:
return ide_data_write_string(pc->ide2, buf, size, count);
case 0xf1f4:
return emulink_data_write_string(pc->emulink, buf, size, count);
}
return 0;
}
static void load_bios_and_reset(PC *pc);
void pc_vga_step(void *o)
{
PC *pc = o;
int refresh = vga_step(pc->vga);
if (refresh) {
vga_refresh(pc->vga, pc->redraw, pc->redraw_data, 0);
}
}
void pc_step(PC *pc)
{
#ifndef USEKVM
if (pc->reset_request) {
pc->reset_request = 0;
load_bios_and_reset(pc);
}
#endif
#ifndef BUILD_ESP32
int refresh = vga_step(pc->vga);
#endif
i8254_update_irq(pc->pit);
cmos_update_irq(pc->cmos);
if (pc->enable_serial)
u8250_update(pc->serial);
kbd_step(pc->i8042);
ne2000_step(pc->ne2000);
i8257_dma_run(pc->isa_dma);
i8257_dma_run(pc->isa_hdma);
#ifndef BUILD_ESP32
pc->poll(pc->redraw_data);
if (refresh) {
vga_refresh(pc->vga, pc->redraw, pc->redraw_data,
pc->full_update != 0);
if (pc->full_update == 2)
pc->full_update = 0;
}
#endif
#ifdef USEKVM
cpukvm_step(pc->cpu, 4096);
#else
#ifdef BUILD_ESP32
cpui386_step(pc->cpu, 512);
#else
cpui386_step(pc->cpu, 10240);
#endif
#endif
}
static void raise_irq(void *o, PicState2 *s)
{
cpu_raise_irq(o);
}
static int read_irq(void *o)
{
PicState2 *s = o;
return i8259_read_irq(s);
}
static void set_irq(void *o, int irq, int level)
{
PicState2 *s = o;
return i8259_set_irq(s, irq, level);
}
static void set_pci_vga_bar(void *opaque, int bar_num, uint32_t addr, bool enabled)
{
PC *pc = opaque;
if (enabled)
pc->pci_vga_ram_addr = addr;
else
pc->pci_vga_ram_addr = -1;
#ifdef USEKVM
if (enabled)
cpukvm_register_mem(pc->cpu, 2, addr, pc->vga_mem_size,
pc->vga_mem);
else
cpukvm_register_mem(pc->cpu, 2, addr, 0,
NULL);
#endif
}
static u8 iomem_read8(void *iomem, uword addr)
{
PC *pc = iomem;
uword vga_addr2 = pc->pci_vga_ram_addr;
if (addr >= vga_addr2) {
addr -= vga_addr2;
if (addr < pc->vga_mem_size)
return pc->vga_mem[addr];
else
return 0;
}
return vga_mem_read(pc->vga, addr - 0xa0000);
}
static void iomem_write8(void *iomem, uword addr, u8 val)
{
PC *pc = iomem;
uword vga_addr2 = pc->pci_vga_ram_addr;
if (addr >= vga_addr2) {
addr -= vga_addr2;
if (addr < pc->vga_mem_size)
pc->vga_mem[addr] = val;
return;
}
vga_mem_write(pc->vga, addr - 0xa0000, val);
}
static u16 iomem_read16(void *iomem, uword addr)
{
return iomem_read8(iomem, addr) |
((u16) iomem_read8(iomem, addr + 1) << 8);
}
static void iomem_write16(void *iomem, uword addr, u16 val)
{
PC *pc = iomem;
// fast path for vga ram
uword vga_addr2 = pc->pci_vga_ram_addr;
if (addr >= vga_addr2) {
addr -= vga_addr2;
if (addr + 1 < pc->vga_mem_size)
*(uint16_t *)&(pc->vga_mem[addr]) = val;
return;
}
vga_mem_write16(pc->vga, addr - 0xa0000, val);
}
static u32 iomem_read32(void *iomem, uword addr)
{
return iomem_read16(iomem, addr) |
((u32) iomem_read16(iomem, addr + 2) << 16);
}
static void iomem_write32(void *iomem, uword addr, u32 val)
{
PC *pc = iomem;
// fast path for vga ram
uword vga_addr2 = pc->pci_vga_ram_addr;
if (addr >= vga_addr2) {
uword vga_addr2 = pc->pci_vga_ram_addr;
addr -= vga_addr2;
if (addr + 3 < pc->vga_mem_size)
*(uint32_t *)&(pc->vga_mem[addr]) = val;
return;
}
vga_mem_write32(pc->vga, addr - 0xa0000, val);
}
static bool iomem_write_string(void *iomem, uword addr, uint8_t *buf, int len)
{
PC *pc = iomem;
// fast path for vga ram
uword vga_addr2 = pc->pci_vga_ram_addr;
if (addr >= vga_addr2) {
uword vga_addr2 = pc->pci_vga_ram_addr;
addr -= vga_addr2;
if (addr + len < pc->vga_mem_size) {
memcpy(pc->vga_mem + addr, buf, len);
return true;
}
return false;
}
return vga_mem_write_string(pc->vga, addr - 0xa0000, buf, len);
}
static void pc_reset_request(void *p)
{
PC *pc = p;
pc->reset_request = 1;
}
struct pcconfig {
const char *linuxstart;
const char *kernel;
const char *initrd;
const char *cmdline;
const char *bios;
const char *vga_bios;
long mem_size;
long vga_mem_size;
const char *disks[4];
int iscd[4];
const char *fdd[2];
int fill_cmos;
int width;
int height;
int cpu_gen;
int fpu;
int enable_serial;
};
PC *pc_new(SimpleFBDrawFunc *redraw, void (*poll)(void *), void *redraw_data,
u8 *fb, struct pcconfig *conf)
{
PC *pc = malloc(sizeof(PC));
char *mem = bigmalloc(conf->mem_size);
CPU_CB *cb = NULL;
memset(mem, 0, conf->mem_size);
#ifdef BUILD_ESP32
pcram = mem + 0xa0000;
pcram_len = 0xc0000 - 0xa0000;
#endif
#ifdef USEKVM
pc->cpu = cpukvm_new(mem, conf->mem_size, &cb);
#else
pc->cpu = cpui386_new(conf->cpu_gen, mem, conf->mem_size, &cb);
if (conf->fpu)
cpui386_enable_fpu(pc->cpu);
#endif
pc->bios = conf->bios;
pc->vga_bios = conf->vga_bios;
pc->linuxstart = conf->linuxstart;
pc->kernel = conf->kernel;
pc->initrd = conf->initrd;
pc->cmdline = conf->cmdline;
pc->enable_serial = conf->enable_serial;
#if !defined(_WIN32) && !defined(__wasm__) && !defined(NANOSHELL)
if (pc->enable_serial)
CaptureKeyboardInput();
#endif
pc->full_update = 0;
pc->pic = i8259_init(raise_irq, pc->cpu);
cb->pic = pc->pic;
cb->pic_read_irq = read_irq;
pc->pit = i8254_init(0, pc->pic, set_irq);
pc->serial = u8250_init(4, pc->pic, set_irq);
pc->cmos = cmos_init(conf->mem_size, 8, pc->pic, set_irq);
pc->ide = ide_allocate(14, pc->pic, set_irq);
pc->ide2 = ide_allocate(15, pc->pic, set_irq);
const char **disks = conf->disks;
for (int i = 0; i < 4; i++) {
if (!disks[i] || disks[i][0] == 0)
continue;
int ret;
if (i < 2) {
if (conf->iscd[i])
ret = ide_attach_cd(pc->ide, i, disks[i]);
else
ret = ide_attach(pc->ide, i, disks[i]);
assert(ret == 0);
} else {
if (conf->iscd[i])
ret = ide_attach_cd(pc->ide2, i - 2, disks[i]);
else
ret = ide_attach(pc->ide2, i - 2, disks[i]);
assert(ret == 0);
}
}
if (conf->fill_cmos)
ide_fill_cmos(pc->ide, pc->cmos, cmos_set);
int piix3_devfn;
pc->i440fx = i440fx_init(&pc->pcibus, &piix3_devfn);
pc->pci_ide = piix3_ide_init(pc->pcibus, piix3_devfn + 1);
pc->phys_mem = mem;
pc->phys_mem_size = conf->mem_size;
cb->io = pc;
cb->io_read8 = pc_io_read;
cb->io_write8 = pc_io_write;
cb->io_read16 = pc_io_read16;
cb->io_write16 = pc_io_write16;
cb->io_read32 = pc_io_read32;
cb->io_write32 = pc_io_write32;
cb->io_read_string = pc_io_read_string;
cb->io_write_string = pc_io_write_string;
pc->boot_start_time = 0;
pc->vga_mem_size = conf->vga_mem_size;
pc->vga_mem = bigmalloc(pc->vga_mem_size);
memset(pc->vga_mem, 0, pc->vga_mem_size);
pc->vga = vga_init(pc->vga_mem, pc->vga_mem_size,
fb, conf->width, conf->height);
pc->pci_vga = vga_pci_init(pc->vga, pc->pcibus, pc, set_pci_vga_bar);
pc->pci_vga_ram_addr = -1;
pc->emulink = emulink_init();
const char **fdd = conf->fdd;
for (int i = 0; i < 2; i++) {
if (!fdd[i] || fdd[i][0] == 0)
continue;
int ret;
ret = emulink_attach_floppy(pc->emulink, i, fdd[i]);
assert(ret == 0);
}
cb->iomem = pc;
cb->iomem_read8 = iomem_read8;
cb->iomem_write8 = iomem_write8;
cb->iomem_read16 = iomem_read16;
cb->iomem_write16 = iomem_write16;
cb->iomem_read32 = iomem_read32;
cb->iomem_write32 = iomem_write32;
cb->iomem_write_string = iomem_write_string;
pc->redraw = redraw;
pc->redraw_data = redraw_data;
pc->poll = poll;
pc->i8042 = i8042_init(&(pc->kbd), &(pc->mouse),
1, 12, pc->pic, set_irq,
pc, pc_reset_request);
pc->adlib = adlib_new();
pc->ne2000 = isa_ne2000_init(0x300, 9, pc->pic, set_irq);
pc->isa_dma = i8257_new(pc->phys_mem, pc->phys_mem_size,
0x00, 0x80, 0x480, 0);
pc->isa_hdma = i8257_new(pc->phys_mem, pc->phys_mem_size,
0xc0, 0x88, 0x488, 1);
pc->sb16 = sb16_new(0x220, 5,
pc->isa_dma, pc->isa_hdma,
pc->pic, set_irq);
pc->pcspk = pcspk_init(pc->pit);
pc->port92 = 0x2;
pc->shutdown_state = 0;
pc->reset_request = 0;
return pc;
}
#ifdef BUILD_ESP32
#define MIXER_BUF_LEN 128
#else
#define MIXER_BUF_LEN 2048
#endif
void mixer_callback (void *opaque, uint8_t *stream, int free)
{
uint8_t tmpbuf[MIXER_BUF_LEN];
PC *pc = opaque;
assert(free / 2 <= MIXER_BUF_LEN);
memset(tmpbuf, 0, MIXER_BUF_LEN);
adlib_callback(pc->adlib, tmpbuf, free / 2); // s16, mono
sb16_audio_callback(pc->sb16, stream, free); // s16, stereo
int16_t *d2 = (int16_t *) stream;
int16_t *d1 = (int16_t *) tmpbuf;
for (int i = 0; i < free / 2; i++) {
int res = d2[i] + d1[i / 2];
if (res > 32767) res = 32767;
if (res < -32768) res = -32768;
d2[i] = res;
}
if (pcspk_get_active_out(pc->pcspk)) {
memset(tmpbuf, 0x80, MIXER_BUF_LEN / 2);
pcspk_callback(pc->pcspk, tmpbuf, free / 4); // u8, mono
for (int i = 0; i < free / 2; i++) {
int res = d2[i];
res += ((int) tmpbuf[i / 2] - 0x80) << 5;
if (res > 32767) res = 32767;
if (res < -32768) res = -32768;
d2[i] = res;
}
}
}
#ifdef BUILD_ESP32
#include "esp_partition.h"
static int load(PC *pc, const char *file, uword addr, int backward)
{
if (file && file[0] == '/') {
FILE *fp = fopen(file, "rb");
assert(fp);
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
fprintf(stderr, "%s len %d\n", file, len);
rewind(fp);
if (backward)
fread(pc->phys_mem + addr - len, 1, len, fp);
else
fread(pc->phys_mem + addr, 1, len, fp);
fclose(fp);
return len;
}
const esp_partition_t *part =
esp_partition_find_first(ESP_PARTITION_TYPE_ANY,
ESP_PARTITION_SUBTYPE_ANY,
file);
assert(part);
int len = part->size;
fprintf(stderr, "%s len %d\n", file, len);
if (backward)
esp_partition_read(part, 0, pc->phys_mem + addr - len, len);
else
esp_partition_read(part, 0, pc->phys_mem + addr, len);
return len;
}
#else
static int load(PC *pc, const char *file, uword addr, int backward)
{
FILE *fp = fopen(file, "rb");
if (!fp) {
perror(file);
fprintf(stderr, "FAILED to load %s.\n", file);
return 0;
}
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
fprintf(stderr, "%s len %d\n", file, len);
rewind(fp);
if (backward)
fread(pc->phys_mem + addr - len, 1, len, fp);
else
fread(pc->phys_mem + addr, 1, len, fp);
fclose(fp);
return len;
}
#endif
#ifdef NANOSHELL
#include "nanoshell_console.h"
#endif
#ifndef NOSDL
#include "SDL.h"
#include "osd/osd.h"
typedef struct {
int width, height;
SDL_Surface *screen;
PC *pc;