Skip to content

Commit

Permalink
Merge pull request #45 from vanfanel/more_1.31_changes
Browse files Browse the repository at this point in the history
Add more upstream 1.31 changes.
  • Loading branch information
LibretroAdmin authored Oct 21, 2024
2 parents 3c12611 + 6cb4502 commit 319b102
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SOURCES_CXX += \
SOURCES_CXX += \
$(CORE_EMU_DIR)/cart/backup.cpp \
$(CORE_EMU_DIR)/cart/cs1ram.cpp \
$(CORE_EMU_DIR)/cart/debug.cpp \
$(CORE_EMU_DIR)/cart/bootrom.cpp \
$(CORE_EMU_DIR)/cart/extram.cpp \
$(CORE_EMU_DIR)/cart/rom.cpp \
$(CORE_EMU_DIR)/cart/ar4mp.cpp
Expand Down
95 changes: 95 additions & 0 deletions mednafen/hash/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,101 @@ sha256_digest sha256(const void* data, const uint64 len)
return ret;
}

sha256_hasher::sha256_hasher()
{
reset();
}

void sha256_hasher::reset(void)
{
buf_count = 0;
bytes_processed = 0;

h = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
}

INLINE void sha256_hasher::process_block(const uint8* blk_data)
{
alignas(16) uint32 w[64];
alignas(16) auto v = h;

for(unsigned t = 0; t < 16; t++)
w[t] = MDFN_de32msb(blk_data + (t << 2));

for(unsigned t = 16; t < 64; t++)
w[t] = ls1(w[t - 2]) + w[t - 7] + ls0(w[t - 15]) + w[t - 16];

for(unsigned t = 0; t < 64; t++)
{
uint32 T1 = v[7] + bs1(v[4]) + ch(v[4], v[5], v[6]) + K[t] + w[t];
uint32 T2 = bs0(v[0]) + maj(v[0], v[1], v[2]);

v[7] = v[6];
v[6] = v[5];
v[5] = v[4];
v[4] = v[3] + T1;
v[3] = v[2];
v[2] = v[1];
v[1] = v[0];
v[0] = T1 + T2;
}

for(unsigned i = 0; i < h.size(); i++)
h[i] += v[i];
}

void sha256_hasher::process(const void* data, size_t len)
{
uint8* d8 = (uint8*)data;

bytes_processed += len;

while(len)
{
if(buf_count || len < 0x40)
{
const size_t copy_len = std::min<size_t>(0x40 - buf_count, len);

memcpy(&buf[buf_count], d8, copy_len);
len -= copy_len;
d8 += copy_len;
buf_count += copy_len;
if(buf_count == 0x40)
{
process_block(buf);
buf_count = 0;
}
}
else
{
process_block(d8);
d8 += 0x40;
len -= 0x40;
}
}
}


sha256_digest sha256_hasher::digest(void) const
{
sha256_digest ret;
sha256_hasher tmp = *this;
const size_t footer_len = ((buf_count <= (0x40 - 9)) ? 0x40 : 0x80) - buf_count;
alignas(16) uint8 footer[0x80];

memset(footer, 0, sizeof(footer));
footer[0] |= 0x80;

MDFN_en64msb(&footer[footer_len - 8], bytes_processed * 8);

tmp.process(footer, footer_len);

for(unsigned i = 0; i < 8; i++)
MDFN_en32msb(&ret[i * 4], tmp.h[i]);

return ret;
}

