Search
lxdream.org :: lxdream/src/sh4/timer.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/timer.c
changeset 561:533f6b478071
prev422:61a0598e07ff
next619:0800a0137472
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@23
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@23
     3
 * 
nkeynes@23
     4
 * SH4 Timer/Clock peripheral modules (CPG, TMU, RTC), combined together to
nkeynes@23
     5
 * keep things simple (they intertwine a bit).
nkeynes@23
     6
 *
nkeynes@23
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@23
     8
 *
nkeynes@23
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@23
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@23
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@23
    12
 * (at your option) any later version.
nkeynes@23
    13
 *
nkeynes@23
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@23
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@23
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@23
    17
 * GNU General Public License for more details.
nkeynes@23
    18
 */
nkeynes@23
    19
nkeynes@23
    20
#include "dream.h"
nkeynes@23
    21
#include "mem.h"
nkeynes@23
    22
#include "clock.h"
nkeynes@23
    23
#include "sh4core.h"
nkeynes@23
    24
#include "sh4mmio.h"
nkeynes@53
    25
#include "intc.h"
nkeynes@23
    26
nkeynes@23
    27
/********************************* CPG *************************************/
nkeynes@53
    28
/* This is the base clock from which all other clocks are derived */
nkeynes@53
    29
uint32_t sh4_input_freq = SH4_BASE_RATE;
nkeynes@53
    30
nkeynes@414
    31
uint32_t sh4_cpu_multiplier = 2000; /* = 0.5 * frequency */
nkeynes@414
    32
nkeynes@53
    33
uint32_t sh4_cpu_freq = SH4_BASE_RATE;
nkeynes@53
    34
uint32_t sh4_bus_freq = SH4_BASE_RATE;
nkeynes@53
    35
uint32_t sh4_peripheral_freq = SH4_BASE_RATE / 2;
nkeynes@53
    36
nkeynes@53
    37
uint32_t sh4_cpu_period = 1000 / SH4_BASE_RATE; /* in nanoseconds */
nkeynes@53
    38
uint32_t sh4_bus_period = 1000 / SH4_BASE_RATE;
nkeynes@53
    39
uint32_t sh4_peripheral_period = 2000 / SH4_BASE_RATE;
nkeynes@23
    40
nkeynes@23
    41
int32_t mmio_region_CPG_read( uint32_t reg )
nkeynes@23
    42
{
nkeynes@23
    43
    return MMIO_READ( CPG, reg );
nkeynes@23
    44
}
nkeynes@23
    45
nkeynes@53
    46
/* CPU + bus dividers (note officially only the first 6 values are valid) */
nkeynes@53
    47
int ifc_divider[8] = { 1, 2, 3, 4, 5, 8, 8, 8 };
nkeynes@53
    48
/* Peripheral clock dividers (only first 5 are officially valid) */
nkeynes@53
    49
int pfc_divider[8] = { 2, 3, 4, 6, 8, 8, 8, 8 };
nkeynes@53
    50
nkeynes@23
    51
void mmio_region_CPG_write( uint32_t reg, uint32_t val )
nkeynes@23
    52
{
nkeynes@53
    53
    uint32_t div;
nkeynes@53
    54
    switch( reg ) {
nkeynes@53
    55
    case FRQCR: /* Frequency control */
nkeynes@53
    56
	div = ifc_divider[(val >> 6) & 0x07];
nkeynes@53
    57
	sh4_cpu_freq = sh4_input_freq / div;
nkeynes@414
    58
	sh4_cpu_period = sh4_cpu_multiplier * div / sh4_input_freq;
nkeynes@53
    59
	div = ifc_divider[(val >> 3) & 0x07];
nkeynes@53
    60
	sh4_bus_freq = sh4_input_freq / div;
nkeynes@53
    61
	sh4_bus_period = 1000 * div / sh4_input_freq;
nkeynes@53
    62
	div = pfc_divider[val & 0x07];
nkeynes@53
    63
	sh4_peripheral_freq = sh4_input_freq / div;
nkeynes@53
    64
	sh4_peripheral_period = 1000 * div / sh4_input_freq;
nkeynes@53
    65
nkeynes@53
    66
	/* Update everything that depends on the peripheral frequency */
nkeynes@53
    67
	SCIF_update_line_speed();
nkeynes@53
    68
	break;
nkeynes@53
    69
    case WTCSR: /* Watchdog timer */
nkeynes@53
    70
	break;
nkeynes@53
    71
    }
nkeynes@53
    72
	
nkeynes@23
    73
    MMIO_WRITE( CPG, reg, val );
nkeynes@23
    74
}
nkeynes@23
    75
