continuing cleanup.. sorry, this is like a month's worth of work

This commit is contained in:
zeromus 2012-02-24 20:42:11 +00:00
parent 9de1e4093b
commit 8b3246a510
12 changed files with 69 additions and 951 deletions

View File

@ -0,0 +1,69 @@
using System;
using System.IO;
using System.Collections.Generic;
namespace BizHawk.Emulation.Consoles.Calculator
{
[CoreVersion("0.0.1", FriendlyName = "PsxHawk!")]
public class PSX : IEmulator, IVideoProvider, ISoundProvider
{
PsxApi api = new PsxApi();
public string SystemId { get { return "PSX"; } }
public static readonly ControllerDefinition NullController = new ControllerDefinition { Name = "Null Controller" };
private int[] frameBuffer = new int[256 * 192];
private Random rand = new Random();
public CoreInputComm CoreInputComm { get; set; }
public CoreOutputComm CoreOutputComm { get; private set; }
public IVideoProvider VideoProvider { get { return this; } }
public ISoundProvider SoundProvider { get { return this; } }
public PSX()
{
CoreOutputComm = new CoreOutputComm();
CoreInputComm = new CoreInputComm();
}
public void ResetFrameCounter()
{
Frame = 0;
}
public void LoadFile(string file)
{
api.Load_EXE(file);
}
public void FrameAdvance(bool render)
{
if (render == false) return;
for (int i = 0; i < 256 * 192; i++)
frameBuffer[i] = Colors.Luminosity((byte)rand.Next());
api.RunForever();
}
public ControllerDefinition ControllerDefinition { get { return NullController; } }
public IController Controller { get; set; }
public int Frame { get; set; }
public int LagCount { get { return 0; } set { return; } }
public bool IsLagFrame { get { return false; } }
public byte[] SaveRam { get { return new byte[0]; } }
public bool DeterministicEmulation { get; set; }
public bool SaveRamModified { get; set; }
public void SaveStateText(TextWriter writer) { }
public void LoadStateText(TextReader reader) { }
public void SaveStateBinary(BinaryWriter writer) { }
public void LoadStateBinary(BinaryReader reader) { }
public byte[] SaveStateBinary() { return new byte[1]; }
public int[] GetVideoBuffer() { return frameBuffer; }
public int BufferWidth { get { return 256; } }
public int BufferHeight { get { return 192; } }
public int BackgroundColor { get { return 0; } }
public void GetSamples(short[] samples) { }
public void DiscardSamples() { }
public int MaxVolume { get; set; }
public IList<MemoryDomain> MemoryDomains { get { return new List<MemoryDomain>(); } }
public MemoryDomain MainMemory { get { return null; } }
public void Dispose() { }
}
}

View File

@ -1,187 +0,0 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="BizHawk.UnmanagedCore"
ProjectGUID="{1CE74E20-B345-4126-AACB-A21FA23149DE}"
RootNamespace="BizHawkUnmanagedCore"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="4"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_LIB;_CRT_SECURE_NO_WARNINGS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
CallingConvention="0"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="4"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_LIB;_CRT_SECURE_NO_WARNINGS"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
CallingConvention="0"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<File
RelativePath=".\core.cpp"
>
</File>
<File
RelativePath=".\core.h"
>
</File>
<File
RelativePath=".\DiscInterface.cpp"
>
</File>
<File
RelativePath=".\DiscInterface.h"
>
</File>
<File
RelativePath=".\emufile.cpp"
>
</File>
<File
RelativePath=".\emufile.h"
>
</File>
<File
RelativePath=".\emufile_hawk.cpp"
>
</File>
<File
RelativePath=".\emufile_hawk.h"
>
</File>
<File
RelativePath=".\types.h"
>
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@ -1,95 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{1CE74E20-B345-4126-AACB-A21FA23149DE}</ProjectGuid>
<RootNamespace>BizHawkUnmanagedCore</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</OutDir>
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</OutDir>
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</IntDir>
<CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<CallingConvention>Cdecl</CallingConvention>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FunctionLevelLinking>true</FunctionLevelLinking>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<CallingConvention>Cdecl</CallingConvention>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="core.cpp" />
<ClCompile Include="DiscInterface.cpp" />
<ClCompile Include="emufile.cpp" />
<ClCompile Include="emufile_hawk.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="core.h" />
<ClInclude Include="DiscInterface.h" />
<ClInclude Include="emufile.h" />
<ClInclude Include="emufile_hawk.h" />
<ClInclude Include="types.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -1,11 +0,0 @@
#include "DiscInterface.h"
static FunctionRecord records[] = {
REG("DiscInterface.Construct", &DiscInterface::Construct),
REG("DiscInterface.Set_fp", &DiscInterface::Set_fp),
};
void* DiscInterface::Construct(void* ManagedOpaque)
{
return new DiscInterface(ManagedOpaque);
}

