ùúùúÿýÍïÿýÍï  ¿ÿýÍïÿýÍïÿýÍïÿýÍïÿýÍïÿýÍï;---------------T

debug	equ	0
brutal	equ	0

	ifne	debug
	lea	cli(pc),a0
	moveq	#cliend-cli,d0
	bra.b	aa
cli	dc.b	'-f -9600 dh3:tmp/soundtracker.000   '
	dc.b	10
cliend	
	even
	endc

	incdir	'include34/'
	include	'exec/types.i'
	include	'exec/exec_lib.i'
	include	'devices/serial.i'
	include	'devices/timer.i'
	include	'libraries/dos.i'
	include	'libraries/dos_lib.i'
	include	'mine.i'

	addwatch	a4,h
	addwatch	a5,h
	addwatch	mybuffer,h
	
aa	move.l	sp,stack

	move.l	a0,a5
	lea	-1(a0,d0),a1
.skipmore	cmp.b	#33,-(a1)
	bcs.b	.skipmore
	addq.l	#1,a1
	sf	(a1)	;null terminated
	move.l	a1,nameend

	move.l	4.w,a6

	lea	dosnm(pc),a1
	jsrlib	OldOpenLibrary
	move.l	d0,dosbas
	beq.w	nodos

	lea	SerialPortName(pc),a1
	bsr.w	CreatePort0
	move.l	d0,SerPort
	beq.w	NoPort

	move.l	d0,a1
	moveq	#ioextser_size,d0
	bsr.w	CreateIO
	move.l	d0,SerIO
	beq.w	NoSerIO

	move.l	d0,a4
		
	moveq	#serf_7wire+serf_shared,d0
	and.b	serdefflags(pc),d0
	move.b	d0,io_serflags(a4)	;7wire, shared

	lea	devname(pc),a0
	moveq	#0,d0		;unit
	move.l	a4,a1
	moveq	#0,d1		;flags
	jsrlib	OpenDevice
	tst.l	d0
	bmi.w	NoDevice

	bsr.w	WorkOnCLI

**************************** Main Program *******************************

	bsr.w	SetParams	;set your device parameters

	tst.b	txmode
	beq.w	Loading
	bpl.b	SendFile

Saving	move.l	filenameptr,a0
	lea	mybuffer,a1
	bsr.w	LoadFile	;d0 length
	tst.l	d0
	beq.w	Exitus

	bsr.w	SendXbytes
sendexit	tst.l	d0
	bne.w	Exitus
	bra.w	allok

SendFile	move.l	filenameptr,a0
	lea	mybuffer,a1
	bsr.w	LoadFile	;d0 length
	cmp.l	#19,d0
	bne.w	Exitus
	bsr.w	BumpNumber
	lea	mybuffer,a2
	tst.b	(a2)+		;0 means header
	bne.w	Exitus
	cmp.b	#4,(a2)		;type 0-3
	bcc.w	Exitus
	lea	11(a2),a2	;skip name
	movem.w	(a2),d3-d5	;length,start,vars
	lea	-10(a2),a2	;skip after type
	move.w	d3,(a2)+	;length
	move.w	d4,(a2)+	;start
	move.w	d5,(a2)+	;vars
	move.l	filenameptr,a0
	lea	1(a2),a1
	bsr.w	LoadFile
	subq.l	#2,d0		;checksum + leader
	bcs.b	Exitus
	ror.w	#8,d3		;get mc68000 word from length
	cmp.w	d0,d3
	bne.b	Exitus
	cmp.b	#255,1(a2)	;0 means data block
	bne.b	Exitus
	move.w	d4,(a2)		;line
	add.l	#9,d0
	lea	-7(a2),a0
	bsr.w	SendBytes
	bra.b	SendExit
	
Loading	moveq	#9,d0
	bsr.w	ReceiveXbytes
	tst.l	d0
	bne.b	Exitus

	lea	mybuffer+1,a1
	move.w	1(a1),d0
	move.b	(a1),d0
	moveq	#9,d7
	add.l	d0,d7
	lea	mybuffer+9,a0

	bsr.w	ReceiveBytes
	tst.l	d0
	bne.b	Exitus

	move.l	filenameptr,a0
	lea	mybuffer,a1
	move.l	d7,d0
	bsr.w	SaveFile
	
