SID.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. SID.h - Atmega8 MOS6581 SID Emulator
  3. Copyright (c) 2007 Christoph Haberer, christoph(at)roboterclub-freiburg.de
  4. Arduino Library Conversion by Mario Patino, cybernesto(at)gmail.com
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. // ensure this library description is only included once
  18. #ifndef SID_h
  19. #define SID_h
  20. #include <inttypes.h>
  21. #define NUMREGISTERS 29
  22. #define OSCILLATORS 3
  23. #define MAXLEVEL ( 0xFFFF / OSCILLATORS )
  24. #define SUSTAINFACTOR ( MAXLEVEL / 15 )
  25. #define SAMPLEFREQ 16000L
  26. #define SAMPLERATECOUNT (F_CPU/(8*SAMPLEFREQ)-1)
  27. #define ENVELOPE_FREQ 1000L
  28. #define MSCOUNT (SAMPLEFREQ/ENVELOPE_FREQ-1)
  29. // SID Registers
  30. #define VOICE1 0
  31. #define VOICE2 7
  32. #define VOICE3 14
  33. #define CONTROLREG 4
  34. #define ATTACKDECAY 5
  35. #define SUSTAINRELEASE 6
  36. // SID voice control register bits
  37. #define GATE (1<<0)
  38. #define SYNC (1<<1)
  39. #define RINGMOD (1<<2)
  40. #define TEST (1<<3) // not implemented
  41. #define TRIANGLE (1<<4)
  42. #define SAWTOOTH (1<<5)
  43. #define RECTANGLE (1<<6)
  44. #define NOISE (1<<7)
  45. // SID RES/FILT ( reg.23 )
  46. #define FILT1 (1<<0)
  47. #define FILT2 (1<<1)
  48. #define FILT3 (1<<2)
  49. // SID MODE/VOL ( reg.24 )
  50. #define VOICE3OFF (1<<7)
  51. typedef struct
  52. {
  53. uint16_t Freq; // Frequency: FreqLo/FreqHi
  54. uint16_t PW; // PulseWidth: PW LO/HI only 12 bits used in SID
  55. uint8_t ControlReg; // NOISE,RECTANGLE,SAWTOOTH,TRIANGLE,TEST,RINGMOD,SYNC,GATE
  56. uint8_t AttackDecay; // bit0-3 decay, bit4-7 attack
  57. uint8_t SustainRelease; // bit0-3 release, bit4-7 sustain
  58. } Voice_t;
  59. typedef struct
  60. {
  61. Voice_t voice[3];
  62. uint16_t FC; // not implemented
  63. uint8_t RES_Filt; // partly implemented
  64. uint8_t Mode_Vol; // partly implemented
  65. uint8_t POTX; // not implemented
  66. uint8_t POTY; // not implemented
  67. uint8_t OSC3_Random;// not implemented
  68. uint8_t ENV3; // not implemented
  69. } Blocks_t;
  70. typedef union
  71. {
  72. Blocks_t block;
  73. uint8_t sidregister[NUMREGISTERS];
  74. } Sid_t;
  75. typedef struct
  76. {
  77. uint16_t freq_coefficient;
  78. uint8_t envelope;
  79. uint16_t m_attack;
  80. uint16_t m_decay;
  81. uint16_t m_release;
  82. uint8_t attackdecay_flag;
  83. int16_t level_sustain;
  84. int16_t amp;
  85. } Oscillator_t;
  86. // library interface description
  87. class SID
  88. {
  89. // user-accessible "public" interface
  90. public:
  91. void begin();
  92. uint8_t set_register(uint8_t regnum, uint8_t value);
  93. uint8_t get_register(uint8_t regnum);
  94. // library-accessible "private" interface
  95. private:
  96. uint8_t get_wavenum(Voice_t *voice);
  97. void setfreq(Voice_t *voice,uint16_t freq);
  98. void init_waveform(Voice_t *voice);
  99. void setenvelope(Voice_t *voice);
  100. };
  101. #endif