/*
 * timer.S86  -  time handling routines
 *
 * Copyright (C) 2003-2007 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: timer.S86,v 1.5 2007/01/06 18:30:51 gkminix Exp $
 *
 *====================================================================
 */


#include <common.i86>
#include <system/bios.i86>

.file __FILE__
.line __LINE__



/*
 *====================================================================
 *
 * Some definitions which are local to this module
 */

#define TICKS_PER_SEC	18		/* Number of system ticks per second */




.line __LINE__
/*
 *====================================================================
 *
 * Start the text segment and define all public entry points
 */

	.text

	.global	get_ticks
	.global	set_ticks
	.global	set_timeout
	.global	chk_timeout



/*
 *====================================================================
 *
 * Get current number of timer ticks
 * Input:  none
 * Output: EAX  -  number of timer ticks
 * Registers changed: EAX
 */
get_ticks:

	push	cx
	push	dx
	xor	ah,ah			# get current tick count from BIOS
	int	INT_TIME
	mov	ax,cx
	shl	eax,16			# move into EAX
	mov	ax,dx
	pop	dx
	pop	cx
	ret



/*
 *====================================================================
 *
 * Set a timeout in timer ticks
 * Input:  EDX  -  Number of ticks until timeout
 * Output: none
 * Registers changed: none
 */
set_ticks:

	push	eax
	call	get_ticks		# get current tick count from BIOS
	add	eax,edx
	mov	[endval],eax		# compute end count
	pop	eax
	ret



/*
 *====================================================================
 *
 * Set a timeout in seconds
 * Input:  AX     -  Number of seconds until timeout
 * Output: none
 * Registers changed: AX
 */
set_timeout:

	push	edx
	mov	dx,TICKS_PER_SEC
	mul	dx			# compute number of ticks to timeout
	shl	edx,16
	mov	dx,ax
	call	set_ticks		# set end tick count
	pop	edx
	ret



/*
 *====================================================================
 *
 * Check if a timeout exceeded
 * Input:  none
 * Output: Carry flag set if timeout exceeded
 * Registers changed: none
 */
chk_timeout:

	push	eax
	call	get_ticks		# get current tick count from BIOS
	cmp	eax,[endval]		# check if timeout exceeded
	cmc
	pop	eax
	ret




/*
 *====================================================================
 *
 * Variable definitions
 */
	.bss

	.lcomm	endval,4		# timeout end tick value




/*
 *====================================================================
 */
	.end

