2010-04-25 00:31:27 +00:00
|
|
|
/*
|
2009-02-09 21:15:56 +00:00
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* 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, or (at your option)
|
|
|
|
* any later version.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-02-09 21:15:56 +00:00
|
|
|
* 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.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-02-09 21:15:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2009-02-09 21:15:56 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
#include "stdafx.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
#include "GSDump.h"
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
GSDumpBase::GSDumpBase(const std::string& fn)
|
|
|
|
: m_frames(0)
|
|
|
|
, m_extra_frames(2)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2018-07-13 23:56:49 +00:00
|
|
|
m_gs = px_fopen(fn, "wb");
|
2017-04-30 19:00:23 +00:00
|
|
|
if (!m_gs)
|
|
|
|
fprintf(stderr, "GSDump: Error failed to open %s\n", fn.c_str());
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
GSDumpBase::~GSDumpBase()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2017-04-30 19:00:23 +00:00
|
|
|
if(m_gs)
|
|
|
|
fclose(m_gs);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
void GSDumpBase::AddHeader(uint32 crc, const GSFreezeData& fd, const GSPrivRegSet* regs)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2017-04-30 19:00:23 +00:00
|
|
|
AppendRawData(&crc, 4);
|
|
|
|
AppendRawData(&fd.size, 4);
|
|
|
|
AppendRawData(fd.data, fd.size);
|
|
|
|
AppendRawData(regs, sizeof(*regs));
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
void GSDumpBase::Transfer(int index, const uint8* mem, size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
2009-03-22 13:10:31 +00:00
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
AppendRawData(0);
|
2017-06-09 13:42:39 +00:00
|
|
|
AppendRawData(static_cast<uint8>(index));
|
2017-04-30 19:00:23 +00:00
|
|
|
AppendRawData(&size, 4);
|
|
|
|
AppendRawData(mem, size);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
void GSDumpBase::ReadFIFO(uint32 size)
|
2009-03-22 13:10:31 +00:00
|
|
|
{
|
2017-04-30 19:00:23 +00:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AppendRawData(2);
|
|
|
|
AppendRawData(&size, 4);
|
2009-03-22 13:10:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
bool GSDumpBase::VSync(int field, bool last, const GSPrivRegSet* regs)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2017-04-30 19:00:23 +00:00
|
|
|
// dump file is bad, return done to delete the object
|
|
|
|
if (!m_gs)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
AppendRawData(3);
|
|
|
|
AppendRawData(regs, sizeof(*regs));
|
|
|
|
|
|
|
|
AppendRawData(1);
|
2017-06-09 13:42:39 +00:00
|
|
|
AppendRawData(static_cast<uint8>(field));
|
2017-04-30 19:00:23 +00:00
|
|
|
|
|
|
|
if (last)
|
|
|
|
m_extra_frames--;
|
|
|
|
|
|
|
|
return (++m_frames & 1) == 0 && last && (m_extra_frames < 0);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
void GSDumpBase::Write(const void *data, size_t size)
|
|
|
|
{
|
|
|
|
if (!m_gs || size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t written = fwrite(data, 1, size, m_gs);
|
|
|
|
if (written != size)
|
|
|
|
fprintf(stderr, "GSDump: Error failed to write data\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// GSDump implementation
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GSDump::GSDump(const std::string& fn, uint32 crc, const GSFreezeData& fd, const GSPrivRegSet* regs)
|
|
|
|
: GSDumpBase(fn + ".gs")
|
|
|
|
{
|
|
|
|
AddHeader(crc, fd, regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDump::AppendRawData(const void *data, size_t size)
|
|
|
|
{
|
|
|
|
Write(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDump::AppendRawData(uint8 c)
|
|
|
|
{
|
|
|
|
Write(&c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// GSDumpXz implementation
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GSDumpXz::GSDumpXz(const std::string& fn, uint32 crc, const GSFreezeData& fd, const GSPrivRegSet* regs)
|
|
|
|
: GSDumpBase(fn + ".gs.xz")
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2017-04-30 19:00:23 +00:00
|
|
|
m_strm = LZMA_STREAM_INIT;
|
|
|
|
lzma_ret ret = lzma_easy_encoder(&m_strm, 6 /*level*/, LZMA_CHECK_CRC64);
|
|
|
|
if (ret != LZMA_OK) {
|
|
|
|
fprintf(stderr, "GSDumpXz: Error initializing LZMA encoder ! (error code %u)\n", ret);
|
|
|
|
return;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2017-04-30 19:00:23 +00:00
|
|
|
|
|
|
|
AddHeader(crc, fd, regs);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:00:23 +00:00
|
|
|
GSDumpXz::~GSDumpXz()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2017-04-30 19:00:23 +00:00
|
|
|
Flush();
|
|
|
|
|
|
|
|
// Finish the stream
|
|
|
|
m_strm.avail_in = 0;
|
|
|
|
Compress(LZMA_FINISH, LZMA_STREAM_END);
|
|
|
|
|
|
|
|
lzma_end(&m_strm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDumpXz::AppendRawData(const void *data, size_t size)
|
|
|
|
{
|
|
|
|
size_t old_size = m_in_buff.size();
|
|
|
|
m_in_buff.resize(old_size + size);
|
|
|
|
memcpy(&m_in_buff[old_size], data, size);
|
|
|
|
|
|
|
|
// Enough data was accumulated, time to write/compress it. If compression
|
|
|
|
// is enabled, it will freeze PCSX2. 1GB should be enough for long dump.
|
|
|
|
//
|
|
|
|
// Note: long dumps are currently not supported so this path won't be executed
|
|
|
|
if (m_in_buff.size() > 1024*1024*1024)
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDumpXz::AppendRawData(uint8 c)
|
|
|
|
{
|
|
|
|
m_in_buff.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDumpXz::Flush()
|
|
|
|
{
|
|
|
|
if (m_in_buff.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_strm.next_in = m_in_buff.data();
|
|
|
|
m_strm.avail_in = m_in_buff.size();
|
|
|
|
|
|
|
|
Compress(LZMA_RUN, LZMA_OK);
|
|
|
|
|
|
|
|
m_in_buff.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDumpXz::Compress(lzma_action action, lzma_ret expected_status)
|
|
|
|
{
|
2017-05-24 17:18:52 +00:00
|
|
|
std::vector<uint8> out_buff(1024*1024);
|
2017-04-30 19:00:23 +00:00
|
|
|
do {
|
|
|
|
m_strm.next_out = out_buff.data();
|
|
|
|
m_strm.avail_out = out_buff.size();
|
|
|
|
|
|
|
|
lzma_ret ret = lzma_code(&m_strm, action);
|
|
|
|
|
|
|
|
if (ret != expected_status) {
|
|
|
|
fprintf (stderr, "GSDumpXz: Error %d\n", (int) ret);
|
|
|
|
return;
|
2009-03-22 13:10:31 +00:00
|
|
|
}
|
2017-04-30 19:00:23 +00:00
|
|
|
|
|
|
|
size_t write_size = out_buff.size() - m_strm.avail_out;
|
|
|
|
Write(out_buff.data(), write_size);
|
|
|
|
|
|
|
|
} while (m_strm.avail_out == 0);
|
2009-03-22 13:10:31 +00:00
|
|
|
}
|