Categorygithub.com/jrsteele09/go-6502-emulator
repository
0.0.0-20241004132933-c42c46a170df
Repository: https://github.com/jrsteele09/go-6502-emulator.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

go-6502-emulator

Go Report Card GoDoc

go-6502-emulator is a Go library that emulates the 6502 processor, providing memory management, cycle exact CPU operations, and a disassembler for interpreting machine code.

Table of Contents

Installation

To install the package, run:

go get -u github.com/jrsteele09/go-6502-emulator

Usage

Example

package main

import (
    "fmt"
    "go-6502-emulator/cpu"
    "go-6502-emulator/memory"
)

func main() {
    // Initialize memory with a size of 64KB
    mem := memory.NewMemory[uint16](64 * 1024)
    
    // Initialize the CPU with the memory
    cpu := cpu.NewCpu(mem)
    
    // Write some instructions to memory (example instructions)
    startAddress := uint16(0x0200)
    mem.Write(startAddress, 0xA9, 0x05, 0x00) // LDA #$05; BRK

    // Set the program counter to the start address
    cpu.Registers().PC = startAddress
    
    // Execute instructions until completion
    var complete cpu.Completed
    for !complete {
        c, err := cpu.Execute()
        if err != nil {
            fmt.Println("Error executing instruction:", err)
            break
        }
        complete = c
    }

}

Disassembler

The library includes a disassembler to interpret 6502 machine code into human-readable assembly instructions.


	m := memory.NewMemory[uint16](64 * 1024)
	p := cpu.NewCpu(m)
	m.Write(0xC000,
		0x02,
		0xA9, 0x01,
		0xA9, 0x80,
		0xA9, 0x00,
		0xA5, 0x80,
		0xB5, 0x80,
		0xAD, 0x80, 0x00,
		0xBD, 0x80, 0x00,
		0xBD, 0x01, 0x00,
		0xB9, 0x80, 0x00,
		0xB9, 0x01, 0x00,
		0xA1, 0x05,
		0xB1, 0x05,
	) //0xc01d

	dissassembler := NewDisassembler(m, cpu.OpCodes(p))
	dissassembledCode := ""

	address := uint16(0xC000)
	for address < uint16(0xC01D) {
		line, bytes := dissassembler.Disassemble(address)
		dissassembledCode += fmt.Sprintf("%s\n",line)
		address += uint16(bytes)
	}

Contributing

We appreciate any contributions to improve go-6502-emulator. Please feel free to file issues or submit pull requests.

License

MIT License. See LICENSE for more information.