Fixes #19 - Allow ROM regions to be loaded from files

This commit is contained in:
Stefan Arentz 2016-11-16 15:28:35 -05:00
parent f131038f70
commit 6e0eaaa5be
2 changed files with 33 additions and 3 deletions

33
cpu.c
View File

@ -21,13 +21,14 @@
// SOFTWARE.
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
@ -351,7 +352,7 @@ static uint8_t _rom_read(struct cpu_t *cpu, struct mem_t *mem, uint16_t addr) {
return ((uint8_t*) mem->obj)[addr - mem->start];
}
void cpu_add_rom(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data) {
void cpu_add_rom_data(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data) {
struct mem_t *mem = (struct mem_t*) malloc(sizeof(struct mem_t));
mem->type = MEM_TYPE_ROM;
mem->obj = data;
@ -363,6 +364,34 @@ void cpu_add_rom(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *da
cpu_add_mem(cpu, mem);
}
void cpu_add_rom_file(struct cpu_t *cpu, uint16_t start, char *path) {
int fd = open(path, O_RDONLY);
if (fd == -1) {
return;
}
struct stat file_info;
if (fstat(fd, &file_info) == -1) {
close(fd);
return;
}
if (file_info.st_size > (64 * 1024 - start)) {
close(fd);
return;
}
char *data = malloc(file_info.st_size);
if (read(fd, data, file_info.st_size) != file_info.st_size) {
close(fd);
return;
}
close(fd);
cpu_add_rom_data(cpu, start, file_info.st_size, (uint8_t*) data);
}
// IO Memory
void cpu_add_iom(struct cpu_t *cpu, uint16_t start, uint16_t length, void *obj, mem_read_handler_t read_handler, mem_write_handler_t write_handler) {

3
cpu.h
View File

@ -73,7 +73,8 @@ void cpu_init(struct cpu_t *cpu);
void cpu_add_mem(struct cpu_t *cpu, struct mem_t *mem);
void cpu_add_ram(struct cpu_t *cpu, uint16_t start, uint16_t length);
void cpu_add_rom(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data);
void cpu_add_rom_file(struct cpu_t *cpu, uint16_t start, char *path);
void cpu_add_rom_data(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data);
void cpu_add_iom(struct cpu_t *cpu, uint16_t start, uint16_t length, void *obj, mem_read_handler_t read_handler, mem_write_handler_t write_handler);
void cpu_trace(struct cpu_t *cpu, uint8_t trace);