1: /*
     2:  *  Guitar-ZyX(tm)::MasterControlProgram - portable guitar F/X controller
     3:  *  Copyright (C) 2009  Douglas McClendon
     4:  *
     5:  *  This program is free software: you can redistribute it and/or modify
     6:  *  it under the terms of the GNU General Public License as published by
     7:  *  the Free Software Foundation, version 3 of the License.
     8:  *
     9:  *  This program is distributed in the hope that it will be useful,
    10:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12:  *  GNU General Public License for more details.
    13:  *
    14:  *  You should have received a copy of the GNU General Public License
    15:  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16: */
    17: /*
    18: #############################################################################
    19: #############################################################################
    20: ## 
    21: ## gzmcpc::time: timekeeping functions
    22: ##
    23: #############################################################################
    24: ##
    25: ## Copyright 2008-2009 Douglas McClendon <dmc AT filteredperception DOT org>
    26: ##
    27: #############################################################################
    28: #############################################################################
    29: #
    30: */
    31: 
    32: 
    33: 
    34: #include <nds.h> 
    35: 
    36: #include "time.h" 
    37: 
    38: #include "debug.h" 
    39: 
    40: 
    41: 
    42: 
    43: char timer_num_ticks = 0;
    44: 
    45: time_val num_ticks = {0, 0};
    46: 
    47: 
    48: 
    49: int time_val_compare(time_val time_a, time_val time_b) {
    50:   
    51:   if (time_b.s > time_a.s) {
    52:     return 1;
    53:   } else if (time_b.s < time_a.s) {
    54:     return -1;
    55:   } else if (time_b.ms > time_a.ms) {
    56:     return 1;
    57:   } else if (time_b.ms < time_a.ms) {
    58:     return -1;
    59:   } else {
    60:     return 0;
    61:   }
    62: }
    63: 
    64: time_val time_val_add_ms(time_val time_a, int ms) {
    65:   
    66:   time_val return_value;
    67: 
    68:   return_value = time_a;
    69:   return_value.ms += ms;
    70:   return_value.s += return_value.ms / 1000;
    71:   return_value.ms = return_value.ms % 1000;
    72: 
    73:   return return_value;
    74: }
    75: 
    76: long ms_since(time_val time) {
    77:   return (num_ticks.s - time.s) * 1000 + (num_ticks.ms - time.ms);
    78: }
    79: 
    80: void ms_timer_handler(void) {
    81: 
    82:   timer_num_ticks++;
    83: 
    84:   if (timer_num_ticks == 16) {
    85: 
    86:     timer_num_ticks = 0;
    87: 
    88:     num_ticks.ms++;
    89:     
    90:     if (num_ticks.ms == 1000) {
    91:       num_ticks.s++;
    92:       num_ticks.ms = 0;
    93:       
    94:       //    if (num_ticks.s == 3600 * 24 * 366) {
    95:       if (num_ticks.s == 31622400) {
    96: 	// one year is old enough for this program
    97: 	// XXX ... instead of exerting the mental effort to consider
    98: 	//         all wrapping situations 
    99: 	die();
   100:       }
   101: 
   102:     }
   103: 
   104:   }
   105: 
   106: }
   107: 
   108: void gzmcpc_init_time(void) {
   109: 
   110:   TIMER0_CR = 0;
   111: 
   112:   // 16khz timer
   113:   TIMER_DATA(0) = TIMER_FREQ(16384);
   114: 
   115:   // note: emacs syntax tab auto formatting did this to the line extensions.
   116:   TIMER_CR(0) =					\
   117:     TIMER_ENABLE |				\
   118:     TIMER_DIV_1 |				\
   119:     TIMER_IRQ_REQ;
   120: 
   121:   // configure gzmcp tick timer interrupt handler
   122:   irqSet(IRQ_TIMER0, ms_timer_handler);
   123: 
   124:   // enable the ms timer irq
   125:   irqEnable(IRQ_TIMER0);
   126: 
   127: }
   128: 
   129: 
   130: