/
ns_tandy.c
212 lines (162 loc) · 5.96 KB
/
ns_tandy.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
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include "ns_dpmi.h"
#include "ns_task.h"
#include "ns_cards.h"
#include "ns_user.h"
#include "ns_tandy.h"
#include "ns_muldf.h"
#include "ns_fxm.h"
#include "m_misc.h"
#include "doomstat.h"
#include "options.h"
#define TANDY_Port 0x2C0
static int TANDY_Installed = 0;
static char *TANDY_BufferStart;
static char *TANDY_CurrentBuffer;
static int TANDY_BufferNum = 0;
static int TANDY_NumBuffers = 0;
static int TANDY_TransferLength = 0;
static int TANDY_CurrentLength = 0;
static char *TANDY_SoundPtr;
volatile int TANDY_SoundPlaying;
static task *TANDY_Timer;
void (*TANDY_CallBack)(void);
/*---------------------------------------------------------------------
Function: TANDY_ServiceInterrupt
Handles interrupt generated by sound card at the end of a voice
transfer. Calls the user supplied callback function.
---------------------------------------------------------------------*/
const unsigned char TandyLUTdb[256] = {
0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
0x9F, 0x9F, 0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x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x91, 0x91, 0x91, 0x91, 0x91,
0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
0x91, 0x91, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
};
static void TANDY_Silence()
{
// Silence 3 channels + noise
outp(TANDY_Port, 0x9F); // Channel 0
outp(TANDY_Port, 0xBF); // Channel 1
outp(TANDY_Port, 0xDF); // Channel 2
outp(TANDY_Port, 0xFF); // Noise
}
static void TANDY_StartStreaming()
{
TANDY_Silence();
// Set channel 0 to 125 KHz
outp(TANDY_Port, 0x80);
outp(TANDY_Port, 0x00);
}
static void TANDY_ServiceInterrupt(task *Task)
{
unsigned char value = TandyLUTdb[*((unsigned char *)TANDY_SoundPtr)];
// Set volume for channel 0
outp(TANDY_Port, value);
TANDY_SoundPtr++;
TANDY_CurrentLength--;
if (TANDY_CurrentLength == 0)
{
// Keep track of current buffer
TANDY_CurrentBuffer += TANDY_TransferLength;
TANDY_BufferNum++;
if (TANDY_BufferNum >= TANDY_NumBuffers)
{
TANDY_BufferNum = 0;
TANDY_CurrentBuffer = TANDY_BufferStart;
}
TANDY_CurrentLength = TANDY_TransferLength;
TANDY_SoundPtr = TANDY_CurrentBuffer;
// Call the caller's callback function
if (TANDY_CallBack != NULL)
{
MV_ServiceVoc();
}
}
}
/*---------------------------------------------------------------------
Function: TANDY_StopPlayback
Ends the transfer of digitized sound to the Sound Source.
---------------------------------------------------------------------*/
void TANDY_StopPlayback(void)
{
if (TANDY_SoundPlaying)
{
TS_Terminate(TANDY_Timer);
TANDY_SoundPlaying = 0;
TANDY_BufferStart = NULL;
}
TANDY_Silence();
}
/*---------------------------------------------------------------------
Function: TANDY_BeginBufferedPlayback
Begins multibuffered playback of digitized sound on the Sound Source.
---------------------------------------------------------------------*/
int TANDY_BeginBufferedPlayback(
char *BufferStart,
int BufferSize,
int NumDivisions,
void (*CallBackFunc)(void))
{
if (TANDY_SoundPlaying)
{
TANDY_StopPlayback();
}
TANDY_CallBack = CallBackFunc;
TANDY_BufferStart = BufferStart;
TANDY_CurrentBuffer = BufferStart;
TANDY_SoundPtr = BufferStart;
// VITI95: OPTIMIZE
TANDY_TransferLength = BufferSize / NumDivisions;
TANDY_CurrentLength = TANDY_TransferLength;
TANDY_BufferNum = 0;
TANDY_NumBuffers = NumDivisions;
TANDY_SoundPlaying = 1;
TANDY_Timer = TS_ScheduleTask(TANDY_ServiceInterrupt, FX_MixRate, 1, NULL);
TS_Dispatch();
return (TANDY_Ok);
}
/*---------------------------------------------------------------------
Function: TANDY_Init
Initializes the Sound Source prepares the module to play digitized
sounds.
---------------------------------------------------------------------*/
int TANDY_Init(int soundcard)
{
int i;
if (TANDY_Installed)
{
TANDY_Shutdown();
}
TANDY_StartStreaming();
TANDY_SoundPlaying = 0;
TANDY_CallBack = NULL;
TANDY_BufferStart = NULL;
TANDY_Installed = 1;
return (TANDY_Ok);
}
/*---------------------------------------------------------------------
Function: TANDY_Shutdown
Ends transfer of sound data to the Sound Source.
---------------------------------------------------------------------*/
void TANDY_Shutdown(void)
{
TANDY_StopPlayback();
TANDY_SoundPlaying = 0;
TANDY_BufferStart = NULL;
TANDY_CallBack = NULL;
TANDY_Installed = 0;
}