I'm halfway-ish done making my PPU cycle-specific, but I'm still wondering when the CPU needs to let the PPU cycle 3 times. Basically, what I have right now, is that the PPU's tick() method (basically just calls cycle() 3x) is called in:
- CPU's memory.read method
- CPU's memory.write method
- Whenever an additional cycle is required when branching/page crossing
Now, I have implemented dummy memory.reads to get that extra cycle in 1-byte operations, which makes sense. The things that confuse me are instructions like Zero Page ASL (OP $06) because they require 5 cycles, and here is what I might be misunderstanding (or not):
- Cycle 1: Fetching OP from PC (check)
- Cycle 2: Fetching zero page address from PC + 1 (check)
- Cycle 3: Fetching data from zero page address (check)
- Cycle 4: Writing altered data back to memory (check)
- Cycle 5: ??????? (not check)
Now, if I had to guess, this 5th cycle would actually be the 4th cycle (so between the above mentioned cycle 3 and 4), and basically this cycle is where all the shifting and flag checking would happen. If that's the case, then my (Java) code looks something like this:
Is this correct, or am I misunderstanding something?
- CPU's memory.read method
- CPU's memory.write method
- Whenever an additional cycle is required when branching/page crossing
Now, I have implemented dummy memory.reads to get that extra cycle in 1-byte operations, which makes sense. The things that confuse me are instructions like Zero Page ASL (OP $06) because they require 5 cycles, and here is what I might be misunderstanding (or not):
- Cycle 1: Fetching OP from PC (check)
- Cycle 2: Fetching zero page address from PC + 1 (check)
- Cycle 3: Fetching data from zero page address (check)
- Cycle 4: Writing altered data back to memory (check)
- Cycle 5: ??????? (not check)
Now, if I had to guess, this 5th cycle would actually be the 4th cycle (so between the above mentioned cycle 3 and 4), and basically this cycle is where all the shifting and flag checking would happen. If that's the case, then my (Java) code looks something like this:
Code:
public void executeInstruction() {
int op = memory.read(PC++); //CPU cycles once, PPU cycles 3 times
..... //something something something
switch(op) {
.....
case 0x06: //ASL with zero page addressing
asl(getZeroAddress());
break;
.....
}
.....
.....
.....
public void asl(int address) {
int data = memory.read(address); //CPU cycles once, PPU cycles 3 times
data <<= 1;
checkCarry(data);
checkZero(data);
checkSign(data);
data &= 0xFF;
ppu.tick(); //CPU cycled once, let the PPU cycle 3 times
memory.write(address, data); //CPU cycles once, PPU cycles 3 times
}
.....
.....
.....
public int getZeroAddress() {
return memory.read(PC++); //CPU cycles once, PPU cycles 3 times
}
int op = memory.read(PC++); //CPU cycles once, PPU cycles 3 times
..... //something something something
switch(op) {
.....
case 0x06: //ASL with zero page addressing
asl(getZeroAddress());
break;
.....
}
.....
.....
.....
public void asl(int address) {
int data = memory.read(address); //CPU cycles once, PPU cycles 3 times
data <<= 1;
checkCarry(data);
checkZero(data);
checkSign(data);
data &= 0xFF;
ppu.tick(); //CPU cycled once, let the PPU cycle 3 times
memory.write(address, data); //CPU cycles once, PPU cycles 3 times
}
.....
.....
.....
public int getZeroAddress() {
return memory.read(PC++); //CPU cycles once, PPU cycles 3 times
}
Is this correct, or am I misunderstanding something?