- [CHG] Gestió de interrupcions pasada a "mem" (futur "bus")

- [CHG] Reorganitzacions i neteja de codi
- [NEW] Constants del espai io en la seua pròpia capçalera
- [NEW] Comentaris en algunes parts menys obvies del codi
- [CHG] Alguns defines passats a constexpr
- [CHG] display::init() no feia més que cridar a display::reinit(). Concretats els dos en display::init()
- [ONGOING] Passant la responsabilitat de tornar el foco a la finestra principal (que estaba en el mòdul debug) al gestor de finestres.
This commit is contained in:
2026-06-04 13:44:50 +02:00
parent 2fe0ce6152
commit ed2c014c7e
11 changed files with 61 additions and 62 deletions
+1 -2
View File
@@ -108,10 +108,9 @@ namespace debug
con_y = win_h - con_h; con_y = win_h - con_h;
sym_h = win_h - sym_y; sym_h = win_h - sym_y;
debug::refresh(); debug::refresh();
display::redraw();
} else if (e->window.event == SDL_WINDOWEVENT_CLOSE) { } else if (e->window.event == SDL_WINDOWEVENT_CLOSE) {
hide(); hide();
display::focus(); //display::focus();
} }
} }
if (e->type == SDL_MOUSEWHEEL) { if (e->type == SDL_MOUSEWHEEL) {
+3 -8
View File
@@ -69,7 +69,7 @@ namespace display
return true; return true;
} }
void reinit() void init()
{ {
if (win) ui::window::unregisterWindow(SDL_GetWindowID(win)); if (win) ui::window::unregisterWindow(SDL_GetWindowID(win));
@@ -106,11 +106,6 @@ namespace display
focus(); focus();
} }
void init()
{
reinit();
}
void focus() void focus()
{ {
if (win) if (win)
@@ -188,7 +183,7 @@ namespace display
if (144*value > dm.h) return; if (144*value > dm.h) return;
zoom = value; zoom = value;
reinit(); init();
} }
void incZoom() void incZoom()
@@ -206,7 +201,7 @@ namespace display
void toggleFullscreen() void toggleFullscreen()
{ {
fullscreen = !fullscreen; fullscreen = !fullscreen;
reinit(); init();
} }
const bool getFullscreen() const bool getFullscreen()
-1
View File
@@ -3,7 +3,6 @@
namespace display namespace display
{ {
void init(); void init();
void reinit();
void focus(); void focus();
void redraw(const bool present=true); void redraw(const bool present=true);
void present(); void present();
+12 -5
View File
@@ -1,7 +1,14 @@
#pragma once #pragma once
#include <stdint.h>
#define INTERRUPT_VBLANK 0x01 namespace gb
#define INTERRUPT_LCD 0x02 {
#define INTERRUPT_TIMER 0x04 namespace interrupts
#define INTERRUPT_SERIAL 0x08 {
#define INTERRUPT_JOYPAD 0x10 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;
}
}
+3
View File
@@ -0,0 +1,3 @@
#pragma once
#define io_interrupts_address 0xff0f
+14 -9
View File
@@ -52,13 +52,17 @@ namespace actions
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// Control de paràmetres
if (argc < 2) { printf("ABORTING: No rom specified.\n"); exit(1); } if (argc < 2) { printf("ABORTING: No rom specified.\n"); exit(1); }
// Velocitat de rellotge
const uint32_t clock = 4194304; const uint32_t clock = 4194304;
const uint32_t update_freq = clock >> 3; const uint32_t update_freq = clock >> 3;
// Inicia SDL
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
// Carrega la rom
FILE *f = fopen(argv[1], "rb"); FILE *f = fopen(argv[1], "rb");
if (!f) { printf("ABORTING: Rom not found.\n"); exit(1); } if (!f) { printf("ABORTING: Rom not found.\n"); exit(1); }
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@@ -68,8 +72,10 @@ int main(int argc, char *argv[])
fread(buffer, filesize, 1, f); fread(buffer, filesize, 1, f);
fclose(f); fclose(f);
// i mem inicialitza el mbc
mem::init(buffer, filesize); mem::init(buffer, filesize);
// Iniciem tots els sistemes
sm83dis::loadSymbols(); sm83dis::loadSymbols();
sm83::reset(); sm83::reset();
@@ -77,6 +83,7 @@ int main(int argc, char *argv[])
display::init(); display::init();
debug::init(); debug::init();
// Iniciem el menú princpal (ací? perqué? No es de display.h?)
ui::menu::init(); ui::menu::init();
ui::menu::setexitcallback(actions::exitMenu); 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::addbooloption(menu, "STOP ON INVALID OP", sm83::getOption(SM83_OPTION_STOP_ON_INVALID), actions::decZoom);
ui::menu::addoption(menu, "SHOW ANALYZER", actions::showAnalyzer); ui::menu::addoption(menu, "SHOW ANALYZER", actions::showAnalyzer);
//zx_ula::sound_init();
//debug::stop();
bool should_exit = false; bool should_exit = false;
SDL_Event e; SDL_Event e;
@@ -174,9 +177,7 @@ int main(int argc, char *argv[])
} }
if (!debug::debugging() && !debug::paused()) { if (!debug::debugging() && !debug::paused()) {
bool fastload=false; // En cada pas de bucle fem 10 pasos de la CPU, sino s'ofega
// En cada bucle fem 10 pasos de la CPU, sino s'ofega
for (int i=0;i<20;++i) { for (int i=0;i<20;++i) {
if (debug::isbreak(sm83::getPC(), 9)) { if (debug::isbreak(sm83::getPC(), 9)) {
debug::stop(); 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) if (t_states>=update_freq)
{ {
while (SDL_GetTicks()<(time+125)) {} while (SDL_GetTicks()<(time+125)) {}
t_states -= update_freq; t_states -= update_freq;
time = SDL_GetTicks(); time = SDL_GetTicks();
//z80analyze::refresh();
} }
//z80analyze::refresh(true);
} else if (!debug::debugging() && debug::paused()) { } else if (!debug::debugging() && debug::paused()) {
+7 -2
View File
@@ -4,7 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "apu.h" #include "apu.h"
#include "io_constants.h"
#include "mbc_none.h" #include "mbc_none.h"
#include "mbc1.h" #include "mbc1.h"
#include "mbc3.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) uint8_t getTag(uint16_t address)
{ {
return tags[address]; return tags[address];
@@ -248,7 +253,7 @@ namespace mem
TIMA++; TIMA++;
else { else {
TIMA = TMA; TIMA = TMA;
sm83::interrupt(INTERRUPT_TIMER); mem::requestInterrupt(gb::interrupts::TIMER);
} }
} }
+2 -1
View File
@@ -29,7 +29,8 @@ namespace mem
uint8_t readMem(uint16_t address); uint8_t readMem(uint16_t address);
void writeMem(uint16_t address, uint8_t value); void writeMem(uint16_t address, uint8_t value);
void requestInterrupt(uint8_t type);
uint8_t getTag(uint16_t address); uint8_t getTag(uint16_t address);
void setTag(uint16_t address, uint8_t value); void setTag(uint16_t address, uint8_t value);
+6 -10
View File
@@ -45,7 +45,6 @@ namespace ppu
#define WY (*_WY) #define WY (*_WY)
#define WX (*_WX) #define WX (*_WX)
#define IF 0xff0f
bool last_interrupt_lcd_state = false; bool last_interrupt_lcd_state = false;
void init() void init()
@@ -212,7 +211,6 @@ namespace ppu
} }
// gestió de en quin dot i linea estem, i tot el que ha de passar // 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; bool current_interrupt_lcd_state = false;
dots_in_scanline++; dots_in_scanline++;
if ( (dots_in_scanline==80) && (LY<144) ) if ( (dots_in_scanline==80) && (LY<144) )
@@ -222,7 +220,7 @@ namespace ppu
else if ( (dots_in_scanline==252) && (LY<144) ) else if ( (dots_in_scanline==252) && (LY<144) )
{ {
STAT = (STAT & 0xFC); // Set mode 0 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) else if (dots_in_scanline==456)
{ {
@@ -232,17 +230,16 @@ namespace ppu
if (LY==144) if (LY==144)
{ {
// Set vblank interrupt // Set vblank interrupt
mem::writeMem(IF, mem::readMem(IF) | INTERRUPT_VBLANK); mem::requestInterrupt(gb::interrupts::VBLANK);
STAT = (STAT & 0xFC) | 0x01; // Set mode 1 STAT = (STAT & 0xFC) | 0x01; // Set mode 1
//interrupts |= INTERRUPT_VBLANK; if (STAT&0x10) current_interrupt_lcd_state = true;
if (STAT&0x10) current_interrupt_lcd_state = true; //interrupts |= INTERRUPT_LCD;
} }
else else
{ {
if (LY<144) if (LY<144)
{ {
STAT = (STAT & 0xFC) | 0x02; // Set mode 2 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_bkg();
fill_line_buffer_win(); fill_line_buffer_win();
fill_line_buffer_obj(); fill_line_buffer_obj();
@@ -251,7 +248,7 @@ namespace ppu
if (LY==LYC) if (LY==LYC)
{ {
STAT = (STAT & 0xFB) | 0x04; STAT = (STAT & 0xFB) | 0x04;
if (STAT&0x40) current_interrupt_lcd_state = true; // interrupts |= INTERRUPT_LCD; if (STAT&0x40) current_interrupt_lcd_state = true;
} }
else else
{ {
@@ -261,8 +258,7 @@ namespace ppu
if (!last_interrupt_lcd_state && current_interrupt_lcd_state) if (!last_interrupt_lcd_state && current_interrupt_lcd_state)
{ {
mem::writeMem(IF, mem::readMem(IF) | INTERRUPT_LCD); mem::requestInterrupt(gb::interrupts::LCD);
//sm83::interrupt(interrupts);
} }
last_interrupt_lcd_state = current_interrupt_lcd_state; last_interrupt_lcd_state = current_interrupt_lcd_state;
+13 -23
View File
@@ -46,10 +46,6 @@ namespace sm83
uint8_t *_rIME = &regs[12]; uint8_t *_rIME = &regs[12];
bool halted = false;
bool exit_from_halt = false;
int pending_ei = 0;
#define rA (*_rA) #define rA (*_rA)
#define rF (*_rF) #define rF (*_rF)
#define rB (*_rB) #define rB (*_rB)
@@ -71,6 +67,9 @@ namespace sm83
#define EX(a,b) {auto temp=a;a=b;b=temp;} #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; bool reading_m1 = false;
uint8_t READ_MEM_8(const uint16_t addr, const bool code=false) uint8_t READ_MEM_8(const uint16_t addr, const bool code=false)
@@ -534,21 +533,21 @@ namespace sm83
DI(); DI();
PUSH(rPC); PUSH(rPC);
//t+=20; //t+=20;
if (*IF & INTERRUPT_VBLANK) { if (*IF & gb::interrupts::VBLANK) {
rPC = 0x40; rPC = 0x40;
*IF = *IF & ~INTERRUPT_VBLANK; *IF = *IF & ~gb::interrupts::VBLANK;
} else if (*IF & INTERRUPT_LCD) { } else if (*IF & gb::interrupts::LCD) {
rPC = 0x48; rPC = 0x48;
*IF = *IF & ~INTERRUPT_LCD; *IF = *IF & ~gb::interrupts::LCD;
} else if (*IF & INTERRUPT_TIMER) { } else if (*IF & gb::interrupts::TIMER) {
rPC = 0x50; rPC = 0x50;
*IF = *IF & ~INTERRUPT_TIMER; *IF = *IF & ~gb::interrupts::TIMER;
} else if (*IF & INTERRUPT_SERIAL) { } else if (*IF & gb::interrupts::SERIAL) {
rPC = 0x58; rPC = 0x58;
*IF = *IF & ~INTERRUPT_SERIAL; *IF = *IF & ~gb::interrupts::SERIAL;
} else if (*IF & INTERRUPT_JOYPAD) { } else if (*IF & gb::interrupts::JOYPAD) {
rPC = 0x60; rPC = 0x60;
*IF = *IF & ~INTERRUPT_JOYPAD; *IF = *IF & ~gb::interrupts::JOYPAD;
} }
/*if (options[Z80_OPTION_BREAK_ON_INTERRUPT]) { /*if (options[Z80_OPTION_BREAK_ON_INTERRUPT]) {
printf("Break on interrupt! 0x%2x, PC: 0x%2x\n", address, rPC); printf("Break on interrupt! 0x%2x, PC: 0x%2x\n", address, rPC);
@@ -616,15 +615,6 @@ namespace sm83
HALT(); 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) static inline const uint8_t RLC(const uint8_t v)
{ {
const uint8_t res = (v>>7) | (v<<1); const uint8_t res = (v>>7) | (v<<1);
-1
View File
@@ -16,7 +16,6 @@ namespace sm83
uint32_t getClock(); uint32_t getClock();
void processInterrupts(); void processInterrupts();
void interrupt(uint8_t type);
uint32_t step(); uint32_t step();