Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beep command #234

Merged
merged 5 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ image: $(img)
cargo bootimage --no-default-features --features $(output),$(nic) --release
dd conv=notrunc if=$(bin) of=$(img)

opts = -m 32 -cpu max -nic model=$(nic) -hda $(img)
opts = -m 32 -cpu max -nic model=$(nic) -hda $(img) -soundhw pcspk
ifeq ($(output),serial)
opts += -display none -serial stdio
endif
Expand Down
28 changes: 28 additions & 0 deletions dsk/tmp/beep/mario.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Source: http://blog.dhampir.no/content/fun-with-beep
beep -f 130 -l 100
beep -f 262 -l 100
beep -f 330 -l 100
beep -f 392 -l 100
beep -f 523 -l 100
beep -f 660 -l 100
beep -f 784 -l 300
beep -f 660 -l 300
beep -f 146 -l 100
beep -f 262 -l 100
beep -f 311 -l 100
beep -f 415 -l 100
beep -f 523 -l 100
beep -f 622 -l 100
beep -f 831 -l 300
beep -f 622 -l 300
beep -f 155 -l 100
beep -f 294 -l 100
beep -f 349 -l 100
beep -f 466 -l 100
beep -f 588 -l 100
beep -f 699 -l 100
beep -f 933 -l 300
beep -f 933 -l 100
beep -f 933 -l 100
beep -f 933 -l 100
beep -f 1047 -l 400
132 changes: 132 additions & 0 deletions dsk/tmp/beep/starwars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Source: http://kirrus.co.uk/2010/09/linux-beep-music
beep -l 350 -f 392
sleep 0.100
beep -l 350 -f 392
sleep 0.100
beep -l 350 -f 392
sleep 0.100
beep -l 250 -f 311.1
sleep 0.100
beep -l 25 -f 466.2
sleep 0.100
beep -l 350 -f 392
sleep 0.100
beep -l 250 -f 311.1
sleep 0.100
beep -l 25 -f 466.2
sleep 0.100
beep -l 700 -f 392
sleep 0.100
beep -l 350 -f 587.32
sleep 0.100
beep -l 350 -f 587.32
sleep 0.100
beep -l 350 -f 587.32
sleep 0.100
beep -l 250 -f 622.26
sleep 0.100
beep -l 25 -f 466.2
sleep 0.100
beep -l 350 -f 369.99
sleep 0.100
beep -l 250 -f 311.1
sleep 0.100
beep -l 25 -f 466.2
sleep 0.100
beep -l 700 -f 392
sleep 0.100
beep -l 350 -f 784
sleep 0.100
beep -l 250 -f 392
sleep 0.100
beep -l 25 -f 392
sleep 0.100
beep -l 350 -f 784
sleep 0.100
beep -l 250 -f 739.98
sleep 0.100
beep -l 25 -f 698.46
sleep 0.100
beep -l 25 -f 659.26
sleep 0.100
beep -l 25 -f 622.26
sleep 0.100
beep -l 50 -f 659.26
sleep 0.400
beep -l 25 -f 415.3
sleep 0.200
beep -l 350 -f 554.36
sleep 0.100
beep -l 250 -f 523.25
sleep 0.100
beep -l 25 -f 493.88
sleep 0.100
beep -l 25 -f 466.16
sleep 0.100
beep -l 25 -f 440
sleep 0.100
beep -l 50 -f 466.16
sleep 0.400
beep -l 25 -f 311.13
sleep 0.200
beep -l 350 -f 369.99
sleep 0.100
beep -l 250 -f 311.13
sleep 0.100
beep -l 25 -f 392
sleep 0.100
beep -l 350 -f 466.16
sleep 0.100
beep -l 250 -f 392
sleep 0.100
beep -l 25 -f 466.16
sleep 0.100
beep -l 700 -f 587.32
sleep 0.100
beep -l 350 -f 784
sleep 0.100
beep -l 250 -f 392
sleep 0.100
beep -l 25 -f 392
sleep 0.100
beep -l 350 -f 784
sleep 0.100
beep -l 250 -f 739.98
sleep 0.100
beep -l 25 -f 698.46
sleep 0.100
beep -l 25 -f 659.26
sleep 0.100
beep -l 25 -f 622.26
sleep 0.100
beep -l 50 -f 659.26
sleep 0.400
beep -l 25 -f 415.3
sleep 0.200
beep -l 350 -f 554.36
sleep 0.100
beep -l 250 -f 523.25
sleep 0.100
beep -l 25 -f 493.88
sleep 0.100
beep -l 25 -f 466.16
sleep 0.100
beep -l 25 -f 440
sleep 0.100
beep -l 50 -f 466.16
sleep 0.400
beep -l 25 -f 311.13
sleep 0.200
beep -l 350 -f 392
sleep 0.100
beep -l 250 -f 311.13
sleep 0.100
beep -l 25 -f 466.16
sleep 0.100
beep -l 300 -f 392.00
sleep 0.150
beep -l 250 -f 311.13
sleep 0.100
beep -l 25 -f 466.16
sleep 0.100
beep -l 700 -f 392
91 changes: 91 additions & 0 deletions dsk/tmp/beep/tetris.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Source: http://kirrus.co.uk/2010/09/linux-beep-music
beep -f 330 -l 150
beep -f 1 -l 40
beep -f 494 -l 159
beep -f 1 -l 40
beep -f 660 -l 150
beep -f 1 -l 40
beep -f 590 -l 150
beep -f 660 -l 150
beep -f 494 -l 100
beep -f 494 -l 100
beep -f 523 -l 150
beep -f 1 -l 40
beep -f 440 -l 150
beep -f 1 -l 40
beep -f 494 -l 150
beep -f 1 -l 40
beep -f 392 -l 100
beep -f 392 -l 100
beep -f 440 -l 150
beep -f 370 -l 150
beep -f 1 -l 40
beep -f 392 -l 150
beep -f 1 -l 40
beep -f 330 -l 100
beep -f 330 -l 100
beep -f 370 -l 150
beep -f 1 -l 40
beep -f 294 -l 150
beep -f 1 -l 40
beep -f 330 -l 150
beep -f 247 -l 100
beep -f 247 -l 100
beep -f 261 -l 150
beep -f 1 -l 40
beep -f 311 -l 150
beep -f 1 -l 40
beep -f 330 -l 150
beep -f 1 -l 40
beep -f 247 -l 100
beep -f 247 -l 100
beep -f 262 -l 150
beep -f 1 -l 40
beep -f 370 -l 150
beep -f 1 -l 40
beep -f 330 -l 150
beep -f 1 -l 40
beep -f 494 -l 159
beep -f 1 -l 40
beep -f 660 -l 150
beep -f 1 -l 40
beep -f 590 -l 150
beep -f 660 -l 150
beep -f 494 -l 100
beep -f 494 -l 100
beep -f 523 -l 150
beep -f 1 -l 40
beep -f 440 -l 150
beep -f 1 -l 40
beep -f 494 -l 150
beep -f 1 -l 40
beep -f 392 -l 100
beep -f 392 -l 100
beep -f 440 -l 150
beep -f 370 -l 150
beep -f 1 -l 40
beep -f 392 -l 150
beep -f 1 -l 40
beep -f 330 -l 100
beep -f 330 -l 100
beep -f 370 -l 150
beep -f 1 -l 40
beep -f 294 -l 150
beep -f 1 -l 40
beep -f 330 -l 150
beep -f 247 -l 100
beep -f 247 -l 100
beep -f 261 -l 150
beep -f 1 -l 40
beep -f 311 -l 150
beep -f 1 -l 40
beep -f 330 -l 150
beep -f 1 -l 40
beep -f 247 -l 100
beep -f 247 -l 100
beep -f 262 -l 150
beep -f 1 -l 40
beep -f 370 -l 150
beep -f 1 -l 40
beep -f 330 -l 150
beep -f 1 -l 40
2 changes: 1 addition & 1 deletion run/bochs.rc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
megs: 16
megs: 32
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=disk, path="../disk.img", mode=flat, cylinders=0, heads=0, spt=0
boot: disk
13 changes: 8 additions & 5 deletions src/sys/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use x86_64::instructions::port::Port;
// which will result in about 54.926 ms between ticks.
// During init we will change the divider to 1193 to have about 1.000 ms
// between ticks to improve time measurements accuracy.
const PIT_FREQUENCY: f64 = 3_579_545.0 / 3.0; // 1_193_181.666 Hz
pub const PIT_FREQUENCY: f64 = 3_579_545.0 / 3.0; // 1_193_181.666 Hz
const PIT_DIVIDER: usize = 1193;
const PIT_INTERVAL: f64 = (PIT_DIVIDER as f64) / PIT_FREQUENCY;

