mirror of
https://github.com/gopher64/gopher64.git
synced 2026-07-11 09:35:21 +02:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 040ca493c8 | |||
| bda2266d74 | |||
| 195412a48e | |||
| 9257e8c66d | |||
| 87ae2b940a | |||
| ba8f39f283 | |||
| 1a823399ab | |||
| 26f0936dc7 | |||
| ada4b7e3b4 | |||
| 004af133eb | |||
| 0af411d662 | |||
| 549d71bebf | |||
| 9eacf8a916 | |||
| dbd12516cf | |||
| 4ead0218d9 | |||
| 14ea0eee3f | |||
| b860be4143 | |||
| b065f807a2 | |||
| ee95816385 | |||
| d021cb5d32 | |||
| 9819fad475 | |||
| dd767047fd | |||
| f6480ea312 | |||
| e4174c7d4b | |||
| f3d8080a26 | |||
| b581bdcd40 | |||
| 842c81988f | |||
| 45d05cecf0 | |||
| eae81c4623 | |||
| b3f8688ba5 |
+15
-3
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "gopher64"
|
||||
version = "0.1.18"
|
||||
version = "0.1.20"
|
||||
edition = "2021"
|
||||
rust-version = "1.82"
|
||||
|
||||
@@ -15,20 +15,32 @@ chrono = "0.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
eframe = "0.30"
|
||||
rfd = "0.15"
|
||||
sha2 = "0.10"
|
||||
rusttype = "0.9"
|
||||
futures = "0.3"
|
||||
tokio = { version = "1.43", features = ["rt-multi-thread", "macros"] }
|
||||
sdl2-sys = "0.37"
|
||||
spin_sleep = "1.3"
|
||||
encoding_rs = "0.8"
|
||||
mimalloc = "0.1"
|
||||
regex = "1.11"
|
||||
tungstenite = "0.26"
|
||||
clap = { version = "4.4", features = ["derive"] }
|
||||
|
||||
[dependencies.reqwest]
|
||||
version = "0.12"
|
||||
default-features = false
|
||||
features = ["http2","rustls-tls","charset","json"]
|
||||
|
||||
[dependencies.sdl2]
|
||||
version = "0.37"
|
||||
default-features = false
|
||||
features = ["static-link","bundled"]
|
||||
|
||||
[dependencies.rfd]
|
||||
version = "0.15"
|
||||
default-features = false
|
||||
features = ["xdg-portal","tokio"]
|
||||
|
||||
[build-dependencies]
|
||||
winres = "0.1"
|
||||
bindgen = "0.71"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# gopher64
|
||||
(gopher64 also has a GUI that can be launched by just running the executable)
|
||||
|
||||
```
|
||||
Usage: gopher64 [OPTIONS] [GAME]
|
||||
@@ -11,6 +12,8 @@ Options:
|
||||
|
||||
-c, --configure-input-profile <PROFILE_NAME>
|
||||
Create a new input profile (keyboard/gamepad mappings).
|
||||
-u, --use-dinput
|
||||
Use DirectInput when configuring a new input profile.
|
||||
-b, --bind-input-profile <PROFILE_NAME>
|
||||
Must also specify --port. Used to bind a previously created profile to a port
|
||||
-l, --list-controllers
|
||||
@@ -19,11 +22,29 @@ Options:
|
||||
Must also specify --port. Used to assign a controller listed in --list-controllers to a port
|
||||
-p, --port <PORT>
|
||||
Valid values: 1-4. To be used alongside --bind-input-profile and --assign-controller
|
||||
-z, --clear-input-bindings
|
||||
Clear all input profile bindings and controller assignments
|
||||
-h, --help
|
||||
Print help
|
||||
-V, --version
|
||||
Print version
|
||||
```
|
||||
## discord
|
||||
|
||||
There is no dedicated server at this point, but there is a #gopher64 channel on the simple64 server: https://discord.gg/R2cz7S94vD
|
||||
|
||||
## controls
|
||||
|
||||
Keys are mapped according to mupen64plus defaults: https://mupen64plus.org/wiki/index.php/KeyboardSetup#2._Default_Key_Mappings_for_SDL-Input_Plugin. Xbox-style controllers also have a default mapping applied.
|
||||
|
||||
You can create you own mappings by running `./gopher64 --configure-input-profile my_profile`. You then bind that profile to a port: `./gopher64 --bind-input-profile my_profile --port 1`
|
||||
|
||||
In order to use a controller (for example, an Xbox controller), run `./gopher64 --list-controllers` to get a list of attached controllers, and then assign it by doing `./gopher64 --assign-controller <controller_number> --port 1`
|
||||
|
||||
## portable mode
|
||||
|
||||
If you would like to keep all the game data in the same folder as executable, you just need to create a file called "portable.txt" in the same directory as the executable.
|
||||
|
||||
## goals
|
||||
|
||||
1. Performance. I want to be able to use this emulator on my laptop.
|
||||
@@ -38,18 +59,6 @@ Options:
|
||||
4. `cargo build --release`
|
||||
5. `./target/release/gopher64 /path/to/rom.z64`
|
||||
|
||||
## controls
|
||||
|
||||
Keys are mapped according to mupen64plus defaults: https://mupen64plus.org/wiki/index.php/KeyboardSetup#2._Default_Key_Mappings_for_SDL-Input_Plugin. Xbox-style controllers also have a default mapping applied.
|
||||
|
||||
You can create you own mappings by running `./gopher64 --configure-input-profile my_profile`. You then bind that profile to a port: `./gopher64 --bind-input-profile my_profile --port 1`
|
||||
|
||||
In order to use a controller (for example, an Xbox controller), run `./gopher64 --list-controllers` to get a list of attached controllers, and then assign it by doing `./gopher64 --assign-controller <controller_number> --port 1`
|
||||
|
||||
## portable mode
|
||||
|
||||
If you would like to keep all the game data in the same folder as executable, you just need to create a file called "portable.txt" in the same directory as the executable.
|
||||
|
||||
## contributing
|
||||
|
||||
I am very open to contributions! Please reach out to me via a GitHub issue, or via discord (loganmc10) before doing substantial work on a PR.
|
||||
|
||||
@@ -96,7 +96,6 @@ fn main() {
|
||||
.allowlist_function("rdp_set_vi_register")
|
||||
.allowlist_function("rdp_update_screen")
|
||||
.allowlist_function("rdp_process_commands")
|
||||
.allowlist_function("rdp_full_sync")
|
||||
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
|
||||
.generate()
|
||||
.expect("Unable to generate bindings");
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+32
-45
@@ -51,6 +51,7 @@ enum vi_registers
|
||||
};
|
||||
|
||||
static bool fullscreen;
|
||||
static bool integer_scaling;
|
||||
static SDL_Window *window;
|
||||
static RDP::CommandProcessor *processor;
|
||||
static SDL_WSIPlatform *wsi_platform;
|
||||
@@ -59,12 +60,8 @@ static uint32_t cmd_data[0x00040000 >> 2];
|
||||
static int cmd_cur;
|
||||
static int cmd_ptr;
|
||||
static bool emu_running;
|
||||
static uint64_t rdp_sync_signal;
|
||||
static GFX_INFO gfx_info;
|
||||
|
||||
static uint64_t last_frame_counter;
|
||||
static uint64_t frame_counter;
|
||||
|
||||
static const unsigned cmd_len_lut[64] = {
|
||||
1,
|
||||
1,
|
||||
@@ -166,13 +163,14 @@ int sdl_event_filter(void *userdata, SDL_Event *event)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rdp_init(void *_window, GFX_INFO _gfx_info, bool _fullscreen, bool _upscale)
|
||||
void rdp_init(void *_window, GFX_INFO _gfx_info, bool _upscale, bool _integer_scaling, bool _fullscreen)
|
||||
{
|
||||
window = (SDL_Window *)_window;
|
||||
SDL_AddEventWatch(sdl_event_filter, nullptr);
|
||||
|
||||
gfx_info = _gfx_info;
|
||||
fullscreen = _fullscreen;
|
||||
integer_scaling = _integer_scaling;
|
||||
bool window_vsync = 0;
|
||||
wsi = new WSI;
|
||||
wsi_platform = new SDL_WSIPlatform;
|
||||
@@ -207,8 +205,6 @@ void rdp_init(void *_window, GFX_INFO _gfx_info, bool _fullscreen, bool _upscale
|
||||
wsi->begin_frame();
|
||||
|
||||
emu_running = true;
|
||||
last_frame_counter = 0;
|
||||
frame_counter = 0;
|
||||
}
|
||||
|
||||
void rdp_close()
|
||||
@@ -234,34 +230,42 @@ void rdp_close()
|
||||
|
||||
static void calculate_viewport(float *x, float *y, float *width, float *height)
|
||||
{
|
||||
bool window_widescreen = false;
|
||||
int32_t display_width = (window_widescreen ? 854 : 640);
|
||||
int32_t display_height = 480;
|
||||
const int32_t display_width = 320;
|
||||
const int32_t display_height = 240;
|
||||
|
||||
int w, h;
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
|
||||
*width = w;
|
||||
*height = h;
|
||||
*x = 0;
|
||||
*y = 0;
|
||||
int32_t hw = display_height * *width;
|
||||
int32_t wh = display_width * *height;
|
||||
if (integer_scaling)
|
||||
{
|
||||
// Integer scaling path
|
||||
int scale_x = w / display_width;
|
||||
int scale_y = h / display_height;
|
||||
int scale = (scale_x < scale_y) ? scale_x : scale_y;
|
||||
if (scale < 1)
|
||||
scale = 1;
|
||||
|
||||
// add letterboxes or pillarboxes if the window has a different aspect ratio
|
||||
// than the current display mode
|
||||
if (hw > wh)
|
||||
{
|
||||
int32_t w_max = wh / display_height;
|
||||
*x += (*width - w_max) / 2;
|
||||
*width = w_max;
|
||||
// Calculate scaled dimensions
|
||||
int scaled_width = display_width * scale;
|
||||
int scaled_height = display_height * scale;
|
||||
|
||||
*width = scaled_width;
|
||||
*height = scaled_height;
|
||||
}
|
||||
else if (hw < wh)
|
||||
else
|
||||
{
|
||||
int32_t h_max = hw / display_width;
|
||||
*y += (*height - h_max) / 2;
|
||||
*height = h_max;
|
||||
// Regular scaling path - maintain aspect ratio
|
||||
float scale_x = w / (float)display_width;
|
||||
float scale_y = h / (float)display_height;
|
||||
float scale = (scale_x < scale_y) ? scale_x : scale_y;
|
||||
|
||||
*width = display_width * scale;
|
||||
*height = display_height * scale;
|
||||
}
|
||||
|
||||
// Center the viewport
|
||||
*x = (w - *width) / 2.0f;
|
||||
*y = (h - *height) / 2.0f;
|
||||
}
|
||||
|
||||
static void render_frame(Vulkan::Device &device)
|
||||
@@ -326,7 +330,6 @@ bool rdp_update_screen()
|
||||
render_frame(device);
|
||||
wsi->end_frame();
|
||||
wsi->begin_frame();
|
||||
frame_counter++;
|
||||
return emu_running;
|
||||
}
|
||||
|
||||
@@ -433,15 +436,7 @@ uint64_t rdp_process_commands()
|
||||
|
||||
if (RDP::Op(command) == RDP::Op::SyncFull)
|
||||
{
|
||||
if (frame_counter != last_frame_counter) // Only sync once per frame
|
||||
{
|
||||
rdp_sync_signal = processor->signal_timeline();
|
||||
last_frame_counter = frame_counter;
|
||||
}
|
||||
else
|
||||
{
|
||||
rdp_sync_signal = 0;
|
||||
}
|
||||
processor->wait_for_timeline(processor->signal_timeline());
|
||||
|
||||
uint32_t width = viCalculateHorizonalWidth(*gfx_info.VI_H_START_REG, *gfx_info.VI_X_SCALE_REG, *gfx_info.VI_WIDTH_REG);
|
||||
if (width == 0)
|
||||
@@ -468,11 +463,3 @@ uint64_t rdp_process_commands()
|
||||
|
||||
return interrupt_timer;
|
||||
}
|
||||
|
||||
void rdp_full_sync()
|
||||
{
|
||||
if (rdp_sync_signal)
|
||||
{
|
||||
processor->wait_for_timeline(rdp_sync_signal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,12 +23,11 @@ extern "C"
|
||||
uint32_t *VI_WIDTH_REG;
|
||||
} GFX_INFO;
|
||||
|
||||
void rdp_init(void *_window, GFX_INFO _gfx_info, bool _fullscreen, bool _upscale);
|
||||
void rdp_init(void *_window, GFX_INFO _gfx_info, bool _upscale, bool _integer_scaling, bool _fullscreen);
|
||||
void rdp_close();
|
||||
void rdp_set_vi_register(uint32_t reg, uint32_t value);
|
||||
bool rdp_update_screen();
|
||||
uint64_t rdp_process_commands();
|
||||
void rdp_full_sync();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
unsafe extern "C" {
|
||||
#[link_name = "_mm_srli_epi16__extern"]
|
||||
pub fn _mm_srli_epi16(__a: __m128i, __imm8: ::std::os::raw::c_int) -> __m128i;
|
||||
fn _mm_srli_epi16(__a: __m128i, __imm8: ::std::os::raw::c_int) -> __m128i;
|
||||
|
||||
#[link_name = "_mm_insert_epi8__extern"]
|
||||
pub fn _mm_insert_epi8(
|
||||
fn _mm_insert_epi8(
|
||||
__a: __m128i,
|
||||
__i: ::std::os::raw::c_int,
|
||||
__imm8: ::std::os::raw::c_int,
|
||||
) -> __m128i;
|
||||
|
||||
#[link_name = "_mm_extract_epi8__extern"]
|
||||
pub fn _mm_extract_epi8(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
|
||||
fn _mm_extract_epi8(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
|
||||
|
||||
#[link_name = "_mm_insert_epi16__extern"]
|
||||
pub fn _mm_insert_epi16(
|
||||
fn _mm_insert_epi16(
|
||||
__a: __m128i,
|
||||
__i: ::std::os::raw::c_int,
|
||||
__imm8: ::std::os::raw::c_int,
|
||||
) -> __m128i;
|
||||
|
||||
#[link_name = "_mm_extract_epi16__extern"]
|
||||
pub fn _mm_extract_epi16(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
|
||||
fn _mm_extract_epi16(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
|
||||
|
||||
#[link_name = "_mm_insert_epi32__extern"]
|
||||
pub fn _mm_insert_epi32(
|
||||
fn _mm_insert_epi32(
|
||||
__a: __m128i,
|
||||
__i: ::std::os::raw::c_int,
|
||||
__imm8: ::std::os::raw::c_int,
|
||||
) -> __m128i;
|
||||
|
||||
#[link_name = "_mm_extract_epi32__extern"]
|
||||
pub fn _mm_extract_epi32(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
|
||||
fn _mm_extract_epi32(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
|
||||
|
||||
#[link_name = "_mm_insert_epi64__extern"]
|
||||
pub fn _mm_insert_epi64(
|
||||
fn _mm_insert_epi64(
|
||||
__a: __m128i,
|
||||
__i: ::std::os::raw::c_longlong,
|
||||
__imm8: ::std::os::raw::c_int,
|
||||
) -> __m128i;
|
||||
|
||||
#[link_name = "_mm_extract_epi64__extern"]
|
||||
pub fn _mm_extract_epi64(
|
||||
fn _mm_extract_epi64(
|
||||
__a: __m128i,
|
||||
__imm8: ::std::os::raw::c_int,
|
||||
) -> ::std::os::raw::c_longlong;
|
||||
|
||||
+14
-13
@@ -10,6 +10,7 @@ include!(concat!(env!("OUT_DIR"), "/simd_bindings.rs"));
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use std::arch::x86_64::*;
|
||||
|
||||
use crate::netplay;
|
||||
use crate::ui;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
@@ -30,7 +31,6 @@ pub mod exceptions;
|
||||
pub mod fpu_instructions;
|
||||
pub mod is_viewer;
|
||||
pub mod memory;
|
||||
pub mod mempak;
|
||||
pub mod mi;
|
||||
pub mod pi;
|
||||
pub mod pif;
|
||||
@@ -46,16 +46,13 @@ pub mod sram;
|
||||
pub mod tlb;
|
||||
pub mod unmapped;
|
||||
pub mod vi;
|
||||
pub mod vru;
|
||||
|
||||
pub fn run_game(
|
||||
file_path: &std::path::Path,
|
||||
rom_contents: Vec<u8>,
|
||||
data_dir: std::path::PathBuf,
|
||||
device: &mut Device,
|
||||
fullscreen: bool,
|
||||
) {
|
||||
let rom_contents = get_rom_contents(file_path);
|
||||
|
||||
cart_rom::init(device, rom_contents); // cart needs to come before rdram
|
||||
|
||||
// rdram pointer is shared with parallel-rdp
|
||||
@@ -68,7 +65,7 @@ pub fn run_game(
|
||||
mi::init(device);
|
||||
pif::init(device);
|
||||
if device.ui.config.input.emulate_vru {
|
||||
vru::init(device);
|
||||
controller::vru::init(device);
|
||||
}
|
||||
memory::init(device);
|
||||
rsp_interface::init(device);
|
||||
@@ -77,13 +74,13 @@ pub fn run_game(
|
||||
cpu::init(device);
|
||||
|
||||
ui::storage::init(&mut device.ui, data_dir);
|
||||
ui::storage::load_saves(&mut device.ui);
|
||||
ui::storage::load_saves(&mut device.ui, &mut device.netplay);
|
||||
cart_rom::load_rom_save(device);
|
||||
|
||||
cpu::run(device);
|
||||
|
||||
ui::video::close();
|
||||
ui::storage::write_saves(&device.ui);
|
||||
ui::storage::write_saves(&device.ui, &device.netplay);
|
||||
}
|
||||
|
||||
fn swap_rom(contents: Vec<u8>) -> Vec<u8> {
|
||||
@@ -108,11 +105,12 @@ fn swap_rom(contents: Vec<u8>) -> Vec<u8> {
|
||||
}
|
||||
return data;
|
||||
} else {
|
||||
panic!("unknown rom format")
|
||||
let data: Vec<u8> = vec![];
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rom_contents(file_path: &std::path::Path) -> Vec<u8> {
|
||||
pub fn get_rom_contents(file_path: &std::path::Path) -> Vec<u8> {
|
||||
let mut contents = vec![];
|
||||
if file_path.extension().unwrap().eq_ignore_ascii_case("zip") {
|
||||
let zip_file = fs::File::open(file_path).unwrap();
|
||||
@@ -163,9 +161,10 @@ fn get_rom_contents(file_path: &std::path::Path) -> Vec<u8> {
|
||||
}
|
||||
|
||||
pub struct Device {
|
||||
pub netplay: Option<netplay::Netplay>,
|
||||
pub ui: ui::Ui,
|
||||
byte_swap: usize,
|
||||
cpu: cpu::Cpu,
|
||||
pub cpu: cpu::Cpu,
|
||||
pif: pif::Pif,
|
||||
cart: cart_rom::Cart,
|
||||
memory: memory::Memory,
|
||||
@@ -179,7 +178,7 @@ pub struct Device {
|
||||
si: si::Si,
|
||||
ri: ri::Ri,
|
||||
flashram: sram::Flashram,
|
||||
pub vru: vru::Vru,
|
||||
pub vru: controller::vru::Vru,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
@@ -192,6 +191,7 @@ impl Device {
|
||||
byte_swap = 3;
|
||||
}
|
||||
Device {
|
||||
netplay: None,
|
||||
ui: ui::Ui::new(config_dir),
|
||||
byte_swap,
|
||||
cpu: cpu::Cpu {
|
||||
@@ -288,6 +288,7 @@ impl Device {
|
||||
rx_buf: None,
|
||||
process: None,
|
||||
pak_handler: None,
|
||||
change_pak: controller::PakType::None,
|
||||
}; 5],
|
||||
},
|
||||
cart: cart_rom::Cart {
|
||||
@@ -419,7 +420,7 @@ impl Device {
|
||||
silicon_id: [sram::FLASHRAM_TYPE_ID, sram::MX29L1100_ID],
|
||||
mode: sram::FlashramMode::ReadArray,
|
||||
},
|
||||
vru: vru::Vru {
|
||||
vru: controller::vru::Vru {
|
||||
status: 0,
|
||||
voice_state: 0,
|
||||
load_offset: 0,
|
||||
|
||||
+6
-6
@@ -26,7 +26,7 @@ pub struct AiDma {
|
||||
pub duration: u64,
|
||||
}
|
||||
|
||||
pub fn get_remaining_dma_length(device: &mut device::Device) -> u64 {
|
||||
fn get_remaining_dma_length(device: &mut device::Device) -> u64 {
|
||||
if device.ai.fifo[0].duration == 0 {
|
||||
return 0;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ pub fn get_remaining_dma_length(device: &mut device::Device) -> u64 {
|
||||
dma_length & !7
|
||||
}
|
||||
|
||||
pub fn get_dma_duration(device: &device::Device) -> u64 {
|
||||
fn get_dma_duration(device: &device::Device) -> u64 {
|
||||
let samples_per_sec = device.vi.clock / (1 + device.ai.regs[AI_DACRATE_REG as usize]) as u64;
|
||||
let bytes_per_sample = 4; /* XXX: assume 16bit stereo - should depends on bitrate instead */
|
||||
let length = (device.ai.regs[AI_LEN_REG as usize] & !7) as u64;
|
||||
@@ -51,7 +51,7 @@ pub fn get_dma_duration(device: &device::Device) -> u64 {
|
||||
length * (device.cpu.clock_rate / (bytes_per_sample * samples_per_sec))
|
||||
}
|
||||
|
||||
pub fn do_dma(device: &mut device::Device) {
|
||||
fn do_dma(device: &mut device::Device) {
|
||||
device.ai.last_read = device.ai.fifo[0].length;
|
||||
|
||||
if device.ai.delayed_carry {
|
||||
@@ -71,7 +71,7 @@ pub fn do_dma(device: &mut device::Device) {
|
||||
device::mi::schedule_rcp_interrupt(device, device::mi::MI_INTR_AI);
|
||||
}
|
||||
|
||||
pub fn fifo_push(device: &mut device::Device) {
|
||||
fn fifo_push(device: &mut device::Device) {
|
||||
let duration = get_dma_duration(device);
|
||||
|
||||
if (device.ai.regs[AI_STATUS_REG as usize] & AI_STATUS_BUSY) != 0 {
|
||||
@@ -91,7 +91,7 @@ pub fn fifo_push(device: &mut device::Device) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fifo_pop(device: &mut device::Device) {
|
||||
fn fifo_pop(device: &mut device::Device) {
|
||||
if device.ai.regs[AI_STATUS_REG as usize] & AI_STATUS_FULL != 0 {
|
||||
device.ai.fifo[0].address = device.ai.fifo[1].address;
|
||||
device.ai.fifo[0].length = device.ai.fifo[1].length;
|
||||
@@ -152,7 +152,7 @@ pub fn write_regs(device: &mut device::Device, address: u64, value: u32, mask: u
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dma_event(device: &mut device::Device) {
|
||||
fn dma_event(device: &mut device::Device) {
|
||||
if device.ai.last_read != 0 {
|
||||
let diff = device.ai.fifo[0].length - device.ai.last_read;
|
||||
ui::audio::play_audio(
|
||||
|
||||
+1
-1
@@ -205,7 +205,7 @@ pub fn dcache_writeback(device: &mut device::Device, line_index: usize) {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn dcache_fill(device: &mut device::Device, line_index: usize, phys_address: u64) {
|
||||
fn dcache_fill(device: &mut device::Device, line_index: usize, phys_address: u64) {
|
||||
device::cop0::add_cycles(device, 7);
|
||||
|
||||
device.memory.dcache[line_index].valid = true;
|
||||
|
||||
+8
-8
@@ -16,13 +16,13 @@ const JDT_AF_RTC: u16 = 0x1000; /* RTC */
|
||||
const JDT_EEPROM_4K: u16 = 0x8000; /* 4k EEPROM */
|
||||
const JDT_EEPROM_16K: u16 = 0xc000; /* 16k EEPROM */
|
||||
const EEPROM_BLOCK_SIZE: usize = 8;
|
||||
const EEPROM_MAX_SIZE: usize = 0x800;
|
||||
pub const EEPROM_MAX_SIZE: usize = 0x800;
|
||||
|
||||
pub struct AfRtc {
|
||||
pub control: u16,
|
||||
}
|
||||
|
||||
pub fn byte2bcd(mut n: u32) -> u8 {
|
||||
fn byte2bcd(mut n: u32) -> u8 {
|
||||
n %= 100;
|
||||
(((n / 10) << 4) | (n % 10)) as u8
|
||||
}
|
||||
@@ -95,13 +95,13 @@ pub fn process(device: &mut device::Device, channel: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_eeprom(device: &mut device::Device) {
|
||||
fn format_eeprom(device: &mut device::Device) {
|
||||
if device.ui.saves.eeprom.0.len() < EEPROM_MAX_SIZE {
|
||||
device.ui.saves.eeprom.0.resize(EEPROM_MAX_SIZE, 0xFF)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eeprom_read_block(device: &mut device::Device, block: usize, offset: usize) {
|
||||
fn eeprom_read_block(device: &mut device::Device, block: usize, offset: usize) {
|
||||
let address = device.pif.ram[block] as usize * EEPROM_BLOCK_SIZE;
|
||||
|
||||
format_eeprom(device);
|
||||
@@ -110,7 +110,7 @@ pub fn eeprom_read_block(device: &mut device::Device, block: usize, offset: usiz
|
||||
.copy_from_slice(&device.ui.saves.eeprom.0[address..address + EEPROM_BLOCK_SIZE]);
|
||||
}
|
||||
|
||||
pub fn eeprom_write_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
|
||||
fn eeprom_write_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
|
||||
let address = device.pif.ram[block] as usize * EEPROM_BLOCK_SIZE;
|
||||
|
||||
format_eeprom(device);
|
||||
@@ -123,7 +123,7 @@ pub fn eeprom_write_block(device: &mut device::Device, block: usize, offset: usi
|
||||
device.ui.saves.eeprom.1 = true
|
||||
}
|
||||
|
||||
pub fn time2data(device: &mut device::Device, offset: usize) {
|
||||
fn time2data(device: &mut device::Device, offset: usize) {
|
||||
let now: chrono::DateTime<chrono::Local> = chrono::Local::now();
|
||||
|
||||
device.pif.ram[offset] = byte2bcd(now.second());
|
||||
@@ -136,7 +136,7 @@ pub fn time2data(device: &mut device::Device, offset: usize) {
|
||||
device.pif.ram[offset + 7] = byte2bcd((now.year() as u32 - 1900) / 100);
|
||||
}
|
||||
|
||||
pub fn af_rtc_read_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
|
||||
fn af_rtc_read_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
|
||||
match device.pif.ram[block] {
|
||||
0 => {
|
||||
device.pif.ram[offset] = device.cart.rtc.control as u8;
|
||||
@@ -155,7 +155,7 @@ pub fn af_rtc_read_block(device: &mut device::Device, block: usize, offset: usiz
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn af_rtc_write_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
|
||||
fn af_rtc_write_block(device: &mut device::Device, block: usize, offset: usize, status: usize) {
|
||||
match device.pif.ram[block] {
|
||||
0 => {
|
||||
device.cart.rtc.control =
|
||||
|
||||
@@ -144,7 +144,7 @@ pub fn load_rom_save(device: &mut device::Device) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_system_region(device: &mut device::Device, country: u8) {
|
||||
fn set_system_region(device: &mut device::Device, country: u8) {
|
||||
let pal_codes: [u8; 8] = [b'D', b'F', b'I', b'P', b'S', b'U', b'X', b'Y'];
|
||||
for i in pal_codes {
|
||||
if country == i {
|
||||
@@ -153,7 +153,7 @@ pub fn set_system_region(device: &mut device::Device, country: u8) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_cic(device: &mut device::Device) {
|
||||
fn set_cic(device: &mut device::Device) {
|
||||
let hash = calculate_hash(&device.cart.rom[0x40..0x1000]);
|
||||
match hash.as_str() {
|
||||
"B99F06C4802C2377E31E388435955EF3E99C618A6D55D24699D828EB1075F1EB" => {
|
||||
@@ -195,7 +195,7 @@ pub fn set_cic(device: &mut device::Device) {
|
||||
}
|
||||
}
|
||||
|
||||
fn calculate_hash(data: &[u8]) -> String {
|
||||
pub fn calculate_hash(data: &[u8]) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(data);
|
||||
format!("{:X}", hasher.finalize())
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
use crate::device;
|
||||
use crate::netplay;
|
||||
use crate::ui;
|
||||
|
||||
pub mod mempak;
|
||||
pub mod rumble;
|
||||
pub mod vru;
|
||||
|
||||
pub const JCMD_STATUS: u8 = 0x00;
|
||||
pub const JCMD_CONTROLLER_READ: u8 = 0x01;
|
||||
const JCMD_PAK_READ: u8 = 0x02;
|
||||
@@ -12,14 +17,22 @@ const JDT_JOY_ABS_COUNTERS: u16 = 0x0001; /* joystick with absolute coordinates
|
||||
//const JDT_JOY_REL_COUNTERS: u16 = 0x0002; /* joystick with relative coordinates (= mouse) */
|
||||
const JDT_JOY_PORT: u16 = 0x0004; /* has port for external paks */
|
||||
const PAK_CHUNK_SIZE: usize = 0x20;
|
||||
const CONT_STATUS_PAK_NOT_PRESENT: u8 = 0;
|
||||
const CONT_STATUS_PAK_PRESENT: u8 = 1;
|
||||
const CONT_STATUS_PAK_NOT_PRESENT: u8 = 2;
|
||||
const CONT_FLAVOR: u16 = JDT_JOY_ABS_COUNTERS | JDT_JOY_PORT;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum PakType {
|
||||
None = 0,
|
||||
MemPak = 1,
|
||||
RumblePak = 2,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct PakHandler {
|
||||
pub read: fn(&mut device::Device, usize, u16, usize, usize),
|
||||
pub write: fn(&mut device::Device, usize, u16, usize, usize),
|
||||
pub pak_type: PakType,
|
||||
}
|
||||
|
||||
pub fn process(device: &mut device::Device, channel: usize) {
|
||||
@@ -40,8 +53,34 @@ pub fn process(device: &mut device::Device, channel: usize) {
|
||||
}
|
||||
JCMD_CONTROLLER_READ => {
|
||||
let offset = device.pif.channels[channel].rx_buf.unwrap();
|
||||
let input = ui::input::get(&mut device.ui, channel);
|
||||
device.pif.ram[offset..offset + 4].copy_from_slice(&input.to_ne_bytes());
|
||||
let input = if device.netplay.is_none() {
|
||||
ui::input::get(&mut device.ui, channel)
|
||||
} else {
|
||||
if device.netplay.as_ref().unwrap().player_number as usize == channel {
|
||||
let local_input = ui::input::get(&mut device.ui, 0);
|
||||
netplay::send_input(device.netplay.as_ref().unwrap(), local_input);
|
||||
}
|
||||
|
||||
netplay::get_input(device, channel)
|
||||
};
|
||||
|
||||
device.pif.ram[offset..offset + 4].copy_from_slice(&input.0.to_ne_bytes());
|
||||
if input.1 {
|
||||
// pak change button pressed
|
||||
if device::events::get_event(device, device::events::EventType::PakSwitch).is_none()
|
||||
{
|
||||
device.pif.channels[channel].change_pak =
|
||||
device.pif.channels[channel].pak_handler.unwrap().pak_type;
|
||||
device.pif.channels[channel].pak_handler = None;
|
||||
device::events::create_event(
|
||||
device,
|
||||
device::events::EventType::PakSwitch,
|
||||
device.cpu.cop0.regs[device::cop0::COP0_COUNT_REG as usize]
|
||||
+ (device.cpu.clock_rate), // 1 second
|
||||
pak_switch_event,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
JCMD_PAK_READ => pak_read_block(
|
||||
device,
|
||||
@@ -61,7 +100,7 @@ pub fn process(device: &mut device::Device, channel: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pak_read_block(
|
||||
fn pak_read_block(
|
||||
device: &mut device::Device,
|
||||
addr_acrc: usize,
|
||||
data: usize,
|
||||
@@ -80,7 +119,7 @@ pub fn pak_read_block(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pak_write_block(
|
||||
fn pak_write_block(
|
||||
device: &mut device::Device,
|
||||
addr_acrc: usize,
|
||||
data: usize,
|
||||
@@ -99,7 +138,7 @@ pub fn pak_write_block(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn data_crc(device: &device::Device, data_offset: usize, size: usize) -> u8 {
|
||||
fn data_crc(device: &device::Device, data_offset: usize, size: usize) -> u8 {
|
||||
let mut i = 0;
|
||||
let mut crc = 0;
|
||||
|
||||
@@ -118,3 +157,34 @@ pub fn data_crc(device: &device::Device, data_offset: usize, size: usize) -> u8
|
||||
}
|
||||
crc
|
||||
}
|
||||
|
||||
fn pak_switch_event(device: &mut device::Device) {
|
||||
for (i, channel) in device.pif.channels.iter_mut().enumerate() {
|
||||
if channel.change_pak != PakType::None {
|
||||
if channel.change_pak == PakType::RumblePak {
|
||||
if device.netplay.is_none() {
|
||||
device::ui::input::set_rumble(&mut device.ui, i, 0);
|
||||
} else if device.netplay.as_ref().unwrap().player_number as usize == i {
|
||||
device::ui::input::set_rumble(&mut device.ui, 0, 0);
|
||||
}
|
||||
|
||||
let handler = device::controller::PakHandler {
|
||||
read: device::controller::mempak::read,
|
||||
write: device::controller::mempak::write,
|
||||
pak_type: device::controller::PakType::MemPak,
|
||||
};
|
||||
channel.pak_handler = Some(handler);
|
||||
ui::audio::play_pak_switch(&mut device.ui, PakType::MemPak);
|
||||
} else if channel.change_pak == PakType::MemPak {
|
||||
let handler = device::controller::PakHandler {
|
||||
read: device::controller::rumble::read,
|
||||
write: device::controller::rumble::write,
|
||||
pak_type: device::controller::PakType::RumblePak,
|
||||
};
|
||||
channel.pak_handler = Some(handler);
|
||||
ui::audio::play_pak_switch(&mut device.ui, PakType::RumblePak);
|
||||
}
|
||||
channel.change_pak = PakType::None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::device;
|
||||
|
||||
const MEMPAK_SIZE: usize = 0x8000;
|
||||
pub const MEMPAK_SIZE: usize = 0x8000;
|
||||
const MPK_PAGE_SIZE: usize = 256;
|
||||
|
||||
pub fn format_mempak(device: &mut device::Device) {
|
||||
fn format_mempak(device: &mut device::Device) {
|
||||
if device.ui.saves.mempak.0.len() < MEMPAK_SIZE * 4 {
|
||||
device.ui.saves.mempak.0.resize(MEMPAK_SIZE * 4, 0);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
use crate::device;
|
||||
|
||||
pub fn read(device: &mut device::Device, _channel: usize, address: u16, data: usize, size: usize) {
|
||||
let value: u8 = if (0x8000..0x9000).contains(&address) {
|
||||
0x80
|
||||
} else {
|
||||
0x00
|
||||
};
|
||||
|
||||
for i in 0..size {
|
||||
device.pif.ram[data + i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(device: &mut device::Device, channel: usize, address: u16, data: usize, size: usize) {
|
||||
if address == 0xc000 {
|
||||
let rumble = device.pif.ram[data + size - 1];
|
||||
if device.netplay.is_none() {
|
||||
device::ui::input::set_rumble(&mut device.ui, channel, rumble);
|
||||
} else if device.netplay.as_ref().unwrap().player_number as usize == channel {
|
||||
device::ui::input::set_rumble(&mut device.ui, 0, rumble);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,8 @@ pub struct Vru {
|
||||
pub words: Vec<String>,
|
||||
pub talking: bool,
|
||||
pub word_mappings: HashMap<String, String>,
|
||||
pub window_notifier: Option<std::sync::mpsc::Sender<Vec<String>>>,
|
||||
pub word_receiver: Option<std::sync::mpsc::Receiver<String>>,
|
||||
pub window_notifier: Option<tokio::sync::mpsc::Sender<Vec<String>>>,
|
||||
pub word_receiver: Option<tokio::sync::mpsc::Receiver<String>>,
|
||||
pub gui_ctx: Option<egui::Context>,
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ pub fn init(device: &mut device::Device) {
|
||||
create_word_mappings(device);
|
||||
}
|
||||
|
||||
pub fn reset_vru(device: &mut device::Device) {
|
||||
fn reset_vru(device: &mut device::Device) {
|
||||
device.vru.status = 0x00;
|
||||
if device.cart.rom[0x3E] == /* Japan */ 0x4A || device.cart.rom[0x3E] == /* Demo */ 0x00 {
|
||||
device.vru.voice_state = VOICE_STATUS_READY;
|
||||
@@ -187,7 +187,7 @@ pub fn process(device: &mut device::Device, channel: usize) {
|
||||
ui::vru::prompt_for_match(
|
||||
&device.vru.words,
|
||||
device.vru.window_notifier.as_ref().unwrap(),
|
||||
device.vru.word_receiver.as_ref().unwrap(),
|
||||
device.vru.word_receiver.as_mut().unwrap(),
|
||||
device.vru.gui_ctx.as_ref().unwrap(),
|
||||
)
|
||||
} else {
|
||||
@@ -250,11 +250,11 @@ pub fn process(device: &mut device::Device, channel: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vru_talking_event(device: &mut device::Device) {
|
||||
fn vru_talking_event(device: &mut device::Device) {
|
||||
device.vru.talking = false
|
||||
}
|
||||
|
||||
pub fn create_word_mappings(device: &mut device::Device) {
|
||||
fn create_word_mappings(device: &mut device::Device) {
|
||||
device.vru.word_mappings = HashMap::from([
|
||||
(
|
||||
String::from("03A50024000303CF00A80003036000EA"),
|
||||
+14
-14
@@ -99,18 +99,18 @@ pub struct Cop0 {
|
||||
pub tlb_entries: [device::tlb::TlbEntry; 32],
|
||||
}
|
||||
|
||||
pub fn mfc0(device: &mut device::Device, opcode: u32) {
|
||||
fn mfc0(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.gpr[device::cpu_instructions::rt(opcode) as usize] = device::cpu_instructions::se32(
|
||||
(get_control_registers(device, device::cpu_instructions::rd(opcode))) as u32 as i32,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dmfc0(device: &mut device::Device, opcode: u32) {
|
||||
fn dmfc0(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.gpr[device::cpu_instructions::rt(opcode) as usize] =
|
||||
get_control_registers(device, device::cpu_instructions::rd(opcode))
|
||||
}
|
||||
|
||||
pub fn mtc0(device: &mut device::Device, opcode: u32) {
|
||||
fn mtc0(device: &mut device::Device, opcode: u32) {
|
||||
device::cop0::set_control_registers(
|
||||
device,
|
||||
device::cpu_instructions::rd(opcode),
|
||||
@@ -120,7 +120,7 @@ pub fn mtc0(device: &mut device::Device, opcode: u32) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dmtc0(device: &mut device::Device, opcode: u32) {
|
||||
fn dmtc0(device: &mut device::Device, opcode: u32) {
|
||||
device::cop0::set_control_registers(
|
||||
device,
|
||||
device::cpu_instructions::rd(opcode),
|
||||
@@ -128,24 +128,24 @@ pub fn dmtc0(device: &mut device::Device, opcode: u32) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn tlbr(device: &mut device::Device, _opcode: u32) {
|
||||
fn tlbr(device: &mut device::Device, _opcode: u32) {
|
||||
device::tlb::read(device, device.cpu.cop0.regs[COP0_INDEX_REG as usize])
|
||||
}
|
||||
|
||||
pub fn tlbwi(device: &mut device::Device, _opcode: u32) {
|
||||
fn tlbwi(device: &mut device::Device, _opcode: u32) {
|
||||
device::tlb::write(device, device.cpu.cop0.regs[COP0_INDEX_REG as usize])
|
||||
}
|
||||
|
||||
pub fn tlbwr(device: &mut device::Device, _opcode: u32) {
|
||||
fn tlbwr(device: &mut device::Device, _opcode: u32) {
|
||||
let random = set_random_register(device);
|
||||
device::tlb::write(device, random)
|
||||
}
|
||||
|
||||
pub fn tlbp(device: &mut device::Device, _opcode: u32) {
|
||||
fn tlbp(device: &mut device::Device, _opcode: u32) {
|
||||
device::tlb::probe(device)
|
||||
}
|
||||
|
||||
pub fn eret(device: &mut device::Device, _opcode: u32) {
|
||||
fn eret(device: &mut device::Device, _opcode: u32) {
|
||||
if device.cpu.cop0.regs[COP0_STATUS_REG as usize] & COP0_STATUS_ERL != 0 {
|
||||
device.cpu.pc = device.cpu.cop0.regs[COP0_ERROREPC_REG as usize];
|
||||
device.cpu.cop0.regs[COP0_STATUS_REG as usize] &= !COP0_STATUS_ERL
|
||||
@@ -158,7 +158,7 @@ pub fn eret(device: &mut device::Device, _opcode: u32) {
|
||||
device::exceptions::check_pending_interrupts(device)
|
||||
}
|
||||
|
||||
pub fn execute_cp0(device: &mut device::Device, opcode: u32) {
|
||||
fn execute_cp0(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop0.instrs2[(opcode & 0x3F) as usize](device, opcode)
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ pub fn reserved(device: &mut device::Device, _opcode: u32) {
|
||||
device::exceptions::reserved_exception(device, 0);
|
||||
}
|
||||
|
||||
pub fn get_control_registers(device: &device::Device, index: u32) -> u64 {
|
||||
fn get_control_registers(device: &device::Device, index: u32) -> u64 {
|
||||
match index {
|
||||
COP0_COUNT_REG => device.cpu.cop0.regs[index as usize] >> 1,
|
||||
COP0_RANDOM_REG => set_random_register(device),
|
||||
@@ -183,7 +183,7 @@ pub fn get_control_registers(device: &device::Device, index: u32) -> u64 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_control_registers(device: &mut device::Device, index: u32, mut data: u64) {
|
||||
fn set_control_registers(device: &mut device::Device, index: u32, mut data: u64) {
|
||||
device.cpu.cop0.reg_latch = data;
|
||||
match index {
|
||||
COP0_COUNT_REG => {
|
||||
@@ -230,7 +230,7 @@ pub fn set_control_registers(device: &mut device::Device, index: u32, mut data:
|
||||
device::exceptions::check_pending_interrupts(device);
|
||||
}
|
||||
|
||||
pub fn compare_event(device: &mut device::Device) {
|
||||
fn compare_event(device: &mut device::Device) {
|
||||
device.cpu.cop0.regs[COP0_CAUSE_REG as usize] &= !COP0_CAUSE_EXCCODE_MASK;
|
||||
device.cpu.cop0.regs[COP0_CAUSE_REG as usize] |= COP0_CAUSE_IP7;
|
||||
|
||||
@@ -243,7 +243,7 @@ pub fn compare_event(device: &mut device::Device) {
|
||||
device::exceptions::check_pending_interrupts(device);
|
||||
}
|
||||
|
||||
pub fn set_random_register(device: &device::Device) -> u64 {
|
||||
fn set_random_register(device: &device::Device) -> u64 {
|
||||
if device.cpu.cop0.regs[COP0_WIRED_REG as usize] > 31 {
|
||||
(u64::MAX - device.cpu.cop0.regs[COP0_COUNT_REG as usize]) & 0x3F
|
||||
} else {
|
||||
|
||||
+16
-16
@@ -173,7 +173,7 @@ pub fn sdc1(device: &mut device::Device, opcode: u32) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn mfc1(device: &mut device::Device, opcode: u32) {
|
||||
fn mfc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -185,7 +185,7 @@ pub fn mfc1(device: &mut device::Device, opcode: u32) {
|
||||
device::cpu_instructions::se32(u32::from_ne_bytes(value.to_ne_bytes()) as i32);
|
||||
}
|
||||
|
||||
pub fn dmfc1(device: &mut device::Device, opcode: u32) {
|
||||
fn dmfc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -197,7 +197,7 @@ pub fn dmfc1(device: &mut device::Device, opcode: u32) {
|
||||
u64::from_ne_bytes(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
pub fn cfc1(device: &mut device::Device, opcode: u32) {
|
||||
fn cfc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -209,7 +209,7 @@ pub fn cfc1(device: &mut device::Device, opcode: u32) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dcfc1(device: &mut device::Device, opcode: u32) {
|
||||
fn dcfc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -221,7 +221,7 @@ pub fn dcfc1(device: &mut device::Device, opcode: u32) {
|
||||
device::exceptions::floating_point_exception(device)
|
||||
}
|
||||
|
||||
pub fn mtc1(device: &mut device::Device, opcode: u32) {
|
||||
fn mtc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -239,7 +239,7 @@ pub fn mtc1(device: &mut device::Device, opcode: u32) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dmtc1(device: &mut device::Device, opcode: u32) {
|
||||
fn dmtc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -252,7 +252,7 @@ pub fn dmtc1(device: &mut device::Device, opcode: u32) {
|
||||
set_fpr_double(device, device::fpu_instructions::fs(opcode) as usize, value)
|
||||
}
|
||||
|
||||
pub fn ctc1(device: &mut device::Device, opcode: u32) {
|
||||
fn ctc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -266,7 +266,7 @@ pub fn ctc1(device: &mut device::Device, opcode: u32) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dctc1(device: &mut device::Device, opcode: u32) {
|
||||
fn dctc1(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -278,7 +278,7 @@ pub fn dctc1(device: &mut device::Device, opcode: u32) {
|
||||
device::exceptions::floating_point_exception(device)
|
||||
}
|
||||
|
||||
pub fn execute_cop1_b(device: &mut device::Device, opcode: u32) {
|
||||
fn execute_cop1_b(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -288,7 +288,7 @@ pub fn execute_cop1_b(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop1.b_instrs[((opcode >> 16) & 0x3) as usize](device, opcode)
|
||||
}
|
||||
|
||||
pub fn execute_cop1_s(device: &mut device::Device, opcode: u32) {
|
||||
fn execute_cop1_s(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -298,7 +298,7 @@ pub fn execute_cop1_s(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop1.s_instrs[(opcode & 0x3F) as usize](device, opcode)
|
||||
}
|
||||
|
||||
pub fn execute_cop1_d(device: &mut device::Device, opcode: u32) {
|
||||
fn execute_cop1_d(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -308,7 +308,7 @@ pub fn execute_cop1_d(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop1.d_instrs[(opcode & 0x3F) as usize](device, opcode)
|
||||
}
|
||||
|
||||
pub fn execute_cop1_l(device: &mut device::Device, opcode: u32) {
|
||||
fn execute_cop1_l(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -318,7 +318,7 @@ pub fn execute_cop1_l(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop1.l_instrs[(opcode & 0x3F) as usize](device, opcode)
|
||||
}
|
||||
|
||||
pub fn execute_cop1_w(device: &mut device::Device, opcode: u32) {
|
||||
fn execute_cop1_w(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU1
|
||||
== 0
|
||||
{
|
||||
@@ -328,7 +328,7 @@ pub fn execute_cop1_w(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop1.w_instrs[(opcode & 0x3F) as usize](device, opcode)
|
||||
}
|
||||
|
||||
pub fn unusable(device: &mut device::Device, _opcode: u32) {
|
||||
fn unusable(device: &mut device::Device, _opcode: u32) {
|
||||
device::exceptions::cop_unusable_exception(device, device::cop0::COP0_CAUSE_CE1)
|
||||
}
|
||||
|
||||
@@ -342,7 +342,7 @@ pub fn reserved(device: &mut device::Device, opcode: u32) {
|
||||
device::exceptions::reserved_exception(device, device::cop0::COP0_CAUSE_CE1);
|
||||
}
|
||||
|
||||
pub fn get_control_registers_fpu(device: &device::Device, index: u32) -> u32 {
|
||||
fn get_control_registers_fpu(device: &device::Device, index: u32) -> u32 {
|
||||
match index {
|
||||
0 => device.cpu.cop1.fcr0,
|
||||
31 => device.cpu.cop1.fcr31,
|
||||
@@ -352,7 +352,7 @@ pub fn get_control_registers_fpu(device: &device::Device, index: u32) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_control_registers_fpu(device: &mut device::Device, index: u32, data: u32) {
|
||||
fn set_control_registers_fpu(device: &mut device::Device, index: u32, data: u32) {
|
||||
match index {
|
||||
0 => { // read only
|
||||
}
|
||||
|
||||
+7
-7
@@ -5,7 +5,7 @@ pub struct Cop2 {
|
||||
pub reg_latch: u64,
|
||||
}
|
||||
|
||||
pub fn mfc2(device: &mut device::Device, opcode: u32) {
|
||||
fn mfc2(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU2
|
||||
== 0
|
||||
{
|
||||
@@ -15,7 +15,7 @@ pub fn mfc2(device: &mut device::Device, opcode: u32) {
|
||||
device::cpu_instructions::se32((device.cpu.cop2.reg_latch) as u32 as i32)
|
||||
}
|
||||
|
||||
pub fn dmfc2(device: &mut device::Device, opcode: u32) {
|
||||
fn dmfc2(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU2
|
||||
== 0
|
||||
{
|
||||
@@ -24,7 +24,7 @@ pub fn dmfc2(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.gpr[device::cpu_instructions::rt(opcode) as usize] = device.cpu.cop2.reg_latch
|
||||
}
|
||||
|
||||
pub fn cfc2(device: &mut device::Device, opcode: u32) {
|
||||
fn cfc2(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU2
|
||||
== 0
|
||||
{
|
||||
@@ -34,7 +34,7 @@ pub fn cfc2(device: &mut device::Device, opcode: u32) {
|
||||
device::cpu_instructions::se32((device.cpu.cop2.reg_latch) as u32 as i32)
|
||||
}
|
||||
|
||||
pub fn mtc2(device: &mut device::Device, opcode: u32) {
|
||||
fn mtc2(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU2
|
||||
== 0
|
||||
{
|
||||
@@ -43,7 +43,7 @@ pub fn mtc2(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop2.reg_latch = device.cpu.gpr[device::cpu_instructions::rt(opcode) as usize]
|
||||
}
|
||||
|
||||
pub fn dmtc2(device: &mut device::Device, opcode: u32) {
|
||||
fn dmtc2(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU2
|
||||
== 0
|
||||
{
|
||||
@@ -52,7 +52,7 @@ pub fn dmtc2(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.cop2.reg_latch = device.cpu.gpr[device::cpu_instructions::rt(opcode) as usize]
|
||||
}
|
||||
|
||||
pub fn ctc2(device: &mut device::Device, opcode: u32) {
|
||||
fn ctc2(device: &mut device::Device, opcode: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_CU2
|
||||
== 0
|
||||
{
|
||||
@@ -72,7 +72,7 @@ pub fn reserved(device: &mut device::Device, opcode: u32) {
|
||||
device::exceptions::reserved_exception(device, device::cop0::COP0_CAUSE_CE2);
|
||||
}
|
||||
|
||||
pub fn unusable(device: &mut device::Device, _opcode: u32) {
|
||||
fn unusable(device: &mut device::Device, _opcode: u32) {
|
||||
device::exceptions::cop_unusable_exception(device, device::cop0::COP0_CAUSE_CE2)
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn rt(opcode: u32) -> u32 {
|
||||
(opcode >> 16) & 0x1F
|
||||
}
|
||||
|
||||
pub fn sa(opcode: u32) -> u32 {
|
||||
fn sa(opcode: u32) -> u32 {
|
||||
(opcode >> 6) & 0x1F
|
||||
}
|
||||
|
||||
@@ -28,23 +28,23 @@ pub fn se16(value: i16) -> u64 {
|
||||
value as i64 as u64
|
||||
}
|
||||
|
||||
pub fn se8(value: i8) -> u64 {
|
||||
fn se8(value: i8) -> u64 {
|
||||
value as i64 as u64
|
||||
}
|
||||
|
||||
pub fn bshift<T: Into<u64>>(address: T) -> u64 {
|
||||
fn bshift<T: Into<u64>>(address: T) -> u64 {
|
||||
((address.into() & 3) ^ 3) << 3
|
||||
}
|
||||
|
||||
pub fn hshift<T: Into<u64>>(address: T) -> u64 {
|
||||
fn hshift<T: Into<u64>>(address: T) -> u64 {
|
||||
((address.into() & 2) ^ 2) << 3
|
||||
}
|
||||
|
||||
pub fn bits_below_mask<T: Into<u64>>(x: T) -> u64 {
|
||||
fn bits_below_mask<T: Into<u64>>(x: T) -> u64 {
|
||||
(1 << x.into()) - 1
|
||||
}
|
||||
|
||||
pub fn bits_above_mask<T: Into<u64>>(x: T) -> u64 {
|
||||
fn bits_above_mask<T: Into<u64>>(x: T) -> u64 {
|
||||
!bits_below_mask(x)
|
||||
}
|
||||
|
||||
@@ -60,8 +60,8 @@ pub fn check_relative_idle_loop(device: &mut device::Device, opcode: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_absolute_idle_loop(device: &mut device::Device, opcode: u32) {
|
||||
if (opcode & 0x3FFFFFF) as u64 == (device.cpu.pc_phys & 0x0FFFFFFF) >> 2
|
||||
fn check_absolute_idle_loop(device: &mut device::Device, target: u64) {
|
||||
if device.cpu.pc == target
|
||||
&& device.memory.fast_read[(device.cpu.pc_phys >> 16) as usize](
|
||||
device,
|
||||
device.cpu.pc_phys + 4,
|
||||
@@ -76,10 +76,10 @@ pub fn j(device: &mut device::Device, opcode: u32) {
|
||||
if device::cpu::in_delay_slot_taken(device) {
|
||||
return;
|
||||
}
|
||||
check_absolute_idle_loop(device, opcode);
|
||||
let target = (device.cpu.pc + 4) & 0xFFFFFFFFF0000000 | ((opcode & 0x3FFFFFF) << 2) as u64;
|
||||
check_absolute_idle_loop(device, target);
|
||||
device.cpu.branch_state.state = device::cpu::State::Take;
|
||||
device.cpu.branch_state.pc =
|
||||
(device.cpu.pc + 4) & 0xFFFFFFFFF0000000 | ((opcode & 0x3FFFFFF) << 2) as u64
|
||||
device.cpu.branch_state.pc = target
|
||||
}
|
||||
|
||||
pub fn jal(device: &mut device::Device, opcode: u32) {
|
||||
@@ -89,10 +89,10 @@ pub fn jal(device: &mut device::Device, opcode: u32) {
|
||||
device.cpu.gpr[31] = device.cpu.pc + 8
|
||||
}
|
||||
if !device::cpu::in_delay_slot_taken(device) {
|
||||
check_absolute_idle_loop(device, opcode);
|
||||
let target = (device.cpu.pc + 4) & 0xFFFFFFFFF0000000 | ((opcode & 0x3FFFFFF) << 2) as u64;
|
||||
check_absolute_idle_loop(device, target);
|
||||
device.cpu.branch_state.state = device::cpu::State::Take;
|
||||
device.cpu.branch_state.pc =
|
||||
(device.cpu.pc + 4) & 0xFFFFFFFFF0000000 | ((opcode & 0x3FFFFFF) << 2) as u64
|
||||
device.cpu.branch_state.pc = target
|
||||
} else if !device::cpu::in_delay_slot(device) {
|
||||
device.cpu.branch_state.state = device::cpu::State::NotTaken;
|
||||
}
|
||||
@@ -966,7 +966,6 @@ pub fn srav(device: &mut device::Device, opcode: u32) {
|
||||
|
||||
pub fn jr(device: &mut device::Device, opcode: u32) {
|
||||
if !device::cpu::in_delay_slot_taken(device) {
|
||||
check_relative_idle_loop(device, opcode);
|
||||
device.cpu.branch_state.state = device::cpu::State::Take;
|
||||
device.cpu.branch_state.pc = device.cpu.gpr[rs(opcode) as usize];
|
||||
} else if !device::cpu::in_delay_slot(device) {
|
||||
@@ -978,7 +977,6 @@ pub fn jalr(device: &mut device::Device, opcode: u32) {
|
||||
let in_delay_slot_taken = device::cpu::in_delay_slot_taken(device);
|
||||
|
||||
if !in_delay_slot_taken {
|
||||
check_relative_idle_loop(device, opcode);
|
||||
device.cpu.branch_state.state = device::cpu::State::Take;
|
||||
device.cpu.branch_state.pc = device.cpu.gpr[rs(opcode) as usize];
|
||||
} else if !device::cpu::in_delay_slot(device) {
|
||||
|
||||
@@ -12,6 +12,7 @@ pub enum EventType {
|
||||
SPDma,
|
||||
Compare,
|
||||
Vru,
|
||||
PakSwitch,
|
||||
Count,
|
||||
}
|
||||
|
||||
@@ -56,7 +57,7 @@ pub fn trigger_event(device: &mut device::Device) {
|
||||
set_next_event(device);
|
||||
}
|
||||
|
||||
pub fn set_next_event(device: &mut device::Device) {
|
||||
fn set_next_event(device: &mut device::Device) {
|
||||
device.cpu.next_event_count = u64::MAX;
|
||||
for (pos, i) in device.cpu.events.iter().enumerate() {
|
||||
if i.enabled && i.count < device.cpu.next_event_count {
|
||||
|
||||
@@ -133,7 +133,7 @@ pub fn tlb_miss_exception(
|
||||
exception_general(device, vector_offset)
|
||||
}
|
||||
|
||||
pub fn exception_general(device: &mut device::Device, vector_offset: u32) {
|
||||
fn exception_general(device: &mut device::Device, vector_offset: u32) {
|
||||
if device.cpu.cop0.regs[device::cop0::COP0_STATUS_REG as usize] & device::cop0::COP0_STATUS_EXL
|
||||
== 0
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ pub fn ft(opcode: u32) -> u32 {
|
||||
(opcode >> 16) & 0x1F
|
||||
}
|
||||
|
||||
pub fn fd(opcode: u32) -> u32 {
|
||||
fn fd(opcode: u32) -> u32 {
|
||||
(opcode >> 6) & 0x1F
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -75,7 +75,7 @@ pub fn write_regs(device: &mut device::Device, address: u64, value: u32, mask: u
|
||||
device::exceptions::check_pending_interrupts(device)
|
||||
}
|
||||
|
||||
pub fn update_init_mode(device: &mut device::Device, w: u32) {
|
||||
fn update_init_mode(device: &mut device::Device, w: u32) {
|
||||
device.mi.regs[MI_INIT_MODE_REG as usize] &= !MI_INIT_LENGTH_MASK;
|
||||
device.mi.regs[MI_INIT_MODE_REG as usize] |= w & MI_INIT_LENGTH_MASK;
|
||||
|
||||
@@ -103,7 +103,7 @@ pub fn update_init_mode(device: &mut device::Device, w: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_intr_mask(device: &mut device::Device, w: u32) {
|
||||
fn update_intr_mask(device: &mut device::Device, w: u32) {
|
||||
if w & MI_CLR_SP != 0 {
|
||||
device.mi.regs[MI_INTR_MASK_REG as usize] &= !MI_INTR_SP
|
||||
}
|
||||
|
||||
+4
-4
@@ -30,7 +30,7 @@ pub struct Pi {
|
||||
pub regs: [u32; PI_REGS_COUNT as usize],
|
||||
}
|
||||
|
||||
pub struct PiHandler {
|
||||
struct PiHandler {
|
||||
read: fn(&mut device::Device, u32, u32, u32) -> u64,
|
||||
write: fn(&mut device::Device, u32, u32, u32) -> u64,
|
||||
}
|
||||
@@ -49,7 +49,7 @@ pub fn read_regs(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dma_read(device: &mut device::Device) {
|
||||
fn dma_read(device: &mut device::Device) {
|
||||
let handler = get_handler(device.pi.regs[PI_CART_ADDR_REG as usize]);
|
||||
|
||||
let cart_addr = device.pi.regs[PI_CART_ADDR_REG as usize] & !1;
|
||||
@@ -79,7 +79,7 @@ pub fn dma_read(device: &mut device::Device) {
|
||||
device.pi.regs[PI_STATUS_REG as usize] |= PI_STATUS_DMA_BUSY
|
||||
}
|
||||
|
||||
pub fn dma_write(device: &mut device::Device) {
|
||||
fn dma_write(device: &mut device::Device) {
|
||||
let handler = get_handler(device.pi.regs[PI_CART_ADDR_REG as usize]);
|
||||
|
||||
let cart_addr = device.pi.regs[PI_CART_ADDR_REG as usize] & !1;
|
||||
@@ -112,7 +112,7 @@ pub fn dma_write(device: &mut device::Device) {
|
||||
device.pi.regs[PI_STATUS_REG as usize] |= PI_STATUS_DMA_BUSY
|
||||
}
|
||||
|
||||
pub fn get_handler(address: u32) -> PiHandler {
|
||||
fn get_handler(address: u32) -> PiHandler {
|
||||
let mut handler = PiHandler {
|
||||
read: device::cart_rom::dma_read,
|
||||
write: device::cart_rom::dma_write,
|
||||
|
||||
+18
-10
@@ -15,6 +15,7 @@ pub struct PifChannel {
|
||||
pub rx_buf: Option<usize>,
|
||||
pub process: Option<fn(&mut device::Device, usize)>,
|
||||
pub pak_handler: Option<device::controller::PakHandler>,
|
||||
pub change_pak: device::controller::PakType,
|
||||
}
|
||||
|
||||
pub const PIF_RAM_SIZE: usize = 64;
|
||||
@@ -72,7 +73,7 @@ pub fn write_mem(device: &mut device::Device, address: u64, value: u32, mask: u3
|
||||
device::si::SI_STATUS_DMA_BUSY | device::si::SI_STATUS_IO_BUSY
|
||||
}
|
||||
|
||||
pub fn process_channel(device: &mut device::Device, channel: usize) -> usize {
|
||||
fn process_channel(device: &mut device::Device, channel: usize) -> usize {
|
||||
/* don't process channel if it has been disabled */
|
||||
if device.pif.channels[channel].tx.is_none() {
|
||||
return 0;
|
||||
@@ -99,17 +100,18 @@ pub fn update_pif_ram(device: &mut device::Device) -> u64 {
|
||||
for k in 0..PIF_CHANNELS_COUNT {
|
||||
active_channels += process_channel(device, k)
|
||||
}
|
||||
|
||||
(24000 + (active_channels * 30000)) as u64
|
||||
}
|
||||
|
||||
pub fn disable_pif_channel(channel: &mut PifChannel) {
|
||||
fn disable_pif_channel(channel: &mut PifChannel) {
|
||||
channel.tx = None;
|
||||
channel.rx = None;
|
||||
channel.tx_buf = None;
|
||||
channel.rx_buf = None;
|
||||
}
|
||||
|
||||
pub fn setup_pif_channel(device: &mut device::Device, channel: usize, buf: usize) -> usize {
|
||||
fn setup_pif_channel(device: &mut device::Device, channel: usize, buf: usize) -> usize {
|
||||
let tx = device.pif.ram[buf] & 0x3f;
|
||||
let rx = device.pif.ram[buf + 1] & 0x3f;
|
||||
|
||||
@@ -123,7 +125,7 @@ pub fn setup_pif_channel(device: &mut device::Device, channel: usize, buf: usize
|
||||
(2 + tx + rx) as usize
|
||||
}
|
||||
|
||||
pub fn setup_channels_format(device: &mut device::Device) {
|
||||
fn setup_channels_format(device: &mut device::Device) {
|
||||
let mut i: usize = 0;
|
||||
let mut k: usize = 0;
|
||||
while i < PIF_RAM_SIZE && k < PIF_CHANNELS_COUNT {
|
||||
@@ -225,24 +227,30 @@ pub fn init(device: &mut device::Device) {
|
||||
device.pif.ram[0x27] = device.cart.cic_seed;
|
||||
|
||||
let mempak_handler = device::controller::PakHandler {
|
||||
read: device::mempak::read,
|
||||
write: device::mempak::write,
|
||||
read: device::controller::mempak::read,
|
||||
write: device::controller::mempak::write,
|
||||
pak_type: device::controller::PakType::MemPak,
|
||||
};
|
||||
|
||||
for i in 0..4 {
|
||||
if device.ui.config.input.controller_enabled[i] {
|
||||
if device.netplay.is_none() {
|
||||
if device.ui.config.input.controller_enabled[i] {
|
||||
device.pif.channels[i].pak_handler = Some(mempak_handler);
|
||||
device.pif.channels[i].process = Some(device::controller::process);
|
||||
}
|
||||
} else if device.netplay.as_ref().unwrap().player_data[i].reg_id != 0 {
|
||||
device.pif.channels[i].pak_handler = Some(mempak_handler);
|
||||
device.pif.channels[i].process = Some(device::controller::process);
|
||||
}
|
||||
}
|
||||
if device.ui.config.input.emulate_vru {
|
||||
device.pif.channels[3].pak_handler = None;
|
||||
device.pif.channels[3].process = Some(device::vru::process);
|
||||
device.pif.channels[3].process = Some(device::controller::vru::process);
|
||||
}
|
||||
device.pif.channels[4].process = Some(device::cart::process)
|
||||
}
|
||||
|
||||
pub fn process_cic_challenge(device: &mut device::Device) {
|
||||
fn process_cic_challenge(device: &mut device::Device) {
|
||||
let mut challenge: [u8; 30] = [0; 30];
|
||||
let mut response: [u8; 30] = [0; 30];
|
||||
|
||||
@@ -263,7 +271,7 @@ pub fn process_cic_challenge(device: &mut device::Device) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n64_cic_nus_6105(chl: [u8; 30], rsp: &mut [u8; 30], len: usize) {
|
||||
fn n64_cic_nus_6105(chl: [u8; 30], rsp: &mut [u8; 30], len: usize) {
|
||||
let lut0: [u8; 0x10] = [
|
||||
0x4, 0x7, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, 0xC, 0xF, 0x8, 0xF, 0x6, 0x3, 0x6, 0x9,
|
||||
];
|
||||
|
||||
+3
-5
@@ -91,7 +91,7 @@ pub fn write_regs_dpc(device: &mut device::Device, address: u64, value: u32, mas
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_rdp(device: &mut device::Device) {
|
||||
fn run_rdp(device: &mut device::Device) {
|
||||
let timer = ui::video::process_rdp_list();
|
||||
|
||||
if timer != 0 {
|
||||
@@ -120,7 +120,7 @@ pub fn write_regs_dps(device: &mut device::Device, address: u64, value: u32, mas
|
||||
)
|
||||
}
|
||||
|
||||
pub fn update_dpc_status(device: &mut device::Device, w: u32) {
|
||||
fn update_dpc_status(device: &mut device::Device, w: u32) {
|
||||
/* clear / set xbus_dmem_dma */
|
||||
if w & DPC_CLR_XBUS_DMEM_DMA != 0 {
|
||||
device.rdp.regs_dpc[DPC_STATUS_REG as usize] &= !DPC_STATUS_XBUS_DMEM_DMA
|
||||
@@ -173,8 +173,6 @@ pub fn init(device: &mut device::Device) {
|
||||
DPC_STATUS_START_GCLK | DPC_STATUS_PIPE_BUSY | DPC_STATUS_CBUF_READY;
|
||||
}
|
||||
|
||||
pub fn rdp_interrupt_event(device: &mut device::Device) {
|
||||
ui::video::full_sync();
|
||||
|
||||
fn rdp_interrupt_event(device: &mut device::Device) {
|
||||
device::mi::set_rcp_interrupt(device, device::mi::MI_INTR_DP)
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ pub fn write_mem(device: &mut device::Device, address: u64, value: u32, _mask: u
|
||||
// SH/SB are broken: They overwrite the whole 32 bit, filling everything that isn't written with zeroes
|
||||
}
|
||||
|
||||
pub fn do_dma(device: &mut device::Device, dma: RspDma) {
|
||||
fn do_dma(device: &mut device::Device, dma: RspDma) {
|
||||
let l = dma.length;
|
||||
|
||||
let length = ((l & 0xfff) | 7) + 1;
|
||||
@@ -205,7 +205,7 @@ pub fn do_dma(device: &mut device::Device, dma: RspDma) {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn fifo_push(device: &mut device::Device, dir: DmaDir) {
|
||||
fn fifo_push(device: &mut device::Device, dir: DmaDir) {
|
||||
if device.rsp.regs[SP_DMA_FULL_REG as usize] != 0 {
|
||||
panic!("RSP DMA already full")
|
||||
}
|
||||
@@ -237,7 +237,7 @@ pub fn fifo_push(device: &mut device::Device, dir: DmaDir) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fifo_pop(device: &mut device::Device) {
|
||||
fn fifo_pop(device: &mut device::Device) {
|
||||
if device.rsp.regs[SP_DMA_FULL_REG as usize] != 0 {
|
||||
device.rsp.fifo[0].dir = device.rsp.fifo[1].dir;
|
||||
device.rsp.fifo[0].length = device.rsp.fifo[1].length;
|
||||
@@ -312,7 +312,7 @@ pub fn write_regs2(device: &mut device::Device, address: u64, value: u32, mask:
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_sp_status(device: &mut device::Device, w: u32) {
|
||||
fn update_sp_status(device: &mut device::Device, w: u32) {
|
||||
let was_halted = device.rsp.regs[SP_STATUS_REG as usize] & SP_STATUS_HALT != 0;
|
||||
|
||||
/* clear / set halt */
|
||||
@@ -426,7 +426,7 @@ pub fn update_sp_status(device: &mut device::Device, w: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_task(device: &mut device::Device) {
|
||||
fn do_task(device: &mut device::Device) {
|
||||
let timer = device::rsp_cpu::run(device);
|
||||
|
||||
device::events::create_event(
|
||||
@@ -437,7 +437,7 @@ pub fn do_task(device: &mut device::Device) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn rsp_event(device: &mut device::Device) {
|
||||
fn rsp_event(device: &mut device::Device) {
|
||||
if device.rsp.cpu.broken {
|
||||
device.rsp.regs[SP_STATUS_REG as usize] |= SP_STATUS_HALT | SP_STATUS_BROKE;
|
||||
|
||||
|
||||
@@ -8,45 +8,45 @@ use crate::device;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use std::arch::x86_64::*;
|
||||
|
||||
pub fn rd(opcode: u32) -> u32 {
|
||||
fn rd(opcode: u32) -> u32 {
|
||||
(opcode >> 11) & 0x1F
|
||||
}
|
||||
|
||||
pub fn rs(opcode: u32) -> u32 {
|
||||
fn rs(opcode: u32) -> u32 {
|
||||
(opcode >> 21) & 0x1F
|
||||
}
|
||||
|
||||
pub fn rt(opcode: u32) -> u32 {
|
||||
fn rt(opcode: u32) -> u32 {
|
||||
(opcode >> 16) & 0x1F
|
||||
}
|
||||
|
||||
pub fn sa(opcode: u32) -> u32 {
|
||||
fn sa(opcode: u32) -> u32 {
|
||||
(opcode >> 6) & 0x1F
|
||||
}
|
||||
|
||||
pub fn imm(opcode: u32) -> u16 {
|
||||
fn imm(opcode: u32) -> u16 {
|
||||
opcode as u16
|
||||
}
|
||||
|
||||
pub fn se16(value: i16) -> u32 {
|
||||
fn se16(value: i16) -> u32 {
|
||||
value as i32 as u32
|
||||
}
|
||||
|
||||
pub fn voffset(opcode: u32) -> u8 {
|
||||
fn voffset(opcode: u32) -> u8 {
|
||||
(opcode & 0x7F) as u8
|
||||
}
|
||||
|
||||
pub fn velement(opcode: u32) -> u8 {
|
||||
fn velement(opcode: u32) -> u8 {
|
||||
((opcode >> 7) & 0xF) as u8
|
||||
}
|
||||
|
||||
pub fn sign_extend_7bit_offset(offset: u8, shift_amount: u32) -> u32 {
|
||||
fn sign_extend_7bit_offset(offset: u8, shift_amount: u32) -> u32 {
|
||||
let soffset = (((offset << 1) & 0x80) | offset) as i8;
|
||||
|
||||
(((soffset) as i32) as u32) << shift_amount
|
||||
}
|
||||
|
||||
pub fn modify_vpr8(vpr: &mut __m128i, element: u8, value: u8) {
|
||||
fn modify_vpr8(vpr: &mut __m128i, element: u8, value: u8) {
|
||||
unsafe {
|
||||
*vpr = match element & 15 {
|
||||
0 => _mm_insert_epi8(*vpr, value as i32, 15),
|
||||
@@ -70,7 +70,7 @@ pub fn modify_vpr8(vpr: &mut __m128i, element: u8, value: u8) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_vpr8(vpr: __m128i, element: u8) -> u8 {
|
||||
fn get_vpr8(vpr: __m128i, element: u8) -> u8 {
|
||||
unsafe {
|
||||
match element & 15 {
|
||||
0 => _mm_extract_epi8(vpr, 15) as u8,
|
||||
@@ -126,7 +126,7 @@ pub fn get_vpr16(vpr: __m128i, element: u8) -> u16 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn modify_vpr32(vpr: &mut __m128i, element: u8, value: u32) {
|
||||
fn modify_vpr32(vpr: &mut __m128i, element: u8, value: u32) {
|
||||
unsafe {
|
||||
*vpr = match element & 3 {
|
||||
0 => _mm_insert_epi32(*vpr, value as i32, 3),
|
||||
@@ -138,7 +138,7 @@ pub fn modify_vpr32(vpr: &mut __m128i, element: u8, value: u32) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_vpr32(vpr: __m128i, element: u8) -> u32 {
|
||||
fn get_vpr32(vpr: __m128i, element: u8) -> u32 {
|
||||
unsafe {
|
||||
match element & 3 {
|
||||
0 => _mm_extract_epi32(vpr, 3) as u32,
|
||||
@@ -150,7 +150,7 @@ pub fn get_vpr32(vpr: __m128i, element: u8) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn modify_vpr64(vpr: &mut __m128i, element: u8, value: u64) {
|
||||
fn modify_vpr64(vpr: &mut __m128i, element: u8, value: u64) {
|
||||
unsafe {
|
||||
*vpr = match element & 1 {
|
||||
0 => _mm_insert_epi64(*vpr, value as i64, 1),
|
||||
@@ -160,7 +160,7 @@ pub fn modify_vpr64(vpr: &mut __m128i, element: u8, value: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_vpr64(vpr: __m128i, element: u8) -> u64 {
|
||||
fn get_vpr64(vpr: __m128i, element: u8) -> u64 {
|
||||
unsafe {
|
||||
match element & 1 {
|
||||
0 => _mm_extract_epi64(vpr, 1) as u64,
|
||||
@@ -170,11 +170,11 @@ pub fn get_vpr64(vpr: __m128i, element: u8) -> u64 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn modify_vpr128(vpr: &mut __m128i, value: u128) {
|
||||
fn modify_vpr128(vpr: &mut __m128i, value: u128) {
|
||||
unsafe { *vpr = std::mem::transmute::<u128, __m128i>(value) }
|
||||
}
|
||||
|
||||
pub fn get_vpr128(vpr: __m128i) -> u128 {
|
||||
fn get_vpr128(vpr: __m128i) -> u128 {
|
||||
unsafe { std::mem::transmute::<__m128i, u128>(vpr) }
|
||||
}
|
||||
|
||||
|
||||
@@ -9,41 +9,41 @@ use crate::device::rsp_su_instructions::{get_vpr16, modify_vpr16};
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use std::arch::x86_64::*;
|
||||
|
||||
pub fn vt(opcode: u32) -> u32 {
|
||||
fn vt(opcode: u32) -> u32 {
|
||||
(opcode >> 16) & 0x1F
|
||||
}
|
||||
|
||||
pub fn ve(opcode: u32) -> u32 {
|
||||
fn ve(opcode: u32) -> u32 {
|
||||
(opcode >> 21) & 0xF
|
||||
}
|
||||
|
||||
pub fn vs(opcode: u32) -> u32 {
|
||||
fn vs(opcode: u32) -> u32 {
|
||||
(opcode >> 11) & 0x1F
|
||||
}
|
||||
|
||||
pub fn vd(opcode: u32) -> u32 {
|
||||
fn vd(opcode: u32) -> u32 {
|
||||
(opcode >> 6) & 0x1F
|
||||
}
|
||||
|
||||
pub fn de(opcode: u32) -> u32 {
|
||||
fn de(opcode: u32) -> u32 {
|
||||
(opcode >> 11) & 0x7
|
||||
}
|
||||
|
||||
pub fn clamp_signed_32(value: i32) -> i16 {
|
||||
fn clamp_signed_32(value: i32) -> i16 {
|
||||
value.clamp(-32768, 32767) as i16
|
||||
}
|
||||
|
||||
pub fn clamp_signed_64(value: i64) -> i16 {
|
||||
fn clamp_signed_64(value: i64) -> i16 {
|
||||
value.clamp(-32768, 32767) as i16
|
||||
}
|
||||
|
||||
pub fn s_clip(x: i64, bits: u32) -> i64 {
|
||||
fn s_clip(x: i64, bits: u32) -> i64 {
|
||||
let b = 1_u64 << (bits - 1);
|
||||
let m = b * 2 - 1;
|
||||
((((x as u64) & m) ^ b).wrapping_sub(b)) as i64
|
||||
}
|
||||
|
||||
pub fn vte(device: &device::Device, vt: u32, index: usize) -> __m128i {
|
||||
fn vte(device: &device::Device, vt: u32, index: usize) -> __m128i {
|
||||
unsafe {
|
||||
_mm_shuffle_epi8(
|
||||
device.rsp.cpu.vpr[vt as usize],
|
||||
|
||||
+3
-3
@@ -34,7 +34,7 @@ pub fn read_regs(
|
||||
device.si.regs[((address & 0xFFFF) >> 2) as usize]
|
||||
}
|
||||
|
||||
pub fn dma_read(device: &mut device::Device) {
|
||||
fn dma_read(device: &mut device::Device) {
|
||||
device.si.dma_dir = DmaDir::Read;
|
||||
|
||||
let duration = device::pif::update_pif_ram(device);
|
||||
@@ -49,7 +49,7 @@ pub fn dma_read(device: &mut device::Device) {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dma_write(device: &mut device::Device) {
|
||||
fn dma_write(device: &mut device::Device) {
|
||||
device.si.dma_dir = DmaDir::Write;
|
||||
|
||||
copy_pif_rdram(device);
|
||||
@@ -78,7 +78,7 @@ pub fn write_regs(device: &mut device::Device, address: u64, value: u32, mask: u
|
||||
}
|
||||
|
||||
//rdram is in native endian format, and pif memory is in big endian format
|
||||
pub fn copy_pif_rdram(device: &mut device::Device) {
|
||||
fn copy_pif_rdram(device: &mut device::Device) {
|
||||
let dram_addr = device.si.regs[SI_DRAM_ADDR_REG as usize] as usize & device::rdram::RDRAM_MASK;
|
||||
if device.si.dma_dir == DmaDir::Write {
|
||||
let mut i = 0;
|
||||
|
||||
+13
-18
@@ -2,8 +2,8 @@ use crate::device;
|
||||
use crate::ui;
|
||||
|
||||
const SRAM_MASK: usize = 0xFFFF;
|
||||
const SRAM_SIZE: usize = 0x8000;
|
||||
const FLASHRAM_SIZE: usize = 0x20000;
|
||||
pub const SRAM_SIZE: usize = 0x8000;
|
||||
pub const FLASHRAM_SIZE: usize = 0x20000;
|
||||
pub const FLASHRAM_TYPE_ID: u32 = 0x11118001;
|
||||
pub const MX29L1100_ID: u32 = 0x00c2001e;
|
||||
const MX29L0000_ID: u32 = 0x00c20000;
|
||||
@@ -27,19 +27,19 @@ pub struct Flashram {
|
||||
pub silicon_id: [u32; 2],
|
||||
}
|
||||
|
||||
pub fn format_sram(device: &mut device::Device) {
|
||||
fn format_sram(device: &mut device::Device) {
|
||||
if device.ui.saves.sram.0.len() < SRAM_SIZE {
|
||||
device.ui.saves.sram.0.resize(SRAM_SIZE, 0xFF)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_flash(device: &mut device::Device) {
|
||||
fn format_flash(device: &mut device::Device) {
|
||||
if device.ui.saves.flash.0.len() < FLASHRAM_SIZE {
|
||||
device.ui.saves.flash.0.resize(FLASHRAM_SIZE, 0xFF)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_mem_sram(device: &mut device::Device, address: u64) -> u32 {
|
||||
fn read_mem_sram(device: &mut device::Device, address: u64) -> u32 {
|
||||
let masked_address = address as usize & SRAM_MASK;
|
||||
|
||||
format_sram(device);
|
||||
@@ -51,7 +51,7 @@ pub fn read_mem_sram(device: &mut device::Device, address: u64) -> u32 {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn read_mem_flash(device: &device::Device, address: u64) -> u32 {
|
||||
fn read_mem_flash(device: &device::Device, address: u64) -> u32 {
|
||||
if (address & 0x1ffff) == 0x00000 && device.flashram.mode == FlashramMode::Status {
|
||||
/* read Status register */
|
||||
device.flashram.status
|
||||
@@ -80,7 +80,7 @@ pub fn read_mem(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_mem_sram(device: &mut device::Device, address: u64, value: u32, mask: u32) {
|
||||
fn write_mem_sram(device: &mut device::Device, address: u64, value: u32, mask: u32) {
|
||||
let masked_address = address as usize & SRAM_MASK;
|
||||
|
||||
format_sram(device);
|
||||
@@ -96,7 +96,7 @@ pub fn write_mem_sram(device: &mut device::Device, address: u64, value: u32, mas
|
||||
device.ui.saves.sram.1 = true
|
||||
}
|
||||
|
||||
pub fn write_mem_flash(device: &mut device::Device, address: u64, value: u32, mask: u32) {
|
||||
fn write_mem_flash(device: &mut device::Device, address: u64, value: u32, mask: u32) {
|
||||
if (address & 0x1ffff) == 0x00000 && device.flashram.mode == FlashramMode::Status {
|
||||
/* clear/set Status register */
|
||||
device.flashram.status = (value & mask) & 0xff;
|
||||
@@ -128,12 +128,7 @@ pub fn write_mem(device: &mut device::Device, address: u64, value: u32, mask: u3
|
||||
);
|
||||
}
|
||||
|
||||
pub fn dma_read_sram(
|
||||
device: &mut device::Device,
|
||||
mut cart_addr: u32,
|
||||
mut dram_addr: u32,
|
||||
length: u32,
|
||||
) {
|
||||
fn dma_read_sram(device: &mut device::Device, mut cart_addr: u32, mut dram_addr: u32, length: u32) {
|
||||
dram_addr &= device::rdram::RDRAM_MASK as u32;
|
||||
cart_addr &= SRAM_MASK as u32;
|
||||
let mut i = dram_addr;
|
||||
@@ -150,7 +145,7 @@ pub fn dma_read_sram(
|
||||
device.ui.saves.sram.1 = true
|
||||
}
|
||||
|
||||
pub fn dma_read_flash(device: &mut device::Device, cart_addr: u32, dram_addr: u32, length: u32) {
|
||||
fn dma_read_flash(device: &mut device::Device, cart_addr: u32, dram_addr: u32, length: u32) {
|
||||
format_flash(device);
|
||||
|
||||
if (cart_addr & 0x1ffff) == 0x00000
|
||||
@@ -178,7 +173,7 @@ pub fn dma_read(device: &mut device::Device, cart_addr: u32, dram_addr: u32, len
|
||||
device::pi::calculate_cycles(device, 2, length)
|
||||
}
|
||||
|
||||
pub fn dma_write_sram(
|
||||
fn dma_write_sram(
|
||||
device: &mut device::Device,
|
||||
mut cart_addr: u32,
|
||||
mut dram_addr: u32,
|
||||
@@ -198,7 +193,7 @@ pub fn dma_write_sram(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dma_write_flash(
|
||||
fn dma_write_flash(
|
||||
device: &mut device::Device,
|
||||
mut cart_addr: u32,
|
||||
mut dram_addr: u32,
|
||||
@@ -251,7 +246,7 @@ pub fn dma_write(device: &mut device::Device, cart_addr: u32, dram_addr: u32, le
|
||||
device::pi::calculate_cycles(device, 2, length)
|
||||
}
|
||||
|
||||
pub fn flashram_command(device: &mut device::Device, command: u32) {
|
||||
fn flashram_command(device: &mut device::Device, command: u32) {
|
||||
match command & 0xff000000 {
|
||||
0x3c000000 => {
|
||||
/* set chip erase mode */
|
||||
|
||||
+2
-2
@@ -145,7 +145,7 @@ pub fn probe(device: &mut device::Device) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tlb_unmap(device: &mut device::Device, index: u64) {
|
||||
fn tlb_unmap(device: &mut device::Device, index: u64) {
|
||||
let e = &mut device.cpu.cop0.tlb_entries[index as usize];
|
||||
|
||||
if e.v_even != 0 {
|
||||
@@ -183,7 +183,7 @@ pub fn tlb_unmap(device: &mut device::Device, index: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tlb_map(device: &mut device::Device, index: u64) {
|
||||
fn tlb_map(device: &mut device::Device, index: u64) {
|
||||
let e = &mut device.cpu.cop0.tlb_entries[index as usize];
|
||||
|
||||
if e.v_even != 0
|
||||
|
||||
+13
-6
@@ -1,4 +1,5 @@
|
||||
use crate::device;
|
||||
use crate::netplay;
|
||||
use crate::ui;
|
||||
use governor::clock::Clock;
|
||||
|
||||
@@ -32,7 +33,7 @@ pub struct Vi {
|
||||
|
||||
const LIMIT_BUFFER: u64 = 3;
|
||||
|
||||
pub fn set_expected_refresh_rate(device: &mut device::Device) {
|
||||
fn set_expected_refresh_rate(device: &mut device::Device) {
|
||||
let expected_refresh_rate = device.vi.clock as f64
|
||||
/ (device.vi.regs[VI_V_SYNC_REG as usize] + 1) as f64
|
||||
/ ((device.vi.regs[VI_H_SYNC_REG as usize] & 0xFFF) + 1) as f64
|
||||
@@ -48,7 +49,7 @@ pub fn set_expected_refresh_rate(device: &mut device::Device) {
|
||||
device.vi.limiter = Some(governor::RateLimiter::direct(quota))
|
||||
}
|
||||
|
||||
pub fn set_vertical_interrupt(device: &mut device::Device) {
|
||||
fn set_vertical_interrupt(device: &mut device::Device) {
|
||||
if device::events::get_event(device, device::events::EventType::VI).is_none() {
|
||||
device::events::create_event(
|
||||
device,
|
||||
@@ -59,7 +60,7 @@ pub fn set_vertical_interrupt(device: &mut device::Device) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_current_line(device: &mut device::Device) {
|
||||
fn set_current_line(device: &mut device::Device) {
|
||||
let delay = device.vi.delay;
|
||||
let next_vi = device::events::get_event(device, device::events::EventType::VI);
|
||||
if next_vi.is_some() {
|
||||
@@ -115,7 +116,7 @@ pub fn write_regs(device: &mut device::Device, address: u64, value: u32, mask: u
|
||||
ui::video::set_register(reg as u32, device.vi.regs[reg as usize])
|
||||
}
|
||||
|
||||
pub fn vertical_interrupt_event(device: &mut device::Device) {
|
||||
fn vertical_interrupt_event(device: &mut device::Device) {
|
||||
device.cpu.running = ui::video::update_screen();
|
||||
|
||||
/*
|
||||
@@ -124,8 +125,14 @@ pub fn vertical_interrupt_event(device: &mut device::Device) {
|
||||
}
|
||||
*/
|
||||
|
||||
let mut enable_speed_limiter = true;
|
||||
if device.netplay.is_some() {
|
||||
netplay::send_sync_check(device);
|
||||
enable_speed_limiter = !device.netplay.as_ref().unwrap().fast_forward;
|
||||
}
|
||||
|
||||
device.vi.vi_counter += 1;
|
||||
if device.vi.vi_counter % LIMIT_BUFFER == 0 {
|
||||
if device.vi.vi_counter % LIMIT_BUFFER == 0 && enable_speed_limiter {
|
||||
speed_limiter(device);
|
||||
}
|
||||
|
||||
@@ -162,7 +169,7 @@ pub fn init(device: &mut device::Device) {
|
||||
*/
|
||||
}
|
||||
|
||||
pub fn speed_limiter(device: &device::Device) {
|
||||
fn speed_limiter(device: &device::Device) {
|
||||
let result = device.vi.limiter.as_ref().unwrap().check();
|
||||
if result.is_err() {
|
||||
let outcome = result.unwrap_err();
|
||||
|
||||
+19
-2
@@ -1,6 +1,10 @@
|
||||
#![deny(warnings)]
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
mod device;
|
||||
mod netplay;
|
||||
mod ui;
|
||||
use clap::Parser;
|
||||
|
||||
@@ -18,6 +22,12 @@ struct Args {
|
||||
help = "Create a new input profile (keyboard/gamepad mappings)."
|
||||
)]
|
||||
configure_input_profile: Option<String>,
|
||||
#[arg(
|
||||
short,
|
||||
long,
|
||||
help = "Use DirectInput when configuring a new input profile."
|
||||
)]
|
||||
use_dinput: bool,
|
||||
#[arg(
|
||||
short,
|
||||
long,
|
||||
@@ -53,7 +63,8 @@ struct Args {
|
||||
clear_input_bindings: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let exe_path = std::env::current_exe().unwrap();
|
||||
let portable_dir = exe_path.parent();
|
||||
let portable = portable_dir.unwrap().join("portable.txt").exists();
|
||||
@@ -139,13 +150,19 @@ fn main() {
|
||||
ui::input::configure_input_profile(
|
||||
&mut device.ui,
|
||||
args.configure_input_profile.unwrap(),
|
||||
args.use_dinput,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if args.game.is_some() {
|
||||
let file_path = std::path::Path::new(args.game.as_ref().unwrap());
|
||||
device::run_game(file_path, data_dir, &mut device, args.fullscreen);
|
||||
let rom_contents = device::get_rom_contents(file_path);
|
||||
if rom_contents.is_empty() {
|
||||
println!("Could not read rom file");
|
||||
return;
|
||||
}
|
||||
device::run_game(rom_contents, data_dir, &mut device, args.fullscreen);
|
||||
}
|
||||
} else {
|
||||
let options = eframe::NativeOptions {
|
||||
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
use crate::device;
|
||||
use crate::ui;
|
||||
use eframe::egui;
|
||||
use std::io::{Read, Write};
|
||||
//UDP packet formats
|
||||
const UDP_SEND_KEY_INFO: u8 = 0;
|
||||
const UDP_RECEIVE_KEY_INFO: u8 = 1;
|
||||
const UDP_REQUEST_KEY_INFO: u8 = 2;
|
||||
const UDP_RECEIVE_KEY_INFO_GRATUITOUS: u8 = 3;
|
||||
const UDP_SYNC_DATA: u8 = 4;
|
||||
|
||||
//TCP packet formats
|
||||
const TCP_SEND_SAVE: u8 = 1;
|
||||
const TCP_RECEIVE_SAVE: u8 = 2;
|
||||
//const TCP_SEND_SETTINGS: u8 = 3;
|
||||
//const TCP_RECEIVE_SETTINGS: u8 = 4;
|
||||
const TCP_REGISTER_PLAYER: u8 = 5;
|
||||
const TCP_GET_REGISTRATION: u8 = 6;
|
||||
const TCP_DISCONNECT_NOTICE: u8 = 7;
|
||||
|
||||
pub struct Netplay {
|
||||
udp_socket: std::net::UdpSocket,
|
||||
tcp_stream: std::net::TcpStream,
|
||||
pub player_number: u8,
|
||||
pub player_data: [PlayerData; 4],
|
||||
vi_counter: u32,
|
||||
status: u8,
|
||||
buffer_target: u8,
|
||||
pub fast_forward: bool,
|
||||
pub error_notifier: tokio::sync::mpsc::Sender<String>,
|
||||
pub gui_ctx: egui::Context,
|
||||
}
|
||||
|
||||
pub struct PlayerData {
|
||||
lag: u8,
|
||||
count: u32,
|
||||
pub reg_id: u32,
|
||||
input_events: std::collections::HashMap<u32, InputEvent>,
|
||||
}
|
||||
|
||||
struct InputEvent {
|
||||
input: u32,
|
||||
plugin: u8,
|
||||
}
|
||||
|
||||
fn send_error(netplay: &mut Netplay, error: String) {
|
||||
netplay.error_notifier.try_send(error).unwrap();
|
||||
|
||||
netplay.gui_ctx.request_repaint(); // this is so the window pops up right away
|
||||
}
|
||||
|
||||
pub fn send_save(netplay: &mut Netplay, save_type: &str, save_data: &[u8], size: usize) {
|
||||
let mut request: Vec<u8> = [TCP_SEND_SAVE].to_vec();
|
||||
request.extend_from_slice(save_type.as_bytes());
|
||||
request.push(0); // null terminate string
|
||||
request.extend_from_slice(&(size as u32).to_be_bytes());
|
||||
|
||||
let mut send_data = save_data.to_owned();
|
||||
send_data.resize(size, 0); // pad with zeros if needed
|
||||
request.extend(send_data);
|
||||
netplay.tcp_stream.write_all(&request).unwrap();
|
||||
}
|
||||
|
||||
pub fn receive_save(netplay: &mut Netplay, save_type: &str, save_data: &mut Vec<u8>, size: usize) {
|
||||
let mut request: Vec<u8> = [TCP_RECEIVE_SAVE].to_vec();
|
||||
request.extend_from_slice(save_type.as_bytes());
|
||||
request.push(0); // null terminate string
|
||||
netplay.tcp_stream.write_all(&request).unwrap();
|
||||
|
||||
let mut response: Vec<u8> = vec![0; size];
|
||||
netplay.tcp_stream.read_exact(&mut response).unwrap();
|
||||
*save_data = response;
|
||||
}
|
||||
|
||||
pub fn send_sync_check(device: &mut device::Device) {
|
||||
let netplay = device.netplay.as_mut().unwrap();
|
||||
if netplay.vi_counter % 600 == 0 {
|
||||
let mut request: Vec<u8> = [UDP_SYNC_DATA].to_vec();
|
||||
request.extend_from_slice(&(netplay.vi_counter).to_be_bytes());
|
||||
|
||||
for i in 0..device::cop0::COP0_REGS_COUNT as usize {
|
||||
request.extend_from_slice(&(device.cpu.cop0.regs[i] as u32).to_be_bytes());
|
||||
}
|
||||
|
||||
netplay.udp_socket.send(&request).unwrap();
|
||||
}
|
||||
netplay.vi_counter = netplay.vi_counter.wrapping_add(1);
|
||||
}
|
||||
|
||||
pub fn send_input(netplay: &Netplay, input: (u32, bool)) {
|
||||
let mut request: Vec<u8> = [UDP_SEND_KEY_INFO].to_vec();
|
||||
request.push(netplay.player_number);
|
||||
request.extend_from_slice(
|
||||
&(netplay.player_data[netplay.player_number as usize].count).to_be_bytes(),
|
||||
);
|
||||
request.extend_from_slice(&(input.0).to_be_bytes());
|
||||
request.push(input.1 as u8);
|
||||
netplay.udp_socket.send(&request).unwrap();
|
||||
}
|
||||
|
||||
pub fn get_input(device: &mut device::Device, channel: usize) -> (u32, bool) {
|
||||
let netplay = device.netplay.as_mut().unwrap();
|
||||
let mut input = None;
|
||||
|
||||
let timeout = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||
let mut request_timer = std::time::Instant::now() + std::time::Duration::from_millis(5);
|
||||
request_input(netplay, channel);
|
||||
while input.is_none() {
|
||||
if std::time::Instant::now() > request_timer {
|
||||
// sends a request packet every 5ms
|
||||
request_input(netplay, channel);
|
||||
request_timer = std::time::Instant::now() + std::time::Duration::from_millis(5);
|
||||
}
|
||||
process_incoming(netplay);
|
||||
input = netplay.player_data[channel]
|
||||
.input_events
|
||||
.remove(&netplay.player_data[channel].count);
|
||||
|
||||
if std::time::Instant::now() > timeout {
|
||||
send_error(
|
||||
netplay,
|
||||
"Timed out waiting for input. Lost connection to server".to_string(),
|
||||
);
|
||||
input = Some(InputEvent {
|
||||
input: 0,
|
||||
plugin: 0,
|
||||
});
|
||||
device.cpu.running = false;
|
||||
}
|
||||
}
|
||||
|
||||
netplay.fast_forward = netplay.player_data[channel].lag > 0
|
||||
&& netplay.player_data[channel].input_events.len() as u8 > netplay.buffer_target;
|
||||
|
||||
netplay.player_data[channel].count = netplay.player_data[channel].count.wrapping_add(1);
|
||||
(
|
||||
input.as_ref().unwrap().input,
|
||||
input.as_ref().unwrap().plugin != 0,
|
||||
)
|
||||
}
|
||||
|
||||
fn request_input(netplay: &Netplay, channel: usize) {
|
||||
let mut request: Vec<u8> = [UDP_REQUEST_KEY_INFO].to_vec();
|
||||
request.push(channel as u8); //The player we need input for
|
||||
request.extend_from_slice(
|
||||
&(netplay.player_data[netplay.player_number as usize].reg_id).to_be_bytes(),
|
||||
);
|
||||
request.extend_from_slice(&(netplay.player_data[channel].count).to_be_bytes());
|
||||
request.push(0); //spectator mode
|
||||
request.push(netplay.player_data[channel].input_events.len() as u8);
|
||||
netplay.udp_socket.send(&request).unwrap();
|
||||
}
|
||||
|
||||
fn process_incoming(netplay: &mut Netplay) {
|
||||
let mut buf: [u8; 1024] = [0; 1024];
|
||||
while let Ok(_incoming) = netplay.udp_socket.recv(&mut buf) {
|
||||
match buf[0] {
|
||||
UDP_RECEIVE_KEY_INFO | UDP_RECEIVE_KEY_INFO_GRATUITOUS => {
|
||||
let player = buf[1] as usize;
|
||||
//current_status is a status update from the server
|
||||
//it will let us know if another player has disconnected, or the games have desynced
|
||||
let current_status = buf[2];
|
||||
if buf[0] == UDP_RECEIVE_KEY_INFO {
|
||||
netplay.player_data[player].lag = buf[3];
|
||||
}
|
||||
if current_status != netplay.status {
|
||||
if ((current_status & 0x1) ^ (netplay.status & 0x1)) != 0 {
|
||||
send_error(
|
||||
netplay,
|
||||
format!("Players have desynced at VI {}", netplay.vi_counter),
|
||||
);
|
||||
}
|
||||
for dis in 1..5 {
|
||||
if ((current_status & (0x1 << dis)) ^ (netplay.status & (0x1 << dis))) != 0
|
||||
{
|
||||
send_error(netplay, format!("Player {} has disconnected", dis));
|
||||
}
|
||||
}
|
||||
netplay.status = current_status;
|
||||
}
|
||||
|
||||
let mut buffer_offset = 5;
|
||||
for _i in 0..buf[4] {
|
||||
let count = u32::from_be_bytes(
|
||||
buf[buffer_offset..buffer_offset + 4].try_into().unwrap(),
|
||||
);
|
||||
buffer_offset += 4;
|
||||
|
||||
if (count.wrapping_sub(netplay.player_data[player].count)) > (u32::MAX / 2) {
|
||||
//event doesn't need to be recorded
|
||||
buffer_offset += 5;
|
||||
continue;
|
||||
}
|
||||
|
||||
let input = u32::from_be_bytes(
|
||||
buf[buffer_offset..buffer_offset + 4].try_into().unwrap(),
|
||||
);
|
||||
buffer_offset += 4;
|
||||
let plugin = buf[buffer_offset];
|
||||
buffer_offset += 1;
|
||||
let input_event = InputEvent { input, plugin };
|
||||
netplay.player_data[player]
|
||||
.input_events
|
||||
.insert(count, input_event);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
panic! {"unknown UDP packet"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(
|
||||
mut peer_addr: std::net::SocketAddr,
|
||||
session: ui::gui::gui_netplay::NetplayRoom,
|
||||
player_number: u8,
|
||||
error_notifier: tokio::sync::mpsc::Sender<String>,
|
||||
gui_ctx: egui::Context,
|
||||
) -> Netplay {
|
||||
peer_addr.set_port(session.port.unwrap() as u16);
|
||||
let udp_socket = if peer_addr.is_ipv4() {
|
||||
std::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, 0))
|
||||
.expect("couldn't bind to address")
|
||||
} else {
|
||||
std::net::UdpSocket::bind((std::net::Ipv6Addr::UNSPECIFIED, 0))
|
||||
.expect("couldn't bind to address")
|
||||
};
|
||||
udp_socket.connect(peer_addr).unwrap();
|
||||
udp_socket.set_nonblocking(true).unwrap();
|
||||
|
||||
let mut stream = std::net::TcpStream::connect(peer_addr).unwrap();
|
||||
|
||||
let regid = (player_number + 1) as u32;
|
||||
let mut request: [u8; 8] = [
|
||||
TCP_REGISTER_PLAYER,
|
||||
player_number,
|
||||
0, //plugin/pak
|
||||
0, //rawdata
|
||||
0, //regid (u32)
|
||||
0, //regid (u32)
|
||||
0, //regid (u32)
|
||||
0, //regid (u32)
|
||||
];
|
||||
|
||||
request[4..8].copy_from_slice(®id.to_be_bytes());
|
||||
stream.write_all(&request).unwrap();
|
||||
|
||||
let mut response: [u8; 2] = [0, 0];
|
||||
stream.read_exact(&mut response).unwrap();
|
||||
if response[0] != 1 {
|
||||
panic!("Failed to register player");
|
||||
}
|
||||
let buffer_target = response[1];
|
||||
|
||||
let request: [u8; 1] = [TCP_GET_REGISTRATION];
|
||||
stream.write_all(&request).unwrap();
|
||||
let mut response: [u8; 24] = [0; 24];
|
||||
stream.read_exact(&mut response).unwrap();
|
||||
|
||||
let mut reg_id: [u32; 4] = [0; 4];
|
||||
for i in 0..4 {
|
||||
// reg_id of 0 means no player connected
|
||||
reg_id[i] = u32::from_be_bytes(response[(i * 6)..(i * 6) + 4].try_into().unwrap());
|
||||
}
|
||||
Netplay {
|
||||
udp_socket,
|
||||
tcp_stream: stream,
|
||||
player_number,
|
||||
vi_counter: 0,
|
||||
status: 0,
|
||||
buffer_target,
|
||||
fast_forward: false,
|
||||
error_notifier,
|
||||
gui_ctx,
|
||||
player_data: [
|
||||
PlayerData {
|
||||
lag: 0,
|
||||
count: 0,
|
||||
reg_id: reg_id[0],
|
||||
input_events: std::collections::HashMap::new(),
|
||||
},
|
||||
PlayerData {
|
||||
lag: 0,
|
||||
count: 0,
|
||||
reg_id: reg_id[1],
|
||||
input_events: std::collections::HashMap::new(),
|
||||
},
|
||||
PlayerData {
|
||||
lag: 0,
|
||||
count: 0,
|
||||
reg_id: reg_id[2],
|
||||
input_events: std::collections::HashMap::new(),
|
||||
},
|
||||
PlayerData {
|
||||
lag: 0,
|
||||
count: 0,
|
||||
reg_id: reg_id[3],
|
||||
input_events: std::collections::HashMap::new(),
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(device: &mut device::Device) {
|
||||
let netplay = device.netplay.as_mut().unwrap();
|
||||
let regid = (netplay.player_number + 1) as u32;
|
||||
let mut request: [u8; 5] = [TCP_DISCONNECT_NOTICE, 0, 0, 0, 0];
|
||||
request[1..5].copy_from_slice(®id.to_be_bytes());
|
||||
|
||||
netplay.tcp_stream.write_all(&request).unwrap();
|
||||
netplay.tcp_stream.flush().unwrap();
|
||||
netplay
|
||||
.tcp_stream
|
||||
.shutdown(std::net::Shutdown::Both)
|
||||
.unwrap();
|
||||
}
|
||||
@@ -18,6 +18,7 @@ pub struct Ui {
|
||||
pub sdl_context: Option<sdl2::Sdl>,
|
||||
pub video_subsystem: Option<sdl2::VideoSubsystem>,
|
||||
pub audio_subsystem: Option<sdl2::AudioSubsystem>,
|
||||
pub pak_audio: Option<audio::PakAudio>,
|
||||
pub joystick_subsystem: Option<sdl2::JoystickSubsystem>,
|
||||
#[allow(dead_code)]
|
||||
pub controller_subsystem: Option<sdl2::GameControllerSubsystem>,
|
||||
@@ -59,18 +60,22 @@ impl Ui {
|
||||
input::Controllers {
|
||||
game_controller: None,
|
||||
joystick: None,
|
||||
rumble: false,
|
||||
},
|
||||
input::Controllers {
|
||||
game_controller: None,
|
||||
joystick: None,
|
||||
rumble: false,
|
||||
},
|
||||
input::Controllers {
|
||||
game_controller: None,
|
||||
joystick: None,
|
||||
rumble: false,
|
||||
},
|
||||
input::Controllers {
|
||||
game_controller: None,
|
||||
joystick: None,
|
||||
rumble: false,
|
||||
},
|
||||
],
|
||||
config_file_path,
|
||||
@@ -99,6 +104,7 @@ impl Ui {
|
||||
controller_subsystem: Some(controller_subsystem),
|
||||
window: None,
|
||||
audio_device: None,
|
||||
pak_audio: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+52
-1
@@ -1,6 +1,11 @@
|
||||
use crate::device;
|
||||
use crate::ui;
|
||||
|
||||
pub struct PakAudio {
|
||||
mempak: Vec<u8>,
|
||||
rumblepak: Vec<u8>,
|
||||
}
|
||||
|
||||
pub fn init(ui: &mut ui::Ui, frequency: u64) {
|
||||
let desired_spec = sdl2::audio::AudioSpecDesired {
|
||||
freq: Some(frequency as i32),
|
||||
@@ -14,7 +19,53 @@ pub fn init(ui: &mut ui::Ui, frequency: u64) {
|
||||
.open_queue::<i16, _>(None, &desired_spec)
|
||||
.unwrap(),
|
||||
);
|
||||
ui.audio_device.as_ref().unwrap().resume();
|
||||
let audio_device = ui.audio_device.as_ref().unwrap();
|
||||
audio_device.resume();
|
||||
|
||||
let mempak_audio = Box::new(sdl2::audio::AudioSpecWAV::load_wav_rw(
|
||||
&mut sdl2::rwops::RWops::from_bytes(include_bytes!("../../data/mempak.wav"))
|
||||
.expect("Could not mempak WAV file"),
|
||||
))
|
||||
.expect("Could not load mempak WAV file");
|
||||
let rumblepak_audio = Box::new(
|
||||
sdl2::audio::AudioSpecWAV::load_wav_rw(
|
||||
&mut sdl2::rwops::RWops::from_bytes(include_bytes!("../../data/rumblepak.wav"))
|
||||
.expect("Could not load rumblepak WAV file"),
|
||||
)
|
||||
.expect("Could not load rumblepak WAV file"),
|
||||
);
|
||||
|
||||
let cvt = sdl2::audio::AudioCVT::new(
|
||||
mempak_audio.format,
|
||||
mempak_audio.channels,
|
||||
mempak_audio.freq,
|
||||
audio_device.spec().format,
|
||||
audio_device.spec().channels,
|
||||
audio_device.spec().freq,
|
||||
)
|
||||
.expect("Could not create AudioCVT");
|
||||
|
||||
ui.pak_audio = Some(PakAudio {
|
||||
mempak: cvt.convert(mempak_audio.buffer().to_vec()),
|
||||
rumblepak: cvt.convert(rumblepak_audio.buffer().to_vec()),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn play_pak_switch(ui: &mut ui::Ui, pak: device::controller::PakType) {
|
||||
let sound;
|
||||
if pak == device::controller::PakType::RumblePak {
|
||||
sound = &ui.pak_audio.as_ref().unwrap().rumblepak;
|
||||
} else if pak == device::controller::PakType::MemPak {
|
||||
sound = &ui.pak_audio.as_ref().unwrap().mempak;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
let audio_device = ui.audio_device.as_ref().unwrap();
|
||||
let i16_buffer: Vec<i16> = sound
|
||||
.chunks_exact(2)
|
||||
.map(|chunk| i16::from_le_bytes([chunk[0], chunk[1]]))
|
||||
.collect();
|
||||
audio_device.queue_audio(&i16_buffer).unwrap();
|
||||
}
|
||||
|
||||
pub fn play_audio(device: &mut device::Device, dram_addr: usize, length: u64) {
|
||||
|
||||
+9
-6
@@ -2,12 +2,13 @@ use crate::ui;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct InputProfile {
|
||||
pub keys: [(bool, i32); 18],
|
||||
pub controller_buttons: [(bool, i32); 14],
|
||||
pub controller_axis: [(bool, i32, i16); 18],
|
||||
pub joystick_buttons: [(bool, u32); 14],
|
||||
pub joystick_hat: [(bool, u32, u8); 14],
|
||||
pub joystick_axis: [(bool, u32, i16); 18],
|
||||
pub keys: [(bool, i32); ui::input::PROFILE_SIZE],
|
||||
pub controller_buttons: [(bool, i32); ui::input::PROFILE_SIZE],
|
||||
pub controller_axis: [(bool, i32, i16); ui::input::PROFILE_SIZE],
|
||||
pub joystick_buttons: [(bool, u32); ui::input::PROFILE_SIZE],
|
||||
pub joystick_hat: [(bool, u32, u8); ui::input::PROFILE_SIZE],
|
||||
pub joystick_axis: [(bool, u32, i16); ui::input::PROFILE_SIZE],
|
||||
pub dinput: bool,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
@@ -21,6 +22,7 @@ pub struct Input {
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct Video {
|
||||
pub upscale: bool,
|
||||
pub integer_scaling: bool,
|
||||
pub fullscreen: bool,
|
||||
}
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
@@ -48,6 +50,7 @@ impl Config {
|
||||
},
|
||||
video: Video {
|
||||
upscale: false,
|
||||
integer_scaling: false,
|
||||
fullscreen: false,
|
||||
},
|
||||
}
|
||||
|
||||
+283
-168
@@ -1,6 +1,8 @@
|
||||
use crate::device;
|
||||
use crate::netplay;
|
||||
use crate::ui;
|
||||
use eframe::egui;
|
||||
pub mod gui_netplay;
|
||||
|
||||
pub struct GopherEguiApp {
|
||||
config_dir: std::path::PathBuf,
|
||||
@@ -14,12 +16,26 @@ pub struct GopherEguiApp {
|
||||
input_profiles: Vec<String>,
|
||||
controller_enabled: [bool; 4],
|
||||
upscale: bool,
|
||||
integer_scaling: bool,
|
||||
fullscreen: bool,
|
||||
emulate_vru: bool,
|
||||
dinput: bool,
|
||||
show_vru_dialog: bool,
|
||||
vru_window_receiver: Option<std::sync::mpsc::Receiver<Vec<String>>>,
|
||||
vru_word_notifier: Option<std::sync::mpsc::Sender<String>>,
|
||||
vru_window_receiver: Option<tokio::sync::mpsc::Receiver<Vec<String>>>,
|
||||
netplay_error_receiver: Option<tokio::sync::mpsc::Receiver<String>>,
|
||||
vru_word_notifier: Option<tokio::sync::mpsc::Sender<String>>,
|
||||
vru_word_list: Vec<String>,
|
||||
pub netplay: gui_netplay::GuiNetplay,
|
||||
}
|
||||
|
||||
struct SaveConfig {
|
||||
selected_controller: [i32; 4],
|
||||
selected_profile: [String; 4],
|
||||
controller_enabled: [bool; 4],
|
||||
upscale: bool,
|
||||
integer_scaling: bool,
|
||||
fullscreen: bool,
|
||||
emulate_vru: bool,
|
||||
}
|
||||
|
||||
fn get_input_profiles(game_ui: &ui::Ui) -> Vec<String> {
|
||||
@@ -85,27 +101,23 @@ impl GopherEguiApp {
|
||||
input_profiles: get_input_profiles(&game_ui),
|
||||
controller_enabled: game_ui.config.input.controller_enabled,
|
||||
upscale: game_ui.config.video.upscale,
|
||||
integer_scaling: game_ui.config.video.integer_scaling,
|
||||
fullscreen: game_ui.config.video.fullscreen,
|
||||
emulate_vru: game_ui.config.input.emulate_vru,
|
||||
show_vru_dialog: false,
|
||||
dinput: false,
|
||||
netplay_error_receiver: None,
|
||||
vru_window_receiver: None,
|
||||
vru_word_notifier: None,
|
||||
vru_word_list: Vec::new(),
|
||||
netplay: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn save_config(
|
||||
game_ui: &mut ui::Ui,
|
||||
selected_controller: [i32; 4],
|
||||
selected_profile: [String; 4],
|
||||
controller_enabled: [bool; 4],
|
||||
upscale: bool,
|
||||
fullscreen: bool,
|
||||
emulate_vru: bool,
|
||||
) {
|
||||
fn save_config(game_ui: &mut ui::Ui, save_config_items: SaveConfig) {
|
||||
let joystick_subsystem = game_ui.joystick_subsystem.as_ref().unwrap();
|
||||
for (pos, item) in selected_controller.iter().enumerate() {
|
||||
for (pos, item) in save_config_items.selected_controller.iter().enumerate() {
|
||||
if *item != -1 {
|
||||
game_ui.config.input.controller_assignment[pos] = Some(
|
||||
joystick_subsystem
|
||||
@@ -118,143 +130,285 @@ fn save_config(
|
||||
}
|
||||
}
|
||||
|
||||
game_ui.config.input.input_profile_binding = selected_profile;
|
||||
game_ui.config.input.controller_enabled = controller_enabled;
|
||||
game_ui.config.input.input_profile_binding = save_config_items.selected_profile;
|
||||
game_ui.config.input.controller_enabled = save_config_items.controller_enabled;
|
||||
|
||||
game_ui.config.video.fullscreen = fullscreen;
|
||||
game_ui.config.video.upscale = upscale;
|
||||
game_ui.config.input.emulate_vru = emulate_vru;
|
||||
game_ui.config.video.upscale = save_config_items.upscale;
|
||||
game_ui.config.video.integer_scaling = save_config_items.integer_scaling;
|
||||
game_ui.config.video.fullscreen = save_config_items.fullscreen;
|
||||
game_ui.config.input.emulate_vru = save_config_items.emulate_vru;
|
||||
}
|
||||
|
||||
impl Drop for GopherEguiApp {
|
||||
fn drop(&mut self) {
|
||||
let mut game_ui = ui::Ui::new(self.config_dir.clone());
|
||||
save_config(
|
||||
&mut game_ui,
|
||||
self.selected_controller,
|
||||
self.selected_profile.clone(),
|
||||
self.controller_enabled,
|
||||
self.upscale,
|
||||
self.fullscreen,
|
||||
self.emulate_vru,
|
||||
);
|
||||
let save_config_items = SaveConfig {
|
||||
selected_controller: self.selected_controller,
|
||||
selected_profile: self.selected_profile.clone(),
|
||||
controller_enabled: self.controller_enabled,
|
||||
upscale: self.upscale,
|
||||
integer_scaling: self.integer_scaling,
|
||||
fullscreen: self.fullscreen,
|
||||
emulate_vru: self.emulate_vru,
|
||||
};
|
||||
save_config(&mut game_ui, save_config_items);
|
||||
}
|
||||
}
|
||||
|
||||
fn configure_profile(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
egui::Window::new("Configure Input Profile")
|
||||
// .open(&mut self.configure_profile)
|
||||
.show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
let name_label = ui.label("Profile Name:");
|
||||
ui.text_edit_singleline(&mut app.profile_name)
|
||||
.labelled_by(name_label.id);
|
||||
});
|
||||
ui.checkbox(&mut app.dinput, "Use DirectInput");
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Configure Profile").clicked() {
|
||||
let profile_name = app.profile_name.clone();
|
||||
let config_dir = app.config_dir.clone();
|
||||
let dinput = app.dinput;
|
||||
tokio::spawn(async move {
|
||||
let mut game_ui = ui::Ui::new(config_dir);
|
||||
ui::input::configure_input_profile(&mut game_ui, profile_name, dinput);
|
||||
});
|
||||
app.configure_profile = false;
|
||||
if !app.profile_name.is_empty()
|
||||
&& !app.input_profiles.contains(&app.profile_name)
|
||||
{
|
||||
app.input_profiles.push(app.profile_name.clone())
|
||||
}
|
||||
};
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button("Close").clicked() {
|
||||
app.configure_profile = false
|
||||
};
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn show_vru_dialog(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
ctx.show_viewport_immediate(
|
||||
egui::ViewportId::from_hash_of("vru_dialog"),
|
||||
egui::ViewportBuilder::default()
|
||||
.with_title("What would you like to say?")
|
||||
.with_always_on_top(),
|
||||
|ctx, class| {
|
||||
assert!(
|
||||
class == egui::ViewportClass::Immediate,
|
||||
"This egui backend doesn't support multiple viewports"
|
||||
);
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
egui::Grid::new("vru_words").show(ui, |ui| {
|
||||
for (i, v) in app.vru_word_list.iter().enumerate() {
|
||||
if i % 5 == 0 {
|
||||
ui.end_row();
|
||||
}
|
||||
if ui.button((*v).to_string()).clicked() {
|
||||
app.vru_word_notifier
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.try_send(v.clone())
|
||||
.unwrap();
|
||||
app.show_vru_dialog = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if ctx.input(|i| i.viewport().close_requested()) {
|
||||
app.vru_word_notifier
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.try_send(String::from(""))
|
||||
.unwrap();
|
||||
app.show_vru_dialog = false;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn open_rom(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
let task;
|
||||
let netplay;
|
||||
|
||||
let selected_controller = app.selected_controller;
|
||||
let selected_profile = app.selected_profile.clone();
|
||||
let controller_enabled = app.controller_enabled;
|
||||
let upscale = app.upscale;
|
||||
let integer_scaling = app.integer_scaling;
|
||||
let fullscreen = app.fullscreen;
|
||||
let emulate_vru = app.emulate_vru;
|
||||
let config_dir = app.config_dir.clone();
|
||||
let cache_dir = app.cache_dir.clone();
|
||||
let data_dir = app.data_dir.clone();
|
||||
let peer_addr;
|
||||
let session;
|
||||
let player_number;
|
||||
|
||||
if app.netplay.player_name.is_empty() {
|
||||
task = Some(rfd::AsyncFileDialog::new().pick_file());
|
||||
netplay = false;
|
||||
peer_addr = None;
|
||||
session = None;
|
||||
player_number = None;
|
||||
} else {
|
||||
task = None;
|
||||
netplay = true;
|
||||
peer_addr = app.netplay.peer_addr;
|
||||
session = app.netplay.waiting_session.clone();
|
||||
player_number = Some(app.netplay.player_number);
|
||||
}
|
||||
|
||||
let (netplay_error_notifier, netplay_error_receiver): (
|
||||
tokio::sync::mpsc::Sender<String>,
|
||||
tokio::sync::mpsc::Receiver<String>,
|
||||
) = tokio::sync::mpsc::channel(8);
|
||||
|
||||
let (vru_window_notifier, vru_window_receiver): (
|
||||
tokio::sync::mpsc::Sender<Vec<String>>,
|
||||
tokio::sync::mpsc::Receiver<Vec<String>>,
|
||||
) = tokio::sync::mpsc::channel(1);
|
||||
|
||||
let (vru_word_notifier, vru_word_receiver): (
|
||||
tokio::sync::mpsc::Sender<String>,
|
||||
tokio::sync::mpsc::Receiver<String>,
|
||||
) = tokio::sync::mpsc::channel(1);
|
||||
|
||||
if netplay {
|
||||
app.netplay_error_receiver = Some(netplay_error_receiver);
|
||||
}
|
||||
if emulate_vru && !netplay {
|
||||
app.vru_window_receiver = Some(vru_window_receiver);
|
||||
app.vru_word_notifier = Some(vru_word_notifier);
|
||||
} else {
|
||||
app.vru_window_receiver = None;
|
||||
app.vru_word_notifier = None;
|
||||
}
|
||||
|
||||
let rom_contents = app.netplay.rom_contents.clone();
|
||||
let gui_ctx = ctx.clone();
|
||||
tokio::spawn(async move {
|
||||
let file = if !netplay { task.unwrap().await } else { None };
|
||||
|
||||
if file.is_some() || netplay {
|
||||
let running_file = cache_dir.join("game_running");
|
||||
if running_file.exists() {
|
||||
println!("Game already running");
|
||||
return;
|
||||
}
|
||||
let result = std::fs::File::create(running_file.clone());
|
||||
if result.is_err() {
|
||||
panic!("could not create running file: {}", result.err().unwrap())
|
||||
}
|
||||
let mut device = device::Device::new(config_dir);
|
||||
|
||||
let save_config_items = SaveConfig {
|
||||
selected_controller,
|
||||
selected_profile,
|
||||
controller_enabled,
|
||||
upscale,
|
||||
integer_scaling,
|
||||
fullscreen,
|
||||
emulate_vru,
|
||||
};
|
||||
save_config(&mut device.ui, save_config_items);
|
||||
|
||||
if netplay {
|
||||
device.netplay = Some(netplay::init(
|
||||
peer_addr.unwrap(),
|
||||
session.unwrap(),
|
||||
player_number.unwrap(),
|
||||
netplay_error_notifier,
|
||||
gui_ctx,
|
||||
));
|
||||
device::run_game(rom_contents, data_dir, &mut device, fullscreen);
|
||||
netplay::close(&mut device);
|
||||
} else {
|
||||
if emulate_vru {
|
||||
device.vru.window_notifier = Some(vru_window_notifier);
|
||||
device.vru.word_receiver = Some(vru_word_receiver);
|
||||
device.vru.gui_ctx = Some(gui_ctx);
|
||||
}
|
||||
|
||||
let rom_contents = device::get_rom_contents(file.unwrap().path());
|
||||
if rom_contents.is_empty() {
|
||||
println!("Could not read rom file");
|
||||
} else {
|
||||
device::run_game(rom_contents, data_dir, &mut device, fullscreen);
|
||||
}
|
||||
}
|
||||
let result = std::fs::remove_file(running_file.clone());
|
||||
if result.is_err() {
|
||||
panic!("could not remove running file: {}", result.err().unwrap())
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
impl eframe::App for GopherEguiApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
if self.netplay.create {
|
||||
gui_netplay::netplay_create(self, ctx);
|
||||
}
|
||||
|
||||
if self.netplay.join {
|
||||
gui_netplay::netplay_join(self, ctx);
|
||||
}
|
||||
|
||||
if self.netplay.wait {
|
||||
gui_netplay::netplay_wait(self, ctx);
|
||||
}
|
||||
|
||||
if self.netplay_error_receiver.is_some() {
|
||||
let result = self.netplay_error_receiver.as_mut().unwrap().try_recv();
|
||||
if result.is_ok() {
|
||||
self.netplay.error = result.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
if !self.netplay.error.is_empty() {
|
||||
gui_netplay::netplay_error(self, ctx, self.netplay.error.clone());
|
||||
}
|
||||
|
||||
if self.configure_profile {
|
||||
egui::Window::new("Configure Input Profile")
|
||||
// .open(&mut self.configure_profile)
|
||||
.show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
let name_label = ui.label("Profile Name: ");
|
||||
ui.text_edit_singleline(&mut self.profile_name)
|
||||
.labelled_by(name_label.id);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Configure Profile").clicked() {
|
||||
let profile_name = self.profile_name.clone();
|
||||
let config_dir = self.config_dir.clone();
|
||||
execute(async {
|
||||
let mut game_ui = ui::Ui::new(config_dir);
|
||||
ui::input::configure_input_profile(&mut game_ui, profile_name);
|
||||
});
|
||||
self.configure_profile = false;
|
||||
if !self.profile_name.is_empty()
|
||||
&& !self.input_profiles.contains(&self.profile_name)
|
||||
{
|
||||
self.input_profiles.push(self.profile_name.clone())
|
||||
}
|
||||
};
|
||||
if ui.button("Close").clicked() {
|
||||
self.configure_profile = false
|
||||
};
|
||||
});
|
||||
});
|
||||
configure_profile(self, ctx);
|
||||
}
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
if self.configure_profile {
|
||||
ui.disable()
|
||||
}
|
||||
egui::Grid::new("button_grid")
|
||||
.min_col_width(200.0)
|
||||
.show(ui, |ui| {
|
||||
if ui.button("Open ROM").clicked() {
|
||||
open_rom(self, ctx);
|
||||
}
|
||||
|
||||
if ui.button("Open ROM").clicked() {
|
||||
// Spawn dialog on main thread
|
||||
let task = rfd::AsyncFileDialog::new().pick_file();
|
||||
let selected_controller = self.selected_controller;
|
||||
let selected_profile = self.selected_profile.clone();
|
||||
let controller_enabled = self.controller_enabled;
|
||||
let upscale = self.upscale;
|
||||
let fullscreen = self.fullscreen;
|
||||
let emulate_vru = self.emulate_vru;
|
||||
let config_dir = self.config_dir.clone();
|
||||
let cache_dir = self.cache_dir.clone();
|
||||
let data_dir = self.data_dir.clone();
|
||||
if ui.button("Netplay: Create Session").clicked()
|
||||
&& !self.cache_dir.join("game_running").exists()
|
||||
{
|
||||
self.netplay.create = true;
|
||||
}
|
||||
|
||||
let (vru_window_notifier, vru_window_receiver): (
|
||||
std::sync::mpsc::Sender<Vec<String>>,
|
||||
std::sync::mpsc::Receiver<Vec<String>>,
|
||||
) = std::sync::mpsc::channel();
|
||||
ui.end_row();
|
||||
|
||||
let (vru_word_notifier, vru_word_receiver): (
|
||||
std::sync::mpsc::Sender<String>,
|
||||
std::sync::mpsc::Receiver<String>,
|
||||
) = std::sync::mpsc::channel();
|
||||
if ui.button("Configure Input Profile").clicked()
|
||||
&& !self.cache_dir.join("game_running").exists()
|
||||
{
|
||||
self.configure_profile = true;
|
||||
}
|
||||
|
||||
if emulate_vru {
|
||||
self.vru_window_receiver = Some(vru_window_receiver);
|
||||
self.vru_word_notifier = Some(vru_word_notifier);
|
||||
}
|
||||
|
||||
let gui_ctx = ctx.clone();
|
||||
execute(async move {
|
||||
let file = task.await;
|
||||
|
||||
if let Some(file) = file {
|
||||
let running_file = cache_dir.join("game_running");
|
||||
if running_file.exists() {
|
||||
return;
|
||||
}
|
||||
let result = std::fs::File::create(running_file.clone());
|
||||
if result.is_err() {
|
||||
panic!("could not create running file: {}", result.err().unwrap())
|
||||
}
|
||||
let mut device = device::Device::new(config_dir);
|
||||
save_config(
|
||||
&mut device.ui,
|
||||
selected_controller,
|
||||
selected_profile,
|
||||
controller_enabled,
|
||||
upscale,
|
||||
fullscreen,
|
||||
emulate_vru,
|
||||
);
|
||||
if emulate_vru {
|
||||
device.vru.window_notifier = Some(vru_window_notifier);
|
||||
device.vru.word_receiver = Some(vru_word_receiver);
|
||||
device.vru.gui_ctx = Some(gui_ctx);
|
||||
}
|
||||
device::run_game(
|
||||
std::path::Path::new(file.path()),
|
||||
data_dir,
|
||||
&mut device,
|
||||
fullscreen,
|
||||
);
|
||||
let result = std::fs::remove_file(running_file.clone());
|
||||
if result.is_err() {
|
||||
panic!("could not remove running file: {}", result.err().unwrap())
|
||||
}
|
||||
if ui.button("Netplay: Join Session").clicked()
|
||||
&& !self.cache_dir.join("game_running").exists()
|
||||
{
|
||||
self.netplay.join = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ui.button("Configure Input Profile").clicked() {
|
||||
if self.cache_dir.join("game_running").exists() {
|
||||
return;
|
||||
}
|
||||
self.configure_profile = true;
|
||||
}
|
||||
|
||||
ui.add_space(32.0);
|
||||
ui.label("Controller Config:");
|
||||
@@ -306,7 +460,9 @@ impl eframe::App for GopherEguiApp {
|
||||
});
|
||||
ui.add_space(32.0);
|
||||
ui.checkbox(&mut self.upscale, "High-Res Graphics");
|
||||
ui.checkbox(&mut self.integer_scaling, "Integer Scaling");
|
||||
ui.checkbox(&mut self.fullscreen, "Fullscreen (Esc closes game)");
|
||||
ui.add_space(32.0);
|
||||
ui.checkbox(
|
||||
&mut self.emulate_vru,
|
||||
"Emulate VRU (connects VRU to controller port 4)",
|
||||
@@ -316,7 +472,7 @@ impl eframe::App for GopherEguiApp {
|
||||
});
|
||||
|
||||
if self.emulate_vru && self.vru_window_receiver.is_some() {
|
||||
let result = self.vru_window_receiver.as_ref().unwrap().try_recv();
|
||||
let result = self.vru_window_receiver.as_mut().unwrap().try_recv();
|
||||
if result.is_ok() {
|
||||
self.show_vru_dialog = true;
|
||||
self.vru_word_list = result.unwrap();
|
||||
@@ -324,52 +480,11 @@ impl eframe::App for GopherEguiApp {
|
||||
}
|
||||
|
||||
if self.show_vru_dialog {
|
||||
ctx.show_viewport_immediate(
|
||||
egui::ViewportId::from_hash_of("vru_dialog"),
|
||||
egui::ViewportBuilder::default()
|
||||
.with_title("What would you like to say?")
|
||||
.with_always_on_top(),
|
||||
|ctx, class| {
|
||||
assert!(
|
||||
class == egui::ViewportClass::Immediate,
|
||||
"This egui backend doesn't support multiple viewports"
|
||||
);
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
egui::Grid::new("vru_words").show(ui, |ui| {
|
||||
for (i, v) in self.vru_word_list.iter().enumerate() {
|
||||
if i % 5 == 0 {
|
||||
ui.end_row();
|
||||
}
|
||||
if ui.button((*v).to_string()).clicked() {
|
||||
self.vru_word_notifier
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send(v.clone())
|
||||
.unwrap();
|
||||
self.show_vru_dialog = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if ctx.input(|i| i.viewport().close_requested()) {
|
||||
self.vru_word_notifier
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send(String::from(""))
|
||||
.unwrap();
|
||||
self.show_vru_dialog = false;
|
||||
}
|
||||
},
|
||||
);
|
||||
show_vru_dialog(self, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute<F: std::future::Future<Output = ()> + Send + 'static>(f: F) {
|
||||
std::thread::spawn(move || futures::executor::block_on(f));
|
||||
}
|
||||
|
||||
fn add_japanese_font(ctx: &egui::Context) {
|
||||
ctx.add_font(eframe::epaint::text::FontInsert::new(
|
||||
"japanese_font",
|
||||
|
||||
@@ -0,0 +1,870 @@
|
||||
use crate::device;
|
||||
use crate::ui::gui;
|
||||
use crate::ui::gui::GopherEguiApp;
|
||||
use eframe::egui;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const NETPLAY_VERSION: i32 = 17;
|
||||
const EMU_NAME: &str = "gopher64";
|
||||
|
||||
type GameInfo = (String, String, String, Vec<u8>);
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GuiNetplay {
|
||||
pub create: bool,
|
||||
pub join: bool,
|
||||
pub wait: bool,
|
||||
pub session_name: String,
|
||||
pub password: String,
|
||||
pub player_name: String,
|
||||
pub error: String,
|
||||
pub create_rom_label: String,
|
||||
pub join_rom_label: String,
|
||||
pub send_chat: bool,
|
||||
pub have_sessions: Option<(String, String)>,
|
||||
pub begin_game: bool,
|
||||
pub chat_log: String,
|
||||
pub chat_message: String,
|
||||
pub selected_session: Option<NetplayRoom>,
|
||||
pub pending_begin: bool,
|
||||
pub peer_addr: Option<std::net::SocketAddr>,
|
||||
pub motd: String,
|
||||
pub sessions: Vec<NetplayRoom>,
|
||||
pub rom_contents: Vec<u8>,
|
||||
pub player_number: u8,
|
||||
pub player_names: [String; 4],
|
||||
pub server: (String, String),
|
||||
pub socket_waiting: bool,
|
||||
pub game_info: GameInfo,
|
||||
pub servers: std::collections::HashMap<String, String>,
|
||||
pub waiting_session: Option<NetplayRoom>,
|
||||
pub socket:
|
||||
Option<tungstenite::WebSocket<tungstenite::stream::MaybeTlsStream<std::net::TcpStream>>>,
|
||||
pub server_receiver:
|
||||
Option<tokio::sync::mpsc::Receiver<std::collections::HashMap<String, String>>>,
|
||||
pub game_info_receiver: Option<tokio::sync::mpsc::Receiver<GameInfo>>,
|
||||
pub broadcast_socket: Option<std::net::UdpSocket>,
|
||||
pub broadcast_timer: Option<std::time::Instant>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Default, Clone)]
|
||||
pub struct NetplayRoom {
|
||||
room_name: Option<String>,
|
||||
password: Option<String>,
|
||||
protected: Option<bool>,
|
||||
#[serde(rename = "MD5")]
|
||||
md5: Option<String>,
|
||||
game_name: Option<String>,
|
||||
pub port: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct NetplayMessage {
|
||||
#[serde(rename = "type")]
|
||||
message_type: String,
|
||||
player_name: Option<String>,
|
||||
client_sha: Option<String>,
|
||||
netplay_version: Option<i32>,
|
||||
emulator: Option<String>,
|
||||
room: Option<NetplayRoom>,
|
||||
accept: Option<i32>,
|
||||
message: Option<String>,
|
||||
auth: Option<String>,
|
||||
player_names: Option<[String; 4]>,
|
||||
#[serde(rename = "authTime")]
|
||||
auth_time: Option<String>,
|
||||
rooms: Option<Vec<NetplayRoom>>,
|
||||
}
|
||||
|
||||
fn get_servers(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
if app.netplay.servers.is_empty() {
|
||||
if app.netplay.broadcast_socket.is_none() {
|
||||
app.netplay.broadcast_socket = Some(
|
||||
std::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, 0))
|
||||
.expect("couldn't bind to address"),
|
||||
);
|
||||
let socket = app.netplay.broadcast_socket.as_ref().unwrap();
|
||||
socket
|
||||
.set_broadcast(true)
|
||||
.expect("set_broadcast call failed");
|
||||
socket
|
||||
.set_nonblocking(true)
|
||||
.expect("could not set up socket");
|
||||
let data: [u8; 1] = [1];
|
||||
socket
|
||||
.send_to(&data, (std::net::Ipv4Addr::BROADCAST, 45000))
|
||||
.expect("couldn't send data");
|
||||
app.netplay.broadcast_timer =
|
||||
Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
|
||||
ctx.request_repaint();
|
||||
}
|
||||
if app.netplay.server_receiver.is_none() {
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(1);
|
||||
app.netplay.server_receiver = Some(rx);
|
||||
let gui_ctx = ctx.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Ok(response) =
|
||||
reqwest::get("https://m64p.s3.amazonaws.com/servers.json").await
|
||||
{
|
||||
if let Ok(servers) = response
|
||||
.json::<std::collections::HashMap<String, String>>()
|
||||
.await
|
||||
{
|
||||
let _ = tx.send(servers).await;
|
||||
gui_ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if app.netplay.broadcast_timer.is_some()
|
||||
&& std::time::Instant::now() > app.netplay.broadcast_timer.unwrap()
|
||||
{
|
||||
app.netplay.broadcast_timer = None;
|
||||
}
|
||||
if app.netplay.broadcast_socket.is_some() && app.netplay.broadcast_timer.is_some() {
|
||||
let mut buffer = [0; 1024];
|
||||
let result = app
|
||||
.netplay
|
||||
.broadcast_socket
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.recv_from(&mut buffer);
|
||||
if result.is_ok() {
|
||||
let (amt, _src) = result.unwrap();
|
||||
let data: std::collections::HashMap<String, String> =
|
||||
serde_json::from_slice(&buffer[..amt]).unwrap();
|
||||
for server in data.iter() {
|
||||
let (server_name, server_ip) = server;
|
||||
app.netplay
|
||||
.servers
|
||||
.insert(server_name.to_string(), server_ip.to_string());
|
||||
app.netplay.server = (server.0.clone(), server.1.clone());
|
||||
}
|
||||
app.netplay.broadcast_socket = None;
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
if app.netplay.server_receiver.is_some() {
|
||||
let result = app.netplay.server_receiver.as_mut().unwrap().try_recv();
|
||||
if result.is_ok() {
|
||||
app.netplay.servers.extend(result.unwrap());
|
||||
app.netplay.server_receiver = None;
|
||||
if app.netplay.server.0.is_empty() {
|
||||
let first_server = app.netplay.servers.iter().next().unwrap();
|
||||
app.netplay.server = (first_server.0.clone(), first_server.1.clone());
|
||||
}
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn netplay_create(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
egui::Window::new("Create Netplay Session").show(ctx, |ui| {
|
||||
egui::Grid::new("netplay_create_grid").show(ui, |ui| {
|
||||
let profile_name_label = ui.label("Session Name:");
|
||||
let mut size = ui.spacing().interact_size;
|
||||
size.x = 200.0;
|
||||
ui.add_sized(size, |ui: &mut egui::Ui| {
|
||||
ui.text_edit_singleline(&mut app.netplay.session_name)
|
||||
.labelled_by(profile_name_label.id)
|
||||
});
|
||||
|
||||
ui.end_row();
|
||||
|
||||
let password_label = ui.label("Password (Optional):");
|
||||
|
||||
ui.text_edit_singleline(&mut app.netplay.password)
|
||||
.labelled_by(password_label.id);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("ROM");
|
||||
if app.netplay.create_rom_label.is_empty() {
|
||||
app.netplay.create_rom_label = "Open ROM".to_string();
|
||||
}
|
||||
if ui.button(&app.netplay.create_rom_label).clicked() {
|
||||
let task = rfd::AsyncFileDialog::new().pick_file();
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(1);
|
||||
app.netplay.game_info_receiver = Some(rx);
|
||||
let gui_ctx = ctx.clone();
|
||||
app.netplay.create_rom_label = "Inspecting ROM".to_string();
|
||||
tokio::spawn(async move {
|
||||
let file = task.await;
|
||||
|
||||
if let Some(file) = file {
|
||||
let rom_contents = device::get_rom_contents(file.path());
|
||||
if !rom_contents.is_empty() {
|
||||
let hash = device::cart_rom::calculate_hash(&rom_contents);
|
||||
let game_name = std::str::from_utf8(&rom_contents[0x20..0x20 + 0x14])
|
||||
.unwrap()
|
||||
.to_string();
|
||||
let _ = tx
|
||||
.send((hash, game_name, file.file_name(), rom_contents))
|
||||
.await;
|
||||
} else {
|
||||
let _ = tx
|
||||
.send((
|
||||
"".to_string(),
|
||||
"".to_string(),
|
||||
"Invalid ROM".to_string(),
|
||||
vec![],
|
||||
))
|
||||
.await;
|
||||
}
|
||||
} else {
|
||||
let _ = tx
|
||||
.send((
|
||||
"".to_string(),
|
||||
"".to_string(),
|
||||
"Open ROM".to_string(),
|
||||
vec![],
|
||||
))
|
||||
.await;
|
||||
}
|
||||
gui_ctx.request_repaint();
|
||||
});
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
|
||||
let player_name_label = ui.label("Player Name:");
|
||||
|
||||
ui.text_edit_singleline(&mut app.netplay.player_name)
|
||||
.labelled_by(player_name_label.id);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Server:");
|
||||
|
||||
get_servers(app, ctx);
|
||||
|
||||
if app.netplay.game_info_receiver.is_some() {
|
||||
let result = app.netplay.game_info_receiver.as_mut().unwrap().try_recv();
|
||||
if result.is_ok() {
|
||||
app.netplay.game_info_receiver = None;
|
||||
let data = result.unwrap();
|
||||
if !data.0.is_empty() {
|
||||
app.netplay.game_info = data;
|
||||
app.netplay.rom_contents = app.netplay.game_info.3.clone();
|
||||
app.netplay.create_rom_label = app.netplay.game_info.2.clone();
|
||||
} else {
|
||||
app.netplay.create_rom_label = data.2;
|
||||
}
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
|
||||
egui::ComboBox::from_id_salt("server-combobox")
|
||||
.selected_text(app.netplay.server.0.to_string())
|
||||
.show_ui(ui, |ui| {
|
||||
for server in app.netplay.servers.iter() {
|
||||
ui.selectable_value(
|
||||
&mut app.netplay.server,
|
||||
(server.0.clone(), server.1.clone()),
|
||||
server.0,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
ui.end_row();
|
||||
|
||||
if app.netplay.socket_waiting {
|
||||
let data = app.netplay.socket.as_mut().unwrap().read();
|
||||
if data.is_ok() {
|
||||
let message: NetplayMessage =
|
||||
serde_json::from_slice(&data.unwrap().into_data()).unwrap();
|
||||
if message.accept.unwrap() == 0 {
|
||||
if message.message_type == "reply_create_room" {
|
||||
app.netplay.create = false;
|
||||
app.netplay.wait = true;
|
||||
app.netplay.waiting_session = Some(message.room.unwrap());
|
||||
}
|
||||
} else {
|
||||
app.netplay.error = message.message.unwrap();
|
||||
}
|
||||
app.netplay.socket_waiting = false;
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Create Session").clicked() {
|
||||
if app.netplay.player_name.is_empty() {
|
||||
app.netplay.error = "Player Name cannot be empty".to_string();
|
||||
} else if app.netplay.session_name.is_empty() {
|
||||
app.netplay.error = "Session Name cannot be empty".to_string();
|
||||
} else if app.netplay.game_info.0.is_empty() {
|
||||
app.netplay.error = "ROM not loaded".to_string();
|
||||
} else {
|
||||
let now_utc = chrono::Utc::now().timestamp_millis().to_string();
|
||||
let hasher = Sha256::new().chain_update(&now_utc).chain_update(EMU_NAME);
|
||||
let mut game_name = app.netplay.game_info.1.trim().to_string();
|
||||
if game_name.is_empty() {
|
||||
// If the ROM doesn't report a name, use the filename
|
||||
game_name = app.netplay.create_rom_label.clone();
|
||||
}
|
||||
let netplay_message = NetplayMessage {
|
||||
message_type: "request_create_room".to_string(),
|
||||
player_name: Some(app.netplay.player_name.clone()),
|
||||
client_sha: Some(env!("CARGO_PKG_VERSION").to_string()),
|
||||
netplay_version: Some(NETPLAY_VERSION),
|
||||
emulator: Some(EMU_NAME.to_string()),
|
||||
accept: None,
|
||||
message: None,
|
||||
rooms: None,
|
||||
auth_time: Some(now_utc),
|
||||
player_names: None,
|
||||
auth: Some(format!("{:x}", hasher.finalize())),
|
||||
room: Some(NetplayRoom {
|
||||
room_name: Some(app.netplay.session_name.clone()),
|
||||
password: Some(app.netplay.password.clone()),
|
||||
game_name: Some(game_name),
|
||||
md5: Some(app.netplay.game_info.0.clone()),
|
||||
protected: None,
|
||||
port: None,
|
||||
}),
|
||||
};
|
||||
let (mut socket, _response) =
|
||||
tungstenite::connect(&app.netplay.server.1).expect("Can't connect");
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&netplay_message).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
match socket.get_mut() {
|
||||
tungstenite::stream::MaybeTlsStream::Plain(stream) => {
|
||||
app.netplay.peer_addr = Some(stream.peer_addr().unwrap());
|
||||
stream.set_nonblocking(true)
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
.expect("could not set socket to non-blocking");
|
||||
app.netplay.socket_waiting = true;
|
||||
app.netplay.socket = Some(socket);
|
||||
}
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button("Close").clicked() {
|
||||
if let Some(socket) = app.netplay.socket.as_mut() {
|
||||
socket.close(None).unwrap();
|
||||
while socket.read().is_ok() {}
|
||||
}
|
||||
app.netplay = Default::default();
|
||||
};
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn get_sessions(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
if app.netplay.have_sessions.is_some()
|
||||
&& app.netplay.server != app.netplay.have_sessions.as_ref().unwrap().clone()
|
||||
{
|
||||
// User has changed the server
|
||||
app.netplay.have_sessions = None;
|
||||
app.netplay.socket = None;
|
||||
}
|
||||
if app.netplay.socket.is_none() {
|
||||
let (mut sock, _response) =
|
||||
tungstenite::connect(&app.netplay.server.1).expect("Can't connect");
|
||||
match sock.get_mut() {
|
||||
tungstenite::stream::MaybeTlsStream::Plain(stream) => {
|
||||
app.netplay.peer_addr = Some(stream.peer_addr().unwrap());
|
||||
stream.set_nonblocking(true)
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
.expect("could not set socket to non-blocking");
|
||||
app.netplay.socket = Some(sock);
|
||||
}
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
if app.netplay.have_sessions.is_none() {
|
||||
let now_utc = chrono::Utc::now().timestamp_millis().to_string();
|
||||
let hasher = Sha256::new().chain_update(&now_utc).chain_update(EMU_NAME);
|
||||
let request_rooms = NetplayMessage {
|
||||
message_type: "request_get_rooms".to_string(),
|
||||
player_name: None,
|
||||
client_sha: None,
|
||||
netplay_version: Some(NETPLAY_VERSION),
|
||||
player_names: None,
|
||||
emulator: Some(EMU_NAME.to_string()),
|
||||
accept: None,
|
||||
rooms: None,
|
||||
message: None,
|
||||
auth_time: Some(now_utc),
|
||||
auth: Some(format!("{:x}", hasher.finalize())),
|
||||
room: None,
|
||||
};
|
||||
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&request_rooms).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
app.netplay.have_sessions = Some(app.netplay.server.clone());
|
||||
app.netplay.socket_waiting = true;
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn netplay_join(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
if app.netplay.socket_waiting {
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
let data = socket.read();
|
||||
if data.is_ok() {
|
||||
let message: NetplayMessage =
|
||||
serde_json::from_slice(&data.unwrap().into_data()).unwrap();
|
||||
if message.accept.unwrap() == 0 {
|
||||
if message.message_type == "reply_get_rooms" {
|
||||
if message.rooms.is_some() {
|
||||
app.netplay.sessions = message.rooms.unwrap();
|
||||
}
|
||||
} else if message.message_type == "reply_join_room" {
|
||||
app.netplay.join = false;
|
||||
app.netplay.wait = true;
|
||||
app.netplay.waiting_session = Some(message.room.unwrap());
|
||||
}
|
||||
app.netplay.socket_waiting = false;
|
||||
} else {
|
||||
app.netplay.error = message.message.unwrap();
|
||||
app.netplay.join_rom_label = "Join Session (Open ROM)".to_string();
|
||||
}
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
if app.netplay.game_info_receiver.is_some() {
|
||||
let result = app.netplay.game_info_receiver.as_mut().unwrap().try_recv();
|
||||
if result.is_ok() {
|
||||
app.netplay.game_info_receiver = None;
|
||||
let data = result.unwrap();
|
||||
if !data.0.is_empty() {
|
||||
app.netplay.game_info = data;
|
||||
app.netplay.rom_contents = app.netplay.game_info.3.clone();
|
||||
|
||||
let netplay_message = NetplayMessage {
|
||||
message_type: "request_join_room".to_string(),
|
||||
player_name: Some(app.netplay.player_name.clone()),
|
||||
client_sha: Some(env!("CARGO_PKG_VERSION").to_string()),
|
||||
netplay_version: None,
|
||||
emulator: None,
|
||||
accept: None,
|
||||
message: None,
|
||||
rooms: None,
|
||||
auth_time: None,
|
||||
player_names: None,
|
||||
auth: None,
|
||||
room: Some(NetplayRoom {
|
||||
room_name: None,
|
||||
password: Some(app.netplay.password.clone()),
|
||||
game_name: None,
|
||||
md5: Some(app.netplay.game_info.0.clone()),
|
||||
protected: None,
|
||||
port: app.netplay.selected_session.as_ref().unwrap().port,
|
||||
}),
|
||||
};
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&netplay_message).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
|
||||
app.netplay.socket_waiting = true;
|
||||
} else {
|
||||
app.netplay.error = data.2;
|
||||
app.netplay.join_rom_label = "Join Session (Open ROM)".to_string();
|
||||
}
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
egui::Window::new("Join Netplay Session").show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
let mut size = ui.spacing().interact_size;
|
||||
size.x = 100.0;
|
||||
ui.add_sized(
|
||||
size,
|
||||
egui::TextEdit::singleline(&mut app.netplay.player_name).hint_text("Player name"),
|
||||
);
|
||||
|
||||
get_servers(app, ctx);
|
||||
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button("Refresh").clicked() {
|
||||
app.netplay.socket = None;
|
||||
app.netplay.have_sessions = None;
|
||||
app.netplay.selected_session = None;
|
||||
ctx.request_repaint();
|
||||
}
|
||||
egui::ComboBox::from_id_salt("server-combobox")
|
||||
.selected_text(app.netplay.server.0.to_string())
|
||||
.show_ui(ui, |ui| {
|
||||
for server in app.netplay.servers.iter() {
|
||||
ui.selectable_value(
|
||||
&mut app.netplay.server,
|
||||
(server.0.clone(), server.1.clone()),
|
||||
server.0,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
if !app.netplay.server.0.is_empty() {
|
||||
get_sessions(app, ctx);
|
||||
}
|
||||
ui.add_space(16.0);
|
||||
if app.netplay.sessions.is_empty() {
|
||||
ui.label("No sessions available");
|
||||
} else {
|
||||
egui::Grid::new("netplay_join_grid").show(ui, |ui| {
|
||||
ui.label(egui::RichText::new("Session Name (click to select)").underline());
|
||||
ui.label(egui::RichText::new("Game Name").underline());
|
||||
ui.label(egui::RichText::new("Password Protected").underline());
|
||||
ui.end_row();
|
||||
|
||||
for room in app.netplay.sessions.iter() {
|
||||
ui.selectable_value(
|
||||
&mut app.netplay.selected_session,
|
||||
Some(room.clone()),
|
||||
room.room_name.as_ref().unwrap(),
|
||||
);
|
||||
ui.label(room.game_name.as_ref().unwrap());
|
||||
ui.label(room.protected.unwrap_or(false).to_string());
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
}
|
||||
ui.add_space(16.0);
|
||||
ui.horizontal(|ui| {
|
||||
let mut size = ui.spacing().interact_size;
|
||||
size.x = 130.0;
|
||||
ui.add_sized(
|
||||
size,
|
||||
egui::TextEdit::singleline(&mut app.netplay.password)
|
||||
.hint_text("Password (if required)"),
|
||||
);
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button("Close").clicked() {
|
||||
if let Some(socket) = app.netplay.socket.as_mut() {
|
||||
socket.close(None).unwrap();
|
||||
while socket.read().is_ok() {}
|
||||
}
|
||||
app.netplay = Default::default();
|
||||
};
|
||||
if app.netplay.join_rom_label.is_empty() {
|
||||
app.netplay.join_rom_label = "Join Session (Open ROM)".to_string();
|
||||
}
|
||||
if ui.button(&app.netplay.join_rom_label).clicked() {
|
||||
if app.netplay.player_name.is_empty() {
|
||||
app.netplay.error = "Player Name cannot be empty".to_string();
|
||||
} else if app.netplay.selected_session.is_none() {
|
||||
app.netplay.error = "No session selected".to_string();
|
||||
} else if app
|
||||
.netplay
|
||||
.selected_session
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.protected
|
||||
.unwrap()
|
||||
&& app.netplay.password.is_empty()
|
||||
{
|
||||
app.netplay.error = "Session requires a password".to_string();
|
||||
} else {
|
||||
let task = rfd::AsyncFileDialog::new().pick_file();
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(1);
|
||||
app.netplay.game_info_receiver = Some(rx);
|
||||
let gui_ctx = ctx.clone();
|
||||
app.netplay.join_rom_label = "Inspecting ROM".to_string();
|
||||
tokio::spawn(async move {
|
||||
let file = task.await;
|
||||
|
||||
if let Some(file) = file {
|
||||
let rom_contents = device::get_rom_contents(file.path());
|
||||
if !rom_contents.is_empty() {
|
||||
let hash = device::cart_rom::calculate_hash(&rom_contents);
|
||||
let game_name =
|
||||
std::str::from_utf8(&rom_contents[0x20..0x20 + 0x14])
|
||||
.unwrap()
|
||||
.to_string();
|
||||
let _ = tx
|
||||
.send((hash, game_name, file.file_name(), rom_contents))
|
||||
.await;
|
||||
} else {
|
||||
let _ = tx
|
||||
.send((
|
||||
"".to_string(),
|
||||
"".to_string(),
|
||||
"Invalid ROM".to_string(),
|
||||
vec![],
|
||||
))
|
||||
.await;
|
||||
}
|
||||
} else {
|
||||
let _ = tx
|
||||
.send((
|
||||
"".to_string(),
|
||||
"".to_string(),
|
||||
"No ROM selected".to_string(),
|
||||
vec![],
|
||||
))
|
||||
.await;
|
||||
}
|
||||
gui_ctx.request_repaint();
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn netplay_wait(app: &mut GopherEguiApp, ctx: &egui::Context) {
|
||||
let motd_message = NetplayMessage {
|
||||
message_type: "request_motd".to_string(),
|
||||
player_name: None,
|
||||
client_sha: None,
|
||||
netplay_version: None,
|
||||
emulator: None,
|
||||
accept: None,
|
||||
rooms: None,
|
||||
player_names: None,
|
||||
message: None,
|
||||
auth_time: None,
|
||||
auth: None,
|
||||
room: None,
|
||||
};
|
||||
let request_players = NetplayMessage {
|
||||
message_type: "request_players".to_string(),
|
||||
player_name: None,
|
||||
client_sha: None,
|
||||
netplay_version: None,
|
||||
player_names: None,
|
||||
emulator: None,
|
||||
rooms: None,
|
||||
accept: None,
|
||||
message: None,
|
||||
auth_time: None,
|
||||
auth: None,
|
||||
room: Some(NetplayRoom {
|
||||
room_name: None,
|
||||
password: None,
|
||||
game_name: None,
|
||||
md5: None,
|
||||
protected: None,
|
||||
port: app.netplay.waiting_session.as_ref().unwrap().port,
|
||||
}),
|
||||
};
|
||||
|
||||
if !app.netplay.socket_waiting {
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&motd_message).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&request_players).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
app.netplay.socket_waiting = true;
|
||||
}
|
||||
|
||||
if app.netplay.begin_game {
|
||||
let begin_game = NetplayMessage {
|
||||
message_type: "request_begin_game".to_string(),
|
||||
player_name: None,
|
||||
client_sha: None,
|
||||
netplay_version: None,
|
||||
player_names: None,
|
||||
rooms: None,
|
||||
emulator: None,
|
||||
accept: None,
|
||||
message: None,
|
||||
auth_time: None,
|
||||
auth: None,
|
||||
room: Some(NetplayRoom {
|
||||
room_name: None,
|
||||
password: None,
|
||||
game_name: None,
|
||||
md5: None,
|
||||
protected: None,
|
||||
port: app.netplay.waiting_session.as_ref().unwrap().port,
|
||||
}),
|
||||
};
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&begin_game).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
app.netplay.begin_game = false;
|
||||
}
|
||||
|
||||
if app.netplay.send_chat {
|
||||
let send_chat = NetplayMessage {
|
||||
message_type: "request_chat_message".to_string(),
|
||||
player_name: Some(app.netplay.player_name.clone()),
|
||||
client_sha: None,
|
||||
netplay_version: None,
|
||||
player_names: None,
|
||||
rooms: None,
|
||||
emulator: None,
|
||||
accept: None,
|
||||
message: Some(app.netplay.chat_message.clone()),
|
||||
auth_time: None,
|
||||
auth: None,
|
||||
room: Some(NetplayRoom {
|
||||
room_name: None,
|
||||
password: None,
|
||||
game_name: None,
|
||||
md5: None,
|
||||
protected: None,
|
||||
port: app.netplay.waiting_session.as_ref().unwrap().port,
|
||||
}),
|
||||
};
|
||||
app.netplay.chat_message.clear();
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
socket
|
||||
.send(tungstenite::Message::Binary(tungstenite::Bytes::from(
|
||||
serde_json::to_vec(&send_chat).unwrap(),
|
||||
)))
|
||||
.unwrap();
|
||||
app.netplay.send_chat = false;
|
||||
}
|
||||
|
||||
if app.netplay.socket_waiting {
|
||||
let socket = app.netplay.socket.as_mut().unwrap();
|
||||
let data = socket.read();
|
||||
if data.is_ok() {
|
||||
let message: NetplayMessage =
|
||||
serde_json::from_slice(&data.unwrap().into_data()).unwrap();
|
||||
if message.accept.unwrap() == 0 {
|
||||
if message.message_type == "reply_motd" {
|
||||
let re = regex::Regex::new(r"<[^>]*>").unwrap();
|
||||
app.netplay.motd = re
|
||||
.replace_all(message.message.unwrap().as_str(), "")
|
||||
.into_owned();
|
||||
} else if message.message_type == "reply_players" {
|
||||
app.netplay.player_names = message.player_names.unwrap();
|
||||
} else if message.message_type == "reply_chat_message" {
|
||||
app.netplay.chat_log.push_str(&message.message.unwrap());
|
||||
app.netplay.chat_log.push('\n');
|
||||
} else if message.message_type == "reply_begin_game" {
|
||||
let mut player = 0;
|
||||
for (i, name) in app.netplay.player_names.iter().enumerate() {
|
||||
if *name == app.netplay.player_name {
|
||||
player = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
app.netplay.player_number = player as u8;
|
||||
socket.close(None).unwrap();
|
||||
while socket.read().is_ok() {}
|
||||
gui::open_rom(app, ctx);
|
||||
app.netplay = Default::default();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
app.netplay.error = message.message.unwrap();
|
||||
}
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
|
||||
egui::Window::new("Pending Netplay Session").show(ctx, |ui| {
|
||||
egui::Grid::new("netplay_wait_grid_1").show(ui, |ui| {
|
||||
ui.label("Session Name:");
|
||||
let room_name = app
|
||||
.netplay
|
||||
.waiting_session
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.room_name
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
let game_name = app
|
||||
.netplay
|
||||
.waiting_session
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.game_name
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
ui.label(room_name);
|
||||
ui.end_row();
|
||||
ui.label("Game Name:");
|
||||
ui.label(game_name);
|
||||
ui.end_row();
|
||||
for i in 0..4 {
|
||||
ui.label(format!("Player {}:", i + 1));
|
||||
ui.label(app.netplay.player_names[i].clone());
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
egui::ScrollArea::vertical()
|
||||
.max_height(100.0)
|
||||
.stick_to_bottom(true)
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut app.netplay.chat_log)
|
||||
.interactive(false)
|
||||
.desired_width(ui.available_width()),
|
||||
);
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let mut size = ui.spacing().interact_size;
|
||||
size.x = 200.0;
|
||||
|
||||
ui.add_sized(
|
||||
size,
|
||||
egui::TextEdit::singleline(&mut app.netplay.chat_message)
|
||||
.hint_text("Enter chat message here"),
|
||||
);
|
||||
|
||||
if ui
|
||||
.add_enabled(!app.netplay.send_chat, egui::Button::new("Send Message"))
|
||||
.clicked()
|
||||
&& !app.netplay.chat_message.is_empty()
|
||||
{
|
||||
app.netplay.send_chat = true;
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(16.0);
|
||||
ui.label(app.netplay.motd.clone());
|
||||
ui.add_space(16.0);
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let button_enabled = app.netplay.player_name == app.netplay.player_names[0]
|
||||
&& !app.netplay.pending_begin;
|
||||
if ui
|
||||
.add_enabled(button_enabled, egui::Button::new("Start Session"))
|
||||
.clicked()
|
||||
{
|
||||
app.netplay.begin_game = true;
|
||||
app.netplay.pending_begin = true;
|
||||
}
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button("Close").clicked() {
|
||||
if let Some(socket) = app.netplay.socket.as_mut() {
|
||||
socket.close(None).unwrap();
|
||||
while socket.read().is_ok() {}
|
||||
}
|
||||
app.netplay = Default::default();
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn netplay_error(app: &mut GopherEguiApp, ctx: &egui::Context, error: String) {
|
||||
egui::Window::new("Netplay Error").show(ctx, |ui| {
|
||||
ui.label(error);
|
||||
if ui.button("Close").clicked() {
|
||||
app.netplay.error = "".to_string();
|
||||
};
|
||||
});
|
||||
}
|
||||
+173
-46
@@ -16,22 +16,25 @@ const D_CBUTTON: usize = 10;
|
||||
const U_CBUTTON: usize = 11;
|
||||
const R_TRIG: usize = 12;
|
||||
const L_TRIG: usize = 13;
|
||||
const X_AXIS: usize = 16;
|
||||
const Y_AXIS: usize = 24;
|
||||
|
||||
const AXIS_LEFT: usize = 14;
|
||||
const AXIS_RIGHT: usize = 15;
|
||||
const AXIS_UP: usize = 16;
|
||||
const AXIS_DOWN: usize = 17;
|
||||
const CHANGE_PAK: usize = 18;
|
||||
pub const PROFILE_SIZE: usize = 19;
|
||||
|
||||
const X_AXIS_SHIFT: usize = 16;
|
||||
const Y_AXIS_SHIFT: usize = 24;
|
||||
|
||||
const MAX_AXIS_VALUE: f64 = 85.0;
|
||||
|
||||
pub struct Controllers {
|
||||
pub rumble: bool,
|
||||
pub game_controller: Option<sdl2::controller::GameController>,
|
||||
pub joystick: Option<sdl2::joystick::Joystick>,
|
||||
}
|
||||
|
||||
pub fn bound_axis(x: &mut f64, y: &mut f64) {
|
||||
fn bound_axis(x: &mut f64, y: &mut f64) {
|
||||
let radius = 95.0; // this is roughly the maxium diagonal distance of the controller
|
||||
|
||||
// Calculate the distance from the origin (0, 0)
|
||||
@@ -45,7 +48,7 @@ pub fn bound_axis(x: &mut f64, y: &mut f64) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_axis_from_joystick(
|
||||
fn set_axis_from_joystick(
|
||||
profile: &ui::config::InputProfile,
|
||||
joystick: &sdl2::joystick::Joystick,
|
||||
) -> (f64, f64) {
|
||||
@@ -111,7 +114,7 @@ fn get_button_from_i32(button: i32) -> sdl2::controller::Button {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_axis_from_controller(
|
||||
fn set_axis_from_controller(
|
||||
profile: &ui::config::InputProfile,
|
||||
controller: &sdl2::controller::GameController,
|
||||
) -> (f64, f64) {
|
||||
@@ -147,9 +150,9 @@ pub fn set_axis_from_controller(
|
||||
(x, y)
|
||||
}
|
||||
|
||||
pub fn set_axis_from_keys(
|
||||
fn set_axis_from_keys(
|
||||
profile: &ui::config::InputProfile,
|
||||
keyboard_state: sdl2::keyboard::KeyboardState,
|
||||
keyboard_state: &sdl2::keyboard::KeyboardState,
|
||||
) -> (f64, f64) {
|
||||
let mut x = 0.0;
|
||||
let mut y = 0.0;
|
||||
@@ -184,7 +187,7 @@ pub fn set_axis_from_keys(
|
||||
(x, y)
|
||||
}
|
||||
|
||||
pub fn set_buttons_from_joystick(
|
||||
fn set_buttons_from_joystick(
|
||||
profile: &ui::config::InputProfile,
|
||||
i: usize,
|
||||
joystick: &sdl2::joystick::Joystick,
|
||||
@@ -214,7 +217,7 @@ pub fn set_buttons_from_joystick(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_buttons_from_controller(
|
||||
fn set_buttons_from_controller(
|
||||
profile: &ui::config::InputProfile,
|
||||
i: usize,
|
||||
controller: &sdl2::controller::GameController,
|
||||
@@ -236,7 +239,69 @@ pub fn set_buttons_from_controller(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(ui: &mut ui::Ui, channel: usize) -> u32 {
|
||||
pub fn set_rumble(ui: &mut ui::Ui, channel: usize, rumble: u8) {
|
||||
if !ui.controllers[channel].rumble {
|
||||
return;
|
||||
}
|
||||
let controller = &mut ui.controllers[channel].game_controller;
|
||||
let joystick = &mut ui.controllers[channel].joystick;
|
||||
if controller.is_some() {
|
||||
controller
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.set_rumble(
|
||||
(rumble & 1) as u16 * u16::MAX,
|
||||
(rumble & 1) as u16 * u16::MAX,
|
||||
(rumble & 1) as u32 * 60000,
|
||||
)
|
||||
.unwrap();
|
||||
} else if joystick.is_some() {
|
||||
joystick
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.set_rumble(
|
||||
(rumble & 1) as u16 * u16::MAX,
|
||||
(rumble & 1) as u16 * u16::MAX,
|
||||
(rumble & 1) as u32 * 60000,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn change_paks(
|
||||
profile: &ui::config::InputProfile,
|
||||
joystick: &Option<sdl2::joystick::Joystick>,
|
||||
controller: &Option<sdl2::controller::GameController>,
|
||||
keyboard_state: &sdl2::keyboard::KeyboardState,
|
||||
) -> bool {
|
||||
let controller_button = profile.controller_buttons[CHANGE_PAK];
|
||||
let joystick_button = profile.joystick_buttons[CHANGE_PAK];
|
||||
let joystick_hat = profile.joystick_hat[CHANGE_PAK];
|
||||
let key = profile.keys[CHANGE_PAK];
|
||||
|
||||
let mut pressed = false;
|
||||
if controller_button.0 && controller.is_some() {
|
||||
pressed = controller
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.button(get_button_from_i32(controller_button.1));
|
||||
} else if joystick_button.0 && joystick.is_some() {
|
||||
pressed = joystick
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.button(joystick_button.1)
|
||||
.unwrap();
|
||||
} else if joystick_hat.0 && joystick.is_some() {
|
||||
pressed = joystick.as_ref().unwrap().hat(joystick_hat.1).unwrap()
|
||||
== sdl2::joystick::HatState::from_raw(joystick_hat.2);
|
||||
} else if key.0 {
|
||||
pressed =
|
||||
keyboard_state.is_scancode_pressed(sdl2::keyboard::Scancode::from_i32(key.1).unwrap());
|
||||
}
|
||||
pressed
|
||||
}
|
||||
|
||||
pub fn get(ui: &mut ui::Ui, channel: usize) -> (u32, bool) {
|
||||
let events = ui.sdl_context.as_ref().unwrap().event_pump().unwrap();
|
||||
let keyboard_state = events.keyboard_state();
|
||||
|
||||
@@ -256,10 +321,10 @@ pub fn get(ui: &mut ui::Ui, channel: usize) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
if joystick.is_some() {
|
||||
set_buttons_from_joystick(profile, i, joystick.as_ref().unwrap(), &mut keys);
|
||||
} else if controller.is_some() {
|
||||
if controller.is_some() {
|
||||
set_buttons_from_controller(profile, i, controller.as_ref().unwrap(), &mut keys);
|
||||
} else if joystick.is_some() {
|
||||
set_buttons_from_joystick(profile, i, joystick.as_ref().unwrap(), &mut keys);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,19 +332,23 @@ pub fn get(ui: &mut ui::Ui, channel: usize) -> u32 {
|
||||
let mut y: f64 = 0.0;
|
||||
|
||||
if profile_name != "default" || channel == 0 {
|
||||
(x, y) = set_axis_from_keys(profile, keyboard_state);
|
||||
(x, y) = set_axis_from_keys(profile, &keyboard_state);
|
||||
}
|
||||
|
||||
if joystick.is_some() {
|
||||
(x, y) = set_axis_from_joystick(profile, joystick.as_ref().unwrap())
|
||||
} else if controller.is_some() {
|
||||
if controller.is_some() {
|
||||
(x, y) = set_axis_from_controller(profile, controller.as_ref().unwrap())
|
||||
} else if joystick.is_some() {
|
||||
(x, y) = set_axis_from_joystick(profile, joystick.as_ref().unwrap())
|
||||
}
|
||||
bound_axis(&mut x, &mut y);
|
||||
|
||||
keys |= (x.round() as i8 as u8 as u32) << X_AXIS;
|
||||
keys |= (y.round() as i8 as u8 as u32) << Y_AXIS;
|
||||
keys
|
||||
keys |= (x.round() as i8 as u8 as u32) << X_AXIS_SHIFT;
|
||||
keys |= (y.round() as i8 as u8 as u32) << Y_AXIS_SHIFT;
|
||||
|
||||
(
|
||||
keys,
|
||||
change_paks(profile, joystick, controller, &keyboard_state),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn list_controllers(ui: &ui::Ui) {
|
||||
@@ -323,7 +392,7 @@ pub fn clear_bindings(ui: &mut ui::Ui) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
|
||||
pub fn configure_input_profile(ui: &mut ui::Ui, profile: String, dinput: bool) {
|
||||
if profile == "default" {
|
||||
println!("Profile name cannot be default");
|
||||
return;
|
||||
@@ -332,10 +401,20 @@ pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
|
||||
println!("Profile name cannot be empty");
|
||||
return;
|
||||
}
|
||||
let controller_subsystem = ui.controller_subsystem.as_ref().unwrap();
|
||||
let joystick_subsystem = ui.joystick_subsystem.as_ref().unwrap();
|
||||
let mut controllers = vec![];
|
||||
let mut joysticks = vec![];
|
||||
for i in 0..joystick_subsystem.num_joysticks().unwrap() {
|
||||
joysticks.push(joystick_subsystem.open(i));
|
||||
if !dinput {
|
||||
if let Ok(controller) = controller_subsystem.open(i) {
|
||||
controllers.push(controller);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Ok(joystick) = joystick_subsystem.open(i) {
|
||||
joysticks.push(joystick);
|
||||
}
|
||||
}
|
||||
|
||||
let mut builder =
|
||||
@@ -349,7 +428,7 @@ pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
|
||||
let font =
|
||||
rusttype::Font::try_from_bytes(include_bytes!("../../data/Roboto-Regular.ttf")).unwrap();
|
||||
|
||||
let key_labels = [
|
||||
let key_labels: [(&str, usize); PROFILE_SIZE] = [
|
||||
("A", A_BUTTON),
|
||||
("B", B_BUTTON),
|
||||
("Start", START_BUTTON),
|
||||
@@ -368,14 +447,18 @@ pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
|
||||
("Control Stick Down", AXIS_DOWN),
|
||||
("Control Stick Left", AXIS_LEFT),
|
||||
("Control Stick Right", AXIS_RIGHT),
|
||||
("Change Pak", CHANGE_PAK),
|
||||
];
|
||||
|
||||
let mut new_keys = [(false, 0); 18];
|
||||
let mut new_joystick_buttons = [(false, 0u32); 14];
|
||||
let mut new_joystick_hat = [(false, 0u32, 0); 14];
|
||||
let mut new_joystick_axis = [(false, 0u32, 0); 18];
|
||||
let mut new_keys = [(false, 0); PROFILE_SIZE];
|
||||
let mut new_joystick_buttons = [(false, 0u32); PROFILE_SIZE];
|
||||
let mut new_joystick_hat = [(false, 0u32, 0); PROFILE_SIZE];
|
||||
let mut new_joystick_axis = [(false, 0u32, 0); PROFILE_SIZE];
|
||||
let mut new_controller_buttons = [(false, 0i32); PROFILE_SIZE];
|
||||
let mut new_controller_axis = [(false, 0i32, 0); PROFILE_SIZE];
|
||||
|
||||
let mut last_axis_result = (false, 0, 0);
|
||||
let mut last_joystick_axis_result = (false, 0, 0);
|
||||
let mut last_controller_axis_result = (false, 0, 0);
|
||||
let mut events = ui.sdl_context.as_ref().unwrap().event_pump().unwrap();
|
||||
for (key, value) in key_labels.iter() {
|
||||
for _event in events.poll_iter() {} // clear events
|
||||
@@ -402,32 +485,57 @@ pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
|
||||
new_keys[*value] = (true, scancode as i32);
|
||||
key_set = true
|
||||
}
|
||||
sdl2::event::Event::ControllerButtonDown { button, .. } => {
|
||||
if !controllers.is_empty() {
|
||||
new_controller_buttons[*value] = (true, button as i32);
|
||||
key_set = true
|
||||
}
|
||||
}
|
||||
sdl2::event::Event::ControllerAxisMotion {
|
||||
axis,
|
||||
value: axis_value,
|
||||
..
|
||||
} => {
|
||||
if !controllers.is_empty() && axis_value.saturating_abs() > i16::MAX / 2 {
|
||||
let result =
|
||||
(true, axis as i32, axis_value / axis_value.saturating_abs());
|
||||
if result != last_controller_axis_result {
|
||||
new_controller_axis[*value] = result;
|
||||
last_controller_axis_result = result;
|
||||
key_set = true
|
||||
}
|
||||
}
|
||||
}
|
||||
sdl2::event::Event::JoyButtonDown { button_idx, .. } => {
|
||||
new_joystick_buttons[*value] = (true, button_idx as u32);
|
||||
key_set = true
|
||||
if !joysticks.is_empty() {
|
||||
new_joystick_buttons[*value] = (true, button_idx as u32);
|
||||
key_set = true
|
||||
}
|
||||
}
|
||||
sdl2::event::Event::JoyHatMotion { hat_idx, state, .. } => {
|
||||
new_joystick_hat[*value] = (
|
||||
true,
|
||||
hat_idx as u32,
|
||||
sdl2::joystick::HatState::to_raw(state),
|
||||
);
|
||||
key_set = true
|
||||
if !joysticks.is_empty() && state != sdl2::joystick::HatState::Centered {
|
||||
new_joystick_hat[*value] = (
|
||||
true,
|
||||
hat_idx as u32,
|
||||
sdl2::joystick::HatState::to_raw(state),
|
||||
);
|
||||
key_set = true
|
||||
}
|
||||
}
|
||||
sdl2::event::Event::JoyAxisMotion {
|
||||
axis_idx,
|
||||
value: axis_value,
|
||||
..
|
||||
} => {
|
||||
if axis_value.saturating_abs() > 24576 {
|
||||
if !joysticks.is_empty() && axis_value.saturating_abs() > i16::MAX / 2 {
|
||||
let result = (
|
||||
true,
|
||||
axis_idx as u32,
|
||||
axis_value / axis_value.saturating_abs(),
|
||||
);
|
||||
if result != last_axis_result {
|
||||
if result != last_joystick_axis_result {
|
||||
new_joystick_axis[*value] = result;
|
||||
last_axis_result = result;
|
||||
last_joystick_axis_result = result;
|
||||
key_set = true
|
||||
}
|
||||
}
|
||||
@@ -440,19 +548,20 @@ pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
|
||||
|
||||
let new_profile = ui::config::InputProfile {
|
||||
keys: new_keys,
|
||||
controller_buttons: Default::default(),
|
||||
controller_axis: Default::default(),
|
||||
controller_buttons: new_controller_buttons,
|
||||
controller_axis: new_controller_axis,
|
||||
joystick_buttons: new_joystick_buttons,
|
||||
joystick_hat: new_joystick_hat,
|
||||
joystick_axis: new_joystick_axis,
|
||||
dinput,
|
||||
};
|
||||
ui.config.input.input_profiles.insert(profile, new_profile);
|
||||
}
|
||||
|
||||
pub fn get_default_profile() -> ui::config::InputProfile {
|
||||
let mut default_controller_buttons = [(false, 0); 14];
|
||||
let mut default_controller_axis = [(false, 0, 0); 18];
|
||||
let mut default_keys = [(false, 0); 18];
|
||||
let mut default_controller_buttons = [(false, 0); PROFILE_SIZE];
|
||||
let mut default_controller_axis = [(false, 0, 0); PROFILE_SIZE];
|
||||
let mut default_keys = [(false, 0); PROFILE_SIZE];
|
||||
default_keys[R_DPAD] = (true, sdl2::keyboard::Scancode::D as i32);
|
||||
default_keys[L_DPAD] = (true, sdl2::keyboard::Scancode::A as i32);
|
||||
default_keys[D_DPAD] = (true, sdl2::keyboard::Scancode::S as i32);
|
||||
@@ -471,6 +580,7 @@ pub fn get_default_profile() -> ui::config::InputProfile {
|
||||
default_keys[AXIS_RIGHT] = (true, sdl2::keyboard::Scancode::Right as i32);
|
||||
default_keys[AXIS_UP] = (true, sdl2::keyboard::Scancode::Up as i32);
|
||||
default_keys[AXIS_DOWN] = (true, sdl2::keyboard::Scancode::Down as i32);
|
||||
default_keys[CHANGE_PAK] = (true, sdl2::keyboard::Scancode::Comma as i32);
|
||||
|
||||
default_controller_buttons[R_DPAD] = (true, sdl2::controller::Button::DPadRight as i32);
|
||||
default_controller_buttons[L_DPAD] = (true, sdl2::controller::Button::DPadLeft as i32);
|
||||
@@ -490,6 +600,7 @@ pub fn get_default_profile() -> ui::config::InputProfile {
|
||||
default_controller_axis[AXIS_RIGHT] = (true, sdl2::controller::Axis::LeftX as i32, 1);
|
||||
default_controller_axis[AXIS_UP] = (true, sdl2::controller::Axis::LeftY as i32, -1);
|
||||
default_controller_axis[AXIS_DOWN] = (true, sdl2::controller::Axis::LeftY as i32, 1);
|
||||
default_controller_buttons[CHANGE_PAK] = (true, sdl2::controller::Button::Back as i32);
|
||||
|
||||
ui::config::InputProfile {
|
||||
keys: default_keys,
|
||||
@@ -498,6 +609,7 @@ pub fn get_default_profile() -> ui::config::InputProfile {
|
||||
joystick_buttons: Default::default(),
|
||||
joystick_hat: Default::default(),
|
||||
joystick_axis: Default::default(),
|
||||
dinput: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,12 +632,24 @@ pub fn init(ui: &mut ui::Ui) {
|
||||
}
|
||||
}
|
||||
if joystick_index < u32::MAX {
|
||||
if ui.config.input.input_profile_binding[i] == "default" {
|
||||
let profile_name = ui.config.input.input_profile_binding[i].clone();
|
||||
let profile = ui.config.input.input_profiles.get(&profile_name).unwrap();
|
||||
|
||||
if !profile.dinput {
|
||||
let controller_result = controller_subsystem.open(joystick_index);
|
||||
if controller_result.is_ok() {
|
||||
ui.controllers[i].game_controller = Some(controller_result.unwrap());
|
||||
if ui.controllers[i]
|
||||
.game_controller
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.has_rumble()
|
||||
{
|
||||
ui.controllers[i].rumble = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ui.controllers[i].game_controller.is_none() {
|
||||
let joystick_result = joystick_subsystem.open(joystick_index);
|
||||
if joystick_result.is_err() {
|
||||
@@ -535,6 +659,9 @@ pub fn init(ui: &mut ui::Ui) {
|
||||
)
|
||||
} else {
|
||||
ui.controllers[i].joystick = Some(joystick_result.unwrap());
|
||||
if ui.controllers[i].joystick.as_ref().unwrap().has_rumble() {
|
||||
ui.controllers[i].rumble = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
+100
-37
@@ -1,3 +1,5 @@
|
||||
use crate::device;
|
||||
use crate::netplay;
|
||||
use crate::ui;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
@@ -28,7 +30,7 @@ pub struct Saves {
|
||||
pub romsave: (serde_json::Map<String, serde_json::Value>, bool),
|
||||
}
|
||||
|
||||
pub fn get_save_type(game_id: &str) -> Vec<SaveTypes> {
|
||||
fn get_save_type(game_id: &str) -> Vec<SaveTypes> {
|
||||
match game_id {
|
||||
"NB7" | // Banjo-Tooie [Banjo to Kazooie no Daiboken 2 (J)]
|
||||
"NGT" | // City Tour GrandPrix - Zen Nihon GT Senshuken
|
||||
@@ -118,53 +120,114 @@ pub fn init(ui: &mut ui::Ui, data_dir: std::path::PathBuf) {
|
||||
.push(ui.game_id.to_owned() + "-" + &ui.game_hash + ".romsave");
|
||||
}
|
||||
|
||||
pub fn load_saves(ui: &mut ui::Ui) {
|
||||
let eep = std::fs::read(&mut ui.paths.eep_file_path);
|
||||
if eep.is_ok() {
|
||||
ui.saves.eeprom.0 = eep.unwrap();
|
||||
pub fn load_saves(ui: &mut ui::Ui, netplay: &mut Option<netplay::Netplay>) {
|
||||
if netplay.is_none() || netplay.as_ref().unwrap().player_number == 0 {
|
||||
let eep = std::fs::read(&mut ui.paths.eep_file_path);
|
||||
if eep.is_ok() {
|
||||
ui.saves.eeprom.0 = eep.unwrap();
|
||||
}
|
||||
let sra = std::fs::read(&mut ui.paths.sra_file_path);
|
||||
if sra.is_ok() {
|
||||
ui.saves.sram.0 = sra.unwrap();
|
||||
}
|
||||
let fla = std::fs::read(&mut ui.paths.fla_file_path);
|
||||
if fla.is_ok() {
|
||||
ui.saves.flash.0 = fla.unwrap();
|
||||
}
|
||||
let mempak = std::fs::read(&mut ui.paths.pak_file_path);
|
||||
if mempak.is_ok() {
|
||||
ui.saves.mempak.0 = mempak.unwrap();
|
||||
}
|
||||
if netplay.is_none() {
|
||||
// can't do romsaves with the current netplay implementation
|
||||
let romsave = std::fs::read(&mut ui.paths.romsave_file_path);
|
||||
if romsave.is_ok() {
|
||||
ui.saves.romsave.0 = serde_json::from_slice(romsave.unwrap().as_ref()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
let sra = std::fs::read(&mut ui.paths.sra_file_path);
|
||||
if sra.is_ok() {
|
||||
ui.saves.sram.0 = sra.unwrap();
|
||||
}
|
||||
let fla = std::fs::read(&mut ui.paths.fla_file_path);
|
||||
if fla.is_ok() {
|
||||
ui.saves.flash.0 = fla.unwrap();
|
||||
}
|
||||
let mempak = std::fs::read(&mut ui.paths.pak_file_path);
|
||||
if mempak.is_ok() {
|
||||
ui.saves.mempak.0 = mempak.unwrap();
|
||||
}
|
||||
let romsave = std::fs::read(&mut ui.paths.romsave_file_path);
|
||||
if romsave.is_ok() {
|
||||
ui.saves.romsave.0 = serde_json::from_slice(romsave.unwrap().as_ref()).unwrap();
|
||||
|
||||
if netplay.is_some() {
|
||||
if netplay.as_ref().unwrap().player_number == 0 {
|
||||
netplay::send_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"eep",
|
||||
&ui.saves.eeprom.0,
|
||||
device::cart::EEPROM_MAX_SIZE,
|
||||
);
|
||||
netplay::send_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"sra",
|
||||
&ui.saves.sram.0,
|
||||
device::sram::SRAM_SIZE,
|
||||
);
|
||||
netplay::send_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"fla",
|
||||
&ui.saves.flash.0,
|
||||
device::sram::FLASHRAM_SIZE,
|
||||
);
|
||||
netplay::send_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"mpk",
|
||||
&ui.saves.mempak.0,
|
||||
device::controller::mempak::MEMPAK_SIZE * 4,
|
||||
);
|
||||
} else {
|
||||
netplay::receive_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"eep",
|
||||
&mut ui.saves.eeprom.0,
|
||||
device::cart::EEPROM_MAX_SIZE,
|
||||
);
|
||||
netplay::receive_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"sra",
|
||||
&mut ui.saves.sram.0,
|
||||
device::sram::SRAM_SIZE,
|
||||
);
|
||||
netplay::receive_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"fla",
|
||||
&mut ui.saves.flash.0,
|
||||
device::sram::FLASHRAM_SIZE,
|
||||
);
|
||||
netplay::receive_save(
|
||||
netplay.as_mut().unwrap(),
|
||||
"mpk",
|
||||
&mut ui.saves.mempak.0,
|
||||
device::controller::mempak::MEMPAK_SIZE * 4,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_rom_save(ui: &ui::Ui) {
|
||||
fn write_rom_save(ui: &ui::Ui) {
|
||||
let f = std::fs::File::create(ui.paths.romsave_file_path.clone()).unwrap();
|
||||
serde_json::to_writer(f, &ui.saves.romsave.0).unwrap();
|
||||
}
|
||||
|
||||
pub fn write_saves(ui: &ui::Ui) {
|
||||
if ui.saves.eeprom.1 {
|
||||
write_save(ui, SaveTypes::Eeprom16k)
|
||||
}
|
||||
if ui.saves.sram.1 {
|
||||
write_save(ui, SaveTypes::Sram)
|
||||
}
|
||||
if ui.saves.flash.1 {
|
||||
write_save(ui, SaveTypes::Flash)
|
||||
}
|
||||
if ui.saves.mempak.1 {
|
||||
write_save(ui, SaveTypes::Mempak)
|
||||
}
|
||||
if ui.saves.romsave.1 {
|
||||
write_save(ui, SaveTypes::Romsave)
|
||||
pub fn write_saves(ui: &ui::Ui, netplay: &Option<netplay::Netplay>) {
|
||||
if netplay.is_none() || netplay.as_ref().unwrap().player_number == 0 {
|
||||
if ui.saves.eeprom.1 {
|
||||
write_save(ui, SaveTypes::Eeprom16k)
|
||||
}
|
||||
if ui.saves.sram.1 {
|
||||
write_save(ui, SaveTypes::Sram)
|
||||
}
|
||||
if ui.saves.flash.1 {
|
||||
write_save(ui, SaveTypes::Flash)
|
||||
}
|
||||
if ui.saves.mempak.1 {
|
||||
write_save(ui, SaveTypes::Mempak)
|
||||
}
|
||||
if ui.saves.romsave.1 && netplay.is_none() {
|
||||
write_save(ui, SaveTypes::Romsave)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_save(ui: &ui::Ui, save_type: SaveTypes) {
|
||||
fn write_save(ui: &ui::Ui, save_type: SaveTypes) {
|
||||
let path: &std::path::Path;
|
||||
let data: &Vec<u8>;
|
||||
match save_type {
|
||||
|
||||
+2
-7
@@ -38,8 +38,9 @@ pub fn init(device: &mut device::Device, fullscreen: bool) {
|
||||
rdp_init(
|
||||
device.ui.window.as_ref().unwrap().raw() as *mut std::ffi::c_void,
|
||||
gfx_info,
|
||||
fullscreen,
|
||||
device.ui.config.video.upscale,
|
||||
device.ui.config.video.integer_scaling,
|
||||
fullscreen,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -65,12 +66,6 @@ pub fn process_rdp_list() -> u64 {
|
||||
unsafe { rdp_process_commands() }
|
||||
}
|
||||
|
||||
pub fn full_sync() {
|
||||
unsafe {
|
||||
rdp_full_sync();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_text(
|
||||
text: &str,
|
||||
canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,
|
||||
|
||||
+5
-5
@@ -4,18 +4,18 @@ use crate::ui;
|
||||
|
||||
pub fn prompt_for_match(
|
||||
words: &[String],
|
||||
window_notifier: &std::sync::mpsc::Sender<Vec<String>>,
|
||||
word_index_notifier: &std::sync::mpsc::Receiver<String>,
|
||||
window_notifier: &tokio::sync::mpsc::Sender<Vec<String>>,
|
||||
word_index_receiver: &mut tokio::sync::mpsc::Receiver<String>,
|
||||
gui_ctx: &egui::Context,
|
||||
) -> u16 {
|
||||
let mut dedup_words = words.to_owned();
|
||||
dedup_words.sort();
|
||||
dedup_words.dedup();
|
||||
window_notifier.send(dedup_words).unwrap();
|
||||
window_notifier.try_send(dedup_words).unwrap();
|
||||
gui_ctx.request_repaint(); // this is so the window pops up right away
|
||||
let mut result = word_index_notifier.try_recv();
|
||||
let mut result = word_index_receiver.try_recv();
|
||||
while result.is_err() {
|
||||
result = word_index_notifier.try_recv();
|
||||
result = word_index_receiver.try_recv();
|
||||
std::thread::sleep(std::time::Duration::from_secs_f64(1.0 / 60.0));
|
||||
ui::video::update_screen(); // so the OS doesn't complain about the game window being frozen
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user