Skip to content

Commit bd5c0b4

Browse files
committed
joystick support
1 parent 58e1af4 commit bd5c0b4

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

index.html

+11-10
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,7 @@
452452
let body;
453453
let canvas;
454454
let canvas_input;
455-
let gamepad;
456-
let gamepad_ico;
455+
let gamepad_elem;
457456
let run_stop_btn;
458457
let interval;
459458
let last_ms;
@@ -519,13 +518,15 @@
519518
localStorage.setItem("settings.vscan", e.target.checked);
520519
}
521520

522-
function gamepad_connect(e) {
521+
function gamepad_connection(e) {
523522
if(e.type === "gamepaddisconnected") {
524-
gamepad = undefined;
525-
gamepad_ico.className= "gamepad off";
523+
if(motherboard.joystick.gamepad == e.gamepad) {
524+
motherboard.joystick.gamepad = undefined;
525+
gamepad_elem.className= "gamepad off";
526+
}
526527
} else {
527-
gamepad = e.gamepad;
528-
gamepad_ico.className= "gamepad";
528+
motherboard.joystick.gamepad = e.gamepad;
529+
gamepad_elem.className= "gamepad";
529530
}
530531
}
531532

@@ -589,9 +590,9 @@
589590
document.addEventListener('keydown', ff?key_down:key_down_trans);
590591
document.addEventListener('keyup', key_up);
591592

592-
gamepad_ico = document.querySelector("div.gamepad");
593-
window.addEventListener("gamepadconnected", gamepad_connect);
594-
window.addEventListener("gamepaddisconnected", gamepad_connect);
593+
gamepad_elem = document.querySelector("div.gamepad");
594+
window.addEventListener("gamepadconnected", gamepad_connection);
595+
window.addEventListener("gamepaddisconnected", gamepad_connection);
595596

596597
document.getElementById("fullscreen").addEventListener("click", display_fullscreen);
597598
const tcolor = document.getElementById("tcolor");

io_manager.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@
6363

6464
export class IOManager
6565
{
66-
constructor(memory, keyboard, display_text, display_hires, display_double_hires, audio_cb) {
66+
constructor(memory, keyboard, display_text, display_hires, display_double_hires, audio_cb, joystick) {
6767
this._mem = memory;
6868
this._kbd = keyboard;
6969
this._display_text = display_text;
7070
this._display_hires = display_hires;
7171
this._display_double_hires = display_double_hires;
7272
this._audio_cb = audio_cb;
73+
this._joystick = joystick;
7374

7475
this._c3_rom = false;
7576
this._c8_rom = false;
@@ -138,6 +139,22 @@ export class IOManager
138139
return this._altchar_mode ? 0x80 : 0;
139140
case 0xc01f: // 80 col mode (0: 40 cols, 0x80: 80 cols)
140141
return this._80col_mode ? 0x80 : 0;
142+
case 0xc061: // js pb0
143+
return this._joystick.button0 ? 0x80 : 0;
144+
case 0xc062: // js pb1
145+
return this._joystick.button1 ? 0x80 : 0;
146+
case 0xc063: // js pb2
147+
return this._joystick.button2 ? 0x80 : 0;
148+
case 0xc064: // js pdl-0
149+
return this._joystick.axis0;
150+
case 0xc065: // js pdl-1
151+
return this._joystick.axis1;
152+
case 0xc066: // js pdl-2
153+
return this._joystick.axis2;
154+
case 0xc067: // js pdl-3
155+
return this._joystick.axis3;
156+
case 0xc070: // trigger paddle read
157+
return 0;
141158
case 0xc07e: // iou disable (0: iou is enabled, 0x80: iou is disabled)
142159
//console.log("iou disable: " + this._iou_disable);
143160
return this._iou_disable ? 0x80 : 0;

joystick.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// joystick wrapper for browser gamepad
3+
//
4+
// Copyright 2018, John Clark
5+
//
6+
// Released under the GNU General Public License
7+
// https://www.gnu.org/licenses/gpl.html
8+
//
9+
10+
11+
const ps4 = {
12+
button0: 2,
13+
button1: 0,
14+
button2: 1,
15+
axis0: 0,
16+
axis1: 1,
17+
axis2: 2,
18+
axis3: 3
19+
};
20+
21+
22+
export class Joystick
23+
{
24+
constructor() {
25+
this.gamepad = undefined;
26+
}
27+
28+
get button0() {
29+
return this.get_button_pressed("button0");
30+
};
31+
32+
get button1() {
33+
return this.get_button_pressed("button1");
34+
};
35+
36+
get button2() {
37+
return this.get_button_pressed("button2");
38+
};
39+
40+
get axis0() {
41+
return this.get_axis_value("axis0");
42+
};
43+
44+
get axis1() {
45+
return this.get_axis_value("axis1");
46+
};
47+
48+
get axis2() {
49+
return this.get_axis_value("axis2");
50+
};
51+
52+
get axis3() {
53+
return this.get_axis_value("axis3");
54+
};
55+
56+
get_button_pressed(btn) {
57+
if(!this.gamepad) return false;
58+
return this.gamepad.buttons[ps4[btn]].pressed;
59+
}
60+
61+
get_axis_value(axis) {
62+
if(!this.gamepad) return 128;
63+
let val = Math.floor((this.gamepad.axes[ps4[axis]] * 128) + 128);
64+
if(val < 0) val = 0;
65+
if(val > 255) val = 255;
66+
if((val > 122) && (val < 133)) val = 128;
67+
return val;
68+
}
69+
}
70+

motherboard.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {DoubleHiresDisplay} from "./display_double_hires.js";
1818
import {Keyboard} from "./keyboard.js";
1919
import {Floppy525} from "./floppy525.js";
2020
import {AppleAudio} from "./apple_audio.js";
21+
import {Joystick} from "./joystick.js";
2122
import {rom_342_0304_cd} from "./rom/342-0304-cd.js";
2223
import {rom_342_0303_ef} from "./rom/342-0303-ef.js";
2324

@@ -33,7 +34,10 @@ export class Motherboard
3334
this.display_double_hires = new DoubleHiresDisplay(this.memory, canvas);
3435
this.floppy525 = new Floppy525(6, this.memory, floppy_led_cb);
3536
this.audio = new AppleAudio(khz);
36-
this.io_manager = new IOManager(this.memory, this.keyboard, this.display_text, this.display_hires, this.display_double_hires, this.audio_click.bind(this));
37+
this.joystick = new Joystick();
38+
this.io_manager = new IOManager(this.memory, this.keyboard,
39+
this.display_text, this.display_hires, this.display_double_hires,
40+
this.audio_click.bind(this), this.joystick);
3741

3842
this.cycles = 0;
3943
}

0 commit comments

Comments
 (0)