summaryrefslogtreecommitdiff
path: root/SID.cs
blob: f6496ace8f6c7bc930f279472f93d01a2a4582ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SIDFX
// Copyright (C) 2004  Ian Cowburn
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// $Id$
//
using System;
using HardsidInterface;

namespace SIDFX
{
	/// <summary>
	/// Description of SID.	
	/// </summary>
	public class SID
	{
		[Flags]
		public enum ControlFlag
		{
			NOISE=128,
			PULSE=64,
			SAW=32,
			TRIANGLE=16,
			TEST=8,
			RING=4,
			SYNC=2,
			GATE=1,
			NONE=0
		};

		[Flags]
		public enum FilterFlag
		{
			OFF3=128,
			HP=64,
			BP=32,
			LP=16,
			EXT=8,
			V3=4,
			V2=2,
			V1=1,
			NONE=0
		};
			
		public static void Volume(byte volume)
		{
			byte v=(byte)(Hardsid.Read(24)&0xf0u);
			
			v|=volume;
			Hardsid.Write(24,v);
		}
		
		public static void Frequency(byte voice, uint val)
		{
			byte r=(byte)(voice*7);
			byte h=(byte)(val>>8);
			byte l=(byte)(val&0xff);
			
			Hardsid.Write(r,l);
			Hardsid.Write(r+1u,h);
		}
		
		public static void PulseWidth(byte voice, uint val)
		{
			byte r=(byte)(voice*7);
			byte h=(byte)(val>>8);
			byte l=(byte)(val&0xff);
			
			Hardsid.Write(r+2u,l);
			Hardsid.Write(r+3u,h);
		}
		
		public static void ADSR(byte voice,
		                        byte attack, byte decay,
		                        byte sustain, byte release)
		{
			byte r=(byte)(voice*7);
			byte ad=(byte)(((attack&0x0fu)<<4)|(decay&0x0fu));
			byte sr=(byte)(((sustain&0x0fu)<<4)|(release&0x0fu));
			
			Hardsid.Write(r+5u,ad);
			Hardsid.Write(r+6u,sr);
		}
		
		public static void Control(byte voice, ControlFlag control)
		{
			byte r=(byte)(voice*7);

			Hardsid.Write(r+4u,(uint)control);
		}
		
		public static void Cutoff(uint val)
		{
			byte h=(byte)(val>>3);
			byte l=(byte)(val&0x7);
			
			Hardsid.Write(21,l);
			Hardsid.Write(22+1u,h);
		}
		
		public static void Resonance(byte val)
		{
			byte v=(byte)(Hardsid.Read(23)&0x0fu);
			
			v|=(byte)((val&0x0fu)<<4);
			Hardsid.Write(23,v);
		}
		
		public static void Filter(FilterFlag flags)
		{
			byte h=(byte)((uint)flags&0xf0u);
			byte l=(byte)((uint)flags&0x0fu);

			byte v=(byte)(Hardsid.Read(23)&0xf0u);
			
			v|=l;
			Hardsid.Write(23,v);

			v=(byte)(Hardsid.Read(24)&0x0fu);
			
			v|=h;
			Hardsid.Write(24,v);
		}
		
		private SID() {}
	}
}