From 05f557f302ce566247c37619b130ebebae2a39c8 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 15 Apr 2021 18:18:05 +0200 Subject: Add tests for page flipping on mode 4 --- src/main.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 7c33166..2a6abb3 100644 --- a/src/main.c +++ b/src/main.c @@ -25,6 +25,9 @@ #define DISP_STATUS *((vu32*)(MEM_IO + 0x0004)) #define DISP_VCOUNT *((vu32*)(MEM_IO + 0x0006)) +// Bits for display control. +#define DISP_CONTROL_PAGE (1 << 4) + // Display modes. #define DISP_MODE_0 0x0000 #define DISP_MODE_1 0x0001 @@ -40,11 +43,6 @@ #define DISP_BG3 0x0800 #define DISP_OBJ 0x1000 -static inline void -set_display_mode(u16 value) { - *((vu32*)(MEM_IO + 0x0000)) = value; -} - // Screen settings. #define SCREEN_WIDTH 240 #define SCREEN_HEIGHT 160 @@ -236,9 +234,9 @@ wait_vsync() { // memory, instead we need to read a u16 word, mask and or the corresponding // bits and wave the updated u16. static inline void -put_pixel_m4(int x, int y, u8 col_index) { +put_pixel_m4(int x, int y, u8 col_index, u16 *buffer) { int buffer_index = (y * SCREEN_WIDTH + x) / 2; - u16 *destination = &SCREEN_BUFFER[buffer_index]; + u16 *destination = &buffer[buffer_index]; // Odd pixels will go to the top 8 bits of the destination. Even pixels to // the lower 8 bits. int odd = x & 0x1; @@ -250,24 +248,41 @@ put_pixel_m4(int x, int y, u8 col_index) { } static inline void -draw_fill_rect_m4(int x0, int y0, int x1, int y1, u8 col_index) { +draw_fill_rect_m4(int x0, int y0, int x1, int y1, u8 col_index, u16 *buffer) { int ix, iy; for(iy = y0; iy < y1; iy++) { for(ix = x0; ix < x1; ix++) { - put_pixel_m4(ix, iy, col_index); + put_pixel_m4(ix, iy, col_index, buffer); } } } +static inline u16 +flip_page() { + DISP_CONTROL ^= DISP_CONTROL_PAGE; +} + +#define SCREEN_PAGE_1 ((vu16*) MEM_VRAM) +#define SCREEN_PAGE_2 ((vu16*) (MEM_VRAM + 0xa000)) + int main(void) { - set_display_mode(DISP_MODE_4 | DISP_BG2); + DISP_CONTROL = DISP_MODE_4 | DISP_BG2; PAL_BUFFER[1] = COLOR_RED; PAL_BUFFER[2] = COLOR_BLUE; - draw_fill_rect_m4(0, 0, 20, 20, 1); + // Prepare the screen buffers by drawing a rectangle with different colors + // to each page. + draw_fill_rect_m4(0, 0, 20, 20, 1, SCREEN_PAGE_1); + draw_fill_rect_m4(0, 0, 20, 20, 2, SCREEN_PAGE_2); + + int frame_counter = 0; while(true) { wait_vsync(); + if (frame_counter++ > 30) { + frame_counter = 0; + flip_page(); + } }; return 0; -- cgit v1.2.1