Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 7th, 2012  |  syntax: HTML 5  |  size: 2.33 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="utf-8">
  5.   </head>
  6.   <body>
  7.     <script>
  8.  
  9.     function Memory(arrayBuffer) {
  10.       this.arrayBuffer = arrayBuffer;
  11.       this.length      = arrayBuffer.byteLength;
  12.       this.u8          = new Uint8Array(arrayBuffer);
  13.     }
  14.  
  15.     Memory.prototype = {
  16.       readU32 : function (offset) {
  17.         return ((this.u8[offset] << 24) | (this.u8[offset+1] << 16) | (this.u8[offset+2] << 8) | this.u8[offset+3])>>>0;
  18.       },
  19.  
  20.      write32 : function (offset, value) {
  21.         this.u8[offset  ] = value >> 24;
  22.         this.u8[offset+1] = value >> 16;
  23.         this.u8[offset+2] = value >>  8;
  24.         this.u8[offset+3] = value;
  25.       }
  26.     };
  27.  
  28.     function Device(mem, rangeStart) {
  29.       this.mem        = mem;
  30.       this.rangeStart = rangeStart;
  31.     }
  32.  
  33.     Device.prototype = {
  34.       read32 : function (address) {
  35.         var ea = address - this.rangeStart;
  36.         return this.mem.readU32(ea);
  37.       },
  38.  
  39.       write32 : function (address, value) {
  40.         var ea = address - this.rangeStart;
  41.         this.mem.write32(ea, value);
  42.       }
  43.     };
  44.  
  45.     var mem = new Memory(new ArrayBuffer(0x2000)); // NB doesn't repro with less than 0x2000?
  46.     var dev = new Device(mem, 0xa0000000);
  47.  
  48.     // With this value, the test fails 100% of the time after 130-134 iterations.
  49.     var magic_value = 0xaaaaaaaa;
  50.  
  51.     // NB with 31 bit value, e.g 0x04123456,
  52.     // the test fails about 25% of the time after 132 iterations.
  53.     //var magic_value = 0x0aaaaaaa;
  54.  
  55.     function test(addr, val, i) {
  56.       dev.write32(addr, val);
  57.  
  58.       var actual = dev.read32(addr);
  59.       if (actual!== magic_value) {
  60.         var o = addr - dev.rangeStart;
  61.         alert('test failed after ' + i + ' iterations.\n' +
  62.           'Expecting ' + magic_value.toString(16) + ' but got ' + actual.toString(16) + '\n' +
  63.           'Values: ' + dev.mem.u8[o+0].toString(16) + ' ' +
  64.                        dev.mem.u8[o+1].toString(16) + ' ' +
  65.                        dev.mem.u8[o+2].toString(16) + ' ' +
  66.                        dev.mem.u8[o+2].toString(16)
  67.       );
  68.         return false;
  69.       }
  70.       return true;
  71.     }
  72.  
  73.     var addr = 0xa0000000;
  74.  
  75.     for(var i = 0; i < 100000; ++i) {
  76.      if (!test(addr, magic_value, i)) {
  77.        break;
  78.      }
  79.    }
  80.  
  81.    </script>
  82.  
  83.   </body>
  84. </html>
clone this paste RAW Paste Data