| 1 |
// Based on ymf278b.c written by R. Belmont and O. Galibert |
| 2 |
|
| 3 |
// Improved by Valley Bell, 2018 |
| 4 |
// Thanks to niekniek and l_oliveira for providing recordings from OPL4 hardware. |
| 5 |
// Thanks to superctr and wouterv for discussing changes. |
| 6 |
// |
| 7 |
// Improvements: |
| 8 |
// - added TL interpolation, recordings show that internal TL levels are 0x00..0xff |
| 9 |
// - fixed ADSR speeds, attack rate 15 is now instant |
| 10 |
// - correct clamping of intermediate Rate Correction values |
| 11 |
// - emulation of "loop glitch" (going out-of-bounds by playing a sample faster than it the loop is long) |
| 12 |
// - made calculation of sample position cleaner and closer to how the HW works |
| 13 |
// - increased output resolution from TL (0.375dB) to envelope (0.09375dB) |
| 14 |
// - fixed volume table -6dB steps are done using bit shifts, steps in between are multiplicators |
| 15 |
// - made octave -8 freeze the sample |
| 16 |
// - verified that TL and envelope levels are applied separately, both go silent at -60dB |
| 17 |
// - implemented pseudo-reverb and damping according to manual |
| 18 |
// - made pseudo-reverb ignore Rate Correction (real hardware ignores it) |
| 19 |
// - reimplemented LFO, speed exactly matches the formulas that were probably used when creating the manual |
| 20 |
// - fixed LFO (tremolo) amplitude modulation |
| 21 |
// - made LFO vibrato and tremolo accurate to hardware |
| 22 |
// |
| 23 |
// Known issues: |
| 24 |
// - Octave -8 was only tested with fnum 0. Other fnum values might behave differently. |
| 25 |
|
| 26 |
// This class doesn't model a full YMF278b chip. Instead it only models the |
| 27 |
// wave part. The FM part in modeled in YMF262 (it's almost 100% compatible, |
| 28 |
// the small differences are handled in YMF262). The status register and |
| 29 |
// interaction with the FM registers (e.g. the NEW2 bit) is currently handled |
| 30 |
// in the MSXMoonSound class. |
| 31 |
|
| 32 |
// MODIFIED: |
| 33 |
// Add YMW258 support by Grauw |
| 34 |
// Add DO1 output support by cam900 |
| 35 |
|
| 36 |
#include "ymf278.h" |
| 37 |
#include <algorithm> |
| 38 |
#include <cassert> |
| 39 |
|
| 40 |
// envelope output entries |
| 41 |
// fixed to match recordings from actual OPL4 -Valley Bell |
| 42 |
constexpr int MAX_ATT_INDEX = 0x280; // makes attack phase right and also goes well with "envelope stops at -60dB" |
| 43 |
constexpr int MIN_ATT_INDEX = 0; |
| 44 |
constexpr int TL_SHIFT = 2; // envelope values are 4x as fine as TL levels |
| 45 |
|
| 46 |
constexpr unsigned LFO_SHIFT = 18; // LFO period of up to 0x40000 sample |
| 47 |
constexpr unsigned LFO_PERIOD = 1 << LFO_SHIFT; |
| 48 |
|
| 49 |
// Envelope Generator phases |
| 50 |
constexpr int EG_ATT = 4; |
| 51 |
constexpr int EG_DEC = 3; |
| 52 |
constexpr int EG_SUS = 2; |
| 53 |
constexpr int EG_REL = 1; |
| 54 |
constexpr int EG_OFF = 0; |
| 55 |
|
| 56 |
// Pan values, units are -3dB, i.e. 8. |
| 57 |
constexpr uint8_t pan_left[16] = { |
| 58 |
0, 8, 16, 24, 32, 40, 48, 255, 255, 0, 0, 0, 0, 0, 0, 0 |
| 59 |
}; |
| 60 |
constexpr uint8_t pan_right[16] = { |
| 61 |
0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 48, 40, 32, 24, 16, 8 |
| 62 |
}; |
| 63 |
|
| 64 |
// decay level table (3dB per step) |
| 65 |
// 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB) |
| 66 |
static constexpr int16_t SC(int dB) { return int16_t(dB / 3 * 0x20); } |
| 67 |
constexpr int16_t dl_tab[16] = { |
| 68 |
SC( 0), SC( 3), SC( 6), SC( 9), SC(12), SC(15), SC(18), SC(21), |
| 69 |
SC(24), SC(27), SC(30), SC(33), SC(36), SC(39), SC(42), SC(93) |
| 70 |
}; |
| 71 |
|
| 72 |
constexpr byte RATE_STEPS = 8; |
| 73 |
constexpr byte eg_inc[15 * RATE_STEPS] = { |
| 74 |
//cycle:0 1 2 3 4 5 6 7 |
| 75 |
0, 1, 0, 1, 0, 1, 0, 1, // 0 rates 00..12 0 (increment by 0 or 1) |
| 76 |
0, 1, 0, 1, 1, 1, 0, 1, // 1 rates 00..12 1 |
| 77 |
0, 1, 1, 1, 0, 1, 1, 1, // 2 rates 00..12 2 |
| 78 |
0, 1, 1, 1, 1, 1, 1, 1, // 3 rates 00..12 3 |
| 79 |
|
| 80 |
1, 1, 1, 1, 1, 1, 1, 1, // 4 rate 13 0 (increment by 1) |
| 81 |
1, 1, 1, 2, 1, 1, 1, 2, // 5 rate 13 1 |
| 82 |
1, 2, 1, 2, 1, 2, 1, 2, // 6 rate 13 2 |
| 83 |
1, 2, 2, 2, 1, 2, 2, 2, // 7 rate 13 3 |
| 84 |
|
| 85 |
2, 2, 2, 2, 2, 2, 2, 2, // 8 rate 14 0 (increment by 2) |
| 86 |
2, 2, 2, 4, 2, 2, 2, 4, // 9 rate 14 1 |
| 87 |
2, 4, 2, 4, 2, 4, 2, 4, // 10 rate 14 2 |
| 88 |
2, 4, 4, 4, 2, 4, 4, 4, // 11 rate 14 3 |
| 89 |
|
| 90 |
4, 4, 4, 4, 4, 4, 4, 4, // 12 rates 15 0, 15 1, 15 2, 15 3 for decay |
| 91 |
8, 8, 8, 8, 8, 8, 8, 8, // 13 rates 15 0, 15 1, 15 2, 15 3 for attack (zero time) |
| 92 |
0, 0, 0, 0, 0, 0, 0, 0, // 14 infinity rates for attack and decay(s) |
| 93 |
}; |
| 94 |
|
| 95 |
static constexpr byte O(int a) { return a * RATE_STEPS; } |
| 96 |
constexpr byte eg_rate_select[64] = { |
| 97 |
O(14),O(14),O(14),O(14), // inf rate |
| 98 |
O( 0),O( 1),O( 2),O( 3), |
| 99 |
O( 0),O( 1),O( 2),O( 3), |
| 100 |
O( 0),O( 1),O( 2),O( 3), |
| 101 |
O( 0),O( 1),O( 2),O( 3), |
| 102 |
O( 0),O( 1),O( 2),O( 3), |
| 103 |
O( 0),O( 1),O( 2),O( 3), |
| 104 |
O( 0),O( 1),O( 2),O( 3), |
| 105 |
O( 0),O( 1),O( 2),O( 3), |
| 106 |
O( 0),O( 1),O( 2),O( 3), |
| 107 |
O( 0),O( 1),O( 2),O( 3), |
| 108 |
O( 0),O( 1),O( 2),O( 3), |
| 109 |
O( 0),O( 1),O( 2),O( 3), |
| 110 |
O( 4),O( 5),O( 6),O( 7), |
| 111 |
O( 8),O( 9),O(10),O(11), |
| 112 |
O(12),O(12),O(12),O(12), |
| 113 |
}; |
| 114 |
|
| 115 |
// rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 116 |
// shift 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0 |
| 117 |
// mask 4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0 |
| 118 |
constexpr byte eg_rate_shift[64] = { |
| 119 |
12, 12, 12, 12, |
| 120 |
11, 11, 11, 11, |
| 121 |
10, 10, 10, 10, |
| 122 |
9, 9, 9, 9, |
| 123 |
8, 8, 8, 8, |
| 124 |
7, 7, 7, 7, |
| 125 |
6, 6, 6, 6, |
| 126 |
5, 5, 5, 5, |
| 127 |
4, 4, 4, 4, |
| 128 |
3, 3, 3, 3, |
| 129 |
2, 2, 2, 2, |
| 130 |
1, 1, 1, 1, |
| 131 |
0, 0, 0, 0, |
| 132 |
0, 0, 0, 0, |
| 133 |
0, 0, 0, 0, |
| 134 |
0, 0, 0, 0, |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
// number of steps the LFO counter advances per sample |
| 139 |
// LFO frequency (Hz) -> LFO counter steps per sample |
| 140 |
static constexpr int L(double a) { return int((LFO_PERIOD * a) / 44100.0 + 0.5); } |
| 141 |
constexpr int lfo_period[8] = { |
| 142 |
L(0.168), // step: 1, period: 262144 samples |
| 143 |
L(2.019), // step: 12, period: 21845 samples |
| 144 |
L(3.196), // step: 19, period: 13797 samples |
| 145 |
L(4.206), // step: 25, period: 10486 samples |
| 146 |
L(5.215), // step: 31, period: 8456 samples |
| 147 |
L(5.888), // step: 35, period: 7490 samples |
| 148 |
L(6.224), // step: 37, period: 7085 samples |
| 149 |
L(7.066), // step: 42, period: 6242 samples |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
// formula used by Yamaha docs: |
| 154 |
// vib_depth_cents(x) = (log2(0x400 + x) - 10) * 1200 |
| 155 |
constexpr int16_t vib_depth[8] = { |
| 156 |
0, // 0.000 cents |
| 157 |
2, // 3.378 cents |
| 158 |
3, // 5.065 cents |
| 159 |
4, // 6.750 cents |
| 160 |
6, // 10.114 cents |
| 161 |
12, // 20.170 cents |
| 162 |
24, // 40.106 cents |
| 163 |
48, // 79.307 cents |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
// formula used by Yamaha docs: |
| 168 |
// am_depth_db(x) = (x-1) / 0x40 * 6.0 |
| 169 |
// They use (x-1), because the depth is multiplied with the AM counter, which has a range of 0..0x7F. |
| 170 |
// Thus the maximum attenuation with x=0x80 is (0x7F * 0x80) >> 7 = 0x7F. |
| 171 |
// reversed formula: |
| 172 |
// am_depth(dB) = round(dB / 6.0 * 0x40) + 1 |
| 173 |
constexpr uint8_t am_depth[8] = { |
| 174 |
0x00, // 0.000 dB |
| 175 |
0x14, // 1.781 dB |
| 176 |
0x20, // 2.906 dB |
| 177 |
0x28, // 3.656 dB |
| 178 |
0x30, // 4.406 dB |
| 179 |
0x40, // 5.906 dB |
| 180 |
0x50, // 7.406 dB |
| 181 |
0x80, // 11.910 dB |
| 182 |
}; |
| 183 |
|
| 184 |
// divisions of 16 |
| 185 |
constexpr int mix_level[8] = { |
| 186 |
16, // 0dB |
| 187 |
12, // -3dB (approx) |
| 188 |
8, // -6dB |
| 189 |
6, // -9dB (approx) |
| 190 |
4, // -12dB |
| 191 |
3, // -15dB (approx) |
| 192 |
2, // -18dB |
| 193 |
0, // -inf dB |
| 194 |
}; |
| 195 |
|
| 196 |
YMF278::Slot::Slot() |
| 197 |
{ |
| 198 |
reset(); |
| 199 |
} |
| 200 |
|
| 201 |
// Sign extend a 4-bit value to int (32-bit) |
| 202 |
// require: x in range [0..15] |
| 203 |
static constexpr int sign_extend_4(int x) |
| 204 |
{ |
| 205 |
return (x ^ 8) - 8; |
| 206 |
} |
| 207 |
|
| 208 |
// Params: oct in [-8 .. +7] |
| 209 |
// fn in [ 0 .. 1023] |
| 210 |
// We want to interpret oct as a signed 4-bit number and calculate |
| 211 |
// ((fn | 1024) + vib) << (5 + sign_extend_4(oct)) |
| 212 |
// Though in this formula the shift can go over a negative distance (in that |
| 213 |
// case we should shift in the other direction). |
| 214 |
static constexpr unsigned calcStep(int8_t oct, uint16_t fn, int16_t vib = 0) |
| 215 |
{ |
| 216 |
if (oct == -8) return 0; |
| 217 |
unsigned t = (fn + 1024 + vib) << (8 + oct); // use '+' iso '|' (generates slightly better code) |
| 218 |
return t >> 3; // was shifted 3 positions too far |
| 219 |
} |
| 220 |
|
| 221 |
void YMF278::Slot::reset() |
| 222 |
{ |
| 223 |
wave = FN = OCT = TLdest = TL = pan = vib = AM = 0; |
| 224 |
DL = AR = D1R = D2R = RC = RR = 0; |
| 225 |
PRVB = keyon = DAMP = false; |
| 226 |
stepptr = 0; |
| 227 |
step = calcStep(OCT, FN); |
| 228 |
bits = startaddr = loopaddr = endaddr = 0; |
| 229 |
env_vol = MAX_ATT_INDEX; |
| 230 |
|
| 231 |
lfo_active = false; |
| 232 |
lfo_cnt = 0; |
| 233 |
lfo = 0; |
| 234 |
|
| 235 |
state = EG_OFF; |
| 236 |
|
| 237 |
// not strictly needed, but avoid UMR on savestate |
| 238 |
pos = 0; |
| 239 |
} |
| 240 |
|
| 241 |
int YMF278::Slot::compute_rate(int val) const |
| 242 |
{ |
| 243 |
if (val == 0) { |
| 244 |
return 0; |
| 245 |
} else if (val == 15) { |
| 246 |
return 63; |
| 247 |
} |
| 248 |
int res = val * 4; |
| 249 |
if (RC != 15) { |
| 250 |
// clamping verified with HW tests -Valley Bell |
| 251 |
res += 2 * YMF_clamp(OCT + RC, 0, 15); |
| 252 |
res += (FN & 0x200) ? 1 : 0; |
| 253 |
} |
| 254 |
return YMF_clamp(res, 0, 63); |
| 255 |
} |
| 256 |
|
| 257 |
int YMF278::Slot::compute_decay_rate(int val) const |
| 258 |
{ |
| 259 |
if (DAMP) { |
| 260 |
// damping |
| 261 |
// The manual lists these values for time and attenuation: (44100 samples/second) |
| 262 |
// -12dB at 5.8ms, sample 256 |
| 263 |
// -48dB at 8.0ms, sample 352 |
| 264 |
// -72dB at 9.4ms, sample 416 |
| 265 |
// -96dB at 10.9ms, sample 480 |
| 266 |
// This results in these durations and rate values for the respective phases: |
| 267 |
// 0dB .. -12dB: 256 samples (5.80ms) -> 128 samples per -6dB = rate 48 |
| 268 |
// -12dB .. -48dB: 96 samples (2.18ms) -> 16 samples per -6dB = rate 63 |
| 269 |
// -48dB .. -72dB: 64 samples (1.45ms) -> 16 samples per -6dB = rate 63 |
| 270 |
// -72dB .. -96dB: 64 samples (1.45ms) -> 16 samples per -6dB = rate 63 |
| 271 |
// Damping was verified to ignore rate correction. |
| 272 |
if (env_vol < dl_tab[4]) { |
| 273 |
return 48; // 0dB .. -12dB |
| 274 |
} else { |
| 275 |
return 63; // -12dB .. -96dB |
| 276 |
} |
| 277 |
} |
| 278 |
if (PRVB) { |
| 279 |
// pseudo reverb |
| 280 |
// activated when reaching -18dB, overrides D1R/D2R/RR with reverb rate 5 |
| 281 |
// |
| 282 |
// The manual is actually a bit unclear and just says "RATE=5", |
| 283 |
// referring to the D1R/D2R/RR register value. However, later |
| 284 |
// pages use "RATE" to refer to the "internal" rate, which is |
| 285 |
// (register * 4) + rate correction. HW recordings prove that |
| 286 |
// Rate Correction is ignored, so pseudo reverb just sets the |
| 287 |
// "internal" rate to a value of 4*5 = 20. |
| 288 |
if (env_vol >= dl_tab[6]) { |
| 289 |
return 20; |
| 290 |
} |
| 291 |
} |
| 292 |
return compute_rate(val); |
| 293 |
} |
| 294 |
|
| 295 |
int16_t YMF278::Slot::compute_vib() const |
| 296 |
{ |
| 297 |
// verified via hardware recording: |
| 298 |
// With LFO speed 0 (period 262144 samples), each vibrato step takes |
| 299 |
// 4096 samples. |
| 300 |
// -> 64 steps total |
| 301 |
// Also, with vibrato depth 7 (80 cents) and an F-Num of 0x400, the |
| 302 |
// final F-Nums are: 0x400 .. 0x43C, 0x43C .. 0x400, 0x400 .. 0x3C4, |
| 303 |
// 0x3C4 .. 0x400 |
| 304 |
int16_t lfo_fm = lfo_cnt / (LFO_PERIOD / 0x40); |
| 305 |
// results in +0x00..+0x0F, +0x0F..+0x00, -0x00..-0x0F, -0x0F..-0x00 |
| 306 |
if (lfo_fm & 0x10) lfo_fm ^= 0x1F; |
| 307 |
if (lfo_fm & 0x20) lfo_fm = -(lfo_fm & 0x0F); |
| 308 |
|
| 309 |
return (lfo_fm * vib_depth[vib]) / 12; |
| 310 |
} |
| 311 |
|
| 312 |
uint16_t YMF278::Slot::compute_am() const |
| 313 |
{ |
| 314 |
// verified via hardware recording: |
| 315 |
// With LFO speed 0 (period 262144 samples), each tremolo step takes |
| 316 |
// 1024 samples. |
| 317 |
// -> 256 steps total |
| 318 |
uint16_t lfo_am = lfo_cnt / (LFO_PERIOD / 0x100); |
| 319 |
// results in 0x00..0x7F, 0x7F..0x00 |
| 320 |
if (lfo_am >= 0x80) lfo_am ^= 0xFF; |
| 321 |
|
| 322 |
return (lfo_am * am_depth[AM]) >> 7; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
void YMF278Base::advance() |
| 327 |
{ |
| 328 |
eg_cnt++; |
| 329 |
|
| 330 |
// modulo counters for volume interpolation |
| 331 |
int tl_int_cnt = eg_cnt % 9; // 0 .. 8 |
| 332 |
int tl_int_step = (eg_cnt / 9) % 3; // 0 .. 2 |
| 333 |
|
| 334 |
for (auto& op : slots) { |
| 335 |
// volume interpolation |
| 336 |
if (tl_int_cnt == 0) { |
| 337 |
if (tl_int_step == 0) { |
| 338 |
// decrease volume by one step every 27 samples |
| 339 |
if (op.TL < op.TLdest) ++op.TL; |
| 340 |
} else { |
| 341 |
// increase volume by one step every 13.5 samples |
| 342 |
if (op.TL > op.TLdest) --op.TL; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (op.lfo_active) { |
| 347 |
op.lfo_cnt = (op.lfo_cnt + lfo_period[op.lfo]) & (LFO_PERIOD - 1); |
| 348 |
} |
| 349 |
|
| 350 |
// Envelope Generator |
| 351 |
switch (op.state) { |
| 352 |
case EG_ATT: { // attack phase |
| 353 |
uint8_t rate = op.compute_rate(op.AR); |
| 354 |
// Verified by HW recording (and matches Nemesis' tests of the YM2612): |
| 355 |
// AR = 0xF during KeyOn results in instant switch to EG_DEC. (see keyOnHelper) |
| 356 |
// Setting AR = 0xF while the attack phase is in progress freezes the envelope. |
| 357 |
if (rate >= 63) { |
| 358 |
break; |
| 359 |
} |
| 360 |
uint8_t shift = eg_rate_shift[rate]; |
| 361 |
if (!(eg_cnt & ((1 << shift) - 1))) { |
| 362 |
uint8_t select = eg_rate_select[rate]; |
| 363 |
// >>4 makes the attack phase's shape match the actual chip -Valley Bell |
| 364 |
op.env_vol += (~op.env_vol * eg_inc[select + ((eg_cnt >> shift) & 7)]) >> 4; |
| 365 |
if (op.env_vol <= MIN_ATT_INDEX) { |
| 366 |
op.env_vol = MIN_ATT_INDEX; |
| 367 |
// TODO does the real HW skip EG_DEC completely, |
| 368 |
// or is it active for 1 sample? |
| 369 |
op.state = op.DL ? EG_DEC : EG_SUS; |
| 370 |
} |
| 371 |
} |
| 372 |
break; |
| 373 |
} |
| 374 |
case EG_DEC: { // decay phase |
| 375 |
uint8_t rate = op.compute_decay_rate(op.D1R); |
| 376 |
uint8_t shift = eg_rate_shift[rate]; |
| 377 |
if (!(eg_cnt & ((1 << shift) - 1))) { |
| 378 |
uint8_t select = eg_rate_select[rate]; |
| 379 |
op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)]; |
| 380 |
if (op.env_vol >= op.DL) { |
| 381 |
op.state = (op.env_vol < MAX_ATT_INDEX) ? EG_SUS : EG_OFF; |
| 382 |
} |
| 383 |
} |
| 384 |
break; |
| 385 |
} |
| 386 |
case EG_SUS: { // sustain phase |
| 387 |
uint8_t rate = op.compute_decay_rate(op.D2R); |
| 388 |
uint8_t shift = eg_rate_shift[rate]; |
| 389 |
if (!(eg_cnt & ((1 << shift) - 1))) { |
| 390 |
uint8_t select = eg_rate_select[rate]; |
| 391 |
op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)]; |
| 392 |
if (op.env_vol >= MAX_ATT_INDEX) { |
| 393 |
op.env_vol = MAX_ATT_INDEX; |
| 394 |
op.state = EG_OFF; |
| 395 |
} |
| 396 |
} |
| 397 |
break; |
| 398 |
} |
| 399 |
case EG_REL: { // release phase |
| 400 |
uint8_t rate = op.compute_decay_rate(op.RR); |
| 401 |
uint8_t shift = eg_rate_shift[rate]; |
| 402 |
if (!(eg_cnt & ((1 << shift) - 1))) { |
| 403 |
uint8_t select = eg_rate_select[rate]; |
| 404 |
op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)]; |
| 405 |
if (op.env_vol >= MAX_ATT_INDEX) { |
| 406 |
op.env_vol = MAX_ATT_INDEX; |
| 407 |
op.state = EG_OFF; |
| 408 |
} |
| 409 |
} |
| 410 |
break; |
| 411 |
} |
| 412 |
case EG_OFF: |
| 413 |
// nothing |
| 414 |
break; |
| 415 |
|
| 416 |
default: |
| 417 |
UNREACHABLE; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
int16_t YMF278Base::getSample(Slot& slot, uint16_t pos) const |
| 423 |
{ |
| 424 |
// TODO How does this behave when R#2 bit 0 = 1? |
| 425 |
// As-if read returns 0xff? (Like for CPU memory reads.) Or is |
| 426 |
// sound generation blocked at some higher level? |
| 427 |
switch (slot.bits) { |
| 428 |
case 0: { |
| 429 |
// 8 bit |
| 430 |
return memory[slot.startaddr + pos] << 8; |
| 431 |
} |
| 432 |
case 1: { |
| 433 |
// 12 bit |
| 434 |
unsigned addr = slot.startaddr + ((pos / 2) * 3); |
| 435 |
if (pos & 1) { |
| 436 |
return (memory[addr + 2] << 8) | |
| 437 |
(memory[addr + 1] & 0xF0); |
| 438 |
} else { |
| 439 |
return (memory[addr + 0] << 8) | |
| 440 |
((memory[addr + 1] << 4) & 0xF0); |
| 441 |
} |
| 442 |
} |
| 443 |
case 2: { |
| 444 |
// 16 bit |
| 445 |
unsigned addr = slot.startaddr + (pos * 2); |
| 446 |
return (memory[addr + 0] << 8) | |
| 447 |
(memory[addr + 1]); |
| 448 |
} |
| 449 |
default: |
| 450 |
// TODO unspecified |
| 451 |
return 0; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
uint16_t YMF278Base::nextPos(Slot& slot, uint16_t pos, uint16_t increment) |
| 456 |
{ |
| 457 |
// If there is a 4-sample loop and you advance 12 samples per step, |
| 458 |
// it may exceed the end offset. |
| 459 |
// This is abused by the "Lizard Star" song to generate noise at 0:52. -Valley Bell |
| 460 |
pos += increment; |
| 461 |
if ((uint32_t(pos) + slot.endaddr) >= 0x10000) // check position >= (negated) end address |
| 462 |
pos += slot.endaddr + slot.loopaddr; // This is how the actual chip does it. |
| 463 |
return pos; |
| 464 |
} |
| 465 |
|
| 466 |
bool YMF278Base::anyActive() |
| 467 |
{ |
| 468 |
return std::any_of(std::begin(slots), std::end(slots), [](auto& op) { return op.state != EG_OFF; }); |
| 469 |
} |
| 470 |
|
| 471 |
// In: 'envVol', 0=max volume, others -> -3/32 = -0.09375 dB/step |
| 472 |
// Out: 'x' attenuated by the corresponding factor. |
| 473 |
// Note: microbenchmarks have shown that re-doing this calculation is about the |
| 474 |
// same speed as using a 4kB lookup table. |
| 475 |
static constexpr int vol_factor(int x, unsigned envVol) |
| 476 |
{ |
| 477 |
if (envVol >= MAX_ATT_INDEX) return 0; // hardware clips to silence below -60dB |
| 478 |
int vol_mul = 0x80 - (envVol & 0x3F); // 0x40 values per 6dB |
| 479 |
int vol_shift = 7 + (envVol >> 6); |
| 480 |
return (x * ((0x8000 * vol_mul) >> vol_shift)) >> 15; |
| 481 |
} |
| 482 |
|
| 483 |
void YMF278Base::generate(short& fleft, short& fright, short& rleft, short& rright, short* channelBufs) |
| 484 |
{ |
| 485 |
int sampleFLeft = 0; |
| 486 |
int sampleFRight = 0; |
| 487 |
int sampleRLeft = 0; |
| 488 |
int sampleRRight = 0; |
| 489 |
for (size_t i = 0, count = slots.size(); i < count; i++) { |
| 490 |
Slot& sl = slots[i]; |
| 491 |
if (sl.state == EG_OFF) { |
| 492 |
//sampleLeft += 0; |
| 493 |
//sampleRight += 0; |
| 494 |
if (channelBufs != nullptr) { |
| 495 |
channelBufs[i] = 0; |
| 496 |
} |
| 497 |
continue; |
| 498 |
} |
| 499 |
|
| 500 |
int16_t sample = (getSample(sl, sl.pos) * (0x10000 - sl.stepptr) + |
| 501 |
getSample(sl, nextPos(sl, sl.pos, 1)) * sl.stepptr) >> 16; |
| 502 |
// TL levels are 00..FF internally (TL register value 7F is mapped to TL level FF) |
| 503 |
// Envelope levels have 4x the resolution (000..3FF) |
| 504 |
// Volume levels are approximate logarithmic. -6dB result in half volume. Steps in between use linear interpolation. |
| 505 |
// A volume of -60dB or lower results in silence. (value 0x280..0x3FF). |
| 506 |
// Recordings from actual hardware indicate that TL level and envelope level are applied separarely. |
| 507 |
// Each of them is clipped to silence below -60dB, but TL+envelope might result in a lower volume. -Valley Bell |
| 508 |
uint16_t envVol = std::min(sl.env_vol + ((sl.lfo_active && sl.AM) ? sl.compute_am() : 0), |
| 509 |
MAX_ATT_INDEX); |
| 510 |
int smplOut = vol_factor(vol_factor(sample, envVol), sl.TL << TL_SHIFT); |
| 511 |
|
| 512 |
// Panning is also done separately. (low-volume TL + low-volume panning goes below -60dB) |
| 513 |
// I'll be taking wild guess and assume that -3dB is approximated with 75%. (same as with TL and envelope levels) |
| 514 |
// The same applies to the PCM mix level. |
| 515 |
int32_t volLeft = pan_left [sl.pan]; // note: register 0xF9 is handled externally |
| 516 |
int32_t volRight = pan_right[sl.pan]; |
| 517 |
// 0 -> 0x20, 8 -> 0x18, 16 -> 0x10, 24 -> 0x0C, etc. (not using vol_factor here saves array boundary checks) |
| 518 |
volLeft = (0x20 - (volLeft & 0x0f)) >> (volLeft >> 4); |
| 519 |
volRight = (0x20 - (volRight & 0x0f)) >> (volRight >> 4); |
| 520 |
|
| 521 |
if (sl.ch) |
| 522 |
{ |
| 523 |
sampleRLeft += (smplOut * volLeft ) >> 5; |
| 524 |
sampleRRight += (smplOut * volRight) >> 5; |
| 525 |
} |
| 526 |
else |
| 527 |
{ |
| 528 |
sampleFLeft += (smplOut * volLeft ) >> 5; |
| 529 |
sampleFRight += (smplOut * volRight) >> 5; |
| 530 |
} |
| 531 |
|
| 532 |
unsigned step = (sl.lfo_active && sl.vib) |
| 533 |
? calcStep(sl.OCT, sl.FN, sl.compute_vib()) |
| 534 |
: sl.step; |
| 535 |
sl.stepptr += step; |
| 536 |
|
| 537 |
if (sl.stepptr >= 0x10000) { |
| 538 |
sl.pos = nextPos(sl, sl.pos, sl.stepptr >> 16); |
| 539 |
sl.stepptr &= 0xffff; |
| 540 |
} |
| 541 |
|
| 542 |
if (channelBufs != nullptr) { |
| 543 |
channelBufs[i] = sl.pan != 8 ? smplOut : 0; |
| 544 |
} |
| 545 |
} |
| 546 |
advance(); |
| 547 |
|
| 548 |
fleft = sampleFLeft >> 4; |
| 549 |
fright = sampleFRight >> 4; |
| 550 |
rleft = sampleRLeft >> 4; |
| 551 |
rright = sampleRRight >> 4; |
| 552 |
} |
| 553 |
|
| 554 |
void YMF278Base::keyOnHelper(Slot& slot) |
| 555 |
{ |
| 556 |
// Unlike FM, the envelope level is reset. (And it makes sense, because you restart the sample.) |
| 557 |
slot.env_vol = MAX_ATT_INDEX; |
| 558 |
if (slot.compute_rate(slot.AR) < 63) { |
| 559 |
slot.state = EG_ATT; |
| 560 |
} else { |
| 561 |
// Nuke.YKT verified that the FM part does it exactly this way, |
| 562 |
// and the OPL4 manual says it's instant as well. |
| 563 |
slot.env_vol = MIN_ATT_INDEX; |
| 564 |
// see comment in 'case EG_ATT' in YMF278::advance() |
| 565 |
slot.state = slot.DL ? EG_DEC : EG_SUS; |
| 566 |
} |
| 567 |
slot.stepptr = 0; |
| 568 |
slot.pos = 0; |
| 569 |
} |
| 570 |
|
| 571 |
YMF278Base::YMF278Base(MemoryInterface& memory, int channelCount, int clockDivider, double clockFrequency) |
| 572 |
: memory(memory) |
| 573 |
, slots(channelCount) |
| 574 |
, channelCount(channelCount) |
| 575 |
, clockDivider(clockDivider) |
| 576 |
, clockFrequency(clockFrequency) |
| 577 |
{ |
| 578 |
reset(); |
| 579 |
} |
| 580 |
|
| 581 |
YMF278Base::~YMF278Base() |
| 582 |
{ |
| 583 |
} |
| 584 |
|
| 585 |
int YMF278Base::getChannelCount() |
| 586 |
{ |
| 587 |
return channelCount; |
| 588 |
} |
| 589 |
|
| 590 |
int YMF278Base::getClockDivider() |
| 591 |
{ |
| 592 |
return clockDivider; |
| 593 |
} |
| 594 |
|
| 595 |
double YMF278Base::getClockFrequency() |
| 596 |
{ |
| 597 |
return clockFrequency; |
| 598 |
} |
| 599 |
|
| 600 |
void YMF278Base::setClockFrequency(double clockFrequency_) |
| 601 |
{ |
| 602 |
clockFrequency = clockFrequency_; |
| 603 |
} |
| 604 |
|
| 605 |
double YMF278Base::getSampleRate() |
| 606 |
{ |
| 607 |
return clockFrequency / (channelCount * clockDivider); |
| 608 |
} |
| 609 |
|
| 610 |
void YMF278Base::reset() |
| 611 |
{ |
| 612 |
eg_cnt = 0; |
| 613 |
for (auto& op : slots) { |
| 614 |
op.reset(); |
| 615 |
} |
| 616 |
memory.setMemoryType(false); |
| 617 |
} |
| 618 |
|
| 619 |
YMF278::YMF278(MemoryInterface& memory) |
| 620 |
: YMF278Base(memory, 24, 32, 33868800) |
| 621 |
, fmMixL(0), fmMixR(0), pcmMixL(0), pcmMixR(0) |
| 622 |
{ |
| 623 |
memAdr = 0; // avoid UMR |
| 624 |
std::fill(std::begin(regs), std::end(regs), 0); |
| 625 |
} |
| 626 |
|
| 627 |
void YMF278::reset() |
| 628 |
{ |
| 629 |
YMF278Base::reset(); |
| 630 |
|
| 631 |
regs[2] = 0; // avoid UMR |
| 632 |
for (int i = 0xf7; i >= 0; --i) { // reverse order to avoid UMR |
| 633 |
writeReg(i, 0); |
| 634 |
} |
| 635 |
writeReg(0xf8, 0x1b); |
| 636 |
writeReg(0xf9, 0x00); |
| 637 |
memAdr = 0; |
| 638 |
} |
| 639 |
|
| 640 |
void YMF278::writeReg(byte reg, byte data) |
| 641 |
{ |
| 642 |
// Handle slot registers specifically |
| 643 |
if (reg >= 0x08 && reg <= 0xF7) { |
| 644 |
int sNum = (reg - 8) % 24; |
| 645 |
auto& slot = slots[sNum]; |
| 646 |
switch ((reg - 8) / 24) { |
| 647 |
case 0: { |
| 648 |
slot.wave = (slot.wave & 0x100) | data; |
| 649 |
int waveTblHdr = (regs[2] >> 2) & 0x7; |
| 650 |
int base = (slot.wave < 384 || !waveTblHdr) ? |
| 651 |
(slot.wave * 12) : |
| 652 |
(waveTblHdr * 0x80000 + ((slot.wave - 384) * 12)); |
| 653 |
byte buf[12]; |
| 654 |
for (unsigned i = 0; i < 12; ++i) { |
| 655 |
// TODO What if R#2 bit 0 = 1? |
| 656 |
// See also getSample() |
| 657 |
buf[i] = memory[base + i]; |
| 658 |
} |
| 659 |
slot.bits = (buf[0] & 0xC0) >> 6; |
| 660 |
slot.startaddr = buf[2] | (buf[1] << 8) | ((buf[0] & 0x3F) << 16); |
| 661 |
slot.loopaddr = buf[4] | (buf[3] << 8); |
| 662 |
slot.endaddr = buf[6] | (buf[5] << 8); |
| 663 |
for (unsigned i = 7; i < 12; ++i) { |
| 664 |
// Verified on real YMF278: |
| 665 |
// After tone loading, if you read these |
| 666 |
// registers, their value actually has changed. |
| 667 |
writeReg(8 + sNum + (i - 2) * 24, buf[i]); |
| 668 |
} |
| 669 |
if (slot.keyon) { |
| 670 |
keyOnHelper(slot); |
| 671 |
} else { |
| 672 |
slot.stepptr = 0; |
| 673 |
slot.pos = 0; |
| 674 |
} |
| 675 |
break; |
| 676 |
} |
| 677 |
case 1: { |
| 678 |
slot.wave = (slot.wave & 0xFF) | ((data & 0x1) << 8); |
| 679 |
slot.FN = (slot.FN & 0x380) | (data >> 1); |
| 680 |
slot.step = calcStep(slot.OCT, slot.FN); |
| 681 |
break; |
| 682 |
} |
| 683 |
case 2: { |
| 684 |
slot.FN = (slot.FN & 0x07F) | ((data & 0x07) << 7); |
| 685 |
slot.PRVB = (data & 0x08) != 0; |
| 686 |
slot.OCT = sign_extend_4((data & 0xF0) >> 4); |
| 687 |
slot.step = calcStep(slot.OCT, slot.FN); |
| 688 |
break; |
| 689 |
} |
| 690 |
case 3: { |
| 691 |
uint8_t t = data >> 1; |
| 692 |
slot.TLdest = (t != 0x7f) ? t : 0xff; // verified on HW via volume interpolation |
| 693 |
if (data & 1) { |
| 694 |
// directly change volume |
| 695 |
slot.TL = slot.TLdest; |
| 696 |
} else { |
| 697 |
// interpolate volume |
| 698 |
} |
| 699 |
break; |
| 700 |
} |
| 701 |
case 4: |
| 702 |
slot.ch = data & 0x10; |
| 703 |
slot.pan = data & 0x0F; |
| 704 |
|
| 705 |
if (data & 0x20) { |
| 706 |
// LFO reset |
| 707 |
slot.lfo_active = false; |
| 708 |
slot.lfo_cnt = 0; |
| 709 |
} else { |
| 710 |
// LFO activate |
| 711 |
slot.lfo_active = true; |
| 712 |
} |
| 713 |
|
| 714 |
slot.DAMP = (data & 0x40) != 0; |
| 715 |
|
| 716 |
if (data & 0x80) { |
| 717 |
if (!slot.keyon) { |
| 718 |
slot.keyon = true; |
| 719 |
keyOnHelper(slot); |
| 720 |
} |
| 721 |
} else { |
| 722 |
if (slot.keyon) { |
| 723 |
slot.keyon = false; |
| 724 |
slot.state = EG_REL; |
| 725 |
} |
| 726 |
} |
| 727 |
break; |
| 728 |
case 5: |
| 729 |
slot.lfo = (data >> 3) & 0x7; |
| 730 |
slot.vib = data & 0x7; |
| 731 |
break; |
| 732 |
case 6: |
| 733 |
slot.AR = data >> 4; |
| 734 |
slot.D1R = data & 0xF; |
| 735 |
break; |
| 736 |
case 7: |
| 737 |
slot.DL = dl_tab[data >> 4]; |
| 738 |
slot.D2R = data & 0xF; |
| 739 |
break; |
| 740 |
case 8: |
| 741 |
slot.RC = data >> 4; |
| 742 |
slot.RR = data & 0xF; |
| 743 |
break; |
| 744 |
case 9: |
| 745 |
slot.AM = data & 0x7; |
| 746 |
break; |
| 747 |
} |
| 748 |
} else { |
| 749 |
// All non-slot registers |
| 750 |
switch (reg) { |
| 751 |
case 0x00: // TEST |
| 752 |
case 0x01: |
| 753 |
break; |
| 754 |
|
| 755 |
case 0x02: |
| 756 |
// wave-table-header / memory-type / memory-access-mode |
| 757 |
// Simply store in regs[2] |
| 758 |
memory.setMemoryType(regs[2] & 2); |
| 759 |
break; |
| 760 |
|
| 761 |
case 0x03: |
| 762 |
// Verified on real YMF278: |
| 763 |
// * Don't update the 'memAdr' variable on writes to |
| 764 |
// reg 3 and 4. Only store the value in the 'regs' |
| 765 |
// array for later use. |
| 766 |
// * The upper 2 bits are not used to address the |
| 767 |
// external memories (so from a HW pov they don't |
| 768 |
// matter). But if you read back this register, the |
| 769 |
// upper 2 bits always read as '0' (even if you wrote |
| 770 |
// '1'). So we mask the bits here already. |
| 771 |
data &= 0x3F; |
| 772 |
break; |
| 773 |
|
| 774 |
case 0x04: |
| 775 |
// See reg 3. |
| 776 |
break; |
| 777 |
|
| 778 |
case 0x05: |
| 779 |
// Verified on real YMF278: (see above) |
| 780 |
// Only writes to reg 5 change the (full) 'memAdr'. |
| 781 |
memAdr = (regs[3] << 16) | (regs[4] << 8) | data; |
| 782 |
break; |
| 783 |
|
| 784 |
case 0x06: // memory data |
| 785 |
if (regs[2] & 1) { |
| 786 |
memory.write(memAdr, data); |
| 787 |
++memAdr; // no need to mask (again) here |
| 788 |
} else { |
| 789 |
// Verified on real YMF278: |
| 790 |
// - writes are ignored |
| 791 |
// - memAdr is NOT increased |
| 792 |
} |
| 793 |
break; |
| 794 |
|
| 795 |
case 0xf8: |
| 796 |
fmMixL = mix_level[data & 0x7]; |
| 797 |
fmMixR = mix_level[data >> 3 & 0x7]; |
| 798 |
break; |
| 799 |
case 0xf9: |
| 800 |
pcmMixL = mix_level[data & 0x7]; |
| 801 |
pcmMixR = mix_level[data >> 3 & 0x7]; |
| 802 |
break; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
regs[reg] = data; |
| 807 |
} |
| 808 |
|
| 809 |
byte YMF278::readReg(byte reg) |
| 810 |
{ |
| 811 |
// no need to call updateStream(time) |
| 812 |
byte result = peekReg(reg); |
| 813 |
if (reg == 6) { |
| 814 |
// Memory Data Register |
| 815 |
if (regs[2] & 1) { |
| 816 |
// Verified on real YMF278: |
| 817 |
// memAdr is only increased when 'regs[2] & 1' |
| 818 |
++memAdr; // no need to mask (again) here |
| 819 |
} |
| 820 |
} |
| 821 |
return result; |
| 822 |
} |
| 823 |
|
| 824 |
byte YMF278::peekReg(byte reg) const |
| 825 |
{ |
| 826 |
switch (reg) { |
| 827 |
case 2: // 3 upper bits are device ID |
| 828 |
return (regs[2] & 0x1F) | 0x20; |
| 829 |
|
| 830 |
case 6: // Memory Data Register |
| 831 |
if (regs[2] & 1) { |
| 832 |
return memory[memAdr]; |
| 833 |
} else { |
| 834 |
// Verified on real YMF278 |
| 835 |
return 0xff; |
| 836 |
} |
| 837 |
|
| 838 |
default: |
| 839 |
return regs[reg]; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
YMW258::YMW258(MemoryInterface& memory) |
| 844 |
: YMF278Base(memory, 28, 8, 9878400) |
| 845 |
{ |
| 846 |
} |
| 847 |
|
| 848 |
void YMW258::writeReg(byte channel, byte reg, byte data) |
| 849 |
{ |
| 850 |
if ((channel & 0x7) == 0x7 || channel >= 0x20 || reg >= 0x8) |
| 851 |
return; |
| 852 |
int sNum = (channel >> 3) * 7 + (channel & 0x7); |
| 853 |
auto& slot = slots[sNum]; |
| 854 |
|
| 855 |
switch (reg) { |
| 856 |
case 0: { |
| 857 |
slot.pan = data >> 4; |
| 858 |
break; |
| 859 |
} |
| 860 |
case 1: { |
| 861 |
slot.wave = (slot.wave & 0x100) | data; |
| 862 |
int base = slot.wave * 12; |
| 863 |
byte buf[12]; |
| 864 |
for (unsigned i = 0; i < 12; ++i) { |
| 865 |
buf[i] = memory[base + i]; |
| 866 |
} |
| 867 |
slot.bits = (buf[0] >> 6) == 0x3 ? 1 : 0; // 00 / 10: 8 bit, 11: 12 bit, 01: unknown |
| 868 |
slot.startaddr = buf[2] | (buf[1] << 8) | ((buf[0] & 0x1F) << 16); |
| 869 |
slot.loopaddr = buf[4] | (buf[3] << 8); |
| 870 |
slot.endaddr = buf[6] | (buf[5] << 8); |
| 871 |
slot.lfo = (buf[7] >> 3) & 0x7; |
| 872 |
slot.vib = buf[7] & 0x7; |
| 873 |
slot.AR = buf[8] >> 4; |
| 874 |
slot.D1R = buf[8] & 0xF; |
| 875 |
slot.DL = dl_tab[buf[9] >> 4]; |
| 876 |
slot.D2R = buf[9] & 0xF; |
| 877 |
slot.RC = buf[10] >> 4; |
| 878 |
slot.RR = buf[10] & 0xF; |
| 879 |
slot.AM = buf[11] & 0x7; |
| 880 |
if (slot.keyon) { |
| 881 |
keyOnHelper(slot); |
| 882 |
} else { |
| 883 |
slot.stepptr = 0; |
| 884 |
slot.pos = 0; |
| 885 |
} |
| 886 |
break; |
| 887 |
} |
| 888 |
case 2: { |
| 889 |
slot.wave = (slot.wave & 0xFF) | ((data & 0x1) << 8); |
| 890 |
slot.FN = (slot.FN & 0x3C0) | (data >> 2); |
| 891 |
slot.step = calcStep(slot.OCT, slot.FN); |
| 892 |
break; |
| 893 |
} |
| 894 |
case 3: { |
| 895 |
slot.FN = (slot.FN & 0x03F) | ((data & 0x0F) << 6); |
| 896 |
slot.OCT = sign_extend_4((data & 0xF0) >> 4); |
| 897 |
slot.step = calcStep(slot.OCT, slot.FN); |
| 898 |
break; |
| 899 |
} |
| 900 |
case 4: { |
| 901 |
slot.lfo_active = true; |
| 902 |
if (data & 0x80) { |
| 903 |
if (!slot.keyon) { |
| 904 |
slot.keyon = true; |
| 905 |
keyOnHelper(slot); |
| 906 |
} |
| 907 |
} else { |
| 908 |
if (slot.keyon) { |
| 909 |
slot.keyon = false; |
| 910 |
slot.state = EG_REL; |
| 911 |
} |
| 912 |
} |
| 913 |
break; |
| 914 |
} |
| 915 |
case 5: { |
| 916 |
uint8_t t = data >> 1; |
| 917 |
slot.TLdest = (t != 0x7f) ? t : 0xff; // verified on YMF278 via volume interpolation |
| 918 |
if (data & 1) { |
| 919 |
// directly change volume |
| 920 |
slot.TL = slot.TLdest; |
| 921 |
} else { |
| 922 |
// interpolate volume |
| 923 |
} |
| 924 |
break; |
| 925 |
} |
| 926 |
case 6: { |
| 927 |
slot.lfo = (data >> 3) & 0x7; |
| 928 |
slot.vib = data & 0x7; |
| 929 |
break; |
| 930 |
} |
| 931 |
case 7: { |
| 932 |
slot.AM = data & 0x7; |
| 933 |
break; |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
MemoryMoonSound::MemoryMoonSound(MemoryInterface& rom, MemoryInterface& ram) |
| 939 |
: rom(rom) |
| 940 |
, ram(ram) |
| 941 |
, memoryType(false) |
| 942 |
{ |
| 943 |
if (rom.getSize() != 0x200000) { // 2MB |
| 944 |
assert(false); |
| 945 |
} |
| 946 |
assert((ram.getSize() & (1024 - 1)) == 0); |
| 947 |
int ramSize_ = ram.getSize() / 1024; |
| 948 |
if ((ramSize_ != 0) && // - - |
| 949 |
(ramSize_ != 128) && // 128kB - |
| 950 |
(ramSize_ != 256) && // 128kB 128kB |
| 951 |
(ramSize_ != 512) && // 512kB - |
| 952 |
(ramSize_ != 640) && // 512kB 128kB |
| 953 |
(ramSize_ != 1024) && // 512kB 512kB |
| 954 |
(ramSize_ != 2048)) { // 512kB 512kB 512kB 512kB |
| 955 |
assert(false); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
// This routine translates an address from the (upper) MoonSound address space |
| 960 |
// to an address inside the (linearized) SRAM address space. |
| 961 |
// |
| 962 |
// The following info is based on measurements on a real MoonSound (v2.0) |
| 963 |
// PCB. This PCB can have several possible SRAM configurations: |
| 964 |
// 128kB: |
| 965 |
// 1 SRAM chip of 128kB, chip enable (/CE) of this SRAM chip is connected to |
| 966 |
// the 1Y0 output of a 74LS139 (2-to-4 decoder). The enable input of the |
| 967 |
// 74LS139 is connected to YMF278 pin /MCS6 and the 74LS139 1B:1A inputs are |
| 968 |
// connected to YMF278 pins MA18:MA17. So the SRAM is selected when /MC6 is |
| 969 |
// active and MA18:MA17 == 0:0. |
| 970 |
// 256kB: |
| 971 |
// 2 SRAM chips of 128kB. First one connected as above. Second one has /CE |
| 972 |
// connected to 74LS139 pin 1Y1. So SRAM2 is selected when /MSC6 is active |
| 973 |
// and MA18:MA17 == 0:1. |
| 974 |
// 512kB: |
| 975 |
// 1 SRAM chip of 512kB, /CE connected to /MCS6 |
| 976 |
// 640kB: |
| 977 |
// 1 SRAM chip of 512kB, /CE connected to /MCS6 |
| 978 |
// 1 SRAM chip of 128kB, /CE connected to /MCS7. |
| 979 |
// (This means SRAM2 is potentially mirrored over a 512kB region) |
| 980 |
// 1024kB: |
| 981 |
// 1 SRAM chip of 512kB, /CE connected to /MCS6 |
| 982 |
// 1 SRAM chip of 512kB, /CE connected to /MCS7 |
| 983 |
// 2048kB: |
| 984 |
// 1 SRAM chip of 512kB, /CE connected to /MCS6 |
| 985 |
// 1 SRAM chip of 512kB, /CE connected to /MCS7 |
| 986 |
// 1 SRAM chip of 512kB, /CE connected to /MCS8 |
| 987 |
// 1 SRAM chip of 512kB, /CE connected to /MCS9 |
| 988 |
// This configuration is not so easy to create on the v2.0 PCB. So it's |
| 989 |
// very rare. |
| 990 |
// |
| 991 |
// So the /MCS6 and /MCS7 (and /MCS8 and /MCS9 in case of 2048kB) signals are |
| 992 |
// used to select the different SRAM chips. The meaning of these signals |
| 993 |
// depends on the 'memory access mode'. This mode can be changed at run-time |
| 994 |
// via bit 1 in register 2. The following table indicates for which regions |
| 995 |
// these signals are active (normally MoonSound should be used with mode=0): |
| 996 |
// mode=0 mode=1 |
| 997 |
// /MCS6 0x200000-0x27FFFF 0x380000-0x39FFFF |
| 998 |
// /MCS7 0x280000-0x2FFFFF 0x3A0000-0x3BFFFF |
| 999 |
// /MCS8 0x300000-0x37FFFF 0x3C0000-0x3DFFFF |
| 1000 |
// /MCS9 0x380000-0x3FFFFF 0x3E0000-0x3FFFFF |
| 1001 |
// |
| 1002 |
// (For completeness) MoonSound also has 2MB ROM (YRW801), /CE of this ROM is |
| 1003 |
// connected to YMF278 /MCS0. In both mode=0 and mode=1 this signal is active |
| 1004 |
// for the region 0x000000-0x1FFFFF. (But this routine does not handle ROM). |
| 1005 |
unsigned MemoryMoonSound::getRamAddress(unsigned addr) const |
| 1006 |
{ |
| 1007 |
addr -= 0x200000; // RAM starts at 0x200000 |
| 1008 |
if (memoryType) { |
| 1009 |
// Normally MoonSound is used in 'memory access mode = 0'. But |
| 1010 |
// in the rare case that mode=1 we adjust the address. |
| 1011 |
if ((0x180000 <= addr) && (addr <= 0x1FFFFF)) { |
| 1012 |
addr -= 0x180000; |
| 1013 |
switch (addr & 0x060000) { |
| 1014 |
case 0x000000: // [0x380000-0x39FFFF] |
| 1015 |
// 1st 128kB of SRAM1 |
| 1016 |
break; |
| 1017 |
case 0x020000: // [0x3A0000-0x3BFFFF] |
| 1018 |
if (ram.getSize() == 256 * 1024) { |
| 1019 |
// 2nd 128kB SRAM chip |
| 1020 |
} else { |
| 1021 |
// 2nd block of 128kB in SRAM2 |
| 1022 |
// In case of 512+128, we use mirroring |
| 1023 |
addr += 0x080000; |
| 1024 |
} |
| 1025 |
break; |
| 1026 |
case 0x040000: // [0x3C0000-0x3DFFFF] |
| 1027 |
// 3rd 128kB block in SRAM3 |
| 1028 |
addr += 0x100000; |
| 1029 |
break; |
| 1030 |
case 0x060000: // [0x3EFFFF-0x3FFFFF] |
| 1031 |
// 4th 128kB block in SRAM4 |
| 1032 |
addr += 0x180000; |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
} else { |
| 1036 |
addr = unsigned(-1); // unmapped |
| 1037 |
} |
| 1038 |
} |
| 1039 |
if (ram.getSize() == 640 * 1024) { |
| 1040 |
// Verified on real MoonSound cartridge (v2.0): In case of |
| 1041 |
// 640kB (1x512kB + 1x128kB), the 128kB SRAM chip is 4 times |
| 1042 |
// visible. None of the other SRAM configurations show similar |
| 1043 |
// mirroring (because the others are powers of two). |
| 1044 |
if (addr > 0x080000) { |
| 1045 |
addr &= ~0x060000; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
return addr; |
| 1049 |
} |
| 1050 |
|
| 1051 |
byte MemoryMoonSound::operator[](unsigned address) const |
| 1052 |
{ |
| 1053 |
// Verified on real YMF278: address space wraps at 4MB. |
| 1054 |
address &= 0x3FFFFF; |
| 1055 |
if (address < 0x200000) { |
| 1056 |
// ROM connected to /MCS0 |
| 1057 |
return rom[address]; |
| 1058 |
} else { |
| 1059 |
unsigned ramAddr = getRamAddress(address); |
| 1060 |
if (ramAddr < ram.getSize()) { |
| 1061 |
return ram[ramAddr]; |
| 1062 |
} else { |
| 1063 |
// unmapped region |
| 1064 |
return 255; // TODO check |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
unsigned MemoryMoonSound::getSize() const { |
| 1070 |
return 0x400000; |
| 1071 |
} |
| 1072 |
|
| 1073 |
void MemoryMoonSound::write(unsigned address, byte value) |
| 1074 |
{ |
| 1075 |
address &= 0x3FFFFF; |
| 1076 |
if (address < 0x200000) { |
| 1077 |
// can't write to ROM |
| 1078 |
} else { |
| 1079 |
unsigned ramAddr = getRamAddress(address); |
| 1080 |
if (ramAddr < ram.getSize()) { |
| 1081 |
ram.write(ramAddr, value); |
| 1082 |
} else { |
| 1083 |
// can't write to unmapped memory |
| 1084 |
} |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
void MemoryMoonSound::clear(byte value) { |
| 1089 |
ram.clear(value); |
| 1090 |
} |
| 1091 |
|
| 1092 |
void MemoryMoonSound::setMemoryType(bool memoryType_) { |
| 1093 |
memoryType = memoryType_; |
| 1094 |
} |