; XMDPW6.200 -- 8/21/88
;
; XMDPW5.200 of 12/25/87 with device file support added; macros repaired
;
; by Phil Wheeler -- 71266,125
;
; Based on M-100 XmodemXtend/ Copyright (C) 1985 by J R Chenoweth
;
; Includes 	Splitscreen code by James Yi
;		DIRACC code by Hugo Ferreyra
;		Macro code by Jon Diercks
;		Scroll Lock by Wilson Van Alst
;
; Version for Tandy 200
;
;
;	  	 T200	  M100 
;                ----     ----
;--------------------------------
maxfil	equ	$9be1
null	equ	7 (point to 0)
open	equ	$5bd1
select	equ	$5b7e
readio	equ	$5d36
writio	equ	$5a05
closei	equ	$5bf7
devout	equ	$f73f
setmax	equ	$4c4a
Rflptr	equ	11362
GetLen	equ	11587
FmtFnm	equ	23242
;-----------------------------

CHKF	equ	$6e4f	;$5aab
FADDR	equ	$6e8c	;$5ae3
ABTMSG	EQU	$677E	;$5771
BEEP	EQU	$4F45	;$4229
CALMSG	EQU	$6142	;$5244
CAPTUR	EQU	$F4F0	;$FAC2
CHGET	EQU	$12F7	;$12CB
CHSNS	EQU	$1422	;$13DB
CLSCOM	EQU	$87B5	;$6ECB
CONECT	EQU	$61E7	;$52E4
CONVD	EQU	$470B	;$39D4
CRLF	EQU	$4F3E	;$4222
CUROFF	EQU	$4F6D	;$424E
CURSON	EQU	$4F68	;$4249
DIALER	EQU	$622B	;$532D
DIRRST	EQU	$2C62	;$2146
DISCON	EQU	$61BA	;$52BB
DISMSG	EQU	$6793	;$5786
DWNLDR	EQU	$6619	;$568F
DWNMSG	EQU	$6775	;$5768
DSPFNK	EQU	$4FC7	;$42A8
DUPDSP	EQU	$64BE	;$5544
DUPLEX	EQU	$64B8	;$553E
DUPFLG	EQU	$EF39	;$F658
ECHDSP	EQU	$64D0	;$5556
ECHOTG	EQU	$64CA	;$5550
FILENG	EQU	$2D43	;$21FA
FNDFL0	EQU	$6E4A	;$5AA6
FNTFND	EQU	$6789	;$577C
FILES	EQU	$2A2A	;$1F3A
FNDTBL	EQU	$7133	;$5D2B
FNAME	EQU	$5ACA	;$4C0B
FNKSB	EQU	$6E42	;$5A9E
FRERAM	EQU	$9AFD	;$7EAC
GTXTTB	EQU	$6E8C	;$5AE3
INPLIN	EQU	$54F6	;$4644
INPRMP	EQU	$54F0	;$463E
KEYDSP	EQU	$F074	;$F789
KEYX	EQU	$8B31	;$7270
KILASC	EQU	$2AB4	;$1FBE
EVALEX	EQU	$1158	;$112E
L208F	EQU	$2BAC	;$208F
L3457	EQU	$41A8	;$3457
L45D3	EQU	$53FB	;$45D3
L56C5	EQU	$6662	;$56C5
PRTBUF	EQU	$6DF6	;$5A58
FNDTXT	EQU	$7042	;$5C3F
INCRDE	EQU	$7076	;$5C6D
CHKBYT	EQU	$707D	;$5C74
L5CAE	EQU	$70B7	;$5CAE
L5DB1	EQU	$715C	;$5DB1
L5DBC	EQU	$716B	;$5DBC
L5DC5	EQU	$7174	;$5DC5
L6370	EQU	$7751	;$6370
L6383	EQU	$7764	;$6383
L6CA7	EQU	$83ED	;$6CA7
CHKBK2	EQU	$8B69	;$729F
PRTCHR	EQU	$5A05	;$4B44
CALPPS	EQU	$EEF4	;$F62B
LF650	EQU	$EF32	;$F650
LF659	EQU	$EF3A	;$F659
COMLF	EQU	$EF3B	;$F65A
LF67B	EQU	$EF66	;$F67B
LF7D9	EQU	$F0C4	;$F7D9
LFAC3	EQU	$F4F1	;$FAC3
LFAC6	EQU	$F4F4	;$FAC6
ENDLCD	EQU	$FCF0	;$FF40
XONOFF	EQU	$FCF2	;$FF42
MAKHOL	EQU	$82A8	;$6B6D
MAKTXT	EQU	$2D7C	;$220F
MENU	EQU	$67A4	;$5797
CHGDSP	EQU	$64E5	;$556B
PREV	EQU	$649D	;$5523
PRTNUL	EQU	$3517	;$27B1
PRTMSG	EQU	$679E	;$5791
PRTTAB	EQU	$5A14	;$4B55 -- not used in 200 Term
RCVX	EQU	$8508	;$6D6D
RTNADR	EQU	$EF34	;$F652
RV232C	EQU	$8519	;$6D7E
SD232C	EQU	$8643	;$6E32
SNDCOM	EQU	$864F	;$6E3A
SERPRM	EQU	$EF3C	;$F65B
SETDSP	EQU	$4FC4	;$42A5
SETFNK	EQU	$6E20	;$5A7C
SETSER	EQU	$191D	;$17E6
STKINI	EQU	$714B	;$5D53
STKSET	EQU	$7155	;$5D5D
UNLOCK	EQU	$4F63	;$4244
UPCASE	EQU	$1014	;$0FE9
UPLDR	EQU	$651A	;$55A0
UPLMSG	EQU	$6766	;$5759
WATDSP	EQU	$64DC	;$5562
SCLFLG	EQU	$EF0B	;$F63E
SCLHLT	EQU	$4F5E	;$423F
CTRLQ	EQU	$8608	;$6E0B

