Index: firmware/5200/mainmenu.c
===================================================================
--- firmware/5200/mainmenu.c	(revision 424)
+++ firmware/5200/mainmenu.c	(revision 425)
@@ -258,10 +258,24 @@
 		debug_adjust = row==3 ? 128 : 0;
 		printf("Cart:%s", file_name(files[4]) ? file_name(files[4]) : "NONE");
 
+#ifdef USBSETTINGS
 		debug_pos = 240;
 		debug_adjust = row==4 ? 128 : 0;
+		printf("Rotate USB joysticks");
+
+		debug_pos = 320;
+		debug_adjust = row==5 ? 128 : 0;
 		printf("Exit");
 
+		debug_adjust = 0;
+
+		usb_devices(400);
+#else
+		debug_pos = 240;
+		debug_adjust = row==4 ? 128 : 0;
+		printf("Exit");
+#endif
+
 /*
 while (1)
 {
@@ -288,7 +302,11 @@
 
 		row+=joy.y_;
 		if (row<0) row = 0;
+#ifdef USBSETTINGS
+		if (row>5) row = 5;
+#else
 		if (row>4) row = 4;
+#endif
 		switch (row)
 		{
 		case 0:
@@ -330,12 +348,27 @@
 				}
 			}
 			break;
+#ifdef USBSETTINGS
 		case 4:
 			if (joy.fire_)
 			{
+				rotate_usb_sticks();
+			}
+			break;
+		case 5:
+			if (joy.fire_)
+			{
 				done = 1;
 			}
 			break;
+#else
+		case 4:
+			if (joy.fire_)
+			{
+				done = 1;
+			}
+			break;
+#endif
 		}
 	}
 
Index: firmware/a800/mainmenu.c
===================================================================
--- firmware/a800/mainmenu.c	(revision 424)
+++ firmware/a800/mainmenu.c	(revision 425)
@@ -6,13 +6,6 @@
 
 unsigned char freezer_rom_present;
 
-#ifdef USB
-#include "usb.h"
-#include "usb/debug.h"
-#endif
-
-#undef USBSETTINGS
-
 void loadosrom()
 {
 	if (file_size(files[5]) == 0x4000)
@@ -189,60 +182,7 @@
 	}*/
 }
 
-#ifdef USBSETTINGS
-void usb_settings()
-{
-	struct joystick_status joy;
-	joy.x_ = joy.y_ = joy.fire_ = joy.escape_ = 0;
 
-	int row = 0;
-
-	int done = 0;
-	for (;!done;)
-	{
-		// Render
-		clearscreen();
-		debug_pos = 0;
-		debug_adjust = 0;
-		printf("USB Se");
-		debug_adjust = 128;
-		printf("ttings");
-		debug_pos = 80;
-		debug_adjust = row==0 ? 128 : 0;
-		printf("Hello world");
-
-		debug_pos = 200;
-		debug_adjust = row==1 ? 128 : 0;
-		printf("Exit");
-
-		// Slow it down a bit
-		wait_us(100000);
-
-		// move
-		joystick_wait(&joy,WAIT_QUIET);
-		joystick_wait(&joy,WAIT_EITHER);
-		if (joy.escape_) break;
-
-		row+=joy.y_;
-		if (row<0) row = 0;
-		if (row>1) row = 1;
-		switch (row)
-		{
-		case 0:
-			{
-			}
-			break;
-		case 1:
-			if (joy.fire_)
-			{
-				done = 1;
-			}
-			break;
-		}
-	}
-}
-#endif
-
 int settings()
 {
 	struct joystick_status joy;
@@ -290,26 +230,30 @@
 		debug_adjust = row==8 ? 128 : 0;
 		printf("Cart: %s", get_cart_select() ? file_name(files[4]) : "NONE");
 
-		debug_pos = 520;
+/*		debug_pos = 520;
 		debug_adjust = row==9 ? 128 : 0;
 		printf("Load memory");
 
 		debug_pos = 560;
 		debug_adjust = row==10 ? 128 : 0;
-		printf("Save memory (for debugging)");
+		printf("Save memory (for debugging)");*/
 
 
 #ifdef USBSETTINGS
-		debug_pos = 640;
-		debug_adjust = row==11 ? 128 : 0;
-		printf("USB");
+		debug_pos = 480;
+		debug_adjust = row==9 ? 128 : 0;
+		printf("Rotate USB joysticks");
 
-		debug_pos = 720;
-		debug_adjust = row==12 ? 128 : 0;
+		debug_pos = 560;
+		debug_adjust = row==10 ? 128 : 0;
 		printf("Exit");
+
+		debug_adjust = 0;
+
+		usb_devices(640);
 #else
-		debug_pos = 640;
-		debug_adjust = row==11 ? 128 : 0;
+		debug_pos = 520;
+		debug_adjust = row==9 ? 128 : 0;
 		printf("Exit");
 #endif
 
@@ -323,10 +267,10 @@
 
 		row+=joy.y_;
 		if (row<0) row = 0;
-#ifdef USB
-		if (row>12) row = 12;
+#ifdef USBSETTINGS
+		if (row>10) row = 10;
 #else
-		if (row>11) row = 11;
+		if (row>9) row = 9;
 #endif
 		switch (row)
 		{
@@ -423,7 +367,7 @@
 				}
 			}
 			break;
