Skip to content

Commit

Permalink
fix(vgm2la): Eliminates redundant duplication.
Browse files Browse the repository at this point in the history
For most sound chips, it makes no sense to write the same value to a
register twice (except for PSG R#13 register); some of the VGM data is
redundant, and eliminating such redundant duplicates will make the
converted output (= LA0 data) smaller/light.
  • Loading branch information
mori0091 committed Oct 13, 2024
1 parent 884b6e2 commit 5c08fc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
43 changes: 22 additions & 21 deletions tools/vgm2la/src/SoundChip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

void SoundChip::OPLL_set(uint8_t reg, uint8_t val) {
// printf("$%02x=%02x ", (int)reg, (int)val);
sample_vector.push_back(reg+0xc0);
sample_vector.push_back(val);
put(reg+0xc0, val);
soundchip_enable |= 8;
}

void SoundChip::PSG_set(uint8_t reg, uint8_t val) {
// printf("P%02x=%02x ", (int)reg, (int)val);
sample_vector.push_back(reg+0xb0);
sample_vector.push_back(val);
if (reg == 7) {
// PSG R#7 must be 010****** for MSX.
val = (val & 0xbf) | 0x80;
}
put(reg+0xb0, val);
soundchip_enable |= 4;
if (8 <= reg && reg <= 10 && val) {
channel_enable |= (1 << (reg - 8));
Expand All @@ -39,11 +41,12 @@ void SoundChip::SCC_set(uint8_t port, uint8_t reg, uint8_t val) {
if (reg < 32*4) {
uint8_t ch = reg / 32;
uint8_t idx = reg % 32;
waveform[ch][idx] = (int8_t)val;
if (!(waveform_updated & (1 << ch))) {
waveform_updated |= (1 << ch);
sample_vector.push_back(0xfa);
sample_vector.push_back(ch);
if (waveform[ch][idx] != (int8_t)val) {
waveform[ch][idx] = (int8_t)val;
if (!(waveform_updated & (1 << ch))) {
waveform_updated |= (1 << ch);
put(0xfa, ch);
}
}
soundchip_enable |= 1;
// printf("S%02x=%02x ", (int)reg, (int)val);
Expand All @@ -52,17 +55,15 @@ void SoundChip::SCC_set(uint8_t port, uint8_t reg, uint8_t val) {
case 1:
// SCC/SCC+ FDR
if (reg < 2*5) {
sample_vector.push_back(reg+0xa0);
sample_vector.push_back(val);
put(reg+0xa0, val);
soundchip_enable |= 1;
// printf("S%02x=%02x ", (int)0xa0+reg, (int)val);
}
break;
case 2:
// SCC/SCC+ Volume
if (reg < 5) {
sample_vector.push_back(reg+0xaa);
sample_vector.push_back(val);
put(reg+0xaa, val);
soundchip_enable |= 1;
if (val) {
channel_enable |= (8 << reg);
Expand All @@ -73,8 +74,7 @@ void SoundChip::SCC_set(uint8_t port, uint8_t reg, uint8_t val) {
case 3:
// SCC/SCC+ channel mask
if (reg < 1) {
sample_vector.push_back(0xaf);
sample_vector.push_back(val);
put(0xaf, val);
soundchip_enable |= 1;
// printf("Saf=%02x ", (int)val);
}
Expand All @@ -84,11 +84,12 @@ void SoundChip::SCC_set(uint8_t port, uint8_t reg, uint8_t val) {
if (reg < 32*5) {
uint8_t ch = reg / 32;
uint8_t idx = reg % 32;
waveform[ch][idx] = (int8_t)val;
if (!(waveform_updated & (1 << ch))) {
waveform_updated |= (1 << ch);
sample_vector.push_back(0xfa);
sample_vector.push_back(ch);
if (waveform[ch][idx] != (int8_t)val) {
waveform[ch][idx] = (int8_t)val;
if (!(waveform_updated & (1 << ch))) {
waveform_updated |= (1 << ch);
put(0xfa, ch);
}
}
soundchip_enable |= 2;
// printf("S%02x=%02x ", (int)reg, (int)val);
Expand Down Expand Up @@ -119,7 +120,7 @@ static uint8_t find_waveform(const SoundChip::SCC_Waveform & wave, std::vector<S
}

SoundChip::Sample SoundChip::sample(std::vector<SCC_Waveform> & wavedb) {
// printf("\n");
// printf(".\n");
if (waveform_updated) {
for (size_t i = 0; i < sample_vector.size(); i += 2) {
if (sample_vector[i] == 0xfa) {
Expand Down
12 changes: 11 additions & 1 deletion tools/vgm2la/src/SoundChip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ struct SoundChip {
using SCC_Waveform = std::array<int8_t, 32>;
using Sample = std::vector<uint8_t>;

std::array<bool, 256> updated;
std::array<uint8_t, 256> latest_value;
uint8_t soundchip_enable;
uint8_t channel_enable; // Using channels of SCC/SCC+ and PSG

uint8_t waveform_updated;
SCC_Waveform waveform[5];
Sample sample_vector;

SoundChip() : soundchip_enable(0), channel_enable(0), waveform_updated(0), waveform(), sample_vector() {}
SoundChip() : updated(), latest_value(), soundchip_enable(0), channel_enable(0), waveform_updated(0), waveform(), sample_vector() {}

/**
* Set a value to OPLL register.
Expand Down Expand Up @@ -74,6 +76,14 @@ struct SoundChip {
*/
Sample sample(std::vector<SCC_Waveform> & wavedb);

void put(uint8_t cmd, uint8_t val) {
if (cmd == 0xbd || cmd == 0xfa || !updated[cmd] || latest_value[cmd] != val) {
sample_vector.push_back(cmd);
sample_vector.push_back(val);
updated[cmd] = true;
latest_value[cmd] = val;
}
}
};

#endif // SOUNDCHIP_HPP_

0 comments on commit 5c08fc4

Please sign in to comment.