-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMOS.js
63 lines (60 loc) · 2.03 KB
/
CMOS.js
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
/*
CMOS Ram Memory, actually just the RTC Clock Emulator
Useful references:
------------------
http://www.bioscentral.com/misc/cmosmap.htm
http://wiki.osdev.org/CMOS
add by hao:
cmos的核心模拟代码
*/
/*
In this implementation, bytes are stored in the RTC in BCD format
binary -> bcd: bcd = ((bin / 10) << 4) | (bin % 10)
bcd -> binary: bin = ((bcd / 16) * 10) + (bcd & 0xf)
*/
function bin_to_bcd(a) { return ((a / 10) << 4) | (a % 10);}
function CMOS(PC) {
var time_array, d;
time_array = new Uint8Array(128);
this.cmos_data = time_array;
this.cmos_index = 0;
d = new Date();
time_array[0] = bin_to_bcd(d.getUTCSeconds());
time_array[2] = bin_to_bcd(d.getUTCMinutes());
time_array[4] = bin_to_bcd(d.getUTCHours());
time_array[6] = bin_to_bcd(d.getUTCDay());
time_array[7] = bin_to_bcd(d.getUTCDate());
time_array[8] = bin_to_bcd(d.getUTCMonth() + 1);
time_array[9] = bin_to_bcd(d.getUTCFullYear() % 100);
time_array[10] = 0x26;
time_array[11] = 0x02;
time_array[12] = 0x00;
time_array[13] = 0x80;
time_array[0x14] = 0x02;
PC.register_ioport_write(0x70, 2, 1, this.ioport_write.bind(this));
PC.register_ioport_read(0x70, 2, 1, this.ioport_read.bind(this));
}
CMOS.prototype.ioport_write = function(mem8_loc, data) {
if (mem8_loc == 0x70) {
// the high order bit is used to indicate NMI masking
// low order bits are used to address CMOS
// the index written here is used on an ioread 0x71
this.cmos_index = data & 0x7f;
}
};
CMOS.prototype.ioport_read = function(mem8_loc) {
var data;
if (mem8_loc == 0x70) {
return 0xff;
} else {
// else here => 0x71, i.e., CMOS read
data = this.cmos_data[this.cmos_index];
if (this.cmos_index == 10)
// flip the UIP (update in progress) bit on a read
this.cmos_data[10] ^= 0x80;
else if (this.cmos_index == 12)
// Always return interrupt status == 0
this.cmos_data[12] = 0x00;
return data;
}
};