2009-07-28 23:32:10 +02:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 06:30:24 +01:00
|
|
|
|
|
|
|
// 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-10-07 21:54:56 +02:00
|
|
|
|
|
|
|
#include "VideoConfig.h"
|
2009-02-22 22:16:12 +01:00
|
|
|
#include "Setup.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
#include "MemoryUtil.h"
|
|
|
|
#include "Thread.h"
|
2009-07-13 07:38:34 +02:00
|
|
|
#include "Atomic.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
#include "OpcodeDecoding.h"
|
2009-10-10 23:19:39 +02:00
|
|
|
#include "CommandProcessor.h"
|
2009-12-16 18:05:30 +01:00
|
|
|
#include "ChunkFile.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
#include "Fifo.h"
|
|
|
|
|
2009-08-08 03:39:56 +02:00
|
|
|
volatile bool g_bSkipCurrentFrame = false;
|
2009-07-02 12:16:06 +02:00
|
|
|
volatile bool g_EFBAccessRequested = false;
|
2009-08-15 09:33:35 +02:00
|
|
|
extern u8* g_pVideoData;
|
2009-07-02 12:16:06 +02:00
|
|
|
|
2009-08-15 09:33:35 +02:00
|
|
|
namespace
|
|
|
|
{
|
2009-02-22 23:49:42 +01:00
|
|
|
static volatile bool fifoStateRun = false;
|
2010-01-07 21:01:41 +01:00
|
|
|
static volatile bool EmuRunning = false;
|
2008-12-08 06:30:24 +01:00
|
|
|
static u8 *videoBuffer;
|
2010-01-14 11:52:14 +01:00
|
|
|
static Common::EventEx fifo_run_event;
|
2009-02-20 23:04:52 +01:00
|
|
|
// STATE_TO_SAVE
|
2008-12-08 06:30:24 +01:00
|
|
|
static int size = 0;
|
2009-02-20 23:04:52 +01:00
|
|
|
} // namespace
|
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
void Fifo_DoState(PointerWrap &p)
|
|
|
|
{
|
2009-12-16 18:05:30 +01:00
|
|
|
CommandProcessor::FifoCriticalEnter();
|
2009-07-03 12:00:09 +02:00
|
|
|
|
2008-12-08 06:30:24 +01:00
|
|
|
p.DoArray(videoBuffer, FIFO_SIZE);
|
|
|
|
p.Do(size);
|
2009-02-20 23:04:52 +01:00
|
|
|
int pos = (int)(g_pVideoData - videoBuffer); // get offset
|
2008-12-08 06:30:24 +01: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 12:00:09 +02:00
|
|
|
|
2009-12-16 18:05:30 +01:00
|
|
|
CommandProcessor::FifoCriticalLeave();
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fifo_Init()
|
|
|
|
{
|
|
|
|
videoBuffer = (u8*)AllocateMemoryPages(FIFO_SIZE);
|
2010-01-03 17:04:40 +01:00
|
|
|
fifo_run_event.Init();
|
2009-02-21 01:54:52 +01:00
|
|
|
fifoStateRun = false;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fifo_Shutdown()
|
|
|
|
{
|
2010-01-03 17:04:40 +01:00
|
|
|
if (fifoStateRun) PanicAlert("Fifo shutting down while active");
|
|
|
|
fifo_run_event.Shutdown();
|
2008-12-08 06:30:24 +01:00
|
|
|
FreeMemoryPages(videoBuffer, FIFO_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* FAKE_GetFifoStartPtr()
|
|
|
|
{
|
|
|
|
return videoBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* FAKE_GetFifoEndPtr()
|
|
|
|
{
|
|
|
|
return &videoBuffer[size];
|
|
|
|
}
|
|
|
|
|
2009-08-15 09:33:35 +02:00
|
|
|
void Fifo_SetRendering(bool enabled)
|
|
|
|
{
|
|
|
|
g_bSkipCurrentFrame = !enabled;
|
2009-08-08 03:39:56 +02:00
|
|
|
}
|
|
|
|
|
2009-02-22 23:49:42 +01: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 23:04:52 +01:00
|
|
|
void Fifo_ExitLoop()
|
|
|
|
{
|
2010-01-03 17:04:40 +01:00
|
|
|
Fifo_ExitLoopNonBlocking();
|
2009-02-20 23:04:52 +01:00
|
|
|
}
|
|
|
|
|
2009-03-08 00:34:16 +01:00
|
|
|
// May be executed from any thread, even the graphics thread.
|
|
|
|
// Created to allow for self shutdown.
|
2009-08-09 13:03:58 +02:00
|
|
|
void Fifo_ExitLoopNonBlocking()
|
|
|
|
{
|
2009-03-08 00:34:16 +01:00
|
|
|
fifoStateRun = false;
|
2010-01-03 17:04:40 +01:00
|
|
|
fifo_run_event.Set();
|
|
|
|
}
|
|
|
|
|
2010-01-07 21:01:41 +01:00
|
|
|
void Fifo_RunLoop(bool run)
|
2010-01-03 17:04:40 +01:00
|
|
|
{
|
2010-01-07 21:01:41 +01:00
|
|
|
EmuRunning = run;
|
|
|
|
if (run)
|
|
|
|
fifo_run_event.Set();
|
2009-03-08 00:34:16 +01:00
|
|
|
}
|
|
|
|
|
2009-06-15 06:30:02 +02:00
|
|
|
// Description: Fifo_EnterLoop() sends data through this function.
|
2009-07-18 00:57:02 +02:00
|
|
|
void Fifo_SendFifoData(u8* _uData, u32 len)
|
2009-06-15 06:30:02 +02: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;
|
2009-12-16 18:05:30 +01:00
|
|
|
g_pVideoData = videoBuffer;
|
2009-06-15 06:30:02 +02:00
|
|
|
}
|
|
|
|
// Copy new video instructions to videoBuffer for future use in rendering the new picture
|
|
|
|
memcpy(videoBuffer + size, _uData, len);
|
|
|
|
size += len;
|
2009-08-09 13:03:58 +02:00
|
|
|
OpcodeDecoder_Run(g_bSkipCurrentFrame);
|
2009-06-15 06:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Description: Main FIFO update loop
|
|
|
|
// Purpose: Keep the Core HW updated about the CPU-GPU distance
|
2008-12-08 06:30:24 +01:00
|
|
|
void Fifo_EnterLoop(const SVideoInitialize &video_initialize)
|
|
|
|
{
|
2009-12-31 21:49:04 +01:00
|
|
|
fifoStateRun = true;
|
|
|
|
SCPFifoStruct &_fifo = CommandProcessor::fifo;
|
2009-07-24 00:32:21 +02:00
|
|
|
s32 distToSend;
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2009-12-31 21:49:04 +01:00
|
|
|
while (fifoStateRun)
|
|
|
|
{
|
2009-07-23 23:17:14 +02:00
|
|
|
video_initialize.pPeekMessages();
|
2009-12-28 20:13:06 +01:00
|
|
|
|
|
|
|
VideoFifo_CheckEFBAccess();
|
2009-07-23 23:17:14 +02:00
|
|
|
VideoFifo_CheckSwapRequest();
|
2009-07-18 00:57:02 +02:00
|
|
|
|
2009-12-31 21:49:04 +01:00
|
|
|
// check if we are able to run this buffer
|
|
|
|
while (_fifo.bFF_GPReadEnable && ((!_fifo.bFF_BPEnable && _fifo.CPReadWriteDistance) || (_fifo.bFF_BPEnable && !_fifo.bFF_Breakpoint)))
|
2009-12-16 18:05:30 +01:00
|
|
|
{
|
2009-12-31 17:25:12 +01:00
|
|
|
if (!fifoStateRun)
|
|
|
|
break;
|
2009-07-18 00:57:02 +02:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
CommandProcessor::FifoCriticalEnter();
|
2009-07-03 12:00:09 +02:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
// Create pointer to video data and send it to the VideoPlugin
|
|
|
|
u32 readPtr = _fifo.CPReadPointer;
|
|
|
|
u8 *uData = video_initialize.pGetMemoryPointer(readPtr);
|
2009-08-15 09:33:35 +02:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
// If we are in BP mode we only send 32B chunks to Video plugin for BP checking
|
|
|
|
if (_fifo.bFF_BPEnable)
|
|
|
|
{
|
|
|
|
// Sometimes we have already exceeded the BP even before it is set
|
|
|
|
// so careful check is required
|
|
|
|
if (
|
|
|
|
(readPtr == _fifo.CPBreakpoint) ||
|
2010-01-03 17:04:40 +01:00
|
|
|
//(readPtr <= _fifo.CPBreakpoint && readPtr + 32 > _fifo.CPBreakpoint) ||
|
|
|
|
(readPtr <= _fifo.CPWritePointer && _fifo.CPWritePointer < _fifo.CPBreakpoint) ||
|
|
|
|
(readPtr <= _fifo.CPWritePointer && readPtr > _fifo.CPBreakpoint) ||
|
2009-12-31 21:49:04 +01:00
|
|
|
(readPtr > _fifo.CPBreakpoint && _fifo.CPBreakpoint > _fifo.CPWritePointer)
|
2009-12-31 17:25:12 +01:00
|
|
|
)
|
2009-07-23 23:17:14 +02:00
|
|
|
{
|
2009-12-31 17:25:12 +01:00
|
|
|
Common::AtomicStore(_fifo.bFF_Breakpoint, 1);
|
2010-01-13 10:03:55 +01:00
|
|
|
CommandProcessor::UpdateInterruptsFromVideoPlugin(true);
|
2010-01-14 11:52:14 +01:00
|
|
|
CommandProcessor::FifoCriticalLeave();
|
2009-12-31 17:25:12 +01:00
|
|
|
break;
|
2009-07-23 23:17:14 +02:00
|
|
|
}
|
2009-12-31 17:25:12 +01:00
|
|
|
distToSend = 32;
|
|
|
|
|
|
|
|
if ( readPtr >= _fifo.CPEnd)
|
|
|
|
readPtr = _fifo.CPBase;
|
2009-08-15 09:33:35 +02:00
|
|
|
else
|
2009-12-31 17:25:12 +01:00
|
|
|
readPtr += 32;
|
|
|
|
}
|
2009-12-31 21:49:04 +01:00
|
|
|
// If we are not in BP mode we send all the chunk we have to speed up
|
2009-12-31 17:25:12 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
distToSend = _fifo.CPReadWriteDistance;
|
|
|
|
// send 1024B chunk max length to have better control over PeekMessages' period
|
|
|
|
distToSend = distToSend > 1024 ? 1024 : distToSend;
|
|
|
|
// add 32 bytes because the cp end points to the start of the last 32 byte chunk
|
|
|
|
if ((distToSend + readPtr) >= (_fifo.CPEnd + 32)) // TODO: better?
|
2009-10-21 02:01:07 +02:00
|
|
|
{
|
2009-12-31 17:25:12 +01:00
|
|
|
distToSend =(_fifo.CPEnd + 32) - readPtr;
|
|
|
|
readPtr = _fifo.CPBase;
|
2009-10-21 02:01:07 +02:00
|
|
|
}
|
2009-12-31 17:25:12 +01:00
|
|
|
else
|
|
|
|
readPtr += distToSend;
|
|
|
|
}
|
2009-07-23 23:17:14 +02:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
// Execute new instructions found in uData
|
|
|
|
Fifo_SendFifoData(uData, distToSend);
|
2009-07-20 03:10:00 +02:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
Common::AtomicStore(_fifo.CPReadPointer, readPtr);
|
|
|
|
Common::AtomicAdd(_fifo.CPReadWriteDistance, -distToSend);
|
2009-12-17 05:01:34 +01:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
CommandProcessor::FifoCriticalLeave();
|
2009-12-17 05:01:34 +01:00
|
|
|
|
2009-12-31 17:25:12 +01:00
|
|
|
// Those two are pretty important and must be called in the FIFO Loop.
|
|
|
|
// If we don't, s_swapRequested (OGL only) or s_efbAccessRequested won't be set to false
|
|
|
|
// leading the CPU thread to wait in Video_BeginField or Video_AccessEFB thus slowing things down.
|
|
|
|
VideoFifo_CheckEFBAccess();
|
|
|
|
VideoFifo_CheckSwapRequest();
|
2009-12-31 21:49:04 +01:00
|
|
|
}
|
2010-01-02 06:15:36 +01:00
|
|
|
CommandProcessor::SetFifoIdleFromVideoPlugin();
|
2010-01-07 21:01:41 +01:00
|
|
|
if (EmuRunning)
|
2010-01-03 19:58:50 +01:00
|
|
|
Common::YieldCPU();
|
|
|
|
else
|
|
|
|
fifo_run_event.MsgWait();
|
2009-12-28 20:13:06 +01:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
2009-06-15 08:39:26 +02:00
|
|
|
|