allok	clr.l	errornr

*************************** closing device ******************************
	
Exitus	move.l	stack(pc),sp

	move.l	a4,a1
	jsrlib	CloseDevice
	
NoDevice	move.l	serIO,a1
	bsr.w	RemoveIO

NoSerIO	move.l	serport,a1
	bsr.w	RemovePort
noport
	move.l	dosbas,a1
	jsrlib	CloseLibrary

nodos	move.l	errornr(pc),d0
	rts

errornr	dc.l	20
stack	dc.l	0

*************************** Parameter parsing ***************************

WorkOnCLI
.loop	bsr.b	SkipSpaces
	beq.b	Exitus
	cmp.b	#'-',(a5)+
	bne.b	.exit
	move.b	(a5)+,d0
	beq.b	Exitus
	cmp.b	#'r',d0
	bne.b	.notload
	sf	txmode
	bra.b	.loop
.notload	cmp.b	#'f',d0
	bne.b	.notsend
	move.b	#1,txmode
	bra.b	.loop
.notsend	cmp.b	#'s',d0
	bne.b	.notsave
	st	txmode
	bra.b	.loop
.notsave	subq.l	#1,a5
	bsr.b	GetNumber
	beq.b	Exitus
	move.l	d0,baud
	bra.b	.loop

.exit	subq.l	#1,a5
	move.l	a5,filenameptr
	rts

SkipSpaces	moveq	#32,d0
.loop	cmp.b	(a5)+,d0
	beq.b	.loop
testend	tst.b	-(a5)
	rts

getnumber	moveq	#0,d0
	move.l	d0,d1
	moveq	#10,d2
.loop	move.b	(a5)+,d1
	sub.b	#'0',d1
	bcs.B	testend
	cmp.b	d2,d1
	bcc.B	testend
	mulu	d2,d0
	add	d1,d0
	bra.B	.loop

BumpNumber	move.l	nameend,a0
.loop	cmp.b	#'9',-(a0)
	bcs.b	.thisone
	move.b	#'0',(a0)
	bra.b	.loop
.thisone	addq.b	#1,(a0)
	rts

********************** set serial.device parameters *********************

SetParams	move	#sdcmd_setparams,io_command(a4)

	lea	io_CtlChar(a4),a1
	lea	ser_defaults(pc),a0
	moveq	#(serdefslen+3)/4-1,d0
setserdefs	move.l	(a0)+,(a1)+
	dbra	d0,setserdefs

	move.l	a4,a1
	bra.w	DelayedDoIO10
	

********************* get serial.device defaults ************************

;!!!Resets all pending requests!!!

GetDefaults	move	#cmd_reset,io_command(a4)

	move.l	a4,a1
	bsr.w	DelayedDoIO10

	lea	io_CtlChar(a4),a0
	lea	ser_defaults(pc),a1
	moveq	#(serdefslen+3)/4-1,d0
getserdefs	move.l	(a0)+,(a1)+
	dbra	d0,getserdefs
	rts

*************************** send some bytes *****************************

SendXbytes	lea	mybuffer,a0

;a0 = buffer, d0=len, a4 ioreq

SendBytes	pushm	d2-d7/a2-a6
	move.l	a0,a2
	move.l	d0,d3

.loop	move.l	#256,d0
	sub.l	d0,d3
	bhi.b	.again
	add.l	d3,d0
	beq.b	.done
	moveq	#0,d3
.again
	move.l	a2,io_data(a4)
	move.l	d0,io_length(a4)
	add.l	d0,a2
	move.w	#cmd_write,io_command(a4)

	bsr.b	CalcDelay

	move.l	a4,a1
	bsr.w	DelayedDoIO
	tst.l	d0
	beq.b	.loop
.done	popm	d2-d7/a2-a6
	rts

*************************** get some bytes *****************************

ReceiveXbytes	lea	mybuffer,a0

;a0 = buffer, d0=len, a4 ioreq

ReceiveBytes	pushm	d2-d7/a2-a6
	move.l	a0,a2
	move.l	d0,d3

.loop	move.l	#256,d0
	sub.l	d0,d3
	bhi.b	.again
	add.l	d3,d0
	beq.b	.done
	moveq	#0,d3
