Cleanup of branch instructions

This commit is contained in:
Preston Skupinski 2012-01-05 00:50:49 -05:00
parent 577b47a497
commit cdf374dcd0

259
cpu.js
View File

@ -28,6 +28,34 @@ var cpu_lib = {
} }
} }
}, },
branch: {
Branch: function(flag, branch_on) {
var always_branch = typeof flag === 'undefined';
this.bytes_required = function() {
return 2;
},
this.execute = function(cpu, bytes) {
cpu.cycle_count+=3;
if(cpu.p.e)
cpu.cycle_count++;
if(always_branch||(cpu.p[flag]==branch_on)) {
if(!always_branch)
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
}
},
inc: { inc: {
INC_register: function(register, flag, value) { INC_register: function(register, flag, value) {
if(typeof value === 'undefined') if(typeof value === 'undefined')
@ -2174,236 +2202,23 @@ var ADC_stack_relative = new cpu_lib.addressing.Stack_relative(ADC_const);
var ADC_stack_relative_indirect_indexed_y = var ADC_stack_relative_indirect_indexed_y =
new cpu_lib.addressing.Stack_relative_indirect_indexed_y(ADC_const); new cpu_lib.addressing.Stack_relative_indirect_indexed_y(ADC_const);
var BMI = { var BMI = new cpu_lib.branch.Branch('n', true);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e) var BPL = new cpu_lib.branch.Branch('n', false);
cpu.cycle_count++;
if(cpu.p.n) { var BVC = new cpu_lib.branch.Branch('v', false);
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument. var BVS = new cpu_lib.branch.Branch('v', true);
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BPL = { var BCC = new cpu_lib.branch.Branch('c', false);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e) var BCS = new cpu_lib.branch.Branch('c', true);
cpu.cycle_count++;
if(!cpu.p.n) { var BEQ = new cpu_lib.branch.Branch('z', true);
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument. var BNE = new cpu_lib.branch.Branch('z', false);
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BVC = { var BRA = new cpu_lib.branch.Branch();
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e)
cpu.cycle_count++;
if(!cpu.p.v) {
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BVS = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e)
cpu.cycle_count++;
if(cpu.p.v) {
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BCC = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e)
cpu.cycle_count++;
if(!cpu.p.c) {
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BCS = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e)
cpu.cycle_count++;
if(cpu.p.c) {
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BEQ = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e)
cpu.cycle_count++;
if(cpu.p.z) {
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BNE = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e)
cpu.cycle_count++;
if(!cpu.p.z) {
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
}
};
var BRA = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=3;
if(cpu.p.e)
cpu.cycle_count++;
// Handle single byte two's complement numbers as the branch argument.
if(bytes[0]<=127) {
cpu.r.pc+=bytes[0];
cpu.r.pc&=0xffff;
} else {
cpu.r.pc-=256-bytes[0];
if(cpu.r.pc<0)
cpu.r.pc+=0xffff;
}
}
};
var BRL = { var BRL = {
bytes_required:function() { bytes_required:function() {