File: GSDump.cpp

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,464 kB
  • sloc: cpp: 299,796; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (194 lines) | stat: -rw-r--r-- 4,665 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 *	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.
 *
 *  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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "stdafx.h"
#include "GSDump.h"

GSDumpBase::GSDumpBase(const std::string& fn)
	: m_frames(0)
	, m_extra_frames(2)
{
	m_gs = px_fopen(fn, "wb");
	if (!m_gs)
		fprintf(stderr, "GSDump: Error failed to open %s\n", fn.c_str());
}

GSDumpBase::~GSDumpBase()
{
	if(m_gs)
		fclose(m_gs);
}

void GSDumpBase::AddHeader(uint32 crc, const GSFreezeData& fd, const GSPrivRegSet* regs)
{
	AppendRawData(&crc, 4);
	AppendRawData(&fd.size, 4);
	AppendRawData(fd.data, fd.size);
	AppendRawData(regs, sizeof(*regs));
}

void GSDumpBase::Transfer(int index, const uint8* mem, size_t size)
{
	if (size == 0)
		return;

	AppendRawData(0);
	AppendRawData(static_cast<uint8>(index));
	AppendRawData(&size, 4);
	AppendRawData(mem, size);
}

void GSDumpBase::ReadFIFO(uint32 size)
{
	if (size == 0)
		return;

	AppendRawData(2);
	AppendRawData(&size, 4);
}

bool GSDumpBase::VSync(int field, bool last, const GSPrivRegSet* regs)
{
	// dump file is bad, return done to delete the object
	if (!m_gs)
		return true;

	AppendRawData(3);
	AppendRawData(regs, sizeof(*regs));

	AppendRawData(1);
	AppendRawData(static_cast<uint8>(field));

	if (last)
		m_extra_frames--;

	return (++m_frames & 1) == 0 && last && (m_extra_frames < 0);
}

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")
{
	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;
	}

	AddHeader(crc, fd, regs);
}

GSDumpXz::~GSDumpXz()
{
	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)
{
	std::vector<uint8> out_buff(1024*1024);
	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;
		}

		size_t write_size = out_buff.size() - m_strm.avail_out;
		Write(out_buff.data(), write_size);

	} while (m_strm.avail_out == 0);
}