3

私は JavaScript エミュレーターを作成しようとしていますが、非常に軽量にしたいので、jQuery と jDataView で「ROM」をロードしたくありません。私は純粋な JS で独自の ROM ローダーを作成しました。

これはかなりうまく機能します (このサイトの多くのトピックのおかげです) が、IE にはまだ問題があり、他の場所でヘルプを見つけることができませんでした。

これが私のJSコードです:

/**
* @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
});

これが私の .htaccess です (chip8 ROM の拡張子は .ch8 です):

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

そして、これが私のテストROMです(バイト0x00から0xFFが含まれています)

http://www.filedropper.com/test_16

テストの結果:

Firefox と Chrome は正常に動作します: 0x00 から 0xFF までのすべてのバイトをログに記録します

IE9 も同じことを行いますが、0x80 から 0x9F までの 32 バイトはまったく無関係な数値に置き換えられます。(バイナリコードを比較しても明らかな変換ロジックはありません)

だから私の質問は:

  • IE9 はこれらのバイトで何をしますか?
  • どうすれば修正できますか?

あなたのアイデア(または解決策)をありがとう!

最大。

4

2 に答える 2

1

base-64 でデータをエンコードし、クライアント側でデコードすることを検討しましたか?

于 2012-08-24T18:34:17.857 に答える
1

私はついに答えを見つけました:

  • IE は、ユーザーが何をしても、これらの文字を変換します。

  • しかし、AJAX 応答をバイト配列で直接エクスポートする方法を提供します。これは、他のブラウザーよりも優れています。

    var バイト = 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 に答える