-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsb.c
379 lines (345 loc) · 9.22 KB
/
sb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#include "sb.h"
void single_cycle_playback();
void read_buffer( short buffer );
short sb_base; /* default 220h */
char sb_irq; /* default 7 */
char sb_dma; /* default 1 */
void interrupt ( *old_irq )();
short r_buffer;
FILE *raw_file;
long file_size;
volatile int playing;
volatile long to_be_played;
volatile long to_be_read;
unsigned char *dma_buffer;
short page;
short offset;
#define DMA_BLOCK_LENGTH 0x7FFF
#define SB_BLOCK_LENGTH 0x3FFF
#define SB_RESET 0x6
#define SB_READ_DATA 0xA
#define SB_READ_DATA_STATUS 0xE
#define SB_WRITE_DATA 0xC
#define SB_PAUSE_PLAYBACK 0xD0
#define SB_ENABLE_SPEAKER 0xD1
#define SB_DISABLE_SPEAKER 0xD3
#define SB_SET_PLAYBACK_FREQUENCY 0x40
#define SB_SINGLE_CYCLE_PLAYBACK 0x14
#define SB_START_AUTOINIT_PLAYBACK 0x1C
#define SB_SET_BLOCK_SIZE 0x48
#define SB_STOP_AUTOINIT_PLAYBACK 0xDA
#define MASK_REGISTER 0x0A
#define MODE_REGISTER 0x0B
#define MSB_LSB_FLIP_FLOP 0x0C
#define DMA_CHANNEL_0 0x87
#define DMA_CHANNEL_1 0x83
#define DMA_CHANNEL_3 0x82
int reset_dsp(short port)
{
outportb( port + SB_RESET, 1 );
delay(10);
outportb( port + SB_RESET, 0 );
delay(10);
if( ((inportb(port + SB_READ_DATA_STATUS) & 0x80) == 0x80)
&& (inportb(port + SB_READ_DATA) == 0x0AA )
) {
sb_base = port;
return 1;
}
return 0;
}
void write_dsp( unsigned char command )
{
while( (inportb( sb_base + SB_WRITE_DATA ) & 0x80) == 0x80 );
outportb( sb_base + SB_WRITE_DATA, command );
}
int sb_detect()
{
char temp;
char *BLASTER;
sb_base = 0;
/* possible values: 210, 220, 230, 240, 250, 260, 280 */
for( temp = 1; temp < 9; temp++ ) {
if( temp != 7 ) {
if( reset_dsp( 0x200 + (temp << 4) ) ) {
break;
}
}
}
if( temp == 9 ) {
return 0;
}
BLASTER = getenv("BLASTER");
sb_dma = 0;
for( temp = 0; temp < strlen( BLASTER ); ++temp ) {
if((BLASTER[temp] | 32) == 'd') {
sb_dma = BLASTER[temp + 1] - '0';
}
}
for( temp = 0; temp < strlen( BLASTER ); ++temp ) {
if((BLASTER[temp] | 32) == 'i') {
sb_irq = BLASTER[temp + 1] - '0';
if( BLASTER[temp + 2] != ' ' ) {
sb_irq = sb_irq * 10 + BLASTER[ temp + 2 ] - '0';
}
}
}
return sb_base != 0;
}
void interrupt sb_irq_handler()
{
inportb(sb_base + SB_READ_DATA_STATUS);
outportb( 0x20, 0x20 );
if( sb_irq == 2 || sb_irq == 10 || sb_irq == 11 ) {
outportb( 0xA0, 0x20 );
}
if( playing ) {
to_be_played -= 16384;
if( to_be_played > 0 ) {
read_buffer( r_buffer );
if( to_be_played <= 16384 ) {
r_buffer ^= 1;
single_cycle_playback();
} else if( to_be_played <= 32768 ) {
write_dsp( SB_STOP_AUTOINIT_PLAYBACK );
} else {
/* All is good! continue playing! */
}
} else {
playing = 0;
}
}
r_buffer ^= 1;
}
void init_irq()
{
/* save the old irq vector */
if( sb_irq == 2 || sb_irq == 10 || sb_irq == 11 ) {
if( sb_irq == 2) old_irq = getvect( 0x71 );
if( sb_irq == 10) old_irq = getvect( 0x72 );
if( sb_irq == 11) old_irq = getvect( 0x73 );
} else {
old_irq = getvect( sb_irq + 8 );
}
/* set our own irq vector */
if( sb_irq == 2 || sb_irq == 10 || sb_irq == 11 ) {
if( sb_irq == 2) setvect( 0x71, sb_irq_handler );
if( sb_irq == 10) setvect( 0x72, sb_irq_handler );
if( sb_irq == 11) setvect( 0x73, sb_irq_handler );
} else {
setvect( sb_irq + 8, sb_irq_handler );
}
/* enable irq with the mainboard's PIC */
if( sb_irq == 2 || sb_irq == 10 || sb_irq == 11 ) {
if( sb_irq == 2) outportb( 0xA1, inportb( 0xA1 ) & 253 );
if( sb_irq == 10) outportb( 0xA1, inportb( 0xA1 ) & 251 );
if( sb_irq == 11) outportb( 0xA1, inportb( 0xA1 ) & 247 );
outportb( 0x21, inportb( 0x21 ) & 251 );
} else {
outportb( 0x21, inportb( 0x21 ) & !(1 << sb_irq) );
}
}
void deinit_irq()
{
/* restore the old irq vector */
if( sb_irq == 2 || sb_irq == 10 || sb_irq == 11 ) {
if( sb_irq == 2) setvect( 0x71, old_irq );
if( sb_irq == 10) setvect( 0x72, old_irq );
if( sb_irq == 11) setvect( 0x73, old_irq );
} else {
setvect( sb_irq + 8, old_irq );
}
/* enable irq with the mainboard's PIC */
if( sb_irq == 2 || sb_irq == 10 || sb_irq == 11 ) {
if( sb_irq == 2) outportb( 0xA1, inportb( 0xA1 ) | 2 );
if( sb_irq == 10) outportb( 0xA1, inportb( 0xA1 ) | 4 );
if( sb_irq == 11) outportb( 0xA1, inportb( 0xA1 ) | 8 );
outportb( 0x21, inportb( 0x21 ) | 4 );
} else {
outportb( 0x21, inportb( 0x21 ) & (1 << sb_irq) );
}
}
void assign_dma_buffer()
{
unsigned char* temp_buf;
long linear_address;
short page1, page2;
temp_buf = (char *) malloc(32768);
linear_address = FP_SEG(temp_buf);
linear_address = (linear_address << 4)+FP_OFF(temp_buf);
page1 = linear_address >> 16;
page2 = (linear_address + 32767) >> 16;
if( page1 != page2 ) {
dma_buffer = (char *)malloc(32768);
free( temp_buf );
} else {
dma_buffer = temp_buf;
}
linear_address = FP_SEG(dma_buffer);
linear_address = (linear_address << 4)+FP_OFF(dma_buffer);
page = linear_address >> 16;
offset = linear_address & 0xFFFF;
}
void sb_init()
{
init_irq();
assign_dma_buffer();
write_dsp( SB_ENABLE_SPEAKER );
}
void sb_deinit()
{
write_dsp( SB_DISABLE_SPEAKER );
free( dma_buffer );
deinit_irq();
}
void single_cycle_playback()
{
playing = 1;
/* program the DMA controller */
outportb( MASK_REGISTER, 4 | sb_dma );
outportb( MSB_LSB_FLIP_FLOP, 0 );
outportb( MODE_REGISTER, 0x48 | sb_dma );
outportb( sb_dma << 1, offset & 0xFF );
outportb( sb_dma << 1, offset >> 8 );
switch( sb_dma ) {
case 0:
outportb( DMA_CHANNEL_0, page );
break;
case 1:
outportb( DMA_CHANNEL_1, page );
break;
case 3:
outportb( DMA_CHANNEL_3, page );
break;
}
outportb((sb_dma << 1) + 1, to_be_played & 0xFF );
outportb((sb_dma << 1) + 1, to_be_played >> 8 );
outportb(MASK_REGISTER, sb_dma);
write_dsp(SB_SINGLE_CYCLE_PLAYBACK);
write_dsp(to_be_played & 0xFF);
write_dsp(to_be_played >> 8);
to_be_played = 0;
}
void sb_single_play( const char *file_name )
{
memset(dma_buffer, 0, 32768);
raw_file = fopen(file_name, "rb");
fseek(raw_file, 0L, SEEK_END);
file_size = ftell( raw_file );
fseek(raw_file, 0L, SEEK_SET);
fread(dma_buffer, 1, file_size, raw_file);
write_dsp(SB_SET_PLAYBACK_FREQUENCY);
write_dsp(256-1000000/11000);
to_be_played = file_size;
single_cycle_playback();
}
void sb_stop()
{
write_dsp( SB_PAUSE_PLAYBACK );
playing = 0;
fclose( raw_file );
}
void read_buffer( short buffer )
{
const int buf_off = buffer << 14;
if( to_be_read <= 0 ) {
return;
}
if( to_be_read < 16384 ) {
memset( dma_buffer + buf_off, 128, 16384 );
fread( dma_buffer + buf_off, 1, to_be_read, raw_file );
to_be_read = 0;
} else {
fread( dma_buffer + buf_off, 1, 16384, raw_file );
to_be_read -= 16384;
}
}
void auto_init_playback()
{
/* program the DMA controller */
outportb( MASK_REGISTER, 4 | sb_dma );
outportb( MSB_LSB_FLIP_FLOP, 0 );
/*
* 0x58 + DMA = 010110xx
* ^^ block mode
* ^^ auto init
* ^^ read operation (SB reads from memory)
*/
outportb( MODE_REGISTER, 0x58 | sb_dma );
outportb( sb_dma << 1, offset & 0xFF );
outportb( sb_dma << 1, offset >> 8 );
switch( sb_dma ) {
case 0:
outportb( DMA_CHANNEL_0, page );
break;
case 1:
outportb( DMA_CHANNEL_1, page );
break;
case 3:
outportb( DMA_CHANNEL_3, page );
break;
}
outportb((sb_dma << 1) + 1, DMA_BLOCK_LENGTH & 0xFF );
outportb((sb_dma << 1) + 1, DMA_BLOCK_LENGTH >> 8 );
outportb(MASK_REGISTER, sb_dma);
write_dsp(SB_SET_BLOCK_SIZE);
write_dsp(SB_BLOCK_LENGTH & 0xFF);
write_dsp(SB_BLOCK_LENGTH >> 8);
write_dsp(SB_START_AUTOINIT_PLAYBACK);
}
void sb_auto_play( const char *file_name )
{
if( !dma_buffer ) {
return;
}
r_buffer = 0;
memset( dma_buffer, 0, 32768 );
raw_file = fopen( file_name, "rb" );
if( !raw_file ) {
printf("error: file not found %s\n", file_name );
return;
}
fseek( raw_file, 0L, SEEK_END );
file_size = ftell( raw_file );
fseek( raw_file, 0L, SEEK_SET );
to_be_read = to_be_played = file_size;
printf("to be read: %lu\n", to_be_read );
write_dsp( SB_SET_PLAYBACK_FREQUENCY );
write_dsp( 256-1000000/11000 );
write_dsp( SB_ENABLE_SPEAKER );
read_buffer( 0 );
read_buffer( 1 );
if( to_be_read > 0 ) {
printf( "auto init playback...\n" );
auto_init_playback();
} else {
printf( "single cycle playback...\n" );
single_cycle_playback();
}
playing = 1;
}
#if 0
int main()
{
int sb_detected = 0;
sb_detected = sb_detect();
if( !sb_detected ) {
printf("SoundBlaster not found!\n");
return 1;
} else {
printf("SoundBlaster found at A%x I%u D%u.\n", sb_base, sb_irq, sb_dma );
}
sb_init();
sb_auto_play("lwhome.raw");
while( playing && !kbhit() )
;
sb_stop();
sb_deinit();
return 0;
}
#endif