# cfxnes API
- [cfxnes](#user-content-cfxnesoptions)
- [nes](#user-content-nes)
- [nes.rom](#user-content-nesrom)
- [nes.video](#user-content-nesvideo)
- [nes.fullscreen](#user-content-nesfullscreen)
- [nes.audio](#user-content-nesaudio)
- [nes.devices](#user-content-nesdevices)
- [nes.inputs](#user-content-nesinputs)
- [nes.config](#user-content-nesconfig)
## cfxnes([options])
Function that returns new emulator instance.
**options** - initialization options. See the following documentation sections for their description. Options with `undefined` value will be ignored.
``` javascript
let nes;
// No options specified
nes = cfxnes();
// All options and their default values
nes = cfxnes({
region: 'auto',
speed: 1,
rom: undefined,
JSZip: undefined,
video: {
output: null,
renderer: 'webgl',
scale: 1,
palette: 'fceux',
filter: 'nearest',
debug: false,
},
fullscreen: {
type: 'maximized',
}
audio: {
enabled: true,
volume: {
master: 0.5,
pulse1: 1,
pulse2: 1,
triangle: 1,
noise: 1,
dmc: 1,
},
},
devices: {
1: 'joypad',
2: 'zapper',
},
inputs: {
'1.joypad.a': 'keyboard.x',
'1.joypad.b': ['keyboard.y', 'keyboard.z'],
'1.joypad.start': 'keyboard.enter',
'1.joypad.select': 'keyboard.shift',
'1.joypad.up': 'keyboard.up',
'1.joypad.down': 'keyboard.down',
'1.joypad.left': 'keyboard.left',
'1.joypad.right': 'keyboard.right',
'2.zapper.trigger': 'mouse.left',
},
});
```
### Properties
| Name | Type | Writable | Default | Description |
| ---- | ---- | -------- | ------- | ----------- |
| version | `string` | no | | cfxnes version. |
| logLevel | `string` | yes | `'warn'` | Verbosity of logging for all emulator instances. `'off'` - Logging is disabled. `'error'` - Log errors. `'warn'` - Log errors and warnings. `'info'` - Log errors, warnings and info messages. |
``` javascript
cfxnes.version; // '0.4.0', '1.0.0', '1.2.1', etc.
cfxnes.logLevel = 'off'; // Disable logging
cfxnes.logLevel = 'info'; // Log everything
```
## nes
Emulator instance returned by the `cfxnes` function.
### Properties
| Name | Type | Writable | Default | Description |
| ---- | ---- | -------- | ------- | ----------- |
| running | `boolean` | no | `false` | `true` when emulator is running, `false` otherwise. |
| fps | `number` | no || Number of frames per second of running emulator. |
| region | `string` | yes | `'auto'` | Emulated NES region. `'auto'` - Automatic region detection (not very reliable). `'ntsc'` - NTSC region (60 FPS). `'pal'` - PAL region (50 FPS). |
| speed | `number` | yes | `1` | Emulation speed multiplier. It must be larger than 0. |
| nvram | `Uint8Array` | no | null | Provides access to NVRAM. NVRAM (Non-Volatile RAM) is a memory that is usually battery-backed and serves as a place for game saves. NVRAM is only used by some games (e.g., The Legend of Zelda or Final Fantasy). The property is `null` when NVRAM is not available. |
| rom | `object` | no | | [ROM module](#user-content-nesrom) |
| video | `object` | no | | [Video module](#user-content-nesvideo) |
| fullscreen | `object` | no | | [Fullscreen module](#user-content-nesfullscreen) |
| audio | `object` | no | | [Audio module](#user-content-nesaudio) |
| devices | `object` | no | | [Devices module](#user-content-nesdevices) |
| inputs | `object` | no | | [Inputs module](#user-content-nesinputs) |
| config | `object` | no | | [Configuration module](#user-content-nesconfig) |
### Methods
| Signature | Description |
| --------- | ----------- |
| start() | Starts emulator. |
| stop() | Stops emulator. |
| step() | Forces emulator to render one frame. |
| power() | HW reset (NES power button). |
| reset() | SW reset (NES reset button). |
``` javascript
const nes = cfxnes();
nes.region = 'pal'; // Set PAL region
nes.speed = 1.5; // Set 1.5x emulation speed
nes.running; // false
nes.start(); // Start emulator
nes.running; // true
nes.stop(); // Stop emulator
nes.running; // false
nes.power(); // HW reset
nes.reset(); // SW reset
if (nvram.data) {
nvram.data.fill(0); // Clear NVRAM of the currently running game
}
```
## nes.rom
Module that can load ROM images into emulator.
Supported ROM image formats are [iNES](http://wiki.nesdev.com/w/index.php/INES) and [NES 2.0](http://wiki.nesdev.com/w/index.php/NES_2.0). Cfxnes can also load zipped ROM image when [JSZip library](https://github.com/Stuk/jszip) (^3.1.0) is present. JSZip can be provided either through global variable `window.JSZip` or using `JSZip` initialization option.
``` javascript
cfxnes({JSZip});
```
ROM image can be loaded in two ways:
1. Use the `rom` initialization option. This will load ROM image from the specified source and then automatically starts execution. All loading errors will be logged using `console.error`.
2. Call the `nes.rom.load()` method. This allows more precise control over the loading process.
``` javascript
// 1. Use the 'rom' initialization option
const nes = cfxnes({rom: source});
// 2. Call the nes.rom.load() method
nes.rom.load(source)
.then(() => {/* handle success */})
.catch(error => {/* handle error */})
```
The source can be `Uint8Array`, `ArrayBuffer`, `Array`, `Blob` or `string`. String value is interpreted as URL of a ROM image.
### Properties
| Name | Type | Writable | Default | Description |
| ---- | ---- | -------- | ------- | ----------- |
| loaded | `boolean` | no | `false` | `true` when a ROM image is loaded, `false` otherwise. |
| sha1 | `string` | no | `null` | SHA-1 of loaded ROM image, `null` otherwise. |
### Methods
| Signature | Returns | Description |
| --------- | ------- | ----------- |
| load(source) | `Promise` | Loads ROM image from the specified source. |
| unload() || Unloads loaded ROM image. |
``` javascript
const nes = cfxnes();
const {rom} = nes;
rom.loaded; // false
rom.sha1; // null
// Load ROM image from relative URL
rom.load('data/game.nes').then(() => {
rom.loaded; // true
rom.sha1; // SHA-1 of the loaded image
rom.unload(); // Unload the image
rom.loaded; // false;
}).catch(error => {
console.error('Oops!', error);
})
// Load ROM image from memory or blob
var data = getData(); // Uint8Array, ArrayBuffer, Array, Blob
rom.load(data).then(/* ... */);
```
## nes.video
Display settings module.
Cfxnes requires a `canvas` element to render its video output. The canvas can be specified in several ways:
1. Create canvas element with `cfxnes` ID. It will be automatically used during cfxnes initialization.
2. Use the `video.output` initialization option.
3. Use the `nes.video.output` property.
``` html