Debian Codegraph @

src/engine/platform/sound/sm8521.c

raw
1 /*
2
3 ============================================================================
4
5 SM8521 sound emulator
6 by cam900
7
8 MODIFIED BY TILDEARROW
9 THIS IS NOT THE ORIGINAL VERSION
10
11 This file is licensed under zlib license.
12
13 ============================================================================
14
15 zlib License
16
17 (C) 2023-present cam900 and contributors
18
19 This software is provided 'as-is', without any express or implied
20 warranty. In no event will the authors be held liable for any damages
21 arising from the use of this software.
22
23 Permission is granted to anyone to use this software for any purpose,
24 including commercial applications, and to alter it and redistribute it
25 freely, subject to the following restrictions:
26
27 1. The origin of this software must not be misrepresented; you must not
28 claim that you wrote the original software. If you use this software
29 in a product, an acknowledgment in the product documentation would be
30 appreciated but is not required.
31 2. Altered source versions must be plainly marked as such, and must not be
32 misrepresented as being the original software.
33 3. This notice may not be removed or altered from any source distribution.
34
35 ============================================================================
36
37 TODO:
38 - needs hardware test
39
40 */
41
42 #include "sm8521.h"
43 #include <stdlib.h>
44
45 enum sm8521_sgc
46 {
47 SM8521_SGC_SONDOUT = (1 << 7),
48 SM8521_SGC_DIROUT = (1 << 3),
49 SM8521_SGC_SG2OUT = (1 << 2),
50 SM8521_SGC_SG1OUT = (1 << 1),
51 SM8521_SGC_SG0OUT = (1 << 0)
52 };
53
54 void sm8521_sg_wave_tick(struct sm8521_wave_t *sg, const int cycle)
55 {
56 sg->base.counter += cycle;
57 while (sg->base.counter >= (sg->base.t + 1))
58 {
59 sg->addr++;
60 sg->base.counter -= (sg->base.t + 1);
61 }
62 int wave = (sg->wave[(sg->addr >> 1) & 0xf] >> ((sg->addr & 1) << 2)) & 0xf;
63 if (wave & 0x8)
64 {
65 wave = -(0x8 - (wave & 0x7));
66 }
67 sg->base.out = (wave * sg->base.level) >> 1; // scale out to 8bit
68 }
69
70 void sm8521_noise_tick(struct sm8521_noise_t *noise, const int cycle)
71 {
72 noise->base.counter += cycle;
73 while (noise->base.counter >= (noise->base.t + 1))
74 {
75 // https://github.com/tildearrow/furnace/issues/2567
76 // unknown algorithm, but don't use rand()
77 //
78 // some research suggests VIC-like noise, although
79 // that remains to be confirmed
80 noise->oldLFSR = noise->lfsr & 1;
81 noise->lfsr = ( noise->lfsr>>1|(((noise->lfsr) ^ (noise->lfsr >> 5) ^ (noise->lfsr >> 8) ^ (noise->lfsr >> 13) ) & 1)<<31);
82 noise->base.counter -= (noise->base.t + 1);
83 if (noise->oldLFSR^(noise->lfsr&1)) {
84 noise->out ^= 1;
85 }
86 }
87 noise->base.out = ((noise->out ? 7 : -8) * noise->base.level) >> 1; // scale out to 8bit
88 }
89
90 void sm8521_sound_tick(struct sm8521_t *sm8521, const int cycle)
91 {
92 int out = 0;
93 if (sm8521->sgc & SM8521_SGC_SONDOUT)
94 {
95 if (sm8521->sgc & SM8521_SGC_DIROUT)
96 {
97 out = sm8521->sgda - 0x80;
98 }
99 else
100 {
101 if (sm8521->sgc & SM8521_SGC_SG0OUT)
102 {
103 sm8521_sg_wave_tick(&sm8521->sg[0], cycle);
104 out += sm8521->sg[0].base.out;
105 }
106 if (sm8521->sgc & SM8521_SGC_SG1OUT)
107 {
108 sm8521_sg_wave_tick(&sm8521->sg[1], cycle);
109 out += sm8521->sg[1].base.out;
110 }
111 if (sm8521->sgc & SM8521_SGC_SG2OUT)
112 {
113 sm8521_noise_tick(&sm8521->noise, cycle);
114 out += sm8521->noise.base.out;
115 }
116 out = (out < -0x80) ? -0x80 : ((out > 0x7f) ? 0x7f : out); // clamp
117 }
118 }
119 sm8521->out = out;
120 }
121
122 void sm8521_reset(struct sm8521_t *sm8521)
123 {
124 for (int i = 0; i < 2; i++)
125 {
126 sm8521->sg[i].base.t = 0;
127 sm8521->sg[i].base.level = 0;
128 sm8521->sg[i].base.out = 0;
129 sm8521->sg[i].base.counter = 0;
130 sm8521->sg[i].addr = 0;
131 for (int j = 0; j < 16; j++)
132 {
133 sm8521->sg[i].wave[j] = 0;
134 }
135 }
136 sm8521->noise.base.t = 0;
137 sm8521->noise.base.level = 0;
138 sm8521->noise.base.out = 0;
139 sm8521->noise.base.counter = 0;
140 sm8521->noise.lfsr = 0x89abcdef;
141 sm8521->noise.oldLFSR = 1;
142 sm8521->noise.out = 0;
143 sm8521->out = 0;
144 sm8521->sgda = 0;
145 sm8521->sgc = 0;
146 }
147
148 unsigned char sm8521_read(struct sm8521_t *sm8521, const unsigned char a)
149 {
150 if ((a & 0xe0) == 0x60)
151 {
152 if ((a & 0x10) && (!(sm8521->sgc & SM8521_SGC_SG1OUT))) // 0x70-0x7f SG1W0-15
153 {
154 return sm8521->sg[1].wave[a & 0xf];
155 }
156 else if ((!(a & 0x10)) && (!(sm8521->sgc & SM8521_SGC_SG0OUT))) // 0x60-0x6f SG0W0-15
157 {
158 return sm8521->sg[0].wave[a & 0xf];
159 }
160 return 0;
161 }
162 switch (a)
163 {
164 case 0x40: // SGC
165 return sm8521->sgc;
166 break;
167 case 0x42: // SG0L
168 return sm8521->sg[0].base.level & 0x1f;
169 break;
170 case 0x44: // SG1L
171 return sm8521->sg[1].base.level & 0x1f;
172 break;
173 case 0x46: // SG0TH
174 return (sm8521->sg[0].base.t >> 8) & 0xf;
175 break;
176 case 0x47: // SG0TL
177 return sm8521->sg[0].base.t & 0xff;
178 break;
179 case 0x48: // SG1TH
180 return (sm8521->sg[1].base.t >> 8) & 0xf;
181 break;
182 case 0x49: // SG1TL
183 return sm8521->sg[1].base.t & 0x0ff;
184 break;
185 case 0x4a: // SG2L
186 return sm8521->noise.base.level & 0x1f;
187 break;
188 case 0x4c: // SG2TH
189 return (sm8521->noise.base.t >> 8) & 0xf;
190 break;
191 case 0x4d: // SG2TL
192 return sm8521->noise.base.t & 0xff;
193 break;
194 default:
195 return 0;
196 break;
197 }
198 }
199 void sm8521_write(struct sm8521_t *sm8521, const unsigned char a, const unsigned char d)
200 {
201 if ((a & 0xe0) == 0x60)
202 {
203 if ((a & 0x10) && (!(sm8521->sgc & SM8521_SGC_SG1OUT))) // 0x70-0x7f SG1W0-15
204 {
205 sm8521->sg[1].wave[a & 0xf] = d;
206 }
207 else if ((!(a & 0x10)) && (!(sm8521->sgc & SM8521_SGC_SG0OUT))) // 0x60-0x6f SG0W0-15
208 {
209 sm8521->sg[0].wave[a & 0xf] = d;
210 }
211 return;
212 }
213 switch (a)
214 {
215 case 0x40: // SGC
216 sm8521->sgc = d;
217 break;
218 case 0x42: // SG0L
219 sm8521->sg[0].base.level = d & 0x1f;
220 break;
221 case 0x44: // SG1L
222 sm8521->sg[1].base.level = d & 0x1f;
223 break;
224 case 0x46: // SG0TH
225 sm8521->sg[0].base.t = (sm8521->sg[0].base.t & 0x0ff) | ((d << 8) & 0xf00);
226 break;
227 case 0x47: // SG0TL
228 sm8521->sg[0].base.t = (sm8521->sg[0].base.t & 0xf00) | (d & 0x0ff);
229 break;
230 case 0x48: // SG1TH
231 sm8521->sg[1].base.t = (sm8521->sg[1].base.t & 0x0ff) | ((d << 8) & 0xf00);
232 break;
233 case 0x49: // SG1TL
234 sm8521->sg[1].base.t = (sm8521->sg[1].base.t & 0xf00) | (d & 0x0ff);
235 break;
236 case 0x4a: // SG2L
237 sm8521->noise.base.level = d & 0x1f;
238 break;
239 case 0x4c: // SG2TH
240 sm8521->noise.base.t = (sm8521->noise.base.t & 0x0ff) | ((d << 8) & 0xf00);
241 break;
242 case 0x4d: // SG2TL
243 sm8521->noise.base.t = (sm8521->noise.base.t & 0xf00) | (d & 0x0ff);
244 break;
245 case 0x4e: // SGDA
246 sm8521->sgda = d;
247 break;
248 }
249 }