Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions core/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#define IMAGE_VERSION 0xCECE001B
#define IMAGE_VERSION 0xCECE001C

void EMSCRIPTEN_KEEPALIVE emu_exit(void) {
cpu_set_signal(CPU_SIGNAL_EXIT);
Expand All @@ -43,7 +44,7 @@ bool emu_save(emu_data_t type, const char *path) {
success = fwrite(&version, sizeof version, 1, file) == 1 && asic_save(file);
break;
case EMU_DATA_ROM:
success = fwrite(mem.flash.block, 1, SIZE_FLASH, file) == SIZE_FLASH;
success = fwrite(mem.flash.block, 1, mem.flash.size, file) == mem.flash.size;
break;
case EMU_DATA_RAM:
success = fwrite(mem.ram.block, 1, SIZE_RAM, file) == SIZE_RAM;
Expand Down Expand Up @@ -115,15 +116,35 @@ emu_state_t emu_load(emu_data_t type, const char *path) {

if (fseek(file, 0L, SEEK_END) < 0) goto rerr;
size = (size_t)ftell(file);
if (size > SIZE_FLASH) {
gui_console_err_printf("[CEmu] Invalid ROM size (%u bytes | max %u bytes)\n", (unsigned int)size, SIZE_FLASH);
if (size > SIZE_FLASH_MAX) {
gui_console_err_printf("[CEmu] Invalid ROM size (%u bytes | max %u bytes)\n", (unsigned int)size, SIZE_FLASH_MAX);
goto rerr;
}
rewind(file);

asic_free();
asic_init();

/* Allocate FLASH memory */
if (size < SIZE_FLASH_MIN) {
mem.flash.size = SIZE_FLASH_MIN;
} else {
/* Round up to next power of 2 */
uint32_t rounded = size - 1;
rounded |= rounded >> 1;
rounded |= rounded >> 2;
rounded |= rounded >> 4;
rounded |= rounded >> 8;
rounded |= rounded >> 16;
mem.flash.size = rounded + 1;
}
mem.flash.block = malloc(mem.flash.size);
if (mem.flash.block == NULL) {
gui_console_err_printf("[CEmu] Error allocating memory for ROM\n");
goto rerr;
}
memset(mem.flash.block, 0xFF, mem.flash.size);

if (fread(mem.flash.block, size, 1, file) != 1) {
gui_console_err_printf("[CEmu] Error reading ROM image\n");
goto rerr;
Expand All @@ -134,7 +155,7 @@ emu_state_t emu_load(emu_data_t type, const char *path) {
outer = mem.flash.block;

/* Outer 0x800(0) field. */
if (cert_field_get(outer + offset, SIZE_FLASH - offset, &field_type, &outer, &outer_field_size)) break;
if (cert_field_get(outer + offset, mem.flash.size - offset, &field_type, &outer, &outer_field_size)) break;
if (field_type != 0x800F) continue;

/* Inner 0x801(0) field: calculator model */
Expand Down
18 changes: 12 additions & 6 deletions core/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
| flash.maskReg[1] << 8
| flash.maskReg[2] << 16
| flash.maskReg[3] << 24;
flash.mask = ~value & (SIZE_FLASH - 1);
flash.mask = ~value & (mem.flash.size - 1);
}

void flash_flush_cache(void) {
Expand Down Expand Up @@ -108,9 +108,9 @@
}

static void flash_erase(uint32_t size) {
assert(!(size & (size - 1)));
assert(!(size & (size - 1)) && size <= mem.flash.size);
if (flash.commandStatus[1] & 1 << 1) {
memset(&mem.flash.block[flash.commandAddress & (SIZE_FLASH - 1) & -size], 0xFF, size);
memset(&mem.flash.block[flash.commandAddress & (mem.flash.size - size)], 0xFF, size);
}
flash_finish_command();
}
Expand Down Expand Up @@ -150,7 +150,7 @@
flash_erase(64 << 10);
break;
case 0xC7: case 0x60: // Chip Erase
flash_erase(SIZE_FLASH);
flash_erase(mem.flash.size);
break;
}
if (!flash.commandLength && flash.commandStatus[0] & 3 << 1) {
Expand Down Expand Up @@ -201,8 +201,14 @@
flash.commandAddress = 0;
}
addr = 0;
} else if (addr == 2) {
value = 0x16;
while ((1 << value) < mem.flash.size) {

Check warning on line 206 in core/flash.c

View workflow job for this annotation

GitHub Actions / Build: ubuntu-22.04 - x64-Dynamic

comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Wsign-compare]

Check warning on line 206 in core/flash.c

View workflow job for this annotation

GitHub Actions / Build: ubuntu-22.04 - x64

comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Wsign-compare]
value++;
}
} else {
value = 0xEF40 >> (1 - addr) * 8;
}
value = 0xEF4016 >> (2 - addr) * 8;
if (!peek) {
flash.commandAddress++;
}
Expand All @@ -213,7 +219,7 @@
static void flash_write_command(uint8_t byte) {
switch (flash.command[0xF]) {
case 0x32: // Quad Input Page Program
mem.flash.block[flash.commandAddress & (SIZE_FLASH - 1)] &= byte;
mem.flash.block[flash.commandAddress & (mem.flash.size - 1)] &= byte;
flash.commandAddress = (flash.commandAddress & ~0xFF) | ((flash.commandAddress + 1) & 0xFF);
break;
}
Expand Down
8 changes: 4 additions & 4 deletions core/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ eZ80portrange_t init_intrpt(void) {
}

bool intrpt_save(FILE *image) {
bool ret = false;
bool ret = true;
size_t request;
for (request = 0; request < sizeof(intrpt) / sizeof(*intrpt); request++) {
ret |= fwrite(&intrpt[request], sizeof(intrpt[request]), 1, image) == 1;
ret &= fwrite(&intrpt[request], sizeof(intrpt[request]), 1, image) == 1;
}
return ret;
}

bool intrpt_restore(FILE *image) {
bool ret = false;
bool ret = true;
size_t request;
for (request = 0; request < sizeof(intrpt) / sizeof(*intrpt); request++) {
ret |= fread(&intrpt[request], sizeof(intrpt[request]), 1, image) == 1;
ret &= fread(&intrpt[request], sizeof(intrpt[request]), 1, image) == 1;
}
return ret;
}
2 changes: 1 addition & 1 deletion core/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ void emu_set_lcd_ptrs(uint32_t **dat, uint32_t **dat_end, int width, int height,
}

if (addr < 0xD00000) {
mem_end = mem.flash.block + SIZE_FLASH;
mem_end = mem.flash.block + mem.flash.size;
data_start = mem.flash.block + addr;
} else if (addr < 0xE00000) {
mem_end = mem.ram.block + SIZE_RAM;
Expand Down
Loading
Loading