From e10a937bd1c1e6d577991c059860126deb1bb983 Mon Sep 17 00:00:00 2001 From: "S.D" Date: Sun, 11 Oct 2020 20:55:31 +0100 Subject: [PATCH] start trying to get wasm workinng --- .drone.yml | 5 ++- chip8/chip8.go | 53 +++++++++++++++++++++++++- cmd/web/main.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++ go.sum | 14 +++++++ 5 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 cmd/web/main.go create mode 100644 go.sum diff --git a/.drone.yml b/.drone.yml index 548e1ad..f7d1771 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,7 +19,10 @@ steps: image: golang:latest commands: - GOOS=darwin GOARCH=amd64 go build -o mac_test ./cmd/test_prog - +- name : build wasm + image: golang:latest + commands: + - GOOS=js GOARCH=wasm go build -o main.wasm ./cmd/web - name: publish image: plugins/gitea-release diff --git a/chip8/chip8.go b/chip8/chip8.go index 65b38dc..69ac891 100644 --- a/chip8/chip8.go +++ b/chip8/chip8.go @@ -57,9 +57,14 @@ func NewCHIP8(prog []byte) *Chip8 { } func (cpu *Chip8) GetGraphicsBuffer() [graphicsBufferSize]byte { + cpu.drawRequired = false return cpu.graphics } +func (cpu *Chip8) DrawIsNeeded() bool { + return cpu.drawRequired +} + func (cpu *Chip8) TickTimers() { if cpu.delayTimer > 0 { cpu.delayTimer-- @@ -241,7 +246,7 @@ func (cpu *Chip8) displaySprite() { cpu.drawRequired = true } -func (cpu *Chip8) SkipOnKeyOpcodes() { +func (cpu *Chip8) skipOnKeyOpcodes() { opcode := cpu.opcode & 0xFF key := cpu.registers[(cpu.opcode>>8)&0x0F] switch opcode { @@ -260,7 +265,7 @@ func (cpu *Chip8) SkipOnKeyOpcodes() { } } -func (cpu *Chip8) FifteenIndexOpcodes() { +func (cpu *Chip8) fifteenIndexOpcodes() { instruction := cpu.opcode & 0xFF reg := int(cpu.opcode>>8) & 0xF switch instruction { @@ -271,11 +276,16 @@ func (cpu *Chip8) FifteenIndexOpcodes() { case 0x0A: // FX0A // block + wait for key press + found := false for i, val := range cpu.keys { if val != 0 { cpu.registers[reg] = byte(i) + found = true } } + if !found { + cpu.pc -= 2 + } case 0x15: // FX15 // Set delay timer to VX @@ -321,6 +331,45 @@ func (cpu *Chip8) FifteenIndexOpcodes() { } +func (cpu *Chip8) PerformCycle() { + var opcode uint16 = (uint16(cpu.memory[cpu.pc]) << 8) + uint16(cpu.memory[cpu.pc]) + cpu.pc += 2 + switch opcode >> 12 { + case 0: + cpu.zeroIndexOpcodes() + case 1: + cpu.goTo() + case 2: + cpu.callSubroutine() + case 3: + cpu.skipIfRegisterEqual() + case 4: + cpu.skipIfRegisterNotEqual() + case 5: + cpu.skipIfRegistersEqual() + case 6: + cpu.setRegisterTo() + case 7: + cpu.registerPlusEqualsNN() + case 8: + cpu.bitOpsAndMath() + case 9: + cpu.skipIfRegistersNotEqual() + case 0xA: + cpu.setAddressRegister() + case 0xB: + cpu.jumpToV0PlusAddress() + case 0xC: + cpu.setRegisterToRand() + case 0xD: + cpu.displaySprite() + case 0xE: + cpu.skipOnKeyOpcodes() + case 0xF: + cpu.fifteenIndexOpcodes() + } +} + func main() { fmt.Printf("Hello world!\n") prog := []byte{1, 2, 3, 4} diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..8eb5648 --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "image/color" + "time" + "sync" + + "github.com/llgcode/draw2d/draw2dimg" + "github.com/llgcode/draw2d/draw2dkit" + "github.com/markfarnan/go-canvas/canvas" + + "git.jacknet.io/S.D/Chip-8_Go/chip8" +) + +var done chan struct{} + +var cvs *canvas.Canvas2d +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!") + + cvs, _ = canvas.NewCanvas2d(false) + cvs.Create(int(width)*sizeMultiplier, int(height)*sizeMultiplier) + + 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) + + cvs.Start(60, Render) + i := 0 + for { + c := make(chan int) + go timeCycle(c) + cpu.PerformCycle() + if cpu.DrawIsNeeded() { + graphicsLock.Lock() + drawBuf = cpu.GetGraphicsBuffer() + graphicsLock.Unlock() + } + i++ + if i > 7 { + cpu.TickTimers() + i = 0 + } + <-c + } +} + +func timeCycle(c chan int) { + time.Sleep(sleeptime * time.Microsecond) + c <- 0 +} + +func Render(gc *draw2dimg.GraphicContext) bool { + gc.SetFillColor(color.RGBA{0x00, 0x00, 0x00, 0xff}) + gc.Clear() + + gc.SetFillColor(color.RGBA{0x00, 0xff, 0x00, 0xff}) + //gc.SetStrokeColor(color.RGBA{0x00, 0xff, 0x00, 0xff}) + + gc.BeginPath() + //gc.ArcTo(gs.laserX, gs.laserY, gs.laserSize, gs.laserSize, 0, math.Pi*2) + graphicsLock.Lock() + defer graphicsLock.Unlock() + for i, val := range drawBuf { + if val != 0 { + x := i % 64 + y := i / 64 + // println("drawing to ", x, y) + draw2dkit.Rectangle(gc, float64(x*sizeMultiplier), float64(y*sizeMultiplier), float64((x*sizeMultiplier)+sizeMultiplier), float64((y*sizeMultiplier)+sizeMultiplier)) + } + } + gc.FillStroke() + gc.Close() + + return true +} \ No newline at end of file diff --git a/go.mod b/go.mod index a927f9a..b22511c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module git.jacknet.io/S.D/Chip-8_Go go 1.15 + +require ( + github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e + github.com/markfarnan/go-canvas v0.0.0-20200722235510-6971ccd00770 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..073c500 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/go-gl/gl v0.0.0-20180407155706-68e253793080/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= +github.com/go-gl/glfw v0.0.0-20180426074136-46a8d530c326/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= +github.com/llgcode/draw2d v0.0.0-20200110163050-b96d8208fcfc/go.mod h1:mVa0dA29Db2S4LVqDYLlsePDzRJLDfdhVZiI15uY0FA= +github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e h1:YRRazju3DMGuZTSWEj0nE2SCRcK3DW/qdHQ4UQx7sgs= +github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e/go.mod h1:mVa0dA29Db2S4LVqDYLlsePDzRJLDfdhVZiI15uY0FA= +github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb h1:61ndUreYSlWFeCY44JxDDkngVoI7/1MVhEl98Nm0KOk= +github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb/go.mod h1:1l8ky+Ew27CMX29uG+a2hNOKpeNYEQjjtiALiBlFQbY= +github.com/markfarnan/go-canvas v0.0.0-20200722235510-6971ccd00770 h1:7t1KxXXIIysYDk1ymAB3NeCRdp8WnV5+JqgSwAm6deg= +github.com/markfarnan/go-canvas v0.0.0-20200722235510-6971ccd00770/go.mod h1:PqPh9d/lLHBot/ZoW4ZbZaRVLdBIHJZfPYLaC84RSI8= +golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 h1:00VmoueYNlNz/aHIilyyQz/MHSqGoWJzpFv/HW8xpzI= +golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=