diff --git a/src/6502/6502.c b/src/6502/6502.c index 05fa8f7..5974c8e 100644 --- a/src/6502/6502.c +++ b/src/6502/6502.c @@ -70,87 +70,92 @@ void write_word(byte addr, word value) { void execute() { while (1) { byte instruction = fetch_byte(); + byte value; + byte zero_page_addr; + byte zero_page_x_addr; + byte zero_page_y_addr; + word jsr_addr; switch(instruction) { case INS_LDA_IM: - byte value = fetch_byte(); + value = fetch_byte(); cpu.A = value; cpu.Z = (cpu.A == 0); - cpu.N = (cpu.A & 0b1000000) > 0; + cpu.N = (cpu.A & 0x40) > 0; // 0x40 means 0b1000000 (fixed to not receive a warning from compiler) break; case INS_LDA_ZP: - byte zero_page_addr = fetch_byte(); + zero_page_addr = fetch_byte(); cpu.A = read_byte(zero_page_addr); cpu.Z = (cpu.A == 0); - cpu.N = (cpu.A & 0b1000000) > 0; + cpu.N = (cpu.A & 0x40) > 0; break; case INS_LDA_ZPX: - byte zero_page_x_addr = cpu.X + fetch_byte(); + zero_page_x_addr = cpu.X + fetch_byte(); cpu.A = read_byte(zero_page_x_addr); cpu.Z = (cpu.A == 0); - cpu.N = (cpu.A & 0b1000000) > 0; + cpu.N = (cpu.A & 0x40) > 0; break; case INS_LDX_IM: value = fetch_byte(); cpu.X = value; cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; break; case INS_LDX_ZP: zero_page_addr = fetch_byte(); cpu.X = read_byte(zero_page_addr); cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; break; case INS_LDX_ZPY: - byte zero_page_y_addr = cpu.Y + fetch_byte(); + zero_page_y_addr = cpu.Y + fetch_byte(); cpu.X = read_byte(zero_page_y_addr); cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; break; case INS_LDY_IM: value = fetch_byte(); cpu.Y = value; cpu.Z = (cpu.Y == 0); - cpu.N = (cpu.Y & 0b1000000) > 0; + cpu.N = (cpu.Y & 0x40) > 0; break; case INS_LDY_ZP: zero_page_addr = fetch_byte(); cpu.Y = read_byte(zero_page_addr); cpu.Z = (cpu.Y == 0); - cpu.N = (cpu.Y & 0b1000000) > 0; + cpu.N = (cpu.Y & 0x40) > 0; break; case INS_LDY_ZPX: zero_page_x_addr = cpu.Y + fetch_byte(); cpu.Y = read_byte(zero_page_x_addr); cpu.Z = (cpu.Y == 0); - cpu.N = (cpu.Y & 0b1000000) > 0; + cpu.N = (cpu.Y & 0x40) > 0; break; case INS_TAX: cpu.X = cpu.A; cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; cpu.PC++; break; case INS_TAY: cpu.Y = cpu.A; cpu.Z = (cpu.Y == 0); - cpu.N = (cpu.Y & 0b1000000) > 0; + cpu.N = (cpu.Y & 0x40) > 0; cpu.PC++; break; case INS_TSX: cpu.X = cpu.SP; cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; cpu.PC++; break; case INS_TXA: cpu.A = cpu.X; cpu.Z = (cpu.A == 0); - cpu.N = (cpu.A & 0b1000000) > 0; + cpu.N = (cpu.A & 0x40) > 0; cpu.PC++; break; case INS_TXS: @@ -160,12 +165,12 @@ void execute() { case INS_TYA: cpu.A = cpu.Y; cpu.Z = (cpu.A == 0); - cpu.N = (cpu.A & 0b1000000) > 0; + cpu.N = (cpu.A & 0x40) > 0; cpu.PC++; break; case INS_JSR: - word jsr_addr = fetch_word(); + jsr_addr = fetch_word(); write_word(cpu.PC - 1, cpu.SP); cpu.SP++; cpu.PC = jsr_addr; @@ -173,29 +178,29 @@ void execute() { case INS_RTS: // TODO break; - case INS_NOP: + case INS_6502_NOP: break; case INS_INX: cpu.X++; cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; break; case INS_INY: cpu.Y++; cpu.Z = (cpu.Y == 0); - cpu.N = (cpu.Y & 0b1000000) > 0; + cpu.N = (cpu.Y & 0x40) > 0; break; case INS_DEX: cpu.X--; cpu.Z = (cpu.X == 0); - cpu.N = (cpu.X & 0b1000000) > 0; + cpu.N = (cpu.X & 0x40) > 0; break; case INS_DEY: cpu.Y--; cpu.Z = (cpu.Y == 0); - cpu.N = (cpu.Y & 0b1000000) > 0; + cpu.N = (cpu.Y & 0x40) > 0; break; case 0x00: // EOF return; diff --git a/src/6502/6502.h b/src/6502/6502.h index 5b800b9..b6072c9 100644 --- a/src/6502/6502.h +++ b/src/6502/6502.h @@ -36,7 +36,7 @@ #define INS_JSR 0x20 // JSR #define INS_RTS 0x60 // RTS -#define INS_NOP 0xEA // NOP +#define INS_6502_NOP 0xEA // NOP #define INS_INX 0xE8 // INX Implied #define INS_INY 0xC8 // INY Implied diff --git a/src/Makefile b/src/Makefile index d8fbaf9..9d6100b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS= -O2 -Wall -Wextra +CFLAGS= -O2 -pedantic -Wall -Wextra LFLAGS= SRCFILES= main.c 6502/6502.c ocpu/ocpu.c diff --git a/src/ocpu/ocpu.c b/src/ocpu/ocpu.c index 7b28fed..d43d3c4 100644 --- a/src/ocpu/ocpu.c +++ b/src/ocpu/ocpu.c @@ -100,15 +100,18 @@ int match_register(byte reg) { // Function that returns register's value by taki } void ocpu_execute() { + byte reg; + byte value; + word word_value; while (1) { byte instruction = ocpu_fetch_byte(); switch(instruction) { case INS_MOV_IM: - byte reg = ocpu_fetch_byte(); + reg = ocpu_fetch_byte(); switch(reg) { case REG_AL: - byte value = ocpu_fetch_byte(); + value = ocpu_fetch_byte(); ocpu.A = (ocpu.A & 0xFF00) | (value & 0xFF); ocpu.AL = ocpu.A & 0xFF; break; @@ -118,7 +121,7 @@ void ocpu_execute() { ocpu.AH = ocpu.A >> 8; break; case REG_A: - word word_value = ocpu_fetch_word(); + word_value = ocpu_fetch_word(); ocpu.A = word_value; break; @@ -206,7 +209,7 @@ void ocpu_execute() { reg = ocpu_fetch_byte(); switch(reg) { case REG_AL: - byte value = match_register(ocpu_fetch_byte()); + value = match_register(ocpu_fetch_byte()); ocpu.A = (ocpu.A & 0xFF00) | (value & 0xFF); ocpu.AL = ocpu.A & 0xFF; break; @@ -216,7 +219,7 @@ void ocpu_execute() { ocpu.AH = ocpu.A >> 8; break; case REG_A: - word word_value = match_register(ocpu_fetch_byte()); + word_value = match_register(ocpu_fetch_byte()); ocpu.A = word_value; break; @@ -305,7 +308,7 @@ void ocpu_execute() { switch(reg) { case REG_AL: - byte value = ocpu_fetch_byte(); + value = ocpu_fetch_byte(); ocpu.A += (ocpu.A & 0xFF00) | (value & 0xFF); ocpu.AL = ocpu.A & 0xFF; break; @@ -315,7 +318,7 @@ void ocpu_execute() { ocpu.AH = ocpu.A >> 8; break; case REG_A: - word word_value = ocpu_fetch_word(); + word_value = ocpu_fetch_word(); ocpu.A += word_value; break; @@ -403,7 +406,7 @@ void ocpu_execute() { reg = ocpu_fetch_byte(); switch(reg) { case REG_AL: - byte value = match_register(ocpu_fetch_byte()); + value = match_register(ocpu_fetch_byte()); ocpu.A += (ocpu.A & 0xFF00) | (value & 0xFF); ocpu.AL = ocpu.A & 0xFF; break; @@ -413,7 +416,7 @@ void ocpu_execute() { ocpu.AH = ocpu.A >> 8; break; case REG_A: - word word_value = match_register(ocpu_fetch_byte()); + word_value = match_register(ocpu_fetch_byte()); ocpu.A += word_value; break; @@ -657,7 +660,7 @@ void ocpu_execute() { return; } break; - case INS_NOP: + case INS_OCPU_NOP: break; case 0x00: return; diff --git a/src/ocpu/ocpu.h b/src/ocpu/ocpu.h index 177b8ee..94597af 100644 --- a/src/ocpu/ocpu.h +++ b/src/ocpu/ocpu.h @@ -37,7 +37,7 @@ #define INS_ADD_REG 0xB0 #define INS_INC 0x15 #define INS_DEC 0x16 -#define INS_NOP 0x90 +#define INS_OCPU_NOP 0x90 struct OCPU_MEMORY { byte memory[MAX_MEMORY];