check for signed overflow with ADC

This commit is contained in:
Preston Skupinski 2011-05-07 12:17:14 -04:00
parent 676b111870
commit 861a8183b2
2 changed files with 59 additions and 2 deletions

23
cpu.js
View File

@ -131,6 +131,7 @@ var ADC_const = {
execute:function(cpu, bytes) {
// TODO: Signed overflow checking.
if(cpu.p.m) {
var old_a = cpu.r.a;
cpu.r.a += bytes[0] + cpu.p.c;
if(cpu.r.a & 0x100) {
cpu.p.c = 1;
@ -139,8 +140,19 @@ var ADC_const = {
}
cpu.r.a &= 0xff;
cpu.p.n = cpu.r.a >> 7;
// Check for signed overflow.
// If they started with the same sign and then the resulting sign is
// different then we have a signed overflow.
if((!((old_a ^ bytes[0]) & 0x80)) && ((cpu.r.a ^ old_a) & 0x80)) {
cpu.p.v = 1;
} else {
cpu.p.v = 0;
}
} else {
cpu.r.a += (bytes[1]<<8)|bytes[0] + cpu.p.c;
var old_a = cpu.r.a;
var argument = (bytes[1]<<8)|bytes[0];
cpu.r.a += argument + cpu.p.c;
if(cpu.r.a & 0x10000) {
cpu.p.c = 1;
} else {
@ -148,6 +160,15 @@ var ADC_const = {
}
cpu.r.a &= 0xffff;
cpu.p.n = cpu.r.a >> 15;
// Check for signed overflow.
// If they started with the same sign and then the resulting sign is
// different then we have a signed overflow.
if((!((old_a ^ argument) & 0x8000)) && ((cpu.r.a ^ old_a) & 0x8000)) {
cpu.p.v = 1;
} else {
cpu.p.v = 0;
}
}
if(cpu.r.a===0) {

View File

@ -23,7 +23,6 @@ function run_tests() {
function test_adc() {
module("ADC");
// TODO: signed overflow testing.
test("Test normal addition of two 16-bit numbers that don't cause an "+
"overflow (m bit is 0)", function() {
var cpu = new CPU_65816();
@ -36,6 +35,8 @@ function test_adc() {
"adding with ADC");
equals(cpu.p.z, 0, "0x0001 + 0x0001 should not set the zero(z) bit when "+
"adding with ADC");
equals(cpu.p.v, 0, "0x0001 + 0x0001 should not set the overflow(v) bit "+
"when adding with ADC");
});
test("Test normal addition of two 8-bit numbers that don't cause an "+
"overflow (m bit is 1)", function() {
@ -49,6 +50,8 @@ function test_adc() {
"adding with ADC");
equals(cpu.p.z, 0, "0x01 + 0x01 should not set the zero(z) bit when "+
"adding with ADC");
equals(cpu.p.v, 0, "0x01 + 0x01 should not set the overflow(v) bit "+
"when adding with ADC");
});
test("Test that overflow sets the carry flag and works in general with two"+
@ -63,6 +66,8 @@ function test_adc() {
"when using ADC");
equals(cpu.p.z, 1, "0xffff + 0x0001 should set the zero(z) bit when using "+
"ADC");
equals(cpu.p.v, 0, "0xffff + 0x0001 should not set the overflow(v) bit "+
"when using ADC");
});
test("Test that overflow sets the carry flag and works in general with two"+
@ -77,6 +82,37 @@ function test_adc() {
"using ADC");
equals(cpu.p.z, 1, "0xff + 0x01 should set the zero(z) bit when using "+
"ADC");
equals(cpu.p.v, 0, "0xff + 0x01 should not set the overflow(v) bit when "+
"using ADC");
});
test("Test signed overflow with two 8-bit numbers (m bit is 1)", function() {
var cpu = new CPU_65816();
cpu.execute("18fb18e230a97f6901");
equals(cpu.r.a, 0x80, "0x7f + 0x01 should result in 0x80 when "+
"using ADC");
equals(cpu.p.v, 1, "0x7f + 0x01 should set the overflow(v) bit when "+
"using ADC");
equals(cpu.p.c, 0, "0x7f + 0x01 should not set the carry(c) bit when "+
"using ADC");
equals(cpu.p.z, 0, "0x7f + 0x01 should not set the zero(z) bit when "+
"using ADC");
equals(cpu.p.n, 1, "0x7f + 0x01 should set the negative(n) bit when "+
"using ADC");
});
test("Test signed overflow with two 16-bit numbers (m bit is 0)", function() {
var cpu = new CPU_65816();
cpu.execute("18fb18c230a9ff7f690100");
equals(cpu.r.a, 0x8000, "0x7fff + 0x0001 should result in 0x8000 when "+
"using ADC");
equals(cpu.p.v, 1, "0x7fff + 0x0001 should set the overflow(v) bit when "+
"using ADC");
equals(cpu.p.c, 0, "0x7fff + 0x0001 should not set the carry(c) bit when "+
"using ADC");
equals(cpu.p.z, 0, "0x7fff + 0x0001 should not set the zero(z) bit when "+
"using ADC");
equals(cpu.p.n, 1, "0x7fff + 0x0001 should set the negative(n) bit when "+
"using ADC");
});
}