
	TITLE	EVENT
	NAME	EVENT

	include	dos.mac
	.286p

X	equ	4		; for near calls
;X	equ	6		; for far calls

ebuf	STRUC
start_addr	dw	?
count		dw	?
ebuf	ENDS

mbuf	STRUC
dp	dw	?
next	dw	?
len	dw	?
mbuf	ENDS

	extrn	innet:word
	extrn	@ipinterpret:near
;	extrn	@nettick:near
	extrn	_mbfree:word
	extrn	_mbprocess:word
	extrn	_mbnext:word
	extrn	_eb:byte

LIBTCP_DATA	segment
	even
	dw	512 dup (0)
savesp	dw	0
savess	dw	0
save1c	dd	0
ticks	dw	0
inproc	dw	0
LIBTCP_DATA	ends

LIBTCP_TEXT	segment
	ASSUME	cs: LIBTCP_TEXT, ds: nothing

; This routine is called from DOS with an "int 1ch" instruction.  Therefore
; interrupts are disabled upon entry to this routine.  The "iret" after the
; "call" below will leave interrupts disabled at that point.
event_tick proc far
	push	ds
	push	ax

	mov	ax,seg LIBTCP_DATA_GROUP
	mov	ds,ax
	ASSUME ds: LIBTCP_DATA_GROUP

	pushf
	call	DWORD PTR save1c

	mov	ax,inproc
	or	ax,innet
	jz	$A10
$A01:
	jmp	$A100
$A10:
	mov	inproc,1
	mov	savess,ss
	mov	savesp,sp
	mov	ax,seg LIBTCP_DATA_GROUP
	mov	ss,ax
	ASSUME	ss: LIBTCP_DATA_GROUP
	mov	sp,offset savesp

	push	si
	push	bx
	push	es
	push	di
	push	cx
	push	dx

	cld				; for "C"
	mov	si,_mbprocess
	or	si,si
	jnz	$A12
	jmp	$A0
$A12:
	mov	bx,[si].next
	mov	_mbprocess,bx
	or	bx,bx
	jnz	$A4
	mov	_mbnext,offset _mbprocess
$A4:
	sti
	mov	bx,[si].dp
	mov	ax,[si].len
	call	@ipinterpret

	mov	ax,ds
	mov	es,ax
	ASSUME	es: LIBTCP_DATA_GROUP

	mov	bx,[si].dp		; bx = start of data
	mov	dx,[si].len
	add	dx,bx			; dx = byte past end of buffer
	mov	di,offset _eb
	cli
$A3:
	mov	cx,[di].count
	jcxz	$A6			; slot is not used
	mov	ax,[di].start_addr
	cmp	ax,dx
	je	$A6			; fix address and count
	ja	$A5			; move slot down
	add	ax,cx
	cmp	ax,bx
	je	$A7			; fix count only
	add	di,size ebuf		; point to next entry
	cmp	di,offset _eb + 32 * size ebuf
	jb	$A3			; are we done?
$AXX:
	sub	bx,bx			; joe code!!!
	mov	ax,0e13h
	int	10h
	jmp short $A9
$A5:
	push	di
	push	si
	mov	si,di			; si = current ebuf
$A30:
	add	di,size ebuf		; point to next ebuf
	mov	cx,[di].count		; get count
	jcxz	$A31
	cmp	di,offset _eb + 31 * size ebuf
					; are we at the last ebuf?
	jb	$A30
	jmp short $AXX			; joe code!!
$A31:
	mov	cx,di			; count = new ebuf - original ebuf
	sub	cx,si
	shr	cx,1			; make it words
	dec	di
	dec	di
	mov	si,di			; point to end of real data
	add	di,size ebuf
	std
	rep	movsw
	cld

	pop	si
	pop	di
	mov	[di].start_addr,bx
	mov	ax,[si].len
	mov	[di].count,ax
	jmp short $A9
$A6:
	mov	[di].start_addr,bx
	add	dx,cx
	mov	ax,dx
	sub	dx,bx
	mov	[di].count,dx
	cmp	di,offset _eb
	jbe	$A9
	sub	di,size ebuf
	jmp short $A8
$A7:
	mov	ax,dx
	add	dx,cx			; add in original ebuf count
	sub	dx,bx			; restore the count
	mov	[di].count,dx
$A8:					; check for continuous ebufs
	cmp	ax,[di + size ebuf].start_addr
	jne	$A9
	add	dx,[di + size ebuf].count
	mov	[di].count,dx		; add in next ebuf's count
	add	di,size ebuf
	cmp	di,offset _eb + 31 * size ebuf
	jae	$A9
	push	si
	mov	si,di
	add	si,size ebuf
$A81:
	movsw				; copy in start_addr
	lodsw				; get count
	stosw				; save it
	or 	ax,ax
	jnz	$A81
	pop	si
$A9:	
	mov	bx,_mbfree
	mov	[si].next,bx
	mov	_mbfree,si
	jmp short $A99
$A0:
	sti
	comment *
	mov	ax,ticks
	inc	ax
	and	ax,31
	mov	ticks,ax
	jnz	$A97

	sub	bx,bx
	mov	ax,0e2ah
	int	10h
$A97:
	*
;	call	@nettick
$A99:
	cli
	pop	dx
	pop	cx
	pop	di
	pop	es
	ASSUME	es: nothing
	pop	bx
	pop	si

	mov	ax,savess
	mov	ss,ax
	mov	sp,savesp
	ASSUME	ss: nothing
	mov	inproc,0

$A100:
	pop	ax
	pop	ds
	ASSUME	ds: nothing
	iret
event_tick endp

	ASSUME	ds: LIBTCP_DATA_GROUP
	public	_neteventinit
_neteventinit proc near
	push	ds
	mov	ax,351ch	; get old int 1ch vector
	int	21h
	cmp	bx,offset event_tick
	jne	$B0
	mov	ax,es
	mov	cx,ds
	cmp	ax,cx
	je	$B99
$B0:
	mov	word ptr save1c,bx
	mov	word ptr save1c+2,es

	mov	dx,offset event_tick
	mov	ax,cs
	mov	ds,ax
	ASSUME	ds: nothing
	mov	ax,251ch
	int	21h
$B99:
	pop	ds
	ASSUME	ds: LIBTCP_DATA_GROUP
	ret
_neteventinit endp

	public	_neteventshut
_neteventshut proc near
	push	ds

	mov	dx,word ptr save1c
	mov	ax,word ptr save1c+2
	mov	ds,ax
	ASSUME	ds: nothing
	mov	ax,251ch
	int	21h

	pop	ds
	ASSUME	ds: LIBTCP_DATA_GROUP
	ret
_neteventshut endp

LIBTCP_TEXT	ends
	end