;Program variables are held in a buffer at the end of the program:
;
;	BFR	= Record Number
;	BUF+2	= Target file directory address
;	BUF+4	= File starting address
;	BUF+6	= Block address
;	BUF+8	= Block checksum
;	BUF+9	= Operation retry number
;	BUF+10	= End of file flag
;	BUF+11	= EOF successfully recieved flag
;	BUF+12	= CRC mode flag
;	BUF+13	= CRC value
;	BUF+15	= First block flag
;	BUF+16	= Norm/Xmod key mode table address
;	BUF+18	= Normal Serial parameters saved to here
;	BUF+26	= Xmodem serial parameters stored here
;	BUF+34	= CRC Lookup Table
;	BUF+546	= Receive block buffer
;

;code starts here
;
;	prt
	ORG	55000	;up against MAXRAM for this version	

BEGIN	NOP		;start of main pgm
;	ENT	BEGIN
	mvi	a,1
	call	maxfil
	call	setmax
	LXI	H,INIMSG
	CALL	PRTMSG
START	CALL	UNLOCK
	LXI	H,FTAB1
	CALL	SETDSP
	JMP	PPARM
;***
HILBL	POP	H	;DIRACC exit from Basic (F6 call in Basic)
	INR	L
	CALL	$39d2
	CALL	$4f4d	
	JMP	TRMCN0
;***
SCROLL	call	f6chk	;check for SHIFT -> DIRACC to BASIC if pressed
	LDA	SCLFLG	;scroll toggle code
	CPI	$00
	JNZ	UNLOCK
	CALL	SCLHLT
	JMP	CTRLQ
;***
CMDENT	CALL	BEEP
	LXI	H,FTAB1
	CALL	SETFNK
CMDPMT	CALL	STKINI
	CALL	SETXPM	;run this from XMDHAZ at entry
	CALL	SAVSER	;ditto!
	LXI	H,CMDENT
	SHLD	RTNADR
	LXI	H,PMTMSG
	CALL	PRTMSG
	CALL	INPLIN
	RST	2
	ANA	A
	JZ	CMDPMT
	LXI	D,CMDLST
	CALL	L6CA7
	JZ	CMDENT
	RET	
