From 843c6c91c0d108f96d067fee6ece427edcf744c8 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Thu, 21 Jan 2021 04:39:26 +0100 Subject: [PATCH] Bitmap mode - wip --- .../cache/fragment-cache-wdc65c02.asm | 227 ++++++++++++++++++ src/main/kc/include/bitmap-draw.h | 2 +- src/main/kc/lib/bitmap-draw-cx16.c | 84 +++++-- src/main/kc/lib/cx16.c | 19 ++ src/main/kc/lib/veralib.c | 14 ++ .../kc/examples/cx16/bitmap_8bpp_320_x_200.c | 2 +- 6 files changed, 325 insertions(+), 23 deletions(-) diff --git a/src/main/fragment/cache/fragment-cache-wdc65c02.asm b/src/main/fragment/cache/fragment-cache-wdc65c02.asm index b5b99892d..9639e1b2a 100644 --- a/src/main/fragment/cache/fragment-cache-wdc65c02.asm +++ b/src/main/fragment/cache/fragment-cache-wdc65c02.asm @@ -4922,3 +4922,230 @@ sta {z1} lda {z1}+1 eor {z2}+1 sta {z1}+1 +//FRAGMENT vbuz1=vbuz1_ror_2 +lda {z1} +lsr +lsr +sta {z1} +//FRAGMENT vbuz1=vbuz1_ror_4 +lda {z1} +lsr +lsr +lsr +lsr +sta {z1} +//FRAGMENT vbuxx=vbuxx_ror_2 +txa +lsr +lsr +tax +//FRAGMENT vduz1=vduz1_rol_2 +asl {z1} +rol {z1}+1 +rol {z1}+2 +rol {z1}+3 +asl {z1} +rol {z1}+1 +rol {z1}+2 +rol {z1}+3 +//FRAGMENT vduz1=vduz1_rol_3 +ldy #3 +!: +asl {z1} +rol {z1}+1 +rol {z1}+2 +rol {z1}+3 +dey +bne !- +//FRAGMENT vduz1=_dword_vwuz2 +lda {z2} +sta {z1} +lda {z2}+1 +sta {z1}+1 +lda #0 +sta {z1}+2 +sta {z1}+3 +//FRAGMENT vwuz1_neq_0_then_la1 +lda {z1} +ora {z1}+1 +bne {la1} +//FRAGMENT vbuz1_eq_0_then_la1 +lda {z1} +cmp #0 +beq {la1} +//FRAGMENT vduz1=vduz1_plus_vduz2 +lda {z1} +clc +adc {z2} +sta {z1} +lda {z1}+1 +adc {z2}+1 +sta {z1}+1 +lda {z1}+2 +adc {z2}+2 +sta {z1}+2 +lda {z1}+3 +adc {z2}+3 +sta {z1}+3 +//FRAGMENT vwuz1=vwuz1_ror_1 +lsr {z1}+1 +ror {z1} +//FRAGMENT vbuaa_eq_0_then_la1 +cmp #0 +beq {la1} +//FRAGMENT vbuxx_eq_0_then_la1 +cpx #0 +beq {la1} +//FRAGMENT vbuyy_eq_0_then_la1 +cpy #0 +beq {la1} +//FRAGMENT vwuz1=_word_vduz2 +lda {z2} +sta {z1} +lda {z2}+1 +sta {z1}+1 +//FRAGMENT vwuz1=vwuz1_rol_6 +lda {z1}+1 +lsr +sta $ff +lda {z1} +ror +sta {z1}+1 +lda #0 +ror +sta {z1} +lsr $ff +ror {z1}+1 +ror {z1} +//FRAGMENT vwuz1=vwuz1_rol_5 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +//FRAGMENT vwuz1=vwuz1_rol_4 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +//FRAGMENT vwuz1=vwuz1_rol_3 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +//FRAGMENT vwuz1=vwuz1_rol_2 +asl {z1} +rol {z1}+1 +asl {z1} +rol {z1}+1 +//FRAGMENT vwuz1_lt_pwuc1_derefidx_vbuz2_then_la1 +ldy {z2} +lda {z1}+1 +cmp {c1}+1,y +bcc {la1} +bne !+ +lda {z1} +cmp {c1},y +bcc {la1} +!: +//FRAGMENT vwuz1_lt_pwuc1_derefidx_vbuxx_then_la1 +lda {z1}+1 +cmp {c1}+1,x +bcc {la1} +bne !+ +lda {z1} +cmp {c1},x +bcc {la1} +!: +//FRAGMENT vwuz1_lt_pwuc1_derefidx_vbuyy_then_la1 +lda {z1}+1 +cmp {c1}+1,y +bcc {la1} +bne !+ +lda {z1} +cmp {c1},y +bcc {la1} +!: +//FRAGMENT vwuz1_lt_pwuc1_derefidx_vbuaa_then_la1 +tay +lda {z1}+1 +cmp {c1}+1,y +bcc {la1} +bne !+ +lda {z1} +cmp {c1},y +bcc {la1} +!: +//FRAGMENT vbuz1=_deref_pbuc1_minus__deref_pbuc2 +lda {c1} +sec +sbc {c2} +sta {z1} +//FRAGMENT vwuz1=vwuz1_ror_2 +lsr {z1}+1 +ror {z1} +lsr {z1}+1 +ror {z1} +//FRAGMENT vbuaa=_deref_pbuc1_minus__deref_pbuc2 +lda {c1} +sec +sbc {c2} +//FRAGMENT vbuxx=_deref_pbuc1_minus__deref_pbuc2 +lda {c1} +sec +sbc {c2} +tax +//FRAGMENT vbuyy=_deref_pbuc1_minus__deref_pbuc2 +lda {c1} +sec +sbc {c2} +tay +//FRAGMENT vwuz1=_word_vbuaa +sta {z1} +lda #0 +sta {z1}+1 +//FRAGMENT vduz1=vwuz2 +lda {z2} +sta {z1} +lda {z2}+1 +sta {z1}+1 +lda #0 +sta {z1}+2 +sta {z1}+3 +//FRAGMENT vduz1_lt_vduz2_then_la1 +lda {z1}+3 +cmp {z2}+3 +bcc {la1} +bne !+ +lda {z1}+2 +cmp {z2}+2 +bcc {la1} +bne !+ +lda {z1}+1 +cmp {z2}+1 +bcc {la1} +bne !+ +lda {z1} +cmp {z2} +bcc {la1} +!: +//FRAGMENT vduz1=_inc_vduz1 +inc {z1} +bne !+ +inc {z1}+1 +bne !+ +inc {z1}+2 +bne !+ +inc {z1}+3 +!: diff --git a/src/main/kc/include/bitmap-draw.h b/src/main/kc/include/bitmap-draw.h index 198726896..bfbadd85c 100644 --- a/src/main/kc/include/bitmap-draw.h +++ b/src/main/kc/include/bitmap-draw.h @@ -2,7 +2,7 @@ // Currently it can only plot on the first 256 x-positions. // Initialize the bitmap plotter tables for a specific bitmap -void bitmap_init(byte layer, word address); +void bitmap_init(byte layer, dword address); // Clear all graphics on the bitmap void bitmap_clear(); diff --git a/src/main/kc/lib/bitmap-draw-cx16.c b/src/main/kc/lib/bitmap-draw-cx16.c index e4171a070..dd47b2dab 100644 --- a/src/main/kc/lib/bitmap-draw-cx16.c +++ b/src/main/kc/lib/bitmap-draw-cx16.c @@ -3,55 +3,97 @@ #include #include +#include // Tables for the plotter - initialized by calling bitmap_draw_init(); const word bitmap_plot_x[640]; -const word bitmap_plot_y[480]; +const dword bitmap_plot_y[480]; const byte bitmap_plot_bit[640]; -__ma word bitmap_address = 0; +__ma dword bitmap_address = 0; __ma byte bitmap_layer = 0; +const word hdeltas[4] = {0, 80, 40, 20}; +const word vdeltas[4] = {0, 60, 30, 15}; +const byte bitmasks[5] = {0, $80, $C0, $F0, $FF}; + // Initialize the bitmap plotter tables for a specific bitmap -void bitmap_init(byte layer, word address) { +void bitmap_init(byte layer, dword address) { bitmap_address = address; bitmap_layer = layer; byte color_depth = vera_layer_get_color_depth(bitmap_layer); - word vdeltas[4] = {0, 80, 40, 20}; byte hscale = vera_display_get_hscale(); // Returns 1 when 640 and 2 when 320. + byte vscale = vera_display_get_vscale(); // Returns 1 when 480 and 2 when 240. + byte bitmask = bitmasks[color_depth]; - - byte bit = $80; for(word x : 0..639) { - if(color_depth==1) bitmap_plot_x[x] = (x >> 3); - if(color_depth==2) bitmap_plot_x[x] = (x >> 2); - if(color_depth==4) bitmap_plot_x[x] = (x >> 1); - if(color_depth==8) {} - bitmap_plot_bit[x] = bit; - bit >>= 1; - if(bit==0) - bit=$80; + if(color_depth==1) { + bitmap_plot_x[x] = (x >> 3); + bitmap_plot_bit[x] = bitmask; + bitmask >>= 1; + } + if(color_depth==2) { + bitmap_plot_x[x] = (x >> 2); + bitmap_plot_bit[x] = bitmask; + bitmask >>= 2; + } + if(color_depth==4) { + bitmap_plot_x[x] = (x >> 1); + bitmap_plot_bit[x] = bitmask; + bitmask >>= 4; + } + if(color_depth==8) { + bitmap_plot_x[x] = x; + bitmap_plot_bit[x] = bitmask; + } + if(bitmask==0) { + bitmask = bitmasks[color_depth]; + } } // This sets the right delta to skip a whole line based on the scale, depending on the color depth. - word vdelta = vdeltas[hscale]; + word hdelta = hdeltas[hscale]; + if(color_depth==8) hdelta = hdelta << 3; + if(color_depth==4) hdelta = hdelta << 2; + if(color_depth==2) hdelta = hdelta << 1; + // We start at the bitmap address; The plot_y contains the bitmap address embedded so we know where a line starts. - word yoffs = bitmap_address; + dword yoffs = bitmap_address; for(word y : 0..479) { - bitmap_plot_y[y] = yoffs; - yoffs = yoffs + vdelta; + //bitmap_plot_y[y] = yoffs; + yoffs = yoffs + hdelta; } } // Clear all graphics on the bitmap void bitmap_clear() { - memset_vram(0,bitmap_address,0,80*60*8); + byte color_depth = vera_layer_get_color_depth(bitmap_layer); + byte bitmask = bitmasks[color_depth]; + byte hscale = vera_display_get_hscale(); // Returns 1 when 640 and 2 when 320. + byte vscale = vera_display_get_vscale(); // Returns 1 when 480 and 2 when 240. + word count = hdeltas[hscale]; + if(color_depth==8) count = count << 6; + if(color_depth==4) count = count << 5; + if(color_depth==2) count = count << 4; + if(color_depth==1) count = count << 3; + word lines = vera_display_get_height(); + dword address = bitmap_address; + word hdelta = hdeltas[hscale]; + if(color_depth==8) hdelta = hdelta << 3; + if(color_depth==4) hdelta = hdelta << 2; + if(color_depth==2) hdelta = hdelta << 1; + for(word line=0; line(vdest_address); + *VERA_ADDRX_H |= VERA_INC_1; + // Transfer the data + for(dword i = 0; i> 1; + break; + case 3: + height = height >> 2; + break; + } + return height<<1; +} + // --- VERA layer management --- diff --git a/src/test/kc/examples/cx16/bitmap_8bpp_320_x_200.c b/src/test/kc/examples/cx16/bitmap_8bpp_320_x_200.c index bc8146231..c72f4c2d4 100644 --- a/src/test/kc/examples/cx16/bitmap_8bpp_320_x_200.c +++ b/src/test/kc/examples/cx16/bitmap_8bpp_320_x_200.c @@ -24,7 +24,7 @@ void lines() { } void main() { - vera_layer_mode_bitmap(0, (dword)0x04000, 320, 1); + vera_layer_mode_bitmap(0, (dword)0x04000, 640, 1); screenlayer(1); textcolor(RED);