Skip to content
This repository has been archived by the owner on Jan 31, 2021. It is now read-only.
zecoo edited this page Mar 3, 2019 · 8 revisions

Weaknes

Weaknes is a NES emulator written with JavaScript.

Build

$ npm install
$ npm run build

Usage

NES emulator

index.js (All-in-one)

AllInOne instance includes instances of nes, screen, audio and rom.

async function main() {
  const path = './assets/nestest/nestest.nes'

  const AllInOne = NesPack.AllInOne
  const screenId = 'canvas'
  const isDebug = false
  const allInOne = new AllInOne(screenId, isDebug)
  await allInOne.run(path)
}

main()

index.js (Separated)

async function main() {
  const Nes = NesPack.Nes
  const Screen = NesPack.Screen
  const Audio = NesPack.Audio
  const Rom = NesPack.Rom

  nes = new Nes()

  /* A screen's argument is Canvas tag's id */
  const screen = new Screen.Browser('canvas')
  const audio = new Audio()

  nes.connect({
    screen,
    audio
  })

  /* Download ROM */
  const path = './assets/nestest/nestest.nes'
  const data = await fetch(path)
  .then(response => response.arrayBuffer())
  .then(buffer => new Uint8Array(buffer))

  const rom = new Rom(data)
  nes.rom = rom

  nes.run()
}

main()

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="icon" href="data:;base64,iVBORwOKGO=" />
  <title>Screen of NES emulator</title>
</head>
<body>
  <canvas id="canvas" width="256" height="240"></canvas>
  <script src="dist/bundle.js"></script>
  <script src="index.js"></script>
</body>
</html>

Tool

Tool dumps CHR-ROM, Color palettes and 4 nametables.

Dump CHR-ROM

Dump color palettes

Dump 4 nametables

index.js

If you want to update dumps automatically, use setInterval.

async function main() {
  const path = './assets/nestest/nestest.nes'

  const AllInOne = NesPack.AllInOne
  const screenId = 'canvas'
  const isDebug = false
  const allInOne = new AllInOne(screenId, isDebug)
  await allInOne.run(path)

  const nes = allInOne.nes
  const Tools = NesPack.Tools
  const palette = [0x31, 0x3d, 0x2d, 0x1f]
  const tools = new Tools(nes)

  tools.dumpChrRom('chr-dump', palette)

  //Auto update
  const interval = 300
  setInterval(tools.dumpPalette.bind(tools, 'palette-dump'), 300)
  const isShadowEnabled = true
  setInterval(tools.dumpBackground.bind(tools, 'background', isShadowEnabled), 300)
}

main()

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="icon" href="data:;base64,iVBORwOKGO=" />
  <title>Screen of NES emulator</title>
</head>
<body>
  <canvas id="canvas" width="256" height="240"></canvas>
  <div>
    <canvas id="chr-dump" width="256" height="120"></canvas>
    <canvas id="palette-dump" width="256" height="120"></canvas>
  </div>
  <canvas id="background" width="512" height="480"></canvas>
  <script src="dist/bundle.js"></script>
  <script src="index.js"></script>
</body>
</html>

Run samples

There are samples in samples directory.
Let's run below commands with rom's path.

Browser

$ npm run nes:browser <rom's path>

Electron

$ npm run nes:electron <rom's path>

Nodejs

$ npm run nes:nodejs <rom's path>