- Comença a vores el depurador

This commit is contained in:
2024-04-10 20:35:36 +02:00
parent 1dec5f7a30
commit 9a65ef3915
5 changed files with 101 additions and 19 deletions

View File

@@ -2,10 +2,17 @@
#include <stdio.h> #include <stdio.h>
#include "z80.h" #include "z80.h"
#include "z80dis.h" #include "z80dis.h"
#include <ncurses.h>
#include <string.h>
uint8_t memory[65536]; uint8_t memory[65536];
uint32_t t = 0; uint32_t t = 0;
int w, h;
WINDOW *win_regs;
WINDOW *win_mem;
WINDOW *win_code;
int ula_in() int ula_in()
{ {
return 0; return 0;
@@ -16,6 +23,17 @@ void ula_out(int val)
} }
void refresh_registers()
{
mvwprintw(win_regs, 1, 2, "AF: %04X AF': %04X", z80::getAF(), z80::getAF(true));
mvwprintw(win_regs, 2, 2, "BC: %04X BC': %04X", z80::getBC(), z80::getBC(true));
mvwprintw(win_regs, 3, 2, "DE: %04X DE': %04X", z80::getDE(), z80::getDE(true));
mvwprintw(win_regs, 4, 2, "HL: %04X HL': %04X", z80::getHL(), z80::getHL(true));
mvwprintw(win_regs, 5, 2, "IX: %04X IY': %04X", z80::getIX(), z80::getIY());
mvwprintw(win_regs, 6, 2, "SP: %04X PC': %04X", z80::getSP(), z80::getPC());
wrefresh(win_regs);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
FILE* f = fopen("48.rom", "rb"); FILE* f = fopen("48.rom", "rb");
@@ -25,16 +43,54 @@ int main(int argc, char *argv[])
z80::reset(memory); z80::reset(memory);
z80::connect_port(0xfe, ula_in, ula_out); z80::connect_port(0xfe, ula_in, ula_out);
initscr();
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLACK);
init_pair(2, COLOR_YELLOW, COLOR_BLUE);
getmaxyx(stdscr, h, w);
win_regs = newwin(10,23,0,0);
win_mem = newwin(10,w-23,0,23);
win_code = newwin(h-10,w,10,0);
refresh();
box(win_regs,0,0);
mvwprintw(win_regs, 0, 2, " REGISTERS: ");
refresh_registers();
box(win_mem,0,0);
mvwprintw(win_mem, 0, 2, " MEMORY: ");
mvwprintw(win_mem, 1, 2, "%ix%i", w, h);
wrefresh(win_mem);
box(win_code,0,0);
//wattron(win_code, COLOR_PAIR(1));
//mvwprintw(win_code, 1, 1, "Hello world %s !!!", "hola");
//wattroff(win_code, COLOR_PAIR(1));
wrefresh(win_code);
bool should_exit = false; bool should_exit = false;
int linea=1;
char old_line[256];
old_line[0]=0;
while(!should_exit) while(!should_exit)
{ {
uint16_t PC = z80::getPC(); uint16_t PC = z80::getPC();
printf("[%04x] ", PC); if (old_line[0]!=0) {
wattron(win_code, COLOR_PAIR(1));
z80dis::printAsm(&memory[PC]); mvwprintw(win_code, linea-1, 1, old_line);
wattroff(win_code, COLOR_PAIR(1));
}
sprintf(old_line, "[%04x] ", PC);
strcat(old_line, z80dis::getAsm(&memory[PC]));
wattron(win_code, COLOR_PAIR(2));
mvwprintw(win_code, linea, 1, old_line);
wattron(win_code, COLOR_PAIR(2));
wrefresh(win_code);
linea++;
t += z80::step(); t += z80::step();
getchar(); refresh_registers();
getch();
} }
return 0; return 0;

15
z80.cpp
View File

