idv_timer_sim.sv

Go to the documentation of this file.
00001 // $Id: idv_timer_sim.sv 6 2009-04-14 06:13:21Z seanoboyle $
00002 //----------------------------------------------------------------------------
00003 //    This program is free software: you can redistribute it and/or modify
00004 //    it under the terms of the GNU General Public License as published by
00005 //    the Free Software Foundation, either version 3 of the License, or
00006 //    (at your option) any later version.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License
00014 //    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 //
00016 //----------------------------------------------------------------------------
00017 
00018 /**
00019  * Simulation Countdown Timer.
00020  * This file contains a simulation countdown timer.<br>
00021  * <br>
00022  * @par Download the most recent version here:
00023  * http://intelligentdv.com/downloads/
00024  * <br>
00025  * @par File Bugs Here:
00026  * http://bugs.intelligentdv.com/ <br>
00027  * Project:  Timers
00028  *
00029  * @file idv_timer_sim.sv
00030  * @author Sean O'Boyle
00031  * @par Contact:
00032  * http://intelligentdv.com/contact/
00033  * @par Company:
00034  * <a href="http://intelligentdv.com">Intelligent Design Verification</a>
00035  *
00036  * $LastChangedRevision: 6 $
00037  * $LastChangedDate: 2009-04-13 23:13:21 -0700 (Mon, 13 Apr 2009) $
00038  * $LastChangedBy: seanoboyle $
00039  *
00040  */
00041 
00042 `ifndef IDV_TIMER_SIM__SV
00043 `define IDV_TIMER_SIM__SV
00044 
00045 /**
00046  *  Simulation Countdown Timer.
00047  *  This class is a simulation countdown timer.  This timer
00048  *  behaves like an 'egg timer' for simulation time. We say
00049  *  simulation time to differentiate from a 'wall time' countdown
00050  *  timer.<br>
00051  *  Timer behaves like a stopwatch countdown 'egg timer.'  It can
00052  *  started, stopped, restarted (from stopped time), and reset
00053  *  to the initial time.<br>
00054  *  @par Usage: <br>
00055  *  @par Where to Use:<br>
00056  *  use anywhere that you want to timeout after a specific amount of simulation time.<br>
00057  *  For example: <br>
00058  *   <ul>
00059  *     <li>set a limit on how long you want you testcase to run by putting a timer in your environment
00060  *     <li>use in a bfm to time out if a specific signal wasn't recieved in time
00061  *     <li>use to do something after the time expires -- by reimplementing the alarm() method in a class that you derive from this class
00062  *   </ul>
00063  *  @par How to Use:
00064  *   <ul>
00065  *     <li>construct the timer: set the amount of time to countdown; and select whether you want the timer to start counting immediately
00066  *     @code
00067  *        idv_timer_sim mytimeout;
00068  *        mytimeout = new(50 ns, "timer name"); // set timeout to 100ns, name 'timer name', do not autostart
00069  *     @endcode
00070  *     <li>stop - stop the timer at its current time
00071  *     @code
00072  *        mytimeout.stop_timer();
00073  *     @endcode
00074  *     <li> start - start the timer -- continue countdown from where the timer was last stopped
00075  *     @code
00076  *        mytimeout.start_timer();
00077  *     @endcode
00078  *     <li>reset - reset the timer to the original value
00079  *     @code
00080  *        mytimeout.stop_timer();
00081  *     @endcode
00082  *     <li>restart - restart the timer; set to the original value and then start the countdown
00083  *     @code
00084  *        mytimeout.restart_timer();
00085  *     @endcode
00086  *     <li>set a new timeout value -- by constructing a new instance
00087  *     @code
00088  *        mytimeout = new(50 ns, "timer name", 1);  // set timeout to 50 us, name 'timer name' and immediately start countdown timer
00089  *     @endcode
00090  *  </ul>
00091  *
00092  *  @class idv_timer_sim
00093  *
00094  */
00095 class idv_timer_sim;
00096 
00097    string        m_name;            ///< Name of this instance of timer - displayed with console messages
00098    local time    m_countdown_time;  ///< Countdown Time - time to countdown from
00099    local time    m_split_time;      ///< Split Time - time at stop
00100    protected bit m_timer_running;   ///< Timer is Running
00101    local event   m_stop_timer;      ///< Stop Timer Event - stop the timer
00102    local event   m_reset_timer;     ///< Reset Timer Event - reset to countdown_time
00103 
00104    /**
00105     *  Constructor.
00106     *  Class Constructor<br>
00107     *
00108     *  @param countdown_time time - Time to countdown from
00109     *  @param name string - Name of this instance of timer - displayed with console messages
00110     *  @param autostart bit - Start timer on construction (defaults to off)
00111     */
00112    extern function new(time countdown_time, string name = "IDV Timer", bit autostart = 0);
00113 
00114    /**
00115     *  Show
00116     *  Show the member variable contents
00117     *
00118     *  @return void
00119     *
00120     */
00121    extern function void show();
00122 
00123    /**
00124     *  Timer Task - timer core.
00125     *  Forks off 3 threads:<br>
00126     *  1. Countdown timer: counts down and then sounds alarm<br>
00127     *  2. Thread waiting for stop: stops timer on stop event and stores split time for restart<br>
00128     *  3. Thread waiting for reset: resets timer on reset event<br>
00129     *
00130     */
00131    extern local task timer();
00132 
00133    /**
00134     *  Start Timer.
00135     *  Start the timer - forks off the timer task
00136     *
00137     *  @return void
00138     */
00139    extern function void start_timer();
00140 
00141    /**
00142     *  Stop Timer.
00143     *  Stop the timer
00144     *
00145     *  @return void
00146     */
00147    extern function void stop_timer();
00148 
00149    /**
00150     *  Reset Timer.
00151     *  Reset the timer.<br>
00152     *  If the timer is running stop and reset the timer.<br>
00153     *  If the timer is not running just reset the timer.<br>
00154     *
00155     *  @return void
00156     */
00157    extern function void reset_timer();
00158 
00159    /**
00160     * Restart Timer.
00161     * Restart the Timer<br>
00162     * Start the timer over - resets and then starts the timer from the original countdown timme
00163     *
00164     * @return void
00165     */
00166    extern function void restart_timer();
00167 
00168    /**
00169     *  Alarm.
00170     *  Defines what happens when the countdown time expires.<br>
00171     *  Method defined virtual - so a derived class handle can redefine what happens in the event of an alarm. <br>
00172     *  For example: if the timer is to be used in a VMM environment - then extend the timer class and
00173     *  redefine this method with a vmm_log and a vmm_fatal or vmm_error call.<br>
00174     *
00175     *  @return void
00176     */
00177    extern virtual function void alarm();
00178 
00179    /**
00180     *  Fatal Error.
00181     *  Defines what happens when a fatal error occurs in the base class (this class).<br>
00182     *  Method defined virtual - so a derived class handle can redefine what happens in the event of a fatal error.<br>
00183     *  For example: if the timer is to be used in a VMM environment - then extend the timer class and
00184     *  redefine this method with a vmm_log and a vmm_fatal call.<br>
00185     *
00186     *  @return void
00187     */
00188    extern virtual function void fatal_error(string msg = "");
00189 
00190 endclass:idv_timer_sim
00191 
00192 //----------------------------------------------------------------------------
00193 // Constructor
00194 //----------------------------------------------------------------------------
00195 function idv_timer_sim::new(time countdown_time, string name = "IDV Timer", bit autostart = 0);
00196    m_name = name;
00197    m_countdown_time = countdown_time;
00198    m_split_time = 0ns;
00199    if (autostart) begin
00200       start_timer();
00201    end
00202 endfunction: new
00203 
00204 //----------------------------------------------------------------------------
00205 // Show
00206 //----------------------------------------------------------------------------
00207 function void idv_timer_sim::show();
00208    $display("idv_timer_sim::show()");
00209    $display("m_name %s", m_name);
00210    $display("m_countdown_time %0t", m_countdown_time);
00211    $display("m_split_time     %0t", m_split_time);
00212    $display("m_timer_running  %0b", m_timer_running);
00213    $display("");
00214 endfunction: show
00215 
00216 //----------------------------------------------------------------------------
00217 // Timer
00218 //----------------------------------------------------------------------------
00219 task idv_timer_sim::timer();
00220    time start_time = 0ns;
00221    time stop_time = 0ns;
00222    fork: timer_fork
00223       begin
00224          start_time = $time;
00225          #(m_countdown_time - m_split_time);
00226          alarm();
00227       end
00228       begin
00229          @(m_stop_timer);
00230          stop_time = $time;
00231          m_split_time = stop_time - start_time;
00232       end
00233       begin
00234          @(m_reset_timer);
00235          m_split_time = 0ns;
00236       end
00237    join_any
00238    disable timer_fork;
00239 endtask: timer
00240 
00241 //----------------------------------------------------------------------------
00242 // Start Timer
00243 //----------------------------------------------------------------------------
00244 function void idv_timer_sim::start_timer();
00245    if (m_timer_running) begin
00246       fatal_error(" idv_timer_sim can only have one timer running at one time; start_timer called when timer already running.");
00247    end
00248    m_timer_running = 1;
00249    fork
00250       timer();
00251    join_none
00252 endfunction: start_timer
00253 
00254 //----------------------------------------------------------------------------
00255 // Stop Timer
00256 //----------------------------------------------------------------------------
00257 function void idv_timer_sim::stop_timer();
00258    -> m_stop_timer;
00259    m_timer_running = 0;
00260 endfunction: stop_timer
00261 
00262 //----------------------------------------------------------------------------
00263 // Reset Timer
00264 //----------------------------------------------------------------------------
00265 function void idv_timer_sim::reset_timer();
00266    if (m_timer_running) begin
00267       -> m_reset_timer;
00268       m_timer_running = 0;
00269    end
00270    else begin
00271       m_split_time = 0ns;
00272    end
00273 endfunction: reset_timer
00274 
00275 //----------------------------------------------------------------------------
00276 // Restart Timer
00277 //----------------------------------------------------------------------------
00278 function void idv_timer_sim::restart_timer();
00279    reset_timer();
00280    start_timer();
00281 endfunction: restart_timer
00282 
00283 //----------------------------------------------------------------------------
00284 // Alarm Timer
00285 //----------------------------------------------------------------------------
00286 function void idv_timer_sim::alarm();
00287    $display("%s: ALARM! Time expired at time %0t", m_name, $time);
00288    $stop;
00289 endfunction: alarm
00290 
00291 //----------------------------------------------------------------------------
00292 // Fatal Error
00293 //----------------------------------------------------------------------------
00294 function void idv_timer_sim::fatal_error(string msg = "");
00295    $display("%s: FATAL! At time %0t; %s", m_name, $time, msg);
00296    $stop;
00297 endfunction: fatal_error
00298 
00299 
00300 `endif
00301 

Intelligent Design Verification
Intelligent Design Verification
Project: Timers, Revision: 1.2.0
Copyright (c) 2008-2010 Intelligent Design Verification.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included here:
http://www.intelligentdv.com/licenses/fdl.txt
doxygen
Doxygen Version: 1.6.3
IDV SV Filter Version: 2.6.3
Sat Jun 19 12:13:58 2010
Find a documentation bug? Report bugs to: bugs.intelligentdv.com Project: DoxygenFilterSV