bsnes/snesreader/snesreader.cpp

228 lines
6.1 KiB
C++
Raw Normal View History

Include all the code from the bsnes v068 tarball. byuu describes the changes since v067: This release officially introduces the accuracy and performance cores, alongside the previously-existing compatibility core. The accuracy core allows the most accurate SNES emulation ever seen, with every last processor running at the lowest possible clock synchronization level. The performance core allows slower computers the chance to finally use bsnes. It is capable of attaining 60fps in standard games even on an entry-level Intel Atom processor, commonly found in netbooks. The accuracy core is absolutely not meant for casual gaming at all. It is meant solely for getting as close to 100% perfection as possible, no matter the cost to speed. It should only be used for testing, development or debugging. The compatibility core is identical to bsnes v067 and earlier, but is now roughly 10% faster. This is the default and recommended core for casual gaming. The performance core contains an entirely new S-CPU core, with range-tested IRQs; and uses blargg's heavily-optimized S-DSP core directly. Although there are very minor accuracy tradeoffs to increase speed, I am confident that the performance core is still more accurate and compatible than any other SNES emulator. The S-CPU, S-SMP, S-DSP, SuperFX and SA-1 processors are all clock-based, just as in the accuracy and compatibility cores; and as always, there are zero game-specific hacks. Its compatibility is still well above 99%, running even the most challenging games flawlessly. If you have held off from using bsnes in the past due to its system requirements, please give the performance core a try. I think you will be impressed. I'm also not finished: I believe performance can be increased even further. I would also strongly suggest Windows Vista and Windows 7 users to take advantage of the new XAudio2 driver by OV2. Not only does it give you a performance boost, it also lowers latency and provides better sound by way of skipping an API emulation layer. Changelog: - Split core into three profiles: accuracy, compatibility and performance - Accuracy core now takes advantage of variable-bitlength integers (eg uint24_t) - Performance core uses a new S-CPU core, written from scratch for speed - Performance core uses blargg's snes_dsp library for S-DSP emulation - Binaries are now compiled using GCC 4.5 - Added a workaround in the SA-1 core for a bug in GCC 4.5+ - The clock-based S-PPU renderer has greatly improved OAM emulation; fixing Winter Gold and Megalomania rendering issues - Corrected pseudo-hires color math in the clock-based S-PPU renderer; fixing Super Buster Bros backgrounds - Fixed a clamping bug in the Cx4 16-bit triangle operation [Jonas Quinn]; fixing Mega Man X2 "gained weapon" star background effect - Updated video renderer to properly handle mixed-resolution screens with interlace enabled; fixing Air Strike Patrol level briefing screen - Added mightymo's 2010-08-19 cheat code pack - Windows port: added XAudio2 output support [OV2] - Source: major code restructuring; virtual base classes for processor - cores removed, build system heavily modified, etc.
2010-08-22 01:02:42 +00:00
#include "snesreader.hpp"
#if defined(_WIN32)
#define dllexport __declspec(dllexport)
#else
#define dllexport
#endif
#include "fex/fex.h"
#include "libjma/jma.h"
extern "C" char* uncompressStream(int, int); //micro-bunzip
#define QT_CORE_LIB
#include <QtGui>
#include <nall/file.hpp>
#include <nall/string.hpp>
using namespace nall;
dllexport const char* snesreader_supported() {
return "*.zip *.z *.7z *.rar *.gz *.bz2 *.jma";
}
void snesreader_apply_ips(const char *filename, uint8_t *&data, unsigned &size) {
file fp;
if(fp.open(filename, file::mode_read) == false) return;
unsigned psize = fp.size();
uint8_t *pdata = new uint8_t[psize];
fp.read(pdata, psize);
fp.close();
if(psize < 8 || pdata[0] != 'P' || pdata[1] != 'A' || pdata[2] != 'T' || pdata[3] != 'C' || pdata[4] != 'H') { delete[] pdata; return; }
unsigned outsize = 0;
uint8_t *outdata = new uint8_t[16 * 1024 * 1024];
memset(outdata, 0, 16 * 1024 * 1024);
memcpy(outdata, data, size);
unsigned offset = 5;
while(offset < psize - 3) {
unsigned addr;
addr = pdata[offset++] << 16;
addr |= pdata[offset++] << 8;
addr |= pdata[offset++] << 0;
unsigned size;
size = pdata[offset++] << 8;
size |= pdata[offset++] << 0;
if(size == 0) {
//RLE
size = pdata[offset++] << 8;
size |= pdata[offset++] << 0;
for(unsigned n = addr; n < addr + size;) {
outdata[n++] = pdata[offset];
if(n > outsize) outsize = n;
}
offset++;
} else {
//uncompressed
for(unsigned n = addr; n < addr + size;) {
outdata[n++] = pdata[offset++];
if(n > outsize) outsize = n;
}
}
}
delete[] pdata;
delete[] data;
data = outdata;
size = max(size, outsize);
}
bool snesreader_load_normal(const char *filename, uint8_t *&data, unsigned &size) {
file fp;
if(fp.open(filename, file::mode_read) == false) return false;
size = fp.size();
data = new uint8_t[size];
fp.read(data, size);
fp.close();
return true;
}
#include "filechooser.cpp"
bool snesreader_load_fex(string &filename, uint8_t *&data, unsigned &size) {
fex_t *fex;
fex_open(&fex, filename);
if(fex_done(fex)) { fex_close(fex); return false; }
if(!fileChooser) fileChooser = new FileChooser;
fileChooser->list.reset();
while(fex_done(fex) == false) {
fex_stat(fex);
const char *name = fex_name(fex);
//only add valid ROM extensions to list (ignore text files, save RAM files, etc)
if(striend(name, ".sfc") || striend(name, ".smc")
|| striend(name, ".swc") || striend(name, ".fig")
|| striend(name, ".bs") || striend(name, ".st")
|| striend(name, ".gb") || striend(name, ".sgb") || striend(name, ".gbc")
|| striend(filename, ".gz") //GZip files only contain a single file
) {
fileChooser->list[fileChooser->list.size()] = name;
}
fex_next(fex);
}
string name = fileChooser->exec();
if(name == "") { fex_close(fex); return false; }
fex_rewind(fex);
while(fex_done(fex) == false) {
fex_stat(fex);
if(name == fex_name(fex)) {
size = fex_size(fex);
data = new uint8_t[size];
fex_read(fex, data, size);
fex_close(fex);
if(fileChooser->list.size() > 1) {
strtr(name, "\\", "/");
strtr(filename, "\\", "/");
//retain only path from filename, "/foo/bar.7z" -> "/foo/"
for(signed i = filename.length() - 1; i >= 0; i--) {
if(filename[i] == '/') {
filename[i + 1] = 0;
break;
}
}
//append only filename from archive, "foo/bar.sfc" -> "bar.sfc"
lstring part;
part.split("/", name);
filename = string() << filename << part[part.size() - 1];
}
return true;
}
fex_next(fex);
}
fex_close(fex);
return false;
}
bool snesreader_load_bz2(const char *filename, uint8_t *&data, unsigned &size) {
//TODO: need a way to get the size of a bzip2 file, so we can pre-allocate
//a buffer to decompress into memory. for now, use a temporary file.
string name = "/tmp/.bz2_temporary_decompression_object";
FILE *wr;
wr = fopen_utf8(name, "wb");
if(!wr) {
//try the local directory
name = ".bz2_temporary_decompression_object";
wr = fopen_utf8(name, "wb");
//can't get write access, so give up
if(!wr) return false;
}
FILE *fp = fopen_utf8(filename, "rb");
uncompressStream(fileno(fp), fileno(wr));
fclose(fp);
fclose(wr);
bool success = snesreader_load_normal(name, data, size);
unlink(name);
return success;
}
bool snesreader_load_jma(const char *filename, uint8_t *&data, unsigned &size) {
try {
JMA::jma_open JMAFile(filename);
std::string name;
std::vector<JMA::jma_public_file_info> file_info = JMAFile.get_files_info();
for(std::vector<JMA::jma_public_file_info>::iterator i = file_info.begin(); i != file_info.end(); i++) {
name = i->name;
size = i->size;
break;
}
data = new uint8_t[size];
JMAFile.extract_file(name, data);
return true;
} catch(JMA::jma_errors) {
return false;
}
}
dllexport bool snesreader_load(string &filename, uint8_t *&data, unsigned &size) {
if(file::exists(filename) == false) return false;
bool success = false;
if(striend(filename, ".zip")
|| striend(filename, ".z")
|| striend(filename, ".7z")
|| striend(filename, ".rar")
|| striend(filename, ".gz")) {
success = snesreader_load_fex(filename, data, size);
} else if(striend(filename, ".bz2")) {
success = snesreader_load_bz2(filename, data, size);
} else if(striend(filename, ".jma")) {
success = snesreader_load_jma(filename, data, size);
} else {
success = snesreader_load_normal(filename, data, size);
}
if(success == false) return false;
//apply IPS patch, if it exists
string patchname = filename;
for(int i = patchname.length() - 1; i >= 0; i--) {
if(patchname[i] == '.') { patchname[i] = 0; break; }
}
patchname << ".ips";
if(file::exists(patchname)) snesreader_apply_ips(patchname, data, size);
//remove copier header, if it exists
if((size & 0x7fff) == 512) memmove(data, data + 512, size -= 512);
return true;
}