From ed2c014c7ecbaca89edcb22181e1f1c66f516a82 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Thu, 4 Jun 2026 13:44:50 +0200 Subject: [PATCH] =?UTF-8?q?-=20[CHG]=20Gesti=C3=B3=20de=20interrupcions=20?= =?UTF-8?q?pasada=20a=20"mem"=20(futur=20"bus")=20-=20[CHG]=20Reorganitzac?= =?UTF-8?q?ions=20i=20neteja=20de=20codi=20-=20[NEW]=20Constants=20del=20e?= =?UTF-8?q?spai=20io=20en=20la=20seua=20pr=C3=B2pia=20cap=C3=A7alera=20-?= =?UTF-8?q?=20[NEW]=20Comentaris=20en=20algunes=20parts=20menys=20obvies?= =?UTF-8?q?=20del=20codi=20-=20[CHG]=20Alguns=20defines=20passats=20a=20co?= =?UTF-8?q?nstexpr=20-=20[CHG]=20display::init()=20no=20feia=20m=C3=A9s=20?= =?UTF-8?q?que=20cridar=20a=20display::reinit().=20Concretats=20els=20dos?= =?UTF-8?q?=20en=20display::init()=20-=20[ONGOING]=20Passant=20la=20respon?= =?UTF-8?q?sabilitat=20de=20tornar=20el=20foco=20a=20la=20finestra=20princ?= =?UTF-8?q?ipal=20(que=20estaba=20en=20el=20m=C3=B2dul=20debug)=20al=20ges?= =?UTF-8?q?tor=20de=20finestres.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/debug.cpp | 3 +-- source/display.cpp | 11 +++-------- source/display.h | 1 - source/interrupts.h | 17 ++++++++++++----- source/io_constants.h | 3 +++ source/main.cpp | 23 ++++++++++++++--------- source/mem.cpp | 9 +++++++-- source/mem.h | 3 ++- source/ppu.cpp | 16 ++++++---------- source/sm83.cpp | 36 +++++++++++++----------------------- source/sm83.h | 1 - 11 files changed, 61 insertions(+), 62 deletions(-) create mode 100644 source/io_constants.h diff --git a/source/debug.cpp b/source/debug.cpp index 211ba7a..d6f1589 100644 --- a/source/debug.cpp +++ b/source/debug.cpp @@ -108,10 +108,9 @@ namespace debug con_y = win_h - con_h; sym_h = win_h - sym_y; debug::refresh(); - display::redraw(); } else if (e->window.event == SDL_WINDOWEVENT_CLOSE) { hide(); - display::focus(); + //display::focus(); } } if (e->type == SDL_MOUSEWHEEL) { diff --git a/source/display.cpp b/source/display.cpp index 9dfc66f..6797ad5 100644 --- a/source/display.cpp +++ b/source/display.cpp @@ -69,7 +69,7 @@ namespace display return true; } - void reinit() + void init() { if (win) ui::window::unregisterWindow(SDL_GetWindowID(win)); @@ -106,11 +106,6 @@ namespace display focus(); } - void init() - { - reinit(); - } - void focus() { if (win) @@ -188,7 +183,7 @@ namespace display if (144*value > dm.h) return; zoom = value; - reinit(); + init(); } void incZoom() @@ -206,7 +201,7 @@ namespace display void toggleFullscreen() { fullscreen = !fullscreen; - reinit(); + init(); } const bool getFullscreen() diff --git a/source/display.h b/source/display.h index 98a698d..566ae08 100644 --- a/source/display.h +++ b/source/display.h @@ -3,7 +3,6 @@ namespace display { void init(); - void reinit(); void focus(); void redraw(const bool present=true); void present(); diff --git a/source/interrupts.h b/source/interrupts.h index 05aeba1..c7848af 100644 --- a/source/interrupts.h +++ b/source/interrupts.h @@ -1,7 +1,14 @@ #pragma once +#include -#define INTERRUPT_VBLANK 0x01 -#define INTERRUPT_LCD 0x02 -#define INTERRUPT_TIMER 0x04 -#define INTERRUPT_SERIAL 0x08 -#define INTERRUPT_JOYPAD 0x10 +namespace gb +{ + namespace interrupts + { + constexpr uint8_t VBLANK = 0x01; + constexpr uint8_t LCD = 0x02; + constexpr uint8_t TIMER = 0x04; + constexpr uint8_t SERIAL = 0x08; + constexpr uint8_t JOYPAD = 0x10; + } +} diff --git a/source/io_constants.h b/source/io_constants.h new file mode 100644 index 0000000..f8b937a --- /dev/null +++ b/source/io_constants.h @@ -0,0 +1,3 @@ +#pragma once + +#define io_interrupts_address 0xff0f diff --git a/source/main.cpp b/source/main.cpp index 4e93ce7..91f9a71 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -52,13 +52,17 @@ namespace actions int main(int argc, char *argv[]) { + // Control de paràmetres if (argc < 2) { printf("ABORTING: No rom specified.\n"); exit(1); } + // Velocitat de rellotge const uint32_t clock = 4194304; const uint32_t update_freq = clock >> 3; + // Inicia SDL SDL_Init(SDL_INIT_EVERYTHING); + // Carrega la rom FILE *f = fopen(argv[1], "rb"); if (!f) { printf("ABORTING: Rom not found.\n"); exit(1); } fseek(f, 0, SEEK_END); @@ -68,8 +72,10 @@ int main(int argc, char *argv[]) fread(buffer, filesize, 1, f); fclose(f); + // i mem inicialitza el mbc mem::init(buffer, filesize); + // Iniciem tots els sistemes sm83dis::loadSymbols(); sm83::reset(); @@ -77,6 +83,7 @@ int main(int argc, char *argv[]) display::init(); debug::init(); + // Iniciem el menú princpal (ací? perqué? No es de display.h?) ui::menu::init(); ui::menu::setexitcallback(actions::exitMenu); @@ -96,10 +103,6 @@ int main(int argc, char *argv[]) ui::menu::addbooloption(menu, "STOP ON INVALID OP", sm83::getOption(SM83_OPTION_STOP_ON_INVALID), actions::decZoom); ui::menu::addoption(menu, "SHOW ANALYZER", actions::showAnalyzer); - //zx_ula::sound_init(); - - //debug::stop(); - bool should_exit = false; SDL_Event e; @@ -174,9 +177,7 @@ int main(int argc, char *argv[]) } if (!debug::debugging() && !debug::paused()) { - bool fastload=false; - - // En cada bucle fem 10 pasos de la CPU, sino s'ofega + // En cada pas de bucle fem 10 pasos de la CPU, sino s'ofega for (int i=0;i<20;++i) { if (debug::isbreak(sm83::getPC(), 9)) { debug::stop(); @@ -191,14 +192,18 @@ int main(int argc, char *argv[]) } } + // Sincronització: + // la frequència del rellotge diu quants t_states pot executar per segon. + // update_freq es la freq del rellotge / 8. Per tant, el nombre de t_states que + // pot executar cada 125 ms. Ací comprobem si ja havem executat eixe nombre + // de t_states i, si ho havem fet, esperem fins que passen 125ms des de l'ultima + // vegada que ho comprobarem. if (t_states>=update_freq) { while (SDL_GetTicks()<(time+125)) {} t_states -= update_freq; time = SDL_GetTicks(); - //z80analyze::refresh(); } - //z80analyze::refresh(true); } else if (!debug::debugging() && debug::paused()) { diff --git a/source/mem.cpp b/source/mem.cpp index d488bfb..5712b5f 100644 --- a/source/mem.cpp +++ b/source/mem.cpp @@ -4,7 +4,7 @@ #include #include #include "apu.h" - +#include "io_constants.h" #include "mbc_none.h" #include "mbc1.h" #include "mbc3.h" @@ -183,6 +183,11 @@ namespace mem } } + void requestInterrupt(uint8_t type) + { + mem::writeMem(io_interrupts_address, mem::readMem(io_interrupts_address) | type); + } + uint8_t getTag(uint16_t address) { return tags[address]; @@ -248,7 +253,7 @@ namespace mem TIMA++; else { TIMA = TMA; - sm83::interrupt(INTERRUPT_TIMER); + mem::requestInterrupt(gb::interrupts::TIMER); } } diff --git a/source/mem.h b/source/mem.h index 3bfc20a..93edc08 100644 --- a/source/mem.h +++ b/source/mem.h @@ -29,7 +29,8 @@ namespace mem uint8_t readMem(uint16_t address); void writeMem(uint16_t address, uint8_t value); - + void requestInterrupt(uint8_t type); + uint8_t getTag(uint16_t address); void setTag(uint16_t address, uint8_t value); diff --git a/source/ppu.cpp b/source/ppu.cpp index 62a918e..fe906f8 100644 --- a/source/ppu.cpp +++ b/source/ppu.cpp @@ -45,7 +45,6 @@ namespace ppu #define WY (*_WY) #define WX (*_WX) - #define IF 0xff0f bool last_interrupt_lcd_state = false; void init() @@ -212,7 +211,6 @@ namespace ppu } // gestió de en quin dot i linea estem, i tot el que ha de passar - //uint8_t interrupts = 0x00; bool current_interrupt_lcd_state = false; dots_in_scanline++; if ( (dots_in_scanline==80) && (LY<144) ) @@ -222,7 +220,7 @@ namespace ppu else if ( (dots_in_scanline==252) && (LY<144) ) { STAT = (STAT & 0xFC); // Set mode 0 - if (STAT&0x08) current_interrupt_lcd_state = true; // interrupts |= INTERRUPT_LCD; + if (STAT&0x08) current_interrupt_lcd_state = true; } else if (dots_in_scanline==456) { @@ -232,17 +230,16 @@ namespace ppu if (LY==144) { // Set vblank interrupt - mem::writeMem(IF, mem::readMem(IF) | INTERRUPT_VBLANK); + mem::requestInterrupt(gb::interrupts::VBLANK); STAT = (STAT & 0xFC) | 0x01; // Set mode 1 - //interrupts |= INTERRUPT_VBLANK; - if (STAT&0x10) current_interrupt_lcd_state = true; //interrupts |= INTERRUPT_LCD; + if (STAT&0x10) current_interrupt_lcd_state = true; } else { if (LY<144) { STAT = (STAT & 0xFC) | 0x02; // Set mode 2 - if (STAT&0x20) current_interrupt_lcd_state = true; // interrupts |= INTERRUPT_LCD; + if (STAT&0x20) current_interrupt_lcd_state = true; fill_line_buffer_bkg(); fill_line_buffer_win(); fill_line_buffer_obj(); @@ -251,7 +248,7 @@ namespace ppu if (LY==LYC) { STAT = (STAT & 0xFB) | 0x04; - if (STAT&0x40) current_interrupt_lcd_state = true; // interrupts |= INTERRUPT_LCD; + if (STAT&0x40) current_interrupt_lcd_state = true; } else { @@ -261,8 +258,7 @@ namespace ppu if (!last_interrupt_lcd_state && current_interrupt_lcd_state) { - mem::writeMem(IF, mem::readMem(IF) | INTERRUPT_LCD); - //sm83::interrupt(interrupts); + mem::requestInterrupt(gb::interrupts::LCD); } last_interrupt_lcd_state = current_interrupt_lcd_state; diff --git a/source/sm83.cpp b/source/sm83.cpp index ed6e4fb..5f69e72 100644 --- a/source/sm83.cpp +++ b/source/sm83.cpp @@ -46,10 +46,6 @@ namespace sm83 uint8_t *_rIME = ®s[12]; - bool halted = false; - bool exit_from_halt = false; - int pending_ei = 0; - #define rA (*_rA) #define rF (*_rF) #define rB (*_rB) @@ -71,6 +67,9 @@ namespace sm83 #define EX(a,b) {auto temp=a;a=b;b=temp;} + bool halted = false; + bool exit_from_halt = false; + int pending_ei = 0; bool reading_m1 = false; uint8_t READ_MEM_8(const uint16_t addr, const bool code=false) @@ -534,21 +533,21 @@ namespace sm83 DI(); PUSH(rPC); //t+=20; - if (*IF & INTERRUPT_VBLANK) { + if (*IF & gb::interrupts::VBLANK) { rPC = 0x40; - *IF = *IF & ~INTERRUPT_VBLANK; - } else if (*IF & INTERRUPT_LCD) { + *IF = *IF & ~gb::interrupts::VBLANK; + } else if (*IF & gb::interrupts::LCD) { rPC = 0x48; - *IF = *IF & ~INTERRUPT_LCD; - } else if (*IF & INTERRUPT_TIMER) { + *IF = *IF & ~gb::interrupts::LCD; + } else if (*IF & gb::interrupts::TIMER) { rPC = 0x50; - *IF = *IF & ~INTERRUPT_TIMER; - } else if (*IF & INTERRUPT_SERIAL) { + *IF = *IF & ~gb::interrupts::TIMER; + } else if (*IF & gb::interrupts::SERIAL) { rPC = 0x58; - *IF = *IF & ~INTERRUPT_SERIAL; - } else if (*IF & INTERRUPT_JOYPAD) { + *IF = *IF & ~gb::interrupts::SERIAL; + } else if (*IF & gb::interrupts::JOYPAD) { rPC = 0x60; - *IF = *IF & ~INTERRUPT_JOYPAD; + *IF = *IF & ~gb::interrupts::JOYPAD; } /*if (options[Z80_OPTION_BREAK_ON_INTERRUPT]) { printf("Break on interrupt! 0x%2x, PC: 0x%2x\n", address, rPC); @@ -616,15 +615,6 @@ namespace sm83 HALT(); } - void interrupt(uint8_t type) - { - const uint8_t IE = mem::readMem(0xffff); - //if (IE & type) exit_from_halt = true; - const uint8_t IF = mem::readMem(0xff0f); - mem::writeMem(0xff0f, IF | type); - //processInterrupts(); - } - static inline const uint8_t RLC(const uint8_t v) { const uint8_t res = (v>>7) | (v<<1); diff --git a/source/sm83.h b/source/sm83.h index 4cd0eca..53603ae 100644 --- a/source/sm83.h +++ b/source/sm83.h @@ -16,7 +16,6 @@ namespace sm83 uint32_t getClock(); void processInterrupts(); - void interrupt(uint8_t type); uint32_t step();