emuname documentation 2016 Mar 18

Plugins - Class Emuname - Abstract Class ENPlugin - Interface ENMethod - Class ENEvent
Class PixelCanvas - Class NES - Class Memory - Class Mapper - Class CPU6502 - Class PPU2C02 - Script engine

Plugins

To create a plugin

• Add the path to emuname.jar to your compiler's classpath
• Write an ENPlugin subclass, importing net.shygoo.emuname.*
• Write a constructor for the class which has no parameters and calls super(String id, int initOrder)
• Override init(Emuname emuname)
• You may use emuname.on to add methods to hooks, and emuname.addPluginMenuItem to add plugin items to the Plugins menu
• JAR your class(es), with the Main-Class attribute of the JAR's manifest set to your ENPlugin subclass's fully qualified name
• Drop the JAR in emuname's plugins directory

Example

This plugin uses the active Graphics2D object of every frame to draw "Hello world" to the screen.
MyPlugin.java
package example.plugin; import net.shygoo.emuname.*; import java.awt.Graphics2D; public class MyPlugin extends ENPlugin { public MyPlugin(){ super("my_plugin", 0); } public void init(Emuname emuname){ emuname.on("drawend", (e) -> { e.getGraphics().drawString("Hello World!", 20, 20); }); } }

Manifest.txt
Manifest-Version: 1.0 Main-Class: example.plugin.MyPlugin

build.bat
@echo off set /p emunamejar="Enter path to emuname.jar: " mkdir bin javac -d bin -cp .;%emunamejar% MyPlugin.java cd bin jar -cmf ../Manifest.txt ../MyPlugin.jar . cd .. rmdir /q /s bin pause

Available plugin hooks

IDExecution timeENEvent type
drawstartBefore every frame is drawn (emuname.screen.preframe)EVENT_GRAPHICS
drawendAfter every frame is drawn (emuname.screen.postframe)EVENT_GRAPHICS
stepstartBefore every CPU step (emuname.nes.prestep)EVENT_NULL
stependAfter every CPU step (emuname.nes.poststep)EVENT_NULL
clickWhen mouse clicks the game screenEVENT_MOUSE
wheelWhen mouse wheel is moved over the game screenEVENT_WHEEL
mouseoverWhen mouse enters the game screenEVENT_MOUSE
mouseoutWhen mouse exits the game screenEVENT_MOUSE
mousemoveWhen mouse moves over the game screenEVENT_MOUSE
mousedownWhen mouse button is pressed on the game screenEVENT_MOUSE
mouseupWhen mouse button is released on the game screenEVENT_MOUSE
keyupWhen key is released over the game screenEVENT_KEY
keydownWhen key is pressed over the game screenEVENT_KEY

Default plugins

IDInitiation orderJAR file
default_menus0DefaultMenus.jar
debugger1Debugger.jar
crt_effects1CRTEffects.jar

Default plugin menu items

IDDefining pluginCaptionFunctionJava class
dbgdefault_menusDebuggingDebugger submenuJMenu
dbg_memorydebugger> Memory...Shows memory windowJMenuItem
dbg_scannerdebugger> Memory scanner...Shows memory scanner windowJMenuItem
dbg_commandsdebugger> Commands...Shows CPU commands windowJMenuItem
gamesdefault_menusGamesSubmenu for game-specific pluginsJMenu
games_balloon_fight_mouseballoon_fight_mouse> Balloon fight draggerToggles Balloon Fight entity drag n' dropJMenuItem
viddefault_menusVideo filtersSubmenu for video filtersJMenu
vid_smoothingcrt_effects> Bilinear smoothingToggles Bilinear smoothingJCheckBoxMenuItem
vid_scanlinescrt_effects> CRT ScanlinesToggles CRT-like scanlinesJCheckBoxMenuItem
vid_noisecrt_effects> CRT NoiseToggles CRT-like noiseJCheckBoxMenuItem

Class Emuname

net.shygoo.emuname.Emuname extends javax.swing.JFrame
The main window.

Fields

NESnes

Methods

