1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 23:52:26 +00:00

Moved operand to stack-local storage.

This commit is contained in:
Thomas Harte 2016-03-20 22:50:16 -04:00
parent 5966ac845f
commit 2f174b3a3e

View File

@ -550,6 +550,7 @@ template <class T> class Processor {
// to date in this stack frame only); which saves some complicated addressing // to date in this stack frame only); which saves some complicated addressing
unsigned int scheduleProgramsReadPointer = _scheduleProgramsReadPointer; unsigned int scheduleProgramsReadPointer = _scheduleProgramsReadPointer;
unsigned int scheduleProgramProgramCounter = _scheduleProgramProgramCounter; unsigned int scheduleProgramProgramCounter = _scheduleProgramProgramCounter;
uint8_t operand = _operand;
#define checkSchedule(op) \ #define checkSchedule(op) \
if(!_scheduledPrograms[scheduleProgramsReadPointer]) {\ if(!_scheduledPrograms[scheduleProgramsReadPointer]) {\
@ -615,7 +616,7 @@ template <class T> class Processor {
} break; } break;
case CycleFetchOperand: case CycleFetchOperand:
read_mem(_operand, _pc.full); read_mem(operand, _pc.full);
break; break;
case OperationDecodeOperation: case OperationDecodeOperation:
@ -639,7 +640,7 @@ template <class T> class Processor {
case CycleIncPCPushPCH: _pc.full++; // deliberate fallthrough case CycleIncPCPushPCH: _pc.full++; // deliberate fallthrough
case CyclePushPCH: push(_pc.bytes.high); break; case CyclePushPCH: push(_pc.bytes.high); break;
case CyclePushPCL: push(_pc.bytes.low); break; case CyclePushPCL: push(_pc.bytes.low); break;
case CyclePushOperand: push(_operand); break; case CyclePushOperand: push(operand); break;
case CyclePushA: push(_a); break; case CyclePushA: push(_a); break;
#undef push #undef push
@ -655,10 +656,10 @@ template <class T> class Processor {
case CyclePullPCL: _s++; read_mem(_pc.bytes.low, _s | 0x100); break; case CyclePullPCL: _s++; read_mem(_pc.bytes.low, _s | 0x100); break;
case CyclePullPCH: _s++; read_mem(_pc.bytes.high, _s | 0x100); break; case CyclePullPCH: _s++; read_mem(_pc.bytes.high, _s | 0x100); break;
case CyclePullA: _s++; read_mem(_a, _s | 0x100); break; case CyclePullA: _s++; read_mem(_a, _s | 0x100); break;
case CyclePullOperand: _s++; read_mem(_operand, _s | 0x100); break; case CyclePullOperand: _s++; read_mem(operand, _s | 0x100); break;
case OperationSetFlagsFromOperand: set_flags(_operand); break; case OperationSetFlagsFromOperand: set_flags(operand); break;
case OperationSetOperandFromFlagsWithBRKSet: _operand = get_flags() | Flag::Break; break; case OperationSetOperandFromFlagsWithBRKSet: operand = get_flags() | Flag::Break; break;
case OperationSetOperandFromFlags: _operand = get_flags(); break; case OperationSetOperandFromFlags: operand = get_flags(); break;
case OperationSetFlagsFromA: _zeroResult = _negativeResult = _a; break; case OperationSetFlagsFromA: _zeroResult = _negativeResult = _a; break;
case CycleIncrementPCAndReadStack: _pc.full++; throwaway_read(_s | 0x100); break; case CycleIncrementPCAndReadStack: _pc.full++; throwaway_read(_s | 0x100); break;
@ -686,45 +687,45 @@ template <class T> class Processor {
#pragma mark - Bitwise #pragma mark - Bitwise
case OperationORA: _a |= _operand; _negativeResult = _zeroResult = _a; break; case OperationORA: _a |= operand; _negativeResult = _zeroResult = _a; break;
case OperationAND: _a &= _operand; _negativeResult = _zeroResult = _a; break; case OperationAND: _a &= operand; _negativeResult = _zeroResult = _a; break;
case OperationEOR: _a ^= _operand; _negativeResult = _zeroResult = _a; break; case OperationEOR: _a ^= operand; _negativeResult = _zeroResult = _a; break;
#pragma mark - Load and Store #pragma mark - Load and Store
case OperationLDA: _a = _negativeResult = _zeroResult = _operand; break; case OperationLDA: _a = _negativeResult = _zeroResult = operand; break;
case OperationLDX: _x = _negativeResult = _zeroResult = _operand; break; case OperationLDX: _x = _negativeResult = _zeroResult = operand; break;
case OperationLDY: _y = _negativeResult = _zeroResult = _operand; break; case OperationLDY: _y = _negativeResult = _zeroResult = operand; break;
case OperationLAX: _a = _x = _negativeResult = _zeroResult = _operand; break; case OperationLAX: _a = _x = _negativeResult = _zeroResult = operand; break;
case OperationSTA: _operand = _a; break; case OperationSTA: operand = _a; break;
case OperationSTX: _operand = _x; break; case OperationSTX: operand = _x; break;
case OperationSTY: _operand = _y; break; case OperationSTY: operand = _y; break;
case OperationSAX: _operand = _a & _x; break; case OperationSAX: operand = _a & _x; break;
case OperationSHA: _operand = _a & _x & (_address.bytes.high+1); break; case OperationSHA: operand = _a & _x & (_address.bytes.high+1); break;
case OperationSHX: _operand = _x & (_address.bytes.high+1); break; case OperationSHX: operand = _x & (_address.bytes.high+1); break;
case OperationSHY: _operand = _y & (_address.bytes.high+1); break; case OperationSHY: operand = _y & (_address.bytes.high+1); break;
case OperationSHS: _s = _a & _x; _operand = _s & (_address.bytes.high+1); break; case OperationSHS: _s = _a & _x; operand = _s & (_address.bytes.high+1); break;
case OperationLXA: case OperationLXA:
_a = _x = (_a | 0xee) & _operand; _a = _x = (_a | 0xee) & operand;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
break; break;
#pragma mark - Compare #pragma mark - Compare
case OperationCMP: { case OperationCMP: {
const uint16_t temp16 = _a - _operand; const uint16_t temp16 = _a - operand;
_negativeResult = _zeroResult = (uint8_t)temp16; _negativeResult = _zeroResult = (uint8_t)temp16;
_carryFlag = ((~temp16) >> 8)&1; _carryFlag = ((~temp16) >> 8)&1;
} break; } break;
case OperationCPX: { case OperationCPX: {
const uint16_t temp16 = _x - _operand; const uint16_t temp16 = _x - operand;
_negativeResult = _zeroResult = (uint8_t)temp16; _negativeResult = _zeroResult = (uint8_t)temp16;
_carryFlag = ((~temp16) >> 8)&1; _carryFlag = ((~temp16) >> 8)&1;
} break; } break;
case OperationCPY: { case OperationCPY: {
const uint16_t temp16 = _y - _operand; const uint16_t temp16 = _y - operand;
_negativeResult = _zeroResult = (uint8_t)temp16; _negativeResult = _zeroResult = (uint8_t)temp16;
_carryFlag = ((~temp16) >> 8)&1; _carryFlag = ((~temp16) >> 8)&1;
} break; } break;
@ -732,27 +733,27 @@ template <class T> class Processor {
#pragma mark - BIT #pragma mark - BIT
case OperationBIT: case OperationBIT:
_zeroResult = _operand & _a; _zeroResult = operand & _a;
_negativeResult = _operand; _negativeResult = operand;
_overflowFlag = _operand&Flag::Overflow; _overflowFlag = operand&Flag::Overflow;
break; break;
#pragma mark ADC/SBC (and INS) #pragma mark ADC/SBC (and INS)
case OperationINS: case OperationINS:
_operand++; // deliberate fallthrough operand++; // deliberate fallthrough
case OperationSBC: case OperationSBC:
if(_decimalFlag) { if(_decimalFlag) {
const uint16_t notCarry = _carryFlag ^ 0x1; const uint16_t notCarry = _carryFlag ^ 0x1;
const uint16_t decimalResult = (uint16_t)_a - (uint16_t)_operand - notCarry; const uint16_t decimalResult = (uint16_t)_a - (uint16_t)operand - notCarry;
uint16_t temp16; uint16_t temp16;
temp16 = (_a&0xf) - (_operand&0xf) - notCarry; temp16 = (_a&0xf) - (operand&0xf) - notCarry;
if(temp16 > 0xf) temp16 -= 0x6; if(temp16 > 0xf) temp16 -= 0x6;
temp16 = (temp16&0x0f) | ((temp16 > 0x0f) ? 0xfff0 : 0x00); temp16 = (temp16&0x0f) | ((temp16 > 0x0f) ? 0xfff0 : 0x00);
temp16 += (_a&0xf0) - (_operand&0xf0); temp16 += (_a&0xf0) - (operand&0xf0);
_overflowFlag = ( ( (decimalResult^_a)&(~decimalResult^_operand) )&0x80) >> 1; _overflowFlag = ( ( (decimalResult^_a)&(~decimalResult^operand) )&0x80) >> 1;
_negativeResult = (uint8_t)temp16; _negativeResult = (uint8_t)temp16;
_zeroResult = (uint8_t)decimalResult; _zeroResult = (uint8_t)decimalResult;
@ -762,20 +763,20 @@ template <class T> class Processor {
_a = (uint8_t)temp16; _a = (uint8_t)temp16;
break; break;
} else { } else {
_operand = ~_operand; operand = ~operand;
} }
// deliberate fallthrough // deliberate fallthrough
case OperationADC: case OperationADC:
if(_decimalFlag) { if(_decimalFlag) {
const uint16_t decimalResult = (uint16_t)_a + (uint16_t)_operand + (uint16_t)_carryFlag; const uint16_t decimalResult = (uint16_t)_a + (uint16_t)operand + (uint16_t)_carryFlag;
uint16_t temp16; uint16_t temp16;
temp16 = (_a&0xf) + (_operand&0xf) + _carryFlag; temp16 = (_a&0xf) + (operand&0xf) + _carryFlag;
if(temp16 > 0x9) temp16 += 0x6; if(temp16 > 0x9) temp16 += 0x6;
temp16 = (temp16&0x0f) + ((temp16 > 0x0f) ? 0x10 : 0x00) + (_a&0xf0) + (_operand&0xf0); temp16 = (temp16&0x0f) + ((temp16 > 0x0f) ? 0x10 : 0x00) + (_a&0xf0) + (operand&0xf0);
_overflowFlag = (( (decimalResult^_a)&(decimalResult^_operand) )&0x80) >> 1; _overflowFlag = (( (decimalResult^_a)&(decimalResult^operand) )&0x80) >> 1;
_negativeResult = (uint8_t)temp16; _negativeResult = (uint8_t)temp16;
_zeroResult = (uint8_t)decimalResult; _zeroResult = (uint8_t)decimalResult;
@ -784,79 +785,79 @@ template <class T> class Processor {
_carryFlag = (temp16 > 0xff) ? Flag::Carry : 0; _carryFlag = (temp16 > 0xff) ? Flag::Carry : 0;
_a = (uint8_t)temp16; _a = (uint8_t)temp16;
} else { } else {
const uint16_t decimalResult = (uint16_t)_a + (uint16_t)_operand + (uint16_t)_carryFlag; const uint16_t decimalResult = (uint16_t)_a + (uint16_t)operand + (uint16_t)_carryFlag;
_overflowFlag = (( (decimalResult^_a)&(decimalResult^_operand) )&0x80) >> 1; _overflowFlag = (( (decimalResult^_a)&(decimalResult^operand) )&0x80) >> 1;
_negativeResult = _zeroResult = _a = (uint8_t)decimalResult; _negativeResult = _zeroResult = _a = (uint8_t)decimalResult;
_carryFlag = (decimalResult >> 8)&1; _carryFlag = (decimalResult >> 8)&1;
} }
// fix up in case this was INS // fix up in case this was INS
if(cycle == OperationINS) _operand = ~_operand; if(cycle == OperationINS) operand = ~operand;
break; break;
#pragma mark - Shifts and Rolls #pragma mark - Shifts and Rolls
case OperationASL: case OperationASL:
_carryFlag = _operand >> 7; _carryFlag = operand >> 7;
_operand <<= 1; operand <<= 1;
_negativeResult = _zeroResult = _operand; _negativeResult = _zeroResult = operand;
break; break;
case OperationASO: case OperationASO:
_carryFlag = _operand >> 7; _carryFlag = operand >> 7;
_operand <<= 1; operand <<= 1;
_a |= _operand; _a |= operand;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
break; break;
case OperationROL: { case OperationROL: {
const uint8_t temp8 = (uint8_t)((_operand << 1) | _carryFlag); const uint8_t temp8 = (uint8_t)((operand << 1) | _carryFlag);
_carryFlag = _operand >> 7; _carryFlag = operand >> 7;
_operand = _negativeResult = _zeroResult = temp8; operand = _negativeResult = _zeroResult = temp8;
} break; } break;
case OperationRLA: { case OperationRLA: {
const uint8_t temp8 = (uint8_t)((_operand << 1) | _carryFlag); const uint8_t temp8 = (uint8_t)((operand << 1) | _carryFlag);
_carryFlag = _operand >> 7; _carryFlag = operand >> 7;
_operand = temp8; operand = temp8;
_a &= _operand; _a &= operand;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
} break; } break;
case OperationLSR: case OperationLSR:
_carryFlag = _operand & 1; _carryFlag = operand & 1;
_operand >>= 1; operand >>= 1;
_negativeResult = _zeroResult = _operand; _negativeResult = _zeroResult = operand;
break; break;
case OperationLSE: case OperationLSE:
_carryFlag = _operand & 1; _carryFlag = operand & 1;
_operand >>= 1; operand >>= 1;
_a ^= _operand; _a ^= operand;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
break; break;
case OperationASR: case OperationASR:
_a &= _operand; _a &= operand;
_carryFlag = _a & 1; _carryFlag = _a & 1;
_a >>= 1; _a >>= 1;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
break; break;
case OperationROR: { case OperationROR: {
const uint8_t temp8 = (uint8_t)((_operand >> 1) | (_carryFlag << 7)); const uint8_t temp8 = (uint8_t)((operand >> 1) | (_carryFlag << 7));
_carryFlag = _operand & 1; _carryFlag = operand & 1;
_operand = _negativeResult = _zeroResult = temp8; operand = _negativeResult = _zeroResult = temp8;
} break; } break;
case OperationRRA: { case OperationRRA: {
const uint8_t temp8 = (uint8_t)((_operand >> 1) | (_carryFlag << 7)); const uint8_t temp8 = (uint8_t)((operand >> 1) | (_carryFlag << 7));
_carryFlag = _operand & 1; _carryFlag = operand & 1;
_operand = temp8; operand = temp8;
} break; } break;
case OperationDecrementOperand: _operand--; break; case OperationDecrementOperand: operand--; break;
case OperationIncrementOperand: _operand++; break; case OperationIncrementOperand: operand++; break;
case OperationCLC: _carryFlag = 0; break; case OperationCLC: _carryFlag = 0; break;
case OperationCLI: _interruptFlag = 0; break; case OperationCLI: _interruptFlag = 0; break;
@ -867,26 +868,26 @@ template <class T> class Processor {
case OperationSEI: _interruptFlag = Flag::Interrupt; break; case OperationSEI: _interruptFlag = Flag::Interrupt; break;
case OperationSED: _decimalFlag = Flag::Decimal; break; case OperationSED: _decimalFlag = Flag::Decimal; break;
case OperationINC: _operand++; _negativeResult = _zeroResult = _operand; break; case OperationINC: operand++; _negativeResult = _zeroResult = operand; break;
case OperationDEC: _operand--; _negativeResult = _zeroResult = _operand; break; case OperationDEC: operand--; _negativeResult = _zeroResult = operand; break;
case OperationINX: _x++; _negativeResult = _zeroResult = _x; break; case OperationINX: _x++; _negativeResult = _zeroResult = _x; break;
case OperationDEX: _x--; _negativeResult = _zeroResult = _x; break; case OperationDEX: _x--; _negativeResult = _zeroResult = _x; break;
case OperationINY: _y++; _negativeResult = _zeroResult = _y; break; case OperationINY: _y++; _negativeResult = _zeroResult = _y; break;
case OperationDEY: _y--; _negativeResult = _zeroResult = _y; break; case OperationDEY: _y--; _negativeResult = _zeroResult = _y; break;
case OperationANE: case OperationANE:
_a = (_a | 0xee) & _operand & _x; _a = (_a | 0xee) & operand & _x;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
break; break;
case OperationANC: case OperationANC:
_a &= _operand; _a &= operand;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
_carryFlag = _a >> 7; _carryFlag = _a >> 7;
break; break;
case OperationLAS: case OperationLAS:
_a = _x = _s = _s & _operand; _a = _x = _s = _s & operand;
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
break; break;
@ -921,58 +922,58 @@ template <class T> class Processor {
break; break;
case CycleIncrementPCFetchAddressLowFromOperand: case CycleIncrementPCFetchAddressLowFromOperand:
_pc.full++; _pc.full++;
read_mem(_address.bytes.low, _operand); read_mem(_address.bytes.low, operand);
break; break;
case CycleAddXToOperandFetchAddressLow: case CycleAddXToOperandFetchAddressLow:
_operand += _x; operand += _x;
read_mem(_address.bytes.low, _operand); read_mem(_address.bytes.low, operand);
break; break;
case CycleIncrementOperandFetchAddressHigh: case CycleIncrementOperandFetchAddressHigh:
_operand++; operand++;
read_mem(_address.bytes.high, _operand); read_mem(_address.bytes.high, operand);
break; break;
case CycleIncrementPCReadPCHLoadPCL: // deliberate fallthrough case CycleIncrementPCReadPCHLoadPCL: // deliberate fallthrough
_pc.full++; _pc.full++;
case CycleReadPCHLoadPCL: { case CycleReadPCHLoadPCL: {
uint16_t oldPC = _pc.full; uint16_t oldPC = _pc.full;
_pc.bytes.low = _operand; _pc.bytes.low = operand;
read_mem(_pc.bytes.high, oldPC); read_mem(_pc.bytes.high, oldPC);
} break; } break;
case CycleReadAddressHLoadAddressL: case CycleReadAddressHLoadAddressL:
_address.bytes.low = _operand; _pc.full++; _address.bytes.low = operand; _pc.full++;
read_mem(_address.bytes.high, _pc.full); read_mem(_address.bytes.high, _pc.full);
break; break;
case CycleLoadAddressAbsolute: { case CycleLoadAddressAbsolute: {
uint16_t nextPC = _pc.full+1; uint16_t nextPC = _pc.full+1;
_pc.full += 2; _pc.full += 2;
_address.bytes.low = _operand; _address.bytes.low = operand;
read_mem(_address.bytes.high, nextPC); read_mem(_address.bytes.high, nextPC);
} break; } break;
case OperationLoadAddressZeroPage: case OperationLoadAddressZeroPage:
_pc.full++; _pc.full++;
_address.full = _operand; _address.full = operand;
break; break;
case CycleLoadAddessZeroX: case CycleLoadAddessZeroX:
_pc.full++; _pc.full++;
_address.full = (_operand + _x)&0xff; _address.full = (operand + _x)&0xff;
throwaway_read(_operand); throwaway_read(operand);
break; break;
case CycleLoadAddessZeroY: case CycleLoadAddessZeroY:
_pc.full++; _pc.full++;
_address.full = (_operand + _y)&0xff; _address.full = (operand + _y)&0xff;
throwaway_read(_operand); throwaway_read(operand);
break; break;
case OperationIncrementPC: _pc.full++; break; case OperationIncrementPC: _pc.full++; break;
case CycleFetchOperandFromAddress: read_mem(_operand, _address.full); break; case CycleFetchOperandFromAddress: read_mem(operand, _address.full); break;
case CycleWriteOperandToAddress: write_mem(_operand, _address.full); break; case CycleWriteOperandToAddress: write_mem(operand, _address.full); break;
case OperationCopyOperandFromA: _operand = _a; break; case OperationCopyOperandFromA: operand = _a; break;
case OperationCopyOperandToA: _a = _operand; break; case OperationCopyOperandToA: _a = operand; break;
#pragma mark - Branching #pragma mark - Branching
@ -988,7 +989,7 @@ template <class T> class Processor {
case OperationBEQ: BRA(!_zeroResult); break; case OperationBEQ: BRA(!_zeroResult); break;
case CycleAddSignedOperandToPC: case CycleAddSignedOperandToPC:
_nextAddress.full = (uint16_t)(_pc.full + (int8_t)_operand); _nextAddress.full = (uint16_t)(_pc.full + (int8_t)operand);
_pc.bytes.low = _nextAddress.bytes.low; _pc.bytes.low = _nextAddress.bytes.low;
if(_nextAddress.bytes.high != _pc.bytes.high) { if(_nextAddress.bytes.high != _pc.bytes.high) {
uint16_t halfUpdatedPc = _pc.full; uint16_t halfUpdatedPc = _pc.full;
@ -1010,7 +1011,7 @@ template <class T> class Processor {
case OperationARR: case OperationARR:
if(_decimalFlag) { if(_decimalFlag) {
_a &= _operand; _a &= operand;
uint8_t unshiftedA = _a; uint8_t unshiftedA = _a;
_a = (uint8_t)((_a >> 1) | (_carryFlag << 7)); _a = (uint8_t)((_a >> 1) | (_carryFlag << 7));
_zeroResult = _negativeResult = _a; _zeroResult = _negativeResult = _a;
@ -1022,7 +1023,7 @@ template <class T> class Processor {
if (_carryFlag) _a += 0x60; if (_carryFlag) _a += 0x60;
} else { } else {
_a &= _operand; _a &= operand;
_a = (uint8_t)((_a >> 1) | (_carryFlag << 7)); _a = (uint8_t)((_a >> 1) | (_carryFlag << 7));
_negativeResult = _zeroResult = _a; _negativeResult = _zeroResult = _a;
_carryFlag = (_a >> 6)&1; _carryFlag = (_a >> 6)&1;
@ -1032,7 +1033,7 @@ template <class T> class Processor {
case OperationSBX: case OperationSBX:
_x &= _a; _x &= _a;
uint16_t difference = _x - _operand; uint16_t difference = _x - operand;
_x = (uint8_t)difference; _x = (uint8_t)difference;
_negativeResult = _zeroResult = _x; _negativeResult = _zeroResult = _x;
_carryFlag = ((difference >> 8)&1)^1; _carryFlag = ((difference >> 8)&1)^1;
@ -1047,6 +1048,7 @@ template <class T> class Processor {
_cycles_left_to_run = number_of_cycles; _cycles_left_to_run = number_of_cycles;
_scheduleProgramsReadPointer = scheduleProgramsReadPointer; _scheduleProgramsReadPointer = scheduleProgramsReadPointer;
_scheduleProgramProgramCounter = scheduleProgramProgramCounter; _scheduleProgramProgramCounter = scheduleProgramProgramCounter;
_operand = operand;
} }
} }