2009-07-29 00:32:10 +03:00
// Copyright (C) 2003 Dolphin Project.
2008-12-08 07:30:24 +02: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/
2009-10-07 22:54:56 +03:00
# include "VideoConfig.h"
2009-02-22 23:16:12 +02:00
# include "Setup.h"
2008-12-08 07:30:24 +02:00
# include "MemoryUtil.h"
# include "Thread.h"
2009-07-13 08:38:34 +03:00
# include "Atomic.h"
2008-12-08 07:30:24 +02:00
# include "OpcodeDecoding.h"
2009-10-11 00:19:39 +03:00
# include "CommandProcessor.h"
2009-12-16 19:05:30 +02:00
# include "ChunkFile.h"
2008-12-08 07:30:24 +02:00
# include "Fifo.h"
2011-01-31 03:28:32 +02:00
# include "HW/Memmap.h"
2008-12-08 07:30:24 +02:00
2009-08-08 04:39:56 +03:00
volatile bool g_bSkipCurrentFrame = false ;
2009-08-15 10:33:35 +03:00
extern u8 * g_pVideoData ;
2009-07-02 13:16:06 +03:00
2009-08-15 10:33:35 +03:00
namespace
{
2009-02-23 00:49:42 +02:00
static volatile bool fifoStateRun = false ;
2010-01-07 22:01:41 +02:00
static volatile bool EmuRunning = false ;
2008-12-08 07:30:24 +02:00
static u8 * videoBuffer ;
2009-02-21 00:04:52 +02:00
// STATE_TO_SAVE
2008-12-08 07:30:24 +02:00
static int size = 0 ;
2009-02-21 00:04:52 +02:00
} // namespace
2008-12-08 07:30:24 +02:00
void Fifo_DoState ( PointerWrap & p )
{
2009-07-03 13:00:09 +03:00
2008-12-08 07:30:24 +02:00
p . DoArray ( videoBuffer , FIFO_SIZE ) ;
p . Do ( size ) ;
2009-02-21 00:04:52 +02:00
int pos = ( int ) ( g_pVideoData - videoBuffer ) ; // get offset
2008-12-08 07:30:24 +02: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 13:00:09 +03:00
2008-12-08 07:30:24 +02:00
}
void Fifo_Init ( )
2011-02-13 06:10:40 +02:00
{
2008-12-08 07:30:24 +02:00
videoBuffer = ( u8 * ) AllocateMemoryPages ( FIFO_SIZE ) ;
2011-02-13 06:10:40 +02:00
size = 0 ;
2009-02-21 02:54:52 +02:00
fifoStateRun = false ;
2008-12-08 07:30:24 +02:00
}
void Fifo_Shutdown ( )
{
2010-01-03 18:04:40 +02:00
if ( fifoStateRun ) PanicAlert ( " Fifo shutting down while active " ) ;
2008-12-08 07:30:24 +02:00
FreeMemoryPages ( videoBuffer , FIFO_SIZE ) ;
}
u8 * FAKE_GetFifoStartPtr ( )
{
return videoBuffer ;
}
u8 * FAKE_GetFifoEndPtr ( )
{
return & videoBuffer [ size ] ;
}
2009-08-15 10:33:35 +03:00
void Fifo_SetRendering ( bool enabled )
{
g_bSkipCurrentFrame = ! enabled ;
2009-08-08 04:39:56 +03:00
}
2009-03-08 01:34:16 +02:00
// May be executed from any thread, even the graphics thread.
// Created to allow for self shutdown.
2011-02-02 20:21:20 +02:00
void Fifo_ExitLoop ( )
2009-08-09 14:03:58 +03:00
{
2010-01-17 00:37:38 +02:00
// This should break the wait loop in CPU thread
CommandProcessor : : fifo . bFF_GPReadEnable = false ;
2011-02-13 06:10:40 +02:00
SCPFifoStruct & _fifo = CommandProcessor : : fifo ;
while ( _fifo . isFifoProcesingData ) Common : : YieldCPU ( ) ;
2010-01-17 00:37:38 +02:00
// Terminate GPU thread loop
2009-03-08 01:34:16 +02:00
fifoStateRun = false ;
2011-01-30 23:20:33 +02:00
EmuRunning = true ;
2010-01-03 18:04:40 +02:00
}
2010-01-07 22:01:41 +02:00
void Fifo_RunLoop ( bool run )
2010-01-03 18:04:40 +02:00
{
2010-01-07 22:01:41 +02:00
EmuRunning = run ;
2009-03-08 01:34:16 +02:00
}
2011-02-10 06:47:02 +02:00
2009-06-15 07:30:02 +03:00
// Description: Fifo_EnterLoop() sends data through this function.
2009-07-18 01:57:02 +03:00
void Fifo_SendFifoData ( u8 * _uData , u32 len )
2009-06-15 07:30:02 +03:00
{
if ( size + len > = FIFO_SIZE )
{
int pos = ( int ) ( g_pVideoData - videoBuffer ) ;
2010-06-14 19:32:40 +03:00
size - = pos ;
if ( size + len > FIFO_SIZE )
2009-06-15 07:30:02 +03:00
{
PanicAlert ( " FIFO out of bounds (sz = %i, at %08x) " , size , pos ) ;
}
2010-06-14 19:32:40 +03:00
memmove ( & videoBuffer [ 0 ] , & videoBuffer [ pos ] , size ) ;
2009-12-16 19:05:30 +02:00
g_pVideoData = videoBuffer ;
2009-06-15 07:30:02 +03:00
}
// Copy new video instructions to videoBuffer for future use in rendering the new picture
memcpy ( videoBuffer + size , _uData , len ) ;
size + = len ;
}
2011-01-29 23:26:46 +02:00
void ResetVideoBuffer ( )
{
g_pVideoData = videoBuffer ;
size = 0 ;
}
2010-12-11 14:42:55 +02:00
2010-01-23 23:06:12 +02:00
// Description: Main FIFO update loop
// Purpose: Keep the Core HW updated about the CPU-GPU distance
2011-01-31 03:28:32 +02:00
void Fifo_EnterLoop ( )
2008-12-08 07:30:24 +02:00
{
2010-01-23 23:06:12 +02:00
fifoStateRun = true ;
2009-12-31 22:49:04 +02:00
SCPFifoStruct & _fifo = CommandProcessor : : fifo ;
2009-07-24 01:32:21 +03:00
s32 distToSend ;
2008-12-08 07:30:24 +02:00
2010-01-23 23:06:12 +02:00
while ( fifoStateRun )
2009-12-31 22:49:04 +02:00
{
2011-01-31 03:28:32 +02:00
g_video_backend - > PeekMessages ( ) ;
2009-07-18 01:57:02 +03:00
2010-08-05 00:02:32 +03:00
VideoFifo_CheckAsyncRequest ( ) ;
2009-08-15 10:33:35 +03:00
2010-06-24 16:28:54 +03:00
// check if we are able to run this buffer
2010-12-11 14:42:55 +02:00
CommandProcessor : : SetStatus ( ) ;
while ( ! CommandProcessor : : interruptWaiting & & _fifo . bFF_GPReadEnable & &
2010-12-27 05:46:17 +02:00
_fifo . CPReadWriteDistance & & ( ! AtBreakpoint ( ) | | CommandProcessor : : OnOverflow ) )
2010-01-23 14:50:56 +02:00
{
2011-02-13 04:21:11 +02:00
_fifo . isFifoProcesingData = true ;
2011-02-10 06:47:02 +02:00
CommandProcessor : : isPossibleWaitingSetDrawDone = _fifo . bFF_GPLinkEnable ;
2010-06-24 16:28:54 +03:00
2010-12-11 14:42:55 +02:00
if ( ! fifoStateRun ) break ;
2010-01-23 14:50:56 +02:00
2010-01-23 23:06:12 +02:00
// Create pointer to video data and send it to the VideoPlugin
u32 readPtr = _fifo . CPReadPointer ;
2011-01-31 03:28:32 +02:00
u8 * uData = Memory : : GetPointer ( readPtr ) ;
2009-07-20 04:10:00 +03:00
2010-12-11 14:42:55 +02:00
distToSend = 32 ;
if ( readPtr = = _fifo . CPEnd )
readPtr = _fifo . CPBase ;
else
readPtr + = 32 ;
2010-11-28 22:12:41 +02:00
2010-12-05 17:59:11 +02:00
_assert_msg_ ( COMMANDPROCESSOR , ( s32 ) _fifo . CPReadWriteDistance - distToSend > = 0 ,
2010-11-28 22:12:41 +02:00
" Negative fifo.CPReadWriteDistance = %i in FIFO Loop ! \n That can produce inestabilty in the game. Please report it. " , _fifo . CPReadWriteDistance - distToSend ) ;
2010-12-11 14:42:55 +02:00
// Execute new instructions found in uData
2011-01-29 22:04:16 +02:00
Fifo_SendFifoData ( uData , distToSend ) ;
OpcodeDecoder_Run ( g_bSkipCurrentFrame ) ;
2010-12-11 14:42:55 +02:00
Common : : AtomicStore ( _fifo . CPReadPointer , readPtr ) ;
Common : : AtomicAdd ( _fifo . CPReadWriteDistance , - distToSend ) ;
2011-02-13 17:08:37 +02:00
if ( ( FAKE_GetFifoEndPtr ( ) - g_pVideoData ) = = 0 )
Common : : AtomicStore ( _fifo . SafeCPReadPointer , _fifo . CPReadPointer ) ;
2010-12-11 14:42:55 +02:00
CommandProcessor : : SetStatus ( ) ;
2011-01-30 23:20:33 +02:00
// This call is pretty important in DualCore mode and must be called in the FIFO Loop.
// If we don't, s_swapRequested or s_efbAccessRequested won't be set to false
2010-01-23 23:06:12 +02:00
// leading the CPU thread to wait in Video_BeginField or Video_AccessEFB thus slowing things down.
2011-02-08 03:11:11 +02:00
VideoFifo_CheckAsyncRequest ( ) ;
2011-02-10 06:47:02 +02:00
CommandProcessor : : isPossibleWaitingSetDrawDone = false ;
2010-01-23 23:06:12 +02:00
}
2011-02-10 06:47:02 +02:00
2011-02-13 04:21:11 +02:00
_fifo . isFifoProcesingData = false ;
2011-02-08 03:11:11 +02:00
2010-01-23 23:06:12 +02:00
CommandProcessor : : SetFifoIdleFromVideoPlugin ( ) ;
2011-01-30 23:20:33 +02:00
2010-01-07 22:01:41 +02:00
if ( EmuRunning )
2010-01-03 20:58:50 +02:00
Common : : YieldCPU ( ) ;
else
2011-01-30 23:20:33 +02:00
{
// While the emu is paused, we still handle async request such as Savestates then sleep.
while ( ! EmuRunning )
{
2011-01-31 03:28:32 +02:00
g_video_backend - > PeekMessages ( ) ;
2011-02-08 12:37:47 +02:00
VideoFifo_CheckStateRequest ( ) ;
Common : : SleepCurrentThread ( 1 ) ;
2011-01-30 23:20:33 +02:00
}
}
2009-12-28 21:13:06 +02:00
}
2008-12-08 07:30:24 +02:00
}
2009-06-15 09:39:26 +03:00
2010-12-11 14:42:55 +02:00
bool AtBreakpoint ( )
{
SCPFifoStruct & _fifo = CommandProcessor : : fifo ;
return _fifo . bFF_BPEnable & & ( _fifo . CPReadPointer = = _fifo . CPBreakpoint ) ;
}