From 25d1e99571271d8277069e58c106e4d959b9617f Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Wed, 23 Mar 2022 23:40:51 +0000 Subject: [PATCH] peanut-gb: detect when ROM has halted forever Signed-off-by: Mahyar Koshkouei --- inc/peanut_gb.h | 8 ++++++++ src/main.c | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/inc/peanut_gb.h b/inc/peanut_gb.h index d6f5b85..de43b18 100644 --- a/inc/peanut_gb.h +++ b/inc/peanut_gb.h @@ -490,6 +490,8 @@ struct gb_s uint8_t joypad; }; + unsigned ended : 1; + /* Implementation defined data. Set to NULL if not required. */ void *priv; } direct; @@ -2200,6 +2202,11 @@ void __gb_step_cpu(struct gb_s *gb) case 0x76: /* HALT */ /* TODO: Emulate HALT bug? */ gb->gb_halt = 1; + if(gb->gb_reg.IE == 0) + { + gb->direct.ended = 1; + return; + } break; case 0x77: /* LD (HL), A */ @@ -3636,6 +3643,7 @@ void gb_reset(struct gb_s *gb) gb->gb_reg.IE = 0x00; gb->direct.joypad = 0xFF; + gb->direct.ended = 0; gb->gb_reg.P1 = 0xCF; memset(gb->vram, 0x00, VRAM_SIZE); diff --git a/src/main.c b/src/main.c index 86c8fe4..a076360 100644 --- a/src/main.c +++ b/src/main.c @@ -128,11 +128,24 @@ int main(void) gb_init_serial(&gb, gb_serial_tx, gb_serial_rx); - while(1) - gb_run_frame(&gb); + uint_fast32_t instrs = 0; + uint64_t start_time = time_us_64(); + uint64_t end_time; + while(!gb.direct.ended) + { + __gb_step_cpu(&gb); + instrs++; + } + + end_time = time_us_64(); + + puts("\nEmulation Ended"); + printf("Instructions: %u\n" + "Time: %llu us\n", instrs, end_time-start_time); /* Sleep forever. */ sleep: + stdio_flush(); while(1) __wfi();