NESgetNes()Returns the NES object
PixelCanvasgetScreen()Returns the PixelCanvas component
ConfiggetConfig()Returns the config object
System
voidreset()Soft-resets game
voidpause()Pauses emulation
voidresume()Resumes emulation
voidend()Stops emulation and shows the rom selection screen
Plugin management
inton(String hookId, ENMethod method)Adds a method to the hook specified by hookId
Returns a unique numeric ID for the method
voidoff(String hookId, int id)Removes a method from the hook specified by hookId
ENPlugingetPlugin(String id)Returns a plugin object by id
String[]getPluginIds()Returns an array of existing plugin IDs
voidaddPluginMenuItem(String id, JMenuItem menuItem) Adds menuItem to the plugin menu
voidaddPluginMenuItem(String id, String attachToId,
 JMenuItem menuItem)
Adds menuItem to an existing menu item specified by attachToId
voidaddPluginMenuItem(String id, JFrame frame,
 String itemCaption)
Adds menuItem that shows frame when clicked
voidaddPluginMenuItem(String id, String attachToId,
 JFrame frame, String itemCaption)
Adds a MenuItem to an existing menu item specified by attachToId
Shows frame when clicked
JMenuItemgetPluginMenuItem(String id)Returns a plugin menu item by id
String[]getPluginMenuItemIds()Returns an array of existing plugin menu IDs

Abstract Class ENPlugin

net.shygoo.emuname.ENPlugin
Abstract class for subclassing third party emuname plugins. Subclasses must invoke super(String id, int initOrder) to define the plugin's final identification string and initiation order.

Constructor

-ENPlugin(String id, int initOrder)

Methods

abstract voidinit(Emuname emuname)Serves as a post-constructor, providing access to emuname
StringgetId()Returns the plugin's ID string
intgetInitOrder()Returns the plugin's initiation order

Interface ENMethod

net.shygoo.emuname.ENMethod
Functional interface for the plugin hooks of emuname.

Methods

voidrun(ENEvent e)The method

Class ENEvent

net.shygoo.emuname.ENEvent
Wrapper class for various events. Instance methods will throw a runtime exception if they don't correspond with the event type.

Constructors

-ENEvent()Constructs an ENEvent object with the type set to EVENT_NULL
-ENEvent(MouseEvent e, double xScale, double yScale)Constructs an ENEvent object with the type set to EVENT_MOUSE
-ENEvent(MouseWheelEvent e, double xScale, double yScale)Constructs an ENEvent object with the type set to EVENT_WHEEL
-ENEvent(KeyEvent e)Constructs an ENEvent object with the type set to EVENT_KEY
-ENEvent(Graphics2D g)Constructs an ENEvent object with the type set to EVENT_GRAPHICS

Fields

static intEVENT_NULL(0)
static intEVENT_MOUSE(1)
static intEVENT_WHEEL(2)
static intEVENT_KEY(3)
static intEVENT_GRAPHICS(4)

Methods

static StringgetTypeName(int type)Returns the name of an event type number
intgetType()Returns the instance's event type number
StringgetTypeName()Returns the instance's event type name
For EVENT_MOUSE and EVENT_WHEEL
intgetX()Returns the scaled x position of the cursor on the game screen
intgetY()Returns the scaled y position of the cursor on the game screen
For EVENT_MOUSE
MouseEventgetMouseEvent()Returns the associated AWT MouseEvent
For EVENT_WHEEL
MouseWheelEventgetMouseWheelEvent()Returns the associated AWT MouseWheelEvent
intgetScrollAmount()Returns the event's mouse wheel scroll amount
For EVENT_KEY
KeyEventgetKeyEvent()Returns the associated AWT KeyEvent
intgetKeyCode()Returns the event's key code
chargetKeyChar()Returns the event's key character
For EVENT_GRAPHICS
Graphics2DgetGraphics()Returns the associated AWT Graphics2D

Class NES

net.shygoo.emuname.NES

Constructor

-NES()Constructs a NES emulator

Fields