-		case 9:
+/*		case 9:
 		case 10:
 			{
 				if (joy.fire_)
@@ -441,15 +385,15 @@
 					}
 				}
 			}
-			break;
+			break;*/
 #ifdef USBSETTINGS
-		case 11:
+		case 9:
 			if (joy.fire_)
 			{
-				usb_settings();
+				rotate_usb_sticks();
 			}
 			break;
-		case 12:
+		case 10:
 			if (joy.fire_)
 			{
 				done = 1;
@@ -456,7 +400,7 @@
 			}
 			break;
 #else
-		case 11:
+		case 9:
 			if (joy.fire_)
 			{
 				done = 1;
@@ -547,3 +491,5 @@
 }
 
 
+
+
Index: firmware/main.h
===================================================================
--- firmware/main.h	(revision 424)
+++ firmware/main.h	(revision 425)
@@ -19,6 +19,12 @@
 #define after_set_reg_hook() do { } while(0)
 #endif
 
+#ifdef USB
+#include "usb.h"
+#include "usb/debug.h"
+#define USBSETTINGS
+#endif
+
 #include "memory.h"
 
 extern char ROM_DIR[];
@@ -274,3 +280,99 @@
 	return 0;
 }
 
+#ifdef USBSETTINGS
+void rotate_usb_sticks()
+{
+	int max_jindex = hid_get_joysticks()-1;
+	if (max_jindex == 0) // If only one stick connected allow it to be 0 or 1
+	{
+		max_jindex = 1;
+	}
+
+	int i;
+
+	usb_device_t * devices = usb_get_devices();
+	for (i=0;i!=USB_NUMDEVICES;++i)	
+	{
+		usb_device_t *dev  = devices + i;
+		if (dev->bAddress)
+		{
+			if (dev->class == &usb_hid_class)
+			{
+				int j=0;
+				for (j=0;j!=dev->hid_info.bNumIfaces;++j)
+				{
+					int type = dev->hid_info.iface[j].device_type;
+					if (type == HID_DEVICE_JOYSTICK)
+					{
+						int jindex = dev->hid_info.iface[j].jindex;
+						event_digital_joystick(jindex, 0);
+						event_analog_joystick(jindex, 0,0);
+
+						jindex++;
+						if (jindex > max_jindex)
+							jindex = 0;
+						
+						dev->hid_info.iface[j].jindex = jindex;
+					}
+				}
+			}
+		}
+	}
+}
+
+void usb_devices(int debugPos)
+{
+	usb_device_t *devices = usb_get_devices();
+	int nextDebugPos = debugPos;
+	int i=0;
+	int j=0;
+	for (i=0;i!=USB_NUMDEVICES;++i)	
+	{
+		debug_pos = nextDebugPos;
+		usb_device_t *dev  = devices + i;
+		if (dev->bAddress)
+		{
+			if (dev->class == &usb_hub_class)
+			{
+				//printf("%x.Hub. %d ports. poll=%d",dev->bAddress,dev->hub_info.bNbrPorts, dev->hub_info.bPollEnable);
+				printf("%x.Hub. %d ports",dev->bAddress,dev->hub_info.bNbrPorts);
+			}
+			else if (dev->class == &usb_hid_class)
+			{
+				//printf("%x.HID. %d ifaces. poll=%d",dev->bAddress,dev->hid_info.bNumIfaces,dev->hid_info.bPollEnable);
+				printf("%x.HID",dev->bAddress);
+				int adjPos = 0;
+				for (j=0;j!=dev->hid_info.bNumIfaces;++j)
+				{
+					//usb_device_descriptor_t desc;
+					int type = dev->hid_info.iface[j].device_type;
+					if (adjPos && (type == HID_DEVICE_MOUSE || type == HID_DEVICE_KEYBOARD || type == HID_DEVICE_JOYSTICK))
+					{
+						nextDebugPos = nextDebugPos+40;
+						debug_pos = nextDebugPos;
+					}
+					if (type == HID_DEVICE_MOUSE)
+						printf(" Mouse");
+					else if (type == HID_DEVICE_KEYBOARD)
+						printf(" Keyboard");
+					else if (type == HID_DEVICE_JOYSTICK)
+					{
+						printf(" Joystick:%d",dev->hid_info.iface[j].jindex+1);
+					}
+					else
+						continue;
+
+					adjPos = 1;
+
+					/*int rcode = usb_get_dev_descr( dev, 12, &desc );
+					if( !rcode ) {
+						printf(" V:%02x%02x P:%02x%02x C:%02x",desc.idVendorH,desc.idVendorL,desc.idProductH,desc.idProductL,desc.bDeviceClass);
+					}*/
+				}
+			}
+		}
+		nextDebugPos = nextDebugPos+40;
+	}
+}
+#endif
Index: firmware/mif_file_stuff/rom_prologue.vhd
===================================================================
--- firmware/mif_file_stuff/rom_prologue.vhd	(revision 424)
+++ firmware/mif_file_stuff/rom_prologue.vhd	(revision 425)
@@ -1,4 +1,4 @@
-DEPTH = 4096; % Memory depth and width are required % 
+DEPTH = 8192; % Memory depth and width are required % 
  % DEPTH is the number of addresses % 
 WIDTH = 32; % WIDTH is the number of bits of data per word % 
 % DEPTH and WIDTH should be entered as decimal numbers % 
