Search
lxdream.org :: lxdream/src/asic.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/asic.c
changeset 56:3224dceaf2a3
prev42:d06affd949ec
next94:8d80d9c7cc7d
author nkeynes
date Sun Jan 01 08:09:42 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Implement PVR DMA channel
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@56
     2
 * $Id: asic.c,v 1.10 2006-01-01 08:09:42 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@1
    23
#include "dream.h"
nkeynes@1
    24
#include "mem.h"
nkeynes@1
    25
#include "sh4/intc.h"
nkeynes@56
    26
#include "sh4/dmac.h"
nkeynes@2
    27
#include "dreamcast.h"
nkeynes@25
    28
#include "maple/maple.h"
nkeynes@25
    29
#include "gdrom/ide.h"
nkeynes@15
    30
#include "asic.h"
nkeynes@1
    31
#define MMIO_IMPL
nkeynes@1
    32
#include "asic.h"
nkeynes@1
    33
/*
nkeynes@1
    34
 * Open questions:
nkeynes@1
    35
 *   1) Does changing the mask after event occurance result in the
nkeynes@1
    36
 *      interrupt being delivered immediately?
nkeynes@1
    37
 * TODO: Logic diagram of ASIC event/interrupt logic.
nkeynes@1
    38
 *
nkeynes@1
    39
 * ... don't even get me started on the "EXTDMA" page, about which, apparently,
nkeynes@1
    40
 * practically nothing is publicly known...
nkeynes@1
    41
 */
nkeynes@1
    42
nkeynes@15
    43
struct dreamcast_module asic_module = { "ASIC", asic_init, NULL, NULL, NULL,
nkeynes@23
    44
					NULL, NULL, NULL };
nkeynes@15
    45
nkeynes@20
    46
void asic_check_cleared_events( void );
nkeynes@20
    47
nkeynes@1
    48
void asic_init( void )
nkeynes@1
    49
{
nkeynes@1
    50
    register_io_region( &mmio_region_ASIC );
nkeynes@1
    51
    register_io_region( &mmio_region_EXTDMA );
nkeynes@1
    52
    mmio_region_ASIC.trace_flag = 0; /* Because this is called so often */
nkeynes@1
    53
    asic_event( EVENT_GDROM_CMD );
nkeynes@1
    54
}
nkeynes@1
    55
nkeynes@1
    56
