Previous 15994 Revisions Next

r1300 Monday 19th November, 2007 at 17:56:51 UTC by R. Belmont
[SDLMESS] Updates for 0.121, including workaround for validity crash/hang.
[src/emu]validity.c
[src/osd/sdl]input.c osdsdl.h osinline.h sdlmain.c sdlwork.c

src/osd/sdl/input.c
r1299r1300
77//
88//  SDLMAME by Olivier Galibert and R. Belmont
99//
10//  SixAxis info: left analog is axes 0 & 1, right analog is axes 2 & 3,
11//                analog L2 is axis 12 and analog L3 is axis 13
12//
1013//============================================================
1114
1215// standard sdl header
r1299r1300
149152   char      *name;
150153} joy_map[MAX_JOYMAP];
151154
155static int sixaxis_mode;
156
152157static int joy_logical[MAX_JOYSTICKS];
153158
154159//============================================================
r1299r1300
618623      // loop over all buttons
619624      for (button = 0; button < SDL_JoystickNumButtons(joy); button++)
620625      {
626         devinfo->joystick.buttons[button] = 0;
627
621628         if (button < 16)
622629         {
623630            sprintf(tempname, "J%d button %d", stick + 1, button);
r1299r1300
714721   mouse_list = NULL;
715722   lightgun_list = NULL;
716723
724   // get Sixaxis special mode info
725   sixaxis_mode = options_get_bool(mame_options(), SDLOPTION_SIXAXIS);
726
717727   // we need pause and exit callbacks
718728   add_pause_callback(machine, sdlinput_pause);
719729   add_exit_callback(machine, sdlinput_exit);
r1299r1300
957967         devinfo = generic_device_find_index(joystick_list, event.jaxis.which);
958968         if (devinfo)
959969         {
960            devinfo->joystick.axes[event.jaxis.axis] = (event.jaxis.value * 2);
970            if (sixaxis_mode)
971            {
972               int axis = event.jaxis.axis;
973
974               if (axis <= 3)
975               {
976                  devinfo->joystick.axes[event.jaxis.axis] = (event.jaxis.value * 2);
977               }
978               if ((axis == 12) || (axis == 13))
979               {
980                  devinfo->joystick.axes[event.jaxis.axis] = (event.jaxis.value / 2) + 16383;
981               }
982            }
983            else
984            {
985               devinfo->joystick.axes[event.jaxis.axis] = (event.jaxis.value * 2);
986            }
961987         }
962988         break;
963989      case SDL_JOYHATMOTION:
src/osd/sdl/osinline.h
r1299r1300
2323}
2424
2525
26//============================================================
27//  osd_interlocked_increment
28//============================================================
29
30ATTR_UNUSED
31INLINE INT32 _osd_interlocked_increment(INT32 volatile *ptr)
32{
33   register INT32 ret;
34   __asm__ __volatile__(
35      " mov           $1,%[ret]     ;"
36      " lock ; xadd   %[ret],%[ptr] ;"
37      " inc           %[ret]        ;"
38      : [ptr] "+m"  (*ptr)
39      , [ret] "=&r" (ret)
40      :
41      : "%cc"
42   );
43   return ret;
44}
45#define osd_interlocked_increment _osd_interlocked_increment
46
47
48//============================================================
49//  osd_interlocked_decrement
50//============================================================
51
52ATTR_UNUSED
53INLINE INT32 _osd_interlocked_decrement(INT32 volatile *ptr)
54{
55   register INT32 ret;
56   __asm__ __volatile__(
57      " mov           $-1,%[ret]    ;"
58      " lock ; xadd   %[ret],%[ptr] ;"
59      " dec           %[ret]        ;"
60      : [ptr] "+m"  (*ptr)
61      , [ret] "=&r" (ret)
62      :
63      : "%cc"
64   );
65   return ret;
66}
67#define osd_interlocked_decrement _osd_interlocked_decrement
68
69
7026#if defined(__x86_64__)
7127
7228//============================================================
r1299r1300
9955}
10056
10157
102//============================================================
103//  osd_interlocked_increment
104//============================================================
10558
106ATTR_UNUSED
107INLINE INT32 _osd_interlocked_increment(INT32 volatile *ptr)
108{
109   register INT32 ret;
110   __asm__ __volatile__(
111      "1: lwarx  %[ret], 0, %[ptr] \n"
112      "   addi   %[ret], %[ret], 1 \n"
113      "   sync                     \n"
114      "   stwcx. %[ret], 0, %[ptr] \n"
115      "   bne-   1b                \n"
116      : [ret] "=&b" (ret)
117      : [ptr] "r"   (ptr)
118      : "cr0"
119   );
120   return ret;
121}
122#define osd_interlocked_increment _osd_interlocked_increment
123
124
125//============================================================
126//  osd_interlocked_decrement
127//============================================================
128
129ATTR_UNUSED
130INLINE INT32 _osd_interlocked_decrement(INT32 volatile *ptr)
131{
132   register INT32 ret;
133   __asm__ __volatile__(
134      "1: lwarx  %[ret], 0, %[ptr]  \n"
135      "   addi   %[ret], %[ret], -1 \n"
136      "   sync                      \n"
137      "   stwcx. %[ret], 0, %[ptr]  \n"
138      "   bne-   1b                 \n"
139      : [ret] "=&b" (ret)
140      : [ptr] "r"   (ptr)
141      : "cr0"
142   );
143   return ret;
144}
145#define osd_interlocked_decrement _osd_interlocked_decrement
146
147
14859#if defined(__ppc64__) || defined(__PPC64__)
14960
15061//============================================================
src/osd/sdl/sdlwork.c
r1299r1300
1616#include "os2work.c"
1717#else
1818
19// standard headers
20#include <time.h>
21#if defined(SDLMAME_UNIX) && !defined(SDLMAME_DARWIN)
22#include <sys/time.h>
23#endif
24#include <pthread.h>
25#include <unistd.h>
2619#include "osdcore.h"
2720#include "osinline.h"
2821
r1299r1300
4841//============================================================
4942
5043#define INFINITE            (osd_ticks_per_second()*10000)
51#define MAX_THREADS            (16)
5244#define SPIN_LOOP_TIME         (osd_ticks_per_second() / 1000)
5345
5446
r1299r1300
5850//============================================================
5951
6052#if KEEP_STATISTICS
61#define add_to_stat(v,x)      do { atomic_add32((v), (x)); } while (0)
53#define add_to_stat(v,x)      do { interlocked_add((v), (x)); } while (0)
6254#define begin_timing(v)         do { (v) -= osd_profiling_ticks(); } while (0)
6355#define end_timing(v)         do { (v) += osd_profiling_ticks(); } while (0)
6456#else
r1299r1300
7971   struct
8072   {
8173      volatile INT32    haslock;      // do we have the lock?
82      UINT8          filler[60];      // assumes a 64-byte cache line
83   } slot[MAX_THREADS];               // one slot per thread
74      INT32          filler[64/4-1];   // assumes a 64-byte cache line
75   } slot[WORK_MAX_THREADS];         // one slot per thread
8476   volatile INT32       nextindex;      // index of next slot to use
8577};
8678
8779
88typedef struct _mame_thread_info mame_thread_info;
89struct _mame_thread_info
80typedef struct _work_thread_info work_thread_info;
81struct _work_thread_info
9082{
9183   osd_work_queue *   queue;         // pointer back to the queue
9284   osd_thread *      handle;         // handle to the thread
r1299r1300
115107   volatile UINT8      exiting;      // should the threads exit on their next opportunity?
116108   UINT32            threads;      // number of threads in this queue
117109   UINT32            flags;         // creation flags
118   mame_thread_info *   thread;         // array of thread information
110   work_thread_info *   thread;         // array of thread information
119111   osd_event   *      doneevent;      // event signalled when work is complete
120112
121113#if KEEP_STATISTICS
r1299r1300
139131   volatile INT32      done;         // is the item done?
140132};
141133
134typedef void *PVOID;
142135
143
144136//============================================================
145137//  FUNCTION PROTOTYPES
146138//============================================================
147139
148140static int effective_num_processors(void);
149141static void * worker_thread_entry(void *param);
150static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread);
142static void worker_thread_process(osd_work_queue *queue, work_thread_info *thread);
151143
152144//============================================================
153145//  INLINE FUNCTIONS
154146//============================================================
155147
156#ifndef osd_interlocked_increment
157INLINE INT32 osd_interlocked_increment(INT32 volatile *ptr)
148INLINE INT32 interlocked_exchange32(INT32 volatile *ptr, INT32 value)
158149{
159   return atomic_add32(ptr, 1);
150   return atomic_exchange32(ptr, value);
160151}
161#endif
162152
163#ifndef osd_interlocked_decrement
164INLINE INT32 osd_interlocked_decrement(INT32 volatile *ptr)
153INLINE INT32 interlocked_increment(INT32 volatile *ptr)
165154{
166   return atomic_add32(ptr, -1);
155   return atomic_increment32(ptr);
167156}
168#endif
169157
158
159INLINE INT32 interlocked_decrement(INT32 volatile *ptr)
160{
161   return atomic_decrement32(ptr);
162}
163
164
165INLINE INT32 interlocked_add(INT32 volatile *ptr, INT32 add)
166{
167   return atomic_add32(ptr, add);
168}
169
170
171
172//============================================================
173//  Scalable Locks
174//============================================================
175
170176INLINE void scalable_lock_init(scalable_lock *lock)
171177{
172178   memset(lock, 0, sizeof(*lock));
r1299r1300
176182
177183INLINE INT32 scalable_lock_acquire(scalable_lock *lock)
178184{
179   INT32 myslot = (osd_interlocked_increment(&lock->nextindex) - 1) & (MAX_THREADS - 1);
185   INT32 myslot = (atomic_increment32(&lock->nextindex) - 1) & (WORK_MAX_THREADS - 1);
180186
181187#if defined(__i386__) || defined(__x86_64__)
182188   register INT32 tmp;
r1299r1300
230236   return myslot;
231237}
232238
239
233240INLINE void scalable_lock_release(scalable_lock *lock, INT32 myslot)
234241{
235242#if defined(__i386__) || defined(__x86_64__)
236243   register INT32 tmp = TRUE;
237244   __asm__ __volatile__ (
238245      " xchg   %[haslock], %[tmp] ;"
239      : [haslock] "+m" (lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock)
246      : [haslock] "+m" (lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock)
240247      , [tmp]     "+r" (tmp)
241248      :
242249   );
243250#elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__)
244   lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock = TRUE;
251   lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock = TRUE;
245252   __asm__ __volatile__ ( " eieio " : : );
246253#else
247   osd_exchange32(&lock->slot[(myslot + 1) & (MAX_THREADS - 1)].haslock, TRUE);
254   osd_exchange32(&lock->slot[(myslot + 1) & (WORK_MAX_THREADS - 1)].haslock, TRUE);
248255#endif
249256}
250257
r1299r1300
287294      queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1;
288295
289296   // clamp to the maximum
290   queue->threads = MIN(queue->threads, MAX_THREADS);
297   queue->threads = MIN(queue->threads, WORK_MAX_THREADS);
291298
292299   // allocate memory for thread array (+1 to count the calling thread)
293300   queue->thread = malloc((queue->threads + 1) * sizeof(queue->thread[0]));
r1299r1300
298305   // iterate over threads
299306   for (threadnum = 0; threadnum < queue->threads; threadnum++)
300307   {
301      mame_thread_info *thread = &queue->thread[threadnum];
308      work_thread_info *thread = &queue->thread[threadnum];
302309
303310      // set a pointer back to the queue
304311      thread->queue = queue;
r1299r1300
359366   // if this is a multi queue, help out rather than doing nothing
360367   if (queue->flags & WORK_QUEUE_FLAG_MULTI)
361368   {
362      mame_thread_info *thread = &queue->thread[queue->threads];
369      work_thread_info *thread = &queue->thread[queue->threads];
363370      osd_ticks_t stopspin = osd_ticks() + timeout;
364371
365372      end_timing(thread->waittime);
r1299r1300
367374      // process what we can as a worker thread
368375      worker_thread_process(queue, thread);
369376
370      // spin until we're done
371      begin_timing(thread->spintime);
372      while (queue->items != 0 && osd_ticks() < stopspin)
373         osd_yield_processor();
374      end_timing(thread->spintime);
377      // if we're a high frequency queue, spin until done
378      if (queue->flags & WORK_QUEUE_FLAG_HIGH_FREQ)
379      {
380         // spin until we're done
381         begin_timing(thread->spintime);
382         while (queue->items != 0 && osd_ticks() < stopspin)
383            osd_yield_processor();
384         end_timing(thread->spintime);
375385
386         begin_timing(thread->waittime);
387         return (queue->items == 0);
388      }
376389      begin_timing(thread->waittime);
377      return (queue->items == 0);
378390   }
379391
380392   // reset our done event and double-check the items before waiting
381393   osd_event_reset(queue->doneevent);
382   atomic_exchange32(&queue->waiting, TRUE);
394   interlocked_exchange32(&queue->waiting, TRUE);
383395   if (queue->items != 0)
384396      osd_event_wait(queue->doneevent, timeout);
385   atomic_exchange32(&queue->waiting, FALSE);
397   interlocked_exchange32(&queue->waiting, FALSE);
386398
387399   // return TRUE if we actually hit 0
388400   return (queue->items == 0);
r1299r1300
407419      queue->exiting = TRUE;
408420      for (threadnum = 0; threadnum < queue->threads; threadnum++)
409421      {
410         mame_thread_info *thread = &queue->thread[threadnum];
422         work_thread_info *thread = &queue->thread[threadnum];
411423         if (thread->wakeevent != NULL)
412424            osd_event_set(thread->wakeevent);
413425      }
r1299r1300
415427      // wait for all the threads to go away
416428      for (threadnum = 0; threadnum < queue->threads; threadnum++)
417429      {
418         mame_thread_info *thread = &queue->thread[threadnum];
430         work_thread_info *thread = &queue->thread[threadnum];
419431
420432         // block on the thread going away, then close the handle
421433         if (thread->handle != NULL)
r1299r1300
432444      // output per-thread statistics
433445      for (threadnum = 0; threadnum <= queue->threads; threadnum++)
434446      {
435         mame_thread_info *thread = &queue->thread[threadnum];
447         work_thread_info *thread = &queue->thread[threadnum];
436448         osd_ticks_t total = thread->runtime + thread->waittime + thread->spintime;
437449         printf("Thread %d:  items=%9d run=%5.2f%% (%5.2f%%)  spin=%5.2f%%  wait/other=%5.2f%% total=%9d\n",
438450               threadnum, thread->itemsdone,
r1299r1300
504516      do
505517      {
506518         item = (osd_work_item *)queue->free;
507      } while (item != NULL && compare_exchange_ptr((void * volatile *)&queue->free, item, item->next) != item);
519      } while (item != NULL && compare_exchange_ptr((PVOID volatile *)&queue->free, item, item->next) != item);
508520
509521      // if nothing, allocate something new
510522      if (item == NULL)
r1299r1300
537549   scalable_lock_release(&queue->lock, lockslot);
538550
539551   // increment the number of items in the queue
540   atomic_add32(&queue->items, numitems);
552   interlocked_add(&queue->items, numitems);
541553   add_to_stat(&queue->itemsqueued, numitems);
542554
543555   // look for free threads to do the work
r1299r1300
548560      // iterate over all the threads
549561      for (threadnum = 0; threadnum < queue->threads; threadnum++)
550562      {
551         mame_thread_info *thread = &queue->thread[threadnum];
563         work_thread_info *thread = &queue->thread[threadnum];
552564
553565         // if this thread is not active, wake him up
554566         if (!thread->active)
r1299r1300
634646   {
635647      next = (osd_work_item *)item->queue->free;
636648      item->next = next;
637   } while (compare_exchange_ptr((void * volatile *)&item->queue->free, next, item) != next);
649   } while (compare_exchange_ptr((PVOID volatile *)&item->queue->free, next, item) != next);
638650}
639651
640652
r1299r1300
663675
664676static void *worker_thread_entry(void *param)
665677{
666   mame_thread_info *thread = param;
678   work_thread_info *thread = param;
667679   osd_work_queue *queue = thread->queue;
668680
669681   // loop until we exit
r1299r1300
681693         break;
682694
683695      // indicate that we are live
684      atomic_exchange32(&thread->active, TRUE);
685      osd_interlocked_increment(&queue->livethreads);
696      interlocked_exchange32(&thread->active, TRUE);
697      interlocked_increment(&queue->livethreads);
686698
687699      // process work items
688700      for ( ;; )
r1299r1300
692704         // process as much as we can
693705         worker_thread_process(queue, thread);
694706
695         // spin for a while looking for more work
707         // if we're a high frequency queue, spin for a while before giving up
696708         if (queue->flags & WORK_QUEUE_FLAG_HIGH_FREQ)
697709         {
710            // spin for a while looking for more work
698711            begin_timing(thread->spintime);
699712            stopspin = osd_ticks() + SPIN_LOOP_TIME;
700713            while (queue->list == NULL && osd_ticks() < stopspin)
701714               osd_yield_processor();
702715            end_timing(thread->spintime);
703716         }
717
704718         // if nothing more, release the processor
705719         if (queue->list == NULL)
706720            break;
r1299r1300
708722      }
709723
710724      // decrement the live thread count
711      atomic_exchange32(&thread->active, FALSE);
712      osd_interlocked_decrement(&queue->livethreads);
725      interlocked_exchange32(&thread->active, FALSE);
726      interlocked_decrement(&queue->livethreads);
713727   }
714728   return NULL;
715729}
r1299r1300
719733//  worker_thread_process
720734//============================================================
721735
722static void worker_thread_process(osd_work_queue *queue, mame_thread_info *thread)
736static void worker_thread_process(osd_work_queue *queue, work_thread_info *thread)
723737{
724738   int threadid = thread - queue->thread;
725739
r1299r1300
754768         end_timing(thread->actruntime);
755769
756770         // decrement the item count after we are done
757         osd_interlocked_decrement(&queue->items);
758         item->done = TRUE;
759
771         interlocked_decrement(&queue->items);
772         interlocked_exchange32(&item->done, TRUE);
760773         add_to_stat(&thread->itemsdone, 1);
761774
762775         // if it's an auto-release item, release it
src/osd/sdl/sdlmain.c
r1299r1300
195195   { NULL,                                NULL,        OPTION_HEADER,     "SDL JOYSTICK MAPPING" },
196196   { SDLOPTION_JOYMAP,                      "0",         OPTION_BOOLEAN,    "enable physical to logical joystick mapping" },
197197   { SDLOPTION_JOYMAP_FILE,                "joymap.dat", 0,               "joymap filename" },
198   { SDLOPTION_SIXAXIS,         "0",   OPTION_BOOLEAN, "Use special handling for PS3 Sixaxis controllers" },
198199   { NULL }
199200};
200201
src/osd/sdl/osdsdl.h
r1299r1300
7777#define SDLOPTION_JOYMAP            "remapjoys"
7878#define SDLOPTION_JOYMAP_FILE         "remapjoyfile"
7979#define SDLOPTION_UIMODEKEY            "uimodekey"
80#define SDLOPTION_SIXAXIS         "sixaxis"
8081
8182#define SDLOPTION_SHADER_MAME(x)      "glsl_shader_mame" x
8283#define SDLOPTION_SHADER_SCREEN(x)      "glsl_shader_screen" x
src/emu/validity.c
r1299r1300
300300   if (resultu32 != expectedu32)
301301      { mame_printf_error("Error testing modu_64x32 (%08X%08X / %08X) = %08X (expected %08X)\n", (UINT32)(testu64a >> 32), (UINT32)testu64a, testu32a, resultu32, expectedu32); error = TRUE; }
302302
303#if 0
303304   while ((INT64)testi32a * (INT64)0x7fffffff < ((INT32)testi64a << 3))
304305      testi64a /= 2;
305306   while ((UINT64)testu32a * (UINT64)0xffffffff < ((UINT32)testu64a << 3))
306307      testu64a /= 2;
307
308#endif
308309   resulti32 = div_32x32_shift((INT32)testi64a, testi32a, 3);
309310   expectedi32 = ((INT64)(INT32)testi64a << 3) / (INT64)testi32a;
310311   if (resulti32 != expectedi32)

Previous 15994 Revisions Next


© 1998-2018 The MESS Team