Now execute circles not get passed to execute variable - it can check now if there is EOF
This commit is contained in:
parent
6b167f6cdc
commit
ab23f2f98f
|
@ -4,7 +4,6 @@
|
|||
|
||||
struct CPU cpu;
|
||||
struct MEMORY mem;
|
||||
byte cycles;
|
||||
|
||||
void memory_init () {
|
||||
for (int i = 0; i < MAX_MEMORY; i++){
|
||||
|
@ -32,7 +31,6 @@ void dump_cpu(struct CPU cpu) {
|
|||
byte fetch_byte() {
|
||||
byte data = mem.memory[cpu.PC];
|
||||
cpu.PC++;
|
||||
cycles--;
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -42,14 +40,12 @@ word fetch_word() {
|
|||
|
||||
data |= (mem.memory[cpu.PC] << 8);
|
||||
cpu.PC++;
|
||||
cycles -= 2;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
byte read_byte(word addr) {
|
||||
byte data = mem.memory[addr];
|
||||
cycles--;
|
||||
cpu.PC++;
|
||||
return data;
|
||||
}
|
||||
|
@ -59,28 +55,20 @@ word read_word(word addr) {
|
|||
data |= (mem.memory[addr+1] << 8);
|
||||
cpu.PC++;
|
||||
cpu.PC++;
|
||||
cycles -= 2;
|
||||
return data;
|
||||
}
|
||||
|
||||
void write_byte(byte addr, byte value) {
|
||||
mem.memory[addr] = value;
|
||||
cycles--;
|
||||
}
|
||||
|
||||
void write_word(byte addr, word value) {
|
||||
mem.memory[addr] = value & 0xFF;
|
||||
mem.memory[addr+1] = (value >> 8);
|
||||
cycles -= 2;
|
||||
}
|
||||
|
||||
void execute(byte exec_cycles) {
|
||||
cycles = exec_cycles;
|
||||
if (cycles <= 0) {
|
||||
printf("ERROR\n");
|
||||
exit(1);
|
||||
}
|
||||
while (cycles > 0) {
|
||||
void execute() {
|
||||
while (1) {
|
||||
byte instruction = fetch_byte();
|
||||
switch(instruction) {
|
||||
case INS_LDA_IM:
|
||||
|
@ -97,7 +85,6 @@ void execute(byte exec_cycles) {
|
|||
break;
|
||||
case INS_LDA_ZPX:
|
||||
byte zero_page_x_addr = cpu.X + fetch_byte();
|
||||
cycles--;
|
||||
cpu.A = read_byte(zero_page_x_addr);
|
||||
cpu.Z = (cpu.A == 0);
|
||||
cpu.N = (cpu.A & 0b1000000) > 0;
|
||||
|
@ -117,7 +104,6 @@ void execute(byte exec_cycles) {
|
|||
break;
|
||||
case INS_LDX_ZPY:
|
||||
byte zero_page_y_addr = cpu.Y + fetch_byte();
|
||||
cycles--;
|
||||
cpu.X = read_byte(zero_page_y_addr);
|
||||
cpu.Z = (cpu.X == 0);
|
||||
cpu.N = (cpu.X & 0b1000000) > 0;
|
||||
|
@ -137,7 +123,6 @@ void execute(byte exec_cycles) {
|
|||
break;
|
||||
case INS_LDY_ZPX:
|
||||
zero_page_x_addr = cpu.Y + fetch_byte();
|
||||
cycles--;
|
||||
cpu.Y = read_byte(zero_page_x_addr);
|
||||
cpu.Z = (cpu.Y == 0);
|
||||
cpu.N = (cpu.Y & 0b1000000) > 0;
|
||||
|
@ -148,14 +133,15 @@ void execute(byte exec_cycles) {
|
|||
write_word(cpu.PC - 1, cpu.SP);
|
||||
cpu.SP++;
|
||||
cpu.PC = jsr_addr;
|
||||
cycles--;
|
||||
break;
|
||||
case INS_RTS:
|
||||
// TODO
|
||||
break;
|
||||
case 0xFF: // EOF
|
||||
return;
|
||||
default:
|
||||
printf("%04X: Unhandled instruction: %04X\n", cpu.PC, instruction);
|
||||
cycles = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -172,8 +158,9 @@ int main(int argc, char *argv[]) {
|
|||
mem.memory[pos] = ch;
|
||||
pos++;
|
||||
}
|
||||
mem.memory[pos] = EOF;
|
||||
|
||||
execute(5); // FIXME: count how many circles is needed based on instructions
|
||||
execute();
|
||||
dump_cpu(cpu);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
ゥVV
|
||||
ゥVゥdゥB
|
Loading…
Reference in New Issue