; Scrolling text console with line wrapping, 30x30 characters. ; Buffers lines for speed. Will work even if PPU doesn't ; support scrolling. ; ** ASCII font must already be in CHR, and mirroring ; must be vertical or single-screen. ; Number of characters of margin on left and right, to avoid ; text getting cut off by common TVs console_margin = 1 console_buf_size = 32 console_width = console_buf_size - (console_margin*2) zp_byte console_pos ram_res console_scroll,1 ram_res console_temp,1 bss_res console_buf,console_buf_size ; Clears then waits for VBL ; Preserved: A, X, Y console_wait_vbl: bit PPUSTATUS : bit PPUSTATUS bpl :- rts ; Initializes console console_init: jsr console_hide sta PPUCTRL ; Load palette ldx #$3F stx PPUADDR sta PPUADDR ldx #$0F ; black background stx PPUDATA ldx #$30 ; white text stx PPUDATA stx PPUDATA stx PPUDATA ; Fill nametable with spaces ldx #$20 stx PPUADDR sta PPUADDR txa ldx #240 : sta PPUDATA sta PPUDATA sta PPUDATA sta PPUDATA dex bne :- ; Clear attributes txa ldx #$40 : sta PPUDATA dex bne :- ; In case PPU doesn't support scrolling, start a ; couple of lines down lda #16 sta console_scroll ; FALL THROUGH ; Shows console display ; Preserved: X, Y console_show: pha stx console_temp jsr console_wait_vbl lda #$0A sta PPUMASK jmp console_apply_scroll_ ; Hides console display ; Preserved: X, Y console_hide: jsr console_wait_vbl lda #0 sta PPUMASK rts ; Prints char A to console. Will not appear until a ; newline is printed. ; Preserved: A, X, Y console_print: cmp #10 beq console_newline ; Write to buffer stx console_temp ldx console_pos sta console_buf+console_margin,x dec console_pos bmi :+ ; reached end of line ldx console_temp rts ; Prints new line ; Preserved: A, X, Y console_newline: stx console_temp : pha jsr console_wait_vbl ; Address line in nametable lda console_scroll sta console_pos lda #$08 asl console_pos rol a asl console_pos rol a sta PPUADDR lda console_pos sta PPUADDR ; Copy line ldx #console_buf_size-1 : lda console_buf,x sta PPUDATA dex bpl :- ; Scroll up 8 pixels lda console_scroll cmp #240-8 bcc :+ adc #16-1;+1 for set carry : adc #8 sta console_scroll ; Apply new scroll console_apply_scroll_: lda #0 sta PPUADDR sta PPUADDR sta PPUSCROLL lda console_scroll sta PPUSCROLL ; Start new clear line lda #' ' ldx #console_buf_size-1 : sta console_buf,x dex bpl :- ldx #console_width-1 stx console_pos ldx console_temp pla rts