Giter Club home page Giter Club logo

swift6502's Introduction

Swift6502 – a simple 6502 emulator and disassembler

This repo contains a simple, but working, software implementation of a generic system based on the 6502 processor containing a CPU, bus and RAM.

It is capable of running programs written for this architecture. No GUI or CLI capabilities have been added yet.

A working disassembler also exists.

Usage

Emulator

The emulator has no output yet, so all that's currently possible is to run some code and make assertions in a test scenario. Clock frequency is not emulated, so unless code with an infinite loop is given it'll finish quite fast.

// Provide a program as a byte array.
let program: [UInt8] = [0x20, 0x09, 0x00, 0x20, 0x0C, 0x00, 0x20, 0x12, 0x00, 0xA2, 0x00, 0x60, 0xE8, 0xE0, 0x05, 0xD0, 0xFB, 0x60, 0x00]

// Create RAM (of length 0xFFFF + 1), with the program placed at the beginning.
let ram = Array.createRam(withProgram: program)

// Create the CPU.
let cpu = CPU.create(ram: ram)

// Run the program.
cpu.run()

Disassembler

The disassembler is capable of disassembling a program back into human readable code.

// Provide a program as a byte array.
let program: [UInt8] = [0x20, 0x09, 0x00, 0x20, 0x0C, 0x00, 0x20, 0x12, 0x00, 0xA2, 0x00, 0x60, 0xE8, 0xE0, 0x05, 0xD0, 0xFB, 0x60, 0x00]

// Create the disassembler with the given program.
let disassembler = Disassembler(program: program)

// Disassemble the program. The return is a `[String]` containing human readable code.
let code = disassembler.disassemble()

Motivation

I've had a lot of fun watching videos on YouTube of people either writing or disassembling games for the NES, which uses the 6502, and I wanted to see if I could write a software emulator that could run code assembled for that processor.

The main goal of this repo is to just have fun and learn something new I don't work with on a daily basis.

Ideas / todo

  • Make Bus own CPU – not the other way around.
  • Make types public, if they need to.
  • Don't break program flow when encountering opcode 0x00, aka BRK.
  • Add support for decimal mode.
  • Implement the interrupt vector.
  • Programs don't start at address 0x0000. Fix this somehow.
  • Emulate clock frequency.
  • Improve disassembler output.
  • Add labels for disassembly output for i.e. JSR.
  • Create a CLI tool for the disassembler.
  • Create a visual tool/app that can/has:
    • Load binaries from file.
    • Step debugging support.
    • Visualize registers and status flags.
    • Live disassemble while stepping through.

References

What Description
Masswerk Probably the most useful resource I've used. Contains description of all instructions and addressing modes, as well as descriptions on when/how status flags are set.
Instruction set Have answered some questions I couldn't find on Masswerk's site.
Overflow explanation Setting the overflow flag properly was probably the thing I struggled the most with. This site gave some highly appreciated insights.
Online assembler This page has a nice step debugger and visualization of registers and flags.
Online disassembler Used to compare the output of my disassembler against a result from someone who actually knows what they're doing.

swift6502's People

Contributors

bstien avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.