INES file format constants
static intINES_HEADERSIZE (16) iNES rom header size
static intINES_HEADER_PRGROM_SIZE(0x04) iNES rom offset of program rom size byte
static intINES_HEADER_CHRROM_SIZE(0x05) iNES rom offset of character rom size byte
static intINES_HEADER_FLAGS6 (0x06) iNES rom offset of flags6 byte
static intINES_HEADER_FLAGS7 (0x07) iNES rom offset of flags7 byte
System memory I/O register offsets
static intREG_PPU_CTRL1 (0x2000) PPU Control 1
static intREG_PPU_CTRL2 (0x2001) PPU Control 2
static intREG_PPU_STATUS (0x2002)
static intREG_PPU_OAM_ADDRESS(0x2003)
static intREG_PPU_OAM_IO (0x2004)
static intREG_PPU_SCROLL (0x2005)
static intREG_PPU_ADDRESS (0x2006)
static intREG_PPU_IO (0x2007)
static intREG_APU_PULSE1_CTRL(0x4000)
static intREG_APU_PULSE1_RC (0x4001)
static intREG_APU_PULSE1_FT (0x4002)
static intREG_APU_PULSE1_CT (0x4003)
static intREG_APU_PULSE2_CTRL(0x4004)
static intREG_APU_PULSE2_RC (0x4005)
static intREG_APU_PULSE2_FT (0x4006)
static intREG_APU_PULSE2_CT (0x4007)
static intREG_APU_TRI_CTRL1 (0x4008)
static intREG_APU_TRI_CTRL2 (0x4009)
static intREG_APU_TRI_FREQ1 (0x400A)
static intREG_APU_TRI_FREQ2 (0x400B)
static intREG_APU_NOISE_CTRL1(0x400C)
static intREG_APU_NOISE_CTRL2(0x400D)
static intREG_APU_NOISE_FREQ1(0x400E)
static intREG_APU_NOISE_FREQ2(0x400F)
static intREG_APU_DM_CTRL (0x4010)
static intREG_APU_DM_DA (0x4011)
static intREG_APU_DM_ADDRESS (0x4012)
static intREG_APU_DM_LENGTH (0x4013)
static intREG_PPU_OAM_DMA (0x4014)
static intREG_APU_VCS (0x4015)
static intREG_JOYPAD1 (0x4016)
static intREG_JOYPAD2 (0x4017)
Joypad strobe positions
static intBUTTON_A (0)
static intBUTTON_B (1)
static intBUTTON_SELECT (2)
static intBUTTON_START (3)
static intBUTTON_UP (4)
static intBUTTON_DOWN (5)
static intBUTTON_LEFT (6)
static intBUTTON_RIGHT (7)
Hardware
CPU6502cpu Central processing unit
PPU2C02ppu Picture processing unit
APU2A03apu Audio processing unit (not started)
Memorymem System/CPU memory
Mappermapper Cartridge memory mapper
Miscellaneous
byte[]romiNES rom file data

Methods

voidresetMemory()Reinstantiates mem
voidloadRom(String path)
voidrunUntilVBlank()
voiddbgBreak()Ends the current runUntilVBlank loop
voidprestep()Does nothing by default
Invoked before every CPU step
voidpoststep()Does nothing by default
Invoked after every CPU step

Class CPU6502

net.shygoo.hw.CPU6502

Constructor

-CPU6502(Memory mem)Constructs a new CPU6502 hardware emulator that uses mem as memory space for programs

Methods

intgetPC()Returns program counter offset
intgetAC()Returns Accumulator value
intgetXR()Returns X index register value
intgetYR()Returns Y index register value
intgetSP()Returns Stack pointer value
intgetSR()Returns Status register value
voidsetPC(int value)Sets Program counter offset
voidsetAC(int value)Sets Accumulator value
voidsetXR(int value)Sets X index register value
voidsetYR(int value)Sets Y index register value
voidsetSP(int value)Sets Stack pointer value
voidsetSR(int value)Sets Status register value
voidclock(int cycles)
voidnonMaskableInterrupt()
voidreset()
intstep()
voidsetMemory(Memory mem)

Class PPU2C02

net.shygoo.hw.PPU2C02

Constructor

-PPU2C02(Memory chrMem)Constructs a PPU2C02 hardware emulator that uses chrMem as character memory space

Fields

Mirroring mode constants
static intMIRRORING_ONESCREEN (0)
static intMIRRORING_HORIZONTAL (1)
static intMIRRORING_VERTICAL (2)
static intMIRRORING_FOURSCREEN (3)
Hardware
Memory mem PPU main memory
byte[0x100] oam PPU object attribute (sprite) memory
Memory chrMem Cartridge character memory
Internal states
int scrollX
int scrollY
int scrollState // 0 = x, 1 = y // todo consts
int address
int addressState // 0 = address msb, 1 = address lsb //todo consts
int nameTableOffset
int increment
int pTableSprOffset
int pTableBgOffset
int sprSize
boolean nmi
boolean monochrome
boolean clipBg
boolean clipSpr
boolean showBg
boolean showSpr
int bgColor
Miscellaneous
int[0x10000]display RGB888 frame buffer
int currentScanline // max at 262

