#include #include #include "memory_map.h" #include "ram.h" #include "state.h" #include "exception.h" #include "execute.h" #include "state_helpers.h" #include "operations.h" #include "decode_print.h" void render(struct architectural_state * state, struct memory_map * map) { char const * instruction_buf; char operand_buf[128]; printf("\e[1;1H\e[2J"); for (int i = 0; i < 8; i++) { int ra = i; int rb = i+8; int rc = i+16; printf(" r%-2d 0x%08x r%-2d 0x%08x xr%-2d 0x%08x\n", ra, state->general_register[ra], rb, state->general_register[rb], (rc-16), state->general_register[rc] ); } printf("\n"); for (int i = 0; i < 8; i++) { int ra = i; int rb = i+8; int rc = i+16; int rd = i+24; printf(" fr%-2d 0x%08x fr%-2d 0x%08x xf%-2d 0x%08x xf%-2d 0x%08x\n", ra, state->floating_point_register.fr[ra], rb, state->floating_point_register.fr[rb], (rc-16), state->floating_point_register.fr[rc], (rd-16), state->floating_point_register.fr[rd] ); } printf("\n"); printf(" pc 0x%08x pr 0x%08x\n", state->pc[0], state->pr[0]); printf(" mach 0x%08x macl 0x%08x\n", state->mach, state->macl ); printf(" fpscr 0x%08x fpul 0x%08x\n", state->fpscr.value, state->fpul); printf(" sr 0x%08x\n", state->sr.value); printf("md:%d ", state->sr.bits.md); printf("rb:%d ", state->sr.bits.rb); printf("bl:%d ", state->sr.bits.bl); printf("fd:%d ", state->sr.bits.fd); printf("m:%d ", state->sr.bits.m); printf("q:%d ", state->sr.bits.q); printf("imask:%d ", state->sr.bits.imask); printf("s:%d ", state->sr.bits.s); printf("t:%d ", state->sr.bits.t); printf("\n"); uint32_t instruction_code; printf("\n\n"); for (int i = -1; i < 8; i++) { uint32_t address = zero_extend32(sign_extend32(state->pc[0]) + i * 2); instruction_code = read_memory16(map, address); decode_and_print_instruction(state, map, instruction_code, &instruction_buf, operand_buf, 128); const char p = i == 0 ? '>' : ' '; printf(" %c %08x %-7s %-20s\n", p, address, instruction_buf, operand_buf); } } 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); state.pc[2] = state.pc[0] + 2; state.pr[2] = state.pr[0]; render(&state, &map); bool free = 1; int ix = 0; while (1) { if (physical_address(state.pc[0]) == 0x96) break; if (!free) getchar(); step(&state, &map); if (!free || (ix % 1000) == 0) render(&state, &map); ix++; } render(&state, &map); printf("part1 %x\n", state.floating_point_register.fr[9]); }