Skip to content

Commit

Permalink
feat: switch from chrono to time
Browse files Browse the repository at this point in the history
  • Loading branch information
EliseZeroTwo committed Jan 2, 2024
1 parent 984ead0 commit 654ff62
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 148 deletions.
88 changes: 34 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
resolver = "2"
members = ["meowgb", "meowgb-core", "meowgb-opcode", "meowgb-tests"]
default-members = ["meowgb"]
1 change: 0 additions & 1 deletion meowgb-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ log = "0.4.14"
paste = "1.0.6"
meowgb-opcode = { path = "../meowgb-opcode" }
bmp = "0.5.0"
chrono = "0.4.19"
thiserror = "1.0.30"
sha1 = { version = "0.10.6", features = ["std"] }
10 changes: 6 additions & 4 deletions meowgb-core/src/gameboy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod serial;
mod sound;
mod timer;

use std::time::{Duration, Instant};

use interrupts::Interrupts;
use joypad::Joypad;
use mapper::Mapper;
Expand Down Expand Up @@ -206,7 +208,7 @@ impl<S: SerialWriter> Gameboy<S> {
log::info!("\n-- Registers --\nAF: {:04X}\nBC: {:04X}\nDE: {:04X}\nHL: {:04X}\nSP: {:04X}\nPC: {:04X}\nZero: {}\nSubtract: {}\nHalf-Carry: {}\nCarry: {}\n-- Interrupts --\nIME: {}\nIE VBlank: {}\nIE LCD Stat: {}\nIE Timer: {}\nIE Serial: {}\nIE Joypad: {}\nIF VBlank: {}\nIF LCD Stat: {}\nIF Timer: {}\nIF Serial: {}\nIF Joypad: {}\n", self.registers.get_af(), self.registers.get_bc(), self.registers.get_de(), self.registers.get_hl(), self.registers.get_sp(), self.registers.pc, self.registers.get_zero(), self.registers.get_subtract(), self.registers.get_half_carry(), self.registers.get_carry(), self.interrupts.ime, self.interrupts.read_ie_vblank(), self.interrupts.read_ie_lcd_stat(), self.interrupts.read_ie_timer(), self.interrupts.read_ie_serial(), self.interrupts.read_ie_joypad(), self.interrupts.read_if_vblank(), self.interrupts.read_if_lcd_stat(), self.interrupts.read_if_timer(), self.interrupts.read_if_serial(), self.interrupts.read_if_joypad());
}

