Search
lxdream.org :: lxdream/src/asic.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/asic.c
changeset 158:a0a82246b44e
prev155:be61d1a20937
next163:cf6b5e87f58d
author nkeynes
date Thu Jun 15 10:32:42 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix IDE DMA to actually work for real.
Implement the mystery packet 0x71 by cribbing a block of data from the dc.
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@158
     2
 * $Id: asic.c,v 1.16 2006-06-15 10:32:38 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@31
     4
 * Support for the miscellaneous ASIC functions (Primarily event multiplexing,
nkeynes@31
     5
 * and DMA). 
nkeynes@31
     6
 *
nkeynes@31
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     8
 *
nkeynes@31
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    12
 * (at your option) any later version.
nkeynes@31
    13
 *
nkeynes@31
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    17
 * GNU General Public License for more details.
nkeynes@31
    18
 */
nkeynes@35
    19
nkeynes@35
    20
#define MODULE asic_module
nkeynes@35
    21
nkeynes@1
    22
#include <assert.h>
nkeynes@137
    23
#include <stdlib.h>
nkeynes@1
    24
#include "dream.h"
nkeynes@1
    25
#include "mem.h"
nkeynes@1
    26
#include "sh4/intc.h"
nkeynes@56
    27
#include "sh4/dmac.h"
nkeynes@2
    28
#include "dreamcast.h"
nkeynes@25
    29
#include "maple/maple.h"
nkeynes@25
    30
#include "gdrom/ide.h"
nkeynes@15
    31
#include "asic.h"
nkeynes@1
    32
#define MMIO_IMPL
nkeynes@1
    33
#include "asic.h"
nkeynes@1
    34
/*
nkeynes@1
    35
 * Open questions:
nkeynes@1
    36
 *   1) Does changing the mask after event occurance result in the
nkeynes@1
    37
 *      interrupt being delivered immediately?
nkeynes@1
    38
 * TODO: Logic diagram of ASIC event/interrupt logic.
nkeynes@1
    39
 *
nkeynes@1
    40
 * ... don't even get me started on the "EXTDMA" page, about which, apparently,
nkeynes@1
    41
 * practically nothing is publicly known...
nkeynes@1
    42
 */
nkeynes@1
    43
nkeynes@155
    44
static void asic_check_cleared_events( void );
nkeynes@155
    45
static void asic_init( void );
nkeynes@155
    46
static void asic_reset( void );
nkeynes@155
    47
static void asic_save_state( FILE *f );
nkeynes@155
    48
static int asic_load_state( FILE *f );
nkeynes@155
    49
nkeynes@155
    50
struct dreamcast_module asic_module = { "ASIC", asic_init, asic_reset, NULL, NULL,
nkeynes@155
    51
					NULL, asic_save_state, asic_load_state };
nkeynes@15
    52
nkeynes@137
    53
#define G2_BIT5_TICKS 8
nkeynes@137
    54
#define G2_BIT4_TICKS 16
nkeynes@137
    55
#define G2_BIT0_ON_TICKS 24
nkeynes@137
    56
#define G2_BIT0_OFF_TICKS 24
nkeynes@137
    57
nkeynes@137
    58
struct asic_g2_state {
nkeynes@137
    59
    unsigned int bit5_off_timer;
nkeynes@137
    60
    unsigned int bit4_on_timer;
nkeynes@137
    61
    unsigned int bit4_off_timer;
nkeynes@137
    62
    unsigned int bit0_on_timer;
nkeynes@137
    63
    unsigned int bit0_off_timer;
nkeynes@155
    64
};
nkeynes@155
    65
nkeynes@155
    66
static struct asic_g2_state g2_state;
nkeynes@155
    67
nkeynes@155
    68
static void asic_init( void )
nkeynes@155
    69
{
nkeynes@155
    70
    register_io_region( &mmio_region_ASIC );
nkeynes@155
    71
    register_io_region( &mmio_region_EXTDMA );
nkeynes@155
    72
    mmio_region_ASIC.trace_flag = 0; /* Because this is called so often */
nkeynes@155
    73
    asic_reset();
nkeynes@155
    74
}
nkeynes@155
    75
nkeynes@155
    76
static void asic_reset( void )
nkeynes@155
    77
{
nkeynes@155
    78
    memset( &g2_state, 0, sizeof(g2_state) );
nkeynes@155
    79
}    
nkeynes@155
    80
nkeynes@155
    81
static void asic_save_state( FILE *f )
nkeynes@155
    82
{
nkeynes@155
    83
    fwrite( &g2_state, sizeof(g2_state), 1, f );
nkeynes@155
    84
}
nkeynes@155
    85
nkeynes@155
    86
static int asic_load_state( FILE *f )
nkeynes@155
    87
{
nkeynes@155
    88
    if( fread( &g2_state, sizeof(g2_state), 1, f ) != 1 )
nkeynes@155
    89
	return 1;
nkeynes@155
    90
    else
nkeynes@155
    91
	return 0;
nkeynes@155
    92
}
nkeynes@155
    93
nkeynes@137
    94
nkeynes@137
    95
/* FIXME: Handle rollover */
nkeynes@137
    96
void asic_g2_write_word()
nkeynes@137
    97
{
nkeynes@137
    98
    g2_state.bit5_off_timer = sh4r.icount + G2_BIT5_TICKS;
nkeynes@137
    99
    if( g2_state.bit4_off_timer < sh4r.icount )
nkeynes@137
   100
	g2_state.bit4_on_timer = sh4r.icount + G2_BIT5_TICKS;
nkeynes@137
   101
    g2_state.bit4_off_timer = max(sh4r.icount,g2_state.bit4_off_timer) + G2_BIT4_TICKS;
nkeynes@137
   102
    if( g2_state.bit0_off_timer < sh4r.icount ) {
nkeynes@137
   103
	g2_state.bit0_on_timer = sh4r.icount + G2_BIT0_ON_TICKS;
nkeynes@137
   104
	g2_state.bit0_off_timer = g2_state.bit0_on_timer + G2_BIT0_OFF_TICKS;
nkeynes@137
   105
    } else {
nkeynes@137
   106
	g2_state.bit0_off_timer += G2_BIT0_OFF_TICKS;
nkeynes@137
   107
    }
nkeynes@137
   108
    MMIO_WRITE( ASIC, G2STATUS, MMIO_READ(ASIC, G2STATUS) | 0x20 );
nkeynes@137
   109
}
nkeynes@137
   110
nkeynes@137
   111
static uint32_t g2_read_status()
nkeynes@137
   112
{
nkeynes@137
   113
    uint32_t val = MMIO_READ( ASIC, G2STATUS );
nkeynes@137
   114
    if( g2_state.bit5_off_timer <= sh4r.icount )
nkeynes@137
   115
	val = val & (~0x20);
nkeynes@137
   116
    if( g2_state.bit4_off_timer <= sh4r.icount )
nkeynes@137
   117
	val = val & (~0x10);
nkeynes@137
   118
    else if( g2_state.bit4_on_timer <= sh4r.icount )
nkeynes@137
   119
	val = val | 0x10;
nkeynes@137
   120
    if( g2_state.bit0_off_timer <= sh4r.icount )
nkeynes@137
   121
	val = val & (~0x01);
nkeynes@137
   122
    else if( g2_state.bit0_on_timer <= sh4r.icount )
nkeynes@137
   123
	val = val | 0x01;
nkeynes@137
   124
    return val | 0x0E;
nkeynes@137
   125
}   
nkeynes@137
   126
nkeynes@20
   127
nkeynes@155
   128
void asic_event( int event )
nkeynes@1
   129
{
nkeynes@155
   130
    int offset = ((event&0x60)>>3);
nkeynes@155
   131
    int result = (MMIO_READ(ASIC, PIRQ0 + offset))  |=  (1<<(event&0x1F));
nkeynes@155
   132
nkeynes@155
   133
    if( result & MMIO_READ(ASIC, IRQA0 + offset) )
nkeynes@155
   134
        intc_raise_interrupt( INT_IRQ13 );
nkeynes@155
   135
    if( result & MMIO_READ(ASIC, IRQB0 + offset) )
nkeynes@155
   136
        intc_raise_interrupt( INT_IRQ11 );
nkeynes@155
   137
    if( result & MMIO_READ(ASIC, IRQC0 + offset) )
nkeynes@155
   138
        intc_raise_interrupt( INT_IRQ9 );
nkeynes@1
   139
}
nkeynes@1
   140
