Skip to content

Fail under 60% code coverage #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/basic-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
uses: dtolnay/rust-toolchain@stable

- name: Build
env:
RUSTFLAGS: -D warnings
run: cargo build

- name: Test
Expand Down Expand Up @@ -62,4 +64,4 @@ jobs:
with:
packages: libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libsdl2-gfx-dev gcc
- run: if ls ~/.cargo/bin/ | grep -q "^cargo-tarpaulin" ; then echo "cargo-tarpaulin is already installed"; else cargo install cargo-tarpaulin; fi
- run: cargo tarpaulin --ignore-tests
- run: cargo tarpaulin --fail-under 60
1 change: 0 additions & 1 deletion emulator/src/cartridge/cartridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::cartridge::formats::i_nes::Ines;
use crate::cartridge::formats::nes_2::Nes2;
use crate::cartridge::registers::chr_rom::ChrRom;
use crate::cartridge::registers::prg_rom::PrgRom;
use byteorder::ReadBytesExt;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::Path;
Expand Down
43 changes: 41 additions & 2 deletions emulator/src/cartridge/common/enums.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
#[derive(Debug, PartialEq)]
use std::fmt::Debug;
pub enum Nes {
Ines,
Nes2,
}

#[derive(Debug, PartialEq)]
impl Debug for Nes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Nes::Ines => write!(f, "Ines"),
Nes::Nes2 => write!(f, "Nes2"),
}
}
}

impl PartialEq for Nes {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(Nes::Ines, Nes::Ines) | (Nes::Nes2, Nes::Nes2)
)
}
}
pub enum Mirroring {
Horizontal,
Vertical,
SingleScreen,
FourScreen,
}

impl Debug for Mirroring {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mirroring::Horizontal => write!(f, "Mirroring::Horizontal"),
Mirroring::Vertical => write!(f, "Mirroring::Vertical"),
Mirroring::SingleScreen => write!(f, "Mirroring::SingleScreen"),
Mirroring::FourScreen => write!(f, "Mirroring::FourScreen"),
}
}
}

impl PartialEq for Mirroring {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(Mirroring::Horizontal, Mirroring::Horizontal)
| (Mirroring::Vertical, Mirroring::Vertical)
| (Mirroring::SingleScreen, Mirroring::SingleScreen)
| (Mirroring::FourScreen, Mirroring::FourScreen)
)
}
}
9 changes: 0 additions & 9 deletions emulator/src/cartridge/file_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ pub fn read_banks<R: Read>(
Ok(banks)
}

fn read_check_is_valid_nes_file<R: Read>(file: &mut R) -> anyhow::Result<()> {
let mut magic_bytes = [0; 4];
file.read_exact(&mut magic_bytes)?;
if (magic_bytes) != NES_FILE_MAGIC_BYTES {
return Err(anyhow::Error::new(NesRomReadError::FileFormatNotSupported));
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
37 changes: 35 additions & 2 deletions emulator/src/cartridge/formats/i_nes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

use std::fmt::Debug;

// Bytes Description
// 0-3 Constant $4E $45 $53 $1A (ASCII "NES" followed by MS-DOS end-of-file)
// 4 Size of PRG ROM in 16 KB units
Expand All @@ -20,7 +22,6 @@ use std::path::Path;
// 9 Flags 9 – TV system (rarely used extension)
// 10 Flags 10 – TV system, PRG-RAM presence (unofficial, rarely used extension)
// 11-15 Unused padding (should be filled with zero, but some rippers put their name across bytes 7-15)
#[derive(Debug)]
struct InesHeader {
prg_rom_size: u8,
chr_rom_size: u8,
Expand All @@ -32,14 +33,28 @@ struct InesHeader {
zero: [u8; 5],
}

impl Debug for InesHeader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InesHeader")
.field("prg_rom_size", &self.prg_rom_size)
.field("chr_rom_size", &self.chr_rom_size)
.field("flags_6", &self.flags_6)
.field("flags_7", &self.flags_7)
.field("prg_ram_size", &self.prg_ram_size)
.field("flags_9", &self.flags_9)
.field("flags_10", &self.flags_10)
.field("zero", &self.zero)
.finish()
}
}

// Header (16 bytes)
// Trainer, if present (0 or 512 bytes)
// PRG ROM data (16384 * x bytes)
// CHR ROM data, if present (8192 * y bytes)
// PlayChoice INST-ROM, if present (0 or 8192 bytes)
// PlayChoice PROM, if present (16 bytes Data, 16 bytes CounterOut) (this is often missing; see PC10 ROM-Images for details)
// Some ROM-Images additionally contain a 128-byte (or sometimes 127-byte) title at the end of the file.
#[derive(Debug)]
pub struct Ines {
header: InesHeader,
trainer: Option<[u8; 512]>,
Expand All @@ -54,6 +69,24 @@ pub struct Ines {
title: Option<[u8; 128]>,
}

impl Debug for Ines {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Ines")
.field("header", &self.header)
.field("trainer", &self.trainer)
.field("mirroring", &self.mirroring)
.field("battery", &self.battery)
.field("four_screen_vram", &self.four_screen_vram)
.field("prg_rom", &self.prg_rom)
.field("chr_rom", &self.chr_rom)
.field("mapper", &self.mapper)
.field("play_choice_inst_rom", &self.play_choice_inst_rom)
.field("play_choice_10", &self.play_choice_10)
.field("title", &self.title)
.finish()
}
}

impl Ines {
fn header_from_file<R: Read>(file: &mut R) -> anyhow::Result<InesHeader> {
let mut header = [0; 16];
Expand Down
16 changes: 15 additions & 1 deletion emulator/src/cartridge/formats/nes_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::{BufReader, Read};
use std::path::Path;

struct Nes2Header {}

#[allow(dead_code)]
pub struct Nes2 {
header: Nes2Header,
}
Expand Down Expand Up @@ -46,3 +46,17 @@ impl FileLoadable for Nes2 {
Ok(Nes2 { header })
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_header_from_file() {
let data = [
'N' as u8, 'E' as u8, 'S' as u8, 0x1A, 0, 0, 0, 0x08, 0, 0, 0, 0, 0, 0, 0, 0,
];
let mut cursor = std::io::Cursor::new(data);
let header = Nes2::header_from_file(&mut cursor).unwrap();
}
}
5 changes: 3 additions & 2 deletions emulator/src/cpu/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::bus::BusLike;
use crate::cpu::micro_instructions::{MicroInstruction, MicroInstructionSequence};
use crate::cpu::operations::Operation;
use crate::cpu::registers::Registers;

#[allow(dead_code)]
pub struct CPU<T: BusLike> {
bus: T,
registers: Registers,
Expand All @@ -28,7 +28,7 @@ pub enum CPUState {
Fetching,
Execution,
}

#[allow(dead_code)]
impl<T: BusLike> CPU<T> {
fn new(bus: T) -> Self {
let registers = Registers::new();
Expand Down Expand Up @@ -168,6 +168,7 @@ impl CPUFlag {

#[cfg(test)]
mod tests {
use crate::cpu::operations::Operation;
use std::collections::btree_map::Values;

use crate::bus;
Expand Down
3 changes: 2 additions & 1 deletion emulator/src/cpu/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::cpu::cpu::CPUFlag;
use crate::cpu::micro_instructions::MicroInstructionSequence;
use crate::cpu::operations::Operation;

#[allow(dead_code)]
pub struct Registers {
pub x: u8,
pub y: u8,
Expand Down Expand Up @@ -93,7 +94,7 @@ impl Registers {
pub fn read_operation_code<T: BusLike>(&mut self, bus: &mut T) {
self.operation = bus.read(self.program_counter as u16);
}

#[allow(unused_variables)]
pub fn decode_operation<T: BusLike>(&mut self, bus: &T) {
let operation_code = self.operation;
println!("Operation code: {:#X}", operation_code);
Expand Down
1 change: 1 addition & 0 deletions emulator/src/ppu/vram/vram.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::addressing::Addressable;
use crate::mirroring::Mirroring;
use log::{debug, info};
#[allow(unused_imports)]
use std::cmp::PartialEq;
use std::fmt::Debug;

Expand Down
Loading