;***
PMTMSG	DM	XMDcommand: 
	DB	0
;
CMDLST	DM	STAT
	DW	STAT
	DM	TERM
	DW	TERM
	DM	CALL
	DW	CALL
	DM	FIND
	DW	FIND
	DM	MENU
	DW	MENU
	DM	FILE
	DW	FILE
	DM	FREE
	DW	FREE
	DB	$FF
;
FTAB1	DM	Find 
	DB	$80
	DM	Call 
	DB	$80
	DM	Stat 
	DB	$80
	DM	File
	DB	$8D
	DM	Free
	DB	$8D
	DB	$80
	DM	Term
	DB	$8D
	DM	Menu
	DB	$8D
	DM	JRC37
;
FILE	CALL	DIR
	JMP	CMDPMT
;***
FREE	CALL	FRE
	JMP	CMDPMT
;***
SPMSG	DM	Serial Port Status: 
	DB	0
STAT	DCX	H
	RST	2
	INR	A
	DCR	A
	JNZ	CHSTAT
PPARM	LXI	H,SPMSG
	CALL	PRTMSG
	LXI	H,SERPRM
	MVI	B,$07	;$05 in m100
PPARM2	MOV	A,M
	RST	4
	INX	H
	DCR	B
	JNZ	PPARM2
	MVI	A,$2C
	RST	4
	lda	61252	;ans/orig (200 add)
	ana	a
	mvi	a,79
	jz	LBL01
	mvi	a,65
LBL01	rst	4
	mvi	a,44
	rst	4
	LDA	CALPPS
	ana	a
	jnz	LBL02
	mvi	a,84
	rst	4
	jmp	CMDPMT
LBL02	RRC	
	MVI	A,$32
	SBB	B
	RST	4
	LXI	H,PULMSG
	CALL	PRTNUL
	JMP	CMDPMT
;***
PULMSG	DM	0 pps
	DB	0
CHSTAT	JC	SERSET
	CPI	$2C
	JZ	SETPUL
	CALL	UPCASE
	CPI	$4D
	JNZ	CMDENT
	INX	H
SERSET	CALL	SETSER
	CALL	CLSCOM
	DCX	H
	RST	2
	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NK
	CALL	L5CAE
	PUSH	A
	LXI	H,FTAB1
	CALL	SETFNK
	CALL	L5DBC
	POP	A
	CPI	$51
	POP	D
	RET	
;***
TERM	LXI	H,COMLF	;entry poiint from XMDHAZ
	RST	2
	CNC	L3457
	PUSH	A
	CALL	SETSER
	POP	A
	CMC	
	CC	CONECT
	JC	GOBYE
	CALL	SAVSER
TRMCN0	MVI	A,$40
	STA	LF650
	STA	LF67B
	XRA	A
	sta	jy224	;split screen mod
	STA	CAPTUR
	STA	LFAC3
	sta	64749
	sta	64751
	CALL	L45D3
	MVI	A,$FF
	STA	BFR+12
	CALL	CRCGEN
TRMCN1	CALL	FSETN
	CALL	DUPDSP
	CALL	ECHDSP
	CALL	WATDSP
TRMCN2	CALL	DSPFNK
	CALL	CURSON
TRMMON	CALL	STKSET
	LXI	H,TRMMN6
	SHLD	RTNADR
	LDA	XONOFF
	call	$866c
	cc	$66fb
	ANA	A
	JZ	TRMMN2
	LDA	ENDLCD
	LXI	H,LF7D9
	XRA	M
	RRC	
	CC	WATDSP
TRMMN2	CALL	CHSNS
	CALL	$6745
	JNZ	LBLAA
	CALL	$1404
	JZ	TRMMN3
	CALL	CHGET
	JC	CTLKEY