nkeynes@155
   141
void asic_clear_event( int event ) {
nkeynes@155
   142
    int offset = ((event&0x60)>>3);
nkeynes@155
   143
    uint32_t result = MMIO_READ(ASIC, PIRQ0 + offset)  & (~(1<<(event&0x1F)));
nkeynes@155
   144
    MMIO_WRITE( ASIC, PIRQ0 + offset, result );
nkeynes@155
   145
nkeynes@155
   146
    asic_check_cleared_events();
nkeynes@155
   147
}
nkeynes@155
   148
nkeynes@155
   149
void asic_check_cleared_events( )
nkeynes@155
   150
{
nkeynes@155
   151
    int i, setA = 0, setB = 0, setC = 0;
nkeynes@155
   152
    uint32_t bits;
nkeynes@155
   153
    for( i=0; i<3; i++ ) {
nkeynes@155
   154
	bits = MMIO_READ( ASIC, PIRQ0 + i );
nkeynes@155
   155
	setA |= (bits & MMIO_READ(ASIC, IRQA0 + i ));
nkeynes@155
   156
	setB |= (bits & MMIO_READ(ASIC, IRQB0 + i ));
nkeynes@155
   157
	setC |= (bits & MMIO_READ(ASIC, IRQC0 + i ));
nkeynes@155
   158
    }
nkeynes@155
   159
    if( setA == 0 )
nkeynes@155
   160
	intc_clear_interrupt( INT_IRQ13 );
nkeynes@155
   161
    if( setB == 0 )
nkeynes@155
   162
	intc_clear_interrupt( INT_IRQ11 );
nkeynes@155
   163
    if( setC == 0 )
nkeynes@155
   164
	intc_clear_interrupt( INT_IRQ9 );
nkeynes@155
   165
}
nkeynes@155
   166
nkeynes@155
   167
nkeynes@155
   168
void asic_ide_dma_transfer( )
nkeynes@155
   169
{	
nkeynes@158
   170
    if( MMIO_READ( EXTDMA, IDEDMACTL2 ) == 1 ) {
nkeynes@158
   171
	if( MMIO_READ( EXTDMA, IDEDMACTL1 ) == 1 ) {
nkeynes@158
   172
	    MMIO_WRITE( EXTDMA, IDEDMATXSIZ, 0 );
nkeynes@158
   173
	    
nkeynes@158
   174
	    uint32_t addr = MMIO_READ( EXTDMA, IDEDMASH4 );
nkeynes@158
   175
	    uint32_t length = MMIO_READ( EXTDMA, IDEDMASIZ );
nkeynes@158
   176
	    int dir = MMIO_READ( EXTDMA, IDEDMADIR );
nkeynes@158
   177
	    
nkeynes@158
   178
	    uint32_t xfer = ide_read_data_dma( addr, length );
nkeynes@158
   179
	    MMIO_WRITE( EXTDMA, IDEDMATXSIZ, xfer );
nkeynes@158
   180
	    MMIO_WRITE( EXTDMA, IDEDMACTL2, 0 );
nkeynes@158
   181
	} else { /* 0 */
nkeynes@158
   182
	    MMIO_WRITE( EXTDMA, IDEDMACTL2, 0 );
nkeynes@155
   183
	}
nkeynes@155
   184
    }
nkeynes@155
   185
nkeynes@155
   186
}
nkeynes@155
   187
nkeynes@155
   188
nkeynes@1
   189
