2008-12-08 05:30:24 +00:00
|
|
|
// Copyright (C) 2003-2008 Dolphin Project.
|
|
|
|
|
|
|
|
// 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, version 2.0.
|
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "MemoryUtil.h"
|
|
|
|
#include "Thread.h"
|
|
|
|
#include "OpcodeDecoding.h"
|
2009-02-22 06:47:27 +00:00
|
|
|
#include "Setup.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
#include "Fifo.h"
|
|
|
|
|
|
|
|
|
2009-01-24 14:43:17 +00:00
|
|
|
// TODO (mb2): move/rm this global
|
2009-01-24 22:02:27 +00:00
|
|
|
volatile u32 g_XFBUpdateRequested = FALSE;
|
2009-02-20 22:04:52 +00:00
|
|
|
extern u8* g_pVideoData;
|
2009-01-24 14:43:17 +00:00
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
namespace {
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
static bool fifoStateRun = false;
|
2008-12-08 05:30:24 +00:00
|
|
|
static u8 *videoBuffer;
|
2009-02-20 22:04:52 +00:00
|
|
|
static Common::Event fifo_exit_event;
|
|
|
|
// STATE_TO_SAVE
|
2008-12-08 05:30:24 +00:00
|
|
|
static int size = 0;
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
} // namespace
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
void Fifo_DoState(PointerWrap &p)
|
|
|
|
{
|
|
|
|
p.DoArray(videoBuffer, FIFO_SIZE);
|
|
|
|
p.Do(size);
|
2009-02-20 22:04:52 +00:00
|
|
|
int pos = (int)(g_pVideoData - videoBuffer); // get offset
|
2008-12-08 05:30:24 +00:00
|
|
|
p.Do(pos); // read or write offset (depends on the mode afaik)
|
|
|
|
g_pVideoData = &videoBuffer[pos]; // overwrite g_pVideoData -> expected no change when load ss and change when save ss
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fifo_Init()
|
|
|
|
{
|
|
|
|
videoBuffer = (u8*)AllocateMemoryPages(FIFO_SIZE);
|
2009-02-20 22:04:52 +00:00
|
|
|
fifo_exit_event.Init();
|
2009-02-21 00:54:52 +00:00
|
|
|
fifoStateRun = false;
|
2009-01-24 14:43:17 +00:00
|
|
|
g_XFBUpdateRequested = FALSE;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fifo_Shutdown()
|
|
|
|
{
|
2009-02-20 22:04:52 +00:00
|
|
|
if (fifoStateRun)
|
|
|
|
PanicAlert("Fifo shutting down while active");
|
2008-12-08 05:30:24 +00:00
|
|
|
FreeMemoryPages(videoBuffer, FIFO_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* FAKE_GetFifoStartPtr()
|
|
|
|
{
|
|
|
|
return videoBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* FAKE_GetFifoEndPtr()
|
|
|
|
{
|
|
|
|
return &videoBuffer[size];
|
|
|
|
}
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
// The loop in EnterLoop sends data through this function.
|
|
|
|
// TODO: Possibly inline it? This one is exported so it will likely not be inlined at all.
|
2008-12-08 05:30:24 +00:00
|
|
|
void Video_SendFifoData(u8* _uData, u32 len)
|
|
|
|
{
|
|
|
|
if (size + len >= FIFO_SIZE)
|
|
|
|
{
|
2009-02-20 22:04:52 +00:00
|
|
|
int pos = (int)(g_pVideoData - videoBuffer);
|
|
|
|
if (size - pos > pos)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
PanicAlert("FIFO out of bounds (sz = %i, at %08x)", size, pos);
|
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
memmove(&videoBuffer[0], &videoBuffer[pos], size - pos);
|
2008-12-08 05:30:24 +00:00
|
|
|
size -= pos;
|
|
|
|
g_pVideoData = FAKE_GetFifoStartPtr();
|
|
|
|
}
|
|
|
|
memcpy(videoBuffer + size, _uData, len);
|
|
|
|
size += len;
|
|
|
|
OpcodeDecoder_Run();
|
|
|
|
}
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
void Fifo_ExitLoop()
|
|
|
|
{
|
|
|
|
fifoStateRun = false;
|
|
|
|
fifo_exit_event.Wait();
|
|
|
|
fifo_exit_event.Shutdown();
|
|
|
|
}
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
void Fifo_EnterLoop(const SVideoInitialize &video_initialize)
|
|
|
|
{
|
2009-02-21 00:54:52 +00:00
|
|
|
fifoStateRun = true;
|
2008-12-08 05:30:24 +00:00
|
|
|
SCPFifoStruct &_fifo = *video_initialize.pCPFifo;
|
2009-01-27 20:59:27 +00:00
|
|
|
s32 distToSend;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
while (fifoStateRun)
|
|
|
|
{
|
2009-02-22 06:47:27 +00:00
|
|
|
#if defined(_WIN32) && !defined(SETUP_AVOID_OPENGL_SCREEN_MESSAGE_HANG)
|
2009-02-20 22:04:52 +00:00
|
|
|
video_initialize.pPeekMessages();
|
|
|
|
#endif
|
2009-01-24 14:43:17 +00:00
|
|
|
if (_fifo.CPReadWriteDistance == 0)
|
2008-12-08 05:30:24 +00:00
|
|
|
Common::SleepCurrentThread(1);
|
|
|
|
|
2009-01-27 20:59:27 +00:00
|
|
|
// Draw XFB if CP/GPfifo isn't used
|
2009-01-24 14:43:17 +00:00
|
|
|
if (g_XFBUpdateRequested)
|
|
|
|
{
|
|
|
|
Video_UpdateXFB(NULL, 0, 0, 0, FALSE);
|
|
|
|
video_initialize.pCopiedToXFB();
|
|
|
|
}
|
2009-01-27 20:59:27 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
// check if we are able to run this buffer
|
|
|
|
if ((_fifo.bFF_GPReadEnable) && _fifo.CPReadWriteDistance && !(_fifo.bFF_BPEnable && _fifo.bFF_Breakpoint))
|
|
|
|
{
|
|
|
|
Common::SyncInterlockedExchange((LONG*)&_fifo.CPReadIdle, 0);
|
|
|
|
//video_initialize.pLog("RUN...........................",FALSE);
|
|
|
|
int peek_counter = 0;
|
2009-01-24 14:43:17 +00:00
|
|
|
while (_fifo.bFF_GPReadEnable && _fifo.CPReadWriteDistance)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
peek_counter++;
|
2009-01-24 14:43:17 +00:00
|
|
|
if (peek_counter == 1000) {
|
2008-12-08 05:30:24 +00:00
|
|
|
video_initialize.pPeekMessages();
|
|
|
|
peek_counter = 0;
|
|
|
|
}
|
|
|
|
// read the data and send it to the VideoPlugin
|
|
|
|
u32 readPtr = _fifo.CPReadPointer;
|
|
|
|
u8 *uData = video_initialize.pGetMemoryPointer(readPtr);
|
|
|
|
|
|
|
|
// if we are on BP mode we must send 32B chunks to Video plugin for BP checking
|
|
|
|
// TODO (mb2): test & check if MP1/MP2 realy need this now.
|
|
|
|
if (_fifo.bFF_BPEnable)
|
|
|
|
{
|
|
|
|
if (readPtr == _fifo.CPBreakpoint)
|
|
|
|
{
|
|
|
|
video_initialize.pLog("!!! BP irq raised",FALSE);
|
|
|
|
Common::SyncInterlockedExchange((LONG*)&_fifo.bFF_Breakpoint, 1);
|
|
|
|
|
|
|
|
video_initialize.pUpdateInterrupts();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
distToSend = 32;
|
|
|
|
readPtr += 32;
|
|
|
|
if ( readPtr >= _fifo.CPEnd)
|
|
|
|
readPtr = _fifo.CPBase;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if 0 // ugly random GP slowdown for testing DC robustness... TODO: remove when completly sure DC is ok
|
2009-02-20 22:04:52 +00:00
|
|
|
int r = rand();
|
|
|
|
if ((r & 0xF) == r)
|
|
|
|
Common::SleepCurrentThread(r);
|
|
|
|
distToSend = 32;
|
|
|
|
readPtr += 32;
|
|
|
|
if (readPtr >= _fifo.CPEnd)
|
|
|
|
readPtr = _fifo.CPBase;
|
2008-12-08 05:30:24 +00:00
|
|
|
#else
|
|
|
|
distToSend = _fifo.CPReadWriteDistance;
|
2009-02-20 22:04:52 +00:00
|
|
|
// send 1024B chunk max length to have better control over PeekMessages' period
|
2009-01-24 14:43:17 +00:00
|
|
|
distToSend = distToSend > 1024 ? 1024 : distToSend;
|
2009-02-20 22:04:52 +00:00
|
|
|
if ((distToSend + readPtr) >= _fifo.CPEnd) // TODO: better?
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
distToSend =_fifo.CPEnd - readPtr;
|
|
|
|
readPtr = _fifo.CPBase;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
readPtr += distToSend;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
Video_SendFifoData(uData, distToSend);
|
|
|
|
Common::SyncInterlockedExchange((LONG*)&_fifo.CPReadPointer, readPtr);
|
2009-01-27 20:59:27 +00:00
|
|
|
Common::SyncInterlockedExchangeAdd((LONG*)&_fifo.CPReadWriteDistance, -distToSend);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
//video_initialize.pLog("..........................IDLE",FALSE);
|
|
|
|
Common::SyncInterlockedExchange((LONG*)&_fifo.CPReadIdle, 1);
|
|
|
|
}
|
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
fifo_exit_event.Set();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|