Search
lxdream.org :: lxdream/src/aica/aica.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/aica/aica.c
changeset 463:0655796f9bb5
prev434:8af49a412d92
next465:3bd7be575792
author nkeynes
date Wed Oct 24 21:24:09 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Implement channel position readback
file annotate diff log raw
nkeynes@11
     1
/**
nkeynes@463
     2
 * $Id: aica.c,v 1.24 2007-10-24 21:24:09 nkeynes Exp $
nkeynes@11
     3
 * 
nkeynes@11
     4
 * This is the core sound system (ie the bit which does the actual work)
nkeynes@11
     5
 *
nkeynes@11
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@11
     7
 *
nkeynes@11
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@11
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@11
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@11
    11
 * (at your option) any later version.
nkeynes@11
    12
 *
nkeynes@11
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@11
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@11
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@11
    16
 * GNU General Public License for more details.
nkeynes@11
    17
 */
nkeynes@11
    18
nkeynes@35
    19
#define MODULE aica_module
nkeynes@35
    20
nkeynes@131
    21
#include <time.h>
nkeynes@11
    22
#include "dream.h"
nkeynes@66
    23
#include "dreamcast.h"
nkeynes@15
    24
#include "mem.h"
nkeynes@106
    25
#include "aica/aica.h"
nkeynes@61
    26
#include "armcore.h"
nkeynes@106
    27
#include "aica/audio.h"
nkeynes@11
    28
#define MMIO_IMPL
nkeynes@11
    29
#include "aica.h"
nkeynes@11
    30
nkeynes@11
    31
MMIO_REGION_READ_DEFFN( AICA0 )
nkeynes@11
    32
MMIO_REGION_READ_DEFFN( AICA1 )
nkeynes@11
    33
nkeynes@23
    34
void aica_init( void );
nkeynes@23
    35
void aica_reset( void );
nkeynes@23
    36
void aica_start( void );
nkeynes@23
    37
void aica_stop( void );
nkeynes@35
    38
void aica_save_state( FILE *f );
nkeynes@35
    39
int aica_load_state( FILE *f );
nkeynes@30
    40
uint32_t aica_run_slice( uint32_t );
nkeynes@23
    41
nkeynes@23
    42
struct dreamcast_module aica_module = { "AICA", aica_init, aica_reset, 
nkeynes@23
    43
					aica_start, aica_run_slice, aica_stop,
nkeynes@35
    44
					aica_save_state, aica_load_state };
nkeynes@15
    45
nkeynes@173
    46
/* 20 years in seconds */
nkeynes@173
    47
#define RTC_OFFSET 631152000
nkeynes@173
    48
unsigned int aica_time_of_day = 0;
nkeynes@173
    49
nkeynes@11
    50
/**
nkeynes@11
    51
 * Initialize the AICA subsystem. Note requires that 
nkeynes@11
    52
 */
nkeynes@11
    53
void aica_init( void )
nkeynes@11
    54
{
nkeynes@11
    55
    register_io_regions( mmio_list_spu );
nkeynes@11
    56
    MMIO_NOTRACE(AICA0);
nkeynes@11
    57
    MMIO_NOTRACE(AICA1);
nkeynes@11
    58
    arm_mem_init();
nkeynes@66
    59
    aica_reset();
nkeynes@11
    60
}
nkeynes@11
    61
nkeynes@11
    62
void aica_reset( void )
nkeynes@11
    63
{
nkeynes@35
    64
    arm_reset();
nkeynes@66
    65
    aica_event(2); /* Pre-deliver a timer interrupt */
nkeynes@173
    66
    aica_time_of_day = 0x5bfc8900;
nkeynes@11
    67
}
nkeynes@11
    68
nkeynes@23
    69
void aica_start( void )
nkeynes@23
    70
{
nkeynes@23
    71
nkeynes@23
    72
}
nkeynes@23
    73
nkeynes@66
    74
/**
nkeynes@66
    75
 * Keep track of what we've done so far this second, to try to keep the
nkeynes@66
    76
 * precision of samples/second.
nkeynes@66
    77
 */
nkeynes@66
    78
