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