Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/LLeny/holani.git
Browse files Browse the repository at this point in the history
  • Loading branch information
LLeny committed Dec 1, 2024
2 parents 9ae9c8d + a2a802a commit dc3d4fb
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 130 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ edition = "2021"
[dependencies]
env_logger = { version = "0.11.5", default-features = false, features = [ "auto-color", "humantime", ] }
log = { version = "0.4", features = ["max_level_trace", "release_max_level_warn"] }
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
postcard = {version = "1.0", features = ["experimental-derive"] }
bitflags = { version = "2.6", features = ["serde"] }
md5 = "0.7.0"
lazy_static = "1.5.0"
shared_memory = { version = "0.12", optional = true }
hashbrown = "0.15"
parking_lot = "0.12"

[features]
comlynx_shared_memory = ["dep:shared_memory"]
comlynx_shared_memory = ["dep:shared_memory"]
6 changes: 2 additions & 4 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt;

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -87,8 +85,8 @@ impl Default for Bus {
}
}

impl fmt::Debug for Bus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl core::fmt::Debug for Bus {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{{ addr:{:04x} data:{:04x} status:{:?} request:{:?} grant:{:?} }}", self.addr, self.data, self.status, self.request, self.grant)
}
}
1 change: 1 addition & 0 deletions src/cartridge/eeprom/ee93cxx.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use log::trace;
use serde::{Deserialize, Serialize};
use bitflags::bitflags;
Expand Down
5 changes: 3 additions & 2 deletions src/cartridge/lnx_header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::{string::String, vec::Vec};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Copy, Default)]
Expand Down Expand Up @@ -25,8 +26,8 @@ impl LNXHeader {
pub fn new() -> Self {
Self {
rotation: Default::default(),
manufacturer: "unknown".to_string(),
title: "unknown".to_string(),
manufacturer: "unknown".into(),
title: "unknown".into(),
version: 0,
bank0_size: 0,
bank1_size: 0,
Expand Down
13 changes: 7 additions & 6 deletions src/cartridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ pub mod lnx_header;
mod cartridge_generic;
mod eeprom;
mod no_intro;
use std::io::Error;

use alloc::string::String;
use cartridge_generic::*;
use eeprom::Eeprom;
use lnx_header::{LNXHeader, LNXRotation};
Expand Down Expand Up @@ -118,7 +119,7 @@ impl Default for Cartridge {
}

impl Cartridge {
pub fn from_slice(data: &[u8]) -> Result<Self, Error> {
pub fn from_slice(data: &[u8]) -> Result<Self, &'static str> {
let mut cart = Self::default();

if cart.is_lnx(data) {
Expand All @@ -129,7 +130,7 @@ impl Cartridge {
cart.nointro(data);
}
else {
return Err(Error::new(std::io::ErrorKind::Other, "Couldn't identify cart file format."));
return Err("Couldn't identify cart file format.");
}

Ok(cart)
Expand Down Expand Up @@ -186,7 +187,7 @@ impl Cartridge {
self.cart = CartType::Generic(cart);

if let Ok(cart_info) = check_no_intro(file_content) {
self.header.set_title(cart_info.0.to_string());
self.header.set_title(cart_info.0.into());
self.header.set_rotation(cart_info.1);
}

Expand Down Expand Up @@ -234,11 +235,11 @@ impl Cartridge {
self.header.set_version(TO_U16!(file_content[8], file_content[9]));
self.header.set_title(match String::from_utf8(file_content[10..=41].to_vec()) {
Ok(t) => t,
Err(_) => "Error".to_string()
Err(_) => "Error".into()
});
self.header.set_manufacturer(match String::from_utf8(file_content[42..=58].to_vec()) {
Ok(m) => m,
Err(_) => "Error".to_string()
Err(_) => "Error".into()
});
self.header.set_rotation(match file_content[58] {
1 => LNXRotation::_270,
Expand Down
2 changes: 1 addition & 1 deletion src/cartridge/no_intro.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hashbrown::HashMap;
use lazy_static::lazy_static;
use std::collections::HashMap;
use md5;

use super::lnx_header::LNXRotation;
Expand Down
2 changes: 1 addition & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A page mode op-code read takes 4 ticks, a normal read or write to RAM takes 5 ti
pub const RAM_NORMAL_READ_TICKS: i8 = 4;
pub const RAM_NORMAL_WRITE_TICKS: i8 = 4;
pub const RAM_PAGE_READ_TICKS: i8 = 3;
pub const RAM_DMA_READ_TICKS: i8 = 4;
pub const RAM_DMA_READ_TICKS: i8 = 3;

pub const RAM_PEEK_DATA_OPCODE: u8 = 0b00000001;
pub const RAM_PEEK_DATA_DMA: u8 = 0b00000010;
Expand Down
19 changes: 12 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

pub mod bus;
pub mod cartridge;
pub mod mikey;
Expand All @@ -8,6 +10,7 @@ pub mod vectors;
pub mod consts;
mod shared_memory;

use alloc::vec::Vec;
use bus::*;
use cartridge::*;
use consts::*;
Expand All @@ -21,11 +24,13 @@ use mikey::{video::{LYNX_SCREEN_HEIGHT, LYNX_SCREEN_WIDTH}, Mikey};
use ram::*;
use rom::Rom;
use shared_memory::SharedMemory;
use std::io::Error;
use suzy::{registers::{joystick_swap, Joystick, Switches}, Suzy};
use vectors::Vectors;
use serde::{Serialize, Deserialize};

#[macro_use]
extern crate alloc;

#[derive(Serialize, Deserialize)]
pub struct Lynx {
ram: Ram,
Expand Down Expand Up @@ -65,7 +70,7 @@ impl Lynx {
self.mikey.cpu_prefetch(reset_vec, &mut self.rom);
}

pub fn load_cart_from_slice(&mut self, data: &[u8]) -> Result<(), Error> {
pub fn load_cart_from_slice(&mut self, data: &[u8]) -> Result<(), &'static str> {
trace!("Load cart");
match Cartridge::from_slice(data) {
Err(e) => Err(e),
Expand All @@ -76,7 +81,7 @@ impl Lynx {
}
}

pub fn load_rom_from_slice(&mut self, data: &[u8]) -> Result<(), Error> {
pub fn load_rom_from_slice(&mut self, data: &[u8]) -> Result<(), &'static str> {
trace!("Load rom");
match Rom::from_slice(data) {
Err(e) => Err(e),
Expand Down Expand Up @@ -338,16 +343,16 @@ impl Default for Lynx {
}
}

pub fn serialize(lynx: &Lynx, data: &mut [u8]) -> Result<(), Error> {
pub fn serialize(lynx: &Lynx, data: &mut [u8]) -> Result<(), &'static str> {
match postcard::to_slice(&lynx, data) {
Err(e) => Err(Error::new(std::io::ErrorKind::InvalidData, format!("{}", e))),
Err(_) => Err("Serialization error."),
Ok(_) => Ok(()),
}
}

pub fn deserialize(data: &[u8], source: &Lynx) -> Result<Lynx, Error> {
pub fn deserialize(data: &[u8], source: &Lynx) -> Result<Lynx, &'static str> {
let mut lynx = match postcard::from_bytes::<Lynx>(data) {
Err(e) => return Err(Error::new(std::io::ErrorKind::InvalidData, format!("{}", e))),
Err(_) => return Err("Deserialization error"),
Ok(l) => l
};
lynx.cart.copy_from(&source.cart);
Expand Down
3 changes: 2 additions & 1 deletion src/mikey/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
extern crate alloc;
use alloc::{boxed::Box, fmt};
use bitflags::bitflags;
use super::*;

Expand Down
7 changes: 3 additions & 4 deletions src/mikey/timers/audio_channel_timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fmt;

use alloc::fmt;
use log::trace;

use super::*;
Expand Down Expand Up @@ -185,8 +184,8 @@ impl AudioChannelTimer{
self.control_b &= !CTRLB_BORROW_OUT_BIT;
self.control_b |= CTRLB_BORROW_IN_BIT;
match self.count.cmp(&0) {
std::cmp::Ordering::Greater => self.count -= 1,
std::cmp::Ordering::Equal => {
core::cmp::Ordering::Greater => self.count -= 1,
core::cmp::Ordering::Equal => {
if self.reload_enabled {
trace!("AudioTimer #{} reload 0x{:02x}.", self.id, self.backup);
self.count = self.backup;
Expand Down
7 changes: 4 additions & 3 deletions src/mikey/timers/base_timer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use alloc::fmt;

use super::*;

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -135,8 +136,8 @@ impl BaseTimer{
self.control_b &= !CTRLB_BORROW_OUT_BIT;
self.control_b |= CTRLB_BORROW_IN_BIT;
match self.count.cmp(&0) {
std::cmp::Ordering::Greater => self.count -= 1,
std::cmp::Ordering::Equal => {
core::cmp::Ordering::Greater => self.count -= 1,
core::cmp::Ordering::Equal => {
if self.reload_enabled {
trace!("Timer #{} reload 0x{:02x} next trigger @ {}.", self.id, self.backup, self.next_trigger_tick);
self.count = self.backup;
Expand Down
9 changes: 5 additions & 4 deletions src/mikey/uart/comlynx_cable_mutex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{fmt, sync::{Arc, Mutex}};
use alloc::{fmt, sync::Arc};
use parking_lot::Mutex;
use redeye_status::RedeyeStatus;
use serde::{de::{self, Visitor}, Deserializer, Serializer};

Expand All @@ -18,11 +19,11 @@ impl ComlynxCable {
}

pub fn status(&self) -> RedeyeStatus {
*self.redeye_pin.lock().unwrap()
*self.redeye_pin.lock()
}

pub fn set(&mut self, status: RedeyeStatus) {
*self.redeye_pin.lock().unwrap() = status;
*self.redeye_pin.lock() = status;
}
}

Expand All @@ -45,7 +46,7 @@ impl Serialize for ComlynxCable {
where
S: Serializer,
{
let v = *self.redeye_pin.lock().unwrap() as u8;
let v = *self.redeye_pin.lock() as u8;
serializer.serialize_u8(v)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mikey/uart/comlynx_cable_shared_memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use alloc::fmt;
use redeye_status::RedeyeStatus;
use serde::{de::{self, Visitor}, Deserializer, Serializer};
use ::shared_memory::{Shmem, ShmemConf, ShmemError};
Expand Down
6 changes: 2 additions & 4 deletions src/rom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::{self, Error};

use super::*;

const ROM_SIZE: usize = 512;
Expand Down Expand Up @@ -29,10 +27,10 @@ impl Default for Rom {
}

impl Rom {
pub fn from_slice(data: &[u8]) -> Result<Rom, Error> {
pub fn from_slice(data: &[u8]) -> Result<Rom, &'static str> {
let mut r = Rom::default();
if data.len() != ROM_SIZE {
return Err(Error::new(io::ErrorKind::Other, "ROM file non valid."));
return Err("ROM file non valid.");
}
r.data = data.to_vec();
Ok(r)
Expand Down
7 changes: 4 additions & 3 deletions src/shared_memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::UnsafeCell;
use std::fmt;
use std::ops::{Index, IndexMut};
use core::cell::UnsafeCell;
use core::ops::{Index, IndexMut};
use alloc::fmt;
use alloc::vec::Vec;
use serde::de::Visitor;
use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down
2 changes: 1 addition & 1 deletion src/suzy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Suzy {
pub fn tick(&mut self, bus: &mut Bus, dma_ram: &mut Ram) {
self.ticks += 1;
self.manage_bus(bus);
if self.registers.data(SUZYBUSEN) != 1 {
if self.pending_bus_request_ticks >= 0 || self.registers.data(SUZYBUSEN) != 1 {
return;
}
self.manage_ir(bus);
Expand Down
8 changes: 2 additions & 6 deletions src/suzy/registers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ops::Add;
use bitflags::bitflags;
use super::*;

Expand Down Expand Up @@ -80,13 +79,13 @@ impl TaskStep {
pub const ONE: TaskStep = TaskStep::InitializePainting;
}

impl Add<u8> for TaskStep {
impl core::ops::Add<u8> for TaskStep {
type Output = Self;
fn add(self, rhs: u8) -> Self::Output {
let mut s = self as u8;
s += rhs;
s %= TaskStep::MaxSteps as u8;
unsafe { std::mem::transmute(s) }
unsafe { core::mem::transmute(s) }
}
}

Expand Down Expand Up @@ -676,7 +675,6 @@ mod tests {

macro_rules! TJKLM {
($c: expr, $v: expr, $cy: expr, $wn: expr) => {
println!("test jklm = {}, carry = {}, warning = {}", $v as u32, $cy, $wn);
T!($v == JKLM!($c));
T!(CY!($c) == $cy);
T!(WN!($c) == $wn);
Expand All @@ -685,7 +683,6 @@ mod tests {

macro_rules! MULT_T {
($c: expr, $ab: expr, $cd: expr, $exp: expr) => {
println!("test {} * {} = {}", $ab, $cd, $exp);
SAB!($c, $ab as u16);
SCD!($c, $cd as u16);
MULT!($c);
Expand All @@ -697,7 +694,6 @@ mod tests {
($c: expr, $efgh: expr, $np: expr) => {
let div = if $np == 0 { u32::MAX } else { $efgh / $np };
let mo = if $np == 0 { 0 } else { $efgh % $np };
println!("test {} / {} = {}, {}", $efgh, $np, div, mo);
SEFGH!($c, $efgh as u32);
SNP!($c, $np as u16);
DIV!($c);
Expand Down
Loading

0 comments on commit dc3d4fb

Please sign in to comment.