gpu.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <numeric>
  6. #include <type_traits>
  7. #include "common/color.h"
  8. #include "common/common_types.h"
  9. #include "common/logging/log.h"
  10. #include "common/math_util.h"
  11. #include "common/microprofile.h"
  12. #include "common/thread.h"
  13. #include "common/timer.h"
  14. #include "common/vector_math.h"
  15. #include "core/core_timing.h"
  16. #include "core/hle/service/gsp_gpu.h"
  17. #include "core/hw/gpu.h"
  18. #include "core/hw/hw.h"
  19. #include "core/memory.h"
  20. #include "core/settings.h"
  21. #include "core/tracer/recorder.h"
  22. #include "video_core/command_processor.h"
  23. #include "video_core/debug_utils/debug_utils.h"
  24. #include "video_core/rasterizer_interface.h"
  25. #include "video_core/renderer_base.h"
  26. #include "video_core/utils.h"
  27. #include "video_core/video_core.h"
  28. namespace GPU {
  29. Regs g_regs;
  30. /// 268MHz CPU clocks / 60Hz frames per second
  31. const u64 frame_ticks = BASE_CLOCK_RATE_ARM11 / 60;
  32. /// Event id for CoreTiming
  33. static int vblank_event;
  34. /// Total number of frames drawn
  35. static u64 frame_count;
  36. /// Start clock for frame limiter
  37. static u32 time_point;
  38. /// Total delay caused by slow frames
  39. static float time_delay;
  40. constexpr float FIXED_FRAME_TIME = 1000.0f / 60;
  41. // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
  42. // values increases time needed to limit frame rate after spikes
  43. constexpr float MAX_LAG_TIME = 18;
  44. template <typename T>
  45. inline void Read(T& var, const u32 raw_addr) {
  46. u32 addr = raw_addr - HW::VADDR_GPU;
  47. u32 index = addr / 4;
  48. // Reads other than u32 are untested, so I'd rather have them abort than silently fail
  49. if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) {
  50. LOG_ERROR(HW_GPU, "unknown Read%lu @ 0x%08X", sizeof(var) * 8, addr);
  51. return;
  52. }
  53. var = g_regs[addr / 4];
  54. }
  55. static Math::Vec4<u8> DecodePixel(Regs::PixelFormat input_format, const u8* src_pixel) {
  56. switch (input_format) {
  57. case Regs::PixelFormat::RGBA8:
  58. return Color::DecodeRGBA8(src_pixel);
  59. case Regs::PixelFormat::RGB8:
  60. return Color::DecodeRGB8(src_pixel);
  61. case Regs::PixelFormat::RGB565:
  62. return Color::DecodeRGB565(src_pixel);
  63. case Regs::PixelFormat::RGB5A1:
  64. return Color::DecodeRGB5A1(src_pixel);
  65. case Regs::PixelFormat::RGBA4:
  66. return Color::DecodeRGBA4(src_pixel);
  67. default:
  68. LOG_ERROR(HW_GPU, "Unknown source framebuffer format %x", input_format);
  69. return {0, 0, 0, 0};
  70. }
  71. }
  72. MICROPROFILE_DEFINE(GPU_DisplayTransfer, "GPU", "DisplayTransfer", MP_RGB(100, 100, 255));
  73. MICROPROFILE_DEFINE(GPU_CmdlistProcessing, "GPU", "Cmdlist Processing", MP_RGB(100, 255, 100));
  74. static void MemoryFill(const Regs::MemoryFillConfig& config) {
  75. const PAddr start_addr = config.GetStartAddress();
  76. const PAddr end_addr = config.GetEndAddress();
  77. // TODO: do hwtest with these cases
  78. if (!Memory::IsValidPhysicalAddress(start_addr)) {
  79. LOG_CRITICAL(HW_GPU, "invalid start address 0x%08X", start_addr);
  80. return;
  81. }
  82. if (!Memory::IsValidPhysicalAddress(end_addr)) {
  83. LOG_CRITICAL(HW_GPU, "invalid end address 0x%08X", end_addr);
  84. return;
  85. }
  86. if (end_addr <= start_addr) {
  87. LOG_CRITICAL(HW_GPU, "invalid memory range from 0x%08X to 0x%08X", start_addr, end_addr);
  88. return;
  89. }
  90. u8* start = Memory::GetPhysicalPointer(start_addr);
  91. u8* end = Memory::GetPhysicalPointer(end_addr);
  92. // TODO: Consider always accelerating and returning vector of
  93. // regions that the accelerated fill did not cover to
  94. // reduce/eliminate the fill that the cpu has to do.
  95. // This would also mean that the flush below is not needed.
  96. // Fill should first flush all surfaces that touch but are
  97. // not completely within the fill range.
  98. // Then fill all completely covered surfaces, and return the
  99. // regions that were between surfaces or within the touching
  100. // ones for cpu to manually fill here.
  101. if (VideoCore::g_renderer->Rasterizer()->AccelerateFill(config))
  102. return;
  103. Memory::RasterizerFlushAndInvalidateRegion(config.GetStartAddress(),
  104. config.GetEndAddress() - config.GetStartAddress());
  105. if (config.fill_24bit) {
  106. // fill with 24-bit values
  107. for (u8* ptr = start; ptr < end; ptr += 3) {
  108. ptr[0] = config.value_24bit_r;
  109. ptr[1] = config.value_24bit_g;
  110. ptr[2] = config.value_24bit_b;
  111. }
  112. } else if (config.fill_32bit) {
  113. // fill with 32-bit values
  114. if (end > start) {
  115. u32 value = config.value_32bit;
  116. size_t len = (end - start) / sizeof(u32);
  117. for (size_t i = 0; i < len; ++i)
  118. memcpy(&start[i * sizeof(u32)], &value, sizeof(u32));
  119. }
  120. } else {
  121. // fill with 16-bit values
  122. u16 value_16bit = config.value_16bit.Value();
  123. for (u8* ptr = start; ptr < end; ptr += sizeof(u16))
  124. memcpy(ptr, &value_16bit, sizeof(u16));
  125. }
  126. }
  127. static void DisplayTransfer(const Regs::DisplayTransferConfig& config) {
  128. const PAddr src_addr = config.GetPhysicalInputAddress();
  129. const PAddr dst_addr = config.GetPhysicalOutputAddress();
  130. // TODO: do hwtest with these cases
  131. if (!Memory::IsValidPhysicalAddress(src_addr)) {
  132. LOG_CRITICAL(HW_GPU, "invalid input address 0x%08X", src_addr);
  133. return;
  134. }
  135. if (!Memory::IsValidPhysicalAddress(dst_addr)) {
  136. LOG_CRITICAL(HW_GPU, "invalid output address 0x%08X", dst_addr);
  137. return;
  138. }
  139. if (config.input_width == 0) {
  140. LOG_CRITICAL(HW_GPU, "zero input width");
  141. return;
  142. }
  143. if (config.input_height == 0) {
  144. LOG_CRITICAL(HW_GPU, "zero input height");
  145. return;
  146. }
  147. if (config.output_width == 0) {
  148. LOG_CRITICAL(HW_GPU, "zero output width");
  149. return;
  150. }
  151. if (config.output_height == 0) {
  152. LOG_CRITICAL(HW_GPU, "zero output height");
  153. return;
  154. }
  155. if (VideoCore::g_renderer->Rasterizer()->AccelerateDisplayTransfer(config))
  156. return;
  157. u8* src_pointer = Memory::GetPhysicalPointer(src_addr);
  158. u8* dst_pointer = Memory::GetPhysicalPointer(dst_addr);
  159. if (config.scaling > config.ScaleXY) {
  160. LOG_CRITICAL(HW_GPU, "Unimplemented display transfer scaling mode %u",
  161. config.scaling.Value());
  162. UNIMPLEMENTED();
  163. return;
  164. }
  165. if (config.input_linear && config.scaling != config.NoScale) {
  166. LOG_CRITICAL(HW_GPU, "Scaling is only implemented on tiled input");
  167. UNIMPLEMENTED();
  168. return;
  169. }
  170. int horizontal_scale = config.scaling != config.NoScale ? 1 : 0;
  171. int vertical_scale = config.scaling == config.ScaleXY ? 1 : 0;
  172. u32 output_width = config.output_width >> horizontal_scale;
  173. u32 output_height = config.output_height >> vertical_scale;
  174. u32 input_size =
  175. config.input_width * config.input_height * GPU::Regs::BytesPerPixel(config.input_format);
  176. u32 output_size = output_width * output_height * GPU::Regs::BytesPerPixel(config.output_format);
  177. Memory::RasterizerFlushRegion(config.GetPhysicalInputAddress(), input_size);
  178. Memory::RasterizerFlushAndInvalidateRegion(config.GetPhysicalOutputAddress(), output_size);
  179. for (u32 y = 0; y < output_height; ++y) {
  180. for (u32 x = 0; x < output_width; ++x) {
  181. Math::Vec4<u8> src_color;
  182. // Calculate the [x,y] position of the input image
  183. // based on the current output position and the scale
  184. u32 input_x = x << horizontal_scale;
  185. u32 input_y = y << vertical_scale;
  186. u32 output_y;
  187. if (config.flip_vertically) {
  188. // Flip the y value of the output data,
  189. // we do this after calculating the [x,y] position of the input image
  190. // to account for the scaling options.
  191. output_y = output_height - y - 1;
  192. } else {
  193. output_y = y;
  194. }
  195. u32 dst_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.output_format);
  196. u32 src_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.input_format);
  197. u32 src_offset;
  198. u32 dst_offset;
  199. if (config.input_linear) {
  200. if (!config.dont_swizzle) {
  201. // Interpret the input as linear and the output as tiled
  202. u32 coarse_y = output_y & ~7;
  203. u32 stride = output_width * dst_bytes_per_pixel;
  204. src_offset = (input_x + input_y * config.input_width) * src_bytes_per_pixel;
  205. dst_offset = VideoCore::GetMortonOffset(x, output_y, dst_bytes_per_pixel) +
  206. coarse_y * stride;
  207. } else {
  208. // Both input and output are linear
  209. src_offset = (input_x + input_y * config.input_width) * src_bytes_per_pixel;
  210. dst_offset = (x + output_y * output_width) * dst_bytes_per_pixel;
  211. }
  212. } else {
  213. if (!config.dont_swizzle) {
  214. // Interpret the input as tiled and the output as linear
  215. u32 coarse_y = input_y & ~7;
  216. u32 stride = config.input_width * src_bytes_per_pixel;
  217. src_offset = VideoCore::GetMortonOffset(input_x, input_y, src_bytes_per_pixel) +
  218. coarse_y * stride;
  219. dst_offset = (x + output_y * output_width) * dst_bytes_per_pixel;
  220. } else {
  221. // Both input and output are tiled
  222. u32 out_coarse_y = output_y & ~7;
  223. u32 out_stride = output_width * dst_bytes_per_pixel;
  224. u32 in_coarse_y = input_y & ~7;
  225. u32 in_stride = config.input_width * src_bytes_per_pixel;
  226. src_offset = VideoCore::GetMortonOffset(input_x, input_y, src_bytes_per_pixel) +
  227. in_coarse_y * in_stride;
  228. dst_offset = VideoCore::GetMortonOffset(x, output_y, dst_bytes_per_pixel) +
  229. out_coarse_y * out_stride;
  230. }
  231. }
  232. const u8* src_pixel = src_pointer + src_offset;
  233. src_color = DecodePixel(config.input_format, src_pixel);
  234. if (config.scaling == config.ScaleX) {
  235. Math::Vec4<u8> pixel =
  236. DecodePixel(config.input_format, src_pixel + src_bytes_per_pixel);
  237. src_color = ((src_color + pixel) / 2).Cast<u8>();
  238. } else if (config.scaling == config.ScaleXY) {
  239. Math::Vec4<u8> pixel1 =
  240. DecodePixel(config.input_format, src_pixel + 1 * src_bytes_per_pixel);
  241. Math::Vec4<u8> pixel2 =
  242. DecodePixel(config.input_format, src_pixel + 2 * src_bytes_per_pixel);
  243. Math::Vec4<u8> pixel3 =
  244. DecodePixel(config.input_format, src_pixel + 3 * src_bytes_per_pixel);
  245. src_color = (((src_color + pixel1) + (pixel2 + pixel3)) / 4).Cast<u8>();
  246. }
  247. u8* dst_pixel = dst_pointer + dst_offset;
  248. switch (config.output_format) {
  249. case Regs::PixelFormat::RGBA8:
  250. Color::EncodeRGBA8(src_color, dst_pixel);
  251. break;
  252. case Regs::PixelFormat::RGB8:
  253. Color::EncodeRGB8(src_color, dst_pixel);
  254. break;
  255. case Regs::PixelFormat::RGB565:
  256. Color::EncodeRGB565(src_color, dst_pixel);
  257. break;
  258. case Regs::PixelFormat::RGB5A1:
  259. Color::EncodeRGB5A1(src_color, dst_pixel);
  260. break;
  261. case Regs::PixelFormat::RGBA4:
  262. Color::EncodeRGBA4(src_color, dst_pixel);
  263. break;
  264. default:
  265. LOG_ERROR(HW_GPU, "Unknown destination framebuffer format %x",
  266. config.output_format.Value());
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. static void TextureCopy(const Regs::DisplayTransferConfig& config) {
  273. const PAddr src_addr = config.GetPhysicalInputAddress();
  274. const PAddr dst_addr = config.GetPhysicalOutputAddress();
  275. // TODO: do hwtest with these cases
  276. if (!Memory::IsValidPhysicalAddress(src_addr)) {
  277. LOG_CRITICAL(HW_GPU, "invalid input address 0x%08X", src_addr);
  278. return;
  279. }
  280. if (!Memory::IsValidPhysicalAddress(dst_addr)) {
  281. LOG_CRITICAL(HW_GPU, "invalid output address 0x%08X", dst_addr);
  282. return;
  283. }
  284. if (config.texture_copy.input_width == 0) {
  285. LOG_CRITICAL(HW_GPU, "zero input width");
  286. return;
  287. }
  288. if (config.texture_copy.output_width == 0) {
  289. LOG_CRITICAL(HW_GPU, "zero output width");
  290. return;
  291. }
  292. if (config.texture_copy.size == 0) {
  293. LOG_CRITICAL(HW_GPU, "zero size");
  294. return;
  295. }
  296. if (VideoCore::g_renderer->Rasterizer()->AccelerateTextureCopy(config))
  297. return;
  298. u8* src_pointer = Memory::GetPhysicalPointer(src_addr);
  299. u8* dst_pointer = Memory::GetPhysicalPointer(dst_addr);
  300. u32 input_width = config.texture_copy.input_width * 16;
  301. u32 input_gap = config.texture_copy.input_gap * 16;
  302. u32 output_width = config.texture_copy.output_width * 16;
  303. u32 output_gap = config.texture_copy.output_gap * 16;
  304. size_t contiguous_input_size =
  305. config.texture_copy.size / input_width * (input_width + input_gap);
  306. Memory::RasterizerFlushRegion(config.GetPhysicalInputAddress(),
  307. static_cast<u32>(contiguous_input_size));
  308. size_t contiguous_output_size =
  309. config.texture_copy.size / output_width * (output_width + output_gap);
  310. Memory::RasterizerFlushAndInvalidateRegion(config.GetPhysicalOutputAddress(),
  311. static_cast<u32>(contiguous_output_size));
  312. u32 remaining_size = config.texture_copy.size;
  313. u32 remaining_input = input_width;
  314. u32 remaining_output = output_width;
  315. while (remaining_size > 0) {
  316. u32 copy_size = std::min({remaining_input, remaining_output, remaining_size});
  317. std::memcpy(dst_pointer, src_pointer, copy_size);
  318. src_pointer += copy_size;
  319. dst_pointer += copy_size;
  320. remaining_input -= copy_size;
  321. remaining_output -= copy_size;
  322. remaining_size -= copy_size;
  323. if (remaining_input == 0) {
  324. remaining_input = input_width;
  325. src_pointer += input_gap;
  326. }
  327. if (remaining_output == 0) {
  328. remaining_output = output_width;
  329. dst_pointer += output_gap;
  330. }
  331. }
  332. }
  333. template <typename T>
  334. inline void Write(u32 addr, const T data) {
  335. addr -= HW::VADDR_GPU;
  336. u32 index = addr / 4;
  337. // Writes other than u32 are untested, so I'd rather have them abort than silently fail
  338. if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) {
  339. LOG_ERROR(HW_GPU, "unknown Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32)data, addr);
  340. return;
  341. }
  342. g_regs[index] = static_cast<u32>(data);
  343. switch (index) {
  344. // Memory fills are triggered once the fill value is written.
  345. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[0].trigger, 0x00004 + 0x3):
  346. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[1].trigger, 0x00008 + 0x3): {
  347. const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger));
  348. auto& config = g_regs.memory_fill_config[is_second_filler];
  349. if (config.trigger) {
  350. MemoryFill(config);
  351. LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(),
  352. config.GetEndAddress());
  353. // It seems that it won't signal interrupt if "address_start" is zero.
  354. // TODO: hwtest this
  355. if (config.GetStartAddress() != 0) {
  356. if (!is_second_filler) {
  357. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PSC0);
  358. } else {
  359. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PSC1);
  360. }
  361. }
  362. // Reset "trigger" flag and set the "finish" flag
  363. // NOTE: This was confirmed to happen on hardware even if "address_start" is zero.
  364. config.trigger.Assign(0);
  365. config.finished.Assign(1);
  366. }
  367. break;
  368. }
  369. case GPU_REG_INDEX(display_transfer_config.trigger): {
  370. MICROPROFILE_SCOPE(GPU_DisplayTransfer);
  371. const auto& config = g_regs.display_transfer_config;
  372. if (config.trigger & 1) {
  373. if (Pica::g_debug_context)
  374. Pica::g_debug_context->OnEvent(Pica::DebugContext::Event::IncomingDisplayTransfer,
  375. nullptr);
  376. if (config.is_texture_copy) {
  377. TextureCopy(config);
  378. LOG_TRACE(HW_GPU, "TextureCopy: 0x%X bytes from 0x%08X(%u+%u)-> "
  379. "0x%08X(%u+%u), flags 0x%08X",
  380. config.texture_copy.size, config.GetPhysicalInputAddress(),
  381. config.texture_copy.input_width * 16, config.texture_copy.input_gap * 16,
  382. config.GetPhysicalOutputAddress(), config.texture_copy.output_width * 16,
  383. config.texture_copy.output_gap * 16, config.flags);
  384. } else {
  385. DisplayTransfer(config);
  386. LOG_TRACE(HW_GPU, "DisplayTransfer: 0x%08x(%ux%u)-> "
  387. "0x%08x(%ux%u), dst format %x, flags 0x%08X",
  388. config.GetPhysicalInputAddress(), config.input_width.Value(),
  389. config.input_height.Value(), config.GetPhysicalOutputAddress(),
  390. config.output_width.Value(), config.output_height.Value(),
  391. config.output_format.Value(), config.flags);
  392. }
  393. g_regs.display_transfer_config.trigger = 0;
  394. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PPF);
  395. }
  396. break;
  397. }
  398. // Seems like writing to this register triggers processing
  399. case GPU_REG_INDEX(command_processor_config.trigger): {
  400. const auto& config = g_regs.command_processor_config;
  401. if (config.trigger & 1) {
  402. MICROPROFILE_SCOPE(GPU_CmdlistProcessing);
  403. u32* buffer = (u32*)Memory::GetPhysicalPointer(config.GetPhysicalAddress());
  404. if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
  405. Pica::g_debug_context->recorder->MemoryAccessed((u8*)buffer, config.size,
  406. config.GetPhysicalAddress());
  407. }
  408. Pica::CommandProcessor::ProcessCommandList(buffer, config.size);
  409. g_regs.command_processor_config.trigger = 0;
  410. }
  411. break;
  412. }
  413. default:
  414. break;
  415. }
  416. // Notify tracer about the register write
  417. // This is happening *after* handling the write to make sure we properly catch all memory reads.
  418. if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
  419. // addr + GPU VBase - IO VBase + IO PBase
  420. Pica::g_debug_context->recorder->RegisterWritten<T>(
  421. addr + 0x1EF00000 - 0x1EC00000 + 0x10100000, data);
  422. }
  423. }
  424. // Explicitly instantiate template functions because we aren't defining this in the header:
  425. template void Read<u64>(u64& var, const u32 addr);
  426. template void Read<u32>(u32& var, const u32 addr);
  427. template void Read<u16>(u16& var, const u32 addr);
  428. template void Read<u8>(u8& var, const u32 addr);
  429. template void Write<u64>(u32 addr, const u64 data);
  430. template void Write<u32>(u32 addr, const u32 data);
  431. template void Write<u16>(u32 addr, const u16 data);
  432. template void Write<u8>(u32 addr, const u8 data);
  433. static void FrameLimiter() {
  434. time_delay += FIXED_FRAME_TIME;
  435. time_delay = MathUtil::Clamp(time_delay, -MAX_LAG_TIME, MAX_LAG_TIME);
  436. s32 desired_time = static_cast<s32>(time_delay);
  437. s32 elapsed_time = static_cast<s32>(Common::Timer::GetTimeMs() - time_point);
  438. if (elapsed_time < desired_time) {
  439. Common::SleepCurrentThread(desired_time - elapsed_time);
  440. }
  441. u32 frame_time = Common::Timer::GetTimeMs() - time_point;
  442. time_delay -= frame_time;
  443. }
  444. /// Update hardware
  445. static void VBlankCallback(u64 userdata, int cycles_late) {
  446. frame_count++;
  447. VideoCore::g_renderer->SwapBuffers();
  448. // Signal to GSP that GPU interrupt has occurred
  449. // TODO(yuriks): hwtest to determine if PDC0 is for the Top screen and PDC1 for the Sub
  450. // screen, or if both use the same interrupts and these two instead determine the
  451. // beginning and end of the VBlank period. If needed, split the interrupt firing into
  452. // two different intervals.
  453. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC0);
  454. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC1);
  455. if (!Settings::values.use_vsync && Settings::values.toggle_framelimit) {
  456. FrameLimiter();
  457. }
  458. time_point = Common::Timer::GetTimeMs();
  459. // Reschedule recurrent event
  460. CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event);
  461. }
  462. /// Initialize hardware
  463. void Init() {
  464. memset(&g_regs, 0, sizeof(g_regs));
  465. auto& framebuffer_top = g_regs.framebuffer_config[0];
  466. auto& framebuffer_sub = g_regs.framebuffer_config[1];
  467. // Setup default framebuffer addresses (located in VRAM)
  468. // .. or at least these are the ones used by system applets.
  469. // There's probably a smarter way to come up with addresses
  470. // like this which does not require hardcoding.
  471. framebuffer_top.address_left1 = 0x181E6000;
  472. framebuffer_top.address_left2 = 0x1822C800;
  473. framebuffer_top.address_right1 = 0x18273000;
  474. framebuffer_top.address_right2 = 0x182B9800;
  475. framebuffer_sub.address_left1 = 0x1848F000;
  476. framebuffer_sub.address_left2 = 0x184C7800;
  477. framebuffer_top.width.Assign(240);
  478. framebuffer_top.height.Assign(400);
  479. framebuffer_top.stride = 3 * 240;
  480. framebuffer_top.color_format.Assign(Regs::PixelFormat::RGB8);
  481. framebuffer_top.active_fb = 0;
  482. framebuffer_sub.width.Assign(240);
  483. framebuffer_sub.height.Assign(320);
  484. framebuffer_sub.stride = 3 * 240;
  485. framebuffer_sub.color_format.Assign(Regs::PixelFormat::RGB8);
  486. framebuffer_sub.active_fb = 0;
  487. frame_count = 0;
  488. time_point = Common::Timer::GetTimeMs();
  489. vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback);
  490. CoreTiming::ScheduleEvent(frame_ticks, vblank_event);
  491. LOG_DEBUG(HW_GPU, "initialized OK");
  492. }
  493. /// Shutdown hardware
  494. void Shutdown() {
  495. LOG_DEBUG(HW_GPU, "shutdown OK");
  496. }
  497. } // namespace