From 5e4aac70d20760fc381e459bb730fe4f6e503bb2 Mon Sep 17 00:00:00 2001 From: Will Scullin Date: Sat, 23 Nov 2019 19:05:52 -0800 Subject: [PATCH] Use bit math more places. (#15) --- js/canvas.js | 73 ++++++++++++++++++++++-------------------- js/cards/ramfactor.js | 4 +-- js/cards/videoterm.js | 4 +-- js/ram.js | 10 ++---- js/roms/intbasic.js | 2 +- js/util.js | 2 +- test/roms/6502test.js | 4 +-- test/roms/65C02test.js | 4 +-- 8 files changed, 51 insertions(+), 52 deletions(-) diff --git a/js/canvas.js b/js/canvas.js index 766b75e..418bd6b 100644 --- a/js/canvas.js +++ b/js/canvas.js @@ -106,14 +106,15 @@ export function LoresPage(page, charset, e, context) data[off + 0] = data[off + 4] = c0; data[off + 1] = data[off + 5] = c1; data[off + 2] = data[off + 6] = c2; + var nextOff = off + 560 * 4; if (!scanlines) { - data[off + 560 * 4] = data[off + 560 * 4 + 4] = c0; - data[off + 560 * 4 + 1] = data[off + 560 * 4 + 5] = c1; - data[off + 560 * 4 + 2] = data[off + 560 * 4 + 6] = c2; + data[nextOff] = data[nextOff + 4] = c0; + data[nextOff + 1] = data[nextOff + 5] = c1; + data[nextOff + 2] = data[nextOff + 6] = c2; } else { - data[off + 560 * 4] = data[off + 560 * 4 + 4] = c0 >> 1; - data[off + 560 * 4 + 1] = data[off + 560 * 4 + 5] = c1 >> 1; - data[off + 560 * 4 + 2] = data[off + 560 * 4 + 6] = c2 >> 1; + data[nextOff] = data[nextOff + 4] = c0 >> 1; + data[nextOff + 1] = data[nextOff + 5] = c1 >> 1; + data[nextOff + 2] = data[nextOff + 6] = c2 >> 1; } } @@ -122,14 +123,15 @@ export function LoresPage(page, charset, e, context) data[off + 0] = c0; data[off + 1] = c1; data[off + 2] = c2; + var nextOff = off + 560 * 4; if (!scanlines) { - data[off + 560 * 4] = c0; - data[off + 560 * 4 + 1] = c1; - data[off + 560 * 4 + 2] = c2; + data[nextOff] = c0; + data[nextOff + 1] = c1; + data[nextOff + 2] = c2; } else { - data[off + 560 * 4] = c0 >> 1; - data[off + 560 * 4 + 1] = c1 >> 1; - data[off + 560 * 4 + 2] = c2 >> 1; + data[nextOff] = c0 >> 1; + data[nextOff + 1] = c1 >> 1; + data[nextOff + 2] = c2 >> 1; } } @@ -207,7 +209,7 @@ export function LoresPage(page, charset, e, context) var data = _imageData.data; if ((row < 24) && (col < 40)) { - var y = row * 16; + var y = row << 4; if (y < _dirty.top) { _dirty.top = y; } y += 16; if (y > _dirty.bottom) { _dirty.bottom = y; } @@ -553,14 +555,15 @@ export function HiresPage(page, context) data[off + 0] = data[off + 4] = c0; data[off + 1] = data[off + 5] = c1; data[off + 2] = data[off + 6] = c2; + var nextOff = off + 560 * 4; if (!scanlines) { - data[off + 560 * 4] = data[off + 560 * 4 + 4] = c0; - data[off + 560 * 4 + 1] = data[off + 560 * 4 + 5] = c1; - data[off + 560 * 4 + 2] = data[off + 560 * 4 + 6] = c2; + data[nextOff] = data[nextOff + 4] = c0; + data[nextOff + 1] = data[nextOff + 5] = c1; + data[nextOff + 2] = data[nextOff + 6] = c2; } else { - data[off + 560 * 4] = data[off + 560 * 4 + 4] = c0 >> 1; - data[off + 560 * 4 + 1] = data[off + 560 * 4 + 5] = c1 >> 1; - data[off + 560 * 4 + 2] = data[off + 560 * 4 + 6] = c2 >> 1; + data[nextOff] = data[nextOff + 4] = c0 >> 1; + data[nextOff + 1] = data[nextOff + 5] = c1 >> 1; + data[nextOff + 2] = data[nextOff + 6] = c2 >> 1; } } @@ -569,14 +572,16 @@ export function HiresPage(page, context) data[off + 0] = c0; data[off + 1] = c1; data[off + 2] = c2; + + var nextOff = off + 560 * 4; if (!scanlines) { - data[off + 560 * 4] = c0; - data[off + 560 * 4 + 1] = c1; - data[off + 560 * 4 + 2] = c2; + data[nextOff] = c0; + data[nextOff + 1] = c1; + data[nextOff + 2] = c2; } else { - data[off + 560 * 4] = c0 >> 1; - data[off + 560 * 4 + 1] = c1 >> 1; - data[off + 560 * 4 + 2] = c2 >> 1; + data[nextOff] = c0 >> 1; + data[nextOff + 1] = c1 >> 1; + data[nextOff + 2] = c2 >> 1; } } @@ -661,7 +666,7 @@ export function HiresPage(page, context) return; } - var y = rowa * 16 + rowb * 2; + var y = rowa << 4 | rowb << 1; if (y < _dirty.top) { _dirty.top = y; } y += 2; if (y > _dirty.bottom) { _dirty.bottom = y; } @@ -670,7 +675,7 @@ export function HiresPage(page, context) x += 14; if (x > _dirty.right) { _dirty.right = x; } - dy = rowa * 16 + rowb * 2; + dy = rowa << 4 | rowb << 1; var bz, b0, b1, b2, b3, b4, c, hb; if (doubleHiresMode) { // Every 4 bytes is 7 pixels @@ -681,13 +686,13 @@ export function HiresPage(page, context) // 76543210 76543210 76543210 76543210 // 1111222 2333344 4455556 6667777 - var mod = col % 2, mcol = col - mod; - bz = _buffer[0][base - mod - 1]; - b0 = _buffer[1][base - mod]; - b1 = _buffer[0][base - mod]; - b2 = _buffer[1][base - mod + 1]; - b3 = _buffer[0][base - mod + 1]; - b4 = _buffer[1][base - mod + 2]; + var mod = col % 2, mcol = col - mod, baseOff = base - mod; + bz = _buffer[0][baseOff - 1]; + b0 = _buffer[1][baseOff]; + b1 = _buffer[0][baseOff]; + b2 = _buffer[1][baseOff + 1]; + b3 = _buffer[0][baseOff + 1]; + b4 = _buffer[1][baseOff + 2]; c = [ 0, ((b0 & 0x0f) >> 0), // 0 diff --git a/js/cards/ramfactor.js b/js/cards/ramfactor.js index 7ac05a9..1b97edc 100644 --- a/js/cards/ramfactor.js +++ b/js/cards/ramfactor.js @@ -1159,9 +1159,9 @@ export default function RAMFactor(io, slot, size) { read: function ramfactor_read(page, off) { var result; if (page == 0xc0 + slot) { - result = rom[slot * 0x100 + off]; + result = rom[slot << 8 | off]; } else { - result = rom[_firmware * 0x1000 + (page - 0xC0) * 0x100 + off]; + result = rom[_firmware << 12 | (page - 0xC0) << 8 | off]; } return result; }, diff --git a/js/cards/videoterm.js b/js/cards/videoterm.js index e4dcfbf..1518ff0 100644 --- a/js/cards/videoterm.js +++ b/js/cards/videoterm.js @@ -493,8 +493,8 @@ export default function Videoterm(io, slot, context) { var row = (saddr / 80) & 0xff; var col = saddr % 80; var x = col * 7; - var y = row * 16; - var c = val * 16; + var y = row << 4; + var c = val << 4; var color; if (row < 25) { diff --git a/js/ram.js b/js/ram.js index cdf3ced..a196041 100644 --- a/js/ram.js +++ b/js/ram.js @@ -19,12 +19,6 @@ export default function RAM(sp, ep) { mem = allocMemPages(ep - sp + 1); - for (var page = 0; page <= ep; page++) { - for (var off = 0; off < 0x100; off++) { - mem[page * 0x100 + off] = 0; // Math.floor(Math.random()*256); - } - } - return { start: function() { return start_page; @@ -33,10 +27,10 @@ export default function RAM(sp, ep) { return end_page; }, read: function(page, off) { - return mem[(page - start_page) * 0x100 + off]; + return mem[(page - start_page) << 8 | off]; }, write: function(page, off, val) { - mem[(page - start_page) * 0x100 + off] = val; + mem[(page - start_page) << 8 | off] = val; }, getState: function() { diff --git a/js/roms/intbasic.js b/js/roms/intbasic.js index 7add7ca..f9824fb 100644 --- a/js/roms/intbasic.js +++ b/js/roms/intbasic.js @@ -1547,7 +1547,7 @@ export default function IntBASIC() return 0xff; }, read: function(page, off) { - return rom[((page - 0xD0) << 8) + off]; + return rom[(page - 0xD0) << 8 | off]; }, write: function() { } diff --git a/js/util.js b/js/util.js index 02b9ad9..7cf95c6 100644 --- a/js/util.js +++ b/js/util.js @@ -39,7 +39,7 @@ export function allocMem(size) { } export function allocMemPages(pages) { - return allocMem(pages * 0x100); + return allocMem(pages << 8); } export function bytify(ary) { diff --git a/test/roms/6502test.js b/test/roms/6502test.js index 0cf15fb..6c0192f 100644 --- a/test/roms/6502test.js +++ b/test/roms/6502test.js @@ -8204,10 +8204,10 @@ export default function Test6502() { return 0xff; }, read: function(page, off) { - return data[page * 0x100 + off]; + return data[page << 8 | off]; }, write: function(page, off, val) { - data[page * 0x100 + off] = val; + data[page << 8 | off] = val; } }; } diff --git a/test/roms/65C02test.js b/test/roms/65C02test.js index d56ab50..15e78e9 100644 --- a/test/roms/65C02test.js +++ b/test/roms/65C02test.js @@ -8204,10 +8204,10 @@ export default function Test65C02() { return 0xff; }, read: function(page, off) { - return data[page * 0x100 + off]; + return data[page << 8 | off]; }, write: function(page, off, val) { - data[page * 0x100 + off] = val; + data[page << 8 | off] = val; } }; }