int samples_done = 0;
nkeynes@66
    79
uint32_t nanosecs_done = 0;
nkeynes@66
    80
nkeynes@30
    81
uint32_t aica_run_slice( uint32_t nanosecs )
nkeynes@23
    82
{
nkeynes@23
    83
    /* Run arm instructions */
nkeynes@35
    84
    int reset = MMIO_READ( AICA2, AICA_RESET );
nkeynes@66
    85
    if( (reset & 1) == 0 ) { /* Running */
nkeynes@76
    86
	int num_samples = (int)((uint64_t)AICA_SAMPLE_RATE * (nanosecs_done + nanosecs) / 1000000000) - samples_done;
nkeynes@73
    87
	num_samples = arm_run_slice( num_samples );
nkeynes@73
    88
	audio_mix_samples( num_samples );
nkeynes@73
    89
nkeynes@66
    90
	samples_done += num_samples;
nkeynes@66
    91
	nanosecs_done += nanosecs;
nkeynes@35
    92
    }
nkeynes@73
    93
    if( nanosecs_done > 1000000000 ) {
nkeynes@73
    94
	samples_done -= AICA_SAMPLE_RATE;
nkeynes@73
    95
	nanosecs_done -= 1000000000;
nkeynes@173
    96
	aica_time_of_day++;
nkeynes@73
    97
    }
nkeynes@43
    98
    return nanosecs;
nkeynes@23
    99
}
nkeynes@23
   100
nkeynes@23
   101
void aica_stop( void )
nkeynes@23
   102
{
nkeynes@23
   103
nkeynes@23
   104
}
nkeynes@23
   105
nkeynes@35
   106
void aica_save_state( FILE *f )
nkeynes@35
   107
{
nkeynes@35
   108
    arm_save_state( f );
nkeynes@35
   109
}
nkeynes@35
   110
nkeynes@35
   111
int aica_load_state( FILE *f )
nkeynes@35
   112
{
nkeynes@35
   113
    return arm_load_state( f );
nkeynes@35
   114
}
nkeynes@35
   115
nkeynes@61
   116
int aica_event_pending = 0;
nkeynes@61
   117
int aica_clear_count = 0;
nkeynes@61
   118
nkeynes@61
   119
/* Note: This is probably not necessarily technically correct but it should
nkeynes@61
   120
 * work in the meantime.
nkeynes@61
   121
 */
nkeynes@61
   122
nkeynes@61
   123
void aica_event( int event )
nkeynes@61
   124
{
nkeynes@61
   125
    if( aica_event_pending == 0 )
nkeynes@61
   126
	armr.int_pending |= CPSR_F;
nkeynes@61
   127
    aica_event_pending |= (1<<event);
nkeynes@61
   128
    
nkeynes@61
   129
    int pending = MMIO_READ( AICA2, AICA_IRQ );
nkeynes@61
   130
    if( pending == 0 || event < pending )
nkeynes@61
   131
	MMIO_WRITE( AICA2, AICA_IRQ, event );
nkeynes@61
   132
}
nkeynes@61
   133
nkeynes@61
   134
void aica_clear_event( )
nkeynes@61
   135
{
nkeynes@61
   136
    aica_clear_count++;
nkeynes@61
   137
    if( aica_clear_count == 4 ) {
nkeynes@61
   138
	int i;
nkeynes@61
   139
	aica_clear_count = 0;
nkeynes@61
   140
nkeynes@61
   141
	for( i=0; i<8; i++ ) {
nkeynes@61
   142
	    if( aica_event_pending & (1<<i) ) {
nkeynes@61
   143
		aica_event_pending &= ~(1<<i);
nkeynes@61
   144
		break;
nkeynes@61
   145
	    }
nkeynes@61
   146
	}
nkeynes@61
   147
	for( ;i<8; i++ ) {
nkeynes@61
   148
	    if( aica_event_pending & (1<<i) ) {
nkeynes@61
   149
		MMIO_WRITE( AICA2, AICA_IRQ, i );
nkeynes@61
   150
		break;
nkeynes@61
   151
	    }
nkeynes@61
   152
	}
nkeynes@61
   153
	if( aica_event_pending == 0 )
nkeynes@61
   154
	    armr.int_pending &= ~CPSR_F;
nkeynes@61
   155
    }
nkeynes@61
   156
}
nkeynes@66
   157
