1; #LAYOUT# STD *        #TAKE
2; #LAYOUT# *   KERNAL_0 #TAKE
3; #LAYOUT# *   *        #IGNORE
4
5
6screen_grow_logical_line_done_scroll:
7
8	inc TBLX
9
10	; FALLTROUGH
11
12screen_grow_logical_line_done:
13
14	jmp screen_calculate_PNT_USER
15
16
17screen_grow_logical_line_screen_up:
18
19	jsr screen_scroll_up
20
21	; FALLTROUGH
22
23
24screen_grow_logical_line:
25
26	; Do not grow line if previus one is grown
27	ldy TBLX
28	lda LDTBL+0, y
29	bpl screen_grow_logical_line_done_scroll
30
31	; If last line, scroll the screen up
32	cpy nlinesm1
33	beq screen_grow_logical_line_screen_up
34
35	; Do not grow line if already grown
36	lda LDTBL+1, y
37	bpl screen_grow_logical_line_done
38
39	; Scroll LDTBL down (start from the end)
40	ldy nlinesm1
41	dey
42:
43	cpy TBLX
44	beq :+
45	bcc :+
46
47	lda LDTBL+0, y
48	sta LDTBL+1, y
49
50	dey
51	bne :-
52:
53	; Mark current line as grown
54	ldy TBLX
55	lda #$00
56	sta LDTBL+1, y
57
58	; Now we have to scroll lines downwards to make space. We start from the end,
59	; and work backwards. We cannot be as simple and efficient here as we are
60	; for scrolling up, because we do not know how much must be scrolled.
61
62	; Work out how many physical lines to scroll down
63
64	lda nlinesm1
65	clc ; subtract one more
66	sbc TBLX
67	beq screen_grow_logical_line_copy_done       ; branch if no need to copyy
68
69	ldx nlinesm1
70:	jsr screen_set_position
71	dex
72	jsr screen_copy_line
73	cpx TBLX
74	bne :-
75
76screen_grow_logical_line_copy_done:
77
78	; Erase newly inserted line and quit
79
80	ldx TBLX
81	inx
82	jsr screen_clear_line
83	jmp screen_calculate_PNTR_LNMX
84