-
Notifications
You must be signed in to change notification settings - Fork 3
/
JUDAS.C
3004 lines (2631 loc) · 118 KB
/
JUDAS.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* JUDAS Apocalyptic Softwaremixing Sound System V2.10d
* original version 2.06y by Faust & Yehar
* extended version 2.10b with Intel ICH AC97/HDA support by Black Spider
* Please be aware that HDA support in this version falls under the GNU/GPL
* license. The HDA code was written after analyzing various HDA linux related
* sources, the drivers from ALSA project made by Takashi Iwai, the OSS sound
* system project and others. Although this code is different and pretty unique
* some similarities especially in the HDA nodes access strategy can be found.
* That is why I decided to publicly release this version of Judas under the
* GNU/GPL license.
*
* Supported:
* - SOUND BLASTER (8bit, mono, up to 22050 Hz)
* - SOUND BLASTER PRO (8bit, stereo, up to 22050 Hz or mono up to 44100 Hz)
* - SOUND BLASTER 16 (16bit, stereo, up to 44100 Hz)
* - ULTRASOUND (16bit, stereo, up to 44100 Hz)
* - INTEL ICH0/ICH/ICH2/ICH3/ICH4 AC97 CODEC (16bit, stereo, up to 48000 Hz)
* - High Definition Audio codecs (downgrade to 16bit stereo at 48000 Hz max)
* - WAV file output
* - XMs, MODs, S3Ms, WAVs, raw samples
*
* Other features:
* - Clipping of sound output
* - Interpolation options: None, linear, cubic
* - Quality mixer: Click removing, high quality, 32 bit, clipping indicator
*
* This is the main module, where soundcards & mixer are initialized. It's not
* very good coding style, but who cares!
*
* Changes:
* V2.01 Doesn't fuck up if soundcard variables contain XXX-shit!
* V2.04y Added selectable quality mixer. Old mixer is now called fast mixer
* Added file writer
* V2.06y Wrote "char *volatile pos" instead of old "volatile char *pos"!
* Removed file writer and made the WAV writer in JUDASWAV.C
* V2.07a Support for Intel ICH0/ICH/ICH2/ICH3 and compatible AC97 Codecs
* added by Black Spider
* (Read JUDAS.DOC for complete history)
* V2.07y Improved Intel ICH0/ICH/ICH2/ICH3 AC97 code
* Added ICH4 controller support
* V2.08y Improved Intel ICH4 controller support
* (Read JUDAS.DOC for complete history)
* V2.09a Added support for SIS7012 - now it works :)
* V2.09b Corrected ICH4 and above chips support
* V2.09c dev version
* V2.09d corrected dev version with full WATCOM C++ & DJGPP support
* V2.09e all AC97 devices now use memory mapped IO if available
* V2.09f WAV library builder added to JUDAS
* V2.10a Intel HDA codecs support added
* basing on info from ALSA sources and other interesting HDA resources
* on the web i was finally able to add High Definition audio codecs support
* for Judas Sound System (this is not a public release).
* V2.10b judas_setwavlib function added - first public release with HDA code
* licensed under the GNU/GPL
* V2.10d HDA codecs support update for intel 5, 6, 7 series test by RayeR
*/
/*
* Borland / Watcom REGS structure compatibility
*/
#ifdef __DJGPP__
#define _BORLAND_DOS_REGS
#endif
#include <io.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <mem.h>
#include <stdarg.h>
#include "judasdma.h"
#include "judasmem.h"
#include "judascfg.h"
#include "judasgus.h"
#include "judaserr.h"
#include "judasac.h"
#ifdef __DJGPP__
#include <go32.h>
#include <dpmi.h>
#include <sys/nearptr.h>
#define HANDLE_PRAGMA_PACK_PUSH_POP 1
/* define as nothing nothing */
#define interrupt
/* make port access compatible with Watcom */
#define inp(P) inportb(P)
#define inpw(P) inportw(P)
#define inpd(P) inportl(P)
#define outp(P,V) outportb(P,V)
#define outpw(P,V) outportw(P,V)
#define outpd(P,V) outportl(P,V)
#else
#define __djgpp_conventional_base 0
#endif
/*
Debug or retail release (only for AC97/HDA related code)
*/
// #define AC97_DEBUG
// #define HDA_DEBUG
/*
* Sound device numbers
*/
#define DEV_NOSOUND 0
#define DEV_SB 1
#define DEV_SBPRO 2
#define DEV_SB16 3
#define DEV_GUS 4
#define DEV_AC97 5
#define DEV_HDA 6
#define DEV_FILE 7
/*
* Interrupt controller ports
*/
#define PICPORT1 0x20
#define PICPORT2 0xa0
#define PICMASK1 0x21
#define PICMASK2 0xa1
/*
* Mixer numbers
*/
#define FASTMIXER 1
#define QUALITYMIXER 2
/*
* Mixmode bits
*/
#define MONO 0
#define STEREO 1
#define EIGHTBIT 0
#define SIXTEENBIT 2
/*
* Voicemode bits
*/
#define VM_OFF 0
#define VM_ON 1
#define VM_LOOP 2
#define VM_16BIT 4
/*
* Sample & channel structures
*/
typedef struct
{
char *start;
char *repeat;
char *end;
char *vuprofile;
unsigned char voicemode;
} SAMPLE;
#pragma pack(push,1)
typedef struct
{
char *volatile pos;
char *repeat;
char *end;
SAMPLE *volatile sample;
unsigned freq;
volatile unsigned short fractpos;
unsigned char mastervol;
unsigned char panning;
signed short vol;
volatile unsigned char voicemode;
/* For quality mixer */
volatile char prevvm;
char *volatile prevpos;
volatile int lastvalleft;
volatile int lastvalright;
volatile int smoothvoll;
volatile int smoothvolr;
} CHANNEL;
#pragma pack(pop)
/*
* Prototypes
*/
void judas_config(void);
int judas_init(unsigned mixrate, unsigned mixer, unsigned mixmode, int interpolation);
void judas_uninit(void);
int judas_songisplaying(void);
static int judas_lock(void);
static int initmixer(void);
static void sb_delay(void);
static void sb_write(unsigned char value);
static unsigned char sb_read(void);
static int sb_reset(void);
static void sb_getversion(void);
static int gus_detect(void);
static void gus_reset(void);
static void gus_delay(void);
static void gus_setupchannels(void);
static void gus_setupchannel(unsigned char channel, unsigned start, unsigned length);
static void gus_stopchannel(unsigned char channel);
/* debug and log functions for ac97 code - internal to judas.c */
static void ac97_set_error_message(char *message);
static void ac97_display_error_message(void);
static void ac97_clear_error_message(void);
static void logmessage(const char *text,...);
/* pci stuff for ich ac97 compatible codecs */
static BYTE pci_config_read_byte(int index);
static WORD pci_config_read_word(int index);
static DWORD pci_config_read_dword(int index);
static void pci_config_write_byte(int index, BYTE data);
static void pci_config_write_word(int index, WORD data);
static void pci_config_write_dword(int index, DWORD data);
static BOOL pci_check_bios(void);
static BOOL pci_find_device();
static void pci_enable_io_access();
static void pci_enable_memory_access();
static void pci_enable_busmaster();
static BOOL detect_windows(void);
static BOOL detect_os2(void);
static BOOL detect_linux(void);
/* ac97 audio mixer registers access helper functions */
static BOOL ac97_codec_semaphore(void);
static WORD ac97_read_codec(BYTE index);
static void ac97_write_codec(BYTE index, WORD data);
static void ac97_stop(void);
static int ac97_reset(void);
static unsigned int ich_INL (int base, unsigned int a);
static unsigned short ich_INW (int base, unsigned short a);
static unsigned char ich_INB (int base, unsigned char a);
static void ich_OUTL (unsigned int d, int base, unsigned int a);
static void ich_OUTW (unsigned short d, int base, unsigned short a);
static void ich_OUTB (unsigned char d, int base, unsigned char a);
/* HDA audio mixer registers access helper functions */
static unsigned int hda_calc_stream_format (int mixrate);
/* HDA register access */
static unsigned int hda_INL (unsigned int a);
static unsigned short hda_INW (unsigned int a);
static unsigned char hda_INB (unsigned int a);
static void hda_OUTL (unsigned int a, unsigned int d);
static void hda_OUTW (unsigned int a, unsigned short d);
static void hda_OUTB (unsigned int a, unsigned char d);
/* HDA Basic read/write to codecs - Single immediate command instead of CORB/RIRB buffers for simplicity */
static BOOL hda_single_send_cmd (unsigned short nid, unsigned int direct, unsigned int verb, unsigned int param);
static unsigned int hda_single_get_response (void);
static unsigned int hda_codec_read (unsigned short nid, unsigned int direct, unsigned int verb, unsigned int param);
static unsigned int hda_param_read (unsigned short nid ,unsigned int param);
static int hda_codec_write (unsigned short nid, unsigned int direct, unsigned int verb, unsigned int param);
/* HDA main functions */
static void hda_codec_stop (void);
static void hda_codec_start (void);
extern long hda_getbufpos (void);
static BOOL hda_reset (void);
/* HDA mixer functions */
static unsigned int hda_mixer_init (void);
static void hda_set_volume (unsigned long reg, unsigned long val);
static unsigned long hda_get_volume (unsigned long reg);
/* nodes identification prototypes */
static unsigned int hda_get_sub_nodes (unsigned short nid, unsigned short *start_node);
static void hda_search_audio_node (void);
static int hda_get_connections (unsigned short nid, unsigned short *conn_list, int max_conns);
static int hda_add_new_node (struct hda_node *node, unsigned short nid);
static struct hda_node *hda_get_node (unsigned short nid);
/* nodes functions prototypes */
static void hda_set_vol_mute (unsigned short nid, int ch, int direction, int index, int val);
static unsigned int hda_get_vol_mute (unsigned short nid, int ch, int direction, int index);
static int hda_codec_amp_update (unsigned short nid, int ch, int direction, int idx, int mask, int val);
static void hda_unmute_output (struct hda_node *node);
static void hda_unmute_input (struct hda_node *node, unsigned int index);
static int hda_select_input_connection (struct hda_node *node, unsigned int index);
static void hda_clear_check_flags (void);
/* output path - select connections */
static int hda_parse_output_path (struct hda_node *node, int dac_idx);
static struct hda_node *hda_parse_output_jack (int jack_type);
static int hda_parse_output (void);
/*
* Assembler functions in JUDASASM.ASM / JASMDJ.ASM
*/
void judas_update(void);
void interrupt sb_handler(void);
void interrupt sb_aihandler(void);
void interrupt sb16_handler(void);
void interrupt gus_handler(void);
void gus_poke(unsigned location, unsigned char data);
unsigned char gus_peek(unsigned location);
void gus_startchannels(void);
void gus_dmaprogram(unsigned pos, unsigned length);
void gus_dmainit(void);
void gus_dmawait(void);
void ac97_poke(unsigned location, unsigned char data);
unsigned char ac97_peek(unsigned location);
void ac97_startchannels(void);
void ac97_dmaprogram(unsigned pos, unsigned length);
void ac97_dmainit(void);
void ac97_dmawait(void);
void fmixer(void *address, int length);
void qmixer(void *address, int length);
void safemixer(void *address, int length);
void normalmix(void);
void ipmix(void);
void qmix_linear(void);
void qmix_cubic(void);
void judas_code_lock_start(void);
void judas_code_lock_end(void);
unsigned short judas_get_ds(void);
/*
* Variables
*/
int judas_error = JUDAS_OK;
void (*judas_player)(void) = NULL;
void (*judas_mixroutine)(void) = &normalmix;
void (*judas_mixersys)(void *address, int length) = &qmixer;
#ifdef __DJGPP__
/* DJGPP version
_go32_dpmi_seginfo structure contains ( unsigned long size; unsigned long pm_offset; unsigned short pm_selector; unsigned short rm_offset; unsigned short rm_segment; ) */
static _go32_dpmi_seginfo judas_oldvect;
static _go32_dpmi_seginfo judas_newvect;
#else
/* WATCOM C++ version */
static void (__interrupt __far *judas_oldvect)();
static void (__interrupt __far *judas_newvect)();
#endif
unsigned judascfg_device = DEV_NOSOUND;
unsigned judascfg_port = -1;
unsigned judascfg_irq = -1;
unsigned judascfg_dma1 = -1;
unsigned judascfg_dma2 = -1;
unsigned judas_irqcount = 0;
unsigned judas_device;
unsigned judas_port;
unsigned judas_irq;
unsigned judas_dma;
unsigned judas_int;
unsigned judas_mixrate;
unsigned judas_mixpos;
unsigned judas_bufferlength;
unsigned judas_buffermask;
unsigned judas_bpmcount;
int *judas_clipbuffer;
int *judas_zladdbuffer; /* Alustukset, li! *** */
int judas_zerolevell = 0;
int judas_zerolevelr = 0;
short *judas_cliptable;
int *judas_volumetable;
unsigned short judas_ds;
static unsigned short dsp_version;
static unsigned char mixer_firsttime = 1;
static unsigned char judas_locked = 0;
static unsigned char judas_oldpicmask1;
static unsigned char judas_oldpicmask2;
unsigned char judas_initialized = 0;
unsigned char judas_mixer;
unsigned char judas_mixmode;
unsigned char judas_bpmtempo;
unsigned char judas_samplesize;
CHANNEL judas_channel[CHANNELS];
char judas_clipped = 0;
char *filewriterbuffer;
int filewriterbuffersize = 65536;
AUDIO_PCI_DEV audio_pci = {0}; /* Pci device structure for AC97/HDA */
unsigned long hda_civ = 0;
unsigned long hda_lpib = 0;
/*
* JUDAS device names
*/
char *judas_devname[] =
{
"No Sound",
"Sound Blaster",
"Sound Blaster Pro",
"Sound Blaster 16",
"UltraSound",
"AC97 Audio Codec",
"HDA Audio Codec",
"File Writer"
};
/*
* Mixer names
*/
char *judas_mixername[] =
{
"(No mixer, big shit will happen)",
"Fast Mixer",
"Quality Mixer",
};
/*
* Mixmode names
*/
char *judas_mixmodename[] =
{
"8-bit mono",
"8-bit stereo",
"16-bit mono",
"16-bit stereo"
};
/*
* Interpolation mode names
*/
char *judas_ipmodename[] =
{
"(?)",
"(?)"
};
/*
* JUDAS error texts
*/
char *judas_errortext[] =
{
"Everything OK",
"Couldn't open file",
"Couldn't read file",
"Incorrect file format",
"Out of memory",
"Hardware init failure",
"Configuration incorrect",
"Out of channels"
};
static unsigned char gus_dmalatch[] =
{
0x40, 0x41, 0x40, 0x42, 0x40, 0x43, 0x44, 0x45
};
static unsigned char gus_irqlatch[] =
{
0x40, 0x40, 0x40, 0x43, 0x40, 0x42, 0x40, 0x44,
0x40, 0x41, 0x40, 0x45, 0x46, 0x40, 0x40, 0x47
};
/*
* Fake sample returned by the routines when in NOSOUND mode
*/
SAMPLE fakesample = {NULL, NULL, NULL, NULL, VM_OFF};
int judas_songisplaying(void) {
return (judas_player != NULL)? 1:0;
}
/********************************************************************
* Error and debug functions for AC97 (internal to judas.c)
********************************************************************/
#define AC97_ERROR_MESSAGE_BUFFER_SIZE 256
static char ac97_error_message[AC97_ERROR_MESSAGE_BUFFER_SIZE] = {0};
static void ac97_set_error_message(char *message)
{
int count = 0;
char cvalue;
while (count < AC97_ERROR_MESSAGE_BUFFER_SIZE - 3) {
cvalue = message[count];
if (!cvalue) break;
ac97_error_message[count] = cvalue;
count++;
}
ac97_error_message[count] = '\n';
ac97_error_message[count + 1] = '\0';
}
static void ac97_display_error_message(void)
{
printf(">>> %s", &ac97_error_message[0]);
}
static void ac97_clear_error_message(void)
{
ac97_error_message[0] = '\0';
}
static void logmessage(const char *text,...)
{
va_list arg;
va_start(arg, text);
vfprintf(stdout, text, arg);
va_end(arg);
}
/********************************************************************
* Intel PCI BIOS helper funtions
********************************************************************/
#ifndef PCI_ANY_ID
#define PCI_ANY_ID ((WORD)(~0))
#endif
static BYTE pci_config_read_byte(int index)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B108; // config read byte
r.x.ebx = (DWORD)audio_pci.device_bus_number;
r.x.edi = (DWORD)index;
int386(0x1a, &r, &r);
if (r.h.ah != 0) {
#ifdef AC97_DEBUG
logmessage("Error : PCI read config byte failed\n");
#endif
r.x.ecx = 0;
}
return (BYTE)r.x.ecx;
}
static WORD pci_config_read_word(int index)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B109; // config read word
r.x.ebx = (DWORD)audio_pci.device_bus_number;
r.x.edi = (DWORD)index;
int386(0x1a, &r, &r);
if (r.h.ah != 0 ){
#ifdef AC97_DEBUG
logmessage("Error : PCI read config word failed\n");
#endif
r.x.ecx = 0;
}
return (WORD)r.x.ecx;
}
static DWORD pci_config_read_dword(int index)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B10A; // config read dword
r.x.ebx = (DWORD)audio_pci.device_bus_number;
r.x.edi = (DWORD)index;
int386(0x1a, &r, &r);
if (r.h.ah != 0 ){
#ifdef AC97_DEBUG
logmessage("Error : PCI read config dword failed\n");
#endif
r.x.ecx = 0;
}
return (DWORD)r.x.ecx;
}
static void pci_config_write_byte(int index, BYTE data)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B10B; // config write byte
r.x.ebx = (DWORD)audio_pci.device_bus_number;
r.x.ecx = (DWORD)data;
r.x.edi = (DWORD)index;
int386(0x1a, &r, &r);
if (r.h.ah != 0 ){
#ifdef AC97_DEBUG
logmessage("Error : PCI write config byte failed\n");
#endif
}
}
static void pci_config_write_word(int index, WORD data)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B10C; // config write word
r.x.ebx = (DWORD)audio_pci.device_bus_number;
r.x.ecx = (DWORD)data;
r.x.edi = (DWORD)index;
int386(0x1a, &r, &r);
if (r.h.ah != 0 ){
#ifdef AC97_DEBUG
logmessage("Error : PCI write config word failed\n");
#endif
}
}
static void pci_config_write_dword(int index, DWORD data)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B10D; // config write dword
r.x.ebx = (DWORD)audio_pci.device_bus_number;
r.x.ecx = (DWORD)data;
r.x.edi = (DWORD)index;
int386(0x1a, &r, &r);
if (r.h.ah != 0 ){
#ifdef AC97_DEBUG
logmessage("Error : PCI write config dword failed\n");
#endif
}
}
static BOOL pci_check_bios(void)
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B101; // PCI BIOS - installation check
r.x.edi = 0x00000000;
int386(0x1a, &r, &r);
if (r.x.edx != 0x20494350) return FALSE; // ' ICP' identifier found ?
return TRUE;
}
static BOOL pci_find_device()
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0000B102; // PCI BIOS - find PCI device
r.x.ecx = audio_pci.device_id; // device ID
r.x.edx = audio_pci.vender_id; // vender ID
r.x.esi = 0x00000000; // device index
int386(0x1a, &r, &r);
if (r.h.ah != 0 ) return FALSE; // device not found
audio_pci.device_bus_number = r.w.bx; // save device & bus/funct number
if(audio_pci.sub_vender_id != PCI_ANY_ID){
// check subsystem vender id
if(pci_config_read_word(0x2C) != audio_pci.sub_vender_id) return FALSE;
}
if(audio_pci.sub_device_id != PCI_ANY_ID){
// check subsystem device id
if(pci_config_read_word(0x2E) != audio_pci.sub_device_id) return FALSE;
}
return TRUE; // device found
}
static void pci_enable_io_access()
{
pci_config_write_word(0x04, pci_config_read_word(0x04) | BIT0);
}
static void pci_enable_memory_access()
{
pci_config_write_word(0x04, pci_config_read_word(0x04) | BIT1);
}
static void pci_enable_busmaster()
{
pci_config_write_word(0x04, pci_config_read_word(0x04) | BIT2);
}
/********************************************************************
* Windows/OS2/Linux detection helper functions
********************************************************************/
static BOOL detect_windows(void) // Win 3.1+ detection
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x1600;
int386(0x2F, &r, &r);
if ((r.h.al & 0x7F) != 0) return TRUE;
return FALSE;
}
static BOOL detect_os2(void) // OS2 2.0+ detection
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x4010;
int386(0x2F, &r, &r);
if (r.w.ax == 0) return TRUE;
return FALSE;
}
static BOOL detect_linux(void) // Linux DOSEMU detection ???
{
union REGS r;
memset(&r, 0, sizeof(r));
r.x.eax = 0x0200;
r.x.ebx = 0x00E6;
int386(0x31, &r, &r); // segment of this vector should be 0xF000
if (r.w.cx != 0xF000) return FALSE;
memset(&r, 0, sizeof(r));
r.x.eax = 0;
int386(0xE6, &r, &r);
if (r.w.ax == 0xAA55) return TRUE;
return FALSE;
}
/********************************************************************
* Judas config and init procedures
********************************************************************/
void judas_config(void)
{
char *envstr;
judascfg_device = DEV_NOSOUND;
/*
* Try to find BLASTER enviroment variable
*/
envstr = getenv("BLASTER");
if (envstr)
{
judascfg_device = DEV_SB;
judascfg_port = -1;
judascfg_irq = -1;
judascfg_dma1 = -1;
judascfg_dma2 = -1;
while (*envstr)
{
unsigned sb_type = 0;
if ((*envstr == 'A') || (*envstr == 'a')) sscanf(envstr + 1, "%x", &judascfg_port);
if ((*envstr == 'I') || (*envstr == 'i')) sscanf(envstr + 1, "%d", &judascfg_irq);
if ((*envstr == 'D') || (*envstr == 'd'))
{
sscanf(envstr + 1, "%d", &judascfg_dma1);
}
if ((*envstr == 'T') || (*envstr == 't'))
{
sscanf(envstr + 1, "%d", &sb_type);
if ((sb_type == 2) || (sb_type == 4)) judascfg_device = DEV_SBPRO;
}
if ((*envstr == 'H') || (*envstr == 'h'))
{
sscanf(envstr + 1, "%d", &judascfg_dma2);
judascfg_device = DEV_SB16;
}
envstr++;
}
}
/*
* Try to find ULTRASND enviroment variable
*/
envstr = getenv("ULTRASND");
if (envstr)
{
unsigned irq2;
judascfg_device = DEV_GUS;
judascfg_port = -1;
judascfg_irq = -1;
judascfg_dma1 = -1;
judascfg_dma2 = -1;
sscanf(envstr, "%x,%d,%d,%d,%d", &judascfg_port, &judascfg_dma1,
&judascfg_dma2, &judascfg_irq, &irq2);
/* If MIDI IRQ is lower then use it (for DOS4G) */
if (irq2 < judascfg_irq) judascfg_irq = irq2;
}
/*
* Try to detect Intel ICH AC97 via PCI access
*/
if (judascfg_device == DEV_NOSOUND)
{
int device;
if (detect_windows() == TRUE) {
ac97_set_error_message("Error : AC97 Audio Codec driver works only in pure DOS.\n");
return;
}
if (detect_os2() == TRUE) {
ac97_set_error_message("Error : AC97 Audio Codec driver works only in pure DOS.\n");
return;
}
if (detect_linux() == TRUE) {
ac97_set_error_message("Error : AC97 Audio Codec driver works only in pure DOS.\n");
return;
}
if (pci_check_bios() == FALSE) {
ac97_set_error_message("Error : PCI BIOS not found.\n");
return;
}
device = 0;
while (audio_dev_list[device].vender_id != 0x0000) {
audio_pci.vender_id = audio_dev_list[device].vender_id;
audio_pci.device_id = audio_dev_list[device].device_id;
audio_pci.sub_vender_id = audio_dev_list[device].sub_vender_id;
audio_pci.sub_device_id = audio_dev_list[device].sub_device_id;
if (pci_find_device() == TRUE){
#ifdef AC97_DEBUG
logmessage("%s found.\n", audio_dev_list[device].string);
#endif
break;
}
device++;
}
if (audio_dev_list[device].vender_id == NULL) {
ac97_set_error_message("Error : AC97/HDA Audio Codec compatible device could not be found.\n");
return;
}
// save device info
audio_pci.device_type = audio_dev_list[device].type;
strcpy (audio_pci.device_name, audio_dev_list[device].string);
// read pci configuration
audio_pci.command = pci_config_read_word (PCI_COMMAND);
audio_pci.irq = pci_config_read_byte (PCI_INTERRUPT_LINE);
audio_pci.pin = pci_config_read_byte (PCI_INT_LINE);
audio_pci.base0 = pci_config_read_dword (PCI_BASE_ADDRESS_0); // NAMBAR
audio_pci.base1 = pci_config_read_dword (PCI_BASE_ADDRESS_1); // NABMBAR
#ifdef AC97_DEBUG
logmessage("AC97/HDA Audio Codec PCI IRQ at %d\n", audio_pci.irq);
logmessage("AC97/HDA Audio Codec IRQ PIN nr %d\n", audio_pci.pin);
#endif
// if memory type IO is enabled then use memory type IO (other manufacturers)
if (audio_pci.command & PCI_COMMAND_MEMORY) audio_pci.mem_mode = 1;
// start configuring devices
switch (audio_pci.device_type) {
case DEVICE_INTEL_ICH4:
// try to go for memory type IO as default for ICH4+ Intel controllers even if disabled in PCI command config
audio_pci.mem_mode = 1;
break;
case DEVICE_NFORCE:
pci_config_write_dword (0x4c, pci_config_read_dword (0x4c) | 0x1000000);
break;
case DEVICE_HDA_INTEL:
audio_pci.hda_mode = 1;
audio_pci.mem_mode = 1;
break;
case DEVICE_HDA_ATI:
audio_pci.hda_mode = 1;
audio_pci.mem_mode = 1;
// enable snoop for ATI SB450 Azalia HD Audio
pci_config_write_byte (0x42, (pci_config_read_byte (0x42) & 0xf8) | 0x2);
break;
case DEVICE_HDA_NVIDIA:
audio_pci.hda_mode = 1;
audio_pci.mem_mode = 1;
// enable snoop for nVidia Azalia HD Audio
pci_config_write_byte (0x4e, (pci_config_read_byte (0x4e) & 0xf0) | 0x0f);
break;
case DEVICE_HDA_SIS:
audio_pci.hda_mode = 1;
audio_pci.mem_mode = 1;
break;
case DEVICE_HDA_ULI:
audio_pci.hda_mode = 1;
audio_pci.mem_mode = 1;
pci_config_write_word (0x40, pci_config_read_word (0x40) | 0x10);
pci_config_write_dword (PCI_MEM_BASE_ADDRESS_1, 0);
break;
case DEVICE_HDA_VIA:
audio_pci.hda_mode = 1;
audio_pci.mem_mode = 1;
break;
default:
break;
}
// HDA configuration
if (audio_pci.hda_mode) {
audio_pci.base0 = pci_config_read_dword (PCI_MEM_BASE_ADDRESS_0); // AZBAR
audio_pci.base0 &= ~7;
#ifdef AC97_DEBUG
logmessage("HDA Audio Codec PCI BASE0 at I/O %04X\n", audio_pci.base0);
#endif
// map linear memory - convert physical address to linear address
if (!DPMI_MapMemory ((unsigned long *)&audio_pci.base0, (unsigned long *)&audio_pci.base0, 0x4000)) audio_pci.mem_mode = 0;
// enable memory, IO and bus master - activate the device
audio_pci.command |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
pci_config_write_word (PCI_COMMAND, audio_pci.command);
// Clear bits 0-2 of PCI register TCSEL (Traffic Class Select Register at offset 0x44)
// Ensuring these bits are 0 clears playback static on some Azalia HD Audio codecs
pci_config_write_byte (0x44, pci_config_read_byte (0x44) & 0xf8);
// HDA config done now we need to initialize the codec
// AC97 configuration
} else {
// get I/O base0 port - this is the Audio Mixer base port
audio_pci.base0 = pci_config_read_dword (PCI_BASE_ADDRESS_0); // NAMBAR
// Remove I/O space marker in bit 0
audio_pci.base0 &= ~0xf;
#ifdef AC97_DEBUG
logmessage("AC97 Audio Codec PCI BASE0 at I/O %04X\n", audio_pci.base0);
#endif
// get I/O base1 port - this is the Bus Master base port
audio_pci.base1 = pci_config_read_dword (PCI_BASE_ADDRESS_1); // NABMBAR
// Remove I/O space marker in bit 0
audio_pci.base1 &= ~0xf;
judascfg_port = audio_pci.base1;
#ifdef AC97_DEBUG
logmessage("AC97 Audio Codec PCI BASE1 at I/O %04X\n", audio_pci.base1);
#endif
// get memory mapped IO for ICH4+ and other new chips
if ((audio_pci.base0 == 0) || (audio_pci.mem_mode)) {
audio_pci.base2 = pci_config_read_dword (PCI_MEM_BASE_ADDRESS_2); // NAMBAR
audio_pci.base3 = pci_config_read_dword (PCI_MEM_BASE_ADDRESS_3); // NABMBAR
// map linear memory - convery physical address to linear address
if (!DPMI_MapMemory ((unsigned long *)&audio_pci.base2, (unsigned long *)&audio_pci.base2, 0x1000)) audio_pci.mem_mode = FALSE;
if (!DPMI_MapMemory ((unsigned long *)&audio_pci.base3, (unsigned long *)&audio_pci.base3, 0x1000)) audio_pci.mem_mode = FALSE;
// for information purposes only
if (audio_pci.mem_mode) judascfg_port = audio_pci.base3;
}
// legacy I/O access
if (audio_pci.mem_mode == FALSE) {
if (audio_pci.device_type == DEVICE_INTEL_ICH4) {
// enable the IOSE bit in 0x41 for legacy mode for ICH4/ICH5
pci_config_write_byte (PCI_ICH4_CFG_REG, 1);
// Set the secondary codec ID
pci_config_write_byte (PCI_COMMAND_PARITY, 0x39);
}
// enable IO and bus master
audio_pci.command |= PCI_COMMAND_MASTER | PCI_COMMAND_IO;
pci_config_write_word (PCI_COMMAND, audio_pci.command);
} else {
// enable port IO access compatibility even though we're going for memory mapped IO !!!
if (audio_pci.device_type == DEVICE_INTEL_ICH4) {
// enable the IOSE bit in 0x41 for legacy mode for ICH4/ICH5
pci_config_write_byte (PCI_ICH4_CFG_REG, 1);
// Set the secondary codec ID
pci_config_write_byte (PCI_COMMAND_PARITY, 0x39);
}
// enable memory, IO and bus master
audio_pci.command |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
pci_config_write_word (PCI_COMMAND, audio_pci.command);
}
// AC97 config done now we need to initialize the codec
}
// test if both I/O range and memory mapped range are NULL
// note : memory range address will be NULL if DPMI_MapMemory function fails for some reason
if ((audio_pci.base0 == 0) && (audio_pci.mem_mode == 0)) {
ac97_set_error_message("Error : AC97/HDA normal I/O and memory mapped I/O access failed!\n");
if (audio_pci.base0) DPMI_UnmapMemory ((unsigned long *)&audio_pci.base0);
if (audio_pci.base2) DPMI_UnmapMemory ((unsigned long *)&audio_pci.base2);
if (audio_pci.base3) DPMI_UnmapMemory ((unsigned long *)&audio_pci.base3);
return; // still DEV_NOSOUND at this point
}
if ((audio_pci.base0 == 0) && (audio_pci.base2 == 0)) {
ac97_set_error_message("Error : AC97/HDA Audio Codec device found, but disabled.\n");
if (audio_pci.base0) DPMI_UnmapMemory ((unsigned long *)&audio_pci.base0);
if (audio_pci.base2) DPMI_UnmapMemory ((unsigned long *)&audio_pci.base2);
if (audio_pci.base3) DPMI_UnmapMemory ((unsigned long *)&audio_pci.base3);
return; // still DEV_NOSOUND at this point
}
if (audio_pci.hda_mode) judascfg_device = DEV_HDA;
else judascfg_device = DEV_AC97;
}
}
int judas_init (unsigned mixrate, unsigned mixer, unsigned mixmode, int interpolation)
{
int sbrate = 0;
/*