wave.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // ---------------------------------------------------------------------------
  2. // This file is part of reSID, a MOS6581 SID emulator engine.
  3. // Copyright (C) 2004 Dag Lem <resid@nimrod.no>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. // ---------------------------------------------------------------------------
  19. #ifndef __WAVE_H__
  20. #define __WAVE_H__
  21. #include "siddefs.h"
  22. // ----------------------------------------------------------------------------
  23. // A 24 bit accumulator is the basis for waveform generation. FREQ is added to
  24. // the lower 16 bits of the accumulator each cycle.
  25. // The accumulator is set to zero when TEST is set, and starts counting
  26. // when TEST is cleared.
  27. // The noise waveform is taken from intermediate bits of a 23 bit shift
  28. // register. This register is clocked by bit 19 of the accumulator.
  29. // ----------------------------------------------------------------------------
  30. class WaveformGenerator
  31. {
  32. public:
  33. WaveformGenerator();
  34. void set_sync_source(WaveformGenerator*);
  35. void set_chip_model(chip_model model);
  36. RESID_INLINE void clock();
  37. RESID_INLINE void clock(cycle_count delta_t);
  38. RESID_INLINE void synchronize();
  39. void reset();
  40. void writeFREQ_LO(reg8);
  41. void writeFREQ_HI(reg8);
  42. void writePW_LO(reg8);
  43. void writePW_HI(reg8);
  44. void writeCONTROL_REG(reg8);
  45. reg8 readOSC();
  46. // 12-bit waveform output.
  47. RESID_INLINE reg12 output();
  48. protected:
  49. const WaveformGenerator* sync_source;
  50. WaveformGenerator* sync_dest;
  51. // Tell whether the accumulator MSB was set high on this cycle.
  52. bool msb_rising;
  53. reg24 accumulator;
  54. reg24 shift_register;
  55. // Fout = (Fn*Fclk/16777216)Hz
  56. reg16 freq;
  57. // PWout = (PWn/40.95)%
  58. reg12 pw;
  59. // The control register right-shifted 4 bits; used for output function
  60. // table lookup.
  61. reg8 waveform;
  62. // The remaining control register bits.
  63. reg8 test;
  64. reg8 ring_mod;
  65. reg8 sync;
  66. // The gate bit is handled by the EnvelopeGenerator.
  67. // 16 possible combinations of waveforms.
  68. RESID_INLINE reg12 output____();
  69. RESID_INLINE reg12 output___T();
  70. RESID_INLINE reg12 output__S_();
  71. RESID_INLINE reg12 output__ST();
  72. RESID_INLINE reg12 output_P__();
  73. RESID_INLINE reg12 output_P_T();
  74. RESID_INLINE reg12 output_PS_();
  75. RESID_INLINE reg12 output_PST();
  76. RESID_INLINE reg12 outputN___();
  77. RESID_INLINE reg12 outputN__T();
  78. RESID_INLINE reg12 outputN_S_();
  79. RESID_INLINE reg12 outputN_ST();
  80. RESID_INLINE reg12 outputNP__();
  81. RESID_INLINE reg12 outputNP_T();
  82. RESID_INLINE reg12 outputNPS_();
  83. RESID_INLINE reg12 outputNPST();
  84. // Sample data for combinations of waveforms.
  85. static reg8 wave6581__ST[];
  86. static reg8 wave6581_P_T[];
  87. static reg8 wave6581_PS_[];
  88. static reg8 wave6581_PST[];
  89. static reg8 wave8580__ST[];
  90. static reg8 wave8580_P_T[];
  91. static reg8 wave8580_PS_[];
  92. static reg8 wave8580_PST[];
  93. reg8* wave__ST;
  94. reg8* wave_P_T;
  95. reg8* wave_PS_;
  96. reg8* wave_PST;
  97. friend class Voice;
  98. friend class cSID;
  99. };
  100. // ----------------------------------------------------------------------------
  101. // Inline functions.
  102. // The following functions are defined inline because they are called every
  103. // time a sample is calculated.
  104. // ----------------------------------------------------------------------------
  105. #if RESID_INLINING || defined(__WAVE_CC__)
  106. // ----------------------------------------------------------------------------
  107. // SID clocking - 1 cycle.
  108. // ----------------------------------------------------------------------------
  109. RESID_INLINE
  110. void WaveformGenerator::clock()
  111. {
  112. // No operation if test bit is set.
  113. if (test) {
  114. return;
  115. }
  116. reg24 accumulator_prev = accumulator;
  117. // Calculate new accumulator value;
  118. accumulator += freq;
  119. accumulator &= 0xffffff;
  120. // Check whether the MSB is set high. This is used for synchronization.
  121. msb_rising = !(accumulator_prev & 0x800000) && (accumulator & 0x800000);
  122. // Shift noise register once for each time accumulator bit 19 is set high.
  123. if (!(accumulator_prev & 0x080000) && (accumulator & 0x080000)) {
  124. reg24 bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
  125. shift_register <<= 1;
  126. shift_register &= 0x7fffff;
  127. shift_register |= bit0;
  128. }
  129. }
  130. // ----------------------------------------------------------------------------
  131. // SID clocking - delta_t cycles.
  132. // ----------------------------------------------------------------------------
  133. RESID_INLINE
  134. void WaveformGenerator::clock(cycle_count delta_t)
  135. {
  136. // No operation if test bit is set.
  137. if (test) {
  138. return;
  139. }
  140. reg24 accumulator_prev = accumulator;
  141. // Calculate new accumulator value;
  142. reg24 delta_accumulator = delta_t*freq;
  143. accumulator += delta_accumulator;
  144. accumulator &= 0xffffff;
  145. // Check whether the MSB is set high. This is used for synchronization.
  146. msb_rising = !(accumulator_prev & 0x800000) && (accumulator & 0x800000);
  147. // Shift noise register once for each time accumulator bit 19 is set high.
  148. // Bit 19 is set high each time 2^20 (0x100000) is added to the accumulator.
  149. reg24 shift_period = 0x100000;
  150. while (delta_accumulator) {
  151. if (delta_accumulator < shift_period) {
  152. shift_period = delta_accumulator;
  153. // Determine whether bit 19 is set on the last period.
  154. // NB! Requires two's complement integer.
  155. if (shift_period <= 0x080000) {
  156. // Check for flip from 0 to 1.
  157. if (((accumulator - shift_period) & 0x080000) || !(accumulator & 0x080000))
  158. {
  159. break;
  160. }
  161. }
  162. else {
  163. // Check for flip from 0 (to 1 or via 1 to 0) or from 1 via 0 to 1.
  164. if (((accumulator - shift_period) & 0x080000) && !(accumulator & 0x080000))
  165. {
  166. break;
  167. }
  168. }
  169. }
  170. // Shift the noise/random register.
  171. // NB! The shift is actually delayed 2 cycles, this is not modeled.
  172. reg24 bit0 = ((shift_register >> 22) ^ (shift_register >> 17)) & 0x1;
  173. shift_register <<= 1;
  174. shift_register &= 0x7fffff;
  175. shift_register |= bit0;
  176. delta_accumulator -= shift_period;
  177. }
  178. }
  179. // ----------------------------------------------------------------------------
  180. // Synchronize oscillators.
  181. // This must be done after all the oscillators have been clock()'ed since the
  182. // oscillators operate in parallel.
  183. // Note that the oscillators must be clocked exactly on the cycle when the
  184. // MSB is set high for hard sync to operate correctly. See SID::clock().
  185. // ----------------------------------------------------------------------------
  186. RESID_INLINE
  187. void WaveformGenerator::synchronize()
  188. {
  189. // A special case occurs when a sync source is synced itself on the same
  190. // cycle as when its MSB is set high. In this case the destination will
  191. // not be synced. This has been verified by sampling OSC3.
  192. if (msb_rising && sync_dest->sync && !(sync && sync_source->msb_rising)) {
  193. sync_dest->accumulator = 0;
  194. }
  195. }
  196. // ----------------------------------------------------------------------------
  197. // Output functions.
  198. // NB! The output from SID 8580 is delayed one cycle compared to SID 6581,
  199. // this is not modeled.
  200. // ----------------------------------------------------------------------------
  201. // No waveform:
  202. // Zero output.
  203. //
  204. RESID_INLINE
  205. reg12 WaveformGenerator::output____()
  206. {
  207. return 0x000;
  208. }
  209. // Triangle:
  210. // The upper 12 bits of the accumulator are used.
  211. // The MSB is used to create the falling edge of the triangle by inverting
  212. // the lower 11 bits. The MSB is thrown away and the lower 11 bits are
  213. // left-shifted (half the resolution, full amplitude).
  214. // Ring modulation substitutes the MSB with MSB EOR sync_source MSB.
  215. //
  216. RESID_INLINE
  217. reg12 WaveformGenerator::output___T()
  218. {
  219. reg24 msb = (ring_mod ? accumulator ^ sync_source->accumulator : accumulator)
  220. & 0x800000;
  221. return ((msb ? ~accumulator : accumulator) >> 11) & 0xfff;
  222. }
  223. // Sawtooth:
  224. // The output is identical to the upper 12 bits of the accumulator.
  225. //
  226. RESID_INLINE
  227. reg12 WaveformGenerator::output__S_()
  228. {
  229. return accumulator >> 12;
  230. }
  231. // Pulse:
  232. // The upper 12 bits of the accumulator are used.
  233. // These bits are compared to the pulse width register by a 12 bit digital
  234. // comparator; output is either all one or all zero bits.
  235. // NB! The output is actually delayed one cycle after the compare.
  236. // This is not modeled.
  237. //
  238. // The test bit, when set to one, holds the pulse waveform output at 0xfff
  239. // regardless of the pulse width setting.
  240. //
  241. RESID_INLINE
  242. reg12 WaveformGenerator::output_P__()
  243. {
  244. return (test || (accumulator >> 12) >= pw) ? 0xfff : 0x000;
  245. }
  246. // Noise:
  247. // The noise output is taken from intermediate bits of a 23-bit shift register
  248. // which is clocked by bit 19 of the accumulator.
  249. // NB! The output is actually delayed 2 cycles after bit 19 is set high.
  250. // This is not modeled.
  251. //
  252. // Operation: Calculate EOR result, shift register, set bit 0 = result.
  253. //
  254. // ----------------------->---------------------
  255. // | |
  256. // ----EOR---- |
  257. // | | |
  258. // 2 2 2 1 1 1 1 1 1 1 1 1 1 |
  259. // Register bits: 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 <---
  260. // | | | | | | | |
  261. // OSC3 bits : 7 6 5 4 3 2 1 0
  262. //
  263. // Since waveform output is 12 bits the output is left-shifted 4 times.
  264. //
  265. RESID_INLINE
  266. reg12 WaveformGenerator::outputN___()
  267. {
  268. return
  269. ((shift_register & 0x400000) >> 11) |
  270. ((shift_register & 0x100000) >> 10) |
  271. ((shift_register & 0x010000) >> 7) |
  272. ((shift_register & 0x002000) >> 5) |
  273. ((shift_register & 0x000800) >> 4) |
  274. ((shift_register & 0x000080) >> 1) |
  275. ((shift_register & 0x000010) << 1) |
  276. ((shift_register & 0x000004) << 2);
  277. }
  278. // Combined waveforms:
  279. // By combining waveforms, the bits of each waveform are effectively short
  280. // circuited. A zero bit in one waveform will result in a zero output bit
  281. // (thus the infamous claim that the waveforms are AND'ed).
  282. // However, a zero bit in one waveform will also affect the neighboring bits
  283. // in the output. The reason for this has not been determined.
  284. //
  285. // Example:
  286. //
  287. // 1 1
  288. // Bit # 1 0 9 8 7 6 5 4 3 2 1 0
  289. // -----------------------
  290. // Sawtooth 0 0 0 1 1 1 1 1 1 0 0 0
  291. //
  292. // Triangle 0 0 1 1 1 1 1 1 0 0 0 0
  293. //
  294. // AND 0 0 0 1 1 1 1 1 0 0 0 0
  295. //
  296. // Output 0 0 0 0 1 1 1 0 0 0 0 0
  297. //
  298. //
  299. // This behavior would be quite difficult to model exactly, since the SID
  300. // in this case does not act as a digital state machine. Tests show that minor
  301. // (1 bit) differences can actually occur in the output from otherwise
  302. // identical samples from OSC3 when waveforms are combined. To further
  303. // complicate the situation the output changes slightly with time (more
  304. // neighboring bits are successively set) when the 12-bit waveform
  305. // registers are kept unchanged.
  306. //
  307. // It is probably possible to come up with a valid model for the
  308. // behavior, however this would be far too slow for practical use since it
  309. // would have to be based on the mutual influence of individual bits.
  310. //
  311. // The output is instead approximated by using the upper bits of the
  312. // accumulator as an index to look up the combined output in a table
  313. // containing actual combined waveform samples from OSC3.
  314. // These samples are 8 bit, so 4 bits of waveform resolution is lost.
  315. // All OSC3 samples are taken with FREQ=0x1000, adding a 1 to the upper 12
  316. // bits of the accumulator each cycle for a sample period of 4096 cycles.
  317. //
  318. // Sawtooth+Triangle:
  319. // The sawtooth output is used to look up an OSC3 sample.
  320. //
  321. // Pulse+Triangle:
  322. // The triangle output is right-shifted and used to look up an OSC3 sample.
  323. // The sample is output if the pulse output is on.
  324. // The reason for using the triangle output as the index is to handle ring
  325. // modulation. Only the first half of the sample is used, which should be OK
  326. // since the triangle waveform has half the resolution of the accumulator.
  327. //
  328. // Pulse+Sawtooth:
  329. // The sawtooth output is used to look up an OSC3 sample.
  330. // The sample is output if the pulse output is on.
  331. //
  332. // Pulse+Sawtooth+Triangle:
  333. // The sawtooth output is used to look up an OSC3 sample.
  334. // The sample is output if the pulse output is on.
  335. //
  336. RESID_INLINE
  337. reg12 WaveformGenerator::output__ST()
  338. {
  339. return wave__ST[output__S_()] << 4;
  340. }
  341. RESID_INLINE
  342. reg12 WaveformGenerator::output_P_T()
  343. {
  344. return (wave_P_T[output___T() >> 1] << 4) & output_P__();
  345. }
  346. RESID_INLINE
  347. reg12 WaveformGenerator::output_PS_()
  348. {
  349. return (wave_PS_[output__S_()] << 4) & output_P__();
  350. }
  351. RESID_INLINE
  352. reg12 WaveformGenerator::output_PST()
  353. {
  354. return (wave_PST[output__S_()] << 4) & output_P__();
  355. }
  356. // Combined waveforms including noise:
  357. // All waveform combinations including noise output zero after a few cycles.
  358. // NB! The effects of such combinations are not fully explored. It is claimed
  359. // that the shift register may be filled with zeroes and locked up, which
  360. // seems to be true.
  361. // We have not attempted to model this behavior, suffice to say that
  362. // there is very little audible output from waveform combinations including
  363. // noise. We hope that nobody is actually using it.
  364. //
  365. RESID_INLINE
  366. reg12 WaveformGenerator::outputN__T()
  367. {
  368. return 0;
  369. }
  370. RESID_INLINE
  371. reg12 WaveformGenerator::outputN_S_()
  372. {
  373. return 0;
  374. }
  375. RESID_INLINE
  376. reg12 WaveformGenerator::outputN_ST()
  377. {
  378. return 0;
  379. }
  380. RESID_INLINE
  381. reg12 WaveformGenerator::outputNP__()
  382. {
  383. return 0;
  384. }
  385. RESID_INLINE
  386. reg12 WaveformGenerator::outputNP_T()
  387. {
  388. return 0;
  389. }
  390. RESID_INLINE
  391. reg12 WaveformGenerator::outputNPS_()
  392. {
  393. return 0;
  394. }
  395. RESID_INLINE
  396. reg12 WaveformGenerator::outputNPST()
  397. {
  398. return 0;
  399. }
  400. // ----------------------------------------------------------------------------
  401. // Select one of 16 possible combinations of waveforms.
  402. // ----------------------------------------------------------------------------
  403. RESID_INLINE
  404. reg12 WaveformGenerator::output()
  405. {
  406. // It may seem cleaner to use an array of member functions to return
  407. // waveform output; however a switch with inline functions is faster.
  408. switch (waveform) {
  409. default:
  410. case 0x0:
  411. return output____();
  412. case 0x1:
  413. return output___T();
  414. case 0x2:
  415. return output__S_();
  416. case 0x3:
  417. return output__ST();
  418. case 0x4:
  419. return output_P__();
  420. case 0x5:
  421. return output_P_T();
  422. case 0x6:
  423. return output_PS_();
  424. case 0x7:
  425. return output_PST();
  426. case 0x8:
  427. return outputN___();
  428. case 0x9:
  429. return outputN__T();
  430. case 0xa:
  431. return outputN_S_();
  432. case 0xb:
  433. return outputN_ST();
  434. case 0xc:
  435. return outputNP__();
  436. case 0xd:
  437. return outputNP_T();
  438. case 0xe:
  439. return outputNPS_();
  440. case 0xf:
  441. return outputNPST();
  442. }
  443. }
  444. #endif // RESID_INLINING || defined(__WAVE_CC__)
  445. #endif // not __WAVE_H__