2014-04-08 23:15:46 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-04-08 23:15:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2014-04-05 05:23:51 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
#include <cstddef>
|
2015-06-27 16:56:17 +00:00
|
|
|
#include <type_traits>
|
2020-01-03 14:00:10 +00:00
|
|
|
#include <boost/serialization/access.hpp>
|
|
|
|
#include <boost/serialization/binary_object.hpp>
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/assert.h"
|
2014-05-31 22:08:00 +00:00
|
|
|
#include "common/bit_field.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
2020-04-26 22:02:25 +00:00
|
|
|
#include "core/core_timing.h"
|
2014-04-05 05:23:51 +00:00
|
|
|
|
2018-11-21 16:53:10 +00:00
|
|
|
namespace Memory {
|
|
|
|
class MemorySystem;
|
|
|
|
}
|
|
|
|
|
2014-05-17 20:50:33 +00:00
|
|
|
namespace GPU {
|
2014-04-05 05:23:51 +00:00
|
|
|
|
2020-04-26 22:02:25 +00:00
|
|
|
// Measured on hardware to be 2240568 timer cycles or 4481136 ARM11 cycles
|
|
|
|
constexpr u64 frame_ticks = 4481136ull;
|
|
|
|
|
|
|
|
// Refresh rate defined by ratio of ARM11 frequency to ARM11 ticks per frame
|
|
|
|
// (268,111,856) / (4,481,136) = 59.83122493939037Hz
|
|
|
|
constexpr double SCREEN_REFRESH_RATE = BASE_CLOCK_RATE_ARM11 / static_cast<double>(frame_ticks);
|
2017-02-19 22:34:47 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
// Returns index corresponding to the Regs member labeled by field_name
|
|
|
|
#define GPU_REG_INDEX(field_name) (offsetof(GPU::Regs, field_name) / sizeof(u32))
|
|
|
|
|
2021-05-11 03:10:29 +00:00
|
|
|
// Returns index corresponding to the Regs::FramebufferConfig labeled by field_name
|
|
|
|
// screen_id is a subscript for Regs::framebuffer_config
|
|
|
|
#define GPU_FRAMEBUFFER_REG_INDEX(screen_id, field_name) \
|
|
|
|
((offsetof(GPU::Regs, framebuffer_config) + \
|
|
|
|
sizeof(GPU::Regs::FramebufferConfig) * (screen_id) + \
|
|
|
|
offsetof(GPU::Regs::FramebufferConfig, field_name)) / \
|
|
|
|
sizeof(u32))
|
|
|
|
|
2014-07-16 09:24:09 +00:00
|
|
|
// MMIO region 0x1EFxxxxx
|
|
|
|
struct Regs {
|
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
// helper macro to make sure the defined structures are of the expected size.
|
2016-09-18 00:38:01 +00:00
|
|
|
#define ASSERT_MEMBER_SIZE(name, size_in_bytes) \
|
|
|
|
static_assert(sizeof(name) == size_in_bytes, \
|
2014-08-23 11:42:34 +00:00
|
|
|
"Structure size and register block length don't match")
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2015-02-27 03:11:39 +00:00
|
|
|
// Components are laid out in reverse byte order, most significant bits first.
|
2014-08-28 18:17:09 +00:00
|
|
|
enum class PixelFormat : u32 {
|
2016-09-18 00:38:01 +00:00
|
|
|
RGBA8 = 0,
|
|
|
|
RGB8 = 1,
|
2014-07-11 17:10:08 +00:00
|
|
|
RGB565 = 2,
|
|
|
|
RGB5A1 = 3,
|
2016-09-18 00:38:01 +00:00
|
|
|
RGBA4 = 4,
|
2014-07-11 17:10:08 +00:00
|
|
|
};
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2015-02-05 14:15:42 +00: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;
|
2022-11-04 22:32:57 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2015-02-05 14:15:42 +00:00
|
|
|
}
|
2017-08-19 17:04:40 +00:00
|
|
|
|
2022-11-04 22:32:57 +00:00
|
|
|
return 0;
|
2015-02-05 14:15:42 +00:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x4);
|
2014-07-16 09:27:58 +00:00
|
|
|
|
2016-04-16 22:57:57 +00:00
|
|
|
struct MemoryFillConfig {
|
2014-08-03 14:00:52 +00:00
|
|
|
u32 address_start;
|
2015-01-01 18:58:18 +00:00
|
|
|
u32 address_end;
|
|
|
|
|
|
|
|
union {
|
|
|
|
u32 value_32bit;
|
|
|
|
|
|
|
|
BitField<0, 16, u32> value_16bit;
|
|
|
|
|
|
|
|
// TODO: Verify component order
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 8, u32> value_24bit_r;
|
|
|
|
BitField<8, 8, u32> value_24bit_g;
|
2015-01-01 18:58:18 +00:00
|
|
|
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.
|
2015-03-09 22:15:13 +00:00
|
|
|
BitField<1, 1, u32> finished;
|
2015-01-01 18:58:18 +00:00
|
|
|
|
2015-03-09 14:01:41 +00:00
|
|
|
// If both of these bits are unset, then it will fill the memory with a 16 bit value
|
|
|
|
// 1: fill with 24-bit wide values
|
2015-01-01 18:58:18 +00:00
|
|
|
BitField<8, 1, u32> fill_24bit;
|
2015-03-09 14:01:41 +00:00
|
|
|
// 1: fill with 32-bit wide values
|
2015-01-01 18:58:18 +00:00
|
|
|
BitField<9, 1, u32> fill_32bit;
|
|
|
|
};
|
2014-07-16 09:27:58 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
inline u32 GetStartAddress() const {
|
|
|
|
return DecodeAddressRegister(address_start);
|
|
|
|
}
|
2014-06-04 16:30:23 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
inline u32 GetEndAddress() const {
|
|
|
|
return DecodeAddressRegister(address_end);
|
|
|
|
}
|
|
|
|
} memory_fill_config[2];
|
|
|
|
ASSERT_MEMBER_SIZE(memory_fill_config[0], 0x10);
|
2014-05-17 20:07:06 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x10b);
|
2014-07-11 17:10:08 +00:00
|
|
|
|
2014-08-28 18:17:09 +00:00
|
|
|
struct FramebufferConfig {
|
2014-08-03 14:00:52 +00:00
|
|
|
union {
|
|
|
|
u32 size;
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 16, u32> width;
|
2014-08-03 14:00:52 +00:00
|
|
|
BitField<16, 16, u32> height;
|
|
|
|
};
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x2);
|
2014-07-11 17:10:08 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
u32 address_left1;
|
|
|
|
u32 address_left2;
|
2014-07-11 17:10:08 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
union {
|
|
|
|
u32 format;
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 3, PixelFormat> color_format;
|
2014-08-03 14:00:52 +00:00
|
|
|
};
|
2014-07-11 17:10:08 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x1);
|
2014-07-11 17:10:08 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
union {
|
|
|
|
u32 active_fb;
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
// 0: Use parameters ending with "1"
|
|
|
|
// 1: Use parameters ending with "2"
|
|
|
|
BitField<0, 1, u32> second_fb_active;
|
|
|
|
};
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x5);
|
2014-07-16 09:27:58 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
// Distance between two pixel rows, in bytes
|
|
|
|
u32 stride;
|
2014-07-16 09:24:09 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
u32 address_right1;
|
|
|
|
u32 address_right2;
|
2014-07-11 17:10:08 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x30);
|
|
|
|
} framebuffer_config[2];
|
|
|
|
ASSERT_MEMBER_SIZE(framebuffer_config[0], 0x100);
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x169);
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2016-04-16 22:57:57 +00:00
|
|
|
struct DisplayTransferConfig {
|
2014-08-03 14:00:52 +00:00
|
|
|
u32 input_address;
|
|
|
|
u32 output_address;
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
inline u32 GetPhysicalInputAddress() const {
|
|
|
|
return DecodeAddressRegister(input_address);
|
|
|
|
}
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
inline u32 GetPhysicalOutputAddress() const {
|
|
|
|
return DecodeAddressRegister(output_address);
|
|
|
|
}
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
union {
|
|
|
|
u32 output_size;
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 16, u32> output_width;
|
2014-08-03 14:00:52 +00:00
|
|
|
BitField<16, 16, u32> output_height;
|
|
|
|
};
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
union {
|
|
|
|
u32 input_size;
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 16, u32> input_width;
|
2014-08-03 14:00:52 +00:00
|
|
|
BitField<16, 16, u32> input_height;
|
|
|
|
};
|
2014-07-22 11:21:57 +00:00
|
|
|
|
2015-03-12 18:11:57 +00:00
|
|
|
enum ScalingMode : u32 {
|
2016-09-18 00:38:01 +00:00
|
|
|
NoScale = 0, // Doesn't scale the image
|
|
|
|
ScaleX = 1, // Downscales the image in half in the X axis and applies a box filter
|
|
|
|
ScaleXY =
|
|
|
|
2, // Downscales the image in half in both the X and Y axes and applies a box filter
|
2015-03-12 18:11:57 +00:00
|
|
|
};
|
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
union {
|
|
|
|
u32 flags;
|
2014-05-31 22:08:00 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 1, u32> flip_vertically; // flips input data vertically
|
|
|
|
BitField<1, 1, u32> input_linear; // Converts from linear to tiled format
|
|
|
|
BitField<2, 1, u32> crop_input_lines;
|
|
|
|
BitField<3, 1, u32> is_texture_copy; // Copies the data without performing any
|
|
|
|
// processing and respecting texture copy fields
|
|
|
|
BitField<5, 1, u32> dont_swizzle;
|
|
|
|
BitField<8, 3, PixelFormat> input_format;
|
2014-08-28 18:17:09 +00:00
|
|
|
BitField<12, 3, PixelFormat> output_format;
|
2015-07-23 19:25:59 +00:00
|
|
|
/// Uses some kind of 32x32 block swizzling mode, instead of the usual 8x8 one.
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<16, 1, u32> block_32; // TODO(yuriks): unimplemented
|
2015-03-12 18:11:57 +00:00
|
|
|
BitField<24, 2, ScalingMode> scaling; // Determines the scaling mode of the transfer
|
2014-08-03 14:00:52 +00:00
|
|
|
};
|
2014-07-22 11:21:57 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x1);
|
2014-07-22 11:21:57 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
// it seems that writing to this field triggers the display transfer
|
|
|
|
u32 trigger;
|
2015-07-23 19:25:59 +00:00
|
|
|
|
|
|
|
INSERT_PADDING_WORDS(0x1);
|
|
|
|
|
|
|
|
struct {
|
2017-06-29 10:09:23 +00:00
|
|
|
u32 size; // The lower 4 bits are ignored
|
2015-07-23 19:25:59 +00:00
|
|
|
|
|
|
|
union {
|
|
|
|
u32 input_size;
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 16, u32> input_width;
|
2015-07-23 19:25:59 +00:00
|
|
|
BitField<16, 16, u32> input_gap;
|
|
|
|
};
|
|
|
|
|
|
|
|
union {
|
|
|
|
u32 output_size;
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
BitField<0, 16, u32> output_width;
|
2015-07-23 19:25:59 +00:00
|
|
|
BitField<16, 16, u32> output_gap;
|
|
|
|
};
|
|
|
|
} texture_copy;
|
2014-08-03 14:00:52 +00:00
|
|
|
} display_transfer_config;
|
2015-07-23 19:25:59 +00:00
|
|
|
ASSERT_MEMBER_SIZE(display_transfer_config, 0x2c);
|
2014-07-22 11:21:57 +00:00
|
|
|
|
2015-07-23 19:25:59 +00:00
|
|
|
INSERT_PADDING_WORDS(0x32D);
|
2014-07-22 11:21:57 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
struct {
|
2014-12-03 06:05:16 +00:00
|
|
|
// command list size (in bytes)
|
2014-08-03 14:00:52 +00: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 09:24:09 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
inline u32 GetPhysicalAddress() const {
|
|
|
|
return DecodeAddressRegister(address);
|
|
|
|
}
|
|
|
|
} command_processor_config;
|
|
|
|
ASSERT_MEMBER_SIZE(command_processor_config, 0x14);
|
2014-04-27 16:39:57 +00:00
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
INSERT_PADDING_WORDS(0x9c3);
|
|
|
|
|
2018-09-06 20:03:28 +00:00
|
|
|
static constexpr std::size_t NumIds() {
|
2014-08-03 14:00:52 +00:00
|
|
|
return sizeof(Regs) / sizeof(u32);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
const u32& operator[](int index) const {
|
2016-03-17 04:27:12 +00:00
|
|
|
const u32* content = reinterpret_cast<const u32*>(this);
|
2014-08-03 14:00:52 +00:00
|
|
|
return content[index];
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
u32& operator[](int index) {
|
2016-03-17 04:27:12 +00:00
|
|
|
u32* content = reinterpret_cast<u32*>(this);
|
2014-08-03 14:00:52 +00:00
|
|
|
return content[index];
|
|
|
|
}
|
|
|
|
|
2014-10-13 05:40:26 +00:00
|
|
|
#undef ASSERT_MEMBER_SIZE
|
|
|
|
|
2014-08-03 14:00:52 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-08-06 16:45:06 +00:00
|
|
|
|
2020-01-03 14:00:10 +00:00
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int) {
|
2020-01-03 17:19:59 +00:00
|
|
|
ar& boost::serialization::make_binary_object(this, sizeof(Regs));
|
2020-01-03 14:00:10 +00:00
|
|
|
}
|
|
|
|
friend class boost::serialization::access;
|
2014-08-03 14:00:52 +00:00
|
|
|
};
|
|
|
|
static_assert(std::is_standard_layout<Regs>::value, "Structure does not use standard layout");
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
#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);
|
2014-08-03 14:00:52 +00:00
|
|
|
ASSERT_REG_POSITION(command_processor_config, 0x00638);
|
|
|
|
|
|
|
|
#undef ASSERT_REG_POSITION
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
// The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value
|
|
|
|
// anyway.
|
2014-08-03 14:00:52 +00:00
|
|
|
static_assert(sizeof(Regs) == 0x1000 * sizeof(u32), "Invalid total size of register set");
|
|
|
|
|
|
|
|
extern Regs g_regs;
|
2014-04-27 16:39:57 +00:00
|
|
|
|
2014-04-05 05:23:51 +00:00
|
|
|
template <typename T>
|
2016-09-18 00:38:01 +00:00
|
|
|
void Read(T& var, const u32 addr);
|
2014-04-05 05:23:51 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2014-07-22 11:49:25 +00:00
|
|
|
void Write(u32 addr, const T data);
|
2014-04-05 05:23:51 +00:00
|
|
|
|
|
|
|
/// Initialize hardware
|
2018-11-21 16:53:10 +00:00
|
|
|
void Init(Memory::MemorySystem& memory);
|
2014-04-05 05:23:51 +00:00
|
|
|
|
|
|
|
/// Shutdown hardware
|
|
|
|
void Shutdown();
|
|
|
|
|
2018-03-09 17:54:43 +00:00
|
|
|
} // namespace GPU
|