Improving debugger.

This commit is contained in:
2017-02-01 16:47:23 +01:00
parent 42d738b5fa
commit ad4b15514d
8 changed files with 71 additions and 57 deletions

View File

@@ -116,9 +116,11 @@ static void debug_print_datastack(byte offset) {
debug_draw_space(72, 2, 3, 20, "DS:");
char cadena[10];
for (int i = 0; i < 20; i++) {
sprintf(cadena, "%.2X", debug_mem[0x8801 + i + offset]);
if (debug_mem[0x8800] == 1 + i + offset) debug_set_ink(255, 0, 0); else debug_set_ink(255, 255, 255);
debug_print(72, 2 + i, cadena); debug_print_ex(74, 2 + i, &debug_mem[0x8801 + i + offset], 1);
sprintf(cadena, "%.2X", debug_mem[0x8800 + i + offset]);
if (i + offset == 0) { debug_set_ink(255, 0, 0); }
else if(debug_mem[0x8800] == i + offset) { debug_set_ink(255, 255, 0); }
else { debug_set_ink(255, 255, 255); }
debug_print(72, 2 + i, cadena); debug_print_ex(74, 2 + i, &debug_mem[0x8800 + i + offset], 1);
}
}
@@ -126,9 +128,11 @@ static void debug_print_callstack(byte offset) {
debug_draw_space(76, 2, 3, 20, "CS:");
char cadena[10];
for (int i = 0; i < 20; i++) {
sprintf(cadena, "%.2X", debug_mem[0x8901 + i + offset]);
if (debug_mem[0x8900] == 1 + i + offset) debug_set_ink(255, 0, 0); else debug_set_ink(255, 255, 255);
debug_print(76, 2 + i, cadena); debug_print_ex(78, 2 + i, &debug_mem[0x8901 + i + offset], 1);
sprintf(cadena, "%.2X", debug_mem[0x8900 + i + offset]);
if (i + offset == 0) { debug_set_ink(255, 0, 0); }
else if (debug_mem[0x8900] == i + offset) { debug_set_ink(255, 255, 0); }
else { debug_set_ink(255, 255, 255); }
debug_print(76, 2 + i, cadena); debug_print_ex(78, 2 + i, &debug_mem[0x8900 + i + offset], 1);
}
}
@@ -165,6 +169,7 @@ static void debug_print_registers() {
debug_draw_space(72, 23, 6, 1, "rX:");
debug_draw_space(72, 25, 3, 1, "rY:");
debug_draw_space(72, 27, 3, 1, "rZ:");
debug_draw_space(72, 29, 6, 1, "PC:");
debug_set_ink(255, 255, 255);
char cadena[10];
sprintf(cadena, "%.4X", *rX);
@@ -173,10 +178,12 @@ static void debug_print_registers() {
debug_print(72, 25, cadena); debug_print_ex(74, 25, &debug_mem[0xFFFC], 1);
sprintf(cadena, "%.2X", *rZ);
debug_print(72, 27, cadena); debug_print_ex(74, 27, &debug_mem[0xFFFD], 1);
sprintf(cadena, "%.4X", *pc);
debug_print(72, 29, cadena); debug_print_ex(76, 29, &debug_mem[0xFFFE], 2);
}
char asm_line[255];
static void get_op1(word& offset, const char* op) { sprintf(asm_line, "%.4X: %.2X %s", offset, debug_mem[offset++], op); }
static void get_op1(word& offset, const char* op) { sprintf(asm_line, "%.4X: %.2X %s", offset, debug_mem[offset], op); offset += 1; }
static void get_op2(word& offset, const char* op) { sprintf(asm_line, "%.4X: %.2X%.2X %s %.2X", offset, debug_mem[offset], debug_mem[offset + 1], op, debug_mem[offset + 1]); offset += 2; }
static void get_op3(word& offset, const char* op) { sprintf(asm_line, "%.4X: %.2X%.2X%.2X %s %.4X", offset, debug_mem[offset], debug_mem[offset + 1], debug_mem[offset + 2], op, debug_mem[offset + 1] + (debug_mem[offset + 2] << 8));; offset += 3; }
static char* get_asm_line(word& offset) {
@@ -260,7 +267,7 @@ static char* get_asm_line(word& offset) {
}
void debug_print_asm(byte offset) {
word pointer = *pc + offset; if (pointer >= 10) pointer -= 10;
word pointer = *pc + offset;// if (pointer >= 10) pointer -= 10;
debug_draw_space(39, 39, 40, 20, "ASM:");
debug_set_ink(255, 255, 255);
char cadena[255];
@@ -284,7 +291,7 @@ void debug_init(unsigned char* mem) {
rZ = &debug_mem[0xFFFD];
pc = (unsigned short*)&debug_mem[0xFFFE];
debug_window = SDL_CreateWindow("Definitely PaCo Debugger", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
debug_window = SDL_CreateWindow("Definitely PaCo Debugger", 1120, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
debug_renderer = SDL_CreateRenderer(debug_window, -1, SDL_RENDERER_PRESENTVSYNC);
//SDL_RenderSetLogicalSize(debug_renderer, 152, 120);
SDL_SetRenderDrawColor(debug_renderer, 128, 128, 128, 255);

View File

@@ -46,6 +46,8 @@ int main(int argc, char** argv) {
debug_init(vm_get_memory());
bool running = false;
static bool should_quit = false;
static SDL_Event sdlEvent;
SDL_Scancode just_pressed;
@@ -57,14 +59,16 @@ int main(int argc, char** argv) {
if (sdlEvent.type == SDL_WINDOWEVENT_CLOSE) { should_quit = true; break; }
if (sdlEvent.type == SDL_QUIT) { should_quit = true; break; }
else if (sdlEvent.type == SDL_KEYDOWN) {
anykey = true;
//anykey = true;
just_pressed = sdlEvent.key.keysym.scancode;
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_SPACE) { anykey = true; }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { should_quit = true; }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_F10) { vm_big_step(); debug_update(); }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_F11) { vm_step(); debug_update(); }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_F5) { running = !running; if (!running) debug_update(); }
}
}
//vm_step();
if (running) vm_step();
//if (SDL_GetTicks() - ticks >= 15) { vdp_flip(); ticks = SDL_GetTicks(); }
}

View File

@@ -42,6 +42,7 @@ struct t_constant {
static byte* code; // [MAX_CODE_SIZE];
static word codepos = 0;
static word lines[32768];
static word current_line;
static t_label known_labels[MAX_LABELS];
static int num_known_labels = 0;
@@ -86,14 +87,14 @@ static void int_to_string(int value, char* label) {
/****************************************************************************************/
static void emmit(const byte value) {
lines[codepos] = tkn_get_line();
lines[codepos] = current_line;
code[codepos++] = value;
}
static void emmit_w(const word value) {
lines[codepos] = tkn_get_line();
lines[codepos] = current_line;
code[codepos++] = value & 255;
lines[codepos] = tkn_get_line();
lines[codepos] = current_line;
code[codepos++] = value >> 8;
}
@@ -114,7 +115,7 @@ static int get_variable_index(const char* string, const int array_size = 1) {
t_variable* variable = variables;
t_variable* last = variables;
while (variable != nullptr) {
if (strcmp(variable->name, string) == 0) { variable_is_array = (variable->size > 1); return variable->index; }
if (strcmp(variable->name, string) == 0) { variable_is_array = (variable->size > 1); return variable->index + 0xC000; }
last = variable;
variable = variable->next;
}
@@ -749,6 +750,7 @@ static void parse_wait() {
static void parse_statements() {
while (!parser_finished) {
current_line = tkn_get_line();
switch (tkn_get_token()) {
case TOKEN_CONST:
tkn_next(); parse_const(); break;

View File

@@ -6,14 +6,14 @@
locate 4 4 print chr &h87, chr &h81, chr &h8D, chr &h85, chr &h85, chr &h85, chr &h8D, chr &h85
locate 3 8 print "PRESS SPACE"
updatescr
x = 0
x = 8
dx = 1
setsprite 0 &HF9 INK_RED
peiv:
putsprite 0 x 40
putsprite 0 x 40
putsprite 0 x 40
putsprite 0 x 10
updatescr
x = x + dx
if x = 8 or x = 128 then dx = -dx end
if anykey then goto continuar end
goto peiv
continuar:

View File

@@ -66,7 +66,7 @@ t_token_op tkn_tokens[] = {
{ "wait", TOKEN_WAIT },
};
static char hex_digit(const char digit) { return digit - (digit < 57 ? 48 : 55); }
static char hex_digit(const char digit) { return digit - (digit <= 57 ? 48 : 55); }
#define CCHR *ptr
#define NEXT if(*ptr==10){line++;row=0;}else{row++;};ptr++

View File

@@ -64,7 +64,7 @@ static Uint8 palette[16][4] = {
void vdp_init() {
SDL_Init(SDL_INIT_EVERYTHING);
sdlWindow = SDL_CreateWindow("PaCo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 608, 480, SDL_WINDOW_SHOWN);
sdlWindow = SDL_CreateWindow("PaCo", 160, SDL_WINDOWPOS_UNDEFINED, 608, 480, SDL_WINDOW_SHOWN);
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
//SDL_SetRenderDrawBlendMode(sdlRenderer, SDL_BLENDMODE_BLEND);
SDL_RenderSetLogicalSize(sdlRenderer, 152, 120);

69
vm.cpp
View File

@@ -111,6 +111,7 @@ static int vm_cycles = 0;
#define PUSH(x, y) x[++x[0]] = y
#define POP(x) (x[x[0]--])
#define PEEK(x) (x[x[0]])
#define GRAB(x) a = x[x[0]--]; b = x[x[0]--];
bool sleeping = false;
@@ -160,10 +161,10 @@ void vm_init(const char* filename) {
inline unsigned short WORD() { vm_pc += 2; return vm_program[vm_pc - 2] + (vm_program[vm_pc - 1] << 8); }
const int vm_step() {
if (sleeping) return vm_cycles;
const bool vm_step() {
if (sleeping) return false;
char val, val2;
char a, b, c;
OPS opcode = (OPS)vm_program[vm_pc++];
switch (opcode) {
@@ -171,12 +172,12 @@ const int vm_step() {
case OP_NOP: vm_cycles++; break;
case OP_PUSH: PUSH(ds, vm_program[vm_pc++]); vm_cycles++; break;
case OP_POP: POP(ds); vm_cycles++; break;
case OP_DUP: PUSH(ds, PEEK(ds)); vm_cycles++; break;
case OP_SWAP: val = POP(ds); val2 = POP(ds); PUSH(ds, val); PUSH(ds, val2); vm_cycles++; break;
case OP_DUP: a = PEEK(ds); PUSH(ds, a); vm_cycles++; break;
case OP_SWAP: a = POP(ds); b = POP(ds); PUSH(ds, a); PUSH(ds, b); vm_cycles++; break;
case OP_LOAD: PUSH(ds, mem[WORD()]); vm_cycles++; break;
case OP_LOADI: PUSH(ds, mem[WORD() + rY]); vm_cycles++; break;
case OP_STORE: mem[WORD()] = POP(ds); vm_cycles++; break;
case OP_STOREI: val = POP(ds); mem[WORD() + rY] = val; vm_cycles++; break;
case OP_STOREI: a = POP(ds); mem[WORD() + rY] = a; vm_cycles++; break;
case OP_LOADXY: PUSH(ds, mem[rX + rY]); vm_cycles++; break;
case OP_STOREXY: mem[rX + rY] = POP(ds); vm_cycles++; break;
case OP_SETX: rX = WORD(); vm_cycles++; break;
@@ -193,8 +194,8 @@ const int vm_step() {
case OP_JMP: vm_pc = WORD(); vm_cycles++; break;
case OP_JNT: if (POP(ds) == 0) { vm_pc = WORD(); } else vm_pc+=2; vm_cycles++; break;
case OP_JTR: if (POP(ds) != 0) { vm_pc = WORD(); } else vm_pc+=2; vm_cycles++; break;
case OP_JSR: PUSH(cs, vm_pc + 2); PUSH(cs, rX & 255); PUSH(cs, rX >> 8); PUSH(cs, rY); PUSH(cs, rZ); vm_pc = WORD(); vm_cycles++; break;
case OP_RET: rZ = POP(cs); rY = POP(cs); rX = (POP(cs) << 8) + POP(cs); vm_pc = POP(cs); vm_cycles++; break;
case OP_JSR: PUSH(cs, (vm_pc + 2) & 0xff); PUSH(cs, (vm_pc + 2) >> 8); PUSH(cs, rX & 255); PUSH(cs, rX >> 8); PUSH(cs, rY); PUSH(cs, rZ); vm_pc = WORD(); vm_cycles++; break;
case OP_RET: rZ = POP(cs); rY = POP(cs); rX = (POP(cs) << 8) + POP(cs); vm_pc = POP(cs); vm_pc = (vm_pc << 8) + POP(cs); vm_cycles++; break;
case OP_CALL: /*external_calls[vm_program[vm_pc++]](vm_datastack); vm_cycles++;*/ break;
case OP_RJ: vm_pc += vm_program[vm_pc]; vm_cycles++; break;
case OP_RB: vm_pc -= vm_program[vm_pc]+1; vm_cycles++; break;
@@ -210,42 +211,42 @@ const int vm_step() {
case OP_RJZN: if (rZ != 0) vm_pc += vm_program[vm_pc]; else vm_pc++; vm_cycles++; break;
case OP_RBZZ: if (rZ == 0) vm_pc -= vm_program[vm_pc] + 1; else vm_pc++; vm_cycles++; break;
case OP_RBZN: if (rZ != 0) vm_pc -= vm_program[vm_pc] + 1; else vm_pc++; vm_cycles++; break;
case OP_ADD: PUSH(ds, POP(ds) + POP(ds)); vm_cycles++; break;
case OP_SUB: PUSH(ds, POP(ds) - POP(ds)); vm_cycles++; break;
case OP_MUL: PUSH(ds, POP(ds) * POP(ds)); vm_cycles++; break;
case OP_DIV: PUSH(ds, POP(ds) / POP(ds)); vm_cycles++; break;
case OP_MOD: PUSH(ds, POP(ds) % POP(ds)); vm_cycles++; break;
case OP_AND: PUSH(ds, POP(ds) & POP(ds)); vm_cycles++; break;
case OP_OR: PUSH(ds, POP(ds) | POP(ds)); vm_cycles++; break;
case OP_NOT: PUSH(ds, !POP(ds)); vm_cycles++; break;
case OP_NEG: PUSH(ds, -POP(ds)); vm_cycles++; break;
case OP_INC: PUSH(ds, POP(ds) + 1); vm_cycles++; break;
case OP_DEC: PUSH(ds, POP(ds) - 1); vm_cycles++; break;
case OP_ADD: GRAB(ds); PUSH(ds, a + b); vm_cycles++; break;
case OP_SUB: GRAB(ds); PUSH(ds, a - b); vm_cycles++; break;
case OP_MUL: GRAB(ds); PUSH(ds, a * b); vm_cycles++; break;
case OP_DIV: GRAB(ds); PUSH(ds, a / b); vm_cycles++; break;
case OP_MOD: GRAB(ds); PUSH(ds, a % b); vm_cycles++; break;
case OP_AND: GRAB(ds); PUSH(ds, a & b); vm_cycles++; break;
case OP_OR: GRAB(ds); PUSH(ds, a | b); vm_cycles++; break;
case OP_NOT: a = POP(ds); PUSH(ds, !a); vm_cycles++; break;
case OP_NEG: a = POP(ds); PUSH(ds, -a); vm_cycles++; break;
case OP_INC: a = POP(ds); PUSH(ds, a + 1); vm_cycles++; break;
case OP_DEC: a = POP(ds); PUSH(ds, a - 1); vm_cycles++; break;
case OP_CONCAT:
val = POP(ds);
for (int i = 0; i < val; i++) PUSH(cs, POP(ds));
val2 = POP(ds);
for (int i = 0; i < val; i++) PUSH(ds, POP(cs));
PUSH(ds, val + val2);
a = POP(ds);
for (int i = 0; i < a; i++) { c = POP(ds); PUSH(cs, c); }
b = POP(ds);
for (int i = 0; i < a; i++) { c = POP(cs); PUSH(ds, c); }
PUSH(ds, a + b);
vm_cycles++; break;
case OP_EQ: PUSH(ds, POP(ds) == POP(ds)); vm_cycles++; break;
case OP_NEQ: PUSH(ds, POP(ds) != POP(ds)); vm_cycles++; break;
case OP_LT: PUSH(ds, POP(ds) < POP(ds)); vm_cycles++; break;
case OP_GT: PUSH(ds, POP(ds) > POP(ds)); vm_cycles++; break;
case OP_LEQ: PUSH(ds, POP(ds) <= POP(ds)); vm_cycles++; break;
case OP_GEQ: PUSH(ds, POP(ds) >= POP(ds)); vm_cycles++; break;
case OP_EQ: GRAB(ds); PUSH(ds, a == b); vm_cycles++; break;
case OP_NEQ: GRAB(ds); PUSH(ds, a != b); vm_cycles++; break;
case OP_LT: GRAB(ds); PUSH(ds, a < b); vm_cycles++; break;
case OP_GT: GRAB(ds); PUSH(ds, a > b); vm_cycles++; break;
case OP_LEQ: GRAB(ds); PUSH(ds, a <= b); vm_cycles++; break;
case OP_GEQ: GRAB(ds); PUSH(ds, a >= b); vm_cycles++; break;
case OP_IN: PUSH(ds, in_ports[vm_program[vm_pc++]]()); vm_cycles++; break;
case OP_OUT: out_ports[vm_program[vm_pc++]](POP(ds)); vm_cycles++; break;
case OP_SLEEP: SDL_Delay(POP(ds) * 1000); vm_cycles++; break;
}
return vm_cycles;
return false;
}
const int vm_big_step() {
word* lines = parser_get_lines();
word current_line = lines[vm_pc];
while (vm_pc > 32768 || lines[vm_pc] == current_line) { vm_step(); }
return 0;
word current_line = lines[vm_pc]-1;
while ((vm_pc > 32768) || (lines[vm_pc]-1 == current_line)) { vm_step(); }
return false;
}
/*void vm_register_call(void(*callback)(t_stack&)) {
external_calls[numcallbacks++] = callback;

4
vm.h
View File

@@ -2,8 +2,8 @@
//#include "stack.h"
void vm_init(const char* filename);
const int vm_step();
const int vm_big_step();
const bool vm_step();
const bool vm_big_step();
void vm_register_in_port(const unsigned char port, unsigned char(*callback)(void));
void vm_register_out_port(const unsigned char port, void(*callback)(const unsigned char&));
void vm_call_interrupt(const char num);