.again
	move.l	a2,io_data(a4)
	move.l	d0,io_length(a4)
	add.l	d0,a2
	move.w	#cmd_read,io_command(a4)

	bsr.b	CalcDelay

	move.l	a4,a1
	bsr.w	DelayedDoIO
	tst.l	d0
	beq.b	.loop
.done	popm	d2-d7/a2-a6
	rts

********************************************************************

CalcDelay	move.l	baud,d1
	moveq	#15,d2	;ne 10, ale 15, cimz se da 150% doba
	divu	d2,d1
	addq.w	#1,d1	;ceil
	divu	d1,d0
	and.l	#$ffff,d0	;zbav se modula
	addq.w	#8,d0	;minimalne 8 vterin
	moveq	#0,d1
	rts


;CreatePort(pri,name)(d0,a1)

CreatePort0	moveq	#0,d0

CreatePort	pushm	a5-a6

	sub.l	a5,a5
	
	move.l	4.w,a6
	pushm	d0/a1
	moveq	#mp_size,d0
	move.l	#$10001,d1	;public clear
	jsrlib	AllocMem
	popm	d1/a1
	tst.l	d0
	beq.b	.nomem

	move.l	d0,a5
	move.b	#NT_MSGPORT,ln_type(a5)
	move.b	d1,ln_pri(a5)
	move.l	a1,ln_name(a5)

;port is signal cause of clear mem

	moveq	#-1,d0
	jsrlib	AllocSignal
	move.b	d0,mp_sigbit(a5)
	bmi.b	RemovePort|.nosignal
	
	sub.l	a1,a1
	jsrlib	FindTask
	move.l	d0,mp_sigtask(a5)

	move.l	a5,a1
	jsrlib	AddPort

.nomem	move.l	a5,d0
	popm	a5-a6
	rts

;RemovePort(port)(a1)

RemovePort	pushm	a5-a6

	move.l	4.w,a6
	move.l	a1,a5

	jsrlib	RemPort

.replymsgs	move.l	a5,a0
	jsrlib	GetMsg
	tst.l	d0
	beq.b	.nomoremsgs
	move.l	d0,a1
	cmp.l	mn_replyport(a1),a5
	beq.b	.replymsgs
	jsrlib	ReplyMsg
	bra.b	.replymsgs

.nomoremsgs	move.b	mp_sigbit(a5),d0
	jsrlib	FreeSignal

.nosignal	move.l	a5,a1
	moveq	#mp_size,d0
	jsrlib	FreeMem
	
	moveq	#0,d0
	popm	a5-a6
	rts

;CreateIO(size,msgport)(d0,a1)

CreateIO	pushm	a5-a6

	sub.l	a5,a5
	
	move.l	4.w,a6
	pushm	d0/a1
	move.l	#$10001,d1
	jsrlib	AllocMem
	popm	d1/a1
	tst.l	d0
	beq.b	.nomem

	move.l	d0,a5
	move.w	d1,mn_length(a5)
	move.l	a1,mn_replyport(a5)

.nomem	move.l	a5,d0
	popm	a5-a6
	rts

;RemoveIO(IOrequest)(a1)

RemoveIO	push	a6
	move.l	4.w,a6
	moveq	#0,d0
	move.w	mn_length(a1),d0
	jsrlib	FreeMem
	pop	a6
	rts

;DelayedIO(IOrequest,secs,micros)

DelayedDoIO10	moveq	#10,d0
	moveq	#0,d1

DelayedDoIO	pushm	d2-d3/d7/a3-a6
	push	mn_replyport(a1)

	moveq	#-1,d7		;global failure

	pushm	d0-d1/a1
	popm	d2-d3/a3

	move.l	4.w,a6

	lea	TimerPortName(pc),a1
	bsr.w	CreatePort0
	tst.l	d0
	beq.b	.noport

	move.l	d0,a5

	move.l	a5,a1
	moveq	#iotv_size,d0
	bsr.b	CreateIO
	tst.l	d0
	beq.b	.noio
	
	move.l	d0,a4

	lea	TimerDevName(pc),a0
	moveq	#unit_vblank,d0	
	move.l	a4,a1
	moveq	#0,d1
	jsrlib	OpenDevice
	tst.l	d0
	bne.b	.NoDevice

	move	#tr_addrequest,io_command(a4)
	move.l	d2,iotv_time+tv_secs(a4)
	move.l	d3,iotv_time+tv_micro(a4)
	jsrlib	SendIO

	move.l	a5,mn_replyport(a3)
	move.l	a3,a1
	jsrlib	SendIO

	move.l	a5,a0
	jsrlib	WaitPort

	move.l	a5,a0
	jsrlib	GetMsg

	move.l	a4,a1
	cmp.l	a3,d0
	beq.b	.nottimeouted
	move.l	a3,a1
