Cleanup unsafe functions (#134)

* cleanup unsafe blocks

* comment out flush mode stuff

* remove unsafe from input
This commit is contained in:
Logan McNaughton 2025-01-13 10:16:27 +01:00 committed by GitHub
parent 97e4921b6a
commit f75636f7e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 125 additions and 132 deletions

View File

@ -1,32 +1,24 @@
unsafe extern "C" {
#[link_name = "_mm_srli_epi16__extern"]
pub fn _mm_srli_epi16(__a: __m128i, __imm8: ::std::os::raw::c_int) -> __m128i;
}
unsafe extern "C" {
#[link_name = "_mm_insert_epi8__extern"]
pub fn _mm_insert_epi8(
__a: __m128i,
__i: ::std::os::raw::c_int,
__imm8: ::std::os::raw::c_int,
) -> __m128i;
}
unsafe extern "C" {
#[link_name = "_mm_extract_epi8__extern"]
pub fn _mm_extract_epi8(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
}
unsafe extern "C" {
#[link_name = "_mm_insert_epi16__extern"]
pub fn _mm_insert_epi16(
__a: __m128i,
__i: ::std::os::raw::c_int,
__imm8: ::std::os::raw::c_int,
) -> __m128i;
}
unsafe extern "C" {
#[link_name = "_mm_extract_epi16__extern"]
pub fn _mm_extract_epi16(__a: __m128i, __imm8: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
}

View File

@ -240,8 +240,8 @@ impl Device {
cop1: cop1::Cop1 {
fcr0: 0,
fcr31: 0,
#[cfg(target_arch = "x86_64")]
flush_mode: 0,
//#[cfg(target_arch = "x86_64")]
//flush_mode: 0,
fgr32: [[0; 4]; 32],
fgr64: [[0; 8]; 32],
instrs: [cop1::reserved; 32],

View File

@ -17,8 +17,8 @@ use crate::device;
//const FCR31_CAUSE_INVALID_BIT: u32 = 1 << 16;
const FCR31_CAUSE_UNIMP_BIT: u32 = 1 << 17;
pub const FCR31_CMP_BIT: u32 = 1 << 23;
#[cfg(target_arch = "x86_64")]
const FCR31_FS_BIT: u32 = 1 << 24;
//#[cfg(target_arch = "x86_64")]
//const FCR31_FS_BIT: u32 = 1 << 24;
const FCR31_CAUSE_MASK: u32 = 0b00000000000000111111000000000000;
const FCR31_ENABLE_MASK: u32 = 0b00000000000000000000111110000000;
@ -27,8 +27,8 @@ const FCR31_WRITE_MASK: u32 = 0b00000001100000111111111111111111;
pub struct Cop1 {
pub fcr0: u32,
pub fcr31: u32,
#[cfg(target_arch = "x86_64")]
pub flush_mode: u32,
//#[cfg(target_arch = "x86_64")]
//pub flush_mode: u32,
pub fgr32: [[u8; 4]; 32],
pub fgr64: [[u8; 8]; 32],
pub instrs: [fn(&mut device::Device, u32); 32],
@ -368,27 +368,28 @@ pub fn set_control_registers_fpu(device: &mut device::Device, index: u32, data:
{
device::exceptions::floating_point_exception(device)
}
/*
#[cfg(target_arch = "x86_64")]
{
unsafe {
let flush_mode;
if (device.cpu.cop1.fcr31 & 2) != 0 {
if (device.cpu.cop1.fcr31 & FCR31_FS_BIT) != 0 {
flush_mode = std::arch::x86_64::_MM_FLUSH_ZERO_OFF
} else {
flush_mode = std::arch::x86_64::_MM_FLUSH_ZERO_ON;
}
let flush_mode;
if (device.cpu.cop1.fcr31 & 2) != 0 {
if (device.cpu.cop1.fcr31 & FCR31_FS_BIT) != 0 {
flush_mode = std::arch::x86_64::_MM_FLUSH_ZERO_OFF
} else {
flush_mode = std::arch::x86_64::_MM_FLUSH_ZERO_ON;
}
if flush_mode != device.cpu.cop1.flush_mode {
#[allow(deprecated)]
std::arch::x86_64::_MM_SET_FLUSH_ZERO_MODE(flush_mode);
device.cpu.cop1.flush_mode = flush_mode;
}
} else {
flush_mode = std::arch::x86_64::_MM_FLUSH_ZERO_ON;
}
if flush_mode != device.cpu.cop1.flush_mode {
#[allow(deprecated)]
unsafe {
std::arch::x86_64::_MM_SET_FLUSH_ZERO_MODE(flush_mode)
};
device.cpu.cop1.flush_mode = flush_mode;
}
}
*/
}
_ => {
panic!("unknown FCR register")

View File

@ -6,7 +6,7 @@ pub struct InputProfile {
pub controller_buttons: [(bool, i32); 14],
pub controller_axis: [(bool, i32, i16); 18],
pub joystick_buttons: [(bool, u32); 14],
pub joystick_hat: [(bool, u32, i8); 14],
pub joystick_hat: [(bool, u32, u8); 14],
pub joystick_axis: [(bool, u32, i16); 18],
}

View File

@ -78,94 +78,110 @@ pub fn set_axis_from_joystick(
(x, y)
}
fn get_axis_from_i32(axis: i32) -> sdl2::controller::Axis {
match axis {
0 => sdl2::controller::Axis::LeftX,
1 => sdl2::controller::Axis::LeftY,
2 => sdl2::controller::Axis::RightX,
3 => sdl2::controller::Axis::RightY,
4 => sdl2::controller::Axis::TriggerLeft,
5 => sdl2::controller::Axis::TriggerRight,
_ => panic!("Invalid axis"),
}
}
fn get_button_from_i32(button: i32) -> sdl2::controller::Button {
match button {
0 => sdl2::controller::Button::A,
1 => sdl2::controller::Button::B,
2 => sdl2::controller::Button::X,
3 => sdl2::controller::Button::Y,
4 => sdl2::controller::Button::Back,
5 => sdl2::controller::Button::Guide,
6 => sdl2::controller::Button::Start,
7 => sdl2::controller::Button::LeftStick,
8 => sdl2::controller::Button::RightStick,
9 => sdl2::controller::Button::LeftShoulder,
10 => sdl2::controller::Button::RightShoulder,
11 => sdl2::controller::Button::DPadUp,
12 => sdl2::controller::Button::DPadDown,
13 => sdl2::controller::Button::DPadLeft,
14 => sdl2::controller::Button::DPadRight,
_ => panic!("Invalid button"),
}
}
pub fn set_axis_from_controller(
profile: &ui::config::InputProfile,
controller: &sdl2::controller::GameController,
) -> (f64, f64) {
unsafe {
let mut x = 0.0;
let mut y = 0.0;
if profile.controller_axis[AXIS_LEFT].0 {
let axis_position =
controller.axis(std::mem::transmute::<i32, sdl2::controller::Axis>(
profile.controller_axis[AXIS_LEFT].1,
));
if axis_position as isize * profile.controller_axis[AXIS_LEFT].2 as isize > 0 {
x = axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64;
}
let mut x = 0.0;
let mut y = 0.0;
if profile.controller_axis[AXIS_LEFT].0 {
let axis_position =
controller.axis(get_axis_from_i32(profile.controller_axis[AXIS_LEFT].1));
if axis_position as isize * profile.controller_axis[AXIS_LEFT].2 as isize > 0 {
x = axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64;
}
if profile.controller_axis[AXIS_RIGHT].0 {
let axis_position =
controller.axis(std::mem::transmute::<i32, sdl2::controller::Axis>(
profile.controller_axis[AXIS_RIGHT].1,
));
if axis_position as isize * profile.controller_axis[AXIS_RIGHT].2 as isize > 0 {
x = axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64;
}
}
if profile.controller_axis[AXIS_DOWN].0 {
let axis_position =
controller.axis(std::mem::transmute::<i32, sdl2::controller::Axis>(
profile.controller_axis[AXIS_DOWN].1,
));
if axis_position as isize * profile.controller_axis[AXIS_DOWN].2 as isize > 0 {
y = (axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64).neg();
}
}
if profile.controller_axis[AXIS_UP].0 {
let axis_position =
controller.axis(std::mem::transmute::<i32, sdl2::controller::Axis>(
profile.controller_axis[AXIS_UP].1,
));
if axis_position as isize * profile.controller_axis[AXIS_UP].2 as isize > 0 {
y = (axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64).neg();
}
}
(x, y)
}
if profile.controller_axis[AXIS_RIGHT].0 {
let axis_position =
controller.axis(get_axis_from_i32(profile.controller_axis[AXIS_RIGHT].1));
if axis_position as isize * profile.controller_axis[AXIS_RIGHT].2 as isize > 0 {
x = axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64;
}
}
if profile.controller_axis[AXIS_DOWN].0 {
let axis_position =
controller.axis(get_axis_from_i32(profile.controller_axis[AXIS_DOWN].1));
if axis_position as isize * profile.controller_axis[AXIS_DOWN].2 as isize > 0 {
y = (axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64).neg();
}
}
if profile.controller_axis[AXIS_UP].0 {
let axis_position = controller.axis(get_axis_from_i32(profile.controller_axis[AXIS_UP].1));
if axis_position as isize * profile.controller_axis[AXIS_UP].2 as isize > 0 {
y = (axis_position as f64 * MAX_AXIS_VALUE / i16::MAX as f64).neg();
}
}
(x, y)
}
pub fn set_axis_from_keys(
profile: &ui::config::InputProfile,
keyboard_state: sdl2::keyboard::KeyboardState,
) -> (f64, f64) {
unsafe {
let mut x = 0.0;
let mut y = 0.0;
if profile.keys[AXIS_LEFT].0
&& keyboard_state
.is_scancode_pressed(std::mem::transmute::<i32, sdl2::keyboard::Scancode>(
profile.keys[AXIS_LEFT].1,
))
{
x = -MAX_AXIS_VALUE
}
if profile.keys[AXIS_RIGHT].0
&& keyboard_state
.is_scancode_pressed(std::mem::transmute::<i32, sdl2::keyboard::Scancode>(
profile.keys[AXIS_RIGHT].1,
))
{
x = MAX_AXIS_VALUE
}
if profile.keys[AXIS_DOWN].0
&& keyboard_state
.is_scancode_pressed(std::mem::transmute::<i32, sdl2::keyboard::Scancode>(
profile.keys[AXIS_DOWN].1,
))
{
y = -MAX_AXIS_VALUE
}
if profile.keys[AXIS_UP].0
&& keyboard_state
.is_scancode_pressed(std::mem::transmute::<i32, sdl2::keyboard::Scancode>(
profile.keys[AXIS_UP].1,
))
{
y = MAX_AXIS_VALUE
}
(x, y)
let mut x = 0.0;
let mut y = 0.0;
if profile.keys[AXIS_LEFT].0
&& keyboard_state.is_scancode_pressed(
sdl2::keyboard::Scancode::from_i32(profile.keys[AXIS_LEFT].1).unwrap(),
)
{
x = -MAX_AXIS_VALUE
}
if profile.keys[AXIS_RIGHT].0
&& keyboard_state.is_scancode_pressed(
sdl2::keyboard::Scancode::from_i32(profile.keys[AXIS_RIGHT].1).unwrap(),
)
{
x = MAX_AXIS_VALUE
}
if profile.keys[AXIS_DOWN].0
&& keyboard_state.is_scancode_pressed(
sdl2::keyboard::Scancode::from_i32(profile.keys[AXIS_DOWN].1).unwrap(),
)
{
y = -MAX_AXIS_VALUE
}
if profile.keys[AXIS_UP].0
&& keyboard_state.is_scancode_pressed(
sdl2::keyboard::Scancode::from_i32(profile.keys[AXIS_UP].1).unwrap(),
)
{
y = MAX_AXIS_VALUE
}
(x, y)
}
pub fn set_buttons_from_joystick(
@ -182,9 +198,7 @@ pub fn set_buttons_from_joystick(
let profile_joystick_hat = profile.joystick_hat[i];
if profile_joystick_hat.0
&& joystick.hat(profile_joystick_hat.1).unwrap()
== unsafe {
std::mem::transmute::<i8, sdl2::joystick::HatState>(profile_joystick_hat.2)
}
== sdl2::joystick::HatState::from_raw(profile_joystick_hat.2)
{
*keys |= 1 << i;
}
@ -208,21 +222,12 @@ pub fn set_buttons_from_controller(
) {
let profile_controller_button = profile.controller_buttons[i];
if profile_controller_button.0 {
unsafe {
*keys |= (controller.button(std::mem::transmute::<i32, sdl2::controller::Button>(
profile_controller_button.1,
)) as u32)
<< i;
}
*keys |= (controller.button(get_button_from_i32(profile_controller_button.1)) as u32) << i;
}
let profile_controller_axis = profile.controller_axis[i];
if profile_controller_axis.0 {
let axis_position = unsafe {
controller.axis(std::mem::transmute::<i32, sdl2::controller::Axis>(
profile_controller_axis.1,
))
};
let axis_position = controller.axis(get_axis_from_i32(profile_controller_axis.1));
if axis_position as isize * profile_controller_axis.2 as isize > 0
&& axis_position.saturating_abs() > i16::MAX / 2
{
@ -245,13 +250,10 @@ pub fn get(ui: &mut ui::Ui, channel: usize) -> u32 {
if profile_name != "default" || channel == 0 {
let profile_key = profile.keys[i];
if profile_key.0 {
unsafe {
keys |= (keyboard_state
.is_scancode_pressed(std::mem::transmute::<i32, sdl2::keyboard::Scancode>(
profile_key.1,
)) as u32)
<< i;
}
keys |= (keyboard_state
.is_scancode_pressed(sdl2::keyboard::Scancode::from_i32(profile_key.1).unwrap())
as u32)
<< i;
}
}
@ -406,13 +408,11 @@ pub fn configure_input_profile(ui: &mut ui::Ui, profile: String) {
key_set = true
}
sdl2::event::Event::JoyHatMotion { hat_idx, state, .. } => {
new_joystick_hat[*value] = unsafe {
(
true,
hat_idx as u32,
std::mem::transmute::<sdl2::joystick::HatState, i8>(state),
)
};
new_joystick_hat[*value] = (
true,
hat_idx as u32,
sdl2::joystick::HatState::to_raw(state),
);
key_set = true
}
sdl2::event::Event::JoyAxisMotion {