Merge remote-tracking branch 'origin/feature/recompiler_improvements' into ndr-memblocks-tail

This commit is contained in:
Cacodemon345
2025-12-18 23:04:30 +06:00
6 changed files with 118 additions and 16 deletions

View File

@@ -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

View File

@@ -7,6 +7,7 @@
# include <windows.h>
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -18,6 +19,59 @@
#include "codegen.h"
#include "codegen_allocator.h"
#include "codegen_backend.h"
struct mem_code_block_t;
typedef struct mem_code_block_t
{
struct mem_code_block_t* prev;
struct mem_code_block_t* next;
int number;
} mem_code_block_t;
static bool valid_code_blocks[BLOCK_SIZE];
static mem_code_block_t mem_code_blocks[BLOCK_SIZE];
static mem_code_block_t* mem_code_block_head = NULL;
static mem_code_block_t* mem_code_block_tail = NULL;
static void
remove_from_block_list(mem_code_block_t* block)
{
valid_code_blocks[block->number] = 0;
if (block->prev) {
block->prev->next = block->next;
if (block->next) {
block->next->prev = block->prev;
} else {
mem_code_block_tail = block->prev;
}
} else if (block->next) {
mem_code_block_head = block->next;
if (mem_code_block_head && mem_code_block_head->next) {
mem_code_block_head->next->prev = mem_code_block_head;
}
} else if (block == mem_code_block_head) {
mem_code_block_head = mem_code_block_tail = NULL;
}
block->next = block->prev = NULL;
}
static void
add_to_block_list(int code_block)
{
if (!mem_code_block_head) {
mem_code_block_head = &mem_code_blocks[code_block];
mem_code_block_head->number = code_block;
mem_code_block_tail = mem_code_block_head;
} else {
mem_code_block_tail->next = &mem_code_blocks[code_block];
mem_code_blocks[code_block].prev = mem_code_block_tail;
mem_code_block_tail = &mem_code_blocks[code_block];
mem_code_blocks[code_block].number = code_block;
}
}
typedef struct mem_block_t {
uint32_t offset; /*Offset into mem_block_alloc*/
@@ -55,15 +109,26 @@ codegen_allocator_allocate(mem_block_t *parent, int code_block)
mem_block_t *block;
uint32_t block_nr;
while (!mem_block_free_list) {
/*Pick a random memory block and free the owning code block*/
block_nr = rand() & MEM_BLOCK_MASK;
block = &mem_blocks[block_nr];
if (!mem_block_free_list) {
if (mem_code_block_head == mem_code_block_tail) {
fatal("Out of memory blocks!\n");
} else {
mem_code_block_t* mem_code_block = mem_code_block_head;
while (mem_code_block) {
if (code_block != mem_code_block->number) {
codegen_delete_block(&codeblock[mem_code_block->number]);
}
mem_code_block = mem_code_block->next;
}
if (block->code_block && block->code_block != code_block)
codegen_delete_block(&codeblock[block->code_block]);
if (mem_block_free_list)
goto block_allocate;
fatal("Out of memory blocks!\n");
}
}
block_allocate:
/*Remove from free list*/
block_nr = mem_block_free_list;
block = &mem_blocks[block_nr - 1];
@@ -79,9 +144,15 @@ codegen_allocator_allocate(mem_block_t *parent, int code_block)
else
parent->next = parent->tail = block_nr;
block->next = block->tail = 0;
} else
} else {
block->next = block->tail = 0;
if (!valid_code_blocks[code_block]) {
valid_code_blocks[code_block] = 1;
add_to_block_list(code_block);
}
}
codegen_allocator_usage++;
return block;
}
@@ -91,6 +162,9 @@ codegen_allocator_free(mem_block_t *block)
int block_nr = (((uintptr_t) block - (uintptr_t) mem_blocks) / sizeof(mem_block_t)) + 1;
block->tail = 0;
if (valid_code_blocks[block->code_block])
remove_from_block_list(&mem_code_blocks[block->code_block]);
while (1) {
int next_block_nr = block->next;
codegen_allocator_usage--;

View File

@@ -559,6 +559,10 @@ codegen_block_start_recompile(codeblock_t *block)
fatal("Recompile to used block!\n");
#endif
if (block->head_mem_block) {
codegen_allocator_free(block->head_mem_block);
block->head_mem_block = NULL;
}
block->head_mem_block = codegen_allocator_allocate(NULL, block_current);
block->data = codeblock_allocator_get_ptr(block->head_mem_block);

View File

@@ -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) {

View File

@@ -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];

View File

@@ -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