@@ -129,11 +129,6 @@ namespace z80
#define SWAP(a,b) {auto temp=a;a=b;b=temp;} #define SWAP(a,b) {auto temp=a;a=b;b=temp;}
uint16_t getPC()
{
return rPC;
}
uint8_t READ_MEM_8(const uint16_t addr) uint8_t READ_MEM_8(const uint16_t addr)
{ {
t+=3; t+=3;
@@ -2562,5 +2557,15 @@ namespace z80
} }
} }
uint16_t getAF(const bool alt) { return alt?rAF2:rAF; }
uint16_t getBC(const bool alt) { return alt?rBC2:rBC; }
uint16_t getDE(const bool alt) { return alt?rDE2:rDE; }
uint16_t getHL(const bool alt) { return alt?rHL2:rHL; }
uint16_t getIX() { return rIX; }
uint16_t getIY() { return rIY; }
uint16_t getSP() { return rSP; }
uint16_t getPC() { return rPC; }
} }

9
z80.h
View File

@@ -7,5 +7,14 @@ namespace z80
void reset(uint8_t* mem); void reset(uint8_t* mem);
void connect_port(int num, int (*in_ptr)(), void (*out_ptr)(int)); void connect_port(int num, int (*in_ptr)(), void (*out_ptr)(int));
uint32_t step(); uint32_t step();
uint16_t getAF(const bool alt=false);
uint16_t getBC(const bool alt=false);
uint16_t getDE(const bool alt=false);
uint16_t getHL(const bool alt=false);
uint16_t getIX();
uint16_t getIY();
uint16_t getSP();
uint16_t getPC(); uint16_t getPC();
} }

View File

@@ -4,7 +4,7 @@
namespace z80dis namespace z80dis
{ {
//char buffer[256]; char buffer[256];
int opcode_size = 0; int opcode_size = 0;
// $%04x // $%04x
// $%02x // $%02x
@@ -58,35 +58,47 @@ namespace z80dis
void printOpcode(const uint8_t *memory) void printOpcode(const uint8_t *memory)
{ {
for (int i=0; i<4;++i) if (opcode_size>i) printf("%02x ", *(memory+i)); else printf(" "); char hex[4];
for (int i=0; i<4;++i)
{
if (opcode_size>i)
sprintf(hex, "%02x ", *(memory+i));
else
sprintf(hex, " ");
strcat(buffer, hex);
}
} }
void printAsm(const uint8_t *memory) const char *getAsm(const uint8_t *memory, const bool include_opcode)
{ {
opcode_size = 0; opcode_size = 0;
buffer[0]=0;
char txt[20];
const char *base = getBase(memory); const char *base = getBase(memory);
if (strstr(base, "4x")) { if (strstr(base, "4x")) {
opcode_size+=2; opcode_size+=2;
printOpcode(memory); printOpcode(memory);
const uint16_t word = *(uint16_t*)(memory+1); const uint16_t word = *(uint16_t*)(memory+1);
printf(base, word); sprintf(txt, base, word);
} else if (strstr(base, "2x")) { } else if (strstr(base, "2x")) {
opcode_size+=1; opcode_size+=1;
printOpcode(memory); printOpcode(memory);
printf(base, *(memory+1)); sprintf(txt, base, *(memory+1));
} else if (strstr(base, "hhd")) { } else if (strstr(base, "hhd")) {
opcode_size+=1; opcode_size+=1;
printOpcode(memory); printOpcode(memory);
if (opcode_size>4) { if (opcode_size>4) {
opcode_size=4; opcode_size=4;
printf(base, (int8_t)*(memory+2)); sprintf(txt, base, (int8_t)*(memory+2));
} else { } else {
printf(base, (int8_t)*(memory+1)); sprintf(txt, base, (int8_t)*(memory+1));
} }
} else { } else {
printOpcode(memory); printOpcode(memory);
printf(base); sprintf(txt, base);
} }
strcat(buffer, txt);
return buffer;
} }
} }

View File

@@ -4,5 +4,5 @@
namespace z80dis namespace z80dis
{ {
void printAsm(const uint8_t *memory); const char *getAsm(const uint8_t *memory, const bool include_opcode=true);
} }