	;jk-net v0.6
	;copyright by infinity 1993,94


FNLEN	equ	$b7		;file name lenght
FA	equ	$ba		;device number
FNADR	equ	$bb		;name address

IERROR	equ	$0300		;error
ILOAD	equ	$0330		;load
ISAVE	equ	$0332		;save

CHKIN	equ	$ffc6		;open for input
CHKOUT	equ	$ffc9		;open for output 
CHRIN	equ	$ffcf		;get char from input
CHROUT	equ	$ffd2		;put char to output

org_ILOAD	equ	$f4a5		;orginal load
org_ISAVE	equ	$f5ed		;orginal save

searc_m	equ	$f5af		;print searching for 'name' message
load_m	equ	$f5d2		;print loading message
save_m	equ	$f68f		;print saving 'name' message
print_str	equ	$ab1e		;print string ending null

e_fnf	equ	$04		;FILE NOT FOUND
e_mfn	equ	$08		;MISSING FILE NAME

addr	equ	$fc		;start address
len	equ	$fe		;lenght and end address

LOAD_code	equ	$01
SAVE_code	equ	$02
DIR_code	equ	$03
QUIT_code	equ	$04



*	equ	52780

; main --------------------------------

	lda #<load_main		;install jk-net
	sta ILOAD
	lda #>load_main
	sta ILOAD+1
	lda #<save_main
	sta ISAVE
	lda #>save_main
	sta ISAVE+1

	jsr init_port		;init_port

	lda #<m_cr
	ldy #>m_cr
	jmp print_str		;print copyright

; chk name ----------------------------

chk_name	lda FNLEN			;check file name lenght
	bne not_0			;null ?,no -> not_0
	pla
	pla
	ldx #e_mfn		;missign file name
	jmp (IERROR)		;error
not_0	cmp #17
	bcc not_16		;over 16 ?, no -> not_16
	lda #16
	sta FNLEN
not_16
	rts

; send name ---------------------------

send_name	ldy #0			;send name to amiga
send_loop	lda (FNADR),y
	jsr put_char
	iny
	cpy FNLEN
	bne send_loop
	lda #32
c_s_name
	cpy #16
	beq name_sent
	jsr put_char
	iny
	jmp c_s_name
name_sent
	rts

; save main ---------------------------

save_main	pha			;push accu
	lda FA			;device number
	cmp #7
	beq s_yes_7		;7 ?,yes -> s_yes_7
	pla
	jmp org_ISAVE		;orginal save

s_yes_7	sei			;no interrupts
	jsr chk_name		;check name lenght
	jsr save_m		;print saving 'name' message
	jsr port_put		;port output
	lda #SAVE_code		;SAVE_code to amiga
	jsr put_char
	jsr send_name		;send name to amiga

	lda 43			;start address
	sta addr
	lda 44
	sta addr+1

	sec			;calculate lenght
	lda 45
	sbc addr
	sta len
	lda 46
	sbc addr+1
	sta len+1

	lda len+1			;send lenght !UPPER! byte 
	jsr put_char
	lda len			;send lenght !LOWER! byte
	jsr put_char

	lda addr			;send address
	jsr put_char
	lda addr+1
	jsr put_char

	lda #11			;blanck screen
	sta 53265
	lda #$36			;basic ROM off
	sta $01

	ldy #$00
save_loop
	lda (addr),y		;get char from memory
	jsr put_char		;send it to amiga

	inc $d020
	dec $d020

	inc addr			;increment pointer lower byte
	bne s_chk_len		;null ?, no -> s_chk_len
	inc addr+1		;and upper byte
s_chk_len
	lda addr+1
	cmp 46
	bne save_loop
	lda addr
	cmp 45
	bne save_loop

	lda #$37			;basic ROM on
	sta $01
	lda #27			;screen to normal
	sta 53265

	cli
	jmp $a474			;back to basic

; load main ---------------------------

load_main	pha			;push accu
	lda FA			;get device number
	cmp #7
	beq yes_7			;7 ?,yes -> yes_7
	pla			;pull accu
	jmp org_ILOAD		;not 7 -> orginal load

yes_7	jsr chk_name		;check name lenght
	ldy #0
	lda (FNADR),y		;get first char of name
	cmp #36			;if it '$'
	bne load			;no -> load
	jmp load_dir		;yes -> load_dir

; load --------------------------------

load	sei			;no interrupts
	jsr port_put		;port output
	lda #LOAD_code
	jsr put_char		;send LOAD_code to amiga
	jsr send_name		;send name to amiga
	jsr searc_m		;print searching for 'name' message
	jsr port_get		;port input
	jsr get_char		;get lower lenght
	sta len			;store it
	jsr get_char		;get upper lenght
	sta len+1			;store it

	lda len			;check lenght
	bne len_not_0
	lda len+1
	bne len_not_0		;if not null -> len_not_0

	ldx #e_fnf		;error file not found
	cli
	jmp (IERROR)		;print it
len_not_0
	jsr load_m		;print loading message
	jsr get_char		;get lower address
	sta addr			;store it
	jsr get_char		;get upper address
	sta addr+1		;store it

	clc
	lda addr			;calculate end address
	adc len
	sta len
	lda addr+1
	adc len+1
	sta len+1

	lda #11			;blanck screen
	sta 53265
	ldy #$00
load_loop
	jsr get_char		;get char from amiga
	sta (addr),y		;store it to memory

	inc $d020
	dec $d020

	inc addr			;increment lower byte
	bne chk_len		;null ?, no -> chk_len
	inc addr+1
chk_len
	lda addr+1
	cmp len+1
	bne load_loop
	lda addr
	cmp len
	bne load_loop

	lda #27			;screen to normal
	sta 53265

	lda len			;end address
	sta 45
	lda len+1
	sta 46
	jsr 42291

	cli
	jmp $a474			;back to basic

; port put ----------------------------

port_put	lda #$ff			;port output
	sta $dd03
	lda $dd0d
	rts

; port get ----------------------------

port_get	lda #$00			;port input
	sta $dd03
	rts

; init port ---------------------------

init_port	lda $dd02			;init port
	ora #$04
	sta $dd02
	lda $dd00
	and #$fb
	sta $dd00
	rts

;put char -----------------------------

put_char	sta $dd01			;send char
put_wait	lda #$10
	bit $dd0d			;clear to send next char ?
	beq put_wait
	rts

; get char ----------------------------

get_char	lda #$10
	bit $dd0d			;wait for char
	beq get_char
	lda $dd01			;get it
	rts

; load dir ----------------------------

load_dir	jsr port_put		;port output
	lda #DIR_code		;directory code
	jsr put_char
	jsr port_get		;port input
dir_loop	jsr get_char		;get char
	beq dir_end		;end ?, yes -> dir_end
	jsr CHROUT		;print it
	jmp dir_loop
dir_end	cli
	jmp $a474			;back to basic

; data --------------------------------

m_cr	db	'JK-NET V0.6 (C) INFINITY 1993,94',13,0
	db	0
