src/haptic/nds/SDL_syshaptic.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Feb 2011 11:36:56 -0800
changeset 5270 7aba0406c273
parent 4868 d6adaafcfb10
child 5535 96594ac5fd1a
permissions -rw-r--r--
Frank Zago to sdl

The following patch fixes some of the bitrot for the Nintendo DS port.
The support is still basic at the moment, but it allows to run the "general"
test under the current head of tree (parent: 5269:11bd1585efb5 tip).
Most of the patch is mine, but I integrated a couple changes that John
Magnotti posted on Feb 1st.
dalton@2749
     1
/*
dalton@2749
     2
    SDL - Simple DirectMedia Layer
dalton@2749
     3
    Copyright (C) 2008 Edgar Simo
dalton@2749
     4
dalton@2749
     5
    This library is free software; you can redistribute it and/or
dalton@2749
     6
    modify it under the terms of the GNU Lesser General Public
dalton@2749
     7
    License as published by the Free Software Foundation; either
dalton@2749
     8
    version 2.1 of the License, or (at your option) any later version.
dalton@2749
     9
dalton@2749
    10
    This library is distributed in the hope that it will be useful,
dalton@2749
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
dalton@2749
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dalton@2749
    13
    Lesser General Public License for more details.
dalton@2749
    14
dalton@2749
    15
    You should have received a copy of the GNU Lesser General Public
dalton@2749
    16
    License along with this library; if not, write to the Free Software
dalton@2749
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
dalton@2749
    18
dalton@2749
    19
    Sam Lantinga
dalton@2749
    20
    slouken@libsdl.org
dalton@2749
    21
*/
dalton@2749
    22
#include "SDL_config.h"
dalton@2749
    23
dalton@2749
    24
#ifdef SDL_HAPTIC_NDS
dalton@2749
    25
dalton@2749
    26
#include "SDL_haptic.h"
dalton@2749
    27
#include "../SDL_syshaptic.h"
dalton@2749
    28
#include "SDL_joystick.h"
slouken@5270
    29
#include <nds/memory.h>
dalton@2749
    30
#include <nds/arm9/rumble.h>
dalton@2749
    31
dalton@2749
    32
#define MAX_HAPTICS  1
dalton@2749
    33
/* right now only the ezf3in1 (and maybe official rumble pak) are supported
dalton@2749
    34
   and there can only be one of those in at a time (in GBA slot.) */
dalton@2749
    35
slouken@5270
    36
static SDL_Haptic *nds_haptic = NULL;
dalton@2749
    37
slouken@5270
    38
struct haptic_hwdata
dalton@2749
    39
{
dalton@2749
    40
    enum
dalton@2749
    41
    { NONE, OFFICIAL, EZF3IN1 } type;
dalton@2749
    42
    int pos;
slouken@5270
    43
};
dalton@2749
    44
dalton@2749
    45
slouken@4761
    46
void
slouken@4761
    47
NDS_EZF_OpenNorWrite() 
dalton@2750
    48
{
slouken@4761
    49
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
    50
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
    51
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
    52
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
    53
    GBA_BUS[0x0E20000] = 0x1500;
slouken@4761
    54
    GBA_BUS[0x0FE0000] = 0x1500;
slouken@4868
    55
}
dalton@2749
    56
slouken@4868
    57
void
slouken@4761
    58
NDS_EZF_CloseNorWrite() 
dalton@2750
    59
{
slouken@4761
    60
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
    61
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
    62
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
    63
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
    64
    GBA_BUS[0x0E20000] = 0xD200;
slouken@4761
    65
    GBA_BUS[0x0FE0000] = 0x1500;
slouken@4761
    66
}
dalton@2750
    67
dalton@2750
    68
void
dalton@2750
    69
NDS_EZF_ChipReset()
dalton@2750
    70
{
slouken@4761
    71
    GBA_BUS[0x0000] = 0x00F0;
slouken@4761
    72
    GBA_BUS[0x1000] = 0x00F0;
slouken@4761
    73
} uint32 NDS_EZF_IsPresent() 
dalton@2750
    74
{
slouken@4761
    75
    vuint16 id1, id2;
dalton@2749
    76
dalton@2749
    77
    NDS_EZF_OpenNorWrite();
dalton@2749
    78
dalton@2749
    79
    GBA_BUS[0x0555] = 0x00AA;
dalton@2749
    80
    GBA_BUS[0x02AA] = 0x0055;
dalton@2749
    81
    GBA_BUS[0x0555] = 0x0090;
dalton@2749
    82
    GBA_BUS[0x1555] = 0x00AA;
dalton@2749
    83
    GBA_BUS[0x12AA] = 0x0055;
dalton@2749
    84
    GBA_BUS[0x1555] = 0x0090;
slouken@4761
    85
    id1 = GBA_BUS[0x0001];
slouken@4761
    86
    id2 = GBA_BUS[0x1001];
slouken@4761
    87
    if ((id1 != 0x227E) || (id2 != 0x227E)) {
dalton@2750
    88
        NDS_EZF_CloseNorWrite();
slouken@4761
    89
        return 0;
dalton@2750
    90
    }
slouken@4761
    91
    id1 = GBA_BUS[0x000E];
slouken@4761
    92
    id2 = GBA_BUS[0x100E];
dalton@2749
    93
dalton@2749
    94
    NDS_EZF_CloseNorWrite();
slouken@4761
    95
    if (id1 == 0x2218 && id2 == 0x2218) {
slouken@4761
    96
        return 1;
dalton@2749
    97
    }
slouken@4761
    98
    return 0;
slouken@4761
    99
}
slouken@4761
   100
