<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<script>
function Memory(arrayBuffer) {
this.arrayBuffer = arrayBuffer;
this.length = arrayBuffer.byteLength;
this.u8 = new Uint8Array(arrayBuffer);
}
Memory.prototype = {
readU32 : function (offset) {
return ((this.u8[offset] << 24) | (this.u8[offset+1] << 16) | (this.u8[offset+2] << 8) | this.u8[offset+3])>>>0;
},
write32 : function (offset, value) {
this.u8[offset ] = value >> 24;
this.u8[offset+1] = value >> 16;
this.u8[offset+2] = value >> 8;
this.u8[offset+3] = value;
}
};
function Device(mem, rangeStart) {
this.mem = mem;
this.rangeStart = rangeStart;
}
Device.prototype = {
read32 : function (address) {
var ea = address - this.rangeStart;
return this.mem.readU32(ea);
},
write32 : function (address, value) {
var ea = address - this.rangeStart;
this.mem.write32(ea, value);
}
};
var mem = new Memory(new ArrayBuffer(0x2000)); // NB doesn't repro with less than 0x2000?
var dev = new Device(mem, 0xa0000000);
// With this value, the test fails 100% of the time after 130-134 iterations.
var magic_value = 0xaaaaaaaa;
// NB with 31 bit value, e.g 0x04123456,
// the test fails about 25% of the time after 132 iterations.
//var magic_value = 0x0aaaaaaa;
function test(addr, val, i) {
dev.write32(addr, val);
var actual = dev.read32(addr);
if (actual!== magic_value) {
var o = addr - dev.rangeStart;
alert('test failed after ' + i + ' iterations.\n' +
'Expecting ' + magic_value.toString(16) + ' but got ' + actual.toString(16) + '\n' +
'Values: ' + dev.mem.u8[o+0].toString(16) + ' ' +
dev.mem.u8[o+1].toString(16) + ' ' +
dev.mem.u8[o+2].toString(16) + ' ' +
dev.mem.u8[o+2].toString(16)
);
return false;
}
return true;
}
var addr = 0xa0000000;
for(var i = 0; i < 100000; ++i) {
if (!test(addr, magic_value, i)) {
break;
}
}
</script>
</body>
</html>