Permalink
Browse files

Initial refactor to go back to SDL over GLFW

  • Loading branch information...
1 parent 3eede26 commit 2835162948a0a1dc9ccbae528851849943a0aea7 Scott Ferguson committed Oct 9, 2012
Showing with 107 additions and 94 deletions.
  1. +1 −1 .travis.yml
  2. +4 −1 6502.go
  3. +5 −3 README.md
  4. +13 −47 controller.go
  5. +2 −3 machine.go
  6. +3 −3 mmc1.go
  7. +22 −14 mmc3.go
  8. +57 −22 video.go
View
@@ -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
@@ -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,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
@@ -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
@@ -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
@@ -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)
Oops, something went wrong.

0 comments on commit 2835162

Please sign in to comment.