Skip to content

Commit 89a0026

Browse files
committed
Very cool woker implementation
* also, logging implemented (console.log does not work from worker (!))
1 parent 71b36ba commit 89a0026

16 files changed

+488
-94
lines changed

block_reader.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function BlockReader(url, block_size_kb, block_count, malloc_fun) {
1919
this.sector_cb = null;
2020
this.malloc_fun = malloc_fun;
2121
}
22+
23+
BlockReader.prototype.log = function () {
24+
};
25+
2226
BlockReader.prototype.get_sector_count = function () {
2327
return this.nb_sectors;
2428
};
@@ -164,5 +168,8 @@ BlockReader.prototype.preload_cb = function (block_number, data, data_len) {
164168
}
165169
};
166170
BlockReader.prototype.write_async = function (sector_num, buf, sector_count, callback) {
171+
this.log('Attempting to write to readonly device');
167172
return -1;
168173
};
174+
175+
self.BlockReader = BlockReader;

clipboard.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function ClipboardDevice(pc_emulator, io_port, read_func, write_func, get_boot_t
1111
this.write_func = write_func;
1212
this.get_boot_time = get_boot_time;
1313
}
14+
15+
ClipboardDevice.prototype.log = function () {
16+
};
17+
1418
ClipboardDevice.prototype.ioport_writeb = function (io_port, byte_value) {
1519
this.doc_str += String.fromCharCode(byte_value);
1620
};
@@ -71,3 +75,5 @@ ClipboardDevice.prototype.ioport_readl = function (io_port) {
7175
}
7276
}
7377
};
78+
79+
self.ClipboardDevice = ClipboardDevice;