View File

@ -1,61 +0,0 @@
#ifndef _DISCINTERFACE_H_
#define _DISCINTERFACE_H_
#include "DiscInterface.h"
#include "core.h"
class DiscInterface
{
void* ManagedOpaque;
public:
enum ETrackType : int
{
ETrackType_Mode1_2352,
ETrackType_Mode1_2048,
ETrackType_Mode2_2352,
ETrackType_Audio
};
struct TrackInfo
{
ETrackType TrackType;
int length_lba;
int start_lba;
};
FUNC<void()> Dispose;
FUNC<int()> GetNumSessions;
FUNC<int(int)> GetNumTracks;
FUNC<TrackInfo(int,int)> GetTrack;
~DiscInterface()
{
Dispose.func();
}
DiscInterface(void* _ManagedOpaque)
: ManagedOpaque(_ManagedOpaque)
{
}
void* Construct(void* ManagedOpaque);
void Delete()
{
delete this;
}
void Set_fp(const char* param, void* value)
{
if(!strcmp(param,"GetNumSessions")) GetNumSessions.set(value);
if(!strcmp(param,"GetNumTracks")) GetNumTracks.set(value);
if(!strcmp(param,"GetTrack")) GetTrack.set(value);
if(!strcmp(param,"Dispose")) Dispose.set(value);
}
};
#endif //_DISCINTERFACE_H_

View File

@ -1,87 +0,0 @@
#include <string>
#include "core.h"
#include "emufile.h"
#include "emufile_hawk.h"
#include "DiscInterface.h"
//TODO - setup a null file to use as the default console, so we dont have to check whether its set to null everywhere
class EMUFILE_HAWK;
EMUFILE_HAWK* con = NULL;
static void* (*ClientSignal_cb)(const char*,void*,const char*,void*);
void* ClientSignal(const char* type, void* obj, const char* _param, void* value)
{
return ClientSignal_cb(type,obj,_param,value);
}
//core objects function pointers get registered here
class FunctionRegistry
{
private:
typedef std::map<std::string, void*> TFunctionMap;
TFunctionMap map;
public:
static FunctionRegistry& Instance() {
static FunctionRegistry inst;
return inst;
}
void Register(const char* _name, void* _funcptr)
{
map[_name] = _funcptr;
}
void* Lookup(const char* name)
{
TFunctionMap::iterator it(map.find(name));
if(it == map.end()) return NULL;
else return it->second;
}
private:
FunctionRegistry() {}
};
void _registerFunction(const char* _name, void* _funcptr)
{
FunctionRegistry::Instance().Register(_name,_funcptr);
}
//maybe youll need this some day... but probably not.
//#pragma comment(linker, "/include:_Core_signal")
extern "C" __declspec(dllexport) void* Core_signal(const char* type, void* obj, const char* param, void* value)
{
//use this to log signals
if(con) con->fprintf("core signal: %s : %s\n",type?type:"n/a",param?param:"n/a");
if(!strcmp(type,"SET_CLIENT_SIGNAL"))
{
ClientSignal_cb = (void *(*)(const char*,void*,const char*,void*))value;
return 0;
}
if(!strcmp(type,"SET_CONSOLE"))
{
con = (EMUFILE_HAWK*)value;
return 0;
}
//query a function pointer for later blazing fast reuse
if(!strcmp(type,"QUERY_FUNCTION"))
return FunctionRegistry::Instance().Lookup(param);
//TODO - custom core static operations?
//force a reference to our core types. a bit annoying but if its this easy i guess i dont mind
if(!strcmp(type,"IMPOSSIBLE"))
{
con->fprintf("%x\n",((DiscInterface*)NULL)->Construct(NULL));
con->fprintf("%x\n",((EMUFILE_HAWK*)NULL)->Construct(NULL));
return 0;
}
return 0;
}

View File

