;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
;; petscii: https://www.c64-wiki.com/wiki/File:ASCII-Codes.gif
;;https://www.c64-wiki.com/wiki/Character_set
;; Takes in a PETSCII-code in CODE
;; And prints it ON X_pos, Y_pos
;; Note that this is the real positions divided by 8
.proc char_draw; user-procedure :clobbers (A X Y) :clobbers-arguments 4
        .include "char.inc"

;;Do arithimatic to know where to read and write bytes.
calculate_petski_position:
        ;We first need to clear some memory
        LDA #$00
        STA petski_position + 1

        ;;We need the relative offset for bytes to read and write.
        ;; This is code *8 because 8byte is one character
        ;; *8 = 2*2*2
        ASL code ;Will never owerflow, therefore 8byte
        Mult_16 petski_position, petski_position + 1
        Mult_16 petski_position, petski_position + 1
        ;; Add starting position
        Add_16 petski_position, petski_position + 1, #<Character_generator_ROM , #>Character_generator_ROM, !

calculate_screen_position:
        ;; Therefore we have that: btp_mem_pos = big_x_offset + smal_y_offset + big_y_offset    + bitmap_offset

	;; We use a lookup-table for big_y_offset + bitmap_offset (see END.s)
        LDA Y_pos
	LSR A
	LSR A
	;; We need to remove the last digit if its still there
	;; same as LSR ASR
	AND #%11111110
	TAX
	LDA big_y_offset, X
	STA screen_position
	INX
	LDA big_y_offset, X
	STA screen_position + 1

	;; Y = big_x_offset
	LDA #%11111000
	AND X_pos
	STA X_pos
	;; Y += smal_y_offset
	LDA #%00000111 ; A = y (mod 8)
	AND Y_pos ;; offset to add
	CLC
	ADC X_pos
	Add_16 screen_position, screen_position + 1, A, #$00, !

move_data:
        ;; One character is 8 byte, move these bytes to screen
        LDY #$07
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$06
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$05
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$04
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$03
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$02
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$01
        LDA (petski_position), Y
        STA (screen_position), Y
        DEY ;$00
        LDA (petski_position), Y
        STA (screen_position), Y
        RTS
.endproc