Lua: add sound.get(), remove iowritelib.

This commit is contained in:
gocha 2011-02-26 15:20:30 +00:00
parent e57b79861e
commit ffca5efaca
6 changed files with 179 additions and 42 deletions

View File

@ -205,8 +205,6 @@ static readfunc *AReadG;
static writefunc *BWriteG;
static int RWWrap=0;
uint8 IOWriteLog[0x10000];
//mbg merge 7/18/06 docs
//bit0 indicates whether emulation is paused
//bit1 indicates whether emulation is in frame step mode

View File

@ -54,8 +54,6 @@ uint8 FCEU_ReadRomByte(uint32 i);
extern readfunc ARead[0x10000];
extern writefunc BWrite[0x10000];
extern uint8 IOWriteLog[0x10000];
enum GI {
GI_RESETM2 =1,
GI_POWER =2,

View File

@ -9,6 +9,7 @@
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#ifdef __linux
#include <unistd.h>
@ -25,6 +26,7 @@
#include "types.h"
#include "fceu.h"
#include "video.h"
#include "sound.h"
#include "drawing.h"
#include "state.h"
#include "movie.h"
@ -1206,8 +1208,6 @@ static int rom_gethash(lua_State *L) {
else lua_pushstring(L, "");
return 1;
}
static int iowrite_readbyte(lua_State *L) { int addr = luaL_checkinteger(L,1); lua_pushinteger(L, (addr >= 0 && addr <= 0xFFFF) ? IOWriteLog[addr] : 0); return 1; }
static int iowrite_readbytesigned(lua_State *L) { int addr = luaL_checkinteger(L,1); lua_pushinteger(L, (addr >= 0 && addr <= 0xFFFF) ? (signed char)IOWriteLog[addr] : 0); return 1; }
static int memory_readbyte(lua_State *L) { lua_pushinteger(L, FCEU_CheatGetByte(luaL_checkinteger(L,1))); return 1; }
static int memory_writebyte(lua_State *L) { FCEU_CheatSetByte(luaL_checkinteger(L,1), luaL_checkinteger(L,2)); return 0; }
static int memory_readbyterange(lua_State *L) {
@ -4098,6 +4098,157 @@ static int gui_register(lua_State *L) {
}
static int sound_get(lua_State *L)
{
extern ENVUNIT EnvUnits[3];
extern int CheckFreq(uint32 cf, uint8 sr);
extern int32 curfreq[2];
extern uint8 PSG[0x10];
extern int32 lengthcount[4];
extern uint8 TriCount;
extern const uint32 *NoiseFreqTable;
extern int32 DMCPeriod;
extern uint8 DMCAddressLatch, DMCSizeLatch;
extern uint8 DMCFormat;
extern char DMCHaveSample;
int freqReg;
double freq;
lua_newtable(L);
// rp2a03 start
lua_newtable(L);
// rp2a03 info setup
double nesVolumes[3];
for (int i = 0; i < 3; i++)
{
if ((EnvUnits[i].Mode & 1) != 0)
nesVolumes[i] = EnvUnits[i].Speed;
else
nesVolumes[i] = EnvUnits[i].decvolume;
nesVolumes[i] /= 15.0;
}
// rp2a03/square1
lua_newtable(L);
if((curfreq[0] < 8 || curfreq[0] > 0x7ff) ||
(CheckFreq(curfreq[0], PSG[1]) == 0) ||
(lengthcount[0] == 0))
lua_pushnumber(L, 0.0);
else
lua_pushnumber(L, nesVolumes[0]);
lua_setfield(L, -2, "volume");
lua_pushinteger(L, curfreq[0]);
lua_setfield(L, -2, "freqreg");
freq = (39375000.0/352.0) / curfreq[0];
lua_pushnumber(L, freq);
lua_setfield(L, -2, "frequency");
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
lua_setfield(L, -2, "midikey");
lua_pushinteger(L, (PSG[0] & 0xC0) >> 6);
lua_setfield(L, -2, "duty");
lua_setfield(L, -2, "square1");
// rp2a03/square2
lua_newtable(L);
if((curfreq[1] < 8 || curfreq[1] > 0x7ff) ||
(CheckFreq(curfreq[1], PSG[5]) == 0) ||
(lengthcount[1] == 0))
lua_pushnumber(L, 0.0);
else
lua_pushnumber(L, nesVolumes[1]);
lua_setfield(L, -2, "volume");
lua_pushinteger(L, curfreq[1]);
lua_setfield(L, -2, "freqreg");
freq = (39375000.0/352.0) / curfreq[1];
lua_pushnumber(L, freq);
lua_setfield(L, -2, "frequency");
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
lua_setfield(L, -2, "midikey");
lua_pushinteger(L, (PSG[4] & 0xC0) >> 6);
lua_setfield(L, -2, "duty");
lua_setfield(L, -2, "square2");
// rp2a03/triangle
lua_newtable(L);
if(lengthcount[2] == 0 || TriCount == 0)
lua_pushnumber(L, 0.0);
else
lua_pushnumber(L, 1.0);
lua_setfield(L, -2, "volume");
freqReg = PSG[0xa] | ((PSG[0xb] & 7) << 8);
lua_pushinteger(L, freqReg);
lua_setfield(L, -2, "freqreg");
freq = (39375000.0/704.0) / freqReg;
lua_pushnumber(L, freq);
lua_setfield(L, -2, "frequency");
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
lua_setfield(L, -2, "midikey");
lua_setfield(L, -2, "triangle");
// rp2a03/noise
lua_newtable(L);
if(lengthcount[3] == 0)
lua_pushnumber(L, 0.0);
else
lua_pushnumber(L, nesVolumes[2]);
lua_setfield(L, -2, "volume");
freqReg = PSG[0xE] & 0xF;
lua_pushinteger(L, freqReg);
lua_setfield(L, -2, "freqreg");
lua_pushboolean(L, (PSG[0xE] & 0x80) != 0);
lua_setfield(L, -2, "short");
freq = (39375000.0/44.0) / NoiseFreqTable[freqReg]; // probably wrong
lua_pushnumber(L, freq);
lua_setfield(L, -2, "frequency");
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
lua_setfield(L, -2, "midikey");
lua_setfield(L, -2, "noise");
// rp2a03/dpcm
lua_newtable(L);
if (DMCHaveSample == 0)
lua_pushnumber(L, 0.0);
else
lua_pushnumber(L, 1.0);
lua_setfield(L, -2, "volume");
lua_pushinteger(L, DMCFormat & 0xF);
lua_setfield(L, -2, "freqreg");
freq = (39375000.0/2.0) / DMCPeriod;
lua_pushnumber(L, freq);
lua_setfield(L, -2, "frequency");
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
lua_setfield(L, -2, "midikey");
lua_pushinteger(L, 0xC000 + (DMCAddressLatch << 6));
lua_setfield(L, -2, "dmcaddress");
lua_pushinteger(L, (DMCSizeLatch << 4) + 1);
lua_setfield(L, -2, "dmcsize");
lua_setfield(L, -2, "dpcm");
// rp2a03 end
lua_setfield(L, -2, "rp2a03");
return 1;
/*
#ifdef WIN32
// keyboard and mouse button status
{
extern int EnableBackgroundInput;
unsigned char keys [256];
if(!EnableBackgroundInput)
{
if(GetKeyboardState(keys))
{
for(int i = 1; i < 255; i++)
{
int mask = (i == VK_CAPITAL || i == VK_NUMLOCK || i == VK_SCROLL) ? 0x01 : 0x80;
if(keys[i] & mask)
{
const char* name = s_keyToName[i];
if(name)
{
lua_pushboolean(L, true);
lua_setfield(L, -2, name);
}
*/
}
static int doPopup(lua_State *L, const char* deftype, const char* deficon) {
const char *str = luaL_checkstring(L, 1);
const char* type = lua_type(L,2) == LUA_TSTRING ? lua_tostring(L,2) : deftype;
@ -4701,16 +4852,6 @@ static const struct luaL_reg memorylib [] = {
{NULL,NULL}
};
static const struct luaL_reg iowritelib [] = {
{"readbyte", iowrite_readbyte},
{"readbytesigned", iowrite_readbytesigned},
// alternate naming scheme for unsigned
{"readbyteunsigned", iowrite_readbyte},
{NULL,NULL}
};
static const struct luaL_reg joypadlib[] = {
{"get", joypad_get},
{"getdown", joypad_getdown},
@ -4815,6 +4956,12 @@ static const struct luaL_reg guilib[] = {
{NULL,NULL}
};
static const struct luaL_reg soundlib[] = {
{"get", sound_get},
{NULL,NULL}
};
void CallExitFunction() {
if (!L)
return;
@ -4933,7 +5080,6 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg) {
luaL_register(L, "emu", emulib); // added for better cross-emulator compatibility
luaL_register(L, "FCEU", emulib); // kept for backward compatibility
luaL_register(L, "memory", memorylib);
luaL_register(L, "iowrite", iowritelib);
luaL_register(L, "rom", romlib);
luaL_register(L, "joypad", joypadlib);
luaL_register(L, "zapper", zapperlib);
@ -4941,6 +5087,7 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg) {
luaL_register(L, "savestate", savestatelib);
luaL_register(L, "movie", movielib);
luaL_register(L, "gui", guilib);
luaL_register(L, "sound", soundlib);
luaL_register(L, "bit", bit_funcs); // LuaBitOp library
lua_settop(L, 0); // clean the stack, because each call to luaL_register leaves a table on top

View File

@ -42,7 +42,7 @@ int32 WaveFinal[2048+512];
EXPSOUND GameExpSound={0,0,0};
static uint8 TriCount=0;
/*static*/ uint8 TriCount=0;
static uint8 TriMode=0;
static int32 tristep=0;
@ -50,26 +50,18 @@ static int32 tristep=0;
static int32 wlcount[4]={0,0,0,0}; /* Wave length counters. */
static uint8 IRQFrameMode=0; /* $4017 / xx000000 */
static uint8 PSG[0x10];
/*static*/ uint8 PSG[0x10];
static uint8 RawDALatch=0; /* $4011 0xxxxxxx */
uint8 EnabledChannels=0; /* Byte written to $4015 */
typedef struct {
uint8 Speed;
uint8 Mode; /* Fixed volume(1), and loop(2) */
uint8 DecCountTo1;
uint8 decvolume;
int reloaddec;
} ENVUNIT;
static ENVUNIT EnvUnits[3];
/*static*/ ENVUNIT EnvUnits[3];
static const int RectDuties[4]={1,2,4,6};
static int32 RectDutyCount[2];
static uint8 sweepon[2];
static int32 curfreq[2];
/*static*/ int32 curfreq[2];
static uint8 SweepCount[2];
static uint16 nreg=0;
@ -87,7 +79,7 @@ uint32 soundtsi=0;
static int32 sqacc[2];
/* LQ variables segment ends. */
static int32 lengthcount[4];
/*static*/ int32 lengthcount[4];
static const uint8 lengthtable[0x20]=
{
10,254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14,
@ -107,7 +99,7 @@ static const uint32 NoiseFreqTablePAL[0x10] =
236, 354, 472, 708, 944, 1890, 3778
};
static const uint32 *NoiseFreqTable = NoiseFreqTableNTSC;
/*static*/ const uint32 *NoiseFreqTable = NoiseFreqTableNTSC;
static const uint32 NTSCDMCTable[0x10]=
{
@ -133,11 +125,11 @@ static const uint32 PALDMCTable[0x10]=
// $4013 - Size register: Size in bytes = (V+1)*64
/*static*/ int32 DMCacc=1;
static int32 DMCPeriod=0;
/*static*/ int32 DMCPeriod=0;
/*static*/ uint8 DMCBitCount=0;
static uint8 DMCAddressLatch=0,DMCSizeLatch=0; /* writes to 4012 and 4013 */
static uint8 DMCFormat=0; /* Write to $4010 */
/*static*/ uint8 DMCAddressLatch=0,DMCSizeLatch=0; /* writes to 4012 and 4013 */
/*static*/ uint8 DMCFormat=0; /* Write to $4010 */
static uint32 DMCAddress=0;
static int32 DMCSize=0;
@ -146,7 +138,7 @@ static uint8 SIRQStat=0;
static char DMCHaveDMA=0;
static uint8 DMCDMABuf=0;
static char DMCHaveSample=0;
/*static*/ char DMCHaveSample=0;
static void Dummyfunc(void) {};
static void (*DoNoise)(void)=Dummyfunc;
@ -205,7 +197,7 @@ void LogDPCM(int romaddress, int dpcmsize){
/* Instantaneous? Maybe the new freq value is being calculated all of the time... */
static int CheckFreq(uint32 cf, uint8 sr)
/*static*/ int CheckFreq(uint32 cf, uint8 sr)
{
uint32 mod;
if(!(sr&0x8))
@ -239,7 +231,6 @@ static void SQReload(int x, uint8 V)
static DECLFW(Write_PSG)
{
IOWriteLog[A] = V;
A&=0x1F;
switch(A)
{
@ -312,7 +303,6 @@ static DECLFW(Write_PSG)
static DECLFW(Write_DMCRegs)
{
IOWriteLog[A] = V;
A&=0xF;
switch(A)
@ -344,7 +334,6 @@ static DECLFW(Write_DMCRegs)
static DECLFW(StatusWrite)
{
int x;
IOWriteLog[A] = V;
DoSQ1();
DoSQ2();
@ -1008,7 +997,6 @@ static void RDoNoise(void)
DECLFW(Write_IRQFM)
{
IOWriteLog[A] = V;
V=(V&0xC0)>>6;
fcnt=0;
if(V&0x2)

View File

@ -73,4 +73,12 @@ void Write_IRQFM (uint32 A, uint8 V); //mbg merge 7/17/06 brought over from late
void LogDPCM(int romaddress, int dpcmsize);
typedef struct {
uint8 Speed;
uint8 Mode; /* Fixed volume(1), and loop(2) */
uint8 DecCountTo1;
uint8 decvolume;
int reloaddec;
} ENVUNIT;
#endif

View File

@ -50,7 +50,6 @@ static INLINE uint8 RdMem(unsigned int A)
//normal memory write
static INLINE void WrMem(unsigned int A, uint8 V)
{
IOWriteLog[A] = V;
BWrite[A](A,V);
#ifdef _S9XLUA_H
CallRegisteredLuaMemHook(A, 1, V, LUAMEMHOOK_WRITE);
@ -81,7 +80,6 @@ uint8 X6502_DMR(uint32 A)
void X6502_DMW(uint32 A, uint8 V)
{
ADDCYC(1);
IOWriteLog[A] = V;
BWrite[A](A,V);
#ifdef _S9XLUA_H
CallRegisteredLuaMemHook(A, 1, V, LUAMEMHOOK_WRITE);