nkeynes@86
   158
void aica_enable( void )
nkeynes@86
   159
{
nkeynes@86
   160
    mmio_region_AICA2_write( AICA_RESET, MMIO_READ(AICA2,AICA_RESET) & ~1 );
nkeynes@86
   161
}
nkeynes@86
   162
nkeynes@11
   163
/** Channel register structure:
nkeynes@43
   164
 * 00  4  Channel config
nkeynes@43
   165
 * 04  4  Waveform address lo (16 bits)
nkeynes@11
   166
 * 08  4  Loop start address
nkeynes@11
   167
 * 0C  4  Loop end address
nkeynes@11
   168
 * 10  4  Volume envelope
nkeynes@43
   169
 * 14  4  Init to 0x1F
nkeynes@43
   170
 * 18  4  Frequency (floating point)
nkeynes@43
   171
 * 1C  4  ?? 
nkeynes@43
   172
 * 20  4  ??
nkeynes@11
   173
 * 24  1  Pan
nkeynes@11
   174
 * 25  1  ??
nkeynes@11
   175
 * 26  
nkeynes@11
   176
 * 27  
nkeynes@11
   177
 * 28  1  ??
nkeynes@11
   178
 * 29  1  Volume
nkeynes@11
   179
 * 2C
nkeynes@11
   180
 * 30
nkeynes@431
   181
 */
nkeynes@11
   182
nkeynes@11
   183
/* Write to channels 0-31 */
nkeynes@11
   184
void mmio_region_AICA0_write( uint32_t reg, uint32_t val )
nkeynes@11
   185
{
nkeynes@35
   186
    MMIO_WRITE( AICA0, reg, val );
nkeynes@66
   187
    aica_write_channel( reg >> 7, reg % 128, val );
nkeynes@37
   188
    //    DEBUG( "AICA0 Write %08X => %08X", val, reg );
nkeynes@11
   189
}
nkeynes@11
   190
nkeynes@11
   191
/* Write to channels 32-64 */
nkeynes@11
   192
void mmio_region_AICA1_write( uint32_t reg, uint32_t val )
nkeynes@11
   193
{
nkeynes@35
   194
    MMIO_WRITE( AICA1, reg, val );
nkeynes@66
   195
    aica_write_channel( (reg >> 7) + 32, reg % 128, val );
nkeynes@37
   196
    // DEBUG( "AICA1 Write %08X => %08X", val, reg );
nkeynes@11
   197
}
nkeynes@11
   198
nkeynes@66
   199
/**
nkeynes@66
   200
 * AICA control registers 
nkeynes@66
   201
 */
nkeynes@11
   202
void mmio_region_AICA2_write( uint32_t reg, uint32_t val )
nkeynes@11
   203
{
nkeynes@35
   204
    uint32_t tmp;
nkeynes@35
   205
    switch( reg ) {
nkeynes@35
   206
    case AICA_RESET:
nkeynes@35
   207
	tmp = MMIO_READ( AICA2, AICA_RESET );
nkeynes@37
   208
	if( (tmp & 1) == 1 && (val & 1) == 0 ) {
nkeynes@35
   209
	    /* ARM enabled - execute a core reset */
nkeynes@37
   210
	    DEBUG( "ARM enabled" );
nkeynes@35
   211
	    arm_reset();
nkeynes@66
   212
	    samples_done = 0;
nkeynes@66
   213
	    nanosecs_done = 0;
nkeynes@37
   214
	} else if( (tmp&1) == 0 && (val&1) == 1 ) {
nkeynes@37
   215
	    DEBUG( "ARM disabled" );
nkeynes@35
   216
	}
nkeynes@35
   217
	MMIO_WRITE( AICA2, AICA_RESET, val );
nkeynes@35
   218
	break;
nkeynes@61
   219
    case AICA_IRQCLEAR:
nkeynes@61
   220
	aica_clear_event();
nkeynes@61
   221
	break;
nkeynes@35
   222
    default:
nkeynes@35
   223
	MMIO_WRITE( AICA2, reg, val );
nkeynes@35
   224
	break;
nkeynes@35
   225
    }
nkeynes@11
   226
}
nkeynes@66
   227
