Works on modern browsers again by fixing XHR loading of binaries to be asynchronous.

This commit is contained in:
Anselm Levskaya 2014-10-19 15:24:09 -07:00
parent 541ce3c478
commit 8eaef79602
4 changed files with 83 additions and 153 deletions

View File

@ -36,7 +36,7 @@ function PCEmulator(params) {
cpu.get_hard_intno = this.pic.get_hard_intno.bind(this.pic); cpu.get_hard_intno = this.pic.get_hard_intno.bind(this.pic);
} }
PCEmulator.prototype.load_binary = function(url, mem8_loc) { return this.cpu.load_binary(url, mem8_loc); }; PCEmulator.prototype.load_binary = function(binary_array, mem8_loc) { return this.cpu.load_binary(binary_array, mem8_loc); };
PCEmulator.prototype.start = function() { setTimeout(this.timer_func.bind(this), 10); }; PCEmulator.prototype.start = function() { setTimeout(this.timer_func.bind(this), 10); };

View File

@ -9703,154 +9703,17 @@ CPU_X86.prototype.exec = function(N_cycles) {
/* /*
Binary Loaders Binary Loader
========================================================================================== ==========================================================================================
These routines load binary files into memory via AJAX. This routine loads a binary array into memory.
*/ */
CPU_X86.prototype.load_binary_ie9 = function(url, mem8_loc) { CPU_X86.prototype.load_binary = function(binary_array, mem8_loc) {
var req, binary_array, len, i; var len, i, typed_binary_array;
req = new XMLHttpRequest();
req.open('GET', url, false);
req.send(null);
if (req.status != 200 && req.status != 0) {
throw "Error while loading " + url;
}
binary_array = new VBArray(req.responseBody).toArray();
len = binary_array.length;
for (i = 0; i < len; i++) {
this.st8_phys(mem8_loc + i, binary_array[i]);
}
return len;
};
CPU_X86.prototype.load_binary = function(url, mem8_loc) {
var req, binary_array, len, i, typed_binary_array, typed_arrays_exist;
if (typeof ActiveXObject == "function")
return this.load_binary_ie9(url, mem8_loc);
req = new XMLHttpRequest();
req.open('GET', url, false);
typed_arrays_exist = ('ArrayBuffer' in window && 'Uint8Array' in window);
if (typed_arrays_exist && 'mozResponseType' in req) {
req.mozResponseType = 'arraybuffer';
} else if (typed_arrays_exist && 'responseType' in req) {
req.responseType = 'arraybuffer';
} else {
req.overrideMimeType('text/plain; charset=x-user-defined');
typed_arrays_exist = false;
}
req.send(null);
if (req.status != 200 && req.status != 0) {
throw "Error while loading " + url;
}
if (typed_arrays_exist && 'mozResponse' in req) {
binary_array = req.mozResponse;
} else if (typed_arrays_exist && req.mozResponseArrayBuffer) {
binary_array = req.mozResponseArrayBuffer;
} else if ('responseType' in req) {
binary_array = req.response;
} else {
binary_array = req.responseText;
typed_arrays_exist = false;
}
if (typed_arrays_exist) {
len = binary_array.byteLength; len = binary_array.byteLength;
typed_binary_array = new Uint8Array(binary_array, 0, len); typed_binary_array = new Uint8Array(binary_array, 0, len);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
this.st8_phys(mem8_loc + i, typed_binary_array[i]); this.st8_phys(mem8_loc + i, typed_binary_array[i]);
} }
} else {
len = binary_array.length;
for (i = 0; i < len; i++) {
this.st8_phys(mem8_loc + i, binary_array.charCodeAt(i));
}
}
return len; return len;
}; };

View File

@ -25,7 +25,7 @@
} }
</style> </style>
</head> </head>
<body onload="start()"> <body onload="load_binaries()">
<table border="0"> <table border="0">
<tr valign="top"><td> <tr valign="top"><td>

View File

@ -50,6 +50,73 @@ function get_boot_time()
return (+new Date()) - boot_start_time; return (+new Date()) - boot_start_time;
} }
/* global to hold binary data from async XHR requests */
var binaries = [false,false,false];
function loadbinary(url,slot) {
var req, binary_array, len, typed_arrays_exist;
req = new XMLHttpRequest();
req.open('GET', url, true);
typed_arrays_exist = ('ArrayBuffer' in window && 'Uint8Array' in window);
if (typed_arrays_exist && 'mozResponseType' in req) {
req.mozResponseType = 'arraybuffer';
} else if (typed_arrays_exist && 'responseType' in req) {
req.responseType = 'arraybuffer';
} else {
req.overrideMimeType('text/plain; charset=x-user-defined');
typed_arrays_exist = false;
}
req.onerror = function(e) {
throw "Error while loading " + req.statusText;
};
req.onload = function (e) {
console.log('onload triggered');
if (req.readyState === 4) {
if (req.status === 200) {
if (typed_arrays_exist && 'mozResponse' in req) {
binaries[slot] = req.mozResponse;
} else if (typed_arrays_exist && req.mozResponseArrayBuffer) {
binaries[slot] = req.mozResponseArrayBuffer;
} else if ('responseType' in req) {
binaries[slot] = req.response;
} else {
binaries[slot] = req.responseText;
}
//cb_f()
} else {
throw "Error while loading " + url;
}
}
}
req.send(null);
};
function checkbinaries() {
//console.log("checkbinaries: ",(binaries[0]!=false),(binaries[1]!=false),(binaries[2]!=false));
if((binaries[0] != false) && (binaries[1] != false) && (binaries[2] != false)){
console.log("...binaries done loading, calling start()")
start();
} else {
setTimeout(checkbinaries, 500);
}
};
function load_binaries() {
console.log("requesting binaries");
loadbinary("vmlinux-2.6.20.bin", 0);
loadbinary("root.bin", 1);
loadbinary("linuxstart.bin", 2);
console.log("waiting for binaries to finish loading...");
checkbinaries();
}
function start() function start()
{ {
var start_addr, initrd_size, params, cmdline_addr; var start_addr, initrd_size, params, cmdline_addr;
@ -70,12 +137,12 @@ function start()
pc = new PCEmulator(params); pc = new PCEmulator(params);
pc.load_binary("vmlinux-2.6.20.bin", 0x00100000); pc.load_binary(binaries[0], 0x00100000);
initrd_size = pc.load_binary("root.bin", 0x00400000); initrd_size = pc.load_binary(binaries[1], 0x00400000);
start_addr = 0x10000; start_addr = 0x10000;
pc.load_binary("linuxstart.bin", start_addr); pc.load_binary(binaries[2], start_addr);
/* set the Linux kernel command line */ /* set the Linux kernel command line */
/* Note: we don't use initramfs because it is not possible to /* Note: we don't use initramfs because it is not possible to