-
Notifications
You must be signed in to change notification settings - Fork 1
/
scd_pcm.c
240 lines (208 loc) · 6.37 KB
/
scd_pcm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "scd_pcm.h"
typedef struct
{
uint32_t cmd;
uint16_t arg[6];
} scd_cmd_t;
#define MAX_SCD_CMDS 16
extern void write_byte(unsigned int dst, unsigned char val);
extern void write_word(unsigned int dst, unsigned short val);
extern void write_long(unsigned int dst, unsigned int val);
extern unsigned char read_byte(unsigned int src);
extern unsigned short read_word(unsigned int src);
extern unsigned int read_long(unsigned int src);
static scd_cmd_t scd_cmds[MAX_SCD_CMDS];
static int16_t num_scd_cmds;
static void scd_delay(void) SCD_CODE_ATTR;
static char wait_cmd_ack(void) SCD_CODE_ATTR;
static void wait_do_cmd(char cmd) SCD_CODE_ATTR;
static char wait_cmd_ack(void)
{
char ack = 0;
do {
scd_delay();
ack = read_byte(0xA1200F); // wait for acknowledge byte in sub comm port
} while (!ack);
return ack;
}
static void wait_do_cmd(char cmd)
{
while (read_byte(0xA1200F)) {
scd_delay(); // wait until Sub-CPU is ready to receive command
}
write_byte(0xA1200E, cmd); // set main comm port to command
}
void scd_init_pcm(void)
{
/*
* Initialize the PCM driver
*/
wait_do_cmd('I');
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
}
void scd_upload_buf(uint16_t buf_id, const uint8_t *data, uint32_t data_len)
{
uint8_t *scdWordRam = (uint8_t *)0x600000;
memcpy(scdWordRam, data, data_len);
write_word(0xA12010, buf_id); /* buf_id */
write_long(0xA12014, 0x0C0000); /* word ram on CD side (in 1M mode) */
write_long(0xA12018, data_len); /* sample length */
wait_do_cmd('B'); // SfxCopyBuffer command
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
}
uint8_t scd_play_src(uint8_t src_id, uint16_t buf_id, uint16_t freq, uint8_t pan, uint8_t vol, uint8_t autoloop)
{
write_long(0xA12010, ((unsigned)src_id<<16)|buf_id); /* src|buf_id */
write_long(0xA12014, ((unsigned)freq<<16)|pan); /* freq|pan */
write_long(0xA12018, ((unsigned)vol<<16)|autoloop); /* vol|autoloop */
wait_do_cmd('A'); // SfxPlaySource command
wait_cmd_ack();
src_id = read_byte(0xA12020);
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
return src_id;
}
uint8_t scd_punpause_src(uint8_t src_id, uint8_t paused)
{
write_long(0xA12010, ((unsigned)src_id<<16)|paused); /* src|paused */
wait_do_cmd('N'); // SfxPUnPSource command
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
return src_id;
}
void scd_update_src(uint8_t src_id, uint16_t freq, uint8_t pan, uint8_t vol, uint8_t autoloop)
{
write_long(0xA12010, ((unsigned)src_id<<16)); /* src|0 */
write_long(0xA12014, ((unsigned)freq<<16)|pan); /* freq|pan */
write_long(0xA12018, ((unsigned)vol<<16)|autoloop); /* vol|autoloop */
wait_do_cmd('U'); // SfxUpdateSource command
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
}
uint16_t scd_getpos_for_src(uint8_t src_id)
{
uint16_t pos;
write_long(0xA12010, src_id<<16);
wait_do_cmd('G'); // SfxGetSourcePosition command
wait_cmd_ack();
pos = read_word(0xA12020);
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
return pos;
}
void scd_stop_src(uint8_t src_id)
{
write_long(0xA12010, ((unsigned)src_id<<16)); /* src|0 */
wait_do_cmd('O'); // SfxStopSource command
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
}
void scd_rewind_src(uint8_t src_id)
{
write_long(0xA12010, ((unsigned)src_id<<16)); /* src|0 */
wait_do_cmd('W'); // SfxRewindSource command
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
}
void scd_clear_pcm(void)
{
wait_do_cmd('L'); // SfxClear command
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
}
int scd_get_playback_status(void)
{
return read_byte(0xA1202F);
}
uint8_t scd_queue_play_src(uint8_t src_id, uint16_t buf_id, uint16_t freq, uint8_t pan, uint8_t vol, uint8_t autoloop)
{
scd_cmd_t *cmd = scd_cmds + num_scd_cmds;
if (num_scd_cmds >= MAX_SCD_CMDS)
return 0;
cmd->cmd = 'A';
cmd->arg[0] = src_id;
cmd->arg[1] = buf_id;
cmd->arg[2] = freq;
cmd->arg[3] = pan;
cmd->arg[4] = vol;
cmd->arg[5] = autoloop;
num_scd_cmds++;
return 0;
}
void scd_queue_update_src(uint8_t src_id, uint16_t freq, uint8_t pan, uint8_t vol, uint8_t autoloop)
{
scd_cmd_t *cmd = scd_cmds + num_scd_cmds;
if (num_scd_cmds >= MAX_SCD_CMDS)
return;
cmd->cmd = 'U';
cmd->arg[0] = src_id;
cmd->arg[1] = freq;
cmd->arg[2] = pan;
cmd->arg[3] = vol;
cmd->arg[4] = autoloop;
num_scd_cmds++;
}
void scd_queue_stop_src(uint8_t src_id)
{
scd_cmd_t *cmd = scd_cmds + num_scd_cmds;
if (num_scd_cmds >= MAX_SCD_CMDS)
return;
cmd->cmd = 'S';
cmd->arg[0] = src_id;
num_scd_cmds++;
}
void scd_queue_clear_pcm(void)
{
scd_cmd_t *cmd = scd_cmds + num_scd_cmds;
if (num_scd_cmds >= MAX_SCD_CMDS)
return;
cmd->cmd = 'L';
num_scd_cmds++;
}
int scd_flush_cmd_queue(void)
{
int i;
scd_cmd_t *cmd;
if (!num_scd_cmds) {
return 0;
}
write_byte(0xA12010, 1);
wait_do_cmd('E'); // suspend the mixer/decoder
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
for (i = 0, cmd = scd_cmds; i < num_scd_cmds; i++, cmd++) {
switch (cmd->cmd) {
case 'A':
scd_play_src(cmd->arg[0], cmd->arg[1], cmd->arg[2], cmd->arg[3], cmd->arg[4], cmd->arg[5]);
break;
case 'U':
scd_update_src(cmd->arg[0], cmd->arg[1], cmd->arg[2], cmd->arg[3], cmd->arg[4]);
break;
case 'S':
scd_stop_src(cmd->arg[0]);
break;
case 'L':
scd_clear_pcm();
break;
default:
break;
}
}
write_byte(0xA12010, 0);
wait_do_cmd('E'); // unsuspend
wait_cmd_ack();
write_byte(0xA1200E, 0x00); // acknowledge receipt of command result
num_scd_cmds = 0;
return i;
}
static void scd_delay(void)
{
int cnt = 5;
do {
asm __volatile("nop");
} while (--cnt);
}