Debian Codegraph @

extern/YM3812-LLE/fmopl2.c

raw
1 /*
2 * Copyright (C) 2023 nukeykt
3 *
4 * This file is part of YM3812-LLE.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * YM3812 emulator
17 * Thanks:
18 * Travis Goodspeed:
19 * YM3812 decap and die shot
20 *
21 */
22
23 #include "fmopl2.h"
24
25
26 void FMOPL2_DoShiftRegisters(fmopl2_t *chip, int sel)
27 {
28 int j;
29 int to = sel;
30 int from = sel ^ 1;
31 int rot = sel == 0 ? 1 : 0;
32 #define CH_ROTATE(x) rot ? ((x << 1) | ((x >> 8) & 1)) : x
33 #define OP_ROTATE(x) rot ? ((x << 1) | ((x >> 17) & 1)) : x
34 // channel registers
35
36 // fnum
37 for (j = 0; j < 10; j++)
38 chip->ch_fnum[j][to] = CH_ROTATE(chip->ch_fnum[j][from]);
39 // block
40 for (j = 0; j < 3; j++)
41 chip->ch_block[j][to] = CH_ROTATE(chip->ch_block[j][from]);
42 // kon
43 chip->ch_keyon[to] = CH_ROTATE(chip->ch_keyon[from]);
44 // connect
45 chip->ch_connect[to] = CH_ROTATE(chip->ch_connect[from]);
46 // feedback
47 for (j = 0; j < 3; j++)
48 chip->ch_fb[j][to] = CH_ROTATE(chip->ch_fb[j][from]);
49 // multi
50 for (j = 0; j < 4; j++)
51 chip->op_multi[j][to] = OP_ROTATE(chip->op_multi[j][from]);
52 // ksr
53 chip->op_ksr[to] = OP_ROTATE(chip->op_ksr[from]);
54 // egt
55 chip->op_egt[to] = OP_ROTATE(chip->op_egt[from]);
56 // vib
57 chip->op_vib[to] = OP_ROTATE(chip->op_vib[from]);
58 // am
59 chip->op_am[to] = OP_ROTATE(chip->op_am[from]);
60 // tl
61 for (j = 0; j < 6; j++)
62 chip->op_tl[j][to] = OP_ROTATE(chip->op_tl[j][from]);
63 // ksl
64 for (j = 0; j < 2; j++)
65 chip->op_ksl[j][to] = OP_ROTATE(chip->op_ksl[j][from]);
66 // ar
67 for (j = 0; j < 4; j++)
68 chip->op_ar[j][to] = OP_ROTATE(chip->op_ar[j][from]);
69 // dr
70 for (j = 0; j < 4; j++)
71 chip->op_dr[j][to] = OP_ROTATE(chip->op_dr[j][from]);
72 // sl
73 for (j = 0; j < 4; j++)
74 chip->op_sl[j][to] = OP_ROTATE(chip->op_sl[j][from]);
75 // rr
76 for (j = 0; j < 4; j++)
77 chip->op_rr[j][to] = OP_ROTATE(chip->op_rr[j][from]);
78 // wf
79 for (j = 0; j < 2; j++)
80 chip->op_wf[j][to] = OP_ROTATE(chip->op_wf[j][from]);
81 #undef CH_ROTATE
82 #undef OP_ROTATE
83 }
84
85 enum {
86 eg_state_attack = 0,
87 eg_state_decay,
88 eg_state_sustain,
89 eg_state_release
90 };
91
92 void FMOPL2_Clock(fmopl2_t *chip)
93 {
94 int i;
95
96 chip->mclk1 = !chip->input.mclk;
97 chip->mclk2 = chip->input.mclk;
98
99 chip->reset1 = !chip->input.ic;
100 chip->io_rd = !chip->input.rd;
101 chip->io_wr = !chip->input.wr;
102 chip->io_cs = !chip->input.cs;
103 chip->io_a0 = chip->input.address & 1;
104
105 if (chip->mclk1)
106 {
107 int prescaler_reset = !(chip->prescaler_reset_l[1] & 2) && chip->reset1;
108 chip->prescaler_reset_l[0] = (chip->prescaler_reset_l[1] << 1) | chip->reset1;
109 if (prescaler_reset)
110 chip->prescaler_cnt[0] = 0;
111 else
112 chip->prescaler_cnt[0] = (chip->prescaler_cnt[1] + 1) & 3;
113
114 chip->prescaler_l1[0] = !prescaler_reset && chip->prescaler_cnt[1] == 1;
115 chip->prescaler_l2[0] = chip->prescaler_cnt[1] == 3;
116 }
117 if (chip->mclk2)
118 {
119 chip->prescaler_reset_l[1] = chip->prescaler_reset_l[0];
120 chip->prescaler_cnt[1] = chip->prescaler_cnt[0];
121 chip->prescaler_l1[1] = chip->prescaler_l1[0];
122 chip->prescaler_l2[1] = chip->prescaler_l2[0];
123 }
124
125 chip->clk1 = chip->prescaler_l1[1];
126 chip->clk2 = chip->prescaler_l2[1];
127
128 chip->io_read0 = !chip->reset1 && chip->io_cs && chip->io_rd && !chip->io_a0;
129 chip->io_read1 = !chip->reset1 && chip->io_cs && chip->io_rd && chip->io_a0;
130 chip->io_write = !chip->reset1 && chip->io_cs && chip->io_wr;
131 chip->io_write0 = !chip->reset1 && chip->io_cs && chip->io_wr && !chip->io_a0;
132 chip->io_write1 = !chip->reset1 && chip->io_cs && chip->io_wr && chip->io_a0;
133 chip->io_dir = chip->io_cs && chip->io_rd;
134
135 int irq = chip->t1_status || chip->t2_status || chip->unk_status1 || chip->unk_status2;
136
137 if (!chip->io_dir)
138 chip->io_data = chip->input.data_i;
139
140 if (chip->io_write)
141 chip->data_latch = chip->io_data;
142
143 if (chip->write0)
144 chip->write0_sr = 0;
145 else if (chip->io_write0)
146 chip->write0_sr = 1;
147
148 if (chip->write1)
149 chip->write1_sr = 0;
150 else if (chip->io_write1)
151 chip->write1_sr = 1;
152
153 if (chip->mclk1)
154 {
155 chip->write0_latch[1] = chip->write0_latch[0];
156 chip->write1_latch[1] = chip->write1_latch[0];
157 }
158 if (chip->mclk2)
159 {
160 chip->write0_latch[0] = chip->write0_sr;
161 chip->write0_latch[2] = chip->write0_latch[1];
162
163 chip->write1_latch[0] = chip->write1_sr;
164 chip->write1_latch[2] = chip->write1_latch[1];
165 }
166
167 if (chip->clk1)
168 {
169 chip->write0_latch[4] = chip->write0_latch[3];
170 chip->write1_latch[4] = chip->write1_latch[3];
171 }
172 if (chip->clk2)
173 {
174 chip->write0_latch[3] = chip->write0_latch[2];
175 chip->write0_latch[5] = chip->write0_latch[4];
176
177 chip->write1_latch[3] = chip->write1_latch[2];
178 chip->write1_latch[5] = chip->write1_latch[4];
179 }
180
181 chip->write0 = chip->write0_latch[5];
182 chip->write1 = chip->write1_latch[5];
183
184 ////
185
186 if (chip->o_clk1 == chip->clk1 && chip->o_clk2 == chip->clk2 && chip->o_reset1 == chip->reset1
187 && chip->o_write0 == chip->write0 && chip->o_write1 == chip->write1 && chip->o_data_latch == chip->data_latch)
188 goto end; // opt
189
190 chip->o_clk1 = chip->clk1;
191 chip->o_clk2 = chip->clk2;
192 chip->o_reset1 = chip->reset1;
193 chip->o_write0 = chip->write0;
194 chip->o_write1 = chip->write1;
195 chip->o_data_latch = chip->data_latch;
196
197 if (chip->write0)
198 {
199 chip->reg_sel1 = chip->data_latch == 1;
200 chip->reg_sel2 = chip->data_latch == 2;
201 chip->reg_sel3 = chip->data_latch == 3;
202 chip->reg_sel4 = chip->data_latch == 4;
203 chip->reg_sel8 = chip->data_latch == 8;
204 chip->reg_selbd = chip->data_latch == 0xbd;
205 }
206
207 chip->reg_sel4_wr = chip->write1 && chip->reg_sel4 && (chip->data_latch & 128) == 0;
208 chip->reg_sel4_rst = (chip->write1 && chip->reg_sel4 && (chip->data_latch & 128) != 0) || chip->reset1;
209
210 if (chip->reset1)
211 {
212 chip->reg_test = 0;
213 chip->reg_timer1 = 0;
214 chip->reg_timer2 = 0;
215 chip->reg_notesel = 0;
216 chip->reg_csm = 0;
217 chip->rhythm = 0;
218 chip->reg_rh_kon = 0;
219 chip->reg_da = 0;
220 chip->reg_dv = 0;
221 }
222 else if (chip->write1)
223 {
224 if (chip->reg_sel1)
225 chip->reg_test = chip->data_latch & 255;
226 if (chip->reg_sel2)
227 chip->reg_timer1 = chip->data_latch & 255;
228 if (chip->reg_sel3)
229 chip->reg_timer2 = chip->data_latch & 255;
230 if (chip->reg_sel8)
231 {
232 chip->reg_notesel = (chip->data_latch & 64) != 0;
233 chip->reg_csm = (chip->data_latch & 128) != 0;
234 }
235 if (chip->reg_selbd)
236 {
237 chip->reg_rh_kon = chip->data_latch & 31;
238 chip->rhythm = (chip->data_latch & 32) != 0;
239 chip->reg_dv = (chip->data_latch & 64) != 0;
240 chip->reg_da = (chip->data_latch & 128) != 0;
241 }
242 }
243
244 if (chip->reset1)
245 {
246 chip->reg_t1_mask = 0;
247 chip->reg_t2_mask = 0;
248 chip->reg_t1_start = 0;
249 chip->reg_t2_start = 0;
250 chip->reg_mode_b3 = 0;
251 chip->reg_mode_b4 = 0;
252 }
253 else if (chip->reg_sel4_wr)
254 {
255 chip->reg_t1_mask = (chip->data_latch & 64) != 0;
256 chip->reg_t2_mask = (chip->data_latch & 32) != 0;
257 chip->reg_t1_start = (chip->data_latch & 1) != 0;
258 chip->reg_t2_start = (chip->data_latch & 2) != 0;
259 chip->reg_mode_b3 = (chip->data_latch & 8) != 0;
260 chip->reg_mode_b4 = (chip->data_latch & 16) != 0;
261 }
262
263 {
264 chip->fsm_reset = !(chip->fsm_reset_l[1] & 2) && chip->reset1;
265 chip->fsm_cnt1_of = (chip->fsm_cnt1[1] & 5) == 5;
266 chip->fsm_cnt2_of = chip->fsm_cnt1_of && (chip->fsm_cnt2[1] & 2) != 0;
267
268 chip->fsm_cnt = (chip->fsm_cnt2[1] << 3) | chip->fsm_cnt1[1];
269
270 chip->fsm_sel[0] = chip->fsm_cnt == 20 && chip->rhythm;
271 chip->fsm_sel[1] = chip->fsm_cnt == 19 && chip->rhythm;
272 chip->fsm_sel[2] = chip->fsm_cnt == 18 && chip->rhythm;
273 chip->fsm_sel[3] = chip->fsm_cnt == 17 && chip->rhythm;
274 chip->fsm_sel[4] = chip->fsm_cnt == 16 && chip->rhythm;
275 chip->fsm_sel[5] = chip->fsm_cnt == 20 && chip->rhythm;
276 chip->fsm_sel[6] = chip->fsm_cnt == 19 && chip->rhythm;
277 chip->fsm_sel[7] = (chip->fsm_cnt & 5) == 4;
278 chip->fsm_sel[8] = chip->fsm_cnt == 16;
279 chip->fsm_sel[9] = (chip->fsm_cnt & 29) == 5;
280 chip->fsm_sel[10] = chip->fsm_cnt == 16;
281 chip->fsm_sel[11] = chip->fsm_cnt == 11;
282 chip->fsm_sel[12] = chip->fsm_cnt == 20;
283
284 int fsm_mc = !(chip->fsm_sel[7] || (chip->fsm_cnt & 2) != 0);
285
286 chip->fsm_out[0] = ((chip->connect_l[1] & 2) != 0 || chip->fsm_sel[0] || chip->fsm_sel[1] || fsm_mc) && !chip->fsm_sel[2];
287
288 chip->fsm_out[1] = fsm_mc && !chip->fsm_sel[3] && !chip->fsm_sel[4];
289
290 chip->fsm_out[2] = !fsm_mc && !chip->fsm_sel[5] && !chip->fsm_sel[6];
291
292 chip->fsm_out[3] = !(chip->fsm_l1[1] && 1);
293
294 chip->fsm_out[4] = chip->fsm_l2[1];
295
296 chip->fsm_out[5] = chip->fsm_sel[10];
297
298 chip->fsm_out[6] = chip->fsm_sel[11];
299
300 chip->fsm_out[7] = chip->fsm_sel[12];
301
302 chip->fsm_out[8] = (chip->fsm_l3[1] & 1) != 0;
303
304 chip->fsm_out[9] = (chip->fsm_l3[1] & 2) != 0;
305
306 chip->fsm_out[10] = (chip->fsm_l3[1] & 2) != 0;
307
308 chip->fsm_out[11] = (chip->fsm_l4[1] & 2) != 0 && chip->rhythm;
309
310 chip->fsm_out[12] = (chip->fsm_l5[1] & 4) != 0;
311
312 chip->fsm_out[13] = (chip->fsm_l6[1] & 4) != 0;
313
314 chip->fsm_out[14] = !(chip->fsm_out[12] || (chip->fsm_cnt & 16) != 0);
315
316 chip->fsm_out[15] = !(chip->fsm_out[12] || chip->fsm_out[13]);
317 }
318
319 if (chip->clk1)
320 {
321 if (chip->fsm_reset || chip->fsm_cnt1_of)
322 chip->fsm_cnt1[0] = 0;
323 else
324 chip->fsm_cnt1[0] = (chip->fsm_cnt1[1] + 1) & 7;
325 if (chip->fsm_reset || chip->fsm_cnt2_of)
326 chip->fsm_cnt2[0] = 0;
327 else
328 chip->fsm_cnt2[0] = (chip->fsm_cnt2[1] + chip->fsm_cnt1_of) & 3;
329
330 chip->fsm_reset_l[0] = (chip->fsm_reset_l[1] << 1) | chip->reset1;
331
332 chip->fsm_l1[0] = !chip->fsm_sel[8] && !chip->fsm_sel[9] && (chip->fsm_cnt & 8) == 0;
333
334 chip->fsm_l2[0] = chip->fsm_sel[10];
335
336 chip->fsm_l3[0] = (chip->fsm_l3[1] << 1) | chip->fsm_sel[12];
337
338 chip->fsm_l4[0] = (chip->fsm_l4[1] << 1) | ((chip->fsm_cnt & 16) != 0);
339
340 chip->fsm_l5[0] = (chip->fsm_l5[1] << 1) | ((chip->fsm_cnt & 8) != 0);
341
342 chip->fsm_l6[0] = (chip->fsm_l6[1] << 1) | ((chip->fsm_cnt & 16) != 0);
343 }
344 if (chip->clk2)
345 {
346 chip->fsm_cnt1[1] = chip->fsm_cnt1[0];
347 chip->fsm_cnt2[1] = chip->fsm_cnt2[0];
348 chip->fsm_reset_l[1] = chip->fsm_reset_l[0];
349 chip->fsm_l1[1] = chip->fsm_l1[0];
350 chip->fsm_l2[1] = chip->fsm_l2[0];
351 chip->fsm_l3[1] = chip->fsm_l3[0];
352 chip->fsm_l4[1] = chip->fsm_l4[0];
353 chip->fsm_l5[1] = chip->fsm_l5[0];
354 chip->fsm_l6[1] = chip->fsm_l6[0];
355 }
356
357 if (chip->clk1)
358 chip->timer_st_load_l = chip->fsm_out[8];
359 chip->timer_st_load = chip->fsm_out[8] && !chip->timer_st_load_l;
360
361 if (chip->timer_st_load)
362 chip->t1_start = chip->reg_t1_start;
363
364 if (chip->clk1)
365 {
366 int lfo = chip->lfo_cnt[1];
367 int add = chip->fsm_out[8];
368
369 chip->lfo_cnt[0] = (chip->reg_test & 128) != 0 ? 0 : (lfo + add) & 1023;
370 chip->vib_cnt[0] = (chip->reg_test & 128) != 0 ? 0 : (chip->vib_cnt[1] + chip->vib_step) & 7;
371 }
372 if (chip->clk2)
373 {
374 chip->lfo_cnt[1] = chip->lfo_cnt[0];
375 chip->vib_cnt[1] = chip->vib_cnt[0];
376 }
377
378 {
379 int lfo = chip->lfo_cnt[1];
380 int add = chip->fsm_out[8];
381
382 chip->t1_step = (((lfo & 3) + add) & 4) != 0;
383 chip->t2_step = (((lfo & 15) + add) & 16) != 0;
384 chip->am_step = (((lfo & 63) + add) & 64) != 0;
385 chip->vib_step = (((lfo & 1023) + add) & 1024) != 0;
386 chip->vib_step |= (chip->reg_test & 8) != 0 && add;
387 }
388
389 if (chip->clk1)
390 {
391 int value = chip->t1_load ? chip->reg_timer1 : chip->t1_cnt[1];
392 value += ((chip->t1_start_l[1] & 1) != 0 && chip->t1_step) || (chip->reg_test & 2) != 0;
393 chip->t1_of[0] = (value & 256) != 0;
394 chip->t1_cnt[0] = (chip->t1_start_l[1] & 1) == 0 ? 0 : (value & 255);
395
396 value = (chip->t2_of[1] || (chip->t2_start_l[1] & 3) == 1) ? chip->reg_timer2 : chip->t2_cnt[1];
397 value += ((chip->t2_start_l[1] & 1) != 0 && chip->t2_step) || (chip->reg_test & 2) != 0;
398 chip->t2_of[0] = (value & 256) != 0;
399 chip->t2_cnt[0] = (chip->t2_start_l[1] & 1) == 0 ? 0 : (value & 255);
400
401 chip->t1_start_l[0] = (chip->t1_start_l[1] << 1) | chip->t1_start;
402 chip->t2_start_l[0] = (chip->t2_start_l[1] << 1) | chip->reg_t2_start;
403 }
404 if (chip->clk2)
405 {
406 chip->t1_cnt[1] = chip->t1_cnt[0];
407 chip->t1_of[1] = chip->t1_of[0];
408 chip->t2_cnt[1] = chip->t2_cnt[0];
409 chip->t2_of[1] = chip->t2_of[0];
410
411 chip->t1_start_l[1] = chip->t1_start_l[0];
412 chip->t2_start_l[1] = chip->t2_start_l[0];
413
414 chip->t1_load = (chip->t1_of[1] || (chip->t1_start_l[1] & 3) == 1); // opt
415 }
416
417 if (chip->reg_sel4_rst || chip->reg_t1_mask)
418 chip->t1_status = 0;
419 else if (chip->t1_of[1])
420 chip->t1_status = 1;
421
422 if (chip->reg_sel4_rst || chip->reg_t2_mask)
423 chip->t2_status = 0;
424 else if (chip->t2_of[1])
425 chip->t2_status = 1;
426
427 if (chip->reg_sel4_rst || chip->reg_mode_b4)
428 chip->unk_status1 = 0;
429 else if (0)
430 chip->unk_status1 = 1;
431
432 chip->unk_status2 = 0;
433
434 if (chip->clk1)
435 chip->csm_load_l = chip->fsm_out[10];
436 chip->csm_load = chip->fsm_out[10] && !chip->csm_load_l;
437
438 if (chip->csm_load)
439 chip->csm_kon = chip->reg_csm && chip->t1_load;
440
441 chip->rh_sel0 = chip->rhythm && chip->fsm_out[5];
442
443 if (chip->clk1)
444 {
445 chip->rh_sel[0] = (chip->rh_sel[1] << 1) | chip->rh_sel0;
446 }
447 if (chip->clk2)
448 {
449 chip->rh_sel[1] = chip->rh_sel[0];
450 }
451
452 //if (chip->clk1) // opt
453 {
454 chip->keyon_comb = chip->keyon || chip->csm_kon
455 || (chip->rh_sel0 && (chip->reg_rh_kon & 16) != 0) // bd0
456 || ((chip->rh_sel[1] & 1) != 0 && (chip->reg_rh_kon & 1) != 0) // hh
457 || ((chip->rh_sel[1] & 2) != 0 && (chip->reg_rh_kon & 4) != 0) // tom
458 || ((chip->rh_sel[1] & 4) != 0 && (chip->reg_rh_kon & 16) != 0) // bd1
459 || ((chip->rh_sel[1] & 8) != 0 && (chip->reg_rh_kon & 8) != 0) // sd
460 || ((chip->rh_sel[1] & 16) != 0 && (chip->reg_rh_kon & 2) != 0); // tc
461 }
462
463 if (chip->reset1)
464 chip->address = 0;
465 else if ((chip->data_latch & 0xe0) != 0 && chip->write0)
466 chip->address = chip->data_latch;
467
468 if (chip->write0)
469 chip->address_valid = (chip->data_latch & 0xe0) != 0;
470
471 if (chip->reset1)
472 chip->data = 0;
473 else if (chip->address_valid && chip->write1)
474 chip->data = chip->data_latch;
475
476 chip->address_valid2 = chip->address_valid_l[1] && !chip->write0;
477
478 if (chip->clk1)
479 {
480 chip->address_valid_l[0] = (chip->address_valid && chip->write1) || chip->address_valid2;
481
482 int slot_cnt1_of = (chip->slot_cnt1[1] & 5) == 5;
483
484 if (chip->fsm_out[8] || slot_cnt1_of)
485 chip->slot_cnt1[0] = 0;
486 else
487 chip->slot_cnt1[0] = (chip->slot_cnt1[1] + 1) & 7;
488
489 if (chip->fsm_out[8] || (slot_cnt1_of && (chip->slot_cnt2[1] & 2) != 0))
490 chip->slot_cnt2[0] = 0;
491 else
492 chip->slot_cnt2[0] = (chip->slot_cnt2[1] + slot_cnt1_of) & 3;
493 }
494 if (chip->clk2)
495 {
496 chip->address_valid_l[1] = chip->address_valid_l[0];
497
498 chip->slot_cnt1[1] = chip->slot_cnt1[0];
499 chip->slot_cnt2[1] = chip->slot_cnt2[0];
500
501 chip->slot_cnt = (chip->slot_cnt2[1] << 3) | chip->slot_cnt1[1]; // opt
502 }
503
504 if (chip->clk1)
505 {
506 int sel_ch = (chip->address & 0xf0) == 0xa0 || (chip->address & 0xf0) == 0xb0 || (chip->address & 0xf0) == 0xc0;
507 int addr_add = sel_ch && ((chip->address & 8) != 0 || (chip->address & 6) == 6);
508
509 int addr_sel = chip->address & 1;
510
511 addr_sel |= (((chip->address >> 1) + addr_add) & 7) << 1;
512
513 if (!sel_ch)
514 addr_sel |= chip->address & 16;
515
516 int addr_match = addr_sel == chip->slot_cnt && chip->address_valid2;
517
518 int sel_20 = (chip->address & 0xe0) == 0x20 && addr_match;
519 int sel_40 = (chip->address & 0xe0) == 0x40 && addr_match;
520 int sel_60 = (chip->address & 0xe0) == 0x60 && addr_match;
521 int sel_80 = (chip->address & 0xe0) == 0x80 && addr_match;
522 int sel_e0 = (chip->address & 0xe0) == 0xe0 && addr_match && (chip->reg_test & 32) != 0;
523
524 int sel_a0 = (chip->address & 0xf0) == 0xa0 && addr_match;
525 int sel_b0 = (chip->address & 0xf0) == 0xb0 && addr_match;
526 int sel_c0 = (chip->address & 0xf0) == 0xc0 && addr_match;
527
528 FMOPL2_DoShiftRegisters(chip, 0);
529
530 if (chip->reset1)
531 {
532 for (i = 0; i < 10; i++)
533 chip->ch_fnum[i][0] &= ~1;
534 for (i = 0; i < 3; i++)
535 chip->ch_block[i][0] &= ~1;
536 chip->ch_keyon[0] &= ~1;
537 chip->ch_connect[0] &= ~1;
538 for (i = 0; i < 3; i++)
539 chip->ch_fb[i][0] &= ~1;
540
541 for (i = 0; i < 4; i++)
542 chip->op_multi[i][0] &= ~1;
543 chip->op_ksr[0] &= ~1;
544 chip->op_egt[0] &= ~1;
545 chip->op_vib[0] &= ~1;
546 chip->op_am[0] &= ~1;
547 for (i = 0; i < 6; i++)
548 chip->op_tl[i][0] &= ~1;
549 for (i = 0; i < 2; i++)
550 chip->op_ksl[i][0] &= ~1;
551 for (i = 0; i < 4; i++)
552 chip->op_ar[i][0] &= ~1;
553 for (i = 0; i < 4; i++)
554 chip->op_dr[i][0] &= ~1;
555 for (i = 0; i < 4; i++)
556 chip->op_sl[i][0] &= ~1;
557 for (i = 0; i < 4; i++)
558 chip->op_rr[i][0] &= ~1;
559 for (i = 0; i < 2; i++)
560 chip->op_wf[i][0] &= ~1;
561 }
562 else
563 {
564 if (sel_a0)
565 {
566 for (i = 0; i < 8; i++)
567 chip->ch_fnum[i][0] &= ~1;
568
569 for (i = 0; i < 8; i++)
570 chip->ch_fnum[i][0] |= (chip->data >> i) & 1;
571 }
572 if (sel_b0)
573 {
574 for (i = 8; i < 10; i++)
575 chip->ch_fnum[i][0] &= ~1;
576 for (i = 0; i < 3; i++)
577 chip->ch_block[i][0] &= ~1;
578 chip->ch_keyon[0] &= ~1;
579
580 for (i = 8; i < 10; i++)
581 chip->ch_fnum[i][0] |= (chip->data >> (i - 8)) & 1;
582 for (i = 0; i < 3; i++)
583 chip->ch_block[i][0] |= (chip->data >> (i + 2)) & 1;
584 chip->ch_keyon[0] |= (chip->data >> 5) & 1;
585 }
586 if (sel_c0)
587 {
588 chip->ch_connect[0] &= ~1;
589 for (i = 0; i < 3; i++)
590 chip->ch_fb[i][0] &= ~1;
591
592 chip->ch_connect[0] |= (chip->data >> 0) & 1;
593 for (i = 0; i < 3; i++)
594 chip->ch_fb[i][0] |= (chip->data >> (i + 1)) & 1;
595 }
596 if (sel_20)
597 {
598 for (i = 0; i < 4; i++)
599 chip->op_multi[i][0] &= ~1;
600 chip->op_ksr[0] &= ~1;
601 chip->op_egt[0] &= ~1;
602 chip->op_vib[0] &= ~1;
603 chip->op_am[0] &= ~1;
604
605 for (i = 0; i < 4; i++)
606 chip->op_multi[i][0] |= (chip->data >> i) & 1;
607 chip->op_ksr[0] |= (chip->data >> 4) & 1;
608 chip->op_egt[0] |= (chip->data >> 5) & 1;
609 chip->op_vib[0] |= (chip->data >> 6) & 1;
610 chip->op_am[0] |= (chip->data >> 7) & 1;
611 }
612 if (sel_40)
613 {
614 for (i = 0; i < 6; i++)
615 chip->op_tl[i][0] &= ~1;
616 for (i = 0; i < 2; i++)
617 chip->op_ksl[i][0] &= ~1;
618
619 for (i = 0; i < 6; i++)
620 chip->op_tl[i][0] |= (chip->data >> i) & 1;
621 for (i = 0; i < 2; i++)
622 chip->op_ksl[i][0] |= (chip->data >> (i + 6)) & 1;
623 }
624 if (sel_60)
625 {
626 for (i = 0; i < 4; i++)
627 chip->op_ar[i][0] &= ~1;
628 for (i = 0; i < 4; i++)
629 chip->op_dr[i][0] &= ~1;
630
631 for (i = 0; i < 4; i++)
632 chip->op_ar[i][0] |= (chip->data >> (i + 4)) & 1;
633 for (i = 0; i < 4; i++)
634 chip->op_dr[i][0] |= (chip->data >> i) & 1;
635 }
636 if (sel_80)
637 {
638 for (i = 0; i < 4; i++)
639 chip->op_sl[i][0] &= ~1;
640 for (i = 0; i < 4; i++)
641 chip->op_rr[i][0] &= ~1;
642
643 for (i = 0; i < 4; i++)
644 chip->op_sl[i][0] |= (chip->data >> (i + 4)) & 1;
645 for (i = 0; i < 4; i++)
646 chip->op_rr[i][0] |= (chip->data >> i) & 1;
647 }
648 if (sel_e0)
649 {
650 for (i = 0; i < 2; i++)
651 chip->op_wf[i][0] &= ~1;
652
653 for (i = 0; i < 2; i++)
654 chip->op_wf[i][0] |= (chip->data >> i) & 1;
655 }
656 }
657 }
658 if (chip->clk2)
659 {
660 FMOPL2_DoShiftRegisters(chip, 1);
661 }
662
663 //if (chip->clk2) // opt
664 {
665 int shift = 0;
666
667 if (chip->fsm_out[13])
668 shift = 8;
669 else if (chip->fsm_out[12])
670 shift = 5;
671 else if (chip->fsm_out[15])
672 shift = 2;
673
674 chip->block = 0;
675 chip->fnum = 0;
676 for (i = 0; i < 3; i++)
677 chip->block |= ((chip->ch_block[i][1] >> shift) & 1) << i;
678 for (i = 0; i < 10; i++)
679 chip->fnum |= ((chip->ch_fnum[i][1] >> shift) & 1) << i;
680 chip->keyon = (chip->ch_keyon[1] >> shift) & 1;
681 chip->connect = (chip->ch_connect[1] >> shift) & 1;
682
683 chip->fb = 0;
684 if (chip->fsm_out[13])
685 shift = 5;
686 else if (chip->fsm_out[12])
687 shift = 2;
688 else if (chip->fsm_out[15])
689 shift = 8;
690 for (i = 0; i < 3; i++)
691 chip->fb |= ((chip->ch_fb[i][1] >> shift) & 1) << i;
692
693 chip->multi = 0;
694 chip->tl = 0;
695 chip->ksl = 0;
696 chip->ar = 0;
697 chip->dr = 0;
698 chip->sl = 0;
699 chip->rr = 0;
700 chip->wf = 0;
701
702 for (i = 0; i < 4; i++)
703 chip->multi |= ((chip->op_multi[i][1] >> 17) & 1) << i;
704
705 chip->ksr = (chip->op_ksr[1] >> 17) & 1;
706 chip->egt = (chip->op_egt[1] >> 17) & 1;
707 chip->vib = (chip->op_vib[1] >> 17) & 1;
708 chip->am = (chip->op_am[1] >> 17) & 1;
709
710 for (i = 0; i < 6; i++)
711 chip->tl |= ((chip->op_tl[i][1] >> 17) & 1) << i;
712
713 for (i = 0; i < 2; i++)
714 chip->ksl |= ((chip->op_ksl[i][1] >> 17) & 1) << i;
715
716 for (i = 0; i < 4; i++)
717 chip->ar |= ((chip->op_ar[i][1] >> 17) & 1) << i;
718
719 for (i = 0; i < 4; i++)
720 chip->dr |= ((chip->op_dr[i][1] >> 17) & 1) << i;
721
722 for (i = 0; i < 4; i++)
723 chip->sl |= ((chip->op_sl[i][1] >> 17) & 1) << i;
724
725 for (i = 0; i < 4; i++)
726 chip->rr |= ((chip->op_rr[i][1] >> 17) & 1) << i;
727
728 for (i = 0; i < 2; i++)
729 chip->wf |= ((chip->op_wf[i][1] >> 17) & 1) << i;
730
731 }
732
733 if (chip->clk1)
734 {
735 chip->connect_l[0] = (chip->connect_l[1] << 1) | chip->connect;
736 chip->fb_l[0][0] = chip->fb;
737 chip->fb_l[1][0] = chip->fb_l[0][1];
738 }
739 if (chip->clk2)
740 {
741 chip->connect_l[1] = chip->connect_l[0];
742 chip->fb_l[0][1] = chip->fb_l[0][0];
743 chip->fb_l[1][1] = chip->fb_l[1][0];
744 }
745
746 if (chip->clk1)
747 {
748 chip->eg_load1_l = chip->fsm_out[8];
749 chip->eg_load2_l = chip->fsm_out[9];
750 chip->eg_load3_l = chip->eg_subcnt_l[1] && chip->eg_sync_l[1];
751 }
752 chip->eg_load1 = !chip->eg_load1_l && chip->fsm_out[8];
753 chip->eg_load2 = !chip->eg_load2_l && chip->fsm_out[9];
754 chip->eg_load3 = !chip->eg_load3_l && chip->eg_subcnt_l[1] && chip->eg_sync_l[1];
755
756 {
757
758 if (chip->eg_load1)
759 chip->trem_step = chip->am_step;
760 if (chip->eg_load2)
761 chip->trem_out = chip->trem_value[1] & 127;
762
763 if (chip->clk1)
764 {
765 int bit = chip->trem_value[1] & 1;
766 int reset = chip->reset1 || (chip->reg_test & 128) != 0;
767
768 int step = ((chip->trem_step || (chip->reg_test & 8) != 0) && (chip->fsm_out[9] || chip->trem_dir[1]))
769 && chip->fsm_out[14];
770 int carry = chip->fsm_out[14] && chip->trem_carry[1];
771
772 bit += step + carry;
773
774 int of = (chip->trem_out == 0) || (chip->trem_out & 105) == 105;
775
776 chip->trem_carry[0] = (bit & 2) != 0;
777 chip->trem_value[0] = (chip->trem_value[1] >> 1) & 255;
778 if (!reset)
779 chip->trem_value[0] |= (bit & 1) << 8;
780 chip->trem_of[0] = of;
781
782 if (reset)
783 chip->trem_dir[0] = 0;
784 else
785 chip->trem_dir[0] = chip->trem_dir[1] ^ (of && !chip->trem_of[1]);
786 }
787 if (chip->clk2)
788 {
789 chip->trem_carry[1] = chip->trem_carry[0];
790 chip->trem_value[1] = chip->trem_value[0];
791 chip->trem_of[1] = chip->trem_of[0];
792 chip->trem_dir[1] = chip->trem_dir[0];
793 }
794 }
795
796 {
797
798 if (chip->eg_load3)
799 {
800 chip->eg_timer_low = chip->eg_timer[1] & 3;
801 chip->eg_shift = 0;
802 if (chip->eg_timer_masked[1] & 0x1555)
803 chip->eg_shift |= 1;
804 if (chip->eg_timer_masked[1] & 0x666)
805 chip->eg_shift |= 2;
806 if (chip->eg_timer_masked[1] & 0x1878)
807 chip->eg_shift |= 4;
808 if (chip->eg_timer_masked[1] & 0x1f80)
809 chip->eg_shift |= 8;
810 }
811
812 if (chip->clk1)
813 {
814 int bit = chip->eg_timer[1] & 1;
815 int bit2;
816 int carry = chip->eg_carry[1] || (chip->eg_subcnt[1] && chip->eg_sync_l[1]);
817 bit += carry;
818
819 if (chip->reset1)
820 bit2 = 0;
821 else
822 bit2 = bit & 1;
823
824 chip->eg_timer[0] = (chip->eg_timer[1] >> 1) & 0x1ffff;
825 chip->eg_timer[0] |= bit2 << 17;
826 chip->eg_carry[0] = (bit & 2) != 0;
827 chip->eg_sync_l[0] = chip->fsm_out[8];
828 chip->eg_mask[0] = (chip->reset1 || chip->fsm_out[8]) ? 0 :
829 (chip->eg_mask[1] || bit2);
830 chip->eg_timer_masked[0] = (chip->eg_timer_masked[1] >> 1) & 0x1ffff;
831 if (!chip->eg_mask[1])
832 chip->eg_timer_masked[0] |= bit2 << 17;
833
834 if (chip->reset1)
835 chip->eg_subcnt[0] = 0;
836 else
837 chip->eg_subcnt[0] = chip->eg_subcnt[1] ^ chip->fsm_out[8];
838
839 chip->eg_subcnt_l[0] = chip->eg_subcnt[1];
840 }
841 if (chip->clk2)
842 {
843 chip->eg_timer[1] = chip->eg_timer[0];
844 chip->eg_carry[1] = chip->eg_carry[0];
845 chip->eg_sync_l[1] = chip->eg_sync_l[0];
846 chip->eg_mask[1] = chip->eg_mask[0];
847 chip->eg_timer_masked[1] = chip->eg_timer_masked[0];
848 chip->eg_subcnt[1] = chip->eg_subcnt[0];
849 chip->eg_subcnt_l[1] = chip->eg_subcnt_l[0];
850 }
851 }
852
853 if (chip->clk1)
854 {
855 static const int eg_stephi[4][4] = {
856 { 0, 0, 0, 0 },
857 { 1, 0, 0, 0 },
858 { 1, 0, 1, 0 },
859 { 1, 1, 1, 0 }
860 };
861
862 int state = 0;
863 if (chip->eg_state[0][1] & 0x20000)
864 state |= 1;
865 if (chip->eg_state[1][1] & 0x20000)
866 state |= 2;
867
868 int dokon = state == eg_state_release && chip->keyon_comb;
869 int rate_sel = dokon ? eg_state_attack : state;
870 int rate = 0;
871 int ksr;
872 if (rate_sel == 0)
873 rate |= chip->ar;
874 if (rate_sel == 1)
875 rate |= chip->dr;
876 if (rate_sel == 3 || (rate_sel == 2 && !chip->egt))
877 rate |= chip->rr;
878
879 int sl = chip->sl;
880 if (chip->sl == 15)
881 sl |= 16;
882
883 int ns = chip->reg_notesel ? (chip->fnum & 256) != 0 : (chip->fnum & 512) != 0;
884
885 if (chip->ksr)
886 ksr = (chip->block << 1) | ns;
887 else
888 ksr = chip->block >> 1;
889
890 int rate_hi = rate + (ksr >> 2);
891 if (rate_hi & 16)
892 rate_hi = 15;
893
894 int maxrate = rate_hi == 15;
895
896 int rate12 = rate_hi == 12;
897 int rate13 = rate_hi == 13;
898 int rate14 = rate_hi == 14;
899
900 int inclow = 0;
901
902 if (rate_hi < 12 && rate != 0 && chip->eg_subcnt[1])
903 {
904 int sum = (rate_hi + chip->eg_shift) & 15;
905 switch (sum)
906 {
907 case 12:
908 inclow = 1;
909 break;
910 case 13:
911 inclow = (ksr & 2) != 0;
912 break;
913 case 14:
914 inclow = (ksr & 1) != 0;
915 break;
916 }
917 }
918
919 int stephi = eg_stephi[ksr & 3][chip->eg_timer_low];
920
921 int step1 = 0;
922 int step2 = 0;
923 int step3 = 0;
924
925 switch (rate_hi)
926 {
927 case 12:
928 step1 = stephi || chip->eg_subcnt[1];
929 break;
930 case 13:
931 if (stephi)
932 step2 = 1;
933 else
934 step1 = 1;
935 break;
936 case 14:
937 if (stephi)
938 step3 = 1;
939 else
940 step2 = 1;
941 break;
942 case 15:
943 step3 = 1;
944 break;
945 }
946
947 step1 |= inclow;
948
949 int level = 0;
950
951 for (i = 0; i < 9; i++)
952 {
953 level |= ((chip->eg_level[i][1] >> 17) & 1) << i;
954 }
955
956 int slreach = (level >> 4) == sl;
957 int zeroreach = level == 0;
958 int silent = (level & 0x1f8) == 0x1f8;
959
960 int nextstate = eg_state_attack;
961
962 if (chip->reset1)
963 nextstate = eg_state_release;
964 else if (dokon)
965 nextstate = eg_state_attack;
966 else
967 {
968 if (!chip->keyon_comb)
969 nextstate = eg_state_release;
970 else if (state == eg_state_attack)
971 nextstate = zeroreach ? eg_state_decay : eg_state_attack;
972 else if (state == eg_state_decay)
973 nextstate = slreach ? eg_state_sustain : eg_state_decay;
974 else if (state == eg_state_sustain)
975 nextstate = eg_state_sustain;
976 else if (state == eg_state_release)
977 nextstate = eg_state_release;
978 }
979
980 int linear = !dokon && !silent && ((state & 2) != 0 || (state == eg_state_decay && !slreach));
981 int exponent = state == eg_state_attack && chip->keyon_comb && !maxrate && !zeroreach;
982 int instantattack = dokon && maxrate;
983 int mute = chip->reset1 || (state != eg_state_attack && silent && !dokon);
984
985 int level2 = mute ? 0x1ff : (instantattack ? 0 : level);
986
987 int add = 0;
988 int addshift = 0;
989
990 if (exponent)
991 add |= (~level) >> 1;
992 if (linear)
993 add |= 4;
994
995 if (step1)
996 addshift |= add >> 2;
997 if (step2)
998 addshift |= add >> 1;
999 if (step3)
1000 addshift |= add >> 0;
1001
1002 int levelnext = level2 + addshift;
1003
1004 static const int eg_ksltable[16] = {
1005 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
1006 };
1007
1008 int ksl;
1009 ksl = eg_ksltable[chip->fnum >> 6] ^ 127;
1010
1011 ksl += ((chip->block ^ 7) + 1) << 3;
1012 if (ksl & 128)
1013 ksl = 0;
1014 else
1015 ksl = (ksl ^ 63) & 63;
1016
1017 static int eg_kslshift[4] = {
1018 31, 1, 2, 0
1019 };
1020
1021 ksl = (ksl << 2) >> eg_kslshift[chip->ksl];
1022
1023 int ksltl = ksl + (chip->tl << 2);
1024
1025 int tremolo;
1026
1027 if (!chip->am)
1028 tremolo = 0;
1029 else if (chip->reg_da)
1030 tremolo = chip->trem_out >> 2;
1031 else
1032 tremolo = chip->trem_out >> 4;
1033
1034 int ksltltrem = ksltl + tremolo;
1035 int levelof = 0;
1036
1037 if (ksltltrem & 0x200)
1038 levelof = 1;
1039
1040 int totallevel = level + (ksltltrem & 0x1ff);
1041 if (totallevel & 0x200)
1042 levelof = 1;
1043
1044 int totallevelclamp = (chip->reg_test & 1) != 0 ? 0 : (levelof ? 0x1ff : (totallevel & 0x1ff));
1045
1046
1047 chip->eg_dokon = dokon;
1048
1049 chip->eg_state[0][0] = (chip->eg_state[0][1] << 1) | ((nextstate & 1) != 0);
1050 chip->eg_state[1][0] = (chip->eg_state[1][1] << 1) | ((nextstate & 2) != 0);
1051
1052 for (i = 0; i < 9; i++)
1053 {
1054 chip->eg_level[i][0] = (chip->eg_level[i][1] << 1) | ((levelnext >> i) & 1);
1055 }
1056 chip->eg_out[0] = totallevelclamp;
1057
1058 if (chip->fsm_out[9])
1059 {
1060 for (i = 0; i < 9; i++)
1061 {
1062 if (chip->eg_out[1] & (1 << i))
1063 chip->dbg_serial[0] |= 1 << (17 - i);
1064 }
1065 }
1066
1067 chip->eg_mute[0] = (chip->eg_mute[1] << 1) | mute;
1068 }
1069 if (chip->clk2)
1070 {
1071 chip->eg_state[0][1] = chip->eg_state[0][0];
1072 chip->eg_state[1][1] = chip->eg_state[1][0];
1073
1074 for (i = 0; i < 9; i++)
1075 {
1076 chip->eg_level[i][1] = chip->eg_level[i][0];
1077 }
1078 chip->eg_out[1] = chip->eg_out[0];
1079 chip->eg_mute[1] = chip->eg_mute[0];
1080 }
1081
1082 if (chip->clk1)
1083 {
1084 static const int pg_multi[16] = {
1085 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
1086 };
1087 int fnum = chip->fnum;
1088 int freq;
1089 int pg_add;
1090 int vib_sel1 = (chip->vib_cnt[1] & 3) == 2;
1091 int vib_sel2 = (chip->vib_cnt[1] & 1) == 1;
1092 int vib_sh0 = chip->reg_dv && chip->vib && vib_sel1;
1093 int vib_sh1 = (chip->reg_dv && chip->vib && vib_sel2)
1094 || (!chip->reg_dv && chip->vib && vib_sel1);
1095 int vib_sh2 = !chip->reg_dv && chip->vib && vib_sel2;
1096 int vib_sign = (chip->vib_cnt[1] & 4) != 0 && chip->vib;
1097 int vib_add = 0;
1098 int pg_out = 0;
1099 int phase;
1100 int noise_bit;
1101 if (vib_sh0)
1102 vib_add |= (chip->fnum >> 7) & 7;
1103 if (vib_sh1)
1104 vib_add |= (chip->fnum >> 8) & 3;
1105 if (vib_sh2)
1106 vib_add |= (chip->fnum >> 9) & 1;
1107 if (vib_sign)
1108 {
1109 vib_add ^= 1023;
1110 }
1111 fnum += vib_add;
1112 fnum += vib_sign;
1113 if (vib_sign)
1114 fnum &= 1023;
1115
1116 freq = (fnum << chip->block) >> 1;
1117
1118 pg_add = (freq * pg_multi[chip->multi]) >> 1;
1119
1120 for (i = 0; i < 19; i++)
1121 {
1122 pg_out |= ((chip->pg_phase[i][1] >> 17) & 1) << i;
1123 }
1124
1125 phase = ((chip->eg_dokon || (chip->reg_test & 4) != 0) ? 0 : pg_out) + pg_add;
1126
1127 for (i = 0; i < 19; i++)
1128 {
1129 chip->pg_phase[i][0] = chip->pg_phase[i][1] << 1;
1130 chip->pg_phase[i][0] |= (phase >> i) & 1;
1131 }
1132
1133 chip->dbg_serial[0] = chip->dbg_serial[1] >> 1;
1134
1135 if (chip->fsm_out[9])
1136 {
1137 chip->dbg_serial[0] |= pg_out & 511;
1138 }
1139
1140 noise_bit = ((chip->noise_lfsr[1] >> 22) ^ (chip->noise_lfsr[1] >> 8)) & 1;
1141
1142 if ((chip->noise_lfsr[1] & 0x7fffff) == 0)
1143 noise_bit |= 1;
1144
1145 noise_bit |= (chip->reg_test & 128) != 0;
1146
1147 chip->noise_lfsr[0] = (chip->noise_lfsr[1] << 1) | noise_bit;
1148 }
1149 if (chip->clk2)
1150 {
1151 for (i = 0; i < 19; i++)
1152 {
1153 chip->pg_phase[i][1] = chip->pg_phase[i][0];
1154 }
1155
1156 chip->noise_lfsr[1] = chip->noise_lfsr[0];
1157
1158 chip->pg_out = 0;
1159 for (i = 0; i < 10; i++)
1160 {
1161 chip->pg_out |= ((chip->pg_phase[i+9][1] >> 17) & 1) << i;
1162 }
1163
1164 chip->dbg_serial[1] = chip->dbg_serial[0];
1165 }
1166
1167 {
1168 int hh = chip->fsm_out[4] && chip->rhythm;
1169 int sd = chip->fsm_out[7] && chip->rhythm;
1170 int tc = chip->fsm_out[8] && chip->rhythm;
1171 int rhy = (chip->fsm_out[4] || chip->fsm_out[7] || chip->fsm_out[8]) && chip->rhythm;
1172 if (chip->clk1)
1173 chip->hh_load = chip->fsm_out[4];
1174 if (!chip->hh_load && chip->fsm_out[5])
1175 {
1176 chip->hh_bit2 = (chip->pg_out >> 2) & 1;
1177 chip->hh_bit3 = (chip->pg_out >> 3) & 1;
1178 chip->hh_bit7 = (chip->pg_out >> 7) & 1;
1179 chip->hh_bit8 = (chip->pg_out >> 8) & 1;
1180 }
1181 if (chip->clk1)
1182 chip->tc_load = tc;
1183 if (!chip->tc_load && tc)
1184 {
1185 chip->tc_bit3 = (chip->pg_out >> 3) & 1;
1186 chip->tc_bit5 = (chip->pg_out >> 5) & 1;
1187 }
1188
1189 if (chip->clk1) // opt
1190 {
1191 int rm_bit;
1192 int noise = (chip->noise_lfsr[1] >> 22) & 1;
1193
1194 rm_bit = (chip->hh_bit2 ^ chip->hh_bit7)
1195 | (chip->tc_bit5 ^ chip->hh_bit3)
1196 | (chip->tc_bit5 ^ chip->tc_bit3);
1197
1198 chip->pg_out_rhy = 0;
1199 if (!rhy)
1200 chip->pg_out_rhy |= chip->pg_out;
1201 if (hh)
1202 {
1203 chip->pg_out_rhy |= rm_bit << 9;
1204 if (noise ^ rm_bit)
1205 chip->pg_out_rhy |= 0xd0;
1206 else
1207 chip->pg_out_rhy |= 0x34;
1208 }
1209 if (sd)
1210 chip->pg_out_rhy |= (chip->hh_bit8 << 9) | ((noise ^ chip->hh_bit8) << 8);
1211 if (tc)
1212 chip->pg_out_rhy |= (rm_bit << 9) | 0x100;
1213 }
1214 }
1215
1216 {
1217
1218 if (chip->clk1)
1219 {
1220 static const int logsin[128] = {
1221 0x6c3, 0x58b, 0x4e4, 0x471, 0x41a, 0x3d3, 0x398, 0x365, 0x339, 0x311, 0x2ed, 0x2cd, 0x2af, 0x293, 0x279, 0x261,
1222 0x24b, 0x236, 0x222, 0x20f, 0x1fd, 0x1ec, 0x1dc, 0x1cd, 0x1be, 0x1b0, 0x1a2, 0x195, 0x188, 0x17c, 0x171, 0x166,
1223 0x15b, 0x150, 0x146, 0x13c, 0x133, 0x129, 0x121, 0x118, 0x10f, 0x107, 0x0ff, 0x0f8, 0x0f0, 0x0e9, 0x0e2, 0x0db,
1224 0x0d4, 0x0cd, 0x0c7, 0x0c1, 0x0bb, 0x0b5, 0x0af, 0x0a9, 0x0a4, 0x09f, 0x099, 0x094, 0x08f, 0x08a, 0x086, 0x081,
1225 0x07d, 0x078, 0x074, 0x070, 0x06c, 0x068, 0x064, 0x060, 0x05c, 0x059, 0x055, 0x052, 0x04e, 0x04b, 0x048, 0x045,
1226 0x042, 0x03f, 0x03c, 0x039, 0x037, 0x034, 0x031, 0x02f, 0x02d, 0x02a, 0x028, 0x026, 0x024, 0x022, 0x020, 0x01e,
1227 0x01c, 0x01a, 0x018, 0x017, 0x015, 0x014, 0x012, 0x011, 0x00f, 0x00e, 0x00d, 0x00c, 0x00a, 0x009, 0x008, 0x007,
1228 0x007, 0x006, 0x005, 0x004, 0x004, 0x003, 0x002, 0x002, 0x001, 0x001, 0x001, 0x001, 0x000, 0x000, 0x000, 0x000
1229 };
1230 static const int logsin_d[128] = {
1231 0x196, 0x07c, 0x04a, 0x035, 0x029, 0x022, 0x01d, 0x019, 0x015, 0x013, 0x012, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c,
1232 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x009, 0x008, 0x007, 0x007, 0x007, 0x007, 0x006, 0x007, 0x006, 0x006, 0x005,
1233 0x005, 0x005, 0x005, 0x005, 0x004, 0x005, 0x004, 0x004, 0x005, 0x004, 0x004, 0x003, 0x004, 0x003, 0x003, 0x003,
1234 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003, 0x003, 0x003, 0x003, 0x002, 0x002,
1235 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x001, 0x002, 0x002, 0x002, 0x001,
1236 0x001, 0x001, 0x002, 0x002, 0x001, 0x001, 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
1237 0x001, 0x001, 0x001, 0x000, 0x001, 0x000, 0x001, 0x000, 0x001, 0x001, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001,
1238 0x000, 0x000, 0x000, 0x001, 0x000, 0x000, 0x001, 0x000, 0x001, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
1239 };
1240 static const int pow[128] = {
1241 0x3f5, 0x3ea, 0x3df, 0x3d4, 0x3c9, 0x3bf, 0x3b4, 0x3a9, 0x39f, 0x394, 0x38a, 0x37f, 0x375, 0x36a, 0x360, 0x356,
1242 0x34c, 0x342, 0x338, 0x32e, 0x324, 0x31a, 0x310, 0x306, 0x2fd, 0x2f3, 0x2e9, 0x2e0, 0x2d6, 0x2cd, 0x2c4, 0x2ba,
1243 0x2b1, 0x2a8, 0x29e, 0x295, 0x28c, 0x283, 0x27a, 0x271, 0x268, 0x25f, 0x257, 0x24e, 0x245, 0x23c, 0x234, 0x22b,
1244 0x223, 0x21a, 0x212, 0x209, 0x201, 0x1f9, 0x1f0, 0x1e8, 0x1e0, 0x1d8, 0x1d0, 0x1c8, 0x1c0, 0x1b8, 0x1b0, 0x1a8,
1245 0x1a0, 0x199, 0x191, 0x189, 0x181, 0x17a, 0x172, 0x16b, 0x163, 0x15c, 0x154, 0x14d, 0x146, 0x13e, 0x137, 0x130,
1246 0x129, 0x122, 0x11b, 0x114, 0x10c, 0x106, 0x0ff, 0x0f8, 0x0f1, 0x0ea, 0x0e3, 0x0dc, 0x0d6, 0x0cf, 0x0c8, 0x0c2,
1247 0x0bb, 0x0b5, 0x0ae, 0x0a8, 0x0a1, 0x09b, 0x094, 0x08e, 0x088, 0x082, 0x07b, 0x075, 0x06f, 0x069, 0x063, 0x05d,
1248 0x057, 0x051, 0x04b, 0x045, 0x03f, 0x039, 0x033, 0x02d, 0x028, 0x022, 0x01c, 0x016, 0x011, 0x00b, 0x006, 0x000,
1249 };
1250 static const int pow_d[128] = {
1251 0x005, 0x005, 0x005, 0x006, 0x006, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x006, 0x005, 0x005,
1252 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x004, 0x005,
1253 0x004, 0x004, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x004, 0x004, 0x004, 0x005, 0x004, 0x005,
1254 0x004, 0x004, 0x004, 0x005, 0x004, 0x004, 0x005, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
1255 0x004, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x003, 0x004, 0x004, 0x004,
1256 0x003, 0x003, 0x003, 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x003, 0x003, 0x004, 0x003,
1257 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003,
1258 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003,
1259 };
1260 int phase = chip->pg_out_rhy + chip->op_mod[1];
1261 int sign = (phase & 512) != 0;
1262 int quarter = (phase & 256) != 0;
1263 phase &= 255;
1264 if (quarter)
1265 phase ^= 255;
1266
1267 int ls = logsin[phase >> 1];
1268 if ((phase & 1) == 0)
1269 ls += logsin_d[phase >> 1];
1270
1271 int att = chip->op_logsin[1] + (chip->eg_out[1] << 3);
1272 if (att & 4096)
1273 att = 4095;
1274
1275 int pw = pow[(att >> 1) & 127];
1276 if ((att & 1) == 0)
1277 pw += pow_d[(att >> 1) & 127];
1278
1279 int value = 0;
1280
1281 if (chip->op_mute[1] & 2)
1282 {
1283 value = ((chip->op_pow[1] | 0x400) << 1) >> chip->op_shift[1];
1284 }
1285
1286 if (chip->op_sign[1] & 2)
1287 value ^= 8191;
1288
1289 int sign_wf = sign && chip->wf == 0;
1290 int mute_wf = !((chip->wf == 1 && sign) || (chip->wf == 3 && quarter));
1291
1292 int fb1 = 0;
1293 int fb2 = 0;
1294 for (i = 0; i < 14; i++)
1295 {
1296 int j = i;
1297 if (i == 13)
1298 j = 12;
1299 fb1 |= ((chip->op_fb[0][j][1] >> 5) & 1) << i;
1300 fb2 |= ((chip->op_fb[1][j][1] >> 5) & 1) << i;
1301 }
1302 int fb_sum = fb1 + fb2;
1303 fb_sum &= 16383;
1304 if (fb_sum & 8192)
1305 fb_sum |= ~8191;
1306
1307 int mod = 0;
1308
1309 if (chip->fsm_out[2] && !(chip->connect_l[1] & 2))
1310 mod |= value & 1023;
1311 if (chip->fsm_out[1])
1312 {
1313 if (chip->fb_l[1][1])
1314 {
1315 mod |= (fb_sum >> (9 - chip->fb_l[1][1])) & 1023;
1316 }
1317 }
1318
1319 chip->op_logsin[0] = ls;
1320 chip->op_shift[0] = (att >> 8) & 15;
1321 chip->op_pow[0] = pw;
1322 chip->op_mute[0] = (chip->op_mute[1] << 1) | mute_wf;
1323 chip->op_sign[0] = (chip->op_sign[1] << 1) | sign_wf;
1324
1325 for (i = 0; i < 13; i++)
1326 {
1327 int bit;
1328 chip->op_fb[0][i][0] = chip->op_fb[0][i][1] << 1;
1329 if (chip->fsm_out[2])
1330 bit = (value >> i) & 1;
1331 else
1332 bit = (chip->op_fb[0][i][1] >> 8) & 1;
1333 chip->op_fb[0][i][0] |= bit;
1334 chip->op_fb[1][i][0] = chip->op_fb[1][i][1] << 1;
1335 if (chip->fsm_out[2])
1336 bit = (chip->op_fb[0][i][1] >> 8) & 1;
1337 else
1338 bit = (chip->op_fb[1][i][1] >> 8) & 1;
1339 chip->op_fb[1][i][0] |= bit;
1340 }
1341 chip->op_mod[0] = mod & 1023;
1342
1343 chip->op_value = value;
1344 }
1345 if (chip->clk2)
1346 {
1347 chip->op_logsin[1] = chip->op_logsin[0];
1348 chip->op_shift[1] = chip->op_shift[0];
1349 chip->op_pow[1] = chip->op_pow[0];
1350 chip->op_mute[1] = chip->op_mute[0];
1351 chip->op_sign[1] = chip->op_sign[0];
1352
1353 for (i = 0; i < 13; i++)
1354 {
1355 chip->op_fb[0][i][1] = chip->op_fb[0][i][0];
1356 chip->op_fb[1][i][1] = chip->op_fb[1][i][0];
1357 }
1358 chip->op_mod[1] = chip->op_mod[0];
1359 }
1360 }
1361
1362 {
1363 int accm_out = chip->fsm_out[8] ? (chip->accm_value[1] & 0x7fff) : 0;
1364 if (chip->fsm_out[8] && !(chip->accm_value[1] & 0x20000))
1365 accm_out |= 0x8000;
1366
1367 int top = (chip->accm_value[1] >> 15) & 7;
1368
1369 int clamplow = top == 4 || top == 5 || top == 6;
1370 int clamphigh = top == 3 || top == 2 || top == 1;
1371
1372 if (chip->clk1)
1373 chip->accm_load1_l = chip->fsm_out[8];
1374 chip->accm_load1 = !chip->accm_load1_l && chip->fsm_out[8];
1375
1376 if (chip->accm_load1)
1377 {
1378 chip->accm_clamplow = clamplow;
1379 chip->accm_clamphigh = clamphigh;
1380 chip->accm_top = (accm_out >> 9) & 127;
1381 }
1382
1383 if (chip->clk1)
1384 {
1385 int add = 0;
1386 int op_out = chip->op_value;
1387 if (op_out & 0x1000)
1388 op_out |= ~0xfff;
1389 if (!(chip->eg_mute[1] & 2) && chip->fsm_out[0])
1390 add = chip->fsm_out[11] ? (op_out * 2) : op_out;
1391
1392 int value = chip->fsm_out[8] ? 0 : chip->accm_value[1];
1393 value += add;
1394
1395 chip->op_value_debug = add;
1396
1397 int sign = ((chip->accm_top & 64) != 0 && !chip->accm_clamplow) || chip->accm_clamphigh;
1398
1399 int top_unsigned = chip->accm_top & 63;
1400 if (!sign)
1401 top_unsigned ^= 63;
1402
1403 int shift = 0;
1404
1405 if (top_unsigned & 32)
1406 shift |= 7;
1407 if ((top_unsigned & 48) == 16)
1408 shift |= 6;
1409 if ((top_unsigned & 56) == 8)
1410 shift |= 5;
1411 if ((top_unsigned & 60) == 4)
1412 shift |= 4;
1413 if ((top_unsigned & 62) == 2)
1414 shift |= 3;
1415 if (top_unsigned == 1)
1416 shift |= 2;
1417 if (top_unsigned == 0)
1418 shift |= 1;
1419 if (chip->accm_clamplow)
1420 shift |= 7;
1421 if (chip->accm_clamphigh)
1422 shift |= 7;
1423
1424 int accm_bit = 0;
1425
1426 if (chip->fsm_out[6])
1427 accm_bit |= sign;
1428 if (chip->accm_sel[1] & 1)
1429 accm_bit |= (shift & 1) != 0;
1430 if (chip->accm_sel[1] & 2)
1431 accm_bit |= (shift & 2) != 0;
1432 if (chip->accm_sel[1] & 4)
1433 accm_bit |= (shift & 4) != 0;
1434
1435 if ((chip->accm_sel[1] & 7) == 0 && !chip->fsm_out[6])
1436 {
1437 if (top_unsigned & 32)
1438 accm_bit |= (chip->accm_shifter[1] >> 6) & 1;
1439 if ((top_unsigned & 48) == 16)
1440 accm_bit |= (chip->accm_shifter[1] >> 5) & 1;
1441 if ((top_unsigned & 56) == 8)
1442 accm_bit |= (chip->accm_shifter[1] >> 4) & 1;
1443 if ((top_unsigned & 60) == 4)
1444 accm_bit |= (chip->accm_shifter[1] >> 3) & 1;
1445 if ((top_unsigned & 62) == 2)
1446 accm_bit |= (chip->accm_shifter[1] >> 2) & 1;
1447 if (top_unsigned == 1)
1448 accm_bit |= (chip->accm_shifter[1] >> 1) & 1;
1449 if (top_unsigned == 0)
1450 accm_bit |= chip->accm_shifter[1] & 1;
1451 if (chip->accm_clamphigh)
1452 accm_bit |= 1;
1453 if (chip->accm_clamplow)
1454 accm_bit = 0;
1455 }
1456
1457 chip->accm_value[0] = value & 0x3ffff;
1458 chip->accm_shifter[0] = (chip->accm_shifter[1] >> 1) & 0x7fff;
1459 if (chip->fsm_out[8])
1460 chip->accm_shifter[0] |= accm_out;
1461 chip->accm_sel[0] = (chip->accm_sel[1] << 1) | chip->fsm_out[6];
1462 chip->accm_mo[0] = accm_bit;
1463 }
1464 if (chip->clk2)
1465 {
1466 chip->accm_value[1] = chip->accm_value[0];
1467 chip->accm_shifter[1] = chip->accm_shifter[0];
1468 chip->accm_sel[1] = chip->accm_sel[0];
1469 chip->accm_mo[1] = chip->accm_mo[0];
1470 }
1471 }
1472
1473 end:
1474 chip->o_sh = chip->fsm_out[3];
1475 chip->o_mo = chip->accm_mo[1];
1476 chip->o_irq_pull = irq;
1477 chip->o_sy = chip->clk1;
1478
1479 if (chip->io_read0)
1480 {
1481 chip->io_data &= ~6;
1482 if (chip->reg_test & 64)
1483 chip->io_data |= chip->dbg_serial[1] & 1;
1484 if (irq)
1485 chip->io_data |= 128;
1486 if (chip->t1_status)
1487 chip->io_data |= 64;
1488 if (chip->t2_status)
1489 chip->io_data |= 32;
1490 if (chip->unk_status1)
1491 chip->io_data |= 16;
1492 if (chip->unk_status2)
1493 chip->io_data |= 8;
1494 }
1495
1496 if (chip->io_dir)
1497 {
1498 chip->data_o = chip->io_data;
1499 chip->data_z = 0;
1500 }
1501 else
1502 chip->data_z = 1;
1503 }