Debugger: Cleanup return type for _6502_GetStackReturnAddress()

This commit is contained in:
michaelangel007 2023-03-29 07:33:23 -07:00
parent 87d9cee6c5
commit 55f863d118
3 changed files with 78 additions and 73 deletions

View File

@ -2336,8 +2336,8 @@ Update_t CmdStepOver (int nArgs)
// If the PC isn't at the expected address after the JSR print a diagnostic so the user knows the stack may be buggered up
if (regs.pc != nExpectedAddr)
{
WORD nActualAddr;
bool bValidAddr = _6502_GetStackReturnAddress( nActualAddr ) && (nActualAddr == nExpectedAddr);
WORD nActualAddr = _6502_GetStackReturnAddress();
bool bValidAddr = (nActualAddr == nExpectedAddr);
int nStackOffset = _6502_FindStackReturnAddress( nExpectedAddr ); // Trace stack to seee if our expected address is on it
/*
@ -2388,13 +2388,11 @@ Update_t CmdStepOut (int nArgs)
{
// TODO: "RET" should probably pop the Call stack
// Also see: CmdCursorJumpRetAddr
WORD nAddress;
if (_6502_GetStackReturnAddress( nAddress, true ))
{
nArgs = _Arg_1( nAddress );
g_aArgs[1].sArg[0] = 0;
CmdGo( 1, true );
}
WORD nAddress = _6502_GetStackReturnAddress();
nArgs = _Arg_1( nAddress );
g_aArgs[1].sArg[0] = 0;
CmdGo( 1, true );
return UPDATE_ALL;
}
@ -3367,22 +3365,19 @@ Update_t CmdCursorJumpPC (int nArgs)
//===========================================================================
Update_t CmdCursorJumpRetAddr (int nArgs)
{
WORD nAddress = 0;
if (_6502_GetStackReturnAddress( nAddress ))
{
g_nDisasmCurAddress = nAddress;
WORD nAddress = _6502_GetStackReturnAddress();
g_nDisasmCurAddress = nAddress;
if (CURSOR_ALIGN_CENTER == nArgs)
{
WindowUpdateDisasmSize();
}
else
if (CURSOR_ALIGN_TOP == nArgs)
{
g_nDisasmCurLine = 0;
}
DisasmCalcTopBotAddress();
if (CURSOR_ALIGN_CENTER == nArgs)
{
WindowUpdateDisasmSize();
}
else
if (CURSOR_ALIGN_TOP == nArgs)
{
g_nDisasmCurLine = 0;
}
DisasmCalcTopBotAddress();
return UPDATE_ALL;
}

View File

@ -426,8 +426,60 @@ Fx BEQ r SBC (d),Y sbc (z) --- --- SBC d,X INC z,X --- SED SBC a,Y
void AssemblerHashOpcodes ();
void AssemblerHashDirectives ();
// Implementation ___________________________________________________________
// Utility __________________________________________________________________
// === Stack ===
// Return stack offset if the address is on the stack, else -1 if not found
//===========================================================================
int _6502_FindStackReturnAddress (const WORD nAddress)
{
WORD nReturnAddress;
WORD nStack = regs.sp + 1;
int nDepth = -1; // not found
// Normally would <= _6502_STACK_END-1 since JSR always pushes 2 bytes
// but the SP could be $00 before JSR forcing RTS address to be split $100/$1FF
// or the SP could be $01 before JSR forcing RTS address to be at $100/$101
// R PC 300
// R S 0
// 300:20 04 03 60 60
// <Ctrl-Space>
while (nStack <= (_6502_STACK_END + 1))
{
nReturnAddress = _6502_PeekStackReturnAddress(nStack);
if (nReturnAddress == nAddress)
{
nDepth = (nStack - 2 - regs.sp);
return nDepth;
}
}
return nDepth;
}
// NOTE: If the stack pointer is <= 01 before a JSR the stack will wrap around.
//===========================================================================
WORD _6502_GetStackReturnAddress ()
{
WORD nStack = regs.sp + 1;
WORD nAddress = _6502_PeekStackReturnAddress(nStack);
return nAddress;
}
// NOTE: nStack is both an input and output
//===========================================================================
WORD _6502_PeekStackReturnAddress (WORD & nStack)
{
WORD nAddress;
nAddress = ((unsigned) *(LPBYTE)(mem + 0x100 + (nStack & 0xFF)) ); nStack++;
nAddress += ((unsigned) *(LPBYTE)(mem + 0x100 + (nStack & 0xFF)) << 8);
nAddress++;
return nAddress;
}
// == Opcodes ===
//===========================================================================
bool _6502_CalcRelativeOffset ( int nOpcode, int nBaseAddress, int nTargetAddress, WORD * pTargetOffset_ )
@ -464,32 +516,6 @@ bool _6502_CalcRelativeOffset ( int nOpcode, int nBaseAddress, int nTargetAddres
return false;
}
// Return stack offset if the address is on stack, else -1 if not found
//===========================================================================
int _6502_FindStackReturnAddress (const WORD & nAddress)
{
WORD nStack = regs.sp;
int nDepth = -1; // not found
nStack++;
while (nStack <= (_6502_STACK_END - 1))
{
WORD nReturnAddress = (unsigned)*(LPBYTE)(mem + nStack);
nStack++;
nReturnAddress += ((unsigned)*(LPBYTE)(mem + nStack)) << 8;
nReturnAddress++;
if (nReturnAddress == nAddress)
{
nDepth = (nStack - 2 - regs.sp);
return nDepth;
}
}
return nDepth;
}
//===========================================================================
int _6502_GetOpmodeOpbyte ( const int nBaseAddress, int & iOpmode_, int & nOpbyte_, const DisasmData_t** pData_ )
{
@ -591,27 +617,6 @@ void _6502_GetOpcodeOpmodeOpbyte (int & iOpcode_, int & iOpmode_, int & nOpbyte_
iOpcode_ = _6502_GetOpmodeOpbyte( regs.pc, iOpmode_, nOpbyte_ );
}
// NOTE: If the stack pointer is <= 01 before a JSR the stack will wrap around.
// Use bWrapAround to access stack memory in this case.
//===========================================================================
bool _6502_GetStackReturnAddress (WORD & nAddress_, bool bWrapAround /* = false */)
{
unsigned nStack = regs.sp;
nStack++;
if (bWrapAround || nStack <= (_6502_STACK_END - 1)) // JSR always pushes 2 bytes. If no wrap around then the stack pointer must be <= 0x1FE
{
nAddress_ = (unsigned)*(LPBYTE)(mem + 0x100 + (nStack & 0xFF));
nStack++;
nAddress_ += ((unsigned)*(LPBYTE)(mem + 0x100 + (nStack & 0xFF))) << 8;
nAddress_++;
return true;
}
return false;
}
//===========================================================================
bool _6502_GetTargets (WORD nAddress, int *pTargetPartial_, int *pTargetPartial2_, int *pTargetPointer_, int * pTargetBytes_,
bool bIgnoreBranch /*= true*/, bool bIncludeNextOpcodeAddress /*= true*/ )

