summaryrefslogtreecommitdiff
path: root/src/video_core/regs.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/regs.h')
-rw-r--r--src/video_core/regs.h164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/video_core/regs.h b/src/video_core/regs.h
new file mode 100644
index 000000000..f25edde27
--- /dev/null
+++ b/src/video_core/regs.h
@@ -0,0 +1,164 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <string>
10#ifndef _MSC_VER
11#include <type_traits> // for std::enable_if
12#endif
13
14#include "common/common_funcs.h"
15#include "common/common_types.h"
16#include "video_core/regs_framebuffer.h"
17#include "video_core/regs_lighting.h"
18#include "video_core/regs_pipeline.h"
19#include "video_core/regs_rasterizer.h"
20#include "video_core/regs_shader.h"
21#include "video_core/regs_texturing.h"
22
23namespace Pica {
24
25// Returns index corresponding to the Regs member labeled by field_name
26// TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
27// when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
28// For details cf.
29// https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
30// Hopefully, this will be fixed sometime in the future.
31// For lack of better alternatives, we currently hardcode the offsets when constant
32// expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
33// will then make sure the offsets indeed match the automatically calculated ones).
34#define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
35#if defined(_MSC_VER)
36#define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
37#else
38// NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
39// really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
40// and then performs a (no-op) cast to size_t iff the second argument matches the expected
41// field offset. Otherwise, the compiler will fail to compile this code.
42#define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
43 ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), \
44 size_t>::type)PICA_REG_INDEX(field_name))
45#endif // _MSC_VER
46
47struct Regs {
48 INSERT_PADDING_WORDS(0x10);
49 u32 trigger_irq;
50 INSERT_PADDING_WORDS(0x2f);
51 RasterizerRegs rasterizer;
52 TexturingRegs texturing;
53 FramebufferRegs framebuffer;
54 LightingRegs lighting;
55 PipelineRegs pipeline;
56 ShaderRegs gs;
57 ShaderRegs vs;
58 INSERT_PADDING_WORDS(0x20);
59
60 // Map register indices to names readable by humans
61 // Used for debugging purposes, so performance is not an issue here
62 static std::string GetCommandName(int index);
63
64 static constexpr size_t NumIds() {
65 return sizeof(Regs) / sizeof(u32);
66 }
67
68 const u32& operator[](int index) const {
69 const u32* content = reinterpret_cast<const u32*>(this);
70 return content[index];
71 }
72
73 u32& operator[](int index) {
74 u32* content = reinterpret_cast<u32*>(this);
75 return content[index];
76 }
77
78private:
79 /*
80 * Most physical addresses which Pica registers refer to are 8-byte aligned.
81 * This function should be used to get the address from a raw register value.
82 */
83 static inline u32 DecodeAddressRegister(u32 register_value) {
84 return register_value * 8;
85 }
86};
87
88// TODO: MSVC does not support using offsetof() on non-static data members even though this
89// is technically allowed since C++11. This macro should be enabled once MSVC adds
90// support for that.
91#ifndef _MSC_VER
92#define ASSERT_REG_POSITION(field_name, position) \
93 static_assert(offsetof(Regs, field_name) == position * 4, \
94 "Field " #field_name " has invalid position")
95
96ASSERT_REG_POSITION(trigger_irq, 0x10);
97
98ASSERT_REG_POSITION(rasterizer, 0x40);
99ASSERT_REG_POSITION(rasterizer.cull_mode, 0x40);
100ASSERT_REG_POSITION(rasterizer.viewport_size_x, 0x41);
101ASSERT_REG_POSITION(rasterizer.viewport_size_y, 0x43);
102ASSERT_REG_POSITION(rasterizer.viewport_depth_range, 0x4d);
103ASSERT_REG_POSITION(rasterizer.viewport_depth_near_plane, 0x4e);
104ASSERT_REG_POSITION(rasterizer.vs_output_attributes[0], 0x50);
105ASSERT_REG_POSITION(rasterizer.vs_output_attributes[1], 0x51);
106ASSERT_REG_POSITION(rasterizer.scissor_test, 0x65);
107ASSERT_REG_POSITION(rasterizer.viewport_corner, 0x68);
108ASSERT_REG_POSITION(rasterizer.depthmap_enable, 0x6D);
109
110ASSERT_REG_POSITION(texturing, 0x80);
111ASSERT_REG_POSITION(texturing.texture0_enable, 0x80);
112ASSERT_REG_POSITION(texturing.texture0, 0x81);
113ASSERT_REG_POSITION(texturing.texture0_format, 0x8e);
114ASSERT_REG_POSITION(texturing.fragment_lighting_enable, 0x8f);
115ASSERT_REG_POSITION(texturing.texture1, 0x91);
116ASSERT_REG_POSITION(texturing.texture1_format, 0x96);
117ASSERT_REG_POSITION(texturing.texture2, 0x99);
118ASSERT_REG_POSITION(texturing.texture2_format, 0x9e);
119ASSERT_REG_POSITION(texturing.tev_stage0, 0xc0);
120ASSERT_REG_POSITION(texturing.tev_stage1, 0xc8);
121ASSERT_REG_POSITION(texturing.tev_stage2, 0xd0);
122ASSERT_REG_POSITION(texturing.tev_stage3, 0xd8);
123ASSERT_REG_POSITION(texturing.tev_combiner_buffer_input, 0xe0);
124ASSERT_REG_POSITION(texturing.fog_mode, 0xe0);
125ASSERT_REG_POSITION(texturing.fog_color, 0xe1);
126ASSERT_REG_POSITION(texturing.fog_lut_offset, 0xe6);
127ASSERT_REG_POSITION(texturing.fog_lut_data, 0xe8);
128ASSERT_REG_POSITION(texturing.tev_stage4, 0xf0);
129ASSERT_REG_POSITION(texturing.tev_stage5, 0xf8);
130ASSERT_REG_POSITION(texturing.tev_combiner_buffer_color, 0xfd);
131
132ASSERT_REG_POSITION(framebuffer, 0x100);
133ASSERT_REG_POSITION(framebuffer.output_merger, 0x100);
134ASSERT_REG_POSITION(framebuffer.framebuffer, 0x110);
135
136ASSERT_REG_POSITION(lighting, 0x140);
137
138ASSERT_REG_POSITION(pipeline, 0x200);
139ASSERT_REG_POSITION(pipeline.vertex_attributes, 0x200);
140ASSERT_REG_POSITION(pipeline.index_array, 0x227);
141ASSERT_REG_POSITION(pipeline.num_vertices, 0x228);
142ASSERT_REG_POSITION(pipeline.vertex_offset, 0x22a);
143ASSERT_REG_POSITION(pipeline.trigger_draw, 0x22e);
144ASSERT_REG_POSITION(pipeline.trigger_draw_indexed, 0x22f);
145ASSERT_REG_POSITION(pipeline.vs_default_attributes_setup, 0x232);
146ASSERT_REG_POSITION(pipeline.command_buffer, 0x238);
147ASSERT_REG_POSITION(pipeline.gpu_mode, 0x245);
148ASSERT_REG_POSITION(pipeline.triangle_topology, 0x25e);
149ASSERT_REG_POSITION(pipeline.restart_primitive, 0x25f);
150
151ASSERT_REG_POSITION(gs, 0x280);
152ASSERT_REG_POSITION(vs, 0x2b0);
153
154#undef ASSERT_REG_POSITION
155#endif // !defined(_MSC_VER)
156
157// The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value
158// anyway.
159static_assert(sizeof(Regs) <= 0x300 * sizeof(u32),
160 "Register set structure larger than it should be");
161static_assert(sizeof(Regs) >= 0x300 * sizeof(u32),
162 "Register set structure smaller than it should be");
163
164} // namespace Pica