Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial refactor to go back to SDL over GLFW

  • Loading branch information...
commit 2835162948a0a1dc9ccbae528851849943a0aea7 1 parent 3eede26
@scottferg authored
View
2  .travis.yml
@@ -2,4 +2,4 @@ language: go
before_install:
- sudo apt-get update -qq
- - sudo apt-get install -qq libsdl1.2-dev libsdl-gfx1.2-dev libglfw-dev libglew1.6-dev libxrandr-dev
+ - sudo apt-get install -qq libsdl1.2-dev libsdl-gfx1.2-dev libglew1.6-dev libxrandr-dev
View
5 6502.go
@@ -1,6 +1,7 @@
package main
import (
+ "fmt"
"log"
)
@@ -918,6 +919,7 @@ func (c *Cpu) Bit(location uint16) {
}
func (c *Cpu) PerformIrq() {
+ fmt.Println("Performing IRQ")
high := ProgramCounter >> 8
low := ProgramCounter & 0xFF
@@ -1000,6 +1002,7 @@ func (c *Cpu) Step() int {
c.InterruptRequested = InterruptNone
}
+ logpc := ProgramCounter
opcode, _ := Ram.Read(ProgramCounter)
c.Opcode = opcode
@@ -1495,7 +1498,7 @@ func (c *Cpu) Step() int {
c.CycleCount = 4
c.Bit(c.absoluteAddress())
default:
- log.Fatalf("Invalid opcode at 0x%X: 0x%X", ProgramCounter, opcode)
+ log.Fatalf("Invalid opcode at 0x%X: 0x%X", logpc, opcode)
}
c.Timestamp = (c.CycleCount * 15)
View
8 README.md
@@ -8,17 +8,19 @@ This is an NES emulator, written in Go. It's fairly new and very much a work in
## To build on Linux
- $ sudo apt-get install libsdl1.2-dev libsdl-gfx1.2-dev libglfw-dev libglew1.6-dev libxrandr-dev
+ $ sudo apt-get install libsdl1.2-dev libsdl-gfx1.2-dev libglew1.6-dev libxrandr-dev
$ go get -u github.com/0xe2-0x9a-0x9b/Go-SDL/sdl
+ $ go get -u github.com/0xe2-0x9a-0x9b/Go-SDL/gfx
+ $ go get -u github.com/banthar/gl
$ go test
$ go build
## To build on OSX
- $ brew install sdl sdl_gfx glfw glew
+ $ brew install sdl sdl_gfx glew
+ $ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig go get -u github.com/0xe2-0x9a-0x9b/Go-SDL/sdl
$ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig go get -u github.com/0xe2-0x9a-0x9b/Go-SDL/gfx
$ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig go get -u github.com/banthar/gl
- $ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig go get -u github.com/jteeuwen/glfw
$ go test
$ go build
View
60 controller.go
@@ -1,22 +1,7 @@
package main
import (
- "github.com/jteeuwen/glfw"
-)
-
-const (
- A = 90
- B = 88
- Select = glfw.KeyRshift
- Start = glfw.KeyEnter
- Up = glfw.KeyUp
- Left = glfw.KeyLeft
- Down = glfw.KeyDown
- Right = glfw.KeyRight
-
- KeyEventReset = 82
- KeyEventSave = 83
- KeyEventLoad = 76
+ "github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
)
type Controller struct {
@@ -25,32 +10,32 @@ type Controller struct {
LastWrite Word
}
-func (c *Controller) SetButtonState(k int, v Word) {
- switch k {
- case A: // A
+func (c *Controller) SetButtonState(k sdl.KeyboardEvent, v Word) {
+ switch k.Keysym.Sym {
+ case sdl.K_z: // A
c.ButtonState[0] = v
- case B: // B
+ case sdl.K_x: // B
c.ButtonState[1] = v
- case Select: // Select
+ case sdl.K_RSHIFT: // Select
c.ButtonState[2] = v
- case Start: // Start
+ case sdl.K_RETURN: // Start
c.ButtonState[3] = v
- case Up: // Up
+ case sdl.K_UP: // Up
c.ButtonState[4] = v
- case Down: // Down
+ case sdl.K_DOWN: // Down
c.ButtonState[5] = v
- case Left: // Left
+ case sdl.K_LEFT: // Left
c.ButtonState[6] = v
- case Right: // Right
+ case sdl.K_RIGHT: // Right
c.ButtonState[7] = v
}
}
-func (c *Controller) KeyDown(e int) {
+func (c *Controller) KeyDown(e sdl.KeyboardEvent) {
c.SetButtonState(e, 0x41)
}
-func (c *Controller) KeyUp(e int) {
+func (c *Controller) KeyUp(e sdl.KeyboardEvent) {
c.SetButtonState(e, 0x40)
}
@@ -85,22 +70,3 @@ func (c *Controller) Init() {
c.ButtonState[i] = 0x40
}
}
-
-func KeyListener(key, state int) {
- if state == glfw.KeyPress {
- switch key {
- case glfw.KeyEsc:
- running = false
- case KeyEventReset:
- cpu.RequestInterrupt(InterruptReset)
- case KeyEventLoad:
- LoadState()
- case KeyEventSave:
- SaveState()
- default:
- controller.KeyDown(key)
- }
- } else {
- controller.KeyUp(key)
- }
-}
View
5 machine.go
@@ -207,9 +207,6 @@ func main() {
return
}
- video.Init(v, d, gamename)
- defer video.Close()
-
// Main runloop, in a separate goroutine so that
// the video rendering can happen on this one
go func() {
@@ -224,6 +221,8 @@ func main() {
// This needs to happen on the main thread for OSX
runtime.LockOSThread()
+ video.Init(v, d, gamename)
+ defer video.Close()
video.Render()
return
View
6 mmc1.go
@@ -111,7 +111,7 @@ func (m *Mmc1) SetRegister(reg int, v int) {
case Size8k:
// Swap 8k VROM (in 8k mode, ignore first bit D0)
bank := v & 0xF
- bank %= m.ChrRomCount
+ bank %= len(m.VromBanks)
if v&0x10 == 0x10 {
bank = (len(m.VromBanks) / 2) + (v & 0xF)
@@ -130,7 +130,7 @@ func (m *Mmc1) SetRegister(reg int, v int) {
} else {
bank = v & 0xF
}
- WriteVramBank(m.VromBanks, bank%m.ChrRomCount, 0x0, Size4k)
+ WriteVramBank(m.VromBanks, bank%len(m.VromBanks), 0x0, Size4k)
}
// CHR Bank 1
case 2:
@@ -148,7 +148,7 @@ func (m *Mmc1) SetRegister(reg int, v int) {
} else {
bank = v & 0xF
}
- WriteVramBank(m.VromBanks, bank%m.ChrRomCount, 0x1000, Size4k)
+ WriteVramBank(m.VromBanks, bank%len(m.VromBanks), 0x1000, Size4k)
}
// PRG Bank
case 3:
View
36 mmc3.go
@@ -48,6 +48,8 @@ type Mmc3 struct {
IrqCounter int
IrqPreset int
IrqPresetVbl int
+
+ RamProtectDest [16]int
}
func NewMmc3(r *Rom) *Mmc3 {
@@ -78,8 +80,8 @@ func (m *Mmc3) LoadRom() {
// http://forums.nesdev.com/viewtopic.php?p=38182#p38182
// Write hardwired PRG banks (0xC000 and 0xE000)
- m.Write8kRamBank((m.PrgBankCount-1)*2, 0xC000)
- m.Write8kRamBank(((m.PrgBankCount-1)*2)+1, 0xE000)
+ m.Write8kRamBank((len(m.RomBanks)-1)*2, 0xC000)
+ m.Write8kRamBank(((len(m.RomBanks)-1)*2)+1, 0xE000)
// Write swappable PRG banks (0x8000 and 0xA000)
m.Write8kRamBank(0, 0x8000)
@@ -155,13 +157,14 @@ func (m *Mmc3) BankSelect(v int) {
func (m *Mmc3) BankData(v int) {
loadHardBanks := func() {
+ fmt.Println("ADDRESS CHANGED")
if m.AddressChanged {
if m.PrgBankMode == PrgBankSwapModeLow {
- fmt.Println("Changed address high")
- m.Write8kRamBank((m.PrgBankCount-1)*2, 0xC000)
+ //fmt.Println("Changed address high")
+ m.Write8kRamBank((len(m.RomBanks)-1)*2, 0xC000)
} else {
- fmt.Println("Changed address low")
- m.Write8kRamBank((m.PrgBankCount-1)*2, 0x8000)
+ //fmt.Println("Changed address low")
+ m.Write8kRamBank((len(m.RomBanks)-1)*2, 0x8000)
}
m.AddressChanged = false
@@ -255,14 +258,14 @@ func (m *Mmc3) BankData(v int) {
loadHardBanks()
if m.PrgBankMode == PrgBankSwapModeLow {
- fmt.Printf("0x%X: Low mode (0x8000) PRG switch on bank -> %d\n", ProgramCounter, v)
+ //fmt.Printf("0x%X: Low mode (0x8000) PRG switch on bank -> %d\n", ProgramCounter, v)
m.Write8kRamBank(v, 0x8000)
} else {
- fmt.Printf("0x%X: High mode (0xC000) PRG switch on bank -> %d\n", ProgramCounter, v)
+ //fmt.Printf("0x%X: High mode (0xC000) PRG switch on bank -> %d\n", ProgramCounter, v)
m.Write8kRamBank(v, 0xC000)
}
case PrgBank8kA000:
- fmt.Printf("0x%X: 8k 0xA000 PRG switch on bank -> %d\n", ProgramCounter, v)
+ //fmt.Printf("0x%X: 8k 0xA000 PRG switch on bank -> %d\n", ProgramCounter, v)
m.Write8kRamBank(v, 0xA000)
loadHardBanks()
@@ -279,7 +282,7 @@ func (m *Mmc3) SetMirroring(v int) {
}
func (m *Mmc3) RamProtection(v int) {
- // TODO: WhAT IS THIS I DON'T EVEN
+ // TODO: WhAT IS THIS I DON'T EVEN
fmt.Println("RamProtection register")
}
@@ -311,17 +314,22 @@ func (m *Mmc3) IrqEnable(v int) {
}
func (m *Mmc3) Write8kRamBank(bank, dest int) {
- b := (bank / 2) % m.PrgBankCount
+ if m.RamProtectDest[dest>>12] == bank {
+ return
+ }
+ m.RamProtectDest[dest>>12] = bank
+
+ b := (bank >> 1) % len(m.RomBanks)
offset := (bank % 2) * 0x2000
- //fmt.Printf("Updating bank at: 0x%X\n", dest)
- //fmt.Printf("Upper 8k offset: %d\n", offset)
+ fmt.Printf("Updating bank: %d\n", b)
+ fmt.Printf("Upper 8k offset: %d\n", offset)
WriteOffsetRamBank(m.RomBanks, b, dest, Size8k, offset)
}
func (m *Mmc3) Write1kVramBank(bank, dest int) {
- b := (bank / 4)
+ b := (bank >> 2) % len(m.VromBanks)
offset := (bank % 4) * 0x400
//fmt.Printf("Updating bank: %d\n", b)
View
79 video.go
@@ -3,17 +3,17 @@ package main
import (
"fmt"
"github.com/0xe2-0x9a-0x9b/Go-SDL/gfx"
+ "github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
"github.com/banthar/gl"
- "github.com/jteeuwen/glfw"
+ "log"
"math"
- "os"
- "runtime"
)
type Video struct {
tick <-chan []uint32
debug <-chan []uint32
fpsmanager *gfx.FPSmanager
+ screen *sdl.Surface
tex gl.Texture
}
@@ -21,27 +21,24 @@ func (v *Video) Init(t <-chan []uint32, d <-chan []uint32, n string) {
v.tick = t
v.debug = d
- if err := glfw.Init(); err != nil {
- fmt.Fprintf(os.Stderr, "[e] %v\n", err)
- return
+ if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
+ log.Fatal(sdl.GetError())
}
- if err := glfw.OpenWindow(512, 480, 0, 0, 0, 0, 0, 0, glfw.Windowed); err != nil {
- fmt.Fprintf(os.Stderr, "[e] %v\n", err)
- return
+ v.screen = sdl.SetVideoMode(512, 480, 32, sdl.OPENGL|sdl.RESIZABLE)
+
+ if v.screen == nil {
+ log.Fatal(sdl.GetError())
}
+ sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "")
+
if gl.Init() != 0 {
panic("gl error")
}
gl.Enable(gl.TEXTURE_2D)
-
- glfw.SetWindowTitle(fmt.Sprintf("Fergulator - %s", n))
- glfw.SetWindowSizeCallback(reshape)
- glfw.SetWindowCloseCallback(quit_event)
- glfw.SetKeyCallback(KeyListener)
- reshape(512, 480)
+ v.Reshape(int(v.screen.W), int(v.screen.H))
v.tex = gl.GenTexture()
@@ -49,7 +46,12 @@ func (v *Video) Init(t <-chan []uint32, d <-chan []uint32, n string) {
v.fpsmanager.SetFramerate(70)
}
-func reshape(width int, height int) {
+func (v *Video) ResizeEvent(re *sdl.ResizeEvent) {
+ v.screen = sdl.SetVideoMode(int(re.W), int(re.H), 32, sdl.OPENGL|sdl.RESIZABLE)
+ v.Reshape(int(re.W), int(re.H))
+}
+
+func (v *Video) Reshape(width int, height int) {
x_offset := 0
y_offset := 0
@@ -80,10 +82,42 @@ func quit_event() int {
}
func (v *Video) Render() {
- runtime.LockOSThread()
-
for running {
select {
+ case ev := <-sdl.Events:
+ switch e := ev.(type) {
+ case sdl.QuitEvent:
+ running = false
+ case sdl.ResizeEvent:
+ video.ResizeEvent(&e)
+ case sdl.KeyboardEvent:
+ switch e.Keysym.Sym {
+ case sdl.K_ESCAPE:
+ running = false
+ case sdl.K_r:
+ // Trigger reset interrupt
+ if e.Type == sdl.KEYDOWN {
+ cpu.RequestInterrupt(InterruptReset)
+ }
+ case sdl.K_l:
+ if e.Type == sdl.KEYDOWN {
+ // Trigger reset interrupt
+ LoadState()
+ }
+ case sdl.K_s:
+ if e.Type == sdl.KEYDOWN {
+ // Trigger reset interrupt
+ SaveState()
+ }
+ }
+
+ switch e.Type {
+ case sdl.KEYDOWN:
+ controller.KeyDown(e)
+ case sdl.KEYUP:
+ controller.KeyUp(e)
+ }
+ }
case val := <-v.tick:
slice := make([]uint8, len(val)*3)
for i := 0; i < len(val); i = i + 1 {
@@ -110,13 +144,14 @@ func (v *Video) Render() {
gl.Vertex3f(-1.0, 1.0, 0.0)
gl.End()
- glfw.SwapBuffers()
- v.fpsmanager.FramerateDelay()
+ if v.screen != nil {
+ sdl.GL_SwapBuffers()
+ v.fpsmanager.FramerateDelay()
+ }
}
}
}
func (v *Video) Close() {
- glfw.CloseWindow()
- glfw.Terminate()
+ sdl.Quit()
}
Please sign in to comment.
Something went wrong with that request. Please try again.