Expand Down Expand Up @@ -56,13 +56,15 @@ pub fn nanowait(nanoseconds: u64) {
}

/// The frequency divider must be between 0 and 65535, with 0 acting as 65536
fn set_pit_frequency_divider(divider: u16) {
pub fn set_pit_frequency_divider(divider: u16, channel: u8) {
interrupts::without_interrupts(|| {
let bytes = divider.to_le_bytes();
let mut cmd: Port<u8> = Port::new(0x43);
let mut data: Port<u8> = Port::new(0x40);
let mut data: Port<u8> = Port::new(0x40 + channel as u16);
let operating_mode = 6; // Square wave generator
let access_mode = 3; // Lobyte + Hibyte
unsafe {
cmd.write(0x36);
cmd.write((channel << 6) | (access_mode << 4) | operating_mode);
data.write(bytes[0]);
data.write(bytes[1]);
}
Expand All @@ -81,7 +83,8 @@ pub fn rtc_interrupt_handler() {
pub fn init() {
// PIT timmer
let divider = if PIT_DIVIDER < 65536 { PIT_DIVIDER } else { 0 };
set_pit_frequency_divider(divider as u16);
let channel = 0;
set_pit_frequency_divider(divider as u16, channel);
sys::idt::set_irq_handler(0, pit_interrupt_handler);

// RTC timmer
Expand Down
75 changes: 75 additions & 0 deletions src/usr/beep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::{api, sys, usr};

use x86_64::instructions::port::Port;

// See: https://wiki.osdev.org/PC_Speaker

const SPEAKER_PORT: u16 = 0x61;

fn start_sound(freq: f64) {
let divider = (sys::time::PIT_FREQUENCY / freq) as u16;
let channel = 2;
sys::time::set_pit_frequency_divider(divider, channel);

let mut speaker: Port<u8> = Port::new(SPEAKER_PORT);
let tmp = unsafe { speaker.read() };
if tmp != (tmp | 3) {
unsafe { speaker.write(tmp | 3) };
}
}

fn stop_sound() {
let mut speaker: Port<u8> = Port::new(SPEAKER_PORT);
let tmp = unsafe { speaker.read() } & 0xFC;
unsafe { speaker.write(tmp) };
}

fn beep(freq: f64, len: f64) {
start_sound(freq);
api::syscall::sleep(len);
stop_sound();
}

pub fn main(args: &[&str]) -> usr::shell::ExitCode {
let mut freq = 440.0;
let mut len = 200.0;
let mut i = 1;
let n = args.len();
while i < n {
match args[i] {
"--freq" | "-f" => {
if i + 1 < n {
if let Ok(value) = args[i + 1].parse() {
freq = value;
} else {
println!("Could not parse freq");
return usr::shell::ExitCode::CommandError;
}
i += 1;
} else {
println!("Missing freq");
return usr::shell::ExitCode::CommandError;
}
},
"--len" | "-l" => {
if i + 1 < n {
if let Ok(value) = args[i + 1].parse() {
len = value;
} else {
println!("Could not parse len");
return usr::shell::ExitCode::CommandError;
}
i += 1;
} else {
println!("Missing len");
return usr::shell::ExitCode::CommandError;
}
},
_ => {},
}
i += 1;
}

beep(freq, len / 1000.0);
usr::shell::ExitCode::CommandSuccessful
}
Loading