Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if (NOT EMSCRIPTEN)
src/options.c
src/emulator.c
src/host.c
src/host-input.c
src/host-gl.c
src/host-ui-simple.c
src/joypad.c
Expand All @@ -71,6 +72,7 @@ if (NOT EMSCRIPTEN)
src/options.c
src/emulator-debug.c
src/host.c
src/host-input.c
src/host-gl.c
src/host-ui-imgui.cc
src/joypad.c
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A simple GB/GBC emulator.
* **Rewind** and seek to specific cycle
* Disable/enable each audio channel
* Disable/enable BG, Window and Sprite layers
* **PS/2 keyboard** over link port emulation
* Convenient Python test harness using hashes to validate
* (WIP) **Debugger** with various visualizations (see below)

Expand Down Expand Up @@ -62,6 +63,8 @@ A simple GB/GBC emulator.
Copy the folowing files to your webserver:
* `docs/binjgb.js`
* `docs/binjgb.wasm`
* `docs/keyboard.js`
* `docs/binjgb.ini`
* `docs/simple.html`
* `docs/simple.js`
* `docs/simple.css`
Expand Down Expand Up @@ -199,6 +202,12 @@ Keys:
| Pause | <kbd>Space</kbd> |
| Step one frame | <kbd>N</kbd> |

Keys not reserved for the joypad or the shortcuts above are sent to an
emulated PS/2 keyboard attached to the link port. Each scan code is sent
as two externally-clocked serial bytes holding the 11-bit PS/2 frame.

Use `--no-ps2-keyboard` to disconnect the keyboard.

## INI file

Binjgb tries to read from `binjgb.ini` on startup for configuration. The
Expand Down Expand Up @@ -252,11 +261,54 @@ random-seed=0
# 0=Don't display SGB border
# 1=Display SGB border, even if it doesn't exist.
sgb-border=0

# Connect an emulated PS/2 keyboard to the serial port.
# 0=Disable
# 1=Enable
ps2-keyboard=1

