Skip to content

Commit

Permalink
better rendering with object transparency, x-flip and y-flip
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanballs committed Sep 11, 2024
1 parent 8479881 commit 1553486
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/cpu/execution/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,8 @@ impl CPU {
self.registers.f.half_carry = (self.registers.a & 0xF) < (b & 0xF);
self.registers.f.carry = self.registers.a < b;
}

pub(in crate::cpu) fn sbc(&mut self, b: u8) {
self.sub(b.wrapping_add(self.registers.f.carry as u8));
}
}
12 changes: 12 additions & 0 deletions src/cpu/execution/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ impl CPU {
self.registers.f.subtract = true;
}

pub(in crate::cpu) fn ccf(&mut self) {
self.registers.f.subtract = false;
self.registers.f.half_carry = false;
self.registers.f.carry = !self.registers.f.carry;
}

pub(in crate::cpu) fn scf(&mut self) {
self.registers.f.subtract = false;
self.registers.f.half_carry = false;
self.registers.f.carry = true;
}

pub(in crate::cpu) fn daa(&mut self) {
let mut correction = 0;
let mut set_carry = false;
Expand Down
22 changes: 11 additions & 11 deletions src/cpu/execution/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ impl CPU {
* Call and Return
*
*/
pub(in crate::cpu) fn call(&mut self, mmu: &mut MMU, addr: u16) {
self.set_memory_word(mmu, self.registers.sp - 2, self.registers.pc + 3);
self.registers.sp -= 2;
self.registers.pc = addr;
}

pub(in crate::cpu) fn call_cond(&mut self, mmu: &mut MMU, cond: Cond, addr: u16) {
if self.registers.f.evaluate_condition(cond) {
self.call(mmu, addr)
}
}
//pub(in crate::cpu) fn call(&mut self, mmu: &mut MMU, addr: u16) {
// self.set_memory_word(mmu, self.registers.sp - 2, self.registers.pc + 3);
// self.registers.sp -= 2;
// self.registers.pc = addr;
//}
//
//pub(in crate::cpu) fn call_cond(&mut self, mmu: &mut MMU, cond: Cond, addr: u16) {
// if self.registers.f.evaluate_condition(cond) {
// self.call(mmu, addr)
// }
//}

pub(in crate::cpu) fn rst_tgt3(&mut self, mmu: &mut MMU, addr: u16) {
self.set_memory_word(mmu, self.registers.sp - 2, self.registers.pc + 1);
Expand Down
22 changes: 13 additions & 9 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl CPU {
Instruction::CpAR8(reg) => self.cp(self.get_r8_byte(mmu, reg)),
Instruction::SubAImm8(b) => self.sub(b),
Instruction::SubAR8(reg) => self.sub(self.get_r8_byte(mmu, reg)),
Instruction::SbcAR8(r) => self.sbc(self.get_r8_byte(mmu, r)),
Instruction::SbcAImm8(n) => self.sbc(n),

// Bitwise operations, checking and manipulation
Instruction::AndAR8(reg) => self.and(self.get_r8_byte(mmu, reg)),
Expand All @@ -69,7 +71,18 @@ impl CPU {
Instruction::SetB3R8(bit_index, reg) => self.set(mmu, reg, bit_index),
Instruction::ResB3R8(bit_index, reg) => self.res(mmu, reg, bit_index),
Instruction::BitB3R8(bit_index, reg) => self.bit(mmu, reg, bit_index),
Instruction::Ccf => self.ccf(),

// Bit rotation
Instruction::RlR8(reg) => self.rl(mmu, reg),
Instruction::Rla => self.rla(mmu),
Instruction::SrlR8(reg) => self.srl(mmu, reg),
Instruction::SlaR8(reg) => self.sla(mmu, reg),
Instruction::Cpl => self.cpl(),
Instruction::Rlca => self.rlca(),
Instruction::Daa => self.daa(),
Instruction::SwapR8(reg) => self.swap(mmu, reg),
Instruction::Scf => self.scf(),

// Jump instructions
Instruction::JpImm16(addr) => self.jp(addr.wrapping_sub(length)),
Expand All @@ -96,15 +109,6 @@ impl CPU {
Instruction::PushR16stk(reg) => self.push(mmu, self.registers.get_r16_stk(reg)),
Instruction::PopR16stk(reg) => self.pop(mmu, reg),

// Bit rotation
Instruction::RlR8(reg) => self.rl(mmu, reg),
Instruction::Rla => self.rla(mmu),
Instruction::SrlR8(reg) => self.srl(mmu, reg),
Instruction::SlaR8(reg) => self.sla(mmu, reg),
Instruction::Cpl => self.cpl(),
Instruction::Rlca => self.rlca(),
Instruction::Daa => self.daa(),

// Interrupt enable
Instruction::Di => self.ime = false,
Instruction::Ei => {
Expand Down
1 change: 0 additions & 1 deletion src/mmu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl MMU {
// Echo RAM
0xE000..=0xFDFF => {
println!("tried to read echo ram");
dbg!(addr);
unreachable!()
}

Expand Down
21 changes: 20 additions & 1 deletion src/mmu/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl PPU {
0xFF40 => self.lcdc = value,
0xFF41 => self.stat = value,
0xFF42 => self.scy = value,
0xFF43 => self.scx = value,
0xFF43 => self.scx = dbg!(value),
0xFF45 => self.lyc = value,
0xFF47 => self.bgp = value,
0xFF48 => self.obj_palette_0 = value,
Expand Down Expand Up @@ -193,4 +193,23 @@ impl PPU {

ret
}

pub fn get_object(&self, tile_index: u8) -> Tile {
let start_address = 0x8000 + ((tile_index as u16) * 16) as u16;
let mut ret = [[0u8; 8]; 8];

for i in 0..8 {
let byte_a = self.get_byte(start_address + (2 * i));
let byte_b = self.get_byte(start_address + (2 * i) + 1);

for j in 0..8 {
let bit1 = (byte_a >> 7 - j) & 1;
let bit2 = (byte_b >> 7 - j) & 1;

ret[j as usize][i as usize] = ((bit2 << 1) | bit1) as u8;
}
}

ret
}
}
37 changes: 30 additions & 7 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,35 @@ fn palette(id: u8) -> u32 {
}
}

fn render_tile(tile: Tile, buffer: &mut Vec<u32>, x: usize, y: usize) {
fn render_tile(
buffer: &mut Vec<u32>,
tile: Tile,
x: usize,
y: usize,
flags: u8,
transparency: bool,
) {
let x_flip = (flags & 0x20) > 0;
let y_flip = (flags & 0x40) > 0;

for tile_y in 0..8 {
for tile_x in 0..8 {
let x_offset = x + tile_x;
let y_offset = y.wrapping_add(tile_y).wrapping_mul(WIDTH);
let x_offset = if x_flip { (x + 8) - tile_x } else { x + tile_x };

let y_offset = if y_flip {
y.wrapping_add(8).wrapping_sub(tile_y).wrapping_mul(WIDTH)
} else {
y.wrapping_add(tile_y).wrapping_mul(WIDTH)
};

if x_offset >= WIDTH || y.wrapping_add(tile_y) >= HEIGHT {
continue;
}

if transparency && tile[tile_x][tile_y] == 0 {
continue;
}

buffer[y_offset + x_offset] = palette(tile[tile_x][tile_y]);
}
}
Expand Down Expand Up @@ -77,10 +96,12 @@ pub fn window_loop(rx: Receiver<PPU>, tx: Sender<(bool, Key)>, game_title: &Stri
let tile_index = ppu.get_byte(0x9800 + i);

render_tile(
ppu.get_tile(tile_index),
&mut buffer,
(i as usize * 8) % 256,
ppu.get_tile(tile_index),
(ppu.scx as usize).wrapping_add(i as usize * 8) % 256,
((i as usize / 32) * 8).wrapping_sub(ppu.scy as usize),
0x0,
false,
)
}

Expand All @@ -91,13 +112,15 @@ pub fn window_loop(rx: Receiver<PPU>, tx: Sender<(bool, Key)>, game_title: &Stri

let x_position = ppu.get_byte(start_position + 1);
let tile_index = ppu.get_byte(start_position + 2);
//let flags = ppu.get_byte(start_position + 3);
let flags = ppu.get_byte(start_position + 3);

render_tile(
ppu.get_tile(tile_index),
&mut buffer,
ppu.get_object(tile_index),
x_position.wrapping_sub(8) as usize,
y_position.wrapping_sub(16) as usize,
flags,
true,
);
}
}
Expand Down

0 comments on commit 1553486

Please sign in to comment.