diff options
Diffstat (limited to 'src/core/hle/service/gsp.cpp')
| -rw-r--r-- | src/core/hle/service/gsp.cpp | 143 |
1 files changed, 101 insertions, 42 deletions
diff --git a/src/core/hle/service/gsp.cpp b/src/core/hle/service/gsp.cpp index 1fdbdf342..b20203e27 100644 --- a/src/core/hle/service/gsp.cpp +++ b/src/core/hle/service/gsp.cpp | |||
| @@ -47,11 +47,6 @@ Handle g_shared_memory = 0; | |||
| 47 | 47 | ||
| 48 | u32 g_thread_id = 0; | 48 | u32 g_thread_id = 0; |
| 49 | 49 | ||
| 50 | enum { | ||
| 51 | REG_FRAMEBUFFER_1 = 0x00400468, | ||
| 52 | REG_FRAMEBUFFER_2 = 0x00400494, | ||
| 53 | }; | ||
| 54 | |||
| 55 | /// Gets a pointer to the start (header) of a command buffer in GSP shared memory | 50 | /// Gets a pointer to the start (header) of a command buffer in GSP shared memory |
| 56 | static inline u8* GX_GetCmdBufferPointer(u32 thread_id, u32 offset=0) { | 51 | static inline u8* GX_GetCmdBufferPointer(u32 thread_id, u32 offset=0) { |
| 57 | return Kernel::GetSharedMemoryPointer(g_shared_memory, 0x800 + (thread_id * 0x200) + offset); | 52 | return Kernel::GetSharedMemoryPointer(g_shared_memory, 0x800 + (thread_id * 0x200) + offset); |
| @@ -67,38 +62,62 @@ void GX_FinishCommand(u32 thread_id) { | |||
| 67 | // TODO: Increment header->index? | 62 | // TODO: Increment header->index? |
| 68 | } | 63 | } |
| 69 | 64 | ||
| 65 | /// Write a GSP GPU hardware register | ||
| 66 | void WriteHWRegs(Service::Interface* self) { | ||
| 67 | u32* cmd_buff = Service::GetCommandBuffer(); | ||
| 68 | u32 reg_addr = cmd_buff[1]; | ||
| 69 | u32 size = cmd_buff[2]; | ||
| 70 | |||
| 71 | // TODO: Return proper error codes | ||
| 72 | if (reg_addr + size >= 0x420000) { | ||
| 73 | ERROR_LOG(GPU, "Write address out of range! (address=0x%08x, size=0x%08x)", reg_addr, size); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | // size should be word-aligned | ||
| 78 | if ((size % 4) != 0) { | ||
| 79 | ERROR_LOG(GPU, "Invalid size 0x%08x", size); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | u32* src = (u32*)Memory::GetPointer(cmd_buff[0x4]); | ||
| 84 | |||
| 85 | while (size > 0) { | ||
| 86 | GPU::Write<u32>(reg_addr + 0x1EB00000, *src); | ||
| 87 | |||
| 88 | size -= 4; | ||
| 89 | ++src; | ||
| 90 | reg_addr += 4; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 70 | /// Read a GSP GPU hardware register | 94 | /// Read a GSP GPU hardware register |
| 71 | void ReadHWRegs(Service::Interface* self) { | 95 | void ReadHWRegs(Service::Interface* self) { |
| 72 | static const u32 framebuffer_1[] = {GPU::PADDR_VRAM_TOP_LEFT_FRAME1, GPU::PADDR_VRAM_TOP_RIGHT_FRAME1}; | ||
| 73 | static const u32 framebuffer_2[] = {GPU::PADDR_VRAM_TOP_LEFT_FRAME2, GPU::PADDR_VRAM_TOP_RIGHT_FRAME2}; | ||
| 74 | |||
| 75 | u32* cmd_buff = Service::GetCommandBuffer(); | 96 | u32* cmd_buff = Service::GetCommandBuffer(); |
| 76 | u32 reg_addr = cmd_buff[1]; | 97 | u32 reg_addr = cmd_buff[1]; |
| 77 | u32 size = cmd_buff[2]; | 98 | u32 size = cmd_buff[2]; |
| 78 | u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]); | ||
| 79 | 99 | ||
| 80 | switch (reg_addr) { | 100 | // TODO: Return proper error codes |
| 101 | if (reg_addr + size >= 0x420000) { | ||
| 102 | ERROR_LOG(GPU, "Read address out of range! (address=0x%08x, size=0x%08x)", reg_addr, size); | ||
| 103 | return; | ||
| 104 | } | ||
| 81 | 105 | ||
| 82 | // NOTE: Calling SetFramebufferLocation here is a hack... Not sure the correct way yet to set | 106 | // size should be word-aligned |
| 83 | // whether the framebuffers should be in VRAM or GSP heap, but from what I understand, if the | 107 | if ((size % 4) != 0) { |
| 84 | // user application is reading from either of these registers, then its going to be in VRAM. | 108 | ERROR_LOG(GPU, "Invalid size 0x%08x", size); |
| 109 | return; | ||
| 110 | } | ||
| 85 | 111 | ||
| 86 | // Top framebuffer 1 addresses | 112 | u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]); |
| 87 | case REG_FRAMEBUFFER_1: | ||
| 88 | GPU::SetFramebufferLocation(GPU::FRAMEBUFFER_LOCATION_VRAM); | ||
| 89 | memcpy(dst, framebuffer_1, size); | ||
| 90 | break; | ||
| 91 | 113 | ||
| 92 | // Top framebuffer 2 addresses | 114 | while (size > 0) { |
| 93 | case REG_FRAMEBUFFER_2: | 115 | GPU::Read<u32>(*dst, reg_addr + 0x1EB00000); |
| 94 | GPU::SetFramebufferLocation(GPU::FRAMEBUFFER_LOCATION_VRAM); | ||
| 95 | memcpy(dst, framebuffer_2, size); | ||
| 96 | break; | ||
| 97 | 116 | ||
| 98 | default: | 117 | size -= 4; |
| 99 | ERROR_LOG(GSP, "unknown register read at address %08X", reg_addr); | 118 | ++dst; |
| 119 | reg_addr += 4; | ||
| 100 | } | 120 | } |
| 101 | |||
| 102 | } | 121 | } |
| 103 | 122 | ||
| 104 | /** | 123 | /** |
| @@ -120,8 +139,8 @@ void RegisterInterruptRelayQueue(Service::Interface* self) { | |||
| 120 | 139 | ||
| 121 | Kernel::SetEventLocked(g_event, false); | 140 | Kernel::SetEventLocked(g_event, false); |
| 122 | 141 | ||
| 123 | // Hack - This function will permanently set the state of the GSP event such that GPU command | 142 | // Hack - This function will permanently set the state of the GSP event such that GPU command |
| 124 | // synchronization barriers always passthrough. Correct solution would be to set this after the | 143 | // synchronization barriers always passthrough. Correct solution would be to set this after the |
| 125 | // GPU as processed all queued up commands, but due to the emulator being single-threaded they | 144 | // GPU as processed all queued up commands, but due to the emulator being single-threaded they |
| 126 | // will always be ready. | 145 | // will always be ready. |
| 127 | Kernel::SetPermanentLock(g_event, true); | 146 | Kernel::SetPermanentLock(g_event, true); |
| @@ -134,52 +153,92 @@ void RegisterInterruptRelayQueue(Service::Interface* self) { | |||
| 134 | 153 | ||
| 135 | /// This triggers handling of the GX command written to the command buffer in shared memory. | 154 | /// This triggers handling of the GX command written to the command buffer in shared memory. |
| 136 | void TriggerCmdReqQueue(Service::Interface* self) { | 155 | void TriggerCmdReqQueue(Service::Interface* self) { |
| 156 | |||
| 157 | // Utility function to convert register ID to address | ||
| 158 | auto WriteGPURegister = [](u32 id, u32 data) { | ||
| 159 | GPU::Write<u32>(0x1EF00000 + 4 * id, data); | ||
| 160 | }; | ||
| 161 | |||
| 137 | GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(g_thread_id); | 162 | GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(g_thread_id); |
| 138 | u32* cmd_buff = (u32*)GX_GetCmdBufferPointer(g_thread_id, 0x20 + (header->index * 0x20)); | 163 | auto& command = *(const GXCommand*)GX_GetCmdBufferPointer(g_thread_id, 0x20 + (header->index * 0x20)); |
| 139 | 164 | ||
| 140 | switch (static_cast<GXCommandId>(cmd_buff[0])) { | 165 | switch (command.id) { |
| 141 | 166 | ||
| 142 | // GX request DMA - typically used for copying memory from GSP heap to VRAM | 167 | // GX request DMA - typically used for copying memory from GSP heap to VRAM |
| 143 | case GXCommandId::REQUEST_DMA: | 168 | case GXCommandId::REQUEST_DMA: |
| 144 | memcpy(Memory::GetPointer(cmd_buff[2]), Memory::GetPointer(cmd_buff[1]), cmd_buff[3]); | 169 | memcpy(Memory::GetPointer(command.dma_request.dest_address), |
| 170 | Memory::GetPointer(command.dma_request.source_address), | ||
| 171 | command.dma_request.size); | ||
| 145 | break; | 172 | break; |
| 146 | 173 | ||
| 174 | // ctrulib homebrew sends all relevant command list data with this command, | ||
| 175 | // hence we do all "interesting" stuff here and do nothing in SET_COMMAND_LIST_FIRST. | ||
| 176 | // TODO: This will need some rework in the future. | ||
| 147 | case GXCommandId::SET_COMMAND_LIST_LAST: | 177 | case GXCommandId::SET_COMMAND_LIST_LAST: |
| 148 | GPU::Write<u32>(GPU::Registers::CommandListAddress, cmd_buff[1] >> 3); | 178 | { |
| 149 | GPU::Write<u32>(GPU::Registers::CommandListSize, cmd_buff[2] >> 3); | 179 | auto& params = command.set_command_list_last; |
| 150 | GPU::Write<u32>(GPU::Registers::ProcessCommandList, 1); // TODO: Not sure if we are supposed to always write this | 180 | WriteGPURegister(GPU::Regs::CommandProcessor + 2, params.address >> 3); |
| 181 | WriteGPURegister(GPU::Regs::CommandProcessor, params.size >> 3); | ||
| 182 | WriteGPURegister(GPU::Regs::CommandProcessor + 4, 1); // TODO: Not sure if we are supposed to always write this .. seems to trigger processing though | ||
| 151 | 183 | ||
| 152 | // TODO: Move this to GPU | 184 | // TODO: Move this to GPU |
| 153 | // TODO: Not sure what units the size is measured in | 185 | // TODO: Not sure what units the size is measured in |
| 154 | g_debugger.CommandListCalled(cmd_buff[1], (u32*)Memory::GetPointer(cmd_buff[1]), cmd_buff[2]); | 186 | g_debugger.CommandListCalled(params.address, |
| 187 | (u32*)Memory::GetPointer(params.address), | ||
| 188 | params.size); | ||
| 155 | break; | 189 | break; |
| 190 | } | ||
| 156 | 191 | ||
| 192 | // It's assumed that the two "blocks" behave equivalently. | ||
| 193 | // Presumably this is done simply to allow two memory fills to run in parallel. | ||
| 157 | case GXCommandId::SET_MEMORY_FILL: | 194 | case GXCommandId::SET_MEMORY_FILL: |
| 195 | { | ||
| 196 | auto& params = command.memory_fill; | ||
| 197 | WriteGPURegister(GPU::Regs::MemoryFill, params.start1 >> 3); | ||
| 198 | WriteGPURegister(GPU::Regs::MemoryFill + 1, params.end1 >> 3); | ||
| 199 | WriteGPURegister(GPU::Regs::MemoryFill + 2, params.end1 - params.start1); | ||
| 200 | WriteGPURegister(GPU::Regs::MemoryFill + 3, params.value1); | ||
| 201 | |||
| 202 | WriteGPURegister(GPU::Regs::MemoryFill + 4, params.start2 >> 3); | ||
| 203 | WriteGPURegister(GPU::Regs::MemoryFill + 5, params.end2 >> 3); | ||
| 204 | WriteGPURegister(GPU::Regs::MemoryFill + 6, params.end2 - params.start2); | ||
| 205 | WriteGPURegister(GPU::Regs::MemoryFill + 7, params.value2); | ||
| 158 | break; | 206 | break; |
| 207 | } | ||
| 159 | 208 | ||
| 209 | // TODO: Check if texture copies are implemented correctly.. | ||
| 160 | case GXCommandId::SET_DISPLAY_TRANSFER: | 210 | case GXCommandId::SET_DISPLAY_TRANSFER: |
| 161 | break; | ||
| 162 | |||
| 163 | case GXCommandId::SET_TEXTURE_COPY: | 211 | case GXCommandId::SET_TEXTURE_COPY: |
| 212 | { | ||
| 213 | auto& params = command.image_copy; | ||
| 214 | WriteGPURegister(GPU::Regs::DisplayTransfer, params.in_buffer_address >> 3); | ||
| 215 | WriteGPURegister(GPU::Regs::DisplayTransfer + 1, params.out_buffer_address >> 3); | ||
| 216 | WriteGPURegister(GPU::Regs::DisplayTransfer + 3, params.in_buffer_size); | ||
| 217 | WriteGPURegister(GPU::Regs::DisplayTransfer + 2, params.out_buffer_size); | ||
| 218 | WriteGPURegister(GPU::Regs::DisplayTransfer + 4, params.flags); | ||
| 219 | |||
| 220 | // TODO: Should this only be ORed with 1 for texture copies? | ||
| 221 | // trigger transfer | ||
| 222 | WriteGPURegister(GPU::Regs::DisplayTransfer + 6, 1); | ||
| 164 | break; | 223 | break; |
| 224 | } | ||
| 165 | 225 | ||
| 226 | // TODO: Figure out what exactly SET_COMMAND_LIST_FIRST and SET_COMMAND_LIST_LAST | ||
| 227 | // are supposed to do. | ||
| 166 | case GXCommandId::SET_COMMAND_LIST_FIRST: | 228 | case GXCommandId::SET_COMMAND_LIST_FIRST: |
| 167 | { | 229 | { |
| 168 | //u32* buf0_data = (u32*)Memory::GetPointer(cmd_buff[1]); | ||
| 169 | //u32* buf1_data = (u32*)Memory::GetPointer(cmd_buff[3]); | ||
| 170 | //u32* buf2_data = (u32*)Memory::GetPointer(cmd_buff[5]); | ||
| 171 | break; | 230 | break; |
| 172 | } | 231 | } |
| 173 | 232 | ||
| 174 | default: | 233 | default: |
| 175 | ERROR_LOG(GSP, "unknown command 0x%08X", cmd_buff[0]); | 234 | ERROR_LOG(GSP, "unknown command 0x%08X", (int)command.id.Value()); |
| 176 | } | 235 | } |
| 177 | 236 | ||
| 178 | GX_FinishCommand(g_thread_id); | 237 | GX_FinishCommand(g_thread_id); |
| 179 | } | 238 | } |
| 180 | 239 | ||
| 181 | const Interface::FunctionInfo FunctionTable[] = { | 240 | const Interface::FunctionInfo FunctionTable[] = { |
| 182 | {0x00010082, nullptr, "WriteHWRegs"}, | 241 | {0x00010082, WriteHWRegs, "WriteHWRegs"}, |
| 183 | {0x00020084, nullptr, "WriteHWRegsWithMask"}, | 242 | {0x00020084, nullptr, "WriteHWRegsWithMask"}, |
| 184 | {0x00030082, nullptr, "WriteHWRegRepeat"}, | 243 | {0x00030082, nullptr, "WriteHWRegRepeat"}, |
| 185 | {0x00040080, ReadHWRegs, "ReadHWRegs"}, | 244 | {0x00040080, ReadHWRegs, "ReadHWRegs"}, |