nkeynes@260
    76
/**
nkeynes@260
    77
 * We don't really know what the default reset value is as it's determined
nkeynes@260
    78
 * by the mode select pins. This is the standard value that the BIOS sets,
nkeynes@260
    79
 * however, so it works for now.
nkeynes@260
    80
 */
nkeynes@260
    81
void CPG_reset( )
nkeynes@260
    82
{
nkeynes@260
    83
    mmio_region_CPG_write( FRQCR, 0x0E0A );
nkeynes@260
    84
}
nkeynes@260
    85
nkeynes@260
    86
nkeynes@23
    87
/********************************** RTC *************************************/
nkeynes@23
    88
nkeynes@53
    89
uint32_t rtc_output_period;
nkeynes@53
    90
nkeynes@23
    91
int32_t mmio_region_RTC_read( uint32_t reg )
nkeynes@23
    92
{
nkeynes@23
    93
    return MMIO_READ( RTC, reg );
nkeynes@23
    94
}
nkeynes@23
    95
nkeynes@23
    96
void mmio_region_RTC_write( uint32_t reg, uint32_t val )
nkeynes@23
    97
{
nkeynes@23
    98
    MMIO_WRITE( RTC, reg, val );
nkeynes@23
    99
}
nkeynes@23
   100
nkeynes@23
   101
/********************************** TMU *************************************/
nkeynes@23
   102
nkeynes@260
   103
uint32_t TMU_count( int timer, uint32_t nanosecs );
nkeynes@260
   104
nkeynes@260
   105
nkeynes@53
   106
#define TCR_ICPF 0x0200
nkeynes@53
   107
#define TCR_UNF  0x0100
nkeynes@53
   108
#define TCR_UNIE 0x0020
nkeynes@53
   109
nkeynes@115
   110
#define TCR_IRQ_ACTIVE (TCR_UNF|TCR_UNIE)
nkeynes@115
   111
nkeynes@53
   112
struct TMU_timer {
nkeynes@53
   113
    uint32_t timer_period;
nkeynes@53
   114
    uint32_t timer_remainder; /* left-over cycles from last count */
nkeynes@53
   115
    uint32_t timer_run; /* cycles already run from this slice */
nkeynes@53
   116
};
nkeynes@53
   117
nkeynes@53
   118
struct TMU_timer TMU_timers[3];
nkeynes@23
   119
nkeynes@23
   120
int32_t mmio_region_TMU_read( uint32_t reg )
nkeynes@23
   121
{
nkeynes@260
   122
    switch( reg ) {
nkeynes@260
   123
    case TCNT0:
nkeynes@260
   124
	TMU_count( 0, sh4r.slice_cycle );
nkeynes@260
   125
	TMU_timers[0].timer_run = sh4r.slice_cycle;
nkeynes@260
   126
	break;
nkeynes@260
   127
    case TCNT1:
nkeynes@260
   128
	TMU_count( 1, sh4r.slice_cycle );
nkeynes@260
   129
	TMU_timers[1].timer_run = sh4r.slice_cycle;
nkeynes@260
   130
	break;
nkeynes@260
   131
    case TCNT2:
nkeynes@260
   132
	TMU_count( 2, sh4r.slice_cycle );
nkeynes@260
   133
	TMU_timers[2].timer_run = sh4r.slice_cycle;
nkeynes@260
   134
	break;
nkeynes@260
   135
    }
nkeynes@23
   136
    return MMIO_READ( TMU, reg );
nkeynes@23
   137
}
nkeynes@23
   138
nkeynes@115
   139