pub fn tick_4(&mut self) -> (bool, Option<i64>) {
pub fn tick_4(&mut self) -> (bool, Option<Duration>) {
let mut request_redraw = false;
let mut debug_time = None;
for _ in 0..4 {
Expand All @@ -221,7 +223,7 @@ impl<S: SerialWriter> Gameboy<S> {
(request_redraw, debug_time)
}

pub fn tick(&mut self) -> (bool, Option<i64>) {
pub fn tick(&mut self) -> (bool, Option<Duration>) {
if self.tick_count == 0 {
if self.breakpoints[self.registers.pc as usize] && !self.single_step {
self.single_step = true;
Expand All @@ -231,7 +233,7 @@ impl<S: SerialWriter> Gameboy<S> {
let mut diff = None;

if self.trigger_bp || (self.single_step && self.registers.cycle == 0) {
let entered_step = chrono::Utc::now();
let entered_step = Instant::now();
self.trigger_bp = false;
self.single_step = true;
let mut input = String::new();
Expand Down Expand Up @@ -389,7 +391,7 @@ impl<S: SerialWriter> Gameboy<S> {
Err(stdin_err) => panic!("Failed to lock stdin: {:?}", stdin_err),
}

diff = Some((chrono::Utc::now() - entered_step).num_milliseconds());
diff = Some(entered_step.elapsed());
if exit {
return (false, diff);
}
Expand Down
66 changes: 33 additions & 33 deletions meowgb-core/src/gameboy/bootrom.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
use std::{path::Path, io::Read};
use std::{io::Read, path::Path};

use sha1::{Digest, Sha1};

#[derive(Debug, thiserror::Error)]
pub enum BootromParseError {
#[error("Bootrom file cannot be found")]
BootromNotFound,
#[error("IO error whilst reading bootrom: {0}")]
Io(#[from] std::io::Error),
#[error("Bootrom size is {0} bytes, expected 256 bytes")]
InvalidSize(u64),
#[error("Bootrom has an invalid SHA1 (expected \"4ed31ec6b0b175bb109c0eb5fd3d193da823339f\")")]
InvalidHash,
#[error("Failed to open bootrom file: {0}")]
FileOpen(std::io::Error),
#[error("Failed to read bootrom file: {0}")]
FileRead(std::io::Error),
#[error("Bootrom file cannot be found")]
BootromNotFound,
#[error("IO error whilst reading bootrom: {0}")]
Io(#[from] std::io::Error),
#[error("Bootrom size is {0} bytes, expected 256 bytes")]
InvalidSize(u64),
#[error("Bootrom has an invalid SHA1 (expected \"4ed31ec6b0b175bb109c0eb5fd3d193da823339f\")")]
InvalidHash,
#[error("Failed to open bootrom file: {0}")]
FileOpen(std::io::Error),
#[error("Failed to read bootrom file: {0}")]
FileRead(std::io::Error),
}

pub fn verify_parse_bootrom(path: &Path) -> Result<[u8; 0x100], BootromParseError> {
if !path.is_file() {
return Err(BootromParseError::BootromNotFound);
}
if !path.is_file() {
return Err(BootromParseError::BootromNotFound);
}

let mut bootrom_slice = [0u8; 0x100];
let mut bootrom_slice = [0u8; 0x100];

let mut file = std::fs::File::open(path).map_err(BootromParseError::FileOpen)?;
let metadata = file.metadata()?;
let mut file = std::fs::File::open(path).map_err(BootromParseError::FileOpen)?;
let metadata = file.metadata()?;

if metadata.len() != 256 {
return Err(BootromParseError::InvalidSize(metadata.len()));
}
if metadata.len() != 256 {
return Err(BootromParseError::InvalidSize(metadata.len()));
}

file.read_exact(&mut bootrom_slice).map_err(BootromParseError::FileRead)?;
file.read_exact(&mut bootrom_slice).map_err(BootromParseError::FileRead)?;

let mut hash_ctx = Sha1::new();
hash_ctx.update(&bootrom_slice);
let digest = hash_ctx.finalize();
let mut hash_ctx = Sha1::new();
hash_ctx.update(&bootrom_slice);
let digest = hash_ctx.finalize();

if digest.as_slice()
!= b"\x4e\xd3\x1e\xc6\xb0\xb1\x75\xbb\x10\x9c\x0e\xb5\xfd\x3d\x19\x3d\xa8\x23\x33\x9f"
{
return Err(BootromParseError::InvalidHash);
}
if digest.as_slice()
!= b"\x4e\xd3\x1e\xc6\xb0\xb1\x75\xbb\x10\x9c\x0e\xb5\xfd\x3d\x19\x3d\xa8\x23\x33\x9f"
{
return Err(BootromParseError::InvalidHash);
}

Ok(bootrom_slice)
}
Ok(bootrom_slice)
}
2 changes: 1 addition & 1 deletion meowgb-core/src/gameboy/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod load_store_move;
mod misc;
mod prefixed;

use super::{Gameboy, serial::SerialWriter};
use super::{serial::SerialWriter, Gameboy};

macro_rules! define_register {
($lident:ident, $rident:ident) => {
Expand Down
2 changes: 1 addition & 1 deletion meowgb-core/src/gameboy/cpu/prefixed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use meowgb_opcode::opcode;

use super::CycleResult;
use crate::gameboy::{Gameboy, serial::SerialWriter};
use crate::gameboy::{serial::SerialWriter, Gameboy};

pub fn prefixed_handler(state: &mut Gameboy<impl SerialWriter>) -> CycleResult {
let opcode = match state.registers.current_prefixed_opcode {
Expand Down
16 changes: 9 additions & 7 deletions meowgb-core/src/gameboy/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,7 @@ impl Ppu {
oam_entry.y.wrapping_sub(16).wrapping_add(sprite_height as u8);

if oam_entry.x > 0
&& self.ly < real_oam_y
&& self.ly >= oam_entry.y.wrapping_sub(16)
&& self.ly < real_oam_y && self.ly >= oam_entry.y.wrapping_sub(16)
&& self.sprite_count < 10
{
self.sprite_buffer[self.sprite_count] = Some(oam_entry);
Expand Down Expand Up @@ -764,7 +763,8 @@ impl Ppu {

if self.window_enabled() && wx_match && self.wy_match {
window_drawn = true;
let window_x = (drawn_pixels as u8).wrapping_sub(self.wx.wrapping_sub(7)) as usize;
let window_x =
(drawn_pixels as u8).wrapping_sub(self.wx.wrapping_sub(7)) as usize;
let window_y = self.ly.wrapping_sub(self.wy) as usize;
let tilemap_idx = window_x / 8 + ((window_y / 8) * 32);
let tilemap_value = self.read_window_tile_map()[tilemap_idx];
Expand Down Expand Up @@ -910,9 +910,10 @@ impl Ppu {
}
}

let now = chrono::Utc::now();
let now =
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
std::fs::create_dir_all("./bmp").unwrap();
let file_name = format!("./bmp/fb-{}.bmp", now.timestamp());
let file_name = format!("./bmp/fb-{}.bmp", now);
image.save(file_name.as_str()).unwrap();
file_name
}
Expand Down Expand Up @@ -945,9 +946,10 @@ impl Ppu {
}
}

let now = chrono::Utc::now();
let now =
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
std::fs::create_dir_all("./bmp").unwrap();
let file_name = format!("./bmp/bg-data-{}.bmp", now.timestamp());
let file_name = format!("./bmp/bg-data-{}.bmp", now);
image.save(file_name.as_str()).unwrap();
}

Expand Down
10 changes: 6 additions & 4 deletions meowgb-core/src/gameboy/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ pub trait SerialWriter {
}

impl<T: Write> SerialWriter for T {
fn write_byte(&mut self, byte: u8) {
self.write_all(&[byte]).expect(format!("writing serial to {} failed", std::any::type_name::<T>()).as_str());
self.flush().expect(format!("flushing serial to {} failed", std::any::type_name::<T>()).as_str());
}
fn write_byte(&mut self, byte: u8) {
self.write_all(&[byte])
.expect(format!("writing serial to {} failed", std::any::type_name::<T>()).as_str());
self.flush()
.expect(format!("flushing serial to {} failed", std::any::type_name::<T>()).as_str());
}
}

pub struct Serial<S: SerialWriter> {
Expand Down
Loading

0 comments on commit 654ff62

Please sign in to comment.