Guest User

Untitled

a guest
Jul 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. /* main file for hw3
  2. */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #ifdef WITH_GBA
  9. #include <gba_input.h>
  10. #include <gba_timers.h>
  11. #include <gba_video.h>
  12. #include <gba_console.h>
  13. #else
  14. #include <mygba.h>
  15. #endif
  16.  
  17. // input image
  18. #include "input.h"
  19. const u32 width = 240, height = 160, size = width*height;
  20.  
  21. extern "C" void boxfilter(u16* dst, const u16* src);
  22. extern "C" void myfilter(u16* dst, const u16* src);
  23.  
  24. void setup_timer();
  25. void start_timer();
  26. void stop_timer();
  27. u32 get_timer();
  28. void init();
  29. void setup_text_mode();
  30. void setup_video_mode();
  31. void draw_bitmap(const u16*);
  32. void draw_text(const char*, const char*);
  33. void setup_pad();
  34. bool key_down();
  35. bool key_up();
  36. bool key_left();
  37. bool key_right();
  38. void debug_print(const char*);
  39.  
  40. int main(void)
  41. {
  42. u16 *original = (u16*)input_pic;
  43. u16 *groundtruth = (u16*)malloc(sizeof(u16)*size);
  44. u16 *filtered = (u16*)malloc(sizeof(u16)*size);
  45.  
  46. // groundtruth
  47. boxfilter(groundtruth, original);
  48.  
  49. setup_timer();
  50. start_timer();
  51.  
  52. u32 start = get_timer();
  53. myfilter(filtered, original);
  54. u32 end = get_timer();
  55.  
  56. // verify correct or not
  57. bool correct = true;
  58. u32 at_x, at_y;
  59. for(u32 y = 0; y < height; ++y)
  60. for(u32 x=0; x < width; ++x)
  61. if( groundtruth[width*y + x] != filtered[width*y + x]) {
  62. if(correct) {
  63. at_x = x;
  64. at_y = y;
  65. }
  66. correct = false;
  67. filtered[width*y + x] = 0x1f;
  68. //break;
  69. }
  70.  
  71. init();
  72. setup_video_mode();
  73. draw_bitmap(filtered);
  74.  
  75. char result[256] = {0}, clocks[256] = {0};
  76. if(correct)
  77. sprintf(result, "%s,", correct ? "CORRECT" : "WRONG");
  78. else
  79. sprintf(result, "(%d, %d)", at_x, at_y);
  80. sprintf(clocks, "%u CLOCKS.", end-start);
  81. // debug_print(message);
  82.  
  83. while (1) {
  84. setup_pad();
  85. if (key_up()) { // filtered image
  86. setup_video_mode();
  87. draw_bitmap(filtered);
  88. } else if (key_left()) { // original image
  89. setup_video_mode();
  90. draw_bitmap(original);
  91. } else if (key_right()) { // reference image
  92. setup_video_mode();
  93. draw_bitmap(groundtruth);
  94. } else if (key_down()) { // result and clocks
  95. setup_text_mode();
  96. draw_text(result, clocks);
  97. }
  98. }
  99. return 0;
  100. }
  101.  
  102. void setup_timer()
  103. {
  104. #ifdef WITH_GBA
  105. // prepare the timer
  106. REG_TM0CNT_H &= 0xFFFC;
  107. REG_TM1CNT_H |= 0x0004;
  108. REG_TM0CNT_L = 0;
  109. REG_TM1CNT_L = 0;
  110. #else
  111. M_TIM0CNT_SPEED_SELECT_SET(0);
  112. R_TIM1CNT |= 0x0004;
  113. REG_TM1D = 0;
  114. REG_TM0D = 0;
  115. #endif
  116. }
  117.  
  118. void start_timer()
  119. {
  120. #ifdef WITH_GBA
  121. REG_TM0CNT_H |= 0x0080;
  122. REG_TM1CNT_H |= 0x0080;
  123. #else
  124. M_TIM0CNT_TIMER_START;
  125. M_TIM1CNT_TIMER_START;
  126. #endif
  127. }
  128.  
  129. void stop_timer()
  130. {
  131. #ifdef WITH_GBA
  132. REG_TM0CNT_H &= 0xFF7F;
  133. REG_TM1CNT_H &= 0xFF7F;
  134. #else
  135. M_TIM0CNT_TIMER_STOP;
  136. M_TIM1CNT_TIMER_STOP;
  137. #endif
  138. }
  139.  
  140. u32 get_timer()
  141. {
  142. u16 h, l;
  143. #ifdef WITH_GBA
  144. l = REG_TM0CNT_L;
  145. h = REG_TM1CNT_L;
  146. #else
  147. l = REG_TM0D;
  148. h = REG_TM1D;
  149. #endif
  150. return (h<<16) + l;
  151. }
  152.  
  153. void init()
  154. {
  155. #ifdef WITH_GBA
  156. #else
  157. ham_Init();
  158. #endif
  159. }
  160.  
  161. void setup_text_mode()
  162. {
  163. #ifdef WITH_GBA
  164. consoleDemoInit();
  165. #else
  166. ham_SetBgMode(0);
  167. ham_InitText(0);
  168. #endif
  169. }
  170.  
  171. void setup_video_mode()
  172. {
  173. #ifdef WITH_GBA
  174. SetMode(BG2_ON | MODE_3);
  175. #else
  176. ham_SetBgMode(3);
  177. #endif
  178. }
  179.  
  180. void draw_bitmap(const u16 *bitmap)
  181. {
  182. #ifdef WITH_GBA
  183. memcpy((void*)VRAM, (void*)bitmap, sizeof(u16)*size);
  184. #else
  185. ham_LoadBitmap(const_cast<u16*>(bitmap));
  186. #endif
  187. }
  188.  
  189. void draw_text(const char *result, const char *clocks)
  190. {
  191. #ifdef WITH_GBA
  192. iprintf("\x1b[8;0H");
  193. iprintf(result);
  194. iprintf("\x1b[10;0H");
  195. iprintf(clocks);
  196. #else
  197. ham_DrawText(0, 8, const_cast<char*>(result));
  198. ham_DrawText(0, 10, const_cast<char*>(clocks));
  199. #endif
  200. }
  201.  
  202. #ifdef WITH_GBA
  203. u16 key;
  204. #endif
  205.  
  206. void setup_pad()
  207. {
  208. #ifdef WITH_GBA
  209. scanKeys();
  210. key = keysDown();
  211. #else
  212. ham_UpdatePad();
  213. #endif
  214. }
  215.  
  216. bool key_down()
  217. {
  218. #ifdef WITH_GBA
  219. return key == KEY_DOWN;
  220. #else
  221. return Pad.Pressed.Down;
  222. #endif
  223. }
  224.  
  225. bool key_up()
  226. {
  227. #ifdef WITH_GBA
  228. return key == KEY_UP;
  229. #else
  230. return Pad.Pressed.Up;
  231. #endif
  232. }
  233.  
  234. bool key_left()
  235. {
  236. #ifdef WITH_GBA
  237. return key == KEY_LEFT;
  238. #else
  239. return Pad.Pressed.Left;
  240. #endif
  241. }
  242.  
  243. bool key_right()
  244. {
  245. #ifdef WITH_GBA
  246. return key == KEY_RIGHT;
  247. #else
  248. return Pad.Pressed.Right;
  249. #endif
  250. }
  251.  
  252. void debug_print(const char *s)
  253. {
  254. asm volatile("mov r0, %0;"
  255. "swi 0xff;"
  256. : // no ouput
  257. : "r" (s)
  258. : "r0");
  259. }
Add Comment
Please, Sign In to add comment