From 528649b2f207d0595210e92fbb312c78bdebca15 Mon Sep 17 00:00:00 2001 From: OBattler Date: Wed, 17 Dec 2025 19:33:29 +0100 Subject: [PATCH] Bring back RichardG's recompiler changes. --- src/86box.c | 3 +++ src/cpu/386_dynarec.c | 32 +++++++++++++++++++++++--------- src/include/86box/86box.h | 1 + src/include/86box/mem.h | 6 ++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/86box.c b/src/86box.c index fe5dcf5b7..ad79e391b 100644 --- a/src/86box.c +++ b/src/86box.c @@ -331,6 +331,7 @@ int scrnsz_y = SCREEN_RES_Y; /* current screen size, Y */ int config_changed; /* config has changed */ int title_update; int framecountx = 0; +int seconds_elapsed = 0; int hard_reset_pending = 0; #if 0 @@ -1974,6 +1975,8 @@ pc_onesec(void) framecount = 0; title_update = 1; + + seconds_elapsed++; } void diff --git a/src/cpu/386_dynarec.c b/src/cpu/386_dynarec.c index 852c25427..260346572 100644 --- a/src/cpu/386_dynarec.c +++ b/src/cpu/386_dynarec.c @@ -392,10 +392,9 @@ static inline void __attribute__((optimize("O2"))) #else static __inline void #endif -exec386_dynarec_dyn(void) +exec386_dynarec_dyn(uint32_t phys_addr, page_t *page) { uint32_t start_pc = 0; - uint32_t phys_addr = get_phys(cs + cpu_state.pc); int hash = HASH(phys_addr); # ifdef USE_NEW_DYNAREC codeblock_t *block = &codeblock[codeblock_hash[hash]]; @@ -410,8 +409,6 @@ exec386_dynarec_dyn(void) if (block && !cpu_state.abrt) # endif { - page_t *page = &pages[phys_addr >> 12]; - /* Block must match current CS, PC, code segment size, and physical address. The physical address check will also catch any page faults at this stage */ @@ -445,16 +442,30 @@ exec386_dynarec_dyn(void) if (valid_block && (block->page_mask & *block->dirty_mask)) { # ifdef USE_NEW_DYNAREC codegen_check_flush(page, page->dirty_mask, phys_addr); - if (block->pc == BLOCK_PC_INVALID) + if (block->pc == BLOCK_PC_INVALID) { valid_block = 0; - else if (block->flags & CODEBLOCK_IN_DIRTY_LIST) + goto invalid_block; + } else if (block->flags & CODEBLOCK_IN_DIRTY_LIST) { block->flags &= ~CODEBLOCK_WAS_RECOMPILED; +invalid_block: # else codegen_check_flush(page, page->dirty_mask[(phys_addr >> 10) & 3], phys_addr); page->dirty_mask[(phys_addr >> 10) & 3] = 0; - if (!block->valid) + if (!block->valid) { valid_block = 0; # endif + if (page->inv_timestamp != seconds_elapsed) { + page->inv_timestamp = seconds_elapsed; + page->inv_count = 1; + } else { + page->inv_count++; +# define INVALIDATION_LIMIT 100 /* amount of invalidations *per second* to kick a page to the interpreter */ +# ifdef ENABLE_386_DYNAREC_LOG + if (page->inv_count >= INVALIDATION_LIMIT) + x386_dynarec_log("Forcing interpreter on page %08X\n", phys_addr & 0xfffff000); +# endif + } + } } if (valid_block && block->page_mask2) { /* We don't want the second page to cause a page @@ -779,11 +790,14 @@ exec386_dynarec(int32_t cycs) cycles_old = cycles; oldtsc = tsc; tsc_old = tsc; - if (cpu_force_interpreter || cpu_override_dynarec || (!CACHE_ON())) /*Interpret block*/ + + uint32_t phys_addr = get_phys(cs + cpu_state.pc); + page_t *page = &pages[phys_addr >> 12]; + if (cpu_force_interpreter || cpu_override_dynarec || (page->inv_count >= INVALIDATION_LIMIT) || (!CACHE_ON())) /*Interpret block*/ { exec386_dynarec_int(); } else { - exec386_dynarec_dyn(); + exec386_dynarec_dyn(phys_addr, page); } if (cpu_init) { diff --git a/src/include/86box/86box.h b/src/include/86box/86box.h index d9c105546..f8765aecb 100644 --- a/src/include/86box/86box.h +++ b/src/include/86box/86box.h @@ -321,6 +321,7 @@ extern void do_pause(int p); extern double isa_timing; extern int io_delay; extern int framecountx; +extern int seconds_elapsed; extern volatile int cpu_thread_run; extern uint8_t postcard_codes[POSTCARDS_NUM]; diff --git a/src/include/86box/mem.h b/src/include/86box/mem.h index 9051189a6..215dca438 100644 --- a/src/include/86box/mem.h +++ b/src/include/86box/mem.h @@ -224,6 +224,9 @@ typedef struct page_t { uint64_t *byte_dirty_mask; uint64_t *byte_code_present_mask; + + uint32_t inv_count; + uint32_t inv_timestamp; } page_t; extern uint32_t purgable_page_list_head; @@ -250,6 +253,9 @@ typedef struct _page_ { /*Head of codeblock tree associated with this page*/ struct codeblock_t *head; + + uint32_t inv_count; + uint32_t inv_timestamp; } page_t; #endif