feat: rom loading, and character map
parent
f5fe022f82
commit
ba28865248
|
|
@ -0,0 +1,77 @@
|
||||||
|
package emulator
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
type CHIP8 struct {
|
||||||
|
memory [4096]byte
|
||||||
|
v [16]byte
|
||||||
|
i uint16
|
||||||
|
pc uint16
|
||||||
|
sp uint16
|
||||||
|
delayTimer uint8
|
||||||
|
soundTimer uint8
|
||||||
|
stack [16]uint16
|
||||||
|
keypad [16]bool
|
||||||
|
// To transform the linear Memory to a 2D array for easier drawing,
|
||||||
|
// having a (x, y) coordinate then (x, y) = graphics[x + (y * 64)]
|
||||||
|
graphics [64 * 32]bool
|
||||||
|
opcode uint16
|
||||||
|
fonts [80]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewChip8() *CHIP8 {
|
||||||
|
chip8 := &CHIP8{
|
||||||
|
memory: [4096]byte{},
|
||||||
|
v: [16]byte{},
|
||||||
|
i: 0,
|
||||||
|
// The original 0x200 code of the CHIP-8 interpreter was from 0x0 to 0x200,
|
||||||
|
pc: 0x200,
|
||||||
|
sp: 0,
|
||||||
|
delayTimer: 0,
|
||||||
|
soundTimer: 0,
|
||||||
|
stack: [16]uint16{},
|
||||||
|
keypad: [16]bool{},
|
||||||
|
graphics: [64 * 32]bool{},
|
||||||
|
opcode: 0,
|
||||||
|
fonts: [80]byte{},
|
||||||
|
}
|
||||||
|
chip8.Initialize()
|
||||||
|
return chip8
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CHIP8) Initialize() {
|
||||||
|
charMap := [80]byte{
|
||||||
|
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
|
||||||
|
0x20, 0x60, 0x20, 0x20, 0x70, // 1
|
||||||
|
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
|
||||||
|
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
|
||||||
|
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
|
||||||
|
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
|
||||||
|
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
|
||||||
|
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
|
||||||
|
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
|
||||||
|
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
|
||||||
|
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
|
||||||
|
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
|
||||||
|
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
|
||||||
|
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
|
||||||
|
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
|
||||||
|
0xF0, 0x80, 0xF0, 0x80, 0x80, // F
|
||||||
|
}
|
||||||
|
|
||||||
|
// Char map goes from 0x050 to 0x9F
|
||||||
|
for i, charByte := range charMap {
|
||||||
|
c.memory[i+0x50] = charByte
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CHIP8) LoadROMIntoMemory(dat []byte) error {
|
||||||
|
if len(dat) > len(c.memory)-0x200 {
|
||||||
|
return errors.New("ROM is too large to fit into memory")
|
||||||
|
}
|
||||||
|
// Roms start from 0x200
|
||||||
|
for i, datByte := range dat {
|
||||||
|
c.memory[i+0x200] = datByte
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package emulator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadRomIntoMemory(t *testing.T) {
|
||||||
|
chip8 := NewChip8()
|
||||||
|
romBytes := []byte{0xDE, 0xAD, 0xBE, 0xEF}
|
||||||
|
|
||||||
|
_ = chip8.LoadROMIntoMemory(romBytes)
|
||||||
|
// We always expect roms to be loaded starting from 0x200
|
||||||
|
ans := chip8.memory[0x200:0x204]
|
||||||
|
|
||||||
|
for i, ansByte := range ans {
|
||||||
|
if romBytes[i] != ansByte {
|
||||||
|
t.Errorf("Expected %x, got %x", romBytes[i], ansByte)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
40
main.go
40
main.go
|
|
@ -4,11 +4,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||||
|
"github.com/micosilent/go_chip8/emulator"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
counter int
|
counter int
|
||||||
|
lastFrameTime time.Time
|
||||||
|
debugText string
|
||||||
|
emulator *emulator.CHIP8
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Update() error {
|
func (g *Game) Update() error {
|
||||||
|
|
@ -17,24 +23,46 @@ func (g *Game) Update() error {
|
||||||
|
|
||||||
func newGame() *Game {
|
func newGame() *Game {
|
||||||
return &Game{
|
return &Game{
|
||||||
counter: 1,
|
counter: 1,
|
||||||
|
lastFrameTime: time.Now(),
|
||||||
|
debugText: "",
|
||||||
|
emulator: emulator.NewChip8(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Draw(screen *ebiten.Image) {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("Counter: %d", g.counter))
|
deltaTime := time.Since(g.lastFrameTime)
|
||||||
|
fps := int(1 / deltaTime.Seconds())
|
||||||
|
g.debugText = fmt.Sprintf("Counter: %d, fps: %d", g.counter, fps)
|
||||||
|
ebitenutil.DebugPrint(screen, g.debugText)
|
||||||
g.counter++
|
g.counter++
|
||||||
|
g.lastFrameTime = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
return 320, 240
|
return 320, 240
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Game) LoadROM(path string) {
|
||||||
|
dat, err := os.ReadFile(path)
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
err = g.emulator.LoadROMIntoMemory(dat)
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
game := newGame()
|
game := newGame()
|
||||||
|
game.LoadROM("roms/ibm_logo.ch8")
|
||||||
ebiten.SetWindowSize(480, 320)
|
ebiten.SetWindowSize(480, 320)
|
||||||
ebiten.SetWindowTitle("Go CHIP8 Emulator")
|
ebiten.SetWindowTitle("Go CHIP8 Emulator")
|
||||||
if err := ebiten.RunGame(game); err != nil {
|
|
||||||
log.Fatal(err)
|
err := ebiten.RunGame(game)
|
||||||
}
|
check(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue