From 26ea82204ff7caead5ecfa17d5903ca014983c95 Mon Sep 17 00:00:00 2001 From: Preston Skupinski Date: Tue, 27 Dec 2011 19:33:14 -0500 Subject: [PATCH] added the ability for the cpu to load in an array of bytes --- cpu.js | 26 ++++++++++++++++++-------- test/tests.js | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/cpu.js b/cpu.js index 5a0dd71..46aadf8 100755 --- a/cpu.js +++ b/cpu.js @@ -6630,22 +6630,32 @@ window.CPU_65816 = function() { /** * Load given program into memory and prepare for execution. + * raw_hex could either be a string of hex numbers or an array + * of bytes. */ this.load_binary = function(raw_hex, memory_location_start, bank) { - var byte_buffer = []; + var byte_buffer = [], + i = 0; if(typeof bank === "undefined") { bank = 0; } - for(var i = 0; i < raw_hex.length; i++) { - byte_buffer.push(raw_hex.charAt(i)); - if(byte_buffer.length===2) { - this.mmu.store_byte_long(memory_location_start, bank, - parseInt(byte_buffer[0]+byte_buffer[1], - 16)); + if(typeof raw_hex === 'string') { + for(;i < raw_hex.length; i++) { + byte_buffer.push(raw_hex.charAt(i)); + if(byte_buffer.length===2) { + this.mmu.store_byte_long(memory_location_start, bank, + parseInt(byte_buffer[0]+byte_buffer[1], + 16)); + memory_location_start++; + byte_buffer = []; + } + } + } else { + for(;i < raw_hex.length; i++) { + this.mmu.store_byte_long(memory_location_start, bank, raw_hex[i]); memory_location_start++; - byte_buffer = []; } } }; diff --git a/test/tests.js b/test/tests.js index 144d6a0..0b63c68 100644 --- a/test/tests.js +++ b/test/tests.js @@ -26,6 +26,7 @@ function run_tests() { test_subroutines(); test_mvn_and_mvp(); test_emulation_mode(); + test_cpu_load_binary(); } function test_lda() { @@ -1142,3 +1143,23 @@ function test_rep() { "register"); }); } + +function test_cpu_load_binary() { + test("Make sure that load binary can work with hex strings", function() { + var cpu = new CPU_65816(); + cpu.load_binary("18fb", 0x8000); + equal(cpu.mmu.memory[cpu.r.k][0x8000], 0x18, + "$8000 should equal 0x18"); + equal(cpu.mmu.memory[cpu.r.k][0x8001], 0xfb, + "$8001 should equal 0xfb"); + }); + + test("Make sure that load_binary can work with arrays", function() { + var cpu = new CPU_65816(); + cpu.load_binary([0x18, 0xfb], 0x8000); + equal(cpu.mmu.memory[cpu.r.k][0x8000], 0x18, + "$8000 should equal 0x18"); + equal(cpu.mmu.memory[cpu.r.k][0x8001], 0xfb, + "$8001 should equal 0xfb"); + }); +}