platform.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2008 Alan Williams <mralert@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "../../src/platform.h"
  20. #undef main
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <limits.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include <sys/iosupport.h>
  28. #define BOOL _BOOL
  29. #include <ogc/system.h>
  30. #include <ogc/lwp.h>
  31. #include <ogc/lwp_threads.h>
  32. #include <ogc/lwp_watchdog.h>
  33. #include <fat.h>
  34. #undef BOOL
  35. #define STACKSIZE 8192
  36. static lwpq_t reset_queue;
  37. static lwp_t reset_thread;
  38. static u8 reset_stack[STACKSIZE];
  39. void c_default_exceptionhandler(frame_context *pCtx);
  40. // Emergency exit
  41. static void *wii_reset_thread(void *dud)
  42. {
  43. LWP_ThreadSleep(reset_queue);
  44. platform_quit();
  45. delay(1000);
  46. c_default_exceptionhandler(&_thr_main->context);
  47. exit(0);
  48. return 0;
  49. }
  50. static void reset_callback(u32 irq, void *ctx)
  51. {
  52. static volatile int callcount = 0;
  53. callcount++;
  54. if(callcount > 5)
  55. *(int *)NULL = 0xDEADCAFE; // Try to cause an exception if pressed 6 times
  56. LWP_ThreadSignal(reset_queue);
  57. }
  58. static u64 timebase_offset;
  59. void delay(Uint32 ms)
  60. {
  61. // TODO use nanosleep instead?
  62. usleep(ms * 1000);
  63. }
  64. Uint32 get_ticks(void)
  65. {
  66. return ticks_to_millisecs(gettime() - timebase_offset);
  67. }
  68. boolean platform_init(void)
  69. {
  70. timebase_offset = gettime();
  71. LWP_InitQueue(&reset_queue);
  72. LWP_CreateThread(&reset_thread, wii_reset_thread, NULL, reset_stack,
  73. STACKSIZE, 127);
  74. SYS_SetResetCallback(reset_callback);
  75. if(!fatInitDefault())
  76. return false;
  77. return true;
  78. }
  79. void platform_quit(void)
  80. {
  81. int i;
  82. for(i = 0; i < STD_MAX; i++)
  83. if(devoptab_list[i] && devoptab_list[i]->chdir_r)
  84. fatUnmount(devoptab_list[i]->name);
  85. }
  86. // argc may be 0 if the loader doesn't set args
  87. int main(int argc, char *argv[])
  88. {
  89. static char _argv0[] = SHAREDIR "megazeux";
  90. static char *_argv[] = {_argv0};
  91. if(!argc)
  92. real_main(1, _argv);
  93. else
  94. real_main(argc, argv);
  95. }