Update k573dio/k573fpga to use a proper algorithm for decrypting ddrsbm audio + volume fix (#5071)

* Fix volume bug

* Implement decryption algorithm for DDR Solo Bass Mix which does not rely
on premade tables/keys.

* This shift is useless

* Make use of bitswap
This commit is contained in:
Windy Fairy 2019-05-20 01:39:37 +09:00 committed by R. Belmont
parent 5f7d463268
commit c05732d212
7 changed files with 33 additions and 156 deletions

View File

@ -257,12 +257,12 @@ int gain_to_db(double val) {
}
float gain_to_percentage(int val) {
double db = gain_to_db(val);
if (db == 0) {
if (val == 0) {
return 0; // Special case for muting it seems
}
double db = gain_to_db(val);
return powf(10.0, (db + 6) / 20.0);
}

View File

@ -2350,7 +2350,7 @@ void ksys573_state::ddrsbm(machine_config &config)
{
k573d(config);
subdevice<k573dio_device>("k573dio")->output_callback().set(FUNC(ksys573_state::ddrsolo_output_callback));
subdevice<k573dio_device>("k573dio")->set_fake_fpga(true);
subdevice<k573dio_device>("k573dio")->set_ddrsbm_fpga(true);
cassyi(config);
}

View File

@ -112,7 +112,7 @@ k573dio_device::k573dio_device(const machine_config &mconfig, const char *tag, d
k573fpga(*this, "k573fpga"),
digital_id(*this, "digital_id"),
output_cb(*this),
is_fake_fpga(false),
is_ddrsbm_fpga(false),
buffer_speed(0x400)
{
}
@ -125,7 +125,7 @@ void k573dio_device::device_start()
save_pointer(NAME(ram), 32 * 1024 * 1024 );
k573fpga->set_ram(ram.get());
k573fpga->set_fake_fpga(is_fake_fpga);
k573fpga->set_ddrsbm_fpga(is_ddrsbm_fpga);
k573fpga->set_buffer_speed(buffer_speed);
k573fpga->set_mp3_dynamic_base(mp3_dynamic_base);

View File

@ -19,7 +19,7 @@ public:
required_device<ds2401_device> digital_id;
void amap(address_map &map);
void set_fake_fpga(bool flag) { is_fake_fpga = flag; }
void set_ddrsbm_fpga(bool flag) { is_ddrsbm_fpga = flag; }
void set_buffer_speed(uint32_t speed) { buffer_speed = speed; }
void set_mp3_dynamic_base(uint32_t base) { mp3_dynamic_base = base; }
@ -83,7 +83,7 @@ private:
void output(int offset, uint16_t data);
bool is_fake_fpga;
bool is_ddrsbm_fpga;
uint32_t buffer_speed, mp3_dynamic_base;
};

View File

@ -1,120 +0,0 @@
// license:BSD-3-Clause
// copyright-holders:windyfairy
#ifndef MAME_MACHINE_K573ENC_H
#define MAME_MACHINE_K573ENC_H
#pragma once
#include <map>
std::unordered_map<uint16_t, uint32_t> k573enc_lookup({
{ 0x031a, 0 },
{ 0x038e, 1 },
{ 0x0aa7, 2 },
{ 0x0c51, 3 },
{ 0x0e34, 4 },
{ 0x14d9, 5 },
{ 0x154d, 6 },
{ 0x18a0, 7 },
{ 0x18da, 8 },
{ 0x1c67, 9 },
{ 0x20dc, 10 },
{ 0x21d6, 11 },
{ 0x2698, 12 },
{ 0x28b6, 13 },
{ 0x2a5f, 14 },
{ 0x2c7c, 15 },
{ 0x3009, 16 },
{ 0x3395, 17 },
{ 0x3505, 18 },
{ 0x3722, 19 },
{ 0x38cb, 20 },
{ 0x3aaf, 21 },
{ 0x3e3b, 22 },
{ 0x41c8, 23 },
{ 0x4371, 24 },
{ 0x451b, 25 },
{ 0x4555, 26 },
{ 0x46c4, 27 },
{ 0x48e1, 28 },
{ 0x4c6e, 29 },
{ 0x4fc0, 30 },
{ 0x4ffa, 31 },
{ 0x5387, 32 },
{ 0x5530, 33 },
{ 0x56b4, 34 },
{ 0x5714, 35 },
{ 0x5c10, 36 },
{ 0x5c83, 37 },
{ 0x5dcd, 38 },
{ 0x5fd6, 39 },
{ 0x6010, 40 },
{ 0x617f, 41 },
{ 0x6546, 42 },
{ 0x6729, 43 },
{ 0x685f, 44 },
{ 0x6dcf, 45 },
{ 0x6e43, 46 },
{ 0x6fec, 47 },
{ 0x7195, 48 },
{ 0x755c, 49 },
{ 0x78e8, 50 },
{ 0x7c3b, 51 }
});
uint8_t k573enc_keys[52][16] = {
{ 0x14, 0x26, 0x28, 0x4c, 0x50, 0x98, 0xa0, 0x31, 0x41, 0x62, 0x82, 0xc4, 0x05, 0x89, 0x0a, 0x13 },
{ 0x12, 0x36, 0x24, 0x6c, 0x48, 0xd8, 0x90, 0xb1, 0x21, 0x63, 0x42, 0xc6, 0x84, 0x8d, 0x09, 0x1b },
{ 0x03, 0x7a, 0x06, 0xf4, 0x0c, 0xe9, 0x18, 0xd3, 0x30, 0xa7, 0x60, 0x4f, 0xc0, 0x9e, 0x81, 0x3d },
{ 0x2d, 0x40, 0x5a, 0x80, 0xb4, 0x01, 0x69, 0x02, 0xd2, 0x04, 0xa5, 0x08, 0x4b, 0x10, 0x96, 0x20 },
{ 0x26, 0x68, 0x4c, 0xd0, 0x98, 0xa1, 0x31, 0x43, 0x62, 0x86, 0xc4, 0x0d, 0x89, 0x1a, 0x13, 0x34 },
{ 0x6d, 0x14, 0xda, 0x28, 0xb5, 0x50, 0x6b, 0xa0, 0xd6, 0x41, 0xad, 0x82, 0x5b, 0x05, 0xb6, 0x0a },
{ 0x7b, 0x04, 0xf6, 0x08, 0xed, 0x10, 0xdb, 0x20, 0xb7, 0x40, 0x6f, 0x80, 0xde, 0x01, 0xbd, 0x02 },
{ 0x40, 0x58, 0x80, 0xb0, 0x01, 0x61, 0x02, 0xc2, 0x04, 0x85, 0x08, 0x0b, 0x10, 0x16, 0x20, 0x2c },
{ 0x4c, 0x56, 0x98, 0xac, 0x31, 0x59, 0x62, 0xb2, 0xc4, 0x65, 0x89, 0xca, 0x13, 0x95, 0x26, 0x2b },
{ 0x6b, 0x4a, 0xd6, 0x94, 0xad, 0x29, 0x5b, 0x52, 0xb6, 0xa4, 0x6d, 0x49, 0xda, 0x92, 0xb5, 0x25 },
{ 0x0e, 0x94, 0x1c, 0x29, 0x38, 0x52, 0x70, 0xa4, 0xe0, 0x49, 0xc1, 0x92, 0x83, 0x25, 0x07, 0x4a },
{ 0x1e, 0x92, 0x3c, 0x25, 0x78, 0x4a, 0xf0, 0x94, 0xe1, 0x29, 0xc3, 0x52, 0x87, 0xa4, 0x0f, 0x49 },
{ 0x24, 0xb4, 0x48, 0x69, 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0xb5, 0xdd, 0x6b },
{ 0xa9, 0xe2, 0x53, 0xc5, 0xa6, 0x8b, 0x4d, 0x17, 0x9a, 0x2e, 0x35, 0x5c, 0x6a, 0xb8, 0xd4, 0x71 },
{ 0xba, 0xfc, 0x75, 0xf9, 0xea, 0xf3, 0xd5, 0xe7, 0xab, 0xcf, 0x57, 0x9f, 0xae, 0x3f, 0x5d, 0x7e },
{ 0xd7, 0x90, 0xaf, 0x21, 0x5f, 0x42, 0xbe, 0x84, 0x7d, 0x09, 0xfa, 0x12, 0xf5, 0x24, 0xeb, 0x48 },
{ 0xfe, 0x84, 0xfd, 0x09, 0xfb, 0x12, 0xf7, 0x24, 0xef, 0x48, 0xdf, 0x90, 0xbf, 0x21, 0x7f, 0x42 },
{ 0xc8, 0xdc, 0x91, 0xb9, 0x23, 0x73, 0x46, 0xe6, 0x8c, 0xcd, 0x19, 0x9b, 0x32, 0x37, 0x64, 0x6e },
{ 0xe5, 0xce, 0xcb, 0x9d, 0x97, 0x3b, 0x2f, 0x76, 0x5e, 0xec, 0xbc, 0xd9, 0x79, 0xb3, 0xf2, 0x67 }
};
#endif // MAME_MACHINE_K573ENC_H

View File

@ -4,7 +4,6 @@
#include "speaker.h"
#include "k573fpga.h"
#include "k573enc.h"
// TODO: Check if this is optimal
#define MINIMUM_BUFFER 1
@ -22,7 +21,7 @@ k573fpga_device::k573fpga_device(const machine_config &mconfig, const char *tag,
device_t(mconfig, KONAMI_573_DIGITAL_FPGA, tag, owner, clock),
mas3507d(*this, "mpeg"),
m_samples(*this, "samples"),
use_fake_fpga(false)
use_ddrsbm_fpga(false)
{
}
@ -178,15 +177,6 @@ void k573fpga_device::set_mpeg_ctrl(uint16_t data)
}
}
int32_t k573fpga_device::find_enc_key()
{
if (k573enc_lookup.find(crypto_key1) == k573enc_lookup.end()) {
return -1;
}
return k573enc_lookup[crypto_key1];
}
inline uint16_t k573fpga_device::fpga_decrypt_byte_real(uint16_t v)
{
uint16_t m = crypto_key1 ^ crypto_key2;
@ -231,17 +221,24 @@ inline uint16_t k573fpga_device::fpga_decrypt_byte_real(uint16_t v)
return (v >> 8) | ((v & 0xff) << 8);
}
inline uint16_t k573fpga_device::fpga_decrypt_byte_fake(uint16_t data, uint32_t crypto_idx)
inline uint16_t k573fpga_device::fpga_decrypt_byte_ddrsbm(uint16_t data, uint32_t crypto_idx)
{
// Not the real algorithm. Only used for DDR Solo Bass Mix (ddrsbm)
int32_t crypto_enc_idx = find_enc_key();
uint8_t key[16] = {0};
uint16_t key_state = bitswap<16>(
crypto_key1,
13, 11, 9, 7,
5, 3, 1, 15,
14, 12, 10, 8,
6, 4, 2, 0
);
if (crypto_enc_idx == -1) {
return data;
for (int i = 0; i < 8; i++) {
key[i * 2] = key_state & 0xff;
key[i * 2 + 1] = (key_state >> 8) & 0xff;
key_state = (((((key_state >> 8) >> 7) & 1) | (((key_state >> 8) << 1) & 0xff)) << 8) |
(((key_state & 0xff) >> 7) & 1) | (((key_state & 0xff) << 1) & 0xff);
}
uint8_t *key = k573enc_keys[crypto_enc_idx];
uint16_t output_word = 0;
for (int cur_bit = 0; cur_bit < 8; cur_bit++) {
int even_bit_shift = (cur_bit * 2) & 0xff;
@ -277,7 +274,7 @@ SAMPLES_UPDATE_CB_MEMBER(k573fpga_device::k573fpga_stream_update)
int new_samples = 0;
if (!use_fake_fpga && mp3_start_adr != mp3_dynamic_base) {
if (!use_ddrsbm_fpga && mp3_start_adr != mp3_dynamic_base) {
// Base addresses are kind of a "hack" and should be dealt with eventually.
// Files that are loaded at he base address are dynamic, meaning the data constantly changes.
// Data will not get loaded outside of the base address region past the boot sequence, however.
@ -295,13 +292,13 @@ SAMPLES_UPDATE_CB_MEMBER(k573fpga_device::k573fpga_stream_update)
crypto_key3 = orig_crypto_key3;
for (int32_t cur_idx = mp3_start_adr; cur_idx < mp3_end_adr; cur_idx += 2) {
if (use_fake_fpga) {
ram_swap[cur_idx >> 1] = fpga_decrypt_byte_fake(ram[cur_idx >> 1], (cur_idx - mp3_start_adr) / 2);
if (use_ddrsbm_fpga) {
ram_swap[cur_idx >> 1] = fpga_decrypt_byte_ddrsbm(ram[cur_idx >> 1], (cur_idx - mp3_start_adr) / 2);
} else {
ram_swap[cur_idx >> 1] = fpga_decrypt_byte_real(ram[cur_idx >> 1]);
}
if (!use_fake_fpga) {
if (!use_ddrsbm_fpga) {
crypto_key1 = (crypto_key1 & 0x8000) | ((crypto_key1 << 1) & 0x7FFE) | ((crypto_key1 >> 14) & 1);
if ((((crypto_key1 >> 15) ^ crypto_key1) & 1) != 0) {
@ -355,13 +352,13 @@ SAMPLES_UPDATE_CB_MEMBER(k573fpga_device::k573fpga_stream_update)
int32_t cur_idx;
for (cur_idx = mp3_last_decrypt_adr; cur_idx - mp3_last_decrypt_adr < decrypt_buffer_speed * decrypt_buffer && cur_idx < mp3_end_adr; cur_idx += 2) {
if (use_fake_fpga) {
ram_swap[cur_idx >> 1] = fpga_decrypt_byte_fake(ram[cur_idx >> 1], (cur_idx - mp3_start_adr) / 2);
if (use_ddrsbm_fpga) {
ram_swap[cur_idx >> 1] = fpga_decrypt_byte_ddrsbm(ram[cur_idx >> 1], (cur_idx - mp3_start_adr) / 2);
} else {
ram_swap[cur_idx >> 1] = fpga_decrypt_byte_real(ram[cur_idx >> 1]);
}
if (!use_fake_fpga) {
if (!use_ddrsbm_fpga) {
crypto_key1 = (crypto_key1 & 0x8000) | ((crypto_key1 << 1) & 0x7FFE) | ((crypto_key1 >> 14) & 1);
if ((((crypto_key1 >> 15) ^ crypto_key1) & 1) != 0) {

View File

@ -25,7 +25,7 @@ public:
required_device<mas3507d_device> mas3507d;
required_device<samples_device> m_samples;
void set_fake_fpga(bool flag) { use_fake_fpga = flag; }
void set_ddrsbm_fpga(bool flag) { use_ddrsbm_fpga = flag; }
void set_ram(uint16_t *v) { ram = v; }
@ -63,7 +63,7 @@ private:
uint8_t crypto_key3, orig_crypto_key3;
uint32_t mp3_start_adr, mp3_end_adr, mpeg_ctrl_flag;
bool use_fake_fpga;
bool use_ddrsbm_fpga;
uint32_t mp3_last_frame, mp3_last_adr, mp3_next_sync, mp3_last_decrypt_adr;
int16_t *channel_l_pcm, *channel_r_pcm;
@ -82,7 +82,7 @@ private:
int32_t find_enc_key();
uint16_t fpga_decrypt_byte_real(uint16_t data);
uint16_t fpga_decrypt_byte_fake(uint16_t data, uint32_t crypto_idx);
uint16_t fpga_decrypt_byte_ddrsbm(uint16_t data, uint32_t crypto_idx);
SAMPLES_UPDATE_CB_MEMBER(k573fpga_stream_update);
};