Skip to content

Commit

Permalink
refactor: create new packages for input, font and cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoliveira21 committed Dec 28, 2023
1 parent ca15c83 commit af12642
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 67 deletions.
22 changes: 12 additions & 10 deletions core/chip8.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
"log"

"github.com/gaoliveira21/chip8/core/audio"
"github.com/gaoliveira21/chip8/core/cpu"
"github.com/gaoliveira21/chip8/core/input"
"github.com/hajimehoshi/ebiten/v2"
)

type Chip8 struct {
cpu *CPU
cpu *cpu.CPU
square *ebiten.Image
audioPlayer audio.AudioPlayer
screenWidth int
screenHeight int
}

func (c8 *Chip8) Update() error {
for i := 0; i < int(FREQUENCY/60); i++ {
for key, value := range Keypad {
for i := 0; i < int(cpu.FREQUENCY/60); i++ {
for key, value := range input.Keypad {
if ebiten.IsKeyPressed(key) {
c8.cpu.Keys[value] = 0x01
} else {
Expand All @@ -40,8 +42,8 @@ func (c8 *Chip8) Update() error {
func (c8 *Chip8) Draw(screen *ebiten.Image) {
screen.Fill(color.NRGBA{23, 20, 33, 1})

for h := 0; h < HEIGHT; h++ {
for w := 0; w < WIDTH; w++ {
for h := 0; h < cpu.HEIGHT; h++ {
for w := 0; w < cpu.WIDTH; w++ {
if c8.cpu.Display[h][w] == 0x01 {
imgOpts := &ebiten.DrawImageOptions{}
imgOpts.GeoM.Translate(float64(w*10), float64(h*10))
Expand All @@ -56,8 +58,8 @@ func (c8 *Chip8) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHei
}

func RunChip8(rom []byte, title string) {
cpu := NewCpu()
cpu.LoadROM(rom)
c := cpu.NewCpu()
c.LoadROM(rom)

sqr := ebiten.NewImage(10, 10)
sqr.Fill(color.RGBA{51, 209, 122, 1})
Expand All @@ -70,10 +72,10 @@ func RunChip8(rom []byte, title string) {

c8 := &Chip8{
square: sqr,
cpu: &cpu,
cpu: &c,
audioPlayer: p,
screenWidth: WIDTH * 10,
screenHeight: HEIGHT * 10,
screenWidth: cpu.WIDTH * 10,
screenHeight: cpu.HEIGHT * 10,
}

ebiten.SetWindowSize(c8.screenWidth, c8.screenHeight)
Expand Down
43 changes: 22 additions & 21 deletions core/cpu.go → core/cpu/cpu.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package core
package cpu

import (
"math/rand"
"time"

"github.com/gaoliveira21/chip8/core/font"
"github.com/gaoliveira21/chip8/core/memory"
)

Expand Down Expand Up @@ -44,12 +45,6 @@ type opcode struct {
nnn uint16
}

func (cpu *CPU) loadFont() {
for i := 0x050; i <= 0x09F; i++ {
cpu.mmu.Write(uint16(i), FontData[i-0x050])
}
}

func NewCpu() CPU {
cpu := CPU{
pc: 0x200,
Expand All @@ -66,19 +61,8 @@ func (cpu *CPU) LoadROM(rom []byte) {
}
}

func (cpu *CPU) Decode(data uint16) (oc *opcode) {
return &opcode{
instruction: data & INSTRUCTION_BITMASK,
registerX: uint8((data & X_BITMASK) >> 8),
registerY: uint8((data & Y_BITMASK) >> 4),
n: uint8(data & N_BITMASK),
nn: uint8(data & NN_BITMASK),
nnn: (data & NNN_BITMASK),
}
}

func (cpu *CPU) Run() {
cpu.Clock()
cpu.clock()

if cpu.delayTimer > 0 {
cpu.delayTimer--
Expand All @@ -89,11 +73,28 @@ func (cpu *CPU) Run() {
}
}

func (cpu *CPU) Clock() {
func (cpu *CPU) loadFont() {
for i := 0x050; i <= 0x09F; i++ {
cpu.mmu.Write(uint16(i), font.FontData[i-0x050])
}
}

func (cpu *CPU) decode(data uint16) (oc *opcode) {
return &opcode{
instruction: data & INSTRUCTION_BITMASK,
registerX: uint8((data & X_BITMASK) >> 8),
registerY: uint8((data & Y_BITMASK) >> 4),
n: uint8(data & N_BITMASK),
nn: uint8(data & NN_BITMASK),
nnn: (data & NNN_BITMASK),
}
}

func (cpu *CPU) clock() {
data := cpu.mmu.Fetch(cpu.pc)
cpu.pc += 2

opcode := cpu.Decode(data)
opcode := cpu.decode(data)

switch opcode.instruction {
case 0x0000:
Expand Down
Loading

0 comments on commit af12642

Please sign in to comment.