Skip to content

Commit

Permalink
More GPU boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 10, 2023
1 parent 78db047 commit eebb5b6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::registers::{Registers, Flags};

pub struct CPU {
reg: Registers,
mem: MMU,
pub mem: MMU,
halted: bool,
// Enabled Interrupts
ei: bool
Expand All @@ -14,7 +14,7 @@ impl CPU {
pub fn new(mode: GBMode, rom: Vec<u8>) -> Self {
Self {
reg: Registers::new(mode),
mem: MMU::new(rom),
mem: MMU::new(mode, rom),
halted: false,
ei: true
}
Expand Down
47 changes: 44 additions & 3 deletions src/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bitflags::{bitflags, Flags};
use crate::mode::GBMode;

pub const SCREEN_W: usize = 160;
pub const SCREEN_H: usize = 144;

pub struct GPU {
mode: GBMode,
sy: u8,
sx: u8,
ly: u8,
Expand All @@ -12,7 +14,8 @@ pub struct GPU {
wx: u8,
lcdc: LCDC,
lcds: LCDS,
ram: [u8; 0x4000]
ram: [u8; 0x4000],
frame_buffer: [[[u8; 3]; SCREEN_W]; SCREEN_H]
}

bitflags! {
Expand Down Expand Up @@ -52,8 +55,9 @@ bitflags! {
}
}
impl GPU {
pub fn new() -> Self {
pub fn new(mode: GBMode) -> Self {
Self {
mode,
sy: 0,
sx: 0,
ly: 0,
Expand All @@ -62,10 +66,47 @@ impl GPU {
wx: 0,
lcdc: LCDC::empty(),
lcds: LCDS::empty(),
ram: [0; 0x4000]
ram: [0; 0x4000],
frame_buffer: [[[0; 3]; SCREEN_W]; SCREEN_H]
}
}

pub fn cycle(&mut self) {
self.draw_bg();
}

fn grey_to_l(v: u8, i: usize) -> u8 {
match v >> (2 * i) & 0x03 {
0x00 => 0xFF,
0x01 => 0xFC,
0x02 => 0x60,
_ => 0x00
}
}

fn set_rgb(&mut self, x: usize, r: u8, g: u8, b: u8) {
// TODO: Color mapping from CGB -> sRGB
self.frame_buffer[self.ly as usize][x] = [r, g, b];
}

fn draw_bg(&mut self) {
for x in 0..SCREEN_W {
if self.mode == GBMode::Color {
let r = 0;
let g = 0;
let b = 0;
self.set_rgb(x, r, g, b);
} else {
let lightness = Self::grey_to_l(0, 0);
self.set_rgb(x, lightness, lightness, lightness);
}
}
}

fn draw_sprites(&mut self) {

}

pub fn read(&self, a: u16) -> u8 {
match a {
0xFF40 => self.lcdc.bits(),
Expand Down
22 changes: 12 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,29 @@ async fn main() -> Result<(), impl std::error::Error> {
let game_name = std::str::from_utf8(&name_data[0..index]).expect("Failed to get game name!");
println!("Starting \"{game_name}\"...");

let event_loop = EventLoop::new().unwrap();

let window = WindowBuilder::new()
.with_title(format!("gb-rs - {:}", game_name))
.with_inner_size(winit::dpi::LogicalSize::new(160, 144))
.build(&event_loop)
.unwrap();

let mut context = Context::new(window).await;

let buffer_copy = buffer.clone();
// Start CPU
tokio::spawn(async move {
let mut cpu = CPU::new(GBMode::Classic, buffer_copy);

while true {
let cycles = cpu.cycle();
cpu.mem.gpu.cycle();

sleep(Duration::from_millis((1000_f64 / 4_194_304_f64 * cycles as f64) as u64)).await;
}
});

let event_loop = EventLoop::new().unwrap();

let window = WindowBuilder::new()
.with_title(format!("gb-rs - {:}", game_name))
.with_inner_size(winit::dpi::LogicalSize::new(160, 144))
.build(&event_loop)
.unwrap();

let mut context = Context::new(window).await;

let mut modifiers = ModifiersState::default();

event_loop.run(move |event, elwt| {
Expand Down
7 changes: 4 additions & 3 deletions src/mmu.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use crate::gpu::GPU;
use crate::mode::GBMode;

pub struct MMU {
rom: Vec<u8>,
gpu: GPU,
pub gpu: GPU,
wram: [u8; 0x8000],
hram: [u8; 0x7F],
interrupt: u8,
wram_bank: usize,
}

impl MMU {
pub fn new(rom: Vec<u8>) -> Self {
pub fn new(mode: GBMode, rom: Vec<u8>) -> Self {
Self {
rom,
gpu: GPU::new(),
gpu: GPU::new(mode),
wram: [0; 0x8000],
hram: [0; 0x7f],
interrupt: 0,
Expand Down
1 change: 1 addition & 0 deletions src/mode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Clone, Copy, PartialEq)]
pub enum GBMode {
Classic,
Color,
Expand Down

0 comments on commit eebb5b6

Please sign in to comment.