move common addressing functionality for direct page and absolute to cpu_lib and use that as the basis for instructions that utilize that addressing mode

This commit is contained in:
Preston Skupinski 2011-12-31 14:38:14 -05:00
parent 4c5c5dc631
commit 65b7448052
1 changed files with 76 additions and 474 deletions

550
cpu.js
View File

@ -27,6 +27,45 @@ var cpu_lib = {
} }
} }
} }
},
addressing: {
Direct_page: function(instruction) {
this.bytes_required = function() {
return 2;
};
this.execute = function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
instruction.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
instruction.execute(cpu, [cpu.mmu.read_byte(memory_location),
cpu.mmu.read_byte(memory_location+1)]);
}
};
},
Absolute: function(instruction) {
this.bytes_required = function() {
return 3;
};
this.execute = function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
instruction.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
instruction.execute(cpu, [cpu.mmu.read_byte(memory_location),
cpu.mmu.read_byte(memory_location+1)]);
}
};
}
} }
}; };
@ -220,44 +259,9 @@ var BIT_const = {
} }
}; };
var BIT_absolute = { var BIT_absolute = new cpu_lib.addressing.Absolute(BIT_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0]; var BIT_direct_page = new cpu_lib.addressing.Direct_page(BIT_const);
if(cpu.p.e||cpu.p.m) {
BIT_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
BIT_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var BIT_direct_page = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
BIT_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
BIT_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var BIT_direct_page_indexed_x = { var BIT_direct_page_indexed_x = {
bytes_required:function() { bytes_required:function() {
@ -1269,23 +1273,7 @@ var EOR_const = {
} }
}; };
var EOR_absolute = { var EOR_absolute = new cpu_lib.addressing.Absolute(EOR_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
EOR_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
EOR_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var EOR_absolute_long = { var EOR_absolute_long = {
bytes_required:function() { bytes_required:function() {
@ -1333,26 +1321,7 @@ var EOR_absolute_long_indexed_x = {
} }
}; };
var EOR_direct_page = { var EOR_direct_page = new cpu_lib.addressing.Direct_page(EOR_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
EOR_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
EOR_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var EOR_direct_page_indirect = { var EOR_direct_page_indirect = {
bytes_required:function() { bytes_required:function() {
@ -1643,23 +1612,7 @@ var ORA_const = {
} }
}; };
var ORA_absolute = { var ORA_absolute = new cpu_lib.addressing.Absolute(ORA_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
ORA_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
ORA_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var ORA_absolute_long = { var ORA_absolute_long = {
bytes_required:function() { bytes_required:function() {
@ -1708,26 +1661,7 @@ var ORA_absolute_long_indexed_x = {
} }
}; };
var ORA_direct_page = { var ORA_direct_page = new cpu_lib.addressing.Direct_page(ORA_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
ORA_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
ORA_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var ORA_direct_page_indirect = { var ORA_direct_page_indirect = {
bytes_required:function() { bytes_required:function() {
@ -2019,23 +1953,7 @@ var AND_const = {
} }
}; };
var AND_absolute = { var AND_absolute = new cpu_lib.addressing.Absolute(AND_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
AND_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
AND_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var AND_absolute_long = { var AND_absolute_long = {
bytes_required:function() { bytes_required:function() {
@ -2083,26 +2001,7 @@ var AND_absolute_long_indexed_x = {
} }
}; };
var AND_direct_page = { var AND_direct_page = new cpu_lib.addressing.Direct_page(AND_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
AND_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
AND_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var AND_direct_page_indirect = { var AND_direct_page_indirect = {
bytes_required:function() { bytes_required:function() {
@ -2408,44 +2307,9 @@ var CPX_const = {
} }
}; };
var CPX_direct_page = { var CPX_direct_page = new cpu_lib.addressing.Direct_page(CPX_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0) var CPX_absolute = new cpu_lib.addressing.Absolute(CPX_const);
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
CPX_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
CPX_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var CPX_absolute = {
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.x) {
CPX_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
CPX_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var CPY_const = { var CPY_const = {
bytes_required:function(cpu) { bytes_required:function(cpu) {
@ -2484,44 +2348,9 @@ var CPY_const = {
} }
}; };
var CPY_direct_page = { var CPY_direct_page = new cpu_lib.addressing.Direct_page(CPY_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0) var CPY_absolute = new cpu_lib.addressing.Absolute(CPY_const);
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
CPY_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
CPY_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var CPY_absolute = {
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.x) {
CPY_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
CPY_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var CMP_const = { var CMP_const = {
bytes_required:function(cpu) { bytes_required:function(cpu) {
@ -2561,26 +2390,7 @@ var CMP_const = {
} }
}; };
var CMP_direct_page = { var CMP_direct_page = new cpu_lib.addressing.Direct_page(CMP_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
CMP_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
CMP_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var CMP_direct_page_indexed_x = { var CMP_direct_page_indexed_x = {
bytes_required:function() { bytes_required:function() {
@ -2737,23 +2547,7 @@ var CMP_direct_page_indirect_indexed_y = {
} }
}; };
var CMP_absolute = { var CMP_absolute = new cpu_lib.addressing.Absolute(CMP_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
CMP_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
CMP_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var CMP_absolute_long = { var CMP_absolute_long = {
bytes_required:function() { bytes_required:function() {
@ -3031,44 +2825,9 @@ var SBC_const = {
} }
}; };
var SBC_direct_page = { var SBC_direct_page = new cpu_lib.addressing.Direct_page(SBC_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count++;
if((cpu.r.d&0xff)!==0) var SBC_absolute = new cpu_lib.addressing.Absolute(SBC_const);
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
SBC_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
SBC_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var SBC_absolute = {
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=2;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
SBC_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
SBC_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var SBC_absolute_long = { var SBC_absolute_long = {
bytes_required:function() { bytes_required:function() {
@ -3488,22 +3247,7 @@ var ADC_const = {
} }
}; };
var ADC_absolute = { var ADC_absolute = new cpu_lib.addressing.Absolute(ADC_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
var memory_location = (bytes[1]<<8)|bytes[0];
cpu.cycle_count+=2;
if(cpu.p.e||cpu.p.m) {
ADC_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
ADC_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var ADC_absolute_long = { var ADC_absolute_long = {
bytes_required:function() { bytes_required:function() {
@ -3549,26 +3293,7 @@ var ADC_absolute_long_indexed_x = {
} }
}; };
var ADC_direct_page = { var ADC_direct_page = new cpu_lib.addressing.Direct_page(ADC_const);
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
if((cpu.r.d&0x00ff)!==0)
cpu.cycle_count+=2;
else
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) {
ADC_const.execute(cpu, [cpu.mmu.read_byte(memory_location)]);
} else {
var low_byte = cpu.mmu.read_byte(memory_location),
high_byte = cpu.mmu.read_byte(memory_location+1);
ADC_const.execute(cpu, [low_byte, high_byte]);
}
}
};
var ADC_direct_page_indirect = { var ADC_direct_page_indirect = {
bytes_required:function() { bytes_required:function() {
@ -5070,50 +4795,9 @@ var LDY_direct_page_indexed_x = {
} }
}; };
var LDY_absolute = { var LDY_absolute = new cpu_lib.addressing.Absolute(LDY_const);
bytes_required:function() {
return 3;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=4;
var memory_location = (bytes[1]<<8)|bytes[0]; var LDY_direct_page = new cpu_lib.addressing.Direct_page(LDY_const);
if(cpu.p.e||cpu.p.x) {
cpu.r.y = cpu.mmu.read_byte(memory_location);
cpu.p.n = cpu.r.y >> 7;
} else {
cpu.cycle_count++;
cpu.r.y = cpu.mmu.read_word(memory_location);
cpu.p.n = cpu.r.y >> 15;
}
cpu_lib.r.p.check_z(cpu, cpu.r.y);
}
};
var LDY_direct_page = {
bytes_required:function() {
return 2;
},
execute:function(cpu, bytes) {
cpu.cycle_count+=3;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.x) {
cpu.r.y = cpu.mmu.read_byte(memory_location);
cpu.p.n = cpu.r.y >> 7;
} else {
cpu.cycle_count++;
cpu.r.y = cpu.mmu.read_word(memory_location);
cpu.p.n = cpu.r.y >> 15;
}
cpu_lib.r.p.check_z(cpu, cpu.r.y);
}
};
var DEX = { var DEX = {
bytes_required:function() { bytes_required:function() {
@ -5842,30 +5526,6 @@ var STA_absolute = {
} }
}; };
var LDX_direct_page = {
bytes_required: function() {
return 2;
},
execute: function(cpu, bytes) {
cpu.cycle_count+=3;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = cpu.r.d + bytes[0];
if(cpu.p.e||cpu.p.x) {
cpu.r.x = cpu.mmu.read_byte(memory_location);
cpu.p.n = cpu.r.x >> 7;
} else {
cpu.cycle_count++;
cpu.r.x = cpu.mmu.read_word(memory_location);
cpu.p.n = cpu.r.x >> 15;
}
cpu_lib.r.p.check_z(cpu, cpu.r.x);
}
};
var LDX_direct_page_indexed_y = { var LDX_direct_page_indexed_y = {
bytes_required: function() { bytes_required: function() {
return 2; return 2;
@ -5890,30 +5550,32 @@ var LDX_direct_page_indexed_y = {
} }
}; };
var LDA_direct_page = { var LDA_const = {
bytes_required: function() { bytes_required: function(cpu) {
return 2; if(cpu.p.e||cpu.p.m) {
return 2;
} else {
return 3;
}
}, },
execute: function(cpu, bytes) { execute: function(cpu, bytes) {
cpu.cycle_count+=3; cpu.cycle_count+=2;
if((cpu.r.d&0xff)!==0)
cpu.cycle_count++;
var memory_location = bytes[0] + cpu.r.d;
if(cpu.p.e||cpu.p.m) { if(cpu.p.e||cpu.p.m) {
cpu.r.a = cpu.mmu.read_byte(memory_location); cpu.r.a = bytes[0];
cpu.p.n = cpu.r.a >> 7; cpu.p.n = cpu.r.a >> 7;
} else { } else {
cpu.cycle_count++; cpu.cycle_count++;
cpu.r.a = cpu.mmu.read_word(memory_location); cpu.r.a = (bytes[1]<<8)|bytes[0];
cpu.p.n = cpu.r.a >> 15; cpu.p.n = cpu.r.a >> 15;
} }
cpu_lib.r.p.check_z(cpu, cpu.r.a); cpu_lib.r.p.check_z(cpu, cpu.r.a);
} }
}; };
var LDA_direct_page = new cpu_lib.addressing.Direct_page(LDA_const);
var LDX_absolute_indexed_y = { var LDX_absolute_indexed_y = {
bytes_required: function() { bytes_required: function() {
return 3; return 3;
@ -5940,27 +5602,6 @@ var LDX_absolute_indexed_y = {
} }
}; };
var LDX_absolute = {
bytes_required: function() {
return 3;
},
execute: function(cpu, bytes) {
cpu.cycle_count+=4;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.x) {
cpu.r.x = cpu.mmu.read_byte(memory_location);
cpu.p.n = cpu.r.x >> 7;
} else {
cpu.cycle_count++;
cpu.r.x = cpu.mmu.read_word(memory_location);
cpu.p.n = cpu.r.x >> 15;
}
cpu_lib.r.p.check_z(cpu, cpu.r.x);
}
};
var LDA_absolute_long = { var LDA_absolute_long = {
bytes_required: function() { bytes_required: function() {
return 4; return 4;
@ -6015,50 +5656,7 @@ var LDA_absolute_long_indexed_x = {
} }
}; };
var LDA_absolute = { var LDA_absolute = new cpu_lib.addressing.Absolute(LDA_const);
bytes_required: function() {
return 3;
},
execute: function(cpu, bytes) {
cpu.cycle_count+=4;
var memory_location = (bytes[1]<<8)|bytes[0];
if(cpu.p.e||cpu.p.m) {
cpu.r.a = cpu.mmu.read_byte(memory_location);
cpu.p.n = cpu.r.a >> 7;
} else {
cpu.cycle_count++;
cpu.r.a = cpu.mmu.read_word(memory_location);
cpu.p.n = cpu.r.a >> 15;
}
cpu_lib.r.p.check_z(cpu, cpu.r.a);
}
};
var LDA_const = {
bytes_required: function(cpu) {
if(cpu.p.e||cpu.p.m) {
return 2;
} else {
return 3;
}
},
execute: function(cpu, bytes) {
cpu.cycle_count+=2;
if(cpu.p.e||cpu.p.m) {
cpu.r.a = bytes[0];
cpu.p.n = cpu.r.a >> 7;
} else {
cpu.cycle_count++;
cpu.r.a = (bytes[1]<<8)|bytes[0];
cpu.p.n = cpu.r.a >> 15;
}
cpu_lib.r.p.check_z(cpu, cpu.r.a);
}
};
var LDX_const = { var LDX_const = {
bytes_required: function(cpu) { bytes_required: function(cpu) {
@ -6084,6 +5682,10 @@ var LDX_const = {
} }
}; };
var LDX_direct_page = new cpu_lib.addressing.Direct_page(LDX_const);
var LDX_absolute = new cpu_lib.addressing.Absolute(LDX_const);
// Set bits in the p status register as specified by 1's in the position // Set bits in the p status register as specified by 1's in the position
// that represents each register. // that represents each register.
var SEP = { var SEP = {