From b5cef6eb69f75a0bf3a80438b28c2c6c7c34f5d6 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sun, 12 Jan 2020 20:34:42 +0100 Subject: [PATCH] [Maintenance] Define VRAM access macros for arbitrary bit counts Templates would have been nicer, but as soon as you add just one non-immediate parameter, Turbo C++ generates a useless store to a new local variable, ruining the generated code. Part of P0069, funded by [Anonymous] and Yanga. --- ReC98.h | 25 +++++++++++++++++++++++-- th01/grps2xsc.c | 5 +---- th01/hardware/egccopyr.c | 4 ++-- th02/op_02_1.c | 12 ++---------- th02/op_05.c | 4 +--- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/ReC98.h b/ReC98.h index 4578d984..16b493d5 100644 --- a/ReC98.h +++ b/ReC98.h @@ -103,12 +103,33 @@ extern planar8_t *VRAM_PLANE_G; extern planar8_t *VRAM_PLANE_R; extern planar8_t *VRAM_PLANE_E; +#define VRAM_OFFSET(x, y) ((x) >> 3) + (y << 6) + (y << 4) + +#define VRAM_CHUNK(plane, offset, bit_count) \ + *(planar##bit_count##_t *)(VRAM_PLANE_##plane + offset) + +#define VRAM_SNAP(dst, plane, offset, bit_count) \ + dst = VRAM_CHUNK(plane, offset, bit_count); + +#define VRAM_SNAP_4(dst, offset, bit_count) \ + VRAM_SNAP(dst.B, B, offset, bit_count); \ + VRAM_SNAP(dst.R, R, offset, bit_count); \ + VRAM_SNAP(dst.G, G, offset, bit_count); \ + VRAM_SNAP(dst.E, E, offset, bit_count); + +#define VRAM_PUT(plane, offset, src, bit_count) \ + VRAM_CHUNK(plane, offset, bit_count) = src; + +#define VRAM_PUT_4(offset, src, bit_count) \ + VRAM_PUT(B, offset, src.B, bit_count); \ + VRAM_PUT(R, offset, src.R, bit_count); \ + VRAM_PUT(G, offset, src.G, bit_count); \ + VRAM_PUT(E, offset, src.E, bit_count); + #define PLANE_DWORD_BLIT(dst, src) \ for(p = 0; p < PLANE_SIZE; p += (int)sizeof(planar32_t)) { \ *(planar32_t*)((dst) + p) = *(planar32_t*)((src) + p); \ } -#define VRAM_OFFSET(x, y) ((x) >> 3) + (y << 6) + (y << 4) - void pascal vram_planes_set(void); // ----------------- diff --git a/th01/grps2xsc.c b/th01/grps2xsc.c index a2b344b5..f8977c7a 100644 --- a/th01/grps2xsc.c +++ b/th01/grps2xsc.c @@ -32,10 +32,7 @@ void graph_slow_2xscale_rect_1_to_0(int x0, int y0, int x1, int y1, int w1, int for(col16 = 0; col16 < w1 / 16; col16++) { int scale_p; graph_accesspage_func(1); - px16.B = *(planar16_t*)(VRAM_PLANE_B + p1); - px16.R = *(planar16_t*)(VRAM_PLANE_R + p1); - px16.G = *(planar16_t*)(VRAM_PLANE_G + p1); - px16.E = *(planar16_t*)(VRAM_PLANE_E + p1); + VRAM_SNAP_4(px16, p1, 16); px16_nonzero = px16.B | px16.R | px16.G | px16.E; for(scale_p = 0; scale_p < ROW_SIZE * 2; scale_p += ROW_SIZE) { planar32_t dst32; diff --git a/th01/hardware/egccopyr.c b/th01/hardware/egccopyr.c index 84bc7922..129dad8c 100644 --- a/th01/hardware/egccopyr.c +++ b/th01/hardware/egccopyr.c @@ -33,8 +33,8 @@ void egc_copy_rect_1_to_0(int x, int y, int w, int h) egc_start_copy(); for(row = 0; row < h; row++) { for(col = x_floor, p = row_p; col < x_end; p += 2, col += 16) { - graph_accesspage_func(1); bits = *(planar16_t*)(VRAM_PLANE_B + p); - graph_accesspage_func(0); *(planar16_t*)(VRAM_PLANE_B + p) = bits; + graph_accesspage_func(1); VRAM_SNAP(bits, B, p, 16); + graph_accesspage_func(0); VRAM_PUT(B, p, bits, 16); } row_p += 640 / 8; } diff --git a/th02/op_02_1.c b/th02/op_02_1.c index c0a91d6b..a5116690 100644 --- a/th02/op_02_1.c +++ b/th02/op_02_1.c @@ -33,16 +33,8 @@ void pascal graph_copy_rect_1_to_0(int x, int y, int w, int h) for(col = 0, p = row_p; col < (w >> 4); col++, p += 2) { if(row_p >= 0) { vram_planar_16_pixels_t p16; - graph_accesspage(1); - p16.B = *(planar16_t*)(VRAM_PLANE_B + p); - p16.R = *(planar16_t*)(VRAM_PLANE_R + p); - p16.G = *(planar16_t*)(VRAM_PLANE_G + p); - p16.E = *(planar16_t*)(VRAM_PLANE_E + p); - graph_accesspage(0); - *(planar16_t*)(VRAM_PLANE_B + p) = p16.B; - *(planar16_t*)(VRAM_PLANE_R + p) = p16.R; - *(planar16_t*)(VRAM_PLANE_G + p) = p16.G; - *(planar16_t*)(VRAM_PLANE_E + p) = p16.E; + graph_accesspage(1); VRAM_SNAP_4(p16, p, 16); + graph_accesspage(0); VRAM_PUT_4(p, p16, 16); } } row_p += ROW_SIZE; diff --git a/th02/op_05.c b/th02/op_05.c index f14a147e..2814fe0c 100644 --- a/th02/op_05.c +++ b/th02/op_05.c @@ -69,9 +69,7 @@ void darken_pic_at(int x, int y) grcg_setcolor(GC_RMW, 0); for(row = 0; row < 144; row++, row_p += 640 / 8) { for(col = 0; col < 192 / 8; col += 2) { - *(planar16_t*)(VRAM_PLANE_B + row_p + col) = - row & 1 ? 0xAAAA : 0x5555 - ; + VRAM_PUT(B, row_p + col, (row & 1 ? 0xAAAA : 0x5555), 16); } } grcg_off();