From b505f7a50da6b8a513ed93c6065258a7c7198b14 Mon Sep 17 00:00:00 2001 From: Preston Skupinski Date: Sat, 3 Sep 2011 22:07:44 -0400 Subject: [PATCH] handle the carry bit properly when in decimal mode for adc --- cpu.js | 8 ++++++-- test/tests.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cpu.js b/cpu.js index 051fb8a..aafbf01 100644 --- a/cpu.js +++ b/cpu.js @@ -3620,11 +3620,13 @@ var ADC_const = { ones = bytes[0] & 0x0f; tens = bytes[0] >>4; var dec_arg = (tens*10)+ones; - var result = dec_a + dec_arg; + var result = dec_a + dec_arg + cpu.p.c; // Check for decimal overflow. if(result>99) { result -= 99; cpu.p.c = 1; + } else { + cpu.p.c = 0; } var digits = result.toString(10).split(""); var i = 0; @@ -3667,11 +3669,13 @@ var ADC_const = { hundreds = (argument >> 8) & 0xf; thousands = (argument >> 12) & 0xf; var dec_arg = (thousands*1000)+(hundreds*100)+(tens*10)+ones; - var result = dec_a + dec_arg; + var result = dec_a + dec_arg + cpu.p.c; // Check for decimal overflow. if(result>9999) { result -= 9999; cpu.p.c = 1; + } else { + cpu.p.c = 0; } var digits = result.toString(10).split(""); var i = 0; diff --git a/test/tests.js b/test/tests.js index 99b7dc7..26854af 100644 --- a/test/tests.js +++ b/test/tests.js @@ -648,6 +648,23 @@ function test_adc() { equals(cpu.p.e, 0, "The hidden e flag of the p status register should "+ "be 0 for native mode."); }); + test("Test that ADC handles decimal mode with legal BCD numbers in 8-bit "+ + "memory/accumulator mode with double digit numbers with the carry "+ + "bit set", function() { + var cpu = new CPU_65816(); + cpu.execute("18fb38f8a9156926"); + equals(cpu.r.a, 0x42, "0x15 + 0x26 should result in 0x42 with decimal "+ + "mode on and with 8-bit memory/accumulator mode "+ + "and the carry bit set."); + equals(cpu.p.c, 0, "The carry flag of the p status register should be "+ + "clear after no decimal overflow."); + equals(cpu.p.d, 1, "Decimal mode should be set to 1 in the p status "+ + "register."); + equals(cpu.p.m, 1, "The m flag of the p status register should be 1 for "+ + "8-bit memory/accumulator mode."); + equals(cpu.p.e, 0, "The hidden e flag of the p status register should "+ + "be 0 for native mode."); + }); test("Test that ADC handles decimal mode with legal BCD numbers in 8-bit "+ "memory/accumulator mode when adding two numbers that cause an "+ "overflow.", function() { @@ -715,6 +732,23 @@ function test_adc() { equals(cpu.p.e, 0, "The hidden e flag of the p status register should "+ "be 0 for native mode."); }); + test("Test that ADC handles decimal mode with legal BCD numbers in 16-bit "+ + "memory/accumulator mode when adding two four digit numbers with the "+ + "carry bit set.", function() { + var cpu = new CPU_65816(); + cpu.execute("18fb38f8c220a90110699939"); + equals(cpu.r.a, 0x5001, "0x1001 + 0x3999 should result in 0x5001 with "+ + "decimal mode on and with 16-bit "+ + "memory/accumulator mode and the carry flag set."); + equals(cpu.p.c, 0, "The carry flag of the p status register should be "+ + "clear after no decimal overflow."); + equals(cpu.p.d, 1, "Decimal mode should be set to 1 in the p status "+ + "register."); + equals(cpu.p.m, 0, "The m flag of the p status register should be 0 for "+ + "16-bit memory/accumulator mode."); + equals(cpu.p.e, 0, "The hidden e flag of the p status register should "+ + "be 0 for native mode."); + }); } function test_branching() {