// The CPU is stalled for up to 4 CPU cycles to allow the longest possible write (the return address and write after an IRQ) to finish.
// If OAM DMA is in progress, it is paused for two cycles. The sample fetch always occurs on an even CPU cycle due to its alignment with the APU.
// Specific delay cases:
// 4 cycles if it falls on a CPU read cycle.
// 3 cycles if it falls on a single CPU write cycle (or the second write of a double CPU write).
// 4 cycles if it falls on the first write of a double CPU write cycle.[4]
// 2 cycles if it occurs during an OAM DMA, or on the $4014 write cycle that triggers the OAM DMA.
// 1 cycle if it occurs on the second-last OAM DMA cycle.
// 3 cycles if it occurs on the last OAM DMA cycle.
// The sample buffer is filled with the next sample byte read from the current address, subject to whatever mapping hardware is present.
self.sample_buffer=Some(sample_byte);
// The address is incremented; if it exceeds $FFFF, it is wrapped around to $8000.
ifself.current_address==0xFFFF{
self.current_address=0x8000
}else{
self.current_address+=1;
}
// The bytes remaining counter is decremented; if it becomes zero and the loop flag is set, the sample is restarted (see above); otherwise, if the bytes remaining counter becomes zero and the IRQ enabled flag is set, the interrupt flag is set.
self.bytes_remaining-=1;
// At any time, if the interrupt flag is set, the CPU's IRQ line is continuously asserted until the interrupt flag is cleared.
// The processor will continue on from where it was stalled.