nkeynes@463
   228
int32_t mmio_region_AICA2_read( uint32_t reg )
nkeynes@463
   229
{
nkeynes@463
   230
    audio_channel_t channel;
nkeynes@463
   231
    uint32_t channo;
nkeynes@463
   232
    int32_t val;
nkeynes@463
   233
    switch( reg ) {
nkeynes@463
   234
    case AICA_CHANSTATE:
nkeynes@463
   235
	channo = (MMIO_READ( AICA2, AICA_CHANSEL ) >> 8) & 0x3F;
nkeynes@463
   236
	channel = audio_get_channel(channo);
nkeynes@463
   237
	if( channel->loop == LOOP_LOOPED ) {
nkeynes@463
   238
	    val = 0x8000;
nkeynes@463
   239
	    channel->loop = LOOP_ON;
nkeynes@463
   240
	} else {
nkeynes@463
   241
	    val = 0;
nkeynes@463
   242
	}
nkeynes@463
   243
	return val;
nkeynes@463
   244
    case AICA_CHANPOSN:
nkeynes@463
   245
	channo = (MMIO_READ( AICA2, AICA_CHANSEL ) >> 8) & 0x3F;
nkeynes@463
   246
	channel = audio_get_channel(channo);
nkeynes@463
   247
	return channel->posn;
nkeynes@463
   248
    default:
nkeynes@463
   249
	return MMIO_READ( AICA2, reg );
nkeynes@463
   250
    }
nkeynes@463
   251
}
nkeynes@131
   252
nkeynes@131
   253
int32_t mmio_region_AICARTC_read( uint32_t reg )
nkeynes@131
   254
{
nkeynes@173
   255
    int32_t rv = 0;
nkeynes@131
   256
    switch( reg ) {
nkeynes@131
   257
    case AICA_RTCHI:
nkeynes@173
   258
        rv = (aica_time_of_day >> 16) & 0xFFFF;
nkeynes@131
   259
	break;
nkeynes@131
   260
    case AICA_RTCLO:
nkeynes@173
   261
	rv = aica_time_of_day & 0xFFFF;
nkeynes@131
   262
	break;
nkeynes@131
   263
    }
nkeynes@241
   264
    // DEBUG( "Read AICA RTC %d => %08X", reg, rv );
nkeynes@173
   265
    return rv;
nkeynes@131
   266
}
nkeynes@131
   267
nkeynes@131
   268
nkeynes@301
   269
void mmio_region_AICARTC_write( uint32_t reg, uint32_t val )
nkeynes@301
   270
{
nkeynes@301
   271
    switch( reg ) {
nkeynes@301
   272
    case AICA_RTCEN:
nkeynes@301
   273
	MMIO_WRITE( AICARTC, reg, val&0x01 );
nkeynes@301
   274
	break;
nkeynes@301
   275
    case AICA_RTCLO:
nkeynes@301
   276
	if( MMIO_READ( AICARTC, AICA_RTCEN ) & 0x01 ) {
nkeynes@301
   277
	    aica_time_of_day = (aica_time_of_day & 0xFFFF0000) | (val & 0xFFFF);
nkeynes@301
   278
	}
nkeynes@301
   279
	break;
nkeynes@301
   280
    case AICA_RTCHI:
nkeynes@301
   281
	if( MMIO_READ( AICARTC, AICA_RTCEN ) & 0x01 ) {
nkeynes@301
   282
	    aica_time_of_day = (aica_time_of_day & 0xFFFF) | (val<<16);
nkeynes@301
   283
	    MMIO_WRITE( AICARTC, AICA_RTCEN, 0 );
nkeynes@301
   284
	}
nkeynes@301
   285
	break;
nkeynes@301
   286
    }
nkeynes@301
   287
}
nkeynes@301
   288
	
nkeynes@66
   289
