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