Skip to content

Instantly share code, notes, and snippets.

@okaxaki
Created October 5, 2019 08:35
Show Gist options
  • Save okaxaki/975053f349e1c13990447b8aeea5a972 to your computer and use it in GitHub Desktop.
Save okaxaki/975053f349e1c13990447b8aeea5a972 to your computer and use it in GitHub Desktop.
export type OPLLOperatorParam = {
am: number;
pm: number;
eg: number;
ml: number;
kr: number;
kl: number;
tl: number;
ar: number;
dr: number;
sl: number;
rr: number;
wf: number;
fb: number;
};
export type OPLLVoice = {
mod: OPLLOperatorParam;
car: OPLLOperatorParam;
};
export const OPLL_RAW_VOICES: Uint8Array[] = [
new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
new Uint8Array([0x61, 0x61, 0x1e, 0x17, 0xf0, 0x7f, 0x00, 0x17]),
new Uint8Array([0x13, 0x41, 0x17, 0x0e, 0xff, 0xff, 0x23, 0x13]),
new Uint8Array([0x23, 0x01, 0x9a, 0x04, 0xa3, 0xf4, 0xf0, 0x23]),
new Uint8Array([0x11, 0x61, 0x0e, 0x07, 0xfa, 0x64, 0x70, 0x17]),
new Uint8Array([0x32, 0x21, 0x1e, 0x06, 0xe1, 0x76, 0x01, 0x28]),
new Uint8Array([0x21, 0x22, 0x16, 0x05, 0xf0, 0x71, 0x00, 0x18]),
new Uint8Array([0x21, 0x61, 0x1d, 0x07, 0x82, 0x81, 0x11, 0x07]),
new Uint8Array([0x23, 0x21, 0x2d, 0x16, 0x90, 0x90, 0x00, 0x07]),
new Uint8Array([0x21, 0x21, 0x1b, 0x06, 0x64, 0x65, 0x10, 0x17]),
new Uint8Array([0x21, 0x21, 0x0b, 0x1a, 0x85, 0xa0, 0x70, 0x07]),
new Uint8Array([0x23, 0x01, 0x83, 0x10, 0xff, 0xb4, 0x10, 0xf4]),
new Uint8Array([0x97, 0xc1, 0x20, 0x07, 0xff, 0xf4, 0x22, 0x22]),
new Uint8Array([0x61, 0x00, 0x0c, 0x05, 0xd2, 0xf6, 0x40, 0x43]),
new Uint8Array([0x01, 0x01, 0x56, 0x03, 0xf4, 0xf0, 0x03, 0x02]),
new Uint8Array([0x21, 0x41, 0x89, 0x03, 0xf1, 0xf4, 0xf0, 0x23]),
new Uint8Array([0x01, 0x01, 0x18, 0x0f, 0xdf, 0xf8, 0x6a, 0x6d]), // B.D
new Uint8Array([0x01, 0x01, 0x00, 0x00, 0xc8, 0xd8, 0xa7, 0x68]), // HH & TOM
new Uint8Array([0x05, 0x01, 0x00, 0x00, 0xf8, 0xaa, 0x59, 0x55]), // SD & CYM
];
export function rawVoiceToVoice(d: Uint8Array): OPLLVoice {
return {
mod: {
am: (d[0] >> 7) & 1,
pm: (d[0] >> 6) & 1,
eg: (d[0] >> 5) & 1,
kr: (d[0] >> 4) & 1,
ml: d[0] & 0xf,
kl: (d[2] >> 6) & 3,
tl: d[2] & 0x3f,
ar: (d[4] >> 4) & 0xf,
dr: d[4] & 0xf,
sl: (d[6] >> 4) & 0xf,
rr: d[6] & 0xf,
wf: (d[3] >> 3) & 1,
fb: d[3] & 7,
},
car: {
am: (d[1] >> 7) & 1,
pm: (d[1] >> 6) & 1,
eg: (d[1] >> 5) & 1,
kr: (d[1] >> 4) & 1,
ml: d[1] & 0xf,
kl: (d[3] >> 6) & 3,
tl: 0,
ar: (d[5] >> 4) & 0xf,
dr: d[5] & 0xf,
sl: (d[7] >> 4) & 0xf,
rr: d[7] & 0xf,
wf: (d[3] >> 4) & 1,
fb: 0,
},
};
}
const OPLL_VOICES: OPLLVoice[] = OPLL_RAW_VOICES.map(rawVoiceToVoice);
export default OPLL_VOICES;
import OPLL_VOICES, { OPLLVoice, rawVoiceToVoice } from "./opll-voices";
function getModOffset(ch: number) {
return 8 * Math.floor(ch / 3) + (ch % 3);
}
function _R(rate: number) {
return rate;
}
export default class OPLLToOPL {
_regs = new Uint8Array(256).fill(0);
_oplRegs = new Uint8Array(256).fill(0);
_buildVoiceSetup(
ch: number,
v: OPLLVoice,
modVolume: number | null,
carVolume: number | null,
al: number
): { a: number; d: number }[] {
const modOffset = getModOffset(ch);
const carOffset = modOffset + 3;
return [
{
a: 0x20 + modOffset,
d:
(v.mod.am << 7) |
(v.mod.pm << 6) |
(v.mod.eg << 5) |
(v.mod.kr << 4) |
v.mod.ml,
},
{
a: 0x20 + carOffset,
d:
(v.car.am << 7) |
(v.car.pm << 6) |
(v.car.eg << 5) |
(v.car.kr << 4) |
v.car.ml,
},
{
a: 0x40 + modOffset,
d: (v.mod.kl << 6) | (modVolume ? modVolume : v.mod.tl),
},
{
a: 0x40 + carOffset,
d: (v.car.kl << 6) | (carVolume ? carVolume : v.car.tl),
},
{
a: 0x60 + modOffset,
d: (_R(v.mod.ar) << 4) | _R(v.mod.dr),
},
{
a: 0x60 + carOffset,
d: (_R(v.car.ar) << 4) | _R(v.car.dr),
},
{
a: 0x80 + modOffset,
d: (v.mod.sl << 4) | _R(v.mod.rr),
},
{
a: 0x80 + carOffset,
d: (v.car.sl << 4) | _R(v.car.rr),
},
{
a: 0xc0 + ch,
d: (v.mod.fb << 1) | al,
},
{ a: 0xe0 + modOffset, d: v.mod.wf ? 1 : 0 },
{ a: 0xe0 + carOffset, d: v.car.wf ? 1 : 0 },
];
}
_rflag: boolean = false;
_buildInstAndVolume(ch: number): { a: number; d: number }[] {
const d = this._regs[0x30 + ch];
const inst = (d & 0xf0) >> 4;
const volume = d & 0xf;
let voice: OPLLVoice;
if (inst === 0) {
voice = rawVoiceToVoice(this._regs);
} else {
voice = OPLL_VOICES[inst];
}
const ret: { a: number; d: number }[] = [];
if (this._rflag && 6 <= ch) {
switch (ch) {
case 6:
this._buildVoiceSetup(
6,
OPLL_VOICES[16],
null,
(this._regs[0x36] & 0xf) << 1,
0
).forEach(({ a, d }) => {
ret.push({ a, d });
});
break;
case 7:
this._buildVoiceSetup(
7,
OPLL_VOICES[17],
((this._regs[0x37] >> 4) & 0xf) << 1,
(this._regs[0x37] & 0xf) << 1,
1
).forEach(({ a, d }) => {
ret.push({ a, d });
});
break;
case 8:
this._buildVoiceSetup(
8,
OPLL_VOICES[18],
((this._regs[0x38] >> 4) & 0xf) << 1,
(this._regs[0x38] & 0xf) << 1,
1
).forEach(({ a, d }) => {
ret.push({ a, d });
});
break;
}
} else {
this._buildVoiceSetup(ch, voice, null, volume << 2, 0).forEach(
({ a, d }) => {
ret.push({ a, d });
}
);
}
return ret;
}
_interpretOpll(a: number, d: number): { a: number; d: number }[] {
this._regs[a & 0xff] = d & 0xff;
if (a == 0x0e) {
let ret = [];
if (d & 0x20 && !this._rflag) {
this._rflag = true;
let ret = this._buildInstAndVolume(6);
ret = ret.concat(this._buildInstAndVolume(7));
ret = ret.concat(this._buildInstAndVolume(8));
} else if (!(d & 0x20) && this._rflag) {
this._rflag = false;
let ret = this._buildInstAndVolume(6);
ret = ret.concat(this._buildInstAndVolume(7));
ret = ret.concat(this._buildInstAndVolume(8));
ret.push({ a: 0xbd, d: 0xc0 | (d & 0x3f) });
} else {
this._rflag = d & 0x20 ? true : false;
}
ret.push({ a: 0xbd, d: 0xc0 | (d & 0x3f) });
return ret;
}
if (0x10 <= a && a <= 0x18) {
const ch = a & 0xf;
return [
{
a: 0xb0 + ch,
d: ((this._regs[0x20 + ch] & 0x1f) << 1) | ((d & 0x80) >> 7),
},
{ a: 0xa0 + ch, d: (d & 0x7f) << 1 },
];
}
if (0x20 <= a && a <= 0x28) {
const ch = a & 0xf;
const res = [
{
a: 0xb0 + ch,
d: ((d & 0x1f) << 1) | ((this._regs[0x10 + ch] & 0x80) >> 7),
},
{ a: 0xa0 + ch, d: (this._regs[0x10 + ch] & 0x7f) << 1 },
];
return res;
}
if (0x30 <= a && a <= 0x38) {
const ch = a & 0xf;
return this._buildInstAndVolume(ch);
}
return [];
}
_initialized: boolean = false;
interpretOpll(a: number, d: number) {
let res: { a: number; d: number }[] = [];
if (!this._initialized) {
res.push({
a: 0x01,
d: 0x20, // YM3812 mode
});
this._initialized = true;
}
res = res.concat(this._interpretOpll(a, d));
res = res.filter(({ a, d }) => {
return this._oplRegs[a] !== d;
});
res.forEach(({ a, d }) => {
this._oplRegs[a] = d;
});
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment