nes

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package nes contains everything that lives within the NES system. main entry point above it should be portable.

Index

Constants

View Source
const (
	Mode_Immediate
	Mode_Absolute

	Mode_AbsoluteX
	Mode_AbsoluteY

	Mode_Accumulator
	Mode_Implied
	Mode_Indirect
	Mode_IndirectX
	Mode_IndirectY
	Mode_Relative
	Mode_ZeroPage
	Mode_ZeroPageX
	Mode_ZeroPageY
)

addressing modes

View Source
const (
	ADC
	AND
	ASL
	BCC
	BCS
	BEQ
	BIT
	BMI
	BNE
	BPL
	BRK
	BVC
	BVS
	CLC
	CLD
	CLI
	CLV
	CMP
	CPX
	CPY
	DEC
	DEX
	DEY
	EOR
	INC
	INX
	INY
	JMP
	JSR
	LDA
	LDX
	LDY
	LSR
	NOP
	ORA
	PHA
	PHP
	PLA
	PLP
	ROL
	ROR
	RTI
	RTS
	SBC
	SEC
	SED
	SEI
	STA
	STX
	STY
	TAX
	TAY
	TSX
	TXA
	TXS
	TYA
)
View Source
const (
	// _ = iota
	//  7 6 5 4 3 2 1 0
	//  N V   B D I Z C
	// 0-C=Carry
	// 1 Z=Zero Flag
	// 2 I=Interupt Disable
	// 3 D=Decimal
	// 4 B=Brk/software interupt
	// 5
	// 6 V Overflow Flag
	// 7 N Negative Flag
	Status_C = iota //Carry Flag
	Status_Z        //Zero Flag
	Status_I        //Interupt Disable
	Status_D        //Decimale Flag, no system need but coded anyway
	Status_B        //If break executed, causing an IRQ

	Status_V //Overflow
	Status_N //Negative flag (1 for negative numb)
)
View Source
const CuclesPerFrame = 29780.5
View Source
const CyclesPerSecond = 1789773

CyclesPerSecond = 1.79mhz

Variables

View Source
var Blue = "\033[34m"
View Source
var Cyan = "\033[36m"
View Source
var Gray = "\033[37m"
View Source
var Green = "\033[32m"
View Source
var OpTable = map[int]OpCodeInfo{}/* 151 elements not displayed */
View Source
var Palette = map[int]color.RGBA{
	0x00: {0x75, 0x75, 0x75, 0xff},
	0x01: {0x27, 0x1B, 0x8F, 0xff},
	0x02: {0x00, 0x00, 0xAB, 0xff},
	0x03: {0x47, 0x00, 0x9F, 0xff},
	0x04: {0x8F, 0x00, 0x77, 0xff},
	0x05: {0xAB, 0x00, 0x13, 0xff},
	0x06: {0xA7, 0x00, 0x00, 0xff},
	0x07: {0x7F, 0x0B, 0x00, 0xff},
	0x08: {0x43, 0x2F, 0x00, 0xff},
	0x09: {0x00, 0x47, 0x00, 0xff},
	0x0A: {0x00, 0x51, 0x00, 0xff},
	0x0B: {0x00, 0x3F, 0x17, 0xff},
	0x0C: {0x1B, 0x3F, 0x5F, 0xff},
	0x0D: {0x00, 0x00, 0x00, 0xff},
	0x0E: {0x00, 0x00, 0x00, 0xff},
	0x0F: {0x00, 0x00, 0x00, 0xff},
	0x10: {0xBC, 0xBC, 0xBC, 0xff},
	0x11: {0x00, 0x73, 0xEF, 0xff},
	0x12: {0x23, 0x3B, 0xEF, 0xff},
	0x13: {0x83, 0x00, 0xF3, 0xff},
	0x14: {0xBF, 0x00, 0xBF, 0xff},
	0x15: {0xE7, 0x00, 0x5B, 0xff},
	0x16: {0xDB, 0x2B, 0x00, 0xff},
	0x17: {0xCB, 0x4F, 0x0F, 0xff},
	0x18: {0x8B, 0x73, 0x00, 0xff},
	0x19: {0x00, 0x97, 0x00, 0xff},
	0x1A: {0x00, 0xAB, 0x00, 0xff},
	0x1B: {0x00, 0x93, 0x3B, 0xff},
	0x1C: {0x00, 0x83, 0x8B, 0xff},
	0x1D: {0x00, 0x00, 0x00, 0xff},
	0x1E: {0x00, 0x00, 0x00, 0xff},
	0x1F: {0x00, 0x00, 0x00, 0xff},
	0x20: {0xFF, 0xFF, 0xFF, 0xff},
	0x21: {0x3F, 0xBF, 0xFF, 0xff},
	0x22: {0x5F, 0x97, 0xFF, 0xff},
	0x23: {0xA7, 0x8B, 0xFD, 0xff},
	0x24: {0xF7, 0x7B, 0xFF, 0xff},
	0x25: {0xFF, 0x77, 0xB7, 0xff},
	0x26: {0xFF, 0x77, 0x63, 0xff},
	0x27: {0xFF, 0x9B, 0x3B, 0xff},
	0x28: {0xF3, 0xBF, 0x3F, 0xff},
	0x29: {0x83, 0xD3, 0x13, 0xff},
	0x2A: {0x4F, 0xDF, 0x4B, 0xff},
	0x2B: {0x58, 0xF8, 0x98, 0xff},
	0x2C: {0x00, 0xEB, 0xDB, 0xff},
	0x2D: {0x00, 0x00, 0x00, 0xff},
	0x2E: {0x00, 0x00, 0x00, 0xff},
	0x2F: {0x00, 0x00, 0x00, 0xff},
	0x30: {0xFF, 0xFF, 0xFF, 0xff},
	0x31: {0xAB, 0xE7, 0xFF, 0xff},
	0x32: {0xC7, 0xD7, 0xFF, 0xff},
	0x33: {0xD7, 0xCB, 0xFF, 0xff},
	0x34: {0xFF, 0xC7, 0xFF, 0xff},
	0x35: {0xFF, 0xC7, 0xDB, 0xff},
	0x36: {0xFF, 0xBF, 0xB3, 0xff},
	0x37: {0xFF, 0xDB, 0xAB, 0xff},
	0x38: {0xFF, 0xE7, 0xA3, 0xff},
	0x39: {0xE3, 0xFF, 0xA3, 0xff},
	0x3A: {0xAB, 0xF3, 0xBF, 0xff},
	0x3B: {0xB3, 0xFF, 0xCF, 0xff},
	0x3C: {0x9F, 0xFF, 0xF3, 0xff},
	0x3D: {0x00, 0x00, 0x00, 0xff},
	0x3E: {0x00, 0x00, 0x00, 0xff},
	0x3F: {0x00, 0x00, 0x00, 0xff},
}

Palette contains 64 colors, referenced in Hex for rendering. This is the default palette, I think you can have others? No tests for this file, as go coverage requires functions for test coverage.

View Source
var Purple = "\033[35m"
View Source
var Red = "\033[31m"
View Source
var Reset = "\033[0m"

Testing colors

View Source
var White = "\033[97m"
View Source
var Yellow = "\033[33m"

Functions

func Adc

func Adc(self *Cpu)

Adc Add Memory to Accumulator with Carry Must set Carry and Overflow Flag

func And

func And(self *Cpu)

And is AND Memory with Accumulator A AND M -> A

func Asl

func Asl(self *Cpu)

Asl Shift Left One Bit (Memory or Accumulator) C <- [76543210] <- 0

func Bcc

func Bcc(self *Cpu)

func Bcs

func Bcs(self *Cpu)

func Beq

func Beq(self *Cpu)

BEQ Branch on Result Zero

func Bit

func Bit(self *Cpu)

TODO OH GOD WHAT IS THIS!

func Bmi

func Bmi(self *Cpu)

BMI Branch on Result Minus

func Bne

func Bne(self *Cpu)

BNE Branch on Result not Zero

func BooleanArrayFromByte

func BooleanArrayFromByte(b byte) [8]bool

BooleanArrayFromByte Returns an array of booleans from a byte to do easier creation of sprites

func Bpl

func Bpl(self *Cpu)

BPL Branch on Result Plus

func Brk

func Brk(self *Cpu)

