USBJoystick 0.12

USB Joystick driver for RISC OS 5
Post Reply
JonAbbott
Posts: 2965
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex
Contact:

Re: USB Joystick driver

Post by JonAbbott »

Vanfanel wrote: Sun Apr 01, 2018 11:49 pm -Spheres of Chaos(ADFFS): crashes to a black screen *only when the USBJoystick module is loaded*
The crash is being cause by an unimplemented Serial Port SWI. It's calling VTJoystick_Define (&81541), which isn't currently implemented in USBJoystick:

Code: Select all

ADR R0, &B9D0
MOV R1, #8
SWI &81541
The 8 bytes @ B9D0 are:

Code: Select all

01 C1 00 00 10 00 10 00
It also calls an unimplemented Acorn Joystick SWI, Joystick_CalibrateTopRight (&43F41), so the Acorn Joystick support doesn't work either. I suspect this is a bug in the code, it should probably be calling Joystick_Read 0 (&43F40)

You can temporarily fix it by patching out the Serial Port/Acorn support and loading the RTFM support Module. Add the following to the top of !Run:

Code: Select all

JITMEMORYA B958 7A000016 E1A00000
RMLoad adffs:extras.JoyMod
The code checks for Joystick support in the following order, but obviously fails on the first two due to the unimplemented SWI's:
  1. Magnetic Image
  2. Serial Port
  3. RTFM
  4. Acorn
EDIT: Joystick_CalibrateTopRight (&43F41) was introduced with RISCOS 3.50, but should only be called to calibrate the Joystick in conjunction with Joystick_CalibrateBottomLeft (&43F42). The code in Sphere's of Chaos is therefore incorrect and should be calling Joystick_Read. I'll work on a fix for the boot script to fix Acorn Joystick support.

For Serial Port support, VTJoystick_Define (&81541) will need implementing in USBJoystick.

EDIT2: Reading the Help file, it supports the Magnetic Image Joystick interface, which hangs of the Acorn SWI block and is what the call to SWI &43F41 is doing. It's expecting a pointer to a table of at least four words to be returned in R0. As &43F41 is a valid SWI on RISC OS 3.50+, I suspect it will instantly crash if an Acorn Joystick is available on RISC OS 3.5 or newer.

To fix the crash correctly it needs VTJoystick_Define (&81541) implementing in USBJoystick. Without it, the Joystick controls are mapped incorrectly.
JonAbbott
Posts: 2965
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex
Contact:

Re: USB Joystick driver

Post by JonAbbott »

Vanfanel wrote: Sun Apr 01, 2018 11:49 pm -Magic Pockets (ADFFS): crashes *only when the USBJoystick module is loaded*.
During loading Magic Pockets attempts to add support for RTFM via Joystick->Keymapping by issuing *keytrap arcsocr, internally it only supports the Acorn Joystick interface.

The crash isn't occuring within Magic Pockets, its occurring in USBJoystick within the sequence of FPA instructions below (which is the x/y calculation in swi_joystick_read):

Code: Select all