LBLAA	MOV	B,A
	JC	CTLKEY
	cpi	128	;macro cut-out
	cnc	macro
	push	a	;split screen mod
	call	jy200
	pop	a
	ANA	A
	CNZ	$6722
	JC	TRMMN4
	LDA	DUPFLG
	ANA	A
	MOV	A,B
	CZ	PRTCHR
TRMMN3	CALL	RCVX
	JZ	TRMMON
	CALL	RV232C
	MOV	B,A
	JC	TRMMON
	CPI	$7F
	JNZ	LBLAB
	LDA	SERPRM
	CPI	$3A
	JNC	TRMMON
	MOV	A,B
LBLAB	RST	4
	LXI	H,$FCED
	MOV	A,M
	MOV	M,B
	CPI	$1B
	MOV	A,B
	JNZ	LBLAC
	CPI	$49
	JNZ	LBLAC
	LDA	CAPTUR
	ANA	A
	JNZ	LBLAC
	MVI	B,8
LBLAD	MOV	A,M
	CALL	SD232C
	INX	H
	DCR	B
	JNZ	LBLAD
	MVI	A,$49
LBLAC	CALL	$6452
	CALL	L56C5
	JMP	TRMMON
;***
TRMMN4	XRA	A
	STA	ENDLCD
TRMMN5	CALL	CHKBK2
	JC	TRMMN5
	JMP	TRMMON
;***
TRMMN6	CALL	BEEP
	XRA	A
	STA	LF659
	CALL	ECHDSP
	JMP	TRMMON
;***
CTLKEY	MOV	E,A
	MVI	D,$FF
	LHLD	BFR+16
	DAD	D
	DAD	D
	MOV	A,M
	INX	H
	MOV	H,M
	MOV	L,A
	LXI	D,TRMMON
	PUSH	D
	PCHL	
;
KEYWD	DW	jy129
	DW	DOWNLD
	DW	UPLOAD
	DW	DUPLEX
	DW	ECHOTG
	DW	SCROLL
	DW	XMDM
	DW	BYEBYE
;
FTABN	DM	Pre
	DB	$F6
	DM	Dow
	DB	$EE
	db	' U'
	DB	$F0
	DB	$80
	DB	$80
	DB	$80
	DM	Xmo
	DB	$E4
	db	' By'
	DB	$E5
;
BYEBYE	LXI	H,DISMSG
	CALL	GETANS
	CPI	$59
	JZ	GOBYE
	LXI	H,ABTMSG
	CALL	PRTMSG
	JMP	TRMMON
;***
jy129	lda	61194	;new PREV  !!!
	ora	a
	jz	PREV	;label on?
	lda	64781
	rar
	jnc	PREV	;shift?
	lxi	h,jy224
	inr	m
	mov	a,m
	rar
	jnc	20423
	cmc
	jmp	jy215
;***
GOBYE	XRA	A
	STA	LF650
	MOV	L,A
	MOV	H,A
	SHLD	CAPTUR
	CALL	CLSCOM
	CALL	CUROFF
	CALL	DISCON
	CALL	L6370
	JMP	START
;***
XMDM	call	f7chk	;check for SHIFT -> DIRACC to BASIC if pressed
	LDA	CAPTUR
	ORA	A
	CNZ	DWNEND
	LXI	H,FRE
	SHLD	BFR+16
	LXI	H,FTABX
	CALL	SETFNK
	LDA	BFR+12
	CALL	CRCDSP
	JMP	TRMCN2
;***
FTABX	DM	Pre
	DB	$F6
	DM	Sen
	DB	$E4
	DM	Rec
	DB	$F6
	DM	Fil
	DB	$E5
	DM	Fre
	DB	$E5
	DB	$80
	DB	$80
	DM	Nor
	DB	$ED
;
	DW	jy129
	DW	XMDSND
	DW	XMDRCV
	DW	DIR
	DW	FRE
	DW	SCROLL
	DW	CRCTOG
	DW	TRMCN1
;
	LINK	0:XMD62B	;use R2:XMD62B if it's in Bank 2
