From 9338ccfa4e3b4cce3b93cc4a2536689731c60113 Mon Sep 17 00:00:00 2001 From: dbalsom Date: Sat, 6 Jul 2024 09:30:57 -0400 Subject: [PATCH] move to opaque pointer --- build.rs | 2 +- src/bindings.rs | 1025 ++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 39 +- src/nuked-opl3/opl3.c | 2 +- 4 files changed, 1038 insertions(+), 30 deletions(-) diff --git a/build.rs b/build.rs index 1de510d..64268ce 100644 --- a/build.rs +++ b/build.rs @@ -31,9 +31,9 @@ fn main() { println!("cargo:rerun-if-changed={}", lib_path); let bindings_result = bindgen::Builder::default() + .no_copy(".*") .header(header_path) .allowlist_function("OPL3.*") - .no_copy("OPL3.*") .parse_callbacks(Box::new(RenameCallbacks)) .generate(); diff --git a/src/bindings.rs b/src/bindings.rs index 7fc370f..6dd3f48 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1 +1,1024 @@ -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); \ No newline at end of file +/* automatically generated by rust-bindgen 0.69.4 */ + +use std::marker::PhantomData; + +#[repr(C)] +#[derive(Debug)] +pub struct Opl3Slot { + pub channel: *mut Opl3Channel, + pub chip: *mut Opl3Chip, + pub out: i16, + pub fbmod: i16, + pub mod_: *mut i16, + pub prout: i16, + pub eg_rout: u16, + pub eg_out: u16, + pub eg_inc: u8, + pub eg_gen: u8, + pub eg_rate: u8, + pub eg_ksl: u8, + pub trem: *mut u8, + pub reg_vib: u8, + pub reg_type: u8, + pub reg_ksr: u8, + pub reg_mult: u8, + pub reg_ksl: u8, + pub reg_tl: u8, + pub reg_ar: u8, + pub reg_dr: u8, + pub reg_sl: u8, + pub reg_rr: u8, + pub reg_wf: u8, + pub key: u8, + pub pg_reset: u32, + pub pg_phase: u32, + pub pg_phase_out: u16, + pub slot_num: u8, +} +#[test] +fn bindgen_test_layout_Opl3Slot() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 80usize, + concat!("Size of: ", stringify!(Opl3Slot)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(Opl3Slot)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).channel) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(channel) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chip) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(chip) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).out) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(out) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).fbmod) as usize - ptr as usize }, + 18usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(fbmod) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).mod_) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(mod_) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).prout) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(prout) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_rout) as usize - ptr as usize }, + 34usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(eg_rout) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_out) as usize - ptr as usize }, + 36usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(eg_out) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_inc) as usize - ptr as usize }, + 38usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(eg_inc) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_gen) as usize - ptr as usize }, + 39usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(eg_gen) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_rate) as usize - ptr as usize }, + 40usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(eg_rate) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_ksl) as usize - ptr as usize }, + 41usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(eg_ksl) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).trem) as usize - ptr as usize }, + 48usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(trem) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_vib) as usize - ptr as usize }, + 56usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_vib) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_type) as usize - ptr as usize }, + 57usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_type) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_ksr) as usize - ptr as usize }, + 58usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_ksr) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_mult) as usize - ptr as usize }, + 59usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_mult) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_ksl) as usize - ptr as usize }, + 60usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_ksl) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_tl) as usize - ptr as usize }, + 61usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_tl) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_ar) as usize - ptr as usize }, + 62usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_ar) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_dr) as usize - ptr as usize }, + 63usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_dr) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_sl) as usize - ptr as usize }, + 64usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_sl) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_rr) as usize - ptr as usize }, + 65usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_rr) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg_wf) as usize - ptr as usize }, + 66usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(reg_wf) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).key) as usize - ptr as usize }, + 67usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(key) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pg_reset) as usize - ptr as usize }, + 68usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(pg_reset) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pg_phase) as usize - ptr as usize }, + 72usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(pg_phase) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pg_phase_out) as usize - ptr as usize }, + 76usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(pg_phase_out) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).slot_num) as usize - ptr as usize }, + 78usize, + concat!( + "Offset of field: ", + stringify!(Opl3Slot), + "::", + stringify!(slot_num) + ) + ); +} +#[repr(C)] +#[derive(Debug)] +pub struct Opl3Channel { + pub slotz: [*mut Opl3Slot; 2usize], + pub pair: *mut Opl3Channel, + pub chip: *mut Opl3Chip, + pub out: [*mut i16; 4usize], + pub chtype: u8, + pub f_num: u16, + pub block: u8, + pub fb: u8, + pub con: u8, + pub alg: u8, + pub ksv: u8, + pub cha: u16, + pub chb: u16, + pub chc: u16, + pub chd: u16, + pub ch_num: u8, +} +#[test] +fn bindgen_test_layout_Opl3Channel() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 88usize, + concat!("Size of: ", stringify!(Opl3Channel)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(Opl3Channel)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).slotz) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(slotz) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pair) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(pair) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chip) as usize - ptr as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(chip) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).out) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(out) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chtype) as usize - ptr as usize }, + 64usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(chtype) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).f_num) as usize - ptr as usize }, + 66usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(f_num) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).block) as usize - ptr as usize }, + 68usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(block) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).fb) as usize - ptr as usize }, + 69usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(fb) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).con) as usize - ptr as usize }, + 70usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(con) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).alg) as usize - ptr as usize }, + 71usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(alg) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).ksv) as usize - ptr as usize }, + 72usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(ksv) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).cha) as usize - ptr as usize }, + 74usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(cha) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chb) as usize - ptr as usize }, + 76usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(chb) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chc) as usize - ptr as usize }, + 78usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(chc) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chd) as usize - ptr as usize }, + 80usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(chd) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).ch_num) as usize - ptr as usize }, + 82usize, + concat!( + "Offset of field: ", + stringify!(Opl3Channel), + "::", + stringify!(ch_num) + ) + ); +} +#[repr(C)] +#[derive(Debug)] +pub struct Opl3Writebuf { + pub time: u64, + pub reg: u16, + pub data: u8, +} +#[test] +fn bindgen_test_layout_Opl3Writebuf() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 16usize, + concat!("Size of: ", stringify!(Opl3Writebuf)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(Opl3Writebuf)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).time) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(Opl3Writebuf), + "::", + stringify!(time) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reg) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(Opl3Writebuf), + "::", + stringify!(reg) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).data) as usize - ptr as usize }, + 10usize, + concat!( + "Offset of field: ", + stringify!(Opl3Writebuf), + "::", + stringify!(data) + ) + ); +} +#[repr(C)] +#[derive(Debug)] +pub struct Opl3Chip { + pub channel: [Opl3Channel; 18usize], + pub slot: [Opl3Slot; 36usize], + pub timer: u16, + pub eg_timer: u64, + pub eg_timerrem: u8, + pub eg_state: u8, + pub eg_add: u8, + pub eg_timer_lo: u8, + pub newm: u8, + pub nts: u8, + pub rhy: u8, + pub vibpos: u8, + pub vibshift: u8, + pub tremolo: u8, + pub tremolopos: u8, + pub tremoloshift: u8, + pub noise: u32, + pub zeromod: i16, + pub mixbuff: [i32; 4usize], + pub rm_hh_bit2: u8, + pub rm_hh_bit3: u8, + pub rm_hh_bit7: u8, + pub rm_hh_bit8: u8, + pub rm_tc_bit3: u8, + pub rm_tc_bit5: u8, + pub rateratio: i32, + pub samplecnt: i32, + pub oldsamples: [i16; 4usize], + pub samples: [i16; 4usize], + pub writebuf_samplecnt: u64, + pub writebuf_cur: u32, + pub writebuf_last: u32, + pub writebuf_lasttime: u64, + pub writebuf: [Opl3Writebuf; 1024usize], + pub _marker: PhantomData, +} +#[test] +fn bindgen_test_layout_Opl3Chip() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 20960usize, + concat!("Size of: ", stringify!(Opl3Chip)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(Opl3Chip)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).channel) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(channel) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).slot) as usize - ptr as usize }, + 1584usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(slot) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).timer) as usize - ptr as usize }, + 4464usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(timer) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_timer) as usize - ptr as usize }, + 4472usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(eg_timer) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_timerrem) as usize - ptr as usize }, + 4480usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(eg_timerrem) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_state) as usize - ptr as usize }, + 4481usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(eg_state) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_add) as usize - ptr as usize }, + 4482usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(eg_add) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).eg_timer_lo) as usize - ptr as usize }, + 4483usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(eg_timer_lo) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).newm) as usize - ptr as usize }, + 4484usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(newm) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).nts) as usize - ptr as usize }, + 4485usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(nts) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rhy) as usize - ptr as usize }, + 4486usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rhy) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).vibpos) as usize - ptr as usize }, + 4487usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(vibpos) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).vibshift) as usize - ptr as usize }, + 4488usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(vibshift) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).tremolo) as usize - ptr as usize }, + 4489usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(tremolo) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).tremolopos) as usize - ptr as usize }, + 4490usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(tremolopos) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).tremoloshift) as usize - ptr as usize }, + 4491usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(tremoloshift) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).noise) as usize - ptr as usize }, + 4492usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(noise) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).zeromod) as usize - ptr as usize }, + 4496usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(zeromod) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).mixbuff) as usize - ptr as usize }, + 4500usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(mixbuff) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rm_hh_bit2) as usize - ptr as usize }, + 4516usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rm_hh_bit2) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rm_hh_bit3) as usize - ptr as usize }, + 4517usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rm_hh_bit3) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rm_hh_bit7) as usize - ptr as usize }, + 4518usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rm_hh_bit7) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rm_hh_bit8) as usize - ptr as usize }, + 4519usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rm_hh_bit8) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rm_tc_bit3) as usize - ptr as usize }, + 4520usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rm_tc_bit3) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rm_tc_bit5) as usize - ptr as usize }, + 4521usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rm_tc_bit5) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rateratio) as usize - ptr as usize }, + 4524usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(rateratio) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).samplecnt) as usize - ptr as usize }, + 4528usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(samplecnt) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).oldsamples) as usize - ptr as usize }, + 4532usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(oldsamples) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).samples) as usize - ptr as usize }, + 4540usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(samples) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).writebuf_samplecnt) as usize - ptr as usize }, + 4552usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(writebuf_samplecnt) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).writebuf_cur) as usize - ptr as usize }, + 4560usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(writebuf_cur) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).writebuf_last) as usize - ptr as usize }, + 4564usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(writebuf_last) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).writebuf_lasttime) as usize - ptr as usize }, + 4568usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(writebuf_lasttime) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).writebuf) as usize - ptr as usize }, + 4576usize, + concat!( + "Offset of field: ", + stringify!(Opl3Chip), + "::", + stringify!(writebuf) + ) + ); +} +extern "C" { + #[link_name = "\u{1}OPL3_Generate"] + pub fn Opl3Generate(chip: *mut Opl3Chip, buf: *mut i16); +} +extern "C" { + #[link_name = "\u{1}OPL3_GenerateResampled"] + pub fn Opl3GenerateResampled(chip: *mut Opl3Chip, buf: *mut i16); +} +extern "C" { + #[link_name = "\u{1}OPL3_Reset"] + pub fn Opl3Reset(chip: *mut Opl3Chip, samplerate: u32); +} +extern "C" { + #[link_name = "\u{1}OPL3_WriteReg"] + pub fn Opl3WriteReg(chip: *mut Opl3Chip, reg: u16, v: u8); +} +extern "C" { + #[link_name = "\u{1}OPL3_WriteRegBuffered"] + pub fn Opl3WriteRegBuffered(chip: *mut Opl3Chip, reg: u16, v: u8); +} +extern "C" { + #[link_name = "\u{1}OPL3_GenerateStream"] + pub fn Opl3GenerateStream(chip: *mut Opl3Chip, sndptr: *mut i16, numsamples: u32); +} +extern "C" { + #[link_name = "\u{1}OPL3_Generate4Ch"] + pub fn Opl3Generate4Ch(chip: *mut Opl3Chip, buf4: *mut i16); +} +extern "C" { + #[link_name = "\u{1}OPL3_Generate4ChResampled"] + pub fn Opl3Generate4ChResampled(chip: *mut Opl3Chip, buf4: *mut i16); +} +extern "C" { + #[link_name = "\u{1}OPL3_Generate4ChStream"] + pub fn Opl3Generate4ChStream( + chip: *mut Opl3Chip, + sndptr1: *mut i16, + sndptr2: *mut i16, + numsamples: u32, + ); +} diff --git a/src/lib.rs b/src/lib.rs index d4973fc..f7af59f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,9 +32,6 @@ * YMF262 and VRC VII decaps and die shots. */ -use std::pin::Pin; -use std::sync::{Arc, Mutex}; - use thiserror::Error; mod bindings; @@ -179,7 +176,7 @@ pub struct Opl3Device { registers: [[u8; 256]; 2], timers: [OplTimer; 2], stats: Opl3DeviceStats, - inner_chip: Arc>, + inner_chip: Opl3Chip, samples_fpart: f64, usec_accumulator: f64, } @@ -199,7 +196,7 @@ impl Opl3Device { OplTimer::new(OPL_TIMER_2_RATE), ], stats: Opl3DeviceStats::default(), - inner_chip: Arc::new(Mutex::new(Opl3Chip::new(sample_rate))), + inner_chip: Opl3Chip::new(sample_rate), samples_fpart: 0.0, usec_accumulator: 0.0, } @@ -394,12 +391,9 @@ impl Opl3Device { self.stats.data_writes = self.stats.data_writes.saturating_add(1); if buffered { - self.inner_chip - .lock() - .unwrap() - .write_register_buffered(reg16, value); + self.inner_chip.write_register_buffered(reg16, value); } else { - self.inner_chip.lock().unwrap().write_register(reg16, value); + self.inner_chip.write_register(reg16, value); } } @@ -417,11 +411,7 @@ impl Opl3Device { /// A Result containing either `()` on success or an `OplError` on failure. pub fn reset(&mut self, sample_rate: Option) -> Result<(), OplError> { let new_sample_rate = sample_rate.unwrap_or(self.sample_rate); - if let Ok(mut chip) = self.inner_chip.lock() { - chip.reset(new_sample_rate); - } else { - return Err(OplError::MutexLockFailed); - } + self.inner_chip.reset(new_sample_rate); for file in 0..2 { for reg in 0..256 { self.registers[file][reg] = 0; @@ -443,11 +433,7 @@ impl Opl3Device { /// /// A Result containing either `()` on success or an `OplError` on failure. pub fn generate(&mut self, sample: &mut [i16]) -> Result<(), OplError> { - if let Ok(mut chip) = self.inner_chip.lock() { - chip.generate(sample) - } else { - return Err(OplError::MutexLockFailed); - } + self.inner_chip.generate(sample) } /// Generate a stream of 2 channel, interleaved audio samples in i16 format. @@ -461,13 +447,13 @@ impl Opl3Device { /// /// A Result containing either `()` on success or an `OplError` on failure. pub fn generate_samples(&mut self, buffer: &mut [i16]) -> Result<(), OplError> { - self.inner_chip.lock().unwrap().generate_stream(buffer) + self.inner_chip.generate_stream(buffer) } } /// The `Opl3Chip` struct provides a safe interface for interacting with the Nuked-OPL3 library. pub struct Opl3Chip { - chip: Pin>, + chip: *mut bindings::Opl3Chip, } impl Opl3Chip { @@ -492,11 +478,10 @@ impl Opl3Chip { /// ``` pub fn new(sample_rate: u32) -> Self { unsafe { - let mut chip: bindings::Opl3Chip = std::mem::zeroed(); - bindings::Opl3Reset(&mut chip, sample_rate); - Opl3Chip { - chip: Box::pin(chip), - } + let layout = std::alloc::Layout::new::(); + let chip = std::alloc::alloc(layout) as *mut bindings::Opl3Chip; + bindings::Opl3Reset(chip, sample_rate); + Opl3Chip { chip } } } diff --git a/src/nuked-opl3/opl3.c b/src/nuked-opl3/opl3.c index 7fffd95..178aaa4 100644 --- a/src/nuked-opl3/opl3.c +++ b/src/nuked-opl3/opl3.c @@ -1527,4 +1527,4 @@ void OPL3_GenerateStream(opl3_chip *chip, int16_t *sndptr, uint32_t numsamples) OPL3_GenerateResampled(chip, sndptr); sndptr += 2; } -} \ No newline at end of file +}