		.286
_TEXT		SEGMENT  DWORD PUBLIC 'CODE'
_TEXT		ENDS
_DATA		SEGMENT  WORD PUBLIC 'DATA'
_DATA		ENDS
CONST		SEGMENT  WORD PUBLIC 'CONST'
CONST		ENDS
_BSS		SEGMENT  WORD PUBLIC 'BSS'
_BSS		ENDS
_INST		SEGMENT  DWORD PUBLIC 'INST'
_INST		ENDS
DGROUP		GROUP	 _TEXT, CONST,	_BSS,	_DATA,	_INST

		ASSUME  CS: DGROUP, DS: DGROUP

regs_set	struc
regs_es		dw	?
regs_ds		dw	?
regs_di		dw	?
regs_si		dw	?
regs_bp		dw	?
regs_sp		dw	?
regs_bx		dw	?
regs_dx		dw	?
regs_cx		dw	?
regs_ax		dw	?
regs_ip		dw	?
regs_cs		dw	?
regs_cf		dw	?
regs_set	ends

_TEXT		segment

CHK_BYTE	equ	4

		org	02ch
EnvSeg		label	word

		org	080h
CmdsLine	label	byte

		org	100h
entry:		jmp	main

Chk_Str		db	'TERM'

term_flg	db	0
term_port	db	0
rts_flg		db	0
key_shift	db	?

key_code	dw	0FFFFh
key_addr	dw	0

key_vect	dd	far

RS_status	dw	?
RS_old_param	db	18 dup (?)

RS_param	db	?,?
RS_buf_off	dw	?
RS_buf_seg	dw	?
		dw	?,?
RS_irq_off	dw	?
RS_irq_seg	dw	?
		db	?,?,?

RS_buffer	dw	1024+4+8 dup (?)