void
slouken@4761
   101
NDS_EZF_SetShake(u8 pos) 
dalton@2749
   102
{
dalton@2750
   103
    u16 data = ((pos % 3) | 0x00F0);
slouken@4761
   104
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
   105
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
   106
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
   107
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
   108
    GBA_BUS[0x0F10000] = data;
slouken@4761
   109
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2749
   110
dalton@2750
   111
    GBA_BUS[0] = 0x0000;        /* write any value for vibration. */
dalton@2750
   112
    GBA_BUS[0] = 0x0002;
slouken@4761
   113
}
dalton@2749
   114
dalton@2749
   115
static int
dalton@2749
   116
SDL_SYS_LogicError(void)
dalton@2749
   117
{
dalton@2749
   118
    SDL_SetError("Logic error: No haptic devices available.");
dalton@2749
   119
    return 0;
dalton@2749
   120
}
dalton@2749
   121
dalton@2749
   122
dalton@2749
   123
int
dalton@2749
   124
SDL_SYS_HapticInit(void)
dalton@2749
   125
{
dalton@2749
   126
    int ret = 0;
dalton@2750
   127
    if (isRumbleInserted()) {
dalton@2749
   128
        /* official rumble pak is present. */
dalton@2749
   129
        ret = 1;
dalton@2749
   130
        printf("debug: haptic present: nintendo\n");
dalton@2750
   131
    } else if (NDS_EZF_IsPresent()) {
dalton@2749
   132
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   133
        ret = 1;
dalton@2749
   134
        printf("debug: haptic present: ezf3in1\n");
dalton@2749
   135
        NDS_EZF_ChipReset();
dalton@2749
   136
    } else {
dalton@2749
   137
        printf("debug: no haptic found\n");
dalton@2749
   138
    }
dalton@2749
   139
dalton@2749
   140
    return ret;
dalton@2749
   141
}
dalton@2749
   142
dalton@2749
   143
dalton@2749
   144
const char *
dalton@2749
   145
SDL_SYS_HapticName(int index)
dalton@2749
   146
{
dalton@2750
   147
    if (nds_haptic) {
dalton@2750
   148
        switch (nds_haptic->hwdata->type) {
dalton@2750
   149
        case OFFICIAL:
dalton@2750
   150
            return "Nintendo DS Rumble Pak";
dalton@2750
   151
        case EZF3IN1:
dalton@2750
   152
            return "EZFlash 3-in-1 Rumble";
dalton@2750
   153
        default:
dalton@2750
   154
            return NULL;
dalton@2749
   155
        }
dalton@2749
   156
    }
dalton@2749
   157
    return NULL;
dalton@2749
   158
}
dalton@2749
   159
dalton@2749
   160
dalton@2749
   161
int
dalton@2749
   162
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
dalton@2749
   163
{
dalton@2750
   164
    if (!haptic) {
dalton@2749
   165
        return -1;
dalton@2749
   166
    }
dalton@2749
   167
slouken@5270
   168
    haptic->hwdata = SDL_malloc(sizeof(struct haptic_hwdata));
dalton@2750
   169
    if (!haptic->hwdata) {
dalton@2749
   170
        SDL_OutOfMemory();
dalton@2749
   171
        return -1;
dalton@2749
   172
    }
dalton@2749
   173
    nds_haptic = haptic;
dalton@2749
   174
dalton@2749
   175
    haptic->supported = SDL_HAPTIC_CONSTANT;
dalton@2749
   176
dalton@2749
   177
    /* determine what is here, if anything */
dalton@2749
   178
    haptic->hwdata->type = NONE;
dalton@2750
   179
    if (isRumbleInserted()) {
dalton@2749
   180
        /* official rumble pak is present. */
dalton@2749
   181
        haptic->hwdata->type = OFFICIAL;
dalton@2750
   182
    } else if (NDS_EZF_IsPresent()) {
dalton@2749
   183
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   184
        haptic->hwdata->type = EZF3IN1;
dalton@2749
   185
        NDS_EZF_ChipReset();
dalton@2749
   186
    } else {
dalton@2749
   187
        /* no haptic present */
dalton@2749
   188
        SDL_SYS_LogicError();
dalton@2749
   189
        return -1;
dalton@2749
   190
    }
dalton@2749
   191
dalton@2749
   192
    return 0;
dalton@2749
   193
}
dalton@2749
   194
