annotate ymz263b.c @ 2467:bf8a77a8ddc4

Fix some YMZ263B timer issues
author Michael Pavone <pavone@retrodev.com>
date Sat, 24 Feb 2024 21:07:38 -0800
parents b5640ac9aea9
children 2f4c17b4fe10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <string.h>
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "ymz263b.h"
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include "backend.h"
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 enum {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 YMZ_SELT,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 YMZ_LSI_TEST,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 YMZ_TIMER0_LOW,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 YMZ_TIMER0_HIGH,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 YMZ_TIMER_BASE,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 YMZ_TIMER1,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 YMZ_TIMER2_LOW,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 YMZ_TIMER2_HIGH,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 YMZ_TIMER_CTRL,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 YMZ_PCM_PLAY_CTRL,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 YMZ_PCM_VOL,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 YMZ_PCM_DATA,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 YMZ_PCM_CTRL,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 YMZ_MIDI_CTRL,
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 YMZ_MIDI_DATA
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 };
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 //YMZ_SELT
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 #define BIT_SELT 0x01
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 //YMZ_TIMER_CTRL
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 #define BIT_ST0 0x01
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 #define BIT_ST1 0x02
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 #define BIT_ST2 0x04
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 #define BIT_STBC 0x08
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 #define BIT_T0_MSK 0x10
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 #define BIT_T1_MSK 0x20
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 #define BIT_T2_MSK 0x40
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 #define TIMER_RUN_MASK (BIT_ST0|BIT_ST1|BIT_ST2)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 #define TIMER_INT_MASK (BIT_T0_MSK|BIT_T1_MSK|BIT_T2_MSK)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 //YMZ_PCM_PLAY_CTRL
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
39 #define BIT_ADP_ST 0x01
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
40 #define BIT_PLY_REC 0x02
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
41 #define BIT_PCM 0x04
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
42 #define BIT_PAN_L 0x20
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
43 #define BIT_PAN_R 0x40
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 #define BIT_ADP_RST 0x80
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
46 //YMZ_PCM_FIFO_CTRL
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
47 #define BIT_DMA_ENB 0x01
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
48 #define BIT_MSK_FIF 0x02
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
49 #define BIT_DMA_MOD 0x80
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
50
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 //YMZ_MIDI_CTRL
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 #define BIT_MSK_RRQ 0x01
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 #define BIT_MRC_RST 0x02
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 #define BIT_MSK_TRQ 0x04
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 #define BIT_MTR_RST 0x08
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 #define BIT_MSK_MOV 0x10
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 #define BIT_MSK_POV 0x20
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 #define STATUS_FIF1 0x01
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 #define STATUS_FIF2 0x02
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 #define STATUS_RRQ 0x04
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 #define STATUS_TRQ 0x08
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 #define STATUS_T0 0x10
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 #define STATUS_T1 0x20
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 #define STATUS_T2 0x40
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 #define STATUS_OV 0x80
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
68 #define TIMER_DIVIDER 32
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 #define MIDI_BYTE_DIVIDER 170
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
70 #define PCM_BASE_DIVIDER 12
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 #define FIFO_EMPTY 255
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
73 void ymz263b_init(ymz263b *ymz, uint32_t master_clock, uint32_t clock_divider)
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 memset(ymz, 0, sizeof(*ymz));
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
76 ymz->clock_inc = clock_divider * TIMER_DIVIDER;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
77 ymz->audio = render_audio_source("YMZ263B", master_clock, ymz->clock_inc * PCM_BASE_DIVIDER, 2);
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 ymz->base_regs[YMZ_SELT] = 1;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 ymz->pcm[0].regs[0] = BIT_ADP_RST;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 ymz->pcm[1].regs[0] = BIT_ADP_RST;
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
81 ymz->pcm[0].counter = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
82 ymz->pcm[0].fifo_read = FIFO_EMPTY;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
83 ymz->pcm[1].counter = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
84 ymz->pcm[1].fifo_read = FIFO_EMPTY;
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 ymz->midi_regs[0] = BIT_MTR_RST | BIT_MRC_RST;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 ymz->midi_trs.read = ymz->midi_rcv.read = FIFO_EMPTY;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 ymz->status = 0;
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
88 ymz->pcm_counter = PCM_BASE_DIVIDER;
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 static uint8_t fifo_empty(ymz_midi_fifo *fifo)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 return fifo->read == FIFO_EMPTY;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 static uint8_t fifo_read(ymz_midi_fifo *fifo)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 uint8_t ret = fifo->fifo[fifo->read++];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 fifo->read &= 15;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 if (fifo->read == fifo->write) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 fifo->read = FIFO_EMPTY;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 return ret;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 static uint8_t fifo_write(ymz_midi_fifo *fifo, uint8_t value)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 uint8_t overflow = fifo->read == fifo->write;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 if (fifo->read == FIFO_EMPTY) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 fifo->read = fifo->write;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 fifo->fifo[fifo->write++] = value;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 fifo->write &= 15;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 return overflow;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 static uint8_t fifo_size(ymz_midi_fifo *fifo)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 if (fifo->read == FIFO_EMPTY) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 return 0;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 if (fifo->read == fifo->write) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 return 16;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 return (fifo->write - fifo->read) & 15;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
128 static uint8_t pcm_fifo_empty(ymz263b_pcm *pcm)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
129 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
130 return pcm->fifo_read == FIFO_EMPTY;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
131 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
132
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
133 static uint16_t pcm_fifo_read(ymz263b_pcm *pcm, uint8_t nibbles)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
134 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
135 uint16_t ret = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
136 for (; nibbles && !pcm_fifo_empty(pcm); nibbles--)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
137 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
138 ret <<= 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
139 if (pcm->nibble) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
140 ret |= pcm->fifo[pcm->fifo_read++] & 0xF;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
141 pcm->fifo_read &= sizeof(pcm->fifo) - 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
142 pcm->nibble = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
143 if (pcm->fifo_read == pcm->fifo_write) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
144 pcm->fifo_read = FIFO_EMPTY;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
145 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
146 } else {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
147 ret |= pcm->fifo[pcm->fifo_read] >> 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
148 pcm->nibble = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
149 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
150 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
151 return ret;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
152 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
153
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
154 static uint8_t pcm_fifo_write(ymz263b_pcm *pcm, uint8_t nibbles, uint16_t value)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
155 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
156 uint8_t overflow = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
157 value <<= (4 - nibbles) * 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
158 for (; nibbles; nibbles--)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
159 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
160 if (pcm->nibble_write) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
161 pcm->fifo[pcm->fifo_write++] |= value >> 12;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
162 pcm->fifo_write &= sizeof(pcm->fifo) - 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
163 pcm->nibble_write = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
164 } else {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
165 if (pcm->fifo_read == FIFO_EMPTY) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
166 pcm->fifo_read = pcm->fifo_write;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
167 } else if (pcm->fifo_read == pcm->fifo_write) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
168 overflow = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
169 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
170 pcm->fifo[pcm->fifo_write] = value >> 8 & 0xF0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
171 pcm->nibble_write = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
172 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
173 value <<= 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
174 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
175 return overflow;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
176 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
177
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
178 static uint8_t pcm_fifo_free(ymz263b_pcm *pcm)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
179 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
180 if (pcm->fifo_read == FIFO_EMPTY) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
181 return sizeof(pcm->fifo);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
182 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
183 return (pcm->fifo_read - pcm->fifo_write) & (sizeof(pcm->fifo) - 1);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
184 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
185
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
186 static uint8_t pcm_dividers[] = {1, 2, 4, 6, 8};
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
187 static void ymz263b_pcm_run(ymz263b *ymz, ymz263b_pcm *pcm, int16_t *output)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
188 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
189 if ((pcm->regs[0] & (BIT_ADP_RST|BIT_ADP_ST)) != BIT_ADP_ST) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
190 //PCM channel is either in reset or not started
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
191 return;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
192 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
193 pcm->counter--;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
194 if (!pcm->counter) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
195 uint8_t fs = pcm->regs[0] >> 3 & 3;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
196 if (!(pcm->regs[0] & BIT_PCM)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
197 //ADPCM can't use 44.1 kHz, but gains 5.5125 kHz
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
198 fs++;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
199 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
200 pcm->counter = pcm_dividers[fs];
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
201 uint8_t nibbles = (pcm->regs[3] >> 5 & 3) + 2;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
202 if (nibbles > 4) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
203 //4-bit format is encoded as the highest value for some reason
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
204 nibbles = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
205 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
206 //adlib driver suggests that FMT should be 3 for 4-bit ADPCM, but Copera games seem to use zero
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
207 //maybe FMT is ignored for ADPCM mode?
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
208 if (!(pcm->regs[0] & BIT_PCM)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
209 nibbles = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
210 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
211 //adlib driver sets SELF to 5 for playback to trigger int "when less than 32 bytes in fifo" aka 96 bytes free
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
212 //adlib driver sets SELF to 3 for recording to trigger int "when more than 64 bytes in fifo" aka 64 bytes free
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
213 uint8_t fifo_threshold = ((pcm->regs[3] >> 2 & 7) + 1) << 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
214 if (pcm->regs[0] & BIT_PLY_REC) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
215 //Playback mode
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
216 uint16_t sample = pcm_fifo_read(pcm, nibbles);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
217 if (pcm->regs[0] & BIT_PCM) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
218 //TODO: Presumably SELT bit impacts this
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
219 if (sample & (1 << (nibbles * 4 - 1))) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
220 sample |= 0xFFF << (nibbles * 4);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
221 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
222 switch (nibbles)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
223 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
224 case 1:
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
225 sample <<= 8;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
226 break;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
227 case 2:
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
228 sample <<= 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
229 break;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
230 case 4:
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
231 //PCem's code seems to imply the "hole" is in the middle
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
232 //but that's ugly so hoping it's incorrect
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
233 sample >>= 4;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
234 break;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
235 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
236 pcm->output = sample;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
237 } else {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
238 //Values taken from YMFM 2610 ADPCM-A implementation
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
239 //They are almost certainly wrong for YMZ263B
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
240 static const int16_t mults[49] = {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
241 16, 17, 19, 21, 23, 25, 28,
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
242 31, 34, 37, 41, 45, 50, 55,
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
243 60, 66, 73, 80, 88, 97, 107,
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
244 118, 130, 143, 157, 173, 190, 209,
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
245 230, 253, 279, 307, 337, 371, 408,
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
246 449, 494, 544, 598, 658, 724, 796,
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
247 876, 963, 1060, 1166, 1282, 1411, 1552
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
248 };
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
249 static const int8_t index_deltas[8] = {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
250 -1, -1, -1, -1, 2, 5, 7, 9
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
251 };
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
252 uint16_t mag = sample & 7;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
253 int16_t delta = (((mag << 1) + 1) * mults[pcm->adpcm_mul_index]) >> 3;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
254 if (sample & 8) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
255 delta = -delta;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
256 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
257 uint8_t old_index = pcm->adpcm_mul_index;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
258 pcm->output += delta;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
259 if (pcm->adpcm_mul_index || mag > 3) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
260 pcm->adpcm_mul_index += index_deltas[mag];
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
261 if (pcm->adpcm_mul_index >= sizeof(mults)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
262 pcm->adpcm_mul_index = sizeof(mults) - 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
263 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
264 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
265 int16_t output = pcm->output;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
266 //Supposedly the YM2610 and YM2608 wrap around rather than clamp
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
267 //but since my tables have the wrong values I need to clamp
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
268 //in order to get something resembling the correct output
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
269 if (output > 0x7FF) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
270 pcm->output = 0x7FF;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
271 } else if (output < -0x800) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
272 pcm->output = -0x800;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
273 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
274 //printf("Sample %X, mag %X, delta %d, old index %d, new index %d, out %d\n", sample, mag, delta, old_index, pcm->adpcm_mul_index, (int16_t)pcm->output);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
275 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
276 if (pcm->output & 0x800) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
277 pcm->output |= 0xF000;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
278 } else {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
279 pcm->output &= 0x0FFF;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
280 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
281 if (pcm_fifo_free(pcm) > fifo_threshold) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
282 ymz->status |= pcm == &ymz->pcm[0] ? STATUS_FIF1 : STATUS_FIF2;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
283 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
284 } else {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
285 //Recording mode
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
286 //TODO: support recording actual audio input
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
287 if (pcm_fifo_write(pcm, nibbles, 0)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
288 ymz->status |= STATUS_OV;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
289 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
290 if (pcm_fifo_free(pcm) < fifo_threshold) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
291 ymz->status |= pcm == &ymz->pcm[0] ? STATUS_FIF1 : STATUS_FIF2;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
292 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
293 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
294
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
295 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
296
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
297 if (pcm->regs[0] & BIT_PLY_REC) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
298 //TODO: Volume
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
299 if (pcm->regs[0] & BIT_PAN_L) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
300 output[0] += pcm->output;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
301 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
302 if (pcm->regs[0] & BIT_PAN_R) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
303 output[1] += pcm->output;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
304 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
305 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
306 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
307
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
308 void ymz263b_run(ymz263b *ymz, uint32_t target_cycle)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
309 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
310 uint8_t timer_ctrl = ymz->base_regs[YMZ_TIMER_CTRL];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
311 for (; ymz->cycle < target_cycle; ymz->cycle += ymz->clock_inc)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
312 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
313 if (timer_ctrl & BIT_ST0) {
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
314 ymz->timers[0]--;
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
315 if (!ymz->timers[0]) {
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
316 ymz->timers[0] = ymz->base_regs[YMZ_TIMER0_HIGH] << 8 | ymz->base_regs[YMZ_TIMER0_LOW];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
317 ymz->status |= STATUS_T0;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
318 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
319 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
320 if (timer_ctrl & BIT_STBC) {
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
321 ymz->timers[3]--;
2467
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
322 ymz->timers[3] &= 0xFFF;
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
323 if (!ymz->timers[3]) {
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
324 ymz->timers[3] = ymz->base_regs[YMZ_TIMER1] << 8 & 0xF00;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
325 ymz->timers[3] |= ymz->base_regs[YMZ_TIMER_BASE];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
326
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
327 if (timer_ctrl & BIT_ST1) {
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
328 ymz->timers[1]--;
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
329 if (!ymz->timers[1]) {
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
330 ymz->timers[1] = ymz->base_regs[YMZ_TIMER1] >> 4;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
331 ymz->status |= STATUS_T1;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
332 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
333 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
334
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
335 if (timer_ctrl & BIT_ST2) {
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
336 ymz->timers[2]--;
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
337 if (!ymz->timers[2]) {
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
338 ymz->timers[2] = ymz->base_regs[YMZ_TIMER2_HIGH] << 8 | ymz->base_regs[YMZ_TIMER2_LOW];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
339 ymz->status |= STATUS_T2;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
340 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
341 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
342 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
343 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
344 if (!(ymz->midi_regs[0] & BIT_MTR_RST) && !fifo_empty(&ymz->midi_trs)) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
345 if (ymz->midi_transmit) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
346 --ymz->midi_transmit;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
347 } else {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
348 ymz->midi_transmit = MIDI_BYTE_DIVIDER - 1;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
349 //TODO: send this byte to MIDI device
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
350 uint8_t byte = fifo_read(&ymz->midi_trs);
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
351 printf("MIDI Transmit: %X\n", byte);
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
352 if (fifo_empty(&ymz->midi_trs)) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
353 ymz->status |= STATUS_TRQ;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
354 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
355 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
356 }
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
357 ymz->pcm_counter--;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
358 if (!ymz->pcm_counter) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
359 ymz->pcm_counter = PCM_BASE_DIVIDER;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
360 int16_t output[2] = {0, 0};
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
361 ymz263b_pcm_run(ymz, &ymz->pcm[0], output);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
362 ymz263b_pcm_run(ymz, &ymz->pcm[1], output);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
363 render_put_stereo_sample(ymz->audio, output[0], output[1]);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
364 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
365 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
366 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
367
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
368 static uint32_t predict_fifo_thres(ymz263b *ymz, ymz263b_pcm *pcm)
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
369 {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
370 if ((pcm->regs[0] & (BIT_ADP_RST|BIT_ADP_ST)) != BIT_ADP_ST) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
371 //PCM channel is either in reset or not started
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
372 return CYCLE_NEVER;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
373 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
374 uint32_t next_pcm_cycle = ymz->cycle + ymz->pcm_counter * ymz->clock_inc;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
375 next_pcm_cycle += (pcm->counter - 1) * PCM_BASE_DIVIDER * ymz->clock_inc;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
376 uint32_t fifo_free = pcm_fifo_free(pcm);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
377 //convert to nibbles
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
378 fifo_free <<= 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
379 uint32_t fifo_threshold = ((pcm->regs[3] >> 2 & 7) + 1) << 5;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
380 uint32_t diff;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
381 if (pcm->regs[0] & BIT_PLY_REC) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
382 if (pcm->nibble) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
383 fifo_free++;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
384 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
385 diff = fifo_threshold - fifo_free + 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
386 } else {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
387 if (pcm->nibble_write) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
388 fifo_free--;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
389 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
390 diff = fifo_free - fifo_threshold + 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
391 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
392 uint32_t nibbles_per_samp = (pcm->regs[3] >> 5 & 3) + 2;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
393 if (nibbles_per_samp > 4) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
394 //4-bit format is encoded as the highest value for some reason
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
395 nibbles_per_samp = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
396 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
397 uint8_t fs = pcm->regs[0] >> 3 & 3;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
398 if (!(pcm->regs[0] & BIT_PCM)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
399 //ADPCM can't use 44.1 kHz, but gains 5.5125 kHz
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
400 fs++;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
401 //see note in PCM playback code
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
402 nibbles_per_samp = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
403 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
404 diff /= nibbles_per_samp;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
405
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
406 next_pcm_cycle += diff * PCM_BASE_DIVIDER * pcm_dividers[fs] * ymz->clock_inc;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
407 return next_pcm_cycle;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
408 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
409
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
410 uint32_t ymz263b_next_int(ymz263b *ymz)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
411 {
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
412 //TODO: Handle MIDI receive interrupts
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
413 uint8_t enabled_ints = (~ymz->base_regs[YMZ_TIMER_CTRL]) & TIMER_INT_MASK;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
414 if (!(ymz->base_regs[YMZ_MIDI_CTRL] & (BIT_MTR_RST|BIT_MSK_TRQ))) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
415 enabled_ints |= STATUS_TRQ;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
416 }
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
417 if (!(ymz->pcm[0].regs[3] & BIT_MSK_FIF)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
418 enabled_ints |= STATUS_FIF1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
419 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
420 if (!(ymz->pcm[1].regs[3] & BIT_MSK_FIF)) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
421 enabled_ints |= STATUS_FIF2;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
422 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
423 if (!enabled_ints) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
424 return CYCLE_NEVER;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
425 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
426 //Handle currently pending interrupts
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
427 if (enabled_ints & ymz->status) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
428 return ymz->cycle;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
429 }
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
430
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
431 uint32_t ret = CYCLE_NEVER;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
432 if (enabled_ints & STATUS_TRQ) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
433 uint8_t bytes = fifo_size(&ymz->midi_trs);
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
434 if (bytes) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
435 ret = ymz->cycle + (ymz->midi_transmit + 1) * ymz->clock_inc;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
436 if (bytes > 1) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
437 ret += MIDI_BYTE_DIVIDER * ymz->clock_inc * (bytes - 1);
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
438 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
439 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
440 }
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
441
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
442 if (enabled_ints & STATUS_FIF1) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
443 uint32_t next_pcm = predict_fifo_thres(ymz, &ymz->pcm[0]);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
444 if (next_pcm < ret) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
445 ret = next_pcm;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
446 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
447 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
448 if (enabled_ints & STATUS_FIF2) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
449 uint32_t next_pcm = predict_fifo_thres(ymz, &ymz->pcm[1]);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
450 if (next_pcm < ret) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
451 ret = next_pcm;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
452 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
453 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
454
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
455 enabled_ints >>= 4;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
456 //If timers aren't already expired, interrupts can't fire unless the timers are enabled
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
457 enabled_ints &= ymz->base_regs[YMZ_TIMER_CTRL];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
458 if (!(ymz->base_regs[YMZ_TIMER_CTRL] & BIT_STBC)) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
459 //Timer 1 and Timer 2 depend on the base timer
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
460 enabled_ints &= 1;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
461 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
462 if (enabled_ints & BIT_ST0) {
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
463 uint32_t t0 = ymz->cycle + (ymz->timers[0] ? ymz->timers[0] : 0x10000) * ymz->clock_inc;
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
464 if (t0 < ret) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
465 ret = t0;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
466 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
467 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
468 if (enabled_ints & (BIT_ST1|BIT_ST2)) {
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
469 uint32_t base = ymz->cycle + (ymz->timers[3] ? ymz->timers[3] : 0x1000) * ymz->clock_inc;
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
470 if (base < ret) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
471 uint32_t load = (ymz->base_regs[YMZ_TIMER1] << 8 & 0xF00) | ymz->base_regs[YMZ_TIMER_BASE];
2461
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
472 if (!load) {
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
473 load = 0x1000;
a25e8f304343 Fix handling of zero timer value
Michael Pavone <pavone@retrodev.com>
parents: 2460
diff changeset
474 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
475 if (enabled_ints & BIT_ST1) {
2467
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
476 uint32_t t1 = base + (ymz->timers[1] ? ymz->timers[1] - 1 : 0xF) * load * ymz->clock_inc;
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
477 if (t1 < ret) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
478 ret = t1;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
479 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
480 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
481 if (enabled_ints & BIT_ST2) {
2467
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
482 uint32_t t2 = base + (ymz->timers[2] ? ymz->timers[2] - 1 : 0xFFFF) * load * ymz->clock_inc;
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
483 if (t2 < ret) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
484 ret = t2;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
485 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
486 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
487 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
488 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
489 return ret;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
490 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
491
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
492 void ymz263b_address_write(ymz263b *ymz, uint8_t value)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
493 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
494 ymz->address = value;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
495 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
496
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
497 void ymz263b_data_write(ymz263b *ymz, uint32_t channel, uint8_t value)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
498 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
499 if (channel) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
500 if (ymz->address >= YMZ_PCM_PLAY_CTRL && ymz->address < YMZ_MIDI_CTRL) {
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
501 if (ymz->address == YMZ_PCM_PLAY_CTRL) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
502 if (((value ^ ymz->pcm[1].regs[0]) & ymz->pcm[1].regs[0]) & BIT_ADP_RST) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
503 //Perform reset on falling edge of reset bit
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
504 ymz->pcm[1].counter = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
505 ymz->pcm[1].fifo_read = FIFO_EMPTY;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
506 ymz->pcm[1].nibble = ymz->pcm[1].nibble_write = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
507 ymz->pcm[1].output = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
508 ymz->pcm[1].adpcm_mul_index = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
509 }
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
510 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
511 ymz->pcm[1].regs[ymz->address - YMZ_PCM_PLAY_CTRL] = value;
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
512 if (ymz->address == YMZ_PCM_DATA) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
513 pcm_fifo_write(&ymz->pcm[1], 2, value);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
514 //Does an overflow here set the overflow statu sflag?
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
515 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
516 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
517 } else {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
518 if (ymz->address < YMZ_PCM_PLAY_CTRL) {
2467
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
519 if (ymz->address == YMZ_TIMER_CTRL) {
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
520 uint8_t newly_set = (ymz->base_regs[ymz->address] ^ value) & value;
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
521 if (newly_set & BIT_ST0) {
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
522 ymz->timers[0] = ymz->base_regs[YMZ_TIMER0_HIGH] << 8 | ymz->base_regs[YMZ_TIMER0_LOW];
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
523 }
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
524 if (newly_set & BIT_ST1) {
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
525 ymz->timers[1] = ymz->base_regs[YMZ_TIMER1] >> 4;
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
526 }
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
527 if (newly_set & BIT_ST2) {
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
528 ymz->timers[2] = ymz->base_regs[YMZ_TIMER2_HIGH] << 8 | ymz->base_regs[YMZ_TIMER2_LOW];
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
529 }
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
530 if (newly_set & BIT_STBC) {
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
531 ymz->timers[0] = (ymz->base_regs[YMZ_TIMER1] << 8 | ymz->base_regs[YMZ_TIMER_BASE]) & 0xFFF;
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
532 }
bf8a77a8ddc4 Fix some YMZ263B timer issues
Michael Pavone <pavone@retrodev.com>
parents: 2466
diff changeset
533 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
534 ymz->base_regs[ymz->address] = value;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
535 } else if (ymz->address < YMZ_MIDI_CTRL) {
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
536 if (((value ^ ymz->pcm[0].regs[0]) & ymz->pcm[1].regs[0]) & BIT_ADP_RST) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
537 //Perform reset on falling edge of reset bit
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
538 ymz->pcm[0].counter = 1;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
539 ymz->pcm[0].fifo_read = FIFO_EMPTY;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
540 ymz->pcm[0].nibble = ymz->pcm[1].nibble_write = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
541 ymz->pcm[0].output = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
542 ymz->pcm[0].adpcm_mul_index = 0;
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
543 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
544 ymz->pcm[0].regs[ymz->address - YMZ_PCM_PLAY_CTRL] = value;
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
545 if (ymz->address == YMZ_PCM_DATA) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
546 pcm_fifo_write(&ymz->pcm[0], 2, value);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
547 //Does an overflow here set the overflow statu sflag?
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
548 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
549 } else {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
550 ymz->midi_regs[ymz->address - YMZ_MIDI_CTRL] = value;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
551 if (ymz->address == YMZ_MIDI_DATA) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
552 ymz->status &= ~STATUS_TRQ;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
553 if (fifo_empty(&ymz->midi_trs)) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
554 ymz->midi_transmit = MIDI_BYTE_DIVIDER - 1;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
555 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
556 fifo_write(&ymz->midi_trs, value);
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
557 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
558 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
559 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
560 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
561
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
562 uint8_t ymz263b_data_read(ymz263b *ymz, uint32_t channel)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
563 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
564 //TODO: Supposedly only a few registers are actually readable
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
565 if (channel) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
566 if (ymz->address >= YMZ_PCM_PLAY_CTRL && ymz->address < YMZ_MIDI_CTRL) {
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
567 if (ymz->address == YMZ_PCM_DATA) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
568 return pcm_fifo_read(&ymz->pcm[1], 2);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
569 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
570 return ymz->pcm[1].regs[ymz->address - YMZ_PCM_PLAY_CTRL];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
571 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
572 } else {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
573 if (ymz->address < YMZ_PCM_PLAY_CTRL) {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
574 return ymz->base_regs[ymz->address];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
575 } else if (ymz->address < YMZ_MIDI_CTRL) {
2466
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
576 if (ymz->address == YMZ_PCM_DATA) {
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
577 return pcm_fifo_read(&ymz->pcm[0], 2);
b5640ac9aea9 Initial stab at PCM/ADPCM support in YMZ263B emulation
Michael Pavone <pavone@retrodev.com>
parents: 2461
diff changeset
578 }
2460
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
579 return ymz->pcm[0].regs[ymz->address - YMZ_PCM_PLAY_CTRL];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
580 } else {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
581 return ymz->midi_regs[ymz->address - YMZ_MIDI_CTRL];
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
582 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
583 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
584 return 0XFF;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
585 }
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
586
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
587 uint8_t ymz263b_status_read(ymz263b *ymz)
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
588 {
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
589 uint8_t ret = ymz->status;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
590 ymz->status = 0;//&= ~(STATUS_T0|STATUS_T1|STATUS_T2);
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
591 return ret;
a4f8fa24764b Initial work on emulating the YMZ263B in the Copera
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
592 }