-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsb16.js
1859 lines (1580 loc) · 49 KB
/
sb16.js
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
"use strict";
// Useful documentation, articles, and source codes for reference:
// ===============================================================
//
// Official Hardware Programming Guide
// -> https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf
//
// Official Yamaha YMF262 Manual
// -> http://map.grauw.nl/resources/sound/yamaha_ymf262.pdf
//
// OPL3 Programming Guide
// -> http://www.fit.vutbr.cz/~arnost/opl/opl3.html
//
// DOSBox
// -> https://sourceforge.net/p/dosbox/code-0/HEAD/tree/dosbox/branches/mamesound/src/hardware/sblaster.cpp
// -> https://github.com/duganchen/dosbox/blob/master/src/hardware/sblaster.cpp
// -> https://github.com/joncampbell123/dosbox-x/blob/master/src/hardware/sblaster.cpp
//
// QEMU
// -> https://github.com/qemu/qemu/blob/master/hw/audio/sb16.c
// -> https://github.com/hackndev/qemu/blob/master/hw/sb16.c
//
// VirtualBox
// -> https://www.virtualbox.org/svn/vbox/trunk/src/VBox/Devices/Audio/DevSB16.cpp
// -> https://github.com/mdaniel/virtualbox-org-svn-vbox-trunk/blob/master/src/VBox/Devices/Audio/DevSB16.cpp
var
// Used for drivers to identify device (DSP command 0xE3).
/** @const */ DSP_COPYRIGHT = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.",
// Value of the current DSP command that indicates that the
// next command/data write in port 2xC should be interpreted
// as a command number.
/** @const */ DSP_NO_COMMAND = 0,
// Size (bytes) of the DSP write/read buffers
/** @const */ DSP_BUFSIZE = 64,
// Size (bytes) of the buffers containing floating point linear PCM audio.
/** @const */ DSP_DACSIZE = 65536,
// Size (bytes) of the buffer in which DMA transfers are temporarily
// stored before being processed.
/** @const */ SB_DMA_BUFSIZE = 65536,
// Number of samples to attempt to retrieve per transfer.
/** @const */ SB_DMA_BLOCK_SAMPLES = 1024,
// Usable DMA channels.
/** @const */ SB_DMA0 = 0,
/** @const */ SB_DMA1 = 1,
/** @const */ SB_DMA3 = 3,
/** @const */ SB_DMA5 = 5,
/** @const */ SB_DMA6 = 6,
/** @const */ SB_DMA7 = 7,
// Default DMA channels.
/** @const */ SB_DMA_CHANNEL_8BIT = SB_DMA1,
/** @const */ SB_DMA_CHANNEL_16BIT = SB_DMA5,
// Usable IRQ channels.
/** @const */ SB_IRQ2 = 2,
/** @const */ SB_IRQ5 = 5,
/** @const */ SB_IRQ7 = 7,
/** @const */ SB_IRQ10 = 10,
// Default IRQ channel.
/** @const */ SB_IRQ = SB_IRQ5,
// Indices to the irq_triggered register.
/** @const */ SB_IRQ_8BIT = 0x1,
/** @const */ SB_IRQ_16BIT = 0x2,
/** @const */ SB_IRQ_MIDI = 0x1,
/** @const */ SB_IRQ_MPU = 0x4;
// Probably less efficient, but it's more maintainable, instead
// of having a single large unorganised and decoupled table.
var DSP_COMMAND_SIZES = new Uint8Array(256);
var DSP_COMMAND_HANDLERS = [];
var MIXER_READ_HANDLERS = [];
var MIXER_WRITE_HANDLERS = [];
var MIXER_REGISTER_IS_LEGACY = new Uint8Array(256);
var FM_HANDLERS = [];
/**
* Sound Blaster 16 Emulator, or so it seems.
* @constructor
* @param {CPU} cpu
* @param {BusConnector} bus
*/
function SB16(cpu, bus)
{
/** @const @type {CPU} */
this.cpu = cpu;
/** @const @type {BusConnector} */
this.bus = bus;
// I/O Buffers.
this.write_buffer = new ByteQueue(DSP_BUFSIZE);
this.read_buffer = new ByteQueue(DSP_BUFSIZE);
this.read_buffer_lastvalue = 0;
// Current DSP command info.
this.command = DSP_NO_COMMAND;
this.command_size = 0;
// Mixer.
this.mixer_current_address = 0;
this.mixer_registers = new Uint8Array(256);
this.mixer_reset();
// Dummy status and test registers.
this.dummy_speaker_enabled = false;
this.test_register = 0;
// DSP state.
this.dsp_highspeed = false;
this.dsp_stereo = false;
this.dsp_16bit = false;
this.dsp_signed = false;
// DAC buffer.
// The final destination for audio data before being sent off
// to Web Audio APIs.
// Format:
// Floating precision linear PCM, nominal between -1 and 1.
this.dac_buffers = [
new FloatQueue(DSP_DACSIZE),
new FloatQueue(DSP_DACSIZE),
];
// Direct Memory Access transfer info.
this.dma = cpu.devices.dma;
this.dma_sample_count = 0;
this.dma_bytes_count = 0;
this.dma_bytes_left = 0;
this.dma_bytes_block = 0;
this.dma_irq = 0;
this.dma_channel = 0;
this.dma_channel_8bit = SB_DMA_CHANNEL_8BIT;
this.dma_channel_16bit = SB_DMA_CHANNEL_16BIT;
this.dma_autoinit = false;
this.dma_buffer = new ArrayBuffer(SB_DMA_BUFSIZE);
this.dma_buffer_int8 = new Int8Array(this.dma_buffer);
this.dma_buffer_uint8 = new Uint8Array(this.dma_buffer);
this.dma_buffer_int16 = new Int16Array(this.dma_buffer);
this.dma_buffer_uint16 = new Uint16Array(this.dma_buffer);
this.dma_syncbuffer = new SyncBuffer(this.dma_buffer);
this.dma_waiting_transfer = false;
this.dma_paused = false;
this.sampling_rate = 22050;
bus.send("dac-tell-sampling-rate", this.sampling_rate);
this.bytes_per_sample = 1;
// DMA identification data.
this.e2_value = 0xAA;
this.e2_count = 0;
// ASP data: not understood by me.
this.asp_registers = new Uint8Array(256);
// MPU.
this.mpu_read_buffer = new ByteQueue(DSP_BUFSIZE);
this.mpu_read_buffer_lastvalue = 0;
// FM Synthesizer.
this.fm_current_address0 = 0;
this.fm_current_address1 = 0;
this.fm_waveform_select_enable = false;
// Interrupts.
this.irq = SB_IRQ;
this.irq_triggered = new Uint8Array(0x10);
// IO Ports.
// http://homepages.cae.wisc.edu/~brodskye/sb16doc/sb16doc.html#DSPPorts
// https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf
cpu.io.register_read_consecutive(0x220, this,
this.port2x0_read, this.port2x1_read, this.port2x2_read, this.port2x3_read);
cpu.io.register_read_consecutive(0x388, this,
this.port2x0_read, this.port2x1_read);
cpu.io.register_read_consecutive(0x224, this,
this.port2x4_read, this.port2x5_read);
cpu.io.register_read(0x226, this, this.port2x6_read);
cpu.io.register_read(0x227, this, this.port2x7_read);
cpu.io.register_read(0x228, this, this.port2x8_read);
cpu.io.register_read(0x229, this, this.port2x9_read);
cpu.io.register_read(0x22A, this, this.port2xA_read);
cpu.io.register_read(0x22B, this, this.port2xB_read);
cpu.io.register_read(0x22C, this, this.port2xC_read);
cpu.io.register_read(0x22D, this, this.port2xD_read);
cpu.io.register_read_consecutive(0x22E, this,
this.port2xE_read, this.port2xF_read);
cpu.io.register_write_consecutive(0x220, this,
this.port2x0_write, this.port2x1_write, this.port2x2_write, this.port2x3_write);
cpu.io.register_write_consecutive(0x388, this,
this.port2x0_write, this.port2x1_write);
cpu.io.register_write_consecutive(0x224, this,
this.port2x4_write, this.port2x5_write);
cpu.io.register_write(0x226, this, this.port2x6_write);
cpu.io.register_write(0x227, this, this.port2x7_write);
cpu.io.register_write_consecutive(0x228, this,
this.port2x8_write, this.port2x9_write);
cpu.io.register_write(0x22A, this, this.port2xA_write);
cpu.io.register_write(0x22B, this, this.port2xB_write);
cpu.io.register_write(0x22C, this, this.port2xC_write);
cpu.io.register_write(0x22D, this, this.port2xD_write);
cpu.io.register_write(0x22E, this, this.port2xE_write);
cpu.io.register_write(0x22F, this, this.port2xF_write);
cpu.io.register_read_consecutive(0x330, this, this.port3x0_read, this.port3x1_read);
cpu.io.register_write_consecutive(0x330, this, this.port3x0_write, this.port3x1_write);
this.dma.on_unmask(this.dma_on_unmask, this);
bus.register("dac-request-data", function()
{
this.dac_handle_request();
}, this);
bus.register("speaker-has-initialized", function()
{
this.mixer_reset();
}, this);
bus.send("speaker-confirm-initialized");
this.dsp_reset();
}
//
// General
//
SB16.prototype.dsp_reset = function()
{
this.write_buffer.clear();
this.read_buffer.clear();
this.command = DSP_NO_COMMAND;
this.command_size = 0;
this.dummy_speaker_enabled = false;
this.test_register = 0;
this.dsp_highspeed = false;
this.dsp_stereo = false;
this.dsp_16bit = false;
this.dsp_signed = false;
this.dac_buffers[0].clear();
this.dac_buffers[1].clear();
this.dma_sample_count = 0;
this.dma_bytes_count = 0;
this.dma_bytes_left = 0;
this.dma_bytes_block = 0;
this.dma_irq = 0;
this.dma_channel = 0;
this.dma_autoinit = false;
this.dma_buffer_uint8.fill(0);
this.dma_waiting_transfer = false;
this.dma_paused = false;
this.e2_value = 0xAA;
this.e2_count = 0;
this.sampling_rate = 22050;
this.bytes_per_sample = 1;
this.lower_irq(SB_IRQ_8BIT);
this.irq_triggered.fill(0);
this.asp_registers.fill(0);
this.asp_registers[5] = 0x01;
this.asp_registers[9] = 0xF8;
};
SB16.prototype.get_state = function()
{
var state = [];
// state[0] = this.write_buffer;
// state[1] = this.read_buffer;
state[2] = this.read_buffer_lastvalue;
state[3] = this.command;
state[4] = this.command_size;
state[5] = this.mixer_current_address;
state[6] = this.mixer_registers;
state[7] = this.dummy_speaker_enabled;
state[8] = this.test_register;
state[9] = this.dsp_highspeed;
state[10] = this.dsp_stereo;
state[11] = this.dsp_16bit;
state[12] = this.dsp_signed;
// state[13] = this.dac_buffers;
//state[14]
state[15] = this.dma_sample_count;
state[16] = this.dma_bytes_count;
state[17] = this.dma_bytes_left;
state[18] = this.dma_bytes_block;
state[19] = this.dma_irq;
state[20] = this.dma_channel;
state[21] = this.dma_channel_8bit;
state[22] = this.dma_channel_16bit;
state[23] = this.dma_autoinit;
state[24] = this.dma_buffer_uint8;
state[25] = this.dma_waiting_transfer;
state[26] = this.dma_paused;
state[27] = this.sampling_rate;
state[28] = this.bytes_per_sample;
state[29] = this.e2_value;
state[30] = this.e2_count;
state[31] = this.asp_registers;
// state[32] = this.mpu_read_buffer;
state[33] = this.mpu_read_buffer_last_value;
state[34] = this.irq;
state[35] = this.irq_triggered;
//state[36]
return state;
};
SB16.prototype.set_state = function(state)
{
// this.write_buffer = state[0];
// this.read_buffer = state[1];
this.read_buffer_lastvalue = state[2];
this.command = state[3];
this.command_size = state[4];
this.mixer_current_address = state[5];
this.mixer_registers = state[6];
this.mixer_full_update();
this.dummy_speaker_enabled = state[7];
this.test_register = state[8];
this.dsp_highspeed = state[9];
this.dsp_stereo = state[10];
this.dsp_16bit = state[11];
this.dsp_signed = state[12];
// this.dac_buffers = state[13];
//state[14]
this.dma_sample_count = state[15];
this.dma_bytes_count = state[16];
this.dma_bytes_left = state[17];
this.dma_bytes_block = state[18];
this.dma_irq = state[19];
this.dma_channel = state[20];
this.dma_channel_8bit = state[21];
this.dma_channel_16bit = state[22];
this.dma_autoinit = state[23];
this.dma_buffer_uint8 = state[24];
this.dma_waiting_transfer = state[25];
this.dma_paused = state[26];
this.sampling_rate = state[27];
this.bytes_per_sample = state[28];
this.e2_value = state[29];
this.e2_count = state[30];
this.asp_registers = state[31];
// this.mpu_read_buffer = state[32];
this.mpu_read_buffer_last_value = state[33];
this.irq = state[34];
this.irq_triggered = state[35];
//state[36];
this.dma_buffer = this.dma_buffer_uint8.buffer;
this.dma_buffer_int8 = new Int8Array(this.dma_buffer);
this.dma_buffer_int16 = new Int16Array(this.dma_buffer);
this.dma_buffer_uint16 = new Uint16Array(this.dma_buffer);
this.dma_syncbuffer = new SyncBuffer(this.dma_buffer);
if(this.dma_paused)
{
this.bus.send("dac-disable");
}
else
{
this.bus.send("dac-enable");
}
};
//
// I/O handlers
//
SB16.prototype.port2x0_read = function()
{
dbg_log("220 read: fm music status port (unimplemented)", LOG_SB16);
return 0xFF;
};
SB16.prototype.port2x1_read = function()
{
dbg_log("221 read: fm music data port (write only)", LOG_SB16);
return 0xFF;
};
SB16.prototype.port2x2_read = function()
{
dbg_log("222 read: advanced fm music status port (unimplemented)", LOG_SB16);
return 0xFF;
};
SB16.prototype.port2x3_read = function()
{
dbg_log("223 read: advanced music data port (write only)", LOG_SB16);
return 0xFF;
};
// Mixer Address Port.
SB16.prototype.port2x4_read = function()
{
dbg_log("224 read: mixer address port", LOG_SB16);
return this.mixer_current_address;
};
// Mixer Data Port.
SB16.prototype.port2x5_read = function()
{
dbg_log("225 read: mixer data port", LOG_SB16);
return this.mixer_read(this.mixer_current_address);
};
SB16.prototype.port2x6_read = function()
{
dbg_log("226 read: (write only)", LOG_SB16);
return 0xFF;
};
SB16.prototype.port2x7_read = function()
{
dbg_log("227 read: undocumented", LOG_SB16);
return 0xFF;
};
SB16.prototype.port2x8_read = function()
{
dbg_log("228 read: fm music status port (unimplemented)", LOG_SB16);
return 0xFF;
};
SB16.prototype.port2x9_read = function()
{
dbg_log("229 read: fm music data port (write only)", LOG_SB16);
return 0xFF;
};
// Read Data.
// Used to access in-bound DSP data.
SB16.prototype.port2xA_read = function()
{
dbg_log("22A read: read data", LOG_SB16);
if(this.read_buffer.length)
{
this.read_buffer_lastvalue = this.read_buffer.shift();
}
dbg_log(" <- " + this.read_buffer_lastvalue + " " + h(this.read_buffer_lastvalue) + " '" + String.fromCharCode(this.read_buffer_lastvalue) + "'", LOG_SB16);
return this.read_buffer_lastvalue;
};
SB16.prototype.port2xB_read = function()
{
dbg_log("22B read: undocumented", LOG_SB16);
return 0xFF;
};
// Write-Buffer Status.
// Indicates whether the DSP is ready to accept commands or data.
SB16.prototype.port2xC_read = function()
{
dbg_log("22C read: write-buffer status", LOG_SB16);
// Always return ready (bit-7 set to low)
return 0x7F;
};
SB16.prototype.port2xD_read = function()
{
dbg_log("22D read: undocumented", LOG_SB16);
return 0xFF;
};
// Read-Buffer Status.
// Indicates whether there is any in-bound data available for reading.
// Also used to acknowledge DSP 8-bit interrupt.
SB16.prototype.port2xE_read = function()
{
dbg_log("22E read: read-buffer status / irq 8bit ack.", LOG_SB16);
if(this.irq_triggered[SB_IRQ_8BIT])
{
this.lower_irq(SB_IRQ_8BIT);
}
var ready = this.read_buffer.length && !this.dsp_highspeed;
return (ready << 7) | 0x7F;
};
// DSP 16-bit interrupt acknowledgement.
SB16.prototype.port2xF_read = function()
{
dbg_log("22F read: irq 16bit ack", LOG_SB16);
this.lower_irq(SB_IRQ_16BIT);
return 0;
};
// FM Address Port - primary register.
SB16.prototype.port2x0_write = function(value)
{
dbg_log("220 write: (unimplemented) fm register 0 address = " + h(value), LOG_SB16);
this.fm_current_address0 = 0;
};
// FM Data Port - primary register.
SB16.prototype.port2x1_write = function(value)
{
dbg_log("221 write: (unimplemented) fm register 0 data = " + h(value), LOG_SB16);
var handler = FM_HANDLERS[this.fm_current_address0];
if(!handler)
{
handler = this.fm_default_write;
}
handler.call(this, value, 0, this.fm_current_address0);
};
// FM Address Port - secondary register.
SB16.prototype.port2x2_write = function(value)
{
dbg_log("222 write: (unimplemented) fm register 1 address = " + h(value), LOG_SB16);
this.fm_current_address1 = 0;
};
// FM Data Port - secondary register.
SB16.prototype.port2x3_write = function(value)
{
dbg_log("223 write: (unimplemented) fm register 1 data =" + h(value), LOG_SB16);
var handler = FM_HANDLERS[this.fm_current_address1];
if(!handler)
{
handler = this.fm_default_write;
}
handler.call(this, value, 1, this.fm_current_address1);
};
// Mixer Address Port.
SB16.prototype.port2x4_write = function(value)
{
dbg_log("224 write: mixer address = " + h(value), LOG_SB16);
this.mixer_current_address = value;
};
// Mixer Data Port.
SB16.prototype.port2x5_write = function(value)
{
dbg_log("225 write: mixer data = " + h(value), LOG_SB16);
this.mixer_write(this.mixer_current_address, value);
};
// Reset.
// Used to reset the DSP to its default state and to exit highspeed mode.
SB16.prototype.port2x6_write = function(yesplease)
{
dbg_log("226 write: reset = " + h(yesplease), LOG_SB16);
if(this.dsp_highspeed)
{
dbg_log(" -> exit highspeed", LOG_SB16);
this.dsp_highspeed = false;
}
else if(yesplease)
{
dbg_log(" -> reset", LOG_SB16);
this.dsp_reset();
}
// Signal completion.
this.read_buffer.clear();
this.read_buffer.push(0xAA);
};
SB16.prototype.port2x7_write = function(value)
{
dbg_log("227 write: undocumented", LOG_SB16);
};
SB16.prototype.port2x8_write = function(value)
{
dbg_log("228 write: fm music register port (unimplemented)", LOG_SB16);
};
SB16.prototype.port2x9_write = function(value)
{
dbg_log("229 write: fm music data port (unimplemented)", LOG_SB16);
};
SB16.prototype.port2xA_write = function(value)
{
dbg_log("22A write: dsp read data port (read only)", LOG_SB16);
};
SB16.prototype.port2xB_write = function(value)
{
dbg_log("22B write: undocumented", LOG_SB16);
};
// Write Command/Data.
// Used to send commands or data to the DSP.
SB16.prototype.port2xC_write = function(value)
{
dbg_log("22C write: write command/data", LOG_SB16);
if(this.command === DSP_NO_COMMAND)
{
// New command.
dbg_log("22C write: command = " + h(value), LOG_SB16);
this.command = value;
this.write_buffer.clear();
this.command_size = DSP_COMMAND_SIZES[value];
}
else
{
// More data for current command.
dbg_log("22C write: data: " + h(value), LOG_SB16);
this.write_buffer.push(value);
}
// Perform command when we have all the needed data.
if(this.write_buffer.length >= this.command_size)
{
this.command_do();
}
};
SB16.prototype.port2xD_write = function(value)
{
dbg_log("22D write: undocumented", LOG_SB16);
};
SB16.prototype.port2xE_write = function(value)
{
dbg_log("22E write: dsp read buffer status (read only)", LOG_SB16);
};
SB16.prototype.port2xF_write = function(value)
{
dbg_log("22F write: undocumented", LOG_SB16);
};
// MPU UART Mode - Data Port
SB16.prototype.port3x0_read = function()
{
dbg_log("330 read: mpu data", LOG_SB16);
if(this.mpu_read_buffer.length)
{
this.mpu_read_buffer_lastvalue = this.mpu_read_buffer.shift();
}
dbg_log(" <- " + h(this.mpu_read_buffer_lastvalue), LOG_SB16);
return this.mpu_read_buffer_lastvalue;
};
SB16.prototype.port3x0_write = function(value)
{
dbg_log("330 write: mpu data (unimplemented) : " + h(value), LOG_SB16);
};
// MPU UART Mode - Status Port
SB16.prototype.port3x1_read = function()
{
dbg_log("331 read: mpu status", LOG_SB16);
var status = 0;
status |= 0x40 * 0; // Output Ready
status |= 0x80 * !this.mpu_read_buffer.length; // Input Ready
return status;
};
// MPU UART Mode - Command Port
SB16.prototype.port3x1_write = function(value)
{
dbg_log("331 write: mpu command: " + h(value), LOG_SB16);
if(value == 0xFF)
{
// Command acknowledge.
this.mpu_read_buffer.clear();
this.mpu_read_buffer.push(0xFE);
}
};
//
// DSP command handlers
//
SB16.prototype.command_do = function()
{
var handler = DSP_COMMAND_HANDLERS[this.command];
if(!handler)
{
handler = this.dsp_default_handler;
}
handler.call(this);
// Reset Inputs.
this.command = DSP_NO_COMMAND;
this.command_size = 0;
this.write_buffer.clear();
};
SB16.prototype.dsp_default_handler = function()
{
dbg_log("Unhandled command: " + h(this.command), LOG_SB16);
};
/**
* @param {Array} commands
* @param {number} size
* @param {function()=} handler
*/
function register_dsp_command(commands, size, handler)
{
if(!handler)
{
handler = SB16.prototype.dsp_default_handler;
}
for(var i = 0; i < commands.length; i++)
{
DSP_COMMAND_SIZES[commands[i]] = size;
DSP_COMMAND_HANDLERS[commands[i]] = handler;
}
}
function any_first_digit(base)
{
var commands = [];
for(var i = 0; i < 16; i++)
{
commands.push(base + i);
}
return commands;
}
// ASP set register
register_dsp_command([0x0E], 2, function()
{
this.asp_registers[this.write_buffer.shift()] = this.write_buffer.shift();
});
// ASP get register
register_dsp_command([0x0F], 1, function()
{
this.read_buffer.clear();
this.read_buffer.push(this.asp_registers[this.write_buffer.shift()]);
});
// 8-bit direct mode single byte digitized sound output.
register_dsp_command([0x10], 1, function()
{
var value = audio_normalize(this.write_buffer.shift(), 127.5, -1);
this.dac_buffers[0].push(value);
this.dac_buffers[1].push(value);
this.bus.send("dac-enable");
});
// 8-bit single-cycle DMA mode digitized sound output.
register_dsp_command([0x14, 0x15], 2, function()
{
this.dma_irq = SB_IRQ_8BIT;
this.dma_channel = this.dma_channel_8bit;
this.dma_autoinit = false;
this.dsp_signed = false;
this.dsp_16bit = false;
this.dsp_highspeed = false;
this.dma_transfer_size_set();
this.dma_transfer_start();
});
// Creative 8-bit to 2-bit ADPCM single-cycle DMA mode digitized sound output.
register_dsp_command([0x16], 2);
// Creative 8-bit to 2-bit ADPCM single-cycle DMA mode digitzed sound output
// with reference byte.
register_dsp_command([0x17], 2);
// 8-bit auto-init DMA mode digitized sound output.
register_dsp_command([0x1C], 0, function()
{
this.dma_irq = SB_IRQ_8BIT;
this.dma_channel = this.dma_channel_8bit;
this.dma_autoinit = true;
this.dsp_signed = false;
this.dsp_16bit = false;
this.dsp_highspeed = false;
this.dma_transfer_start();
});
// Creative 8-bit to 2-bit ADPCM auto-init DMA mode digitized sound output
// with reference byte.
register_dsp_command([0x1F], 0);
// 8-bit direct mode single byte digitized sound input.
register_dsp_command([0x20], 0, function()
{
// Fake silent input.
this.read_buffer.clear();
this.read_buffer.push(0x7f);
});
// 8-bit single-cycle DMA mode digitized sound input.
register_dsp_command([0x24], 2);
// 8-bit auto-init DMA mode digitized sound input.
register_dsp_command([0x2C], 0);
// Polling mode MIDI input.
register_dsp_command([0x30], 0);
// Interrupt mode MIDI input.
register_dsp_command([0x31], 0);
// UART polling mode MIDI I/O.
register_dsp_command([0x34], 0);
// UART interrupt mode MIDI I/O.
register_dsp_command([0x35], 0);
// UART polling mode MIDI I/O with time stamping.
register_dsp_command([0x36], 0);
// UART interrupt mode MIDI I/O with time stamping.
register_dsp_command([0x37], 0);
// MIDI output.
register_dsp_command([0x38], 0);
// Set digitized sound transfer Time Constant.
register_dsp_command([0x40], 1, function()
{
// Note: bTimeConstant = 256 * time constant
this.sampling_rate_change(
1000000
/ (256 - this.write_buffer.shift())
/ this.get_channel_count()
);
});
// Set digitized sound output sampling rate.
// Set digitized sound input sampling rate.
register_dsp_command([0x41, 0x42], 2, function()
{
this.sampling_rate_change((this.write_buffer.shift() << 8) | this.write_buffer.shift());
});
// Set DSP block transfer size.
register_dsp_command([0x48], 2, function()
{
// TODO: should be in bytes, but if this is only used
// for 8 bit transfers, then this number is the same
// as number of samples?
// Wrong: e.g. stereo requires two bytes per sample.
this.dma_transfer_size_set();
});
// Creative 8-bit to 4-bit ADPCM single-cycle DMA mode digitized sound output.
register_dsp_command([0x74], 2);
// Creative 8-bit to 4-bit ADPCM single-cycle DMA mode digitized sound output
// with referene byte.
register_dsp_command([0x75], 2);
// Creative 8-bit to 3-bit ADPCM single-cycle DMA mode digitized sound output.
register_dsp_command([0x76], 2);
// Creative 8-bit to 3-bit ADPCM single-cycle DMA mode digitized sound output
// with referene byte.
register_dsp_command([0x77], 2);
// Creative 8-bit to 4-bit ADPCM auto-init DMA mode digitized sound output
// with reference byte.
register_dsp_command([0x7D], 0);
// Creative 8-bit to 3-bit ADPCM auto-init DMA mode digitized sound output
// with reference byte.
register_dsp_command([0x7F], 0);
// Pause DAC for a duration.
register_dsp_command([0x80], 2);
// 8-bit high-speed auto-init DMA mode digitized sound output.
register_dsp_command([0x90], 0, function()
{
this.dma_irq = SB_IRQ_8BIT;
this.dma_channel = this.dma_channel_8bit;
this.dma_autoinit = true;
this.dsp_signed = false;
this.dsp_highspeed = true;
this.dsp_16bit = false;
this.dma_transfer_start();
});
// 8-bit high-speed single-cycle DMA mode digitized sound input.
register_dsp_command([0x91], 0);
// 8-bit high-speed auto-init DMA mode digitized sound input.
register_dsp_command([0x98], 0);
// 8-bit high-speed single-cycle DMA mode digitized sound input.
register_dsp_command([0x99], 0);
// Set input mode to mono.
register_dsp_command([0xA0], 0);
// Set input mode to stereo.
register_dsp_command([0xA8], 0);
// Program 16-bit DMA mode digitized sound I/O.
register_dsp_command(any_first_digit(0xB0), 3, function()
{
if(this.command & (1 << 3))
{
// Analogue to digital not implemented.
this.dsp_default_handler();
return;
}
var mode = this.write_buffer.shift();
this.dma_irq = SB_IRQ_16BIT;
this.dma_channel = this.dma_channel_16bit;
this.dma_autoinit = !!(this.command & (1 << 2));
this.dsp_signed = !!(mode & (1 << 4));
this.dsp_stereo = !!(mode & (1 << 5));
this.dsp_16bit = true;
this.dma_transfer_size_set();
this.dma_transfer_start();
});
// Program 8-bit DMA mode digitized sound I/O.
register_dsp_command(any_first_digit(0xC0), 3, function()
{
if(this.command & (1 << 3))
{
// Analogue to digital not implemented.
this.dsp_default_handler();
return;
}
var mode = this.write_buffer.shift();
this.dma_irq = SB_IRQ_8BIT;
this.dma_channel = this.dma_channel_8bit;
this.dma_autoinit = !!(this.command & (1 << 2));
this.dsp_signed = !!(mode & (1 << 4));
this.dsp_stereo = !!(mode & (1 << 5));
this.dsp_16bit = false;
this.dma_transfer_size_set();
this.dma_transfer_start();
});
// Pause 8-bit DMA mode digitized sound I/O.
register_dsp_command([0xD0], 0, function()
{
this.dma_paused = true;
this.bus.send("dac-disable");
});
// Turn on speaker.
// Documented to have no effect on SB16.
register_dsp_command([0xD1], 0, function()
{
this.dummy_speaker_enabled = true;
});