Skip to content

Commit

Permalink
Continued work on PPU fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
scottferg committed Sep 16, 2012
1 parent 398dcfd commit 39962e1
Show file tree
Hide file tree
Showing 146 changed files with 13,955 additions and 186 deletions.
22 changes: 11 additions & 11 deletions 6502.go
Expand Up @@ -24,6 +24,7 @@ type Cpu struct {

InterruptRequested int
CyclesToWait int
CycleTimestamp int
}

func (cpu *Cpu) getCarry() bool {
Expand Down Expand Up @@ -917,21 +918,18 @@ func (cpu *Cpu) Bit(location int) {
}

func (cpu *Cpu) PerformNmi() {
// $2000.7 enables/disables NMIs
if ppu.NmiOnVblank != 0x0 {
high := ProgramCounter >> 8
low := ProgramCounter & 0xFF
high := ProgramCounter >> 8
low := ProgramCounter & 0xFF

cpu.pushToStack(Word(high))
cpu.pushToStack(Word(low))
cpu.pushToStack(Word(high))
cpu.pushToStack(Word(low))

cpu.pushToStack(cpu.P)
cpu.pushToStack(cpu.P)

h, _ := Ram.Read(0xFFFB)
l, _ := Ram.Read(0xFFFA)
h, _ := Ram.Read(0xFFFB)
l, _ := Ram.Read(0xFFFA)

ProgramCounter = int(h)<<8 + int(l)
}
ProgramCounter = int(h)<<8 + int(l)
}

func (cpu *Cpu) PerformReset() {
Expand Down Expand Up @@ -1480,5 +1478,7 @@ func (cpu *Cpu) Step() int {
panic("Invalid opcode")
}

cpu.CycleTimestamp = (cpu.CycleCount * 15)

return cpu.CycleCount
}
22 changes: 20 additions & 2 deletions 6502_test.go
Expand Up @@ -2,7 +2,9 @@ package main

import (
"encoding/hex"
"fmt"
"io/ioutil"
"strconv"
"strings"
"testing"
)
Expand All @@ -13,6 +15,7 @@ type CpuState struct {
Y int
P int
S int
C int
Op int
}

Expand All @@ -21,6 +24,7 @@ func TestGoldLog(test *testing.T) {

Ram.Init()
cpu.Reset()
ppu.Init()

cpu.P = 0x24

Expand All @@ -43,7 +47,8 @@ func TestGoldLog(test *testing.T) {

log := strings.Split(string(logfile), "\n")

sentinel := 5003
sentinel := 100
//sentinel := 5003
for i := 0; i < sentinel; i++ {
op, _ := hex.DecodeString(log[i][:4])

Expand All @@ -59,18 +64,27 @@ func TestGoldLog(test *testing.T) {
y, _ := hex.DecodeString(strings.Split(registers[2], ":")[1])
p, _ := hex.DecodeString(strings.Split(registers[3], ":")[1])
sp, _ := hex.DecodeString(strings.Split(registers[4], ":")[1])
cyc, _ := strconv.Atoi(strings.TrimSpace(log[i][78:81]))

fmt.Printf("Cycle: %d\n", cyc)

expectedState := CpuState{
A: int(a[0]),
X: int(x[0]),
Y: int(y[0]),
P: int(p[0]),
S: int(sp[0]),
C: cyc,
Op: (int(high) << 8) + int(low),
}

verifyCpuState(ProgramCounter, &cpu, expectedState, test)
cpu.Step()
cycles := cpu.Step()

// 3 PPU cycles for each CPU cycle
for i := 0; i < 3*cycles; i++ {
ppu.Step()
}
}
}

Expand Down Expand Up @@ -98,4 +112,8 @@ func verifyCpuState(pc int, c *Cpu, e CpuState, test *testing.T) {
if c.StackPointer != Word(e.S) {
test.Errorf("PC: 0x%X Stack pointer was 0x%X, was expecting 0x%X\n", pc, c.StackPointer, e.S)
}

if ppu.Cycle != e.C {
test.Errorf("PC: 0x%X PPU Cycle was %d, was expecting %d\n", pc, ppu.Cycle, e.C)
}
}
11 changes: 10 additions & 1 deletion controller.go
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
)

Expand Down Expand Up @@ -84,7 +85,15 @@ func JoypadListen() {
running = false
case sdl.K_r:
// Trigger reset interrupt
cpu.RequestInterrupt(InterruptReset)
if e.Type == sdl.KEYDOWN {
fmt.Printf("VRAM: 0x%X\n", ppu.VramAddress)
cpu.RequestInterrupt(InterruptReset)
}
case sdl.K_n:
if e.Type == sdl.KEYDOWN {
// Trigger reset interrupt
ppu.DebugMode = !ppu.DebugMode
}
}

switch e.Type {
Expand Down
Binary file added games/Hydlide.nes
Binary file not shown.
Binary file added games/Kung Fu.nes
Binary file not shown.
Binary file added games/Spy Vs Spy.nes
Binary file not shown.
Binary file added games/mmc1/Bionic Commando.nes
Binary file not shown.
Binary file added games/mmc1/Blaster Master.nes
Binary file not shown.
Binary file added games/mmc1/Bubble Bobble.nes
Binary file not shown.
Binary file added games/mmc1/Castlevania 2 - Simon's Quest.nes
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions machine.go
Expand Up @@ -35,7 +35,7 @@ func main() {

Ram.Init()
cpu.Init()
v := ppu.Init()
v, d := ppu.Init()
controller.Init()

if contents, err := ioutil.ReadFile(os.Args[1]); err == nil {
Expand All @@ -51,7 +51,7 @@ func main() {
return
}

video.Init(v, os.Args[1])
video.Init(v, d, os.Args[1])
defer video.Close()

go JoypadListen()
Expand Down
58 changes: 21 additions & 37 deletions memory.go
Expand Up @@ -16,40 +16,6 @@ var (
Ram Memory
)

func PpuRegWrite(v Word, a int) {
switch a {
case 0x2000:
ppu.WriteControl(v)
case 0x2001:
ppu.WriteMask(v)
case 0x2003:
ppu.WriteOamAddress(v)
case 0x2004:
ppu.WriteOamData(v)
case 0x2005:
ppu.WriteScroll(v)
case 0x2006:
ppu.WriteAddress(v)
case 0x2007:
ppu.WriteData(v)
case 0x4014:
ppu.WriteDma(v)
}
}

func PpuRegRead(a int) (Word, error) {
switch a {
case 0x2002:
return ppu.ReadStatus()
case 0x2004:
return ppu.ReadOamData()
case 0x2007:
return ppu.ReadData()
}

return 0, nil
}

func fitAddressSize(addr interface{}) (v int, e error) {
switch a := addr.(type) {
case Word:
Expand All @@ -69,16 +35,30 @@ func (m *Memory) Init() {
}
}

func (m *Memory) WriteMirroredRam(v Word, a int) {
for i := 0; i < 0x2FFB; i += 8 {
m[0x2002+i] = v
}
}

func (m *Memory) Write(address interface{}, val Word) error {
if a, err := fitAddressSize(address); err == nil {
if a == 0x2002 {
return nil
}

m[a] = val

if a <= 0x2007 && a >= 0x2000 {
PpuRegWrite(val, a)
ppu.PpuRegWrite(val, a)
} else if a == 0x4014 {
PpuRegWrite(val, a)
ppu.PpuRegWrite(val, a)
} else if a == 0x4016 {
controller.Write(val)
} else if a >= 0x8000 && a <= 0xFFFF {
// MMC1
//rom.Write(val, a)
return nil
}

return nil
Expand All @@ -90,8 +70,12 @@ func (m *Memory) Write(address interface{}, val Word) error {
func (m *Memory) Read(address interface{}) (Word, error) {
a, _ := fitAddressSize(address)

if a == 0x200A {
return m[0x2002], nil
}

if a <= 0x2007 && a >= 0x2000 {
return PpuRegRead(a)
return ppu.PpuRegRead(a)
} else if a == 0x4016 {
return controller.Read(), nil
}
Expand Down

0 comments on commit 39962e1

Please sign in to comment.