// BRK - Force Interupt Chat GPT's description The BRK (Break) instruction on the NES (Nintendo Entertainment System) CPU is used to signal an interrupt and halt the execution of the current program. The BRK instruction consists of a single byte opcode (0x00) and is typically used for software debugging purposes. When the CPU encounters the BRK instruction, it performs the following steps: Push the address of the next instruction on the stack. Set the interrupt flag (I flag) in the status register to prevent any further interrupts. Push the status register on the stack. Load the interrupt vector at address 0xFFFE and jump to that address. The interrupt vector at 0xFFFE is a 16-bit address that points to the location of the interrupt service routine (ISR) for the BRK instruction. The ISR typically contains code that handles the software debugging operations and then returns control to the main program. Once the ISR is finished executing, it will load the program counter with the address that was pushed onto the stack during step 1, then restore the status register from the stack during step 3. The CPU will then continue executing instructions from the program counter that was loaded, as if the BRK instruction never happened. In summary, the NES BRK instruction is used to signal an interrupt and halt the execution of the current program, allowing the CPU to perform a software debugging operation before returning to the main program. push PC+2, push SR

func Bvc

func Bvc(self *Cpu)

BVC Branch on Overflow Clear

func Bvs

func Bvs(self *Cpu)

BVS Branch on Overflow Set

func Clc

func Clc(self *Cpu)

func Cld

func Cld(self *Cpu)

Clear Decimal Flag

func Cli

func Cli(self *Cpu)

func Clv

func Clv(self *Cpu)

func Cmp

func Cmp(self *Cpu)

func Cpx

func Cpx(self *Cpu)

func Cpy

func Cpy(self *Cpu)

func Dec

func Dec(self *Cpu)

DEC Decrement Memory by One

func Dex

func Dex(self *Cpu)

DEX Decrement Index X by One

func Dey

func Dey(self *Cpu)

DEY Decrement Index Y by One

func Eor

func Eor(self *Cpu)

EOR , Excluse OR A EOR M -> A

func Inc

func Inc(self *Cpu)

INC Increment Memory by One

func Inx

func Inx(self *Cpu)

INX Increment X Reg by One

func Iny

func Iny(self *Cpu)

INY Increment Y Reg by One

func Jmp

func Jmp(self *Cpu)

JMP Jump to new location

func Jsr

func Jsr(self *Cpu)

JSR Jump and Store Current Position“

func Lda

func Lda(self *Cpu)

Load memory (M) from Address (self.address) into Accumulator

func Ldx

func Ldx(self *Cpu)

func Ldy

func Ldy(self *Cpu)

func Lsr

func Lsr(self *Cpu)

func Nop

func Nop(self *Cpu)

func Ora

func Ora(self *Cpu)

ORA, Or memory with accumulator A OR M -> A

func Pause

func Pause()

Pause is a helper function that lets me manually step through op code execution one at a time

func Pha

func Pha(self *Cpu)

PHA Push Accumulator on Stack

func Php

func Php(self *Cpu)

PHP - Push Processor Status on Stack

func Pla

func Pla(self *Cpu)

func Plp

func Plp(self *Cpu)

func Rol

func Rol(self *Cpu)

func Ror

func Ror(self *Cpu)

func Rti

func Rti(self *Cpu)

RTI - Return from Interupt pull SR, pull PC

func Rts

func Rts(self *Cpu)

RTS Return from Subroutine pull PC, PC+1 -> PC

func Sbc

func Sbc(self *Cpu)

func Sec

func Sec(self *Cpu)

Set Status Flag of C - Carry Flag to on (00 10 00 00)

func Sed

func Sed(self *Cpu)

Set Status Flag of D - Decimal Flag to on (00 00 01 00) | NOT USED IN NES

func Sei

func Sei(self *Cpu)

Set Status Flag of I - Interupt Disable to on (00 00 01 00)

func Sta

func Sta(self *Cpu)

STA Store Accumulator in Memory

func Stx

func Stx(self *Cpu)

func Sty

func Sty(self *Cpu)

func Tax

func Tax(self *Cpu)

TAX Transfer Accumulator into index X

func Tay

func Tay(self *Cpu)

TAY Transfer Accumulator into index Y

func Tsx

func Tsx(self *Cpu)

TSX Transfer Stack Pointer into Index X

func Txa

func Txa(self *Cpu)

