-
Notifications
You must be signed in to change notification settings - Fork 5
/
pcemulator.js
243 lines (225 loc) · 7.71 KB
/
pcemulator.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
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
"use strict";
function cpu_set_irq(state) {
this.hard_irq = state;
}
function get_ticks() {
return this.cycle_count;
}
function PCEmulator(parameters) {
var cpu, block_readers, p;
var me = this;
var logger = function () {
me.log.apply(me, arguments);
};
cpu = new CPU_X86();
cpu.log = logger;
this.cpu = cpu;
cpu.phys_mem_resize(parameters.mem_size);
this.init_ioports();
this.register_ioport_write(0x80, 1, 1, this.ioport80_write);
this.pic = new PIC(this, 0x20, 0xa0, cpu_set_irq.bind(cpu));
this.pic.log = logger;
this.pit = new PIT(this, this.pic.set_irq.bind(this.pic, 0), get_ticks.bind(cpu));
this.pit.log = logger;
this.cmos = new CMOS(this);
this.cmos.log = logger;
/*
* Most PC-compatible systems in the 1980s and 1990s had one or two ports, with communication interfaces defined like this:
COM1: I/O port 0x3F8, IRQ 4
COM2: I/O port 0x2F8, IRQ 3
COM3: I/O port 0x3E8, IRQ 4
COM4: I/O port 0x2E8, IRQ 3
* */
this.com1 = new SerialPort(this, 0x3f8, this.pic.set_irq.bind(this.pic, 4), parameters.emulname);
this.com1.log = logger;
this.com2 = new SerialPort(this, 0x2f8, this.pic.set_irq.bind(this.pic, 3), parameters.emulname);
this.com2.log = logger;
this.kbd = new Keyboard(this, this.reset.bind(this));
this.kbd.log = logger;
this.reset_request = 0;
block_readers = [];
block_readers[0] = null;
p = parameters.hda;
if (p) {
block_readers[0] = new BlockReader(p.url, p.block_size, p.nb_blocks, cpu.malloc);
block_readers[0].log = logger;
}
block_readers[1] = null;
p = parameters.hdb;
if (p) {
block_readers[1] = new BlockReaderWriter(p.sectors, cpu.malloc, p.filename);
block_readers[1].log = logger;
}
this.ide0 = new IDE_device(this, 0x1f0, 0x3f6, this.pic.set_irq.bind(this.pic, 14), block_readers, cpu.malloc);
this.ide0.log = logger;
if (parameters.clipboard_get && parameters.clipboard_set) {
this.jsclipboard = new ClipboardDevice(this, 0x3e0, parameters.clipboard_get, parameters.clipboard_set, parameters.get_boot_time);
this.jsclipboard.log = logger;
}
cpu.ld8_port = this.ld8_port.bind(this);
cpu.ld16_port = this.ld16_port.bind(this);
cpu.ld32_port = this.ld32_port.bind(this);
cpu.st8_port = this.st8_port.bind(this);
cpu.st16_port = this.st16_port.bind(this);
cpu.st32_port = this.st32_port.bind(this);
cpu.get_hard_intno = this.pic.get_hard_intno.bind(this.pic);
}
PCEmulator.prototype.log = function () {
};
PCEmulator.prototype.render_screenshot = function() {
// should be re-imlemented (monkey-patched)
};
PCEmulator.prototype.load_binary = function (url, address, callback) {
return this.cpu.load_binary(url, address, callback);
};
PCEmulator.prototype.start = function () {
var me = this;
setTimeout(this.timer_func.bind(this), 10);
setInterval(function() {
me.render_screenshot(me.cpu.get_screenshot());
}, 1000);
};
PCEmulator.prototype.timer_func = function () {
var Oa, ii, ji, ki, this_, cpu;
this_ = this;
cpu = this_.cpu;
ii = cpu.cycle_count + 100000;
ji = false;
ki = false;
while (cpu.cycle_count < ii) {
this_.com1.write_tx_fifo();
this_.com2.write_tx_fifo();
this_.pit.update_irq();
Oa = cpu.exec(ii - cpu.cycle_count);
if (Oa == 256) {
if (this_.reset_request) {
ji = true;
break;
}
} else if (Oa == 257) {
ki = true;
break;
} else {
ji = true;
break;
}
}
if (!ji) {
if (ki) {
setTimeout(this.timer_func.bind(this), 10);
} else {
setTimeout(this.timer_func.bind(this), 0);
}
}
};
PCEmulator.prototype.init_ioports = function () {
var i, mi, ni;
this.ioport_readb_table = [];
this.ioport_writeb_table = [];
this.ioport_readw_table = [];
this.ioport_writew_table = [];
this.ioport_readl_table = [];
this.ioport_writel_table = [];
mi = this.default_ioport_readw.bind(this);
ni = this.default_ioport_writew.bind(this);
for (i = 0; i < 1024; i++) {
this.ioport_readb_table[i] = this.default_ioport_readb;
this.ioport_writeb_table[i] = this.default_ioport_writeb;
this.ioport_readw_table[i] = mi;
this.ioport_writew_table[i] = ni;
this.ioport_readl_table[i] = this.default_ioport_readl;
this.ioport_writel_table[i] = this.default_ioport_writel;
}
};
PCEmulator.prototype.default_ioport_readb = function (io_port) {
return 0xff;
};
PCEmulator.prototype.default_ioport_readw = function (io_port) {
var retval_word;
retval_word = this.ioport_readb_table[io_port](io_port);
io_port = (io_port + 1) & (1024 - 1);
retval_word |= this.ioport_readb_table[io_port](io_port) << 8;
return retval_word;
};
PCEmulator.prototype.default_ioport_readl = function (io_port) {
return -1;
};
PCEmulator.prototype.default_ioport_writeb = function (io_port, byte_value) {
};
PCEmulator.prototype.default_ioport_writew = function (io_port, word_value) {
this.ioport_writeb_table[io_port](io_port, word_value & 0xff);
io_port = (io_port + 1) & (1024 - 1);
this.ioport_writeb_table[io_port](io_port, (word_value >> 8) & 0xff);
};
PCEmulator.prototype.default_ioport_writel = function (io_port, dwrod_value) {
};
PCEmulator.prototype.ld8_port = function (io_port) {
var ja;
ja = this.ioport_readb_table[io_port & (1024 - 1)](io_port);
return ja;
};
PCEmulator.prototype.ld16_port = function (io_port) {
var ja;
ja = this.ioport_readw_table[io_port & (1024 - 1)](io_port);
return ja;
};
PCEmulator.prototype.ld32_port = function (io_port) {
var ja;
ja = this.ioport_readl_table[io_port & (1024 - 1)](io_port);
return ja;
};
PCEmulator.prototype.st8_port = function (io_port, byte_value) {
this.ioport_writeb_table[io_port & (1024 - 1)](io_port, byte_value);
};
PCEmulator.prototype.st16_port = function (io_port, word_value) {
this.ioport_writew_table[io_port & (1024 - 1)](io_port, word_value);
};
PCEmulator.prototype.st32_port = function (io_port, dword_value) {
this.ioport_writel_table[io_port & (1024 - 1)](io_port, dword_value);
};
PCEmulator.prototype.register_ioport_read = function (start_io_port, count, op_size, func) {
var i;
switch (op_size) {
case 1:
for (i = start_io_port; i < start_io_port + count; i++) {
this.ioport_readb_table[i] = func;
}
break;
case 2:
for (i = start_io_port; i < start_io_port + count; i += 2) {
this.ioport_readw_table[i] = func;
}
break;
case 4:
for (i = start_io_port; i < start_io_port + count; i += 4) {
this.ioport_readl_table[i] = func;
}
break;
}
};
PCEmulator.prototype.register_ioport_write = function (start_io_port, count, op_size, func) {
var i;
switch (op_size) {
case 1:
for (i = start_io_port; i < start_io_port + count; i++) {
this.ioport_writeb_table[i] = func;
}
break;
case 2:
for (i = start_io_port; i < start_io_port + count; i += 2) {
this.ioport_writew_table[i] = func;
}
break;
case 4:
for (i = start_io_port; i < start_io_port + count; i += 4) {
this.ioport_writel_table[i] = func;
}
break;
}
};
PCEmulator.prototype.ioport80_write = function (io_port, byte_value) {
};
PCEmulator.prototype.reset = function () {
this.reset_request = 1;
};
self.PCEmulator = PCEmulator;