wave.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define __WAVE_CC__
  20. #include "wave.h"
  21. // ----------------------------------------------------------------------------
  22. // Constructor.
  23. // ----------------------------------------------------------------------------
  24. WaveformGenerator::WaveformGenerator()
  25. {
  26. sync_source = this;
  27. set_chip_model(MOS6581);
  28. reset();
  29. }
  30. // ----------------------------------------------------------------------------
  31. // Set sync source.
  32. // ----------------------------------------------------------------------------
  33. void WaveformGenerator::set_sync_source(WaveformGenerator* source)
  34. {
  35. sync_source = source;
  36. source->sync_dest = this;
  37. }
  38. // ----------------------------------------------------------------------------
  39. // Set chip model.
  40. // ----------------------------------------------------------------------------
  41. void WaveformGenerator::set_chip_model(chip_model model)
  42. {
  43. if (model == MOS6581) {
  44. wave__ST = wave6581__ST;
  45. wave_P_T = wave6581_P_T;
  46. wave_PS_ = wave6581_PS_;
  47. wave_PST = wave6581_PST;
  48. }
  49. else {
  50. wave__ST = wave8580__ST;
  51. wave_P_T = wave8580_P_T;
  52. wave_PS_ = wave8580_PS_;
  53. wave_PST = wave8580_PST;
  54. }
  55. }
  56. // ----------------------------------------------------------------------------
  57. // Register functions.
  58. // ----------------------------------------------------------------------------
  59. void WaveformGenerator::writeFREQ_LO(reg8 freq_lo)
  60. {
  61. freq = ( freq & 0xff00 ) | ( freq_lo & 0x00ff );
  62. }
  63. void WaveformGenerator::writeFREQ_HI(reg8 freq_hi)
  64. {
  65. freq = ( (freq_hi << 8) & 0xff00 ) | ( freq & 0x00ff );
  66. }
  67. void WaveformGenerator::writePW_LO(reg8 pw_lo)
  68. {
  69. pw = ( pw & 0xf00 ) | ( pw_lo & 0x0ff );
  70. }
  71. void WaveformGenerator::writePW_HI(reg8 pw_hi)
  72. {
  73. pw = ( (pw_hi << 8) & 0xf00 ) | ( pw & 0x0ff );
  74. }
  75. void WaveformGenerator::writeCONTROL_REG(reg8 control)
  76. {
  77. waveform = (control >> 4) & 0x0f;
  78. ring_mod = control & 0x04;
  79. sync = control & 0x02;
  80. reg8 test_next = control & 0x08;
  81. // Test bit set.
  82. // The accumulator and the shift register are both cleared.
  83. // NB! The shift register is not really cleared immediately. It seems like
  84. // the individual bits in the shift register start to fade down towards
  85. // zero when test is set. All bits reach zero within approximately
  86. // $2000 - $4000 cycles.
  87. // This is not modeled. There should fortunately be little audible output
  88. // from this peculiar behavior.
  89. if (test_next) {
  90. accumulator = 0;
  91. shift_register = 0;
  92. }
  93. // Test bit cleared.
  94. // The accumulator starts counting, and the shift register is reset to
  95. // the value 0x7ffff8.
  96. // NB! The shift register will not actually be set to this exact value if the
  97. // shift register bits have not had time to fade to zero.
  98. // This is not modeled.
  99. else if (test) {
  100. shift_register = 0x7ffff8;
  101. }
  102. test = test_next;
  103. // The gate bit is handled by the EnvelopeGenerator.
  104. }
  105. reg8 WaveformGenerator::readOSC()
  106. {
  107. return output() >> 4;
  108. }
  109. // ----------------------------------------------------------------------------
  110. // SID reset.
  111. // ----------------------------------------------------------------------------
  112. void WaveformGenerator::reset()
  113. {
  114. accumulator = 0;
  115. shift_register = 0x7ffff8;
  116. freq = 0;
  117. pw = 0;
  118. test = 0;
  119. ring_mod = 0;
  120. sync = 0;
  121. msb_rising = false;
  122. }