Skip to content

Commit

Permalink
Fix gdb for non-003 arch's
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Feb 15, 2024
1 parent e1a358d commit de5dbb7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
39 changes: 31 additions & 8 deletions minichlink/minichgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void SendReplyFull( const char * replyMessage );
// Several pieces from picorvd. https://github.com/aappleby/PicoRVD/
int shadow_running_state = 1;
int last_halt_reason = 5;
uint32_t backup_regs[17];
uint32_t backup_regs[33]; //0..15 + PC, or 0..32 + PC

#define MAX_SOFTWARE_BREAKPOINTS 128
int num_software_breakpoints = 0;
Expand Down Expand Up @@ -131,15 +131,25 @@ void RVNetPoll(void * dev )

int RVReadCPURegister( void * dev, int regno, uint32_t * regret )
{
struct InternalState * iss = (struct InternalState*)(((struct ProgrammerStructBase*)dev)->internal);
int nrregs = iss->nr_registers_for_debug;

if( shadow_running_state )
{
MCF.HaltMode( dev, 5 );
RVCommandPrologue( dev );
shadow_running_state = 0;
}

if( regno == 32 ) regno = 16; // Hack - Make 32 also 16 for old GDBs.
if( regno > 16 ) return 0; // Invalid register.
if( nrregs == 16 )
{
if( regno == 32 ) regno = 16; // Hack - Make 32 also 16 for old GDBs.
if( regno > 16 ) return 0; // Invalid register.
}
else
{
if( regno > nrregs ) return 0;
}

*regret = backup_regs[regno];
return 0;
Expand All @@ -148,15 +158,25 @@ int RVReadCPURegister( void * dev, int regno, uint32_t * regret )

int RVWriteCPURegister( void * dev, int regno, uint32_t value )
{
struct InternalState * iss = (struct InternalState*)(((struct ProgrammerStructBase*)dev)->internal);
int nrregs = iss->nr_registers_for_debug;

if( shadow_running_state )
{
MCF.HaltMode( dev, 5 );
RVCommandPrologue( dev );
shadow_running_state = 0;
}

if( regno == 32 ) regno = 16; // Hack - Make 32 also 16 for old GDBs.
if( regno > 16 ) return 0; // Invalid register.
if( nrregs == 16 )
{
if( regno == 32 ) regno = 16; // Hack - Make 32 also 16 for old GDBs.
if( regno > 16 ) return 0; // Invalid register.
}
else
{
if( regno > nrregs ) return 0;
}

backup_regs[regno] = value;

Expand All @@ -177,6 +197,9 @@ int RVWriteCPURegister( void * dev, int regno, uint32_t value )

void RVDebugExec( void * dev, int halt_reset_or_resume )
{
struct InternalState * iss = (struct InternalState*)(((struct ProgrammerStructBase*)dev)->internal);
int nrregs = iss->nr_registers_for_debug;

if( !MCF.HaltMode )
{
fprintf( stderr, "Error: Can't alter halt mode with this programmer.\n" );
Expand All @@ -189,7 +212,7 @@ void RVDebugExec( void * dev, int halt_reset_or_resume )
// First see if we already know about this breakpoint
int matchingbreakpoint = -1;
// For this we want to advance PC.
uint32_t exceptionptr = backup_regs[16];
uint32_t exceptionptr = backup_regs[nrregs];
uint32_t instruction = 0;

int i;
Expand Down Expand Up @@ -224,9 +247,9 @@ void RVDebugExec( void * dev, int halt_reset_or_resume )
MCF.ReadWord( dev, exceptionptr, &instruction );
}
if( instruction == 0x00100073 )
backup_regs[16]+=4;
backup_regs[nrregs]+=4;
else if( ( instruction & 0xffff ) == 0x9002 )
backup_regs[16]+=2;
backup_regs[nrregs]+=2;
else
; //No change, it is a normal instruction.

Expand Down
27 changes: 24 additions & 3 deletions minichlink/minichlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Sleep(uint32_t dwMilliseconds);
static int64_t StringToMemoryAddress( const char * number ) __attribute__((used));
static void StaticUpdatePROGBUFRegs( void * dev ) __attribute__((used));
int DefaultReadBinaryBlob( void * dev, uint32_t address_to_read_from, uint32_t read_size, uint8_t * blob );

void PostSetupConfigureInterface( void * dev );
void TestFunction(void * v );
struct MiniChlinkFunctions MCF;

Expand Down Expand Up @@ -149,6 +149,7 @@ int main( int argc, char ** argv )
printf( "Interface Setup\n" );
}

PostSetupConfigureInterface( dev );
// TestFunction( dev );

int iarg = 1;
Expand Down Expand Up @@ -1540,6 +1541,26 @@ int DefaultErase( void * dev, uint32_t address, uint32_t length, int type )
return -93;
}

void PostSetupConfigureInterface( void * dev )
{
struct InternalState * iss = (struct InternalState*)(((struct ProgrammerStructBase*)dev)->internal);
switch( iss->target_chip_type )
{
case CHIP_CH32V10x:
case CHIP_CH57x:
case CHIP_CH56x:
case CHIP_CH32V20x:
case CHIP_CH32V30x:
case CHIP_CH58x:
default:
iss->nr_registers_for_debug = 32;
break;
case CHIP_CH32V003:
iss->nr_registers_for_debug = 16;
break;
}
}

int DefaultReadBinaryBlob( void * dev, uint32_t address_to_read_from, uint32_t read_size, uint8_t * blob )
{
uint32_t rpos = address_to_read_from;
Expand Down Expand Up @@ -1625,7 +1646,7 @@ int DefaultReadAllCPURegisters( void * dev, uint32_t * regret )
MCF.WriteReg32( dev, DMABSTRACTAUTO, 0x00000001 ); // Disable Autoexec.
iss->statetag = STTAG( "RER2" );
int i;
for( i = 0; i < 16; i++ )
for( i = 0; i < iss->nr_registers_for_debug; i++ )
{
MCF.WriteReg32( dev, DMCOMMAND, 0x00220000 | 0x1000 | i ); // Read xN into DATA0.
if( MCF.ReadReg32( dev, DMDATA0, regret + i ) )
Expand All @@ -1646,7 +1667,7 @@ int DefaultWriteAllCPURegisters( void * dev, uint32_t * regret )
MCF.WriteReg32( dev, DMABSTRACTAUTO, 0x00000001 ); // Disable Autoexec.
iss->statetag = STTAG( "WER2" );
int i;
for( i = 0; i < 16; i++ )
for( i = 0; i < iss->nr_registers_for_debug; i++ )
{
MCF.WriteReg32( dev, DMCOMMAND, 0x00230000 | 0x1000 | i ); // Read xN into DATA0.
if( MCF.WriteReg32( dev, DMDATA0, regret[i] ) )
Expand Down
1 change: 1 addition & 0 deletions minichlink/minichlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct InternalState
int flash_size;
enum RiscVChip target_chip_type;
uint8_t flash_sector_status[MAX_FLASH_SECTORS]; // 0 means unerased/unknown. 1 means erased.
int nr_registers_for_debug; // Updated by PostSetupConfigureInterface
};


Expand Down

0 comments on commit de5dbb7

Please sign in to comment.