2020/c64/screen.asm
2020-12-09 03:17:54 +01:00

182 lines
2.6 KiB
NASM

// vim: filetype=kickass
//
// clear_screen
//
// Destroys: a, y
//
clear_screen:
ldy #0
!loop:
lda #' '
sta SCREEN_RAM, y
sta SCREEN_RAM + $0100, y
sta SCREEN_RAM + $0200, y
sta SCREEN_RAM + $0300, y
sta SCREEN_RAM + $03e7, y
lda #1
sta COLOR_RAM, y
sta COLOR_RAM + $0100, y
sta COLOR_RAM + $0200, y
sta COLOR_RAM + $0300, y
sta COLOR_RAM + $03e7, y
iny
bne !loop-
lda #<SCREEN_RAM
sta cursor_pointer_lo
lda #>SCREEN_RAM
sta cursor_pointer_hi
lda #23
sta $d018
rts
//
// print_carriage_return
//
// Input:
// - cursor_pointer_{lo,hi} - Cursor pointer.
//
// Destroys: a, zp_temp
//
print_newline:
sec
lda $05
adc #40
sta cursor_pointer_lo
bcc !done+
inc cursor_pointer_hi
!done:
rts
//
// print_carriage_return
//
// Input:
// - cursor_pointer_{lo,hi} - Cursor pointer.
//
// Destroys: a, zp_temp
//
print_carriage_return:
// Subtract SCREEN_RAM from cursor pointer
sec
lda cursor_pointer_hi
sbc #>SCREEN_RAM
sta cursor_pointer_hi
// Divide the cursor position by 40 (one row)
i16_div8(cursor_pointer_lo, cursor_pointer_hi)
// At this point, the pointer fits in a 8-bit register
lda cursor_pointer_lo
i8_div5_a()
// Multiply it by 40 again
i8_mul5_a()
sta cursor_pointer_lo
i16_mul8(cursor_pointer_lo, cursor_pointer_hi)
// Add SCREEN_RAM back
clc
lda cursor_pointer_hi
adc #>SCREEN_RAM
sta cursor_pointer_hi
rts
//
// write_string
//
// Input:
// - $03-$04 - Pointer to null-terminated string.
// - $05-$06 - Cursor pointer.
//
// Destroys: a, x, y
//
write_string:
ldx #0
ldy #0
!loop:
lda ($03), y
beq !done+
cmp #'\r'
beq !carriage_return+
cmp #'\n'
beq !newline+
sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi)
iny
jmp !loop-
!newline:
jsr print_newline
!carriage_return:
jsr print_carriage_return
iny
jmp !loop-
!done:
rts
print_hex_0x:
sta zp_temp
ldx #0
lda #'0'
sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi)
lda #'x'
sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi)
lda zp_temp
// fallthrough
print_hex:
ldx #0
sta zp_temp
lsr
lsr
lsr
lsr
tay
lda !hex_chars+, y
sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi)
lda zp_temp
and #$0f
tay
lda !hex_chars+, y
sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi)
rts
!hex_chars:
.text "0123456789ABCDEF"