Code Monkey home page Code Monkey logo

ghip8's Introduction

About

Ghip8 is a golang learning project that implements a Chip8 interpreter/disassembler useful for developing a complete emulator.
Chip8 can be considered one of the first game engine 2D developed by Joseph Weisbecker for the COSMAC VIP.
More information on

https://en.wikipedia.org/wiki/CHIP-8
http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#1.0

Example

This is a full working example of emulator that runs on terminal thanks to the superb termbox-go

package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"time"

	"github.com/ilmich/ghip8"
	"github.com/nsf/termbox-go"
)

// Function tbprint draws a string.
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
	for _, c := range msg {
		termbox.SetCell(x, y, c, fg, bg)
		x++
	}
}

var speedRender = map[uint8]string{
	0x00: "    ", 0x01: "   █", 0x02: "  █ ", 0x03: "  ██",
	0x04: " █  ", 0x05: " █ █", 0x06: " ██ ", 0x07: " ███",
	0x08: "█   ", 0x09: "█  █", 0x0A: "█ █ ", 0x0B: "█ ██",
	0x0C: "██  ", 0x0D: "██ █", 0x0E: "███ ", 0x0F: "████",
}

func main() {

	var filename = flag.String("f", "", "Chip8 source file")

	flag.Parse()

	chip8 := &ghip8.Chip8{}

	fmt.Printf("Loading file %s\n", filename)

	if buffer, err := ioutil.ReadFile(*filename); err == nil {
		chip8.Init()
		chip8.Load(buffer)

		err := termbox.Init()
		if err != nil {
			panic(err)
		}
		defer termbox.Close()

		ev := make(chan termbox.Event)

		go func() {
			for {
				ev <- termbox.PollEvent()
			}
		}()

	mainloop:
		for {
			select {
			case event := <-ev:
				if event.Type == termbox.EventKey {
					if event.Key == termbox.KeyEsc {
						break mainloop
					}
					chip8.KeyPressed(event.Ch)
				}
			default:
				_, cmd := chip8.Run()
				x, y := 0, 0
				for idx, cell := range chip8.VideoMemory {
					sprite := speedRender[cell>>4] + speedRender[cell&0x0F]
					tbprint(x, y, termbox.ColorDefault, termbox.ColorDefault, sprite)
					if (idx+1) %8 == 0 { // go next line
						y++
						x = 0
					} else {
						x += 8
					}
					tbprint(0, 40, termbox.ColorDefault, termbox.ColorDefault, cmd+"          ")
				}
				termbox.Flush()

				time.Sleep(1100 * time.Microsecond)
			}
		}
	} else {
		fmt.Printf("Error loading file %s", err)
	}

}

Chip8 Sample Screenshot Chip8 Sample Screenshot

Documentation

This library is very simple, and in my opinion the above example is self-explanatory, but there is also a godoc

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.