Debian Codegraph @

BambooTracker/chip/register_write_logger.cpp

raw
1 /*
2 * Copyright (C) 2018-2020 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "register_write_logger.hpp"
27 #include "io/export_io.hpp"
28 #include <algorithm>
29 #include <iterator>
30
31 namespace chip
32 {
33 AbstractRegisterWriteLogger::AbstractRegisterWriteLogger(int target)
34 : target_(target),
35 lastWait_(0),
36 isSetLoop_(false),
37 loopPoint_(0),
38 totalSampCnt_(0)
39 {
40 }
41
42 void AbstractRegisterWriteLogger::elapse(size_t count) noexcept
43 {
44 lastWait_ += count;
45 totalSampCnt_ += count;
46 }
47
48 bool AbstractRegisterWriteLogger::empty() const noexcept
49 {
50 return (buf_.empty() || lastWait_ != 0);
51 }
52
53 void AbstractRegisterWriteLogger::clear() noexcept
54 {
55 buf_.clear();
56 lastWait_ = 0;
57 totalSampCnt_ = 0;
58 isSetLoop_ = false;
59 loopPoint_ = 0;
60 }
61
62 std::vector<uint8_t> AbstractRegisterWriteLogger::getData()
63 {
64 if (lastWait_) setWait();
65 return buf_;
66 }
67
68 size_t AbstractRegisterWriteLogger::getSampleLength() const noexcept
69 {
70 return static_cast<size_t>(totalSampCnt_);
71 }
72
73 size_t AbstractRegisterWriteLogger::setLoopPoint()
74 {
75 if (lastWait_) setWait();
76 isSetLoop_ = true;
77 return loopPoint_;
78 }
79
80 size_t AbstractRegisterWriteLogger::forceMoveLoopPoint() noexcept
81 {
82 loopPoint_ = buf_.size();
83 return loopPoint_;
84 }
85
86 //******************************//
87 VgmLogger::VgmLogger(int target, uint32_t intrRate)
88 : AbstractRegisterWriteLogger(target), intrRate_(intrRate) {}
89
90 void VgmLogger::recordRegisterChange(uint32_t offset, uint8_t value)
91 {
92 if (lastWait_) setWait();
93
94 const int fm = target_ & io::Export_FmMask;
95 const int ssg = target_ & io::Export_SsgMask;
96
97 const uint8_t cmdSsg =
98 (ssg != io::Export_InternalSsg) ? 0xa0
99 : (fm == io::Export_YM2608) ? 0x56
100 : (fm == io::Export_YM2203) ? 0x55
101 : (fm == io::Export_YM2610B) ? 0x58
102 : 0x00;
103 const uint8_t cmdFmPortA =
104 (fm == io::Export_YM2608) ? 0x56
105 : (fm == io::Export_YM2612) ? 0x52
106 : (fm == io::Export_YM2203) ? 0x55
107 : (fm == io::Export_YM2610B) ? 0x58
108 : 0x00;
109 const uint8_t cmdFmPortB =
110 (fm == io::Export_YM2608) ? 0x57
111 : (fm == io::Export_YM2612) ? 0x53
112 : (fm == io::Export_YM2610B) ? 0x59
113 : 0x00;
114
115 if (cmdSsg && offset < 0x10) {
116 buf_.push_back(cmdSsg);
117 buf_.push_back(static_cast<uint8_t>(offset));
118 buf_.push_back(value);
119 }
120 else if (cmdFmPortA && (offset & 0x100) == 0) {
121 bool compatible = true;
122
123 if (offset == 0x28) { // Key register
124 if (fm == io::Export_YM2203 && (value & 7) >= 3)
125 compatible = false;
126 }
127 else if (offset == 0x29) // Mode register
128 compatible = fm == io::Export_YM2608;
129 else if ((offset & 0xf0) == 0x10) // Rhythm section
130 compatible = fm == io::Export_YM2608;
131
132 if (compatible) {
133 buf_.push_back(cmdFmPortA);
134 buf_.push_back(offset & 0xff);
135 buf_.push_back(value);
136 }
137 }
138 else if (cmdFmPortB && (offset & 0x100) != 0) {
139 bool compatible = true;
140
141 if (offset < 0x10) // ADPCM section
142 compatible = fm == io::Export_YM2608;
143
144 if (compatible) {
145 buf_.push_back(cmdFmPortB);
146 buf_.push_back(offset & 0xff);
147 buf_.push_back(value);
148 }
149 }
150 }
151
152 void VgmLogger::setDataBlock(std::vector<uint8_t> data)
153 {
154 buf_.push_back(0x67);
155 buf_.push_back(0x66);
156 buf_.push_back(0x81);
157 size_t blockSize = data.size() + 8;
158 buf_.push_back(blockSize & 0xff);
159 buf_.push_back((blockSize >> 8) & 0xff);
160 buf_.push_back((blockSize >> 16) & 0xff);
161 buf_.push_back(blockSize >> 24);
162 buf_.push_back(data.size() & 0xff);
163 buf_.push_back((data.size() >> 8) & 0xff);
164 buf_.push_back((data.size() >> 16) & 0xff);
165 buf_.push_back(data.size() >> 24);
166 buf_.resize(buf_.size() + 4); // Start address is 0
167 std::copy(data.begin(), data.end(), std::back_inserter(buf_));
168 }
169
170 void VgmLogger::setWait()
171 {
172 while (lastWait_) {
173 uint32_t sub;
174
175 if (intrRate_ == 50) {
176 if (lastWait_ > 65535) {
177 uint32_t tmp = static_cast<uint32_t>(lastWait_ - 65535);
178 if (tmp <= 882) {
179 //65535 - (882 - tmp)
180 sub = 64653 + tmp;
181 }
182 else if (tmp <= 1764) {
183 //65535 - (1764 - tmp)
184 sub = 63771 + tmp;
185 }
186 else if (tmp <= 2646) {
187 //65535 - (2646 - tmp)
188 sub = 62889 + tmp;
189 }
190 else {
191 sub = 65535;
192 }
193 buf_.push_back(0x61);
194 buf_.push_back(sub & 0x00ff);
195 buf_.push_back(static_cast<uint8_t>(sub >> 8));
196 }
197 else {
198 if (lastWait_ <= 16) {
199 buf_.push_back(static_cast<uint8_t>(0x70 | (lastWait_ - 1)));
200 }
201 else if (lastWait_ > 2646) {
202 buf_.push_back(0x61);
203 buf_.push_back(lastWait_ & 0x00ff);
204 buf_.push_back(static_cast<uint8_t>(lastWait_ >> 8));
205 }
206 else if (lastWait_ == 2646) {
207 buf_.push_back(0x63);
208 buf_.push_back(0x63);
209 buf_.push_back(0x63);
210 }
211 else if (1764 <= lastWait_ && lastWait_ <= 1780) {
212 uint32_t tmp = static_cast<uint32_t>(lastWait_ - 1764);
213 buf_.push_back(0x63);
214 buf_.push_back(0x63);
215 if (tmp) buf_.push_back(0x70 | (tmp - 1));
216 }
217 else if (882 <= lastWait_ && lastWait_ <= 898) {
218 uint32_t tmp = static_cast<uint32_t>(lastWait_ - 882);
219 buf_.push_back(0x63);
220 if (tmp) buf_.push_back(0x70 | (tmp - 1));
221 }
222 else {
223 buf_.push_back(0x61);
224 buf_.push_back(lastWait_ & 0x00ff);
225 buf_.push_back(static_cast<uint8_t>(lastWait_ >> 8));
226 }
227 sub = static_cast<uint32_t>(lastWait_);
228 }
229 }
230 else if (intrRate_ == 60) {
231 if (lastWait_ > 65535) {
232 uint32_t tmp = static_cast<uint32_t>(lastWait_ - 65535);
233 if (tmp <= 735) {
234 //65535 - (735 - tmp)
235 sub = 64800 + tmp;
236 }
237 else if (tmp <= 1470) {
238 //65535 - (1470 - tmp)
239 sub = 64065 + tmp;
240 }
241 else if (tmp <= 2205) {
242 //65535 - (2205 - tmp)
243 sub = 63330 + tmp;
244 }
245 else {
246 sub = 65535;
247 }
248 buf_.push_back(0x61);
249 buf_.push_back(sub & 0x00ff);
250 buf_.push_back(sub >> 8);
251 }
252 else {
253 if (lastWait_ <= 16) {
254 buf_.push_back(static_cast<uint8_t>(0x70 | (lastWait_ - 1)));
255 }
256 else if (lastWait_ > 2205) {
257 buf_.push_back(0x61);
258 buf_.push_back(lastWait_ & 0x00ff);
259 buf_.push_back(static_cast<uint8_t>(lastWait_ >> 8));
260 }
261 else if (lastWait_ == 2205) {
262 buf_.push_back(0x62);
263 buf_.push_back(0x62);
264 buf_.push_back(0x62);
265 }
266 else if (1470 <= lastWait_ && lastWait_ <= 1486) {
267 uint32_t tmp = static_cast<uint32_t>(lastWait_ - 1470);
268 buf_.push_back(0x62);
269 buf_.push_back(0x62);
270 if (tmp) buf_.push_back(0x70 | (tmp - 1));
271 }
272 else if (735 <= lastWait_ && lastWait_ <= 751) {
273 uint32_t tmp = static_cast<uint32_t>(lastWait_ - 735);
274 buf_.push_back(0x62);
275 if (tmp) buf_.push_back(0x70 | (tmp - 1));
276 }
277 else {
278 buf_.push_back(0x61);
279 buf_.push_back(lastWait_ & 0x00ff);
280 buf_.push_back(static_cast<uint8_t>(lastWait_ >> 8));
281 }
282 sub = static_cast<uint32_t>(lastWait_);
283 }
284 }
285 else {
286 if (lastWait_ > 65535) {
287 sub = 65535;
288 buf_.push_back(0x61);
289 buf_.push_back(sub & 0x00ff);
290 buf_.push_back(sub >> 8);
291 }
292 else {
293 buf_.push_back(0x61);
294 buf_.push_back(lastWait_ & 0x00ff);
295 buf_.push_back(static_cast<uint8_t>(lastWait_ >> 8));
296 }
297 sub = static_cast<uint32_t>(lastWait_);
298 }
299
300 lastWait_ -= sub;
301 }
302
303 if (!isSetLoop_) loopPoint_ = buf_.size();
304 }
305
306 //******************************//
307 S98Logger::S98Logger(int target) : AbstractRegisterWriteLogger(target) {}
308
309 void S98Logger::recordRegisterChange(uint32_t offset, uint8_t value)
310 {
311 if (lastWait_) setWait();
312
313 const int fm = target_ & io::Export_FmMask;
314 const int ssg = target_ & io::Export_SsgMask;
315
316 const uint8_t cmdSsg =
317 (ssg != io::Export_InternalSsg) ? (fm == io::Export_NoneFm) ? 0x01 : 0x02
318 : (fm == io::Export_YM2608) ? 0x00
319 : (fm == io::Export_YM2203) ? 0x00
320 : 0xff;
321 const uint8_t cmdFmPortA =
322 (fm != io::Export_NoneFm) ? 0x00 : 0xff;
323 const uint8_t cmdFmPortB =
324 (fm == io::Export_YM2608 || fm == io::Export_YM2612) ? 0x01 : 0xff;
325
326 if (cmdSsg != 0xff && offset < 0x10) {
327 buf_.push_back(cmdSsg);
328 buf_.push_back(offset);
329 buf_.push_back(value);
330 }
331 else if (cmdFmPortA != 0xff && (offset & 0x100) == 0) {
332 bool compatible = true;
333
334 if (offset == 0x28) { // Key register
335 if (fm == io::Export_YM2203 && (value & 7) >= 3)
336 compatible = false;
337 }
338 else if (offset == 0x29) // Mode register
339 compatible = fm == io::Export_YM2608;
340 else if ((offset & 0xf0) == 0x10) // Rhythm section
341 compatible = fm == io::Export_YM2608;
342
343 if (compatible) {
344 buf_.push_back(cmdFmPortA);
345 buf_.push_back(offset & 0xff);
346 buf_.push_back(value);
347 }
348 }
349 else if (cmdFmPortB != 0xff && (offset & 0x100) != 0) {
350 bool compatible = true;
351
352 if (offset < 0x10) // ADPCM section
353 compatible = fm == io::Export_YM2608;
354
355 if (compatible) {
356 buf_.push_back(cmdFmPortB);
357 buf_.push_back(offset & 0xff);
358 buf_.push_back(value);
359 }
360 }
361 }
362
363 void S98Logger::setWait()
364 {
365 if (lastWait_ == 1) {
366 buf_.push_back(0xff);
367 }
368 else {
369 buf_.push_back(0xfe);
370 lastWait_ -= 2;
371 do {
372 uint8_t b = lastWait_ & 0x7f;
373 lastWait_ >>= 7;
374 if (lastWait_ > 0) b |= 0x80;
375 buf_.push_back(b);
376 } while (lastWait_ > 0);
377 }
378 if (!isSetLoop_) loopPoint_ = buf_.size();
379 lastWait_ = 0;
380 }
381 }