@ -1,100 +0,0 @@
#ifndef _CORE_H_
#define _CORE_H_
#include <map>
#include <string>
#ifndef CTASSERT
#define CTASSERT(x) typedef char __assert ## y[(x) ? 1 : -1];
#endif
//use this to send a signal to the client.
//it may not be completely fully-baked yet though
void* ClientSignal(const char* type, void* obj, const char* _param, void* value);
class EMUFILE_HAWK;
//use this to print output to the client
extern EMUFILE_HAWK* con;
//this is supposedly illegal. but i say its perfectly legal, as long as im not using virtual functions. so stuff it.
//well, since we're doing it illegally, we need to resist the urge to generalize the function pointer system (to emufile and disc)
//since we may need to change all these later to work un-generalized
//but seriously. before doing that, i would rather return sizeof(functionpointer) bytes as a token to the managed code and pass that back in
//(MP stands for MEMBER POINTER)
//we could also try using the FastDelegate. really, we probably should.
template<typename T> void* MP(const T& a)
{
union U{
void* vp;
T t;
} u;
u.t = a;
return u.vp;
CTASSERT(sizeof(U)==4||(sizeof(U)==8&&sizeof(void*)==8));
}
//this is a function pointer which can be assigned without having to type the function protoype again to cast it.
template<typename T> class __FP
{
private:
template<typename T> T MPX(void* a)
{
union U{
void* vp;
T t;
} u;
u.vp = a;
return u.t;
CTASSERT(sizeof(U)==4||(sizeof(U)==8&&sizeof(void*)==8));
}
//protected:
public:
T func;
void set(void* val) { func = MPX<T>(val); }
};
//----------------
//these templates help us call a function pointer directly with () instead of fp.func()
template<typename R=void> struct FUNC0 : public __FP<R(*)()> { public:
R operator()() { return func(); }
};
template<typename A1, typename R=void> struct FUNC1 : public __FP<R(*)(A1)> { public:
R operator()(A1 a1) { return func(a1); }
};
template<typename A1, typename A2, typename R=void> struct FUNC2 : public __FP<R(*)(A1,A2)> { public:
R operator()(A1 a1, A2 a2) { return func(a1,a2); }
};
template<typename A1, typename A2, typename A3, typename R=void> struct FUNC3 : public __FP<R(*)(A1,A2,A3)> { public:
R operator()(A1 a1, A2 a2, A3 a3) { return func(a1,a2,a3); }
};
template<typename A1, typename A2, typename A3, typename A4, typename R=void> struct FUNC4 : public __FP<R(*)(A1,A2,A3,A4)> { public:
R operator()(A1 a1, A2 a2, A3 a3, A4 a4) { return func(a1,a2,a3,a4); }
};
template<typename Signature> class FUNC;
template<typename R> class FUNC<R()> : public FUNC0<R> {};
template<typename A1, typename R> class FUNC<R(A1)> : public FUNC1<A1,R> {};
template<typename A1, typename A2, typename R> class FUNC<R(A1,A2)> : public FUNC2<A1,A2,R> {};
template<typename A1, typename A2, typename A3, typename R> class FUNC<R(A1,A2,A3)> : public FUNC3<A1,A2,A3,R> {};
template<typename A1, typename A2, typename A3, typename A4, typename R> class FUNC<R(A1,A2,A3,A4)> : public FUNC4<A1,A2,A3,A4,R> {};
//----------------
void _registerFunction(const char* _name, void* _funcptr);
struct FunctionRecord
{
FunctionRecord(const char* _name, void* _funcptr)
{
_registerFunction(_name,_funcptr);
}
};
//register a core object member function. put it in a global static array
template<typename T> FunctionRecord REG(const char* name, const T& a)
{
return FunctionRecord(name,MP(a));
}
#endif //_CORE_H_

View File

@ -1,28 +0,0 @@
#include "types.h"
#include "emufile.h"
#include <vector>
bool EMUFILE::readAllBytes(std::vector<u8>* dstbuf, const std::string& fname)
{
EMUFILE_FILE file(fname.c_str(),"rb");
if(file.fail()) return false;
int size = file.size();
dstbuf->resize(size);
file.fread(&dstbuf->at(0),size);
return true;
}
EMUFILE* EMUFILE::memwrap(EMUFILE* fp)
{
EMUFILE_FILE* file;
EMUFILE_MEMORY* mem;
file = dynamic_cast<EMUFILE_FILE*>(fp);
mem = dynamic_cast<EMUFILE_MEMORY*>(fp);
if(mem) return mem;
mem = new EMUFILE_MEMORY(file->size());
if(file->size()==0) return mem;
file->fread(mem->buf(),file->size());
delete file;
return mem;
}

View File

