Skip to content

Commit

Permalink
refactor: change folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoliveira21 committed Dec 23, 2023
1 parent e36de72 commit 1ddb694
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A CHIP-8 Emulator project to put GoLang in practice and learn how an emulator works.

![test_opcode](./assets/test_opcode.png)
![test_opcode](./cli/assets/test_opcode.png)

[corax89’s chip8-test-rom](https://github.com/corax89/chip8-test-rom)

Expand Down
Binary file removed assets/beep.mp3
Binary file not shown.
Binary file removed assets/test_opcode.png
Binary file not shown.
13 changes: 9 additions & 4 deletions core/audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"io"
"os"
"path"
"runtime"

"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/mp3"
Expand All @@ -13,8 +15,11 @@ const sampleRate = 48000

type AudioPlayer = *audio.Player

func readFromFS(mp3FilePath string) (io.Reader, error) {
r, err := os.Open(mp3FilePath)
func readFromFS() (io.Reader, error) {
_, b, _, _ := runtime.Caller(0)
d := path.Join(path.Dir(b), "..", "..", "cli", "assets", "beep.mp3")

r, err := os.Open(d)

if err != nil {
return nil, err
Expand All @@ -23,7 +28,7 @@ func readFromFS(mp3FilePath string) (io.Reader, error) {
return r, nil
}

func NewAudioPlayer(mp3FilePath string) (AudioPlayer, error) {
func NewAudioPlayer() (AudioPlayer, error) {
execMode := os.Getenv("EXEC_MODE")

var r io.Reader
Expand All @@ -33,7 +38,7 @@ func NewAudioPlayer(mp3FilePath string) (AudioPlayer, error) {
return nil, errors.New("audio unsuported on web")
}

r, err = readFromFS(mp3FilePath)
r, err = readFromFS()

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion core/audio/audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Test(t *testing.T) {
p, err := audio.NewAudioPlayer("../../assets/beep.mp3")
p, err := audio.NewAudioPlayer()

if p == nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion core/chip8.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RunChip8(rom []byte, title string) {
sqr := ebiten.NewImage(10, 10)
sqr.Fill(color.RGBA{51, 209, 122, 1})

p, err := audio.NewAudioPlayer("assets/beep.mp3")
p, err := audio.NewAudioPlayer()

if err != nil {
log.Print(err)
Expand Down
22 changes: 0 additions & 22 deletions main.go

This file was deleted.

Binary file removed roms/IBM.ch8
Binary file not shown.
Binary file removed roms/test/delay_timer_test.ch8
Binary file not shown.
Binary file removed roms/test/random_number_test.ch8
Binary file not shown.
Binary file removed roms/test/test_opcode.ch8
Binary file not shown.
File renamed without changes.
Binary file modified web/public/chip8.wasm
Binary file not shown.
File renamed without changes.
10 changes: 7 additions & 3 deletions web/web.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

import (
"log"
"os"

"github.com/gaoliveira21/chip8/core"
"github.com/gaoliveira21/chip8/web/http"
)

func main() {
os.Setenv("EXEC_MODE", "web")

pong := []byte{
0x6A, 0x2, 0x6B, 0xC, 0x6C, 0x3F, 0x6D, 0xC, 0xA2, 0xEA, 0xDA, 0xB6, 0xDC, 0xD6, 0x6E, 0x0, 0x22, 0xD4, 0x66, 0x3, 0x68, 0x2, 0x60, 0x60, 0xF0, 0x15, 0xF0, 0x7, 0x30, 0x0, 0x12, 0x1A, 0xC7, 0x17, 0x77, 0x8, 0x69, 0xFF, 0xA2, 0xF0, 0xD6, 0x71, 0xA2, 0xEA, 0xDA, 0xB6, 0xDC, 0xD6, 0x60, 0x1, 0xE0, 0xA1, 0x7B, 0xFE, 0x60, 0x4, 0xE0, 0xA1, 0x7B, 0x2, 0x60, 0x1F, 0x8B, 0x2, 0xDA, 0xB6, 0x8D, 0x70, 0xC0, 0xA, 0x7D, 0xFE, 0x40, 0x0, 0x7D, 0x2, 0x60, 0x0, 0x60, 0x1F, 0x8D, 0x2, 0xDC, 0xD6, 0xA2, 0xF0, 0xD6, 0x71, 0x86, 0x84, 0x87, 0x94, 0x60, 0x3F, 0x86, 0x2, 0x61, 0x1F, 0x87, 0x12, 0x46, 0x2, 0x12, 0x78, 0x46, 0x3F, 0x12, 0x82, 0x47, 0x1F, 0x69, 0xFF, 0x47, 0x0, 0x69, 0x1, 0xD6, 0x71, 0x12, 0x2A, 0x68, 0x2, 0x63, 0x1, 0x80, 0x70, 0x80, 0xB5, 0x12, 0x8A, 0x68, 0xFE, 0x63, 0xA, 0x80, 0x70, 0x80, 0xD5, 0x3F, 0x1, 0x12, 0xA2, 0x61, 0x2, 0x80, 0x15, 0x3F, 0x1, 0x12, 0xBA, 0x80, 0x15, 0x3F, 0x1, 0x12, 0xC8, 0x80, 0x15, 0x3F, 0x1, 0x12, 0xC2, 0x60, 0x20, 0xF0, 0x18, 0x22, 0xD4, 0x8E, 0x34, 0x22, 0xD4, 0x66, 0x3E, 0x33, 0x1, 0x66, 0x3, 0x68, 0xFE, 0x33, 0x1, 0x68, 0x2, 0x12, 0x16, 0x79, 0xFF, 0x49, 0xFE, 0x69, 0xFF, 0x12, 0xC8, 0x79, 0x1, 0x49, 0x2, 0x69, 0x1, 0x60, 0x4, 0xF0, 0x18, 0x76, 0x1, 0x46, 0x40, 0x76, 0xFE, 0x12, 0x6C, 0xA2, 0xF2, 0xFE, 0x33, 0xF2, 0x65, 0xF1, 0x29, 0x64, 0x14, 0x65, 0x0, 0xD4, 0x55, 0x74, 0x15, 0xF2, 0x29, 0xD4, 0x55, 0x0, 0xEE, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0,
rom, err := http.ReadFile("roms/PONG.ch8")

if err != nil {
log.Fatal(err)
}

core.RunChip8(pong, "[CHIP-8] - PONG")
core.RunChip8(rom, "[CHIP-8] - PONG")
}

0 comments on commit 1ddb694

Please sign in to comment.