Skip to content

Commit 68d24e6

Browse files
committed
vdp: add constants for VRAM and CRAM size that would have prevented the auto-increment bug
1 parent 2b6d9c2 commit 68d24e6

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/vdp.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ macro_rules! debugln {
5050

5151
const VDP_CMD: u16 = 0xBF;
5252
const VDP_DATA: u16 = 0xBE;
53+
const VRAM_SIZE: u16 = 0x4000;
54+
const CRAM_SIZE: u16 = 0x40;
5355

5456
const DEBUG: bool = false;
5557
const OVERFLOW_TRACK: bool = false;
@@ -677,10 +679,10 @@ impl VdpState {
677679
}
678680
0x00 => {
679681
//reads
680-
self.addr = data as u16 | ((val as u16 & 0x3F) << 8);
682+
self.addr = (data as u16 | ((val as u16) << 8)) % VRAM_SIZE;
681683
self.dest = Some(WriteDest::Vram);
682684
self.vram_buffer = self.vram[self.addr as usize];
683-
self.addr = (self.addr + 1) & 0x3FFF;
685+
self.addr = (self.addr + 1) % VRAM_SIZE;
684686
/*
685687
println!(
686688
"setup vram read address to {:04X} and preloaded {:02X}",
@@ -690,7 +692,7 @@ impl VdpState {
690692
}
691693
0x40 => {
692694
//write
693-
self.addr = data as u16 | ((val as u16 & 0x3F) << 8);
695+
self.addr = (data as u16 | ((val as u16) << 8)) % VRAM_SIZE;
694696
self.dest = Some(WriteDest::Vram);
695697
//println!("setup vram write address {:04X}", self.addr);
696698
}
@@ -702,7 +704,7 @@ impl VdpState {
702704
if data >= 64 {
703705
println!("VDP cram data write: {} >= 64", val);
704706
}
705-
self.addr = data as u16 & 0x3F;
707+
self.addr = data as u16 % CRAM_SIZE;
706708
self.dest = Some(WriteDest::Cram);
707709
//println!("setup cram address: {:02X}", data);
708710
}
@@ -750,7 +752,7 @@ impl VdpState {
750752
*/
751753
ram[addr] = val;
752754
self.vram_buffer = val;
753-
self.addr = (self.addr + 1) & 0x3FFF;
755+
self.addr = (self.addr + 1) % VRAM_SIZE;
754756
}
755757
Some(WriteDest::Cram) => {
756758
if addr & 1 == 0 {
@@ -761,7 +763,7 @@ impl VdpState {
761763
ram[addr] = val & 0x0F;
762764
//println!("Written to CRAM: {:02X}{:02X}", ram[addr], ram[addr - 1]);
763765
}
764-
self.addr = (self.addr + 1) & 0x3F;
766+
self.addr = (self.addr + 1) % CRAM_SIZE;
765767
}
766768
None => unreachable!(),
767769
}
@@ -794,7 +796,7 @@ impl VdpState {
794796
);
795797
*/
796798
self.vram_buffer = self.vram[self.addr as usize];
797-
self.addr = (self.addr + 1) & 0x3FFF;
799+
self.addr = (self.addr + 1) % VRAM_SIZE;
798800

799801
Ok(val)
800802
}

0 commit comments

Comments
 (0)