Skip to content

lt/mos6502-fast

Repository files navigation

MOS6502

An interpreting emulator for the 6502 chip written in Go

A personal project into making a fast instruction-level emulator for the MOS 6502.

It's not really useful for any real-world applications and certainly can't be dropped in to any existing emulator.

The goal was to make a clean-room implementation from online documentation and to experiment with micro-optimisations just for fun. It involved a lot of fiddling, a bit of profiling, and even some learning!

Features:

  • Passes Klaus Doorman's 6502 functional tests - This seems a common benchmark.
  • Zero-alloc - Entirely by accident.
  • Most functions can be inlined - ADC and SBC fail the complexity checks.
  • Bounds checks elided on memory - No boilerplate to panic because we can't access invalid indexes.
  • Bit twiddling in favour of boolean states - Profiled where branch prediction was faster though!
  • Avoids dynamic dispatch - Deliberately not using an interface for memory reading or function tables for instructions
  • Cycle accounting and speed limiting - Optional, use the cycles tag. Not sure why I added it really.

Without cycle accounting:

$ go test -bench=. -run BenchFunctional -benchtime 10s
goos: linux
goarch: amd64
pkg: mos6502
cpu: AMD Ryzen 7 3700X 8-Core Processor             
BenchmarkHandler-16     1000000000               4.179 ns/op
PASS
ok      mos6502 4.619

Codegen

The step function is generated from metadata. To regenerate it run:

$  go run ./cpu_step_gen.go

Cycle accounting:

Build, test, or bench with -tags cycles

$  go test -bench=. -run BenchFunctional -benchtime 10s -tags cycles
goos: linux
goarch: amd64
pkg: mos6502
cpu: AMD Ryzen 7 3700X 8-Core Processor             
BenchmarkHandler-16     1000000000               4.355 ns/op
PASS
ok      mos6502 4.804s

Sauces:

Licenses:

  • This project is licensed under Apache 2.0. See LICENSE.
  • Test data in the testdata directory is licensed under GPL v3. See LICENSE-testdata.

About

An interpreting emulator for the 6502 chip written in Go.

Topics

Resources

License

Apache-2.0, GPL-3.0 licenses found

Licenses found

Apache-2.0
LICENSE
GPL-3.0
LICENSE-testdata

Stars

Watchers

Forks