SID
Commodore 64 SID Library
SID.h
Go to the documentation of this file.
1 
11 #ifndef LIB_SID_H_
12 #define LIB_SID_H_
13 
14 #include <inttypes.h>
15 #include <zpuino-types.h>
16 #include <zpuino.h>
17 #include "Arduino.h"
18 
19 #define SID_ADDR_BASE_V1 0x00
20 #define SID_ADDR_BASE_V2 0x07
21 #define SID_ADDR_BASE_V3 0x0E
22 
23 #define SID_ADDR_FILTER_FC_LOW 0x15
24 #define SID_ADDR_FILTER_FC_HI 0x16
25 #define SID_ADDR_FILTER_RES_FILT 0x17
26 #define SID_ADDR_FILTER_MODE_VOL 0x18
27 
28 #define SID_ADDR_MISC_ENV3 0x1C
29 
30 #define SIDBASE IO_SLOT(14)
31 #define SIDREG(x) REGISTER(SIDBASE,x)
32 
33 class SIDVoice
34 {
35  public:
36  SIDVoice();
37  SIDVoice(int address);
38  void setBase(int address);
39  void setNote(int note, boolean active);
40  void setFreq(int freq);
41  void setPWLo(byte dutyCycle);
42  void setPWHi(byte dutyCycle);
43  void setGate(boolean active);
44  void setSync(boolean active);
45  void setRingMod(boolean active);
46  void setTest(boolean active);
47  void setTriangle(boolean active);
48  void setSawtooth(boolean active);
49  void setSquare(boolean active);
50  void setSquare(boolean active, int pwm);
51  void setNoise(boolean active);
52  void setEnvelopeAttack(byte rate);
53  void setEnvelopeDecay(byte rate);
54  void setEnvelopeSustain(byte level);
55  void setEnvelopeRelease(byte rate);
56  void setInstrument(const char* name, byte attack, byte decay, byte sustain, byte release, bool noise, bool square, bool sawtooth, bool triangle, int pwm);
57  void loadInstrument(byte instrument);
58  void handleCC(byte number, byte value);
59  void reset();
60  int getCurrentFreq();
61  private:
62  void writeData(unsigned char address, unsigned char data);
63  void ringMod(byte baseOffset, byte valueOffset, byte value);
64  int baseAddress;
65  int currentFreq;
66  int SID_ADDR_FREQ_LOW;
67  int SID_ADDR_FREQ_HI;
68  int SID_ADDR_PW_LOW;
69  int SID_ADDR_PW_HI;
70  int SID_ADDR_CONTROLREG;
71  int SID_ADDR_ATTACK_DECAY;
72  int SID_ADDR_SUSTAIN_RELEASE;
73  char instrumentName[25];
74  struct SID_REG_CONTROLREG_STRUCT{
75  unsigned int NOISE_WAVE : 1;
76  unsigned int SQUARE_WAVE : 1;
77  unsigned int SAWTOOTH_WAVE : 1;
78  unsigned int TRIANGLE_WAVE : 1;
79  unsigned int TEST : 1;
80  unsigned int RING_MOD : 1;
81  unsigned int SYNC : 1;
82  unsigned int GATE : 1;
83  } ;
84  SID_REG_CONTROLREG_STRUCT SID_REG_CONTROLREG;
85 
86  struct SID_REG_ATTACK_DECAY_STRUCT{
87  unsigned int ATTACK : 4;
88  unsigned int DECAY : 4;
89  };
90  SID_REG_ATTACK_DECAY_STRUCT SID_REG_ATTACK_DECAY;
91 
92  struct SID_REG_SUSTAIN_RELEASE_STRUCT{
93  unsigned int SUSTAIN : 4;
94  unsigned int RELEASE : 4;
95  };
96  SID_REG_SUSTAIN_RELEASE_STRUCT SID_REG_SUSTAIN_RELEASE;
97 };
98 
99 class SID
100 {
101  public:
102  SIDVoice V1;
103  SIDVoice V2;
104  SIDVoice V3;
105  SID();
106  static void writeData(unsigned char address, unsigned char data);
107  void setVolume(byte volume);
108  void reset();
109  static const int MIDI2freq[129];
110  private:
111  struct SID_REG_MODE_VOLUME_STRUCT{
112  unsigned int OFF : 1;
113  unsigned int HP : 1;
114  unsigned int BP : 1;
115  unsigned int LP : 1;
116  unsigned int VOLUME : 4;
117  } ;
118  SID_REG_MODE_VOLUME_STRUCT SID_REG_MODE_VOLUME;
119 };
120 #endif // LIB_SID_H_