Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/drivers/display_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ impl DisplayDriver {
DisplayDriver { canvas: canvas }
}

pub fn draw(&mut self, pixels: &[[u8; CHIP8_WIDTH]; CHIP8_HEIGHT]) {
for (y, row) in pixels.iter().enumerate() {
for (x, &col) in row.iter().enumerate() {
let x = (x as u32) * SCALE_FACTOR;
let y = (y as u32) * SCALE_FACTOR;
pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) {

for y in 0usize..CHIP8_HEIGHT {
for x in 0usize..CHIP8_WIDTH {
let _x = (x as u32) * SCALE_FACTOR;
let _y = (y as u32) * SCALE_FACTOR;

self.canvas.set_draw_color( color( (pixels[y] >> (63 - x) ) & 1 ));

self.canvas.set_draw_color(color(col));
let _ = self.canvas
.fill_rect(Rect::new(x as i32, y as i32, SCALE_FACTOR, SCALE_FACTOR));
.fill_rect( Rect::new( _x as i32, _y as i32, SCALE_FACTOR, SCALE_FACTOR ) );
}
}
self.canvas.present();
}
}

fn color(value: u8) -> pixels::Color {
fn color(value: u64) -> pixels::Color {
if value == 0 {
pixels::Color::RGB(0, 0,100)
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn main() {
display_driver.draw(&output.vram);
processor.vram_changed = false;
}


//buffer of opcodes per 60hz, set it to where it feels right, around 10-15
if opcode_count >=15 {
Expand Down
29 changes: 23 additions & 6 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,35 @@ impl Processor {
0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000

*/
/*
0x1110_1010_0000
0x0011_0101_0010 AND
0x0010_0000_0000
*/

/*
0x0000_1100_0101
0x0000_
*/

fn op_dxyn(&mut self, x: usize, y: usize, n: usize) -> ProgramCounter {
self.v[0x0f] = 0;
for byte in 0..n {
let y = (self.v[y] as usize + byte) % CHIP8_HEIGHT;
for bit in 0..8 {
let x = (self.v[x] as usize + bit) % CHIP8_WIDTH;
let color = (self.ram[self.i + byte] >> (7 - bit)) & 1;
self.v[0x0f] |= color & self.vram[y][x];
self.vram[y][x] ^= color;

let x = self.v[x] as usize % CHIP8_WIDTH;

let mut mask = self.ram[self.i + byte] as u64;

if x + 8 > CHIP8_WIDTH {
let tmp = mask >> (x - 56);
mask <<= 56 + ((x + 8) % CHIP8_WIDTH as usize);
mask |= tmp;
}
else {
mask = mask << (56 - x);
}
self.v[0xf] |= if self.vram[y] & mask > 0 { 1 } else { 0 };
self.vram[y] = self.vram[y] ^ mask;
}
self.vram_changed = true;
ProgramCounter::Next
Expand Down
40 changes: 29 additions & 11 deletions src/processor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,36 @@ fn test_op_cxkk() {
assert_eq!(processor.v[0] & 0xf0, 0);
}

fn print_vram(vram: &[u64; 32]){
for x in 0..32 {
println!("{:#066b}", vram[x]);
}
}

// DRW Vx, Vy, nibble
#[test]
fn test_op_dxyn() {
let mut processor = build_processor();
processor.i = 0;
processor.ram[0] = 0b11111111;
processor.ram[1] = 0b00000000;
processor.vram[0][0] = 1;
processor.vram[0][1] = 0;
processor.vram[1][0] = 1;
processor.vram[1][1] = 0;
//processor.vram[0][0] = 1;
//processor.vram[0][1] = 0;
processor.vram[0] = 0b01 << 63;
//processor.vram[1][0] = 1;
//processor.vram[1][1] = 0;
processor.vram[1] = 0b01 << 63;
processor.v[0] = 0;
processor.run_opcode(0xd002);

assert_eq!(processor.vram[0][0], 0);
assert_eq!(processor.vram[0][1], 1);
assert_eq!(processor.vram[1][0], 1);
assert_eq!(processor.vram[1][1], 0);
print_vram(&processor.vram);
//assert_eq!(processor.vram[0][0], 0);
assert_eq!(processor.vram[0] >> 63 & 1, 0);
//assert_eq!(processor.vram[0][1], 1);
assert_eq!((processor.vram[0] >> 62) & 1, 1);
//assert_eq!(processor.vram[1][0], 1);
assert_eq!(processor.vram[1] >> 63 & 1, 1);
//assert_eq!(processor.vram[1][1], 0);
assert_eq!((processor.vram[1] >> 62) & 1, 0);
assert_eq!(processor.v[0x0f], 1);
assert!(processor.vram_changed);
assert_eq!(processor.pc, NEXT_PC);
Expand All @@ -264,6 +276,8 @@ fn test_op_dxyn_wrap_horizontal() {
processor.v[0] = x as u8;
processor.v[1] = 0;
processor.run_opcode(0xd011);

print_vram(&processor.vram);

assert_eq!(processor.vram[0][x - 1], 0);
assert_eq!(processor.vram[0][x], 1);
Expand Down Expand Up @@ -291,9 +305,13 @@ fn test_op_dxyn_wrap_vertical() {
processor.v[0] = 0;
processor.v[1] = y as u8;
processor.run_opcode(0xd012);

print_vram(&processor.vram);

assert_eq!(processor.vram[y][0], 1);
assert_eq!(processor.vram[0][0], 1);
//assert_eq!(processor.vram[y][0], 1);
assert_eq!(processor.vram[y] >> 63 & 1, 1);
//assert_eq!(processor.vram[0][0], 1);
assert_eq!(processor.vram[0] >> 63 & 1, 1);
assert_eq!(processor.v[0x0f], 0);
}

Expand Down