diff --git a/core/chip8.go b/core/chip8.go index ce128a2..b5f8409 100644 --- a/core/chip8.go +++ b/core/chip8.go @@ -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) @@ -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) diff --git a/core/cpu/cpu.go b/core/cpu/cpu.go index 3215c3a..25b236a 100644 --- a/core/cpu/cpu.go +++ b/core/cpu/cpu.go @@ -5,6 +5,7 @@ import ( "time" "github.com/gaoliveira21/chip8/core/font" + "github.com/gaoliveira21/chip8/core/graphics" "github.com/gaoliveira21/chip8/core/memory" ) @@ -19,18 +20,12 @@ 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 @@ -38,7 +33,8 @@ type CPU struct { func NewCpu() CPU { cpu := CPU{ - pc: 0x200, + pc: 0x200, + Graphics: graphics.NewGraphics(), } cpu.loadFont() @@ -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() { @@ -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 } } diff --git a/core/cpu/cpu_test.go b/core/cpu/cpu_test.go index b4496cb..23e27c2 100644 --- a/core/cpu/cpu_test.go +++ b/core/cpu/cpu_test.go @@ -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)) } } } @@ -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)) } } diff --git a/core/graphics/graphics.go b/core/graphics/graphics.go new file mode 100644 index 0000000..f3d4f96 --- /dev/null +++ b/core/graphics/graphics.go @@ -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 +} diff --git a/core/graphics/graphics_test.go b/core/graphics/graphics_test.go new file mode 100644 index 0000000..dd0c3c8 --- /dev/null +++ b/core/graphics/graphics_test.go @@ -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)) + } +}