sh-dis/c/main.c
Zack Buhman 8a300ba4c6 initial SH4 emulator implementation in C
This currently only implements the SH2 instructions.
2024-04-22 20:53:36 +08:00

61 lines
1.4 KiB
C

#include <assert.h>
#include <stdio.h>
#include "memory_map.h"
#include "ram.h"
#include "state.h"
#include "exception.h"
#include "execute.h"
#include "decode_print.h"
int main(int argc, char *argv[])
{
assert(argc > 1);
FILE * f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "fopen %s\n", argv[1]);
return -1;
}
int ret = fseek(f, 0, SEEK_END);
assert(ret == 0);
uint32_t read_size = ftell(f);
ret = fseek(f, 0, SEEK_SET);
assert(ret == 0);
uint32_t rom_size = ((read_size + 3) & ~3);
uint32_t buf[rom_size / 4];
uint32_t ret_size = fread(buf, 1, read_size, f);
assert(ret_size == read_size);
ret = fclose(f);
assert(ret == 0);
struct memory_map map = { .length = 0 };
map.entry[map.length++] = (struct memory_map_entry){
.start = 0x0000'0000,
.size = rom_size,
.mem = (void *)buf,
.access = ram__memory_access,
};
struct architectural_state state = { 0 };
POWERON(&state);
char const * instruction_buf;
char operand_buf[128];
while (1) {
uint32_t instruction_code = fetch(&state, &map);
decode_and_print_instruction(&state, &map, instruction_code, &instruction_buf, operand_buf, 128);
printf("pc %08x %-7s %-20s\n", state.pc[0], instruction_buf, operand_buf);
if (physical_address(state.pc[0]) == 0x38)
break;
step(&state, &map);
}
printf("part1 %d\n", state.gbr);
printf("part2 %d %d\n", state.mach, state.macl);
}