Debian Codegraph @

src/engine/platform/sound/dave/dave.cpp

raw
1
2 // ep128emu -- portable Enterprise 128 emulator
3 // Copyright (C) 2003-2016 Istvan Varga <[email protected]>
4 // https://github.com/istvan-v/ep128emu/
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (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 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 #include "dave.hpp"
21 #include <math.h>
22 #include <stdlib.h>
23
24 #define EP128EMU_UNLIKELY(x) x
25
26 // Generate polynomial counter of log2(m0) + 1 bits length, and store
27 // (m0 * 2 - 1) samples at 'tabptr' in reverse order.
28 // log2(m1) is the second bit to be used in the XOR operation when
29 // calculating the next bit of output.
30
31 static void calculate_polycnt(uint8_t *tabptr, uint32_t m0, uint32_t m1)
32 {
33 uint32_t sr = 0xFFFFFFFFU;
34 int n = int(m0 << 1) - 1;
35 while (--n >= 0) {
36 uint8_t b0 = uint8_t(bool(sr & m0));
37 uint8_t b1 = uint8_t(bool(sr & m1));
38 tabptr[n] = b0;
39 sr = (sr << 1) | uint32_t(b0 ^ b1);
40 }
41 }
42
43 namespace Ep128 {
44 DaveTables::DaveTables()
45 {
46 static const uint32_t polycnt_params[14] = {
47 0x00000008U, 0x00000004U, // 4-bit: poly = 11001
48 0x00000010U, 0x00000004U, // 5-bit: poly = 101001
49 0x00000040U, 0x00000020U, // 7-bit: poly = 11000001
50 0x00000100U, 0x00000010U, // 9-bit: poly = 1000100001
51 0x00000400U, 0x00000100U, // 11-bit: poly = 101000000001
52 0x00004000U, 0x00002000U, // 15-bit: poly = 1100000000000001
53 0x00010000U, 0x00002000U // 17-bit: poly = 100100000000000001
54 };
55 polycnt4_table = new uint8_t[15 + 31 + 127 + 511 + 2047 + 32767 + 131071];
56 polycnt5_table = &(polycnt4_table[15]);
57 polycnt7_table = &(polycnt5_table[31]);
58 polycnt9_table = &(polycnt7_table[127]);
59 polycnt11_table = &(polycnt9_table[511]);
60 polycnt15_table = &(polycnt11_table[2047]);
61 polycnt17_table = &(polycnt15_table[32767]);
62 uint8_t *bufp = polycnt4_table;
63 for (int i = 0; i < 14; i += 2) {
64 calculate_polycnt(bufp, polycnt_params[i], polycnt_params[i + 1]);
65 bufp = bufp + ((polycnt_params[i] << 1) - 1U);
66 }
67 }
68
69 DaveTables::~DaveTables()
70 {
71 delete[] polycnt4_table;
72 }
73
74 // handle timer interrupts
75
76 inline void Dave::triggerIntSnd()
77 {
78 // trigger interrupt on edge only
79 if (int_snd_active)
80 return;
81 // mark as active
82 int_snd_active = 1;
83 interruptRequest();
84 }
85
86 inline void Dave::triggerInt1Hz()
87 {
88 // trigger interrupt on edge only
89 if (int_1hz_active)
90 return;
91 // mark as active
92 int_1hz_active = 1;
93 interruptRequest();
94 }
95
96 // run DAVE emulation, and also trigger any sound or timer interrupts
97
98 uint32_t Dave::runOneCycle_()
99 {
100 // update polynomial counters
101 if (--polycnt4_phase < 0) // 4-bit
102 polycnt4_phase = 14;
103 polycnt4_state = (int) t.polycnt4_table[polycnt4_phase];
104 if (--polycnt5_phase < 0) // 5-bit
105 polycnt5_phase = 30;
106 polycnt5_state = (int) t.polycnt5_table[polycnt5_phase];
107 if (!noise_polycnt_is_7bit) {
108 // channel 3 uses the variable length polynomial counter
109 if (--polycnt7_phase < 0) // 7-bit
110 polycnt7_phase = 126;
111 polycnt7_state = (int) t.polycnt7_table[polycnt7_phase];
112 // channel 3 polynomial counter: updated on negative edge
113 if (*chn3_clk_source < chn3_clk_source_prv) {
114 if (--polycntVL_phase < 0) // variable length
115 polycntVL_phase = polycntVL_maxphase;
116 polycntVL_state = (int) polycntVL_table[polycntVL_phase];
117 chn3_state1 = polycntVL_state; // input signal to channel 3
118 if (!chn3_lp_2)
119 chn3_state2 = polycntVL_state;
120 }
121 chn3_clk_source_prv = *chn3_clk_source;
122 }
123 else {
124 // channel 3 uses the 7-bit polynomial counter
125 if (*chn3_clk_source < chn3_clk_source_prv) {
126 // update on negative edge
127 if (--polycnt7_phase < 0) // 7-bit
128 polycnt7_phase = 126;
129 polycnt7_state = (int) t.polycnt7_table[polycnt7_phase];
130 chn3_state1 = polycnt7_state; // input signal to channel 3
131 if (!chn3_lp_2)
132 chn3_state2 = polycnt7_state;
133 }
134 chn3_clk_source_prv = *chn3_clk_source;
135 if (--polycntVL_phase < 0) // variable length
136 polycntVL_phase = polycntVL_maxphase;
137 polycntVL_state = (int) polycntVL_table[polycntVL_phase];
138 }
139
140 // update the phase of all oscillators
141 clk_62500_phase--;
142 clk_1000_phase--;
143 chn0_phase -= chn0_run;
144 chn1_phase -= chn1_run;
145 chn2_phase -= chn2_run;
146
147 // reload phase counters if necessary
148 if (EP128EMU_UNLIKELY(clk_1000_phase < 0)) {
149 clk_50_phase--;
150 // trigger interrupts if enabled
151 if ((*int_snd_phase) < 0) {
152 // will reload counter later
153 int_snd_state = (int_snd_state & 1) ^ 1; // invert state
154 if (enable_int_snd)
155 triggerIntSnd();
156 }
157 clk_1000_phase = clk_1000_frq;
158 if (EP128EMU_UNLIKELY(clk_50_phase < 0)) {
159 clk_50_phase = clk_50_frq;
160 clk_1_phase--;
161 if (EP128EMU_UNLIKELY(clk_1_phase < 0)) {
162 clk_1_phase = clk_1_frq; // reload counter
163 int_1hz_state = (int_1hz_state & 1) ^ 1; // invert state
164 if (enable_int_1hz)
165 triggerInt1Hz();
166 }
167 }
168 }
169 else if (EP128EMU_UNLIKELY((*int_snd_phase) < 0)) {
170 // trigger interrupt if enabled
171 int_snd_state = (int_snd_state & 1) ^ 1; // invert state
172 if (enable_int_snd)
173 triggerIntSnd();
174 }
175
176 // calculate oscillator outputs
177 if (clk_62500_phase < 0) {
178 // simple 31250 Hz oscillator
179 clk_62500_phase = clk_62500_frq; // reload counter
180 clk_62500_state = (clk_62500_state & 1) ^ 1; // invert state
181 }
182 // ---- channel 3 ----
183 chn3_prv = chn3_state; // save previous output
184 if (chn3_lp_2 && (chn2_state < chn2_prv)) {
185 // lowpass filter holds signal until negative edge in channel 2
186 chn3_state2 = chn3_state1;
187 }
188 if (chn3_hp_0 && (chn0_state < chn0_prv)) {
189 // highpass filter: sets level to 0 on negative edge in channel 0
190 chn3_state2 = 0;
191 }
192 // store final output signal in chn3_state
193 chn3_state = chn3_state2;
194 if (chn3_rm_1) {
195 // ring modulation: XNOR by channel 1
196 chn3_state ^= (chn1_state ^ 1);
197 }
198 // ---- channel 2 ----
199 chn2_prv = chn2_state; // save previous output
200 if (chn2_phase < 0) {
201 chn2_phase = chn2_frqcode; // reload counter
202 if (chn2_input_polycnt == NULL) {
203 // square wave
204 chn2_state1 = (chn2_state1 & 1) ^ 1;
205 }
206 else {
207 // get input from polynomial counter
208 chn2_state1 = *chn2_input_polycnt;
209 }
210 }
211 if (chn2_hp_3 && (chn3_state < chn3_prv)) {
212 // highpass filter: sets level to 0 on negative edge in channel 3
213 chn2_state1 = 0;
214 }
215 // store final output signal in chn2_state
216 chn2_state = chn2_state1;
217 if (chn2_rm_0) {
218 // ring modulation: XNOR by channel 0
219 chn2_state ^= (chn0_state ^ 1);
220 }
221 // ---- channel 1 ----
222 chn1_prv = chn1_state; // save previous output
223 if (chn1_phase < 0) {
224 chn1_phase = chn1_frqcode; // reload counter
225 if (chn1_input_polycnt == NULL) {
226 // square wave
227 chn1_state1 = (chn1_state1 & 1) ^ 1;
228 }
229 else {
230 // get input from polynomial counter
231 chn1_state1 = *chn1_input_polycnt;
232 }
233 }
234 if (chn1_hp_2 && (chn2_state < chn2_prv)) {
235 // highpass filter: sets level to 0 on negative edge in channel 2
236 chn1_state1 = 0;
237 }
238 // store final output signal in chn1_state
239 chn1_state = chn1_state1;
240 if (chn1_rm_3) {
241 // ring modulation: XNOR by channel 3
242 chn1_state ^= (chn3_state ^ 1);
243 }
244 // ---- channel 0 ----
245 chn0_prv = chn0_state; // save previous output
246 if (chn0_phase < 0) {
247 chn0_phase = chn0_frqcode; // reload counter
248 if (chn0_input_polycnt == NULL) {
249 // square wave
250 chn0_state1 = (chn0_state1 & 1) ^ 1;
251 }
252 else {
253 // get input from polynomial counter
254 chn0_state1 = *chn0_input_polycnt;
255 }
256 }
257 if (chn0_hp_1 && (chn1_state < chn1_prv)) {
258 // highpass filter: sets level to 0 on negative edge in channel 1
259 chn0_state1 = 0;
260 }
261 // store final output signal in chn0_state
262 chn0_state = chn0_state1;
263 if (chn0_rm_2) {
264 // ring modulation: XNOR by channel 2
265 chn0_state ^= (chn2_state ^ 1);
266 }
267
268 // and now the final DAC output (left/right) values
269 // total output range (not including tape feedback): 0 to 252
270 unsigned int lval =
271 ((tape_feedback & tape_input) == 0 ? 0U : 0x3FU); // tape feedback
272 unsigned int rval = lval;
273 if (dac_mode_left) {
274 lval += (chn0_left << 2);
275 if (dac_mode_right) {
276 // simplest case: both channels in DAC mode
277 rval += (chn0_right << 2);
278 }
279 else {
280 // left channel is in DAC mode, but right is not
281 if (chn0_state)
282 rval += chn0_right;
283 if (chn1_state)
284 rval += chn1_right;
285 if (chn2_state)
286 rval += chn2_right;
287 if (chn3_state)
288 rval += chn3_right;
289 }
290 }
291 else if (dac_mode_right) {
292 // right channel is in DAC mode, but left is not
293 rval += (chn0_right << 2);
294 if (chn0_state)
295 lval += chn0_left;
296 if (chn1_state)
297 lval += chn1_left;
298 if (chn2_state)
299 lval += chn2_left;
300 if (chn3_state)
301 lval += chn3_left;
302 }
303 else {
304 // neither channel is in DAC mode
305 if (chn0_state) {
306 lval += chn0_left;
307 rval += chn0_right;
308 }
309 if (chn1_state) {
310 lval += chn1_left;
311 rval += chn1_right;
312 }
313 if (chn2_state) {
314 lval += chn2_left;
315 rval += chn2_right;
316 }
317 if (chn3_state) {
318 lval += chn3_left;
319 rval += chn3_right;
320 }
321 }
322
323 audioOutput = uint32_t(lval + (rval << 16)) << 7;
324 return audioOutput;
325 }
326
327 // returns pointer to the polynomial counter for channels 0, 1, and 2
328 // selected by 'n' (allowed values for 'n' are 0x00, 0x10, 0x20, and 0x30)
329
330 int * Dave::findPolycntForToneChannel(int n)
331 {
332 switch (n) {
333 case 0x10:
334 return (&polycnt4_state); // 4-bit
335 case 0x20:
336 return (&polycnt5_state); // 5-bit
337 case 0x30:
338 if (!noise_polycnt_is_7bit) {
339 return (&polycnt7_state); // 7-bit
340 }
341 else {
342 return (&polycntVL_state); // variable length
343 }
344 }
345 // default to square wave
346 return (int*) NULL;
347 }
348
349 // write to DAVE registers
350
351 void Dave::writePort(uint16_t addr, uint8_t value)
352 {
353 switch (uint8_t(addr & 0x1F)) {
354 case 0x00:
355 // channel 0 frequency
356 chn0_frqcode = (chn0_frqcode & 0x0F00) | (int) value;
357 break;
358 case 0x01:
359 // channel 0 frequency and mode
360 chn0_frqcode = (chn0_frqcode & 0x00FF) | (((int) value & 0x0F) << 8);
361 // select distortion mode
362 chn0_input_polycnt = findPolycntForToneChannel((int) value & 0x30);
363 chn0_hp_1 = ((int) value & 0x40 ? 1 : 0); // highpass
364 chn0_rm_2 = ((int) value & 0x80 ? 1 : 0); // ringmod
365 break;
366 case 0x02:
367 // channel 1 frequency
368 chn1_frqcode = (chn1_frqcode & 0x0F00) | (int) value;
369 break;
370 case 0x03:
371 // channel 1 frequency and mode
372 chn1_frqcode = (chn1_frqcode & 0x00FF) | (((int) value & 0x0F) << 8);
373 // select distortion mode
374 chn1_input_polycnt = findPolycntForToneChannel((int) value & 0x30);
375 chn1_hp_2 = ((int) value & 0x40 ? 1 : 0); // highpass
376 chn1_rm_3 = ((int) value & 0x80 ? 1 : 0); // ringmod
377 break;
378 case 0x04:
379 // channel 2 frequency
380 chn2_frqcode = (chn2_frqcode & 0x0F00) | (int) value;
381 break;
382 case 0x05:
383 // channel 2 frequency and mode
384 chn2_frqcode = (chn2_frqcode & 0x00FF) | (((int) value & 0x0F) << 8);
385 // select distortion mode
386 chn2_input_polycnt = findPolycntForToneChannel((int) value & 0x30);
387 chn2_hp_3 = ((int) value & 0x40 ? 1 : 0); // highpass
388 chn2_rm_0 = ((int) value & 0x80 ? 1 : 0); // ringmod
389 break;
390 case 0x06:
391 // channel 3 parameters
392 switch ((int) value & 0x03) {
393 // polynomial counter clock source
394 case 0x00: chn3_clk_source = &clk_62500_state; break;
395 case 0x01: chn3_clk_source = &chn0_state; break;
396 case 0x02: chn3_clk_source = &chn1_state; break;
397 case 0x03: chn3_clk_source = &chn2_state; break;
398 }
399 // select variable length polynomial counter
400 switch ((int) value & 0x0C) {
401 case 0x00:
402 polycntVL_table = t.polycnt17_table; // 17-bit
403 polycntVL_maxphase = 131070;
404 break;
405 case 0x04:
406 polycntVL_table = t.polycnt15_table; // 15-bit
407 polycntVL_maxphase = 32766;
408 break;
409 case 0x08:
410 polycntVL_table = t.polycnt11_table; // 11-bit
411 polycntVL_maxphase = 2046;
412 break;
413 case 0x0C:
414 polycntVL_table = t.polycnt9_table; // 9-bit
415 polycntVL_maxphase = 510;
416 break;
417 }
418 // wrap the phase of variable length polynomial counter to table length
419 polycntVL_phase = polycntVL_phase % (polycntVL_maxphase + 1);
420 // bit 4: swap 7-bit and variable length polynomial counters if set
421 if ((int) value & 0x10) {
422 noise_polycnt_is_7bit = 1;
423 chn3_input_polycnt = &polycnt7_state;
424 if (chn0_input_polycnt == &polycnt7_state)
425 chn0_input_polycnt = &polycntVL_state;
426 if (chn1_input_polycnt == &polycnt7_state)
427 chn1_input_polycnt = &polycntVL_state;
428 if (chn2_input_polycnt == &polycnt7_state)
429 chn2_input_polycnt = &polycntVL_state;
430 }
431 else {
432 noise_polycnt_is_7bit = 0;
433 chn3_input_polycnt = &polycntVL_state;
434 if (chn0_input_polycnt == &polycntVL_state)
435 chn0_input_polycnt = &polycnt7_state;
436 if (chn1_input_polycnt == &polycntVL_state)
437 chn1_input_polycnt = &polycnt7_state;
438 if (chn2_input_polycnt == &polycntVL_state)
439 chn2_input_polycnt = &polycnt7_state;
440 }
441 chn3_lp_2 = ((int) value & 0x20 ? 1 : 0); // lowpass with channel 2
442 chn3_hp_0 = ((int) value & 0x40 ? 1 : 0); // highpass with channel 0
443 chn3_rm_1 = ((int) value & 0x80 ? 1 : 0); // ring mod. with channel 1
444 break;
445 case 0x07:
446 // sound/interrupt control register
447 if ((int) value & 0x01) {
448 chn0_run = 0; // channel 0 sync
449 chn0_state1 = 0;
450 }
451 else if (!chn0_run) {
452 chn0_phase = chn0_frqcode; // reset phase
453 chn0_run = 1;
454 }
455 if ((int) value & 0x02) {
456 chn1_run = 0; // channel 1 sync
457 chn1_state1 = 0;
458 }
459 else if (!chn1_run) {
460 chn1_phase = chn1_frqcode; // reset phase
461 chn1_run = 1;
462 }
463 if ((int) value & 0x04) {
464 chn2_run = 0; // channel 2 sync
465 chn2_state1 = 0;
466 }
467 else if (!chn2_run) {
468 chn2_phase = chn2_frqcode; // reset phase
469 chn2_run = 1;
470 }
471 dac_mode_left = ((int) value & 0x08 ? 1 : 0); // analogue mode
472 dac_mode_right = ((int) value & 0x10 ? 1 : 0);
473 switch ((int) value & 0x60) {
474 // sound interrupt mode
475 case 0x00:
476 int_snd_phase = &clk_1000_phase;
477 break;
478 case 0x20:
479 int_snd_phase = &clk_50_phase;
480 break;
481 case 0x40:
482 int_snd_phase = &chn0_phase;
483 break;
484 case 0x60:
485 int_snd_phase = &chn1_phase;
486 break;
487 }
488 break;
489 case 0x08:
490 // channel 0 left volume
491 chn0_left = int(value & 0x3F);
492 break;
493 case 0x09:
494 // channel 1 left volume
495 chn1_left = int(value & 0x3F);
496 break;
497 case 0x0A:
498 // channel 2 left volume
499 chn2_left = int(value & 0x3F);
500 break;
501 case 0x0B:
502 // channel 3 left volume
503 chn3_left = int(value & 0x3F);
504 break;
505 case 0x0C:
506 // channel 0 right volume
507 chn0_right = int(value & 0x3F);
508 break;
509 case 0x0D:
510 // channel 1 right volume
511 chn1_right = int(value & 0x3F);
512 break;
513 case 0x0E:
514 // channel 2 right volume
515 chn2_right = int(value & 0x3F);
516 break;
517 case 0x0F:
518 // channel 3 right volume
519 chn3_right = int(value & 0x3F);
520 break;
521 case 0x10:
522 // memory page 0
523 page0Segment = value;
524 setMemoryPage(0, value);
525 break;
526 case 0x11:
527 // memory page 1
528 page1Segment = value;
529 setMemoryPage(1, value);
530 break;
531 case 0x12:
532 // memory page 2
533 page2Segment = value;
534 setMemoryPage(2, value);
535 break;
536 case 0x13:
537 // memory page 3
538 page3Segment = value;
539 setMemoryPage(3, value);
540 break;
541 case 0x14:
542 // interrupt control register
543 {
544 int prv = (int_snd_active | int_1hz_active
545 | int_1_active | int_2_active);
546 uint8_t tmp = (uint8_t) value ^ (uint8_t) 0x55;
547 // sound/timer interrupt
548 enable_int_snd = (tmp & (uint8_t) 0x01 ? 0 : 1);
549 if (tmp & (uint8_t) 0x03)
550 int_snd_active = 0;
551 // 1 Hz interrupt
552 enable_int_1hz = (tmp & (uint8_t) 0x04 ? 0 : 1);
553 if (tmp & (uint8_t) 0x0C)
554 int_1hz_active = 0;
555 // INT 1 (video interrupt)
556 enable_int_1 = (tmp & (uint8_t) 0x10 ? 0 : 1);
557 if (tmp & (uint8_t) 0x30)
558 int_1_active = 0;
559 // INT 2
560 enable_int_2 = (tmp & (uint8_t) 0x40 ? 0 : 1);
561 if (tmp & (uint8_t) 0xC0)
562 int_2_active = 0;
563 if (prv && !(int_snd_active | int_1hz_active
564 | int_1_active | int_2_active)) {
565 // no more active interrupts: clear request to CPU
566 clearInterruptRequest();
567 }
568 }
569 break;
570 case 0x15:
571 // select keyboard row
572 keyboardRow = int(value & 0x0F);
573 tape_feedback = int(!(value & 0x20)); // tape control
574 setRemote1State(value & 0x40 ? 1 : 0);
575 setRemote2State(value & 0x80 ? 1 : 0);
576 break;
577 case 0x1F: // system configuration register
578 {
579 // CPU wait cycle control
580 setMemoryWaitMode(int(value & 0x0C) >> 2);
581 // input clock frequency (note: the frequency is always 8 MHz,
582 // this bit sets the assumed value)
583 // 0: 8 MHz, dave_clock_freq = input_freq / 32
584 // 1: 12 MHz, dave_clock_freq = input_freq / 48
585 clockDiv = ((value & 0x02) == 0 ? 2 : 3);
586 }
587 break;
588 }
589 }
590
591 // read from DAVE registers
592
593 uint8_t Dave::readPort(uint16_t addr) const
594 {
595 switch (uint8_t(addr & 0x1F)) {
596 case 0x10:
597 return page0Segment;
598 case 0x11:
599 return page1Segment;
600 case 0x12:
601 return page2Segment;
602 case 0x13:
603 return page3Segment;
604 case 0x14:
605 {
606 // interrupt state
607 return uint8_t((int_snd_state | (int_snd_active << 1))
608 | ((int_1hz_state | (int_1hz_active << 1)) << 2)
609 | ((int_1_state | (int_1_active << 1)) << 4)
610 | ((int_2_state | (int_2_active << 1)) << 6));
611 }
612 break;
613 case 0x15:
614 // read currently selected keyboard row
615 return (keyboardRow < 10 ? keyboardState[keyboardRow] : 0xFF);
616 case 0x16:
617 {
618 // tape input
619 uint8_t n =
620 uint8_t(((tape_input_level - 1) & 0x40) | ((tape_input - 1) & 0x80)
621 | 0x0F);
622 if (keyboardRow < 5) {
623 if (keyboardRow == 0) {
624 // EnterMice buttons (left and right)
625 n &= uint8_t(0xF9 | (mouseInput >> 3));
626 // EXT1 joystick fire buttons
627 n &= uint8_t(0xF8 | (keyboardState[14] >> 4));
628 }
629 else {
630 if (!(mouseInput & 0x80)) // EnterMice data input on column K
631 n &= uint8_t(0xFD | ((mouseInput << 1) >> (keyboardRow - 1)));
632 // EXT1 joystick (mapped to row 14)
633 n &= uint8_t(0xFE | (keyboardState[14] >> (4 - keyboardRow)));
634 }
635 }
636 else if (keyboardRow < 10) {
637 // external joystick 2 (mapped to keyboard row 15)
638 if (keyboardRow == 5) // fire buttons
639 n &= uint8_t(0xF8 | (keyboardState[15] >> 4));
640 else
641 n &= uint8_t(0xFE | (keyboardState[15] >> (9 - keyboardRow)));
642 }
643 return n;
644 }
645 }
646 // anything else is either handled elsewhere, or is write-only
647 return 0xFF;
648 }
649
650 // set hardware interrupt 1 state, and (possibly) trigger interrupt
651
652 void Dave::setInt1State(int new_state)
653 {
654 int prv = int_1_state;
655 // set new state
656 int_1_state = (new_state ? 1 : 0);
657 // on negative edge, trigger CPU interrupt
658 // (assuming it is enabled, and not active already)
659 if (!enable_int_1)
660 return; // disabled
661 if (int_1_state || !prv)
662 return; // not on negative edge
663 if (int_1_active)
664 return; // already active
665 // now active
666 int_1_active = 1;
667 // send request to CPU
668 interruptRequest();
669 }
670
671 // set hardware interrupt 2 state, and (possibly) trigger interrupt
672
673 void Dave::setInt2State(int new_state)
674 {
675 int prv = int_2_state;
676 // set new state
677 int_2_state = (new_state ? 1 : 0);
678 // on negative edge, trigger CPU interrupt
679 // (assuming it is enabled, and not active already)
680 if (!enable_int_2)
681 return; // disabled
682 if (int_2_state || !prv)
683 return; // not on negative edge
684 if (int_2_active)
685 return; // already active
686 // now active
687 int_2_active = 1;
688 // send request to CPU
689 interruptRequest();
690 }
691
692 Dave::Dave()
693 {
694 clockDiv = 2;
695 clockCnt = 1;
696 polycnt4_state = 0;
697 polycnt5_state = 0;
698 polycnt7_state = 0;
699 polycntVL_state = 0;
700 clk_62500_state = 0;
701 chn0_state = 0;
702 chn0_prv = 0;
703 chn0_state1 = 0;
704 chn0_frqcode = 0;
705 chn0_run = 1;
706 chn1_state = 0;
707 chn1_prv = 0;
708 chn1_state1 = 0;
709 chn1_frqcode = 0;
710 chn1_run = 1;
711 chn2_state = 0;
712 chn2_prv = 0;
713 chn2_state1 = 0;
714 chn2_frqcode = 0;
715 chn2_run = 1;
716 chn3_state = 0;
717 chn3_prv = 0;
718 chn3_state1 = 0;
719 chn3_state2 = 0;
720 chn3_clk_source_prv = 0;
721 int_snd_state = 0;
722 int_1hz_state = 0;
723 int_1_state = 1;
724 int_2_state = 1;
725 int_snd_active = 0;
726 int_1hz_active = 0;
727 int_1_active = 0;
728 int_2_active = 0;
729 audioOutput = 0;
730 tape_input = 0;
731 tape_input_level = 0;
732 this->reset(true);
733 }
734
735 Dave::~Dave()
736 {
737 }
738
739 void Dave::reset(bool isColdReset)
740 {
741 polycnt4_phase = 0;
742 polycnt5_phase = 0;
743 polycnt7_phase = 0;
744 polycntVL_phase = 0;
745 chn0_phase = 0;
746 chn1_phase = 0;
747 chn2_phase = 0;
748 clk_62500_phase = 0;
749 clk_1000_phase = 0;
750 clk_50_phase = 0;
751 clk_1_phase = 0;
752 // initialize registers
753 // this will also reset many variables to the default value
754 for (uint16_t i = 0x00; i < 0x20; i++)
755 writePort(i, 0);
756 // clear all interrupts
757 writePort(0x14, 0xAA);
758 if (isColdReset) {
759 // reset keyboard state
760 for (int i = 0; i < 16; i++)
761 keyboardState[i] = 0xFF;
762 }
763 mouseInput = 0xFF;
764 }
765
766 void Dave::setTapeInput(int state, int level)
767 {
768 tape_input = (state ? 1 : 0);
769 tape_input_level = (level > 0 ? 1 : 0);
770 }
771
772 void Dave::setKeyboardState(int keyCode, int state)
773 {
774 int row = (keyCode & 0x78) >> 3;
775 uint8_t mask = uint8_t(1 << (keyCode & 0x07));
776 if (!state)
777 keyboardState[row] |= mask;
778 else
779 keyboardState[row] &= (~mask);
780 }
781
782 // --------------------------------------------------------------------------
783
784 void Dave::setMemoryPage(uint8_t page, uint8_t segment)
785 {
786 (void) page;
787 (void) segment;
788 }
789
790 void Dave::setMemoryWaitMode(int mode)
791 {
792 (void) mode;
793 }
794
795 void Dave::setRemote1State(int state)
796 {
797 (void) state;
798 }
799
800 void Dave::setRemote2State(int state)
801 {
802 (void) state;
803 }
804
805 void Dave::interruptRequest()
806 {
807 }
808
809 void Dave::clearInterruptRequest()
810 {
811 }
812
813 } // namespace Ep128
814