//sha256_digest goomba = "dccac470d07efd7f989c1f9a5045bc2cfe446622dbb50d4ad7f53996e574cd29"_sha256;
void sha256_test(void)
{
Expand Down
46 changes: 46 additions & 0 deletions mednafen/hash/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,52 @@
#include <array>

typedef std::array<uint8, 32> sha256_digest;
class sha256_hasher
{
public:

sha256_hasher();

void reset(void);

void process(const void* data, size_t len);

INLINE void process_cstr(const char* s)
{
return process(s, strlen(s));
}

template<typename T>
INLINE void process_scalar(const T v)
{
if(std::is_same<T, bool>::value)
{
uint8 tmp = v;

process(&tmp, 1);
}
else
{
alignas(T) uint8 tmp[sizeof(T)];

MDFN_enlsb(&tmp[0], v, sizeof(v[0]));

process(tmp, sizeof(tmp));
}
}

sha256_digest digest(void) const;

private:

void process_block(const uint8* data);
std::array<uint32, 8> h;

uint8 buf[64];
size_t buf_count;

uint64 bytes_processed;
};

void sha256_test(void);
sha256_digest sha256(const void* data, const uint64 len);
Expand Down
8 changes: 8 additions & 0 deletions mednafen/mednafen-endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ static inline void MDFN_en32lsb(uint8_t *buf, uint32_t morp)
buf[3]=morp>>24;
}

static inline void MDFN_enlsb(void* buf, void* value, size_t size)
{
if (size == 2) // 16 bits (2 bytes)
MDFN_en16lsb((uint8_t*)buf, *(uint16_t*)value);
else if (size == 4) // 32 bits (4 bytes)
MDFN_en32lsb((uint8_t*)buf, *(uint32_t*)value);
}

static inline void MDFN_en16msb(uint8_t *buf, uint16_t morp)
{
buf[0] = morp >> 8;
Expand Down
20 changes: 16 additions & 4 deletions mednafen/ss/cart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "cart.h"
#include "cart/backup.h"
#include "cart/cs1ram.h"
#include "cart/debug.h"
#include "cart/bootrom.h"
#include "cart/extram.h"
//#include "cart/nlmodem.h"
#include "cart/rom.h"
Expand Down Expand Up @@ -191,9 +191,21 @@ void CART_Init(const int cart_type)
CART_CS1RAM_Init(&Cart);
break;

case CART_MDFN_DEBUG:
CART_Debug_Init(&Cart);
break;
case CART_BOOTROM:
{
const std::string path_cxx = MDFN_GetSettingS("ss.cart.bootrom_path");
const char *path = MDFN_MakeFName(MDFNMKF_FIRMWARE, 0, path_cxx.c_str());
RFILE *fp = filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);

if (fp)
{
CART_BootROM_Init(&Cart, fp);
filestream_close(fp);
}
}
break;

// case CART_NLMODEM:
// CART_NLModem_Init(&Cart);
Expand Down
2 changes: 1 addition & 1 deletion mednafen/ss/cart.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum

CART_NLMODEM = 0x600,

CART_MDFN_DEBUG = 0xF00
CART_BOOTROM = 0xF00
};