View File

@ -191,16 +191,21 @@ extern int g_aAssemblerFirstDirective[ NUM_ASSEMBLERS ];
// Prototypes _______________________________________________________________
int _6502_FindStackReturnAddress (const WORD & nAddress);
// Stack
int _6502_FindStackReturnAddress (const WORD nAddress);
WORD _6502_GetStackReturnAddress ();
WORD _6502_PeekStackReturnAddress (WORD & nStack);
// Opcodes
int _6502_GetOpmodeOpbyte( const int iAddress, int & iOpmode_, int & nOpbytes_, const DisasmData_t** pData = NULL );
void _6502_GetOpcodeOpmodeOpbyte( int & iOpcode_, int & iOpmode_, int & nOpbytes_ );
bool _6502_GetStackReturnAddress (WORD & nAddress_, bool bWrapAround = false);
bool _6502_GetTargets( WORD nAddress, int *pTargetPartial_, int *pTargetPartial2_, int *pTargetPointer_, int * pBytes_,
bool bIgnoreBranch = true, bool bIncludeNextOpcodeAddress = true );
bool _6502_GetTargetAddress( const WORD & nAddress, WORD & nTarget_ );
bool _6502_IsOpcodeBranch( int nOpcode );
bool _6502_IsOpcodeValid( int nOpcode );
// Assembler
Hash_t AssemblerHashMnemonic ( const TCHAR * pMnemonic );
// bool AssemblerGetAddressingMode ( int iArg, int nArgs, WORD nAddress, std::vector<int> & vOpcodes );
void _CmdAssembleHashDump ();