From 96aac661336405a9c00a2d907fe041d7fb1a11d3 Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Sat, 2 Nov 2024 18:46:49 +0100 Subject: [PATCH] naomi: improve performance of M1 and M4 carts dma Limit buffer size to 1 or 2 KB instead of 32 KB. Only decrypt right before dma transfer. Fixes frame drops and audio underruns in vf4evoct attract mode. Issue #1717 --- core/hw/naomi/m1cartridge.cpp | 3 ++- core/hw/naomi/m1cartridge.h | 4 ++-- core/hw/naomi/m4cartridge.cpp | 2 ++ core/hw/naomi/m4cartridge.h | 4 +++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/core/hw/naomi/m1cartridge.cpp b/core/hw/naomi/m1cartridge.cpp index bd82664ce..e32dd46d5 100644 --- a/core/hw/naomi/m1cartridge.cpp +++ b/core/hw/naomi/m1cartridge.cpp @@ -39,7 +39,6 @@ void M1Cartridge::AdvancePtr(u32 size) has_history = true; buffer_actual_size = 0; } - enc_fill(); } else NaomiCartridge::AdvancePtr(size); @@ -147,11 +146,13 @@ void M1Cartridge::Serialize(Serializer& ser) const void M1Cartridge::Deserialize(Deserializer& deser) { deser >> buffer; + deser.skip(32768 - sizeof(buffer), Deserializer::V52); deser >> dict; deser >> hist; deser >> avail_val; deser >> rom_cur_address; deser >> buffer_actual_size; + buffer_actual_size = std::min(buffer_actual_size, sizeof(buffer)); deser >> avail_bits; deser >> stream_ended; deser >> has_history; diff --git a/core/hw/naomi/m1cartridge.h b/core/hw/naomi/m1cartridge.h index f35497806..1ba034fce 100644 --- a/core/hw/naomi/m1cartridge.h +++ b/core/hw/naomi/m1cartridge.h @@ -26,6 +26,7 @@ class M1Cartridge : public NaomiCartridge { if (encryption) { + enc_fill(); size = std::min(size, (u32)sizeof(buffer)); return buffer; } @@ -48,7 +49,6 @@ class M1Cartridge : public NaomiCartridge //printf("M1 ENCRYPTION ON @ %08x\n", dma_offset); encryption = true; enc_reset(); - enc_fill(); } else encryption = false; @@ -100,7 +100,7 @@ class M1Cartridge : public NaomiCartridge u16 actel_id; - u8 buffer[32768]; + u8 buffer[1024]; u8 dict[111], hist[2]; u64 avail_val; u32 rom_cur_address, buffer_actual_size, avail_bits; diff --git a/core/hw/naomi/m4cartridge.cpp b/core/hw/naomi/m4cartridge.cpp index 20f86ef89..4e1068505 100644 --- a/core/hw/naomi/m4cartridge.cpp +++ b/core/hw/naomi/m4cartridge.cpp @@ -321,8 +321,10 @@ void M4Cartridge::Serialize(Serializer& ser) const void M4Cartridge::Deserialize(Deserializer& deser) { deser >> buffer; + deser.skip(32768 - sizeof(buffer), Deserializer::V52); deser >> rom_cur_address; deser >> buffer_actual_size; + buffer_actual_size = std::min(buffer_actual_size, sizeof(buffer)); deser >> iv; deser >> counter; deser >> encryption; diff --git a/core/hw/naomi/m4cartridge.h b/core/hw/naomi/m4cartridge.h index 7f21974a0..614f273c6 100644 --- a/core/hw/naomi/m4cartridge.h +++ b/core/hw/naomi/m4cartridge.h @@ -69,7 +69,7 @@ class M4Cartridge: public NaomiCartridge { u16 subkey2 = 0; u16 one_round[0x10000]; - u8 buffer[32768]; + u8 buffer[2048]; u32 rom_cur_address, buffer_actual_size; u16 iv; u8 counter; @@ -80,6 +80,8 @@ class M4Cartridge: public NaomiCartridge { void enc_init(); void enc_fill(); u16 decrypt_one_round(u16 word, u16 subkey); + + static_assert(sizeof(RomBootID) <= sizeof(buffer)); }; #endif /* CORE_HW_NAOMI_M4CARTRIDGE_H_ */