.nottimeouted
	jsrlib	AbortIO		;aborts either timeout or user's IO

	move.l	a5,a0
	jsrlib	WaitPort

	move.l	a5,a0
	jsrlib	GetMsg

	move.b	io_error(a3),d7
	ext	d7
	ext.l	d7

	move.l	a4,a1
	jsrlib	CloseDevice

.nodevice	move.l	a4,a1
	bsr.w	RemoveIO

.noio	move.l	a5,a1
	bsr.w	RemovePort

.noport	move.l	d7,d0
	pop	mn_replyport(a3)
	popm	d2-d3/d7/a3-a6
	rts

************************** File Handling **********************

;d0=len (a0 filename, a1 address) (null=error)

LoadFile	pushm	d2-d7/a2-a6
	move.l	a1,a5
	
	move.l	dosbas,a6

	move.l	a0,d1
	move.l	#mode_oldfile,d2
	jsrlib	Open
	move.l	d0,d7
	beq.b	.fail

	move.l	d7,d1
	move.l	a5,d2
	move.l	#mybufflen,d3
	jsrlib	Read

	move.l	d7,d1
	move.l	d0,d7	;read len
	jsrlib	Close

	move.l	d7,d0
.fail	popm	d2-d7/a2-a6	;null=fail
	rts

;void (a0 filename, d0 len, a1 address)

SaveFile	pushm	d2-d7/a2-a6
	move.l	a1,a5
	move.l	d0,d6
	
	move.l	dosbas,a6

	move.l	a0,d1
	move.l	#mode_newfile,d2
	jsrlib	Open
	move.l	d0,d7
	beq.b	.fail

	move.l	d7,d1
	move.l	a5,d2
	move.l	d6,d3
	jsrlib	Write

	move.l	d7,d1
	jsrlib	Close

.fail	popm	d2-d7/a2-a6
	rts

***************************************************************

FileNamePTR	dc.l	0
nameend	dc.l	0

dosbas	dc.l	0

serio	dc.l	0

serport	dc.l	0

ser_defaults	dc.b	$11		;CtlChar - xON
	dc.b	$13		;xOFF
	dc.b	$0		;INQ
	dc.b	$0		;ACQ
	
	dc.l	$2000		;RBuffLen

	dc.l	0		;extflags mspon,msp (mark-space parity)

Baud	dc.l	19200		;Baud

	dc.l	250000		;BrkTime

	dc.b	0,0,0,0,0,0,0,0		;TermArray

	dc.b	8		;ReadLen

	dc.b	8		;WriteLen

	dc.b	2		;StopBits

serdefflags	dc.b	serf_xdisabled+serf_7wire

					;xdisabled
					;eofmode (for each request)
					;shared (on open too)
					;rad_boogie
					;queuedbrk (for each break)
					;7wire (on open too)
					;party_odd
					;party_on
serdefslen	equ	*-ser_defaults
			
SerialPortName	dc.b	'RS Serial',0

TimerPortName	dc.b	'RS Timer',0

TimerDevName	dc.b	'timer.device',0

	ifeq	brutal
DevName	dc.b	'serial.device',0
	else
DevName	dc.b	'brutal.device',0
	endc

dosnm	dc.b	'dos.library',0

txmode	dc.b	0	;0 read, -1 send, 1 send .000 file

	dc.b	'$VER: Amiga<->ZX'
	ifne	brutal
	dc.b	' Brutal'
	endc
	dc.b	' Receive-Send Tool 0.2 (18.10.94)',13,10
	dc.b	'(C) 1994 Patrik Rak - Raxoft'

	section	buffer,bss
mybuffer	ds.b	65536+9 	;9 is for headder
mybufflen	equ	*-mybuffer
	ds.b	1		;1 is for send file odd address

	>extern	"dh0:work/asm/sources/zx/vast.test",mybuffer
