我正在尝试制作一个JavaScript仿真器,并且希望它非常轻巧,所以我不想使用jQuery和jDataView加载“ROMs”。因此,我用纯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个字节,这些字节被替换成完全不相关的数字。(即使比较二进制代码,也没有明显的转换逻辑)
所以我的问题是:
1. IE9对这些字节做了什么? 2. 我该如何解决这个问题?
谢谢你们的想法(或解决方案)!
Max。