TXA Transfer Index X to Accumulator

func Txs

func Txs(self *Cpu)

TXS Transfer Index X to Stack Register

func Tya

func Tya(self *Cpu)

TYA Transfer Index Y to Accumulator

Types

type Cpu

type Cpu struct {
	PC uint16 //Programing Counter, which instruction to read next
	SP byte   //Stack pointer,

	//Registers
	A byte //Accumlator, copying to and from memory + maths fuctions
	X byte //X Register
	Y byte //Y Register
	S byte //Status, 8 flags, for more info see statusflags.go file

	//64 kb of memory, adressing space of 0x0000 to 0xFFFF
	Memory [0xFFFF + 1]byte

	RomReader Rom

	CycleCount uint64
	FrameCount uint64

	Quiet bool //
	// contains filtered or unexported fields
}

func (*Cpu) CheckNZ

func (self *Cpu) CheckNZ(value byte)

Check for negative & zero, common on sets + calculations

func (*Cpu) DecodeInstruction

func (self *Cpu) DecodeInstruction()

func (*Cpu) EmulateCycle

func (self *Cpu) EmulateCycle()

func (*Cpu) Init

func (self *Cpu) Init()

func (*Cpu) Pull

func (self *Cpu) Pull() byte

func (*Cpu) Pull16Bit

func (self *Cpu) Pull16Bit() uint16

func (*Cpu) Push

func (self *Cpu) Push(value byte)

func (*Cpu) Push16Bit

func (self *Cpu) Push16Bit(value uint16)

Push16Bit Push a unsigned 16 bit integer into the stack.

func (*Cpu) ReadAddress

func (self *Cpu) ReadAddress(start uint16) uint16

func (*Cpu) ReadAddressByte

func (self *Cpu) ReadAddressByte(start uint16) uint8

func (*Cpu) ReadWrappedAddress

func (self *Cpu) ReadWrappedAddress(a uint16) uint16

func (*Cpu) SetFlag

func (self *Cpu) SetFlag(flag int, tovalue bool)

SetFlag was Made possible by http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c

func (*Cpu) WriteMemory

func (self *Cpu) WriteMemory(address uint16, value byte)

type Nes

type Nes struct {
	Cpu Cpu
	Rom Rom
	Ppu Ppu
}

Nes is the top level, representing the physical system, it contains all NES Compontents, trying to keep as close to the original system at possible

func (*Nes) Init

func (nes *Nes) Init()

Init Starts NES system. This controls the main loop and emulation of CPU Cycles

type OpCodeInfo

type OpCodeInfo struct {
	Mode          int
	Operation     int
	No_Bytes      byte
	No_Cycles     byte
	BoundaryCross byte
	Function      fn
}

func (*OpCodeInfo) ModeString

func (o *OpCodeInfo) ModeString() string

func (*OpCodeInfo) OperationString

func (o *OpCodeInfo) OperationString() string

func (*OpCodeInfo) RunOperation

func (o *OpCodeInfo) RunOperation(cpu *Cpu)

type Ppu

type Ppu struct {
	Cycle    int // 0-340
	ScanLine int // 0-261, 0-239=are visible frames, 240=post, 241-260=vblank, 261=pre
	Frame    uint64
	Memory   [0x3FFF + 1]byte // 16kb address space.
}

func (*Ppu) EmulateCycle

func (p *Ppu) EmulateCycle()

EmulateCycle is called 3 times for ever 1 CPU Cycle. There are 262 Scanlines per frame. Each scanline is 341 PPU Cycles

func (*Ppu) GetColorFromPalette

func (p *Ppu) GetColorFromPalette(c int) color.RGBA

GetColorFromPalette is used to grab an color.RGBA value from it's hex index of 64 colors. This is mostly used for some basic tests of the default color Palette.

func (*Ppu) GetInfoForPatternTable

func (p *Ppu) GetInfoForPatternTable()

type Rom

type Rom struct {
}

func (*Rom) LoadGame

func (self *Rom) LoadGame(filename string, nes *Nes)

Where the cartridge is read in Read file in curent dir into Memory of NES CPU

type RomHeader

type RomHeader struct {
	PGR_banks byte
	CHR_banks byte
	Control   byte
	Control2  byte
	Ram_banks byte
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL