;**************************************************************************
;**
;** pr_SerFile.asm
;** Copyright (c) 1995,1996 Daniel Kahlin <tlr@stacken.kth.se>
;**
;******


;**************************************************************************
;**
;** DO SRV_LOAD
;**
;** UT: carry=1 (error)
;**
;******
DoSrvLoad:
	jsr	InitSerial
	lda	#$00
	sta	$90
;*** do the command ***
	lda	#TYPE_SERVER
	sta	BUFFER+SRVLDHD_TYPE
	lda	#SUB_SRV_LOAD
	sta	BUFFER+SRVLDHD_SUBTYPE

;*** copy filename ***
	ldy	#0
dsl_lp1:
	lda	($bb),y
	sta	BUFFER+SRVLDHD_FILENAME,y
	iny
	cpy	$b7
	bne	dsl_lp1
	lda	#0
	sta	BUFFER+SRVLDHD_FILENAME,y
	
;*** send command ***
	lda	#<BUFFER
	sta	currzp
	lda	#>BUFFER
	sta	currzp+1
	lda	#15
	sta	channelzp
	lda	#SRVLDHD_SIZEOF
	sta	blocksizezp
	jsr	WriteBlock

;*** wait for response ***
	jsr	WaitOKLong
	bcs	dsl_ex1
	lda	BUFFER+SRVLDRS_START_L
	sta	currzp
	lda	BUFFER+SRVLDRS_START_H
	sta	currzp+1
	lda	BUFFER+SRVLDRS_END_L
	sta	endzp
	lda	BUFFER+SRVLDRS_END_H
	sta	endzp+1

	lda	currzp
	pha
	lda	currzp+1
	pha

;*** send OK ***
	lda	#RESP_OK
	sta	BUFFER+SRVLDRS2_RESPONSE
	lda	currzp
	sta	BUFFER+SRVLDRS2_START_L
	lda	currzp+1
	sta	BUFFER+SRVLDRS2_START_H

	lda	#<BUFFER
	sta	currzp
	lda	#>BUFFER
	sta	currzp+1
	lda	#15
	sta	channelzp
	lda	#SRVLDRS2_SIZEOF
	sta	blocksizezp
	jsr	WriteBlock


	pla
	sta	currzp+1
	pla
	sta	currzp

;*** receive body ***
	jsr	recvbody

	lda	endzp
	sta	$ae
	lda	endzp+1
	sta	$af

;*** send ok ***
	jsr	SendOK


	clc
dsl_ex1:
	php
	bcc dsl_skp3
	lda	#%01000011
	sta	$90
dsl_skp3:
;*** Return safely to earth! ***
	jsr	UninitSerial
	plp
	rts

;**************************************************************************
;**
;** DO SRV_SAVE
;**
;** UT: carry=1 (error)
;**
;** IN: $c1/$c2 startaddress, $ae/$af endaddress
;**
;******
DoSrvSave:
	jsr	InitSerial
	lda	#$00
	sta	$90

;*** do the command ***
	lda	#TYPE_SERVER
	sta	BUFFER+SRVSVHD_TYPE
	lda	#SUB_SRV_SAVE
	sta	BUFFER+SRVSVHD_SUBTYPE

	lda	$c1
	sta	BUFFER+SRVSVHD_START_L
	lda	$c2
	sta	BUFFER+SRVSVHD_START_H
	lda	$ae
	sta	BUFFER+SRVSVHD_END_L
	lda	$af
	sta	BUFFER+SRVSVHD_END_H

;*** copy filename ***
	ldy	#0
dss_lp1:
	lda	($bb),y
	sta	BUFFER+SRVSVHD_FILENAME,y
	iny
	cpy	$b7
	bne	dss_lp1
	lda	#0
	sta	BUFFER+SRVSVHD_FILENAME,y

;*** send command ***
	lda	#<BUFFER
	sta	currzp
	lda	#>BUFFER
	sta	currzp+1
	lda	#15
	sta	channelzp
	lda	#SRVSVHD_SIZEOF
	sta	blocksizezp
	jsr	WriteBlock

;*** wait for response ***
	jsr	WaitOK
	bcs	dss_ex1

;*** sendbody ***
	lda	$c1
	sta	currzp
	lda	$c2
	sta	currzp+1
	lda	$ae
	sta	endzp
	lda	$af
	sta	endzp+1
	jsr	sendbody


;*** wait for response ***
	jsr	WaitOKLong
	bcs	dss_ex1

	clc
dss_ex1:
	php
	bcc dss_skp3
	lda	#%01000011
	sta	$90
dss_skp3:
;*** Return safely to earth! ***
	jsr	UninitSerial
	plp
	rts


;**************************************************************************
;**
;** DO SRV_COMMAND
;**
;******
DoSrvCommand:
	jsr	InitSerial

;*** do the command ***
	lda	#TYPE_SERVER
	sta	BUFFER+SRVCMHD_TYPE
	lda	#SUB_SRV_COMMAND
	sta	BUFFER+SRVCMHD_SUBTYPE

;*** copy filename ***
	ldy	#0
dsc_lp1:
	lda	($bb),y
	sta	BUFFER+SRVLDHD_FILENAME,y
	iny
	cpy	$b7
	bne	dsc_lp1
	lda	#0
	sta	BUFFER+SRVLDHD_FILENAME,y

;*** send command ***
	lda	#<BUFFER
	sta	currzp
	lda	#>BUFFER
	sta	currzp+1
	lda	#15
	sta	channelzp
	lda	#SRVCMHD_SIZEOF
	sta	blocksizezp
	jsr	WriteBlock

;*** wait for response ***
	jsr	WaitOK
	bcs	dsc_ex1

;*** wait for response ***
	jsr	WaitOKLong
	bcs	dsc_ex1

dsc_ex1:
;*** Return safely to earth! ***
	jsr	UninitSerial
	rts


;**************************************************************************
;**
;** DO SRV_READSTRING
;**
;** OUT: Carry=1 -> more pages
;******
dsrs_ex2:
	jmp	dsrs_ex1
DoSrvReadString:
	jsr	InitSerial
;*** prepare clean exit ***
	lda	#0
	sta	returnzp

;*** do the command ***
	lda	#TYPE_SERVER
	sta	BUFFER+SRVRSHD_TYPE
	lda	#SUB_SRV_READSTRING
	sta	BUFFER+SRVRSHD_SUBTYPE
	ldx	#40
	ldy	#25
	stx	BUFFER+SRVRSHD_WIDTH
	sty	BUFFER+SRVRSHD_HEIGHT
;*** send command ***
	lda	#<BUFFER
	sta	currzp
	lda	#>BUFFER
	sta	currzp+1
	lda	#15
	sta	channelzp
	lda	#SRVRSHD_SIZEOF
	sta	blocksizezp
	jsr	WriteBlock

;*** wait for response ***
	jsr	WaitOK
	bcs	dsrs_ex2
	lda	BUFFER+SRVRSRS_ROWS
	beq	dsrs_ex2	;no messages.
	sta	returnzp
	and	#$7f
	sta	rowszp
	jsr	UninitSerialSmall

;*** fix space on screen ***
	ldx	rowszp
	lda	#13
dsrs_lp3:
	jsr	newffd2
	dex
	bne	dsrs_lp3

	lda	$d6
	sec
	sbc	rowszp
	tax
	inx
	ldy	#0
	clc
	jsr	newfff0

;*** calculate pointers ***
	lda	$d1
	sta	currzp
	sta	endzp
	sta	$f3
	lda	$d2
	sta	currzp+1
	sta	endzp+1
	sec
	sbc	$288
	clc
	adc	#$d8
	sta	$f4	

	ldx	rowszp
dsrs_lp2:
	lda	endzp
	clc
	adc	#40
	sta	endzp
	lda	endzp+1
	adc	#0
	sta	endzp+1
	dex
	bne	dsrs_lp2

;** get message **
	jsr	InitSerial
	lda	currzp
	pha
	lda	currzp+1
	pha
	jsr	SendOK
	pla
	sta	currzp+1
	pla
	sta	currzp
	jsr	recvbody


;*** fill up colors ***
	lda	endzp+1
	sec
	sbc	$288
	clc
	adc	#$d8
	sta	endzp+1

	ldy	#0
	lda	$286
dsrs_lp1:
	sta	($f3),y
	inc	$f3
	bne	dsrs_skp1
	inc	$f4
dsrs_skp1:
	ldx	$f3
	cpx	endzp
	bne	dsrs_lp1
	ldx	$f4
	cpx	endzp+1
	bne	dsrs_lp1

;*** fix cursor pos ***
	lda	$d6
	clc
	adc	rowszp
	tax
	dex
	ldy	#0
	clc
	jsr	newfff0

dsrs_ex1:
;*** Return safely to earth! ***
	jsr	UninitSerial
	lda	returnzp
	bmi	dsrs_ex3
	clc
	rts
dsrs_ex3:
	sec
	rts
