3

I'm trying to make a javascript emulator, and i'd like it to be very light, so I don't want to load the "ROMs" with jQuery and jDataView. Si I made my own ROM loader in pure JS.

It works pretty well (thanks to many topics on this site), but there's still a problem on IE, for which I couldn't find any help elsewhere.

Here's my JS code:

/**
* @param file - the path or URL of the ROM file to load. The file must be served with the Mime-Type: 'text/plain; charset=x-user-defined'
* @param callback - a function to call when the loading is complete. Its first parameter contains an array of numbers representing the ROM bytes.
*/
function loadRom(file, callback)
{
  var xhr = new XMLHttpRequest();                             // AJAX loader
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){                                  // When the file content is received
      var str = xhr.responseText;                             // Store it as a string
      var ch, bytes = [];
      for (var i = 0; i < str.length; i++){
        ch = str.charCodeAt(i);                               // Read each character
        bytes.push(ch & 0xFF);                                // Store the last byte of the character
      }
      callback(bytes);                                        // Call the callback function with bytes as parameter
    }
  };
  xhr.open("GET", file, true);                                // Load the file
  xhr.send(null);                                             // Send the AJAX request
}

// Test
var mem=[];
loadRom("TEST.ch8", function(m){
  for(i=0;i<m.length;i++)console.log(m[i].toString(16))                 // Log each byte in hexa
});

Here's my .htaccess (the chip8 ROMs have the extension .ch8):

AddType 'text/plain; charset=x-user-defined' ch8

And here's my test ROM (it contains the bytes 0x00 to 0xFF)

http://www.filedropper.com/test_16

Results of the test:

Firefox and Chrome work fine: they log every byte from 0x00 to 0xFF

IE9 does the same, except for the 32 bytes between 0x80 to 0x9F, that are replaced with totally unrelated numbers. ( Even if we compare binary codes, there's no apparent conversion logic )

So my questions are:

  • What does IE9 do with these bytes?
  • How could I fix it?

Thanks for your ideas (or solutions)!

Max.

4

2 回答 2

1

您是否考虑过对数据进行 base-64 编码并在客户端对其进行解码?

于 2012-08-24T18:34:17.857 回答
1

我终于找到了答案:

  • 无论您做什么,IE 都会转换这些字符。

  • 但它提供了一种将 AJAX 响应直接导出到字节数组中的方法,这几乎比其他浏览器更酷

    var bytes = VBArray(xhr.responseBody).toArray(); // 仅在 IE 上!

所以这是将文件转换为字节数组的函数,直到 IE7!

function loadRom(path, memory, callback)
{
  var i = 0,                                                                                        // Loop iterator
      ie /*@cc_on=1@*/,                                                                             // IE detection with conditional compilation
      xhr = new XMLHttpRequest;                                                                     // XHR object
  xhr.onreadystatechange = function(){                                                              // When the XHR object state changes
    if(xhr.readyState > 3){                                                                         // When the file is received (readyState 4)
      for(xhr = ie ? VBArray(xhr.responseBody).toArray() : xhr.responseText; i < xhr.length; i++){  // Get the response text as a bytes array (on IE) or a string (on other browsers) and iterate
        memory.push(ie ? xhr[i] : xhr.charCodeAt(i) & 0xFF);                                        // Store in memory the byte (on IE) or the last byte of the character code (on other browsers)
      }
      callback()                                                                                    // Call the callback function
    }
  }
  xhr.open("GET", path);                                                                            // Load the file
  xhr.send()                                                                                        // Send the XHR request
}
于 2012-08-25T12:11:29.827 回答