void mmio_region_ASIC_write( uint32_t reg, uint32_t val )
nkeynes@1
    57
{
nkeynes@1
    58
    switch( reg ) {
nkeynes@56
    59
    case PIRQ0:
nkeynes@56
    60
    case PIRQ1:
nkeynes@56
    61
    case PIRQ2:
nkeynes@56
    62
	/* Clear any interrupts */
nkeynes@56
    63
	MMIO_WRITE( ASIC, reg, MMIO_READ(ASIC, reg)&~val );
nkeynes@56
    64
	asic_check_cleared_events();
nkeynes@56
    65
	break;
nkeynes@56
    66
    case MAPLE_STATE:
nkeynes@56
    67
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
    68
	if( val & 1 ) {
nkeynes@56
    69
	    uint32_t maple_addr = MMIO_READ( ASIC, MAPLE_DMA) &0x1FFFFFE0;
nkeynes@56
    70
	    WARN( "Maple request initiated at %08X, halting", maple_addr );
nkeynes@56
    71
	    maple_handle_buffer( maple_addr );
nkeynes@56
    72
	    MMIO_WRITE( ASIC, reg, 0 );
nkeynes@56
    73
	}
nkeynes@56
    74
	break;
nkeynes@56
    75
    case PVRDMACTL: /* Initiate PVR DMA transfer */
nkeynes@56
    76
	if( val & 1 ) {
nkeynes@56
    77
	    uint32_t dest_addr = MMIO_READ( ASIC, PVRDMADEST) &0x1FFFFFE0;
nkeynes@56
    78
	    uint32_t count = MMIO_READ( ASIC, PVRDMACNT );
nkeynes@56
    79
	    char *data = alloca( count );
nkeynes@56
    80
	    uint32_t rcount = DMAC_get_buffer( 2, data, count );
nkeynes@56
    81
	    if( rcount != count )
nkeynes@56
    82
		WARN( "PVR received %08X bytes from DMA, expected %08X", rcount, count );
nkeynes@56
    83
	    if( (dest_addr &0xF0000000) == 0x10000000 ) { /* TA */
nkeynes@56
    84
		pvr2ta_write( data, rcount );
nkeynes@56
    85
	    }
nkeynes@56
    86
	    asic_event( EVENT_PVR_DMA );
nkeynes@56
    87
	}
nkeynes@56
    88
	break;
nkeynes@56
    89
    default:
nkeynes@56
    90
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
    91
	WARN( "Write to ASIC (%03X <= %08X) [%s: %s]",
nkeynes@56
    92
	      reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@1
    93
    }
nkeynes@1
    94
}
nkeynes@1
    95
nkeynes@1
    96
int32_t mmio_region_ASIC_read( uint32_t reg )
nkeynes@1
    97
{
nkeynes@1
    98
    int32_t val;
nkeynes@1
    99
    switch( reg ) {
nkeynes@2
   100
        /*
nkeynes@2
   101
        case 0x89C:
nkeynes@2
   102
            sh4_stop();
nkeynes@2
   103
            return 0x000000B;
nkeynes@2
   104
        */     
nkeynes@1
   105
        case PIRQ0:
nkeynes@1
   106
        case PIRQ1:
nkeynes@1
   107
        case PIRQ2:
nkeynes@1
   108
            val = MMIO_READ(ASIC, reg);
nkeynes@1
   109
//            WARN( "Read from ASIC (%03X => %08X) [%s: %s]",
nkeynes@1
   110
//                  reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@1
   111
            return val;            
nkeynes@1
   112
        case G2STATUS:
nkeynes@1
   113
            return 0; /* find out later if there's any cases we actually need to care about */
nkeynes@1
   114
        default:
nkeynes@1
   115
            val = MMIO_READ(ASIC, reg);
nkeynes@1
   116
            WARN( "Read from ASIC (%03X => %08X) [%s: %s]",
nkeynes@1
   117
                  reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@1
   118
            return val;
nkeynes@1
   119
    }
nkeynes@1
   120
           
nkeynes@1
   121
}
nkeynes@1
   122
nkeynes@1
   123
void asic_event( int event )
nkeynes@1
   124
{
nkeynes@1
   125
    int offset = ((event&0x60)>>3);
nkeynes@1
   126
    int result = (MMIO_READ(ASIC, PIRQ0 + offset))  |=  (1<<(event&0x1F));
nkeynes@1
   127
nkeynes@1
   128
    if( result & MMIO_READ(ASIC, IRQA0 + offset) )
nkeynes@1
   129
        intc_raise_interrupt( INT_IRQ13 );
nkeynes@1
   130
    if( result & MMIO_READ(ASIC, IRQB0 + offset) )
nkeynes@1
   131
        intc_raise_interrupt( INT_IRQ11 );
nkeynes@1
   132
    if( result & MMIO_READ(ASIC, IRQC0 + offset) )
nkeynes@1
   133
        intc_raise_interrupt( INT_IRQ9 );
nkeynes@1
   134
}
nkeynes@1
   135
nkeynes@20
   136
void asic_check_cleared_events( )
nkeynes@20
   137
{
nkeynes@20
   138
    int i, setA = 0, setB = 0, setC = 0;
nkeynes@20
   139
    uint32_t bits;
nkeynes@20
   140
    for( i=0; i<3; i++ ) {
nkeynes@20
   141
	bits = MMIO_READ( ASIC, PIRQ0 + i );
nkeynes@20
   142
	setA |= (bits & MMIO_READ(ASIC, IRQA0 + i ));
nkeynes@20
   143
	setB |= (bits & MMIO_READ(ASIC, IRQB0 + i ));
nkeynes@20
   144
	setC |= (bits & MMIO_READ(ASIC, IRQC0 + i ));
nkeynes@20
   145
    }
nkeynes@20
   146
    if( setA == 0 )
nkeynes@20
   147
	intc_clear_interrupt( INT_IRQ13 );
nkeynes@20
   148
    if( setB == 0 )
nkeynes@20
   149
	intc_clear_interrupt( INT_IRQ11 );
nkeynes@20
   150
    if( setC == 0 )
nkeynes@20
   151
	intc_clear_interrupt( INT_IRQ9 );
nkeynes@20
   152
}
nkeynes@1
   153
nkeynes@1
   154
nkeynes@1
   155
MMIO_REGION_WRITE_FN( EXTDMA, reg, val )
nkeynes@1
   156
{
nkeynes@2
   157
    switch( reg ) {
nkeynes@2
   158
        case IDEALTSTATUS: /* Device control */
nkeynes@2
   159
            ide_write_control( val );
nkeynes@2
   160
            break;
nkeynes@2
   161
        case IDEDATA:
nkeynes@2
   162
            ide_write_data_pio( val );
nkeynes@2
   163
            break;
nkeynes@2
   164
        case IDEFEAT:
nkeynes@2
   165
            if( ide_can_write_regs() )
nkeynes@2
   166
                idereg.feature = (uint8_t)val;
nkeynes@2
   167
            break;
nkeynes@2
   168
        case IDECOUNT:
nkeynes@2
   169
            if( ide_can_write_regs() )
nkeynes@2
   170
                idereg.count = (uint8_t)val;
nkeynes@2
   171
            break;
nkeynes@2
   172
        case IDELBA0:
nkeynes@2
   173
            if( ide_can_write_regs() )
nkeynes@2
   174
                idereg.lba0 = (uint8_t)val;
nkeynes@2
   175
            break;
nkeynes@2
   176
        case IDELBA1:
nkeynes@2
   177
            if( ide_can_write_regs() )
nkeynes@2
   178
                idereg.lba1 = (uint8_t)val;
nkeynes@2
   179
            break;
nkeynes@2
   180
        case IDELBA2:
nkeynes@2
   181
            if( ide_can_write_regs() )
nkeynes@2
   182
                idereg.lba2 = (uint8_t)val;
nkeynes@2
   183
            break;
nkeynes@2
   184
        case IDEDEV:
nkeynes@2
   185
            if( ide_can_write_regs() )
nkeynes@2
   186
                idereg.device = (uint8_t)val;
nkeynes@2
   187
            break;
nkeynes@2
   188
        case IDECMD:
nkeynes@2
   189
            if( ide_can_write_regs() ) {
nkeynes@2
   190
                ide_clear_interrupt();
nkeynes@2
   191
                ide_write_command( (uint8_t)val );
nkeynes@2
   192
            }
nkeynes@2
   193
            break;
nkeynes@2
   194
        default:
nkeynes@56
   195
	    WARN( "EXTDMA write %08X <= %08X", reg, val );
nkeynes@56
   196
nkeynes@2
   197
            MMIO_WRITE( EXTDMA, reg, val );
nkeynes@2
   198
    }
nkeynes@1
   199
}
nkeynes@1
   200
nkeynes@1
   201
MMIO_REGION_READ_FN( EXTDMA, reg )
nkeynes@1
   202
{
nkeynes@56
   203
    uint32_t val;
nkeynes@1
   204
    switch( reg ) {
nkeynes@2
   205
        case IDEALTSTATUS: return idereg.status;
nkeynes@2
   206
        case IDEDATA: return ide_read_data_pio( );
nkeynes@2
   207
        case IDEFEAT: return idereg.error;
nkeynes@2
   208
        case IDECOUNT:return idereg.count;
nkeynes@2
   209
        case IDELBA0: return idereg.disc;
nkeynes@2
   210
        case IDELBA1: return idereg.lba1;
nkeynes@2
   211
        case IDELBA2: return idereg.lba2;
nkeynes@2
   212
        case IDEDEV: return idereg.device;
nkeynes@2
   213
        case IDECMD:
nkeynes@2
   214
            ide_clear_interrupt();
nkeynes@2
   215
            return idereg.status;
nkeynes@1
   216
        default:
nkeynes@56
   217
	    val = MMIO_READ( EXTDMA, reg );
nkeynes@56
   218
	    DEBUG( "EXTDMA read %08X => %08X", reg, val );
nkeynes@56
   219
	    return val;
nkeynes@1
   220
    }
nkeynes@1
   221
}
nkeynes@1
   222
.