Debian Codegraph @

extern/YMF276-LLE/fmopn2.c

raw
1 /*
2 * Copyright (C) 2022-2023 nukeykt
3 *
4 * This file is part of YMF276-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 * YMF276/YM3438 emulator.
17 * Thanks:
18 * John McMaster (siliconpr0n.org):
19 * Yamaha YM3438 & YM2610 decap and die shot.
20 * org, andkorzh, HardWareMan (emu-russia):
21 * help & support, YMF276 and YM2612 decap.
22 *
23 */
24
25 // YMF276/YM3438 core
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "fmopn2.h"
31
32 enum {
33 eg_state_attack = 0,
34 eg_state_decay,
35 eg_state_sustain,
36 eg_state_release
37 };
38
39 static const int fm_algorithm[4][6][8] = {
40 {
41 { 1, 1, 1, 1, 1, 1, 1, 1 }, /* OP1_0 */
42 { 1, 1, 1, 1, 1, 1, 1, 1 }, /* OP1_1 */
43 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP2 */
44 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
45 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
46 { 0, 0, 0, 0, 0, 0, 0, 1 } /* Out */
47 },
48 {
49 { 0, 1, 0, 0, 0, 1, 0, 0 }, /* OP1_0 */
50 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */
51 { 1, 1, 1, 0, 0, 0, 0, 0 }, /* OP2 */
52 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
53 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
54 { 0, 0, 0, 0, 0, 1, 1, 1 } /* Out */
55 },
56 {
57 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_0 */
58 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */
59 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP2 */
60 { 1, 0, 0, 1, 1, 1, 1, 0 }, /* Last operator */
61 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
62 { 0, 0, 0, 0, 1, 1, 1, 1 } /* Out */
63 },
64 {
65 { 0, 0, 1, 0, 0, 1, 0, 0 }, /* OP1_0 */
66 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */
67 { 0, 0, 0, 1, 0, 0, 0, 0 }, /* OP2 */
68 { 1, 1, 0, 1, 1, 0, 0, 0 }, /* Last operator */
69 { 0, 0, 1, 0, 0, 0, 0, 0 }, /* Last operator */
70 { 1, 1, 1, 1, 1, 1, 1, 1 } /* Out */
71 }
72 };
73
74 void FMOPN2_ClockPhase1(fmopn2_t *chip);
75
76 void FMOPN2_Prescaler(fmopn2_t *chip)
77 {
78 if (!chip->pinput.phi)
79 {
80 int ic_check;
81
82 chip->ic_latch[0] = chip->ic_latch[1] << 1;
83 chip->ic_latch[0] |= chip->pinput.ic;
84
85 ic_check = (chip->ic_latch[1] & 0x800) == 0 && chip->pinput.ic;
86
87 chip->prescaler_latch[0] = chip->prescaler_latch[1] << 1;
88 chip->prescaler_latch[0] |= !ic_check && (chip->prescaler_latch[1] & 0x1f) == 0;
89
90 chip->ic_check_latch[0] = chip->ic_check_latch[1] << 1;
91 chip->ic_check_latch[0] |= ic_check;
92
93 chip->phi1_latch[0] = (chip->prescaler_latch[1] & 0x21) != 0;
94 chip->phi2_latch[0] = (chip->prescaler_latch[1] & 0xc) != 0;
95
96 if (!(chip->flags & fmopn2_flags_ym3438))
97 {
98 chip->dphi1_latch[0] = (chip->prescaler_latch[1] & 0x9) != 0;
99 chip->dphi2_latch[0] = (chip->prescaler_latch[1] & 0x24) != 0;
100
101 chip->dphi1_latch[2] = chip->dphi1_latch[1];
102 chip->dphi2_latch[2] = chip->dphi2_latch[1];
103 }
104 }
105 else
106 {
107 chip->ic_latch[1] = chip->ic_latch[0] & 0xfff;
108 chip->ic_check_latch[1] = chip->ic_check_latch[0] & 0xf;
109 chip->prescaler_latch[1] = chip->prescaler_latch[0] & 0x3f;
110 chip->phi1_latch[1] = chip->phi1_latch[0] & 0x1;
111 chip->phi2_latch[1] = chip->phi2_latch[0] & 0x1;
112
113 if (!(chip->flags & fmopn2_flags_ym3438))
114 {
115 chip->dphi1_latch[1] = chip->dphi1_latch[0];
116 chip->dphi2_latch[1] = chip->dphi2_latch[0];
117
118 chip->dphi1_latch[3] = chip->dphi1_latch[2];
119 }
120 }
121
122 chip->fsm_reset = (chip->ic_check_latch[1] & 16) != 0;
123
124 if (!(chip->flags & fmopn2_flags_ym3438))
125 {
126 chip->dclk1 = chip->dphi1_latch[1] && !chip->dphi1_latch[2];
127 chip->dclk2 = chip->dphi2_latch[1] && !chip->dphi2_latch[2];
128 }
129 }
130
131 void FMOPN2_YMF276DAC(fmopn2_t *chip)
132 {
133 chip->fsm_load_l = (chip->fsm_shifter_ctrl[1] & 0x20) != 0 && chip->i_phi2;
134 chip->fsm_load_r = (chip->fsm_shifter_ctrl[1] & 0x20000) != 0 && chip->i_phi2;
135
136 if (chip->dclk1)
137 {
138 chip->dac_shifter[0] = chip->dac_shifter[1] << 1;
139
140 if (chip->fsm_load_l)
141 chip->dac_shifter[0] |= (chip->ch_accm_l[1] >> 1);
142 if (chip->fsm_load_r)
143 chip->dac_shifter[0] |= (chip->ch_accm_r[1] >> 1);
144 }
145 if (chip->dclk2)
146 {
147 chip->dac_shifter[1] = chip->dac_shifter[0];
148 }
149
150 if (!chip->pinput.phi)
151 {
152 chip->fsm_lro_l2[0] = chip->fsm_lro_l2[1] << 1;
153 if (chip->fsm_lro_l[1] & 2)
154 chip->fsm_lro_l2[0] |= 1;
155 chip->fsm_wco_l2[0] = chip->fsm_wco_l2[1] << 1;
156 if (chip->fsm_wco_l[1] & 2)
157 chip->fsm_wco_l2[0] |= 1;
158
159 chip->dac_so_l[0] = chip->dac_so_l[1] << 1;
160 if (chip->dac_shifter[1] & 0x8000)
161 chip->dac_so_l[0] |= 1;
162 }
163 else
164 {
165 chip->fsm_lro_l2[1] = chip->fsm_lro_l2[0];
166 chip->fsm_wco_l2[1] = chip->fsm_wco_l2[0];
167
168 chip->dac_so_l[1] = chip->dac_so_l[0];
169 }
170
171 chip->o_bco = chip->dphi1_latch[2] || chip->dphi1_latch[3];
172 chip->o_wco = (chip->fsm_wco_l2[1] & 32) != 0;
173 chip->o_lro = (chip->fsm_lro_l2[1] & 32) != 0;
174 chip->o_so = (chip->dac_so_l[1] & 4) != 0;
175 }
176
177 void FMOPN2_HandleIO(fmopn2_t *chip)
178 {
179 int write_data = chip->input.cs && chip->input.wr && (chip->input.address & 1) == 1 && !chip->input.ic;
180 int write_addr = (chip->input.cs && chip->input.wr && (chip->input.address & 1) == 0) || chip->input.ic;
181 int read_enable = chip->input.cs && chip->input.rd && !chip->input.ic;
182 int io_dir = chip->input.cs && chip->input.rd && !chip->input.ic;
183 int data_enable = !io_dir && !chip->input.ic;
184
185 if (chip->input.cs && chip->input.wr)
186 {
187 chip->data_latch = chip->input.data;
188 chip->bank_latch = (chip->input.address >> 1) & 1;
189 }
190
191 if (write_addr)
192 chip->write_addr_trig = 1;
193 if (write_data)
194 chip->write_data_trig = 1;
195
196 if (!read_enable)
197 {
198 chip->status_timer_a_dlatch = chip->timer_a_status[1];
199 chip->status_timer_b_dlatch = chip->timer_b_status[1];
200 }
201 }
202
203 int FMOPN2_GetBus(fmopn2_t *chip)
204 {
205 int data = 0;
206 int io_dir = chip->input.cs && chip->input.rd && !chip->input.ic;
207 int data_enable = !io_dir && !chip->input.ic;
208 if (data_enable)
209 data = chip->data_latch;
210 if (chip->io_ic_latch[1])
211 data = 0;
212 return data;
213 }
214
215
216 int FMOPN2_ReadTest(fmopn2_t *chip)
217 {
218 if (chip->mode_test_2c[1] & 128)
219 return chip->fsm_sel23;
220 return 0; // FIXME: high impedance
221 }
222
223 int FMOPN2_ReadStatus(fmopn2_t *chip)
224 {
225 int io_dir = chip->input.cs && chip->input.rd && !chip->input.ic;
226 int read_enable = chip->input.cs && chip->input.rd && !chip->input.ic && (chip->input.address & 3) == 0;
227 int status;
228 int testdata = 0;
229 if (!io_dir)
230 return 0;
231
232 if (!read_enable)
233 {
234 return 0; // FIXME: floating bus
235 }
236 if (chip->mode_test_21[1] & 64)
237 {
238 testdata |= (chip->pg_debug[1] & 1) << 15;
239 if (chip->mode_test_21[1] & 1)
240 testdata |= ((chip->eg_debug[1] >> 9) & 1) << 14;
241 else
242 testdata |= (chip->eg_incsh_nonzero[1] & 1) << 14;
243 if (chip->mode_test_2c[1] & 16)
244 testdata |= chip->ch_out_debug[1] & 0x1ff;
245 else
246 testdata |= chip->op_output[1] & 0x3fff;
247 if (chip->mode_test_21[1] & 128)
248 status = testdata & 255;
249 else
250 status = testdata >> 8;
251 }
252 else
253 {
254 status = (chip->busy_latch[1] << 7) | (chip->status_timer_b_dlatch << 1) | chip->status_timer_a_dlatch;
255 }
256 return status;
257 }
258
259 void FMOPN2_FSM1(fmopn2_t *chip)
260 {
261 int i;
262 int connect = 0;
263 int reset = chip->input.i_fsm_reset;
264 chip->fsm_cnt1[0] = chip->fsm_cnt1[1] + 1;
265 if (reset || (chip->fsm_cnt1[1] & 2) != 0)
266 chip->fsm_cnt1[0] = 0;
267 chip->fsm_cnt2[0] = chip->fsm_cnt2[1];
268 if ((chip->fsm_cnt1[1] & 2) != 0)
269 chip->fsm_cnt2[0]++;
270 if (reset)
271 chip->fsm_cnt2[0] = 0;
272
273 if (!(chip->flags & fmopn2_flags_ym3438))
274 {
275 int cnt_comb = (chip->fsm_cnt2[1] << 2) | chip->fsm_cnt1[1];
276
277 chip->fsm_clock_eg_l = cnt_comb == 30;
278 chip->fsm_op1_sel_l = cnt_comb == 6 || cnt_comb == 8 || cnt_comb == 9 || cnt_comb == 10 || cnt_comb == 12 || cnt_comb == 13;
279 chip->fsm_sel1_l = cnt_comb == 0;
280 chip->fsm_sel2_l = cnt_comb == 1;
281 chip->fsm_sel23_l = cnt_comb == 29;
282 chip->fsm_ch3_sel_l = cnt_comb == 1 || cnt_comb == 9 || cnt_comb == 17 || cnt_comb == 25;
283 chip->fsm_dac_load_l = cnt_comb == 30 || cnt_comb == 4 || cnt_comb == 9 || cnt_comb == 14 || cnt_comb == 20 || cnt_comb == 9;
284 chip->fsm_dac_out_sel_l = cnt_comb == 14 || cnt_comb == 16 || cnt_comb == 17 || cnt_comb == 18 || cnt_comb == 20 || cnt_comb == 21 ||
285 cnt_comb == 22 || cnt_comb == 24 || cnt_comb == 25 || cnt_comb == 26 || cnt_comb == 28 || cnt_comb == 29;
286 chip->fsm_dac_ch6_l = cnt_comb == 4 || cnt_comb == 5 || cnt_comb == 6 || cnt_comb == 8;
287
288 chip->fsm_wco_l[0] = (chip->fsm_wco_l[1] << 1) | ((cnt_comb & 8) == 0);
289 chip->fsm_lro_l[0] = chip->fsm_lro_l[1] << 1;
290 if (((cnt_comb >> 3) ^ (cnt_comb >> 4)) & 1)
291 chip->fsm_lro_l[0] |= 1;
292 }
293
294 }
295
296 void FMOPN2_FSM2(fmopn2_t *chip)
297 {
298 int i, connect = 0;
299 int cnt_comb;
300 chip->fsm_cnt1[1] = chip->fsm_cnt1[0] & 0x3;
301 chip->fsm_cnt2[1] = chip->fsm_cnt2[0] & 0x7;
302
303 cnt_comb = (chip->fsm_cnt2[1] << 2) | chip->fsm_cnt1[1];
304
305 if (!(chip->flags & fmopn2_flags_ym3438))
306 {
307 chip->fsm_clock_eg = chip->fsm_clock_eg_l;
308 chip->fsm_op4_sel = cnt_comb == 0 || cnt_comb == 1 || cnt_comb == 2 || cnt_comb == 4 || cnt_comb == 5 || cnt_comb == 6;
309 chip->fsm_op1_sel = chip->fsm_op1_sel_l;
310 chip->fsm_op3_sel = cnt_comb == 16 || cnt_comb == 17 || cnt_comb == 18 || cnt_comb == 20 || cnt_comb == 21 || cnt_comb == 22;
311 chip->fsm_op2_sel = cnt_comb == 24 || cnt_comb == 25 || cnt_comb == 26 || cnt_comb == 28 || cnt_comb == 29 || cnt_comb == 30;
312 chip->fsm_sel2 = chip->fsm_sel2_l;
313 chip->fsm_sel23 = chip->fsm_sel23_l;
314 chip->fsm_ch3_sel = chip->fsm_ch3_sel_l;
315 chip->fsm_dac_load = chip->fsm_dac_load_l;
316 chip->fsm_dac_out_sel = chip->fsm_dac_out_sel_l;
317 chip->fsm_dac_ch6 = chip->fsm_dac_ch6_l;
318 chip->fsm_clock_timers = chip->fsm_sel2_l;
319 chip->fsm_clock_timers1 = chip->fsm_sel1_l;
320
321 chip->fsm_wco_l[1] = chip->fsm_wco_l[0];
322 chip->fsm_lro_l[1] = chip->fsm_lro_l[0];
323 }
324 else
325 {
326 chip->fsm_clock_eg = cnt_comb == 0;
327 chip->fsm_op4_sel = cnt_comb == 0 || cnt_comb == 1 || cnt_comb == 2 || cnt_comb == 4 || cnt_comb == 5 || cnt_comb == 6;
328 chip->fsm_op1_sel = cnt_comb == 8 || cnt_comb == 9 || cnt_comb == 10 || cnt_comb == 12 || cnt_comb == 13 || cnt_comb == 14;
329 chip->fsm_op3_sel = cnt_comb == 16 || cnt_comb == 17 || cnt_comb == 18 || cnt_comb == 20 || cnt_comb == 21 || cnt_comb == 22;
330 chip->fsm_op2_sel = cnt_comb == 24 || cnt_comb == 25 || cnt_comb == 26 || cnt_comb == 28 || cnt_comb == 29 || cnt_comb == 30;
331 chip->fsm_sel2 = cnt_comb == 2;
332 chip->fsm_sel23 = cnt_comb == 30;
333 chip->fsm_ch3_sel = cnt_comb == 2 || cnt_comb == 10 || cnt_comb == 18 || cnt_comb == 26;
334 chip->fsm_dac_load = cnt_comb == 0 || cnt_comb == 5 || cnt_comb == 10 || cnt_comb == 16 || cnt_comb == 21 || cnt_comb == 26;
335 chip->fsm_dac_out_sel = cnt_comb == 16 || cnt_comb == 17 || cnt_comb == 18 || cnt_comb == 20 || cnt_comb == 21 || cnt_comb == 22 ||
336 cnt_comb == 24 || cnt_comb == 25 || cnt_comb == 26 || cnt_comb == 28 || cnt_comb == 29 || cnt_comb == 30;
337 chip->fsm_dac_ch6 = cnt_comb == 5 || cnt_comb == 6 || cnt_comb == 8 || cnt_comb == 9;
338 chip->fsm_clock_timers = cnt_comb == 2;
339 chip->fsm_clock_timers1 = cnt_comb == 1;
340 }
341
342 for (i = 0; i < 3; i++)
343 connect |= ((chip->chan_connect[i][1] >> 5) & 1) << i;
344
345 chip->alg_mod_op1_0 = 0;
346 chip->alg_mod_op1_1 = 0;
347 chip->alg_mod_op2 = 0;
348 chip->alg_mod_prev_0 = 0;
349 chip->alg_mod_prev_1 = 0;
350 chip->alg_output = 0;
351
352 if (chip->fsm_op2_sel)
353 {
354 chip->alg_mod_op1_0 |= fm_algorithm[0][0][connect];
355 chip->alg_mod_op1_1 |= fm_algorithm[0][1][connect];
356 chip->alg_mod_op2 |= fm_algorithm[0][2][connect];
357 chip->alg_mod_prev_0 |= fm_algorithm[0][3][connect];
358 chip->alg_mod_prev_1 |= fm_algorithm[0][4][connect];
359 chip->alg_output |= fm_algorithm[2][5][connect];
360 }
361 if (chip->fsm_op4_sel)
362 {
363 chip->alg_mod_op1_0 |= fm_algorithm[1][0][connect];
364 chip->alg_mod_op1_1 |= fm_algorithm[1][1][connect];
365 chip->alg_mod_op2 |= fm_algorithm[1][2][connect];
366 chip->alg_mod_prev_0 |= fm_algorithm[1][3][connect];
367 chip->alg_mod_prev_1 |= fm_algorithm[1][4][connect];
368 chip->alg_output |= fm_algorithm[3][5][connect];
369 }
370 if (chip->fsm_op1_sel)
371 {
372 chip->alg_mod_op1_0 |= fm_algorithm[2][0][connect];
373 chip->alg_mod_op1_1 |= fm_algorithm[2][1][connect];
374 chip->alg_mod_op2 |= fm_algorithm[2][2][connect];
375 chip->alg_mod_prev_0 |= fm_algorithm[2][3][connect];
376 chip->alg_mod_prev_1 |= fm_algorithm[2][4][connect];
377 chip->alg_output |= fm_algorithm[0][5][connect];
378 }
379 if (chip->fsm_op3_sel)
380 {
381 chip->alg_mod_op1_0 |= fm_algorithm[3][0][connect];
382 chip->alg_mod_op1_1 |= fm_algorithm[3][1][connect];
383 chip->alg_mod_op2 |= fm_algorithm[3][2][connect];
384 chip->alg_mod_prev_0 |= fm_algorithm[3][3][connect];
385 chip->alg_mod_prev_1 |= fm_algorithm[3][4][connect];
386 chip->alg_output |= fm_algorithm[1][5][connect];
387 }
388 }
389
390 void FMOPN2_HandleIO1(fmopn2_t *chip)
391 {
392 int write_data_en = !chip->write_data_sr[1] && chip->write_data_dlatch;
393 int write_addr_en = !chip->write_addr_sr[1] && chip->write_addr_dlatch;
394 int busy_cnt = chip->busy_cnt[1] + chip->busy_latch[1];
395 int busy_of = (busy_cnt >> 5) & 1;
396 chip->write_addr_trig_sync = chip->write_addr_trig;
397 chip->write_data_trig_sync = chip->write_data_trig;
398 chip->write_addr_sr[0] = chip->write_addr_dlatch;
399 chip->write_data_sr[0] = chip->write_data_dlatch;
400
401 chip->busy_latch[0] = write_data_en || (chip->busy_latch[1] && !(chip->input.ic || busy_of));
402 if (chip->input.ic)
403 busy_cnt = 0;
404 chip->busy_cnt[0] = busy_cnt & 31;
405 chip->io_ic_latch[0] = chip->input.ic;
406 }
407
408 void FMOPN2_HandleIO2(fmopn2_t *chip)
409 {
410 chip->write_addr_dlatch = chip->write_addr_trig_sync;
411 if (chip->write_addr_dlatch)
412 chip->write_addr_trig = 0;
413 chip->write_data_dlatch = chip->write_data_trig_sync;
414 if (chip->write_data_dlatch)
415 chip->write_data_trig = 0;
416 chip->write_addr_sr[1] = chip->write_addr_sr[0] & 1;
417 chip->write_data_sr[1] = chip->write_data_sr[0] & 1;
418 chip->busy_cnt[1] = chip->busy_cnt[0] & 31;
419 chip->busy_latch[1] = chip->busy_latch[0] & 1;
420 chip->io_ic_latch[1] = chip->io_ic_latch[0] & 1;
421 }
422
423 void FMOPN2_DoShiftRegisters(fmopn2_t *chip, int sel)
424 {
425 int i, j;
426 int to = sel;
427 int from = sel ^ 1;
428 int rot = sel == 0 ? 1 : 0;
429 #define SLOT_ROTATE(x) rot ? ((x << 1) | ((x >> 11) & 1)) : x
430 #define CH_ROTATE(x) rot ? ((x << 1) | ((x >> 5) & 1)) : x
431 // slot registers
432 for (i = 0; i < 2; i++)
433 {
434 // multi
435 for (j = 0; j < 4; j++)
436 chip->slot_multi[i][j][to] = SLOT_ROTATE(chip->slot_multi[i][j][from]);
437 // dt
438 for (j = 0; j < 3; j++)
439 chip->slot_dt[i][j][to] = SLOT_ROTATE(chip->slot_dt[i][j][from]);
440 // tl
441 for (j = 0; j < 7; j++)
442 chip->slot_tl[i][j][to] = SLOT_ROTATE(chip->slot_tl[i][j][from]);
443 // ar
444 for (j = 0; j < 5; j++)
445 chip->slot_ar[i][j][to] = SLOT_ROTATE(chip->slot_ar[i][j][from]);
446 // ks
447 for (j = 0; j < 2; j++)
448 chip->slot_ks[i][j][to] = SLOT_ROTATE(chip->slot_ks[i][j][from]);
449 // dr
450 for (j = 0; j < 5; j++)
451 chip->slot_dr[i][j][to] = SLOT_ROTATE(chip->slot_dr[i][j][from]);
452 // am
453 for (j = 0; j < 1; j++)
454 chip->slot_am[i][j][to] = SLOT_ROTATE(chip->slot_am[i][j][from]);
455 // sr
456 for (j = 0; j < 5; j++)
457 chip->slot_sr[i][j][to] = SLOT_ROTATE(chip->slot_sr[i][j][from]);
458 // rr
459 for (j = 0; j < 4; j++)
460 chip->slot_rr[i][j][to] = SLOT_ROTATE(chip->slot_rr[i][j][from]);
461 // sl
462 for (j = 0; j < 4; j++)
463 chip->slot_sl[i][j][to] = SLOT_ROTATE(chip->slot_sl[i][j][from]);
464 // ssg eg
465 for (j = 0; j < 4; j++)
466 chip->slot_ssg_eg[i][j][to] = SLOT_ROTATE(chip->slot_ssg_eg[i][j][from]);
467 }
468 // channel registers
469
470 // fnum
471 for (j = 0; j < 11; j++)
472 chip->chan_fnum[j][to] = CH_ROTATE(chip->chan_fnum[j][from]);
473 // fnum ch3
474 for (j = 0; j < 11; j++)
475 chip->chan_fnum_ch3[j][to] = CH_ROTATE(chip->chan_fnum_ch3[j][from]);
476 // block
477 for (j = 0; j < 3; j++)
478 chip->chan_block[j][to] = CH_ROTATE(chip->chan_block[j][from]);
479 // block ch3
480 for (j = 0; j < 3; j++)
481 chip->chan_block_ch3[j][to] = CH_ROTATE(chip->chan_block_ch3[j][from]);
482 // connect
483 for (j = 0; j < 3; j++)
484 chip->chan_connect[j][to] = CH_ROTATE(chip->chan_connect[j][from]);
485 // fb
486 for (j = 0; j < 3; j++)
487 chip->chan_fb[j][to] = CH_ROTATE(chip->chan_fb[j][from]);
488 // pms
489 for (j = 0; j < 3; j++)
490 chip->chan_pms[j][to] = CH_ROTATE(chip->chan_pms[j][from]);
491 // ams
492 for (j = 0; j < 2; j++)
493 chip->chan_ams[j][to] = CH_ROTATE(chip->chan_ams[j][from]);
494 // pan
495 for (j = 0; j < 2; j++)
496 chip->chan_pan[j][to] = CH_ROTATE(chip->chan_pan[j][from]);
497 #undef SLOT_ROTATE
498 #undef CH_ROTATE
499 }
500
501 void FMOPN2_FMRegisters1(fmopn2_t *chip)
502 {
503 int i, j;
504 int write_data_en = !chip->write_data_sr[1] && chip->write_data_dlatch;
505 int write_addr_en = !chip->write_addr_sr[1] && chip->write_addr_dlatch;
506 int bus = FMOPN2_GetBus(chip);
507 int address = bus | (chip->bank_latch << 8);
508 int fm_write = (bus & 0xf0) != 0;
509
510
511 if (write_addr_en)
512 chip->write_fm_address[0] = fm_write;
513 else
514 chip->write_fm_address[0] = chip->write_fm_address[1];
515
516 if (chip->input.ic)
517 chip->fm_address[0] = 0;
518 else if (fm_write && write_addr_en)
519 chip->fm_address[0] = address;
520 else
521 chip->fm_address[0] = chip->fm_address[1];
522
523 chip->write_fm_data[0] = (chip->write_fm_address[1] && write_data_en) || (chip->write_fm_data[1] && !write_addr_en);
524
525 if (chip->input.ic)
526 chip->fm_data[0] = 0;
527 else if (chip->write_fm_address[1] && write_data_en)
528 chip->fm_data[0] = bus;
529 else
530 chip->fm_data[0] = chip->fm_data[1];
531
532 if (write_addr_en)
533 {
534 chip->write_mode_21[0] = address == 0x21;
535 chip->write_mode_22[0] = address == 0x22;
536 chip->write_mode_24[0] = address == 0x24;
537 chip->write_mode_25[0] = address == 0x25;
538 chip->write_mode_26[0] = address == 0x26;
539 chip->write_mode_27[0] = address == 0x27;
540 chip->write_mode_28[0] = address == 0x28;
541 chip->write_mode_2a[0] = address == 0x2a;
542 chip->write_mode_2b[0] = address == 0x2b;
543 chip->write_mode_2c[0] = address == 0x2c;
544 }
545 else
546 {
547 chip->write_mode_21[0] = chip->write_mode_21[1];
548 chip->write_mode_22[0] = chip->write_mode_22[1];
549 chip->write_mode_24[0] = chip->write_mode_24[1];
550 chip->write_mode_25[0] = chip->write_mode_25[1];
551 chip->write_mode_26[0] = chip->write_mode_26[1];
552 chip->write_mode_27[0] = chip->write_mode_27[1];
553 chip->write_mode_28[0] = chip->write_mode_28[1];
554 chip->write_mode_2a[0] = chip->write_mode_2a[1];
555 chip->write_mode_2b[0] = chip->write_mode_2b[1];
556 chip->write_mode_2c[0] = chip->write_mode_2c[1];
557 }
558
559 if (chip->input.ic)
560 {
561 chip->mode_test_21[0] = 0;
562 chip->mode_lfo_en[0] = 0;
563 chip->mode_lfo_freq[0] = 0;
564 chip->mode_timer_a_reg[0] = 0;
565 chip->mode_timer_b_reg[0] = 0;
566 chip->mode_ch3[0] = 0;
567 chip->mode_timer_a_load[0] = 0;
568 chip->mode_timer_a_enable[0] = 0;
569 chip->mode_timer_a_reset[0] = 0;
570 chip->mode_timer_b_load[0] = 0;
571 chip->mode_timer_b_enable[0] = 0;
572 chip->mode_timer_b_reset[0] = 0;
573 chip->mode_kon_operator[0] = 0;
574 chip->mode_kon_channel[0] = 0;
575 chip->mode_dac_data[0] = 0;
576 chip->mode_dac_en[0] = 0;
577 chip->mode_test_2c[0] = 0;
578 // slot registers
579 for (i = 0; i < 2; i++)
580 {
581 // multi
582 for (j = 0; j < 4; j++)
583 chip->slot_multi[i][j][0] &= ~1;
584 // dt
585 for (j = 0; j < 3; j++)
586 chip->slot_dt[i][j][0] &= ~1;
587 // tl
588 for (j = 0; j < 7; j++)
589 chip->slot_tl[i][j][0] &= ~1;
590 // ar
591 for (j = 0; j < 5; j++)
592 chip->slot_ar[i][j][0] &= ~1;
593 // ks
594 for (j = 0; j < 2; j++)
595 chip->slot_ks[i][j][0] &= ~1;
596 // dr
597 for (j = 0; j < 5; j++)
598 chip->slot_dr[i][j][0] &= ~1;
599 // am
600 for (j = 0; j < 1; j++)
601 chip->slot_am[i][j][0] &= ~1;
602 // sr
603 for (j = 0; j < 5; j++)
604 chip->slot_sr[i][j][0] &= ~1;
605 // rr
606 for (j = 0; j < 4; j++)
607 chip->slot_rr[i][j][0] &= ~1;
608 // sl
609 for (j = 0; j < 4; j++)
610 chip->slot_sl[i][j][0] &= ~1;
611 // ssg eg
612 for (j = 0; j < 4; j++)
613 chip->slot_ssg_eg[i][j][0] &= ~1;
614 }
615 // channel registers
616
617 // fn low
618 for (j = 0; j < 11; j++)
619 chip->chan_fnum[j][0] &= ~1;
620 // fn low ch3
621 for (j = 0; j < 11; j++)
622 chip->chan_fnum_ch3[j][0] &= ~1;
623 // block fn high
624 for (j = 0; j < 3; j++)
625 chip->chan_block[j][0] &= ~1;
626 // block fn high ch3
627 for (j = 0; j < 3; j++)
628 chip->chan_block_ch3[j][0] &= ~1;
629 // connect
630 for (j = 0; j < 3; j++)
631 chip->chan_connect[j][0] &= ~1;
632 // fb
633 for (j = 0; j < 3; j++)
634 chip->chan_fb[j][0] &= ~1;
635 // pms
636 for (j = 0; j < 3; j++)
637 chip->chan_pms[j][0] &= ~1;
638 // ams
639 for (j = 0; j < 2; j++)
640 chip->chan_ams[j][0] &= ~1;
641 // pan
642 for (j = 0; j < 2; j++)
643 chip->chan_pan[j][0] &= ~1;
644
645 chip->chan_a4[0] = 0;
646 chip->chan_ac[0] = 0;
647 }
648 else
649 {
650 if (write_data_en && chip->write_mode_21[1] && !chip->bank_latch)
651 chip->mode_test_21[0] = bus & 255;
652 else
653 chip->mode_test_21[0] = chip->mode_test_21[1];
654 if (write_data_en && chip->write_mode_22[1] && !chip->bank_latch)
655 {
656 chip->mode_lfo_en[0] = (bus >> 3) & 1;
657 chip->mode_lfo_freq[0] = bus & 7;
658 }
659 else
660 {
661 chip->mode_lfo_en[0] = chip->mode_lfo_en[1];
662 chip->mode_lfo_freq[0] = chip->mode_lfo_freq[1];
663 }
664 chip->mode_timer_a_reg[0] = 0;
665 if (write_data_en && chip->write_mode_24[1] && !chip->bank_latch)
666 chip->mode_timer_a_reg[0] |= (bus & 255) << 2;
667 else
668 chip->mode_timer_a_reg[0] |= chip->mode_timer_a_reg[1] & 0x3fc;
669 if (write_data_en && chip->write_mode_25[1] && !chip->bank_latch)
670 chip->mode_timer_a_reg[0] |= bus & 3;
671 else
672 chip->mode_timer_a_reg[0] |= chip->mode_timer_a_reg[1] & 0x3;
673 if (write_data_en && chip->write_mode_26[1] && !chip->bank_latch)
674 chip->mode_timer_b_reg[0] = bus & 255;
675 else
676 chip->mode_timer_b_reg[0] = chip->mode_timer_b_reg[1];
677 if (write_data_en && chip->write_mode_27[1] && !chip->bank_latch)
678 {
679 chip->mode_ch3[0] = (bus >> 6) & 3;
680 chip->mode_timer_a_load[0] = bus & 1;
681 chip->mode_timer_a_enable[0] = (bus >> 2) & 1;
682 chip->mode_timer_a_reset[0] = (bus >> 4) & 1;
683 chip->mode_timer_b_load[0] = (bus >> 1) & 1;
684 chip->mode_timer_b_enable[0] = (bus >> 3) & 1;
685 chip->mode_timer_b_reset[0] = (bus >> 5) & 1;
686 }
687 else
688 {
689 chip->mode_ch3[0] = chip->mode_ch3[1];
690 chip->mode_timer_a_load[0] = chip->mode_timer_a_load[1];
691 chip->mode_timer_a_enable[0] = chip->mode_timer_a_enable[1];
692 chip->mode_timer_a_reset[0] = 0;
693 chip->mode_timer_b_load[0] = chip->mode_timer_b_load[1];
694 chip->mode_timer_b_enable[0] = chip->mode_timer_b_enable[1];
695 chip->mode_timer_b_reset[0] = 0;
696 }
697 if (write_data_en && chip->write_mode_28[1] && !chip->bank_latch)
698 {
699 chip->mode_kon_operator[0] = (bus >> 4) & 15;
700 chip->mode_kon_channel[0] = bus & 15;
701 }
702 else
703 {
704 chip->mode_kon_operator[0] = chip->mode_kon_operator[1];
705 chip->mode_kon_channel[0] = chip->mode_kon_channel[1];
706 }
707 if (write_data_en && chip->write_mode_2a[1] && !chip->bank_latch)
708 chip->mode_dac_data[0] = (bus & 255) ^ 128;
709 else
710 chip->mode_dac_data[0] = chip->mode_dac_data[1];
711 if (write_data_en && chip->write_mode_2b[1] && !chip->bank_latch)
712 chip->mode_dac_en[0] = (bus >> 7) & 1;
713 else
714 chip->mode_dac_en[0] = chip->mode_dac_en[1];
715 if (write_data_en && chip->write_mode_2c[1] && !chip->bank_latch)
716 chip->mode_test_2c[0] = bus & 0xf8;
717 else
718 chip->mode_test_2c[0] = chip->mode_test_2c[1];
719 }
720 if (chip->write_fm_data[1] && (chip->fm_address[1]&3) == chip->reg_cnt1[1]
721 && ((chip->fm_address[1]>>2)&1) == ((chip->reg_cnt2[1]>>1)&1) && ((chip->fm_address[1]>>8)&1) == (chip->reg_cnt2[1]&1))
722 {
723 int bank = (chip->fm_address[1]>>3)&1;
724 switch (chip->fm_address[1] & 0xf0)
725 {
726 case 0x30:
727 // multi
728 for (j = 0; j < 4; j++)
729 {
730 chip->slot_multi[bank][j][0] &= ~1;
731 chip->slot_multi[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
732 }
733 // dt
734 for (j = 0; j < 3; j++)
735 {
736 chip->slot_dt[bank][j][0] &= ~1;
737 chip->slot_dt[bank][j][0] |= (chip->fm_data[1] >> (j + 4)) & 1;
738 }
739 break;
740 case 0x40:
741 // tl
742 for (j = 0; j < 7; j++)
743 {
744 chip->slot_tl[bank][j][0] &= ~1;
745 chip->slot_tl[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
746 }
747 break;
748 case 0x50:
749 // ar
750 for (j = 0; j < 5; j++)
751 {
752 chip->slot_ar[bank][j][0] &= ~1;
753 chip->slot_ar[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
754 }
755 // ks
756 for (j = 0; j < 2; j++)
757 {
758 chip->slot_ks[bank][j][0] &= ~1;
759 chip->slot_ks[bank][j][0] |= (chip->fm_data[1] >> (j + 6)) & 1;
760 }
761 break;
762 case 0x60:
763 // dr
764 for (j = 0; j < 5; j++)
765 {
766 chip->slot_dr[bank][j][0] &= ~1;
767 chip->slot_dr[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
768 }
769 // am
770 for (j = 0; j < 1; j++)
771 {
772 chip->slot_am[bank][j][0] &= ~1;
773 chip->slot_am[bank][j][0] |= (chip->fm_data[1] >> (j + 7)) & 1;
774 }
775 break;
776 case 0x70:
777 // sr
778 for (j = 0; j < 5; j++)
779 {
780 chip->slot_sr[bank][j][0] &= ~1;
781 chip->slot_sr[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
782 }
783 break;
784 case 0x80:
785 // rr
786 for (j = 0; j < 4; j++)
787 {
788 chip->slot_rr[bank][j][0] &= ~1;
789 chip->slot_rr[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
790 }
791 // sl
792 for (j = 0; j < 4; j++)
793 {
794 chip->slot_sl[bank][j][0] &= ~1;
795 chip->slot_sl[bank][j][0] |= (chip->fm_data[1] >> (j + 4)) & 1;
796 }
797 break;
798 case 0x90:
799 // ssg eg
800 for (j = 0; j < 4; j++)
801 {
802 chip->slot_ssg_eg[bank][j][0] &= ~1;
803 chip->slot_ssg_eg[bank][j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
804 }
805 break;
806 }
807 }
808 if (chip->write_fm_data[1] && (chip->fm_address[1] & 3) == chip->reg_cnt1[1] && ((chip->fm_address[1] >> 8) & 1) == (chip->reg_cnt2[1] & 1))
809 {
810 switch (chip->fm_address[1] & 0xfc)
811 {
812 case 0xa0:
813 // fnum
814 for (j = 0; j < 8; j++)
815 {
816 chip->chan_fnum[j][0] &= ~1;
817 chip->chan_fnum[j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
818 }
819 for (j = 0; j < 3; j++)
820 {
821 chip->chan_fnum[8+j][0] &= ~1;
822 chip->chan_fnum[8+j][0] |= (chip->chan_a4[1] >> (j + 0)) & 1;
823 }
824 // block
825 for (j = 0; j < 3; j++)
826 {
827 chip->chan_block[j][0] &= ~1;
828 chip->chan_block[j][0] |= (chip->chan_a4[1] >> (j + 3)) & 1;
829 }
830 break;
831 case 0xa4:
832 chip->chan_a4[0] = chip->fm_data[1] & 0x3f;
833 break;
834 case 0xa8:
835 // fnum
836 for (j = 0; j < 8; j++)
837 {
838 chip->chan_fnum_ch3[j][0] &= ~1;
839 chip->chan_fnum_ch3[j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
840 }
841 for (j = 0; j < 3; j++)
842 {
843 chip->chan_fnum_ch3[8+j][0] &= ~1;
844 chip->chan_fnum_ch3[8+j][0] |= (chip->chan_ac[1] >> (j + 0)) & 1;
845 }
846 // block
847 for (j = 0; j < 3; j++)
848 {
849 chip->chan_block_ch3[j][0] &= ~1;
850 chip->chan_block_ch3[j][0] |= (chip->chan_ac[1] >> (j + 3)) & 1;
851 }
852 break;
853 case 0xac:
854 chip->chan_ac[0] = chip->fm_data[1] & 0x3f;
855 break;
856 case 0xb0:
857 // connect
858 for (j = 0; j < 3; j++)
859 {
860 chip->chan_connect[j][0] &= ~1;
861 chip->chan_connect[j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
862 }
863 // fb
864 for (j = 0; j < 3; j++)
865 {
866 chip->chan_fb[j][0] &= ~1;
867 chip->chan_fb[j][0] |= (chip->fm_data[1] >> (j + 3)) & 1;
868 }
869 break;
870 case 0xb4:
871 // pms
872 for (j = 0; j < 3; j++)
873 {
874 chip->chan_pms[j][0] &= ~1;
875 chip->chan_pms[j][0] |= (chip->fm_data[1] >> (j + 0)) & 1;
876 }
877 // ams
878 for (j = 0; j < 2; j++)
879 {
880 chip->chan_ams[j][0] &= ~1;
881 chip->chan_ams[j][0] |= (chip->fm_data[1] >> (j + 4)) & 1;
882 }
883 // pan
884 for (j = 0; j < 2; j++)
885 {
886 chip->chan_pan[j][0] &= ~1;
887 chip->chan_pan[j][0] |= !((chip->fm_data[1] >> (j + 6)) & 1);
888 }
889 break;
890 }
891 }
892 // keyon
893 chip->mode_kon[0][0] = chip->mode_kon[0][1] << 1;
894 chip->mode_kon[1][0] = chip->mode_kon[1][1] << 1;
895 chip->mode_kon[2][0] = chip->mode_kon[2][1] << 1;
896 chip->mode_kon[3][0] = chip->mode_kon[3][1] << 1;
897 if (chip->reg_cnt2[1] == ((chip->mode_kon_channel[1] >> 2) & 1) && chip->reg_cnt1[1] == (chip->mode_kon_channel[1] & 3))
898 {
899 chip->mode_kon[0][0] |= (chip->mode_kon_operator[1] >> 0) & 1;
900 chip->mode_kon[1][0] |= (chip->mode_kon_operator[1] >> 3) & 1;
901 chip->mode_kon[2][0] |= (chip->mode_kon_operator[1] >> 1) & 1;
902 chip->mode_kon[3][0] |= (chip->mode_kon_operator[1] >> 2) & 1;
903 }
904 else
905 {
906 if (!chip->input.ic)
907 chip->mode_kon[0][0] |= (chip->mode_kon[3][1] >> 5) & 1;
908 chip->mode_kon[1][0] |= (chip->mode_kon[0][1] >> 5) & 1;
909 chip->mode_kon[2][0] |= (chip->mode_kon[1][1] >> 5) & 1;
910 chip->mode_kon[3][0] |= (chip->mode_kon[2][1] >> 5) & 1;
911 }
912 }
913
914 void FMOPN2_FMRegisters2(fmopn2_t *chip)
915 {
916 chip->write_fm_address[1] = chip->write_fm_address[0];
917 chip->write_fm_data[1] = chip->write_fm_data[0];
918 chip->fm_address[1] = chip->fm_address[0];
919 chip->fm_data[1] = chip->fm_data[0];
920 chip->write_mode_21[1] = chip->write_mode_21[0];
921 chip->write_mode_22[1] = chip->write_mode_22[0];
922 chip->write_mode_24[1] = chip->write_mode_24[0];
923 chip->write_mode_25[1] = chip->write_mode_25[0];
924 chip->write_mode_26[1] = chip->write_mode_26[0];
925 chip->write_mode_27[1] = chip->write_mode_27[0];
926 chip->write_mode_28[1] = chip->write_mode_28[0];
927 chip->write_mode_2a[1] = chip->write_mode_2a[0];
928 chip->write_mode_2b[1] = chip->write_mode_2b[0];
929 chip->write_mode_2c[1] = chip->write_mode_2c[0];
930 chip->mode_test_21[1] = chip->mode_test_21[0];
931 chip->mode_lfo_en[1] = chip->mode_lfo_en[0];
932 chip->mode_lfo_freq[1] = chip->mode_lfo_freq[0];
933 chip->mode_timer_a_reg[1] = chip->mode_timer_a_reg[0];
934 chip->mode_timer_b_reg[1] = chip->mode_timer_b_reg[0];
935 chip->mode_ch3[1] = chip->mode_ch3[0];
936 chip->mode_timer_a_load[1] = chip->mode_timer_a_load[0];
937 chip->mode_timer_a_enable[1] = chip->mode_timer_a_enable[0];
938 chip->mode_timer_a_reset[1] = chip->mode_timer_a_reset[0];
939 chip->mode_timer_b_load[1] = chip->mode_timer_b_load[0];
940 chip->mode_timer_b_enable[1] = chip->mode_timer_b_enable[0];
941 chip->mode_timer_b_reset[1] = chip->mode_timer_b_reset[0];
942 chip->mode_kon_operator[1] = chip->mode_kon_operator[0];
943 chip->mode_kon_channel[1] = chip->mode_kon_channel[0];
944 chip->mode_dac_data[1] = chip->mode_dac_data[0];
945 chip->mode_dac_en[1] = chip->mode_dac_en[0];
946 chip->mode_test_2c[1] = chip->mode_test_2c[0];
947 chip->chan_a4[1] = chip->chan_a4[0];
948 chip->chan_ac[1] = chip->chan_ac[0];
949 chip->mode_kon[0][1] = chip->mode_kon[0][0];
950 chip->mode_kon[1][1] = chip->mode_kon[1][0];
951 chip->mode_kon[2][1] = chip->mode_kon[2][0];
952 chip->mode_kon[3][1] = chip->mode_kon[3][0];
953 }
954
955 void FMOPN2_Misc1(fmopn2_t *chip)
956 {
957 chip->reg_cnt1[0] = chip->reg_cnt1[1] + 1;
958 chip->reg_cnt2[0] = chip->reg_cnt2[1];
959 if (chip->reg_cnt1[1] & 2)
960 {
961 chip->reg_cnt1[0] = 0;
962 chip->reg_cnt2[0]++;
963 }
964 if (chip->fsm_sel23 || chip->input.ic)
965 {
966 chip->reg_cnt1[0] = 0;
967 chip->reg_cnt2[0] = 0;
968 }
969 }
970
971 void FMOPN2_Misc2(fmopn2_t *chip)
972 {
973 chip->reg_cnt1[1] = chip->reg_cnt1[0] & 3;
974 chip->reg_cnt2[1] = chip->reg_cnt2[0] & 7;
975 }
976
977 void FMOPN2_LFO1(fmopn2_t *chip)
978 {
979 static const int lfo_cycles[8] = {
980 108, 77, 71, 67, 62, 44, 8, 5
981 };
982 int inc = (chip->mode_test_21[1] & 2) != 0 || chip->fsm_sel23;
983 int freq = chip->mode_lfo_freq[1];
984 int of = (chip->lfo_cnt1[1] & lfo_cycles[freq]) == lfo_cycles[freq];
985
986 chip->lfo_cnt1[0] = chip->lfo_cnt1[1] + inc;
987
988 if (chip->input.ic || of)
989 chip->lfo_cnt1[0] = 0;
990
991 chip->lfo_cnt2[0] = chip->lfo_cnt2[1] + of;
992
993 if (!chip->mode_lfo_en[1])
994 chip->lfo_cnt2[0] = 0;
995
996 chip->lfo_inc_latch[0] = chip->fsm_sel23;
997
998 chip->lfo_dlatch_load = chip->lfo_inc_latch[1];
999 }
1000
1001 void FMOPN2_LFO2(fmopn2_t *chip)
1002 {
1003 chip->lfo_cnt1[1] = chip->lfo_cnt1[0] & 127;
1004 chip->lfo_cnt2[1] = chip->lfo_cnt2[0] & 127;
1005 chip->lfo_inc_latch[1] = chip->lfo_inc_latch[0];
1006 if (chip->lfo_inc_latch[1] && !chip->lfo_dlatch_load)
1007 chip->lfo_dlatch = chip->lfo_cnt2[1];
1008 }
1009
1010 void FMOPN2_PhaseGenerator1(fmopn2_t *chip)
1011 {
1012 // Note table
1013 static const int fn_note[16] = {
1014 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3
1015 };
1016 // LFO shift
1017 static const int pg_lfo_sh1[8][8] = {
1018 { 7, 7, 7, 7, 7, 7, 7, 7 },
1019 { 7, 7, 7, 7, 7, 7, 7, 7 },
1020 { 7, 7, 7, 7, 7, 7, 1, 1 },
1021 { 7, 7, 7, 7, 1, 1, 1, 1 },
1022 { 7, 7, 7, 1, 1, 1, 1, 0 },
1023 { 7, 7, 1, 1, 0, 0, 0, 0 },
1024 { 7, 7, 1, 1, 0, 0, 0, 0 },
1025 { 7, 7, 1, 1, 0, 0, 0, 0 }
1026 };
1027
1028 static const int pg_lfo_sh2[8][8] = {
1029 { 7, 7, 7, 7, 7, 7, 7, 7 },
1030 { 7, 7, 7, 7, 2, 2, 2, 2 },
1031 { 7, 7, 7, 2, 2, 2, 7, 7 },
1032 { 7, 7, 2, 2, 7, 7, 2, 2 },
1033 { 7, 7, 2, 7, 7, 7, 2, 7 },
1034 { 7, 7, 7, 2, 7, 7, 2, 1 },
1035 { 7, 7, 7, 2, 7, 7, 2, 1 },
1036 { 7, 7, 7, 2, 7, 7, 2, 1 }
1037 };
1038 #if 0
1039 // YM2610 decap
1040 static const int pg_lfo_sh2[8][8] = {
1041 { 7, 7, 7, 7, 7, 7, 7, 7 },
1042 { 7, 7, 7, 7, 2, 2, 2, 2 },
1043 { 7, 7, 7, 2, 2, 2, 7, 7 },
1044 { 7, 7, 2, 2, 7, 7, 2, 2 },
1045 { 7, 2, 2, 7, 7, 2, 2, 7 },
1046 { 7, 2, 7, 2, 7, 2, 2, 1 },
1047 { 7, 2, 7, 2, 7, 2, 2, 1 },
1048 { 7, 2, 7, 2, 7, 2, 2, 1 }
1049 };
1050 #endif
1051 static const int pg_detune_add[4] = {
1052 0, 8, 10, 11
1053 };
1054 static const int pg_detune[8] = { 16, 17, 19, 20, 22, 24, 27, 29 };
1055 int i;
1056 int fnum = 0;
1057 int fnum_h;
1058 int block = 0;
1059 int pms = 0;
1060 int dt = 0;
1061 int dt_l = 0;
1062 int dt_sum;
1063 int dt_sum_l;
1064 int dt_sum_h;
1065 int kcode;
1066 int multi = 0;
1067 int lfo;
1068 int ch3_sel = chip->reg_cnt1[1] == 1 && (chip->reg_cnt2[1] & 1) == 0 && chip->mode_ch3[1] != 0;
1069 int op_sel = chip->reg_cnt2[1] >> 1;
1070 int bank = (chip->reg_cnt2[1] >> 2) & 1;
1071 int carry = 0;
1072 int pg_inc;
1073 int reset;
1074 if (ch3_sel && op_sel == 0)
1075 {
1076 for (i = 0; i < 11; i++)
1077 fnum |= ((chip->chan_fnum_ch3[i][1] >> 5) & 1) << i;
1078 for (i = 0; i < 3; i++)
1079 block |= ((chip->chan_block_ch3[i][1] >> 5) & 1) << i;
1080 }
1081 else if (ch3_sel && op_sel == 1)
1082 {
1083 for (i = 0; i < 11; i++)
1084 fnum |= ((chip->chan_fnum_ch3[i][1] >> 0) & 1) << i;
1085 for (i = 0; i < 3; i++)
1086 block |= ((chip->chan_block_ch3[i][1] >> 0) & 1) << i;
1087 }
1088 else if (ch3_sel && op_sel == 2)
1089 {
1090 for (i = 0; i < 11; i++)
1091 fnum |= ((chip->chan_fnum_ch3[i][1] >> 4) & 1) << i;
1092 for (i = 0; i < 3; i++)
1093 block |= ((chip->chan_block_ch3[i][1] >> 4) & 1) << i;
1094 }
1095 else
1096 {
1097 for (i = 0; i < 11; i++)
1098 fnum |= ((chip->chan_fnum[i][1] >> 4) & 1) << i;
1099 for (i = 0; i < 3; i++)
1100 block |= ((chip->chan_block[i][1] >> 4) & 1) << i;
1101 }
1102 for (i = 0; i < 3; i++)
1103 pms |= ((chip->chan_pms[i][1] >> 5) & 1) << i;
1104 for (i = 0; i < 3; i++)
1105 dt |= ((chip->slot_dt[bank][i][1] >> 11) & 1) << i;
1106 for (i = 0; i < 4; i++)
1107 multi |= ((chip->slot_multi[bank][i][1] >> 11) & 1) << i;
1108
1109 kcode = (block << 2) | fn_note[fnum >> 7];
1110
1111 chip->pg_kcode[0][0] = kcode;
1112 chip->pg_kcode[1][0] = chip->pg_kcode[0][1];
1113
1114 chip->pg_fnum[0][0] = fnum;
1115 chip->pg_fnum[1][0] = chip->pg_fnum[0][1];
1116
1117 lfo = (chip->lfo_dlatch >> 2) & 7;
1118
1119 if (chip->lfo_dlatch & 32)
1120 lfo ^= 7;
1121
1122 fnum_h = chip->pg_fnum[0][1] >> 4;
1123
1124 chip->pg_fnum_lfo1 = fnum_h >> pg_lfo_sh1[pms][lfo];
1125 chip->pg_fnum_lfo2 = fnum_h >> pg_lfo_sh2[pms][lfo];
1126
1127 chip->pg_lfo_shift = 2;
1128 if (pms > 5)
1129 chip->pg_lfo_shift = 7 - pms;
1130
1131 chip->pg_lfo_sign = (chip->lfo_dlatch >> 6) & 1;
1132
1133 chip->pg_freq1 = ((chip->pg_fnum[1][1] << 1) + chip->pg_lfo) & 0xfff;
1134 block = chip->pg_kcode[1][1] >> 2;
1135 chip->pg_block = block;
1136 chip->pg_dt[0] = dt;
1137
1138 dt_l = chip->pg_dt[1] & 3;
1139 kcode = chip->pg_kcode[1][1];
1140 if (kcode > 28)
1141 kcode = 28;
1142 dt_sum = kcode + ((pg_detune_add[dt_l] + 1) << 2);
1143 dt_sum_l = dt_sum & 7;
1144 dt_sum_h = dt_sum >> 3;
1145 chip->pg_detune[0] = dt_l ? pg_detune[dt_sum_l] >> (9 - dt_sum_h) : 0;
1146 if (chip->pg_dt[1] & 0x04)
1147 chip->pg_detune[0] = -chip->pg_detune[0];
1148
1149 chip->pg_freq3 = (chip->pg_freq2 + chip->pg_detune[1]) & 0x1ffff;
1150
1151 chip->pg_multi[0][0] = multi;
1152 chip->pg_multi[1][0] = chip->pg_multi[0][1];
1153 chip->pg_multi2 = chip->pg_multi[1][1];
1154 chip->pg_inc[0] = chip->pg_freq4;
1155
1156 chip->pg_inc_mask[0] = chip->pg_inc[1];
1157 if (chip->pg_reset[1] & 2)
1158 chip->pg_inc_mask[0] = 0;
1159
1160 chip->pg_reset_latch[0] = (chip->pg_reset[1] & 2) != 0;
1161
1162 pg_inc = chip->pg_inc_mask[1];
1163
1164 reset = chip->pg_reset_latch[1] || (chip->mode_test_21[1] & 8) != 0;
1165
1166 for (i = 0; i < 20; i++)
1167 {
1168 if (!reset)
1169 carry += (chip->pg_phase[i][1] >> 23) & 1;
1170 carry += pg_inc & 1;
1171 chip->pg_phase[i][0] = (chip->pg_phase[i][1] << 1) | (carry & 1);
1172 pg_inc >>= 1;
1173 carry >>= 1;
1174 }
1175
1176 chip->pg_debug[0] = chip->pg_debug[1] >> 1;
1177 if (chip->fsm_sel2)
1178 {
1179 for (i = 0; i < 10; i++)
1180 {
1181 chip->pg_debug[0] |= ((chip->pg_phase[i][1] >> 23) & 1) << i;
1182 }
1183 }
1184 }
1185
1186 void FMOPN2_PhaseGenerator2(fmopn2_t *chip)
1187 {
1188 int i;
1189 int block = chip->pg_kcode[1][0];
1190 chip->pg_fnum[0][1] = chip->pg_fnum[0][0];
1191 chip->pg_fnum[1][1] = chip->pg_fnum[1][0];
1192 chip->pg_kcode[0][1] = chip->pg_kcode[0][0];
1193 chip->pg_kcode[1][1] = chip->pg_kcode[1][0];
1194 chip->pg_lfo = (chip->pg_fnum_lfo1 + chip->pg_fnum_lfo2) >> chip->pg_lfo_shift;
1195 if (chip->pg_lfo_sign)
1196 chip->pg_lfo = -chip->pg_lfo;
1197 chip->pg_freq2 = (chip->pg_freq1 << chip->pg_block) >> 2;
1198 chip->pg_dt[1] = chip->pg_dt[0];
1199 chip->pg_detune[1] = chip->pg_detune[0];
1200 chip->pg_multi[0][1] = chip->pg_multi[0][0];
1201 chip->pg_multi[1][1] = chip->pg_multi[1][0];
1202 if (!chip->pg_multi2)
1203 chip->pg_freq4 = chip->pg_freq3 >> 1;
1204 else
1205 chip->pg_freq4 = chip->pg_freq3 * chip->pg_multi2;
1206 chip->pg_inc[1] = chip->pg_inc[0];
1207 chip->pg_inc_mask[1] = chip->pg_inc_mask[0];
1208 chip->pg_reset_latch[1] = chip->pg_reset_latch[0];
1209 for (i = 0; i < 20; i++)
1210 {
1211 chip->pg_phase[i][1] = chip->pg_phase[i][0];
1212 }
1213
1214 chip->pg_debug[1] = chip->pg_debug[0];
1215 }
1216
1217 void FMOPN2_EnvelopeGenerator1(fmopn2_t *chip)
1218 {
1219 int i;
1220 int sum;
1221 int add;
1222 int timer_bit;
1223 int timer_bit_masked;
1224 int bank = (chip->reg_cnt2[1] >> 2) & 1;
1225 int rate_sel;
1226 int rate = 0;
1227 int ks = 0;
1228 int b0, b1;
1229 int inc1;
1230 int exp = 0;
1231 int linear = 0;
1232 int inc_total = 0;
1233 int level = 0;
1234 int level2 = 0;
1235 int kon;
1236 int kon2;
1237 int okon;
1238 int okon2;
1239 int state;
1240 int pg_reset;
1241 int kon_event;
1242 int ssg_eg = 0;
1243 int ssg_inv_e = 0;
1244 int ssg_dir = 0;
1245 int ssg_inv = 0;
1246 int ssg_holdup = 0;
1247 int ssg_enable;
1248 int ssg_pgreset = 0;
1249 int ssg_pgrepeat = 0;
1250 int eg_off;
1251 int eg_slreach;
1252 int sl = 0;
1253 int nextlevel = 0;
1254 int nextstate = eg_state_attack;
1255 int tl = 0;
1256 int istantattack = 0;
1257 int eg_output;
1258 int ams = 0;
1259 int csm_kon;
1260 static const int eg_stephi[4][4] = {
1261 { 0, 0, 0, 0 },
1262 { 1, 0, 0, 0 },
1263 { 1, 0, 1, 0 },
1264 { 1, 1, 1, 0 }
1265 };
1266 chip->eg_prescaler_clock_l[0] = chip->fsm_clock_eg;
1267 chip->eg_prescaler[0] = chip->eg_prescaler[1] + chip->fsm_clock_eg;
1268 if (((chip->eg_prescaler[1] & 2) != 0 && chip->fsm_clock_eg) || chip->input.ic)
1269 chip->eg_prescaler[0] = 0;
1270 chip->eg_step[0] = chip->eg_prescaler[1] >> 1;
1271
1272 chip->eg_clock_delay[0] = (chip->eg_clock_delay[1] << 1) | chip->fsm_clock_eg;
1273
1274 chip->eg_timer_load = chip->eg_step[1] && chip->eg_prescaler_clock_l[1];
1275
1276 sum = (chip->eg_timer[1] >> 11) & 1;
1277 add = chip->eg_timer_carry[1];
1278 if ((chip->eg_prescaler[1] & 2) != 0 && chip->eg_prescaler_clock_l[1])
1279 add = 1;
1280 sum += add;
1281 chip->eg_timer_carry[0] = sum >> 1;
1282 sum &= 1;
1283 if (chip->input.ic || (chip->mode_test_21[1] & 32) != 0)
1284 sum = 0;
1285
1286 chip->eg_timer[0] = (chip->eg_timer[1] << 1) | sum;
1287
1288 timer_bit = sum;
1289 if (chip->mode_test_2c[1] & 64)
1290 {
1291 if (chip->mode_test_2c[1] & 128) // Assuming TEST pin is NC
1292 timer_bit |= FMOPN2_ReadTest(chip);
1293 else
1294 timer_bit |= chip->input.test & 1;
1295 }
1296
1297 chip->eg_timer_mask[0] = timer_bit | chip->eg_timer_mask[1];
1298 if (chip->fsm_clock_eg || ((chip->eg_clock_delay[1]>>11) & 1) != 0 || chip->input.ic)
1299 chip->eg_timer_mask[0] = 0;
1300
1301 timer_bit_masked = timer_bit;
1302 if (chip->eg_timer_mask[1])
1303 timer_bit_masked = 0;
1304
1305 chip->eg_timer_masked[0] = (chip->eg_timer_masked[1] << 1) | timer_bit_masked;
1306
1307 for (i = 0; i < 10; i++)
1308 {
1309 level2 |= ((chip->eg_level[i][1] >> 20) & 1) << i;
1310 }
1311
1312 chip->eg_level_latch[0] = level2;
1313
1314 csm_kon = chip->fsm_ch3_sel && chip->timer_csm_key_dlatch;
1315 kon2 = ((chip->mode_kon[3][1] >> 5) & 1) | csm_kon;
1316 chip->eg_kon_latch[0] = (chip->eg_kon_latch[1] << 1) | kon2;
1317 chip->eg_kon_csm[0] = (chip->eg_kon_csm[1] << 1) | csm_kon;
1318
1319 kon = (chip->eg_kon_latch[1] >> 1) & 1;
1320 okon = (chip->eg_key[1] >> 23) & 1;
1321 pg_reset = (kon && !okon) || (chip->eg_ssg_pgreset[1] & 2) != 0;
1322 kon_event = (kon && !okon) || (okon && (chip->eg_ssg_pgrepeat[1] & 2) != 0);
1323 chip->pg_reset[0] = (chip->pg_reset[1] << 1) | pg_reset;
1324 if (chip->eg_ssg_pgreset[1] & 2)
1325 chip->pg_reset[0] |= 1;
1326 chip->eg_key[0] = (chip->eg_key[1] << 1) | kon;
1327
1328 okon2 = (chip->eg_key[1] >> 21) & 1;
1329
1330 for (i = 0; i < 4; i++)
1331 ssg_eg |= ((chip->slot_ssg_eg[bank][i][1] >> 11) & 1) << i;
1332 ssg_enable = (ssg_eg & 8) != 0;
1333 ssg_inv_e = ssg_enable && (ssg_eg & 4) != 0;
1334 if (ssg_enable)
1335 {
1336 if (okon2)
1337 {
1338 ssg_dir = (chip->eg_ssg_dir[1] >> 23) & 1;
1339 if (level2 & 512)
1340 {
1341 if ((ssg_eg & 3) == 2)
1342 ssg_dir ^= 1;
1343 if ((ssg_eg & 3) == 3)
1344 ssg_dir = 1;
1345 }
1346 }
1347 if (kon2)
1348 ssg_holdup = (ssg_eg & 7) == 3 || (ssg_eg & 7) == 5;
1349 if (level2 & 512)
1350 {
1351 if ((ssg_eg & 3) == 0)
1352 ssg_pgreset = 1;
1353 if ((ssg_eg & 1) == 0)
1354 ssg_pgrepeat = 1;
1355 }
1356 }
1357 ssg_inv = okon2 & (((chip->eg_ssg_dir[1] >> 23) & 1) ^ ssg_inv_e);
1358 chip->eg_ssg_dir[0] = (chip->eg_ssg_dir[1] << 1) | ssg_dir;
1359 chip->eg_ssg_inv[0] = ssg_inv;
1360 chip->eg_ssg_holdup[0] = (chip->eg_ssg_holdup[1] << 1) | ssg_holdup;
1361 chip->eg_ssg_enable[0] = (chip->eg_ssg_enable[1] << 1) | ssg_enable;
1362 chip->eg_ssg_pgreset[0] = (chip->eg_ssg_pgreset[1] << 1) | ssg_pgreset;
1363 chip->eg_ssg_pgrepeat[0] = (chip->eg_ssg_pgrepeat[1] << 1) | ssg_pgrepeat;
1364
1365 chip->eg_level_ssg[0] = eg_output = chip->eg_ssg_inv[1] ? chip->eg_level_latch_inv : chip->eg_level_latch[1];
1366 if (chip->mode_test_21[1] & 32)
1367 eg_output = 0;
1368
1369 for (i = 0; i < 4; i++)
1370 sl |= ((chip->slot_sl[bank][i][1] >> 11) & 1) << i;
1371
1372 if (sl == 15)
1373 sl |= 16;
1374
1375 chip->eg_sl[0][0] = sl;
1376 chip->eg_sl[1][0] = chip->eg_sl[0][1];
1377
1378 for (i = 0; i < 7; i++)
1379 tl |= ((chip->slot_tl[bank][i][1] >> 11) & 1) << i;
1380
1381 chip->eg_tl[0][0] = tl;
1382 chip->eg_tl[1][0] = chip->eg_tl[0][1];
1383
1384 b0 = (chip->eg_state[0][1] >> 21) & 1;
1385 b1 = (chip->eg_state[1][1] >> 21) & 1;
1386
1387 rate_sel = b1 * 2 + b0;
1388
1389 if (okon2)
1390 {
1391 if (ssg_pgrepeat)
1392 rate_sel = eg_state_attack;
1393 }
1394 else
1395 {
1396 if (kon2)
1397 rate_sel = eg_state_attack;
1398 }
1399
1400 switch (rate_sel)
1401 {
1402 case eg_state_attack:
1403 for (i = 0; i < 5; i++)
1404 rate |= ((chip->slot_ar[bank][i][1] >> 11) & 1) << i;
1405 break;
1406 case eg_state_decay:
1407 for (i = 0; i < 5; i++)
1408 rate |= ((chip->slot_dr[bank][i][1] >> 11) & 1) << i;
1409 break;
1410 case eg_state_sustain:
1411 for (i = 0; i < 5; i++)
1412 rate |= ((chip->slot_sr[bank][i][1] >> 11) & 1) << i;
1413 break;
1414 case eg_state_release:
1415 rate = 1;
1416 for (i = 0; i < 4; i++)
1417 rate |= ((chip->slot_rr[bank][i][1] >> 11) & 1) << (i + 1);
1418 break;
1419 }
1420
1421 chip->eg_rate_nonzero[0] = rate != 0;
1422 chip->eg_rate = rate;
1423
1424 for (i = 0; i < 2; i++)
1425 ks |= ((chip->slot_ks[bank][i][1] >> 11) & 1) << i;
1426
1427 chip->eg_ksv = chip->pg_kcode[0][1] >> (ks ^ 3);
1428
1429 rate = chip->eg_rate2;
1430 if (rate & 64)
1431 rate = 63;
1432
1433 sum = (rate >> 2) + chip->eg_shift_lock;
1434
1435 inc1 = 0;
1436 if (rate < 48 && chip->eg_rate_nonzero[1])
1437 {
1438 switch (sum & 15)
1439 {
1440 case 12:
1441 inc1 = rate != 0;
1442 break;
1443 case 13:
1444 inc1 = (rate >> 1) & 1;
1445 break;
1446 case 14:
1447 inc1 = rate & 1;
1448 break;
1449 }
1450 }
1451 chip->eg_inc1 = inc1;
1452 chip->eg_inc2 = eg_stephi[rate & 3][chip->eg_timer_low_lock];
1453 chip->eg_rate12 = (rate & 60) == 48;
1454 chip->eg_rate13 = (rate & 60) == 52;
1455 chip->eg_rate14 = (rate & 60) == 56;
1456 chip->eg_rate15 = (rate & 60) == 60;
1457 chip->eg_maxrate[0] = (rate & 62) == 62;
1458 chip->eg_prescaler_l = (chip->eg_prescaler[1] & 2) != 0;
1459
1460 chip->eg_incsh_nonzero[0] = chip->eg_incsh0 | chip->eg_incsh1 | chip->eg_incsh2 | chip->eg_incsh3;
1461
1462
1463 if (okon && !kon)
1464 {
1465 level = chip->eg_level_ssg[1];
1466 }
1467 else
1468 {
1469 for (i = 0; i < 10; i++)
1470 {
1471 level |= ((chip->eg_level[i][1] >> 22) & 1) << i;
1472 }
1473 }
1474
1475 b0 = (chip->eg_state[0][1] >> 23) & 1;
1476 b1 = (chip->eg_state[1][1] >> 23) & 1;
1477
1478 state = b1 * 2 + b0;
1479
1480 exp = kon && (state == eg_state_attack) && !chip->eg_maxrate[1] && level != 0;
1481
1482 eg_off = (chip->eg_ssg_enable[1] & 2) != 0 ? (level & 512) != 0 : (level & 0x3f0) == 0x3f0;
1483 eg_slreach = (level >> 4) == (chip->eg_sl[1][1] << 1);
1484
1485 linear = !kon_event && !eg_off && (state == eg_state_sustain || state == eg_state_release);
1486 linear |= !kon_event && !eg_off && !eg_slreach && state == eg_state_decay;
1487
1488 if (exp)
1489 {
1490 if (chip->eg_incsh0)
1491 inc_total |= ~level >> 4;
1492 if (chip->eg_incsh1)
1493 inc_total |= ~level >> 3;
1494 if (chip->eg_incsh2)
1495 inc_total |= ~level >> 2;
1496 if (chip->eg_incsh3)
1497 inc_total |= ~level >> 1;
1498 }
1499 if (linear)
1500 {
1501 if (chip->eg_ssg_enable[1] & 2)
1502 {
1503 if (chip->eg_incsh0)
1504 inc_total |= 4;
1505 if (chip->eg_incsh1)
1506 inc_total |= 8;
1507 if (chip->eg_incsh2)
1508 inc_total |= 16;
1509 if (chip->eg_incsh3)
1510 inc_total |= 32;
1511 }
1512 else
1513 {
1514 if (chip->eg_incsh0)
1515 inc_total |= 1;
1516 if (chip->eg_incsh1)
1517 inc_total |= 2;
1518 if (chip->eg_incsh2)
1519 inc_total |= 4;
1520 if (chip->eg_incsh3)
1521 inc_total |= 8;
1522 }
1523 }
1524
1525 chip->eg_inc_total = inc_total;
1526
1527 istantattack = chip->eg_maxrate[1] && (!chip->eg_maxrate[1] || kon_event);
1528
1529 if (!istantattack)
1530 nextlevel |= level;
1531
1532 if (chip->eg_kon_csm[1] & 2)
1533 nextlevel |= chip->eg_tl[1][1] << 3;
1534
1535 if ((!kon_event && eg_off && (chip->eg_ssg_holdup[1] & 2) == 0 && state != eg_state_attack) || chip->input.ic)
1536 {
1537 nextlevel = 0x3ff;
1538 nextstate |= eg_state_release;
1539 }
1540
1541 if (!kon_event && state == eg_state_sustain)
1542 {
1543 nextstate |= eg_state_sustain;
1544 }
1545
1546 if (!kon_event && state == eg_state_decay && !eg_slreach)
1547 {
1548 nextstate |= eg_state_decay;
1549 }
1550 if (!kon_event && state == eg_state_decay && eg_slreach)
1551 {
1552 nextstate |= eg_state_sustain;
1553 }
1554
1555 if (!kon && !kon_event)
1556 {
1557 nextstate |= eg_state_release;
1558 }
1559 if (!kon_event && state == eg_state_release)
1560 {
1561 nextstate |= eg_state_release;
1562 }
1563
1564 if (!kon_event && state == eg_state_attack && level == 0)
1565 {
1566 nextstate |= eg_state_decay;
1567 }
1568 if (chip->input.ic)
1569 {
1570 nextstate |= eg_state_release;
1571 }
1572
1573
1574 chip->eg_nextlevel[0] = nextlevel;
1575 b0 = nextstate & 1;
1576 b1 = (nextstate >> 1) & 1;
1577 chip->eg_state[0][0] = (chip->eg_state[0][1] << 1) | b0;
1578 chip->eg_state[1][0] = (chip->eg_state[1][1] << 1) | b1;
1579
1580 nextlevel = chip->eg_nextlevel[1];
1581
1582 for (i = 0; i < 10; i++)
1583 {
1584 chip->eg_level[i][0] = (chip->eg_level[i][1] << 1) | (nextlevel & 1);
1585 nextlevel >>= 1;
1586 }
1587
1588
1589 if (chip->slot_am[bank][0][1] & (1<<11))
1590 for (i = 0; i < 2; i++)
1591 ams |= ((chip->chan_ams[i][1] >> 5) & 1) << i;
1592
1593 chip->eg_ams = ams;
1594
1595 if (chip->lfo_dlatch & 64)
1596 chip->eg_lfo[0] = chip->lfo_dlatch & 63;
1597 else
1598 chip->eg_lfo[0] = chip->lfo_dlatch ^ 63;
1599
1600 chip->eg_ch3_latch[0] = chip->fsm_ch3_sel;
1601
1602 chip->eg_out_tl = chip->eg_tl[0][1];
1603 if (chip->eg_ch3_latch[1] && chip->mode_ch3[1] == 2) // CSM
1604 chip->eg_out_tl = 0;
1605
1606 chip->eg_out = eg_output + chip->eg_lfo[1];
1607
1608 chip->eg_debug[0] = chip->eg_debug[1] << 1;
1609 if (chip->fsm_sel2)
1610 chip->eg_debug[0] |= chip->eg_out_total;
1611 }
1612
1613 void FMOPN2_EnvelopeGenerator2(fmopn2_t *chip)
1614 {
1615 int i;
1616 int b0, b1, b2, b3;
1617 static const int eg_am_shift[4] = {
1618 7, 3, 1, 0
1619 };
1620 chip->eg_prescaler_clock_l[1] = chip->eg_prescaler_clock_l[0];
1621 chip->eg_prescaler[1] = chip->eg_prescaler[0] & 3;
1622 chip->eg_step[1] = chip->eg_step[0];
1623
1624 chip->eg_timer[1] = chip->eg_timer[0];
1625 chip->eg_clock_delay[1] = chip->eg_clock_delay[0];
1626 chip->eg_timer_carry[1] = chip->eg_timer_carry[0];
1627 chip->eg_timer_mask[1] = chip->eg_timer_mask[0];
1628 chip->eg_timer_masked[1] = chip->eg_timer_masked[0];
1629
1630 if (!chip->eg_timer_load && chip->eg_step[1] && chip->eg_prescaler_clock_l[1])
1631 {
1632 b0 = (chip->eg_timer[1] >> 11) & 1;
1633 b1 = (chip->eg_timer[1] >> 10) & 1;
1634 chip->eg_timer_low_lock = b1 * 2 + b0;
1635
1636 b0 = (chip->eg_timer_masked[1] & 0xaaa) != 0;
1637 b1 = (chip->eg_timer_masked[1] & 0x666) != 0;
1638 b2 = (chip->eg_timer_masked[1] & 0x1e1) != 0;
1639 b3 = (chip->eg_timer_masked[1] & 0x1f) != 0;
1640 chip->eg_shift_lock = b3 * 8 + b2 * 4 + b1 * 2 + b0;
1641 }
1642
1643 chip->eg_rate_nonzero[1] = chip->eg_rate_nonzero[0];
1644
1645 chip->eg_rate2 = (chip->eg_rate << 1) + chip->eg_ksv;
1646
1647 chip->eg_maxrate[1] = chip->eg_maxrate[0];
1648
1649 chip->eg_incsh0 = 0;
1650 chip->eg_incsh1 = 0;
1651 chip->eg_incsh2 = 0;
1652 chip->eg_incsh3 = 0;
1653
1654 if (chip->eg_prescaler_l)
1655 {
1656 chip->eg_incsh0 = chip->eg_inc1;
1657 chip->eg_incsh3 = chip->eg_rate15;
1658 if (!chip->eg_inc2)
1659 {
1660 chip->eg_incsh0 |= chip->eg_rate12;
1661 chip->eg_incsh1 = chip->eg_rate13;
1662 chip->eg_incsh2 = chip->eg_rate14;
1663 }
1664 else
1665 {
1666 chip->eg_incsh1 = chip->eg_rate12;
1667 chip->eg_incsh2 = chip->eg_rate13;
1668 chip->eg_incsh3 |= chip->eg_rate14;
1669 }
1670 }
1671
1672 chip->eg_incsh_nonzero[1] = chip->eg_incsh_nonzero[0];
1673 chip->eg_kon_latch[1] = chip->eg_kon_latch[0];
1674
1675 chip->eg_level_ssg[1] = chip->eg_level_ssg[0];
1676
1677 chip->pg_reset[1] = chip->pg_reset[0];
1678
1679 chip->eg_ssg_dir[1] = chip->eg_ssg_dir[0];
1680 chip->eg_ssg_inv[1] = chip->eg_ssg_inv[0];
1681 chip->eg_ssg_holdup[1] = chip->eg_ssg_holdup[0];
1682 chip->eg_ssg_enable[1] = chip->eg_ssg_enable[0];
1683 chip->eg_ssg_pgreset[1] = chip->eg_ssg_pgreset[0];
1684 chip->eg_ssg_pgrepeat[1] = chip->eg_ssg_pgrepeat[0];
1685
1686 chip->eg_level_latch[1] = chip->eg_level_latch[0];
1687 chip->eg_level_latch_inv = (512 - chip->eg_level_latch[0]) & 0x3ff;
1688
1689 chip->eg_sl[0][1] = chip->eg_sl[0][0];
1690 chip->eg_sl[1][1] = chip->eg_sl[1][0];
1691 chip->eg_tl[0][1] = chip->eg_tl[0][0];
1692 chip->eg_tl[1][1] = chip->eg_tl[1][0];
1693
1694 chip->eg_nextlevel[1] = chip->eg_nextlevel[0] + chip->eg_inc_total;
1695
1696 chip->eg_kon_csm[1] = chip->eg_kon_csm[0];
1697
1698 for (i = 0; i < 10; i++)
1699 {
1700 chip->eg_level[i][1] = chip->eg_level[i][0];
1701 }
1702
1703 chip->eg_state[0][1] = chip->eg_state[0][0];
1704 chip->eg_state[1][1] = chip->eg_state[1][0];
1705
1706 chip->eg_lfo[1] = (chip->eg_lfo[0] << 1) >> eg_am_shift[chip->eg_ams];
1707
1708 chip->eg_ch3_latch[1] = chip->eg_ch3_latch[0];
1709
1710 chip->eg_out_total = (chip->eg_out & 1023) + (chip->eg_out_tl << 3);
1711 if ((chip->eg_out & 1024) != 0 || (chip->eg_out_total & 1024) != 0)
1712 chip->eg_out_total = 1023;
1713
1714 chip->eg_debug[1] = chip->eg_debug[0];
1715 chip->eg_key[1] = chip->eg_key[0];
1716 }
1717
1718 void FMOPN2_Operator1(fmopn2_t *chip)
1719 {
1720 int i;
1721 int carry = 0;
1722 int phase = 0;
1723 int quarter;
1724 int index;
1725 int atten = 0;
1726 int output;
1727 int mod1 = 0, mod2 = 0;
1728 int mod;
1729 int fb = 0;
1730 static const int logsin[128] = {
1731 0x6c3, 0x58b, 0x4e4, 0x471, 0x41a, 0x3d3, 0x398, 0x365, 0x339, 0x311, 0x2ed, 0x2cd, 0x2af, 0x293, 0x279, 0x261,
1732 0x24b, 0x236, 0x222, 0x20f, 0x1fd, 0x1ec, 0x1dc, 0x1cd, 0x1be, 0x1b0, 0x1a2, 0x195, 0x188, 0x17c, 0x171, 0x166,
1733 0x15b, 0x150, 0x146, 0x13c, 0x133, 0x129, 0x121, 0x118, 0x10f, 0x107, 0x0ff, 0x0f8, 0x0f0, 0x0e9, 0x0e2, 0x0db,
1734 0x0d4, 0x0cd, 0x0c7, 0x0c1, 0x0bb, 0x0b5, 0x0af, 0x0a9, 0x0a4, 0x09f, 0x099, 0x094, 0x08f, 0x08a, 0x086, 0x081,
1735 0x07d, 0x078, 0x074, 0x070, 0x06c, 0x068, 0x064, 0x060, 0x05c, 0x059, 0x055, 0x052, 0x04e, 0x04b, 0x048, 0x045,
1736 0x042, 0x03f, 0x03c, 0x039, 0x037, 0x034, 0x031, 0x02f, 0x02d, 0x02a, 0x028, 0x026, 0x024, 0x022, 0x020, 0x01e,
1737 0x01c, 0x01a, 0x018, 0x017, 0x015, 0x014, 0x012, 0x011, 0x00f, 0x00e, 0x00d, 0x00c, 0x00a, 0x009, 0x008, 0x007,
1738 0x007, 0x006, 0x005, 0x004, 0x004, 0x003, 0x002, 0x002, 0x001, 0x001, 0x001, 0x001, 0x000, 0x000, 0x000, 0x000
1739 };
1740 static const int logsin_d[128] = {
1741 0x196, 0x07c, 0x04a, 0x035, 0x029, 0x022, 0x01d, 0x019, 0x015, 0x013, 0x012, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c,
1742 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x009, 0x008, 0x007, 0x007, 0x007, 0x007, 0x006, 0x007, 0x006, 0x006, 0x005,
1743 0x005, 0x005, 0x005, 0x005, 0x004, 0x005, 0x004, 0x004, 0x005, 0x004, 0x004, 0x003, 0x004, 0x003, 0x003, 0x003,
1744 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003, 0x003, 0x003, 0x003, 0x002, 0x002,
1745 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x001, 0x002, 0x002, 0x002, 0x001,
1746 0x001, 0x001, 0x002, 0x002, 0x001, 0x001, 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
1747 0x001, 0x001, 0x001, 0x000, 0x001, 0x000, 0x001, 0x000, 0x001, 0x001, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001,
1748 0x000, 0x000, 0x000, 0x001, 0x000, 0x000, 0x001, 0x000, 0x001, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
1749 };
1750 static const int pow[128] = {
1751 0x3f5, 0x3ea, 0x3df, 0x3d4, 0x3c9, 0x3bf, 0x3b4, 0x3a9, 0x39f, 0x394, 0x38a, 0x37f, 0x375, 0x36a, 0x360, 0x356,
1752 0x34c, 0x342, 0x338, 0x32e, 0x324, 0x31a, 0x310, 0x306, 0x2fd, 0x2f3, 0x2e9, 0x2e0, 0x2d6, 0x2cd, 0x2c4, 0x2ba,
1753 0x2b1, 0x2a8, 0x29e, 0x295, 0x28c, 0x283, 0x27a, 0x271, 0x268, 0x25f, 0x257, 0x24e, 0x245, 0x23c, 0x234, 0x22b,
1754 0x223, 0x21a, 0x212, 0x209, 0x201, 0x1f9, 0x1f0, 0x1e8, 0x1e0, 0x1d8, 0x1d0, 0x1c8, 0x1c0, 0x1b8, 0x1b0, 0x1a8,
1755 0x1a0, 0x199, 0x191, 0x189, 0x181, 0x17a, 0x172, 0x16b, 0x163, 0x15c, 0x154, 0x14d, 0x146, 0x13e, 0x137, 0x130,
1756 0x129, 0x122, 0x11b, 0x114, 0x10c, 0x106, 0x0ff, 0x0f8, 0x0f1, 0x0ea, 0x0e3, 0x0dc, 0x0d6, 0x0cf, 0x0c8, 0x0c2,
1757 0x0bb, 0x0b5, 0x0ae, 0x0a8, 0x0a1, 0x09b, 0x094, 0x08e, 0x088, 0x082, 0x07b, 0x075, 0x06f, 0x069, 0x063, 0x05d,
1758 0x057, 0x051, 0x04b, 0x045, 0x03f, 0x039, 0x033, 0x02d, 0x028, 0x022, 0x01c, 0x016, 0x011, 0x00b, 0x006, 0x000,
1759 };
1760 static const int pow_d[128] = {
1761 0x005, 0x005, 0x005, 0x006, 0x006, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x006, 0x005, 0x005,
1762 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x004, 0x005,
1763 0x004, 0x004, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x005, 0x004, 0x004, 0x004, 0x005, 0x004, 0x005,
1764 0x004, 0x004, 0x004, 0x005, 0x004, 0x004, 0x005, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
1765 0x004, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x003, 0x004, 0x004, 0x004,
1766 0x003, 0x003, 0x003, 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x003, 0x003, 0x004, 0x003,
1767 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003,
1768 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x002, 0x003,
1769 };
1770 for (i = 0; i < 10; i++)
1771 {
1772 carry += (chip->op_mod[i][1] >> 5) & 1;
1773 carry += (chip->pg_phase[10 + i][1] >> 19) & 1;
1774 phase += (carry & 1) << i;
1775 carry >>= 1;
1776 }
1777 chip->op_phase[0] = phase;
1778
1779 chip->op_sign[0] = (chip->op_sign[1] << 1) | ((chip->op_phase[1] >> 9) & 1);
1780
1781 quarter = chip->op_phase[1] & 255;
1782 if (chip->op_phase[1] & 256)
1783 quarter ^= 255;
1784
1785 chip->op_logsin_add_delta[0] = (quarter & 1) == 0;
1786
1787 chip->op_logsin_base[0] = logsin[quarter >> 1];
1788 chip->op_logsin_delta[0] = logsin_d[quarter >> 1];
1789
1790 chip->op_env[0] = chip->eg_out_total;
1791
1792 atten = chip->op_logsin_base[1];
1793 if (chip->op_logsin_add_delta[1])
1794 atten += chip->op_logsin_delta[1];
1795
1796 atten += chip->op_env[1] << 2;
1797
1798 chip->op_atten[0] = atten;
1799
1800 atten = chip->op_atten[1];
1801 if (atten & 4096)
1802 atten = 4095;
1803
1804 index = atten & 255;
1805 chip->op_shift[0] = atten >> 8;
1806
1807 chip->op_pow_add_delta[0] = (index & 1) == 0;
1808
1809 chip->op_pow_base[0] = pow[index >> 1];
1810 chip->op_pow_delta[0] = pow_d[index >> 1];
1811
1812 output = chip->op_pow_base[1];
1813 if (chip->op_pow_add_delta[1])
1814 output += chip->op_pow_delta[1];
1815
1816 output |= 0x400;
1817
1818 output = (output << 2) >> chip->op_shift[1];
1819
1820 if (chip->mode_test_21[1] & 16)
1821 output ^= 1 << 13;
1822
1823 if (chip->op_sign[1] & 4)
1824 {
1825 output ^= 0x3fff;
1826 output++;
1827 }
1828
1829 chip->op_output[0] = output;
1830
1831 for (i = 0; i < 14; i++)
1832 {
1833 chip->op_op1[0][i][0] = chip->op_op1[0][i][1] << 1;
1834 chip->op_op1[1][i][0] = chip->op_op1[1][i][1] << 1;
1835 chip->op_op2[i][0] = chip->op_op2[i][1] << 1;
1836 if (chip->fsm_op1_sel)
1837 {
1838 chip->op_op1[0][i][0] |= (chip->op_output[1] >> i) & 1;
1839 chip->op_op1[1][i][0] |= (chip->op_op1[0][i][1] >> 5) & 1;
1840 }
1841 else
1842 {
1843 chip->op_op1[0][i][0] |= (chip->op_op1[0][i][1] >> 5) & 1;
1844 chip->op_op1[1][i][0] |= (chip->op_op1[1][i][1] >> 5) & 1;
1845 }
1846 if (chip->fsm_op2_sel)
1847 chip->op_op2[i][0] |= (chip->op_output[1] >> i) & 1;
1848 else
1849 chip->op_op2[i][0] |= (chip->op_op2[i][1] >> 5) & 1;
1850 }
1851
1852 if (chip->alg_mod_op1_0)
1853 {
1854 for (i = 0; i < 14; i++)
1855 mod2 |= ((chip->op_op1[0][i][1] >> 5) & 1) << i;
1856 }
1857 if (chip->alg_mod_op1_1)
1858 {
1859 for (i = 0; i < 14; i++)
1860 mod1 |= ((chip->op_op1[1][i][1] >> 5) & 1) << i;
1861 }
1862 if (chip->alg_mod_op2)
1863 {
1864 for (i = 0; i < 14; i++)
1865 mod1 |= ((chip->op_op2[i][1] >> 5) & 1) << i;
1866 }
1867 if (chip->alg_mod_prev_0)
1868 {
1869 mod2 |= chip->op_output[1] & 0x3fff;
1870 }
1871 if (chip->alg_mod_prev_1)
1872 {
1873 mod1 |= chip->op_output[1] & 0x3fff;
1874 }
1875 if (mod1 & (1 << 13))
1876 mod1 |= 1 << 14;
1877 if (mod2 & (1 << 13))
1878 mod2 |= 1 << 14;
1879 mod = (mod1 + mod2) >> 1;
1880 mod &= 0x3fff;
1881
1882 chip->op_mod_sum[0] = mod;
1883 chip->op_dofeedback[0] = chip->fsm_op2_sel;
1884
1885 if (chip->op_dofeedback[1])
1886 {
1887 for (i = 0; i < 3; i++)
1888 fb |= (chip->chan_fb[i][1] & 1) << i;
1889 if (!fb)
1890 mod = 0;
1891 else
1892 {
1893 mod = chip->op_mod_sum[1];
1894 if (mod & (1 << 13))
1895 mod |= ~0x3fff;
1896
1897 mod = mod >> (9 - fb);
1898 }
1899 }
1900 else
1901 mod = chip->op_mod_sum[1];
1902
1903 for (i = 0; i < 10; i++)
1904 {
1905 chip->op_mod[i][0] = (chip->op_mod[i][1] << 1) | (mod & 1);
1906 mod >>= 1;
1907 }
1908 }
1909
1910 void FMOPN2_Operator2(fmopn2_t *chip)
1911 {
1912 int i;
1913 for (i = 0; i < 10; i++)
1914 {
1915 chip->op_mod[i][1] = chip->op_mod[i][0];
1916 }
1917 for (i = 0; i < 14; i++)
1918 {
1919 chip->op_op1[0][i][1] = chip->op_op1[0][i][0];
1920 chip->op_op1[1][i][1] = chip->op_op1[1][i][0];
1921 chip->op_op2[i][1] = chip->op_op2[i][0];
1922 }
1923 chip->op_phase[1] = chip->op_phase[0];
1924 chip->op_sign[1] = chip->op_sign[0];
1925 chip->op_logsin_add_delta[1] = chip->op_logsin_add_delta[0];
1926 chip->op_logsin_base[1] = chip->op_logsin_base[0];
1927 chip->op_logsin_delta[1] = chip->op_logsin_delta[0];
1928 chip->op_env[1] = chip->op_env[0];
1929 chip->op_atten[1] = chip->op_atten[0];
1930 chip->op_pow_add_delta[1] = chip->op_pow_add_delta[0];
1931 chip->op_pow_base[1] = chip->op_pow_base[0];
1932 chip->op_pow_delta[1] = chip->op_pow_delta[0];
1933 chip->op_shift[1] = chip->op_shift[0];
1934 chip->op_output[1] = chip->op_output[0];
1935 chip->op_mod_sum[1] = chip->op_mod_sum[0];
1936 chip->op_dofeedback[1] = chip->op_dofeedback[0];
1937 }
1938
1939 void FMOPN2_YM3438Accumulator1(fmopn2_t *chip)
1940 {
1941 int i;
1942 int sum;
1943 int inp = 0;
1944 int acc = 0;
1945 int test_dac = (chip->mode_test_2c[1] & 32) != 0;
1946 int load = test_dac || chip->fsm_op1_sel;
1947 int acc_clear = load && !test_dac;
1948 sum = test_dac;
1949 if (chip->alg_output && !test_dac)
1950 inp = (chip->op_output[1] >> 5) & 511;
1951 if (!acc_clear)
1952 for (i = 0; i < 9; i++)
1953 acc += ((chip->ch_accm[i][1] >> 5) & 1) << i;
1954
1955 sum = test_dac + inp + acc;
1956
1957 sum &= 511;
1958
1959 if ((inp & 256) != 0 && (acc & 256) != 0 && (sum & 256) == 0)
1960 sum = 256;
1961 else if ((inp & 256) == 0 && (acc & 256) == 0 && (sum & 256) != 0)
1962 sum = 255;
1963
1964 for (i = 0; i < 9; i++)
1965 chip->ch_accm[i][0] = (chip->ch_accm[i][1] << 1) | ((sum >> i) & 1);
1966
1967 for (i = 0; i < 9; i++)
1968 {
1969 chip->ch_out[i][0] = chip->ch_out[i][1] << 1;
1970 if (load)
1971 chip->ch_out[i][0] |= (chip->ch_accm[i][1] >> 5) & 1;
1972 else
1973 chip->ch_out[i][0] |= (chip->ch_out[i][1] >> 5) & 1;
1974 }
1975
1976 chip->ch_dac_load = chip->fsm_dac_load;
1977
1978 chip->ch_out_debug[0] = chip->ch_out_dlatch;
1979 }
1980
1981 void FMOPN2_YM3438Accumulator2(fmopn2_t* chip)
1982 {
1983 int i;
1984 int test_dac = (chip->mode_test_2c[1] & 32) != 0;
1985 int do_out = 0;
1986 int sign;
1987 int out;
1988 for (i = 0; i < 9; i++)
1989 {
1990 chip->ch_accm[i][1] = chip->ch_accm[i][0];
1991 chip->ch_out[i][1] = chip->ch_out[i][0];
1992 }
1993 if ((chip->fsm_dac_load && !chip->ch_dac_load) || test_dac)
1994 {
1995 chip->ch_out_dlatch = 0;
1996 if (chip->fsm_dac_out_sel || test_dac)
1997 {
1998 for (i = 0; i < 9; i++)
1999 chip->ch_out_dlatch |= ((chip->ch_out[i][1] >> 5) & 1) << i;
2000 }
2001 else
2002 {
2003 for (i = 0; i < 9; i++)
2004 chip->ch_out_dlatch |= ((chip->ch_out[i][1] >> 4) & 1) << i;
2005 }
2006 }
2007 if ((chip->fsm_dac_ch6 && chip->mode_dac_en[1]) || test_dac)
2008 {
2009 chip->dac_val = chip->mode_dac_data[1] << 1;
2010 chip->dac_val |= (chip->mode_test_2c[1] & 8) != 0;
2011 }
2012 else
2013 chip->dac_val = chip->ch_out_dlatch;
2014
2015 if (chip->fsm_dac_load && !chip->ch_dac_load)
2016 {
2017 chip->ch_out_pan_dlatch = 0;
2018 if (chip->fsm_dac_out_sel)
2019 {
2020 for (i = 0; i < 2; i++)
2021 chip->ch_out_pan_dlatch |= (((chip->chan_pan[i][1] >> 5) & 1) ^ 1) << i;
2022 }
2023 else
2024 {
2025 for (i = 0; i < 2; i++)
2026 chip->ch_out_pan_dlatch |= (((chip->chan_pan[i][1] >> 4) & 1) ^ 1) << i;
2027 }
2028 }
2029
2030 do_out = test_dac || !chip->fsm_dac_load;
2031 if (do_out && (chip->ch_out_pan_dlatch & 2) != 0)
2032 chip->out_l = chip->dac_val;
2033 else
2034 chip->out_l = 0;
2035 if (do_out && (chip->ch_out_pan_dlatch & 1) != 0)
2036 chip->out_r = chip->dac_val;
2037 else
2038 chip->out_r = 0;
2039
2040 if (chip->out_l & 256)
2041 chip->out_l |= ~0x1ff;
2042 if (chip->out_r & 256)
2043 chip->out_r |= ~0x1ff;
2044
2045 chip->ch_out_debug[1] = chip->ch_out_debug[0];
2046 }
2047
2048 void FMOPN2_YMF276Accumulator1(fmopn2_t *chip)
2049 {
2050 int i;
2051 int sum1;
2052 int sum2;
2053 int sum;
2054 int inp = 0;
2055 int accm = 0;
2056 int acc = 0;
2057 int c;
2058 int test_dac = (chip->mode_test_2c[1] & 32) != 0;
2059 int test_dac2 = (chip->mode_test_2c[1] & 8) != 0;
2060 int load = test_dac || chip->fsm_op1_sel;
2061 int acc_clear = load && !test_dac;
2062 int sel_dac = (chip->fsm_op1_sel_l2[1] & 16) != 0 && chip->fsm_op1_sel && chip->mode_dac_en[1];
2063 int sel_fm = chip->fsm_op1_sel && !sel_dac;
2064 int out = 0;
2065 int pan = 0;
2066 int acc_l = 0;
2067 int acc_r = 0;
2068
2069 chip->osc_out = 0;
2070
2071 for (i = 0; i < 14; i++)
2072 accm += ((chip->ch_accm[i][1] >> 5) & 1) << i;
2073 if (chip->alg_output && !test_dac)
2074 inp = chip->op_output[1] & 0x3fff;
2075 if (test_dac2)
2076 inp = 0x3fff;
2077 if (!acc_clear)
2078 acc = accm;
2079
2080 sum1 = (acc & 31) + (inp & 31) + (test_dac && !test_dac2);
2081 c = ((sum1 & 32) != 0 || test_dac) && !test_dac2;
2082 sum2 = (acc >> 5) + (inp >> 5) + c;
2083
2084 sum = ((sum2 & 511) << 5) | (sum1 & 31);
2085
2086 if ((inp & 0x2000) != 0 && (acc & 0x2000) != 0 && (sum & 0x2000) == 0)
2087 sum = 0x2000;
2088 else if ((inp & 0x2000) == 0 && (acc & 0x2000) == 0 && (sum & 0x2000) != 0)
2089 sum = 0x1fff;
2090
2091 for (i = 0; i < 14; i++)
2092 chip->ch_accm[i][0] = (chip->ch_accm[i][1] << 1) | ((sum >> i) & 1);
2093
2094 for (i = 0; i < 9; i++)
2095 {
2096 chip->ch_out[i][0] = chip->ch_out[i][1] << 1;
2097 if (load)
2098 chip->ch_out[i][0] |= (chip->ch_accm[i+5][1] >> 5) & 1;
2099 else
2100 chip->ch_out[i][0] |= (chip->ch_out[i][1] >> 5) & 1;
2101 }
2102
2103 chip->ch_dac_load = chip->fsm_dac_load;
2104
2105 chip->ch_out_debug[0] = chip->ch_out_dlatch;
2106
2107 chip->fsm_op1_sel_l2[0] = (chip->fsm_op1_sel_l2[1] << 1) | chip->fsm_op1_sel;
2108 chip->fsm_op1_sel_l3[0] = chip->fsm_op1_sel;
2109
2110 if (sel_dac)
2111 chip->osc_out |= chip->mode_dac_data[1] << 6;
2112 if (sel_fm)
2113 chip->osc_out |= accm;
2114
2115 if (chip->osc_out & 0x2000)
2116 chip->osc_out |= 0x1c000;
2117
2118 for (i = 0; i < 2; i++)
2119 pan |= (((chip->chan_pan[i][1] >> 5) & 1) ^ 1) << i;
2120
2121 chip->fsm_shifter_ctrl[0] = chip->fsm_shifter_ctrl[1] << 1;
2122
2123 if (chip->fsm_op1_sel && !chip->fsm_op1_sel_l3[1])
2124 {
2125 acc_l = 0;
2126 acc_r = 0;
2127 chip->fsm_shifter_ctrl[0] |= 1;
2128 }
2129 else
2130 {
2131 acc_l = chip->ch_accm_l[1];
2132 acc_r = chip->ch_accm_r[1];
2133 }
2134
2135 chip->ch_accm_l[0] = acc_l + ((pan & 2) != 0 ? chip->osc_out : 0);
2136 chip->ch_accm_r[0] = acc_r + ((pan & 1) != 0 ? chip->osc_out : 0);
2137 }
2138
2139 void FMOPN2_YMF276Accumulator2(fmopn2_t *chip)
2140 {
2141 int i;
2142 int test_dac = (chip->mode_test_2c[1] & 32) != 0;
2143 for (i = 0; i < 14; i++)
2144 {
2145 chip->ch_accm[i][1] = chip->ch_accm[i][0];
2146 }
2147 for (i = 0; i < 9; i++)
2148 {
2149 chip->ch_out[i][1] = chip->ch_out[i][0];
2150 }
2151 if ((chip->fsm_dac_load && !chip->ch_dac_load) || test_dac)
2152 {
2153 chip->ch_out_dlatch = 0;
2154 if (chip->fsm_dac_out_sel || test_dac)
2155 {
2156 for (i = 0; i < 9; i++)
2157 chip->ch_out_dlatch |= ((chip->ch_out[i][1] >> 5) & 1) << i;
2158 }
2159 else
2160 {
2161 for (i = 0; i < 9; i++)
2162 chip->ch_out_dlatch |= ((chip->ch_out[i][1] >> 4) & 1) << i;
2163 }
2164 }
2165
2166 chip->ch_out_debug[1] = chip->ch_out_debug[0];
2167
2168 chip->fsm_op1_sel_l2[1] = chip->fsm_op1_sel_l2[0];
2169 chip->fsm_op1_sel_l3[1] = chip->fsm_op1_sel_l3[0];
2170
2171 chip->ch_accm_l[1] = chip->ch_accm_l[0];
2172 chip->ch_accm_r[1] = chip->ch_accm_r[0];
2173
2174 chip->fsm_shifter_ctrl[1] = chip->fsm_shifter_ctrl[0];
2175 }
2176
2177 void FMOPN2_Timers1(fmopn2_t *chip)
2178 {
2179 int time;
2180 int test_timers = (chip->mode_test_21[1] & 4) != 0;
2181 int reset;
2182 int subcnt;
2183 if (chip->timer_a_load_latch[1])
2184 time = chip->mode_timer_a_reg[1];
2185 else
2186 time = chip->timer_a_cnt[1];
2187
2188 if ((chip->timer_a_load_dlatch && chip->fsm_clock_timers1) || test_timers)
2189 time++;
2190
2191 reset = chip->mode_timer_a_reset[1] || chip->input.ic;
2192
2193 if (reset)
2194 chip->timer_a_status[0] = 0;
2195 else
2196 chip->timer_a_status[0] = chip->timer_a_status[1] || (chip->timer_a_of[1] && chip->mode_timer_a_enable[1]);
2197
2198 chip->timer_a_load_old[0] = chip->timer_a_load_dlatch;
2199 chip->timer_a_load_latch[0] = (!chip->timer_a_load_old[1] && chip->timer_a_load_dlatch) || chip->timer_a_of[1];
2200 if (!chip->timer_a_load_dlatch)
2201 chip->timer_a_cnt[0] = 0;
2202 else
2203 chip->timer_a_cnt[0] = time;
2204 chip->timer_a_of[0] = (time & 1024) != 0;
2205
2206
2207 subcnt = chip->timer_b_subcnt[1];
2208 if (chip->fsm_clock_timers1)
2209 subcnt++;
2210
2211 if (chip->input.ic)
2212 chip->timer_b_subcnt[0] = 0;
2213 else
2214 chip->timer_b_subcnt[0] = subcnt;
2215
2216 chip->timer_b_subcnt_of[0] = (subcnt & 16) != 0;
2217
2218 if (chip->timer_b_load_latch[1])
2219 time = chip->mode_timer_b_reg[1];
2220 else
2221 time = chip->timer_b_cnt[1];
2222
2223 if ((chip->timer_b_load_dlatch && chip->timer_b_subcnt_of[1]) || test_timers)
2224 time++;
2225
2226 reset = chip->mode_timer_b_reset[1] || chip->input.ic;
2227
2228 if (reset)
2229 chip->timer_b_status[0] = 0;
2230 else
2231 chip->timer_b_status[0] = chip->timer_b_status[1] || (chip->timer_b_of[1] && chip->mode_timer_b_enable[1]);
2232
2233 chip->timer_b_load_old[0] = chip->timer_b_load_dlatch;
2234 chip->timer_b_load_latch[0] = (!chip->timer_b_load_old[1] && chip->timer_b_load_dlatch) || chip->timer_b_of[1];
2235 if (!chip->timer_b_load_dlatch)
2236 chip->timer_b_cnt[0] = 0;
2237 else
2238 chip->timer_b_cnt[0] = time;
2239 chip->timer_b_of[0] = (time & 256) != 0;
2240
2241 chip->timer_dlatch = chip->fsm_clock_timers;
2242 }
2243
2244 void FMOPN2_Timers2(fmopn2_t *chip)
2245 {
2246 int read_enable = chip->input.cs && chip->input.rd && !chip->input.ic;
2247 chip->timer_a_load_latch[1] = chip->timer_a_load_latch[0];
2248 chip->timer_a_load_old[1] = chip->timer_a_load_old[0];
2249 chip->timer_a_cnt[1] = chip->timer_a_cnt[0] & 1023;
2250 chip->timer_a_of[1] = chip->timer_a_of[0];
2251 chip->timer_a_status[1] = chip->timer_a_status[0];
2252 chip->timer_b_subcnt[1] = chip->timer_b_subcnt[0] & 15;
2253 chip->timer_b_subcnt_of[1] = chip->timer_b_subcnt_of[0];
2254 chip->timer_b_load_latch[1] = chip->timer_b_load_latch[0];
2255 chip->timer_b_load_old[1] = chip->timer_b_load_old[0];
2256 chip->timer_b_cnt[1] = chip->timer_b_cnt[0] & 255;
2257 chip->timer_b_of[1] = chip->timer_b_of[0];
2258 chip->timer_b_status[1] = chip->timer_b_status[0];
2259 if (!chip->timer_dlatch && chip->fsm_clock_timers)
2260 {
2261 chip->timer_a_load_dlatch = chip->mode_timer_a_load[1];
2262 chip->timer_b_load_dlatch = chip->mode_timer_b_load[1];
2263 chip->timer_csm_key_dlatch = chip->mode_ch3[1] == 2 && ((!chip->timer_a_load_old[1] && chip->timer_a_load_dlatch) || chip->timer_a_of[1]);
2264 }
2265 if (!read_enable)
2266 {
2267 chip->status_timer_a_dlatch = chip->timer_a_status[1];
2268 chip->status_timer_b_dlatch = chip->timer_b_status[1];
2269 }
2270 }
2271
2272 void FMOPN2_ClockPhase1(fmopn2_t *chip)
2273 {
2274 FMOPN2_DoShiftRegisters(chip, 0);
2275 FMOPN2_HandleIO1(chip);
2276 FMOPN2_FMRegisters1(chip);
2277 FMOPN2_FSM1(chip);
2278 FMOPN2_Misc1(chip);
2279 FMOPN2_LFO1(chip);
2280 FMOPN2_PhaseGenerator1(chip);
2281 FMOPN2_EnvelopeGenerator1(chip);
2282 FMOPN2_Operator1(chip);
2283 if (!(chip->flags & fmopn2_flags_ym3438))
2284 FMOPN2_YMF276Accumulator1(chip);
2285 else
2286 FMOPN2_YM3438Accumulator1(chip);
2287 FMOPN2_Timers1(chip);
2288 }
2289
2290 void FMOPN2_ClockPhase2(fmopn2_t *chip)
2291 {
2292 FMOPN2_DoShiftRegisters(chip, 1);
2293 FMOPN2_HandleIO2(chip);
2294 FMOPN2_FMRegisters2(chip);
2295 FMOPN2_FSM2(chip);
2296 FMOPN2_Misc2(chip);
2297 FMOPN2_LFO2(chip);
2298 FMOPN2_PhaseGenerator2(chip);
2299 FMOPN2_EnvelopeGenerator2(chip);
2300 FMOPN2_Operator2(chip);
2301 if (!(chip->flags & fmopn2_flags_ym3438))
2302 FMOPN2_YMF276Accumulator2(chip);
2303 else
2304 FMOPN2_YM3438Accumulator2(chip);
2305 FMOPN2_Timers2(chip);
2306 }
2307
2308 void FMOPN2_ClockFM(fmopn2_t *chip)
2309 {
2310 FMOPN2_HandleIO(chip);
2311 if (chip->i_phi1)
2312 {
2313 FMOPN2_ClockPhase1(chip);
2314 }
2315 if (chip->i_phi2)
2316 {
2317 FMOPN2_ClockPhase2(chip);
2318 }
2319 }
2320
2321 void FMOPN2_Clock(fmopn2_t *chip, int clk)
2322 {
2323 chip->pinput.phi = clk;
2324 if (memcmp(&chip->pinput, &chip->pinput_old, sizeof(chip->pinput)) != 0)
2325 {
2326 FMOPN2_Prescaler(chip);
2327 chip->pinput_old = chip->pinput;
2328 chip->input.i_fsm_reset = chip->fsm_reset;
2329 if (chip->phi1_latch[1])
2330 chip->input.phi_phase = 1;
2331 if (chip->phi2_latch[1])
2332 chip->input.phi_phase = 2;
2333 chip->i_phi1 = chip->phi1_latch[1];
2334 chip->i_phi2 = chip->phi2_latch[1];
2335 }
2336 if (memcmp(&chip->input, &chip->input_old, sizeof(chip->input)) != 0)
2337 {
2338 FMOPN2_ClockFM(chip);
2339 chip->input_old = chip->input;
2340 }
2341
2342 if (!(chip->flags & fmopn2_flags_ym3438))
2343 FMOPN2_YMF276DAC(chip);
2344 }