Giter VIP home page Giter VIP logo

laines's Introduction

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

laines's People

Contributors

andreaorru avatar kraln avatar pudgema avatar tyfkda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laines's Issues

Save settings

Pretty tired of having to reconfigure the joystick every time I open this :)

Rewrite APU

This is an absolute priority in order to remove the dependency from boost and Blargg's stuff.

An online game?

I'm going to change it into a two-player-online-game.Could you give me some advice?

cycle number of Instructions

I find cycles of instruction don't match the information is list here.
for example:

ADC<imm>() :
inline u8  rd(u16 a)            { T; return access<0>(a);      }
#define G  u16 a = m(); u8 p = rd(a) 
inline u16 imm()   { return PC++;                                       }
template<Mode m> void ADC() { G       ; s16 r = A + p + P[C]; upd_cv(A, p, r); upd_nz(A = r); }

it only ticks once, but in that link this ins need two cycles

 +----------------+-----------------------+---------+---------+----------+
  | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles|
  +----------------+-----------------------+---------+---------+----------+
  |  Immediate     |   ADC #Oper           |    69   |    2    |    2     |

Build issues

Hello,
I didn't manage to build the project on Ubuntu
More precisely, "scons" provokes 404 errors
image
Any idea how to fix?
Can you please provide a build executable for people who have the same issue?
Thanks!

Building on OSX with brew requires some extra flags

Building on OSX for me needed some extra flags in the SConstruct.

Error:

>  scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
clang++ -o src/Sound_Queue.o -c -O3 -march=native -std=c++11 -Wno-unused-value -Isrc src/Sound_Queue.cpp
In file included from src/Sound_Queue.cpp:4:
src/Sound_Queue.h:9:10: fatal error: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
         ^
1 error generated.
scons: *** [src/Sound_Queue.o] Error 1
scons: building terminated because of errors.

I get a successful build with:

diff --git a/SConstruct b/SConstruct
index d9b6582..e95887f 100644
--- a/SConstruct
+++ b/SConstruct
@@ -1,6 +1,6 @@
 from os import environ
 
-flags = ['-O3', '-march=native', '-std=c++11']
+flags = ['-O3', '-march=native', '-std=c++11', '-I/usr/local/include', '-L/usr/local/lib']
 
 env = Environment(ENV       = environ,
                   CXX       = 'clang++',

But it also has a bunch of build spam like:

clang: warning: argument unused during compilation: '-L/usr/local/lib'

for every unit that doesn't need SDL. I'm an ignoramus when it comes to scons so posting an issue vs a pull request in the hope someone else knows how to resolve that trivially.

Branches to a new page

Hi,
maybe I'm wrong (if so, sorry for that), but I can't find the two extra circles, if a branch goes to a new ram page. (see: http://obelisk.me.uk/6502/reference.html#BCC )

should this:
template<Flag f, bool v> void br() { s8 j = rd(imm()); if (P[f] == v) { T; PC += j; } }

be maybe something like this:
template<Flag f, bool v> void br() { s8 j = rd(imm()); if (P[f] == v) { T; if(cross(PC, j)) {T;T;} PC += j; } }

Thank you

libretro support

This has been proposed on /r/emulation and Hacker News. I think there would be a lot of value in it. Prioritize.

Fast Execution

Is it possible to modify this code to remove all frame limiting mechanisms? For instance, if I wanted to adapt this library to allow a machine learning algorithm to process game data at something close to 1000 frames per second (rough number), would that be doable?

Removing the frame limiting mechanism in the run function of the GUI appears to provide some speedup, but are there other modifications to potentially boost this further?

Buffer overflow

Hi, there appears to be a global buffer overflow caused by line 187 here:

LaiNES/src/ppu.cpp

Lines 181 to 187 in 52b7920

secOam[n].id = i;
secOam[n].y = oamMem[i*4 + 0];
secOam[n].tile = oamMem[i*4 + 1];
secOam[n].attr = oamMem[i*4 + 2];
secOam[n].x = oamMem[i*4 + 3];
if (++n > 8)

Because n is allowed to be 8, the next loop iteration where n == 8 overflows the secOam buffer because it can only hold 8 elements.

File explorer not showing all files.

Steps to reproduce:

Build OS X version
open lines and open a directory with 40+ rom files
try going to the last one.

Result: while it does select the rom, it won't show it on screen so you have no idea which rom you picked.

Save games.

Battery-backed RAM should be saved in a file. This is pretty important.

Debugger

It would be quite nice (especially for development of the emulator, but also for development of homebrew and/or examining commercial games) for some amount of debugging capability. I could imagine, variously:

  • ROM Header Viewer
  • CPU State Viewer
  • PPU State Viewer
  • Tilemap RAM Viewer
  • Palette Viewer

Etc. ( example: http://www.fceux.com/web/assets/debugging_environment_1900px.png )

I don't know if this aligns with the goals of the project or not.

Debug assertion "Frame went past end of buffer"

Is this a known issue?

../lib/include/Blip_Buffer.h:230: void Blip_Buffer::end_frame(blip_time_t): Assertion `( "Blip_Buffer::end_frame(): Frame went past end of buffer", samples_avail() <= (long) buffer_size_ )' failed.

It happens here:

#4  0x00007fffed56903d in Blip_Buffer::end_frame (this=0x7fffed5ce0c0 <APU::buf>, t=29783) at ../lib/include/Blip_Buffer.h:229
#5  0x00007fffed568e31 in APU::run_frame (elapsed=29783) at ../src/apu.cpp:45
#6  0x00007fffed55da7d in CPU::run_frame () at ../src/cpu.cpp:273

For context, I was working on a libretro port of your emulator core, but I do not think that is the issue here. The frame count is always at 38 when this crash happens. I'm testing with SMB1 (World).

Set Upscaling filter to 'nearest' for crisp pixel art

Hey, I recently stumbled across your project because I am currently fiddling around with emulators myself and got to say, that I really like your simple, clean interface :) Just wanted to recommend setting the SDL_HINT_RENDER_SCALE_QUALITY hint to 'nearest' instead of 'linear' which (on my opinion) looks way better when upscaling the window.

UWP port

Hi!
Nice project. "1000 lines of code" is really hacker-style :))
Please remake it into UWP app for Win11/XBox Compatibilty ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.