void TMU_set_timer_control( int timer,  int tcr )
nkeynes@53
   140
{
nkeynes@53
   141
    uint32_t period = 1;
nkeynes@115
   142
    uint32_t oldtcr = MMIO_READ( TMU, TCR0 + (12*timer) );
nkeynes@115
   143
nkeynes@115
   144
    if( (oldtcr & TCR_UNF) == 0 ) {
nkeynes@115
   145
	tcr = tcr & (~TCR_UNF);
nkeynes@115
   146
    } else {
nkeynes@422
   147
	if( ((oldtcr & TCR_UNIE) == 0) && 
nkeynes@115
   148
	    (tcr & TCR_IRQ_ACTIVE) == TCR_IRQ_ACTIVE ) {
nkeynes@115
   149
	    intc_raise_interrupt( INT_TMU_TUNI0 + timer );
nkeynes@115
   150
	} else if( (oldtcr & TCR_UNIE) != 0 && 
nkeynes@115
   151
		   (tcr & TCR_IRQ_ACTIVE) != TCR_IRQ_ACTIVE ) {
nkeynes@115
   152
	    intc_clear_interrupt( INT_TMU_TUNI0 + timer );
nkeynes@115
   153
	}
nkeynes@115
   154
    }
nkeynes@115
   155
nkeynes@53
   156
    switch( tcr & 0x07 ) {
nkeynes@53
   157
    case 0:
nkeynes@53
   158
	period = sh4_peripheral_period << 2 ;
nkeynes@53
   159
	break;
nkeynes@53
   160
    case 1: 
nkeynes@53
   161
	period = sh4_peripheral_period << 4;
nkeynes@53
   162
	break;
nkeynes@53
   163
    case 2:
nkeynes@53
   164
	period = sh4_peripheral_period << 6;
nkeynes@53
   165
	break;
nkeynes@53
   166
    case 3: 
nkeynes@53
   167
	period = sh4_peripheral_period << 8;
nkeynes@53
   168
	break;
nkeynes@53
   169
    case 4:
nkeynes@53
   170
	period = sh4_peripheral_period << 10;
nkeynes@53
   171
	break;
nkeynes@53
   172
    case 5:
nkeynes@53
   173
	/* Illegal value. */
nkeynes@53
   174
	ERROR( "TMU %d period set to illegal value (5)", timer );
nkeynes@53
   175
	period = sh4_peripheral_period << 12; /* for something to do */
nkeynes@53
   176
	break;
nkeynes@53
   177
    case 6:
nkeynes@53
   178
	period = rtc_output_period;
nkeynes@53
   179
	break;
nkeynes@53
   180
    case 7:
nkeynes@53
   181
	/* External clock... Hrm? */
nkeynes@53
   182
	period = sh4_peripheral_period; /* I dunno... */
nkeynes@53
   183
	break;
nkeynes@53
   184
    }
nkeynes@53
   185
    TMU_timers[timer].timer_period = period;
nkeynes@115
   186
nkeynes@115
   187
    MMIO_WRITE( TMU, TCR0 + (12*timer), tcr );
nkeynes@53
   188
}
nkeynes@23
   189
nkeynes@53
   190
void TMU_start( int timer )
nkeynes@23
   191
{
nkeynes@260
   192
    TMU_timers[timer].timer_run = sh4r.slice_cycle;
nkeynes@53
   193
    TMU_timers[timer].timer_remainder = 0;
nkeynes@53
   194
}
nkeynes@53
   195
nkeynes@264
   196
/**
nkeynes@264
   197
 * Stop the given timer. Run it up to the current time and leave it there.
nkeynes@264
   198
 */
nkeynes@53
   199
void TMU_stop( int timer )
nkeynes@53
   200
{
nkeynes@264
   201
    TMU_count( timer, sh4r.slice_cycle );
nkeynes@264
   202
    TMU_timers[timer].timer_run = sh4r.slice_cycle;
nkeynes@53
   203
}
nkeynes@53
   204
nkeynes@53
   205
/**
nkeynes@53
   206
 * Count the specified timer for a given number of nanoseconds.
nkeynes@53
   207
 */
nkeynes@53
   208
uint32_t TMU_count( int timer, uint32_t nanosecs ) 
nkeynes@53
   209
{
nkeynes@53
   210
    nanosecs = nanosecs + TMU_timers[timer].timer_remainder -
nkeynes@53
   211
	TMU_timers[timer].timer_run;
nkeynes@53
   212
    TMU_timers[timer].timer_remainder = 
nkeynes@53
   213
	nanosecs % TMU_timers[timer].timer_period;
nkeynes@53
   214
    uint32_t count = nanosecs / TMU_timers[timer].timer_period;
nkeynes@53
   215
    uint32_t value = MMIO_READ( TMU, TCNT0 + 12*timer );
nkeynes@53
   216
    uint32_t reset = MMIO_READ( TMU, TCOR0 + 12*timer );
nkeynes@53
   217
    if( count > value ) {
nkeynes@53
   218
	uint32_t tcr = MMIO_READ( TMU, TCR0 + 12*timer );
nkeynes@53
   219
	tcr |= TCR_UNF;
nkeynes@53
   220
	count -= value;
nkeynes@53
   221
        value = reset - (count % reset);
nkeynes@53
   222
	MMIO_WRITE( TMU, TCR0 + 12*timer, tcr );
nkeynes@53
   223
	if( tcr & TCR_UNIE ) 
nkeynes@53
   224
	    intc_raise_interrupt( INT_TMU_TUNI0 + timer );
nkeynes@53
   225
    } else {
nkeynes@53
   226
	value -= count;
nkeynes@23
   227
    }
nkeynes@53
   228
    MMIO_WRITE( TMU, TCNT0 + 12*timer, value );
nkeynes@53
   229
    return value;
nkeynes@23
   230
}
nkeynes@23
   231
