diff options
author | Bad Diode <bd@badd10de.dev> | 2023-04-15 22:03:00 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2023-04-15 22:03:00 +0200 |
commit | 85bff5e057c62b82f9cfc39f0ded555ed2533bd4 (patch) | |
tree | fef72799290bbb53767b6ad7fe0c4e94349c6189 | |
parent | e56c4d8e3932db4edfa04312e02fa2be071f93c4 (diff) | |
download | gba-link-cable-tester-85bff5e057c62b82f9cfc39f0ded555ed2533bd4.tar.gz gba-link-cable-tester-85bff5e057c62b82f9cfc39f0ded555ed2533bd4.zip |
Setup working double buffering example
-rw-r--r-- | src/main.c | 20 | ||||
-rw-r--r-- | src/renderer_m0.c | 63 |
2 files changed, 51 insertions, 32 deletions
@@ -119,32 +119,42 @@ int main(void) { | |||
119 | // Register interrupts. | 119 | // Register interrupts. |
120 | irq_init(); | 120 | irq_init(); |
121 | irs_set(IRQ_VBLANK, irs_stub); | 121 | irs_set(IRQ_VBLANK, irs_stub); |
122 | flip_buffer(); | ||
123 | 122 | ||
123 | // draw_filled_rect(0, 0, 7, 7, 1); | ||
124 | // flip_buffer(); | ||
125 | // draw_filled_rect(0, 0, 7, 7, 2); | ||
126 | // draw_filled_rect(8, 0, 15, 7, 3); | ||
127 | // BG_H_SCROLL_0 = 240; | ||
128 | // BG_H_SCROLL_0 = 240; | ||
129 | // BG_H_SCROLL_1 = 0; | ||
124 | // Main loop. | 130 | // Main loop. |
125 | PROF_INIT(); | 131 | PROF_INIT(); |
126 | int x = 0; | 132 | int x = 0; |
127 | int inc = 1; | 133 | int inc = 1; |
128 | while (true) { | 134 | while (true) { |
129 | bios_vblank_wait(); | 135 | bios_vblank_wait(); |
136 | flip_buffer(); | ||
130 | 137 | ||
131 | PROF(test_clear(), test_clear_cycles); | 138 | PROF(test_clear(), test_clear_cycles); |
132 | // PROF(test_lines(), test_lines_cycles); | 139 | // PROF(test_lines(), test_lines_cycles); |
140 | |||
141 | // ANIMATION EXAMPLE | ||
133 | draw_rect(0 + x, 0, 100 + x, 100, 2); | 142 | draw_rect(0 + x, 0, 100 + x, 100, 2); |
134 | x += inc; | 143 | x += inc; |
135 | if (x >= 50) { | 144 | if (x >= 50) { |
136 | inc = -8; | 145 | inc = -1; |
137 | } else if (x <= 0) { | 146 | } else if (x <= 0) { |
138 | inc = 8; | 147 | inc = 1; |
139 | } | 148 | } |
149 | |||
140 | // draw_rect(50, 10, 200, 110, 1); | 150 | // draw_rect(50, 10, 200, 110, 1); |
141 | // PROF(test_rect(), test_rect_cycles); | 151 | PROF(test_rect(), test_rect_cycles); |
142 | // PROF(test_fill_rect(), test_fill_rect_cycles); | 152 | // PROF(test_fill_rect(), test_fill_rect_cycles); |
143 | // PROF(test_chr(), test_chr_cycles); | 153 | // PROF(test_chr(), test_chr_cycles); |
144 | // PROF(test_icn(), test_icn_cycles); | 154 | // PROF(test_icn(), test_icn_cycles); |
145 | // draw_filled_rect(0, 0, 140, 60, 0); | 155 | // draw_filled_rect(0, 0, 140, 60, 0); |
146 | // PROF_SHOW(); | 156 | // PROF_SHOW(); |
147 | PROF(flip_buffer(), flip_cycles); | 157 | // PROF(flip_buffer(), flip_cycles); |
148 | } | 158 | } |
149 | 159 | ||
150 | return 0; | 160 | return 0; |
diff --git a/src/renderer_m0.c b/src/renderer_m0.c index b0d4a6e..b187903 100644 --- a/src/renderer_m0.c +++ b/src/renderer_m0.c | |||
@@ -29,10 +29,10 @@ | |||
29 | #define CB_1 1 | 29 | #define CB_1 1 |
30 | // NOTE: CB_2 could be at the end of 2 (0x0600:8000) since we only need a clear | 30 | // NOTE: CB_2 could be at the end of 2 (0x0600:8000) since we only need a clear |
31 | // color tile. Testing at 3 for now. | 31 | // color tile. Testing at 3 for now. |
32 | #define CB_2 3 | 32 | // #define CB_2 3 |
33 | #define SB_0 20 | 33 | #define SB_0 20 |
34 | #define SB_1 21 | 34 | #define SB_1 22 |
35 | #define SB_2 23 | 35 | // #define SB_2 23 |
36 | 36 | ||
37 | // Available storage for other memory. | 37 | // Available storage for other memory. |
38 | // #define FG_PIXELS ((u32*)(MEM_VRAM + KB(44))) | 38 | // #define FG_PIXELS ((u32*)(MEM_VRAM + KB(44))) |
@@ -56,7 +56,7 @@ | |||
56 | 56 | ||
57 | // Keep track of which tiles need to be copied to the frontbuffer. | 57 | // Keep track of which tiles need to be copied to the frontbuffer. |
58 | static u32 dirty_tiles[21] = {0}; | 58 | static u32 dirty_tiles[21] = {0}; |
59 | static u32 *backbuf = BUF_1; | 59 | static u32 *backbuf = BUF_0; |
60 | 60 | ||
61 | // Boundchecks can be disable at compile time but this will not always improve | 61 | // Boundchecks can be disable at compile time but this will not always improve |
62 | // the performance and can in fact make it worse. It is possible that this is | 62 | // the performance and can in fact make it worse. It is possible that this is |
@@ -75,6 +75,7 @@ static u32 *backbuf = BUF_1; | |||
75 | 75 | ||
76 | IWRAM_CODE | 76 | IWRAM_CODE |
77 | void screen_fill(u8 clr) { | 77 | void screen_fill(u8 clr) { |
78 | // TODO: revise this with updated renderer. | ||
78 | #if 1 | 79 | #if 1 |
79 | u32 *dst = backbuf; | 80 | u32 *dst = backbuf; |
80 | for(int i = 0; i < KB(20) / 4; i++) { | 81 | for(int i = 0; i < KB(20) / 4; i++) { |
@@ -445,12 +446,16 @@ void | |||
445 | flip_buffer(void) { | 446 | flip_buffer(void) { |
446 | if (backbuf == BUF_0) { | 447 | if (backbuf == BUF_0) { |
447 | backbuf = BUF_1; | 448 | backbuf = BUF_1; |
448 | BG_CTRL(0) = BG_CHARBLOCK(CB_0) | BG_SCREENBLOCK(SB_0) | BG_PRIORITY(0); | 449 | BG_H_SCROLL_0 = 0; |
449 | BG_CTRL(1) = BG_CHARBLOCK(CB_1) | BG_SCREENBLOCK(SB_1) | BG_PRIORITY(2); | 450 | BG_H_SCROLL_1 = 240; |
451 | BG_CTRL(0) = BG_CHARBLOCK(CB_0) | BG_SCREENBLOCK(SB_0) | BG_PRIORITY(0) | BG_SIZE(1); | ||
452 | BG_CTRL(1) = BG_CHARBLOCK(CB_1) | BG_SCREENBLOCK(SB_1) | BG_PRIORITY(2) | BG_SIZE(1); | ||
450 | } else { | 453 | } else { |
451 | backbuf = BUF_0; | 454 | backbuf = BUF_0; |
452 | BG_CTRL(0) = BG_CHARBLOCK(CB_0) | BG_SCREENBLOCK(SB_0) | BG_PRIORITY(2); | 455 | BG_H_SCROLL_0 = 240; |
453 | BG_CTRL(1) = BG_CHARBLOCK(CB_1) | BG_SCREENBLOCK(SB_1) | BG_PRIORITY(0); | 456 | BG_H_SCROLL_1 = 0; |
457 | BG_CTRL(0) = BG_CHARBLOCK(CB_0) | BG_SCREENBLOCK(SB_0) | BG_PRIORITY(2) | BG_SIZE(1); | ||
458 | BG_CTRL(1) = BG_CHARBLOCK(CB_1) | BG_SCREENBLOCK(SB_1) | BG_PRIORITY(0) | BG_SIZE(1); | ||
454 | } | 459 | } |
455 | // TODO: Copying all tiles for now. Study if it's better to use dirty_tiles | 460 | // TODO: Copying all tiles for now. Study if it's better to use dirty_tiles |
456 | // or dirty_lines. | 461 | // or dirty_lines. |
@@ -616,7 +621,7 @@ txt_drawc(char c, size_t x, size_t y, u8 clr) { | |||
616 | void | 621 | void |
617 | renderer_init(void) { | 622 | renderer_init(void) { |
618 | // Initialize display mode and bg palette. | 623 | // Initialize display mode and bg palette. |
619 | DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_BG_1 | DISP_BG_2 | DISP_OBJ; | 624 | DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_BG_1; |
620 | // DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_BG_1 | DISP_OBJ; | 625 | // DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_BG_1 | DISP_OBJ; |
621 | // TODO: black/grey background to block font/back buffers? | 626 | // TODO: black/grey background to block font/back buffers? |
622 | 627 | ||
@@ -624,34 +629,38 @@ renderer_init(void) { | |||
624 | dma_fill(MEM_VRAM, 0, KB(96), 3); | 629 | dma_fill(MEM_VRAM, 0, KB(96), 3); |
625 | 630 | ||
626 | // Initialize backgrounds. | 631 | // Initialize backgrounds. |
627 | BG_CTRL(0) = BG_CHARBLOCK(CB_0) | BG_SCREENBLOCK(SB_0) | BG_PRIORITY(0); | 632 | BG_CTRL(0) = BG_CHARBLOCK(CB_0) | BG_SCREENBLOCK(SB_0) | BG_PRIORITY(0) | BG_SIZE(1); |
628 | BG_CTRL(2) = BG_CHARBLOCK(CB_2) | BG_SCREENBLOCK(SB_2) | BG_PRIORITY(1); | 633 | BG_CTRL(1) = BG_CHARBLOCK(CB_1) | BG_SCREENBLOCK(SB_1) | BG_PRIORITY(1) | BG_SIZE(1); |
629 | BG_CTRL(1) = BG_CHARBLOCK(CB_1) | BG_SCREENBLOCK(SB_1) | BG_PRIORITY(2); | ||
630 | 634 | ||
631 | // Initialize background memory map for frontbuffer. | 635 | // Initialize background memory map for frontbuffer. |
632 | // for (size_t i = 0; i < 32 * 20; ++i) { | 636 | // for (size_t i = 0; i < 32 * 20; ++i) { |
633 | // TILE_MAP[i] = i; | 637 | // TILE_MAP[i] = i; |
634 | // } | 638 | // } |
635 | u16 *mem_map_fg = SCREENBLOCK_MEM[SB_0]; | 639 | u16 *mem_map_fg = SCREENBLOCK_MEM[SB_0]; |
640 | u16 *mem_map_fg_blank = SCREENBLOCK_MEM[SB_0 + 1]; | ||
636 | u16 *mem_map_bg = SCREENBLOCK_MEM[SB_1]; | 641 | u16 *mem_map_bg = SCREENBLOCK_MEM[SB_1]; |
637 | u16 *mem_map_clr = SCREENBLOCK_MEM[SB_2]; | 642 | u16 *mem_map_bg_blank = SCREENBLOCK_MEM[SB_1 + 1]; |
638 | size_t k = 0; | 643 | for (size_t i = 0; i < 32 * 20; ++i) { |
639 | for (size_t i = 0; i < 32 * 20; ++i, ++k) { | 644 | mem_map_fg[i] = i; |
640 | mem_map_fg[i] = k; | 645 | mem_map_fg_blank[i] = 32 * 20 - 1; |
641 | mem_map_bg[i] = k + 32 * 4; | 646 | mem_map_bg[i] = i + 32 * 4; |
642 | mem_map_clr[i] = 0; | 647 | mem_map_bg_blank[i] = 32 * 20 - 1; |
643 | } | 648 | } |
644 | 649 | ||
650 | // Setup initial background state. | ||
651 | BG_H_SCROLL_0 = 240; | ||
652 | BG_H_SCROLL_1 = 0; | ||
653 | |||
645 | // FIXME: clean this up. | 654 | // FIXME: clean this up. |
646 | u32 *clr_tile =((u32*)(MEM_VRAM) + 0xC000 / 4); | 655 | // u32 *clr_tile =((u32*)(MEM_VRAM) + 0xC000 / 4); |
647 | clr_tile[0] = 0x22222222; | 656 | // clr_tile[0] = 0x22222222; |
648 | clr_tile[1] = 0x22222222; | 657 | // clr_tile[1] = 0x22222222; |
649 | clr_tile[2] = 0x22222222; | 658 | // clr_tile[2] = 0x22222222; |
650 | clr_tile[3] = 0x22222222; | 659 | // clr_tile[3] = 0x22222222; |
651 | clr_tile[4] = 0x22222222; | 660 | // clr_tile[4] = 0x22222222; |
652 | clr_tile[5] = 0x22222222; | 661 | // clr_tile[5] = 0x22222222; |
653 | clr_tile[6] = 0x22222222; | 662 | // clr_tile[6] = 0x22222222; |
654 | clr_tile[7] = 0x22222222; | 663 | // clr_tile[7] = 0x22222222; |
655 | 664 | ||
656 | // Initialize default palette. | 665 | // Initialize default palette. |
657 | PAL_BUFFER_BG[0] = COLOR_BLACK; | 666 | PAL_BUFFER_BG[0] = COLOR_BLACK; |