src/haptic/nds/SDL_syshaptic.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5270 7aba0406c273
child 6138 4c64952a58fb
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
dalton@2749
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
dalton@2749
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
dalton@2749
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
dalton@2749
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
dalton@2749
    20
*/
dalton@2749
    21
#include "SDL_config.h"
dalton@2749
    22
dalton@2749
    23
#ifdef SDL_HAPTIC_NDS
dalton@2749
    24
dalton@2749
    25
#include "SDL_haptic.h"
dalton@2749
    26
#include "../SDL_syshaptic.h"
dalton@2749
    27
#include "SDL_joystick.h"
slouken@5270
    28
#include <nds/memory.h>
dalton@2749
    29
#include <nds/arm9/rumble.h>
dalton@2749
    30
dalton@2749
    31
#define MAX_HAPTICS  1
dalton@2749
    32
/* right now only the ezf3in1 (and maybe official rumble pak) are supported
dalton@2749
    33
   and there can only be one of those in at a time (in GBA slot.) */
dalton@2749
    34
slouken@5270
    35
static SDL_Haptic *nds_haptic = NULL;
dalton@2749
    36
slouken@5270
    37
struct haptic_hwdata
dalton@2749
    38
{
dalton@2749
    39
    enum
dalton@2749
    40
    { NONE, OFFICIAL, EZF3IN1 } type;
dalton@2749
    41
    int pos;
slouken@5270
    42
};
dalton@2749
    43
dalton@2749
    44
slouken@4761
    45
void
slouken@4761
    46
NDS_EZF_OpenNorWrite() 
dalton@2750
    47
{
slouken@4761
    48
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
    49
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
    50
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
    51
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
    52
    GBA_BUS[0x0E20000] = 0x1500;
slouken@4761
    53
    GBA_BUS[0x0FE0000] = 0x1500;
slouken@4868
    54
}
dalton@2749
    55
slouken@4868
    56
void
slouken@4761
    57
NDS_EZF_CloseNorWrite() 
dalton@2750
    58
{
slouken@4761
    59
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
    60
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
    61
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
    62
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
    63
    GBA_BUS[0x0E20000] = 0xD200;
slouken@4761
    64
    GBA_BUS[0x0FE0000] = 0x1500;
slouken@4761
    65
}
dalton@2750
    66
dalton@2750
    67
void
dalton@2750
    68
NDS_EZF_ChipReset()
dalton@2750
    69
{
slouken@4761
    70
    GBA_BUS[0x0000] = 0x00F0;
slouken@4761
    71
    GBA_BUS[0x1000] = 0x00F0;
slouken@4761
    72
} uint32 NDS_EZF_IsPresent() 
dalton@2750
    73
{
slouken@4761
    74
    vuint16 id1, id2;
dalton@2749
    75
dalton@2749
    76
    NDS_EZF_OpenNorWrite();
dalton@2749
    77
dalton@2749
    78
    GBA_BUS[0x0555] = 0x00AA;
dalton@2749
    79
    GBA_BUS[0x02AA] = 0x0055;
dalton@2749
    80
    GBA_BUS[0x0555] = 0x0090;
dalton@2749
    81
    GBA_BUS[0x1555] = 0x00AA;
dalton@2749
    82
    GBA_BUS[0x12AA] = 0x0055;
dalton@2749
    83
    GBA_BUS[0x1555] = 0x0090;
slouken@4761
    84
    id1 = GBA_BUS[0x0001];
slouken@4761
    85
    id2 = GBA_BUS[0x1001];
slouken@4761
    86
    if ((id1 != 0x227E) || (id2 != 0x227E)) {
dalton@2750
    87
        NDS_EZF_CloseNorWrite();
slouken@4761
    88
        return 0;
dalton@2750
    89
    }
slouken@4761
    90
    id1 = GBA_BUS[0x000E];
slouken@4761
    91
    id2 = GBA_BUS[0x100E];
dalton@2749
    92
dalton@2749
    93
    NDS_EZF_CloseNorWrite();
slouken@4761
    94
    if (id1 == 0x2218 && id2 == 0x2218) {
slouken@4761
    95
        return 1;
dalton@2749
    96
    }
slouken@4761
    97
    return 0;
slouken@4761
    98
}
slouken@4761
    99
void
slouken@4761
   100