000033C4 E3A03028 : MOV     R3,#&28
000033C8 E59205F8 : LDR     R0,[R2,#&5F8]
000033CC E0202093 : MLA     R0,R3,R0,R2
000033D0 ED90313D : LDFS    F3,[R0,#&0F4]
000033D4 E59010F8 : LDR     R1,[R0,#&0F8]
000033D8 E590C0DC : LDR     R12,[R0,#&0DC]
000033DC E08CC001 : ADD     R12,R12,R1
000033E0 EE00C110 : FLTS    F0,R12
000033E4 EE900103 : FMLS    F0,F0,F3             <- Generates a Floating Point Exception
000033E8 EE100170 : FIXSZ   R0,F0
000033EC E592C5FC : LDR     R12,[R2,#&5FC]
000033F0 E023239C : MLA     R3,R12,R3,R2
000033F4 ED93013D : LDFS    F0,[R3,#&0F4]
000033F8 E593C0DC : LDR     R12,[R3,#&0DC]
000033FC E5923600 : LDR     R3,[R2,#&600]
00003400 E08C1001 : ADD     R1,R12,R1
00003404 E0823103 : ADD     R3,R2,R3,LSL #2
00003408 E5B3235C : LDR     R2,[R3,#&35C]!
0000340C E200C0FF : AND     R12,R0,#&FF
00003410 EE011110 : FLTS    F1,R1
00003414 EE911100 : FMLS    F1,F1,F0
00003418 EE10A101 : MNFS    F2,F1
0000341C EE101172 : FIXSZ   R1,F2
The FPA registers at the time of the crash are:

Code: Select all

     S Exp  J Fraction                       S Exp  J Fraction
F0 = 0 3FFC 1 7E00000000000000          F1 = 1 3FFC 1 7E00000000000000
F2 = 0 3FFC 1 7E00000000000000          F3 = 0 3FFC 1 7E00000000000000
F4 = 0 0000 0 0000000000000000          F5 = 0 0000 0 0000000000000000
F6 = 0 0000 0 0000000000000000          F7 = 0 0000 0 0000000000000000

FPSR = 01070010

F0 = 2.480468750000000000E-0001         F1 = -2.480468750000000000E-0001
F2 = 2.480468750000000000E-0001         F3 = 2.480468750000000000E-0001
F4 = 0.000000000000000000               F5 = 0.000000000000000000
F6 = 0.000000000000000000               F7 = 0.000000000000000000

System:  FPE 400                        Enabled exceptions:    ix uf OF DZ IO
Control: ac ep so ne nd                 Cumulative exceptions: IX uf of dz io
Why the crash only occurs with Magic Pockets, I'm not sure. The game doesn't use the FPA and there's no IRQ handler, Joystick_Read is called from USER so I'm at a loss to explain it.
Vanfanel
Posts: 576
Joined: Mon Sep 16, 2013 12:01 am

Re: USB Joystick driver

Post by Vanfanel »

@Jon: Do you know if the joystick is supposed to work with Manic Miner nativer 32bit version?
https://www.riscosopen.org/forum/forums/1/topics/10127

I am reaaaaally in love with that game and this incredible Risc OS version!
JonAbbott
Posts: 2965
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex
Contact:

Re: USB Joystick driver

Post by JonAbbott »

Vanfanel wrote: Wed Apr 04, 2018 2:04 pm @Jon: Do you know if the joystick is supposed to work with Manic Miner native 32bit version?
It mentions in the !Help that it supports the Acorn Joystick interface, but searching the code I can't see any calls to Joystick_Read - so no, it doesn't support Joystick.
richw
Posts: 159
Joined: Sat Sep 14, 2013 9:05 pm

Re: USB Joystick driver

Post by richw »

I’ve had a little time, so updated the first post to version 0.06 which implements the Joy SWIs previously discussed. Chuck Rock is working for me. I’m currently using the last public release of ADFFS - only issue was that on quitting Chuck Rock, I get ‘Module JoyStick not found’, which I think is coming from the RMKill in the boot script. Might need the boot script patching, unless you’ve done that in the later pre-release (I have not tried that yet).

I have not yet implemented the 16-bit Acorn Joystick SWIs (which Spheres of Chaos looks to be half-using). That is on my to-do list, though. Not many games use it, since so few people had joysticks and RISC OS 3.5+ and a game made after 1994!

My next task will be to get SWI VTJoystick_Define (&81541) implemented - I think it’s documented somewhere in this thread, so I’ll have a read.
Vanfanel
Posts: 576
Joined: Mon Sep 16, 2013 12:01 am

Re: USB Joystick driver

Post by Vanfanel »

@richw: Any new games that should be working now? Seems to have the same compatibility as 0.05, ie Xenon2 still ignores directions for example.
Should Fire & Ice be working somehow?
richw
Posts: 159
Joined: Sat Sep 14, 2013 9:05 pm

Re: USB Joystick driver

Post by richw »

To be honest, the little time I’ve had has been spent on the driver coding (not so much the games!). I have still only tried those I mentioned in my first post, plus Chuck Rock (for the new Joy_ SWIs). Am I right that these are still issues:

- Xenon 2 (ADFFS): only fire button works, no directions
- Magic Pokets(ADFFS): crashes *only when the USBJoystick module is loaded*.
- Spheres of Chaos(ADFFS): crashes to a black screen *only when the USBJoystick module is loaded*.

? If so, I shall try and look at them. I think they are all on the public download area.

Oh, and to implement VTJoystick_Define (&81541). Not sure what it needs to do, mind! I wonder if it is a keyboard/mouse mapper?

Just in case you chaps didn’t realise, the individual APIs can be toggled on and off with *commands: USBJoystick_EmulateAcorn for example.
JonAbbott
Posts: 2965
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex
Contact:

Re: USB Joystick driver

Post by JonAbbott »

richw wrote: Wed Apr 04, 2018 11:01 pm - Xenon 2 (ADFFS): only fire button works, no directions
I'll test this later, but I suspect the Acorn interface isn't returning the correct values for Vanfanel. Wolfenstein 3D also doesn't work for him, but does for me. One posibility for the discrepancy is the values returned for Analogue vs Digital Joysticks, I'll check the code for both games and see if I can figure out the min values they require to register movement.
richw wrote: Wed Apr 04, 2018 11:01 pm - Magic Pokets(ADFFS): crashes *only when the USBJoystick module is loaded*.
- Spheres of Chaos(ADFFS): crashes to a black screen *only when the USBJoystick module is loaded*.

? If so, I shall try and look at them. I think they are all on the public download area.

Oh, and to implement VTJoystick_Define (&81541). Not sure what it needs to do, mind! I wonder if it is a keyboard/mouse mapper?
I don't think I've released Spheres of Chaos yet, but Magic Pockets is available. The simplest solution for Spheres of Chaos is for me to disable the Magnetic Image and Serial Port support in the boot script if USBJoystick is present. We currently have no detailed documentation on either interface so can't implement them fully, in particular VTJoystick_Define, which is causing the Serial Port support to fail.

We do need to figure out why Magic Pockets causes swi_joystick_read to generate a Floating Point Exception. I spent hours looking for a root cause within Magic Pockets yesterday, but couldn't find one but in hindsight I forgot to check what it's passing in R0 to Joystick_Read. The exception occurs early on within the Intro code, if it's passing R0 with "16bit" set, or even a rogue value, it's possible it's causing an out of bounds array access in USBJoystick.

EDIT: Magic Pockets is calling Joystick_Read with R0=0 so we can rule out bounding issues.
richw
Posts: 159
Joined: Sat Sep 14, 2013 9:05 pm

Re: USB Joystick driver

Post by richw »

Just a thought on boot scripts... might it be an idea to abstract any changes you make regarding joysticks? i.e. don't bake in anything specifically for USBJoystick, but use some *command aliases or whatever so that ADFFS or USBJoystick can pick them up. You never know, other solutions could exist (apart from USBJoystick).
JonAbbott
Posts: 2965
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex
Contact:

Re: USB Joystick driver

Post by JonAbbott »

richw wrote: Thu Apr 05, 2018 9:55 am Just a thought on boot scripts... might it be an idea to abstract any changes you make regarding joysticks? i.e. don't bake in anything specifically for USBJoystick, but use some *command aliases or whatever so that ADFFS or USBJoystick can pick them up. You never know, other solutions could exist (apart from USBJoystick).
The boot scripts can be easily modified by anyone, should they want to change the behaviour. If I add a * command to ADFFS then it would be baked in, as it needs to check for the RTFM SWI and load JoyMod if it's not available and the machine supports Econet.

Once we've worked out which games support Joystick(s) and which interfaces they support, then it might be worth adding a * command to ADFFS to automatically load the correct Joystick Modules and bundle them with ADFFS. RTFM SWI is covered by JoyMod, Serial Port/Vertical Twist is a different matter as some use the Acorn SWI's and some use &81540, so there's a conflict if the Acorn Joystick Module is running, requiring manual intervention to load the Serial Port Module only if a Serial Port Joystick is attached. In other words, ADFFS needs to figure out which Joystick(s) are attached and load the relevant Module(s).

I've tested 0.06, which works perfectly. No more overruns on X/Y axis and its returning the correct X/Y values.

Spheres of Chaos - It's Joystick code is a mess. Firstly it's X/Y checks are inverted, this doesn't show up on the Acorn interface as it builds up an RTFM style R0 and then jumps to the RTFM code, which is also inverted...so a double negative. I suspect the Serial Port and Magnetic Image support is also working via a double negative but can't test. I've now fixed this issue for Acorn/RTFM in the boot script, but there's a further issue in that Thrust doesn't work - I'm still looking through the code to figure out why, but suspect it's down to the code that checks for rapid up/down/up movement that triggers Hyperspace.

RTFM direct support - I've noticed the check on the Econet port doesn't always succeed, modified Modules attached which resolve this (see later post).

Xenon 2 - it doesn't work due to its strict adherence to PRM4-218's definition of the values returned for switched Joysticks. It's expecting 64 for Right/Up and 192 for Down/Left, I need to modify the code in the boot script, so it checks for <-32 / >32. This issue will probably affect some other games.
Post Reply