Skip to content

Commit

Permalink
bios: Add gamepad support using IRQ handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrtonm committed Jan 7, 2024
1 parent 0005757 commit 7726a33
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions examples/bios/src/gamepad.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use crate::global::Global;
use crate::println;
use crate::thread;
use crate::thread::Thread;
use crate::thread::{ThreadControlBlock, Thread};
use crate::exceptions::{IRQCtxt, enqueue_handler};
use core::mem::size_of;
use core::ptr;
use core::ptr::NonNull;
use psx::hw::pad::{BaudFactor, CharLength, RxIntMode};
use psx::hw::{cop0, pad, Register};
use psx::hw::irq::IRQ;

#[repr(C)]
struct GamepadBuffer {
Expand Down Expand Up @@ -40,14 +46,58 @@ pub fn init_pad(buf1: &mut [u16], buf2: &mut [u16]) -> u32 {
buffer1: NonNull::new(buf1).unwrap().cast(),
buffer2: NonNull::new(buf2).unwrap().cast(),
};
let mut t = Thread::create_with_arg(gamepad_thread, ctxt).unwrap();
t.unpark();

pad::Baud::skip_load().set_bits(0x88).store();
pad::Control::skip_load()
.enable_tx()
.enable_output()
.enable_rx_interrupt()
.enable_ack_interrupt()
.set_rx_interrupt_mode(RxIntMode::Bits1)
.store();
pad::Mode::skip_load()
.set_baud_factor(BaudFactor::Mul1)
.set_char_length(CharLength::Bits8)
.store();
cop0::Status::new().critical_section(|cs| {
enqueue_handler(poll_gamepad, cs);
});
pad::TxData::send(0x01);
0
}

extern "C" fn gamepad_thread(_ctxt: GamepadCtxt) {
println!("Started gamepad thread");
loop {
thread::resume_main();
fn poll_gamepad(ctxt: IRQCtxt) -> *mut ThreadControlBlock {
const SEQ: [u8; 5] = [1, 0x42, 0, 0, 0];
static N: Global<usize> = Global::new(1);
let n = N.borrow(ctxt.cs);
if ctxt.mask.get_requested(ctxt.stat).contains(IRQ::ControllerMemoryCard) && *n < SEQ.len() {
let mut ctrl = pad::Control::new();
ctrl.disable_output().store();
ctrl.enable_output().store();

println!("Received {:x?} and sending {:x?}", pad::RxData::recv(), SEQ[*n]);
pad::TxData::send(SEQ[*n]);
*n += 1;

ctxt.stat.ack(IRQ::ControllerMemoryCard).store();
pad::Control::new().ack().store();
} else if ctxt.mask.get_requested(ctxt.stat).contains(IRQ::Vblank) && *n == SEQ.len() {
*n = 0;
pad::TxData::send(0x01);
}

// *n += 1;
// if *n < SEQ.len() {
// //*n = 0;
// let mut ctrl = pad::Control::new();
// ctrl.disable_output().store();
// ctrl.enable_output().store();
// //}
// println!("Received {:x?} and sending {:x?}", pad::RxData::recv(), SEQ[*n]);
// pad::TxData::send(SEQ[*n]);

// ctxt.stat.ack(IRQ::ControllerMemoryCard).store();
// pad::Control::new().ack().store();
// }
ptr::null_mut()
}

0 comments on commit 7726a33

Please sign in to comment.