Monster patch to clean up a lot of cruft in the code, and reduce the RAM footptiny of a running XT machine from about 680MB down to about 80MB. Yes, 600MB of unused 0x00's.

This commit is contained in:
waltje
2018-03-17 23:13:46 -05:00
parent a86d717ae1
commit 1c60e22813
217 changed files with 9300 additions and 9128 deletions

181
src/mem.c
View File

@@ -8,7 +8,7 @@
*
* Memory handling and MMU.
*
* Version: @(#)mem.c 1.0.5 2018/03/14
* Version: @(#)mem.c 1.0.6 2018/03/16
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Miran Grca, <mgrca8@gmail.com>
@@ -54,10 +54,10 @@
#ifdef USE_DYNAREC
# include "cpu/codegen.h"
#else
#define PAGE_MASK_INDEX_MASK 3
#define PAGE_MASK_INDEX_SHIFT 10
#define PAGE_MASK_MASK 63
#define PAGE_MASK_SHIFT 4
# define PAGE_MASK_INDEX_MASK 3
# define PAGE_MASK_INDEX_SHIFT 10
# define PAGE_MASK_MASK 63
# define PAGE_MASK_SHIFT 4
#endif
@@ -68,17 +68,18 @@ mem_mapping_t bios_mapping[8];
mem_mapping_t bios_high_mapping[8];
mem_mapping_t romext_mapping;
page_t *pages,
**page_lookup;
uint32_t page_lookup_sz;
page_t *pages, /* RAM page table */
**page_lookup; /* pagetable lookup */
uint32_t pages_sz; /* #pages in table */
uint8_t isram[0x10000];
uint8_t *ram;
uint8_t *ram; /* the virtual RAM */
uint32_t rammask;
uint8_t *rom;
uint8_t *rom; /* the virtual ROM */
uint8_t romext[32768];
uint32_t biosmask;
uint32_t pccache;
uint8_t *pccache2;
@@ -96,10 +97,7 @@ uint32_t mem_logical_addr;
int shadowbios = 0,
shadowbios_write;
int mem_size;
uint32_t biosmask;
int readlnum = 0,
int readlnum = 0,
writelnum = 0;
int pctrans = 0;
int cachesize = 256;
@@ -137,7 +135,11 @@ static mem_mapping_t base_mapping;
static mem_mapping_t ram_remapped_mapping;
static mem_mapping_t ram_split_mapping;
#if 0
static uint8_t ff_array[0x1000];
#else
static uint8_t ff_pccache[4] = { 0xff, 0xff, 0xff, 0xff };
#endif
static int port_92_reg = 0;
@@ -150,16 +152,16 @@ resetreadlookup(void)
/* This is NULL after app startup, when mem_init() has not yet run. */
if (page_lookup == NULL) return;
memset(page_lookup, 0x00, page_lookup_sz);
memset(page_lookup, 0x00, pages_sz * sizeof(page_t *));
for (c = 0; c < 256; c++)
readlookup[c] = 0xffffffff;
memset(readlookup2, 0xff, 1024*1024*sizeof(uintptr_t));
memset(readlookup2, 0xff, pages_sz*sizeof(uintptr_t));
readlnext = 0;
for (c = 0; c < 256; c++)
writelookup[c] = 0xffffffff;
memset(writelookup2, 0xff, 1024*1024*sizeof(uintptr_t));
memset(writelookup2, 0xff, pages_sz*sizeof(uintptr_t));
writelnext = 0;
pccache = 0xffffffff;
@@ -388,7 +390,7 @@ addreadlookup(uint32_t virt, uint32_t phys)
readlookup2[readlookup[readlnext]] = -1;
readlookup2[virt>>12] = (uintptr_t)&ram[(uintptr_t)(phys & ~0xFFF) - (uintptr_t)(virt & ~0xfff)];
readlookupp[readlnext] = mmu_perm;
readlookup[readlnext++] = virt >> 12;
readlnext &= (cachesize-1);
@@ -417,7 +419,7 @@ addwritelookup(uint32_t virt, uint32_t phys)
page_lookup[virt >> 12] = &pages[phys >> 12];
else
writelookup2[virt>>12] = (uintptr_t)&ram[(uintptr_t)(phys & ~0xFFF) - (uintptr_t)(virt & ~0xfff)];
writelookupp[writelnext] = mmu_perm;
writelookup[writelnext++] = virt >> 12;
writelnext &= (cachesize - 1);
@@ -460,7 +462,11 @@ getpccache(uint32_t a)
pclog("Bad getpccache %08X\n", a);
#if 0
return &ff_array[0-(uintptr_t)(a2 & ~0xfff)];
#else
return (uint8_t *)&ff_pccache;
#endif
}
@@ -1449,7 +1455,7 @@ mem_add_bios(void)
mem_read_bios,mem_read_biosw,mem_read_biosl,
mem_write_null,mem_write_nullw,mem_write_nulll,
rom + (0xc000 & biosmask),
MEM_MAPPING_EXTERNAL|MEM_MAPPING_ROM, 0);
MEM_MAPPING_EXTERNAL|MEM_MAPPING_ROM, 0);
}
mem_mapping_add(&bios_mapping[4], 0xf0000, 0x04000,
@@ -1471,7 +1477,7 @@ mem_add_bios(void)
mem_read_bios,mem_read_biosw,mem_read_biosl,
mem_write_null,mem_write_nullw,mem_write_nulll,
rom + (0x1c000 & biosmask),
MEM_MAPPING_EXTERNAL|MEM_MAPPING_ROM, 0);
MEM_MAPPING_EXTERNAL|MEM_MAPPING_ROM, 0);
mem_mapping_add(&bios_high_mapping[0],
(AT && cpu_16bitbus) ? 0xfe0000 : 0xfffe0000, 0x04000,
@@ -1534,14 +1540,20 @@ mem_a20_init(void)
/* Reset the memory state. */
void
mem_reset(void)
{
{
uint32_t c, m;
split_mapping_enabled = 0;
/* Free existing memory and tables. */
if (ram != NULL) free(ram);
if (rom != NULL) free(rom);
if (pages != NULL) free(pages);
if (page_lookup != NULL) free(page_lookup);
if (readlookup2 != NULL) free(readlookup2);
if (writelookup2 != NULL) free(writelookup2);
/* Reset the ROM size mask. */
biosmask = 0xffff;
/*
@@ -1549,7 +1561,7 @@ mem_reset(void)
* is smaller, we'll need this for stupid things like the PS/2
* split mapping.
*/
if (mem_size < 16384)
if (mem_size < 16384)
m = 1024UL * 16384;
else
m = 1024UL * (mem_size + 384); /* 386 extra kB for top remapping */
@@ -1560,24 +1572,40 @@ mem_reset(void)
* Allocate the page table based on how much RAM we have.
* We re-allocate the table on each (hard) reset, as the
* memory amount could have changed.
*/
if (m <= (1 << 24)) /* 80286 max address space (16M) */
p = (m / 4096); /* allocated mem size / pagesize */
else
p = (1 << 20); /* entire 4GB address space */
pclog("MEM: mem_size=%i, m=%i, p=%i\n", mem_size, m, p);
pages = (page_t *)malloc(p * sizeof(page_t));
memset(pages, 0x00, p * sizeof(page_t));
*/
if (AT) {
if (cpu_16bitbus) {
/* 80186/286; maximum address space is 16MB. */
m = 4096;
} else {
/* 80386+; maximum address space is 4GB. */
m = (mem_size + 384) >> 2;
if ((m << 2) < (mem_size + 384))
m++;
if (m < 4096)
m = 4096;
}
} else {
/* 8088/86; maximum address space is 1MB. */
m = 256;
}
pages_sz = m;
pages = (page_t *)malloc(m * sizeof(page_t));
memset(pages, 0x00, m * sizeof(page_t));
for (c=0; c<m; c++) {
pages[c].mem = &ram[c << 12];
pages[c].write_b = mem_write_ramb_page;
pages[c].write_w = mem_write_ramw_page;
pages[c].write_l = mem_write_raml_page;
}
pclog("MEM: pages done (c=%i)\n", c);
page_lookup = malloc(p * sizeof(page_t *));
page_lookup_sz = (p * sizeof(page_t *));
pages[c].write_l = mem_write_raml_page;
}
page_lookup = (page_t **)malloc(pages_sz * sizeof(page_t *));
memset(page_lookup, 0x00, pages_sz * sizeof(page_t *));
/* Allocate and initialize the lookup tables. */
readlookup2 = malloc(pages_sz * sizeof(uintptr_t));
memset(readlookup2, 0xff, pages_sz * sizeof(page_t *));
writelookup2 = malloc(pages_sz * sizeof(uintptr_t));
memset(writelookup2, 0xff, pages_sz * sizeof(page_t *));
@@ -1586,7 +1614,6 @@ pclog("MEM: page_lookup done\n");
isram[c] = 1;
if ((c >= 0xa && c <= 0xf) ||
(cpu_16bitbus && c >= 0xfe && c <= 0xff))
isram[c] = 0;
isram[c] = 0;
}
@@ -1655,7 +1682,6 @@ pclog("MEM: isram done\n");
mem_write_ram,mem_write_ramw,mem_write_raml,
ram + (1 << 20), MEM_MAPPING_INTERNAL, NULL);
mem_mapping_disable(&ram_split_mapping);
mem_a20_init();
}
@@ -1663,24 +1689,20 @@ void
void
mem_init(void)
{
{
/* Perform a one-time init. */
ram = rom = NULL;
pages = NULL;
page_lookup = NULL;
readlookup2 = NULL;
page_lookup = NULL;
/* FIXME: move to reset? */
rom = NULL;
biosmask = 0xffff;
readlookup2 = malloc(1024 * 1024 * sizeof(uintptr_t));
writelookup2 = NULL;
memset(ram_mapped_addr, 0x00, 64 * sizeof(uint32_t));
#if 0
memset(ff_array, 0xff, sizeof(ff_array));
#endif
/* Reset the memory state. */
/* Reset the memory state. */
mem_reset();
}
@@ -1723,67 +1745,6 @@ mem_remap_top_384k(void)
{
mem_remap_top(384);
}
void
mem_split_enable(int max_size, uint32_t addr)
{
uint8_t *mem_split_buffer = &ram[0x80000];
int c;
if (split_mapping_enabled) return;
#if 0
pclog("Split mapping enable at %08X\n", addr);
#endif
mem_set_mem_state(addr, max_size * 1024,
MEM_READ_INTERNAL | MEM_WRITE_INTERNAL);
mem_mapping_set_addr(&ram_split_mapping, addr, max_size * 1024);
mem_mapping_set_exec(&ram_split_mapping, &ram[addr]);
if (max_size == 384)
memcpy(&ram[addr], mem_split_buffer, max_size);
else
memcpy(&ram[addr], &mem_split_buffer[128 * 1024], max_size);
for (c = ((addr/1024)/64); c < (((addr/1024)+max_size-1)/64); c++)
isram[c] = 1;
flushmmucache();
split_mapping_enabled = 1;
}
void
mem_split_disable(int max_size, uint32_t addr)
{
uint8_t *mem_split_buffer = &ram[0x80000];
int c;
if (! split_mapping_enabled) return;
#if 0
pclog("Split mapping disable at %08X\n", addr);
#endif
if (max_size == 384)
memcpy(mem_split_buffer, &ram[addr], max_size);
else
memcpy(&mem_split_buffer[128 * 1024], &ram[addr], max_size);
mem_mapping_disable(&ram_split_mapping);
mem_set_mem_state(addr, max_size * 1024, 0);
mem_mapping_set_exec(&ram_split_mapping, NULL);
for (c = ((addr/1024)/64); c < (((addr/1024)+max_size-1)/64); c++)
isram[c] = 0;
flushmmucache();
split_mapping_enabled = 0;
}
void