NDS_EZF_SetShake(u8 pos) 
dalton@2749
   101
{
dalton@2750
   102
    u16 data = ((pos % 3) | 0x00F0);
slouken@4761
   103
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
   104
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
   105
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
   106
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
   107
    GBA_BUS[0x0F10000] = data;
slouken@4761
   108
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2749
   109
dalton@2750
   110
    GBA_BUS[0] = 0x0000;        /* write any value for vibration. */
dalton@2750
   111
    GBA_BUS[0] = 0x0002;
slouken@4761
   112
}
dalton@2749
   113
dalton@2749
   114
static int
dalton@2749
   115
SDL_SYS_LogicError(void)
dalton@2749
   116
{
dalton@2749
   117
    SDL_SetError("Logic error: No haptic devices available.");
dalton@2749
   118
    return 0;
dalton@2749
   119
}
dalton@2749
   120
dalton@2749
   121
dalton@2749
   122
int
dalton@2749
   123
SDL_SYS_HapticInit(void)
dalton@2749
   124
{
dalton@2749
   125
    int ret = 0;
dalton@2750
   126
    if (isRumbleInserted()) {
dalton@2749
   127
        /* official rumble pak is present. */
dalton@2749
   128
        ret = 1;
dalton@2749
   129
        printf("debug: haptic present: nintendo\n");
dalton@2750
   130
    } else if (NDS_EZF_IsPresent()) {
dalton@2749
   131
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   132
        ret = 1;
dalton@2749
   133
        printf("debug: haptic present: ezf3in1\n");
dalton@2749
   134
        NDS_EZF_ChipReset();
dalton@2749
   135
    } else {
dalton@2749
   136
        printf("debug: no haptic found\n");
dalton@2749
   137
    }
dalton@2749
   138
dalton@2749
   139
    return ret;
dalton@2749
   140
}
dalton@2749
   141
dalton@2749
   142
dalton@2749
   143
const char *
dalton@2749
   144
SDL_SYS_HapticName(int index)
dalton@2749
   145
{
dalton@2750
   146
    if (nds_haptic) {
dalton@2750
   147
        switch (nds_haptic->hwdata->type) {
dalton@2750
   148
        case OFFICIAL:
dalton@2750
   149
            return "Nintendo DS Rumble Pak";
dalton@2750
   150
        case EZF3IN1:
dalton@2750
   151
            return "EZFlash 3-in-1 Rumble";
dalton@2750
   152
        default:
dalton@2750
   153
            return NULL;
dalton@2749
   154
        }
dalton@2749
   155
    }
dalton@2749
   156
    return NULL;
dalton@2749
   157
}
dalton@2749
   158
dalton@2749
   159
dalton@2749
   160
int
dalton@2749
   161
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
dalton@2749
   162
{
dalton@2750
   163
    if (!haptic) {
dalton@2749
   164
        return -1;
dalton@2749
   165
    }
dalton@2749
   166
slouken@5270
   167
    haptic->hwdata = SDL_malloc(sizeof(struct haptic_hwdata));
dalton@2750
   168
    if (!haptic->hwdata) {
dalton@2749
   169
        SDL_OutOfMemory();
dalton@2749
   170
        return -1;
dalton@2749
   171
    }
dalton@2749
   172
    nds_haptic = haptic;
dalton@2749
   173
dalton@2749
   174
    haptic->supported = SDL_HAPTIC_CONSTANT;
dalton@2749
   175
dalton@2749
   176
    /* determine what is here, if anything */
dalton@2749
   177
    haptic->hwdata->type = NONE;
dalton@2750
   178
    if (isRumbleInserted()) {
dalton@2749
   179
        /* official rumble pak is present. */
dalton@2749
   180
        haptic->hwdata->type = OFFICIAL;
dalton@2750
   181
    } else if (NDS_EZF_IsPresent()) {
dalton@2749
   182
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   183
        haptic->hwdata->type = EZF3IN1;
dalton@2749
   184
        NDS_EZF_ChipReset();
dalton@2749
   185
    } else {
dalton@2749
   186
        /* no haptic present */
dalton@2749
   187
        SDL_SYS_LogicError();
dalton@2749
   188
        return -1;
dalton@2749
   189
    }
dalton@2749
   190
dalton@2749
   191
    return 0;
dalton@2749
   192
}
dalton@2749
   193
dalton@2749
   194
dalton@2749
   195
int
dalton@2749
   196
SDL_SYS_HapticMouse(void)
dalton@2749
   197
{
dalton@2749
   198
    return -1;
dalton@2749
   199
}
dalton@2749
   200
dalton@2749
   201
dalton@2749
   202
int
dalton@2749
   203
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
dalton@2749
   204
{
dalton@2749
   205
    return 0;
dalton@2749
   206
}
dalton@2749
   207
