sb16.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. "use strict";
  2. // Useful documentation, articles, and source codes for reference:
  3. // ===============================================================
  4. //
  5. // Official Hardware Programming Guide
  6. // -> https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf
  7. //
  8. // Official Yamaha YMF262 Manual
  9. // -> http://map.grauw.nl/resources/sound/yamaha_ymf262.pdf
  10. //
  11. // OPL3 Programming Guide
  12. // -> http://www.fit.vutbr.cz/~arnost/opl/opl3.html
  13. //
  14. // DOSBox
  15. // -> https://sourceforge.net/p/dosbox/code-0/HEAD/tree/dosbox/branches/mamesound/src/hardware/sblaster.cpp
  16. // -> https://github.com/duganchen/dosbox/blob/master/src/hardware/sblaster.cpp
  17. // -> https://github.com/joncampbell123/dosbox-x/blob/master/src/hardware/sblaster.cpp
  18. //
  19. // QEMU
  20. // -> https://github.com/qemu/qemu/blob/master/hw/audio/sb16.c
  21. // -> https://github.com/hackndev/qemu/blob/master/hw/sb16.c
  22. //
  23. // VirtualBox
  24. // -> https://www.virtualbox.org/svn/vbox/trunk/src/VBox/Devices/Audio/DevSB16.cpp
  25. // -> https://github.com/mdaniel/virtualbox-org-svn-vbox-trunk/blob/master/src/VBox/Devices/Audio/DevSB16.cpp
  26. var
  27. // Used for drivers to identify device (DSP command 0xE3).
  28. /** @const */ DSP_COPYRIGHT = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.",
  29. // Value of the current DSP command that indicates that the
  30. // next command/data write in port 2xC should be interpreted
  31. // as a command number.
  32. /** @const */ DSP_NO_COMMAND = 0,
  33. // Size (bytes) of the DSP write/read buffers
  34. /** @const */ DSP_BUFSIZE = 64,
  35. // Size (bytes) of the buffers containing floating point linear PCM audio.
  36. /** @const */ DSP_DACSIZE = 65536,
  37. // Size (bytes) of the buffer in which DMA transfers are temporarily
  38. // stored before being processed.
  39. /** @const */ SB_DMA_BUFSIZE = 65536,
  40. // Number of samples to attempt to retrieve per transfer.
  41. /** @const */ SB_DMA_BLOCK_SAMPLES = 1024,
  42. // Usable DMA channels.
  43. /** @const */ SB_DMA0 = 0,
  44. /** @const */ SB_DMA1 = 1,
  45. /** @const */ SB_DMA3 = 3,
  46. /** @const */ SB_DMA5 = 5,
  47. /** @const */ SB_DMA6 = 6,
  48. /** @const */ SB_DMA7 = 7,
  49. // Default DMA channels.
  50. /** @const */ SB_DMA_CHANNEL_8BIT = SB_DMA1,
  51. /** @const */ SB_DMA_CHANNEL_16BIT = SB_DMA5,
  52. // Usable IRQ channels.
  53. /** @const */ SB_IRQ2 = 2,
  54. /** @const */ SB_IRQ5 = 5,
  55. /** @const */ SB_IRQ7 = 7,
  56. /** @const */ SB_IRQ10 = 10,
  57. // Default IRQ channel.
  58. /** @const */ SB_IRQ = SB_IRQ5,
  59. // Indices to the irq_triggered register.
  60. /** @const */ SB_IRQ_8BIT = 0x1,
  61. /** @const */ SB_IRQ_16BIT = 0x2,
  62. /** @const */ SB_IRQ_MIDI = 0x1,
  63. /** @const */ SB_IRQ_MPU = 0x4;
  64. // Probably less efficient, but it's more maintainable, instead
  65. // of having a single large unorganised and decoupled table.
  66. var DSP_COMMAND_SIZES = new Uint8Array(256);
  67. var DSP_COMMAND_HANDLERS = [];
  68. var MIXER_READ_HANDLERS = [];
  69. var MIXER_WRITE_HANDLERS = [];
  70. var MIXER_REGISTER_IS_LEGACY = new Uint8Array(256);
  71. var FM_HANDLERS = [];
  72. /**
  73. * Sound Blaster 16 Emulator, or so it seems.
  74. * @constructor
  75. * @param {CPU} cpu
  76. * @param {BusConnector} bus
  77. */
  78. function SB16(cpu, bus)
  79. {
  80. /** @const @type {CPU} */
  81. this.cpu = cpu;
  82. /** @const @type {BusConnector} */
  83. this.bus = bus;
  84. // I/O Buffers.
  85. this.write_buffer = new ByteQueue(DSP_BUFSIZE);
  86. this.read_buffer = new ByteQueue(DSP_BUFSIZE);
  87. this.read_buffer_lastvalue = 0;
  88. // Current DSP command info.
  89. this.command = DSP_NO_COMMAND;
  90. this.command_size = 0;
  91. // Mixer.
  92. this.mixer_current_address = 0;
  93. this.mixer_registers = new Uint8Array(256);
  94. this.mixer_reset();
  95. // Dummy status and test registers.
  96. this.dummy_speaker_enabled = false;
  97. this.test_register = 0;
  98. // DSP state.
  99. this.dsp_highspeed = false;
  100. this.dsp_stereo = false;
  101. this.dsp_16bit = false;
  102. this.dsp_signed = false;
  103. // DAC buffer.
  104. // The final destination for audio data before being sent off
  105. // to Web Audio APIs.
  106. // Format:
  107. // Floating precision linear PCM, nominal between -1 and 1.
  108. this.dac_buffers = [
  109. new FloatQueue(DSP_DACSIZE),
  110. new FloatQueue(DSP_DACSIZE),
  111. ];
  112. // Direct Memory Access transfer info.
  113. this.dma = cpu.devices.dma;
  114. this.dma_sample_count = 0;
  115. this.dma_bytes_count = 0;
  116. this.dma_bytes_left = 0;
  117. this.dma_bytes_block = 0;
  118. this.dma_irq = 0;
  119. this.dma_channel = 0;
  120. this.dma_channel_8bit = SB_DMA_CHANNEL_8BIT;
  121. this.dma_channel_16bit = SB_DMA_CHANNEL_16BIT;
  122. this.dma_autoinit = false;
  123. this.dma_buffer = new ArrayBuffer(SB_DMA_BUFSIZE);
  124. this.dma_buffer_int8 = new Int8Array(this.dma_buffer);
  125. this.dma_buffer_uint8 = new Uint8Array(this.dma_buffer);
  126. this.dma_buffer_int16 = new Int16Array(this.dma_buffer);
  127. this.dma_buffer_uint16 = new Uint16Array(this.dma_buffer);
  128. this.dma_syncbuffer = new v86util.SyncBuffer(this.dma_buffer);
  129. this.dma_waiting_transfer = false;
  130. this.dma_paused = false;
  131. this.sampling_rate = 22050;
  132. bus.send("dac-tell-sampling-rate", this.sampling_rate);
  133. this.bytes_per_sample = 1;
  134. // DMA identification data.
  135. this.e2_value = 0xAA;
  136. this.e2_count = 0;
  137. // ASP data: not understood by me.
  138. this.asp_registers = new Uint8Array(256);
  139. // MPU.
  140. this.mpu_read_buffer = new ByteQueue(DSP_BUFSIZE);
  141. this.mpu_read_buffer_lastvalue = 0;
  142. // FM Synthesizer.
  143. this.fm_current_address0 = 0;
  144. this.fm_current_address1 = 0;
  145. this.fm_waveform_select_enable = false;
  146. // Interrupts.
  147. this.irq = SB_IRQ;
  148. this.irq_triggered = new Uint8Array(0x10);
  149. // IO Ports.
  150. // http://homepages.cae.wisc.edu/~brodskye/sb16doc/sb16doc.html#DSPPorts
  151. // https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf
  152. cpu.io.register_read_consecutive(0x220, this,
  153. this.port2x0_read, this.port2x1_read, this.port2x2_read, this.port2x3_read);
  154. cpu.io.register_read_consecutive(0x388, this,
  155. this.port2x0_read, this.port2x1_read);
  156. cpu.io.register_read_consecutive(0x224, this,
  157. this.port2x4_read, this.port2x5_read);
  158. cpu.io.register_read(0x226, this, this.port2x6_read);
  159. cpu.io.register_read(0x227, this, this.port2x7_read);
  160. cpu.io.register_read(0x228, this, this.port2x8_read);
  161. cpu.io.register_read(0x229, this, this.port2x9_read);
  162. cpu.io.register_read(0x22A, this, this.port2xA_read);
  163. cpu.io.register_read(0x22B, this, this.port2xB_read);
  164. cpu.io.register_read(0x22C, this, this.port2xC_read);
  165. cpu.io.register_read(0x22D, this, this.port2xD_read);
  166. cpu.io.register_read_consecutive(0x22E, this,
  167. this.port2xE_read, this.port2xF_read);
  168. cpu.io.register_write_consecutive(0x220, this,
  169. this.port2x0_write, this.port2x1_write, this.port2x2_write, this.port2x3_write);
  170. cpu.io.register_write_consecutive(0x388, this,
  171. this.port2x0_write, this.port2x1_write);
  172. cpu.io.register_write_consecutive(0x224, this,
  173. this.port2x4_write, this.port2x5_write);
  174. cpu.io.register_write(0x226, this, this.port2x6_write);
  175. cpu.io.register_write(0x227, this, this.port2x7_write);
  176. cpu.io.register_write_consecutive(0x228, this,
  177. this.port2x8_write, this.port2x9_write);
  178. cpu.io.register_write(0x22A, this, this.port2xA_write);
  179. cpu.io.register_write(0x22B, this, this.port2xB_write);
  180. cpu.io.register_write(0x22C, this, this.port2xC_write);
  181. cpu.io.register_write(0x22D, this, this.port2xD_write);
  182. cpu.io.register_write(0x22E, this, this.port2xE_write);
  183. cpu.io.register_write(0x22F, this, this.port2xF_write);
  184. cpu.io.register_read_consecutive(0x330, this, this.port3x0_read, this.port3x1_read);
  185. cpu.io.register_write_consecutive(0x330, this, this.port3x0_write, this.port3x1_write);
  186. this.dma.on_unmask(this.dma_on_unmask, this);
  187. bus.register("dac-request-data", function()
  188. {
  189. this.dac_handle_request();
  190. }, this);
  191. bus.register("speaker-has-initialized", function()
  192. {
  193. this.mixer_reset();
  194. }, this);
  195. bus.send("speaker-confirm-initialized");
  196. this.dsp_reset();
  197. }
  198. //
  199. // General
  200. //
  201. SB16.prototype.dsp_reset = function()
  202. {
  203. this.write_buffer.clear();
  204. this.read_buffer.clear();
  205. this.command = DSP_NO_COMMAND;
  206. this.command_size = 0;
  207. this.dummy_speaker_enabled = false;
  208. this.test_register = 0;
  209. this.dsp_highspeed = false;
  210. this.dsp_stereo = false;
  211. this.dsp_16bit = false;
  212. this.dsp_signed = false;
  213. this.dac_buffers[0].clear();
  214. this.dac_buffers[1].clear();
  215. this.dma_sample_count = 0;
  216. this.dma_bytes_count = 0;
  217. this.dma_bytes_left = 0;
  218. this.dma_bytes_block = 0;
  219. this.dma_irq = 0;
  220. this.dma_channel = 0;
  221. this.dma_autoinit = false;
  222. this.dma_buffer_uint8.fill(0);
  223. this.dma_waiting_transfer = false;
  224. this.dma_paused = false;
  225. this.e2_value = 0xAA;
  226. this.e2_count = 0;
  227. this.sampling_rate = 22050;
  228. this.bytes_per_sample = 1;
  229. this.lower_irq(SB_IRQ_8BIT);
  230. this.irq_triggered.fill(0);
  231. this.asp_registers.fill(0);
  232. this.asp_registers[5] = 0x01;
  233. this.asp_registers[9] = 0xF8;
  234. };
  235. SB16.prototype.get_state = function()
  236. {
  237. var state = [];
  238. // state[0] = this.write_buffer;
  239. // state[1] = this.read_buffer;
  240. state[2] = this.read_buffer_lastvalue;
  241. state[3] = this.command;
  242. state[4] = this.command_size;
  243. state[5] = this.mixer_current_address;
  244. state[6] = this.mixer_registers;
  245. state[7] = this.dummy_speaker_enabled;
  246. state[8] = this.test_register;
  247. state[9] = this.dsp_highspeed;
  248. state[10] = this.dsp_stereo;
  249. state[11] = this.dsp_16bit;
  250. state[12] = this.dsp_signed;
  251. // state[13] = this.dac_buffers;
  252. //state[14]
  253. state[15] = this.dma_sample_count;
  254. state[16] = this.dma_bytes_count;
  255. state[17] = this.dma_bytes_left;
  256. state[18] = this.dma_bytes_block;
  257. state[19] = this.dma_irq;
  258. state[20] = this.dma_channel;
  259. state[21] = this.dma_channel_8bit;
  260. state[22] = this.dma_channel_16bit;
  261. state[23] = this.dma_autoinit;
  262. state[24] = this.dma_buffer_uint8;
  263. state[25] = this.dma_waiting_transfer;
  264. state[26] = this.dma_paused;
  265. state[27] = this.sampling_rate;
  266. state[28] = this.bytes_per_sample;
  267. state[29] = this.e2_value;
  268. state[30] = this.e2_count;
  269. state[31] = this.asp_registers;
  270. // state[32] = this.mpu_read_buffer;
  271. state[33] = this.mpu_read_buffer_last_value;
  272. state[34] = this.irq;
  273. state[35] = this.irq_triggered;
  274. //state[36]
  275. return state;
  276. };
  277. SB16.prototype.set_state = function(state)
  278. {
  279. // this.write_buffer = state[0];
  280. // this.read_buffer = state[1];
  281. this.read_buffer_lastvalue = state[2];
  282. this.command = state[3];
  283. this.command_size = state[4];
  284. this.mixer_current_address = state[5];
  285. this.mixer_registers = state[6];
  286. this.mixer_full_update();
  287. this.dummy_speaker_enabled = state[7];
  288. this.test_register = state[8];
  289. this.dsp_highspeed = state[9];
  290. this.dsp_stereo = state[10];
  291. this.dsp_16bit = state[11];
  292. this.dsp_signed = state[12];
  293. // this.dac_buffers = state[13];
  294. //state[14]
  295. this.dma_sample_count = state[15];
  296. this.dma_bytes_count = state[16];
  297. this.dma_bytes_left = state[17];
  298. this.dma_bytes_block = state[18];
  299. this.dma_irq = state[19];
  300. this.dma_channel = state[20];
  301. this.dma_channel_8bit = state[21];
  302. this.dma_channel_16bit = state[22];
  303. this.dma_autoinit = state[23];
  304. this.dma_buffer_uint8 = state[24];
  305. this.dma_waiting_transfer = state[25];
  306. this.dma_paused = state[26];
  307. this.sampling_rate = state[27];
  308. this.bytes_per_sample = state[28];
  309. this.e2_value = state[29];
  310. this.e2_count = state[30];
  311. this.asp_registers = state[31];
  312. // this.mpu_read_buffer = state[32];
  313. this.mpu_read_buffer_last_value = state[33];
  314. this.irq = state[34];
  315. this.irq_triggered = state[35];
  316. //state[36];
  317. this.dma_buffer = this.dma_buffer_uint8.buffer;
  318. this.dma_buffer_int8 = new Int8Array(this.dma_buffer);
  319. this.dma_buffer_int16 = new Int16Array(this.dma_buffer);
  320. this.dma_buffer_uint16 = new Uint16Array(this.dma_buffer);
  321. this.dma_syncbuffer = new v86util.SyncBuffer(this.dma_buffer);
  322. if(this.dma_paused)
  323. {
  324. this.bus.send("dac-disable");
  325. }
  326. else
  327. {
  328. this.bus.send("dac-enable");
  329. }
  330. };
  331. //
  332. // I/O handlers
  333. //
  334. SB16.prototype.port2x0_read = function()
  335. {
  336. dbg_log("220 read: fm music status port (unimplemented)", LOG_SB16);
  337. return 0xFF;
  338. };
  339. SB16.prototype.port2x1_read = function()
  340. {
  341. dbg_log("221 read: fm music data port (write only)", LOG_SB16);
  342. return 0xFF;
  343. };
  344. SB16.prototype.port2x2_read = function()
  345. {
  346. dbg_log("222 read: advanced fm music status port (unimplemented)", LOG_SB16);
  347. return 0xFF;
  348. };
  349. SB16.prototype.port2x3_read = function()
  350. {
  351. dbg_log("223 read: advanced music data port (write only)", LOG_SB16);
  352. return 0xFF;
  353. };
  354. // Mixer Address Port.
  355. SB16.prototype.port2x4_read = function()
  356. {
  357. dbg_log("224 read: mixer address port", LOG_SB16);
  358. return this.mixer_current_address;
  359. };
  360. // Mixer Data Port.
  361. SB16.prototype.port2x5_read = function()
  362. {
  363. dbg_log("225 read: mixer data port", LOG_SB16);
  364. return this.mixer_read(this.mixer_current_address);
  365. };
  366. SB16.prototype.port2x6_read = function()
  367. {
  368. dbg_log("226 read: (write only)", LOG_SB16);
  369. return 0xFF;
  370. };
  371. SB16.prototype.port2x7_read = function()
  372. {
  373. dbg_log("227 read: undocumented", LOG_SB16);
  374. return 0xFF;
  375. };
  376. SB16.prototype.port2x8_read = function()
  377. {
  378. dbg_log("228 read: fm music status port (unimplemented)", LOG_SB16);
  379. return 0xFF;
  380. };
  381. SB16.prototype.port2x9_read = function()
  382. {
  383. dbg_log("229 read: fm music data port (write only)", LOG_SB16);
  384. return 0xFF;
  385. };
  386. // Read Data.
  387. // Used to access in-bound DSP data.
  388. SB16.prototype.port2xA_read = function()
  389. {
  390. dbg_log("22A read: read data", LOG_SB16);
  391. if(this.read_buffer.length)
  392. {
  393. this.read_buffer_lastvalue = this.read_buffer.shift();
  394. }
  395. dbg_log(" <- " + this.read_buffer_lastvalue + " " + h(this.read_buffer_lastvalue) + " '" + String.fromCharCode(this.read_buffer_lastvalue) + "'", LOG_SB16);
  396. return this.read_buffer_lastvalue;
  397. };
  398. SB16.prototype.port2xB_read = function()
  399. {
  400. dbg_log("22B read: undocumented", LOG_SB16);
  401. return 0xFF;
  402. };
  403. // Write-Buffer Status.
  404. // Indicates whether the DSP is ready to accept commands or data.
  405. SB16.prototype.port2xC_read = function()
  406. {
  407. dbg_log("22C read: write-buffer status", LOG_SB16);
  408. // Always return ready (bit-7 set to low)
  409. return 0x7F;
  410. };
  411. SB16.prototype.port2xD_read = function()
  412. {
  413. dbg_log("22D read: undocumented", LOG_SB16);
  414. return 0xFF;
  415. };
  416. // Read-Buffer Status.
  417. // Indicates whether there is any in-bound data available for reading.
  418. // Also used to acknowledge DSP 8-bit interrupt.
  419. SB16.prototype.port2xE_read = function()
  420. {
  421. dbg_log("22E read: read-buffer status / irq 8bit ack.", LOG_SB16);
  422. if(this.irq_triggered[SB_IRQ_8BIT])
  423. {
  424. this.lower_irq(SB_IRQ_8BIT);
  425. }
  426. var ready = this.read_buffer.length && !this.dsp_highspeed;
  427. return (ready << 7) | 0x7F;
  428. };
  429. // DSP 16-bit interrupt acknowledgement.
  430. SB16.prototype.port2xF_read = function()
  431. {
  432. dbg_log("22F read: irq 16bit ack", LOG_SB16);
  433. this.lower_irq(SB_IRQ_16BIT);
  434. return 0;
  435. };
  436. // FM Address Port - primary register.
  437. SB16.prototype.port2x0_write = function(value)
  438. {
  439. dbg_log("220 write: (unimplemented) fm register 0 address = " + h(value), LOG_SB16);
  440. this.fm_current_address0 = 0;
  441. };
  442. // FM Data Port - primary register.
  443. SB16.prototype.port2x1_write = function(value)
  444. {
  445. dbg_log("221 write: (unimplemented) fm register 0 data = " + h(value), LOG_SB16);
  446. var handler = FM_HANDLERS[this.fm_current_address0];
  447. if(!handler)
  448. {
  449. handler = this.fm_default_write;
  450. }
  451. handler.call(this, value, 0, this.fm_current_address0);
  452. };
  453. // FM Address Port - secondary register.
  454. SB16.prototype.port2x2_write = function(value)
  455. {
  456. dbg_log("222 write: (unimplemented) fm register 1 address = " + h(value), LOG_SB16);
  457. this.fm_current_address1 = 0;
  458. };
  459. // FM Data Port - secondary register.
  460. SB16.prototype.port2x3_write = function(value)
  461. {
  462. dbg_log("223 write: (unimplemented) fm register 1 data =" + h(value), LOG_SB16);
  463. var handler = FM_HANDLERS[this.fm_current_address1];
  464. if(!handler)
  465. {
  466. handler = this.fm_default_write;
  467. }
  468. handler.call(this, value, 1, this.fm_current_address1);
  469. };
  470. // Mixer Address Port.
  471. SB16.prototype.port2x4_write = function(value)
  472. {
  473. dbg_log("224 write: mixer address = " + h(value), LOG_SB16);
  474. this.mixer_current_address = value;
  475. };
  476. // Mixer Data Port.
  477. SB16.prototype.port2x5_write = function(value)
  478. {
  479. dbg_log("225 write: mixer data = " + h(value), LOG_SB16);
  480. this.mixer_write(this.mixer_current_address, value);
  481. };
  482. // Reset.
  483. // Used to reset the DSP to its default state and to exit highspeed mode.
  484. SB16.prototype.port2x6_write = function(yesplease)
  485. {
  486. dbg_log("226 write: reset = " + h(yesplease), LOG_SB16);
  487. if(this.dsp_highspeed)
  488. {
  489. dbg_log(" -> exit highspeed", LOG_SB16);
  490. this.dsp_highspeed = false;
  491. }
  492. else if(yesplease)
  493. {
  494. dbg_log(" -> reset", LOG_SB16);
  495. this.dsp_reset();
  496. }
  497. // Signal completion.
  498. this.read_buffer.clear();
  499. this.read_buffer.push(0xAA);
  500. };
  501. SB16.prototype.port2x7_write = function(value)
  502. {
  503. dbg_log("227 write: undocumented", LOG_SB16);
  504. };
  505. SB16.prototype.port2x8_write = function(value)
  506. {
  507. dbg_log("228 write: fm music register port (unimplemented)", LOG_SB16);
  508. };
  509. SB16.prototype.port2x9_write = function(value)
  510. {
  511. dbg_log("229 write: fm music data port (unimplemented)", LOG_SB16);
  512. };
  513. SB16.prototype.port2xA_write = function(value)
  514. {
  515. dbg_log("22A write: dsp read data port (read only)", LOG_SB16);
  516. };
  517. SB16.prototype.port2xB_write = function(value)
  518. {
  519. dbg_log("22B write: undocumented", LOG_SB16);
  520. };
  521. // Write Command/Data.
  522. // Used to send commands or data to the DSP.
  523. SB16.prototype.port2xC_write = function(value)
  524. {
  525. dbg_log("22C write: write command/data", LOG_SB16);
  526. if(this.command === DSP_NO_COMMAND)
  527. {
  528. // New command.
  529. dbg_log("22C write: command = " + h(value), LOG_SB16);
  530. this.command = value;
  531. this.write_buffer.clear();
  532. this.command_size = DSP_COMMAND_SIZES[value];
  533. }
  534. else
  535. {
  536. // More data for current command.
  537. dbg_log("22C write: data: " + h(value), LOG_SB16);
  538. this.write_buffer.push(value);
  539. }
  540. // Perform command when we have all the needed data.
  541. if(this.write_buffer.length >= this.command_size)
  542. {
  543. this.command_do();
  544. }
  545. };
  546. SB16.prototype.port2xD_write = function(value)
  547. {
  548. dbg_log("22D write: undocumented", LOG_SB16);
  549. };
  550. SB16.prototype.port2xE_write = function(value)
  551. {
  552. dbg_log("22E write: dsp read buffer status (read only)", LOG_SB16);
  553. };
  554. SB16.prototype.port2xF_write = function(value)
  555. {
  556. dbg_log("22F write: undocumented", LOG_SB16);
  557. };
  558. // MPU UART Mode - Data Port
  559. SB16.prototype.port3x0_read = function()
  560. {
  561. dbg_log("330 read: mpu data", LOG_SB16);
  562. if(this.mpu_read_buffer.length)
  563. {
  564. this.mpu_read_buffer_lastvalue = this.mpu_read_buffer.shift();
  565. }
  566. dbg_log(" <- " + h(this.mpu_read_buffer_lastvalue), LOG_SB16);
  567. return this.mpu_read_buffer_lastvalue;
  568. };
  569. SB16.prototype.port3x0_write = function(value)
  570. {
  571. dbg_log("330 write: mpu data (unimplemented) : " + h(value), LOG_SB16);
  572. };
  573. // MPU UART Mode - Status Port
  574. SB16.prototype.port3x1_read = function()
  575. {
  576. dbg_log("331 read: mpu status", LOG_SB16);
  577. var status = 0;
  578. status |= 0x40 * 0; // Output Ready
  579. status |= 0x80 * !this.mpu_read_buffer.length; // Input Ready
  580. return status;
  581. };
  582. // MPU UART Mode - Command Port
  583. SB16.prototype.port3x1_write = function(value)
  584. {
  585. dbg_log("331 write: mpu command: " + h(value), LOG_SB16);
  586. if(value == 0xFF)
  587. {
  588. // Command acknowledge.
  589. this.mpu_read_buffer.clear();
  590. this.mpu_read_buffer.push(0xFE);
  591. }
  592. };
  593. //
  594. // DSP command handlers
  595. //
  596. SB16.prototype.command_do = function()
  597. {
  598. var handler = DSP_COMMAND_HANDLERS[this.command];
  599. if(!handler)
  600. {
  601. handler = this.dsp_default_handler;
  602. }
  603. handler.call(this);
  604. // Reset Inputs.
  605. this.command = DSP_NO_COMMAND;
  606. this.command_size = 0;
  607. this.write_buffer.clear();
  608. };
  609. SB16.prototype.dsp_default_handler = function()
  610. {
  611. dbg_log("Unhandled command: " + h(this.command), LOG_SB16);
  612. };
  613. /**
  614. * @param {Array} commands
  615. * @param {number} size
  616. * @param {function()=} handler
  617. */
  618. function register_dsp_command(commands, size, handler)
  619. {
  620. if(!handler)
  621. {
  622. handler = SB16.prototype.dsp_default_handler;
  623. }
  624. for(var i = 0; i < commands.length; i++)
  625. {
  626. DSP_COMMAND_SIZES[commands[i]] = size;
  627. DSP_COMMAND_HANDLERS[commands[i]] = handler;
  628. }
  629. }
  630. function any_first_digit(base)
  631. {
  632. var commands = [];
  633. for(var i = 0; i < 16; i++)
  634. {
  635. commands.push(base + i);
  636. }
  637. return commands;
  638. }
  639. // ASP set register
  640. register_dsp_command([0x0E], 2, function()
  641. {
  642. this.asp_registers[this.write_buffer.shift()] = this.write_buffer.shift();
  643. });
  644. // ASP get register
  645. register_dsp_command([0x0F], 1, function()
  646. {
  647. this.read_buffer.clear();
  648. this.read_buffer.push(this.asp_registers[this.write_buffer.shift()]);
  649. });
  650. // 8-bit direct mode single byte digitized sound output.
  651. register_dsp_command([0x10], 1, function()
  652. {
  653. var value = audio_normalize(this.write_buffer.shift(), 127.5, -1);
  654. this.dac_buffers[0].push(value);
  655. this.dac_buffers[1].push(value);
  656. this.bus.send("dac-enable");
  657. });
  658. // 8-bit single-cycle DMA mode digitized sound output.
  659. register_dsp_command([0x14, 0x15], 2, function()
  660. {
  661. this.dma_irq = SB_IRQ_8BIT;
  662. this.dma_channel = this.dma_channel_8bit;
  663. this.dma_autoinit = false;
  664. this.dsp_signed = false;
  665. this.dsp_16bit = false;
  666. this.dsp_highspeed = false;
  667. this.dma_transfer_size_set();
  668. this.dma_transfer_start();
  669. });
  670. // Creative 8-bit to 2-bit ADPCM single-cycle DMA mode digitized sound output.
  671. register_dsp_command([0x16], 2);
  672. // Creative 8-bit to 2-bit ADPCM single-cycle DMA mode digitzed sound output
  673. // with reference byte.
  674. register_dsp_command([0x17], 2);
  675. // 8-bit auto-init DMA mode digitized sound output.
  676. register_dsp_command([0x1C], 0, function()
  677. {
  678. this.dma_irq = SB_IRQ_8BIT;
  679. this.dma_channel = this.dma_channel_8bit;
  680. this.dma_autoinit = true;
  681. this.dsp_signed = false;
  682. this.dsp_16bit = false;
  683. this.dsp_highspeed = false;
  684. this.dma_transfer_start();
  685. });
  686. // Creative 8-bit to 2-bit ADPCM auto-init DMA mode digitized sound output
  687. // with reference byte.
  688. register_dsp_command([0x1F], 0);
  689. // 8-bit direct mode single byte digitized sound input.
  690. register_dsp_command([0x20], 0, function()
  691. {
  692. // Fake silent input.
  693. this.read_buffer.clear();
  694. this.read_buffer.push(0x7f);
  695. });
  696. // 8-bit single-cycle DMA mode digitized sound input.
  697. register_dsp_command([0x24], 2);
  698. // 8-bit auto-init DMA mode digitized sound input.
  699. register_dsp_command([0x2C], 0);
  700. // Polling mode MIDI input.
  701. register_dsp_command([0x30], 0);
  702. // Interrupt mode MIDI input.
  703. register_dsp_command([0x31], 0);
  704. // UART polling mode MIDI I/O.
  705. register_dsp_command([0x34], 0);
  706. // UART interrupt mode MIDI I/O.
  707. register_dsp_command([0x35], 0);
  708. // UART polling mode MIDI I/O with time stamping.
  709. register_dsp_command([0x36], 0);
  710. // UART interrupt mode MIDI I/O with time stamping.
  711. register_dsp_command([0x37], 0);
  712. // MIDI output.
  713. register_dsp_command([0x38], 0);
  714. // Set digitized sound transfer Time Constant.
  715. register_dsp_command([0x40], 1, function()
  716. {
  717. // Note: bTimeConstant = 256 * time constant
  718. this.sampling_rate_change(
  719. 1000000 / (256 - this.write_buffer.shift()) / this.get_channel_count()
  720. );
  721. });
  722. // Set digitized sound output sampling rate.
  723. // Set digitized sound input sampling rate.
  724. register_dsp_command([0x41, 0x42], 2, function()
  725. {
  726. this.sampling_rate_change((this.write_buffer.shift() << 8) | this.write_buffer.shift());
  727. });
  728. // Set DSP block transfer size.
  729. register_dsp_command([0x48], 2, function()
  730. {
  731. // TODO: should be in bytes, but if this is only used
  732. // for 8 bit transfers, then this number is the same
  733. // as number of samples?
  734. // Wrong: e.g. stereo requires two bytes per sample.
  735. this.dma_transfer_size_set();
  736. });
  737. // Creative 8-bit to 4-bit ADPCM single-cycle DMA mode digitized sound output.
  738. register_dsp_command([0x74], 2);
  739. // Creative 8-bit to 4-bit ADPCM single-cycle DMA mode digitized sound output
  740. // with referene byte.
  741. register_dsp_command([0x75], 2);
  742. // Creative 8-bit to 3-bit ADPCM single-cycle DMA mode digitized sound output.
  743. register_dsp_command([0x76], 2);
  744. // Creative 8-bit to 3-bit ADPCM single-cycle DMA mode digitized sound output
  745. // with referene byte.
  746. register_dsp_command([0x77], 2);
  747. // Creative 8-bit to 4-bit ADPCM auto-init DMA mode digitized sound output
  748. // with reference byte.
  749. register_dsp_command([0x7D], 0);
  750. // Creative 8-bit to 3-bit ADPCM auto-init DMA mode digitized sound output
  751. // with reference byte.
  752. register_dsp_command([0x7F], 0);
  753. // Pause DAC for a duration.
  754. register_dsp_command([0x80], 2);
  755. // 8-bit high-speed auto-init DMA mode digitized sound output.
  756. register_dsp_command([0x90], 0, function()
  757. {
  758. this.dma_irq = SB_IRQ_8BIT;
  759. this.dma_channel = this.dma_channel_8bit;
  760. this.dma_autoinit = true;
  761. this.dsp_signed = false;
  762. this.dsp_highspeed = true;
  763. this.dsp_16bit = false;
  764. this.dma_transfer_start();
  765. });
  766. // 8-bit high-speed single-cycle DMA mode digitized sound input.
  767. register_dsp_command([0x91], 0);
  768. // 8-bit high-speed auto-init DMA mode digitized sound input.
  769. register_dsp_command([0x98], 0);
  770. // 8-bit high-speed single-cycle DMA mode digitized sound input.
  771. register_dsp_command([0x99], 0);
  772. // Set input mode to mono.
  773. register_dsp_command([0xA0], 0);
  774. // Set input mode to stereo.
  775. register_dsp_command([0xA8], 0);
  776. // Program 16-bit DMA mode digitized sound I/O.
  777. register_dsp_command(any_first_digit(0xB0), 3, function()
  778. {
  779. if(this.command & (1 << 3))
  780. {
  781. // Analogue to digital not implemented.
  782. this.dsp_default_handler();
  783. return;
  784. }
  785. var mode = this.write_buffer.shift();
  786. this.dma_irq = SB_IRQ_16BIT;
  787. this.dma_channel = this.dma_channel_16bit;
  788. this.dma_autoinit = !!(this.command & (1 << 2));
  789. this.dsp_signed = !!(mode & (1 << 4));
  790. this.dsp_stereo = !!(mode & (1 << 5));
  791. this.dsp_16bit = true;
  792. this.dma_transfer_size_set();
  793. this.dma_transfer_start();
  794. });
  795. // Program 8-bit DMA mode digitized sound I/O.
  796. register_dsp_command(any_first_digit(0xC0), 3, function()
  797. {
  798. if(this.command & (1 << 3))
  799. {
  800. // Analogue to digital not implemented.
  801. this.dsp_default_handler();
  802. return;
  803. }
  804. var mode = this.write_buffer.shift();
  805. this.dma_irq = SB_IRQ_8BIT;
  806. this.dma_channel = this.dma_channel_8bit;
  807. this.dma_autoinit = !!(this.command & (1 << 2));
  808. this.dsp_signed = !!(mode & (1 << 4));
  809. this.dsp_stereo = !!(mode & (1 << 5));
  810. this.dsp_16bit = false;
  811. this.dma_transfer_size_set();
  812. this.dma_transfer_start();
  813. });
  814. // Pause 8-bit DMA mode digitized sound I/O.
  815. register_dsp_command([0xD0], 0, function()
  816. {
  817. this.dma_paused = true;
  818. this.bus.send("dac-disable");
  819. });
  820. // Turn on speaker.
  821. // Documented to have no effect on SB16.
  822. register_dsp_command([0xD1], 0, function()
  823. {
  824. this.dummy_speaker_enabled = true;
  825. });
  826. // Turn off speaker.
  827. // Documented to have no effect on SB16.
  828. register_dsp_command([0xD3], 0, function()
  829. {
  830. this.dummy_speaker_enabled = false;
  831. });
  832. // Continue 8-bit DMA mode digitized sound I/O.
  833. register_dsp_command([0xD4], 0, function()
  834. {
  835. this.dma_paused = false;
  836. this.bus.send("dac-enable");
  837. });
  838. // Pause 16-bit DMA mode digitized sound I/O.
  839. register_dsp_command([0xD5], 0, function()
  840. {
  841. this.dma_paused = true;
  842. this.bus.send("dac-disable");
  843. });
  844. // Continue 16-bit DMA mode digitized sound I/O.
  845. register_dsp_command([0xD6], 0, function()
  846. {
  847. this.dma_paused = false;
  848. this.bus.send("dac-enable");
  849. });
  850. // Get speaker status.
  851. register_dsp_command([0xD8], 0, function()
  852. {
  853. this.read_buffer.clear();
  854. this.read_buffer.push(this.dummy_speaker_enabled * 0xFF);
  855. });
  856. // Exit 16-bit auto-init DMA mode digitized sound I/O.
  857. // Exit 8-bit auto-init mode digitized sound I/O.
  858. register_dsp_command([0xD9, 0xDA], 0, function()
  859. {
  860. this.dma_autoinit = false;
  861. });
  862. // DSP identification
  863. register_dsp_command([0xE0], 1, function()
  864. {
  865. this.read_buffer.clear();
  866. this.read_buffer.push(~this.write_buffer.shift());
  867. });
  868. // Get DSP version number.
  869. register_dsp_command([0xE1], 0, function()
  870. {
  871. this.read_buffer.clear();
  872. this.read_buffer.push(4);
  873. this.read_buffer.push(5);
  874. });
  875. // DMA identification.
  876. register_dsp_command([0xE2], 1);
  877. // Get DSP copyright.
  878. register_dsp_command([0xE3], 0, function()
  879. {
  880. this.read_buffer.clear();
  881. for(var i = 0; i < DSP_COPYRIGHT.length; i++)
  882. {
  883. this.read_buffer.push(DSP_COPYRIGHT.charCodeAt(i));
  884. }
  885. // Null terminator.
  886. this.read_buffer.push(0);
  887. });
  888. // Write test register.
  889. register_dsp_command([0xE4], 1, function()
  890. {
  891. this.test_register = this.write_buffer.shift();
  892. });
  893. // Read test register.
  894. register_dsp_command([0xE8], 0, function()
  895. {
  896. this.read_buffer.clear();
  897. this.read_buffer.push(this.test_register);
  898. });
  899. // Trigger IRQ
  900. register_dsp_command([0xF2, 0xF3], 0, function()
  901. {
  902. this.raise_irq();
  903. });
  904. // ASP - unknown function
  905. var SB_F9 = new Uint8Array(256);
  906. SB_F9[0x0E] = 0xFF;
  907. SB_F9[0x0F] = 0x07;
  908. SB_F9[0x37] = 0x38;
  909. register_dsp_command([0xF9], 1, function()
  910. {
  911. var input = this.write_buffer.shift();
  912. dbg_log("dsp 0xf9: unknown function. input: " + input, LOG_SB16);
  913. this.read_buffer.clear();
  914. this.read_buffer.push(SB_F9[input]);
  915. });
  916. //
  917. // Mixer Handlers (CT1745)
  918. //
  919. SB16.prototype.mixer_read = function(address)
  920. {
  921. var handler = MIXER_READ_HANDLERS[address];
  922. var data;
  923. if(handler)
  924. {
  925. data = handler.call(this);
  926. }
  927. else
  928. {
  929. data = this.mixer_registers[address];
  930. dbg_log("unhandled mixer register read. addr:" + h(address) + " data:" + h(data), LOG_SB16);
  931. }
  932. return data;
  933. };
  934. SB16.prototype.mixer_write = function(address, data)
  935. {
  936. var handler = MIXER_WRITE_HANDLERS[address];
  937. if(handler)
  938. {
  939. handler.call(this, data);
  940. }
  941. else
  942. {
  943. dbg_log("unhandled mixer register write. addr:" + h(address) + " data:" + h(data), LOG_SB16);
  944. }
  945. };
  946. SB16.prototype.mixer_default_read = function()
  947. {
  948. dbg_log("mixer register read. addr:" + h(this.mixer_current_address), LOG_SB16);
  949. return this.mixer_registers[this.mixer_current_address];
  950. };
  951. SB16.prototype.mixer_default_write = function(data)
  952. {
  953. dbg_log("mixer register write. addr:" + h(this.mixer_current_address) + " data:" + h(data), LOG_SB16);
  954. this.mixer_registers[this.mixer_current_address] = data;
  955. };
  956. SB16.prototype.mixer_reset = function()
  957. {
  958. // Values intentionally in decimal.
  959. // Default values available at
  960. // https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf
  961. this.mixer_registers[0x04] = 12 << 4 | 12;
  962. this.mixer_registers[0x22] = 12 << 4 | 12;
  963. this.mixer_registers[0x26] = 12 << 4 | 12;
  964. this.mixer_registers[0x28] = 0;
  965. this.mixer_registers[0x2E] = 0;
  966. this.mixer_registers[0x0A] = 0;
  967. this.mixer_registers[0x30] = 24 << 3;
  968. this.mixer_registers[0x31] = 24 << 3;
  969. this.mixer_registers[0x32] = 24 << 3;
  970. this.mixer_registers[0x33] = 24 << 3;
  971. this.mixer_registers[0x34] = 24 << 3;
  972. this.mixer_registers[0x35] = 24 << 3;
  973. this.mixer_registers[0x36] = 0;
  974. this.mixer_registers[0x37] = 0;
  975. this.mixer_registers[0x38] = 0;
  976. this.mixer_registers[0x39] = 0;
  977. this.mixer_registers[0x3B] = 0;
  978. this.mixer_registers[0x3C] = 0x1F;
  979. this.mixer_registers[0x3D] = 0x15;
  980. this.mixer_registers[0x3E] = 0x0B;
  981. this.mixer_registers[0x3F] = 0;
  982. this.mixer_registers[0x40] = 0;
  983. this.mixer_registers[0x41] = 0;
  984. this.mixer_registers[0x42] = 0;
  985. this.mixer_registers[0x43] = 0;
  986. this.mixer_registers[0x44] = 8 << 4;
  987. this.mixer_registers[0x45] = 8 << 4;
  988. this.mixer_registers[0x46] = 8 << 4;
  989. this.mixer_registers[0x47] = 8 << 4;
  990. this.mixer_full_update();
  991. };
  992. SB16.prototype.mixer_full_update = function()
  993. {
  994. // Start at 1. Don't re-reset.
  995. for(var i = 1; i < this.mixer_registers.length; i++)
  996. {
  997. if(MIXER_REGISTER_IS_LEGACY[i])
  998. {
  999. // Legacy registers are actually mapped to other register locations. Update
  1000. // using the new registers rather than the legacy registers.
  1001. continue;
  1002. }
  1003. this.mixer_write(i, this.mixer_registers[i]);
  1004. }
  1005. };
  1006. /**
  1007. * @param{number} address
  1008. * @param{function():number=} handler
  1009. */
  1010. function register_mixer_read(address, handler)
  1011. {
  1012. if(!handler)
  1013. {
  1014. handler = SB16.prototype.mixer_default_read;
  1015. }
  1016. MIXER_READ_HANDLERS[address] = handler;
  1017. }
  1018. /**
  1019. * @param{number} address
  1020. * @param{function(number)=} handler
  1021. */
  1022. function register_mixer_write(address, handler)
  1023. {
  1024. if(!handler)
  1025. {
  1026. handler = SB16.prototype.mixer_default_write;
  1027. }
  1028. MIXER_WRITE_HANDLERS[address] = handler;
  1029. }
  1030. // Legacy registers map each nibble to the last 4 bits of the new registers
  1031. function register_mixer_legacy(address_old, address_new_left, address_new_right)
  1032. {
  1033. MIXER_REGISTER_IS_LEGACY[address_old] = 1;
  1034. /** @this {SB16} */
  1035. MIXER_READ_HANDLERS[address_old] = function()
  1036. {
  1037. var left = this.mixer_registers[address_new_left] & 0xF0;
  1038. var right = this.mixer_registers[address_new_right] >>> 4;
  1039. return left | right;
  1040. };
  1041. /** @this {SB16} */
  1042. MIXER_WRITE_HANDLERS[address_old] = function(data)
  1043. {
  1044. this.mixer_registers[address_old] = data;
  1045. var prev_left = this.mixer_registers[address_new_left];
  1046. var prev_right = this.mixer_registers[address_new_right];
  1047. var left = (data & 0xF0) | (prev_left & 0x0F);
  1048. var right = (data << 4 & 0xF0) | (prev_right & 0x0F);
  1049. this.mixer_write(address_new_left, left);
  1050. this.mixer_write(address_new_right, right);
  1051. };
  1052. }
  1053. /**
  1054. * @param {number} address
  1055. * @param {number} mixer_source
  1056. * @param {number} channel
  1057. */
  1058. function register_mixer_volume(address, mixer_source, channel)
  1059. {
  1060. MIXER_READ_HANDLERS[address] = SB16.prototype.mixer_default_read;
  1061. /** @this {SB16} */
  1062. MIXER_WRITE_HANDLERS[address] = function(data)
  1063. {
  1064. this.mixer_registers[address] = data;
  1065. this.bus.send("mixer-volume",
  1066. [
  1067. mixer_source,
  1068. channel,
  1069. (data >>> 2) - 62
  1070. ]);
  1071. };
  1072. }
  1073. // Reset.
  1074. register_mixer_read(0x00, function()
  1075. {
  1076. this.mixer_reset();
  1077. return 0;
  1078. });
  1079. register_mixer_write(0x00);
  1080. // Legacy Voice Volume Left/Right.
  1081. register_mixer_legacy(0x04, 0x32, 0x33);
  1082. // Legacy Mic Volume. TODO.
  1083. //register_mixer_read(0x0A);
  1084. //register_mixer_write(0x0A, function(data)
  1085. //{
  1086. // this.mixer_registers[0x0A] = data;
  1087. // var prev = this.mixer_registers[0x3A];
  1088. // this.mixer_write(0x3A, data << 5 | (prev & 0x0F));
  1089. //});
  1090. // Legacy Master Volume Left/Right.
  1091. register_mixer_legacy(0x22, 0x30, 0x31);
  1092. // Legacy Midi Volume Left/Right.
  1093. register_mixer_legacy(0x26, 0x34, 0x35);
  1094. // Legacy CD Volume Left/Right.
  1095. register_mixer_legacy(0x28, 0x36, 0x37);
  1096. // Legacy Line Volume Left/Right.
  1097. register_mixer_legacy(0x2E, 0x38, 0x39);
  1098. // Master Volume Left.
  1099. register_mixer_volume(0x30, MIXER_SRC_MASTER, MIXER_CHANNEL_LEFT);
  1100. // Master Volume Right.
  1101. register_mixer_volume(0x31, MIXER_SRC_MASTER, MIXER_CHANNEL_RIGHT);
  1102. // Voice Volume Left.
  1103. register_mixer_volume(0x32, MIXER_SRC_DAC, MIXER_CHANNEL_LEFT);
  1104. // Voice Volume Right.
  1105. register_mixer_volume(0x33, MIXER_SRC_DAC, MIXER_CHANNEL_RIGHT);
  1106. // MIDI Volume Left. TODO.
  1107. //register_mixer_volume(0x34, MIXER_SRC_SYNTH, MIXER_CHANNEL_LEFT);
  1108. // MIDI Volume Right. TODO.
  1109. //register_mixer_volume(0x35, MIXER_SRC_SYNTH, MIXER_CHANNEL_RIGHT);
  1110. // CD Volume Left. TODO.
  1111. //register_mixer_volume(0x36, MIXER_SRC_CD, MIXER_CHANNEL_LEFT);
  1112. // CD Volume Right. TODO.
  1113. //register_mixer_volume(0x37, MIXER_SRC_CD, MIXER_CHANNEL_RIGHT);
  1114. // Line Volume Left. TODO.
  1115. //register_mixer_volume(0x38, MIXER_SRC_LINE, MIXER_CHANNEL_LEFT);
  1116. // Line Volume Right. TODO.
  1117. //register_mixer_volume(0x39, MIXER_SRC_LINE, MIXER_CHANNEL_RIGHT);
  1118. // Mic Volume. TODO.
  1119. //register_mixer_volume(0x3A, MIXER_SRC_MIC, MIXER_CHANNEL_BOTH);
  1120. // PC Speaker Volume.
  1121. register_mixer_read(0x3B);
  1122. register_mixer_write(0x3B, function(data)
  1123. {
  1124. this.mixer_registers[0x3B] = data;
  1125. this.bus.send("mixer-volume", [MIXER_SRC_PCSPEAKER, MIXER_CHANNEL_BOTH, (data >>> 6) * 6 - 18]);
  1126. });
  1127. // Output Mixer Switches. TODO.
  1128. //register_mixer_read(0x3C);
  1129. //register_mixer_write(0x3C, function(data)
  1130. //{
  1131. // this.mixer_registers[0x3C] = data;
  1132. //
  1133. // if(data & 0x01) this.bus.send("mixer-connect", [MIXER_SRC_MIC, MIXER_CHANNEL_BOTH]);
  1134. // else this.bus.send("mixer-disconnect", [MIXER_SRC_MIC, MIXER_CHANNEL_BOTH]);
  1135. //
  1136. // if(data & 0x02) this.bus.send("mixer-connect", [MIXER_SRC_CD, MIXER_CHANNEL_RIGHT]);
  1137. // else this.bus.send("mixer-disconnect", [MIXER_SRC_CD, MIXER_CHANNEL_RIGHT]);
  1138. //
  1139. // if(data & 0x04) this.bus.send("mixer-connect", [MIXER_SRC_CD, MIXER_CHANNEL_LEFT]);
  1140. // else this.bus.send("mixer-disconnect", [MIXER_SRC_CD, MIXER_CHANNEL_LEFT]);
  1141. //
  1142. // if(data & 0x08) this.bus.send("mixer-connect", [MIXER_SRC_LINE, MIXER_CHANNEL_RIGHT]);
  1143. // else this.bus.send("mixer-disconnect", [MIXER_SRC_LINE, MIXER_CHANNEL_RIGHT]);
  1144. //
  1145. // if(data & 0x10) this.bus.send("mixer-connect", [MIXER_SRC_LINE, MIXER_CHANNEL_LEFT]);
  1146. // else this.bus.send("mixer-disconnect", [MIXER_SRC_LINE, MIXER_CHANNEL_LEFT]);
  1147. //});
  1148. // Input Mixer Left Switches. TODO.
  1149. //register_mixer_read(0x3D);
  1150. //register_mixer_write(0x3D);
  1151. // Input Mixer Right Switches. TODO.
  1152. //register_mixer_read(0x3E);
  1153. //register_mixer_write(0x3E);
  1154. // Input Gain Left. TODO.
  1155. //register_mixer_read(0x3F);
  1156. //register_mixer_write(0x3F);
  1157. // Input Gain Right. TODO.
  1158. //register_mixer_read(0x40);
  1159. //register_mixer_write(0x40);
  1160. // Output Gain Left.
  1161. register_mixer_read(0x41);
  1162. register_mixer_write(0x41, function(data)
  1163. {
  1164. this.mixer_registers[0x41] = data;
  1165. this.bus.send("mixer-gain-left", (data >>> 6) * 6);
  1166. });
  1167. // Output Gain Right.
  1168. register_mixer_read(0x42);
  1169. register_mixer_write(0x42, function(data)
  1170. {
  1171. this.mixer_registers[0x42] = data;
  1172. this.bus.send("mixer-gain-right", (data >>> 6) * 6);
  1173. });
  1174. // Mic AGC. TODO.
  1175. //register_mixer_read(0x43);
  1176. //register_mixer_write(0x43);
  1177. // Treble Left.
  1178. register_mixer_read(0x44);
  1179. register_mixer_write(0x44, function(data)
  1180. {
  1181. this.mixer_registers[0x44] = data;
  1182. data >>>= 3;
  1183. this.bus.send("mixer-treble-left", data - (data < 16 ? 14 : 16));
  1184. });
  1185. // Treble Right.
  1186. register_mixer_read(0x45);
  1187. register_mixer_write(0x45, function(data)
  1188. {
  1189. this.mixer_registers[0x45] = data;
  1190. data >>>= 3;
  1191. this.bus.send("mixer-treble-right", data - (data < 16 ? 14 : 16));
  1192. });
  1193. // Bass Left.
  1194. register_mixer_read(0x46);
  1195. register_mixer_write(0x46, function(data)
  1196. {
  1197. this.mixer_registers[0x46] = data;
  1198. data >>>= 3;
  1199. this.bus.send("mixer-bass-right", data - (data < 16 ? 14 : 16));
  1200. });
  1201. // Bass Right.
  1202. register_mixer_read(0x47);
  1203. register_mixer_write(0x47, function(data)
  1204. {
  1205. this.mixer_registers[0x47] = data;
  1206. data >>>= 3;
  1207. this.bus.send("mixer-bass-right", data - (data < 16 ? 14 : 16));
  1208. });
  1209. // IRQ Select.
  1210. register_mixer_read(0x80, function()
  1211. {
  1212. switch(this.irq)
  1213. {
  1214. case SB_IRQ2: return 0x1;
  1215. case SB_IRQ5: return 0x2;
  1216. case SB_IRQ7: return 0x4;
  1217. case SB_IRQ10: return 0x8;
  1218. default: return 0x0;
  1219. }
  1220. });
  1221. register_mixer_write(0x80, function(bits)
  1222. {
  1223. if(bits & 0x1) this.irq = SB_IRQ2;
  1224. if(bits & 0x2) this.irq = SB_IRQ5;
  1225. if(bits & 0x4) this.irq = SB_IRQ7;
  1226. if(bits & 0x8) this.irq = SB_IRQ10;
  1227. });
  1228. // DMA Select.
  1229. register_mixer_read(0x81, function()
  1230. {
  1231. var ret = 0;
  1232. switch(this.dma_channel_8bit)
  1233. {
  1234. case SB_DMA0: ret |= 0x1; break;
  1235. case SB_DMA1: ret |= 0x2; break;
  1236. // Channel 2 is hardwired to floppy disk.
  1237. case SB_DMA3: ret |= 0x8; break;
  1238. }
  1239. switch(this.dma_channel_16bit)
  1240. {
  1241. // Channel 4 cannot be used.
  1242. case SB_DMA5: ret |= 0x20; break;
  1243. case SB_DMA6: ret |= 0x40; break;
  1244. case SB_DMA7: ret |= 0x80; break;
  1245. }
  1246. return ret;
  1247. });
  1248. register_mixer_write(0x81, function(bits)
  1249. {
  1250. if(bits & 0x1) this.dma_channel_8bit = SB_DMA0;
  1251. if(bits & 0x2) this.dma_channel_8bit = SB_DMA1;
  1252. if(bits & 0x8) this.dma_channel_8bit = SB_DMA3;
  1253. if(bits & 0x20) this.dma_channel_16bit = SB_DMA5;
  1254. if(bits & 0x40) this.dma_channel_16bit = SB_DMA6;
  1255. if(bits & 0x80) this.dma_channel_16bit = SB_DMA7;
  1256. });
  1257. // IRQ Status.
  1258. register_mixer_read(0x82, function()
  1259. {
  1260. var ret = 0x20;
  1261. for(var i = 0; i < 16; i++)
  1262. {
  1263. ret |= i * this.irq_triggered[i];
  1264. }
  1265. return ret;
  1266. });
  1267. //
  1268. // FM Handlers
  1269. //
  1270. SB16.prototype.fm_default_write = function(data, register, address)
  1271. {
  1272. dbg_log("unhandled fm register write. addr:" + register + "|" + h(address) + " data:" + h(data), LOG_SB16);
  1273. // No need to save into a dummy register as the registers are write-only.
  1274. };
  1275. /**
  1276. * @param{Array} addresses
  1277. * @param{function(number, number, number)=} handler
  1278. */
  1279. function register_fm_write(addresses, handler)
  1280. {
  1281. if(!handler)
  1282. {
  1283. handler = SB16.prototype.fm_default_write;
  1284. }
  1285. for(var i = 0; i < addresses.length; i++)
  1286. {
  1287. FM_HANDLERS[addresses[i]] = handler;
  1288. }
  1289. }
  1290. function between(start, end)
  1291. {
  1292. var a = [];
  1293. for(var i = start; i <= end; i++)
  1294. {
  1295. a.push(i);
  1296. }
  1297. return a;
  1298. }
  1299. /** @const */ var SB_FM_OPERATORS_BY_OFFSET = new Uint8Array(32);
  1300. SB_FM_OPERATORS_BY_OFFSET[0x00] = 0;
  1301. SB_FM_OPERATORS_BY_OFFSET[0x01] = 1;
  1302. SB_FM_OPERATORS_BY_OFFSET[0x02] = 2;
  1303. SB_FM_OPERATORS_BY_OFFSET[0x03] = 3;
  1304. SB_FM_OPERATORS_BY_OFFSET[0x04] = 4;
  1305. SB_FM_OPERATORS_BY_OFFSET[0x05] = 5;
  1306. SB_FM_OPERATORS_BY_OFFSET[0x08] = 6;
  1307. SB_FM_OPERATORS_BY_OFFSET[0x09] = 7;
  1308. SB_FM_OPERATORS_BY_OFFSET[0x0A] = 8;
  1309. SB_FM_OPERATORS_BY_OFFSET[0x0B] = 9;
  1310. SB_FM_OPERATORS_BY_OFFSET[0x0C] = 10;
  1311. SB_FM_OPERATORS_BY_OFFSET[0x0D] = 11;
  1312. SB_FM_OPERATORS_BY_OFFSET[0x10] = 12;
  1313. SB_FM_OPERATORS_BY_OFFSET[0x11] = 13;
  1314. SB_FM_OPERATORS_BY_OFFSET[0x12] = 14;
  1315. SB_FM_OPERATORS_BY_OFFSET[0x13] = 15;
  1316. SB_FM_OPERATORS_BY_OFFSET[0x14] = 16;
  1317. SB_FM_OPERATORS_BY_OFFSET[0x15] = 17;
  1318. function get_fm_operator(register, offset)
  1319. {
  1320. return register * 18 + SB_FM_OPERATORS_BY_OFFSET[offset];
  1321. }
  1322. register_fm_write([0x01], function(bits, register, address)
  1323. {
  1324. this.fm_waveform_select_enable[register] = bits & 0x20 > 0;
  1325. this.fm_update_waveforms();
  1326. });
  1327. // Timer 1 Count.
  1328. register_fm_write([0x02]);
  1329. // Timer 2 Count.
  1330. register_fm_write([0x03]);
  1331. register_fm_write([0x04], function(bits, register, address)
  1332. {
  1333. switch(register)
  1334. {
  1335. case 0:
  1336. // if(bits & 0x80)
  1337. // {
  1338. // // IQR Reset
  1339. // }
  1340. // else
  1341. // {
  1342. // // Timer masks and on/off
  1343. // }
  1344. break;
  1345. case 1:
  1346. // Four-operator enable
  1347. break;
  1348. }
  1349. });
  1350. register_fm_write([0x05], function(bits, register, address)
  1351. {
  1352. if(register === 0)
  1353. {
  1354. // No registers documented here.
  1355. this.fm_default_write(bits, register, address);
  1356. return;
  1357. }
  1358. // OPL3 Mode Enable
  1359. });
  1360. register_fm_write([0x08], function(bits, register, address)
  1361. {
  1362. // Composite sine wave on/off
  1363. // Note select (keyboard split selection method)
  1364. });
  1365. register_fm_write(between(0x20, 0x35), function(bits, register, address)
  1366. {
  1367. var operator = get_fm_operator(register, address - 0x20);
  1368. // Tremolo
  1369. // Vibrato
  1370. // Sustain
  1371. // KSR Envelope Scaling
  1372. // Frequency Multiplication Factor
  1373. });
  1374. register_fm_write(between(0x40, 0x55), function(bits, register, address)
  1375. {
  1376. var operator = get_fm_operator(register, address - 0x40);
  1377. // Key Scale Level
  1378. // Output Level
  1379. });
  1380. register_fm_write(between(0x60, 0x75), function(bits, register, address)
  1381. {
  1382. var operator = get_fm_operator(register, address - 0x60);
  1383. // Attack Rate
  1384. // Decay Rate
  1385. });
  1386. register_fm_write(between(0x80, 0x95), function(bits, register, address)
  1387. {
  1388. var operator = get_fm_operator(register, address - 0x80);
  1389. // Sustain Level
  1390. // Release Rate
  1391. });
  1392. register_fm_write(between(0xA0, 0xA8), function(bits, register, address)
  1393. {
  1394. var channel = address - 0xA0;
  1395. // Frequency Number (Lower 8 bits)
  1396. });
  1397. register_fm_write(between(0xB0, 0xB8), function(bits, register, address)
  1398. {
  1399. // Key-On
  1400. // Block Number
  1401. // Frequency Number (Higher 2 bits)
  1402. });
  1403. register_fm_write([0xBD], function(bits, register, address)
  1404. {
  1405. // Tremelo Depth
  1406. // Vibrato Depth
  1407. // Percussion Mode
  1408. // Bass Drum Key-On
  1409. // Snare Drum Key-On
  1410. // Tom-Tom Key-On
  1411. // Cymbal Key-On
  1412. // Hi-Hat Key-On
  1413. });
  1414. register_fm_write(between(0xC0, 0xC8), function(bits, register, address)
  1415. {
  1416. // Right Speaker Enable
  1417. // Left Speaker Enable
  1418. // Feedback Modulation Factor
  1419. // Synthesis Type
  1420. });
  1421. register_fm_write(between(0xE0, 0xF5), function(bits, register, address)
  1422. {
  1423. var operator = get_fm_operator(register, address - 0xE0);
  1424. // Waveform Select
  1425. });
  1426. //
  1427. // FM behaviours
  1428. //
  1429. SB16.prototype.fm_update_waveforms = function()
  1430. {
  1431. // To be implemented.
  1432. };
  1433. //
  1434. // General behaviours
  1435. //
  1436. SB16.prototype.sampling_rate_change = function(rate)
  1437. {
  1438. this.sampling_rate = rate;
  1439. this.bus.send("dac-tell-sampling-rate", rate);
  1440. };
  1441. SB16.prototype.get_channel_count = function()
  1442. {
  1443. return this.dsp_stereo ? 2 : 1;
  1444. };
  1445. SB16.prototype.dma_transfer_size_set = function()
  1446. {
  1447. this.dma_sample_count = 1 + (this.write_buffer.shift() << 0) + (this.write_buffer.shift() << 8);
  1448. };
  1449. SB16.prototype.dma_transfer_start = function()
  1450. {
  1451. dbg_log("begin dma transfer", LOG_SB16);
  1452. // (1) Setup appropriate settings.
  1453. this.bytes_per_sample = 1;
  1454. if(this.dsp_16bit) this.bytes_per_sample *= 2;
  1455. // Don't count stereo interleaved bits apparently.
  1456. // Disabling this line is needed for sounds to work correctly,
  1457. // especially double buffering autoinit mode.
  1458. // Learnt the hard way.
  1459. // if(this.dsp_stereo) this.bytes_per_sample *= 2;
  1460. this.dma_bytes_count = this.dma_sample_count * this.bytes_per_sample;
  1461. this.dma_bytes_block = SB_DMA_BLOCK_SAMPLES * this.bytes_per_sample;
  1462. // Ensure block size is small enough but not too small, and is divisible by 4
  1463. var max_bytes_block = Math.max(this.dma_bytes_count >> 2 & ~0x3, 32);
  1464. this.dma_bytes_block = Math.min(max_bytes_block, this.dma_bytes_block);
  1465. // (2) Wait until channel is unmasked (if not already)
  1466. this.dma_waiting_transfer = true;
  1467. if(!this.dma.channel_mask[this.dma_channel])
  1468. {
  1469. this.dma_on_unmask(this.dma_channel);
  1470. }
  1471. };
  1472. SB16.prototype.dma_on_unmask = function(channel)
  1473. {
  1474. if(channel !== this.dma_channel || !this.dma_waiting_transfer)
  1475. {
  1476. return;
  1477. }
  1478. // (3) Configure amount of bytes left to transfer and tell speaker adapter
  1479. // to start requesting transfers
  1480. this.dma_waiting_transfer = false;
  1481. this.dma_bytes_left = this.dma_bytes_count;
  1482. this.dma_paused = false;
  1483. this.bus.send("dac-enable");
  1484. };
  1485. SB16.prototype.dma_transfer_next = function()
  1486. {
  1487. dbg_log("dma transfering next block", LOG_SB16);
  1488. var size = Math.min(this.dma_bytes_left, this.dma_bytes_block);
  1489. var samples = Math.floor(size / this.bytes_per_sample);
  1490. this.dma.do_write(this.dma_syncbuffer, 0, size, this.dma_channel, (error) =>
  1491. {
  1492. dbg_log("dma block transfer " + (error ? "unsuccessful" : "successful"), LOG_SB16);
  1493. if(error) return;
  1494. this.dma_to_dac(samples);
  1495. this.dma_bytes_left -= size;
  1496. if(!this.dma_bytes_left)
  1497. {
  1498. // Completed requested transfer of given size.
  1499. this.raise_irq(this.dma_irq);
  1500. if(this.dma_autoinit)
  1501. {
  1502. // Restart the transfer.
  1503. this.dma_bytes_left = this.dma_bytes_count;
  1504. }
  1505. }
  1506. });
  1507. };
  1508. SB16.prototype.dma_to_dac = function(sample_count)
  1509. {
  1510. var amplitude = this.dsp_16bit ? 32767.5 : 127.5;
  1511. var offset = this.dsp_signed ? 0 : -1;
  1512. var repeats = this.dsp_stereo ? 1 : 2;
  1513. var buffer;
  1514. if(this.dsp_16bit)
  1515. {
  1516. buffer = this.dsp_signed ? this.dma_buffer_int16 : this.dma_buffer_uint16;
  1517. }
  1518. else
  1519. {
  1520. buffer = this.dsp_signed ? this.dma_buffer_int8 : this.dma_buffer_uint8;
  1521. }
  1522. var channel = 0;
  1523. for(var i = 0; i < sample_count; i++)
  1524. {
  1525. var sample = audio_normalize(buffer[i], amplitude, offset);
  1526. for(var j = 0; j < repeats; j++)
  1527. {
  1528. this.dac_buffers[channel].push(sample);
  1529. channel ^= 1;
  1530. }
  1531. }
  1532. this.dac_send();
  1533. };
  1534. SB16.prototype.dac_handle_request = function()
  1535. {
  1536. if(!this.dma_bytes_left || this.dma_paused)
  1537. {
  1538. // No more data to transfer or is paused. Send whatever is in the buffers.
  1539. this.dac_send();
  1540. }
  1541. else
  1542. {
  1543. this.dma_transfer_next();
  1544. }
  1545. };
  1546. SB16.prototype.dac_send = function()
  1547. {
  1548. if(!this.dac_buffers[0].length)
  1549. {
  1550. return;
  1551. }
  1552. var out0 = this.dac_buffers[0].shift_block(this.dac_buffers[0].length);
  1553. var out1 = this.dac_buffers[1].shift_block(this.dac_buffers[1].length);
  1554. this.bus.send("dac-send-data", [out0, out1], [out0.buffer, out1.buffer]);
  1555. };
  1556. SB16.prototype.raise_irq = function(type)
  1557. {
  1558. dbg_log("raise irq", LOG_SB16);
  1559. this.irq_triggered[type] = 1;
  1560. this.cpu.device_raise_irq(this.irq);
  1561. };
  1562. SB16.prototype.lower_irq = function(type)
  1563. {
  1564. dbg_log("lower irq", LOG_SB16);
  1565. this.irq_triggered[type] = 0;
  1566. this.cpu.device_lower_irq(this.irq);
  1567. };
  1568. //
  1569. // Helpers
  1570. //
  1571. function audio_normalize(value, amplitude, offset)
  1572. {
  1573. return audio_clip(value / amplitude + offset, -1, 1);
  1574. }
  1575. function audio_clip(value, low, high)
  1576. {
  1577. return (value < low) * low + (value > high) * high + (low <= value && value <= high) * value;
  1578. }