-
Notifications
You must be signed in to change notification settings - Fork 1
/
TNDY.PAS
398 lines (312 loc) · 6.83 KB
/
TNDY.PAS
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
{Unit to interfere with TLPT and TNDY Tandy-3-voice sound devices
(or real Tandy/PCjr of course)
Version 1.22
27.10.2020 by Jan Knipperts
- Some cleanup and support for new version of TNDYLPT
- Disables ECR on parallel ports if enabled
- New option to use second port (C1h) as Data port (like stunts and some other games do it).
}
Unit TNDY;
interface
type
LPT_ports = record
data,
status,
ctrl,
ecr : word;
LPT : Boolean;
end;
TNDY_Globals = record
IOPort : word;
useC1 : Boolean; {write data bytes to C1h}
end;
var
TANDY : TNDY_Globals;
TLPT : LPT_ports;
const
tandyFreq = 3579545;
tandyDivider = tandyFreq/32;
tandyIntDivider= round(tandyDivider);
minfreq = 110; {A2}
maxfreq = 14080; {A9}
Function TLPT_Init(LPTnum : byte) : Boolean;
Procedure TLPT_Sound(voice:byte; tone:word; level:byte);
Procedure TLPT_Noise(divider, feedback, level:byte);
Procedure TLPT_Mute;
Procedure TNDY_Sound(voice:byte; frequency :word; level:byte);
Procedure TNDY_Noise(divider, feedback, level:byte);
Procedure TNDY_Mute;
Procedure TANDY_PlaySound(voice:byte; tone:word; level:byte);
Procedure TANDY_PlayNoise(divider, feedback, level:byte);
Procedure TANDY_MuteVoice(voice : byte);
Procedure TANDY_Mute_all;
Procedure SpkSound(hz:word);
Procedure SpkNoSound;
implementation
Procedure XDelay(ms:word);
assembler;
asm
mov ax,1000
mul ms
mov cx,dx
mov dx,ax
mov ah,86h
int 15h
end;
{============================= Parallel port Tandy Sound Device ===========================}
Procedure LPT_Send(b : byte);
assembler;
asm
mov al,b
mov dx,TLPT.data
out dx,al
mov al,$0C
mov dx,TLPT.ctrl
out dx,al
mov dx,TLPT.status
mov cx,$18
@@1:
in al,dx
test al,$40
loopnz @@1
cmp cx,0
je @Timeout
mov cx,$18
@@2:
in al,dx
test al,$40
loopz @@2
@Timeout:
mov dx,TLPT.ctrl
mov al,9
out dx,al
end;
procedure TLPT_Sound(voice:byte; tone:word; level:byte);
begin
if tone > 0 then tone := tandyIntDivider div tone;
LPT_Send($80 or (voice*$20) or (tone and $0F));
LPT_Send(tone shr 4);
LPT_Send($90 or (voice*$20) or (level and $0F));
end;
procedure TLPT_Noise(divider, feedback, level:byte);
var noisebyte : byte;
begin
asm
xor al,al
mov al,11100000b
mov noisebyte,al
end;
if feedback > 0 then noisebyte := noisebyte or 4;
noisebyte := noisebyte or divider;
LPT_Send(noisebyte);
asm
xor al,al
mov al,11110000b
or al,level
mov noisebyte,al
end;
LPT_Send(noisebyte);
end;
Procedure TLPT_Mute;
begin
LPT_Send($9F);
LPT_Send($BF);
LPT_Send($DF);
LPT_Send($FF);
end;
Function TLPT_Init(LPTnum : byte) : Boolean;
begin
TANDY.IOport := LPTnum;
TLPT.data := memw[$40:6+LPTnum*2];
if TLPT.data = 0 then
begin
TLPT.lpt := false;
TLPT.status := 0;
TLPT.ctrl := 0;
TLPT.ecr := 0;
end
else
begin
TLPT.lpt := true;
TLPT.status := TLPT.data + 1;
TLPT.ctrl := TLPT.data + 2;
TLPT.ecr := TLPT.data + $402;
asm
mov dx,TLPT.ctrl
xor al,al
out dx,al
mov dx,TLPT.ecr
in al,dx
and al,3
cmp al,1 {Is ECR enabled?}
jne @No_ECR
xor al,al {then disable it}
out dx,al
@No_ECR:
end;
TLPT_Mute;
asm
mov dx,TLPT.ctrl
mov al,07
out dx,al
end;
xdelay(200); {wait}
asm
mov dx,TLPT.ctrl
mov al,09
out dx,al
end;
xdelay(200); {wait}
end;
TLPT_Init := TLPT.lpt;
end;
{============================= Real Tandy/PCjr hardware or TNDY ISA Sound Device ===========================}
Procedure TNDY_Sound(voice:byte; frequency :word; level:byte);
var tone : word;
begin
if frequency > 0 then
begin
tone := tandyIntDivider div frequency;
asm
mov dx,Tandy.IOPort
mov cl,voice
mov al,$10
shl al,cl
and al,$60
or al,$80
mov ch,al {save channel for volume set}
mov bx,tone
mov ah,bl
and ah,$0F
or al,ah
out dx,al {write command byte with first 4 bits of freq.}
cmp Tandy.UseC1,1 {use a scond port for the data byte?}
jne @Second_Byte
inc dx {if so, write data to next port}
@Second_Byte:
mov cl,4
shr bx,cl
mov al,bl
out dx,al {write rest of frequency}
cmp Tandy.UseC1,1 {do we use a scond por?}
jne @Third_Byte
dec dx {then write volume to port C0 again}
@Third_Byte:
mov al,level
and al,$0F
or al,ch
or al,$10
out dx,al {write volume byte}
end;
end;
end;
procedure TNDY_Noise(divider, feedback, level:byte);
assembler;
asm
mov ax,bx
mov dx,Tandy.IOPort
xor al,al
mov al,11100000b
cmp feedback,0
je @SkipFeedback
or al,4
@SkipFeedback:
or al,divider
out dx,al {write noise command byte}
xor al,al
mov al,11110000b
or al,level
out dx,al {write volume byte}
end;
Procedure TNDY_Mute;
assembler;
asm
mov dx,Tandy.IOPort
mov al,$9F {Set volume for all channels to 15 (off)}
out dx,al
mov al,$BF
out dx,al
mov al,$DF
out dx,al
mov al,$FF
out dx,al
end;
{======================== universal procedures to drive Tandy sound on all devices =======================}
Procedure TANDY_PlaySound(voice:byte; tone:word; level:byte);
begin
if tone >= minfreq then
begin
if tone > maxfreq then tone := maxfreq;
if level > 15 then level := 15;
if voice > 2 then voice := 2;
if TLPT.lpt then
TLPT_Sound(voice,tone,level)
else
TNDY_Sound(voice,tone,level);
end;
end;
Procedure TANDY_PlayNoise(divider, feedback, level:byte);
begin
if TLPT.lpt then
begin
TLPT_Noise(divider, feedback, level);
end
else
begin
TNDY_Noise(divider, feedback, level);
end;
end;
Procedure TANDY_MuteVoice(voice : byte);
begin
if TLPT.lpt then
LPT_Send($9F + (voice * $20))
else
port[Tandy.IOport] := $9F + (voice * $20);
end;
Procedure TANDY_Mute_all;
begin
if TLPT.lpt then
begin
TLPT_Mute;
end
else
begin
TNDY_Mute;
end;
end;
{=================================== PC SPEAKER ===================================}
Procedure SpkSound(hz:word);
assembler;
asm
mov bx,hz
mov ax,34ddh
mov dx,0012h
cmp dx,bx
jnc @2
div bx
mov bx,ax
in al,61h
test al,3
jnz @1
or al,3
out 61h,al
mov al,0b6h
out 43h,al
@1:
mov al,bl
out 42h,al
mov al,bh
out 42h,al
@2:
end;
Procedure SpkNoSound;
assembler;
asm
in al,61h
and al,0fch
out 61h,al
end;
begin
Tandy.IOPort := $C0;
Tandy.useC1 := true; {write data bytes to C1h}
end.