//******************************************************************************
//
//  KS10 Console Microcontroller
//
//! Timer Object
//!
//! This object provides a wall timer.
//!
//! \file
//!    timer.cpp
//!
//! \author
//!    Rob Doyle - doyle (at) cox (dot) net
//
//******************************************************************************
//
// Copyright (C) 2013 Rob Doyle
//
// 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
// (at your option) 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//******************************************************************************

#ifndef __TIMER_HPP
#define __TIMER_HPP

#include "sysclk.hpp"

class timer_t {
    public:
        timer_t(void);
        timer_t(unsigned long ticks);
        void start(unsigned long ticks);
        operator bool(void) const;
        void busy_wait(unsigned long ticks);
        static const unsigned long HZ = sysclk_t::HZ;
    protected:
        unsigned long delay;
};

inline timer_t::timer_t(void) {
    delay = 0;
}

inline timer_t::timer_t(unsigned long ticks) {
    delay = ticks + sysclk_t::time();
}

inline void timer_t::start(unsigned long ticks) {
    delay = ticks + sysclk_t::time();
}

inline timer_t::operator bool(void) const {
    return sysclk_t::time() >= delay;
}

inline void timer_t::busy_wait(unsigned long ticks) {
    delay = ticks + sysclk_t::time();
    while (sysclk_t::time() < delay) {
        ;
    }
}

#endif
