Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

wii settings sync

Laylea2ebb28772f4
diff --git a/crates/gecko/src/hollywood/ipc/fs/nand.rs b/crates/gecko/src/hollywood/ipc/fs/nand.rs
index 46dc243..c50debf 100644
--- a/crates/gecko/src/hollywood/ipc/fs/nand.rs
+++ b/crates/gecko/src/hollywood/ipc/fs/nand.rs
@@ -17,13 +17,42 @@ pub fn ensure_skeleton(root: &Path) {
}
}
-pub fn ensure_setting_txt(root: &Path, game_code: [u8; 4]) {
+pub fn sync_setting_txt(root: &Path, game_code: [u8; 4]) {
let path = root.join(SETTING_TXT_PATH);
- if path.exists() {
- return;
+ let setting = RegionSetting::from_game_code(game_code);
+
+ if let Ok(existing) = std::fs::read(&path) {
+ match self::decode_area(&existing) {
+ Some(area) if area == setting.area => return,
+ Some(area) => tracing::info!(
+ old = %area,
+ new = setting.area,
+ "NAND: setting.txt region mismatch, regenerating for disc"
+ ),
+ None => tracing::warn!("NAND: undecodable setting.txt, regenerating"),
+ }
}
- self::write_new(&path, &RegionSetting::from_game_code(game_code).encode());
+ self::write_new(&path, &setting.encode());
+}
+
+fn decode_area(data: &[u8]) -> Option<String> {
+ let mut key = SETTING_SEED;
+
+ let decoded: Vec<u8> = data
+ .iter()
+ .take(0x100)
+ .map(|&b| {
+ let plain = b ^ (key as u8);
+ key = key.rotate_left(1);
+ plain
+ })
+ .collect();
+
+ let text = String::from_utf8_lossy(&decoded);
+ text.lines()
+ .find_map(|line| line.strip_prefix("AREA="))
+ .map(|value| value.trim_end().to_owned())
}
pub fn ensure_title_dirs(root: &Path, title_id: u64) {
@@ -51,7 +80,7 @@ pub fn write_new(path: &Path, data: &[u8]) {
}
}
-struct RegionSetting {
+pub(crate) struct RegionSetting {
area: &'static str,
video: &'static str,
code: &'static str,
@@ -59,7 +88,15 @@ struct RegionSetting {
}
impl RegionSetting {
- fn from_game_code(game_code: [u8; 4]) -> Self {
+ pub(crate) fn language(&self) -> u8 {
+ match self.area {
+ "JPN" => 0,
+ "KOR" => 9,
+ _ => 1,
+ }
+ }
+
+ pub(crate) fn from_game_code(game_code: [u8; 4]) -> Self {
match game_code[3] {
b'J' => Self {
area: "JPN",
diff --git a/crates/gecko/src/hollywood/ipc/sysconf.rs b/crates/gecko/src/hollywood/ipc/sysconf.rs
index bccc456..227ea04 100644
--- a/crates/gecko/src/hollywood/ipc/sysconf.rs
+++ b/crates/gecko/src/hollywood/ipc/sysconf.rs
@@ -28,8 +28,15 @@ pub struct SysConf {
impl SysConf {
pub fn new(host_fs_root: &Path) -> Self {
- let blob = self::build_blob();
+ Self::with_blob(host_fs_root, self::build_blob(1))
+ }
+
+ pub fn for_game_code(host_fs_root: &Path, game_code: [u8; 4]) -> Self {
+ let region = nand::RegionSetting::from_game_code(game_code);
+ Self::with_blob(host_fs_root, self::build_blob(region.language()))
+ }
+ fn with_blob(host_fs_root: &Path, blob: Vec<u8>) -> Self {
let path = host_fs_root.join("shared2/sys/SYSCONF");
if !path.exists() {
nand::write_new(&path, &blob);
@@ -147,14 +154,14 @@ impl EntryData {
}
}
-fn build_blob() -> Vec<u8> {
+fn build_blob(language: u8) -> Vec<u8> {
let entries: [(&[u8], EntryData); 11] = [
(b"BT.DINF", EntryData::BigArray(self::build_bt_dinf())),
(b"BT.MOT", EntryData::Byte(1)),
(b"BT.SENS", EntryData::Long(3)),
(b"BT.BAR", EntryData::Byte(1)),
(b"BT.SPKV", EntryData::Byte(0x58)),
- (b"IPL.LNG", EntryData::Byte(1)),
+ (b"IPL.LNG", EntryData::Byte(language)),
(b"IPL.AR", EntryData::Byte(0)),
(b"IPL.E60", EntryData::Bool(false)),
(b"IPL.PGS", EntryData::Bool(false)),
diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rs
index 1a11071..86c3980 100644
--- a/crates/gecko/src/mmio/bus.rs
+++ b/crates/gecko/src/mmio/bus.rs
@@ -235,7 +235,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
if self.mmio.is_code_chunk(phys) {
return;
}
-
+
self.mmio.phys_slice_mut(phys, 32).fill(0);
}
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 4d5939c..3253a0d 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -143,7 +143,7 @@ impl Scheduler<{ crate::system::WII }> {
impl<const SYSTEM: SystemId> Scheduler<SYSTEM> {
fn with_default_events() -> Self {
let mut s = Self::empty();
- let initial_refresh_rate = RefreshRate::Hz60; // TODO: Detect IPL and schedule accordingly
+ let initial_refresh_rate = RefreshRate::Hz60;
s.schedule_at(
initial_refresh_rate.cycles_per_frame(SYSTEM),
self::vsync_handler::<SYSTEM>,
@@ -179,6 +179,13 @@ pub fn vsync_handler<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
.schedule_in(rate.cycles_per_frame(SYSTEM), self::vsync_handler::<SYSTEM>);
}
+pub fn reseed_vsync<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ let rate = sys.vi.dcr.video_format().refresh_rate();
+ sys.scheduler.cancel(self::vsync_handler::<SYSTEM>);
+ sys.scheduler
+ .schedule_in(rate.cycles_per_frame(SYSTEM), self::vsync_handler::<SYSTEM>);
+}
+
#[inline(always)]
pub const fn dsp_batch_interval(system: SystemId) -> u64 {
self::cpu_cycles_per_dsp_tick(system) * self::DSP_BATCH_SIZE
diff --git a/crates/gecko/src/wii.rs b/crates/gecko/src/wii.rs
index 9ebb9cc..40d8c6b 100644
--- a/crates/gecko/src/wii.rs
+++ b/crates/gecko/src/wii.rs
@@ -255,9 +255,21 @@ impl Wii {
let video_mode: u32 = if header.is_ntsc() { 0 } else { 1 };
emu.mmio.phys_write_u32(0x0000_00CC, video_mode);
- // Generate the System Menu setting.txt for this disc's region if the
- // host fs root doesn't already carry one.
- crate::hollywood::ipc::fs::nand::ensure_setting_txt(&emu.starlet.host_fs_root, header.game_code);
+ if !header.is_ntsc() {
+ emu.vi.dcr.set_video_format(crate::flipper::vi::regs::VideoFormat::Pal);
+ crate::scheduler::reseed_vsync(emu);
+ }
+
+ crate::hollywood::ipc::fs::nand::sync_setting_txt(&emu.starlet.host_fs_root, header.game_code);
+
+ let fs_root = emu.starlet.host_fs_root.clone();
+ emu.starlet.register(
+ "/shared2/sys/SYSCONF",
+ Box::new(crate::hollywood::ipc::sysconf::SysConf::for_game_code(
+ &fs_root,
+ header.game_code,
+ )),
+ );
// Create the booting title's content/data dirs so saves can be written.
crate::hollywood::ipc::fs::nand::ensure_title_dirs(&emu.starlet.host_fs_root, dvd.tmd_title_id());