Skip to content

Commit

Permalink
refactor read6502, add debug_read6502* (#294)
Browse files Browse the repository at this point in the history
* refactor read6502, add debug_read6502*

* refactoring

* using sentinel value for USE_CURRENT_BANK, changed debug_read6502 to macro
  • Loading branch information
mooinglemur authored Aug 18, 2024
1 parent c09206e commit 6262ec7
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 66 deletions.
16 changes: 8 additions & 8 deletions src/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ static void DEBUGHandleKeyEvent(SDL_Keycode key,int isShift) {
break;

case DBGKEY_STEPOVER: // Step over (F10 by default)
opcode = real_read6502(regs.pc, true, currentPCBank); // What opcode is it ?
opcode = debug_read6502(regs.pc, currentPCBank); // What opcode is it ?
if (opcode == 0x20 || opcode == 0xFC || opcode == 0x22) { // Is it JSR or JSL ?
stepBreakPoint.pc = regs.pc + 3 + (opcode == 0x22); // Then break 3 / 4 on.
stepBreakPoint.bank = getCurrentBank(regs.pc);
Expand Down Expand Up @@ -632,7 +632,7 @@ static int DEBUGRenderZeroPageRegisters(int y) {
DEBUGString(dbgRenderer, DBG_ZP_REG, y, lbl, col_label);

int reg_addr = 2 + reg * 2;
int n = real_read6502(direct_page_add(reg_addr+1), true, currentBank)*256+real_read6502(direct_page_add(reg_addr), true, currentBank);
int n = debug_read6502(direct_page_add(reg_addr+1), USE_CURRENT_BANK)*256+debug_read6502(direct_page_add(reg_addr), USE_CURRENT_BANK);

DEBUGNumber(DBG_ZP_REG+5, y, n, 4, col_data);

Expand Down Expand Up @@ -667,7 +667,7 @@ static void DEBUGRenderData(int y,int data) {

for (int i = 0;i < 8;i++) {
bool isDP = ((data+i - regs.dp) & 0xffff) < 256;
int byte = real_read6502((data+i) & 0xFFFF, true, currentBank);
int byte = debug_read6502((data+i) & 0xFFFF, currentBank);
DEBUGNumber(DBG_MEMX+8+i*3,y,byte,2, isDP ? col_directpage : col_data);
DEBUGWrite(dbgRenderer, DBG_MEMX+33+i,y,byte, isDP ? col_directpage : col_data);
}
Expand Down Expand Up @@ -726,7 +726,7 @@ static void DEBUGRenderCode(int lines, int initialPC) {
// still been true without the added logic, anyway.

if (regs.is65c816) {
opcode = real_read6502(initialPC, true, currentPCBank);
opcode = debug_read6502(initialPC, currentPCBank);
switch (opcode) {
case 0x81: // CLC
implied_status &= ~FLAG_CARRY;
Expand All @@ -735,11 +735,11 @@ static void DEBUGRenderCode(int lines, int initialPC) {
implied_status |= FLAG_CARRY;
;;
case 0xC2: // REP
operand = real_read6502((initialPC+1) & 0xffff, true, currentPCBank);
operand = debug_read6502((initialPC+1) & 0xffff, currentPCBank);
implied_status = ~operand & implied_status;
;;
case 0xE2: // SEP
operand = real_read6502((initialPC+1) & 0xffff, true, currentPCBank);
operand = debug_read6502((initialPC+1) & 0xffff, currentPCBank);
implied_status = operand | implied_status;
;;
case 0xFB: // XCE
Expand All @@ -753,7 +753,7 @@ static void DEBUGRenderCode(int lines, int initialPC) {
if (implied_e) implied_status |= FLAG_INDEX_WIDTH | FLAG_MEMORY_WIDTH;

}
int size = disasm(initialPC, RAM, buffer, sizeof(buffer), true, currentPCBank, implied_status, &eff_addr); // Disassemble code
int size = disasm(initialPC, RAM, buffer, sizeof(buffer), currentPCBank, implied_status, &eff_addr); // Disassemble code
// Output assembly highlighting PC
DEBUGString(dbgRenderer, DBG_ASMX+8, y, buffer, initialPC == regs.pc ? col_highlight : col_data);
// Populate effective address
Expand Down Expand Up @@ -903,7 +903,7 @@ static void DEBUGRenderStack(int bytesCount) {
int y= 0;
while (y < bytesCount) {
DEBUGNumber(DBG_STCK,y, sp,4, col_label);
int byte = real_read6502(sp, true, 0);
int byte = debug_read6502(sp, USE_CURRENT_BANK);
DEBUGNumber(DBG_STCK+5,y,byte,2, col_data);
DEBUGWrite(dbgRenderer, DBG_STCK+9,y,byte, col_data);
y++;
Expand Down
42 changes: 21 additions & 21 deletions src/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//
// *******************************************************************************************

int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, bool debugOn, uint8_t bank, uint8_t implied_status, int32_t *eff_addr) {
uint8_t opcode = real_read6502(pc, debugOn, bank);
int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, int16_t bank, uint8_t implied_status, int32_t *eff_addr) {
uint8_t opcode = debug_read6502(pc, bank);
const char *mnemonic = regs.is65c816 ? mnemonics_c816[opcode] : mnemonics_c02[opcode];

*eff_addr = -1;
Expand Down Expand Up @@ -308,45 +308,45 @@ int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, bool de
}

if (isBlockMove) {
snprintf(line, max_line, mnemonic, real_read6502(pc + 1, debugOn, bank), real_read6502(pc + 2, debugOn, bank));
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank), debug_read6502(pc + 2, bank));
length = 3;
if (regs.c != 0xFFFF) *eff_addr = regs.y; // We can have only one effective address, so we're choosing the destination
}
else if (isZprel) {
snprintf(line, max_line, mnemonic, real_read6502(pc + 1, debugOn, bank), pc + 3 + (int8_t)real_read6502(pc + 2, debugOn, bank));
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank), pc + 3 + (int8_t)debug_read6502(pc + 2, bank));
length = 3;
}
else if (isRel16) {
snprintf(line, max_line, mnemonic, (pc + 3 + (int16_t)(real_read6502(pc + 1, debugOn, bank) | (real_read6502(pc + 2, debugOn, bank) << 8))) & 0xffff);
snprintf(line, max_line, mnemonic, (pc + 3 + (int16_t)(debug_read6502(pc + 1, bank) | (debug_read6502(pc + 2, bank) << 8))) & 0xffff);
length = 3;
}
else if (isBrkOrCop) {
snprintf(line, max_line, mnemonic, real_read6502(pc + 1, debugOn, bank));
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank));
length = 2;
} else {
if (strstr(line, "%02x")) {
length = 2;
if (isRel8) {
snprintf(line, max_line, mnemonic, pc + 2 + (int8_t)real_read6502(pc + 1, debugOn, bank));
snprintf(line, max_line, mnemonic, pc + 2 + (int8_t)debug_read6502(pc + 1, bank));
} else {
snprintf(line, max_line, mnemonic, real_read6502(pc + 1, debugOn, bank));
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank));
if (isStackRel) {
uint16_t ptr = regs.sp + real_read6502(pc + 1, debugOn, bank);
uint16_t ptr = regs.sp + debug_read6502(pc + 1, bank);
uint8_t ind_bank = ptr < 0xc000 ? memory_get_ram_bank() : memory_get_rom_bank();
if (isIndirect && isYrel) {
*eff_addr = (real_read6502(ptr, debugOn, ind_bank) | (real_read6502(ptr + 1, debugOn, ind_bank) << 8)) + regs.y;
*eff_addr = (debug_read6502(ptr, ind_bank) | (debug_read6502(ptr + 1, ind_bank) << 8)) + regs.y;
} else {
*eff_addr = ptr;
}
} else if (isIndirect) {
uint16_t ptr = real_read6502(pc + 1, debugOn, bank);
uint16_t ptr = debug_read6502(pc + 1, bank);
if (isXrel)
ptr += regs.x;
*eff_addr = real_read6502(ptr, debugOn, bank) | (real_read6502(ptr + 1, debugOn, bank) << 8);
*eff_addr = debug_read6502(ptr, bank) | (debug_read6502(ptr + 1, bank) << 8);
if (isYrel)
*eff_addr += regs.y;
} else if (!isImmediate) {
*eff_addr = real_read6502(pc + 1, debugOn, bank);
*eff_addr = debug_read6502(pc + 1, bank);
if (isXrel)
*eff_addr += regs.x;
if (isYrel)
Expand All @@ -356,17 +356,17 @@ int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, bool de
}
if (strstr(line, "%04x")) {
length = 3;
snprintf(line, max_line, mnemonic, real_read6502(pc + 1, debugOn, bank) | real_read6502(pc + 2, debugOn, bank) << 8);
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank) | debug_read6502(pc + 2, bank) << 8);
if (isIndirect) {
uint16_t ptr = real_read6502(pc + 1, debugOn, bank) | (real_read6502(pc + 2, debugOn, bank) << 8);
uint16_t ptr = debug_read6502(pc + 1, bank) | (debug_read6502(pc + 2, bank) << 8);
if (isXrel)
ptr += regs.x;
uint8_t ind_bank = ptr < 0xc000 ? memory_get_ram_bank() : memory_get_rom_bank();
*eff_addr = real_read6502(ptr, debugOn, ind_bank) | (real_read6502(ptr + 1, debugOn, ind_bank) << 8);
*eff_addr = debug_read6502(ptr, ind_bank) | (debug_read6502(ptr + 1, ind_bank) << 8);
if (isYrel)
*eff_addr += regs.y;
} else if (!isImmediate) {
*eff_addr = real_read6502(pc + 1, debugOn, bank) | (real_read6502(pc + 2, debugOn, bank) << 8);
*eff_addr = debug_read6502(pc + 1, bank) | (debug_read6502(pc + 2, bank) << 8);
if (isXrel)
*eff_addr += regs.x;
if (isYrel)
Expand All @@ -375,17 +375,17 @@ int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, bool de
}
if (strstr(line, "%06x")) {
length = 4;
snprintf(line, max_line, mnemonic, real_read6502(pc + 1, debugOn, bank) | real_read6502(pc + 2, debugOn, bank) << 8 | real_read6502(pc + 3, debugOn, bank) << 16);
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank) | debug_read6502(pc + 2, bank) << 8 | debug_read6502(pc + 3, bank) << 16);
if (isIndirect) {
uint16_t ptr = real_read6502(pc + 1, debugOn, bank) | (real_read6502(pc + 2, debugOn, bank) << 8);
uint16_t ptr = debug_read6502(pc + 1, bank) | (debug_read6502(pc + 2, bank) << 8);
if (isXrel)
ptr += regs.x;
uint8_t ind_bank = ptr < 0xc000 ? memory_get_ram_bank() : memory_get_rom_bank();
*eff_addr = real_read6502(ptr, debugOn, ind_bank) | (real_read6502(ptr + 1, debugOn, ind_bank) << 8);
*eff_addr = debug_read6502(ptr, ind_bank) | (debug_read6502(ptr + 1, ind_bank) << 8);
if (isYrel)
*eff_addr += regs.y;
} else {
*eff_addr = real_read6502(pc + 1, debugOn, bank) | (real_read6502(pc + 2, debugOn, bank) << 8);
*eff_addr = debug_read6502(pc + 1, bank) | (debug_read6502(pc + 2, bank) << 8);
if (isXrel)
*eff_addr += regs.x;
if (isYrel)
Expand Down
2 changes: 1 addition & 1 deletion src/disasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

#include <stdint.h>

int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, bool debugOn, uint8_t bank, uint8_t implied_status, int32_t *eff_addr);
int disasm(uint16_t pc, uint8_t *RAM, char *line, unsigned int max_line, int16_t bank, uint8_t implied_status, int32_t *eff_addr);

#endif
8 changes: 4 additions & 4 deletions src/ieee.c
Original file line number Diff line number Diff line change
Expand Up @@ -1755,17 +1755,17 @@ ieee_init()
// Locate and remember cbdos_flags variable address in KERNAL vars
{
// check JMP instruction at ACPTR API
if (real_read6502(0xffa5, true, 0) != 0x4c) goto fail;
if (debug_read6502(0xffa5, 0) != 0x4c) goto fail;

// get address of ACPTR routine
uint16_t kacptr = real_read6502(0xffa6, true, 0) | real_read6502(0xffa7, true, 0) << 8;
uint16_t kacptr = debug_read6502(0xffa6, 0) | debug_read6502(0xffa7, 0) << 8;
if (kacptr < 0xc000) goto fail;

// first instruction is BIT cbdos_flags
if (real_read6502(kacptr, true, 0) != 0x2c) goto fail;
if (debug_read6502(kacptr, 0) != 0x2c) goto fail;

// get the address of cbdos_flags
cbdos_flags = real_read6502(kacptr+1, true, 0) | real_read6502(kacptr+2, true, 0) << 8;
cbdos_flags = debug_read6502(kacptr+1, 0) | debug_read6502(kacptr+2, 0) << 8;

if (cbdos_flags < 0x0200 || cbdos_flags >= 0x0400) goto fail;
goto success;
Expand Down
40 changes: 20 additions & 20 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,14 @@ static bool
is_kernal()
{
// only for KERNAL
return (real_read6502(0xfff6, true, memory_get_rom_bank()) == 'M' &&
real_read6502(0xfff7, true, memory_get_rom_bank()) == 'I' &&
real_read6502(0xfff8, true, memory_get_rom_bank()) == 'S' &&
real_read6502(0xfff9, true, memory_get_rom_bank()) == 'T')
|| (real_read6502(0xc008, true, memory_get_rom_bank()) == 'M' &&
real_read6502(0xc009, true, memory_get_rom_bank()) == 'I' &&
real_read6502(0xc00a, true, memory_get_rom_bank()) == 'S' &&
real_read6502(0xc00b, true, memory_get_rom_bank()) == 'T');
return (debug_read6502(0xfff6, USE_CURRENT_BANK) == 'M' &&
debug_read6502(0xfff7, USE_CURRENT_BANK) == 'I' &&
debug_read6502(0xfff8, USE_CURRENT_BANK) == 'S' &&
debug_read6502(0xfff9, USE_CURRENT_BANK) == 'T')
|| (debug_read6502(0xc008, USE_CURRENT_BANK) == 'M' &&
debug_read6502(0xc009, USE_CURRENT_BANK) == 'I' &&
debug_read6502(0xc00a, USE_CURRENT_BANK) == 'S' &&
debug_read6502(0xc00b, USE_CURRENT_BANK) == 'T');
}

static void
Expand Down Expand Up @@ -1219,29 +1219,29 @@ set_kernal_status(uint8_t s)
// from it.

// JMP in the KERNAL API vectors
if (real_read6502(0xffb7, true, 0) != 0x4c) {
if (debug_read6502(0xffb7, 0) != 0x4c) {
return false;
}
// target of KERNAL API vector JMP
uint16_t readst = real_read6502(0xffb8, true, 0) | real_read6502(0xffb9, true, 0) << 8;
uint16_t readst = debug_read6502(0xffb8, 0) | debug_read6502(0xffb9, 0) << 8;
if (readst < 0xc000) {
return false;
}
// ad 89 02 lda $0289
if (real_read6502(readst, true, 0) != 0xad) {
if (debug_read6502(readst, 0) != 0xad) {
return false;
}
// ad 89 02 lda $0289
if (real_read6502(readst + 3, true, 0) != 0x0d) {
if (debug_read6502(readst + 3, 0) != 0x0d) {
return false;
}
// ad 89 02 lda $0289
if (real_read6502(readst + 6, true, 0) != 0x8d) {
if (debug_read6502(readst + 6, 0) != 0x8d) {
return false;
}
uint16_t status0 = real_read6502(readst+1, true, 0) | real_read6502(readst+2, true, 0) << 8;
uint16_t status1 = real_read6502(readst+4, true, 0) | real_read6502(readst+5, true, 0) << 8;
uint16_t status2 = real_read6502(readst+7, true, 0) | real_read6502(readst+8, true, 0) << 8;
uint16_t status0 = debug_read6502(readst+1, 0) | debug_read6502(readst+2, 0) << 8;
uint16_t status1 = debug_read6502(readst+4, 0) | debug_read6502(readst+5, 0) << 8;
uint16_t status2 = debug_read6502(readst+7, 0) | debug_read6502(readst+8, 0) << 8;
// all three addresses must be the same
if (status0 != status1 || status0 != status2) {
return false;
Expand Down Expand Up @@ -1398,9 +1398,9 @@ handle_ieee_intercept()
}

increment_wrap_at_page_boundary(&regs.sp);
uint8_t low = real_read6502(regs.sp, true, 0);
uint8_t low = debug_read6502(regs.sp, USE_CURRENT_BANK);
increment_wrap_at_page_boundary(&regs.sp);
regs.pc = ((real_read6502(regs.sp, true, 0) << 8) | low) + 1;
regs.pc = ((debug_read6502(regs.sp, USE_CURRENT_BANK) << 8) | low) + 1;
}
return handled;
}
Expand Down Expand Up @@ -1491,9 +1491,9 @@ emulator_loop(void *param)
printf(":.,%04x ", regs.pc);

char disasm_line[15];
int len = disasm(regs.pc, RAM, disasm_line, sizeof(disasm_line), false, 0, regs.status, &eff_addr);
int len = disasm(regs.pc, RAM, disasm_line, sizeof(disasm_line), -1, regs.status, &eff_addr);
for (int i = 0; i < len; i++) {
printf("%02x ", real_read6502(regs.pc + i, true, 0));
printf("%02x ", debug_read6502(regs.pc + i, USE_CURRENT_BANK));
}
for (int i = 0; i < 9 - 3 * len; i++) {
printf(" ");
Expand Down
19 changes: 9 additions & 10 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,7 @@ read6502(uint16_t address) {
}
}

return real_read6502(address, false, 0);
}


uint8_t
real_read6502(uint16_t address, bool debugOn, uint8_t bank)
{
if (reportUsageStatisticsFilename!=NULL && debugOn == false) {
if (reportUsageStatisticsFilename!=NULL) {
if (address < 0xa000) {
RAM_system_reads[address]++;
} else if (address < 0xc000) {
Expand All @@ -174,6 +167,12 @@ real_read6502(uint16_t address, bool debugOn, uint8_t bank)
}
}

return real_read6502(address, false, USE_CURRENT_BANK);
}

uint8_t
real_read6502(uint16_t address, bool debugOn, int16_t bank)
{
if (address < 0x9f00) { // RAM
return RAM[address];
} else if (address < 0xa000) { // I/O
Expand Down Expand Up @@ -205,14 +204,14 @@ real_read6502(uint16_t address, bool debugOn, uint8_t bank)
return 0x9f; // open bus read
}
} else if (address < 0xc000) { // banked RAM
int ramBank = debugOn ? bank : effective_ram_bank();
int ramBank = bank >= 0 ? (uint8_t)bank : effective_ram_bank();
if (ramBank < num_ram_banks) {
return RAM[0xa000 + (ramBank << 13) + address - 0xa000];
} else {
return (address >> 8) & 0xff; // open bus read
}
} else { // banked ROM
int romBank = debugOn ? bank : rom_bank;
int romBank = bank >= 0 ? (uint8_t)bank : rom_bank;
if (romBank < 32) {
return ROM[(romBank << 14) + address - 0xc000];
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#include <stdio.h>
#include <SDL.h>

#define USE_CURRENT_BANK (-1)
#define debug_read6502(a, b) real_read6502(a, true, b)

uint8_t read6502(uint16_t address);
uint8_t real_read6502(uint16_t address, bool debugOn, uint8_t bank);
uint8_t real_read6502(uint16_t address, bool debugOn, int16_t bank);
void write6502(uint16_t address, uint8_t value);
void vp6502();

Expand Down
2 changes: 1 addition & 1 deletion src/testbench.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void testbench_init()
if (iaddr == -1) {
invalid();
} else {
printf("%lx\n", (long)real_read6502((uint16_t)iaddr, true, 0));
printf("%lx\n", (long)debug_read6502((uint16_t)iaddr, USE_CURRENT_BANK));
fflush(stdout);
}
}
Expand Down

0 comments on commit 6262ec7

Please sign in to comment.