Debian Codegraph @

extern/Nuked-PSG/ympsg.c

raw
1 // Copyright (C) 2021 Nuke.YKT
2 // License: GPLv2+
3 // Version 1.0.1
4 #include <string.h>
5 #include "ympsg.h"
6
7 const float ympsg_vol[17] = {
8 1.0, 0.772, 0.622, 0.485, 0.382, 0.29, 0.229, 0.174, 0.132, 0.096, 0.072, 0.051, 0.034, 0.019, 0.009, 0.0, -1.059
9 };
10
11 const float tipsg_vol[17] = {
12 1.0, 0.794, 0.631, 0.501, 0.398, 0.316, 0.251, 0.2, 0.158, 0.126, 0.1, 0.079, 0.063, 0.05, 0.04, 0.0, -1.059
13 };
14
15 static void YMPSG_WriteLatch(ympsg_t *chip)
16 {
17 uint8_t data = chip->data;
18 if (chip->data_mask)
19 {
20 data = 0;
21 }
22 if (data & 128)
23 {
24 chip->latch = (data >> 4) & 7;
25 }
26 }
27
28 static void YMPSG_UpdateRegisters(ympsg_t* chip)
29 {
30 uint8_t data = chip->data;
31 if (chip->data_mask)
32 {
33 data = 0;
34 }
35 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 1))
36 {
37 chip->volume[0] = data & 15;
38 if (chip->data_mask)
39 {
40 chip->volume[0] = 15;
41 }
42 }
43 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 3))
44 {
45 chip->volume[1] = data & 15;
46 if (chip->data_mask)
47 {
48 chip->volume[1] = 15;
49 }
50 }
51 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 4))
52 {
53 if ((data & 128) || chip->reg_reset)
54 {
55 chip->freq[2] &= 1008;
56 chip->freq[2] |= data & 15;
57 }
58 if (!(data & 128))
59 {
60 chip->freq[2] &= 15;
61 chip->freq[2] |= (data << 4) & 1008;
62 }
63 }
64 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 2))
65 {
66 if ((data & 128) || chip->reg_reset)
67 {
68 chip->freq[1] &= 1008;
69 chip->freq[1] |= data & 15;
70 }
71 if (!(data & 128))
72 {
73 chip->freq[1] &= 15;
74 chip->freq[1] |= (data << 4) & 1008;
75 }
76 }
77 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 5))
78 {
79 chip->volume[2] = data & 15;
80 if (chip->data_mask)
81 {
82 chip->volume[2] = 15;
83 }
84 }
85 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 0))
86 {
87 if ((data & 128) || chip->reg_reset)
88 {
89 chip->freq[0] &= 1008;
90 chip->freq[0] |= data & 15;
91 }
92 if (!(data & 128))
93 {
94 chip->freq[0] &= 15;
95 chip->freq[0] |= (data << 4) & 1008;
96 }
97 }
98 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 7))
99 {
100 chip->volume[3] = data & 15;
101 if (chip->data_mask)
102 {
103 chip->volume[3] = 15;
104 }
105 }
106 if (chip->reg_reset || (chip->write_flag_l && chip->latch == 6))
107 {
108 chip->noise_data = data & 7;
109 chip->noise_trig = 1;
110 }
111 }
112
113 static void YMPSG_ClockInternal1(ympsg_t *chip)
114 {
115 uint16_t freq = 0;
116 uint8_t chan_sel = chip->chan_sel;
117 uint8_t noise_of, noise_bit1, noise_bit2, noise_next;
118 if ((chip->noise_data & 3) == 3)
119 {
120 noise_of = (chip->sign >> 1) & 1;
121 }
122 else
123 {
124 noise_of = chip->sign & 1;
125 }
126 if (chip->noise_trig_l || (chip->ic_latch2 & 1))
127 {
128 chip->noise = 0;
129 }
130 else if (noise_of && !chip->noise_of)
131 {
132 noise_bit1 = (chip->noise >> chip->noise_tap2) & 1;
133 noise_bit2 = (chip->noise >> chip->noise_tap1) & 1;
134 noise_bit1 ^= noise_bit2;
135 noise_next = ((noise_bit1 && ((chip->noise_data >> 2) & 1)) || ((chip->noise & chip->noise_size) == 0));
136 chip->noise <<= 1;
137 chip->noise |= noise_next;
138 }
139 chip->noise_of = noise_of;
140 if (chip->ic_latch2 & 2)
141 {
142 chan_sel = 0;
143 }
144 if (chip->chan_sel & 1)
145 {
146 freq |= chip->freq[0];
147 }
148 if (chip->chan_sel & 2)
149 {
150 freq |= chip->freq[1];
151 }
152 if (chip->chan_sel & 4)
153 {
154 freq |= chip->freq[2];
155 }
156 if (chip->chan_sel & 8)
157 {
158 if ((chip->noise_data & 3) == 0)
159 {
160 freq |= 16;
161 }
162 if ((chip->noise_data & 3) == 1)
163 {
164 freq |= 32;
165 }
166 if ((chip->noise_data & 3) == 2)
167 {
168 freq |= 64;
169 }
170 }
171 if (chip->chan_sel & 1)
172 {
173 chip->sign ^= chip->counter_of & 15;
174 }
175 if (chip->ic_latch2 & 2)
176 {
177 chip->sign = 0;
178 }
179 chip->counter_of <<= 1;
180 if (chip->counter[chip->rot] >= freq)
181 {
182 chip->counter_of |= 1;
183 chip->counter[chip->rot] = 0;
184 }
185 if (chip->ic_latch2 & 2)
186 {
187 chip->counter[chip->rot] = 0;
188 }
189 chip->counter[chip->rot]++;
190 chip->counter[chip->rot] &= 1023;
191 if ((chip->ic_latch2 & 1) || (chip->chan_sel & 7) != 0)
192 {
193 chip->chan_sel <<= 1;
194 }
195 else
196 {
197 chip->chan_sel <<= 1;
198 chip->chan_sel |= 1;
199 }
200 chip->ic_latch2 <<= 1;
201 chip->ic_latch2 |= chip->ic & 1;
202 chip->noise_trig_l = chip->noise_trig;
203 }
204
205 static void YMPSG_ClockInternal2(ympsg_t *chip)
206 {
207 chip->data_mask = (chip->ic_latch2 >> 1) & 1;
208 chip->reg_reset = (chip->ic_latch2 >> 0) & 1;
209 if (chip->noise_trig_l)
210 {
211 chip->noise_trig = 0;
212 }
213 YMPSG_UpdateRegisters(chip);
214
215 chip->rot = (chip->rot + 1) & 3;
216 chip->sign_l = chip->sign;
217 chip->noise_sign_l = (chip->noise >> 14) & 1;
218 }
219
220 static void YMPSG_UpdateSample(ympsg_t *chip)
221 {
222 uint32_t i;
223 uint8_t sign = chip->sign & 14;
224 sign |= chip->noise_sign_l;
225 if (chip->test & 1)
226 {
227 sign |= 15;
228 }
229 for (i = 0; i < 4; i++)
230 {
231 if ((sign >> (3 - i)) & 1)
232 {
233 chip->volume_out[i] = chip->volume[i];
234 }
235 else
236 {
237 chip->volume_out[i] = 15;
238 }
239 }
240 }
241
242 void YMPSG_Write(ympsg_t *chip, uint8_t data)
243 {
244 chip->data = data;
245 chip->write_flag = 1;
246 }
247
248 void YMPSG_WriteStereo(ympsg_t *chip, uint8_t data)
249 {
250 chip->stereo = data;
251 }
252
253 uint16_t YMPSG_Read(ympsg_t *chip)
254 {
255 uint16_t data = 0;
256 uint32_t i;
257 YMPSG_UpdateSample(chip);
258 for (i = 0; i < 4; i++)
259 {
260 data |= chip->volume_out[i] << ((3 - i) * 4);
261 }
262 return data;
263 }
264
265 void YMPSG_Init(ympsg_t *chip, uint8_t real_sn, uint8_t noise_tap1, uint8_t noise_tap2, uint32_t noise_size)
266 {
267 uint32_t i;
268 memset(chip, 0, sizeof(ympsg_t));
269 YMPSG_SetIC(chip, 1);
270 chip->noise_tap1 = noise_tap1;
271 chip->noise_tap2 = noise_tap2;
272 chip->noise_size = noise_size;
273 chip->stereo = 0xff;
274 for (i = 0; i < 17; i++)
275 {
276 chip->vol_table[i]=(real_sn?tipsg_vol[i]:ympsg_vol[i]) * 8192.0f;
277 }
278 for (i = 0; i < 16; i++)
279 {
280 YMPSG_Clock(chip);
281 }
282 YMPSG_SetIC(chip, 0);
283 }
284
285 void YMPSG_SetIC(ympsg_t *chip, uint32_t ic)
286 {
287 chip->ic = (uint8_t)ic;
288 }
289
290 void YMPSG_Clock(ympsg_t *chip)
291 {
292 uint8_t prescaler2_latch;
293 prescaler2_latch = chip->prescaler_2;
294 chip->prescaler_2 += chip->prescaler_1;
295 chip->prescaler_2 &= 1;
296 chip->prescaler_1 ^= 1;
297 if ((chip->ic_latch1 & 3) == 2)
298 {
299 chip->prescaler_1 = 0;
300 chip->prescaler_2 = 0;
301 }
302 chip->ic_latch1 <<= 1;
303 chip->ic_latch1 |= chip->ic & 1;
304 YMPSG_UpdateRegisters(chip);
305 chip->write_flag_l = 0;
306 if (chip->write_flag)
307 {
308 YMPSG_WriteLatch(chip);
309 chip->write_flag = 0;
310 chip->write_flag_l = 1;
311 }
312 if (chip->prescaler_1)
313 {
314 if (!prescaler2_latch)
315 {
316 YMPSG_ClockInternal1(chip);
317 }
318 else
319 {
320 YMPSG_ClockInternal2(chip);
321 }
322 }
323 }
324
325 void YMPSG_GetOutput(ympsg_t *chip, int* left, int* right)
326 {
327 int sample_left = 0;
328 int sample_right = 0;
329 uint32_t i;
330 YMPSG_UpdateSample(chip);
331 if (chip->test & 1)
332 {
333 sample_left += chip->vol_table[chip->volume_out[chip->test >> 1]];
334 sample_left += chip->vol_table[16] * 3;
335 sample_right += chip->vol_table[chip->volume_out[chip->test >> 1]];
336 sample_right += chip->vol_table[16] * 3;
337 }
338 else if (!chip->mute)
339 {
340 if (chip->stereo&(0x10)) {
341 sample_left += chip->vol_table[chip->volume_out[0]];
342 }
343 if (chip->stereo&(0x01)) {
344 sample_right += chip->vol_table[chip->volume_out[0]];
345 }
346 if (chip->stereo&(0x20)) {
347 sample_left += chip->vol_table[chip->volume_out[1]];
348 }
349 if (chip->stereo&(0x02)) {
350 sample_right += chip->vol_table[chip->volume_out[1]];
351 }
352 if (chip->stereo&(0x40)) {
353 sample_left += chip->vol_table[chip->volume_out[2]];
354 }
355 if (chip->stereo&(0x04)) {
356 sample_right += chip->vol_table[chip->volume_out[2]];
357 }
358 if (chip->stereo&(0x80)) {
359 sample_left += chip->vol_table[chip->volume_out[3]];
360 }
361 if (chip->stereo&(0x08)) {
362 sample_right += chip->vol_table[chip->volume_out[3]];
363 }
364 }
365 else
366 {
367 for (i = 0; i < 4; i++)
368 {
369 if (!((chip->mute>>i) & 1)) {
370 if (chip->stereo&(0x10<<i)) {
371 sample_left += chip->vol_table[chip->volume_out[i]];
372 }
373 if (chip->stereo&(0x01<<i)) {
374 sample_right += chip->vol_table[chip->volume_out[i]];
375 }
376 }
377 }
378 }
379 *left=sample_left;
380 *right=sample_right;
381 }
382
383 void YMPSG_Test(ympsg_t *chip, uint16_t test)
384 {
385 chip->test = (test >> 9) & 7;
386 }
387
388 /*
389 void YMPSG_Generate(ympsg_t *chip, int32_t *buf)
390 {
391 uint32_t i;
392 float out;
393
394 for (i = 0; i < 16; i++)
395 {
396 YMPSG_Clock(chip);
397
398 while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt)
399 {
400 if (!chip->writebuf[chip->writebuf_cur].stat)
401 {
402 break;
403 }
404 chip->writebuf[chip->writebuf_cur].stat = 0;
405 YMPSG_Write(chip, chip->writebuf[chip->writebuf_cur].data);
406 chip->writebuf_cur = (chip->writebuf_cur + 1) % YMPSG_WRITEBUF_SIZE;
407 }
408 chip->writebuf_samplecnt++;
409 }
410 out = YMPSG_GetOutput(chip);
411 *buf = (int32_t)(out * 8192.f);
412 }*/
413
414 void YMPSG_WriteBuffered(ympsg_t *chip, uint8_t data)
415 {
416 uint64_t time1, time2;
417 uint64_t skip;
418
419 if (chip->writebuf[chip->writebuf_last].stat)
420 {
421 YMPSG_Write(chip, chip->writebuf[chip->writebuf_last].data);
422
423 chip->writebuf_cur = (chip->writebuf_last + 1) % YMPSG_WRITEBUF_SIZE;
424 skip = chip->writebuf[chip->writebuf_last].time - chip->writebuf_samplecnt;
425 chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time;
426 while (skip--)
427 {
428 YMPSG_Clock(chip);
429 }
430 }
431
432 chip->writebuf[chip->writebuf_last].stat = 1;
433 chip->writebuf[chip->writebuf_last].data = data;
434 time1 = chip->writebuf_lasttime + YMPSG_WRITEBUF_DELAY;
435 time2 = chip->writebuf_samplecnt;
436
437 if (time1 < time2)
438 {
439 time1 = time2;
440 }
441
442 chip->writebuf[chip->writebuf_last].time = time1;
443 chip->writebuf_lasttime = time1;
444 chip->writebuf_last = (chip->writebuf_last + 1) % YMPSG_WRITEBUF_SIZE;
445 }
446
447 void YMPSG_SetMute(ympsg_t *chip, uint8_t mute)
448 {
449 chip->mute = mute;
450 }