From 0864cdcf890d11dc2ba4507c1829f8d5ef077bd8 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 1 Feb 2017 19:49:26 +0100 Subject: [PATCH] Fixes. Debugger improved. --- .hgignore | 2 +- constants.h | 9 ++++++++ debug.cpp | 49 ++++++++++++++++++++++++++++++---------- debug.h | 4 ++++ main.cpp | 28 +++++++++++++++++++---- rom.bin | Bin 0 -> 200 bytes test.bas | 64 ++++++++++++++++++++++++++++++++-------------------- vm.cpp | 30 ++++++++++++------------ vm.h | 4 ++-- 9 files changed, 132 insertions(+), 58 deletions(-) create mode 100644 rom.bin diff --git a/.hgignore b/.hgignore index 8bef208..0fa0807 100644 --- a/.hgignore +++ b/.hgignore @@ -14,5 +14,5 @@ data/* *.dll .DS_Store trick.ini -*.bin +test.bin diff --git a/constants.h b/constants.h index 9ecfdc9..8aa130a 100644 --- a/constants.h +++ b/constants.h @@ -35,4 +35,13 @@ void register_constants() { parser_register_constant("paper_teal", 0xD0); parser_register_constant("paper_gray", 0xE0); parser_register_constant("paper_black2", 0xF0); + + parser_register_constant("btn_any", 0x00); + parser_register_constant("btn_up", 0x01); + parser_register_constant("btn_down", 0x02); + parser_register_constant("btn_left", 0x03); + parser_register_constant("btn_right", 0x04); + parser_register_constant("btn_a", 0x05); + parser_register_constant("btn_b", 0x06); + parser_register_constant("btn_menu", 0x07); } diff --git a/debug.cpp b/debug.cpp index bf54871..f803c59 100644 --- a/debug.cpp +++ b/debug.cpp @@ -8,6 +8,7 @@ typedef unsigned char byte; typedef unsigned short word; static SDL_Window* debug_window = nullptr; +static Uint32 debug_window_id; static SDL_Renderer* debug_renderer = nullptr; static SDL_Texture* debug_texture = nullptr; static SDL_Rect src, dst; @@ -15,6 +16,8 @@ static Uint8 ink[4]; static Uint8 paper[4]; static unsigned char* debug_mem = nullptr; +static bool breakpoints[65536] {false}; + static unsigned short* rX; static unsigned char* rY; static unsigned char* rZ; @@ -144,21 +147,25 @@ static void debug_print_basic(byte offset) { return; } int line = offset; + int l = 0; + if (lines[*pc] >= 35) line = lines[*pc] + 30; int row = 0; char* prog = program; - if (lines[*pc] == line) { debug_set_ink(0, 0, 0); debug_fill_rect(1, 2 + line, 70, 1); debug_set_ink(255, 255, 0); } + if (lines[*pc] == line) { debug_set_ink(0, 0, 0); debug_fill_rect(1, 2 + l, 70, 1); debug_set_ink(255, 255, 0); } else debug_set_ink(255, 255, 255); while (line < 35 && *prog != 0) { if (*prog == '\t') { row += 4; } else if (*prog == '\n') { - row = 0; line++; - if (lines[*pc] == line) { debug_set_ink(0, 0, 0); debug_fill_rect(1, 2 + line, 70, 1); debug_set_ink(255, 255, 0); } + row = 0; line++; l++; + int bm = 0; while (lines[bm] < line) { bm++; } + if (breakpoints[bm]) { debug_set_ink(64, 0, 0); debug_fill_rect(1, 2 + l, 70, 1); } + if (lines[*pc] == line) { debug_set_ink(0, 0, 0); debug_fill_rect(1, 2 + l, 70, 1); debug_set_ink(255, 255, 0); } else debug_set_ink(255, 255, 255); } else if (*prog == 13) { // row++; } else { - if (row < 70) debug_draw_char(1 + row, 2 + line, *prog); + if (row < 70) debug_draw_char(1 + row, 2 + l, *prog); row++; } prog++; @@ -293,7 +300,7 @@ void debug_init(unsigned char* mem) { 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); + debug_window_id = SDL_GetWindowID(debug_window); SDL_SetRenderDrawColor(debug_renderer, 128, 128, 128, 255); FILE* f = fopen("font.png", "rb"); @@ -321,13 +328,6 @@ void debug_update() { debug_set_paper(128, 128, 128); debug_clear(); -/* debug_set_ink(64, 64, 64); - debug_fill_rect(39, 39, 40, 20); - debug_draw_inset(39, 39, 40, 20); - - debug_set_ink(0, 0, 0); - debug_print(39, 38, "DISASSEMBLY:");*/ - debug_print_memory(0); debug_print_asm(0); debug_print_datastack(0); @@ -337,3 +337,28 @@ void debug_update() { SDL_RenderPresent(debug_renderer); } + +void debug_hide() { + debug_set_paper(128, 128, 128); + debug_clear(); + SDL_RenderPresent(debug_renderer); +} + +void debug_mouse_event(SDL_Event& event) { + if (event.window.windowID != debug_window_id) return; + if (event.type == SDL_MOUSEBUTTONDOWN) { + int x, y; + Uint32 buttons = SDL_GetMouseState(&x, &y); + if (x >= 8 && x <= 568 && y >= 16 && y <= 296) { + int l = (y - 16) >> 3; + int bm = 0; while (lines[bm] < l) { bm++; } + breakpoints[bm] = !breakpoints[bm]; + } + debug_update(); + } + +} + +const bool* debug_get_breakpoints() { + return breakpoints; +} diff --git a/debug.h b/debug.h index 48b72e7..2152e57 100644 --- a/debug.h +++ b/debug.h @@ -1,4 +1,8 @@ #pragma once +#include void debug_init(unsigned char* mem); void debug_update(); +void debug_hide(); +void debug_mouse_event(SDL_Event& event); +const bool* debug_get_breakpoints(); diff --git a/main.cpp b/main.cpp index 252f009..704e959 100644 --- a/main.cpp +++ b/main.cpp @@ -45,6 +45,7 @@ int main(int argc, char** argv) { vm_register_in_port(21, input_data_out); debug_init(vm_get_memory()); + const bool* breakpoints = debug_get_breakpoints(); bool running = false; @@ -56,19 +57,36 @@ int main(int argc, char** argv) { while (!should_quit) { just_pressed = SDL_SCANCODE_UNKNOWN; while (SDL_PollEvent(&sdlEvent)) { - 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) { + switch (sdlEvent.type) { + case SDL_WINDOWEVENT: + switch (sdlEvent.window.event) { + case SDL_WINDOWEVENT_CLOSE: + should_quit = true; break; + } + break; + case SDL_QUIT: + should_quit = true; break; + case SDL_KEYDOWN: //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(); } + if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_F5) { running = !running; if (!running) debug_update(); else debug_hide(); } + break; + case SDL_KEYUP: + if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_SPACE) { anykey = false; } + case SDL_MOUSEBUTTONDOWN: + debug_mouse_event(sdlEvent); + break; } } - if (running) vm_step(); + + if (running) { + unsigned short pc = vm_step(); + if (breakpoints[pc]) { running = false; debug_update(); } + } //if (SDL_GetTicks() - ticks >= 15) { vdp_flip(); ticks = SDL_GetTicks(); } } diff --git a/rom.bin b/rom.bin new file mode 100644 index 0000000000000000000000000000000000000000..61f49fe5827526b82f075eeba4b6a11332f594a5 GIT binary patch literal 200 zcmY+6!3x4a3`BP}+v>)&hi!WaiXbQ`SUh-i|EB-{4Q5*_N+6lMOa@M3lVTdK)IJqX zh=see*9MRk%c;(n?og>Q!gQx&@cW3zEd@*?f&U1Pc4;+pDO7p4%J7kJCQ2q-Ch#?w Z+uP!o&)*I*j(T=tvy5#)vFQFa=o_D?60QIM literal 0 HcmV?d00001 diff --git a/test.bas b/test.bas index bcd44c9..ecf36b4 100644 --- a/test.bas +++ b/test.bas @@ -1,24 +1,40 @@ - border INK_BLACK - color INK_WHITE + PAPER_BLACK - clrscr - locate 4 2 print chr &h87, chr &h85, chr &h87, chr &h85, chr &h87, chr &h85, chr &h87, chr &h81 - locate 4 3 print chr &h85, chr &h85, chr &h85, chr &h85, chr &h85, chr &h85, chr &h85, chr &h84 - 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 = 8 - dx = 1 - setsprite 0 &HF9 INK_RED -peiv: - 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: - clrscr - locate 0 0 print "Ok" - updatescr -ttt: - goto ttt \ No newline at end of file + BORDER INK_BLACK + COLOR INK_WHITE + PAPER_BLACK + CLRSCR + LOCATE 4 2 PRINT CHR &h87, CHR &h85, CHR &h87, CHR &h85, CHR &h87, CHR &h85, CHR &h87, CHR &h81 + LOCATE 4 3 PRINT CHR &h85, CHR &h85, CHR &h85, CHR &h85, CHR &h85, CHR &h85, CHR &h85, CHR &h84 + LOCATE 4 4 PRINT CHR &h87, CHR &h81, CHR &h8D, CHR &h85, CHR &h85, CHR &h85, CHR &h8D, CHR &h85 + W = 0 +MENU_BLINK: + LOCATE 3 8 PRINT "PRESS CTRL" + LOCATE 4 9 PRINT "TO PLAY" + UPDATESCR + INC W + IF KEYPRESSED BTN_A THEN GOTO MAIN_GAME END + IF W < 25 THEN + GOTO MENU_BLINK + END +MENU_BLINK2: + LOCATE 3 8 PRINT " " + LOCATE 4 9 PRINT " " + UPDATESCR + INC W + IF KEYPRESSED BTN_A THEN GOTO MAIN_GAME END + IF W < 50 THEN GOTO MENU_BLINK2 END + W = 0 + GOTO MENU_BLINK +MAIN_GAME: + CLRSCR + GOSUB PINTA_RED + LOCATE 6 1 PRINT "0" + LOCATE 9 1 PRINT "0" + UPDATESCR + GOTO MAIN_GAME +PINTA_RED: + PUTCHAR 7 0 &HD1 INK_WHITE + PAPER_BLACK + PUTCHAR 7 2 &HD1 INK_WHITE + PAPER_BLACK + PUTCHAR 7 4 &HD1 INK_WHITE + PAPER_BLACK + PUTCHAR 7 6 &HD1 INK_WHITE + PAPER_BLACK + PUTCHAR 7 8 &HD1 INK_WHITE + PAPER_BLACK + PUTCHAR 7 10 &HD1 INK_WHITE + PAPER_BLACK + RETURN diff --git a/vm.cpp b/vm.cpp index ee69015..a81391c 100644 --- a/vm.cpp +++ b/vm.cpp @@ -161,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 bool vm_step() { - if (sleeping) return false; +const unsigned short vm_step() { + if (sleeping) return vm_pc; - char a, b, c; + unsigned char a, b, c; OPS opcode = (OPS)vm_program[vm_pc++]; switch (opcode) { @@ -214,8 +214,8 @@ const bool vm_step() { 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_DIV: GRAB(ds); PUSH(ds, b / a); vm_cycles++; break; + case OP_MOD: GRAB(ds); PUSH(ds, b % a); 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; @@ -231,22 +231,24 @@ const bool vm_step() { 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_LT: + GRAB(ds); + PUSH(ds, b < a); vm_cycles++; break; + case OP_GT: GRAB(ds); PUSH(ds, b > a); vm_cycles++; break; + case OP_LEQ: GRAB(ds); PUSH(ds, b <= a); vm_cycles++; break; + case OP_GEQ: GRAB(ds); PUSH(ds, b >= a); 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 false; + return vm_pc; } -const int vm_big_step() { +const unsigned short vm_big_step() { word* lines = parser_get_lines(); - word current_line = lines[vm_pc]-1; - while ((vm_pc > 32768) || (lines[vm_pc]-1 == current_line)) { vm_step(); } - return false; + word current_line = lines[vm_pc]; + while ((vm_pc >= 32768) || (lines[vm_pc] == current_line)) { vm_step(); } + return vm_pc; } /*void vm_register_call(void(*callback)(t_stack&)) { external_calls[numcallbacks++] = callback; diff --git a/vm.h b/vm.h index 76be1d7..6cbae2f 100644 --- a/vm.h +++ b/vm.h @@ -2,8 +2,8 @@ //#include "stack.h" void vm_init(const char* filename); -const bool vm_step(); -const bool vm_big_step(); +const unsigned short vm_step(); +const unsigned short 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);