2014-04-08 19:15:46 -04:00
// Copyright 2014 Citra Emulator Project
2014-12-17 00:38:14 -05:00
// Licensed under GPLv2 or any later version
2014-04-08 19:15:46 -04:00
// Refer to the license.txt file included.
2014-04-05 01:23:51 -04:00
# pragma once
2014-08-03 10:00:52 -04:00
# include <cstddef>
2014-04-08 20:15:08 -04:00
# include "common/common_types.h"
2014-05-31 18:08:00 -04:00
# include "common/bit_field.h"
2014-04-05 01:23:51 -04:00
2014-05-17 16:50:33 -04:00
namespace GPU {
2014-04-05 01:23:51 -04:00
2014-08-03 10:00:52 -04:00
// Returns index corresponding to the Regs member labeled by field_name
// TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
// when used with array elements (e.g. GPU_REG_INDEX(memory_fill_config[0])).
// For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
// Hopefully, this will be fixed sometime in the future.
// For lack of better alternatives, we currently hardcode the offsets when constant
// expressions are needed via GPU_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
// will then make sure the offsets indeed match the automatically calculated ones).
# define GPU_REG_INDEX(field_name) (offsetof(GPU::Regs, field_name) / sizeof(u32))
# if defined(_MSC_VER)
# define GPU_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
# else
// NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
// really is this annoying. This macro just forwards its first argument to GPU_REG_INDEX
// and then performs a (no-op) cast to size_t iff the second argument matches the expected
// field offset. Otherwise, the compiler will fail to compile this code.
# define GPU_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
( ( typename std : : enable_if < backup_workaround_index = = GPU_REG_INDEX ( field_name ) , size_t > : : type ) GPU_REG_INDEX ( field_name ) )
# endif
2014-07-16 05:24:09 -04:00
// MMIO region 0x1EFxxxxx
struct Regs {
2014-08-03 10:00:52 -04:00
// helper macro to make sure the defined structures are of the expected size.
# if defined(_MSC_VER)
// TODO: MSVC does not support using sizeof() on non-static data members even though this
// is technically allowed since C++11. This macro should be enabled once MSVC adds
// support for that.
# define ASSERT_MEMBER_SIZE(name, size_in_bytes)
# else
# define ASSERT_MEMBER_SIZE(name, size_in_bytes) \
static_assert ( sizeof ( name ) = = size_in_bytes , \
2014-08-23 07:42:34 -04:00
" Structure size and register block length don't match " )
2014-08-03 10:00:52 -04:00
# endif
2014-07-16 05:24:09 -04:00
2015-02-26 22:11:39 -05:00
// Components are laid out in reverse byte order, most significant bits first.
2014-08-28 14:17:09 -04:00
enum class PixelFormat : u32 {
2014-07-11 13:10:08 -04:00
RGBA8 = 0 ,
RGB8 = 1 ,
RGB565 = 2 ,
RGB5A1 = 3 ,
RGBA4 = 4 ,
} ;
2014-07-16 05:24:09 -04:00
2015-02-05 09:15:42 -05:00
/**
* Returns the number of bytes per pixel .
*/
static int BytesPerPixel ( PixelFormat format ) {
switch ( format ) {
case PixelFormat : : RGBA8 :
return 4 ;
case PixelFormat : : RGB8 :
return 3 ;
case PixelFormat : : RGB565 :
case PixelFormat : : RGB5A1 :
case PixelFormat : : RGBA4 :
return 2 ;
default :
UNIMPLEMENTED ( ) ;
}
}
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x4 ) ;
2014-07-16 05:27:58 -04:00
2014-08-03 10:00:52 -04:00
struct {
u32 address_start ;
2015-01-01 13:58:18 -05:00
u32 address_end ;
union {
u32 value_32bit ;
BitField < 0 , 16 , u32 > value_16bit ;
// TODO: Verify component order
BitField < 0 , 8 , u32 > value_24bit_r ;
BitField < 8 , 8 , u32 > value_24bit_g ;
BitField < 16 , 8 , u32 > value_24bit_b ;
} ;
union {
u32 control ;
// Setting this field to 1 triggers the memory fill.
// This field also acts as a status flag, and gets reset to 0 upon completion.
BitField < 0 , 1 , u32 > trigger ;
// Set to 1 upon completion.
BitField < 0 , 1 , u32 > finished ;
// 0: fill with 16- or 32-bit wide values; 1: fill with 24-bit wide values
BitField < 8 , 1 , u32 > fill_24bit ;
// 0: fill with 16-bit wide values; 1: fill with 32-bit wide values
BitField < 9 , 1 , u32 > fill_32bit ;
} ;
2014-07-16 05:27:58 -04:00
2014-08-03 10:00:52 -04:00
inline u32 GetStartAddress ( ) const {
return DecodeAddressRegister ( address_start ) ;
}
2014-06-04 12:30:23 -04:00
2014-08-03 10:00:52 -04:00
inline u32 GetEndAddress ( ) const {
return DecodeAddressRegister ( address_end ) ;
}
} memory_fill_config [ 2 ] ;
ASSERT_MEMBER_SIZE ( memory_fill_config [ 0 ] , 0x10 ) ;
2014-05-17 16:07:06 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x10b ) ;
2014-07-11 13:10:08 -04:00
2014-08-28 14:17:09 -04:00
struct FramebufferConfig {
2014-08-03 10:00:52 -04:00
union {
u32 size ;
2014-07-16 05:24:09 -04:00
2014-08-03 10:00:52 -04:00
BitField < 0 , 16 , u32 > width ;
BitField < 16 , 16 , u32 > height ;
} ;
2014-07-16 05:24:09 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x2 ) ;
2014-07-11 13:10:08 -04:00
2014-08-03 10:00:52 -04:00
u32 address_left1 ;
u32 address_left2 ;
2014-07-11 13:10:08 -04:00
2014-08-03 10:00:52 -04:00
union {
u32 format ;
2014-07-16 05:24:09 -04:00
2014-08-28 14:17:09 -04:00
BitField < 0 , 3 , PixelFormat > color_format ;
2014-08-03 10:00:52 -04:00
} ;
2014-07-11 13:10:08 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x1 ) ;
2014-07-11 13:10:08 -04:00
2014-08-03 10:00:52 -04:00
union {
u32 active_fb ;
2014-07-16 05:24:09 -04:00
2014-08-03 10:00:52 -04:00
// 0: Use parameters ending with "1"
// 1: Use parameters ending with "2"
BitField < 0 , 1 , u32 > second_fb_active ;
} ;
2014-07-16 05:24:09 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x5 ) ;
2014-07-16 05:27:58 -04:00
2014-08-03 10:00:52 -04:00
// Distance between two pixel rows, in bytes
u32 stride ;
2014-07-16 05:24:09 -04:00
2014-08-03 10:00:52 -04:00
u32 address_right1 ;
u32 address_right2 ;
2014-07-11 13:10:08 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x30 ) ;
} framebuffer_config [ 2 ] ;
ASSERT_MEMBER_SIZE ( framebuffer_config [ 0 ] , 0x100 ) ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x169 ) ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
struct {
u32 input_address ;
u32 output_address ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
inline u32 GetPhysicalInputAddress ( ) const {
return DecodeAddressRegister ( input_address ) ;
}
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
inline u32 GetPhysicalOutputAddress ( ) const {
return DecodeAddressRegister ( output_address ) ;
}
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
union {
u32 output_size ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
BitField < 0 , 16 , u32 > output_width ;
BitField < 16 , 16 , u32 > output_height ;
} ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
union {
u32 input_size ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
BitField < 0 , 16 , u32 > input_width ;
BitField < 16 , 16 , u32 > input_height ;
} ;
2014-07-22 07:21:57 -04:00
2014-08-03 10:00:52 -04:00
union {
u32 flags ;
2014-05-31 18:08:00 -04:00
2014-08-03 10:00:52 -04:00
BitField < 0 , 1 , u32 > flip_data ; // flips input data horizontally (TODO) if true
2015-02-23 18:24:35 -05:00
BitField < 1 , 1 , u32 > output_tiled ; // Converts from linear to tiled format
BitField < 3 , 1 , u32 > raw_copy ; // Copies the data without performing any processing
2014-08-28 14:17:09 -04:00
BitField < 8 , 3 , PixelFormat > input_format ;
BitField < 12 , 3 , PixelFormat > output_format ;
2014-12-28 17:28:58 -05:00
BitField < 24 , 1 , u32 > scale_horizontally ;
2015-02-23 18:24:35 -05:00
BitField < 25 , 1 , u32 > scale_vertically ;
2014-08-03 10:00:52 -04:00
} ;
2014-07-22 07:21:57 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x1 ) ;
2014-07-22 07:21:57 -04:00
2014-08-03 10:00:52 -04:00
// it seems that writing to this field triggers the display transfer
u32 trigger ;
} display_transfer_config ;
ASSERT_MEMBER_SIZE ( display_transfer_config , 0x1c ) ;
2014-07-22 07:21:57 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x331 ) ;
2014-07-22 07:21:57 -04:00
2014-08-03 10:00:52 -04:00
struct {
2014-12-03 01:05:16 -05:00
// command list size (in bytes)
2014-08-03 10:00:52 -04:00
u32 size ;
INSERT_PADDING_WORDS ( 0x1 ) ;
// command list address
u32 address ;
INSERT_PADDING_WORDS ( 0x1 ) ;
// it seems that writing to this field triggers command list processing
u32 trigger ;
2014-07-16 05:24:09 -04:00
2014-08-03 10:00:52 -04:00
inline u32 GetPhysicalAddress ( ) const {
return DecodeAddressRegister ( address ) ;
}
} command_processor_config ;
ASSERT_MEMBER_SIZE ( command_processor_config , 0x14 ) ;
2014-04-27 12:39:57 -04:00
2014-08-03 10:00:52 -04:00
INSERT_PADDING_WORDS ( 0x9c3 ) ;
2014-09-28 11:20:06 -04:00
static inline size_t NumIds ( ) {
2014-08-03 10:00:52 -04:00
return sizeof ( Regs ) / sizeof ( u32 ) ;
}
u32 & operator [ ] ( int index ) const {
u32 * content = ( u32 * ) this ;
return content [ index ] ;
}
u32 & operator [ ] ( int index ) {
u32 * content = ( u32 * ) this ;
return content [ index ] ;
}
private :
/*
* Most physical addresses which GPU registers refer to are 8 - byte aligned .
* This function should be used to get the address from a raw register value .
*/
static inline u32 DecodeAddressRegister ( u32 register_value ) {
return register_value * 8 ;
}
} ;
static_assert ( std : : is_standard_layout < Regs > : : value , " Structure does not use standard layout " ) ;
// TODO: MSVC does not support using offsetof() on non-static data members even though this
// is technically allowed since C++11. This macro should be enabled once MSVC adds
// support for that.
# ifndef _MSC_VER
# define ASSERT_REG_POSITION(field_name, position) \
static_assert ( offsetof ( Regs , field_name ) = = position * 4 , \
" Field " # field_name " has invalid position " )
ASSERT_REG_POSITION ( memory_fill_config [ 0 ] , 0x00004 ) ;
ASSERT_REG_POSITION ( memory_fill_config [ 1 ] , 0x00008 ) ;
ASSERT_REG_POSITION ( framebuffer_config [ 0 ] , 0x00117 ) ;
ASSERT_REG_POSITION ( framebuffer_config [ 1 ] , 0x00157 ) ;
ASSERT_REG_POSITION ( display_transfer_config , 0x00300 ) ;
ASSERT_REG_POSITION ( command_processor_config , 0x00638 ) ;
# undef ASSERT_REG_POSITION
# endif // !defined(_MSC_VER)
// The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
static_assert ( sizeof ( Regs ) = = 0x1000 * sizeof ( u32 ) , " Invalid total size of register set " ) ;
extern Regs g_regs ;
2014-12-26 21:40:17 -05:00
extern bool g_skip_frame ;
2014-04-27 12:39:57 -04:00
2014-04-05 01:23:51 -04:00
template < typename T >
2014-07-22 07:49:25 -04:00
void Read ( T & var , const u32 addr ) ;
2014-04-05 01:23:51 -04:00
template < typename T >
2014-07-22 07:49:25 -04:00
void Write ( u32 addr , const T data ) ;
2014-04-05 01:23:51 -04:00
/// Initialize hardware
void Init ( ) ;
/// Shutdown hardware
void Shutdown ( ) ;
} // namespace