nkeynes@23
   232
void mmio_region_TMU_write( uint32_t reg, uint32_t val )
nkeynes@23
   233
{
nkeynes@53
   234
    uint32_t oldval;
nkeynes@53
   235
    int i;
nkeynes@23
   236
    switch( reg ) {
nkeynes@53
   237
    case TSTR:
nkeynes@53
   238
	oldval = MMIO_READ( TMU, TSTR );
nkeynes@53
   239
	for( i=0; i<3; i++ ) {
nkeynes@53
   240
	    uint32_t tmp = 1<<i;
nkeynes@264
   241
	    if( (oldval & tmp) != 0 && (val&tmp) == 0  )
nkeynes@53
   242
		TMU_stop(i);
nkeynes@264
   243
	    else if( (oldval&tmp) == 0 && (val&tmp) != 0 )
nkeynes@53
   244
		TMU_start(i);
nkeynes@53
   245
	}
nkeynes@53
   246
	break;
nkeynes@53
   247
    case TCR0:
nkeynes@115
   248
	TMU_set_timer_control( 0, val );
nkeynes@115
   249
	return;
nkeynes@53
   250
    case TCR1:
nkeynes@115
   251
	TMU_set_timer_control( 1, val );
nkeynes@115
   252
	return;
nkeynes@53
   253
    case TCR2:
nkeynes@115
   254
	TMU_set_timer_control( 2, val );
nkeynes@115
   255
	return;
nkeynes@23
   256
    }
nkeynes@23
   257
    MMIO_WRITE( TMU, reg, val );
nkeynes@23
   258
}
nkeynes@23
   259
nkeynes@30
   260
void TMU_run_slice( uint32_t nanosecs )
nkeynes@23
   261
{
nkeynes@23
   262
    int tcr = MMIO_READ( TMU, TSTR );
nkeynes@23
   263
    if( tcr & 0x01 ) {
nkeynes@53
   264
	TMU_count( 0, nanosecs );
nkeynes@53
   265
	TMU_timers[0].timer_run = 0;
nkeynes@23
   266
    }
nkeynes@23
   267
    if( tcr & 0x02 ) {
nkeynes@53
   268
	TMU_count( 1, nanosecs );
nkeynes@53
   269
	TMU_timers[1].timer_run = 0;
nkeynes@23
   270
    }
nkeynes@23
   271
    if( tcr & 0x04 ) {
nkeynes@53
   272
	TMU_count( 2, nanosecs );
nkeynes@53
   273
	TMU_timers[2].timer_run = 0;
nkeynes@23
   274
    }
nkeynes@23
   275
}
nkeynes@53
   276
nkeynes@53
   277
void TMU_update_clocks()
nkeynes@53
   278
{
nkeynes@115
   279
    TMU_set_timer_control( 0, MMIO_READ( TMU, TCR0 ) );
nkeynes@115
   280
    TMU_set_timer_control( 1, MMIO_READ( TMU, TCR1 ) );
nkeynes@115
   281
    TMU_set_timer_control( 2, MMIO_READ( TMU, TCR2 ) );
nkeynes@53
   282
}
nkeynes@53
   283
nkeynes@53
   284
void TMU_reset( )
nkeynes@53
   285
{
nkeynes@53
   286
    TMU_timers[0].timer_remainder = 0;
nkeynes@53
   287
    TMU_timers[0].timer_run = 0;
nkeynes@53
   288
    TMU_timers[1].timer_remainder = 0;
nkeynes@53
   289
    TMU_timers[1].timer_run = 0;
nkeynes@53
   290
    TMU_timers[2].timer_remainder = 0;
nkeynes@53
   291
    TMU_timers[2].timer_run = 0;
nkeynes@53
   292
    TMU_update_clocks();
nkeynes@53
   293
}
nkeynes@53
   294
nkeynes@53
   295
void TMU_save_state( FILE *f ) {
nkeynes@53
   296
    fwrite( &TMU_timers, sizeof(TMU_timers), 1, f );
nkeynes@53
   297
}
nkeynes@53
   298
nkeynes@53
   299
int TMU_load_state( FILE *f ) 
nkeynes@53
   300
{
nkeynes@53
   301
    fread( &TMU_timers, sizeof(TMU_timers), 1, f );
nkeynes@53
   302
    return 0;
nkeynes@53
   303
}
.