dalton@2749
   195
dalton@2749
   196
int
dalton@2749
   197
SDL_SYS_HapticMouse(void)
dalton@2749
   198
{
dalton@2749
   199
    return -1;
dalton@2749
   200
}
dalton@2749
   201
dalton@2749
   202
dalton@2749
   203
int
dalton@2749
   204
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
dalton@2749
   205
{
dalton@2749
   206
    return 0;
dalton@2749
   207
}
dalton@2749
   208
dalton@2749
   209
dalton@2749
   210
int
dalton@2749
   211
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   212
{
dalton@2750
   213
    /*SDL_SYS_LogicError(); */
dalton@2749
   214
    return -1;
dalton@2749
   215
}
dalton@2749
   216
dalton@2749
   217
dalton@2749
   218
int
dalton@2749
   219
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   220
{
dalton@2749
   221
    return 0;
dalton@2749
   222
}
dalton@2749
   223
dalton@2749
   224
dalton@2749
   225
void
dalton@2749
   226
SDL_SYS_HapticClose(SDL_Haptic * haptic)
dalton@2749
   227
{
dalton@2749
   228
    return;
dalton@2749
   229
}
dalton@2749
   230
dalton@2749
   231
dalton@2749
   232
void
dalton@2749
   233
SDL_SYS_HapticQuit(void)
dalton@2749
   234
{
dalton@2749
   235
    return;
dalton@2749
   236
}
dalton@2749
   237
dalton@2749
   238
dalton@2749
   239
int
dalton@2749
   240
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
dalton@2749
   241
                        struct haptic_effect *effect, SDL_HapticEffect * base)
dalton@2749
   242
{
dalton@2749
   243
    SDL_SYS_LogicError();
dalton@2749
   244
    return -1;
dalton@2749
   245
}
dalton@2749
   246
dalton@2749
   247
dalton@2749
   248
int
dalton@2749
   249
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
dalton@2749
   250
                           struct haptic_effect *effect,
dalton@2749
   251
                           SDL_HapticEffect * data)
dalton@2749
   252
{
dalton@2749
   253
    SDL_SYS_LogicError();
dalton@2749
   254
    return -1;
dalton@2749
   255
}
dalton@2749
   256
dalton@2749
   257
dalton@2749
   258
int
dalton@2749
   259
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
dalton@2749
   260
                        Uint32 iterations)
dalton@2749
   261
{
dalton@2749
   262
    SDL_SYS_LogicError();
dalton@2749
   263
    return -1;
dalton@2749
   264
}
dalton@2749
   265
dalton@2749
   266
dalton@2749
   267
int
dalton@2749
   268
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   269
{
dalton@2749
   270
    SDL_SYS_LogicError();
dalton@2749
   271
    return -1;
dalton@2749
   272
}
dalton@2749
   273
dalton@2749
   274
dalton@2749
   275
void
dalton@2749
   276
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   277
{
dalton@2749
   278
    SDL_SYS_LogicError();
dalton@2749
   279
    return;
dalton@2749
   280
}
dalton@2749
   281
dalton@2749
   282
dalton@2749
   283
int
dalton@2749
   284
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
dalton@2749
   285
                              struct haptic_effect *effect)
dalton@2749
   286
{
dalton@2749
   287
    SDL_SYS_LogicError();
dalton@2749
   288
    return -1;
dalton@2749
   289
}
dalton@2749
   290
dalton@2749
   291
dalton@2749
   292
int
dalton@2749
   293
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
dalton@2749
   294
{
dalton@2749
   295
    SDL_SYS_LogicError();
dalton@2749
   296
    return -1;
dalton@2749
   297
}
dalton@2749
   298
dalton@2749
   299
dalton@2749
   300
int
dalton@2749
   301
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
dalton@2749
   302
{
dalton@2749
   303
    SDL_SYS_LogicError();
dalton@2749
   304
    return -1;
dalton@2749
   305
}
dalton@2749
   306
dalton@2749
   307
int
dalton@2749
   308
SDL_SYS_HapticPause(SDL_Haptic * haptic)
dalton@2749
   309
{
dalton@2749
   310
    SDL_SYS_LogicError();
dalton@2749
   311
    return -1;
dalton@2749
   312
}
dalton@2749
   313
dalton@2749
   314
int
dalton@2749
   315
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
dalton@2749
   316
{
dalton@2749
   317
    SDL_SYS_LogicError();
dalton@2749
   318
    return -1;
dalton@2749
   319
}
dalton@2749
   320
dalton@2749
   321
int
dalton@2749
   322
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
dalton@2749
   323
{
dalton@2749
   324
    SDL_SYS_LogicError();
dalton@2749
   325
    return -1;
dalton@2749
   326
}
dalton@2749
   327
dalton@2749
   328
dalton@2749
   329
dalton@2749
   330
#endif /* SDL_HAPTIC_NDS */
dalton@2749
   331
/* vi: set ts=4 sw=4 expandtab: */