• Stars
    star
    1,472
  • Rank 31,940 (Top 0.7 %)
  • Language
    C++
  • License
    BSD 2-Clause "Sim...
  • Created about 11 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cycle-accurate NES emulator in ~1000 lines of code

LaiNES

Compact, cycle-accurate NES emulator in ~1000 lines of C++ (well, originally).

File Browser Super Mario Bros. 3 Kirby's Adventure

Star Wars Super Mario Bros. The Legend of Zelda

Requirements

LaiNES should run on any Unix system that is compatible with the following tools.

  • SCons
  • C++11 compatible compiler (e.g. clang++)
  • SDL2 (including sdl2-ttf and sdl2-image)

Building and running

Install the dependencies:

# Arch Linux:
sudo pacman -S clang scons sdl2 sdl2_image sdl2_ttf

# Debian-based systems:
sudo apt-get install clang scons libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev

# Mac OS X:
brew install scons sdl2 sdl2_image sdl2_ttf

Compile and run:

git clone --recursive https://github.com/AndreaOrru/LaiNES && cd LaiNES
scons
./laines

Usage

The emulator comes bundled with a simple GUI to navigate the filesystem and set preferences. Use arrow keys and Enter to operate it. ESC toggles between emulation and menu.

The size of the window and the controls are customizable. LaiNES supports multiple controllers and should work with joysticks as well. The default controls for the first player are as follows:

Controller Settings

Compatibility

LaiNES implements the most common mappers, which should be enough for a good percentage of the games:

  • NROM (Mapper 000)
  • MMC1 / SxROM (Mapper 001)
  • UxROM (Mapper 002)
  • CNROM (Mapper 003)
  • MMC3, MMC6 / TxROM (Mapper 004)

You can check the compatibility for each ROM in the following list: http://tuxnes.sourceforge.net/nesmapper.txt

Technical notes

The 6502 CPU and the PPU are implemented in just 219 and 283 lines of code respectively. Meta-programming is used extensively to keep the codebase compact. Here is a good example of how that is achieved:

/* Cycle emulation.
 *     For each CPU cycle, we call the PPU thrice, because it runs at 3 times the frequency. */
#define T   tick()
inline void tick() { PPU::step(); PPU::step(); PPU::step(); ... }
...

/* Addressing modes.
 *     These are all the possible ways instructions can access memory. */
typedef u16 (*Mode)(void);
inline u16 imm() { return PC++; }
...
inline u16 zpx() { T; return (zp() + X) % 0x100; }
...

/* Fetch parameter.
 *     Get the address of the opcode parameter in a, and the value in p. */
#define G  u16 a = m(); u8 p = rd(a)
...

/* Instruction emulation (LDx where x is in registers {A, X, Y}).
 *     upd_nz, not shown, just updates the CPU flags register. */
template<u8& r, Mode m> void ld() { G; upd_nz(r = p); }
...

/* Execute a CPU instruction.
 *     Opcodes are instantiated with the right template parameters
 *     (i.e. register and/or addressing mode).*/
void exec()
{
    switch (rd(PC++))  // Fetch the opcode.
    {
        // Select the right function to emulate the instruction:
         ...
         case 0xA0: return ld<Y,imm>();  case 0xA1: return ld<A,izx>();
         ...
    }
}

Known issues

  • If you're experiencing audio issues on Linux, try typing export SDL_AUDIODRIVER=ALSA before running the emulator.

Contributors

  • Jeff Katz - Mapper 002 & 003, configuration.
  • PudgeMa - Scrollable menu and bug fixes.
  • tyfkda - Show error message instead of segfault for unsupported mappers.

References and credits

More Repositories

1

zen

Experimental operating system written in Zig
Assembly
431
star
2

fzf-mopidy-spotify.vim

Add Spotify music from Vim to your Mopidy playlist with fzf-powered fuzzy-finding
Vim Script
35
star
3

gilgamesh

SNES reverse engineering toolkit with support for static recompilation
C++
26
star
4

Lucy

Compiler for a toy language in a couple hundred lines. Just messing around with ANTLR4 and LLVM really
Python
19
star
5

Romhacking

Tools, hacks, technical info about good old games
Python
15
star
6

Utopia

Utopia is an UNIX-like operating system built around a microkernel, with a focus on code clarity
C
13
star
7

Asar

Originally written by Alcaro. Asar is a SNES assembler, intended as a replacement for xkas v0.06
C++
9
star
8

X

X is a new system programming language designed to bring high-level abstractions to bare metal
Python
6
star
9

Japanese

A set of tools to help me study Japanese
Python
6
star
10

PongDS

A port of Pong from SNES to DS. Proof of concept for the Gilgamesh decompiler
C++
5
star
11

todoist-org

Sync to and from Todoist and Emacs Org files
Python
4
star
12

org-todoist.el

Org sync with Todoist
Emacs Lisp
3
star
13

ConstructiveDIM

Constructive neural network algorithm for image decomposition, based on Divisive Input Modulation
MATLAB
3
star
14

spacemacs.d

[DEPRECATED] My Spacemacs configuration
Emacs Lisp
1
star
15

Yugen

Subtly profound text editor inspired by Emacs
Python
1
star
16

MLCourseraPython

Solutions in Python for the Machine Learning course by Andrew Ng on Coursera.
Python
1
star
17

old-emacs.d

My Emacs configuration.
Emacs Lisp
1
star
18

dotfiles

Linux configuration files
Emacs Lisp
1
star
19

vim-dlang-autofold-unittest

Vim Script
1
star