cmos.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ function CMOS(pc_emulator) {
2424
pc_emulator.register_ioport_write(0x70, 2, 1, this.ioport_write.bind(this));
2525
pc_emulator.register_ioport_read(0x70, 2, 1, this.ioport_read.bind(this));
2626
}
27+
28+
CMOS.prototype.log = function () {
29+
};
30+
2731
CMOS.prototype.ioport_write = function (io_port, byte_value) {
2832
if (io_port == 0x70) {
2933
this.cmos_index = byte_value & 0x7f;
@@ -45,3 +49,5 @@ CMOS.prototype.ioport_read = function (io_port) {
4549
}
4650
return retval;
4751
};
52+
53+
self.CMOS = CMOS;

cpux86-ta.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ function CPU_X86() {
5757
this.tlb_pages = new Int32Array(2048);
5858
this.tlb_pages_count = 0;
5959
}
60+
61+
CPU_X86.prototype.log = function () {
62+
};
63+
6064
CPU_X86.prototype.phys_mem_resize = function (new_size) {
6165
this.mem_size = new_size;
6266
new_size += ((15 + 3) & ~3);
@@ -172,13 +176,13 @@ function to_hex_u16(n) {
172176
return to_hex(n, 4);
173177
}
174178
CPU_X86.prototype.dump_short = function () {
175-
console.log("" +
179+
this.log("" +
176180
" EIP=" + to_hex_u32(this.eip) +
177181
" EAX=" + to_hex_u32(this.regs[0]) +
178182
" ECX=" + to_hex_u32(this.regs[1]) +
179183
" EDX=" + to_hex_u32(this.regs[2]) +
180184
" EBX=" + to_hex_u32(this.regs[3]));
181-
console.log("" +
185+
this.log("" +
182186
"EFL=" + to_hex_u32(this.eflags) +
183187
" ESP=" + to_hex_u32(this.regs[4]) +
184188
" EBP=" + to_hex_u32(this.regs[5]) +
@@ -189,14 +193,14 @@ CPU_X86.prototype.dump = function () {
189193
var i, sa, na;
190194
var ta = [" ES", " CS", " SS", " DS", " FS", " GS", "LDT", " TR"];
191195
this.dump_short();
192-
console.log("" +
196+
this.log("" +
193197
"TSC=" + to_hex_u32(this.cycle_count) +
194198
" OP=" + to_hex_u8(this.cc_op) +
195199
" SRC=" + to_hex_u32(this.cc_src) +
196200
" DST=" + to_hex_u32(this.cc_dst) +
197201
" OP2=" + to_hex_u8(this.cc_op2) +
198202
" DST2=" + to_hex_u32(this.cc_dst2));
199-
console.log("" +
203+
this.log("" +
200204
"CPL=" + this.cpl +
201205
" CR0=" + to_hex_u32(this.cr0) +
202206
" CR2=" + to_hex_u32(this.cr2) +
@@ -211,7 +215,7 @@ CPU_X86.prototype.dump = function () {
211215
else sa = this.segs[i];
212216
na += ta[i] + "=" + to_hex_u16(sa.selector) + " " + to_hex_u32(sa.base) + " " + to_hex_u32(sa.limit) + " " + to_hex_u16((sa.flags >> 8) & 0xf0ff);
213217
if (i & 1) {
214-
console.log(na);
218+
this.log(na);
215219
na = "";
216220
} else {
217221
na += " ";
@@ -221,7 +225,7 @@ CPU_X86.prototype.dump = function () {
221225
na = "GDT= " + to_hex_u32(sa.base) + " " + to_hex_u32(sa.limit) + " ";
222226
sa = this.idt;
223227
na += "IDT= " + to_hex_u32(sa.base) + " " + to_hex_u32(sa.limit);
224-
console.log(na);
228+
this.log(na);
225229
};
226230
CPU_X86.prototype.exec_internal = function (ua, va) {
227231
var this_, fa, regs;
@@ -3524,7 +3528,7 @@ CPU_X86.prototype.exec_internal = function (ua, va) {
35243528
if (intno == 0x0e) {
35253529
na += " CR2=" + to_hex_u32(this_.cr2);
35263530
}
3527-
console.log(na);
3531+
this.log(na);
35283532
if (intno == 0x06) {
35293533
var na, i, n;
35303534
na = "Code:";
@@ -3535,7 +3539,7 @@ CPU_X86.prototype.exec_internal = function (ua, va) {
35353539
fa = (Nb + i) & -1;
35363540
na += " " + to_hex_u8(gb());
35373541
}
3538-
console.log(na);
3542+
this.log(na);
35393543
}
35403544
}
35413545
if (this_.cr0 & (1 << 0)) {
@@ -8895,3 +8899,5 @@ CPU_X86.prototype.load_binary = function (url, address, callback) {
88958899
CPU_X86.prototype.malloc = function (n) {
88968900
return new Uint8Array(n);
88978901
};
8902+
8903+
self.CPU_X86 = CPU_X86;

ide.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
function IDE_drive(Gh, Hh, malloc_fun) {
3+
function IDE_drive(ide_interface, block_reader, malloc_fun) {
44
var cylinders, sectors;
5-
this.ide_if = Gh;
6-
this.bs = Hh;
7-
sectors = Hh.get_sector_count();
5+
this.ide_if = ide_interface;
6+
this.bs = block_reader;
7+
sectors = block_reader.get_sector_count();
88
cylinders = sectors / (16 * 63);
99
if (cylinders > 16383) {
1010
cylinders = 16383;
@@ -34,6 +34,10 @@ function IDE_drive(Gh, Hh, malloc_fun) {
3434
this.req_nb_sectors = 0;
3535
this.io_nb_sectors = 0;
3636
}
37+
38+
IDE_drive.prototype.log = function () {
39+
};
40+
3741
IDE_drive.prototype.identify = function () {
3842
function store_word(word_index, word_value) {
3943
io_buffer[word_index * 2] = word_value & 0xff;
@@ -293,13 +297,20 @@ IDE_drive.prototype.exec_cmd = function (byte_command) {
293297
break;
294298
}
295299
};
300+
296301
function IDE_device(pc_emulator, io_port1, io_port2, set_irq_func, block_readers, malloc_fun) {
297-
var i, drive;
302+
var i, drive, me;
298303
this.set_irq_func = set_irq_func;
299304
this.drives = [];
305+
me = this;
306+
var logger = function () {
307+
me.log.apply(me, arguments);
308+
};
309+
300310
for (i = 0; i < 2; i++) {
301311
if (block_readers[i]) {
302312
drive = new IDE_drive(this, block_readers[i], malloc_fun);
313+
drive.log = logger;
303314
} else {
304315
drive = null;
305316
}
@@ -317,6 +328,10 @@ function IDE_device(pc_emulator, io_port1, io_port2, set_irq_func, block_readers
317328
pc_emulator.register_ioport_write(io_port1, 4, 4, this.data_writel.bind(this));
318329
pc_emulator.register_ioport_read(io_port1, 4, 4, this.data_readl.bind(this));
319330
}
331+
332+
IDE_device.prototype.log = function () {
333+
};
334+
320335
IDE_device.prototype.ioport_write = function (io_port, byte_value) {
321336
var current_drive = this.cur_drive;
322337

@@ -504,3 +519,5 @@ IDE_device.prototype.data_readl = function (io_port) {
504519

505520
return retval;
506521
};
522+
523+
self.IDE_device = IDE_device;

index.html

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,25 @@
33
<head>
44
<title>Javascript PC Emulator</title>
55
<link rel="stylesheet" href="jslinux.css">
6-
76
<script type="text/javascript" src="utils.js"></script>
87
<script type="text/javascript" src="term.js"></script>
9-
<script type="text/javascript" src="cpux86-ta.js"></script>
10-
<script type="text/javascript" src="clipboard.js"></script>
11-
<script type="text/javascript" src="cmos.js"></script>
12-
<script type="text/javascript" src="ide.js"></script>
13-
<script type="text/javascript" src="keyboard.js"></script>
14-
<script type="text/javascript" src="pic.js"></script>
15-
<script type="text/javascript" src="pit.js"></script>
16-
<script type="text/javascript" src="serial.js"></script>
17-
<script type="text/javascript" src="block_reader.js"></script>
18-
<script type="text/javascript" src="pcemulator.js"></script>
19-
<script type="text/javascript" src="jslinux.js"></script>
8+
<script type="text/javascript" src="jslinuxwithgui.js"></script>
9+
<script type="text/javascript" src="pseudoworker.js"></script>
2010
<script type="text/javascript">
2111
function megastart() {
22-
var clipboard_element;
23-
24-
if (!window.Uint8Array || !window.Uint16Array || !window.Int32Array || !window.ArrayBuffer) {
25-
alert("Typed array are not supported. Please use modern browser");
26-
return;
27-
}
28-
29-
clipboard_element = document.getElementById("text_clipboard");
30-
31-
function clipboard_set(val) {
32-
clipboard_element.value = val;
33-
}
34-
35-
function clipboard_get() {
36-
return clipboard_element.value;
37-
}
38-
39-
40-
function run_linux(name, ident) {
41-
42-
var keyboard2guest;
43-
var pc, term, com1;
44-
45-
46-
pc = jslinux(clipboard_get, clipboard_set, name);
47-
com1 = pc.com1;
48-
keyboard2guest = function (str) {
49-
// keyboard -> guest
50-
com1.send_chars(str);
51-
};
52-
53-
term = new Term(80, 30, keyboard2guest);
54-
term.open(ident);
55-
com1.write_func = term.write.bind(term); // guest -> terminal
56-
return pc;
57-
}
58-
59-
60-
var linux1 = run_linux('linux1', document.getElementById('terminal1'));
61-
var linux2 = run_linux('linux2', document.getElementById('terminal2'));
62-
63-
var linux1_serial = linux1.com2;
64-
var linux2_serial = linux2.com2;
65-
66-
linux1_serial.write_func = linux2_serial.send_chars.bind(linux2_serial);
67-
linux2_serial.write_func = linux1_serial.send_chars.bind(linux1_serial);
68-
69-
}
12+
var prefix = '/jslinux';
13+
var j1 = new JSLinuxWithGUI(
14+
document.getElementById('terminal1'),
15+
'linux1',
16+
prefix,
17+
"console=ttyS0 root=/dev/hda ro init=/sbin/init notsc=1 hdb=none"
18+
);
19+
j1.start();
7020
}
7121
</script>
7222
</head>
7323

7424
<body onload="megastart()">
75-
<table>
76-
<tr>
77-
<td id="terminal1"></td>
78-
<td id="terminal2"></td>
79-
</tr>
80-
</table>
81-
<br>
82-
<textarea rows="4" cols="16" id="text_clipboard"></textarea>
25+
<div id="terminal1"></div>
8326
</body>
8427
</html>

jslinux.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*/
99
"use strict";
1010

11-
function jslinux(clipboard_get, clipboard_set, emulname) {
11+
function jslinux(clipboard_get, clipboard_set, emulname, bin_prefix, cmdline) {
1212
var pc, boot_start_time, start_addr = 0x10000, mem_size = 16 * 1024 * 1024;
1313

1414
function start2(ret) {
1515
if (ret < 0) {
1616
throw "kernel loading failed";
1717
}
18-
pc.load_binary("bin/linuxstart.bin", start_addr, start3);
18+
pc.load_binary(bin_prefix + "/linuxstart.bin", start_addr, start4);
1919
}
2020

2121
// TODO: automate calculation of preload list...
@@ -36,7 +36,7 @@ function jslinux(clipboard_get, clipboard_set, emulname) {
3636
}
3737
/* set the Linux kernel command line */
3838
cmdline_addr = 0xf800;
39-
pc.cpu.write_string(cmdline_addr, "console=ttyS0 root=/dev/hda ro init=/sbin/init notsc=1 hdb=none");
39+
pc.cpu.write_string(cmdline_addr, cmdline);
4040

4141
pc.cpu.eip = start_addr;
4242
pc.cpu.regs[0] = mem_size;
@@ -61,11 +61,13 @@ function jslinux(clipboard_get, clipboard_set, emulname) {
6161
params.clipboard_get = clipboard_get;
6262
params.clipboard_set = clipboard_set;
6363
params.get_boot_time = get_boot_time;
64-
params.hda = { "url": "bin/hda%d", "nb_blocks": 912, "block_size": 64};
64+
params.hda = { "url": bin_prefix + "/hda%d", "nb_blocks": 120 * 1024 / 64, "block_size": 64};
6565
params.emulname = emulname;
6666
pc = new PCEmulator(params);
6767

68-
pc.load_binary("bin/vmlinux26.bin", 0x00100000, start2);
68+
pc.load_binary(bin_prefix + "/vmlinux26.bin", 0x00100000, start2);
6969

7070
return pc;
7171
}
72+
73+
self.jslinux = jslinux;

0 commit comments

Comments
 (0)