2013-07-10 09:26:01 -04:00
|
|
|
/*******************************************************************************
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
sound.cpp
|
|
|
|
Implements a simple sound system.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
*******************************************************************************/
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2012-08-02 14:39:25 -04:00
|
|
|
#include <sortix/kernel/kthread.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
#include <sortix/kernel/syscall.h>
|
2013-05-12 18:23:24 -04:00
|
|
|
#include <sortix/kernel/cpu.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "sound.h"
|
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
namespace Sound
|
|
|
|
{
|
2012-08-02 14:39:25 -04:00
|
|
|
kthread_mutex_t soundlock;
|
|
|
|
|
2011-08-05 08:25:00 -04:00
|
|
|
void Mute()
|
|
|
|
{
|
2012-08-02 14:39:25 -04:00
|
|
|
ScopedLock lock(&soundlock);
|
2013-07-10 09:26:01 -04:00
|
|
|
uint8_t TMP = (CPU::InPortB(0x61)) & 0xFC;
|
2012-03-21 19:52:29 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
CPU::OutPortB(0x61, TMP);
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2012-03-21 19:52:29 -04:00
|
|
|
|
2012-09-21 13:25:22 -04:00
|
|
|
void Play(unsigned Frequency)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2012-08-02 14:39:25 -04:00
|
|
|
ScopedLock lock(&soundlock);
|
2011-08-05 08:25:00 -04:00
|
|
|
//Set the PIT to the desired frequency
|
|
|
|
uint32_t Div = 1193180 / Frequency;
|
|
|
|
CPU::OutPortB(0x43, 0xB6);
|
|
|
|
CPU::OutPortB(0x42, (uint8_t) (Div) );
|
|
|
|
CPU::OutPortB(0x42, (uint8_t) (Div >> 8));
|
|
|
|
|
|
|
|
// And play the sound using the PC speaker
|
|
|
|
uint8_t TMP = CPU::InPortB(0x61);
|
|
|
|
|
|
|
|
if ( TMP != (TMP | 3) )
|
|
|
|
{
|
|
|
|
CPU::OutPortB(0x61, TMP | 3);
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 18:25:28 -04:00
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
void SysSetFrequency(unsigned frequency)
|
2011-08-21 18:25:28 -04:00
|
|
|
{
|
|
|
|
if ( frequency == 0 ) { Mute(); } else { Play(frequency); }
|
2011-10-26 18:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
2012-08-02 14:39:25 -04:00
|
|
|
soundlock = KTHREAD_MUTEX_INITIALIZER;
|
2011-10-26 18:05:20 -04:00
|
|
|
Syscall::Register(SYSCALL_SET_FREQUENCY, (void*) SysSetFrequency);
|
2011-08-21 18:25:28 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|