Index: firmware/usb/events.h
===================================================================
--- firmware/usb/events.h	(revision 424)
+++ firmware/usb/events.h	(revision 425)
@@ -116,6 +116,7 @@
 		debug_pos = 200 + idx*40;
 		printf("JOY:%d:%08x ",idx,jmap);
 	}*/
+	idx = idx&1;
 	if (idx == 0)
 	{
 		*zpu_out2 = jmap;
@@ -128,6 +129,7 @@
 void event_analog_joystick(uint8_t idx, int8_t x, int8_t y)
 {
 	//printf("Event analog joystick:%d %d %d\n", idx,x,y);
+	idx = idx&1;
 	if (analogx[idx]!=x || analogy[idx]!=y)
 	{
 		analogx[idx] = x;
Index: firmware/usb/hid.c
===================================================================
--- firmware/usb/hid.c	(revision 424)
+++ firmware/usb/hid.c	(revision 425)
@@ -588,6 +588,7 @@
     { 7, 7, 0x41, 0 }, /* 4 -> f8 */
     { 10, 11, 0x43, 0 }, /* l2&r2 -> f10 */
     { 8, 11, 0x42, 0 }, /* l1&r2 -> f9 */
+    { 6, 6, 0x00, 4 }, /* 3 -> fire */
 #endif
 #ifdef FIRMWARE_5200
     { 13, 13, 0x3a, 0 }, /* start -> f1 (start) */
@@ -594,12 +595,12 @@
     { 12, 12, 0x3b, 0 }, /* select -> f2 (pause) */
     { 10, 10, 0x00, 5 }, /* l2 -> fire2 */
     { 11, 11, 0x00, 5 }, /* r2 -> fire2 */
-    { 7, 7, 0x00, 5 }, /* 4 -> fire2 */
+    { 6, 6, 0x00, 5 }, /* 3 -> fire2 */
+    { 7, 7, 0x00, 4 }, /* 4 -> fire */
 #endif
     { 4, 4, 0x45, 0 }, /* 1 -> f12 */
     { 5, 5, 0x44, 0 }, /* 2 -> f11 */
     { 14, 15, 0x29, 0 }, /* both sticks  -> esc */
-    { 6, 6, 0x00, 4 }, /* 3 -> fire */
     { 8, 8, 0x00, 4 }, /* l1 -> fire */
     { 9, 9, 0x00, 4 }, /* r1 -> fire */
     { 10, 15, 0x43, 0 }, /* l2& rstick click -> f10 */
@@ -735,8 +736,11 @@
 			if (buf[2]&0x40) jmap |= 1<<14;  //lstick click
 			if (buf[2]&0x80) jmap |= 1<<15;  //rstick click
 
-			a[0] = 128+(int)buf[7];
-			a[1] = 128+(int)buf[9];
+			int8_t x = (char)buf[7];
+			int8_t y = (char)buf[9];
+
+			a[0] = 128+x;
+			a[1] = 127-y;
 		}
 		else
 		{
@@ -766,11 +770,6 @@
 	               conf->joystick.axis[i].logical.min);
 	          }
 	        }
-
-  	        if(a[0] <  64) jmap |= JOY_LEFT;
-  	        if(a[0] > 192) jmap |= JOY_RIGHT;
-  	        if(a[1] <  64) jmap |= JOY_UP;
-  	        if(a[1] > 192) jmap |= JOY_DOWN;
   	      
   	        //	      iprintf("JOY X:%d Y:%d\n", a[0], a[1]);
   	      
@@ -779,6 +778,11 @@
   	 	  if(buf[conf->joystick.button[i].byte_offset] & 
   	  	     conf->joystick.button[i].bitmask) jmap |= (JOY_BTN1<<i);
               }
+
+  	      if(a[0] <  64) jmap |= JOY_LEFT;
+  	      if(a[0] > 192) jmap |= JOY_RIGHT;
+  	      if(a[1] <  64) jmap |= JOY_UP;
+  	      if(a[1] > 192) jmap |= JOY_DOWN;
 	      
 	      //	      iprintf("JOY D:%d\n", jmap);
 
