You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
Ben Firshman 15924fd0cc Upgrade webpack from 3.9.1 to 5.100.2 8 hours ago
.github Upgrade webpack from 3.9.1 to 5.100.2 8 hours ago
example Fix 60FPS syncronization / sound issues in Embed example 5 years ago
roms Removed all code specific to my personal site and added some example homebrew roms 15 years ago
src strict equality 8 hours ago
test fix: fixes instructions JSR, PHA, PHP, PLA, RST, RTI, RTS, TSX and TXS 13 hours ago
.eslintrc.json Fix ESLint parser to support ES2017+ trailing commas 8 hours ago
.gitignore Ignore local claude settings 8 hours ago
.npmignore Build uncompressed dist 8 years ago
AUTHORS.md Added Ecin to authors (thanks for the fix!) 15 years ago
CLAUDE.md Add CLAUDE.md development guidance file 14 hours ago
LICENSE Switch to Apache 2 license 5 years ago
README.md readme 13 hours ago
index.d.ts Add types for exported module 13 hours ago
package-lock.json Upgrade webpack from 3.9.1 to 5.100.2 8 hours ago
package.json Upgrade webpack from 3.9.1 to 5.100.2 8 hours ago
tsconfig.json Add TypeScript type checking to CI pipeline 8 hours ago
webpack.config.js Upgrade webpack from 3.9.1 to 5.100.2 8 hours ago

README.md

JSNES

A JavaScript NES emulator.

It's a library that works in both the browser and Node.js. The browser UI is available at https://github.com/bfirsh/jsnes-web.

Installation

For Node.js or Webpack:

$ npm install jsnes

In the browser, you can use unpkg:

<script type="text/javascript" src="https://unpkg.com/jsnes/dist/jsnes.min.js"></script>

Usage

// Initialize and set up outputs
var nes = new jsnes.NES({
  onFrame: function(frameBuffer) {
    // ... write frameBuffer to screen
  },
  onAudioSample: function(left, right) {
    // ... play audio sample
  }
});

// Read ROM data from disk (using Node.js APIs, for the sake of this example)
const fs = require('fs');
var romData = fs.readFileSync('path/to/rom.nes', {encoding: 'binary'});

// Load ROM data as a string or byte array
nes.loadROM(romData);

// Run frames at 60 fps, or as fast as you can.
// You are responsible for reliable timing as best you can on your platform.
nes.frame();
nes.frame();
// ...

// Hook up whatever input device you have to the controller.
nes.buttonDown(1, jsnes.Controller.BUTTON_A);
nes.frame();
nes.buttonUp(1, jsnes.Controller.BUTTON_A);
nes.frame();
// ...

Build

To build a distribution:

$ npm run build

This will create dist/jsnes.min.js.

Running tests

$ npm test

Embedding JSNES in a web page

You can use JSNES to embed a playable version of a ROM in a web page. This is handy if you are a homebrew ROM developer and want to put a playable version of your ROM on its web page.

The best implementation is jsnes-web but unfortunately it is not trivial to reuse the code. You'll have to copy and paste the code from that repository, the use the <Emulator> React component. Here is a usage example..

A project for potential contributors (hello!): jsnes-web should be reusable and on NPM! It just needs compiling and bundling.

A more basic example is in the example/ directory of this repository. Unfortunately this is known to be flawed, and doesn't do timing and sound as well as jsnes-web.

Formatting code

All code must conform to Prettier formatting. The test suite won't pass unless it does.

To automatically format all your code, run:

$ npm run format

Thanks

JSNES is based on James Sanders' vNES, and owes an awful lot to it. It also wouldn't have happened without Matt Wescott's JSSpeccy, which sparked the original idea. (Ben, circa 2008: "Hmm, I wonder what else could run in a browser?!")