Methods

voidincAddress() // todo make private
intreadNext()
voidwriteNext(int value)
voidresetScanlines()
booleangetSprite0Hit() // todo isSpriteZeroHit()
booleangetScanline8Sprites()// todo change to isSpriteOverflow()
voiddrawSpritesLine() // todo make private
voiddrawBackgroundLine() // todo make private
voiddrawScanline()
voidsetChrMem(Memory chrMem)
voidresetMemory()
voidsetMirroringMode(int mirroringMode)Sets the nametable screen mirroring mode

Abstract Class Memory

net.shygoo.misc.Memory

Methods

abstract intread(int offset, boolean doEvents)return integer from memory
abstract voidwrite(int offset, int value, boolean doEvents)write integer to memory
intread(int offset)wrapper for read that defaults doEvents to true // TODO make false
voidwrite(int offset, int value)wrapper for write that defaults doEvents to true // TODO make false

Abstract Class Mapper

net.shygoo.emuname.mapper.Mapper

Constructor

-Mapper(NES nes)Constructs a Mapper

Fields

Hardware
MemorychrMemMemory wrapper for cartridge character rom / video ram
MemoryprgMemMemory wrapper for cartridge program rom

Methods

int getDefaultPrgBankA()Get load-time program bank A number
int getDefaultPrgBankB()Get load-time program bank B number
voidsetPrgBankA(int bank)
voidsetPrgBankB(int bank)
intgetPrgBankA()
intgetPrgBankB()
voidassignDefaultBanks()todo change to setPrgBanksDefault
StringgetName()

Class PixelCanvas

net.shygoo.component.PixelCanvas extends javax.swing.JPanel

Constructor

-PixelCanvas(int width, int height, int[] rgbArray)Constructs a PixelCanvas
paint() is overridden to draw whatever is in the rgbArray buffer

Methods

voidsetScale(int scale)Sets the scale of the component
int getScale()Returns the current scale of the component
voidsetDefaultDrawingEnabled(boolean setting)Prevents rgbArray from drawing to the component if false
Should be used when the default drawing technique needs to be replaced (eg upscaling filters)
booleangetDefaultDrawingEnabled()Returns defaultDrawing
voidsetImageSmoothingEnabled(boolean setting)Enables or disables bilinear smoothing based on setting // Not needed, todo move to plugin
voidsetRgbArray(int[] rgbArray)Changes the rgb buffer
Graphics2DgetActiveG2d()Returns the last Graphics2D object passed to paint()
Should be used in the postframe() method for correct timing
voidpreframe()Can be overriden to provide a hook in paint() for before the image is drawn
Changes can be made to rgbArray here
Changes can be made to the Graphics2D object here if default drawing is disabled
voidpostframe()Can be overriden to provide hook in paint() for after the image is drawn
Changes can be made to the current Graphics2D object here

Script engine

Global properties

emuname

Reference to the Emuname object

nes

Reference to the NES object

frameEvents

Management object for a list of javascript functions to be called by the emulation thread during every VBlank phase
 • frameEvents.add(callback) -- Adds a function and returns its ID
 • frameEvents.remove(id) -- Removes a function by its ID
 • frameEvents.clear() -- Removes all functions
 • frameEvents.run() -- Executes all functions (called automatically)

For built in globals, refer to the Nashorn API documentation or try print(Object.getOwnPropertyNames(this))

Example scripts

Sink all enemy players in Balloon Fight:
var OFFSET_ENEMIES_Y = 0x009C; var ENEMIES_MAX_COUNT = 6; frameEvents.add(function(){ for(var i = 0; i < ENEMIES_MAX_COUNT; i++){ var offsetY = OFFSET_ENEMIES_Y + i; var y = nes.mem.read(offsetY); nes.mem.write(offsetY, y + 5); } });

Frame buffer color inversion:
frameEvents.add(function(){ for(var i = 0; i < nes.ppu.display.length; i++){ nes.ppu.display[i] = ~nes.ppu.display[i]; } });

shygoo.net