!zone hardscroll_screen_codegen
  
.speedcode_start                        = $0200

bitmask_x_char                          = $0200
hardscroll_screen_speedcode_start       = $0300

LDA_MNEMONIC           = $bd
STA_MNEMONIC           = $9d
CLC_MNEMONIC           = $18
ADC_MNEMONIC           = $69
DEX_MNEMONIC           = $ca
BPL_MNEMONIC           = $10
LDX_MNEMONIC           = $a2

RTS_MNEMONIC           = $60

; Create speedcode for hardscroll screen
;
;.C:1107  BD 51 08    LDA $0851,X
;.C:110a  9D 78 08    STA $0878,X
;.C:110d  9D 98 08    STA $0898,X
;.C:1110  18          CLC
;.C:1111  69 40       ADC #$40
;.C:1113  9D 80 08    STA $0880,X
;.C:1116  18          CLC
;.C:1117  69 40       ADC #$40
;.C:1119  9D 88 08    STA $0888,X
;.C:111c  18          CLC
;.C:111d  69 40       ADC #$40
;.C:111f  9D 90 08    STA $0890,X
;.C:1122  CA          DEX
;.C:1123  10 02       BPL $1127
;.C:1125  A2 07       LDX #$07
;;-------------------------------- repeat
;.C:1127  BD 52 08    LDA $0852,X
;.C:112a  9D A0 08    STA $08A0,X
;.C:112d  9D C0 08    STA $08C0,X
;.C:1130  18          CLC
;.C:1131  69 40       ADC #$40
;.C:1133  9D A8 08    STA $08A8,X
;.C:1136  18          CLC
;.C:1137  69 40       ADC #$40
;.C:1139  9D B0 08    STA $08B0,X
;.C:113c  18          CLC
;.C:113d  69 40       ADC #$40
;.C:113f  9D B8 08    STA $08B8,X
;.C:1142  CA          DEX
;.C:1143  10 02       BPL $1147
;.C:1145  A2 07       LDX #$07

;  !for ROW = 1 TO 20
;    lda screenpart+ROW,x
;    sta screenpart+((ROW*40)),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
;    clc
;    adc #64
;    sta screenpart+((ROW*40)+32),x
;    dex
;    bpl +
;    ldx #$07
;+    
;  !end
speedcode_generator:
          ldx #$00
          lda #$80
          clc
-
          sta bitmask_x_char,x
          ror
          bcc +
          ror          
+          
          inx
          bne -        

screencopy_speedcode_generator:
          ldy #$00
--        
          ldx #$00

          jsr .inc_load_screenpart
          
;Generate   lda screenpart+ROW,x
          lda #LDA_MNEMONIC
          jsr .STA_and_INC_speedcode_address

          lda .load_screenpart
          jsr .STA_and_INC_speedcode_address 

          lda .load_screenpart+1
          jsr .STA_and_INC_speedcode_address

          ldx #$00
-          
;Generate   sta screenpart+((ROW*40)),x
          lda #$08
          jsr .add_a_save_screenpart

          lda #STA_MNEMONIC
          jsr .STA_and_INC_speedcode_address

          lda .save_screenpart
          jsr .STA_and_INC_speedcode_address

          lda .save_screenpart+1
          jsr .STA_and_INC_speedcode_address

          cpx #$04 
          beq +
          lda #CLC_MNEMONIC
          jsr .STA_and_INC_speedcode_address
          
          lda #ADC_MNEMONIC
          jsr .STA_and_INC_speedcode_address

          lda #$40
          jsr .STA_and_INC_speedcode_address
          
+         inx
          cpx #$05 
          bne -

          lda #DEX_MNEMONIC          
          jsr .STA_and_INC_speedcode_address

          lda #BPL_MNEMONIC
          jsr .STA_and_INC_speedcode_address

          lda #$02
          jsr .STA_and_INC_speedcode_address
        
          lda #LDX_MNEMONIC
          jsr .STA_and_INC_speedcode_address

          lda #$07
          jsr .STA_and_INC_speedcode_address
          
          iny
        
          cpy #20
          bne --

          lda #RTS_MNEMONIC
          jsr .STA_and_INC_speedcode_address
          
          rts
          
.STA_and_INC_speedcode_address:
.speedcode_address:          
          sta hardscroll_screen_speedcode_start 
          inc .speedcode_address+1
          bne +
          inc .speedcode_address+2
+         
          rts
          
.inc_load_screenpart:
          inc .load_screenpart
          bne +
          inc .load_screenpart+1
+
          rts

.add_a_save_screenpart:
          clc
          adc .save_screenpart    
          sta .save_screenpart    
          bne +
          inc .save_screenpart+1
+
          rts
          
.load_screenpart:         
         !byte <(screenpart), >(screenpart)
         
.save_screenpart:         
         !byte <(screenpart+32), >(screenpart+32)
