Permalink
Browse files

Load ROMs dynamically via XHR.

  • Loading branch information...
1 parent 83fa893 commit 50eb13f155be09b5d6bcef0ddc77c7d74dccf226 @nelhage committed Jun 3, 2011
Showing with 55 additions and 14 deletions.
  1. +4 −6 beta.html
  2. +51 −8 betaweb.js
View
@@ -6,15 +6,13 @@
<script language="javascript" type="text/javascript" src="MMU.js"></script>
<script language="javascript" type="text/javascript" src="betaweb.js"></script>
<script language="javascript" type="text/javascript" src="sprintf.js"></script>
- <script language="javascript" type="text/javascript" src="tests/hello.js"></script>
- <script language="javascript" type="text/javascript" src="tests/lab8.js"></script>
- <script language="javascript" type="text/javascript" src="tests/litmus.js"></script>
+ <script language="javascript" type="text/javascript" src="lib/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="termlib/term_styles.css" />
<link rel="stylesheet" type="text/css" href="beta.css" />
<script language="javascript" type="text/javascript">
- betaROMs.push(new BetaROM("Lab 8", lab8_rom));
- betaROMs.push(new BetaROM("litmus", litmus_rom));
- betaROMs.push(new BetaROM("hello", hello_rom));
+ betaROMs.push(new BetaROM("Lab 8", "lab8"));
+ betaROMs.push(new BetaROM("litmus", "litmus"));
+ betaROMs.push(new BetaROM("hello", "hello"));
</script>
</head>
<body onload="initBeta();">
View
@@ -2,9 +2,44 @@ var betaTerm;
var betaDiv;
var betaROMs = [];
-function BetaROM(name, rom) {
- this.name = name;
- this.rom = rom;
+function BetaROM(desc, path) {
+ this.desc = desc;
+ this.path = path;
+ this.rom = null;
+}
+
+function parseROM(data) {
+ var rom = [];
+ var i, b;
+ var val;
+ for (i = 0; i < data.length; ) {
+ val = 0;
+ for (b = 0; b < 4; b++,i++) {
+ val += (data.charCodeAt(i) & 0xFF) << (8 * b);
+ }
+ rom.push(val);
+ }
+ return rom;
+}
+
+BetaROM.prototype.load = function (success, fail) {
+ var rom = this;
+ if (this.rom) {
+ success(this);
+ } else {
+ jQuery.get("tests/" + this.path + ".bin",
+ function (data, status, xhr) {
+ window.xhr = xhr;
+ if (data.length % 4) {
+ fail(rom, "Invalid length: " + data.length);
+ return;
+ }
+ rom.rom = parseROM(data);
+ success(rom);
+ }).error(function(xhr, textStatus, err) {
+ fail(this, err);
+ });
+ }
}
function initBeta() {
@@ -22,22 +57,30 @@ function initBeta() {
for (i = 0; i < betaROMs.length; i++) {
option = document.createElement('option');
option.value = i;
- option.appendChild(document.createTextNode(betaROMs[i].name));
+ option.appendChild(document.createTextNode(betaROMs[i].desc));
select.appendChild(option);
}
- resetBeta();
+ $.ajaxSetup({
+ beforeSend: function (xhr, settings) {
+ xhr.overrideMimeType('text/plain; charset=x-user-defined');
+ }
+ });
+
+ loadROM();
}
function loadROM() {
var select = document.getElementById('romselector');
var rom = betaROMs[select.children[select.selectedIndex].value];
- resetBeta(rom)
+ rom.load(function() {
+ resetBeta(rom);
+ }, function (rom, err) {
+ alert("Unable to load rom: " + err);
+ });
}
function resetBeta(rom) {
- if (rom === undefined)
- rom = betaROMs[0];
betaTerm.clear();
MMU.load(rom.rom);

0 comments on commit 50eb13f

Please sign in to comment.