annotate rf5c164.c @ 2208:3809a0bd680e

Fix a couple of Z80 debugger issues
author Michael Pavone <pavone@retrodev.com>
date Sat, 27 Aug 2022 22:02:09 -0700
parents 4fbe1e7c4a73
children 0d1d5dccdd28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2082
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <stdio.h>
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "rf5c164.h"
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 enum {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 ENV,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 PAN,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 FDL,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 FDH,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 LSL,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 LSH,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 ST,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 CTRL,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 CHAN_ENABLE
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 };
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 enum {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 START,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 NORMAL,
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 LOOP
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 };
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 #define FLAG_SOUNDING 0x80
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 #define FLAG_PENDING 0x01
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 void rf5c164_init(rf5c164* pcm, uint32_t mclks, uint32_t divider)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 pcm->audio = render_audio_source("rf5c164", mclks, divider * 384, 2);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 pcm->clock_step = divider * 4;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30
2164
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2145
diff changeset
31 void rf5c164_deinit(rf5c164* pcm)
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2145
diff changeset
32 {
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2145
diff changeset
33 render_free_source(pcm->audio);
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2145
diff changeset
34 }
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2145
diff changeset
35
2082
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 //48 cycles per channel
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 //1 external write per 16 cycles
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 //3 external writes per channel/sample
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 // TCE pulse width is 200ns @ 10Mhz aka 2 cycles
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 // total cycle is longer than TCE pulse, guessing 4 cycles
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 // 12 memory access slots per channel sample
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 // 3 for external memory writes
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 // 1 for internal register writes?
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 // 1 for refresh?
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 // 6 for register reads?
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 // or maybe 7 for register reads and writes happen when register is read?
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 #define CHECK pcm->cycle += pcm->clock_step; pcm->step++; if (pcm->cycle >= cycle) break
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 #define CHECK_LOOP pcm->cycle += pcm->clock_step; pcm->step = 0; if (pcm->cycle >= cycle) break
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 static void write_if_not_sounding(rf5c164* pcm)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 if (!(pcm->flags & FLAG_SOUNDING) && (pcm->flags & FLAG_PENDING)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 && pcm->pending_address >= 0x1000) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 pcm->ram[(pcm->pending_address & 0xFFF) | pcm->ram_bank] = pcm->pending_byte;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 pcm->flags &= ~FLAG_PENDING;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 static void write_always(rf5c164* pcm)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 if ((pcm->flags & FLAG_PENDING) && pcm->pending_address >= 0x1000) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 pcm->ram[(pcm->pending_address & 0xFFF) | pcm->ram_bank] = pcm->pending_byte;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 pcm->flags &= ~FLAG_PENDING;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 void rf5c164_run(rf5c164* pcm, uint32_t cycle)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 //TODO: Timing of this is all educated guesses based on documentation, do some real measurements
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 while (pcm->cycle < cycle)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 switch (pcm->step)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 case 0:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 //handle internal memory write
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 // RF5C164 datasheet seems to suggest only 1 "internal memory" write every 48 cycles when the chip is not sounding (globally)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 // and 1 write every 384 cycles when the chip is sounding (globally). However, games seem to expect to be able to write faster than that
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 // RF5C68 datasheet suggests internal memory writes are unrestricted regardless of sounding status
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 /*if ((pcm->cur_channel == pcm->selected_channel || !(pcm->flags & FLAG_SOUNDING)) && (pcm->flags & FLAG_PENDING)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 && pcm->pending_address <= ST
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 ) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 printf("pcm_write commit chan %d, %X - %X @ %u\n", pcm->selected_channel, pcm->pending_address, pcm->pending_byte, pcm->cycle);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 pcm->channels[pcm->selected_channel].regs[pcm->pending_address] = pcm->pending_byte;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 pcm->flags &= ~FLAG_PENDING;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 }*/
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 case 1:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 if ((pcm->flags & FLAG_SOUNDING) && !(pcm->channel_enable & (1 << pcm->cur_channel))) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 if (pcm->channels[pcm->cur_channel].state == START) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 pcm->channels[pcm->cur_channel].cur_ptr = pcm->channels[pcm->cur_channel].regs[ST] << 19;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 //printf("chan %d START %X (%X raw)\n", pcm->cur_channel, pcm->channels[pcm->cur_channel].cur_ptr >> 11, pcm->channels[pcm->cur_channel].cur_ptr);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 pcm->channels[pcm->cur_channel].state = NORMAL;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 } else if (pcm->channels[pcm->cur_channel].state == LOOP) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 pcm->channels[pcm->cur_channel].cur_ptr = (pcm->channels[pcm->cur_channel].regs[LSH] << 19) | (pcm->channels[pcm->cur_channel].regs[LSL] << 11);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 //printf("chan %d LOOP %X (%X raw)\n", pcm->cur_channel, pcm->channels[pcm->cur_channel].cur_ptr >> 11, pcm->channels[pcm->cur_channel].cur_ptr);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 pcm->channels[pcm->cur_channel].state = NORMAL;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 case 2: {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 if ((pcm->flags & FLAG_SOUNDING) && !(pcm->channel_enable & (1 << pcm->cur_channel))) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 uint8_t byte = pcm->ram[pcm->channels[pcm->cur_channel].cur_ptr >> 11];
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 if (byte == 0xFF) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 pcm->channels[pcm->cur_channel].state = LOOP;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 } else {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 pcm->channels[pcm->cur_channel].sample = byte;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 case 3:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 write_always(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 case 4:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 if ((pcm->flags & FLAG_SOUNDING) && !(pcm->channel_enable & (1 << pcm->cur_channel))) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 pcm->channels[pcm->cur_channel].cur_ptr += (pcm->channels[pcm->cur_channel].regs[FDH] << 8) | pcm->channels[pcm->cur_channel].regs[FDL];
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 pcm->channels[pcm->cur_channel].cur_ptr &= 0x7FFFFFF;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 case 5:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 case 6:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 case 7:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 write_always(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 case 8:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
136 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 case 9:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138 if ((pcm->flags & FLAG_SOUNDING) && !(pcm->channel_enable & (1 << pcm->cur_channel))) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 int16_t sample = pcm->channels[pcm->cur_channel].sample & 0x7F;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 if (!(pcm->channels[pcm->cur_channel].sample & 0x80)) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 sample = -sample;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143 sample *= pcm->channels[pcm->cur_channel].regs[ENV];
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
144 int16_t left = (sample * (pcm->channels[pcm->cur_channel].regs[PAN] >> 4)) >> 5;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 int16_t right = (sample * (pcm->channels[pcm->cur_channel].regs[PAN] & 0xF)) >> 5;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 //printf("chan %d, raw %X, sample %d, left %d, right %d, ptr %X (raw %X)\n", pcm->cur_channel, pcm->channels[pcm->cur_channel].sample, sample, left, right, pcm->channels[pcm->cur_channel].cur_ptr >> 11, pcm->channels[pcm->cur_channel].cur_ptr);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 pcm->left += left;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
148 pcm->right += right;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150 write_if_not_sounding(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 case 10:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 //refresh?
2128
b0dcf5c9f353 Fix some issues with PCM dma/CPU write conflicts
Michael Pavone <pavone@retrodev.com>
parents: 2082
diff changeset
154 //does refresh happen at the same rate when sounding disabled? warning in sega docs suggests maybe not
b0dcf5c9f353 Fix some issues with PCM dma/CPU write conflicts
Michael Pavone <pavone@retrodev.com>
parents: 2082
diff changeset
155 write_if_not_sounding(pcm);
2082
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156 CHECK;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 case 11:
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 write_always(pcm);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159 pcm->cur_channel++;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 pcm->cur_channel &= 7;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 if (!pcm->cur_channel) {
2145
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
162 if (pcm->left > INT16_MAX) {
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
163 pcm->left = INT16_MAX;
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
164 } else if (pcm->left < INT16_MIN) {
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
165 pcm->left = INT16_MIN;
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
166 }
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
167 if (pcm->right > INT16_MAX) {
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
168 pcm->right = INT16_MAX;
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
169 } else if (pcm->right < INT16_MIN) {
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
170 pcm->right = INT16_MIN;
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
171 }
2082
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 render_put_stereo_sample(pcm->audio, pcm->left, pcm->right);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 pcm->left = pcm->right = 0;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175 CHECK_LOOP;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
176 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
177 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
178 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
179
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
180 void rf5c164_write(rf5c164* pcm, uint16_t address, uint8_t value)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
181 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
182 //printf("pcm_write %X - %X @ %u\n", address, value, pcm->cycle);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 if (address == CTRL) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 pcm->flags &= ~FLAG_SOUNDING;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
185 pcm->flags |= value & FLAG_SOUNDING;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
186 if (value & 0x40) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
187 pcm->selected_channel = value & 0x7;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
188 //printf("selected channel %d\n", pcm->selected_channel);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
189 } else {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 pcm->ram_bank = value << 12 & 0xF000;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191 //printf("selected RAM bank %X\n", pcm->ram_bank);
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 if (!(pcm->flags & FLAG_SOUNDING)) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 pcm->left = pcm->right = 0;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 } else if (address == CHAN_ENABLE) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 uint8_t changed = pcm->channel_enable ^ value;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
198 pcm->channel_enable = value;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 for (int i = 0; i < 8; i++)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
201 int mask = 1 << i;
2145
62a53c052d9b PCM channel add should saturate on overflow, not wrap around
Michael Pavone <pavone@retrodev.com>
parents: 2128
diff changeset
202 if ((changed & mask) && !(mask & value)) {
2082
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203 pcm->channels[i].state = START;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 } else if (address <= ST) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 //See note in first step of rf5c164_run
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
208 pcm->channels[pcm->selected_channel].regs[address] = value;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 } else {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 pcm->pending_address = address;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211 pcm->pending_byte = value;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 pcm->flags |= FLAG_PENDING;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
215
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
216 uint8_t rf5c164_read(rf5c164* pcm, uint16_t address)
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
217 {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 if (address >= 0x10 && address < 0x20) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
219 uint16_t chan = address >> 1 & 0x7;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
220 if (address & 1) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
221 return pcm->channels[chan].cur_ptr >> 19;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
222 } else {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
223 return pcm->channels[chan].cur_ptr >> 11;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
224 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
225 } else if (address >= 0x1000 && !(pcm->flags & FLAG_SOUNDING)) {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
226 return pcm->ram[pcm->ram_bank | (address & 0xFFF)];
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
227 } else {
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
228 return 0xFF;
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
229 }
485834c0fea7 Forgot to add PCM source files
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
230 }