-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMODTEST.EX
2315 lines (2107 loc) · 85.8 KB
/
MODTEST.EX
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
--
-- ZIG version 2.0 - the ZZT-Inspired Game Creation System
-- Copyright (C) 1998-2001, Jacob Hammond
-- Released under Interactive Fantasies
-- Website: http://surf.to/zig
-- E-mail: zig16@hotmail.com
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-307, USA.
--
-- Contact information:
-- * via e-mail: zig16@hotmail.com
-- * via paper mail:
-- Jacob Hammond
-- 1680 Prairie Hawke Ct.
-- McKinleyville, CA 95519
-- USA
--
-- Telephone number available on request.
--
-----------------------------------------------------------------------------
-- modwave.e
-- Module and Wave file player for Euphoria (version 3f)
-- Pete Eberlein <xseal@harborside.com>
-- Revision history highlights:
-- Oct. 28, 1997 (just the loader)
-- Jan. 23, 98 (working on the rest)
-- Feb. 11 fixed the speed, bpm was a major headache
-- Feb. 12 added Jacques Deschenes' doswrap.e for fast DOS file input
-- Feb. 22 did wave files w/ effects
-- Mar. 16 fixed the last mod memory leak (I hope)
-- Mar. 24 fixed sb pro high-speed playback bug
-- Apr. 17 inlined fastfile.e and sound.e
-- Apr. 21 fixed bug that causeways: mod-chan = max-chan
-- Apr. 22 fixed lost keypresses when using Michael Bolin's keyread.e
-- Apr. 25 fixed memory corruption bug (DS,ES not saved on interrupt!)
-- stuff todo:
---- xm, s3m, it loaders (not anytime soon)
---- more effects for wav files
---- kill the inventor of mod files
-- fastfile.e
-- fast binary file operations (converted from doswrap.e by Jacques Deschenes)
-- Pete Eberlein <xseal@harborside.com>
-- fopen } returns -1 if error, file handle otherwise
-- fclose } fresult = -1 if error, 0 otherwise
-- freads \
-- fread_mem } fresult = bytes_read or -1 if error
-- fwrites \
-- fwrite_mem } fresult = bytes_written or -1 if error
-- fseek \
-- fseek_end } fresult = new_file_pos or -1 if error
-- fseek_rel /
include machine.e
constant buffer_size = 32768
constant file_buffer = allocate_low(buffer_size)
if file_buffer = 0 then
puts(1, "out of low memory\n")
abort(1)
end if
sequence regs
regs = repeat(0, 10)
global constant READ = 0, WRITE = 1, READ_WRITE=2
global integer fresult
type filemode(object i)
if atom(i) then
return i >= 0 and i <= 2
end if
return 1
end type
global function fopen(sequence name, filemode mode)
atom name_buffer
name_buffer = allocate_low(length(name)+1)
if not name_buffer then
return -1
end if
poke(name_buffer, name & 0)
if sequence(mode) then
if find('w',mode) or find('W',mode) then -- write
if find('r',mode) or find('R',mode) then -- read+write
mode = READ_WRITE
else
mode = WRITE
end if
else -- read
mode = READ
end if
end if
regs[REG_AX] = #3D00 + mode
regs[REG_DS] = floor(name_buffer/16)
regs[REG_DX] = remainder(name_buffer,16)
regs = dos_interrupt(#21,regs)
if and_bits(regs[REG_FLAGS],1) then
if mode != READ then
regs[REG_AX] = #3C00
regs[REG_CX] = 0 -- attributes
regs[REG_DS] = floor(name_buffer/16)
regs[REG_DX] = remainder(name_buffer,16)
regs = dos_interrupt(#21,regs)
if and_bits(regs[REG_FLAGS],1) then
regs[REG_AX] = -1 -- fail to create
end if
else
regs[REG_AX] = -1 -- fail to open
end if
end if
free_low(name_buffer)
return regs[REG_AX] -- return file handle
end function
global procedure fclose(integer handle)
regs[REG_AX] = #3E00
regs[REG_BX] = handle
regs = dos_interrupt(#21,regs)
fresult = -and_bits(regs[REG_FLAGS], 1)
end procedure
global function freads(integer handle, integer bytes)
sequence data, outregs
regs[REG_AX] = #3F00
regs[REG_BX] = handle
regs[REG_DS] = floor(file_buffer/16)
regs[REG_DX] = remainder(file_buffer,16)
data = {}
fresult = 0
while bytes do
if bytes > buffer_size then
regs[REG_CX] = buffer_size
else
regs[REG_CX] = bytes
end if
bytes = bytes - regs[REG_CX]
outregs = dos_interrupt(#21,regs)
if and_bits(outregs[REG_FLAGS],1) then
fresult = -1
return {} -- failed to read
end if
data = data & peek({file_buffer, outregs[REG_AX]})
fresult = fresult + outregs[REG_AX]
end while
return data
end function
global procedure fread_mem(integer handle, atom mem, integer bytes)
sequence outregs
regs[REG_AX] = #3F00
regs[REG_BX] = handle
regs[REG_DS] = floor(file_buffer/16)
regs[REG_DX] = remainder(file_buffer,16)
fresult = 0
while bytes do
if bytes > buffer_size then
regs[REG_CX] = buffer_size
else
regs[REG_CX] = bytes
end if
bytes = bytes - regs[REG_CX]
outregs = dos_interrupt(#21,regs)
if and_bits(outregs[REG_FLAGS],1) then
fresult = -1
return -- failed to read
end if
mem_copy(mem, file_buffer, outregs[REG_AX])
mem = mem + outregs[REG_AX]
fresult = fresult + outregs[REG_AX]
end while
end procedure
global procedure fwrites(integer handle, sequence data)
sequence outregs
regs[REG_AX] = #4000
regs[REG_BX] = handle
regs[REG_DS] = floor(file_buffer/16)
regs[REG_DX] = remainder(file_buffer,16)
fresult = 0
while length(data) do
if length(data) > buffer_size then
regs[REG_CX] = buffer_size
poke(file_buffer, data[1..buffer_size])
data = data[buffer_size+1..length(data)]
else
regs[REG_CX] = length(data)
poke(file_buffer, data)
data = {}
end if
outregs = dos_interrupt(#21,regs)
if and_bits(outregs[REG_FLAGS],1) then
fresult = -1
return -- failed to write
end if
fresult = fresult + outregs[REG_AX]
end while
end procedure
global procedure fwrite_mem(integer handle, atom mem, integer bytes)
sequence outregs
regs[REG_AX] = #4000
regs[REG_BX] = handle
regs[REG_DS] = floor(file_buffer/16)
regs[REG_DX] = remainder(file_buffer,16)
fresult = 0
while bytes do
if bytes > buffer_size then
regs[REG_CX] = buffer_size
else
regs[REG_CX] = bytes
end if
mem_copy(file_buffer, mem, regs[REG_CX])
mem = mem + regs[REG_CX]
bytes = bytes - regs[REG_CX]
outregs = dos_interrupt(#21,regs)
if and_bits(outregs[REG_FLAGS],1) then
fresult = -1
return -- failed to read
end if
fresult = fresult + outregs[REG_AX]
end while
end procedure
procedure f_seek()
regs = dos_interrupt(#21,regs)
if and_bits(regs[REG_FLAGS], 1) then
fresult = -1
else
fresult = regs[REG_AX] + #10000 * regs[REG_DX]
end if
end procedure
global procedure fseek(integer handle, integer distance)
regs[REG_AX] = #4200
regs[REG_BX] = handle
regs[REG_CX] = floor(distance/#10000)
regs[REG_DX] = remainder(distance,#10000)
f_seek()
end procedure
global procedure fseek_rel(integer handle, integer distance)
regs[REG_AX] = #4201
regs[REG_BX] = handle
regs[REG_CX] = floor(distance/#10000)
regs[REG_DX] = remainder(distance,#10000)
f_seek()
end procedure
global procedure fseek_end(integer handle, integer distance)
regs[REG_AX] = #4202
regs[REG_BX] = handle
regs[REG_CX] = floor(distance/#10000)
regs[REG_DX] = remainder(distance,#10000)
f_seek()
end procedure
------------------------- end fastfile.e -----------------------------------
-- sound.e
-- SoundBlaster interface
-- Pete Eberlein
-- 25 Apr 1998
include machine.e
function allocate_proc(sequence code)
atom proc
proc = allocate(length(code))
if proc then
lock_memory(proc, length(code))
poke(proc, code)
return proc
end if
printf(1, "Unable to allocate memory for length-%d sequence...\nProgram aborted.\n", {length(code)})
abort(1)
end function
----------------------------------------------------------------------------
--PORT I/O------------------------------------------------------------------
----------------------------------------------------------------------------
constant in_byte_result = allocate_proc({0})
constant in_byte_proc = allocate_proc(
{#50, --push eax
#52, --push edx
#BA,#00,#00,#00,#00, --mov edx, port (3)
#EC, --in al, dx
#A2} --mov result, al
& int_to_bytes(in_byte_result) & {
#5A, --pop edx
#58, --pop eax
#C3}) --ret
constant out_byte_proc = allocate_proc(
{#50, --push eax
#52, --push edx
#BA,#00,#00,#00,#00, --mov edx, port (3)
#B0,#00, --mov al, data (8)
#EE, --out dx, al
#5A, --pop edx
#58, --pop eax
#C3}) --ret
constant out_word_proc = allocate_proc(
{#50, --push eax
#52, --push edx
#BA,#00,#00,#00,#00, --mov edx, port (3)
#B8,#00,#00,#00,#00, --mov eax, data (8)
#66,#EF, --out dx, ax
#5A, --pop edx
#58, --pop eax
#C3}) --ret
global function in_byte(integer port)
poke4(in_byte_proc + 3, port)
call(in_byte_proc)
return peek(in_byte_result)
end function
global procedure out_byte(integer port, object data)
poke4(out_byte_proc + 3, port)
if atom(data) then
data = {data}
end if
for i = 1 to length(data) do
poke(out_byte_proc + 8, data[i])
call(out_byte_proc)
end for
end procedure
global procedure out_word(integer port, object data)
poke4(out_word_proc + 3, port)
if atom(data) then
data = {data}
end if
for i = 1 to length(data) do
poke4(out_word_proc + 8, data[i])
call(out_word_proc)
end for
end procedure
----------------------------------------------------------------------------
--SOUND BLASTER INITIALIZATION----------------------------------------------
----------------------------------------------------------------------------
global constant
MONO = 1,
STEREO = 2
global integer playback_mode, playback_bits, playback_rate, volume,
sound_debug_file, pcspeaker
global sequence sound_error
sound_debug_file = 0
playback_mode = 0
pcspeaker = 0
integer base, irq, dma, irq_int_vector, sound_init
sound_init = 0
integer DSP_RESET, -- dsp reset i/o port
DSP_READ_data, -- dsp read data i/o port
DSP_WRITE_data , -- dsp write data i/o port
DSP_WRITE_STATUS, -- dsp write status i/o port
DSP_data_AVAIL, -- dsp read status i/o port
-- some DSP commands
AUTO_INIT_8, -- auto initialize 8-bit
PAUSE_DMA, -- halt dma operation
CONTINUE_DMA, -- continue dma operation
CONTINUE_AUTO_INIT, -- continue auto-initialize dma
EXIT_AUTO_INIT -- exit auto-initialize dma operation
constant
SET_TIME_CONSTANT = #40, -- set dsp sample rate (sb/pro)
SET_SAMPLE_RATE = #41, -- set dsp sample rate (sb16)
SET_BLOCK_SIZE = #48, -- set data block size
SPEAKER_ON = #D1, -- turn on dsp speaker
SPEAKER_OFF = #D3, -- turn off dsp speaker
GET_DSP_VERSION = #E1
integer DMA_ADDRESS,
DMA_COUNT,
DMA_CLEAR,
DMA_MODE,
DMA_PAGE,
DMA_MASK
atom DSPVer, dsp_isr, low_buffer, speakertable
atom dma_buffer, data_buffer
AUTO_INIT_8 = 0
sequence old_dsp_isr
constant dsp_subproc = allocate_proc({0,0,0,0})
constant ret_subproc = allocate_proc({#C3}) -- ret
poke4(dsp_subproc, ret_subproc)
constant cli = allocate_proc(
{#FA, -- 0: cli ; disable interrupts
#C3}) -- 1: ret
constant sti = allocate_proc(
{#FB, -- 0: sti ; enable interrupts
#C3}) -- 1: ret
global integer block_size, num_blocks
block_size = 0 -- will choose later on based on playback_mode and playback_rate
num_blocks = 2
integer code_segment, data_segment
procedure get_segments()
atom segment, save_segment
-- read code and data segments (hardint.ex)
segment = allocate_proc({0,0,0,0})
save_segment = allocate_proc(
{#50, -- push eax
#0E, -- push cs or #1E push ds
#58, -- pop eax
#A3}&int_to_bytes(segment)&{-- mov [segment],eax (4)
#58, -- pop eax
#C3}) -- ret
call(save_segment) -- save code segment
code_segment = peek(segment) + 256 * peek(segment+1)
poke(save_segment+1, #1E)
call(save_segment) -- save data segment
data_segment = peek(segment) + 256 * peek(segment+1)
free(segment)
free(save_segment)
end procedure
procedure write_dsp(integer value)
for i = 0 to 100 do
if and_bits(in_byte(DSP_WRITE_STATUS), #80) = 0 then
out_byte(DSP_WRITE_data, value)
exit
end if
end for
end procedure
function read_dsp()
for i = 0 to 100 do
if and_bits(in_byte(DSP_data_AVAIL), #80) then
return in_byte(DSP_READ_data)
end if
end for
return -1
end function
global constant -- values for set_volume()
MASTER = #22,
VOICE = #04,
FM = #26,
CD = #28,
LINE = #2E,
MIC = #0A
procedure write_mixer(integer index, integer value)
if not pcspeaker then
out_byte(base + 4, index)
out_byte(base + 5, value)
elsif index = MASTER then
for i = 0 to 255 do
poke(speakertable+i, floor(xor_bits(i,128)*value/#1100))
end for
end if
end procedure
procedure reset_dsp()
integer junk
-- resetting the DSP
out_byte(DSP_RESET, 1)
junk = in_byte(DSP_RESET)
junk = in_byte(DSP_RESET)
junk = in_byte(DSP_RESET)
out_byte(DSP_RESET, 0)
for i = 1 to 100 do
if read_dsp() = #AA then
return
end if
end for
sound_error = "DSP failed to reset"
end procedure
global procedure close_sound()
if sound_init = 0 then
return
end if
if pcspeaker then
call(cli)
out_byte(#43, #36)
out_byte(#40, #FC) --lo(65532)
out_byte(#40, #FF) --hi(65532)
set_vector(irq_int_vector, old_dsp_isr)
out_byte(#61, and_bits(in_byte(#61), #FC))
call(sti)
free(speakertable)
else
write_dsp(SPEAKER_OFF)
write_dsp(PAUSE_DMA)
write_dsp(EXIT_AUTO_INIT)
reset_dsp()
set_vector(irq_int_vector, old_dsp_isr)
end if
free(dsp_isr)
if pcspeaker then
free(dma_buffer)
else
free_low(low_buffer)
end if
playback_mode = 0
end procedure
global procedure clear_dma_buffer()
mem_set(dma_buffer, 128, block_size * num_blocks)
end procedure
-- dma channel: 0 1 2 3 4 5 6 7
constant dma_addresses = {#00, #02, #04, #06, #C0, #C4, #C8, #CC}
constant dma_counts = {#01, #03, #05, #07, #C2, #C6, #CA, #CE}
constant dma_pages = {#87, #83, #81, #82, #8F, #8B, #89, #8A}
global function init_sound(integer c, integer b, integer r)
object blaster
integer A, I, D, buffsize
integer PIC_MASK, IRQ_STOP_MASK, IRQ_START_MASK
if sound_init then
close_sound()
end if
pcspeaker = 0
printf(1, "Mode: %d Bits: %d Stereo/mono: %d\n", {r, b, c})
playback_mode = c
playback_bits = b
playback_rate = r
blaster = getenv("BLASTER") & ' '
if atom(blaster) then
puts(1, "BLASTER environment variable not found, using PC speaker.\n")
sound_error = "BLASTER environment variable not found"
if sound_debug_file then
puts(sound_debug_file, sound_error & '\n')
end if
pcspeaker = 1
else
A = find('A', blaster)
I = find('I', blaster)
if playback_bits = 8 then
D = find('D', blaster)
elsif playback_bits = 16 then
D = find('H', blaster)
else
D = 0
end if
if not (A and I) then
sound_error = "Invalid BLASTER environment variable"
if sound_debug_file then
puts(sound_debug_file, sound_error & '\n')
end if
pcspeaker = 1
end if
if D = 0 then
sound_error = sprintf("%d-bit playback rate not supported", {playback_bits})
if sound_debug_file then
puts(sound_debug_file, sound_error & '\n')
end if
pcspeaker = 1
end if
end if
if pcspeaker = 0 then
base = blaster[A+1]*#100 + blaster[A+2]*#10 + blaster[A+3] - #3330
irq = blaster[I+1] - '0'
if blaster[I+2] >= '0' and blaster[I+2] <= '9' then
irq = 10*irq + blaster[I+2] - '0'
end if
dma = blaster[D+1] - '0'
-- setup the dsp ports
DSP_RESET = base + 6 -- dsp reset i/o port
DSP_READ_data = base + #A -- dsp read data i/o port
DSP_WRITE_data = base + #C -- dsp write data i/o port
DSP_WRITE_STATUS = base + #C -- dsp write status i/o port
DSP_data_AVAIL = base + #E -- dsp read status i/o port
-- setup some dsp commands
PAUSE_DMA = #D0 + 5 * (playback_bits=16) -- halt dma operation
CONTINUE_DMA = #D4 + 2 * (playback_bits=16) -- continue dma operation
CONTINUE_AUTO_INIT = #45 + 2 * (playback_bits=16) -- continue auto-initialize dma
EXIT_AUTO_INIT = #DA - (playback_bits=16) -- exit auto-initialize dma operation
-- setup the dma ports
DMA_ADDRESS = dma_addresses[dma+1]
DMA_COUNT = dma_counts[dma+1]
DMA_PAGE = dma_pages[dma+1]
if dma < 4 then
DMA_MASK = #A
DMA_MODE = #B
DMA_CLEAR = #C
else
DMA_MASK = #D4
DMA_MODE = #D6
DMA_CLEAR = #D8
end if
if irq < 8 then
PIC_MASK = #21
irq_int_vector = irq + 8
else
PIC_MASK = #A1
irq_int_vector = #70 + irq - 8
end if
IRQ_STOP_MASK = power(2, and_bits(irq, 7))
IRQ_START_MASK = not_bits(IRQ_STOP_MASK)
reset_dsp()
-- get dsp version number
write_dsp(GET_DSP_VERSION)
DSPVer = read_dsp()
DSPVer = DSPVer + read_dsp() / 100
printf(1, "Sound Blaster detected: A%x I%x D%x (DSP version %1.2f).\n\n", {base, irq, dma, DSPVer})
if DSPVer < 2 then
sound_error = "Old SB card not supported (DSP version < 2.00)"
if sound_debug_file then
puts(sound_debug_file, sound_error & '\n')
end if
pcspeaker = 1
elsif DSPVer < 4 then
if playback_rate * playback_mode > 45454 then
playback_rate = 45454 / playback_mode -- sb pro limited to 44khz mono or 22khz stereo
end if
if sound_debug_file then
printf(1, "Playback rate too high - lowered to %d\n", {playback_rate})
end if
end if
end if
if pcspeaker then
playback_mode = MONO
playback_rate = 10920
block_size = 1024
puts(1, "Using PC speaker. Mono, rate 10920, blocksize 1024.\n")
end if
-- allocate dma buffer and stuff
if block_size = 0 then
block_size = floor(playback_mode * playback_rate / 11025) * 512 + 512
end if
buffsize = block_size * num_blocks
if pcspeaker then
if sound_debug_file then
puts(sound_debug_file, "Using PC speaker\n")
end if
dma_buffer = allocate(buffsize)
if dma_buffer = 0 then
sound_error = "could not allocate memory for dma buffer"
puts(1, sound_error & '\n')
return 1
end if
else
low_buffer = allocate_low(2 * buffsize)
if low_buffer = 0 then
sound_error = "could not allocate low memory for dma buffer"
puts(1, sound_error & '\n')
return 1
end if
if and_bits(low_buffer,#FFFF0000) = and_bits(low_buffer+buffsize-1,#FFFF0000) then
dma_buffer = low_buffer
else
dma_buffer = and_bits(low_buffer, #FFFF0000) + #10000
end if
end if
clear_dma_buffer()
-- poke(dma_buffer, rand(repeat(256, buffsize)))
get_segments()
-- create interupt service routine
if pcspeaker then
speakertable = allocate_proc(repeat(0,256))
data_buffer = allocate_proc(repeat(0,12))
dsp_isr = allocate_proc(
{#9C, -- 0: pushf
#1E, -- 1: push ds
#50, -- 2: push eax
#B8,#00,#00,#00,#00, -- 3: mov eax, data_segment (4)
#50, -- 8: push eax
#1F, -- 9: pop ds
#A1,#00,#00,#00,#00, -- A: mov eax, [bufaddress] (11)
#21,#C0, -- F: and eax, eax
#74,#54, -- 11: jz notplaying
#50, -- 13: push eax
#8A,#00, -- 14: mov al, [eax]
#25,#FF,#00,#00,#00, -- 16: and eax, #FF
#8A,#80,#00,#00,#00,#00,-- 1B: mov al, [eax + speakertable] (29)
#E6,#42, -- 21: out #42, al
#58, -- 23: pop eax
#F7,#05,#00,#00,#00,#00,#01,#00,#00,#00,-- 24: test dword ptr [countdown], 1 (38)
#74,#37, -- 2E: jz notplaying
#FF,#05,#00,#00,#00,#00,-- 30: inc dword ptr [bufaddress] (50)
#FF,#0D,#00,#00,#00,#00,-- 36: dec dword ptr [bufcount] (56)
#75,#29, -- 3C: jnz notplaying
#C7,#05,#00,#00,#00,#00,#00,#00,#00,#00,-- 3E: mov dword ptr [bufcount], dword block_size (64) (68)
#40, -- 48: inc eax
#2D,#00,#00,#00,#00, -- 49: sub eax, dword block_size (74)
#60, -- 4E: pusha
#FF,#15,#00,#00,#00,#00,-- 4F: call near dword ptr [dsp_subproc] (81)
#61, -- 55: popa
#3D,#00,#00,#00,#00, -- 56: cmp eax, dword lastbuffer (87)
#75,#0A, -- 5B: jne notplaying
#C7,#05,#00,#00,#00,#00,#00,#00,#00,#00,-- 5D: mov dword ptr [bufaddress], dword buffer (95) (99)
#58, -- 67: notplaying: pop eax
#FF,#0D,#00,#00,#00,#00,-- 68: dec dword ptr [countdown] (106)
#75,#13, -- 6E: jnz exit_int_8
#C7,#05,#00,#00,#00,#00,#00,#00,#00,#00,-- 70: mov dword ptr [countdown], dword newrate (114) (118)
#1F, -- 7A: pop ds
#9D, -- 7B: popf
#EA,#00,#00,#00,#00,#00,#00,-- 7C: old_int_8: db #EA 0 0 0 0 0 0 (124)
#50, -- 83: exit_int_8: push eax
#B0,#20, -- 84: mov al, #20
#E6,#20, -- 86: out #20, al
#58, -- 88: pop eax
#1F, -- 89: pop ds
#9D, -- 8A: popf
#CF} -- 8B: iret
)
poke4(dsp_isr + 4, data_segment)
poke4(dsp_isr + 11, data_buffer) --bufaddress)
poke4(dsp_isr + 29, speakertable)
poke4(dsp_isr + 38, data_buffer+8) --countdown)
poke4(dsp_isr + 50, data_buffer) --bufaddress)
poke4(dsp_isr + 56, data_buffer+4) --bufcount)
poke4(dsp_isr + 64, data_buffer+4) --bufcount)
poke4(dsp_isr + 68, block_size)
poke4(dsp_isr + 74, block_size)
poke4(dsp_isr + 81, dsp_subproc)
poke4(dsp_isr + 87, dma_buffer + buffsize - block_size)
poke4(dsp_isr + 95, data_buffer) --bufaddress)
poke4(dsp_isr + 99, dma_buffer)
poke4(dsp_isr + 106, data_buffer+8) --countdown)
poke4(dsp_isr + 114, data_buffer+8) --countdown)
poke4(dsp_isr + 118, 1) --newrate)
--poke(dsp_isr + 124, old_int_8)
irq_int_vector = 8
old_dsp_isr = get_vector(irq_int_vector)
poke4(dsp_isr+125, old_dsp_isr[2])
poke(dsp_isr+129, and_bits(old_dsp_isr[1],#FF) & floor(old_dsp_isr[1] / 256))
poke4(data_buffer, {
0, -- dma_buffer
0, -- bufcount
1}) -- tickrate
set_vector(irq_int_vector, {code_segment, dsp_isr})
mem_set(speakertable, 0, 256)
else
data_buffer = allocate_proc({0})
dsp_isr = allocate_proc(
{#60, -- pusha
#06, -- push es
#1E, -- push ds
#9C, -- pushf
#68}&int_to_bytes(data_segment)&{ -- push dword data_seg
#1F, -- pop ds
#A0}&int_to_bytes(data_buffer)&{-- mov al, [data_buffer]
#31,#DB, -- xor ebx, ebx
#88,#C3, -- mov bl, al
#FE,#C0, -- inc al
#3C,num_blocks, -- cmp al, num_blocks
#75,#02, -- jne l2
#30,#C0, -- xor al, al
#A2}&int_to_bytes(data_buffer)&{-- l2: mov [data_buffer], al
#B8}&int_to_bytes(block_size)&{-- mov eax, block_size
#F7,#E3, -- mul ebx
#05}&int_to_bytes(dma_buffer)&{-- add eax, dword dma_buffer
#FB, -- sti
#FF,#15}&int_to_bytes(dsp_subproc)&{-- call near dword ptr [dsp_subproc]
#BA}&int_to_bytes(base+#E+(playback_bits=16))&{-- mov edx, port
#EC, -- in al, dx
#B0,#20, -- mov al, #20
#E6,#A0, -- out #A0, al
#E6,#20, -- out #20, al
#9D, -- popf
#1F, -- push ds
#07, -- push es
#61, -- popa
#CF}) -- iret
call(cli)
-- install interrupt service routine
out_byte(PIC_MASK, or_bits(in_byte(PIC_MASK), IRQ_STOP_MASK))
old_dsp_isr = get_vector(irq_int_vector)
set_vector(irq_int_vector, {code_segment, dsp_isr})
out_byte(PIC_MASK, and_bits(in_byte(PIC_MASK), IRQ_START_MASK))
call(sti)
end if
volume = #FF
write_mixer(MASTER, volume)
sound_init = 1
puts(1, "Sound initialized OK.\n")
return 0
end function
global procedure hook_dsp(atom handler)
call(cli)
if handler then
poke4(dsp_subproc, handler)
else
poke4(dsp_subproc, ret_subproc)
end if
call(sti)
end procedure
global procedure set_playback_rate(integer rate)
-- set playback rate
if sound_init = 0 then
return
end if
playback_rate = rate
if pcspeaker then
rate = floor(rate*2 / 18.2)
call(cli)
poke4(dsp_isr + 118, rate) --newrate)
poke4(data_buffer+8, rate)
out_byte(#43, #36)
rate = floor(65532 / rate)
out_byte(#40, and_bits(rate,#FF))
rate = floor(rate / 256)
out_byte(#40, and_bits(rate,#FF))
out_byte(#43, #90)
out_byte(#61, or_bits(in_byte(#61), 3))
-- out_byte(#42, #00)
call(sti)
elsif DSPVer < 4 then
write_dsp(SET_TIME_CONSTANT)
rate = playback_mode * rate
if rate <= 22727 then
write_dsp(256 - floor(1000000 / rate))
AUTO_INIT_8 = #1C -- normal 8-bit auto-initialize
else
if rate > 45454 then
rate = 45454 -- sb pro limited to 44khz mono or 22khz stereo
playback_rate = rate / playback_mode
puts(1, "rate too high\n")
end if
-- write_dsp(floor((65536 - floor(256000000 / rate)) / 256))
write_dsp(floor((65536 - floor(256000000 / rate)) / 256))
AUTO_INIT_8 = #90 -- high speed 8-bit auto-initialize
end if
else
AUTO_INIT_8 = 0
write_dsp(SET_SAMPLE_RATE)
write_dsp(floor(playback_rate / #100))
write_dsp(and_bits(playback_rate, #FF))
end if
end procedure
integer paused
global procedure start_sound()
integer buf_ofs, buf_len
if sound_init = 0 then
return
end if
paused = 0
if pcspeaker then
set_playback_rate(playback_rate)
poke4(data_buffer, {
dma_buffer, -- dma_buffer
block_size}) -- bufcount
return
end if
poke(data_buffer, 0) -- start on block 0
reset_dsp()
-- set up dma
if playback_bits = 8 then
buf_ofs = and_bits(dma_buffer, #FFFF)
buf_len = and_bits(block_size * num_blocks, #FFFF) - 1
else -- playback_bits = 16
buf_ofs = and_bits(floor(dma_buffer / 2), #FFFF)
buf_len = and_bits(floor(block_size * num_blocks / 2), #FFFF) - 1
end if
out_byte(DMA_MASK, or_bits(dma, 4))
out_byte(DMA_CLEAR, 0)
out_byte(DMA_MODE, #58 + and_bits(dma, 3))
out_byte(DMA_ADDRESS, and_bits(buf_ofs, #FF))
out_byte(DMA_ADDRESS, floor(buf_ofs / #100))
out_byte(DMA_COUNT, and_bits(buf_len, #FF))
out_byte(DMA_COUNT, floor(buf_len / #100))
out_byte(DMA_PAGE, floor(dma_buffer / #10000))
out_byte(DMA_MASK, and_bits(dma, 3))
set_playback_rate(playback_rate)
write_dsp(SPEAKER_ON)
if DSPVer < 4 then -- sb/pro
write_mixer(#E, #11 + 2*(playback_mode=STEREO)) -- set mono or stero playback
write_dsp(SET_BLOCK_SIZE) -- set block size
write_dsp(and_bits(block_size, #FF))
write_dsp(floor(block_size / #100))
write_dsp(AUTO_INIT_8) -- 8-bit auto-initialize (normal/highspeed)
else -- sb16+
write_dsp(#D6 - 2 * playback_bits) -- #C6 (8-bit) or #B6 (16-bit)
write_dsp((playback_mode=STEREO) * #20)
if playback_bits = 8 then
buf_len = block_size - 1
else
buf_len = floor(block_size / 2) - 1
end if
write_dsp(and_bits(buf_len, #FF))
write_dsp(floor(buf_len / #100))
end if
end procedure
global procedure pause_sound()
paused = 1
if pcspeaker then
poke4(data_buffer, {
0, -- dma_buffer
0}) -- bufcount
elsif AUTO_INIT_8 = #90 then
reset_dsp()
else
write_dsp(PAUSE_DMA)
end if
end procedure
global procedure unpause_sound()
paused = 0
if pcspeaker then
poke4(data_buffer, {
dma_buffer, -- dma_buffer
block_size}) -- bufcount
elsif AUTO_INIT_8 = #90 then
start_sound()
else
write_dsp(CONTINUE_DMA)
end if
-- write_dsp(CONTINUE_AUTO_INIT)
end procedure
global procedure toggle_pause()
if paused then
unpause_sound()
else
pause_sound()
end if
end procedure