From 2abd72ff866420e20685e41be74431c30b9b5475 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Tue, 17 Dec 2024 23:03:57 +0100 Subject: [PATCH] added PAL60 signature check to auto detect display type (resolves #1061) --- src/emucore/Console.cxx | 46 ++++++++++++++++++++++++++++++++++++++++- src/emucore/Console.hxx | 20 ++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index 55dbc9c8c..bcc9d7d8c 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -171,8 +171,10 @@ Console::Console(OSystem& osystem, unique_ptr& cart, myDevSettingsHandler = make_unique(myOSystem); // Auto-detect NTSC/PAL mode if it's requested - if (myDisplayFormat == "AUTO") + if(myDisplayFormat == "AUTO") myDisplayFormat = formatFromFilename(); + if(myDisplayFormat == "AUTO") + myDisplayFormat = formatFromSignature(); // Add the real controllers for this system // This must be done before the debugger is initialized @@ -386,6 +388,48 @@ string Console::formatFromFilename() const return "AUTO"; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +string Console::formatFromSignature() const +{ + static constexpr uInt8 PAL60[] = { 'P', 'A', 'L', '6', '0'}; + static constexpr uInt8 PAL_60[] = { 'P', 'A', 'L', ' ', '6', '0'}; + static constexpr uInt8 PAL__60[] = { 'P', 'A', 'L', '-', '6', '0'}; + + size_t size; + const ByteBuffer& image = myCart->getImage(size); + + if(searchForBytes(image, size, PAL60, 5) || + searchForBytes(image, size, PAL_60, 6) || + searchForBytes(image, size, PAL__60, 6)) + return "PAL60"; + // Nothing found + return "AUTO"; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool Console::searchForBytes(const ByteBuffer& image, size_t imagesize, + const uInt8* signature, uInt32 sigsize) const +{ + if(imagesize >= sigsize) + for(uInt32 i = 0; i < imagesize - sigsize; ++i) + { + uInt32 matches = 0; + for(uInt32 j = 0; j < sigsize; ++j) + { + if(image[i + j] == signature[j]) + ++matches; + else + break; + } + if(matches == sigsize) + { + return true; + } + } + + return false; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Console::save(Serializer& out) const { diff --git a/src/emucore/Console.hxx b/src/emucore/Console.hxx index 510b6a167..f58e842cb 100644 --- a/src/emucore/Console.hxx +++ b/src/emucore/Console.hxx @@ -420,6 +420,26 @@ class Console : public Serializable, public ConsoleIO */ string formatFromFilename() const; + /** + * Determine display format by signature string (only PAL?60 so far) + * Returns "AUTO" if nothing is found + */ + string formatFromSignature() const; + + /** + Search the image for the specified byte signature. + + @param image A pointer to the ROM image + @param imagesize The size of the ROM image + @param signature The byte sequence to search for + @param sigsize The number of bytes in the signature + + @return True if the signature was found, else false + */ + bool searchForBytes(const ByteBuffer& image, size_t imagesize, + const uInt8* signature, uInt32 sigsize) const; + + /** Create the audio queue */