platform.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2009 Alistair John Strachan <alistair@devzero.co.uk>
  4. * Copyright (C) 2007-2009 Kevin Vance <kvance@kvance.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdarg.h>
  21. #include "../../src/platform.h"
  22. #undef main
  23. #include "../../src/graphics.h"
  24. #include "../../src/util.h"
  25. #include <fat.h>
  26. #include "render.h"
  27. #include "ram.h"
  28. #include "dlmalloc.h"
  29. #include "exception.h"
  30. static unsigned int y = 0;
  31. // from arch/nds/event.c
  32. extern void nds_update_input(void);
  33. // Timer code stolen from SDL (LGPL)
  34. #define timers2ms(tlow,thigh)(tlow | (thigh<<16)) >> 5
  35. void delay(Uint32 ms)
  36. {
  37. Uint32 now;
  38. now = timers2ms(TIMER0_DATA, TIMER1_DATA);
  39. while((Uint32)timers2ms(TIMER0_DATA, TIMER1_DATA) < now + ms)
  40. ;
  41. }
  42. Uint32 get_ticks(void)
  43. {
  44. return timers2ms(TIMER0_DATA, TIMER1_DATA);
  45. }
  46. static void timer_init(void)
  47. {
  48. // TIMER0, TIMER1: Tick timer
  49. TIMER0_DATA = 0;
  50. TIMER1_DATA = 0;
  51. TIMER0_CR = TIMER_ENABLE | TIMER_DIV_1024;
  52. TIMER1_CR = TIMER_ENABLE | TIMER_CASCADE;
  53. // TIMER2: Input handler IRQ
  54. // We could do this in vblank, but sometimes we need to block new input
  55. // events when the event queue is in use. Using a hardware timer, we can
  56. // keep the graphics flicker running.
  57. TIMER2_DATA = TIMER_FREQ_1024(60);
  58. TIMER2_CR = TIMER_ENABLE | TIMER_DIV_1024 | TIMER_IRQ_REQ;
  59. irqSet(IRQ_TIMER2, nds_update_input);
  60. irqEnable(IRQ_TIMER2);
  61. }
  62. /*
  63. * Special handling for debug output
  64. */
  65. static __attribute__((format(printf, 2, 0)))
  66. void handle_debug_info(const char *type, const char *format, va_list args)
  67. {
  68. if(!has_video_initialized())
  69. {
  70. iprintf("%s: ", type);
  71. viprintf(format, args);
  72. fflush(stdout);
  73. }
  74. // TODO: Have a debug console on the subscreen show these.
  75. else
  76. {
  77. // This is just a hack right now :(
  78. char buffer[81];
  79. size_t len;
  80. sprintf(buffer, "%s: ", type);
  81. len = strlen(buffer);
  82. vsnprintf(buffer + len, 80 - len, format, args);
  83. buffer[80] = 0;
  84. write_string(buffer, 0, y, 0x4F, 1);
  85. y = (y+1)&7;
  86. }
  87. }
  88. void info(const char *format, ...)
  89. {
  90. va_list args;
  91. va_start(args, format);
  92. handle_debug_info("INFO", format, args);
  93. va_end(args);
  94. }
  95. void warn(const char *format, ...)
  96. {
  97. va_list args;
  98. va_start(args, format);
  99. handle_debug_info("WARNING", format, args);
  100. va_end(args);
  101. }
  102. #ifdef DEBUG
  103. void debug(const char *format, ...)
  104. {
  105. va_list args;
  106. va_start(args, format);
  107. handle_debug_info("DEBUG", format, args);
  108. va_end(args);
  109. }
  110. #endif // DEBUG
  111. boolean platform_init(void)
  112. {
  113. powerOn(POWER_ALL);
  114. setMzxExceptionHandler();
  115. if(!fatInitDefault())
  116. {
  117. iprintf("Unable to initialize FAT.\n"
  118. "Check your DLDI driver.\n");
  119. return false;
  120. }
  121. // If the "extra RAM" is missing, warn the user
  122. if(!isDSiMode())
  123. if(nds_ram_init(DETECT_RAM))
  124. nds_ext_lock();
  125. timer_init();
  126. // Enable vblank interrupts, but don't install the handler until the
  127. // graphics have initialized.
  128. irqEnable(IRQ_VBLANK);
  129. return true;
  130. }
  131. void platform_quit(void)
  132. {
  133. nds_ext_unlock();
  134. }
  135. // argc/argv is unreliable on NDS and varies between cards/launchers.
  136. int main(int argc, char *argv[])
  137. {
  138. static char _argv0[] = SHAREDIR "/mzxrun.nds";
  139. static char *_argv[] = {_argv0};
  140. consoleDemoInit();
  141. if(argc < 1 || argv == NULL || argv[0] == NULL)
  142. {
  143. iprintf("argv[0]: not found.\n"
  144. "using '%s'\n"
  145. "WARNING: Use of a loader that supports argv[0] is recommended.\n",
  146. _argv0);
  147. real_main(1, _argv);
  148. }
  149. else
  150. {
  151. iprintf("argv[0]: '%s'\n", argv[0]);
  152. real_main(argc, argv);
  153. }
  154. while(true);
  155. }