/**
nkeynes@66
   290
 * Translate the channel frequency to a sample rate. The frequency is a
nkeynes@66
   291
 * 14-bit floating point number, where bits 0..9 is the mantissa,
nkeynes@66
   292
 * 11..14 is the signed exponent (-8 to +7). Bit 10 appears to
nkeynes@66
   293
 * be unused.
nkeynes@66
   294
 *
nkeynes@66
   295
 * @return sample rate in samples per second.
nkeynes@66
   296
 */
nkeynes@66
   297
uint32_t aica_frequency_to_sample_rate( uint32_t freq )
nkeynes@66
   298
{
nkeynes@66
   299
    uint32_t exponent = (freq & 0x3800) >> 11;
nkeynes@66
   300
    uint32_t mantissa = freq & 0x03FF;
nkeynes@82
   301
    uint32_t rate;
nkeynes@66
   302
    if( freq & 0x4000 ) {
nkeynes@66
   303
	/* neg exponent - rate < 44100 */
nkeynes@66
   304
	exponent = 8 - exponent;
nkeynes@82
   305
	rate = (44100 >> exponent) +
nkeynes@66
   306
	    ((44100 * mantissa) >> (10+exponent));
nkeynes@66
   307
    } else {
nkeynes@66
   308
	/* pos exponent - rate > 44100 */
nkeynes@82
   309
	rate = (44100 << exponent) +
nkeynes@66
   310
	    ((44100 * mantissa) >> (10-exponent));
nkeynes@66
   311
    }
nkeynes@82
   312
    return rate;
nkeynes@66
   313
}
nkeynes@66
   314
nkeynes@434
   315
void aica_start_stop_channels()
nkeynes@434
   316
{
nkeynes@434
   317
    int i;
nkeynes@434
   318
    for( i=0; i<32; i++ ) {
nkeynes@434
   319
	uint32_t val = MMIO_READ( AICA0, i<<7 );
nkeynes@434
   320
	audio_start_stop_channel(i, val&0x4000);
nkeynes@434
   321
    }
nkeynes@434
   322
    for( ; i<64; i++ ) {
nkeynes@434
   323
	uint32_t val = MMIO_READ( AICA1, (i-32)<<7 );
nkeynes@434
   324
	audio_start_stop_channel(i, val&0x4000);
nkeynes@434
   325
    }
nkeynes@434
   326
}
nkeynes@434
   327
nkeynes@82
   328
/**
nkeynes@82
   329
 * Derived directly from Dan Potter's log table
nkeynes@82
   330
 */
nkeynes@82
   331
uint8_t aica_volume_table[256] = {
nkeynes@82
   332
      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,
nkeynes@82
   333
      1,   1,   1,   1,   1,   1,   2,   2,   2,   2,   2,   3,   3,   3,   3,   4,
nkeynes@82
   334
      4,   4,   4,   5,   5,   5,   5,   6,   6,   6,   7,   7,   7,   8,   8,   9,
nkeynes@82
   335
      9,   9,  10,  10,  11,  11,  11,  12,  12,  13,  13,  14,  14,  15,  15,  16,
nkeynes@82
   336
     16,  17,  17,  18,  18,  19,  19,  20,  20,  21,  22,  22,  23,  23,  24,  25,
nkeynes@82
   337
     25,  26,  27,  27,  28,  29,  29,  30,  31,  31,  32,  33,  34,  34,  35,  36,
nkeynes@82
   338
     37,  37,  38,  39,  40,  40,  41,  42,  43,  44,  45,  45,  46,  47,  48,  49,
nkeynes@82
   339
     50,  51,  52,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
nkeynes@82
   340
     65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  76,  77,  78,  79,  80,  81,
nkeynes@82
   341
     82,  83,  85,  86,  87,  88,  89,  90,  92,  93,  94,  95,  97,  98,  99, 100,
nkeynes@82
   342
    102, 103, 104, 105, 107, 108, 109, 111, 112, 113, 115, 116, 117, 119, 120, 121,
nkeynes@82
   343
    123, 124, 126, 127, 128, 130, 131, 133, 134, 136, 137, 139, 140, 142, 143, 145,
nkeynes@82
   344
    146, 148, 149, 151, 152, 154, 155, 157, 159, 160, 162, 163, 165, 167, 168, 170,
nkeynes@82
   345
    171, 173, 175, 176, 178, 180, 181, 183, 185, 187, 188, 190, 192, 194, 195, 197,
nkeynes@82
   346
    199, 201, 202, 204, 206, 208, 210, 211, 213, 215, 217, 219, 221, 223, 224, 226,
nkeynes@82
   347
    228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 253, 254, 255 };