void CART_Init(const int cart_type) MDFN_COLD;
Expand Down
117 changes: 117 additions & 0 deletions mednafen/ss/cart/bootrom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/******************************************************************************/
/* Mednafen Sega Saturn Emulation Module */
/******************************************************************************/
/* bootrom.cpp - Bootable ROM cart emulation
** Copyright (C) 2017-2023 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "common.h"
#include "bootrom.h"
#include "backup.h"

#include <mednafen/hash/sha256.h>

static uint16* ROM = nullptr;
static uint32 ROM_Mask[2];

static MDFN_HOT void CS0_ROM_Read(uint32 A, uint16* DB)
{
const uint32 offs = (A - 0x02000000) & ROM_Mask[0];

*DB = ne16_rbo_be<uint16>(ROM, offs);
}

static MDFN_HOT void CS1_ROM_Read(uint32 A, uint16* DB)
{
const uint32 offs = 0x02000000 + ((A - 0x04000000) & ROM_Mask[1]);

*DB = ne16_rbo_be<uint16>(ROM, offs);
}

static MDFN_COLD void Kill(void)
{
if(ROM)
{
delete[] ROM;
ROM = nullptr;
}
}

void CART_BootROM_Init(CartInfo* c, RFILE* str)
{
try
{
const uint64 ss = filestream_get_size(str);
const uint64 min_size = 1;
const uint64 max_size = 0x3000000;

if(ss < min_size)
throw MDFN_Error(0, _("Bootable Saturn cart ROM image is smaller than the minimum of %llu bytes."), (unsigned long long)min_size);

if(ss > max_size)
throw MDFN_Error(0, _("Bootable Saturn cart ROM image is larger than the maximum of %llu bytes."), (unsigned long long)max_size);
//
//
uint32 ROM_Size;

if(ss > 0x2000000)
ROM_Size = 0x2000000 + round_up_pow2((ss - 0x2000000 + 0xFFFF) &~ 0xFFFF);
else
ROM_Size = round_up_pow2((ss + 0xFFFF) &~ 0xFFFF);

assert(ROM_Size >= ss);
//
//
sha256_hasher h;
sha256_digest dig;

ROM = new uint16[ROM_Size / sizeof(uint16)];
memset(ROM, 0x00, ROM_Size);
filestream_read(str, ROM, ss);
h.process(ROM, ss);
dig = h.digest();
memcpy(MDFNGameInfo->MD5, dig.data(), 16);

for(unsigned i = 0; i < ROM_Size / sizeof(uint16); i++)
ROM[i] = MDFN_de16msb<true>(&ROM[i]);

SS_SetPhysMemMap (0x02000000, 0x03FFFFFF, ROM, std::min<uint32>(0x02000000, ROM_Size), false);
c->CS01_SetRW8W16(0x02000000, 0x03FFFFFF, CS0_ROM_Read);

c->Kill = Kill;

ROM_Mask[0] = (round_up_pow2(ROM_Size) - 1) & 0x01FFFFFE;

if(ROM_Size > 0x2000000)
{
ROM_Mask[1] = (round_up_pow2(ROM_Size - 0x2000000) - 1) & 0x00FFFFFE;

SS_SetPhysMemMap (0x04000000, 0x04FFFFFF, ROM + (0x02000000 / sizeof(uint16)), ROM_Size - 0x02000000, false);
c->CS01_SetRW8W16(0x04000000, 0x04FFFFFF, CS1_ROM_Read);
}
else
{
CART_Backup_Init(c);
assert(c->Kill == Kill);
}
}
catch(...)
{
Kill();
throw;
}
}
29 changes: 29 additions & 0 deletions mednafen/ss/cart/bootrom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/******************************************************************************/
/* Mednafen Sega Saturn Emulation Module */
/******************************************************************************/
/* bootrom.h - Bootable ROM cart emulation
** Copyright (C) 2023 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef __MDFN_SS_CART_BOOTROM_H
#define __MDFN_SS_CART_BOOTROM_H

#include <streams/file_stream.h>

void CART_BootROM_Init(CartInfo* c, RFILE* str) MDFN_COLD;

#endif
7 changes: 7 additions & 0 deletions mednafen/ss/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ void SOUND_Reset68K(void)
SoundCPU.Reset(false);
}

void SOUND_ResetSCSP(void)
{
SCSP.Reset(false);
}

void SOUND_Kill(void)
{
}
Expand Down Expand Up @@ -373,6 +378,8 @@ static MDFN_FASTCALL void SoundCPU_BusRMW(uint32 A, uint8 (MDFN_FASTCALL *cb)(M6

static MDFN_FASTCALL unsigned SoundCPU_BusIntAck(uint8 level)
{
SoundCPU.timestamp += 10;

return M68K::BUS_INT_ACK_AUTO;
}

Expand Down
1 change: 1 addition & 0 deletions mednafen/ss/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void SOUND_Kill(void) MDFN_COLD;

void SOUND_Set68KActive(bool active);
void SOUND_Reset68K(void);
void SOUND_ResetSCSP(void);

void SOUND_SetClockRatio(uint32 ratio); // Ratio between SH-2 clock and 68K clock (sound clock / 2)
sscpu_timestamp_t SOUND_Update(sscpu_timestamp_t timestamp);
Expand Down
Loading

0 comments on commit 319b102

Please sign in to comment.