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>
|
2009-02-22 21:16:12 +00:00
|
|
|
#include "Setup.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
#include "MemoryUtil.h"
|
|
|
|
#include "Thread.h"
|
2009-07-13 05:38:34 +00:00
|
|
|
#include "Atomic.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
#include "OpcodeDecoding.h"
|
|
|
|
|
|
|
|
#include "Fifo.h"
|
|
|
|
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
extern u8* g_pVideoData;
|
2009-01-24 14:43:17 +00:00
|
|
|
|
2009-07-02 10:16:06 +00:00
|
|
|
volatile bool g_EFBAccessRequested = false;
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
namespace {
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-02-22 22:49:42 +00:00
|
|
|
static volatile 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;
|
2009-07-03 10:00:09 +00:00
|
|
|
static Common::CriticalSection s_criticalFifo;
|
2009-02-20 22:04:52 +00:00
|
|
|
// 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)
|
|
|
|
{
|
2009-07-03 10:00:09 +00:00
|
|
|
s_criticalFifo.Enter();
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
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
|
2009-07-03 10:00:09 +00:00
|
|
|
|
|
|
|
s_criticalFifo.Leave();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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-22 22:49:42 +00:00
|
|
|
// Executed from another thread, no the graphics thread!
|
|
|
|
// Basically, all it does is set a flag so that the loop will eventually exit, then
|
|
|
|
// waits for the event to be set, which happens when the loop does exit.
|
|
|
|
// If we look stuck in here, then the video thread is stuck in something and won't exit
|
|
|
|
// the loop. Switch to the video thread and investigate.
|
2009-02-20 22:04:52 +00:00
|
|
|
void Fifo_ExitLoop()
|
|
|
|
{
|
|
|
|
fifoStateRun = false;
|
2009-07-15 15:09:20 +00:00
|
|
|
|
|
|
|
fifo_exit_event.MsgWait();
|
|
|
|
fifo_exit_event.Shutdown();
|
2009-02-20 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 23:34:16 +00:00
|
|
|
// May be executed from any thread, even the graphics thread.
|
|
|
|
// Created to allow for self shutdown.
|
|
|
|
void Fifo_ExitLoopNonBlocking() {
|
|
|
|
fifoStateRun = false;
|
|
|
|
}
|
|
|
|
|
2009-06-15 04:30:02 +00:00
|
|
|
// Description: Fifo_EnterLoop() sends data through this function.
|
2009-07-17 22:57:02 +00:00
|
|
|
void Fifo_SendFifoData(u8* _uData, u32 len)
|
2009-06-15 04:30:02 +00:00
|
|
|
{
|
|
|
|
if (size + len >= FIFO_SIZE)
|
|
|
|
{
|
|
|
|
int pos = (int)(g_pVideoData - videoBuffer);
|
|
|
|
if (size - pos > pos)
|
|
|
|
{
|
|
|
|
PanicAlert("FIFO out of bounds (sz = %i, at %08x)", size, pos);
|
|
|
|
}
|
|
|
|
memmove(&videoBuffer[0], &videoBuffer[pos], size - pos);
|
|
|
|
size -= pos;
|
|
|
|
g_pVideoData = FAKE_GetFifoStartPtr();
|
|
|
|
}
|
|
|
|
// Copy new video instructions to videoBuffer for future use in rendering the new picture
|
|
|
|
memcpy(videoBuffer + size, _uData, len);
|
|
|
|
size += len;
|
|
|
|
OpcodeDecoder_Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description: Main FIFO update loop
|
|
|
|
// Purpose: Keep the Core HW updated about the CPU-GPU distance
|
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-20 22:04:52 +00:00
|
|
|
video_initialize.pPeekMessages();
|
2009-02-22 21:16:12 +00:00
|
|
|
|
2009-07-11 16:52:34 +00:00
|
|
|
VideoFifo_CheckEFBAccess();
|
2009-07-03 15:35:31 +00:00
|
|
|
|
2009-07-17 22:57:02 +00:00
|
|
|
VideoFifo_CheckSwapRequest();
|
|
|
|
|
2009-07-03 10:00:09 +00:00
|
|
|
s_criticalFifo.Enter();
|
2009-07-03 15:35:31 +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))
|
|
|
|
{
|
2009-07-13 05:38:34 +00:00
|
|
|
Common::AtomicStore(_fifo.CPReadIdle, 0);
|
2009-07-17 22:57:02 +00:00
|
|
|
|
2009-01-24 14:43:17 +00:00
|
|
|
while (_fifo.bFF_GPReadEnable && _fifo.CPReadWriteDistance)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-07-03 10:00:09 +00:00
|
|
|
if(!fifoStateRun)
|
|
|
|
break;
|
|
|
|
|
2009-06-15 04:30:02 +00:00
|
|
|
// Create pointer to video data and send it to the VideoPlugin
|
2008-12-08 05:30:24 +00:00
|
|
|
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)
|
|
|
|
{
|
2009-07-13 05:38:34 +00:00
|
|
|
Common::AtomicStore(_fifo.bFF_Breakpoint, 1);
|
2008-12-08 05:30:24 +00:00
|
|
|
video_initialize.pUpdateInterrupts();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
distToSend = 32;
|
|
|
|
readPtr += 32;
|
|
|
|
if ( readPtr >= _fifo.CPEnd)
|
|
|
|
readPtr = _fifo.CPBase;
|
|
|
|
}
|
|
|
|
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
|
2009-07-17 22:57:02 +00:00
|
|
|
readPtr += distToSend;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-07-17 22:57:02 +00:00
|
|
|
|
2009-06-15 04:30:02 +00:00
|
|
|
// Execute new instructions found in uData
|
2009-07-17 22:57:02 +00:00
|
|
|
Fifo_SendFifoData(uData, distToSend);
|
|
|
|
|
2009-07-13 05:38:34 +00:00
|
|
|
Common::AtomicStore(_fifo.CPReadPointer, readPtr);
|
|
|
|
Common::AtomicAdd(_fifo.CPReadWriteDistance, -distToSend);
|
2009-07-17 22:57:02 +00:00
|
|
|
|
|
|
|
video_initialize.pPeekMessages();
|
|
|
|
|
|
|
|
VideoFifo_CheckEFBAccess();
|
|
|
|
|
|
|
|
VideoFifo_CheckSwapRequest();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-07-17 22:57:02 +00:00
|
|
|
|
2009-07-13 05:38:34 +00:00
|
|
|
Common::AtomicStore(_fifo.CPReadIdle, 1);
|
2009-07-18 06:16:24 +00:00
|
|
|
video_initialize.pSetFifoIdle();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-07-17 22:57:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Common::YieldCPU();
|
|
|
|
}
|
|
|
|
|
2009-07-03 10:00:09 +00:00
|
|
|
s_criticalFifo.Leave();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
fifo_exit_event.Set();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|