From 2c01f9e6038b7c02a103427f9d84cbdba82bd260 Mon Sep 17 00:00:00 2001 From: "S.D" Date: Sun, 11 Oct 2020 22:54:30 +0100 Subject: [PATCH] first draw on screen! kinda slow and no input but we good --- chip8/chip8.go | 17 ++++++----------- cmd/test_prog/main.go | 1 + cmd/web/main.go | 24 ++++-------------------- cmd/web/spaceinvaders.go | 5 +++++ 4 files changed, 16 insertions(+), 31 deletions(-) create mode 100644 cmd/web/spaceinvaders.go diff --git a/chip8/chip8.go b/chip8/chip8.go index 69ac891..d64935f 100644 --- a/chip8/chip8.go +++ b/chip8/chip8.go @@ -1,8 +1,8 @@ package chip8 import ( - "fmt" "math/rand" + "fmt" ) const graphicsBufferSize = 64 * 32 @@ -47,7 +47,8 @@ func NewCHIP8(prog []byte) *Chip8 { cpu := Chip8{pc: 0x200} memory_slice := cpu.memory[:80] copy(memory_slice, getLetters()) - memory_slice = cpu.memory[200:] + memory_slice = cpu.memory[0x200:] + fmt.Printf("%d\n", prog[0]) copy(memory_slice, prog) // Do some extra checking to ensure the right @@ -235,7 +236,7 @@ func (cpu *Chip8) displaySprite() { pixel := cpu.memory[int(cpu.addressRegister)+row] for column := 0; column < 8; column++ { if pixel&(0x80>>column) != 0 { - graphicsPosition := x + column + ((y + row) * 60) + graphicsPosition := x + column + ((y + row) * 64) if cpu.graphics[graphicsPosition] == 1 { cpu.registers[0xF] = 1 } @@ -332,9 +333,9 @@ func (cpu *Chip8) fifteenIndexOpcodes() { } func (cpu *Chip8) PerformCycle() { - var opcode uint16 = (uint16(cpu.memory[cpu.pc]) << 8) + uint16(cpu.memory[cpu.pc]) + cpu.opcode = (uint16(cpu.memory[cpu.pc]) << 8) + uint16(cpu.memory[cpu.pc + 1]) cpu.pc += 2 - switch opcode >> 12 { + switch cpu.opcode >> 12 { case 0: cpu.zeroIndexOpcodes() case 1: @@ -370,9 +371,3 @@ func (cpu *Chip8) PerformCycle() { } } -func main() { - fmt.Printf("Hello world!\n") - prog := []byte{1, 2, 3, 4} - new_cpu := NewCHIP8(prog) - fmt.Printf("%d\n", new_cpu.opcode) -} diff --git a/cmd/test_prog/main.go b/cmd/test_prog/main.go index 21e8d6d..dcf34b8 100644 --- a/cmd/test_prog/main.go +++ b/cmd/test_prog/main.go @@ -8,5 +8,6 @@ import ( func main() { prog := make([]byte, 6) cpu := chip8.NewCHIP8(prog) + cpu.PerformCycle() fmt.Printf("This should print out zero: %d!\n", cpu.GetGraphicsBuffer()[0]) } \ No newline at end of file diff --git a/cmd/web/main.go b/cmd/web/main.go index 8eb5648..cd53333 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -19,7 +19,6 @@ var width, height float64 = 64, 32 var sizeMultiplier = 8 var drawBuf = [64 * 32]byte{} var graphicsLock sync.Mutex -var sleeptime = 2000 func main() { println("CHIP8 IS HERE!") @@ -30,23 +29,7 @@ func main() { height = float64(cvs.Height()) width = float64(cvs.Width()) - - // load in a rom!!! - resp, err := http.Get("https://github.com/dmatlack/chip8/raw/master/roms/games/Space%20Invaders%20%5BDavid%20Winter%5D.ch8") - if err != nil { - println("FUCK") - panic() - } - - content := make([]byte, resp.ContentLength) - n, err := resp.Body.Read(content) - - if err != nil { - println("FUCK") - panic() - } - - cpu := chip8.NewCHIP8(content) + cpu := chip8.NewCHIP8(getSpaceInvaders()) cvs.Start(60, Render) i := 0 @@ -63,13 +46,14 @@ func main() { if i > 7 { cpu.TickTimers() i = 0 + //println("here!") } <-c } } func timeCycle(c chan int) { - time.Sleep(sleeptime * time.Microsecond) + time.Sleep(2000 * time.Microsecond) c <- 0 } @@ -94,6 +78,6 @@ func Render(gc *draw2dimg.GraphicContext) bool { } gc.FillStroke() gc.Close() - + //println("drawing") return true } \ No newline at end of file diff --git a/cmd/web/spaceinvaders.go b/cmd/web/spaceinvaders.go new file mode 100644 index 0000000..396c3a5 --- /dev/null +++ b/cmd/web/spaceinvaders.go @@ -0,0 +1,5 @@ +package main + +func getSpaceInvaders() []byte { + return []byte {18, 37, 83, 80, 65, 67, 69, 32, 73, 78, 86, 65, 68, 69, 82, 83, 32, 48, 46, 57, 49, 32, 66, 121, 32, 68, 97, 118, 105, 100, 32, 87, 73, 78, 84, 69, 82, 96, 0, 97, 0, 98, 8, 163, 221, 208, 24, 113, 8, 242, 30, 49, 32, 18, 45, 112, 8, 97, 0, 48, 64, 18, 45, 105, 5, 108, 21, 110, 0, 35, 145, 96, 10, 240, 21, 240, 7, 48, 0, 18, 75, 35, 145, 126, 1, 18, 69, 102, 0, 104, 28, 105, 0, 106, 4, 107, 10, 108, 4, 109, 60, 110, 15, 0, 224, 35, 117, 35, 81, 253, 21, 96, 4, 224, 158, 18, 125, 35, 117, 56, 0, 120, 255, 35, 117, 96, 6, 224, 158, 18, 139, 35, 117, 56, 57, 120, 1, 35, 117, 54, 0, 18, 159, 96, 5, 224, 158, 18, 233, 102, 1, 101, 27, 132, 128, 163, 217, 212, 81, 163, 217, 212, 81, 117, 255, 53, 255, 18, 173, 102, 0, 18, 233, 212, 81, 63, 1, 18, 233, 212, 81, 102, 0, 131, 64, 115, 3, 131, 181, 98, 248, 131, 34, 98, 8, 51, 0, 18, 201, 35, 125, 130, 6, 67, 8, 18, 211, 51, 16, 18, 213, 35, 125, 130, 6, 51, 24, 18, 221, 35, 125, 130, 6, 67, 32, 18, 231, 51, 40, 18, 233, 35, 125, 62, 0, 19, 7, 121, 6, 73, 24, 105, 0, 106, 4, 107, 10, 108, 4, 125, 244, 110, 15, 0, 224, 35, 81, 35, 117, 253, 21, 18, 111, 247, 7, 55, 0, 18, 111, 253, 21, 35, 81, 139, 164, 59, 18, 19, 27, 124, 2, 106, 252, 59, 2, 19, 35, 124, 2, 106, 4, 35, 81, 60, 24, 18, 111, 0, 224, 164, 221, 96, 20, 97, 8, 98, 15, 208, 31, 112, 8, 242, 30, 48, 44, 19, 51, 96, 255, 240, 21, 240, 7, 48, 0, 19, 65, 240, 10, 0, 224, 167, 6, 254, 101, 18, 37, 163, 193, 249, 30, 97, 8, 35, 105, 129, 6, 35, 105, 129, 6, 35, 105, 129, 6, 35, 105, 123, 208, 0, 238, 128, 224, 128, 18, 48, 0, 219, 198, 123, 12, 0, 238, 163, 217, 96, 28, 216, 4, 0, 238, 35, 81, 142, 35, 35, 81, 96, 5, 240, 24, 240, 21, 240, 7, 48, 0, 19, 137, 0, 238, 106, 0, 141, 224, 107, 4, 233, 161, 18, 87, 166, 12, 253, 30, 240, 101, 48, 255, 19, 175, 106, 0, 107, 4, 109, 1, 110, 1, 19, 151, 165, 10, 240, 30, 219, 198, 123, 8, 125, 1, 122, 1, 58, 7, 19, 151, 0, 238, 60, 126, 255, 255, 153, 153, 126, 255, 255, 36, 36, 231, 126, 255, 60, 60, 126, 219, 129, 66, 60, 126, 255, 219, 16, 56, 124, 254, 0, 0, 127, 0, 63, 0, 127, 0, 0, 0, 1, 1, 1, 3, 3, 3, 3, 0, 0, 63, 32, 32, 32, 32, 32, 32, 32, 32, 63, 8, 8, 255, 0, 0, 254, 0, 252, 0, 254, 0, 0, 0, 126, 66, 66, 98, 98, 98, 98, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 125, 0, 65, 125, 5, 125, 125, 0, 0, 194, 194, 198, 68, 108, 40, 56, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 247, 16, 20, 247, 247, 4, 4, 0, 0, 124, 68, 254, 194, 194, 194, 194, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 239, 32, 40, 232, 232, 47, 47, 0, 0, 249, 133, 197, 197, 197, 197, 249, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 190, 0, 32, 48, 32, 190, 190, 0, 0, 247, 4, 231, 133, 133, 132, 244, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 127, 0, 63, 0, 127, 0, 0, 0, 239, 40, 239, 0, 224, 96, 111, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 254, 0, 252, 0, 254, 0, 0, 0, 192, 0, 192, 192, 192, 192, 192, 0, 0, 252, 4, 4, 4, 4, 4, 4, 4, 4, 252, 16, 16, 255, 249, 129, 185, 139, 154, 154, 250, 0, 250, 138, 154, 154, 155, 153, 248, 230, 37, 37, 244, 52, 52, 52, 0, 23, 20, 52, 55, 54, 38, 199, 223, 80, 80, 92, 216, 216, 223, 0, 223, 17, 31, 18, 27, 25, 217, 124, 68, 254, 134, 134, 134, 252, 132, 254, 130, 130, 254, 254, 128, 192, 192, 192, 254, 252, 130, 194, 194, 194, 252, 254, 128, 248, 192, 192, 254, 254, 128, 240, 192, 192, 192, 254, 128, 190, 134, 134, 254, 134, 134, 254, 134, 134, 134, 16, 16, 16, 16, 16, 16, 24, 24, 24, 72, 72, 120, 156, 144, 176, 192, 176, 156, 128, 128, 192, 192, 192, 254, 238, 146, 146, 134, 134, 134, 254, 130, 134, 134, 134, 134, 124, 130, 134, 134, 134, 124, 254, 130, 254, 192, 192, 192, 124, 130, 194, 202, 196, 122, 254, 134, 254, 144, 156, 132, 254, 192, 254, 2, 2, 254, 254, 16, 48, 48, 48, 48, 130, 130, 194, 194, 194, 254, 130, 130, 130, 238, 56, 16, 134, 134, 150, 146, 146, 238, 130, 68, 56, 56, 68, 130, 130, 130, 254, 48, 48, 48, 254, 2, 30, 240, 128, 254, 0, 0, 0, 0, 6, 6, 0, 0, 0, 96, 96, 192, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 0, 24, 124, 198, 12, 24, 0, 24, 0, 0, 254, 254, 0, 0, 254, 130, 134, 134, 134, 254, 8, 8, 8, 24, 24, 24, 254, 2, 254, 192, 192, 254, 254, 2, 30, 6, 6, 254, 132, 196, 196, 254, 4, 4, 254, 128, 254, 6, 6, 254, 192, 192, 192, 254, 130, 254, 254, 2, 2, 6, 6, 6, 124, 68, 254, 134, 134, 254, 254, 130, 254, 6, 6, 6, 68, 254, 68, 68, 254, 68, 168, 168, 168, 168, 168, 168, 168, 108, 90, 0, 12, 24, 168, 48, 78, 126, 0, 18, 24, 102, 108, 168, 90, 102, 84, 36, 102, 0, 72, 72, 24, 18, 168, 6, 144, 168, 18, 0, 126, 48, 18, 168, 132, 48, 78, 114, 24, 102, 168, 168, 168, 168, 168, 168, 144, 84, 120, 168, 72, 120, 108, 114, 168, 18, 24, 108, 114, 102, 84, 144, 168, 114, 42, 24, 168, 48, 78, 126, 0, 18, 24, 102, 108, 168, 114, 84, 168, 90, 102, 24, 126, 24, 78, 114, 168, 114, 42, 24, 48, 102, 168, 48, 78, 126, 0, 108, 48, 84, 78, 156, 168, 168, 168, 168, 168, 168, 168, 72, 84, 126, 24, 168, 144, 84, 120, 102, 168, 108, 42, 48, 90, 168, 132, 48, 114, 42, 168, 216, 168, 0, 78, 18, 168, 228, 162, 168, 0, 78, 18, 168, 108, 42, 84, 84, 114, 168, 132, 48, 114, 42, 168, 222, 156, 168, 114, 42, 24, 168, 12, 84, 72, 90, 120, 114, 24, 102, 168, 102, 24, 90, 84, 102, 114, 108, 168, 114, 42, 0, 114, 168, 114, 42, 24, 168, 48, 78, 126, 0, 18, 24, 102, 108, 168, 0, 102, 24, 168, 48, 78, 12, 102, 24, 0, 108, 48, 78, 36, 168, 114, 42, 24, 48, 102, 168, 30, 84, 102, 12, 24, 156, 168, 36, 84, 84, 18, 168, 66, 120, 12, 60, 168, 174, 168, 168, 168, 168, 168, 168, 168, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} +}