summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-16 11:10:19 +0200
committerBad Diode <bd@badd10de.dev>2021-04-16 11:10:19 +0200
commitfb4a3111051bc63b202fe2b43582d8d39c9d237e (patch)
tree83fef71dcc50f34615d23bcf39438a2c669230eb
parent07ed91a0b730e84ac351748fcefc4c846ff7d5ba (diff)
downloadgba-experiments-fb4a3111051bc63b202fe2b43582d8d39c9d237e.tar.gz
gba-experiments-fb4a3111051bc63b202fe2b43582d8d39c9d237e.zip
Add profiling code and test with m4 vs m3 drawing
-rw-r--r--src/main.c82
1 files changed, 72 insertions, 10 deletions
diff --git a/src/main.c b/src/main.c
index 720f77e..d76273a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -37,11 +37,11 @@
37#define DISP_MODE_5 0x0005 37#define DISP_MODE_5 0x0005
38 38
39// Layers. 39// Layers.
40#define DISP_BG0 0x0100 40#define DISP_BG_0 0x0100
41#define DISP_BG1 0x0200 41#define DISP_BG_1 0x0200
42#define DISP_BG2 0x0400 42#define DISP_BG_2 0x0400
43#define DISP_BG3 0x0800 43#define DISP_BG_3 0x0800
44#define DISP_OBJ 0x1000 44#define DISP_OBJ 0x1000
45 45
46// Screen settings. 46// Screen settings.
47#define SCREEN_WIDTH 240 47#define SCREEN_WIDTH 240
@@ -265,8 +265,49 @@ flip_page() {
265#define SCREEN_PAGE_1 ((vu16*) MEM_VRAM) 265#define SCREEN_PAGE_1 ((vu16*) MEM_VRAM)
266#define SCREEN_PAGE_2 ((vu16*) (MEM_VRAM + 0xa000)) 266#define SCREEN_PAGE_2 ((vu16*) (MEM_VRAM + 0xa000))
267 267
268//
269// Profiling.
270//
271
272#define TIMER_DATA_0 *((vu16*) (0x04000100 + 0x04 * 0))
273#define TIMER_DATA_1 *((vu16*) (0x04000100 + 0x04 * 1))
274#define TIMER_DATA_2 *((vu16*) (0x04000100 + 0x04 * 2))
275#define TIMER_DATA_3 *((vu16*) (0x04000100 + 0x04 * 3))
276#define TIMER_CTRL_0 *((vu16*) (0x04000102 + 0x04 * 0))
277#define TIMER_CTRL_1 *((vu16*) (0x04000102 + 0x04 * 1))
278#define TIMER_CTRL_2 *((vu16*) (0x04000102 + 0x04 * 2))
279#define TIMER_CTRL_3 *((vu16*) (0x04000102 + 0x04 * 3))
280
281// Timer control bits.
282#define TIMER_CTRL_FREQ_0 0
283#define TIMER_CTRL_FREQ_1 1
284#define TIMER_CTRL_FREQ_2 2
285#define TIMER_CTRL_FREQ_3 3
286#define TIMER_CTRL_CASCADE (1 << 2)
287#define TIMER_CTRL_IRQ (1 << 6)
288#define TIMER_CTRL_ENABLE (1 << 7)
289
290// We use timers 2 and 3 to count the number of cycles since the profile_start
291// functions is called. Don't use if the code we are trying to profile make use
292// of these timers.
293static inline
294void profile_start() {
295 TIMER_DATA_2 = 0;
296 TIMER_DATA_3 = 0;
297 TIMER_CTRL_2 = 0;
298 TIMER_CTRL_3 = 0;
299 TIMER_CTRL_3 = TIMER_CTRL_ENABLE | TIMER_CTRL_CASCADE;
300 TIMER_CTRL_2 = TIMER_CTRL_ENABLE;
301}
302
303static inline
304u32 profile_stop() {
305 TIMER_CTRL_2 = 0;
306 return (TIMER_DATA_3 << 16) | TIMER_DATA_2;
307}
308
268int main(void) { 309int main(void) {
269 DISP_CONTROL = DISP_MODE_4 | DISP_BG2; 310 DISP_CONTROL = DISP_MODE_4 | DISP_BG_2;
270 311
271 PAL_BUFFER[1] = COLOR_RED; 312 PAL_BUFFER[1] = COLOR_RED;
272 PAL_BUFFER[2] = COLOR_BLUE; 313 PAL_BUFFER[2] = COLOR_BLUE;
@@ -276,13 +317,34 @@ int main(void) {
276 draw_fill_rect_m4(0, 0, 20, 20, 1, SCREEN_PAGE_1); 317 draw_fill_rect_m4(0, 0, 20, 20, 1, SCREEN_PAGE_1);
277 draw_fill_rect_m4(0, 0, 20, 20, 2, SCREEN_PAGE_2); 318 draw_fill_rect_m4(0, 0, 20, 20, 2, SCREEN_PAGE_2);
278 319
320 // Profile mode 4 vs mode 3 drawing fill_rects.
321 int n_iterations = 1000;
322 profile_start();
323 for (size_t i = 0; i < n_iterations; ++i) {
324 draw_fill_rect_m4(0, 0, 20, 20, 1, SCREEN_PAGE_1);
325 }
326 u32 mode_4_count = profile_stop();
327
328 DISP_CONTROL = DISP_MODE_3 | DISP_BG_2;
329 profile_start();
330 for (size_t i = 0; i < n_iterations; ++i) {
331 draw_fill_rect(0, 0, 20, 20, 1);
332 }
333 u32 mode_3_count = profile_stop();
334
335 char msg[240];
336 sprintf(&msg, "m4: %u", mode_4_count);
337 put_text(0, 0, COLOR_RED, msg);
338 sprintf(&msg, "m3: %u", mode_3_count);
339 put_text(0, 16, COLOR_RED, msg);
340
279 int frame_counter = 0; 341 int frame_counter = 0;
280 while(true) { 342 while(true) {
281 wait_vsync(); 343 wait_vsync();
282 if (frame_counter++ > 30) { 344 // if (frame_counter++ > 30) {
283 frame_counter = 0; 345 // frame_counter = 0;
284 flip_page(); 346 // flip_page();
285 } 347 // }
286 }; 348 };
287 349
288 return 0; 350 return 0;