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-10-02 18:43:56 +00:00
|
|
|
extern u8* g_pVideoData;
|
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;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-10-03 22:05:28 +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-10-05 11:52:44 +00:00
|
|
|
int pos = (int)(g_pVideoData-videoBuffer); // get offset
|
|
|
|
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
|
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-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;
|
|
|
|
}
|
|
|
|
|
2008-10-03 22:05:28 +00:00
|
|
|
void Fifo_Stop()
|
|
|
|
{
|
2008-10-02 17:03:24 +00:00
|
|
|
fifoStateRun = false;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 18:43:56 +00:00
|
|
|
u8* FAKE_GetFifoStartPtr()
|
2008-09-29 17:29:25 +00:00
|
|
|
{
|
2008-10-02 18:43:56 +00:00
|
|
|
return videoBuffer;
|
2008-09-29 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 18:43:56 +00:00
|
|
|
u8* FAKE_GetFifoEndPtr()
|
2008-09-29 17:29:25 +00:00
|
|
|
{
|
2008-10-02 18:43:56 +00:00
|
|
|
return &videoBuffer[size];
|
2008-09-29 17:29:25 +00:00
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-11-14 22:33:48 +00:00
|
|
|
void Video_SendFifoData(u8* _uData, u32 len)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-11-14 22:33:48 +00:00
|
|
|
if (size + len >= FIFO_SIZE)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-10-03 22:05:28 +00:00
|
|
|
int pos = (int)(g_pVideoData-videoBuffer);
|
2008-10-30 18:13:13 +00:00
|
|
|
if (size-pos > pos)
|
|
|
|
{
|
|
|
|
PanicAlert("FIFO out of bounds (sz = %i, at %08x)", size, pos);
|
|
|
|
}
|
2008-10-03 22:05:28 +00:00
|
|
|
memmove(&videoBuffer[0], &videoBuffer[pos], size - pos );
|
|
|
|
size -= pos;
|
|
|
|
g_pVideoData = FAKE_GetFifoStartPtr();
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-11-14 22:33:48 +00:00
|
|
|
memcpy(videoBuffer + size, _uData, len);
|
|
|
|
size += len;
|
2008-07-12 17:40:22 +00:00
|
|
|
OpcodeDecoder_Run();
|
|
|
|
}
|
|
|
|
|
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-11-15 21:54:46 +00:00
|
|
|
u32 distToSend;
|
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-11-15 21:54:46 +00:00
|
|
|
if (_fifo.CPReadWriteDistance < _fifo.CPLoWatermark)
|
2008-11-03 09:01:14 +00:00
|
|
|
Common::SleepCurrentThread(1);
|
2008-07-12 17:40:22 +00:00
|
|
|
//etc...
|
|
|
|
|
|
|
|
// check if we are able to run this buffer
|
2008-11-22 15:25:04 +00:00
|
|
|
if ((_fifo.bFF_GPReadEnable) && _fifo.CPReadWriteDistance && !(_fifo.bFF_BPEnable && _fifo.bFF_Breakpoint))
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-11-22 20:11:26 +00:00
|
|
|
#ifdef _WIN32
|
2008-11-22 15:25:04 +00:00
|
|
|
InterlockedExchange((LONG*)&_fifo.CPReadIdle, 0);
|
2008-11-22 20:11:26 +00:00
|
|
|
#else
|
|
|
|
Common::InterlockedExchange((int*)&_fifo.CPReadIdle, 0);
|
|
|
|
#endif
|
2008-11-24 16:33:38 +00:00
|
|
|
//video_initialize.pLog("RUN...........................",FALSE);
|
2008-11-14 22:33:48 +00:00
|
|
|
while(_fifo.bFF_GPReadEnable && (_fifo.CPReadWriteDistance > 0) )
|
2008-09-24 10:52:58 +00:00
|
|
|
{
|
2008-11-15 21:54:46 +00:00
|
|
|
// read the data and send it to the VideoPlugin
|
|
|
|
u32 readPtr = _fifo.CPReadPointer;
|
2008-11-24 16:33:38 +00:00
|
|
|
u8 *uData = video_initialize.pGetMemoryPointer(readPtr);
|
|
|
|
|
2008-11-18 13:08:46 +00:00
|
|
|
// if we are on BP mode we must send 32B chunks to Video plugin for BP checking
|
2008-11-24 16:33:38 +00:00
|
|
|
// TODO (mb2): test & check if MP1/MP2 realy need this now.
|
2008-11-15 21:54:46 +00:00
|
|
|
if (_fifo.bFF_BPEnable)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-11-15 21:54:46 +00:00
|
|
|
if (readPtr == _fifo.CPBreakpoint)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-10-30 18:13:13 +00:00
|
|
|
video_initialize.pLog("!!! BP irq raised",FALSE);
|
2008-10-09 08:51:57 +00:00
|
|
|
#ifdef _WIN32
|
2008-10-30 18:13:13 +00:00
|
|
|
InterlockedExchange((LONG*)&_fifo.bFF_Breakpoint, 1);
|
2008-10-09 08:51:57 +00:00
|
|
|
#else
|
2008-10-30 19:48:26 +00:00
|
|
|
Common::InterlockedExchange((int*)&_fifo.bFF_Breakpoint, 1);
|
2008-10-09 08:51:57 +00:00
|
|
|
#endif
|
2008-10-07 21:39:50 +00:00
|
|
|
video_initialize.pUpdateInterrupts();
|
2008-07-12 17:40:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-11-18 13:08:46 +00:00
|
|
|
distToSend = 32;
|
|
|
|
readPtr += 32;
|
2008-11-24 16:33:38 +00:00
|
|
|
if ( readPtr >= _fifo.CPEnd)
|
2008-11-18 13:08:46 +00:00
|
|
|
readPtr = _fifo.CPBase;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-11-15 21:54:46 +00:00
|
|
|
else
|
2008-11-14 22:33:48 +00:00
|
|
|
{
|
2008-11-24 16:33:38 +00:00
|
|
|
#if 0 // ugly random GP slowdown for testing DC robustness... TODO: remove when completly sure DC is ok
|
|
|
|
int r=rand();if ((r&0xF)==r) Common::SleepCurrentThread(r);
|
|
|
|
distToSend = 32;
|
|
|
|
readPtr += 32;
|
|
|
|
if ( readPtr >= _fifo.CPEnd)
|
|
|
|
readPtr = _fifo.CPBase;
|
|
|
|
#else
|
2008-11-18 13:08:46 +00:00
|
|
|
// sending the whole CPReadWriteDistance
|
2008-11-15 21:54:46 +00:00
|
|
|
distToSend = _fifo.CPReadWriteDistance;
|
2008-11-24 16:33:38 +00:00
|
|
|
if ( (distToSend+readPtr) >= _fifo.CPEnd) // TODO: better?
|
2008-11-15 21:54:46 +00:00
|
|
|
{
|
|
|
|
distToSend =_fifo.CPEnd - readPtr;
|
|
|
|
readPtr = _fifo.CPBase;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
readPtr += distToSend;
|
2008-11-24 16:33:38 +00:00
|
|
|
#endif
|
2008-11-14 22:33:48 +00:00
|
|
|
}
|
2008-11-15 21:54:46 +00:00
|
|
|
Video_SendFifoData(uData, distToSend);
|
2008-07-12 17:40:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-10-09 08:51:57 +00:00
|
|
|
InterlockedExchange((LONG*)&_fifo.CPReadPointer, readPtr);
|
2008-11-15 21:54:46 +00:00
|
|
|
InterlockedExchangeAdd((LONG*)&_fifo.CPReadWriteDistance, -distToSend);
|
2008-10-02 10:43:32 +00:00
|
|
|
#else
|
2008-10-30 19:48:26 +00:00
|
|
|
Common::InterlockedExchange((int*)&_fifo.CPReadPointer, readPtr);
|
2008-11-15 22:32:20 +00:00
|
|
|
Common::InterlockedExchangeAdd((int*)&_fifo.CPReadWriteDistance, -distToSend);
|
2008-07-12 17:40:22 +00:00
|
|
|
#endif
|
2008-11-14 22:33:48 +00:00
|
|
|
}
|
2008-11-24 16:33:38 +00:00
|
|
|
//video_initialize.pLog("..........................IDLE",FALSE);
|
2008-11-22 20:11:26 +00:00
|
|
|
#ifdef _WIN32
|
2008-11-22 15:25:04 +00:00
|
|
|
InterlockedExchange((LONG*)&_fifo.CPReadIdle, 1);
|
2008-11-22 20:11:26 +00:00
|
|
|
#else
|
|
|
|
Common::InterlockedExchange((int*)&_fifo.CPReadIdle, 1);
|
|
|
|
#endif
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|