move gpu types out

This commit is contained in:
Alex Janka 2023-02-09 12:37:23 +11:00
parent 3d15a1ef6f
commit 6eb791ebf4
2 changed files with 120 additions and 113 deletions

View file

@ -1,122 +1,17 @@
use self::tile_window::TileWindow;
use self::{
tile_window::TileWindow,
types::{
Colour, DrawMode, ObjSize, Object, ObjectFlags, Palette, TiledataArea, TilemapArea, LCDC,
},
};
use crate::{
processor::{as_signed, get_bit, set_bit, set_or_clear_bit, CPU},
processor::{get_bit, set_bit, set_or_clear_bit, CPU},
FACTOR, HEIGHT, WIDTH,
};
use minifb::{Window, WindowOptions};
mod tile_window;
#[derive(PartialEq)]
enum DrawMode {
HBlank,
VBlank,
Mode2,
Mode3,
}
#[derive(Debug)]
enum TilemapArea {
T9800,
T9C00,
}
impl TilemapArea {
fn get_addr(&self, addr: u16) -> u16 {
match self {
TilemapArea::T9800 => 0x9800 + addr,
TilemapArea::T9C00 => 0x9C00 + addr,
}
}
}
#[derive(Debug)]
enum TiledataArea {
D8000,
D9000,
}
impl TiledataArea {
fn get_addr(&self, addr: u8) -> u16 {
match self {
TiledataArea::D8000 => 0x8000 + ((addr as u16) * 16),
TiledataArea::D9000 => 0x9000_u16.wrapping_add_signed((as_signed(addr) as i16) * 16),
}
}
}
#[derive(Debug, PartialEq)]
enum ObjSize {
S8x8,
S8x16,
}
impl ObjSize {
fn get_height(&self) -> u8 {
match self {
ObjSize::S8x8 => 8,
ObjSize::S8x16 => 16,
}
}
}
#[derive(Debug)]
struct LCDC {
enable: bool,
window_tilemap: TilemapArea,
window_enable: bool,
tile_area: TiledataArea,
bg_tilemap: TilemapArea,
obj_size: ObjSize,
obj_enable: bool,
bg_window_enable: bool,
}
#[derive(Clone, Copy, Debug)]
enum Colour {
White,
LightGray,
DarkGray,
Black,
}
impl Colour {
fn to_rgb(&self) -> u32 {
match self {
Colour::White => Self::from_u8_rgb(0xFF, 0xFF, 0xFF),
Colour::LightGray => Self::from_u8_rgb(0xAA, 0xAA, 0xAA),
Colour::DarkGray => Self::from_u8_rgb(0x55, 0x55, 0x55),
Colour::Black => Self::from_u8_rgb(0x00, 0x00, 0x00),
}
}
fn from_u8_rgb(r: u8, g: u8, b: u8) -> u32 {
let (r, g, b) = (r as u32, g as u32, b as u32);
(r << 16) | (g << 8) | b
}
}
#[derive(Clone, Copy)]
struct Palette {
zero: Colour,
one: Colour,
two: Colour,
three: Colour,
}
struct ObjectFlags {
behind_bg_and_window: bool,
y_flip: bool,
x_flip: bool,
palette: Palette,
}
struct Object {
x: u8,
y: u8,
tile_index: u8,
flags: ObjectFlags,
}
mod types;
const TILE_WINDOW_WIDTH: usize = 16 * 8;
const TILE_WINDOW_HEIGHT: usize = 24 * 8;

112
src/processor/gpu/types.rs Normal file
View file

@ -0,0 +1,112 @@
use crate::processor::as_signed;
#[derive(PartialEq)]
pub(super) enum DrawMode {
HBlank,
VBlank,
Mode2,
Mode3,
}
#[derive(Debug)]
pub(super) enum TilemapArea {
T9800,
T9C00,
}
impl TilemapArea {
pub(super) fn get_addr(&self, addr: u16) -> u16 {
match self {
TilemapArea::T9800 => 0x9800 + addr,
TilemapArea::T9C00 => 0x9C00 + addr,
}
}
}
#[derive(Debug)]
pub(super) enum TiledataArea {
D8000,
D9000,
}
impl TiledataArea {
pub(super) fn get_addr(&self, addr: u8) -> u16 {
match self {
TiledataArea::D8000 => 0x8000 + ((addr as u16) * 16),
TiledataArea::D9000 => 0x9000_u16.wrapping_add_signed((as_signed(addr) as i16) * 16),
}
}
}
#[derive(Debug, PartialEq)]
pub(super) enum ObjSize {
S8x8,
S8x16,
}
impl ObjSize {
pub(super) fn get_height(&self) -> u8 {
match self {
ObjSize::S8x8 => 8,
ObjSize::S8x16 => 16,
}
}
}
#[derive(Debug)]
pub(super) struct LCDC {
pub(super) enable: bool,
pub(super) window_tilemap: TilemapArea,
pub(super) window_enable: bool,
pub(super) tile_area: TiledataArea,
pub(super) bg_tilemap: TilemapArea,
pub(super) obj_size: ObjSize,
pub(super) obj_enable: bool,
pub(super) bg_window_enable: bool,
}
#[derive(Clone, Copy, Debug)]
pub(super) enum Colour {
White,
LightGray,
DarkGray,
Black,
}
impl Colour {
pub(super) fn to_rgb(&self) -> u32 {
match self {
Colour::White => Self::from_u8_rgb(0xFF, 0xFF, 0xFF),
Colour::LightGray => Self::from_u8_rgb(0xAA, 0xAA, 0xAA),
Colour::DarkGray => Self::from_u8_rgb(0x55, 0x55, 0x55),
Colour::Black => Self::from_u8_rgb(0x00, 0x00, 0x00),
}
}
pub(super) fn from_u8_rgb(r: u8, g: u8, b: u8) -> u32 {
let (r, g, b) = (r as u32, g as u32, b as u32);
(r << 16) | (g << 8) | b
}
}
#[derive(Clone, Copy)]
pub(super) struct Palette {
pub(super) zero: Colour,
pub(super) one: Colour,
pub(super) two: Colour,
pub(super) three: Colour,
}
pub(super) struct ObjectFlags {
pub(super) behind_bg_and_window: bool,
pub(super) y_flip: bool,
pub(super) x_flip: bool,
pub(super) palette: Palette,
}
pub(super) struct Object {
pub(super) x: u8,
pub(super) y: u8,
pub(super) tile_index: u8,
pub(super) flags: ObjectFlags,
}