-
Notifications
You must be signed in to change notification settings - Fork 0
/
x86_machine.c
2569 lines (2283 loc) · 70.5 KB
/
x86_machine.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
/*
* PC emulator
*
* Copyright (c) 2011-2017 Fabrice Bellard
*
* 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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "cutils.h"
#include "iomem.h"
#include "virtio.h"
#include "x86_cpu.h"
#include "machine.h"
#include "pci.h"
#include "ide.h"
#include "ps2.h"
#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
#define USE_KVM
#endif
#ifdef USE_KVM
#include <linux/kvm.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <sys/time.h>
#endif
//#define DEBUG_BIOS
//#define DUMP_IOPORT
/***********************************************************/
/* cmos emulation */
//#define DEBUG_CMOS
#define RTC_SECONDS 0
#define RTC_SECONDS_ALARM 1
#define RTC_MINUTES 2
#define RTC_MINUTES_ALARM 3
#define RTC_HOURS 4
#define RTC_HOURS_ALARM 5
#define RTC_ALARM_DONT_CARE 0xC0
#define RTC_DAY_OF_WEEK 6
#define RTC_DAY_OF_MONTH 7
#define RTC_MONTH 8
#define RTC_YEAR 9
#define RTC_REG_A 10
#define RTC_REG_B 11
#define RTC_REG_C 12
#define RTC_REG_D 13
#define REG_A_UIP 0x80
#define REG_B_SET 0x80
#define REG_B_PIE 0x40
#define REG_B_AIE 0x20
#define REG_B_UIE 0x10
typedef struct {
uint8_t cmos_index;
uint8_t cmos_data[128];
IRQSignal *irq;
BOOL use_local_time;
/* used for the periodic irq */
uint32_t irq_timeout;
uint32_t irq_period;
} CMOSState;
static void cmos_write(void *opaque, uint32_t offset,
uint32_t data, int size_log2);
static uint32_t cmos_read(void *opaque, uint32_t offset, int size_log2);
static int to_bcd(CMOSState *s, unsigned int a)
{
if (s->cmos_data[RTC_REG_B] & 0x04) {
return a;
} else {
return ((a / 10) << 4) | (a % 10);
}
}
static void cmos_update_time(CMOSState *s, BOOL set_century)
{
struct timeval tv;
struct tm tm;
time_t ti;
int val;
gettimeofday(&tv, NULL);
ti = tv.tv_sec;
if (s->use_local_time) {
localtime_r(&ti, &tm);
} else {
gmtime_r(&ti, &tm);
}
s->cmos_data[RTC_SECONDS] = to_bcd(s, tm.tm_sec);
s->cmos_data[RTC_MINUTES] = to_bcd(s, tm.tm_min);
if (s->cmos_data[RTC_REG_B] & 0x02) {
s->cmos_data[RTC_HOURS] = to_bcd(s, tm.tm_hour);
} else {
s->cmos_data[RTC_HOURS] = to_bcd(s, tm.tm_hour % 12);
if (tm.tm_hour >= 12)
s->cmos_data[RTC_HOURS] |= 0x80;
}
s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm.tm_wday);
s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm.tm_mday);
s->cmos_data[RTC_MONTH] = to_bcd(s, tm.tm_mon + 1);
s->cmos_data[RTC_YEAR] = to_bcd(s, tm.tm_year % 100);
if (set_century) {
/* not set by the hardware, but easier to do it here */
val = to_bcd(s, (tm.tm_year / 100) + 19);
s->cmos_data[0x32] = val;
s->cmos_data[0x37] = val;
}
/* update in progress flag: 8/32768 seconds after change */
if (tv.tv_usec < 244) {
s->cmos_data[RTC_REG_A] |= REG_A_UIP;
} else {
s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
}
}
CMOSState *cmos_init(PhysMemoryMap *port_map, int addr,
IRQSignal *irq, BOOL use_local_time)
{
CMOSState *s;
s = mallocz(sizeof(*s));
s->use_local_time = use_local_time;
s->cmos_index = 0;
s->cmos_data[RTC_REG_A] = 0x26;
s->cmos_data[RTC_REG_B] = 0x02;
s->cmos_data[RTC_REG_C] = 0x00;
s->cmos_data[RTC_REG_D] = 0x80;
cmos_update_time(s, TRUE);
s->irq = irq;
cpu_register_device(port_map, addr, 2, s, cmos_read, cmos_write,
DEVIO_SIZE8);
return s;
}
#define CMOS_FREQ 32768
static uint32_t cmos_get_timer(CMOSState *s)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint32_t)ts.tv_sec * CMOS_FREQ +
((uint64_t)ts.tv_nsec * CMOS_FREQ / 1000000000);
}
static void cmos_update_timer(CMOSState *s)
{
int period_code;
period_code = s->cmos_data[RTC_REG_A] & 0x0f;
if ((s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
period_code != 0) {
if (period_code <= 2)
period_code += 7;
s->irq_period = 1 << (period_code - 1);
s->irq_timeout = (cmos_get_timer(s) + s->irq_period) &
~(s->irq_period - 1);
}
}
/* XXX: could return a delay, but we don't need high precision
(Windows 2000 uses it for delay calibration) */
static void cmos_update_irq(CMOSState *s)
{
uint32_t d;
if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
d = cmos_get_timer(s) - s->irq_timeout;
if ((int32_t)d >= 0) {
/* this is not what the real RTC does. Here we sent the IRQ
immediately */
s->cmos_data[RTC_REG_C] |= 0xc0;
set_irq(s->irq, 1);
/* update for the next irq */
s->irq_timeout += s->irq_period;
}
}
}
static void cmos_write(void *opaque, uint32_t offset,
uint32_t data, int size_log2)
{
CMOSState *s = opaque;
if (offset == 0) {
s->cmos_index = data & 0x7f;
} else {
#ifdef DEBUG_CMOS
printf("cmos_write: reg=0x%02x val=0x%02x\n", s->cmos_index, data);
#endif
switch(s->cmos_index) {
case RTC_REG_A:
s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
(s->cmos_data[RTC_REG_A] & REG_A_UIP);
cmos_update_timer(s);
break;
case RTC_REG_B:
s->cmos_data[s->cmos_index] = data;
cmos_update_timer(s);
break;
default:
s->cmos_data[s->cmos_index] = data;
break;
}
}
}
static uint32_t cmos_read(void *opaque, uint32_t offset, int size_log2)
{
CMOSState *s = opaque;
int ret;
if (offset == 0) {
return 0xff;
} else {
switch(s->cmos_index) {
case RTC_SECONDS:
case RTC_MINUTES:
case RTC_HOURS:
case RTC_DAY_OF_WEEK:
case RTC_DAY_OF_MONTH:
case RTC_MONTH:
case RTC_YEAR:
case RTC_REG_A:
cmos_update_time(s, FALSE);
ret = s->cmos_data[s->cmos_index];
break;
case RTC_REG_C:
ret = s->cmos_data[s->cmos_index];
s->cmos_data[RTC_REG_C] = 0x00;
set_irq(s->irq, 0);
break;
default:
ret = s->cmos_data[s->cmos_index];
}
#ifdef DEBUG_CMOS
printf("cmos_read: reg=0x%02x val=0x%02x\n", s->cmos_index, ret);
#endif
return ret;
}
}
/***********************************************************/
/* 8259 pic emulation */
//#define DEBUG_PIC
typedef void PICUpdateIRQFunc(void *opaque);
typedef struct {
uint8_t last_irr; /* edge detection */
uint8_t irr; /* interrupt request register */
uint8_t imr; /* interrupt mask register */
uint8_t isr; /* interrupt service register */
uint8_t priority_add; /* used to compute irq priority */
uint8_t irq_base;
uint8_t read_reg_select;
uint8_t special_mask;
uint8_t init_state;
uint8_t auto_eoi;
uint8_t rotate_on_autoeoi;
uint8_t init4; /* true if 4 byte init */
uint8_t elcr; /* PIIX edge/trigger selection*/
uint8_t elcr_mask;
PICUpdateIRQFunc *update_irq;
void *opaque;
} PICState;
static void pic_reset(PICState *s);
static void pic_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2);
static uint32_t pic_read(void *opaque, uint32_t offset, int size_log2);
static void pic_elcr_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2);
static uint32_t pic_elcr_read(void *opaque, uint32_t offset, int size_log2);
PICState *pic_init(PhysMemoryMap *port_map, int port, int elcr_port,
int elcr_mask,
PICUpdateIRQFunc *update_irq, void *opaque)
{
PICState *s;
s = mallocz(sizeof(*s));
s->elcr_mask = elcr_mask;
s->update_irq = update_irq;
s->opaque = opaque;
cpu_register_device(port_map, port, 2, s,
pic_read, pic_write, DEVIO_SIZE8);
cpu_register_device(port_map, elcr_port, 1, s,
pic_elcr_read, pic_elcr_write, DEVIO_SIZE8);
pic_reset(s);
return s;
}
static void pic_reset(PICState *s)
{
/* all 8 bit registers */
s->last_irr = 0; /* edge detection */
s->irr = 0; /* interrupt request register */
s->imr = 0; /* interrupt mask register */
s->isr = 0; /* interrupt service register */
s->priority_add = 0; /* used to compute irq priority */
s->irq_base = 0;
s->read_reg_select = 0;
s->special_mask = 0;
s->init_state = 0;
s->auto_eoi = 0;
s->rotate_on_autoeoi = 0;
s->init4 = 0; /* true if 4 byte init */
}
/* set irq level. If an edge is detected, then the IRR is set to 1 */
static void pic_set_irq1(PICState *s, int irq, int level)
{
int mask;
mask = 1 << irq;
if (s->elcr & mask) {
/* level triggered */
if (level) {
s->irr |= mask;
s->last_irr |= mask;
} else {
s->irr &= ~mask;
s->last_irr &= ~mask;
}
} else {
/* edge triggered */
if (level) {
if ((s->last_irr & mask) == 0)
s->irr |= mask;
s->last_irr |= mask;
} else {
s->last_irr &= ~mask;
}
}
}
static int pic_get_priority(PICState *s, int mask)
{
int priority;
if (mask == 0)
return -1;
priority = 7;
while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0)
priority--;
return priority;
}
/* return the pic wanted interrupt. return -1 if none */
static int pic_get_irq(PICState *s)
{
int mask, cur_priority, priority;
mask = s->irr & ~s->imr;
priority = pic_get_priority(s, mask);
if (priority < 0)
return -1;
/* compute current priority */
cur_priority = pic_get_priority(s, s->isr);
if (priority > cur_priority) {
/* higher priority found: an irq should be generated */
return priority;
} else {
return -1;
}
}
/* acknowledge interrupt 'irq' */
static void pic_intack(PICState *s, int irq)
{
if (s->auto_eoi) {
if (s->rotate_on_autoeoi)
s->priority_add = (irq + 1) & 7;
} else {
s->isr |= (1 << irq);
}
/* We don't clear a level sensitive interrupt here */
if (!(s->elcr & (1 << irq)))
s->irr &= ~(1 << irq);
}
static void pic_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2)
{
PICState *s = opaque;
int priority, addr;
addr = offset & 1;
#ifdef DEBUG_PIC
console.log("pic_write: addr=" + toHex2(addr) + " val=" + toHex2(val));
#endif
if (addr == 0) {
if (val & 0x10) {
/* init */
pic_reset(s);
s->init_state = 1;
s->init4 = val & 1;
if (val & 0x02)
abort(); /* "single mode not supported" */
if (val & 0x08)
abort(); /* "level sensitive irq not supported" */
} else if (val & 0x08) {
if (val & 0x02)
s->read_reg_select = val & 1;
if (val & 0x40)
s->special_mask = (val >> 5) & 1;
} else {
switch(val) {
case 0x00:
case 0x80:
s->rotate_on_autoeoi = val >> 7;
break;
case 0x20: /* end of interrupt */
case 0xa0:
priority = pic_get_priority(s, s->isr);
if (priority >= 0) {
s->isr &= ~(1 << ((priority + s->priority_add) & 7));
}
if (val == 0xa0)
s->priority_add = (s->priority_add + 1) & 7;
break;
case 0x60:
case 0x61:
case 0x62:
case 0x63:
case 0x64:
case 0x65:
case 0x66:
case 0x67:
priority = val & 7;
s->isr &= ~(1 << priority);
break;
case 0xc0:
case 0xc1:
case 0xc2:
case 0xc3:
case 0xc4:
case 0xc5:
case 0xc6:
case 0xc7:
s->priority_add = (val + 1) & 7;
break;
case 0xe0:
case 0xe1:
case 0xe2:
case 0xe3:
case 0xe4:
case 0xe5:
case 0xe6:
case 0xe7:
priority = val & 7;
s->isr &= ~(1 << priority);
s->priority_add = (priority + 1) & 7;
break;
}
}
} else {
switch(s->init_state) {
case 0:
/* normal mode */
s->imr = val;
s->update_irq(s->opaque);
break;
case 1:
s->irq_base = val & 0xf8;
s->init_state = 2;
break;
case 2:
if (s->init4) {
s->init_state = 3;
} else {
s->init_state = 0;
}
break;
case 3:
s->auto_eoi = (val >> 1) & 1;
s->init_state = 0;
break;
}
}
}
static uint32_t pic_read(void *opaque, uint32_t offset, int size_log2)
{
PICState *s = opaque;
int addr, ret;
addr = offset & 1;
if (addr == 0) {
if (s->read_reg_select)
ret = s->isr;
else
ret = s->irr;
} else {
ret = s->imr;
}
#ifdef DEBUG_PIC
console.log("pic_read: addr=" + toHex2(addr1) + " val=" + toHex2(ret));
#endif
return ret;
}
static void pic_elcr_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2)
{
PICState *s = opaque;
s->elcr = val & s->elcr_mask;
}
static uint32_t pic_elcr_read(void *opaque, uint32_t offset, int size_log2)
{
PICState *s = opaque;
return s->elcr;
}
typedef struct {
PICState *pics[2];
int irq_requested;
void (*cpu_set_irq)(void *opaque, int level);
void *opaque;
#if defined(DEBUG_PIC)
uint8_t irq_level[16];
#endif
IRQSignal *irqs;
} PIC2State;
static void pic2_update_irq(void *opaque);
static void pic2_set_irq(void *opaque, int irq, int level);
PIC2State *pic2_init(PhysMemoryMap *port_map, uint32_t addr0, uint32_t addr1,
uint32_t elcr_addr0, uint32_t elcr_addr1,
void (*cpu_set_irq)(void *opaque, int level),
void *opaque, IRQSignal *irqs)
{
PIC2State *s;
int i;
s = mallocz(sizeof(*s));
for(i = 0; i < 16; i++) {
irq_init(&irqs[i], pic2_set_irq, s, i);
}
s->cpu_set_irq = cpu_set_irq;
s->opaque = opaque;
s->pics[0] = pic_init(port_map, addr0, elcr_addr0, 0xf8, pic2_update_irq, s);
s->pics[1] = pic_init(port_map, addr1, elcr_addr1, 0xde, pic2_update_irq, s);
s->irq_requested = 0;
return s;
}
void pic2_set_elcr(PIC2State *s, const uint8_t *elcr)
{
int i;
for(i = 0; i < 2; i++) {
s->pics[i]->elcr = elcr[i] & s->pics[i]->elcr_mask;
}
}
/* raise irq to CPU if necessary. must be called every time the active
irq may change */
static void pic2_update_irq(void *opaque)
{
PIC2State *s = opaque;
int irq2, irq;
/* first look at slave pic */
irq2 = pic_get_irq(s->pics[1]);
if (irq2 >= 0) {
/* if irq request by slave pic, signal master PIC */
pic_set_irq1(s->pics[0], 2, 1);
pic_set_irq1(s->pics[0], 2, 0);
}
/* look at requested irq */
irq = pic_get_irq(s->pics[0]);
#if 0
console.log("irr=" + toHex2(s->pics[0].irr) + " imr=" + toHex2(s->pics[0].imr) + " isr=" + toHex2(s->pics[0].isr) + " irq="+ irq);
#endif
if (irq >= 0) {
/* raise IRQ request on the CPU */
s->cpu_set_irq(s->opaque, 1);
} else {
/* lower irq */
s->cpu_set_irq(s->opaque, 0);
}
}
static void pic2_set_irq(void *opaque, int irq, int level)
{
PIC2State *s = opaque;
#if defined(DEBUG_PIC)
if (irq != 0 && level != s->irq_level[irq]) {
console.log("pic_set_irq: irq=" + irq + " level=" + level);
s->irq_level[irq] = level;
}
#endif
pic_set_irq1(s->pics[irq >> 3], irq & 7, level);
pic2_update_irq(s);
}
/* called from the CPU to get the hardware interrupt number */
static int pic2_get_hard_intno(PIC2State *s)
{
int irq, irq2, intno;
irq = pic_get_irq(s->pics[0]);
if (irq >= 0) {
pic_intack(s->pics[0], irq);
if (irq == 2) {
irq2 = pic_get_irq(s->pics[1]);
if (irq2 >= 0) {
pic_intack(s->pics[1], irq2);
} else {
/* spurious IRQ on slave controller */
irq2 = 7;
}
intno = s->pics[1]->irq_base + irq2;
irq = irq2 + 8;
} else {
intno = s->pics[0]->irq_base + irq;
}
} else {
/* spurious IRQ on host controller */
irq = 7;
intno = s->pics[0]->irq_base + irq;
}
pic2_update_irq(s);
#if defined(DEBUG_PIC)
if (irq != 0 && irq != 14)
printf("pic_interrupt: irq=%d\n", irq);
#endif
return intno;
}
/***********************************************************/
/* 8253 PIT emulation */
#define PIT_FREQ 1193182
#define RW_STATE_LSB 0
#define RW_STATE_MSB 1
#define RW_STATE_WORD0 2
#define RW_STATE_WORD1 3
#define RW_STATE_LATCHED_WORD0 4
#define RW_STATE_LATCHED_WORD1 5
//#define DEBUG_PIT
typedef int64_t PITGetTicksFunc(void *opaque);
typedef struct PITState PITState;
typedef struct {
PITState *pit_state;
uint32_t count;
uint32_t latched_count;
uint8_t rw_state;
uint8_t mode;
uint8_t bcd;
uint8_t gate;
int64_t count_load_time;
int64_t last_irq_time;
} PITChannel;
struct PITState {
PITChannel pit_channels[3];
uint8_t speaker_data_on;
PITGetTicksFunc *get_ticks;
IRQSignal *irq;
void *opaque;
};
static void pit_load_count(PITChannel *pc, int val);
static void pit_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2);
static uint32_t pit_read(void *opaque, uint32_t offset, int size_log2);
static void speaker_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2);
static uint32_t speaker_read(void *opaque, uint32_t offset, int size_log2);
PITState *pit_init(PhysMemoryMap *port_map, int addr0, int addr1,
IRQSignal *irq,
PITGetTicksFunc *get_ticks, void *opaque)
{
PITState *s;
PITChannel *pc;
int i;
s = mallocz(sizeof(*s));
s->irq = irq;
s->get_ticks = get_ticks;
s->opaque = opaque;
for(i = 0; i < 3; i++) {
pc = &s->pit_channels[i];
pc->pit_state = s;
pc->mode = 3;
pc->gate = (i != 2) >> 0;
pit_load_count(pc, 0);
}
s->speaker_data_on = 0;
cpu_register_device(port_map, addr0, 4, s, pit_read, pit_write,
DEVIO_SIZE8);
cpu_register_device(port_map, addr1, 1, s, speaker_read, speaker_write,
DEVIO_SIZE8);
return s;
}
/* unit = PIT frequency */
static int64_t pit_get_time(PITChannel *pc)
{
PITState *s = pc->pit_state;
return s->get_ticks(s->opaque);
}
static uint32_t pit_get_count(PITChannel *pc)
{
uint32_t counter;
uint64_t d;
d = pit_get_time(pc) - pc->count_load_time;
switch(pc->mode) {
case 0:
case 1:
case 4:
case 5:
counter = (pc->count - d) & 0xffff;
break;
default:
counter = pc->count - (d % pc->count);
break;
}
return counter;
}
/* get pit output bit */
static int pit_get_out(PITChannel *pc)
{
int out;
int64_t d;
d = pit_get_time(pc) - pc->count_load_time;
switch(pc->mode) {
default:
case 0:
out = (d >= pc->count) >> 0;
break;
case 1:
out = (d < pc->count) >> 0;
break;
case 2:
/* mode used by Linux */
if ((d % pc->count) == 0 && d != 0)
out = 1;
else
out = 0;
break;
case 3:
out = ((d % pc->count) < (pc->count >> 1)) >> 0;
break;
case 4:
case 5:
out = (d == pc->count) >> 0;
break;
}
return out;
}
static void pit_load_count(PITChannel *s, int val)
{
if (val == 0)
val = 0x10000;
s->count_load_time = pit_get_time(s);
s->last_irq_time = 0;
s->count = val;
}
static void pit_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2)
{
PITState *pit = opaque;
int channel, access, addr;
PITChannel *s;
addr = offset & 3;
#ifdef DEBUG_PIT
printf("pit_write: off=%d val=0x%02x\n", addr, val);
#endif
if (addr == 3) {
channel = val >> 6;
if (channel == 3)
return;
s = &pit->pit_channels[channel];
access = (val >> 4) & 3;
switch(access) {
case 0:
s->latched_count = pit_get_count(s);
s->rw_state = RW_STATE_LATCHED_WORD0;
break;
default:
s->mode = (val >> 1) & 7;
s->bcd = val & 1;
s->rw_state = access - 1 + RW_STATE_LSB;
break;
}
} else {
s = &pit->pit_channels[addr];
switch(s->rw_state) {
case RW_STATE_LSB:
pit_load_count(s, val);
break;
case RW_STATE_MSB:
pit_load_count(s, val << 8);
break;
case RW_STATE_WORD0:
case RW_STATE_WORD1:
if (s->rw_state & 1) {
pit_load_count(s, (s->latched_count & 0xff) | (val << 8));
} else {
s->latched_count = val;
}
s->rw_state ^= 1;
break;
}
}
}
static uint32_t pit_read(void *opaque, uint32_t offset, int size_log2)
{
PITState *pit = opaque;
PITChannel *s;
int ret, count, addr;
addr = offset & 3;
if (addr == 3)
return 0xff;
s = &pit->pit_channels[addr];
switch(s->rw_state) {
case RW_STATE_LSB:
case RW_STATE_MSB:
case RW_STATE_WORD0:
case RW_STATE_WORD1:
count = pit_get_count(s);
if (s->rw_state & 1)
ret = (count >> 8) & 0xff;
else
ret = count & 0xff;
if (s->rw_state & 2)
s->rw_state ^= 1;
break;
default:
case RW_STATE_LATCHED_WORD0:
case RW_STATE_LATCHED_WORD1:
if (s->rw_state & 1)
ret = s->latched_count >> 8;
else
ret = s->latched_count & 0xff;
s->rw_state ^= 1;
break;
}
#ifdef DEBUG_PIT
printf("pit_read: off=%d val=0x%02x\n", addr, ret);
#endif
return ret;
}
static void speaker_write(void *opaque, uint32_t offset,
uint32_t val, int size_log2)
{
PITState *pit = opaque;
pit->speaker_data_on = (val >> 1) & 1;
pit->pit_channels[2].gate = val & 1;
}
static uint32_t speaker_read(void *opaque, uint32_t offset, int size_log2)
{
PITState *pit = opaque;
PITChannel *s;
int out, val;
s = &pit->pit_channels[2];
out = pit_get_out(s);
val = (pit->speaker_data_on << 1) | s->gate | (out << 5);
#ifdef DEBUG_PIT
// console.log("speaker_read: addr=" + toHex2(addr) + " val=" + toHex2(val));
#endif
return val;
}
/* set the IRQ if necessary and return the delay in ms until the next
IRQ. Note: The code does not handle all the PIT configurations. */
static int pit_update_irq(PITState *pit)
{
PITChannel *s;
int64_t d, delay;
s = &pit->pit_channels[0];
delay = PIT_FREQ; /* could be infinity delay */
d = pit_get_time(s) - s->count_load_time;
switch(s->mode) {
default:
case 0:
case 1:
case 4:
case 5:
if (s->last_irq_time == 0) {
delay = s->count - d;
if (delay <= 0) {
set_irq(pit->irq, 1);
set_irq(pit->irq, 0);
s->last_irq_time = d;
}
}
break;
case 2: /* mode used by Linux */
case 3:
delay = s->last_irq_time + s->count - d;
if (delay <= 0) {
set_irq(pit->irq, 1);
set_irq(pit->irq, 0);
s->last_irq_time += s->count;
}
break;
}
if (delay <= 0)
return 0;
else
return delay / (PIT_FREQ / 1000);
}
/***********************************************************/
/* serial port emulation */
#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
#define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
#define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
#define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
#define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
#define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
#define UART_IIR_MSI 0x00 /* Modem status interrupt */
#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
#define UART_IIR_FE 0xC0 /* Fifo enabled */