dalton@2749
   208
dalton@2749
   209
int
dalton@2749
   210
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   211
{
dalton@2750
   212
    /*SDL_SYS_LogicError(); */
dalton@2749
   213
    return -1;
dalton@2749
   214
}
dalton@2749
   215
dalton@2749
   216
dalton@2749
   217
int
dalton@2749
   218
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   219
{
dalton@2749
   220
    return 0;
dalton@2749
   221
}
dalton@2749
   222
dalton@2749
   223
dalton@2749
   224
void
dalton@2749
   225
SDL_SYS_HapticClose(SDL_Haptic * haptic)
dalton@2749
   226
{
dalton@2749
   227
    return;
dalton@2749
   228
}
dalton@2749
   229
dalton@2749
   230
dalton@2749
   231
void
dalton@2749
   232
SDL_SYS_HapticQuit(void)
dalton@2749
   233
{
dalton@2749
   234
    return;
dalton@2749
   235
}
dalton@2749
   236
dalton@2749
   237
dalton@2749
   238
int
dalton@2749
   239
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
dalton@2749
   240
                        struct haptic_effect *effect, SDL_HapticEffect * base)
dalton@2749
   241
{
dalton@2749
   242
    SDL_SYS_LogicError();
dalton@2749
   243
    return -1;
dalton@2749
   244
}
dalton@2749
   245
dalton@2749
   246
dalton@2749
   247
int
dalton@2749
   248
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
dalton@2749
   249
                           struct haptic_effect *effect,
dalton@2749
   250
                           SDL_HapticEffect * data)
dalton@2749
   251
{
dalton@2749
   252
    SDL_SYS_LogicError();
dalton@2749
   253
    return -1;
dalton@2749
   254
}
dalton@2749
   255
dalton@2749
   256
dalton@2749
   257
int
dalton@2749
   258
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
dalton@2749
   259
                        Uint32 iterations)
dalton@2749
   260
{
dalton@2749
   261
    SDL_SYS_LogicError();
dalton@2749
   262
    return -1;
dalton@2749
   263
}
dalton@2749
   264
dalton@2749
   265
dalton@2749
   266
int
dalton@2749
   267
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   268
{
dalton@2749
   269
    SDL_SYS_LogicError();
dalton@2749
   270
    return -1;
dalton@2749
   271
}
dalton@2749
   272
dalton@2749
   273
dalton@2749
   274
void
dalton@2749
   275
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   276
{
dalton@2749
   277
    SDL_SYS_LogicError();
dalton@2749
   278
    return;
dalton@2749
   279
}
dalton@2749
   280
dalton@2749
   281
dalton@2749
   282
int
dalton@2749
   283
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
dalton@2749
   284
                              struct haptic_effect *effect)
dalton@2749
   285
{
dalton@2749
   286
    SDL_SYS_LogicError();
dalton@2749
   287
    return -1;
dalton@2749
   288
}
dalton@2749
   289
dalton@2749
   290
dalton@2749
   291
int
dalton@2749
   292
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
dalton@2749
   293
{
dalton@2749
   294
    SDL_SYS_LogicError();
dalton@2749
   295
    return -1;
dalton@2749
   296
}
dalton@2749
   297
dalton@2749
   298
dalton@2749
   299
int
dalton@2749
   300
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
dalton@2749
   301
{
dalton@2749
   302
    SDL_SYS_LogicError();
dalton@2749
   303
    return -1;
dalton@2749
   304
}
dalton@2749
   305
dalton@2749
   306
int
dalton@2749
   307
SDL_SYS_HapticPause(SDL_Haptic * haptic)
dalton@2749
   308
{
dalton@2749
   309
    SDL_SYS_LogicError();
dalton@2749
   310
    return -1;
dalton@2749
   311
}
dalton@2749
   312
dalton@2749
   313
int
dalton@2749
   314
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
dalton@2749
   315
{
dalton@2749
   316
    SDL_SYS_LogicError();
dalton@2749
   317
    return -1;
dalton@2749
   318
}
dalton@2749
   319
dalton@2749
   320
int
dalton@2749
   321
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
dalton@2749
   322
{
dalton@2749
   323
    SDL_SYS_LogicError();
dalton@2749
   324
    return -1;
dalton@2749
   325
}
dalton@2749
   326
dalton@2749
   327
dalton@2749
   328
dalton@2749
   329
#endif /* SDL_HAPTIC_NDS */
dalton@2749
   330
/* vi: set ts=4 sw=4 expandtab: */