In this third episode I’ll create a joystick routine that reads out the joystick / gamepad directions and the fire button.
You’ll need a recent version of java grinder as the initial source base for the Amiga didn’t support reading out memory locations. I contacted the author of grinder and the next day these functions were implemented by Michael Kohn into the memory class, thanks Michael.
I took me a while to figure out how the Amiga reads the joy ports, a bit of trail an error I came to this AmigaJoystick class:
import net.mikekohn.java_grinder.Memory;
public class AmigaJoystick {
static public final int JOY0 = 0;
static public final int JOY1 = 1;
static final int JOY0DAT = 0xDFF00A;
static final int JOY1DAT = 0xDFF00C;
static final int CIAAPRA = 0xBFE001; // 8bit
static public final int FIRE0 = 0x40;
static public final int FIRE1 = 0x80;
static public final int JOYLEFT = 0x0200;
static public final int JOYRIGHT = 0x0002;
static public final int JOYUP = 0x0100;
static public final int JOYDOWN = 0x0001;
public static int getDir(int joy) {
int dir = 0;
if (joy == 0) {
dir = Memory.read16(JOY0DAT);
} else {
dir = Memory.read16(JOY1DAT);
}
if (((dir >> 1 ^ dir) & JOYDOWN) == JOYDOWN) {
return JOYDOWN;
} else if (((dir >> 1 ^ dir) & JOYUP) == JOYUP) {
return JOYUP;
} else if ((dir & JOYLEFT) == JOYLEFT) {
return JOYLEFT;
} else if ((dir & JOYRIGHT) == JOYRIGHT) {
return JOYRIGHT;
} else {
return 0;
}
}
public static boolean getFire(int joy) {
byte fire = 0;
fire = Memory.read8(CIAAPRA);
if (joy == 0) {
if ((fire & FIRE0) == 0x00) {
return true;
}
} else {
if ((fire & FIRE1) == 0x00) {
return true;
}
}
return false;
}
}
Here’s a little example how to use it
int direction = AmigaJoystick.getDir(AmigaJoystick.JOY1);
if(direction == JOYDOWN) {
...;
} else if(direction == JOYUP) {
...;
} else if(direction == JOYLEFT) {
...;
} else if(direction == JOYRIGHT) {
...;
}
if(AmigaJoystick.getFire(AmigaJoystick.JOY1) == true) {
...;
}
Please notice that in the emulator the joystick is usually bound to JOY1 port and mouse to JOY0.
In the next episode I’ll tackle the use of text in Amiga grinder, which is quite difficult as no String class is implemented, so watch this space end enjoy coding.