Debian Codegraph @

android/app/jni/extern/YMF262-LLE/fmopl3.c

raw
1 /*
2 * Copyright (C) 2023 nukeykt
3 *
4 * This file is part of YMF262-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 * YMF262 emulator
17 * Thanks:
18 * John McMaster (siliconpr0n.org):
19 * YMF262 decap and die shot
20 *
21 */
22
23 #include "fmopl3.h"
24
25
26 enum {
27 eg_state_attack = 0,
28 eg_state_decay,
29 eg_state_sustain,
30 eg_state_release
31 };
32
33 void FMOPL3_Clock(fmopl3_t *chip)
34 {
35 int i;
36
37 chip->mclk1 = !chip->input.mclk;
38 chip->mclk2 = chip->input.mclk;
39
40 chip->io_rd = !chip->input.rd;
41 chip->io_wr = !chip->input.wr;
42 chip->io_cs = !chip->input.cs;
43 chip->io_a0 = chip->input.address & 1;
44 chip->io_a1 = (chip->input.address & 2) != 0;
45
46 if (chip->mclk1)
47 {
48 chip->ic_latch[0] = (chip->ic_latch[1] << 1) | (!chip->input.ic);
49 }
50 if (chip->mclk2)
51 {
52 chip->ic_latch[1] = chip->ic_latch[0];
53
54 chip->reset0 = (chip->ic_latch[1] & 2) != 0;
55 }
56
57 chip->io_read = !chip->reset0 && chip->io_cs && chip->io_rd && !chip->io_a0 && !chip->io_a1;
58 chip->io_write = !chip->reset0 && chip->io_cs && chip->io_wr;
59 chip->io_write0 = !chip->reset0 && chip->io_cs && chip->io_wr && !chip->io_a0;
60 chip->io_write1 = !chip->reset0 && chip->io_cs && chip->io_wr && chip->io_a0;
61
62 if (chip->reset0)
63 chip->data_latch = 0;
64 else if (chip->io_write)
65 chip->data_latch = chip->input.data_i & 255;
66 if (chip->reset0)
67 chip->bank_latch = 0;
68 else if (chip->io_write0)
69 chip->bank_latch = chip->io_a1;
70
71 if (chip->mclk2)
72 {
73 chip->prescaler1_reset[1] = chip->prescaler1_reset[0];
74 chip->prescaler1_cnt[1] = chip->prescaler1_cnt[0];
75 }
76
77 int prescaler1_clk = chip->input.mclk; /* Temp: disable prescaler for performance reasons */
78
79 chip->aclk1 = !prescaler1_clk;
80 chip->aclk2 = prescaler1_clk;
81
82 if (chip->aclk2)
83 {
84 chip->prescaler2_reset_l[1] = chip->prescaler2_reset_l[0];
85 chip->prescaler2_cnt[1] = chip->prescaler2_cnt[0];
86 chip->prescaler2_l1[1] = chip->prescaler2_l1[0];
87 chip->prescaler2_l3[1] = chip->prescaler2_l3[0];
88 chip->prescaler2_l5[1] = chip->prescaler2_l5[0];
89 chip->prescaler2_l6[1] = chip->prescaler2_l6[0];
90 }
91
92
93 chip->clk1 = chip->prescaler2_l1[1] && !chip->prescaler2_l2;
94 chip->clk2 = chip->prescaler2_l3[1] && !chip->prescaler2_l4;
95
96 chip->rclk1 = chip->prescaler2_l6[1];
97 chip->rclk2 = chip->prescaler2_l5[1];
98
99 if (chip->aclk1)
100 {
101
102 int ga = (chip->data_latch & 0xe0) != 0;
103
104 int write0 = ga && chip->write0 && (chip->reg_test1 & 16) != 0;
105 int write = chip->write1 || write0;
106
107 chip->ra_w1_l1 = write;
108 }
109
110 if (chip->clk2)
111 {
112 chip->write0_l[1] = chip->write0_l[0];
113 chip->write0_l[3] = chip->write0_l[2];
114
115 chip->write1_l[1] = chip->write1_l[0];
116 chip->write1_l[3] = chip->write1_l[2];
117
118 chip->write0 = chip->write0_l[3] && !chip->write0_l[1];
119 chip->write1 = chip->write1_l[3] && !chip->write1_l[1];
120 }
121
122 //////////////////////
123
124 //if (chip->o_clk1 == chip->clk1 && chip->o_clk2 == chip->clk2 && chip->o_rclk1 == chip->rclk1 && chip->o_rclk2 == chip->rclk2 && chip->o_reset0 == chip->reset0
125 // && chip->o_ra_w1_l1 == chip->ra_w1_l1 && chip->o_bank_latch == chip->bank_latch && chip->o_data_latch == chip->data_latch)
126 // goto end; // opt
127
128 chip->o_clk1 = chip->clk1;
129 chip->o_clk2 = chip->clk2;
130 chip->o_rclk1 = chip->rclk1;
131 chip->o_rclk2 = chip->rclk2;
132 chip->o_reset0 = chip->reset0;
133 chip->o_data_latch = chip->data_latch;
134 chip->o_bank_latch = chip->bank_latch;
135 chip->o_ra_w1_l1 = chip->ra_w1_l1;
136
137 if (chip->reset0)
138 {
139 chip->reg_sel1 = 0;
140 chip->reg_sel2 = 0;
141 chip->reg_sel3 = 0;
142 chip->reg_sel4 = 0;
143 chip->reg_sel5 = 0;
144 chip->reg_sel8 = 0;
145 chip->reg_selbd = 0;
146 }
147 else if (chip->write0)
148 {
149 chip->reg_sel1 = chip->data_latch == 1;
150 chip->reg_sel2 = chip->data_latch == 2;
151 chip->reg_sel3 = chip->data_latch == 3;
152 chip->reg_sel4 = chip->data_latch == 4;
153 chip->reg_sel5 = chip->data_latch == 5;
154 chip->reg_sel8 = chip->data_latch == 8;
155 chip->reg_selbd = chip->data_latch == 0xbd;
156 }
157
158 if (chip->reset0)
159 chip->reg_new = 0;
160 else if (chip->write1 && chip->bank_latch && chip->reg_sel5)
161 chip->reg_new = chip->data_latch & 1;
162
163 int bank_masked = chip->reg_new && chip->bank_latch;
164
165 if (chip->reset0)
166 {
167 chip->reg_test0 = 0;
168 chip->reg_test1 = 0;
169 chip->reg_timer1 = 0;
170 chip->reg_timer2 = 0;
171 chip->reg_notesel = 0;
172 chip->rhythm = 0;
173 chip->reg_rh_kon = 0;
174 chip->reg_da = 0;
175 chip->reg_dv = 0;
176 }
177 else if (chip->write1)
178 {
179 if (chip->reg_sel1 && !bank_masked)
180 chip->reg_test0 = chip->data_latch & 255;
181 if (chip->reg_sel2 && !bank_masked)
182 chip->reg_timer1 = chip->data_latch & 255;
183 if (chip->reg_sel3 && !bank_masked)
184 chip->reg_timer2 = chip->data_latch & 255;
185 if (chip->reg_sel8 && !bank_masked)
186 {
187 chip->reg_notesel = (chip->data_latch & 64) != 0;
188 }
189 if (chip->reg_selbd && !bank_masked)
190 {
191 chip->reg_rh_kon = chip->data_latch & 31;
192 chip->rhythm = (chip->data_latch & 32) != 0;
193 chip->reg_dv = (chip->data_latch & 64) != 0;
194 chip->reg_da = (chip->data_latch & 128) != 0;
195 }
196 if (chip->reg_sel1 && bank_masked)
197 chip->reg_test1 = chip->data_latch & 255;
198 if (chip->reg_sel4 && bank_masked)
199 chip->reg_4op = chip->data_latch & 63;
200 }
201 int reg_sel4_wr = chip->write1 && chip->reg_sel4 && !bank_masked && (chip->data_latch & 128) == 0;
202 int reg_sel4_rst = (chip->write1 && chip->reg_sel4 && !bank_masked && (chip->data_latch & 128) != 0) || chip->reset0;
203
204 if (chip->reset0)
205 {
206 chip->reg_t1_mask = 0;
207 chip->reg_t2_mask = 0;
208 chip->reg_t1_start = 0;
209 chip->reg_t2_start = 0;
210 }
211 else if (reg_sel4_wr)
212 {
213 chip->reg_t1_mask = (chip->data_latch & 64) != 0;
214 chip->reg_t2_mask = (chip->data_latch & 32) != 0;
215 chip->reg_t1_start = (chip->data_latch & 1) != 0;
216 chip->reg_t2_start = (chip->data_latch & 2) != 0;
217 }
218
219 chip->reset1 = chip->reset0 || (chip->reg_test1 & 0xc0) == 0xc0;
220
221 {
222 //int bclk = !prescaler2_reset && chip->prescaler2_l7 && (chip->prescaler2_cnt[1] & 1) == 0;
223
224 int ga = (chip->data_latch & 0xe0) != 0;
225
226 if (chip->reset1)
227 chip->ra_address_latch = 0;
228 else if (chip->write0 && ga)
229 chip->ra_address_latch = (bank_masked << 8) | chip->data_latch;
230 if (chip->reset1)
231 chip->ra_address_good = 0;
232 else if (chip->write0)
233 chip->ra_address_good = ga;
234 if (chip->reset1)
235 chip->ra_data_latch = 0;
236 else if (chip->write1 && chip->ra_address_good)
237 chip->ra_data_latch = chip->data_latch;
238
239 int write0 = ga && chip->write0 && (chip->reg_test1 & 16) != 0;
240 int write = chip->write1 || write0;
241
242 if (chip->aclk1)
243 chip->ra_w1_l1 = write;
244 chip->ra_write = (write && !chip->ra_w1_l1) || (chip->reset1 && chip->clk2);
245 if (chip->clk1)
246 chip->ra_w1_l2 = write;
247 chip->ra_write_a = write && !chip->ra_w1_l2;
248
249 if (chip->clk1)
250 {
251 chip->ra_rst_l[0] = chip->reset1;
252 int rst = (chip->reset1 && !chip->ra_rst_l[1]) || chip->fsm_out[5];
253
254 int of1 = (chip->ra_cnt1[1] & 5) == 5;
255 int of2 = (chip->ra_cnt2[1] & 2) == 2 && of1;
256 int of4 = (chip->ra_cnt4[1] & 2) == 2;
257 if (rst || of1)
258 chip->ra_cnt1[0] = 0;
259 else
260 chip->ra_cnt1[0] = (chip->ra_cnt1[1] + 1) & 7;
261 if (rst || of2)
262 chip->ra_cnt2[0] = 0;
263 else
264 chip->ra_cnt2[0] = (chip->ra_cnt2[1] + of1) & 3;
265 if (rst)
266 chip->ra_cnt3[0] = 0;
267 else
268 chip->ra_cnt3[0] = (chip->ra_cnt3[1] + of2) & 1;
269
270 if (rst || of4 || of1)
271 chip->ra_cnt4[0] = 0;
272 else
273 chip->ra_cnt4[0] = (chip->ra_cnt4[1] + 1) & 3;
274
275 }
276 if (chip->clk2)
277 {
278 chip->ra_rst_l[1] = chip->ra_rst_l[0];
279 chip->ra_cnt1[1] = chip->ra_cnt1[0];
280 chip->ra_cnt2[1] = chip->ra_cnt2[0];
281 chip->ra_cnt3[1] = chip->ra_cnt3[0];
282 chip->ra_cnt4[1] = chip->ra_cnt4[0];
283 chip->ra_cnt = (chip->ra_cnt3[1] << 5) | (chip->ra_cnt2[1] << 3) | chip->ra_cnt1[1];
284 }
285
286 if (chip->ra_write || chip->clk1)
287 {
288 static const int ch_map[32] = {
289 0, 1, 2, -1,
290 3, 4, 5, -1,
291 6, 7, 8, -1,
292 -1, -1, -1, -1,
293 9, 10, 11, -1,
294 12, 13, 14, -1,
295 15, 16, 17, -1,
296 -1, -1, -1, -1
297 };
298
299 static const int op_map[64] = {
300 0, 1, 2, 3, 4, 5, -1, -1,
301 6, 7, 8, 9, 10, 11, -1, -1,
302 12, 13, 14, 15, 16, 17, -1, -1,
303 -1, -1, -1, -1, -1, -1, -1, -1,
304 18, 19, 20, 21, 22, 23, -1, -1,
305 24, 25, 26, 27, 28, 29, -1, -1,
306 30, 31, 32, 33, 34, 35, -1, -1,
307 -1, -1, -1, -1, -1, -1, -1, -1
308 };
309
310 int bank = (chip->ra_address_latch & 0x100) != 0;
311 int op_address = chip->ra_write_a ? ((chip->ra_address_latch & 0x1f) | (bank << 5)) : chip->ra_cnt;
312 int idx = op_map[op_address];
313 if (chip->ra_write && idx != -1)
314 {
315 if ((chip->ra_address_latch & 0xe0) == 0x20 || write0 || chip->reset1)
316 {
317 chip->ra_multi[idx] = chip->ra_data_latch & 15;
318 chip->ra_ksr[idx] = (chip->ra_data_latch >> 4) & 1;
319 chip->ra_egt[idx] = (chip->ra_data_latch >> 5) & 1;
320 chip->ra_vib[idx] = (chip->ra_data_latch >> 6) & 1;
321 chip->ra_am[idx] = (chip->ra_data_latch >> 7) & 1;
322 }
323 if ((chip->ra_address_latch & 0xe0) == 0x40 || write0 || chip->reset1)
324 {
325 chip->ra_tl[idx] = chip->ra_data_latch & 63;
326 chip->ra_ksl[idx] = (chip->ra_data_latch >> 6) & 3;
327 }
328 if ((chip->ra_address_latch & 0xe0) == 0x60 || write0 || chip->reset1)
329 {
330 chip->ra_dr[idx] = chip->ra_data_latch & 15;
331 chip->ra_ar[idx] = (chip->ra_data_latch >> 4) & 15;
332 }
333 if ((chip->ra_address_latch & 0xe0) == 0x80 || write0 || chip->reset1)
334 {
335 chip->ra_rr[idx] = chip->ra_data_latch & 15;
336 chip->ra_sl[idx] = (chip->ra_data_latch >> 4) & 15;
337 }
338 if ((chip->ra_address_latch & 0xe0) == 0xe0 || write0 || chip->reset1)
339 {
340 int data = chip->ra_data_latch & 3;
341 if (chip->reg_new)
342 data |= chip->ra_data_latch & 4;
343 chip->ra_wf[idx] = data;
344 }
345 }
346 int ch_address_write = chip->ra_address_latch & 15;
347 int add = 0;
348 if (ch_address_write == 3 || ch_address_write == 4 || ch_address_write == 5)
349 add |= 1;
350 if (ch_address_write == 6 || ch_address_write == 7 || ch_address_write == 8)
351 add |= 2;
352 int ch_address_mapped = (ch_address_write & 1) + (add & 1);
353 ch_address_mapped |= add & 2;
354 ch_address_mapped += ch_address_write & 14;
355 ch_address_mapped &= 15;
356 ch_address_mapped |= bank << 4;
357 int ch_address_mapped2 = ch_address_mapped & 3;
358 if ((ch_address_mapped & 12) == 8)
359 ch_address_mapped2 |= 4;
360 if ((ch_address_mapped & 12) == 0)
361 ch_address_mapped2 |= 8;
362 if ((ch_address_mapped & 28) == 0 || (ch_address_mapped & 28) == 20 || (ch_address_mapped & 28) == 24)
363 ch_address_mapped2 |= 16;
364
365
366 int ch_address_read = (chip->ra_cnt4[1] & 3) | (chip->ra_cnt2[1] << 2) | (chip->ra_cnt3[1] << 4);
367 int ch_address = chip->ra_write_a ? ch_address_mapped : ch_address_read;
368 int ch_address_read_4op = ch_address_read;
369 if ((chip->ra_cnt2[1] & 2) == 0)
370 {
371 switch (chip->ra_cnt3[1] * 4 + chip->ra_cnt4[1])
372 {
373 case 0: // 0, 3, 6, 9
374 if (chip->reg_4op & 1)
375 ch_address_read_4op &= ~4;
376 break;
377 case 1: // 1, 4, 7, 10
378 if (chip->reg_4op & 2)
379 ch_address_read_4op &= ~4;
380 break;
381 case 2: // 2, 5, 8, 11
382 if (chip->reg_4op & 4)
383 ch_address_read_4op &= ~4;
384 break;
385 case 4: // 0, 3, 6, 9
386 if (chip->reg_4op & 8)
387 ch_address_read_4op &= ~4;
388 break;
389 case 5: // 1, 4, 7, 10
390 if (chip->reg_4op & 16)
391 ch_address_read_4op &= ~4;
392 break;
393 case 6: // 2, 5, 8, 11
394 if (chip->reg_4op & 32)
395 ch_address_read_4op &= ~4;
396 break;
397 }
398 }
399 int ch_address_4op = chip->ra_write_a ? ch_address_mapped : ch_address_read_4op;
400 int ch_address_fb = chip->ra_write_a ? ch_address_mapped2 : ch_address_read;
401
402 int idx1 = ch_map[ch_address];
403 int idx2 = ch_map[ch_address_4op];
404 int idx3 = ch_map[ch_address_fb];
405 if (chip->ra_write && idx1 != -1)
406 {
407 if ((chip->ra_address_latch & 0xf0) == 0xc0 || write0 || chip->reset1)
408 {
409 chip->ra_connect[idx1] = chip->ra_data_latch & 1;
410 int pan_data = 0;
411 if (!chip->reg_new || chip->reset1)
412 pan_data |= 3;
413 if (chip->reg_new)
414 pan_data |= (chip->ra_data_latch >> 4) & 15;
415 chip->ra_pan[idx1] = pan_data;
416 }
417 }
418 if (chip->ra_write && idx2 != -1)
419 {
420 if ((chip->ra_address_latch & 0xf0) == 0xa0 || write0 || chip->reset1)
421 {
422 chip->ra_fnum[idx2] &= 0x300;
423 chip->ra_fnum[idx2] |= chip->ra_data_latch & 0xff;
424 }
425 if ((chip->ra_address_latch & 0xf0) == 0xb0 || write0 || chip->reset1)
426 {
427 chip->ra_fnum[idx2] &= 0xff;
428 chip->ra_fnum[idx2] |= (chip->ra_data_latch & 3) << 8;
429 chip->ra_block[idx2] = (chip->ra_data_latch >> 2) & 7;
430 chip->ra_keyon[idx2] = (chip->ra_data_latch >> 5) & 1;
431 }
432 }
433 if (chip->ra_write && idx3 != -1)
434 {
435 if ((chip->ra_address_latch & 0xf0) == 0xc0 || write0 || chip->reset1)
436 {
437 chip->ra_connect_pair[idx3] = chip->ra_data_latch & 1;
438 chip->ra_fb[idx3] = (chip->ra_data_latch >> 1) & 7;
439 }
440 }
441
442 if (chip->clk1)
443 {
444 if (idx != -1)
445 {
446 chip->multi[0] = chip->ra_multi[idx];
447 chip->ksr[0] = chip->ra_ksr[idx];
448 chip->egt[0] = chip->ra_egt[idx];
449 chip->am[0] = chip->ra_am[idx];
450 chip->vib[0] = chip->ra_vib[idx];
451 chip->tl[0] = chip->ra_tl[idx];
452 chip->ksl[0] = chip->ra_ksl[idx];
453 chip->ar[0] = chip->ra_ar[idx];
454 chip->dr[0] = chip->ra_dr[idx];
455 chip->sl[0] = chip->ra_sl[idx];
456 chip->rr[0] = chip->ra_rr[idx];
457 chip->wf[0] = chip->ra_wf[idx];
458 }
459 if (idx1 != -1)
460 {
461 chip->connect[0] = chip->ra_connect[idx1];
462 chip->pan[0] = chip->ra_pan[idx1];
463 }
464 if (idx2 != -1)
465 {
466 chip->fnum[0] = chip->ra_fnum[idx2];
467 chip->block[0] = chip->ra_block[idx2];
468 chip->keyon[0] = chip->ra_keyon[idx2];
469 }
470 if (idx3 != -1)
471 {
472 chip->connect_pair[0] = chip->ra_connect_pair[idx3];
473 chip->fb[0] = chip->ra_fb[idx3];
474 }
475 }
476 }
477 if (chip->clk2)
478 {
479 chip->multi[1] = chip->multi[0];
480 chip->ksr[1] = chip->ksr[0];
481 chip->egt[1] = chip->egt[0];
482 chip->am[1] = chip->am[0];
483 chip->vib[1] = chip->vib[0];
484 chip->tl[1] = chip->tl[0];
485 chip->ksl[1] = chip->ksl[0];
486 chip->ar[1] = chip->ar[0];
487 chip->dr[1] = chip->dr[0];
488 chip->sl[1] = chip->sl[0];
489 chip->rr[1] = chip->rr[0];
490 chip->wf[1] = chip->wf[0];
491
492 chip->connect[1] = chip->connect[0];
493 chip->pan[1] = chip->pan[0];
494
495 chip->fnum[1] = chip->fnum[0];
496 chip->block[1] = chip->block[0];
497 chip->keyon[1] = chip->keyon[0];
498
499 chip->connect_pair[1] = chip->connect_pair[0];
500 chip->fb[1] = chip->fb[0];
501 }
502 }
503
504 if (chip->clk1)
505 {
506 chip->connect_l[0] = (chip->connect_l[1] << 1) | chip->connect[1];
507 chip->connect_pair_l[0] = (chip->connect_pair_l[1] << 1) | chip->connect_pair[1];
508 chip->fb_l[0][0] = chip->fb[1];
509 chip->fb_l[1][0] = chip->fb_l[0][1];
510 chip->pan_l[0][0] = chip->pan[1];
511 chip->pan_l[1][0] = chip->pan_l[0][1];
512 }
513 if (chip->clk2)
514 {
515 chip->connect_l[1] = chip->connect_l[0];
516 chip->connect_pair_l[1] = chip->connect_pair_l[0];
517 chip->fb_l[0][1] = chip->fb_l[0][0];
518 chip->fb_l[1][1] = chip->fb_l[1][0];
519 chip->pan_l[0][1] = chip->pan_l[0][0];
520 chip->pan_l[1][1] = chip->pan_l[1][0];
521 }
522
523 {
524 int fsm_4op = 0;
525 switch (chip->fsm_cnt)
526 {
527 case 5: // 5
528 fsm_4op = (chip->reg_4op & 1) != 0;
529 break;
530 case 8: // 6
531 fsm_4op = (chip->reg_4op & 2) != 0;
532 break;
533 case 9: // 7
534 fsm_4op = (chip->reg_4op & 4) != 0;
535 break;
536 case 37: // 23
537 fsm_4op = (chip->reg_4op & 8) != 0;
538 break;
539 case 40: // 24
540 fsm_4op = (chip->reg_4op & 16) != 0;
541 break;
542 case 41: // 25
543 fsm_4op = (chip->reg_4op & 32) != 0;
544 break;
545 }
546 int con_4op = fsm_4op && (chip->fsm_l10[1] & 4) != 0; // 01 connect
547
548 if (chip->clk1)
549 {
550 int fsm_reset = (chip->fsm_reset_l[1] & 2) == 0 && chip->reset1;
551 chip->fsm_reset_l[0] = (chip->fsm_reset_l[1] << 1) | chip->reset1;
552
553 int fsm_of1 = (chip->fsm_cnt1[1] & 5) == 5;
554 int fsm_of2 = (chip->fsm_cnt2[1] & 2) == 2 && fsm_of1;
555
556 if (fsm_reset || fsm_of1)
557 chip->fsm_cnt1[0] = 0;
558 else
559 chip->fsm_cnt1[0] = (chip->fsm_cnt1[1] + 1) & 7;
560
561 if (fsm_reset || fsm_of2)
562 chip->fsm_cnt2[0] = 0;
563 else
564 chip->fsm_cnt2[0] = (chip->fsm_cnt2[1] + fsm_of1) & 3;
565
566 if (fsm_reset)
567 chip->fsm_cnt3[0] = 0;
568 else
569 chip->fsm_cnt3[0] = (chip->fsm_cnt3[1] + fsm_of2) & 1;
570
571 chip->fsm_l1[0] = chip->fsm_cnt == 53;
572 chip->fsm_l2[0] = chip->fsm_cnt == 16;
573 chip->fsm_l3[0] = chip->fsm_cnt == 20;
574 chip->fsm_l4[0] = chip->fsm_cnt == 52;
575 chip->fsm_l5[0] = (chip->fsm_l5[1] << 1) | ((chip->fsm_cnt & 56) == 0);
576 chip->fsm_l6[0] = (chip->fsm_l6[1] << 1) | ((chip->fsm_cnt & 56) == 8 || (chip->fsm_cnt & 62) == 16);
577 chip->fsm_l7[0] = (chip->fsm_l7[1] << 1) | ((chip->fsm_cnt & 56) == 40 || (chip->fsm_cnt & 62) == 48);
578 chip->fsm_l8[0] = (chip->fsm_l8[1] << 1) | ((chip->fsm_cnt & 48) == 16);
579 chip->fsm_l9[0] = (chip->fsm_l9[1] << 1) | con_4op;
580 chip->fsm_l10[0] = (chip->fsm_l10[1] << 1) | ((chip->connect_l[1] & 2) == 0 && (chip->connect_pair_l[1] & 2) != 0);
581 }
582 if (chip->clk2)
583 {
584 chip->fsm_reset_l[1] = chip->fsm_reset_l[0];
585 chip->fsm_cnt1[1] = chip->fsm_cnt1[0];
586 chip->fsm_cnt2[1] = chip->fsm_cnt2[0];
587 chip->fsm_cnt3[1] = chip->fsm_cnt3[0];
588
589 chip->fsm_cnt = (chip->fsm_cnt3[1] << 5) | (chip->fsm_cnt2[1] << 3) | chip->fsm_cnt1[1];
590
591 chip->fsm_l1[1] = chip->fsm_l1[0];
592 chip->fsm_l2[1] = chip->fsm_l2[0];
593 chip->fsm_l3[1] = chip->fsm_l3[0];
594 chip->fsm_l4[1] = chip->fsm_l4[0];
595 chip->fsm_l5[1] = chip->fsm_l5[0];
596 chip->fsm_l6[1] = chip->fsm_l6[0];
597 chip->fsm_l7[1] = chip->fsm_l7[0];
598 chip->fsm_l8[1] = chip->fsm_l8[0];
599 chip->fsm_l9[1] = chip->fsm_l9[0];
600 chip->fsm_l10[1] = chip->fsm_l10[0];
601 }
602 {
603 chip->fsm_out[0] = chip->fsm_l1[1]; // 0
604 chip->fsm_out[1] = chip->fsm_cnt == 16; // 12
605 chip->fsm_out[2] = chip->fsm_l2[1]; // 13
606 chip->fsm_out[3] = chip->fsm_cnt == 20; // 16
607 chip->fsm_out[4] = chip->fsm_l3[1]; // 17
608 chip->fsm_out[5] = chip->fsm_cnt == 52; // 34
609 chip->fsm_out[6] = chip->fsm_l4[1]; // 35
610 chip->fsm_out[7] = (chip->fsm_l5[1] & 4) != 0 || ((chip->fsm_cnt & 56) == 0); // 0-8
611 chip->fsm_out[8] = (chip->fsm_cnt & 32) == 0;
612 chip->fsm_out[9] = (chip->fsm_l6[1] & 2) != 0;
613 chip->fsm_out[10] = (chip->fsm_l7[1] & 2) != 0;
614 chip->fsm_out[11] = chip->rhythm && (chip->fsm_l8[1] & 2) != 0; // r 14, 15, 16, 17, 18, 19
615
616 int fsm_mc = !((chip->fsm_cnt & 5) == 4 || (chip->fsm_cnt & 2) != 0);
617 int fsm_mc_4op = fsm_mc && !fsm_4op;
618 int rhy_19_20 = chip->rhythm && (chip->fsm_cnt == 19 || chip->fsm_cnt == 20);
619
620 chip->fsm_out[12] = fsm_mc_4op && !(chip->rhythm && (chip->fsm_cnt == 16 || chip->fsm_cnt == 17)); // feedback
621 chip->fsm_out[14] = con_4op || (!fsm_4op && !(chip->fsm_l9[1] & 4) && (chip->connect_l[1] & 2) != 0); // connect
622 chip->fsm_out[13] = !(chip->rhythm && chip->fsm_cnt == 18) && (fsm_mc_4op || rhy_19_20 || chip->fsm_out[14]); // output
623 chip->fsm_out[15] = !fsm_mc && !rhy_19_20; // load fb
624 chip->fsm_out[16] = !fsm_mc_4op && !rhy_19_20; // modulate
625 }
626 }
627
628 if (chip->clk1)
629 chip->timer_st_load_l = chip->fsm_out[6];
630 chip->timer_st_load = chip->fsm_out[6] && !chip->timer_st_load_l;
631
632 if (chip->timer_st_load)
633 {
634 chip->t1_start = chip->reg_t1_start;
635 chip->t2_start = chip->reg_t2_start;
636 }
637
638 if (chip->clk1)
639 {
640 int lfo = chip->lfo_cnt[1];
641 int add = chip->fsm_out[6];
642 int reset = (chip->reg_test0 & 2) != 0 || chip->reset1;
643
644 chip->lfo_cnt[0] = reset ? 0 : (lfo + add) & 1023;
645 chip->vib_cnt[0] = reset ? 0 : (chip->vib_cnt[1] + chip->vib_step) & 7;
646 }
647 if (chip->clk2)
648 {
649 chip->lfo_cnt[1] = chip->lfo_cnt[0];
650 chip->vib_cnt[1] = chip->vib_cnt[0];
651 }
652
653 {
654 int lfo = chip->lfo_cnt[1];
655 int add = chip->fsm_out[6];
656
657 chip->t1_step = (((lfo & 3) + add) & 4) != 0;
658 chip->t2_step = (((lfo & 15) + add) & 16) != 0;
659 chip->am_step = (((lfo & 63) + add) & 64) != 0;
660 chip->vib_step = (((lfo & 1023) + add) & 1024) != 0;
661 chip->vib_step |= (chip->reg_test0 & 16) != 0 && add;
662 }
663
664 if (chip->clk1)
665 {
666 int value = (chip->t1_of[1] || (chip->t1_start_l[1] & 3) == 1) ? chip->reg_timer1 : chip->t1_cnt[1];
667 value += ((chip->t1_start_l[1] & 1) != 0 && chip->t1_step) || (chip->reg_test1 & 8) != 0;
668 chip->t1_of[0] = (value & 256) != 0;
669 chip->t1_cnt[0] = (chip->t1_start_l[1] & 1) == 0 ? 0 : (value & 255);
670
671 value = (chip->t2_of[1] || (chip->t2_start_l[1] & 3) == 1) ? chip->reg_timer2 : chip->t2_cnt[1];
672 value += ((chip->t2_start_l[1] & 1) != 0 && chip->t2_step) || (chip->reg_test1 & 8) != 0;
673 chip->t2_of[0] = (value & 256) != 0;
674 chip->t2_cnt[0] = (chip->t2_start_l[1] & 1) == 0 ? 0 : (value & 255);
675
676 chip->t1_start_l[0] = (chip->t1_start_l[1] << 1) | chip->t1_start;
677 chip->t2_start_l[0] = (chip->t2_start_l[1] << 1) | chip->t2_start;
678 }
679 if (chip->clk2)
680 {
681 chip->t1_cnt[1] = chip->t1_cnt[0];
682 chip->t1_of[1] = chip->t1_of[0];
683 chip->t2_cnt[1] = chip->t2_cnt[0];
684 chip->t2_of[1] = chip->t2_of[0];
685
686 chip->t1_start_l[1] = chip->t1_start_l[0];
687 chip->t2_start_l[1] = chip->t2_start_l[0];
688 }
689
690 if (reg_sel4_rst || chip->reg_t1_mask)
691 chip->t1_status = 0;
692 else if (chip->t1_of[1])
693 chip->t1_status = 1;
694
695 if (reg_sel4_rst || chip->reg_t2_mask)
696 chip->t2_status = 0;
697 else if (chip->t2_of[1])
698 chip->t2_status = 1;
699
700 chip->rh_sel0 = chip->rhythm && chip->fsm_out[1];
701
702 if (chip->clk1)
703 {
704 chip->rh_sel[0] = (chip->rh_sel[1] << 1) | chip->rh_sel0;
705 }
706 if (chip->clk2)
707 {
708 chip->rh_sel[1] = chip->rh_sel[0];
709 }
710
711 chip->keyon_comb = chip->keyon[1]
712 || (chip->rh_sel0 && (chip->reg_rh_kon & 16) != 0) // bd0
713 || ((chip->rh_sel[1] & 1) != 0 && (chip->reg_rh_kon & 1) != 0) // hh
714 || ((chip->rh_sel[1] & 2) != 0 && (chip->reg_rh_kon & 4) != 0) // tom
715 || ((chip->rh_sel[1] & 4) != 0 && (chip->reg_rh_kon & 16) != 0) // bd1
716 || ((chip->rh_sel[1] & 8) != 0 && (chip->reg_rh_kon & 8) != 0) // sd
717 || ((chip->rh_sel[1] & 16) != 0 && (chip->reg_rh_kon & 2) != 0); // tc
718
719
720 if (chip->clk1)
721 {
722 chip->trem_load_l = chip->fsm_out[0];
723 chip->trem_st_load_l = chip->fsm_out[6];
724 chip->eg_load_l = chip->eg_load_l1[1];
725 }
726 chip->trem_load = !chip->trem_load_l && chip->fsm_out[0];
727 chip->trem_st_load = !chip->trem_st_load_l && chip->fsm_out[6];
728 chip->eg_load = !chip->eg_load_l && chip->eg_load_l1[1];
729
730 {
731 if (chip->trem_st_load)
732 chip->trem_step = chip->am_step;
733 if (chip->trem_load)
734 chip->trem_out = chip->trem_value[1] & 127;
735
736 if (chip->clk1)
737 {
738 int bit = chip->trem_value[1] & 1;
739 int reset = chip->reset1 || (chip->reg_test0 & 2) != 0;
740
741 int step = ((chip->trem_step || (chip->reg_test0 & 16) != 0) && (chip->fsm_out[0] || chip->trem_dir[1]))
742 && chip->fsm_out[7];
743 int carry = chip->fsm_out[7] && chip->trem_carry[1];
744
745 bit += step + carry;
746
747 int of = (chip->trem_out == 0) || (chip->trem_out & 105) == 105;
748
749 chip->trem_carry[0] = (bit & 2) != 0;
750 chip->trem_value[0] = (chip->trem_value[1] >> 1) & 255;
751 if (!reset)
752 chip->trem_value[0] |= (bit & 1) << 8;
753 chip->trem_of[0] = of;
754
755 if (reset)
756 chip->trem_dir[0] = 0;
757 else
758 chip->trem_dir[0] = chip->trem_dir[1] ^ (of && !chip->trem_of[1]);
759 }
760 if (chip->clk2)
761 {
762 chip->trem_carry[1] = chip->trem_carry[0];
763 chip->trem_value[1] = chip->trem_value[0];
764 chip->trem_of[1] = chip->trem_of[0];
765 chip->trem_dir[1] = chip->trem_dir[0];
766 }
767 }
768
769 {
770
771 if (chip->reset1)
772 {
773 chip->eg_timer_low = 0;
774 chip->eg_shift = 0;
775 }
776 else if (chip->eg_load)
777 {
778 chip->eg_timer_low = chip->eg_timer_o[3] | (chip->eg_timer_o[1] << 1);
779 chip->eg_shift = 0;
780 if (chip->eg_timer_masked[1] & 0x1555)
781 chip->eg_shift |= 1;
782 if (chip->eg_timer_masked[1] & 0x666)
783 chip->eg_shift |= 2;
784 if (chip->eg_timer_masked[1] & 0x1878)
785 chip->eg_shift |= 4;
786 if (chip->eg_timer_masked[1] & 0x1f80)
787 chip->eg_shift |= 8;
788 }
789
790 if (chip->clk1)
791 {
792 int bit = chip->eg_timer_o[3];
793 int bit2;
794 int carry = chip->eg_carry[1] || (chip->eg_subcnt[1] && chip->eg_sync_l[1]);
795 bit += carry;
796
797 int rst = chip->reset1 || (chip->reg_test1 & 8) != 0;
798
799 if (rst)
800 bit2 = 0;
801 else
802 bit2 = bit & 1;
803
804 chip->eg_timer_i = bit2;
805 chip->eg_carry[0] = (bit & 2) != 0;
806 chip->eg_sync_l[0] = chip->fsm_out[6];
807 chip->eg_mask[0] = (rst || chip->fsm_out[6]) ? 0 :
808 (chip->eg_mask[1] || bit2);
809 chip->eg_timer_masked[0] = (chip->eg_timer_masked[1] >> 1) & 0x7ffffffffLL;
810 if (!chip->eg_mask[1])
811 chip->eg_timer_masked[0] |= ((int64_t)bit2) << 35;
812 if (!chip->eg_timer_dbg[1] && (chip->reg_test0 & 64) != 0)
813 chip->eg_timer_masked[0] |= 1LL << 35;
814
815 if (chip->reset1)
816 chip->eg_subcnt[0] = 0;
817 else
818 chip->eg_subcnt[0] = chip->eg_subcnt[1] ^ chip->fsm_out[6];
819
820 chip->eg_load_l1[0] = chip->eg_subcnt[1] && chip->fsm_out[6];
821
822 chip->eg_timer_dbg[0] = (chip->reg_test0 & 64) != 0;
823 }
824 if (chip->clk2)
825 {
826 chip->eg_carry[1] = chip->eg_carry[0];
827 chip->eg_sync_l[1] = chip->eg_sync_l[0];
828 chip->eg_mask[1] = chip->eg_mask[0];
829 chip->eg_timer_masked[1] = chip->eg_timer_masked[0];
830 chip->eg_subcnt[1] = chip->eg_subcnt[0];
831 chip->eg_load_l1[1] = chip->eg_load_l1[0];
832 chip->eg_timer_dbg[1] = chip->eg_timer_dbg[0];
833 }
834 }
835
836 if (chip->clk1)
837 {
838 static const int eg_stephi[4][4] = {
839 { 0, 0, 0, 0 },
840 { 1, 0, 0, 0 },
841 { 1, 0, 1, 0 },
842 { 1, 1, 1, 0 }
843 };
844
845 int rst = chip->reset1 || (chip->reg_test1 & 32) != 0;
846
847 int state = chip->eg_state_o[3];
848 int dokon = state == eg_state_release && chip->keyon_comb;
849 int rate_sel = dokon ? eg_state_attack : state;
850 int rate = 0;
851 int ksr;
852 if (rate_sel == 0)
853 rate |= chip->ar[1];
854 if (rate_sel == 1)
855 rate |= chip->dr[1];
856 if (rate_sel == 3 || (rate_sel == 2 && !chip->egt[1]))
857 rate |= chip->rr[1];
858
859 int sl = chip->sl[1];
860 if (chip->sl[1] == 15)
861 sl |= 16;
862
863 int ns = chip->reg_notesel ? (chip->fnum[1] & 256) != 0 : (chip->fnum[1] & 512) != 0;
864
865 if (chip->ksr[1])
866 ksr = (chip->block[1] << 1) | ns;
867 else
868 ksr = chip->block[1] >> 1;
869
870 int rate_hi = rate + (ksr >> 2);
871 if (rate_hi & 16)
872 rate_hi = 15;
873
874 int maxrate = rate_hi == 15;
875
876 int rate12 = rate_hi == 12;
877 int rate13 = rate_hi == 13;
878 int rate14 = rate_hi == 14;
879
880 int inclow = 0;
881
882 if (rate_hi < 12 && rate != 0 && chip->eg_subcnt[1])
883 {
884 int sum = (rate_hi + chip->eg_shift) & 15;
885 switch (sum)
886 {
887 case 12:
888 inclow = 1;
889 break;
890 case 13:
891 inclow = (ksr & 2) != 0;
892 break;
893 case 14:
894 inclow = (ksr & 1) != 0;
895 break;
896 }
897 }
898
899 int stephi = eg_stephi[ksr & 3][chip->eg_timer_low];
900
901 int step1 = 0;
902 int step2 = 0;
903 int step3 = 0;
904
905 switch (rate_hi)
906 {
907 case 12:
908 step1 = stephi || chip->eg_subcnt[1];
909 break;
910 case 13:
911 if (stephi)
912 step2 = 1;
913 else
914 step1 = 1;
915 break;
916 case 14:
917 if (stephi)
918 step3 = 1;
919 else
920 step2 = 1;
921 break;
922 case 15:
923 step3 = 1;
924 break;
925 }
926
927 step1 |= inclow;
928
929 int level = chip->eg_level_o[3];
930 int slreach = (level >> 4) == sl;
931 int zeroreach = level == 0;
932 int silent = (level & 0x1f8) == 0x1f8;
933
934 static const int eg_ksltable[16] = {
935 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
936 };
937
938 int nextstate = eg_state_attack;
939
940 if (rst)
941 nextstate = eg_state_release;
942 else if (dokon)
943 nextstate = eg_state_attack;
944 else
945 {
946 if (!chip->keyon_comb)
947 nextstate = eg_state_release;
948 else if (state == eg_state_attack)
949 nextstate = zeroreach ? eg_state_decay : eg_state_attack;
950 else if (state == eg_state_decay)
951 nextstate = slreach ? eg_state_sustain : eg_state_decay;
952 else if (state == eg_state_sustain)
953 nextstate = eg_state_sustain;
954 else if (state == eg_state_release)
955 nextstate = eg_state_release;
956 }
957
958 int linear = !dokon && !silent && ((state & 2) != 0 || (state == eg_state_decay && !slreach));
959 int exponent = state == eg_state_attack && chip->keyon_comb && !maxrate && !zeroreach;
960 int instantattack = (dokon && maxrate) || (chip->reg_test0 & 16) != 0;
961 int mute = rst || (state != eg_state_attack && silent && !dokon && !(chip->reg_test0 & 16));
962
963 int level2 = mute ? 0x1ff : (instantattack ? 0 : level);
964
965 int add = 0;
966 int addshift = 0;
967
968 if (exponent)
969 add |= (level >> 1) ^ 0xff;
970 if (linear)
971 add |= 4;
972
973 if (exponent && (step1 || step2 || step3))
974 addshift |= 256;
975
976 if (step1)
977 addshift |= (add >> 2) | (exponent << 6) | (exponent << 7) | linear;
978 if (step2)
979 addshift |= (add >> 1) | (exponent << 7) | (linear << 1);
980 if (step3)
981 addshift |= (add >> 0) | (linear << 2);
982
983 int levelnext = level2 + addshift;
984
985 int ksl;
986 ksl = eg_ksltable[chip->fnum[1] >> 6];
987 int ksl_hi = (ksl & 64) != 0;
988
989 ksl = (ksl & 63) + (chip->block[1] << 3);
990 if (ksl_hi || (ksl & 64) != 0)
991 ksl = ksl & 63;
992 else
993 ksl = 0;
994
995 static int eg_kslshift[4] = {
996 31, 1, 2, 0
997 };
998
999 ksl = (ksl << 2) >> eg_kslshift[chip->ksl[1]];
1000
1001 int ksltl = ksl + (chip->tl[1] << 2);
1002
1003 int tremolo;
1004
1005 if (!chip->am[1])
1006 tremolo = 0;
1007 else if (chip->reg_da)
1008 tremolo = chip->trem_out >> 2;
1009 else
1010 tremolo = chip->trem_out >> 4;
1011
1012 int ksltltrem = ksltl + tremolo;
1013 int levelof = 0;
1014
1015 if (ksltltrem & 0x200)
1016 levelof = 1;
1017
1018 int totallevel = level + (ksltltrem & 0x1ff);
1019 if (totallevel & 0x200)
1020 levelof = 1;
1021
1022 int totallevelclamp = (chip->reg_test0 & 1) != 0 ? 0 : (levelof ? 0x1ff : (totallevel & 0x1ff));
1023
1024 chip->eg_out[0] = totallevelclamp;
1025
1026 chip->eg_dbg[0] = chip->eg_dbg[1] >> 1;
1027
1028 if ((chip->reg_test0 & 32) != 0 && !chip->eg_dbg_load_l[1])
1029 {
1030 chip->eg_dbg[0] |= chip->eg_out[1];
1031 }
1032 chip->eg_dbg_load_l[0] = (chip->reg_test0 & 32) != 0;
1033
1034 if (chip->fsm_out[4] || chip->fsm_out[6])
1035 chip->eg_index[0] = 0;
1036 else
1037 chip->eg_index[0] = chip->eg_index[1] + 1;
1038
1039 if (chip->eg_index[1] < 18)
1040 {
1041 int index1 = chip->eg_index[1];
1042 int index2 = (index1 + 17) % 18;
1043 chip->eg_cells[index2] = (nextstate & 3) | ((levelnext & 511) << 2) | (chip->eg_timer_i << 11);
1044 chip->eg_cells[index2 + 18] = chip->eg_cells[index1];
1045 chip->eg_state_o[0] = chip->eg_cells[18 + index1] & 3;
1046 chip->eg_level_o[0] = (chip->eg_cells[18 + index1] >> 2) & 511;
1047 chip->eg_timer_o[0] = (chip->eg_cells[18 + index1] >> 11) & 1;
1048 }
1049 chip->eg_state_o[2] = chip->eg_state_o[1];
1050 chip->eg_level_o[2] = chip->eg_level_o[1];
1051 chip->eg_timer_o[2] = chip->eg_timer_o[1];
1052 }
1053 if (chip->clk2)
1054 {
1055 chip->eg_out[1] = chip->eg_out[0];
1056 chip->eg_dbg[1] = chip->eg_dbg[0];
1057 chip->eg_dbg_load_l[1] = chip->eg_dbg_load_l[0];
1058
1059 chip->eg_index[1] = chip->eg_index[0];
1060 chip->eg_state_o[1] = chip->eg_state_o[0];
1061 chip->eg_state_o[3] = chip->eg_state_o[2];
1062 chip->eg_level_o[1] = chip->eg_level_o[0];
1063 chip->eg_level_o[3] = chip->eg_level_o[2];
1064 chip->eg_timer_o[1] = chip->eg_timer_o[0];
1065 chip->eg_timer_o[3] = chip->eg_timer_o[2];
1066 }
1067
1068 if (chip->clk1)
1069 {
1070 static const int pg_multi[16] = {
1071 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
1072 };
1073 int fnum = chip->fnum[1];
1074 int freq;
1075 int pg_add;
1076 int vib_sel1 = (chip->vib_cnt[1] & 3) == 2;
1077 int vib_sel2 = (chip->vib_cnt[1] & 1) == 1;
1078 int vib_sh0 = chip->reg_dv && chip->vib[1] && vib_sel1;
1079 int vib_sh1 = (chip->reg_dv && chip->vib[1] && vib_sel2)
1080 || (!chip->reg_dv && chip->vib[1] && vib_sel1);
1081 int vib_sh2 = !chip->reg_dv && chip->vib[1] && vib_sel2;
1082 int vib_sign = (chip->vib_cnt[1] & 4) != 0 && chip->vib[1];
1083 int vib_add = 0;
1084 int phase;
1085 if (vib_sh0)
1086 vib_add |= (chip->fnum[1] >> 7) & 7;
1087 if (vib_sh1)
1088 vib_add |= (chip->fnum[1] >> 8) & 3;
1089 if (vib_sh2)
1090 vib_add |= (chip->fnum[1] >> 9) & 1;
1091 if (vib_sign)
1092 {
1093 vib_add ^= 1023;
1094 }
1095 fnum += vib_add;
1096 fnum += vib_sign;
1097 if (vib_sign)
1098 fnum &= 1023;
1099
1100 freq = (fnum << chip->block[1]) >> 1;
1101
1102 pg_add = (freq * pg_multi[chip->multi[1]]) >> 1;
1103
1104 int state = chip->eg_state_o[3];
1105 int dokon = state == eg_state_release && chip->keyon_comb;
1106
1107 phase = ((dokon || (chip->reg_test0 & 4) != 0 || chip->reset1) ? 0 : chip->pg_phase_o[3]) + pg_add;
1108
1109 if (chip->fsm_out[4] || chip->fsm_out[6])
1110 chip->pg_index[0] = 0;
1111 else
1112 chip->pg_index[0] = chip->pg_index[1] + 1;
1113
1114 if (chip->pg_index[1] < 18)
1115 {
1116 int index1 = chip->pg_index[1];
1117 int index2 = (index1 + 17) % 18;
1118 chip->pg_cells[index2] = phase;
1119 chip->pg_cells[index2 + 18] = chip->pg_cells[index1];
1120 chip->pg_phase_o[0] = chip->pg_cells[18 + index1];
1121 }
1122 chip->pg_phase_o[2] = chip->pg_phase_o[1];
1123 }
1124 if (chip->clk2)
1125 {
1126 chip->pg_index[1] = chip->pg_index[0];
1127 chip->pg_phase_o[1] = chip->pg_phase_o[0];
1128 chip->pg_phase_o[3] = chip->pg_phase_o[2];
1129 }
1130
1131 if (chip->rclk1)
1132 {
1133 int noise_bit;
1134
1135 noise_bit = ((chip->noise_lfsr[1] >> 22) ^ (chip->noise_lfsr[1] >> 8)) & 1;
1136
1137 if ((chip->noise_lfsr[1] & 0x7fffff) == 0)
1138 noise_bit |= 1;
1139
1140 noise_bit |= (chip->reg_test0 & 2) != 0;
1141
1142 if (chip->reset1)
1143 noise_bit = 0;
1144
1145 chip->noise_lfsr[0] = (chip->noise_lfsr[1] << 1) | noise_bit;
1146 }
1147 if (chip->rclk2)
1148 {
1149 chip->noise_lfsr[1] = chip->noise_lfsr[0];
1150 }
1151
1152 {
1153 int pg_out = chip->pg_phase_o[3] >> 9;
1154 int hh = chip->fsm_out[2] && chip->rhythm;
1155 int sd = chip->fsm_out[3] && chip->rhythm;
1156 int tc = chip->fsm_out[4] && chip->rhythm;
1157 int rhy = (chip->fsm_out[2] || chip->fsm_out[3] || chip->fsm_out[4]) && chip->rhythm;
1158 if (chip->clk1)
1159 chip->hh_load = chip->fsm_out[2];
1160 if (!chip->hh_load && chip->fsm_out[2])
1161 {
1162 chip->hh_bit2 = (pg_out >> 2) & 1;
1163 chip->hh_bit3 = (pg_out >> 3) & 1;
1164 chip->hh_bit7 = (pg_out >> 7) & 1;
1165 chip->hh_bit8 = (pg_out >> 8) & 1;
1166 }
1167 if (chip->clk1)
1168 chip->tc_load = tc;
1169 if (!chip->tc_load && tc)
1170 {
1171 chip->tc_bit3 = (pg_out >> 3) & 1;
1172 chip->tc_bit5 = (pg_out >> 5) & 1;
1173 }
1174
1175 if (chip->clk1) // opt
1176 {
1177 int rm_bit;
1178 int noise = (chip->noise_lfsr[1] >> 22) & 1;
1179
1180 rm_bit = (chip->hh_bit2 ^ chip->hh_bit7)
1181 | (chip->tc_bit5 ^ chip->hh_bit3)
1182 | (chip->tc_bit5 ^ chip->tc_bit3);
1183
1184 chip->pg_out_rhy = 0;
1185 if (!rhy)
1186 chip->pg_out_rhy |= pg_out;
1187 if (hh)
1188 {
1189 chip->pg_out_rhy |= rm_bit << 9;
1190 if (noise ^ rm_bit)
1191 chip->pg_out_rhy |= 0xd0;
1192 else
1193 chip->pg_out_rhy |= 0x34;
1194 }
1195 if (sd)
1196 chip->pg_out_rhy |= (chip->hh_bit8 << 9) | ((noise ^ chip->hh_bit8) << 8);
1197 if (tc)
1198 chip->pg_out_rhy |= (rm_bit << 9) | 0x80;
1199 }
1200
1201 if (chip->clk1)
1202 {
1203 chip->pg_dbg[0] = chip->pg_dbg[1] >> 1;
1204
1205 chip->pg_dbg_load_l[0] = (chip->reg_test0 & 8) != 0;
1206
1207 if ((chip->reg_test0 & 8) != 0 && !chip->pg_dbg_load_l[1])
1208 {
1209 chip->pg_dbg[0] |= chip->pg_phase_o[3] & 0x1ff;
1210 chip->pg_dbg[0] |= (chip->pg_out_rhy & 0x3ff) << 9;
1211 }
1212 }
1213 if (chip->clk2)
1214 {
1215 chip->pg_dbg_load_l[1] = chip->pg_dbg_load_l[0];
1216
1217 chip->pg_dbg[1] = chip->pg_dbg[0];
1218 }
1219 }
1220
1221 {
1222
1223 if (chip->clk1)
1224 {
1225 static const int logsin[128] = {
1226 0x6c3, 0x58b, 0x4e4, 0x471, 0x41a, 0x3d3, 0x398, 0x365, 0x339, 0x311, 0x2ed, 0x2cd, 0x2af, 0x293, 0x279, 0x261,
1227 0x24b, 0x236, 0x222, 0x20f, 0x1fd, 0x1ec, 0x1dc, 0x1cd, 0x1be, 0x1b0, 0x1a2, 0x195, 0x188, 0x17c, 0x171, 0x166,
1228 0x15b, 0x150, 0x146, 0x13c, 0x133, 0x129, 0x121, 0x118, 0x10f, 0x107, 0x0ff, 0x0f8, 0x0f0, 0x0e9, 0x0e2, 0x0db,
1229 0x0d4, 0x0cd, 0x0c7, 0x0c1, 0x0bb, 0x0b5, 0x0af, 0x0a9, 0x0a4, 0x09f, 0x099, 0x094, 0x08f, 0x08a, 0x086, 0x081,
1230 0x07d, 0x078, 0x074, 0x070, 0x06c, 0x068, 0x064, 0x060, 0x05c, 0x059, 0x055, 0x052, 0x04e, 0x04b, 0x048, 0x045,
1231 0x042, 0x03f, 0x03c, 0x039, 0x037, 0x034, 0x031, 0x02f, 0x02d, 0x02a, 0x028, 0x026, 0x024, 0x022, 0x020, 0x01e,
1232 0x01c, 0x01a, 0x018, 0x017, 0x015, 0x014, 0x012, 0x011, 0x00f, 0x00e, 0x00d, 0x00c, 0x00a, 0x009, 0x008, 0x007,
1233 0x007, 0x006, 0x005, 0x004, 0x004, 0x003, 0x002, 0x002, 0x001, 0x001, 0x001, 0x001, 0x000, 0x000, 0x000, 0x000
1234 };
1235 static const int logsin_d[128] = {
1236 0x196, 0x07c, 0x04a, 0x035, 0x029, 0x022, 0x01d, 0x019, 0x015, 0x013, 0x012, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c,
1237 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x009, 0x008, 0x007, 0x007, 0x007, 0x007, 0x006, 0x007, 0x006, 0x006, 0x005,
1238 0x005, 0x005, 0x005, 0x005, 0x004, 0x005, 0x004, 0x004, 0x005, 0x004, 0x004, 0x003, 0x004, 0x003, 0x003, 0x003,
1239 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003, 0x003, 0x003, 0x003, 0x002, 0x002,
1240 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x001, 0x002, 0x002, 0x002, 0x001,
1241 0x001, 0x001, 0x002, 0x002, 0x001, 0x001, 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
1242 0x001, 0x001, 0x001, 0x000, 0x001, 0x000, 0x001, 0x000, 0x001, 0x001, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001,
1243 0x000, 0x000, 0x000, 0x001, 0x000, 0x000, 0x001, 0x000, 0x001, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
1244 };
1245 static const int pow[128] = {
1246 0x3f5, 0x3ea, 0x3df, 0x3d4, 0x3c9, 0x3bf, 0x3b4, 0x3a9, 0x39f, 0x394, 0x38a, 0x37f, 0x375, 0x36a, 0x360, 0x356,
1247 0x34c, 0x342, 0x338, 0x32e, 0x324, 0x31a, 0x310, 0x306, 0x2fd, 0x2f3, 0x2e9, 0x2e0, 0x2d6, 0x2cd, 0x2c4, 0x2ba,
1248 0x2b1, 0x2a8, 0x29e, 0x295, 0x28c, 0x283, 0x27a, 0x271, 0x268, 0x25f, 0x257, 0x24e, 0x245, 0x23c, 0x234, 0x22b,
1249 0x223, 0x21a, 0x212, 0x209, 0x201, 0x1f9, 0x1f0, 0x1e8, 0x1e0, 0x1d8, 0x1d0, 0x1c8, 0x1c0, 0x1b8, 0x1b0, 0x1a8,
1250 0x1a0, 0x199, 0x191, 0x189, 0x181, 0x17a, 0x172, 0x16b, 0x163, 0x15c, 0x154, 0x14d, 0x146, 0x13e, 0x137, 0x130,
1251 0x129, 0x122, 0x11b, 0x114, 0x10c, 0x106, 0x0ff, 0x0f8, 0x0f1, 0x0ea, 0x0e3, 0x0dc, 0x0d6, 0x0cf, 0x0c8, 0x0c2,
1252 0x0bb, 0x0b5, 0x0ae, 0x0a8, 0x0a1, 0x09b, 0x094, 0x08e, 0x088, 0x082, 0x07b, 0x075, 0x06f, 0x069, 0x063, 0x05d,
1253 0x057, 0x051, 0x04b, 0x045, 0x03f, 0x039, 0x033, 0x02d, 0x028, 0x022, 0x01c, 0x016, 0x011, 0x00b, 0x006, 0x000,
1254 };
1255 static const int pow_d[128] = {
1256 0x005, 0x005, 0x005, 0x006, 0x006, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x006, 0x005, 0x005,
1257 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x004, 0x005,
1258 0x004, 0x004, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x004, 0x004, 0x004, 0x005, 0x004, 0x005,
1259 0x004, 0x004, 0x004, 0x005, 0x004, 0x004, 0x005, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
1260 0x004, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x003, 0x004, 0x004, 0x004,
1261 0x003, 0x003, 0x003, 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x003, 0x003, 0x004, 0x003,
1262 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003,
1263 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003,
1264 };
1265 int wf = chip->wf[1];
1266 int phase = chip->pg_out_rhy + chip->op_mod[1];
1267 int square = wf == 6;
1268 int sawtooth = wf == 7;
1269
1270 int phase2;
1271 if (wf == 4 || wf == 5)
1272 phase2 = phase << 1;
1273 else
1274 phase2 = phase;
1275 phase2 &= 1023;
1276
1277 if (wf == 7 ? (phase2 & 512) != 0 : (phase2 & 256) != 0)
1278 phase2 ^= 511;
1279
1280 int mute = ((phase & 512) != 0 && (wf == 1 || wf == 4 || wf == 5)) || ((phase & 256) != 0 && wf == 3);
1281 int sign = (wf == 2 || wf == 3 || wf == 5) ? 0 : (phase2 & 512) != 0;
1282
1283 int index = square ? 255 : (phase2 & 255);
1284
1285 int ls = logsin[index >> 1];
1286 if ((index & 1) == 0)
1287 ls += logsin_d[index >> 1];
1288
1289 chip->op_logsin[0] = ls;
1290 chip->op_saw[0] = sawtooth;
1291 chip->op_saw_phase[0] = phase2 & 511;
1292
1293 int att = (chip->op_saw[1] ? (chip->op_saw_phase[1] << 3) : chip->op_logsin[1]) + (chip->eg_out[1] << 3);
1294 if (att & 4096)
1295 att = 4095;
1296
1297 int pw = pow[(att >> 1) & 127];
1298 if ((att & 1) == 0)
1299 pw += pow_d[(att >> 1) & 127];
1300
1301 chip->op_shift[0] = (att >> 8) & 15;
1302 chip->op_pow[0] = pw;
1303
1304 chip->op_mute[0] = (chip->op_mute[1] << 1) | mute;
1305 chip->op_sign[0] = (chip->op_sign[1] << 1) | sign;
1306
1307 int value = 0;
1308
1309 if ((chip->op_mute[1] & 2) == 0)
1310 {
1311 value = ((chip->op_pow[1] | 0x400) << 1) >> chip->op_shift[1];
1312 }
1313
1314 if ((chip->op_mute[1] & 2) == 0 && (chip->op_sign[1] & 2) != 0)
1315 value ^= 8191;
1316
1317 for (i = 0; i < 13; i++)
1318 {
1319 int bit;
1320 chip->op_fb[0][i][0] = chip->op_fb[0][i][1] << 1;
1321 if (chip->fsm_out[15])
1322 bit = (value >> i) & 1;
1323 else
1324 bit = (chip->op_fb[0][i][1] >> 8) & 1;
1325 chip->op_fb[0][i][0] |= bit;
1326 chip->op_fb[1][i][0] = chip->op_fb[1][i][1] << 1;
1327 if (chip->fsm_out[15])
1328 bit = (chip->op_fb[0][i][1] >> 8) & 1;
1329 else
1330 bit = (chip->op_fb[1][i][1] >> 8) & 1;
1331 chip->op_fb[1][i][0] |= bit;
1332 chip->op_fb[2][i][0] = chip->op_fb[2][i][1] << 1;
1333 if (chip->fsm_out[15])
1334 bit = (chip->op_fb[1][i][1] >> 8) & 1;
1335 else
1336 bit = (chip->op_fb[2][i][1] >> 8) & 1;
1337 chip->op_fb[2][i][0] |= bit;
1338 chip->op_fb[3][i][0] = chip->op_fb[3][i][1] << 1;
1339 if (chip->fsm_out[15])
1340 bit = (chip->op_fb[2][i][1] >> 8) & 1;
1341 else
1342 bit = (chip->op_fb[3][i][1] >> 8) & 1;
1343 chip->op_fb[3][i][0] |= bit;
1344 }
1345
1346 int fb1 = 0;
1347 int fb2 = 0;
1348 for (i = 0; i < 14; i++)
1349 {
1350 int j = i;
1351 if (i == 13)
1352 j = 12;
1353 fb1 |= ((chip->op_fb[1][j][1] >> 5) & 1) << i;
1354 fb2 |= ((chip->op_fb[3][j][1] >> 5) & 1) << i;
1355 }
1356 int fb_sum = fb1 + fb2;
1357 fb_sum &= 16383;
1358 if (fb_sum & 8192)
1359 fb_sum |= ~8191;
1360
1361 int mod = 0;
1362
1363 if (chip->fsm_out[16] && !chip->fsm_out[14])
1364 mod |= value & 1023;
1365 if (chip->fsm_out[12])
1366 {
1367 if (chip->fb_l[1][1])
1368 {
1369 mod |= (fb_sum >> (9 - chip->fb_l[1][1])) & 1023;
1370 }
1371 }
1372
1373 chip->op_mod[0] = mod & 1023;
1374
1375 chip->op_value = value;
1376 }
1377 if (chip->clk2)
1378 {
1379 chip->op_logsin[1] = chip->op_logsin[0];
1380 chip->op_saw[1] = chip->op_saw[0];
1381 chip->op_saw_phase[1] = chip->op_saw_phase[0];
1382 chip->op_shift[1] = chip->op_shift[0];
1383 chip->op_pow[1] = chip->op_pow[0];
1384 chip->op_mute[1] = chip->op_mute[0];
1385 chip->op_sign[1] = chip->op_sign[0];
1386
1387 for (i = 0; i < 13; i++)
1388 {
1389 chip->op_fb[0][i][1] = chip->op_fb[0][i][0];
1390 chip->op_fb[1][i][1] = chip->op_fb[1][i][0];
1391 chip->op_fb[2][i][1] = chip->op_fb[2][i][0];
1392 chip->op_fb[3][i][1] = chip->op_fb[3][i][0];
1393 }
1394 chip->op_mod[1] = chip->op_mod[0];
1395 }
1396 }
1397
1398 {
1399 if (chip->clk1)
1400 {
1401 chip->accm_load_ac_l = chip->fsm_out[6];
1402 chip->accm_load_bd_l = chip->fsm_out[4];
1403 }
1404 chip->accm_load_ac = !chip->accm_load_ac_l && chip->fsm_out[6];
1405 chip->accm_load_bd = !chip->accm_load_bd_l && chip->fsm_out[4];
1406
1407 if (chip->accm_load_ac)
1408 {
1409 chip->accm_a_sign = (chip->accm_a[1] & 0x40000) == 0;
1410 chip->accm_a_of = !((chip->accm_a[1] & 0x78000) == 0 || (chip->accm_a[1] & 0x78000) == 0x78000);
1411 chip->accm_c_sign = (chip->accm_c[1] & 0x40000) == 0;
1412 chip->accm_c_of = !((chip->accm_c[1] & 0x78000) == 0 || (chip->accm_c[1] & 0x78000) == 0x78000);
1413 }
1414
1415 if (chip->accm_load_bd)
1416 {
1417 chip->accm_b_sign = (chip->accm_b[1] & 0x40000) == 0;
1418 chip->accm_b_of = !((chip->accm_b[1] & 0x78000) == 0 || (chip->accm_b[1] & 0x78000) == 0x78000);
1419 chip->accm_d_sign = (chip->accm_d[1] & 0x40000) == 0;
1420 chip->accm_d_of = !((chip->accm_d[1] & 0x78000) == 0 || (chip->accm_d[1] & 0x78000) == 0x78000);
1421 }
1422
1423 if (chip->clk1)
1424 {
1425 int value = 0;
1426
1427 if (chip->fsm_out[13])
1428 {
1429 if (chip->fsm_out[11])
1430 value = chip->op_value << 1;
1431 else
1432 {
1433 value = chip->op_value;
1434 if (chip->op_value & 0x1000)
1435 value |= 0x2000;
1436 }
1437 }
1438 if (value & 0x2000)
1439 {
1440 value |= 0x7c000;
1441 }
1442
1443 int sign;
1444
1445 chip->op_value_debug = chip->op_value;
1446
1447 int accm_a = chip->fsm_out[6] ? 0 : chip->accm_a[1];
1448 accm_a += (chip->pan_l[1][1] & 1) != 0 ? value : 0;
1449 chip->accm_a[0] = accm_a;
1450 sign = (chip->accm_a[1] & 0x40000) == 0;
1451 chip->accm_shift_a[0] = (chip->accm_shift_a[1] >> 1);
1452 if (chip->fsm_out[6])
1453 {
1454 chip->accm_shift_a[0] |= chip->accm_a[1] & 0x7fff;
1455 if (sign)
1456 chip->accm_shift_a[0] |= 0x8000;
1457 }
1458
1459 int accm_b = chip->fsm_out[4] ? 0 : chip->accm_b[1];
1460 accm_b += (chip->pan_l[1][1] & 2) != 0 ? value : 0;
1461 chip->accm_b[0] = accm_b;
1462 sign = (chip->accm_b[1] & 0x40000) == 0;
1463 chip->accm_shift_b[0] = (chip->accm_shift_b[1] >> 1);
1464 if (chip->fsm_out[4])
1465 {
1466 chip->accm_shift_b[0] |= chip->accm_b[1] & 0x7fff;
1467 if (sign)
1468 chip->accm_shift_b[0] |= 0x8000;
1469 }
1470
1471 int accm_c = chip->fsm_out[6] ? 0 : chip->accm_c[1];
1472 accm_c += (chip->pan_l[1][1] & 4) != 0 ? value : 0;
1473 chip->accm_c[0] = accm_c;
1474 sign = (chip->accm_c[1] & 0x40000) == 0;
1475 chip->accm_shift_c[0] = (chip->accm_shift_c[1] >> 1);
1476 if (chip->fsm_out[6])
1477 {
1478 chip->accm_shift_c[0] |= chip->accm_c[1] & 0x7fff;
1479 if (sign)
1480 chip->accm_shift_c[0] |= 0x8000;
1481 }
1482
1483 int accm_d = chip->fsm_out[4] ? 0 : chip->accm_d[1];
1484 accm_d += (chip->pan_l[1][1] & 8) != 0 ? value : 0;
1485 chip->accm_d[0] = accm_d;
1486 sign = (chip->accm_d[1] & 0x40000) == 0;
1487 chip->accm_shift_d[0] = (chip->accm_shift_d[1] >> 1);
1488 if (chip->fsm_out[4])
1489 {
1490 chip->accm_shift_d[0] |= chip->accm_d[1] & 0x7fff;
1491 if (sign)
1492 chip->accm_shift_d[0] |= 0x8000;
1493 }
1494 }
1495 if (chip->clk2)
1496 {
1497 chip->accm_a[1] = chip->accm_a[0];
1498 chip->accm_b[1] = chip->accm_b[0];
1499 chip->accm_c[1] = chip->accm_c[0];
1500 chip->accm_d[1] = chip->accm_d[0];
1501
1502 chip->accm_shift_a[1] = chip->accm_shift_a[0];
1503 chip->accm_shift_b[1] = chip->accm_shift_b[0];
1504 chip->accm_shift_c[1] = chip->accm_shift_c[0];
1505 chip->accm_shift_d[1] = chip->accm_shift_d[0];
1506 }
1507
1508 if (chip->fsm_out[8])
1509 {
1510 chip->o_doab = chip->accm_a_of ? chip->accm_a_sign : chip->accm_shift_a[1] & 1;
1511 chip->o_docd = chip->accm_c_of ? chip->accm_c_sign : chip->accm_shift_c[1] & 1;
1512 }
1513 else
1514 {
1515 chip->o_doab = chip->accm_b_of ? chip->accm_b_sign : chip->accm_shift_b[1] & 1;
1516 chip->o_docd = chip->accm_d_of ? chip->accm_d_sign : chip->accm_shift_d[1] & 1;
1517 }
1518 }
1519
1520 chip->o_sy = chip->clk2;
1521 chip->o_smpac = chip->fsm_out[10];
1522 chip->o_smpbd = chip->fsm_out[9];
1523 chip->o_irq_pull = chip->t1_status || chip->t2_status;
1524
1525 if (chip->io_read)
1526 {
1527 chip->data_o = 0;
1528 if (chip->t1_status || chip->t2_status)
1529 chip->data_o |= 128;
1530 if (chip->t1_status)
1531 chip->data_o |= 64;
1532 if (chip->t2_status)
1533 chip->data_o |= 32;
1534 chip->data_z = 0;
1535 }
1536 else
1537 chip->data_z = 1;
1538
1539 {
1540 if (chip->clk1)
1541 {
1542 chip->ra_dbg1[0] = chip->ra_dbg1[1] >> 1;
1543 chip->ra_dbg2[0] = chip->ra_dbg2[1] >> 1;
1544 if ((chip->reg_test0 & 128) != 0 && !chip->ra_dbg_load[1])
1545 {
1546 chip->ra_dbg1[0] |= (int64_t)chip->multi[1] << 0;
1547 chip->ra_dbg1[0] |= (int64_t)chip->ksr[1] << 4;
1548 chip->ra_dbg1[0] |= (int64_t)chip->egt[1] << 5;
1549 chip->ra_dbg1[0] |= (int64_t)chip->vib[1] << 6;
1550 chip->ra_dbg1[0] |= (int64_t)chip->am[1] << 7;
1551 chip->ra_dbg1[0] |= (int64_t)chip->tl[1] << 8;
1552 chip->ra_dbg1[0] |= (int64_t)chip->ksl[1] << 14;
1553 chip->ra_dbg1[0] |= (int64_t)chip->dr[1] << 16;
1554 chip->ra_dbg1[0] |= (int64_t)chip->ar[1] << 20;
1555 chip->ra_dbg1[0] |= (int64_t)chip->rr[1] << 24;
1556 chip->ra_dbg1[0] |= (int64_t)chip->sl[1] << 28;
1557 chip->ra_dbg1[0] |= (int64_t)chip->wf[1] << 32;
1558 chip->ra_dbg2[0] |= (int64_t)chip->fnum[1] << 0;
1559 chip->ra_dbg2[0] |= (int64_t)chip->block[1] << 10;
1560 chip->ra_dbg2[0] |= (int64_t)chip->keyon[1] << 13;
1561 chip->ra_dbg2[0] |= (int64_t)chip->connect[1] << 14;
1562 chip->ra_dbg2[0] |= (int64_t)chip->pan[1] << 15;
1563 chip->ra_dbg2[0] |= (int64_t)chip->connect_pair[1] << 19;
1564 chip->ra_dbg2[0] |= (int64_t)chip->fb[1] << 20;
1565 }
1566 chip->ra_dbg_load[0] = (chip->reg_test0 & 128) != 0;
1567 }
1568 if (chip->clk2)
1569 {
1570 chip->ra_dbg1[1] = chip->ra_dbg1[0];
1571 chip->ra_dbg2[1] = chip->ra_dbg2[0];
1572 chip->ra_dbg_load[1] = chip->ra_dbg_load[0];
1573 }
1574 }
1575
1576 switch (chip->reg_test1 & 7)
1577 {
1578 case 0:
1579 chip->o_test = 0;
1580 break;
1581 case 1:
1582 chip->o_test = (chip->ra_dbg1[1] & 1) != 0;
1583 break;
1584 case 2:
1585 chip->o_test = (chip->ra_dbg2[1] & 1) != 0;
1586 break;
1587 case 3:
1588 chip->o_test = (chip->pg_dbg[1] & 1) != 0;
1589 break;
1590 case 4:
1591 chip->o_test = (chip->eg_dbg[1] & 1) != 0;
1592 break;
1593 }
1594
1595 end:
1596
1597 if (chip->io_write0)
1598 chip->write0_sr = 1;
1599 else if (chip->reset0 || chip->write0_l[1])
1600 chip->write0_sr = 0;
1601
1602 if (chip->io_write1)
1603 chip->write1_sr = 1;
1604 else if (chip->reset0 || chip->write1_l[1])
1605 chip->write1_sr = 0;
1606
1607 if (chip->clk1)
1608 {
1609 chip->write0_l[0] = chip->write0_sr;
1610 chip->write0_l[2] = chip->write0_l[1];
1611
1612 chip->write1_l[0] = chip->write1_sr;
1613 chip->write1_l[2] = chip->write1_l[1];
1614 }
1615
1616 if (chip->mclk1)
1617 {
1618 chip->prescaler1_reset[0] = (chip->prescaler1_reset[1] << 1) | chip->reset1;
1619
1620 if (!(chip->prescaler1_reset[1] & 2) && chip->reset1)
1621 chip->prescaler1_cnt[0] = 0;
1622 else
1623 chip->prescaler1_cnt[0] = (chip->prescaler1_cnt[1] + 1) & 3;
1624 }
1625
1626 if (chip->aclk1)
1627 {
1628 int prescaler2_reset = !(chip->prescaler2_reset_l[1] & 2) && chip->reset1;
1629 chip->prescaler2_reset_l[0] = (chip->prescaler2_reset_l[1] << 1) | chip->reset1;
1630
1631 if (prescaler2_reset)
1632 chip->prescaler2_cnt[0] = 0;
1633 else
1634 chip->prescaler2_cnt[0] = (chip->prescaler2_cnt[1] + 1) & 3;
1635
1636 chip->prescaler2_l1[0] = !prescaler2_reset && (chip->prescaler2_cnt[1] & 1) == 0;
1637 chip->prescaler2_l2 = chip->prescaler2_l1[1];
1638
1639 chip->prescaler2_l3[0] = !prescaler2_reset && (chip->prescaler2_cnt[1] & 1) != 0;
1640 chip->prescaler2_l4 = chip->prescaler2_l3[1];
1641
1642 chip->prescaler2_l5[0] = !prescaler2_reset && chip->prescaler2_cnt[1] == 3;
1643
1644 chip->prescaler2_l6[0] = !prescaler2_reset && chip->prescaler2_cnt[1] == 1;
1645
1646 chip->prescaler2_l7 = (chip->prescaler2_cnt[1] & 1) == 0;
1647 }
1648 }