;			+0    +1    +2    +3    +4    +5    +6    +7
;			+8    +9    +a    +b    +c    +d    +e    +f
enc_key_tbl	dw	1B10h,1E10h,2E10h,2C10h,2010h,1310h,2110h,2210h	;+00 ^@
		dw	0F00h,1000h,2410h,4E00h,2610h,1D00h,2F10h,1910h ;+08 ^H
		dw	1A10h,1110h,1410h,1F10h,1510h,1710h,2D10h,1210h	;+10 ^P
		dw	2B10h,1610h,2A10h,0100h,5100h,4F00h,4D00h,5000h ;+18 ^X
		dw	3500h,0204h,0304h,0404h,0504h,0604h,0704h,0804h	;+20 
		dw	0904h,0A04h,2804h,2704h,3100h,0C00h,3200h,3300h ;+28 (
		dw	0B00h,0200h,0300h,0400h,0500h,0600h,0700h,0800h	;+3x 0
		dw	0900h,0A00h,2800h,2700h,3104h,0C04h,3204h,3304h ;+38 8
		dw	1B00h,1E04h,2E04h,2C04h,2004h,1304h,2104h,2204h	;+40 @
		dw	2304h,1804h,2404h,2504h,2604h,3004h,2F04h,1904h ;+48 H
		dw	1A04h,1104h,1404h,1F04h,1504h,1704h,2D04h,1204h	;+50 P
		dw	2B04h,1604h,2A04h,1C00h,0E00h,2900h,0D00h,3400h ;+58 X
		dw	1B04h,1E00h,2E00h,2C00h,2000h,1300h,2100h,2200h	;+60 `
		dw	2300h,1800h,2400h,2500h,2600h,3000h,2F00h,1900h ;+68 h
		dw	1A00h,1100h,1400h,1F00h,1500h,1700h,2D00h,1200h	;+70 p
		dw	2B00h,1600h,2A00h,1C04h,0E04h,2904h,0D04h,4B00h ;+78 x

Term_Ent	proc	near

		cmp	word ptr [key_code],0FFFFh
		je	key_in_loop

		mov	ah,0
		mov	dx,[key_code]
		mov	bx,[key_addr]
		mov	word ptr [key_code],0FFFFh
		jmp	key_out_ret

key_in_loop:	mov	ax,0901h
		pushf
		call	cs:[key_vect]
		mov	[key_shift],bl

		cmp	bh,69h
		jne	term_chk

key_cnt_chk:	mov	ah,07h
		pushf
		call	cs:[key_vect]
		cmp	bh,69h
		jne	term_on_off
		mov	ax,0900h
		pushf
		call	cs:[key_vect]
		jmp	key_cnt_chk

term_on_off:	mov	al,[term_flg]
		xor	al,1
		mov	[term_flg],al
		jmp	key_in_loop

term_chk:	cmp	byte ptr [term_flg],0
		jne	term_chk_2

		cmp	dh,0FFh
		jne	key_out_ret

		jmp	end_of_chk

term_chk_2:	cmp	dh,0FFh
		je	not_key

		cmp	bh,5Ah
		jbe	rs_out

key_out_ret:	mov	byte ptr [bp+regs_ax+1],ah
		mov	[bp+regs_bx],bx
		mov	[bp+regs_dx],dx
		ret

rs_out:		mov	ah,07h
		mov	al,[term_port]
		int	9Bh
		jmp	key_in_loop

not_key:	mov	ah,05h
		mov	al,[term_port]
		int	9Bh
		cmp	ah,0
		jne	end_of_chk

		cmp	[rts_flg],0
		je	rs_chk_1

		cmp	dx,256
		jnb	rs_chk_1

		mov	byte ptr [rts_flg],0
		push	ax
		push	dx
		mov	ah,08h
		mov	al,[term_port]
		mov	dl,22h
		int	9Bh
		pop	dx
		pop	ax

rs_chk_1:	cmp	dx,0
		je	end_of_chk

		mov	ah,06h
		mov	al,[term_port]
		int	9Bh
		cmp	ah,0
		jne	end_of_chk

		cmp	dl,0Ah			; Not Ret 0Ah Key Code
		je	not_key

		sub	bh,bh
		mov	bl,[key_shift]

		sub	dh,dh
		cmp	dl,80h
		jnb	not_enc_key

		mov	bx,dx
		add	bx,dx
		add	bx,offset DGROUP:enc_key_tbl
		mov	bx,[bx]

not_enc_key:	mov	ah,0
		jmp	key_out_ret

end_of_chk:	cmp	byte ptr [bp+regs_ax],0
		jne	end_of_nul
		jmp	key_in_loop

end_of_nul:	mov	ah,0
		mov	bh,0FFh
		mov	bl,[key_shift]
		mov	dx,0FFFFh
		jmp	key_out_ret

Term_Ent	endp

Key_Bios	proc	near
		cmp	ah,09h
		je	term_key
		cmp	ah,07h
		je	term_key
		jmp	cs:[key_vect]

term_key:	pusha
		push	ds
		push	es
		mov	bp,sp

		sti
		push	cs
		pop	ds

		cmp	ah,09
		je	term_key_1

		mov	byte ptr [bp+regs_ax],1
		call	Term_Ent
		mov	[key_code],dx
		mov	[key_addr],bx
		cmp	dh,0FFh
		jne	term_key_2
		mov	byte ptr [bp+regs_ax],0
		jmp	term_key_2

term_key_1:	call	Term_Ent

term_key_2:	pop	es
		pop	ds
		popa
		iret

Key_Bios	endp

RS_irq_ent	proc	far
		cmp	ah,10h
		jne	RS_irq_1
		mov	ah,08h
		mov	al,cs:[term_port]
		mov	dl,02h
		int	9Bh
		mov	byte ptr cs:[rts_flg],1
RS_irq_1:	ret
RS_irq_ent	endp

_TEXT		ends

_INST		segment
		ASSUME CS: DGROUP, DS: DGROUP

Start_init	label	word

RS_init		proc	near
		mov	ah,09h
		mov	al,[term_port]
		int	9Bh
		mov	[RS_status],dx

		mov	ah,02h
		mov	al,[term_port]
		int	9Bh

		mov	ah,04h
		mov	al,[term_port]
		mov	di,offset DGROUP:RS_old_param
		int	9Bh

		mov	ah,04h
		mov	al,[term_port]
		mov	di,offset DGROUP:RS_param
		int	9Bh

		mov	bx,offset DGROUP:RS_buffer+15
		and	bl,0F0h
		mov	word ptr [bx],1024
		mov	[RS_buf_off],bx
		mov	[RS_buf_seg],ds
		mov	[RS_irq_off],offset RS_irq_ent
		mov	[RS_irq_seg],cs

		mov	ah,03h
		mov	al,[term_port]
		mov	di,offset DGROUP:RS_param
		int	9Bh

		mov	ah,01h
		mov	al,[term_port]
		int	9Bh

		ret
RS_init		endp

RS_end		proc	near
		mov	ah,02h
		mov	al,es:[term_port]
		int	9Bh

		push	ds
		mov	ah,03h
		mov	al,es:[term_port]
		push	es
		pop	ds
		mov	di,offset DGROUP:RS_old_param
		int	9Bh
		pop	ds

		mov	dx,es:[RS_status]
		cmp	dh,0
		jne	RS_end_1

		mov	ah,01h
		mov	al,es:[term_port]
		int	9Bh

		mov	ah,08h
		mov	dx,es:[RS_status]
		int	9Bh

RS_end_1:	ret

RS_end		endp

Trap_Chk	proc	near
		push	ax
		push	cx
		push	si
		push	di

		mov	ax,3590h
		int	21h

		cld
		mov	cx,CHK_BYTE
		mov	si,offset DGROUP:Chk_Str
		mov	di,offset DGROUP:Chk_Str
	repe	cmpsb
		jne	Trap_Chk_No
		clc
		jmp	Trap_Chk_End

Trap_Chk_No:	push	ds
		pop	es
		stc

Trap_Chk_End:	pop	di
		pop	si
		pop	cx
		pop	ax
		ret
Trap_Chk	endp

Get_Chr		proc	near
Get_Chr_1:	lodsb
		cmp	al,' '
		je	Get_Chr_1
		cmp	al,09h
		je	Get_Chr_1
		cmp	al,','
		je	Get_Chr_1
		cmp	al,'='
		je	Get_Chr_1
		ret
Get_Chr		endp

main		proc	near
		mov	ax,cs
		mov	ds,ax
		mov	es,ax
		cld

		mov	si,offset DGROUP:CmdsLine
		lodsb
		cmp	al,0
		je	Trap_init
		call	Get_Chr
		cmp	al,0Dh
		je	Trap_init

		cmp	al,'-'
		jne	Param_Error

		call	Trap_Chk
		jnc	Trap_Clr

		mov	dx,offset DGROUP:Non_Msg
		jmp	End_of_Msg

Param_Error:	mov	dx,offset DGROUP:Prm_Msg
		jmp	End_of_Msg

Trap_Clr:	call	RS_end

		push	ds
		lds	dx,es:[key_vect]
		mov	ax,2590h		; Set 21h int Vect
		int	21h
		pop	ds

		mov	ah,49h			; free Now Trap Seg
		int	21h

		mov	dx,offset DGROUP:Clr_Msg
End_of_Msg:	mov	ah,9			; Disp Msg
		int	21h

End_of_Proc:	mov	ax,4C00h		; End of
		int	21h

Trap_init:	call	RS_init

		mov	ax,3590h		; Get 21h Int Vect
		int	21h
		mov	word ptr cs:[key_vect],bx
		mov	word ptr cs:[key_vect+2],es

		mov	dx,offset DGROUP:Key_Bios
		mov	ax,2590h		; Set New Vect
		int	21h

		mov	es,[EnvSeg]
		mov	ah,49h			; Free ENV Seg
		int	21h
		mov	word ptr [EnvSeg],0	; Clear Env Seg

		mov	dx,offset DGROUP:TSR_Msg
		mov	ah,9			; Disp Msg
		int	21h

		mov	dx,offset DGROUP:Start_init
		add	dx,15
		shr	dx,4
		mov	ax,3100h		; TSR End of
		int	21h

main		endp

Clr_Msg		db	'RSKEYの常駐を解除しました'
		db	0Dh,0Ah,'$'

TSR_Msg		db	'RSKEYの常駐しました'
		db	0Dh,0Ah
		db	'PF11キ−のトグル動作でキ−ボ−ド<->RS232Cになります'
		db	0Dh,0Ah,'$'

Non_Msg		db	'RSKEYが常駐されていません？'
		db	0Dh,0Ah,'$'

Prm_Msg		db	'RSKEY - で常駐解除です'
		db	0Dh,0Ah,'$'

_INST		ends
		end	entry
