;##########################################################################
;
;                          *** ct-BASIC Extensions ***
;
; ausgelagerte Routinen für Maus/Renumber etc. im Bereich $B200-$BFFF
;
;##########################################################################



;#####################################################
;###                    RENUMBER                   ###
;#####################################################

; Simple RENUMBER-Routine, ändert keine GOTOs und GOSUBs!

; extern untergebracht im VRAM-Ende ab $BD00


LAB_REN0
	RTS
		
LAB_RENUMBER:
	.IF Mouse
	.ELSE
	
	BCC	LAB_REN1		; branch if next character numeric (RENUMBER n..)
	BEQ	LAB_REN1		; branch if next character [NULL] (RENUMBER)
	CMP	#TK_MINUS		; compare with token for -
	BNE	LAB_REN0		; exit if not - (RENUMBER -m)
					; RENUMBER [[n][-m]]
					; this bit sets the n , if present, as the start and end
LAB_REN1
	PHA
	JSR	LAB_GFPN		; get fixed-point number into temp integer
	JSR	LAB_SSLN		; search BASIC for temp integer line number
					; (pointer in Baslnl/Baslnh)
	PLA
	BNE	LAB_REN2
	STY	Tidx1			
	LDY	#2			; wenn kein Argument, erste Zeile auf 10 setzen
	LDA	#10
	STA	(Baslnl),Y
	INY
	LDA	#0
	STA	(Baslnl),Y
	LDY	Tidx1			; saved Y
LAB_REN2:
	STY	Tidx1			; save Y
	LDY #2
	LDA	(Baslnl),Y
	STA	ptrsrc			; save found line#
	INY
	LDA	(Baslnl),Y
	STA	ptrsrc+1
	LDY	Tidx1			; saved Y
	JSR	LAB_GBYT		; scan memory
	BEQ	LAB_REN3		; branch if no more characters
					; following checks the "-" is present
	CMP	#TK_MINUS		; compare with token for "-"
	BNE	LAB_REN11		; return if not "-" (will be Syntax error)
					; RENUMBER [n]-m
					; the - was there so set m as the end value
	JSR	LAB_IGBY		; increment and scan memory
	JSR	LAB_GFPN		; get fixed-point number into temp integer
	CMP	#$2C			; compare next character with ","
	BNE	LAB_REN3		; no linenumber arg
	LDA	Itempl			; save temp integer
	STA	ptrdest
	LDA	Itemph
	STA	ptrdest+1
	JSR	LAB_IGBY		; increment and scan memory
	JSR	LAB_GFPN		; get fixed-point number into temp integer
	LDA	Itempl
	STA	ptrsrc
	LDA	Itemph
	STA	ptrsrc+1
	LDA	ptrdest			; restore temp integer
	STA	Itempl
	LDA	ptrdest+1
	STA	Itemph
LAB_REN3
	LDA	Itempl			; get temporary integer low byte
	ORA	Itemph			; OR temporary integer high byte
	BNE	LAB_REN5		; branch if start set
	LDA	#$FF			; set for -1
	STA	Itempl			; set temporary integer low byte
	STA	Itemph			; set temporary integer high byte
LAB_REN5
	LDY	#$01			; set index for line
	LDA	(Baslnl),Y		; get next line pointer high byte
					; pointer initially set by search at LAB_REN1
	BEQ	LAB_REN11		; if null all done so exit
	INY				; increment index for line
	LDA	(Baslnl),Y		; get line # low byte
	TAX				; copy to X
	INY				; increment index
	LDA	(Baslnl),Y		; get line # high byte
	CMP	Itemph			; compare with temporary integer high byte
	BNE	LAB_REN6		; branch if no high byte match
	CPX	Itempl			; compare with temporary integer low byte
	BEQ	LAB_REN7		; branch if = last line to do (< will pass next branch)
LAB_REN6				; else ..
	BCS	LAB_REN11		; if greater all done so exit RTS
LAB_REN7
	JSR LAB_RENADD
LAB_REN9
	INY				; increment index
	LDA	(Baslnl),Y		; get next byte
	BNE	LAB_REN9		; branch if not [EOL] (go print character)
	TAY				; else clear index
	LDA	(Baslnl),Y		; get next line pointer low byte
	TAX				; copy to X
	INY				; increment index
	LDA	(Baslnl),Y		; get next line pointer high byte
	STX	Baslnl			; set pointer to line low byte
	STA	Baslnh			; set pointer to line high byte
	BNE	LAB_REN5		; go do next line if not [EOT]
					; else ..
LAB_REN11
	RTS

; Neue Zeilennummer speichern, 10 addieren
LAB_RENADD:
	DEY
	LDA	ptrsrc		; get next line # low byte
	STA	(Baslnl),Y
	CLC
	ADC #10			; +10
	STA	ptrsrc
	PHP
	INY
	LDA	ptrsrc+1	; get next line # high byte
	STA	(Baslnl),Y
	PLP
	ADC #0
	STA	ptrsrc+1
	
	.ENDIF	; Mouse
	RTS

	

;#####################################################
;###         Funktionstasten-Erweiterungen         ###
;#####################################################

; Keyword-Zeichenkette in (ptrsrc) ausgeben, in IBuffs speichern bis Zeichen >=$80
	.IF Mouse
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12:
	JMP	GetKeyNone
Terminal:
	RTS
	.ELSE
	
PutBuf:	
	LDY	#0
	LDX	xsave		; geretteter IBuffs-Pointer
PutBuf_lp:
	CMP	#0	
	BMI	PutBuf_exit	; TK_TOKEN erreicht?
	STA	Ibuffs,X	; in Buffer abgelegen
	JSR	WriteAcc
	LDA	(ptrsrc),Y
	INX
	INY
	JMP	PutBuf_lp
PutBuf_exit:
	STX	xsave		; neuer Pointer auf Ibuffs-Position
	JMP	GetKeyNone

;Funktionstasten
F1:	LDA	#<LBB_LIST
	STA	ptrsrc
	LDA	#>LBB_LIST
	STA	ptrsrc+1
	LDA	#'L'		; erster Buchstabe
	BNE	PutBuf


F2:
	LDA	#<LBB_PRINT
	STA	ptrsrc
	LDA	#>LBB_PRINT
	STA	ptrsrc+1
	LDA	#'P'		;
	BNE	PutBuf

F3:
	LDA	#<LBB_RENUMBER
	STA	ptrsrc
	LDA	#>LBB_RENUMBER
	STA	ptrsrc+1
	LDA	#'R';
	BNE	PutBuf
F4:
	LDA	#<LBB_PEEK
	STA	ptrsrc
	LDA	#>LBB_PEEK
	STA	ptrsrc+1
	LDA	#'P';
	BNE	PutBuf
	
F5:	
	JMP	LAB_CardDir
F6:	LDX	#0
	JMP	LAB_BANK_0
F7:	LDX	#1
	JMP	LAB_BANK_0
F8:
	JMP	Terminal

; BREAK in Monitor
F9:	JMP	MonitorBoot
	
F10:	; LABBUS 0
	LDA 	#0
	BEQ 	SetCOMCH
F11:
	LDA 	#0
	STA	UART_BAUD
	STA	UART_SEL	; Multiplexer für ser. Schnittstelle
	LDA 	#1
	BNE 	SetCOMCH
F12:
	LDA 	#0
	STA	UART_BAUD
	LDA 	#1
	STA	UART_SEL	; Multiplexer für ser. Schnittstelle
	LDA 	#2
SetCOMCH:
	STA 	COMCH
	JSR 	ComStatus
	JMP	GetKeyNone
	
	
	
;#####################################################
;###          einfaches Terminalprogramm           ###
;#####################################################
; erneuert am 15.12.09
PromptFlag = Cmdptr

Terminal:
	LDA 	#0	; Keyboard und Screen EIN
	STA 	Omode	; kein LABOUT
	STA 	Imode	; kein READ
	LDA	#$20	; Kleinbuchstaben nach CapsToggle
	STA	CapsFlg
	JSR	CapsToggle
	LDA 	#6	; gelb
	STA	PromptFlag	; Flag für Prompt
	STA 	Color
	JSR 	LAB_FlushRx
	JSR	WriteMsg
	.DB	$0D,$0A,"Term 1.02, <ESC> quits",$0D,$0A
	NOP
TermScan:
	LDY	TimeOut
	BNE	TermScan_1
	LDA 	#6
	STA 	Color
	LDY	#10
	STY	TimeOut
	LDA	PromptFlag
	BEQ	TermScan_1
	LDA	#0
	STA	PromptFlag
	LDA	#'>'
	JSR	WriteScreen
TermScan_1:
	JSR 	ReceiveByte
	BCC 	TermScan_0	; nix da
	JSR 	WriteScreen
	CMP 	#$0D	; CR? LF anfügen
	BNE 	TermScan_0
	LDA 	#$0A
	JSR 	WriteScreen
	LDY	#10
	STY	TimeOut
TermScan_0:
	JSR 	GetKey
	BEQ 	TermScan
	BMI	TermScan	; Sondertasten
	CMP 	#$1B	; ESC
	BEQ 	TermEnd
	JSR	SendByte
	JSR	WriteScreen	; Local Echo
	CMP	#$0D	; CR? LF anfügen
	BNE	TermScan
	LDA	#$0A
	JSR	WriteScreen
	LDY	#10
	STY	TimeOut
	STY	PromptFlag
TermScanEnd:
	JMP 	TermScan
TermEnd:
	LDA 	#2	; wieder grün
	STA 	Color
	LDA	#$00	; Großbuchstaben in BASIC
	STA	CapsFlg
	JSR	CapsToggle
	LDY	#0	; wird möglicherweise Ibuffs-Pointer
	JMP	0

	.ENDIF	; not Mouse

