From 76b14e566978e2cf87d2b64883b6ac6aebaf14dc Mon Sep 17 00:00:00 2001 From: Jeroen Domburg Date: Thu, 27 Jun 2024 12:45:04 +0800 Subject: [PATCH] Memory size can now be set --- emu.c | 21 +++++++++++------ emu.h | 1 + main.c | 11 +++++++++ mapper.c | 31 ++++++++----------------- mapper.h | 4 ++-- ramrom.c | 70 ++++++++++++++++++++++++++++++++++++-------------------- ramrom.h | 6 +++-- rtcram.c | 3 ++- 8 files changed, 89 insertions(+), 58 deletions(-) diff --git a/emu.c b/emu.c index 7889173..886cde3 100644 --- a/emu.c +++ b/emu.c @@ -143,10 +143,12 @@ unsigned int nop_read(void *obj, unsigned int a) { void nop_write(void *obj, unsigned int a, unsigned int val) { } -void setup_ram(const char *name) { +//Note: if size_bytes is negative, the size of the full region is used. +void setup_ram(const char *name, int size_bytes) { mem_range_t *m=find_range_by_name(name); assert(m); - m->obj=ram_new(m->size); + if (size_bytes<0) size_bytes=m->size; + m->obj=ram_new(size_bytes); m->read8=ram_read8; m->read16=ram_read16; m->read32=ram_read32; @@ -189,7 +191,6 @@ static int check_can_access(mem_range_t *m, unsigned int address) { #define PRINT_MEMREAD 0 static unsigned int read_memory_32(unsigned int address) { - if (address==0) EMU_LOG_DEBUG("read addr 0\n"); mem_range_t *m=find_range_by_addr(address); //HACK! If this is enabled, diags get more verbose #if 0 @@ -654,7 +655,10 @@ int m68k_int_cb(int level) { vectors[cur_cpu][r]=0; } raise_highest_int(); - EMU_LOG_DEBUG("Int ack level %x cpu %x vect %x\n", level, cur_cpu, r); + //we ignore the clock ints when printing debug messages + if (r!=INT_VECT_CLOCK) { + EMU_LOG_DEBUG("Int ack level %x cpu %x vect %x\n", level, cur_cpu, r); + } return r; } @@ -662,7 +666,10 @@ int need_raise_highest_int[2]={0}; void emu_raise_int(uint8_t vector, uint8_t level, int cpu) { if (vectors[cpu][vector]!=level) { - EMU_LOG_DEBUG("Interrupt %s: %x\n", level?"raised":"cleared", vector); + //we ignore the clock ints when printing debug messages + if (vector!=INT_VECT_CLOCK) { + EMU_LOG_DEBUG("Interrupt %s: %x\n", level?"raised":"cleared", vector); + } vectors[cpu][vector]=level; need_raise_highest_int[cpu]=1; m68k_end_timeslice(); @@ -754,8 +761,8 @@ void emu_start(emu_cfg_t *cfg) { tracefile=fopen("trace.txt","w"); //Note: this is a bitmask for which CPU gets logged. (1<<0) for dma, (1<<1) for job cpu. // do_tracefile=(1<<1); - setup_ram("RAM"); - setup_ram("SRAM"); + setup_ram("RAM", cfg->mem_size_bytes); + setup_ram("SRAM", -1); setup_rtcram("RTC_RAM", cfg->rtcram); setup_rom("U15", cfg->u15_rom); //used to be U17 setup_rom("U17", cfg->u17_rom); //used to be U19 diff --git a/emu.h b/emu.h index f2991da..4412ab6 100644 --- a/emu.h +++ b/emu.h @@ -30,6 +30,7 @@ typedef struct { const char *rtcram; int realtime; const char *cow_dir; + int mem_size_bytes; } emu_cfg_t; void emu_start(emu_cfg_t *cfg); diff --git a/main.c b/main.c index 1457327..8afd46a 100644 --- a/main.c +++ b/main.c @@ -73,6 +73,7 @@ int main(int argc, char **argv) { .rtcram="rtcram.bin", #endif .hd0img="plexus-sanitized.img", + .mem_size_bytes=2*1024*1024 }; #ifdef __EMSCRIPTEN__ emscripten_init(); @@ -93,18 +94,28 @@ int main(int argc, char **argv) { } else if (strcmp(argv[i], "-c")==0 && i+18) { + printf("Memory needs to be 1, 2, 4 or 8MiB (%d given)\n"); + printf("Note 1 and 8 MB may not be supported by the OS\n"); + error=1; + } if (error) { printf("Plexus-20 emulator\n"); printf("Usage: %s [args]\n", argv[0]); printf(" -u15 Path to U15 rom file\n"); printf(" -u17 Path to U17 rom file\n"); printf(" -r Try to run at realtime speeds\n"); + printf(" -m n Set the amount of memory to n megabytes\n"); printf(" -l module=level - set logging level of module to specified level\n"); printf(" -l level - Set overal log level to specified level\n"); printf("Modules: "); diff --git a/mapper.c b/mapper.c index 1773df1..ede22f4 100644 --- a/mapper.c +++ b/mapper.c @@ -6,6 +6,7 @@ #include "emu.h" #include "log.h" #include "mapper.h" +#include "ramrom.h" // Debug logging #define MAPPER_LOG(msg_level, format_and_args...) \ @@ -48,8 +49,7 @@ typedef struct { struct mapper_t { //2K entries for usr, 2K for sys desc_t desc[4096]; - uint8_t *physram; - int physram_size; + ram_t *physram; int sysmode; //indicates if next accesses are in sysmode or not int cur_id; }; @@ -184,60 +184,49 @@ int do_map(mapper_t *m, unsigned int a, unsigned int is_write) { void mapper_ram_write8(void *obj, unsigned int a, unsigned int val) { mapper_t *m=(mapper_t*)obj; - uint8_t *buffer=m->physram; a=do_map(m, a, 1); if (a<0) return; - buffer[a]=val; + ram_write8(m->physram, a, val); } void mapper_ram_write16(void *obj, unsigned int a, unsigned int val) { mapper_t *m=(mapper_t*)obj; - uint8_t *buffer=m->physram; a=do_map(m, a, 1); if (a<0) return; - buffer[a]=(val>>8); - buffer[a+1]=val; + ram_write16(m->physram, a, val); } void mapper_ram_write32(void *obj, unsigned int a, unsigned int val) { mapper_t *m=(mapper_t*)obj; - uint8_t *buffer=m->physram; a=do_map(m, a, 1); if (a<0) return; - buffer[a]=(val>>24); - buffer[a+1]=(val>>16); - buffer[a+2]=(val>>8); - buffer[a+3]=val; + ram_write32(m->physram, a, val); } unsigned int mapper_ram_read8(void *obj, unsigned int a) { mapper_t *m=(mapper_t*)obj; - uint8_t *buffer=m->physram; a=do_map(m, a, 0); if (a<0) return 0; - return buffer[a]; + return ram_read8(m->physram, a); } unsigned int mapper_ram_read16(void *obj, unsigned int a) { mapper_t *m=(mapper_t*)obj; - uint8_t *buffer=m->physram; a=do_map(m, a, 0); if (a<0) return 0; - return buffer[a+1]+(buffer[a]<<8); + return ram_read16(m->physram, a); } unsigned int mapper_ram_read32(void *obj, unsigned int a) { mapper_t *m=(mapper_t*)obj; - uint8_t *buffer=m->physram; a=do_map(m, a, 0); if (a<0) return 0; - return buffer[a+3]+(buffer[a+2]<<8)+(buffer[a+1]<<16)+(buffer[a]<<24); + return ram_read32(m->physram, a); } -mapper_t *mapper_new(void *physram, int size) { +mapper_t *mapper_new(ram_t *physram, int size) { mapper_t *ret=calloc(sizeof(mapper_t), 1); - ret->physram=(uint8_t*)physram; - ret->physram_size=size; + ret->physram=physram; return ret; } diff --git a/mapper.h b/mapper.h index 679b902..6bf3d51 100644 --- a/mapper.h +++ b/mapper.h @@ -1,4 +1,4 @@ - +#include "ramrom.h" typedef struct mapper_t mapper_t; @@ -9,7 +9,7 @@ unsigned int mapper_read16(void *obj, unsigned int a); void mapper_write16(void *obj, unsigned int a, unsigned int val); unsigned int mapper_read32(void *obj, unsigned int a); void mapper_write32(void *obj, unsigned int a, unsigned int val); -mapper_t *mapper_new(void *physram, int size); +mapper_t *mapper_new(ram_t *physram, int size); void mapper_ram_write8(void *obj, unsigned int a, unsigned int val); void mapper_ram_write16(void *obj, unsigned int a, unsigned int val); diff --git a/ramrom.c b/ramrom.c index b48be3c..9acf57d 100644 --- a/ramrom.c +++ b/ramrom.c @@ -2,6 +2,13 @@ #include #include #include "log.h" +#include "ramrom.h" + +struct ram_t { + int size_bytes; + int amask; + uint8_t *buffer; +}; // Debug logging #define RAMROM_LOG(msg_level, format_and_args...) \ @@ -10,56 +17,69 @@ #define RAMROM_LOG_WARNING(format_and_args...) RAMROM_LOG(LOG_WARNING, format_and_args) void ram_write8(void *obj, unsigned int a, unsigned int val) { - uint8_t *buffer=(uint8_t*)obj; - buffer[a]=val; + ram_t *ram=(ram_t*)obj; + a=a&ram->amask; + ram->buffer[a]=val; } void ram_write16(void *obj, unsigned int a, unsigned int val) { - uint8_t *buffer=(uint8_t*)obj; - buffer[a]=(val>>8); - buffer[a+1]=val; + ram_t *ram=(ram_t*)obj; + a=a&ram->amask; + ram->buffer[a]=(val>>8); + ram->buffer[a+1]=val; } void ram_write32(void *obj, unsigned int a, unsigned int val) { - uint8_t *buffer=(uint8_t*)obj; - buffer[a]=(val>>24); - buffer[a+1]=(val>>16); - buffer[a+2]=(val>>8); - buffer[a+3]=val; + ram_t *ram=(ram_t*)obj; + a=a&ram->amask; + ram->buffer[a]=(val>>24); + ram->buffer[a+1]=(val>>16); + ram->buffer[a+2]=(val>>8); + ram->buffer[a+3]=val; } unsigned int ram_read8(void *obj, unsigned int a) { - uint8_t *buffer=(uint8_t*)obj; - return buffer[a]; + ram_t *ram=(ram_t*)obj; + a=a&ram->amask; + return ram->buffer[a]; } unsigned int ram_read16(void *obj, unsigned int a) { - uint8_t *buffer=(uint8_t*)obj; - return buffer[a+1]+(buffer[a]<<8); + ram_t *ram=(ram_t*)obj; + a=a&ram->amask; + return ram->buffer[a+1]+(ram->buffer[a]<<8); } unsigned int ram_read32(void *obj, unsigned int a) { - uint8_t *buffer=(uint8_t*)obj; - return buffer[a+3]+(buffer[a+2]<<8)+(buffer[a+1]<<16)+(buffer[a]<<24); + ram_t *ram=(ram_t*)obj; + a=a&ram->amask; + return ram->buffer[a+3]+(ram->buffer[a+2]<<8)+(ram->buffer[a+1]<<16)+(ram->buffer[a]<<24); } -void *rom_new(const char *filename, int size) { +ram_t *rom_new(const char *filename, int size_bytes) { FILE *f=fopen(filename, "rb"); if (!f) { perror(filename); exit(1); } - char *buf=malloc(size); - int r=fread(buf, 1, size, f); + ram_t *rom=ram_new(size_bytes); + int r=fread(rom->buffer, 1, size_bytes, f); fclose(f); - if (r!=size) { - RAMROM_LOG_WARNING("%s: short read: %d bytes for rom region of %d bytes\n", filename, r, size); + if (r!=size_bytes) { + RAMROM_LOG_WARNING("%s: short read: %d bytes for rom region of %d bytes\n", filename, r, size_bytes); } - return buf; + return rom; } -void *ram_new(int size) { - char *buf=calloc(size, 1); - return buf; +ram_t *ram_new(int size_bytes) { + if (size_bytes & (size_bytes-1)) { + printf("ram_new: size should be power of two\n"); + exit(0); + } + ram_t *ram=calloc(sizeof(ram_t), 1); + ram->size_bytes=size_bytes; + ram->amask=(size_bytes-1); //works if size_bytes is power of two + ram->buffer=calloc(size_bytes, 1); + return ram; } diff --git a/ramrom.h b/ramrom.h index fb4802f..18df15e 100644 --- a/ramrom.h +++ b/ramrom.h @@ -1,10 +1,12 @@ +typedef struct ram_t ram_t; + void ram_write8(void *obj, unsigned int a, unsigned int val); void ram_write16(void *obj, unsigned int a, unsigned int val); void ram_write32(void *obj, unsigned int a, unsigned int val); unsigned int ram_read8(void *obj, unsigned int a); unsigned int ram_read16(void *obj, unsigned int a); unsigned int ram_read32(void *obj, unsigned int a); -void *rom_new(const char *filename, int size); -void *ram_new(int size); +ram_t *rom_new(const char *filename, int size); +ram_t *ram_new(int size); diff --git a/rtcram.c b/rtcram.c index c91053f..5544f54 100644 --- a/rtcram.c +++ b/rtcram.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "emu.h" #include "log.h" #include "rtcram.h" @@ -83,7 +84,7 @@ unsigned int rtcram_read32(void *obj, unsigned int a) { rtcram_t *rtcram_new(const char *filename) { rtcram_t *r=calloc(sizeof(rtcram_t), 1); - r->filename=filename; + r->filename=strdup(filename); FILE *rtcramfile = NULL; size_t rtcread = 0;