2008-07-12 17:40:22 +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"
|
2008-08-21 17:49:06 +00:00
|
|
|
|
|
|
|
#include "Fifo.h"
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-29 17:29:25 +00:00
|
|
|
#if defined(DATAREADER_INLINE)
|
|
|
|
extern u32 g_pVideoData;
|
|
|
|
#else
|
2008-07-12 17:40:22 +00:00
|
|
|
FifoReader fifo;
|
2008-09-29 17:29:25 +00:00
|
|
|
#endif
|
2008-08-24 18:50:51 +00:00
|
|
|
|
2008-10-02 17:03:24 +00:00
|
|
|
bool fifoStateRun = true;
|
|
|
|
|
2008-08-24 18:50:51 +00:00
|
|
|
// STATE_TO_SAVE
|
2008-07-12 17:40:22 +00:00
|
|
|
static u8 *videoBuffer;
|
2008-10-02 14:25:03 +00:00
|
|
|
static int size = 0;
|
|
|
|
static int readptr = 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-08-30 12:11:25 +00:00
|
|
|
void Fifo_DoState(PointerWrap &p) {
|
2008-08-30 16:24:45 +00:00
|
|
|
p.DoArray(videoBuffer, FIFO_SIZE);
|
2008-08-30 16:05:32 +00:00
|
|
|
p.Do(size);
|
2008-08-30 12:11:25 +00:00
|
|
|
p.Do(readptr);
|
2008-08-28 09:19:46 +00:00
|
|
|
}
|
|
|
|
|
2008-07-12 17:40:22 +00:00
|
|
|
void Fifo_Init()
|
|
|
|
{
|
|
|
|
videoBuffer = (u8*)AllocateMemoryPages(FIFO_SIZE);
|
2008-09-29 17:29:25 +00:00
|
|
|
#ifndef DATAREADER_INLINE
|
2008-08-30 16:05:32 +00:00
|
|
|
fifo.Init(videoBuffer, videoBuffer); //zero length. there is no data yet.
|
2008-09-29 17:29:25 +00:00
|
|
|
#endif
|
2008-10-02 17:03:24 +00:00
|
|
|
fifoStateRun = true;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fifo_Shutdown()
|
|
|
|
{
|
|
|
|
FreeMemoryPages(videoBuffer, FIFO_SIZE);
|
2008-10-02 17:03:24 +00:00
|
|
|
fifoStateRun = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fifo_Stop() {
|
|
|
|
fifoStateRun = false;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 14:25:03 +00:00
|
|
|
u32 FAKE_GetFifoStartPtr()
|
2008-09-29 17:29:25 +00:00
|
|
|
{
|
2008-10-02 14:25:03 +00:00
|
|
|
return (int)videoBuffer;
|
2008-09-29 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 14:25:03 +00:00
|
|
|
int FAKE_GetFifoSize()
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
if (size < readptr)
|
|
|
|
{
|
2008-08-20 18:31:48 +00:00
|
|
|
PanicAlert("GFX Fifo underrun encountered (size = %i, readptr = %i)", size, readptr);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
return (size - readptr);
|
|
|
|
}
|
2008-10-02 14:25:03 +00:00
|
|
|
int FAKE_GetFifoEndAddr()
|
2008-09-29 17:29:25 +00:00
|
|
|
{
|
2008-10-02 14:25:03 +00:00
|
|
|
return (int)(videoBuffer+size);
|
2008-09-29 17:29:25 +00:00
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
|
|
|
u8 FAKE_PeekFifo8(u32 _uOffset)
|
|
|
|
{
|
|
|
|
return videoBuffer[readptr + _uOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 FAKE_PeekFifo16(u32 _uOffset)
|
|
|
|
{
|
|
|
|
return Common::swap16(*(u16*)&videoBuffer[readptr + _uOffset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 FAKE_PeekFifo32(u32 _uOffset)
|
|
|
|
{
|
|
|
|
return Common::swap32(*(u32*)&videoBuffer[readptr + _uOffset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 FAKE_ReadFifo8()
|
|
|
|
{
|
|
|
|
return videoBuffer[readptr++];
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:36:52 +00:00
|
|
|
int FAKE_GetPosition()
|
|
|
|
{
|
|
|
|
return readptr;
|
|
|
|
}
|
|
|
|
|
2008-10-02 14:25:03 +00:00
|
|
|
int FAKE_GetRealPtr()
|
2008-09-29 17:29:25 +00:00
|
|
|
{
|
2008-10-02 14:25:03 +00:00
|
|
|
return (int)(videoBuffer+readptr);
|
2008-09-29 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2008-07-12 17:40:22 +00:00
|
|
|
u16 FAKE_ReadFifo16()
|
|
|
|
{
|
|
|
|
u16 val = Common::swap16(*(u16*)(videoBuffer+readptr));
|
|
|
|
readptr += 2;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 FAKE_ReadFifo32()
|
|
|
|
{
|
|
|
|
u32 val = Common::swap32(*(u32*)(videoBuffer+readptr));
|
|
|
|
readptr += 4;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAKE_SkipFifo(u32 skip)
|
|
|
|
{
|
|
|
|
readptr += skip;
|
|
|
|
}
|
|
|
|
|
2008-08-31 14:32:35 +00:00
|
|
|
void Video_SendFifoData(u8* _uData)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-29 17:29:25 +00:00
|
|
|
// TODO (mb2): unrolled loop faster than memcpy here?
|
2008-07-12 17:40:22 +00:00
|
|
|
memcpy(videoBuffer + size, _uData, 32);
|
|
|
|
size += 32;
|
|
|
|
if (size + 32 >= FIFO_SIZE)
|
|
|
|
{
|
2008-09-29 17:29:25 +00:00
|
|
|
// TODO (mb2): Better and DataReader inline for DX9
|
|
|
|
#ifdef DATAREADER_INLINE
|
|
|
|
if (g_pVideoData) // for DX9 plugin "compatibility"
|
2008-10-02 14:25:03 +00:00
|
|
|
readptr = g_pVideoData-(u32)videoBuffer;
|
2008-09-29 17:29:25 +00:00
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
if (FAKE_GetFifoSize() > readptr)
|
|
|
|
{
|
2008-08-21 17:49:06 +00:00
|
|
|
PanicAlert("FIFO out of bounds (sz = %i, at %08x)", FAKE_GetFifoSize(), readptr);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-08-21 17:49:06 +00:00
|
|
|
// DebugLog("FAKE BUFFER LOOPS");
|
2008-07-12 17:40:22 +00:00
|
|
|
memmove(&videoBuffer[0], &videoBuffer[readptr], FAKE_GetFifoSize());
|
|
|
|
// memset(&videoBuffer[FAKE_GetFifoSize()], 0, FIFO_SIZE - FAKE_GetFifoSize());
|
|
|
|
size = FAKE_GetFifoSize();
|
|
|
|
readptr = 0;
|
2008-09-29 17:29:25 +00:00
|
|
|
#ifdef DATAREADER_INLINE
|
|
|
|
if (g_pVideoData) // for DX9 plugin "compatibility"
|
|
|
|
g_pVideoData = FAKE_GetFifoStartPtr();
|
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
OpcodeDecoder_Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//TODO - turn inside out, have the "reader" ask for bytes instead
|
|
|
|
// See Core.cpp for threading idea
|
2008-08-21 17:49:06 +00:00
|
|
|
void Fifo_EnterLoop(const SVideoInitialize &video_initialize)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-01 12:11:08 +00:00
|
|
|
SCPFifoStruct &_fifo = *video_initialize.pCPFifo;
|
2008-09-24 10:52:58 +00:00
|
|
|
#if defined(THREAD_VIDEO_WAKEUP_ONIDLE) && defined(_WIN32)
|
|
|
|
HANDLE hEventOnIdle= OpenEventA(EVENT_ALL_ACCESS,FALSE,(LPCSTR)"EventOnIdle");
|
|
|
|
if (hEventOnIdle==NULL) PanicAlert("Fifo_EnterLoop() -> EventOnIdle NULL");
|
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-10-02 17:12:47 +00:00
|
|
|
#ifdef _WIN32
|
2008-07-12 17:40:22 +00:00
|
|
|
// TODO(ector): Don't peek so often!
|
2008-10-02 17:12:47 +00:00
|
|
|
while (video_initialize.pPeekMessages())
|
|
|
|
#else
|
|
|
|
while (fifoStateRun)
|
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-24 10:52:58 +00:00
|
|
|
#if defined(THREAD_VIDEO_WAKEUP_ONIDLE) && defined(_WIN32)
|
|
|
|
if (MsgWaitForMultipleObjects(1, &hEventOnIdle, FALSE, 1L, QS_ALLEVENTS) == WAIT_ABANDONED)
|
|
|
|
break;
|
|
|
|
#endif
|
2008-09-01 12:11:08 +00:00
|
|
|
if (_fifo.CPReadWriteDistance < 1) //fifo.CPLoWatermark)
|
2008-09-24 10:52:58 +00:00
|
|
|
#if defined(THREAD_VIDEO_WAKEUP_ONIDLE) && defined(_WIN32)
|
|
|
|
continue;
|
|
|
|
#else
|
2008-07-12 17:40:22 +00:00
|
|
|
Common::SleepCurrentThread(1);
|
2008-09-24 10:52:58 +00:00
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
//etc...
|
|
|
|
|
|
|
|
// check if we are able to run this buffer
|
2008-09-01 12:11:08 +00:00
|
|
|
if ((_fifo.bFF_GPReadEnable) && !(_fifo.bFF_BPEnable && _fifo.bFF_Breakpoint))
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-24 10:52:58 +00:00
|
|
|
#if defined(THREAD_VIDEO_WAKEUP_ONIDLE) && defined(_WIN32)
|
|
|
|
while(_fifo.CPReadWriteDistance > 0)
|
|
|
|
#else
|
2008-10-02 10:43:32 +00:00
|
|
|
int count = 200;
|
2008-09-01 12:11:08 +00:00
|
|
|
while(_fifo.CPReadWriteDistance > 0 && count)
|
2008-09-24 10:52:58 +00:00
|
|
|
#endif
|
|
|
|
{
|
2008-07-12 17:40:22 +00:00
|
|
|
// check if we are on a breakpoint
|
2008-09-01 12:11:08 +00:00
|
|
|
if (_fifo.bFF_BPEnable)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-01 12:11:08 +00:00
|
|
|
if (_fifo.CPReadPointer == _fifo.CPBreakpoint)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-01 12:11:08 +00:00
|
|
|
_fifo.bFF_Breakpoint = 1;
|
2008-08-21 17:49:06 +00:00
|
|
|
video_initialize.pUpdateInterrupts();
|
2008-07-12 17:40:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the data and send it to the VideoPlugin
|
2008-09-01 12:11:08 +00:00
|
|
|
u8 *uData = video_initialize.pGetMemoryPointer(_fifo.CPReadPointer);
|
2008-07-12 17:40:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-09-01 13:31:24 +00:00
|
|
|
EnterCriticalSection(&_fifo.sync);
|
2008-10-02 10:43:32 +00:00
|
|
|
#else
|
|
|
|
_fifo.sync->Enter();
|
2008-07-12 17:40:22 +00:00
|
|
|
#endif
|
2008-09-01 12:11:08 +00:00
|
|
|
_fifo.CPReadPointer += 32;
|
2008-07-12 17:40:22 +00:00
|
|
|
Video_SendFifoData(uData);
|
|
|
|
#ifdef _WIN32
|
2008-09-01 12:11:08 +00:00
|
|
|
InterlockedExchangeAdd((LONG*)&_fifo.CPReadWriteDistance, -32);
|
2008-09-01 13:31:24 +00:00
|
|
|
LeaveCriticalSection(&_fifo.sync);
|
2008-10-02 10:43:32 +00:00
|
|
|
#else
|
|
|
|
_fifo.CPReadWriteDistance -= 32;
|
|
|
|
_fifo.sync->Leave();
|
2008-07-12 17:40:22 +00:00
|
|
|
#endif
|
|
|
|
// increase the ReadPtr
|
2008-09-01 12:11:08 +00:00
|
|
|
if (_fifo.CPReadPointer >= _fifo.CPEnd)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-01 12:11:08 +00:00
|
|
|
_fifo.CPReadPointer = _fifo.CPBase;
|
2008-07-12 17:40:22 +00:00
|
|
|
//LOG(COMMANDPROCESSOR, "BUFFER LOOP");
|
|
|
|
}
|
2008-09-24 10:52:58 +00:00
|
|
|
#ifndef THREAD_VIDEO_WAKEUP_ONIDLE
|
2008-07-12 17:40:22 +00:00
|
|
|
count--;
|
2008-09-24 10:52:58 +00:00
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2008-09-24 10:52:58 +00:00
|
|
|
#if defined(THREAD_VIDEO_WAKEUP_ONIDLE) && defined(_WIN32)
|
|
|
|
CloseHandle(hEventOnIdle);
|
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|