dsound.c
Go to the documentation of this file.
1 
6 /*
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.0 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS"
13  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
14  * License for the specific language governing rights and limitations
15  * under the License.
16  *
17  * The Original Code is legOS code, released October 17, 1999.
18  *
19  * The Initial Developer of the Original Code is Markus L. Noga.
20  * Portions created by Markus L. Noga are Copyright (C) 1999
21  * Markus L. Noga. All Rights Reserved.
22  *
23  * Contributor(s): Markus L. Noga <markus@noga.de>
24  * Michael Nielsen <mic@daimi.aau.dk>
25  * Kyosuke Ishikawa <kyosuke@da2.so-net.ne.jp>
26  * Martin Cornelius <Martin.Cornelius@t-online.de>
27  */
28 
29 #include <sys/dsound.h>
30 
31 #ifdef CONF_DSOUND
32 
33 #include <sys/bitops.h>
34 #include <sys/h8.h>
35 #include <sys/irq.h>
36 
37 #include <conio.h>
38 #include <sys/tm.h>
39 
41 //
42 // Variables
43 //
45 
47 static const unsigned pitch2freq[]={
48  0x8d03, 0x8603, 0x7d03, 0x7703, 0x7003, 0x6a03, 0x6303, 0x5e03,
49  0x5903, 0x5403, 0x4f03, 0x4a03, 0x4603, 0x4203, 0xfd83, 0xee83,
50  0xe083, 0xd483, 0xc783, 0xbc83, 0xb283, 0xa883, 0x9e83, 0x9583,
51  0x8d83, 0x8583, 0x7e83, 0x7683, 0x7083, 0x6983, 0x6383, 0x5e83,
52  0x5983, 0x5383, 0x4f83, 0x4a83, 0x4683, 0x4283, 0xfc02, 0xee02,
53  0xe102, 0xd402, 0xc802, 0xbd02, 0xb202, 0xa802, 0x9e02, 0x9502,
54  0x8d02, 0x8502, 0xfc82, 0xee82, 0xe082, 0xd482, 0xc882, 0xbd82,
55  0xb282, 0xa882, 0x9e82, 0x9682, 0x8d82, 0x8582, 0x7e82, 0x7682,
56  0x7082, 0x6982, 0x6382, 0x5e82, 0x5882, 0x5382, 0x4f82, 0x4a82,
57  0x4682, 0x4282, 0xfc01, 0xee01, 0xe001, 0xd401, 0xc801, 0xbd01,
58  0xb201, 0xa801, 0x9e01, 0x9501, 0x8d01, 0x8501, 0x7e01, 0x7601,
59  0x7001, 0x6901, 0x6301, 0x5e01, 0x5801, 0x5301, 0x4f01, 0x4a01,
60  0x4601
61 };
62 
64 static const note_t sys_beep[]={
65  {PITCH_A4 , 1}, {PITCH_END, 0}
66 };
67 
70  sys_beep
71 };
72 
73 unsigned dsound_16th_ms;
74 unsigned dsound_internote_ms;
75 volatile note_t *dsound_next_note;
76 volatile time_t dsound_next_time;
77 
78 static volatile int internote;
79 
80 
82 //
83 // Internal Functions
84 //
86 
88 static inline void play_freq(unsigned freq) {
89  unsigned char CKSmask = freq & 0xff;
90  unsigned char match = freq >> 8;
91 
92  T0_CR = 0x00; // timer off
93  T0_CNT = 0x00; // counter reset
94 
95 #if 0
96  bit_load(CKSmask,0x7); // set ICKS0
97  bit_store(&STCR,0x0);
98 #else
99  if (CKSmask & 0x80)
100  STCR |= 0x01; // ICKS0 = 1
101  else
102  STCR &= ~0x01; // ICKS0 = 0
103 #endif
104 
105  T0_CORA = match; // set compare match A
106  T0_CR = CR_CLEAR_ON_A | (CKSmask &0x3);
107 }
108 
110 static inline void play_pause() {
111  T0_CR = 0x00; // timer 0 off
112 }
113 
114 
116 //
117 // System functions
118 //
120 
122 #ifdef CONF_RCX_COMPILER
123 void dsound_handler(void) {
124 #else
125 HANDLER_WRAPPER("dsound_handler","dsound_core");
127 void dsound_core(void) {
128 #endif
129  if (get_system_up_time() >= dsound_next_time) {
130 
131  if(internote) {
132  play_pause();
134 
135  internote=0;
136  return;
137  }
138 
139  if(dsound_next_note) {
140  unsigned char pitch =dsound_next_note->pitch;
141 
142  if(pitch<PITCH_MAX) {
143  if(pitch!=PITCH_PAUSE)
144  play_freq(pitch2freq[pitch]);
145  else
146  play_pause();
147 
148  dsound_next_time = get_system_up_time() + dsound_16th_ms * dsound_next_note->length
150  dsound_next_note++;
151  internote=1;
152  return;
153  }
154  }
155 
156  dsound_stop();
157  }
158 }
159 
161 void dsound_init() {
164  dsound_stop();
165  T0_CSR = CSR_TOGGLE_ON_A; // Output toggles on compare Match A
166 }
167 
169 void dsound_shutdown() {
170  dsound_stop();
171 }
172 
173 
175 //
176 // User functions
177 //
179 
181 void dsound_stop(void) {
182  play_pause();
183  dsound_next_note=0;
184  dsound_next_time=0xffffffff;
185  internote=0;
186 }
187 
190  return !dsound_playing();
191 }
192 
193 #endif // CONF_DSOUND

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2005 by the authors.

Generated on Sat Mar 15 2014 11:28:21 for brickOS Kernel Developer by doxygen 1.8.1.2