@ -1,280 +0,0 @@
#ifndef EMUFILE_H
#define EMUFILE_H
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stdarg.h>
//should be changed to #ifdef FCEUX but too much work
#ifndef DESMUME
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef char s8;
typedef short s16;
typedef int s32;
#endif
#ifdef _XBOX
#undef min;
#undef max;
#endif
class EMUFILE {
protected:
bool failbit;
public:
EMUFILE()
: failbit(false)
{}
//takes control of the provided EMUFILE and returns a new EMUFILE which is guranteed to be in memory
static EMUFILE* memwrap(EMUFILE* fp);
virtual ~EMUFILE() {}
static bool readAllBytes(std::vector<u8>* buf, const std::string& fname);
bool fail() { return failbit; }
bool eof() { return size()==ftell(); }
size_t fread(const void *ptr, size_t bytes){
return _fread(ptr,bytes);
}
void unget() { fseek(-1,SEEK_CUR); }
//virtuals
public:
virtual FILE *get_fp() = 0;
virtual int fprintf(const char *format, ...) = 0;
virtual int fgetc() = 0;
virtual int fputc(int c) = 0;
virtual size_t _fread(const void *ptr, size_t bytes) = 0;
//removing these return values for now so we can find any code that might be using them and make sure
//they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes) = 0;
virtual int fseek(int offset, int origin) = 0;
virtual int ftell() = 0;
virtual int size() = 0;
};
//todo - handle read-only specially?
class EMUFILE_MEMORY : public EMUFILE {
protected:
std::vector<u8> *vec;
bool ownvec;
s32 pos, len;
void reserve(u32 amt) {
if(vec->size() < amt)
vec->resize(amt);
}
public:
EMUFILE_MEMORY(std::vector<u8> *underlying) : vec(underlying), ownvec(false), pos(0), len(underlying->size()) { }
EMUFILE_MEMORY(u32 preallocate) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) {
vec->resize(preallocate);
len = preallocate;
}
EMUFILE_MEMORY() : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) { vec->reserve(1024); }
EMUFILE_MEMORY(void* buf, s32 size) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(size) {
vec->resize(size);
if(size != 0)
memcpy(&vec[0],buf,size);
}
~EMUFILE_MEMORY() {
if(ownvec) delete vec;
}
u8* buf() { return &(*vec)[0]; }
std::vector<u8>* get_vec() { return vec; };
virtual FILE *get_fp() { return NULL; }
virtual int fprintf(const char *format, ...) {
va_list argptr;
va_start(argptr, format);
//we dont generate straight into the buffer because it will null terminate (one more byte than we want)
int amt = vsnprintf(0,0,format,argptr);
char* tempbuf = new char[amt+1];
vsprintf(tempbuf,format,argptr);
fwrite(tempbuf,amt);
delete[] tempbuf;
va_end(argptr);
return amt;
};
virtual int fgetc() {
u8 temp;
//need an optimized codepath
//if(_fread(&temp,1) != 1)
// return EOF;
//else return temp;
u32 remain = len-pos;
if(remain<1) {
failbit = true;
return -1;
}
temp = buf()[pos];
pos++;
return temp;
}
virtual int fputc(int c) {
u8 temp = (u8)c;
//TODO
//if(fwrite(&temp,1)!=1) return EOF;
fwrite(&temp,1);
return 0;
}
virtual size_t _fread(const void *ptr, size_t bytes){
u32 remain = len-pos;
u32 todo = std::min<u32>(remain,(u32)bytes);
memcpy((void*)ptr,buf()+pos,todo);
pos += todo;
if(todo<bytes)
failbit = true;
return todo;
}
//removing these return values for now so we can find any code that might be using them and make sure
//they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes){
reserve(pos+bytes);
memcpy(buf()+pos,ptr,bytes);
pos += bytes;
len = std::max(pos,len);
}
virtual int fseek(int offset, int origin){
//work differently for read-only...?
switch(origin) {
case SEEK_SET:
pos = offset;
break;
case SEEK_CUR:
pos += offset;
break;
case SEEK_END:
pos = size()+offset;
break;
default:
assert(false);
}
reserve(pos);
return 0;
}
virtual int ftell() {
return pos;
}
void trim()
{
vec->resize(len);
}
virtual int size() { return (int)len; }
};
class EMUFILE_FILE : public EMUFILE {
protected:
FILE* fp;
private:
void open(const char* fname, const char* mode)
{
fp = fopen(fname,mode);
if(!fp)
failbit = true;
}
public:
EMUFILE_FILE(const std::string& fname, const char* mode) { open(fname.c_str(),mode); }
EMUFILE_FILE(const char* fname, const char* mode) { open(fname,mode); }
virtual ~EMUFILE_FILE() {
if(NULL != fp)
fclose(fp);
}
virtual FILE *get_fp() {
return fp;
}
bool is_open() { return fp != NULL; }
virtual int fprintf(const char *format, ...) {
va_list argptr;
va_start(argptr, format);
int ret = ::vfprintf(fp, format, argptr);
va_end(argptr);
return ret;
};
virtual int fgetc() {
return ::fgetc(fp);
}
virtual int fputc(int c) {
return ::fputc(c, fp);
}
virtual size_t _fread(const void *ptr, size_t bytes){
size_t ret = ::fread((void*)ptr, 1, bytes, fp);
if(ret < bytes)
failbit = true;
return ret;
}
//removing these return values for now so we can find any code that might be using them and make sure
//they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes){
size_t ret = ::fwrite((void*)ptr, 1, bytes, fp);
if(ret < bytes)
failbit = true;
}
virtual int fseek(int offset, int origin){
return ::fseek(fp, offset, origin);
}
virtual int ftell() {
return (u32)::ftell(fp);
}
virtual int size() {
int oldpos = ftell();
fseek(0,SEEK_END);
int len = ftell();
fseek(oldpos,SEEK_SET);
return len;
}
};
#endif