void mmio_region_ASIC_write( uint32_t reg, uint32_t val )
nkeynes@1
   190
{
nkeynes@1
   191
    switch( reg ) {
nkeynes@125
   192
    case PIRQ1:
nkeynes@125
   193
	val = val & 0xFFFFFFFE; /* Prevent the IDE event from clearing */
nkeynes@125
   194
	/* fallthrough */
nkeynes@56
   195
    case PIRQ0:
nkeynes@56
   196
    case PIRQ2:
nkeynes@56
   197
	/* Clear any interrupts */
nkeynes@56
   198
	MMIO_WRITE( ASIC, reg, MMIO_READ(ASIC, reg)&~val );
nkeynes@56
   199
	asic_check_cleared_events();
nkeynes@56
   200
	break;
nkeynes@56
   201
    case MAPLE_STATE:
nkeynes@56
   202
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
   203
	if( val & 1 ) {
nkeynes@56
   204
	    uint32_t maple_addr = MMIO_READ( ASIC, MAPLE_DMA) &0x1FFFFFE0;
nkeynes@155
   205
	    // WARN( "Maple request initiated at %08X, halting", maple_addr );
nkeynes@56
   206
	    maple_handle_buffer( maple_addr );
nkeynes@56
   207
	    MMIO_WRITE( ASIC, reg, 0 );
nkeynes@56
   208
	}
nkeynes@56
   209
	break;
nkeynes@56
   210
    case PVRDMACTL: /* Initiate PVR DMA transfer */
nkeynes@94
   211
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
   212
	if( val & 1 ) {
nkeynes@56
   213
	    uint32_t dest_addr = MMIO_READ( ASIC, PVRDMADEST) &0x1FFFFFE0;
nkeynes@56
   214
	    uint32_t count = MMIO_READ( ASIC, PVRDMACNT );
nkeynes@56
   215
	    char *data = alloca( count );
nkeynes@56
   216
	    uint32_t rcount = DMAC_get_buffer( 2, data, count );
nkeynes@56
   217
	    if( rcount != count )
nkeynes@56
   218
		WARN( "PVR received %08X bytes from DMA, expected %08X", rcount, count );
nkeynes@100
   219
	    mem_copy_to_sh4( dest_addr, data, rcount );
nkeynes@56
   220
	    asic_event( EVENT_PVR_DMA );
nkeynes@56
   221
	}
nkeynes@56
   222
	break;
nkeynes@158
   223
    case PVRDMADEST: case PVRDMACNT: case MAPLE_DMA:
nkeynes@158
   224
	MMIO_WRITE( ASIC, reg, val );
nkeynes@158
   225
	break;
nkeynes@56
   226
    default:
nkeynes@56
   227
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
   228
	WARN( "Write to ASIC (%03X <= %08X) [%s: %s]",
nkeynes@56
   229
	      reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@1
   230
    }
nkeynes@1
   231
}
nkeynes@1
   232
nkeynes@1
   233
int32_t mmio_region_ASIC_read( uint32_t reg )
nkeynes@1
   234
{
nkeynes@1
   235
    int32_t val;
nkeynes@1
   236
    switch( reg ) {
nkeynes@2
   237
        /*
nkeynes@2
   238
        case 0x89C:
nkeynes@2
   239
            sh4_stop();
nkeynes@2
   240
            return 0x000000B;
nkeynes@2
   241
        */     
nkeynes@94
   242
    case PIRQ0:
nkeynes@94
   243
    case PIRQ1:
nkeynes@94
   244
    case PIRQ2:
nkeynes@94
   245
    case IRQA0:
nkeynes@94
   246
    case IRQA1:
nkeynes@94
   247
    case IRQA2:
nkeynes@94
   248
    case IRQB0:
nkeynes@94
   249
    case IRQB1:
nkeynes@94
   250
    case IRQB2:
nkeynes@94
   251
    case IRQC0:
nkeynes@94
   252
    case IRQC1:
nkeynes@94
   253
    case IRQC2:
nkeynes@158
   254
    case MAPLE_STATE:
nkeynes@94
   255
	val = MMIO_READ(ASIC, reg);
nkeynes@94
   256
	//            WARN( "Read from ASIC (%03X => %08X) [%s: %s]",
nkeynes@94
   257
	//                  reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@94
   258
	return val;            
nkeynes@94
   259
    case G2STATUS:
nkeynes@137
   260
	return g2_read_status();
nkeynes@94
   261
    default:
nkeynes@94
   262
	val = MMIO_READ(ASIC, reg);
nkeynes@94
   263
	WARN( "Read from ASIC (%03X => %08X) [%s: %s]",
nkeynes@94
   264
	      reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@94
   265
	return val;
nkeynes@1
   266
    }
nkeynes@94
   267
    
nkeynes@1
   268
}
nkeynes@1
   269
nkeynes@1
   270
MMIO_REGION_WRITE_FN( EXTDMA, reg, val )
nkeynes@1
   271
{
nkeynes@158
   272
    // WARN( "EXTDMA write %08X <= %08X", reg, val );
nkeynes@125
   273
nkeynes@2
   274
    switch( reg ) {
nkeynes@125
   275
    case IDEALTSTATUS: /* Device control */
nkeynes@125
   276
	ide_write_control( val );
nkeynes@125
   277
	break;
nkeynes@125
   278
    case IDEDATA:
nkeynes@125
   279
	ide_write_data_pio( val );
nkeynes@125
   280
	break;
nkeynes@125
   281
    case IDEFEAT:
nkeynes@125
   282
	if( ide_can_write_regs() )
nkeynes@125
   283
	    idereg.feature = (uint8_t)val;
nkeynes@125
   284
	break;
nkeynes@125
   285
    case IDECOUNT:
nkeynes@125
   286
	if( ide_can_write_regs() )
nkeynes@125
   287
	    idereg.count = (uint8_t)val;
nkeynes@125
   288
	break;
nkeynes@125
   289
    case IDELBA0:
nkeynes@125
   290
	if( ide_can_write_regs() )
nkeynes@125
   291
	    idereg.lba0 = (uint8_t)val;
nkeynes@125
   292
	break;
nkeynes@125
   293
    case IDELBA1:
nkeynes@125
   294
	if( ide_can_write_regs() )
nkeynes@125
   295
	    idereg.lba1 = (uint8_t)val;
nkeynes@125
   296
	break;
nkeynes@125
   297
    case IDELBA2:
nkeynes@125
   298
	if( ide_can_write_regs() )
nkeynes@125
   299
	    idereg.lba2 = (uint8_t)val;
nkeynes@125
   300
	break;
nkeynes@125
   301
    case IDEDEV:
nkeynes@125
   302
	if( ide_can_write_regs() )
nkeynes@125
   303
	    idereg.device = (uint8_t)val;
nkeynes@125
   304
	break;
nkeynes@125
   305
    case IDECMD:
nkeynes@125
   306
	if( ide_can_write_regs() ) {
nkeynes@125
   307
	    ide_write_command( (uint8_t)val );
nkeynes@125
   308
	}
nkeynes@125
   309
	break;
nkeynes@125
   310
    case IDEDMACTL1:
nkeynes@155
   311
	MMIO_WRITE( EXTDMA, reg, val );
nkeynes@125
   312
    case IDEDMACTL2:
nkeynes@125
   313
	MMIO_WRITE( EXTDMA, reg, val );
nkeynes@155
   314
	asic_ide_dma_transfer( );
nkeynes@125
   315
	break;
nkeynes@125
   316
    default:
nkeynes@2
   317
            MMIO_WRITE( EXTDMA, reg, val );
nkeynes@2
   318
    }
nkeynes@1
   319
}
nkeynes@1
   320
nkeynes@1
   321
MMIO_REGION_READ_FN( EXTDMA, reg )
nkeynes@1
   322
{
nkeynes@56
   323
    uint32_t val;
nkeynes@1
   324
    switch( reg ) {
nkeynes@158
   325
    case IDEALTSTATUS: 
nkeynes@158
   326
	val = idereg.status;
nkeynes@158
   327
	return val;
nkeynes@158
   328
    case IDEDATA: return ide_read_data_pio( );
nkeynes@158
   329
    case IDEFEAT: return idereg.error;
nkeynes@158
   330
    case IDECOUNT:return idereg.count;
nkeynes@158
   331
    case IDELBA0: return idereg.disc;
nkeynes@158
   332
    case IDELBA1: return idereg.lba1;
nkeynes@158
   333
    case IDELBA2: return idereg.lba2;
nkeynes@158
   334
    case IDEDEV: return idereg.device;
nkeynes@158
   335
    case IDECMD:
nkeynes@158
   336
	val = ide_read_status();
nkeynes@158
   337
	return val;
nkeynes@158
   338
    default:
nkeynes@158
   339
	val = MMIO_READ( EXTDMA, reg );
nkeynes@158
   340
	return val;
nkeynes@1
   341
    }
nkeynes@1
   342
}
nkeynes@1
   343
.