# Which unreserved keys may be sent to PS/2: all, none, or a comma-separated
# list of physical key names (e.g. A,B,C,SPACE,RETURN).
ps2-keys=all
```

The INI file is loaded before parsing the command line flags, so you can use
the command line to override the values in the INI file.

### Keyboard routing

Keyboard input can only go to one destination. Emulator shortcut are mapped first,
followed by keyboard joypad binding, then PS/2 (if connected and allowed by `ps2-keys`).
Each binding accepts `none`, or a comma-separated list of key names.
Names are case-insensitive and use SDL scancode tokens without the `SDL_SCANCODE_` prefix:

`A` through `Z`, `0` through `9`, `KP_0` through `KP_9`, `F1` through `F12`,
`UP`, `DOWN`, `LEFT`, `RIGHT`, `RETURN`, `ESCAPE`,
`BACKSPACE`, `TAB`, `SPACE`, `LSHIFT`, `RSHIFT`, `LCTRL`, `RCTRL`, `LALT`,
`RALT`, `LGUI`, `RGUI`, `APPLICATION`, `CAPSLOCK`, `NUMLOCKCLEAR`, `SCROLLLOCK`,
`INSERT`, `DELETE`, `HOME`, `END`, `PAGEUP`, `PAGEDOWN`, `MINUS`, `EQUALS`,
`LEFTBRACKET`, `RIGHTBRACKET`, `BACKSLASH`, `SEMICOLON`, `APOSTROPHE`, `GRAVE`,
`COMMA`, `PERIOD`, `SLASH`, `KP_PERIOD`, `KP_MULTIPLY`,
`KP_MINUS`, `KP_PLUS`, `KP_DIVIDE`, and `KP_ENTER`.

| Setting | Default |
| --- | --- |
| `joypad-up`, `joypad-down`, `joypad-left`, `joypad-right` | `UP`, `DOWN`, `LEFT`, `RIGHT` |
| `joypad-b`, `joypad-a`, `joypad-start`, `joypad-select` | `Z`, `X`, `RETURN`, `TAB` |
| `shortcut-quit` | `ESCAPE` |
| `shortcut-save-state`, `shortcut-load-state` | `F6`, `F9` |
| `shortcut-fullscreen` | `F11` |
| `shortcut-audio-1` through `shortcut-audio-4` | `1` through `4` |
| `shortcut-bg`, `shortcut-window`, `shortcut-obj` | `B`, `W`, `O` |
| `shortcut-fast-forward`, `shortcut-rewind` | `LSHIFT`, `BACKSPACE` |
| `shortcut-pause`, `shortcut-step` | `SPACE`, `N` |
| `shortcut-volume-down`, `shortcut-volume-up` | `MINUS`, `EQUALS` |
| `shortcut-palette-prev`, `shortcut-palette-next` | `LEFTBRACKET`, `RIGHTBRACKET` |

Use `joypad=none` or `shortcuts=none` to clear a whole group before assigning
individual bindings. `joypad=default` and `shortcuts=default` restore the
respective defaults.

## Running tests

Run `scripts/build_tests.py` to download and build the necessary testsuites.
Expand Down
19 changes: 19 additions & 0 deletions docs/binjgb.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Keyboard configuration shared by index.html and simple.html.
# Reload the page after editing. Physical key names match the native INI.
ps2-keyboard=1
ps2-keys=all

# Defaults preserve each page's existing joypad and shortcut bindings.
# For a typing profile, uncomment the following block. Physical controllers
# and the simple page's touch controls remain available.
# joypad=none
# shortcuts=none
# shortcut-pause=F5
# shortcut-save-state=F6
# shortcut-rewind=F8
# shortcut-load-state=F9
# shortcut-fullscreen=F11

# Leave Ctrl/Alt/Meta combinations to the browser by default. Set to 1 to
# forward supported combinations to the emulator instead.
capture-browser-shortcuts=0
23 changes: 21 additions & 2 deletions docs/binjgb.js

Large diffs are not rendered by default.

Binary file modified docs/binjgb.wasm
Binary file not shown.
115 changes: 53 additions & 62 deletions docs/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const $ = document.querySelector.bind(document);
let emulator = null;

const binjgbPromise = Binjgb();
const keyboardConfigPromise = BinjgbKeyboard.loadConfig('binjgb.ini');

const dbPromise = idb.open('db', 1, upgradeDb => {
const objectStore = upgradeDb.createObjectStore('games', {keyPath : 'sha1'});
Expand Down Expand Up @@ -201,7 +202,7 @@ let vm = new Vue({
this.files.show = false;
this.loadedFile = file;
this.needsReload = false;
Emulator.start(await binjgbPromise, romBuffer, extRamBuffer);
Emulator.start(await binjgbPromise, romBuffer, extRamBuffer, await keyboardConfigPromise);
emulator.setBuiltinPalette(this.pal);
},
deleteFile: async function(file) {
Expand Down Expand Up @@ -310,6 +311,7 @@ let vm = new Vue({
toggleOpenDialog: function() {
this.files.show = !this.files.show;
if (this.files.show) {
if (emulator) emulator.keyboard.releaseAll();
this.paused = true;
}
},
Expand Down Expand Up @@ -368,9 +370,9 @@ function makeWasmBuffer(module, ptr, size) {
}

class Emulator {
static start(module, romBuffer, extRamBuffer) {
static start(module, romBuffer, extRamBuffer, keyboardConfig) {
Emulator.stop();
emulator = new Emulator(module, romBuffer, extRamBuffer);
emulator = new Emulator(module, romBuffer, extRamBuffer, keyboardConfig);
emulator.run();
}

Expand All @@ -381,7 +383,7 @@ class Emulator {
}
}

constructor(module, romBuffer, extRamBuffer) {
constructor(module, romBuffer, extRamBuffer, keyboardConfig) {
this.module = module;
// Align size up to 32k.
const size = (romBuffer.byteLength + 0x7fff) & ~0x7fff;
Expand All @@ -396,7 +398,8 @@ class Emulator {
throw new Error('Invalid ROM.');
}

this.gamepad = new Gamepad(module, this.e);
this.joypad = new BinjgbJoypad(module, this.e);
this.gamepad = new Gamepad(this.joypad);
this.audio = new Audio(module, this.e);
this.video = new Video(module, this.e, $('canvas'));
this.rewind = new Rewind(module, this.e);
Expand All @@ -411,7 +414,7 @@ class Emulator {
this.loadExtRam(extRamBuffer);
}

this.bindKeys();
this.bindKeys(keyboardConfig);
this.gamepad.init();
}

Expand Down Expand Up @@ -568,10 +571,12 @@ class Emulator {
}

runUntil(untilTicks) {
this.keyboard.flush();
let next60hzTicks = this.getNext60HzTicks(this.ticks);
while (true) {
const event = this.module._emulator_run_until_f64(
this.e, Math.min(untilTicks, next60hzTicks));
this.keyboard.flush();
if (event & EVENT_NEW_FRAME) {
this.rewind.pushBuffer();
this.video.uploadTexture();
Expand Down Expand Up @@ -613,45 +618,36 @@ class Emulator {
this.video.renderTexture();
}

bindKeys() {
this.keyFuncs = {
'ArrowDown': this.module._set_joyp_down.bind(null, this.e),
'ArrowLeft': this.module._set_joyp_left.bind(null, this.e),
'ArrowRight': this.module._set_joyp_right.bind(null, this.e),
'ArrowUp': this.module._set_joyp_up.bind(null, this.e),
'KeyZ': this.module._set_joyp_B.bind(null, this.e),
'KeyX': this.module._set_joyp_A.bind(null, this.e),
'Enter': this.module._set_joyp_start.bind(null, this.e),
'Tab': this.module._set_joyp_select.bind(null, this.e),
'Backspace': this.keyRewind.bind(this),
'Space': this.keyPause.bind(this),
'F6': vm.saveState.bind(vm),
'F9': vm.loadState.bind(vm),
};
this.boundKeyDown = this.keyDown.bind(this);
this.boundKeyUp = this.keyUp.bind(this);

window.addEventListener('keydown', this.boundKeyDown);
window.addEventListener('keyup', this.boundKeyUp);
}

unbindKeys() {
window.removeEventListener('keydown', this.boundKeyDown);
window.removeEventListener('keyup', this.boundKeyUp);
}

keyDown(event) {
if (event.code in this.keyFuncs) {
this.keyFuncs[event.code](true);
event.preventDefault();
bindKeys(config) {
const handlers = {};
for (const [name, button, key] of [
['up', 'up', 'UP'], ['down', 'down', 'DOWN'],
['left', 'left', 'LEFT'], ['right', 'right', 'RIGHT'],
['a', 'A', 'X'], ['b', 'B', 'Z'],
['start', 'start', 'RETURN'], ['select', 'select', 'TAB'],
]) {
handlers[`joypad-${name}`] = {
keys: key, held: true,
callback: down => this.joypad.set(button, 'keyboard', down),
};
}
Object.assign(handlers, {
'shortcut-rewind': {keys: 'BACKSPACE', held: true, callback: this.keyRewind.bind(this)},
'shortcut-pause': {keys: 'SPACE', callback: this.keyPause.bind(this)},
'shortcut-save-state': {keys: 'F6', callback: vm.saveState.bind(vm)},
'shortcut-load-state': {keys: 'F9', callback: vm.loadState.bind(vm)},
'shortcut-fullscreen': {keys: '', callback: () => {
if (document.fullscreenElement) document.exitFullscreen();
else vm.toggleFullscreen();
}},
});
this.keyboard = new BinjgbKeyboard(this.module, this.e, config, handlers, {
isCaptured: () => vm.files.show,
});
}

keyUp(event) {
if (event.code in this.keyFuncs) {
this.keyFuncs[event.code](false);
event.preventDefault();
}
unbindKeys() {
this.keyboard.destroy();
}

keyRewind(isKeyDown) {
Expand All @@ -672,33 +668,27 @@ class Emulator {
}

class Gamepad {
constructor(module, e) {
this.module = module;
this.e = e;
constructor(joypad) {
this.joypad = joypad;
}

// Load a key map for gamepad-to-gameboy buttons
bindKeys(strMapping) {
const bind = (gb_key, gp_button, type = 'button') => {
const button = gb_key === 'a' || gb_key === 'b' ? gb_key.toUpperCase() : gb_key;
return {
gb_key, gp_button, type,
gp_bind: down => this.joypad.set(button, `gamepad:${type}:${gp_button}`, down),
};
};
this.GAMEPAD_KEYMAP_STANDARD = [
{gb_key: "b", gp_button: 0, type: "button", gp_bind:this.module._set_joyp_B.bind(null, this.e) },
{gb_key: "a", gp_button: 1, type: "button", gp_bind:this.module._set_joyp_A.bind(null, this.e) },
{gb_key: "select", gp_button: 8, type: "button", gp_bind:this.module._set_joyp_select.bind(null, this.e) },
{gb_key: "start", gp_button: 9, type: "button", gp_bind:this.module._set_joyp_start.bind(null, this.e) },
{gb_key: "up", gp_button: 12, type: "button", gp_bind:this.module._set_joyp_up.bind(null, this.e) },
{gb_key: "down", gp_button: 13, type: "button", gp_bind:this.module._set_joyp_down.bind(null, this.e) },
{gb_key: "left", gp_button: 14, type: "button", gp_bind:this.module._set_joyp_left.bind(null, this.e) },
{gb_key: "right", gp_button: 15, type: "button", gp_bind:this.module._set_joyp_right.bind(null, this.e) }
bind('b', 0), bind('a', 1), bind('select', 8), bind('start', 9),
bind('up', 12), bind('down', 13), bind('left', 14), bind('right', 15),
];

this.GAMEPAD_KEYMAP_DEFAULT = [
{gb_key: "a", gp_button: 0, type: "button", gp_bind:this.module._set_joyp_A.bind(null, this.e) },
{gb_key: "b", gp_button: 1, type: "button", gp_bind:this.module._set_joyp_B.bind(null, this.e) },
{gb_key: "select", gp_button: 2, type: "button", gp_bind:this.module._set_joyp_select.bind(null, this.e) },
{gb_key: "start", gp_button: 3, type: "button", gp_bind:this.module._set_joyp_start.bind(null, this.e) },
{gb_key: "up", gp_button: 2, type: "axis", gp_bind:this.module._set_joyp_up.bind(null, this.e) },
{gb_key: "down", gp_button: 3, type: "axis", gp_bind:this.module._set_joyp_down.bind(null, this.e) },
{gb_key: "left", gp_button: 0, type: "axis", gp_bind:this.module._set_joyp_left.bind(null, this.e) },
{gb_key: "right", gp_button: 1, type: "axis", gp_bind:this.module._set_joyp_right.bind(null, this.e) }
bind('a', 0), bind('b', 1), bind('select', 2), bind('start', 3),
bind('up', 2, 'axis'), bind('down', 3, 'axis'),
bind('left', 0, 'axis'), bind('right', 1, 'axis'),
];

// Try to use the w3c "standard" gamepad mapping if available
Expand Down Expand Up @@ -821,6 +811,7 @@ class Gamepad {
}

releaseGamepad() {
this.joypad.clearSource('gamepad:');
// Stop polling the gamepad for input
if (this.gp.timerID !== undefined) {
clearInterval(this.gp.timerID);
Expand Down
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,6 @@
</div>
<script src="sha1.js"></script>
<script src="binjgb.js"></script>
<script src="keyboard.js"></script>
<script src="demo.js"></script>
</body>
Loading