some refactoring, fixed bg_color

This commit is contained in:
kirjavascript 2021-05-03 15:16:10 +01:00
parent 5899958116
commit 3ad96f8b48
3 changed files with 39 additions and 16 deletions

View file

@ -131,11 +131,13 @@ fn main() {
// render VRAM
let bg_color = emu.core.mem.vdp.bg_color();
// clear screen
for pixel in screenbuf.chunks_mut(4) {
pixel[0] = cram_rgb[0].0;
pixel[1] = cram_rgb[0].1;
pixel[2] = cram_rgb[0].2;
pixel[0] = bg_color.0;
pixel[1] = bg_color.1;
pixel[2] = bg_color.2;
};
for (i, duxels) in emu.core.mem.vdp.VRAM.chunks(32).enumerate() {

View file

@ -34,7 +34,9 @@ impl Megadrive {
pub fn frame(&mut self) {
// https://segaretro.org/Sega_Mega_Drive/Technical_specifications#Graphics
self.core.mem.vdp.status &= !8; // clear vblank
self.core.mem.vdp.unset_status(vdp::VBLANK_MASK);
self.core.mem.vdp.unset_status(128);
let screen_height = self.core.mem.vdp.screen_height();
let mut hint_counter = self.core.mem.vdp.hint_counter();
@ -51,16 +53,16 @@ impl Megadrive {
}
self.core.mem.vdp.status |= 4;
self.core.mem.vdp.set_status(vdp::HBLANK_MASK);
self.core.execute(636);
self.core.mem.vdp.status &= !4;
self.core.mem.vdp.unset_status(vdp::HBLANK_MASK);
self.core.execute(104);
// render
}
self.core.mem.vdp.status |= 4;
self.core.mem.vdp.set_status(vdp::VBLANK_MASK);
self.core.execute(588);
@ -70,6 +72,7 @@ impl Megadrive {
if self.core.mem.vdp.registers[1] & 0x20 > 0 {
self.core.int_ctrl.request_interrupt(6);
self.core.mem.vdp.set_status(128);
}
self.core.execute(3420-788);

View file

@ -13,6 +13,9 @@ pub struct VDP {
pub dma_pending: bool,
}
pub const VBLANK_MASK: u32 = 8;
pub const HBLANK_MASK: u32 = 4;
pub enum VDPType {
VRAM, CRAM, VSRAM,
}
@ -28,6 +31,14 @@ impl From<u32> for VDPType {
}
}
pub fn cram_to_rgb(color: u16) -> (u8, u8, u8) {
let red = color & 0xf;
let green = (color & 0xf0) >> 4;
let blue = (color & 0xf00) >> 8;
let dupe = |x| (x << 4) | x;
(dupe(red as u8), dupe(green as u8), dupe(blue as u8))
}
impl VDP {
pub fn new() -> Self {
Self {
@ -45,19 +56,19 @@ impl VDP {
pub fn cram_rgb(&self) -> [(u8, u8, u8); 64] {
let mut rgb = [(0, 0, 0); 64];
let dupe = |x| (x << 4) | x;
for (i, color) in self.CRAM.iter().enumerate() {
let red = color & 0xf;
let green = (color & 0xf0) >> 4;
let blue = (color & 0xf00) >> 8;
rgb[i] = (dupe(red as u8), dupe(green as u8), dupe(blue as u8));
rgb[i] = cram_to_rgb(*color);
}
rgb
}
pub fn bg_color(&self) -> (u8, u8, u8) {
let vdp_bg = self.registers[7];
let index = vdp_bg & 0xF;
let line = (vdp_bg >> 4) & 3;
cram_to_rgb(self.CRAM[(index + (line * 0x10)) as usize])
}
pub fn screen_width(&self) -> usize {
if self.registers[12] & 0x01 > 0 { 320 } else { 256 }
}
@ -74,6 +85,14 @@ impl VDP {
self.registers[0x13] as u32 | ((self.registers[0x14] as u32) << 8)
}
pub fn set_status(&mut self, mask: u32) {
self.status |= mask;
}
pub fn unset_status(&mut self, mask: u32) {
self.status &= !mask;
}
pub fn read(&self, mut address: u32) -> u32 {
address &= 0x1F;
@ -84,7 +103,6 @@ impl VDP {
todo!("vdp read {:X}", address);
}
pub fn write(mem: &mut Mem, mut address: u32, value: u32) {
address &= 0x1F;
if address < 0x4 {