added CMP immediate and fixed a misunderstanding as to when the carry bit is set or not set with SBC/CMP

This commit is contained in:
Preston Skupinski 2011-05-07 18:18:25 -04:00
parent 146992b192
commit 96022047d6
2 changed files with 50 additions and 11 deletions

49
cpu.js
View File

@ -84,7 +84,8 @@ function CPU_65816() {
0x65 : ADC_direct_page, 0x72 : ADC_direct_page_indirect,
0x7d : ADC_absolute_indexed_x,
0x79 : ADC_absolute_indexed_y,
0x75 : ADC_direct_page_indexed_x, 0xe9 : SBC_const };
0x75 : ADC_direct_page_indexed_x, 0xe9 : SBC_const,
0xc9 : CMP_const };
/**
* Take a raw hex string representing the program and execute it.
@ -163,6 +164,44 @@ var MMU = {
}
};
var CMP_const = {
bytes_required:function(cpu) {
if(cpu.p.m) {
return 2;
} else {
return 3;
}
},
execute:function(cpu, bytes) {
var result;
if(cpu.p.m) {
result = cpu.r.a - bytes[0];
if(result<0) {
cpu.p.c = 0;
result = 0x100 + result;
} else {
cpu.p.c = 1;
}
cpu.p.n = result >> 7;
} else {
result = cpu.r.a - ((bytes[1]<<8)|bytes[0]);
if(result<0) {
cpu.p.c = 0;
result = 0x10000 + result;
} else {
cpu.p.c = 1;
}
cpu.p.n = result >> 15;
}
if(result===0) {
cpu.p.z = 1;
} else {
cpu.p.z = 0;
}
}
};
var SBC_const = {
bytes_required:function(cpu) {
if(cpu.p.m) {
@ -176,10 +215,10 @@ var SBC_const = {
if(cpu.p.m) {
cpu.r.a -= bytes[0] - cpu.p.c;
if(cpu.r.a < 0) {
cpu.p.c = 1;
cpu.p.c = 0;
cpu.r.a = 0x100 + cpu.r.a;
} else {
cpu.p.c = 0;
cpu.p.c = 1;
}
cpu.p.n = cpu.r.a >> 7;
@ -195,10 +234,10 @@ var SBC_const = {
var argument = (bytes[1]<<8)|bytes[0];
cpu.r.a -= argument - cpu.p.c;
if(cpu.r.a < 0) {
cpu.p.c = 1;
cpu.p.c = 0;
cpu.r.a = 0x10000 + cpu.r.a;
} else {
cpu.p.c = 0;
cpu.p.c = 1;
}
cpu.p.n = cpu.r.a >> 15;

View File

@ -36,8 +36,8 @@ function test_sbc() {
"when using SBC");
equals(cpu.p.v, 0, "0x01 - 0x01 should not set the overflow(v) bit "+
"when using SBC");
equals(cpu.p.c, 0, "0x01 - 0x01 should not set the carry(c) bit when "+
"using SBC");
equals(cpu.p.c, 1, "0x01 - 0x01 should set the carry(c) bit when using "+
"SBC");
});
test("Test normal subtraction of two 16-bit numbers that don't cause a "+
@ -52,8 +52,8 @@ function test_sbc() {
"when using SBC");
equals(cpu.p.v, 0, "0x0001 - 0x0001 should not set the overflow(v) bit "+
"when using SBC");
equals(cpu.p.c, 0, "0x0001 - 0x0001 should not set the carry(c) bit "+
"when using SBC");
equals(cpu.p.c, 1, "0x0001 - 0x0001 should set the carry(c) bit when "+
"using SBC");
});
test("Test subtraction that triggers a borrow with 8-bit numbers",
function() {
@ -67,7 +67,7 @@ function test_sbc() {
"when using SBC");
equals(cpu.p.z, 0, "0xd0 - 0xef should not set the zero(z) bit when "+
"using SBC");
equals(cpu.p.c, 1, "0xd0 - 0xef should set the carry(c) bit when "+
equals(cpu.p.c, 0, "0xd0 - 0xef should not set the carry(c) bit when "+
"using SBC");
});
test("Test subtraction that triggers a borrow with 16-bit numbers",
@ -82,7 +82,7 @@ function test_sbc() {
"when using SBC");
equals(cpu.p.z, 0, "0xd000 - 0xef00 should not set the zero(z) bit when "+
"using SBC");
equals(cpu.p.c, 1, "0xd000 - 0xef00 should set the carry(c) bit when "+
equals(cpu.p.c, 0, "0xd000 - 0xef00 should not set the carry(c) bit when "+
"using SBC");
});
}