From 85bc469a9c23db5f622a51ed6220e3c9dea6e868 Mon Sep 17 00:00:00 2001 From: Luxdragon Date: Sun, 14 Jan 2024 11:00:05 +0100 Subject: [PATCH] Still having I/O problems --- emulator_shell.c | 87 ++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/emulator_shell.c b/emulator_shell.c index 22c2971..d2738f9 100644 --- a/emulator_shell.c +++ b/emulator_shell.c @@ -397,6 +397,35 @@ void printState(State8080* state) { state->e, state->h, state->l, state->sp); } +uint8_t machineIn(State8080* state, uint8_t port) { + uint8_t a; + switch(port) { + case 3: + { + uint16_t v = (state->shift1<<8) | (state->shift0); + a = ((v >> (8-state->shift_offset)) & 0xff); + } + break; + } + return a; +} + +uint8_t machineOut(State8080* state, uint8_t port, uint8_t value) { + switch(port) { + case 2: + { + state->shift_offset = value & 0x7; + } break; + case 4: + { + state->shift0 = state->shift1; + state->shift1 = value; + break; + } + } +} + + int emulate8080(State8080* state) { unsigned char *opcode = &(state->memory[state->pc]); @@ -696,7 +725,8 @@ int emulate8080(State8080* state) { state->sp += 2; break; case 0xd3: // OUT byte - //printf("%c", opcode[1]); + uint8_t port = opcode[1]; + machineOut(state, port, 0x00); // PROBLEM: We need to pass a value to machineOUT state->pc++; break; case 0xd5: // PUSH D @@ -705,7 +735,8 @@ int emulate8080(State8080* state) { state->sp -= 2; break; case 0xdb: // IN byte - //printf("%c", opcode[1]); + uint8_t port = opcode[1]; + state->a = machineIn(state, port); state->pc++; break; case 0xe1: // POP H @@ -781,9 +812,19 @@ int emulate8080(State8080* state) { default: unknownInstruction(state); break; } printState(state); - } + void GenerateInterrupt(State8080* state, int interrupt_num) + { + //perform "PUSH PC" + Push(state, (state->pc & 0xFF00) >> 8, (state->pc & 0xff)); + + //Set the PC to the low memory vector. + //This is identical to an "RST interrupt_num" instruction. + state->pc = 8 * interrupt_num; + } + + void read_Space_Invaders_ROM(State8080* state, const char* filename, int offset) { //Works exclusively with the memory system of Space Invaders FILE* file = fopen(filename, "rb"); @@ -802,34 +843,6 @@ void read_Space_Invaders_ROM(State8080* state, const char* filename, int offset) fclose(file); } -uint8_t machineIn(State8080* state, uint8_t port) { - uint8_t a; - switch(port) { - case 3: - { - uint16_t v = (state->shift1<<8) | (state->shift0); - a = ((v >> (8-state->shift_offset)) & 0xff); - } - break; - } - return a; -} - -uint8_t machineOut(State8080* state, uint8_t port, uint8_t value) { - uint8_t a; - switch(port) { - case 2: - { - state->shift_offset = value & 0x7; - } break; - case 4: - { - state->shift0 = state->shift1; - state->shift1 = value; - break; - } - } -} int main (int argc, char *argv[]) { gtk_init(&argc, &argv); @@ -861,18 +874,6 @@ int main (int argc, char *argv[]) { char done = 0; while (!done) { - uint8_t* opcode = &state->memory[state->pc]; - printf("%d\t\t:", i); - - if (*opcode == 0xdb) { - uint8_t port = opcode[1]; - state->a = machineIn(state, port); - state->pc++; - } else if (*opcode == 0xd3) { - uint8_t port = opcode[1]; - machineOut(state, port, 0x00); - state->pc++; - } emulate8080(state); i++; sleep(0.00001);