nkeynes@82
   348
nkeynes@82
   349
nkeynes@66
   350
void aica_write_channel( int channelNo, uint32_t reg, uint32_t val ) 
nkeynes@66
   351
{
nkeynes@66
   352
    val &= 0x0000FFFF;
nkeynes@66
   353
    audio_channel_t channel = audio_get_channel(channelNo);
nkeynes@66
   354
    switch( reg ) {
nkeynes@66
   355
    case 0x00: /* Config + high address bits*/
nkeynes@66
   356
	channel->start = (channel->start & 0xFFFF) | ((val&0x1F) << 16);
nkeynes@66
   357
	if( val & 0x200 ) 
nkeynes@463
   358
	    channel->loop = LOOP_ON;
nkeynes@66
   359
	else 
nkeynes@463
   360
	    channel->loop = LOOP_OFF;
nkeynes@66
   361
	switch( (val >> 7) & 0x03 ) {
nkeynes@66
   362
	case 0:
nkeynes@66
   363
	    channel->sample_format = AUDIO_FMT_16BIT;
nkeynes@66
   364
	    break;
nkeynes@66
   365
	case 1:
nkeynes@66
   366
	    channel->sample_format = AUDIO_FMT_8BIT;
nkeynes@66
   367
	    break;
nkeynes@66
   368
	case 2:
nkeynes@66
   369
	case 3:
nkeynes@66
   370
	    channel->sample_format = AUDIO_FMT_ADPCM;
nkeynes@66
   371
	    break;
nkeynes@66
   372
	}
nkeynes@434
   373
	if( val & 0x8000 ) {
nkeynes@434
   374
	    aica_start_stop_channels();
nkeynes@66
   375
	}
nkeynes@66
   376
	break;
nkeynes@66
   377
    case 0x04: /* Low 16 address bits */
nkeynes@66
   378
	channel->start = (channel->start & 0x001F0000) | val;
nkeynes@66
   379
	break;
nkeynes@66
   380
    case 0x08: /* Loop start */
nkeynes@66
   381
	channel->loop_start = val;
nkeynes@66
   382
	break;
nkeynes@73
   383
    case 0x0C: /* End */
nkeynes@73
   384
	channel->end = val;
nkeynes@66
   385
	break;
nkeynes@66
   386
    case 0x10: /* Envelope register 1 */
nkeynes@66
   387
	break;
nkeynes@66
   388
    case 0x14: /* Envelope register 2 */
nkeynes@66
   389
	break;
nkeynes@66
   390
    case 0x18: /* Frequency */
nkeynes@66
   391
	channel->sample_rate = aica_frequency_to_sample_rate ( val );
nkeynes@66
   392
	break;
nkeynes@66
   393
    case 0x1C: /* ??? */
nkeynes@66
   394
    case 0x20: /* ??? */
nkeynes@66
   395
    case 0x24: /* Volume? /pan */
nkeynes@82
   396
	val = val & 0x1F;
nkeynes@82
   397
	if( val <= 0x0F ) 
nkeynes@82
   398
	    val = 0x0F - val; /* Convert to smooth pan over 0..31 */
nkeynes@82
   399
	channel->pan = val;
nkeynes@66
   400
	break;
nkeynes@66
   401
    case 0x28: /* Volume */
nkeynes@82
   402
	channel->vol = aica_volume_table[val & 0xFF];
nkeynes@66
   403
	break;
nkeynes@66
   404
    default: /* ??? */
nkeynes@66
   405
	break;
nkeynes@66
   406
    }
nkeynes@66
   407
nkeynes@66
   408
}
.