; 10 SYS (5120)

*=$1201

       BYTE    $0E, $12, $0A, $00, $9E, $20, $28,  $35, $31, $32, $30, $29, $00, $00, $00

*=$1400 


start  
;ClearScreen
       LDA #147
       JSR $E742       
       
;Print Helloword
       LDX #$0
cycle  LDA hworld,X
       CMP #0
       BEQ exit
       STA $1000,X
       LDA #2
       STA $9400,X
       INX
       JMP cycle
exit   NOP
;Print Helloword1
       LDX #$0
cycle1 LDA hworld1,X
       CMP #0
       BEQ exit1
       STA $1016,X
       LDA #2
       STA $9416,X
       INX
       JMP cycle1
exit1  NOP
;Print Helloword2
       LDX #$0
cycle2 LDA hworld2,X
       CMP #0
       BEQ exit2
       STA $102c,X
       LDA #2
       STA $942c,X
       INX
       JMP cycle2
exit2  NOP
       LDX #$00
       LDA #02
color23  STA $9400,X
       INX
       cpx #$ff 
       BNE color23

       
; copy char rom to ram
start2 LDX #$D0
cycle22 LDA $8028,X
       STA $1C28,X       
       DEX
       CPX #$FF
       BNE cycle22

       
        LDA #$CF
        STA $9005
;wait for A=left, X=right
waitspace
        jsr sclear
        jsr multclear
        LDA $C5
        CMP #17
        BNE nota  ;a=17 Z=33
        jsr moveu ;N=28  M=36
nota    CMP #33
        BNE notz
        jsr moved
notz    CMP #28
        BNE notn
        jsr movel
notn    CMP #36
        BNE notm
        jsr mover
notm    jsr mult
        jsr sdraw
        JSR wait1
        jmp waitspace
exit2: RTS

;===========================
; Move wait for a while
wait1 ldy #$50
w2      ldx #$FF
w1      dex
        bne w1
        dey
        bne w2
        rts

;===========================
; Move sprite right
mover   INC xpos
        RTS

;===========================
; Move sprite left
movel   DEC xpos
        RTS

;===========================
; Move sprite left
moveu   DEC ypos
        RTS

;===========================
; Move sprite left
moved   INC ypos
        RTS

;===========================
;clearsprite
sclear   ldx #$1F
sclear1  lda #$00        
        sta $1c08,x
        dex
        bpl sclear1
        rts

; Mult
mult    ldy #$10
        ldA ypos
        CLC
        lsr
        LSR
        LSR
        clc
        TAX
        lda #$00
mult2   adc #22
        bcc mult3
        iny
mult3   dex
        bne mult2
        STA $FB
         LDA XPOS
         CLC
         LSR
         LSR
         LSR
        clc
        adc $FB
        bcc mult4
        iny
mult4   sta $fb
        sty $fc
        ldy #$00
        lda ($fb),y
        sta sbackground
        lda #$1
        sta ($fb),y
        iny
        lda ($fb),y
        sta sbackground+2
        lda #$3
        sta ($fb),y
        tya
        adc #21
        tay
        lda ($fb),y
        sta sbackground+1
        lda #$2
        sta ($fb),y
        iny
        lda ($fb),y
        sta sbackground+3
        lda #$4
        sta ($fb),y
        rts

; Multclear
multclear    ldy #$10
        ldA ypos
        CLC
        lsr
        LSR
        LSR
        clc
        TAX
        lda #$00
multc2   adc #22
        bcc multc3
        iny
multc3   dex
        bne multc2
        STA $FB
         LDA XPOS
         CLC
         LSR
         LSR
         LSR
        clc
        adc $FB
        bcc multc4
        iny
multc4   sta $fb
        sty $fc
        ldy #$00
        lda sbackground
        sta ($fb),y
        iny
        lda sbackground+2
        sta ($fb),y
        tya
        adc #21
        tay
        lda sbackground+1
        sta ($fb),y
        iny
        lda sbackground+3
        sta ($fb),y
        rts

;===========================
; draw sprite

; draw left part of the sprite
; A C
; B D
sdraw   lda #$08
        sta $fb ; sdrawpos
        lda #$1c
        sta $fc ; sdrawpos+1
        lda ypos
        and #$07
        pha
        ldx sbackground ; 1  draw background until sprite
        jsr convertchr        
        lda #$00
        sta tc1
        pla
        tax
sdrawl  ;ldy tc1
        ;lda $8008,y
        ldy #$00
        lda ($fd),y
        sta ($fb),Y
        cpx #$00
        beq sdraw2
        ;iny
        inc $fb
        inc $fd
        dex
        inc tc1
        jmp sdrawl       ;=================================  
sdraw2  ;rts
        ldy #$00 ;offset ; 2  draw sprite + background
sdraw5  lda xpos
        and #$07
        tax
        lda $8008,y
