init_vic:
;BANK
;$DD00 = %xxxxxx11 -> bank0: $0000-$3fff
;$DD00 = %xxxxxx10 -> bank1: $4000-$7fff
;$DD00 = %xxxxxx01 -> bank2: $8000-$bfff
;$DD00 = %xxxxxx00 -> bank3: $c000-$ffff

;CHARMEM
;$D018 = %xxxx000x -> charmem is at $0000
;$D018 = %xxxx001x -> charmem is at $0800
;$D018 = %xxxx010x -> charmem is at $1000
;$D018 = %xxxx011x -> charmem is at $1800
;$D018 = %xxxx100x -> charmem is at $2000
;$D018 = %xxxx101x -> charmem is at $2800
;$D018 = %xxxx110x -> charmem is at $3000
;$D018 = %xxxx111x -> charmem is at $3800

;SCREENMEM
;$D018 = %0000xxxx -> screenmem is at $0000
;$D018 = %0001xxxx -> screenmem is at $0400
;$D018 = %0010xxxx -> screenmem is at $0800
;$D018 = %0011xxxx -> screenmem is at $0c00
;$D018 = %0100xxxx -> screenmem is at $1000
;$D018 = %0101xxxx -> screenmem is at $1400
;$D018 = %0110xxxx -> screenmem is at $1800
;$D018 = %0111xxxx -> screenmem is at $1c00
;$D018 = %1000xxxx -> screenmem is at $2000
;$D018 = %1001xxxx -> screenmem is at $2400
;$D018 = %1010xxxx -> screenmem is at $2800
;$D018 = %1011xxxx -> screenmem is at $2c00
;$D018 = %1100xxxx -> screenmem is at $3000
;$D018 = %1101xxxx -> screenmem is at $3400
;$D018 = %1110xxxx -> screenmem is at $3800
;$D018 = %1111xxxx -> screenmem is at $3c00

  ;Move the VIC 16K block
;  lda $dd00
;  and #%11111100
;  ora #%00000010 ;ora #%000000xx ;<- your desired VIC bank value, see above
;  sta $dd00

  ;Move the VIC address register
  ;lda #%11111110 
  ;lda #%00000000 
  lda #%00100010
  sta $D018
  
  lda #$00
  sta $d020
  
  ;;set ECM background colors
  ;lda #$07 ;yellow
  ;sta $d021
  ;lda #$02 ;red
  ;sta $d022
  ;lda #$06 ;blue
  ;sta $d023
  ;lda #$03 ;cyan
  ;sta $d024    
  
  ;Enable ECM
  lda #$08
  sta $d016
switch_off_screen:    
  lda #$7B 
  sta $d011
  rts

switch_on_screen:    
  lda #$5b
  sta $D011
  rts

;fill_screen_with_a:
;  ldx #$c8
;-
;  sta screen+(2*40)+(0*$c8)-1,x
;  sta screen+(2*40)+(1*$c8)-1,x
;  sta screen+(2*40)+(2*$c8)-1,x
;  sta screen+(2*40)+(3*$c8)-1,x
;  dex
;  bne -
;  rts
  
shift_colors:

  ldx #$07
  ldy colortable_offset+7
-
  lda colortable_offset-1,x
  sta colortable_offset,x 
  dex
  bne -
  sty colortable_offset 

  ldx #31
  ldy colortable+31
-
  lda colortable-1,x
  sta colortable,x 
  dex
  bne -
  sty colortable 
  
;  ldx shift_table
;  lda colortable,x
;  sta $d800+(2*40)+(0*8),x
;  sta $d800+(2*40)+(4*8),x
;  lda colortable+8,x
;  sta $d800+(2*40)+(1*8),x
;  lda colortable+16,x
;  sta $d800+(2*40)+(2*8),x
;  lda colortable+24,x
;  sta $d800+(2*40)+(3*8),x

  !for ROW = 0 TO 4
    ldx colortable_offset + ROW
    lda colortable,x
    sta $d800+((2+ROW)*40)+(0*8),x
    sta $d800+((2+ROW)*40)+(4*8),x
    sta $d800+((10+ROW)*40)+(3*8),x
    sta $d800+((18+ROW)*40)+(2*8),x
    lda colortable+8,x
    sta $d800+((2+ROW)*40)+(1*8),x
    sta $d800+((10+ROW)*40)+(0*8),x
    sta $d800+((10+ROW)*40)+(4*8),x
    sta $d800+((18+ROW)*40)+(3*8),x
    lda colortable+16,x
    sta $d800+((2+ROW)*40)+(2*8),x
    sta $d800+((10+ROW)*40)+(1*8),x
    sta $d800+((18+ROW)*40)+(0*8),x
    sta $d800+((18+ROW)*40)+(4*8),x
    lda colortable+24,x
    sta $d800+((2+ROW)*40)+(3*8),x
    sta $d800+((10+ROW)*40)+(2*8),x
    sta $d800+((18+ROW)*40)+(1*8),x
  !end  
  !for ROW = 5 TO 7
    ldx colortable_offset + ROW
    lda colortable,x
    sta $d800+((2+ROW)*40)+(0*8),x
    sta $d800+((2+ROW)*40)+(4*8),x
    sta $d800+((10+ROW)*40)+(3*8),x
    lda colortable+8,x
    sta $d800+((2+ROW)*40)+(1*8),x
    sta $d800+((10+ROW)*40)+(0*8),x
    sta $d800+((10+ROW)*40)+(4*8),x
    lda colortable+16,x
    sta $d800+((2+ROW)*40)+(2*8),x
    sta $d800+((10+ROW)*40)+(1*8),x
    lda colortable+24,x
    sta $d800+((2+ROW)*40)+(3*8),x
    sta $d800+((10+ROW)*40)+(2*8),x
  !end  
  rts  

charset_offset:
!byte $ff

hardscroll_screen:
  ; first we "scroll" the charset itself
  ; shift charset one char (8*8 bytes)
  ; so that we copy the last char to "temp"
  ldx #64
-
  lda font-1,x
  sta font+7,x 
  dex
  bne -

  ; copy the last "temp"/not visible char to the beginning (8 bytes)
  ldx #8
-
  lda font+63,x
  sta font-1,x 
  dex
  bne -

  inc charset_offset
  lda charset_offset
  cmp #$08
  bne +
  lda #$00
  sta charset_offset
+
  tax
  
;  lda screen+(2*40),x
;  sta screen+(2*40)+8,x
;  clc
;  adc #64
;  sta screen+((2*40)+16),x
;  clc
;  adc #64
;  sta screen+((2*40)+24),x
;  clc
;  adc #64
;  sta screen+((2*40)+32),x
;  sta screen+((2*40)),x
;
;  dex
;  bpl +
;  ldx #$07
;+    
    lda screenpart,x
    sta screenpart+8,x    
    clc
    adc #64
    sta screenpart+16,x
    clc
    adc #64
    sta screenpart+24,x
    clc
    adc #64
    sta screenpart+32,x
    sta screenpart,x
    dex
    bpl +
    ldx #$07
+
;  !for ROW = 1 TO 20
;    lda screenpart+ROW,x
;    sta screenpart+((ROW*40)),x
;    sta screenpart+((ROW*40)+32),x
;    clc
;    adc #64
;    sta screenpart+(ROW*40)+8,x
;    clc
;    adc #64
;    sta screenpart+((ROW*40)+16),x
;    clc
;    adc #64
;    sta screenpart+((ROW*40)+24),x
;    dex
;    bpl +
;    ldx #$07
;+    
;  !end
; rts   
  jmp hardscroll_screen_speedcode_start 

