From 71160c71166cd6415b4bb3cb5cb991a2e8565f08 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 15 Apr 2021 23:34:12 +0100 Subject: [PATCH] Really simple beep --- examples/beep.rs | 18 ++++++++++++++++++ src/lib.rs | 3 +++ src/sound/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 examples/beep.rs create mode 100644 src/sound/mod.rs diff --git a/examples/beep.rs b/examples/beep.rs new file mode 100644 index 00000000..da35dfcc --- /dev/null +++ b/examples/beep.rs @@ -0,0 +1,18 @@ +#![no_std] +#![feature(start)] + +extern crate gba; + +use gba::display; + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + let mut gba = gba::Gba::new(); + let mut bitmap = gba.display.video.bitmap3(); + let vblank = gba.display.vblank.get(); + + gba.sound.enable(); + gba.sound.channel1().play_sound(); + + loop {} +} diff --git a/src/lib.rs b/src/lib.rs index 92e1934d..8a91ce5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub mod display; pub mod input; +pub mod sound; mod interrupt; mod memory_mapped; @@ -37,6 +38,7 @@ static mut GBASINGLE: single::Singleton = single::Singleton::new(unsafe { G pub struct Gba { pub display: display::Display, + pub sound: sound::Sound, } impl Gba { @@ -47,6 +49,7 @@ impl Gba { const unsafe fn single_new() -> Self { Self { display: display::Display::new(), + sound: sound::Sound::new(), } } } diff --git a/src/sound/mod.rs b/src/sound/mod.rs new file mode 100644 index 00000000..9fd4bb66 --- /dev/null +++ b/src/sound/mod.rs @@ -0,0 +1,40 @@ +use crate::memory_mapped::MemoryMapped; + +const CHANNEL_1_SWEEP: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0060) }; +const CHANNEL_1_LENGTH_DUTY_ENVELOPE: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0062) }; +const CHANNEL_1_FREQUENCY_CONTROL: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0064) }; + +const MASTER_SOUND_VOLUME_ENABLE: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0080) }; +const MASTER_SOUND_VOLUME_MIXING: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0082) }; +const MASTER_SOUND_STATUS: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0084) }; + +#[non_exhaustive] +pub struct Sound {} + +impl Sound { + pub(crate) const unsafe fn new() -> Self { + Sound {} + } + + pub fn channel1(&self) -> Channel1 { + Channel1 {} + } + + pub fn enable(&self) { + MASTER_SOUND_STATUS.set_bits(1, 1, 7); + + MASTER_SOUND_VOLUME_ENABLE.set(0b1111_1111_0_111_0_111); + MASTER_SOUND_VOLUME_MIXING.set(0b10); + } +} + +#[non_exhaustive] +pub struct Channel1 {} + +impl Channel1 { + pub fn play_sound(&self) { + CHANNEL_1_SWEEP.set(0b00000000_111_0_010); + CHANNEL_1_LENGTH_DUTY_ENVELOPE.set(0b111_1_001_01_111111); + CHANNEL_1_FREQUENCY_CONTROL.set(0b1_0_000_01000000000); + } +}