Refactor core functionality into a separate module

Former-commit-id: 5d55b9eb0b63ed7c61465b4e814782165caa5002
This commit is contained in:
Michel Heily 2019-07-20 16:46:00 +03:00
parent 0f73abaf98
commit 53115a9a58
29 changed files with 97 additions and 95 deletions

View file

@ -5,11 +5,11 @@ use clap::{App, ArgMatches};
extern crate rustboyadvance_ng; extern crate rustboyadvance_ng;
use rustboyadvance_ng::arm7tdmi::Core; use rustboyadvance_ng::core::arm7tdmi::Core;
use rustboyadvance_ng::cartridge::Cartridge; use rustboyadvance_ng::core::cartridge::Cartridge;
use rustboyadvance_ng::debugger::Debugger; use rustboyadvance_ng::core::{GBAResult, GameBoyAdvance};
use rustboyadvance_ng::util::read_bin_file; use rustboyadvance_ng::util::read_bin_file;
use rustboyadvance_ng::{GBAResult, GameBoyAdvance}; use rustboyadvance_ng::debugger::Debugger;
fn run_debug(matches: &ArgMatches) -> GBAResult<()> { fn run_debug(matches: &ArgMatches) -> GBAResult<()> {
let skip_bios = match matches.occurrences_of("skip_bios") { let skip_bios = match matches.occurrences_of("skip_bios") {

View file

@ -3,7 +3,7 @@ use std::fmt;
use crate::bit::BitIndex; use crate::bit::BitIndex;
use super::{AluOpCode, ArmCond, ArmFormat, ArmHalfwordTransferType, ArmInstruction}; use super::{AluOpCode, ArmCond, ArmFormat, ArmHalfwordTransferType, ArmInstruction};
use crate::arm7tdmi::{ use crate::core::arm7tdmi::{
psr::RegPSR, reg_string, Addr, BarrelShiftOpCode, BarrelShifterValue, ShiftedRegister, REG_PC, psr::RegPSR, reg_string, Addr, BarrelShiftOpCode, BarrelShifterValue, ShiftedRegister, REG_PC,
}; };

View file

@ -1,11 +1,13 @@
use crate::bit::BitIndex; use crate::bit::BitIndex;
use crate::arm7tdmi::alu::*; use crate::core::arm7tdmi::alu::*;
use crate::arm7tdmi::bus::Bus; use crate::core::arm7tdmi::bus::Bus;
use crate::arm7tdmi::cpu::{Core, CpuExecResult}; use crate::core::arm7tdmi::cpu::{Core, CpuExecResult};
use crate::arm7tdmi::exception::Exception; use crate::core::arm7tdmi::exception::Exception;
use crate::arm7tdmi::psr::RegPSR; use crate::core::arm7tdmi::psr::RegPSR;
use crate::arm7tdmi::{Addr, CpuError, CpuMode, CpuResult, CpuState, DecodedInstruction, REG_PC}; use crate::core::arm7tdmi::{
Addr, CpuError, CpuMode, CpuResult, CpuState, DecodedInstruction, REG_PC,
};
use super::*; use super::*;

View file

@ -2,7 +2,7 @@ pub mod display;
pub mod exec; pub mod exec;
use super::alu::*; use super::alu::*;
use crate::arm7tdmi::{Addr, InstructionDecoder, InstructionDecoderError}; use crate::core::arm7tdmi::{Addr, InstructionDecoder, InstructionDecoderError};
use crate::bit::BitIndex; use crate::bit::BitIndex;
use crate::byteorder::{LittleEndian, ReadBytesExt}; use crate::byteorder::{LittleEndian, ReadBytesExt};
@ -351,7 +351,7 @@ impl ArmInstruction {
/// All instructions constants were generated using an ARM assembler. /// All instructions constants were generated using an ARM assembler.
mod tests { mod tests {
use super::*; use super::*;
use crate::arm7tdmi::*; use crate::core::arm7tdmi::*;
use crate::sysbus::BoxedMemory; use crate::sysbus::BoxedMemory;
#[test] #[test]

View file

@ -3,7 +3,7 @@ use std::fmt;
use crate::bit::BitIndex; use crate::bit::BitIndex;
use super::*; use super::*;
use crate::arm7tdmi::*; use crate::core::arm7tdmi::*;
impl ThumbInstruction { impl ThumbInstruction {
fn fmt_thumb_move_shifted_reg(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt_thumb_move_shifted_reg(&self, f: &mut fmt::Formatter) -> fmt::Result {

View file

@ -1,6 +1,6 @@
use crate::arm7tdmi::bus::Bus; use crate::core::arm7tdmi::bus::Bus;
use crate::arm7tdmi::cpu::{Core, CpuExecResult}; use crate::core::arm7tdmi::cpu::{Core, CpuExecResult};
use crate::arm7tdmi::*; use crate::core::arm7tdmi::*;
use super::*; use super::*;
fn push(cpu: &mut Core, bus: &mut Bus, r: usize) { fn push(cpu: &mut Core, bus: &mut Bus, r: usize) {

View file

@ -348,7 +348,7 @@ impl ThumbInstruction {
/// All instructions constants were generated using an ARM assembler. /// All instructions constants were generated using an ARM assembler.
mod tests { mod tests {
use super::*; use super::*;
use crate::arm7tdmi::{Core, Bus}; use crate::core::arm7tdmi::{Bus, Core};
use crate::sysbus::BoxedMemory; use crate::sysbus::BoxedMemory;
#[test] #[test]

View file

@ -2,13 +2,12 @@ use std::str::from_utf8;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use crate::arm7tdmi::{ use super::arm7tdmi::{
bus::{Bus, MemoryAccess, MemoryAccessWidth}, bus::{Bus, MemoryAccess, MemoryAccessWidth},
Addr, Addr,
}; };
use crate::sysbus::WaitState; use super::sysbus::WaitState;
use crate::util::read_bin_file; use crate::util::read_bin_file;
use crate::GBAError;
/// From GBATEK /// From GBATEK
/// ///
@ -79,7 +78,7 @@ pub struct Cartridge {
impl Cartridge { impl Cartridge {
const MIN_SIZE: usize = 4 * 1024 * 1024; const MIN_SIZE: usize = 4 * 1024 * 1024;
pub fn load(path: &str) -> Result<Cartridge, GBAError> { pub fn load(path: &str) -> Result<Cartridge, ::std::io::Error> {
let mut rom_bin = read_bin_file(path)?; let mut rom_bin = read_bin_file(path)?;
if rom_bin.len() < Cartridge::MIN_SIZE { if rom_bin.len() < Cartridge::MIN_SIZE {
rom_bin.resize_with(Cartridge::MIN_SIZE, Default::default); rom_bin.resize_with(Cartridge::MIN_SIZE, Default::default);

View file

@ -7,8 +7,8 @@ use super::gpu::*;
use super::interrupt::*; use super::interrupt::*;
use super::ioregs::consts::*; use super::ioregs::consts::*;
use super::sysbus::SysBus; use super::sysbus::SysBus;
use super::EmuIoDev;
use super::{EmuIoDev, GBAError, GBAResult}; use super::{GBAError, GBAResult};
pub struct GameBoyAdvance { pub struct GameBoyAdvance {
pub cpu: Core, pub cpu: Core,

View file

@ -145,7 +145,7 @@ use GpuState::*;
pub struct Gpu { pub struct Gpu {
cycles: usize, cycles: usize,
pub pixeldata: [Rgb15; 256 * 256], pub pixeldata: [Rgb15; 512 * 512],
pub state: GpuState, pub state: GpuState,
pub current_scanline: usize, // VCOUNT pub current_scanline: usize, // VCOUNT
} }
@ -168,7 +168,7 @@ impl Gpu {
state: HDraw, state: HDraw,
current_scanline: 0, current_scanline: 0,
cycles: 0, cycles: 0,
pixeldata: [Rgb15::from(0); 256 * 256], pixeldata: [Rgb15::from(0); 512 * 512],
} }
} }
@ -300,7 +300,7 @@ impl Gpu {
self.get_palette_color(sysbus, index as u32, 0) self.get_palette_color(sysbus, index as u32, 0)
} }
}; };
self.pixeldata[((px + tile_x) as usize) + py * 256] = color; self.pixeldata[((px + tile_x) as usize) + py * 512] = color;
} }
px += 8; px += 8;
if px == bgcnt.screen_width as u32 { if px == bgcnt.screen_width as u32 {
@ -313,7 +313,7 @@ impl Gpu {
let page: u32 = match dispcnt.display_frame { let page: u32 = match dispcnt.display_frame {
0 => 0x0600_0000, 0 => 0x0600_0000,
1 => 0x0600_a000, 1 => 0x0600_a000,
_ => unreachable!() _ => unreachable!(),
}; };
let y = self.current_scanline; let y = self.current_scanline;
@ -322,7 +322,7 @@ impl Gpu {
let bitmap_index = x + y * Self::DISPLAY_WIDTH; let bitmap_index = x + y * Self::DISPLAY_WIDTH;
let bitmap_addr = page + (bitmap_index as u32); let bitmap_addr = page + (bitmap_index as u32);
let index = sysbus.read_8(bitmap_addr as Addr) as u32; let index = sysbus.read_8(bitmap_addr as Addr) as u32;
self.pixeldata[x + y * 256] = self.get_palette_color(sysbus, index, 0); self.pixeldata[x + y * 512] = self.get_palette_color(sysbus, index, 0);
} }
} }

View file

@ -2,7 +2,7 @@ use std::io;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use crate::arm7tdmi::{Addr, Bus, MemoryAccess}; use crate::core::arm7tdmi::{Addr, Bus, MemoryAccess};
pub mod consts { pub mod consts {
use super::*; use super::*;

45
src/core/mod.rs Normal file
View file

@ -0,0 +1,45 @@
pub mod arm7tdmi;
pub mod cartridge;
pub mod gpu;
pub mod sysbus;
pub use sysbus::SysBus;
pub mod interrupt;
pub mod ioregs;
pub use interrupt::Interrupt;
pub mod gba;
pub use gba::GameBoyAdvance;
pub mod dma;
pub mod palette;
use crate::debugger;
pub trait EmuIoDev {
fn step(&mut self, cycles: usize, sysbus: &mut SysBus) -> (usize, Option<Interrupt>);
}
#[derive(Debug)]
pub enum GBAError {
IO(::std::io::Error),
CpuError(arm7tdmi::CpuError),
DebuggerError(debugger::DebuggerError),
}
pub type GBAResult<T> = Result<T, GBAError>;
impl From<::std::io::Error> for GBAError {
fn from(err: ::std::io::Error) -> GBAError {
GBAError::IO(err)
}
}
impl From<arm7tdmi::CpuError> for GBAError {
fn from(err: arm7tdmi::CpuError) -> GBAError {
GBAError::CpuError(err)
}
}
impl From<debugger::DebuggerError> for GBAError {
fn from(err: debugger::DebuggerError) -> GBAError {
GBAError::DebuggerError(err)
}
}

View file

@ -1,9 +1,11 @@
use crate::arm7tdmi::bus::Bus; use crate::core::arm7tdmi::arm::ArmInstruction;
use crate::arm7tdmi::{Addr, CpuState}; use crate::core::arm7tdmi::bus::Bus;
use crate::core::arm7tdmi::thumb::ThumbInstruction;
use crate::core::arm7tdmi::{Addr, CpuState};
use crate::core::gpu::*;
use crate::core::ioregs::consts::*;
use crate::core::GBAError;
use crate::disass::Disassembler; use crate::disass::Disassembler;
use crate::ioregs::consts::*;
use crate::gpu::*;
use crate::GBAError;
use super::palette_view::create_palette_view; use super::palette_view::create_palette_view;
use super::render_view::create_render_view; use super::render_view::create_render_view;
@ -137,9 +139,6 @@ impl Command {
hexdump::hexdump(&bytes[0..nbytes]); hexdump::hexdump(&bytes[0..nbytes]);
} }
Disass(mode, addr, n) => { Disass(mode, addr, n) => {
use crate::arm7tdmi::arm::ArmInstruction;
use crate::arm7tdmi::thumb::ThumbInstruction;
let bytes = debugger.gba.sysbus.get_bytes(addr); let bytes = debugger.gba.sysbus.get_bytes(addr);
match mode { match mode {
DisassMode::ModeArm => { DisassMode::ModeArm => {

View file

@ -3,8 +3,8 @@ use rustyline::Editor;
use colored::*; use colored::*;
use super::arm7tdmi::{Addr, Bus, CpuError}; use super::core::arm7tdmi::{Addr, Bus, CpuError};
use super::GameBoyAdvance; use super::core::GameBoyAdvance;
mod parser; mod parser;
use parser::{parse_expr, DerefType, Expr, Value}; use parser::{parse_expr, DerefType, Expr, Value};

View file

@ -3,7 +3,7 @@ use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::Canvas; use sdl2::render::Canvas;
use super::super::palette::{Palette, Rgb15}; use crate::core::palette::{Palette, Rgb15};
const PALETTE_RECT_WIDTH: u32 = 20; const PALETTE_RECT_WIDTH: u32 = 20;

View file

@ -4,8 +4,8 @@ use sdl2::event::Event;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::Point; use sdl2::rect::Point;
use crate::gba::GameBoyAdvance; use crate::core::gba::GameBoyAdvance;
use crate::gpu::Gpu; use crate::core::gpu::Gpu;
const SCREEN_WIDTH: u32 = Gpu::DISPLAY_WIDTH as u32; const SCREEN_WIDTH: u32 = Gpu::DISPLAY_WIDTH as u32;
const SCREEN_HEIGHT: u32 = Gpu::DISPLAY_HEIGHT as u32; const SCREEN_HEIGHT: u32 = Gpu::DISPLAY_HEIGHT as u32;
@ -39,7 +39,7 @@ pub fn create_render_view(gba: &GameBoyAdvance) {
for y in 0..Gpu::DISPLAY_HEIGHT { for y in 0..Gpu::DISPLAY_HEIGHT {
for x in 0..Gpu::DISPLAY_WIDTH { for x in 0..Gpu::DISPLAY_WIDTH {
let index = (x as usize) + (y as usize) * (256 as usize); let index = (x as usize) + (y as usize) * (512 as usize);
let color = gba.gpu.pixeldata[index]; let color = gba.gpu.pixeldata[index];
let rgb24: Color = color.into(); let rgb24: Color = color.into();
canvas.set_draw_color(rgb24); canvas.set_draw_color(rgb24);

View file

@ -5,12 +5,12 @@ use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::Canvas; use sdl2::render::Canvas;
use crate::arm7tdmi::bus::Bus; use crate::core::arm7tdmi::bus::Bus;
use crate::gba::GameBoyAdvance; use crate::core::gba::GameBoyAdvance;
use crate::ioregs::consts::*; use crate::core::gpu::*;
use crate::gpu::*; use crate::core::ioregs::consts::*;
use crate::palette::*; use crate::core::palette::*;
use crate::sysbus::SysBus; use crate::core::sysbus::SysBus;
impl Into<Color> for Rgb15 { impl Into<Color> for Rgb15 {
fn into(self) -> Color { fn into(self) -> Color {
@ -100,7 +100,6 @@ pub fn create_tile_view(bg: u32, gba: &GameBoyAdvance) {
} }
} }
canvas.set_draw_color(Color::RGB(00, 00, 00)); canvas.set_draw_color(Color::RGB(00, 00, 00));
canvas.clear(); canvas.clear();

View file

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use super::arm7tdmi::{Addr, InstructionDecoder, InstructionDecoderError}; use super::core::arm7tdmi::{Addr, InstructionDecoder, InstructionDecoderError};
use std::io::ErrorKind; use std::io::ErrorKind;
pub struct Disassembler<'a, D> pub struct Disassembler<'a, D>

View file

@ -14,49 +14,7 @@ extern crate nom;
extern crate ansi_term; extern crate ansi_term;
extern crate colored; // not needed in Rust 2018 extern crate colored; // not needed in Rust 2018
pub mod arm7tdmi; pub mod core;
pub mod cartridge;
pub mod debugger; pub mod debugger;
pub mod disass; pub mod disass;
pub mod gpu;
pub mod sysbus;
pub use sysbus::SysBus;
pub mod interrupt;
pub mod ioregs;
pub use interrupt::Interrupt;
pub mod gba;
pub use gba::GameBoyAdvance;
pub mod dma;
pub mod palette;
pub mod util; pub mod util;
pub trait EmuIoDev {
fn step(&mut self, cycles: usize, sysbus: &mut SysBus) -> (usize, Option<Interrupt>);
}
#[derive(Debug)]
pub enum GBAError {
IO(::std::io::Error),
CpuError(arm7tdmi::CpuError),
DebuggerError(debugger::DebuggerError),
}
pub type GBAResult<T> = Result<T, GBAError>;
impl From<::std::io::Error> for GBAError {
fn from(err: ::std::io::Error) -> GBAError {
GBAError::IO(err)
}
}
impl From<arm7tdmi::CpuError> for GBAError {
fn from(err: arm7tdmi::CpuError) -> GBAError {
GBAError::CpuError(err)
}
}
impl From<debugger::DebuggerError> for GBAError {
fn from(err: debugger::DebuggerError) -> GBAError {
GBAError::DebuggerError(err)
}
}