From fb4a3111051bc63b202fe2b43582d8d39c9d237e Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 16 Apr 2021 11:10:19 +0200 Subject: Add profiling code and test with m4 vs m3 drawing --- src/main.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file 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 @@ #define DISP_MODE_5 0x0005 // Layers. -#define DISP_BG0 0x0100 -#define DISP_BG1 0x0200 -#define DISP_BG2 0x0400 -#define DISP_BG3 0x0800 -#define DISP_OBJ 0x1000 +#define DISP_BG_0 0x0100 +#define DISP_BG_1 0x0200 +#define DISP_BG_2 0x0400 +#define DISP_BG_3 0x0800 +#define DISP_OBJ 0x1000 // Screen settings. #define SCREEN_WIDTH 240 @@ -265,8 +265,49 @@ flip_page() { #define SCREEN_PAGE_1 ((vu16*) MEM_VRAM) #define SCREEN_PAGE_2 ((vu16*) (MEM_VRAM + 0xa000)) +// +// Profiling. +// + +#define TIMER_DATA_0 *((vu16*) (0x04000100 + 0x04 * 0)) +#define TIMER_DATA_1 *((vu16*) (0x04000100 + 0x04 * 1)) +#define TIMER_DATA_2 *((vu16*) (0x04000100 + 0x04 * 2)) +#define TIMER_DATA_3 *((vu16*) (0x04000100 + 0x04 * 3)) +#define TIMER_CTRL_0 *((vu16*) (0x04000102 + 0x04 * 0)) +#define TIMER_CTRL_1 *((vu16*) (0x04000102 + 0x04 * 1)) +#define TIMER_CTRL_2 *((vu16*) (0x04000102 + 0x04 * 2)) +#define TIMER_CTRL_3 *((vu16*) (0x04000102 + 0x04 * 3)) + +// Timer control bits. +#define TIMER_CTRL_FREQ_0 0 +#define TIMER_CTRL_FREQ_1 1 +#define TIMER_CTRL_FREQ_2 2 +#define TIMER_CTRL_FREQ_3 3 +#define TIMER_CTRL_CASCADE (1 << 2) +#define TIMER_CTRL_IRQ (1 << 6) +#define TIMER_CTRL_ENABLE (1 << 7) + +// We use timers 2 and 3 to count the number of cycles since the profile_start +// functions is called. Don't use if the code we are trying to profile make use +// of these timers. +static inline +void profile_start() { + TIMER_DATA_2 = 0; + TIMER_DATA_3 = 0; + TIMER_CTRL_2 = 0; + TIMER_CTRL_3 = 0; + TIMER_CTRL_3 = TIMER_CTRL_ENABLE | TIMER_CTRL_CASCADE; + TIMER_CTRL_2 = TIMER_CTRL_ENABLE; +} + +static inline +u32 profile_stop() { + TIMER_CTRL_2 = 0; + return (TIMER_DATA_3 << 16) | TIMER_DATA_2; +} + int main(void) { - DISP_CONTROL = DISP_MODE_4 | DISP_BG2; + DISP_CONTROL = DISP_MODE_4 | DISP_BG_2; PAL_BUFFER[1] = COLOR_RED; PAL_BUFFER[2] = COLOR_BLUE; @@ -276,13 +317,34 @@ int main(void) { draw_fill_rect_m4(0, 0, 20, 20, 1, SCREEN_PAGE_1); draw_fill_rect_m4(0, 0, 20, 20, 2, SCREEN_PAGE_2); + // Profile mode 4 vs mode 3 drawing fill_rects. + int n_iterations = 1000; + profile_start(); + for (size_t i = 0; i < n_iterations; ++i) { + draw_fill_rect_m4(0, 0, 20, 20, 1, SCREEN_PAGE_1); + } + u32 mode_4_count = profile_stop(); + + DISP_CONTROL = DISP_MODE_3 | DISP_BG_2; + profile_start(); + for (size_t i = 0; i < n_iterations; ++i) { + draw_fill_rect(0, 0, 20, 20, 1); + } + u32 mode_3_count = profile_stop(); + + char msg[240]; + sprintf(&msg, "m4: %u", mode_4_count); + put_text(0, 0, COLOR_RED, msg); + sprintf(&msg, "m3: %u", mode_3_count); + put_text(0, 16, COLOR_RED, msg); + int frame_counter = 0; while(true) { wait_vsync(); - if (frame_counter++ > 30) { - frame_counter = 0; - flip_page(); - } + // if (frame_counter++ > 30) { + // frame_counter = 0; + // flip_page(); + // } }; return 0; -- cgit v1.2.1