Skip to content

Updated PS1 sigscans #109

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
Jun 21, 2025
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
26 changes: 14 additions & 12 deletions src/emulator/ps1/duckstation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{file_format::pe, signature::Signature, Address, Address64, Process};
use crate::{file_format::pe, signature::Signature, Address, Address64, PointerSize, Process};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -7,13 +7,16 @@ pub struct State {

impl State {
pub fn find_ram(&mut self, game: &Process) -> Option<Address> {
const SIG: Signature<8> = Signature::new("48 89 0D ?? ?? ?? ?? B8");

let main_module_range = super::PROCESS_NAMES
.iter()
.filter(|(_, state)| matches!(state, super::State::Duckstation(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

// 32bit versions of Duckstation are not supported (nor developed)
if pe::MachineType::read(game, main_module_range.0)?.pointer_size()? != PointerSize::Bit64 {
return None;
}

// Recent Duckstation releases include a debug symbol that can be used to easily retrieve the address of the emulated RAM
// Info: https://github.com/stenzek/duckstation/commit/c98e0bd0969abdd82589bfc565aea52119fd0f19
if let Some(debug_symbol) = pe::symbols(game, main_module_range.0).find(|symbol| {
Expand All @@ -24,20 +27,19 @@ impl State {
self.addr = debug_symbol.address;
} else {
// For older versions of Duckstation, we fall back to regular sigscanning
let addr = SIG.scan_process_range(game, main_module_range)? + 3;
self.addr = addr + 0x4 + game.read::<i32>(addr).ok()?;
const SIG: Signature<8> = Signature::new("48 89 0D ?? ?? ?? ?? B8");
self.addr = SIG
.scan_process_range(game, main_module_range)
.map(|val| val + 3)
.and_then(|addr| Some(addr + 0x4 + game.read::<i32>(addr).ok()?))?;
}

Some(game.read::<Address64>(self.addr).ok()?.into())
Some(game.read::<Address64>(self.addr).unwrap_or_default().into())
}

pub fn keep_alive(&self, game: &Process, wram_base: &mut Option<Address>) -> bool {
if let Ok(addr) = game.read::<Address64>(self.addr) {
*wram_base = Some(addr.into());
true
} else {
false
}
*wram_base = Some(game.read::<Address64>(self.addr).unwrap_or_default().into());
true
}

pub const fn new() -> Self {
Expand Down
7 changes: 4 additions & 3 deletions src/emulator/ps1/epsxe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::Epsxe(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let ptr = SIG.scan_process_range(game, main_module_range)? + 5;

Some(game.read::<Address32>(ptr).ok()?.into())
SIG.scan_process_range(game, main_module_range)
.map(|val| val + 5)
.and_then(|addr| game.read::<Address32>(addr).ok())
.map(|val| val.into())
}

pub const fn keep_alive(&self) -> bool {
Expand Down
17 changes: 9 additions & 8 deletions src/emulator/ps1/mednafen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Process};
use crate::{file_format::pe, signature::Signature, Address, Address32, PointerSize, Process};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State;
Expand All @@ -14,14 +14,15 @@ impl State {
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let is_64_bit =
pe::MachineType::read(game, main_module_range.0) == Some(pe::MachineType::X86_64);
pe::MachineType::read(game, main_module_range.0)?.pointer_size()? == PointerSize::Bit64;

let ptr = match is_64_bit {
true => SIG_64.scan_process_range(game, main_module_range)?,
false => SIG_32.scan_process_range(game, main_module_range)?,
} + 0x5;

Some(game.read::<Address32>(ptr).ok()?.into())
match is_64_bit {
true => SIG_64.scan_process_range(game, main_module_range),
false => SIG_32.scan_process_range(game, main_module_range),
}
.map(|val| val + 0x5)
.and_then(|addr| game.read::<Address32>(addr).ok())
.map(|val| val.into())
}

pub const fn keep_alive(&self) -> bool {
Expand Down
12 changes: 8 additions & 4 deletions src/emulator/ps1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Emulator {
State::PsxFin(x) => x.keep_alive(),
State::Duckstation(x) => x.keep_alive(&self.process, &mut ram_base),
State::Retroarch(x) => x.keep_alive(&self.process),
State::PcsxRedux(x) => x.keep_alive(&self.process),
State::PcsxRedux(x) => x.keep_alive(&self.process, &mut ram_base),
State::Xebra(x) => x.keep_alive(),
State::Mednafen(x) => x.keep_alive(),
};
Expand All @@ -132,10 +132,14 @@ impl Emulator {
///
/// Valid addresses for the PS1 range from `0x80000000` to `0x817FFFFF`.
pub fn get_address(&self, offset: u32) -> Result<Address, Error> {
let addr = self
.ram_base
.get()
.filter(|addr| !addr.is_null())
.ok_or(Error {})?;

match offset {
(0x80000000..=0x817FFFFF) => {
Ok(self.ram_base.get().ok_or(Error {})? + offset.sub(0x80000000))
}
(0x80000000..=0x817FFFFF) => Ok(addr + offset.sub(0x80000000)),
_ => Err(Error {}),
}
}
Expand Down
72 changes: 27 additions & 45 deletions src/emulator/ps1/pcsx_redux.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::{
file_format::pe, signature::Signature, Address, Address32, Address64, MemoryRangeFlags, Process,
};
use crate::{file_format::pe, signature::Signature, Address, Address64, PointerSize, Process};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
is_64_bit: bool,
addr_base: Address,
addr: Address,
offsets: [u64; 3],
}

impl State {
Expand All @@ -16,58 +13,43 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::PcsxRedux(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

self.is_64_bit =
pe::MachineType::read(game, main_module_range.0) == Some(pe::MachineType::X86_64);

if self.is_64_bit {
const SIG_BASE: Signature<25> = Signature::new(
"48 B9 ?? ?? ?? ?? ?? ?? ?? ?? E8 ?? ?? ?? ?? C7 85 ?? ?? ?? ?? 00 00 00 00",
);
const SIG_OFFSET: Signature<9> = Signature::new("89 D1 C1 E9 10 48 8B ?? ??");

self.addr_base = SIG_BASE.scan_process_range(game, main_module_range)? + 2;
self.addr = game.read::<Address64>(self.addr_base).ok()?.into();
if pe::MachineType::read(game, main_module_range.0)?.pointer_size()? != PointerSize::Bit64 {
return None;
}

let offset = SIG_OFFSET.scan_process_range(game, main_module_range)? + 8;
let offset = game.read::<u8>(offset).ok()? as u64;
const SIG_BASE: Signature<19> =
Signature::new("48 8B 05 ?? ?? ?? ?? 48 8B 80 ?? ?? ?? ?? 48 8B 50 ?? E8");

let addr = game.read::<Address64>(self.addr + offset).ok()?;
let addr = SIG_BASE.scan_process_range(game, main_module_range)? + 3;

Some(game.read::<Address64>(addr).ok()?.into())
} else {
const SIG: Signature<18> =
Signature::new("8B 3D 20 ?? ?? ?? 0F B7 D3 8B 04 95 ?? ?? ?? ?? 21 05");
self.addr_base = addr + 0x4 + game.read::<i32>(addr).ok()?;

self.addr_base = game
.memory_ranges()
.filter(|m| {
m.flags()
.unwrap_or_default()
.contains(MemoryRangeFlags::WRITE)
})
.find_map(|m| SIG.scan_process_range(game, m.range().ok()?))?
+ 2;
self.offsets = [
0,
game.read::<i32>(addr + 7).ok()? as u64,
game.read::<u8>(addr + 14).ok()?.into(),
];

self.addr = game.read::<Address32>(self.addr_base).ok()?.into();
Some(self.addr)
}
Some(
game.read_pointer_path::<Address64>(self.addr_base, PointerSize::Bit64, &self.offsets)
.unwrap_or_default()
.into(),
)
}

pub fn keep_alive(&self, game: &Process) -> bool {
if self.is_64_bit {
game.read::<Address64>(self.addr_base)
.is_ok_and(|addr| self.addr == addr.into())
} else {
game.read::<Address32>(self.addr_base)
.is_ok_and(|addr| self.addr == addr.into())
}
pub fn keep_alive(&self, game: &Process, ram_base: &mut Option<Address>) -> bool {
*ram_base = Some(
game.read_pointer_path::<Address64>(self.addr_base, PointerSize::Bit64, &self.offsets)
.unwrap_or_default()
.into(),
);
true
}

pub const fn new() -> Self {
Self {
is_64_bit: true,
addr_base: Address::NULL,
addr: Address::NULL,
offsets: [0; 3],
}
}
}
38 changes: 19 additions & 19 deletions src/emulator/ps1/psxfin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::PsxFin(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let mut ptr: Address32 = if let Some(sig) = SIG.scan_process_range(game, main_module_range)
{
game.read(sig + 2).ok()?
} else if let Some(sig) = SIG_0.scan_process_range(game, main_module_range) {
game.read(sig + 1).ok()?
} else if let Some(sig) = SIG_1.scan_process_range(game, main_module_range) {
game.read(sig + 1).ok()?
} else if let Some(sig) = SIG_2.scan_process_range(game, main_module_range) {
game.read(sig + 1).ok()?
} else {
return None;
};
let ptr: Address = SIG
.scan_process_range(game, main_module_range)
.and_then(|addr| {
game.read::<Address32>(addr + 2)
.ok()
.map(|addr| addr.into())
})
.or_else(|| SIG_0.scan_process_range(game, main_module_range))
.or_else(|| SIG_1.scan_process_range(game, main_module_range))
.or_else(|| SIG_2.scan_process_range(game, main_module_range))
.and_then(|addr| {
game.read::<Address32>(addr + 1)
.ok()
.map(|addr| addr.into())
})?;

ptr = game.read::<Address32>(ptr).ok()?;

if ptr.is_null() {
None
} else {
Some(ptr.into())
}
game.read::<Address32>(ptr)
.ok()
.filter(|val| !val.is_null())
.map(|addr| addr.into())
}

pub const fn keep_alive(&self) -> bool {
Expand Down
Loading
Loading