sdraw4  cpx #$00 
        beq sdraw3
        lsr
        dex
        jmp sdraw4
sdraw3  ora ($fd),y
        sta ($fb),y
        inc tc1
        iny
        lda tc1              ; fetch ypos within sprite 0-15
        cmp #$08
        bcs sdraw13
        cpy #$08
        bne sdraw5           ;===============================

;sdraw2  ;ldy #$00 ;offset ; 3   draw sprite+background
sdraw13 ;rts
        
        lda #$00
        sta tc1
        ldx sbackground+1 ; draw background until sprite
        jsr convertchr   
sdraw18 lda xpos
        and #$07
        tax
        cpy #$07
        bcs sdraw16
        lda $8008,y
sdraw14 cpx #$00 
        beq sdraw15
        lsr
        dex
        jmp sdraw14
sdraw15 pha
        tya
        sta tc2
        ldy tc1
        pla
        ora ($fd),y
        ldy tc2
        sta ($fb),y
        inc tc1
        iny
        cpy #$07
        bcs sdraw16
        lda tc1              ; fetch ypos within sprite 0-15
        cmp #$08
        bcs sdraw16     ;============================
        jmp sdraw18
        

sdraw16 ;rts          ; 4  draw background until end
        tya
        sta tc2

        lda tc1
        tay
        lda ($fd),y
        ldy tc2
        sta ($fb),Y
        iny
        inc tc1
        lda tc1
        cmp #$08
        bne sdraw16   ;============================
       


      








        

; draw right part of the sprite

        ;ldy #$10
sdraw71 cpy #$00
        beq sdraw91
        inc $fb
        dey
        jmp sdraw71 
sdraw91 lda ypos
        and #$07
        pha
        ldx sbackground+2 ; 1  draw background until sprite
        jsr convertchr        
        lda #$00
        sta tc1
        pla
        tax
sdraw7  ;ldy tc1
        ;lda $8008,y
        ldy #$00
        lda ($fd),y
        sta ($fb),Y
        cpx #$00
        beq sdraw9
        ;iny
        inc $fb
        inc $fd
        dex
        inc tc1
        jmp sdraw7
sdraw9  ;rts             ;===================================================


        ldy #$00 ;offset  2  draw sprite + background
sdraw6  lda #$08
        sec
        sbc xpos
        and #$07
        tax
        cpx #$00 
        beq sdraw10
        lda $8008,y
sdraw8  cpx #$00 
        beq sdraw10
        clc
        rol
        dex
        jmp sdraw8
sdraw10  ora ($fd),y
        sta ($fb),y
        inc tc1
        iny
        lda tc1              ; fetch ypos within sprite 0-15
        cmp #$08
        bcs sdraw17
        cpy #$08
        bne sdraw6           ;===============================

sdraw17 ;rts ;   3 draw sprite+background
        
        lda #$00
        sta tc1
        ldx sbackground+3 ; draw background until sprite
        jsr convertchr   
sdraw19 lda #$08
        sec
        sbc xpos
        and #$07
        tax
        cpx #$00 
        beq sdraw20
        cpy #$07
        bcs sdraw22
        lda $8008,y
sdraw21 cpx #$00 
        beq sdraw20
        clc
        rol
        dex
        jmp sdraw21
sdraw20 pha
        tya
        sta tc2
        ldy tc1
        pla
        ora ($fd),y
        ldy tc2
        sta ($fb),y
        inc tc1
        iny
        cpy #$07
        bcs sdraw22
        lda tc1              ; fetch ypos within sprite 0-15
        cmp #$08
        bcs sdraw22     ;============================
        jmp sdraw19

sdraw22 ;rts          ; 4  draw background until end
        tya
        sta tc2

        lda tc1
        tay
        lda ($fd),y
        ldy tc2
        sta ($fb),Y
        iny
        inc tc1
        lda tc1
        cmp #$08
        bne sdraw22   ;============================
        rts

xpos    byte 50
ypos    byte 12
sbackground byte $20,$20,$20,$20
sdrawpos word $1c18
tc1     byte 0
tc2     byte 0

;hworld text 'ac hello'
hworld text 'hello'
       byte 0
;hworld1 text 'bd hello'
hworld1 text 'hello'
       byte 0
hworld2 text 'hello'
       byte 0


;=================================
; Convert chrs into adressspace
; input in x
; output address in $fd,$fe
convertchr
        php
        pha
        lda #$00
        sta $fd
        lda #$1c
        sta $fe
        lda #$00
        
convertchar1
        clc
        adc #$08
        bcc convertchar2
        inc $fe
convertchar2
        dex
        cpx #$00
        bne convertchar1     
        sta $fd
        pla
        plp        
        rts
;==================================

*=$1C08 ;A
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000

*=$1C10 ;B
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000

*=$1C18 ;C
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000

*=$1C20 ;D
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000
        byte %00000000