View File

@ -1,31 +0,0 @@
#include "emufile_hawk.h"
#include "core.h"
static FunctionRecord records[] = {
REG("EmuFile.Construct", &EMUFILE_HAWK::Construct),
REG("EmuFile.Set_fp", &EMUFILE_HAWK::Set_fp),
REG("EmuFile.Delete", &EMUFILE_HAWK::Delete),
};
int EMUFILE_HAWK::fprintf(const char *format, ...)
{
va_list argptr;
va_start(argptr, format);
//could use a small static buf here optionally for quickness's sake but we may regret it if we multithread later
int amt = vsnprintf(0,0,format,argptr);
char* tempbuf = new char[amt+1];
vsprintf(tempbuf,format,argptr);
fwrite(tempbuf,amt);
delete[] tempbuf;
va_end(argptr);
return amt;
}
void* EMUFILE_HAWK::Construct(void* ManagedOpaque)
{
return new EMUFILE_HAWK(ManagedOpaque);
}

View File

@ -1,63 +0,0 @@
#ifndef _EMUFILE_HAWK_H_
#define _EMUFILE_HAWK_H_
#include "emufile.h"
#include "core.h"
class EMUFILE_HAWK : public EMUFILE
{
void* ManagedOpaque;
struct {
FUNC<int()> fgetc;
FUNC<int(const void* ptr, size_t bytes)> fread;
FUNC<void(const void* ptr, size_t bytes)> fwrite;
FUNC<int(int offset, int origin)> fseek;
FUNC<int()> ftell;
FUNC<int()> size;
FUNC<void()> dispose;
} _;
public:
~EMUFILE_HAWK()
{
_.dispose();
}
EMUFILE_HAWK(void* _ManagedOpaque)
: ManagedOpaque(_ManagedOpaque)
{
}
void* Construct(void* ManagedOpaque);
void Delete()
{
delete this;
}
void Set_fp(const char* param, void* value)
{
if(!strcmp(param,"fgetc")) _.fgetc.set(value);
if(!strcmp(param,"fread")) _.fread.set(value);
if(!strcmp(param,"fwrite")) _.fwrite.set(value);
if(!strcmp(param,"fseek")) _.fseek.set(value);
if(!strcmp(param,"ftell")) _.ftell.set(value);
if(!strcmp(param,"size")) _.size.set(value);
if(!strcmp(param,"dispose")) _.dispose.set(value);
}
virtual int fgetc() { return _.fgetc(); }
virtual FILE *get_fp() { return NULL; }
virtual int fputc(int c) { return -1; }
virtual int fprintf(const char *format, ...);
virtual size_t _fread(const void *ptr, size_t bytes) { return _.fread(ptr,bytes); }
virtual void fwrite(const void *ptr, size_t bytes) { return _.fwrite(ptr,bytes); }
virtual int fseek(int offset, int origin) { return _.fseek(offset,origin); }
virtual int ftell() { return _.ftell(); }
virtual int size() { return _.size(); }
};
#endif //_EMUFILE_HAWK_H_

View File

@ -1,8 +0,0 @@
typedef unsigned char u8;
typedef char s8;
typedef unsigned short u16;
typedef short s16;
typedef unsigned int u32;
typedef int s32;
typedef unsigned __int64 u64;
typedef __int64 s64;