Skip to content

Commit

Permalink
refactor(graphics): add graphics package
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoliveira21 committed Dec 28, 2023
1 parent cb65f6f commit aa55905
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
10 changes: 5 additions & 5 deletions core/chip8.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (c8 *Chip8) Update() error {
func (c8 *Chip8) Draw(screen *ebiten.Image) {
screen.Fill(color.NRGBA{23, 20, 33, 1})

for h := 0; h < cpu.HEIGHT; h++ {
for w := 0; w < cpu.WIDTH; w++ {
if c8.cpu.Display[h][w] == 0x01 {
for h := 0; h < c8.cpu.Graphics.Height; h++ {
for w := 0; w < c8.cpu.Graphics.Width; w++ {
if c8.cpu.Graphics.GetPixel(h, w) == 0x01 {
imgOpts := &ebiten.DrawImageOptions{}
imgOpts.GeoM.Translate(float64(w*10), float64(h*10))
screen.DrawImage(c8.square, imgOpts)
Expand Down Expand Up @@ -74,8 +74,8 @@ func RunChip8(rom []byte, title string) {
square: sqr,
cpu: &c,
audioPlayer: p,
screenWidth: cpu.WIDTH * 10,
screenHeight: cpu.HEIGHT * 10,
screenWidth: c.Graphics.Width * 10,
screenHeight: c.Graphics.Height * 10,
}

ebiten.SetWindowSize(c8.screenWidth, c8.screenHeight)
Expand Down
27 changes: 10 additions & 17 deletions core/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

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

Expand All @@ -19,26 +20,21 @@ const (
NNN_BITMASK = 0x0FFF
)

// Display
const (
WIDTH = 0x40
HEIGHT = 0x20
)

type CPU struct {
pc uint16 // Program Counter
i uint16 // I Register
v [16]byte // Variable registers
mmu memory.MMU
Display [HEIGHT][WIDTH]byte
Graphics *graphics.Graphics
delayTimer uint8
SoundTimer uint8
Keys [16]uint8
}

func NewCpu() CPU {
cpu := CPU{
pc: 0x200,
pc: 0x200,
Graphics: graphics.NewGraphics(),
}

cpu.loadFont()
Expand Down Expand Up @@ -169,11 +165,7 @@ func (cpu *CPU) clock() {
}

func (cpu *CPU) cls() {
for i := 0; i < HEIGHT; i++ {
for j := 0; j < WIDTH; j++ {
cpu.Display[i][j] = 0x00
}
}
cpu.Graphics.Clear()
}

func (cpu *CPU) ret() {
Expand Down Expand Up @@ -316,23 +308,24 @@ func (cpu *CPU) drw(oc *opcode) {

for j := 0; j < 8; j++ {
bit := (pixels >> (7 - j)) & 0x01
pixelOnDisplay := cpu.Graphics.GetPixel(int(y), int(xIndex))

if bit == 0x01 && cpu.Display[y][xIndex] == 0x01 {
if bit == 0x01 && pixelOnDisplay == 0x01 {
cpu.v[0xF] = 0x01
}

cpu.Display[y][xIndex] ^= bit
cpu.Graphics.SetPixel(int(y), int(xIndex), pixelOnDisplay^bit)

xIndex++

if xIndex >= WIDTH {
if int(xIndex) >= cpu.Graphics.Width {
break
}
}

y++

if y >= HEIGHT {
if int(y) >= cpu.Graphics.Height {
break
}
}
Expand Down
28 changes: 14 additions & 14 deletions core/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func TestCLS(t *testing.T) {
cpu.mmu.Write(0x200, 0x00)
cpu.mmu.Write(0x201, 0xE0)

cpu.Display[0][0] = 0xFF
cpu.Display[0][1] = 0xEF
cpu.Graphics.SetPixel(0, 0, 0xFF)
cpu.Graphics.SetPixel(0, 1, 0xEF)

cpu.clock()

for i := 0; i < HEIGHT; i++ {
for j := 0; j < WIDTH; j++ {
if cpu.Display[i][j] != 0x00 {
t.Errorf("cpu.Display[%d][%d] = 0x%X; expected 0x00", i, j, cpu.Display[i][j])
for i := 0; i < cpu.Graphics.Height; i++ {
for j := 0; j < cpu.Graphics.Width; j++ {
if cpu.Graphics.GetPixel(i, j) != 0x00 {
t.Errorf("cpu.Display[%d][%d] = 0x%X; expected 0x00", i, j, cpu.Graphics.GetPixel(i, j))
}
}
}
Expand Down Expand Up @@ -687,19 +687,19 @@ func TestDRWNoWrapAndNoCollision(t *testing.T) {
t.Errorf("cpu.v[0xF] = 0x%X; expected 0x00", cpu.v[0xF])
}

if cpu.Display[0][3] != 0x01 {
t.Errorf("cpu.Display[0][3] = 0x%X; expected 0x01", cpu.Display[0][3])
if cpu.Graphics.GetPixel(0, 3) != 0x01 {
t.Errorf("cpu.Graphics.GetPixel(0,3) = 0x%X; expected 0x01", cpu.Graphics.GetPixel(0, 3))
}

if cpu.Display[0][7] != 0x01 {
t.Errorf("cpu.Display[0][7] = 0x%X; expected 0x01", cpu.Display[0][7])
if cpu.Graphics.GetPixel(0, 7) != 0x01 {
t.Errorf("cpu.Graphics.GetPixel(0,7) = 0x%X; expected 0x01", cpu.Graphics.GetPixel(0, 7))
}

if cpu.Display[1][0] != 0x01 {
t.Errorf("cpu.Display[1][0] = 0x%X; expected 0x01", cpu.Display[1][0])
if cpu.Graphics.GetPixel(1, 0) != 0x01 {
t.Errorf("cpu.Graphics.GetPixel(1,0) = 0x%X; expected 0x01", cpu.Graphics.GetPixel(1, 0))
}

if cpu.Display[1][4] != 0x01 {
t.Errorf("cpu.Display[1][4] = 0x%X; expected 0x01", cpu.Display[1][4])
if cpu.Graphics.GetPixel(1, 4) != 0x01 {
t.Errorf("cpu.Graphics.GetPixel(1,4) = 0x%X; expected 0x01", cpu.Graphics.GetPixel(1, 4))
}
}
33 changes: 33 additions & 0 deletions core/graphics/graphics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package graphics

const width = 0x40
const height = 0x20

type Graphics struct {
display [height][width]byte
Width int
Height int
}

func NewGraphics() *Graphics {
return &Graphics{
Width: width,
Height: height,
}
}

func (g *Graphics) Clear() {
for i := 0; i < g.Height; i++ {
for j := 0; j < g.Width; j++ {
g.display[i][j] = 0x00
}
}
}

func (g *Graphics) GetPixel(y int, x int) byte {
return g.display[y][x]
}

func (g *Graphics) SetPixel(y int, x int, b byte) {
g.display[y][x] = b
}
17 changes: 17 additions & 0 deletions core/graphics/graphics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package graphics_test

import (
"testing"

"github.com/gaoliveira21/chip8/core/graphics"
)

func TestSetAndGetPixel(t *testing.T) {
g := graphics.NewGraphics()

g.SetPixel(0, 0, 0x1)

if g.GetPixel(0, 0) != 0x1 {
t.Errorf("graphics.Display[0][0] = 0x%X; expected 0x01", g.GetPixel(0, 0))
}
}

0 comments on commit aa55905

Please sign in to comment.