Skip to content

Commit

Permalink
Run gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
scottferg committed Sep 19, 2012
1 parent ab52cc2 commit 09e9aa7
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 120 deletions.
2 changes: 1 addition & 1 deletion 6502.go
@@ -1,7 +1,7 @@
package main

import (
"fmt"
"fmt"
)

const (
Expand Down
16 changes: 8 additions & 8 deletions controller.go
Expand Up @@ -85,15 +85,15 @@ func JoypadListen() {
running = false
case sdl.K_r:
// Trigger reset interrupt
if e.Type == sdl.KEYDOWN {
fmt.Printf("VRAM: 0x%X\n", ppu.VramAddress)
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
}
if e.Type == sdl.KEYDOWN {
// Trigger reset interrupt
ppu.DebugMode = !ppu.DebugMode
}
}

switch e.Type {
Expand Down
6 changes: 3 additions & 3 deletions machine.go
Expand Up @@ -60,9 +60,9 @@ func main() {
for running {
cycles := cpu.Step()

for i := 0; i < 3*cycles; i++ {
ppu.Step()
}
for i := 0; i < 3*cycles; i++ {
ppu.Step()
}
}

return
Expand Down
12 changes: 6 additions & 6 deletions memory.go
Expand Up @@ -50,10 +50,10 @@ func (m *Memory) Write(address interface{}, val Word) error {
m[a] = val

if a <= 0x2007 && a >= 0x2000 {
//ppu.Run(cpu.Timestamp * 3)
//ppu.Run(cpu.Timestamp * 3)
ppu.PpuRegWrite(val, a)
} else if a == 0x4014 {
//ppu.Run(cpu.Timestamp * 3)
//ppu.Run(cpu.Timestamp * 3)
ppu.PpuRegWrite(val, a)
} else if a == 0x4016 {
controller.Write(val)
Expand All @@ -72,12 +72,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 == 0x200A {
return m[0x2002], nil
}

if a <= 0x2007 && a >= 0x2000 {
//ppu.Run(cpu.Timestamp)
//ppu.Run(cpu.Timestamp)
return ppu.PpuRegRead(a)
} else if a == 0x4016 {
return controller.Read(), nil
Expand Down
173 changes: 87 additions & 86 deletions ppu.go
Expand Up @@ -36,8 +36,8 @@ type Flags struct {
}

type Pixel struct {
Color int
Value int
Color int
Value int
}

type Masks struct {
Expand All @@ -62,8 +62,8 @@ type Registers struct {
FineX Word
Data Word
WriteLatch bool
HighBitShift uint16
LowBitShift uint16
HighBitShift uint16
LowBitShift uint16
}

type Ppu struct {
Expand Down Expand Up @@ -108,7 +108,7 @@ func (p *Ppu) Init() (chan []int, chan []int) {
p.VblankTime = 20 * 341 * 5 // NTSC
p.TilerowCounter = 0

p.Nametables.Init()
p.Nametables.Init()

for i, _ := range p.Vram {
p.Vram[i] = 0x00
Expand Down Expand Up @@ -188,13 +188,13 @@ func (p *Ppu) writeMirroredVram(a int, v Word) {
}

func (p *Ppu) raster() {
length := len(p.Palettebuffer)
length := len(p.Palettebuffer)
for i := length - 1; i >= 0; i-- {
y := int(math.Floor(float64(i / 256)))
x := i - (y * 256)

var color int
color = p.Palettebuffer[i].Color
var color int
color = p.Palettebuffer[i].Color
p.Framebuffer[(y*256)+x] = color
}

Expand All @@ -218,12 +218,12 @@ func (p *Ppu) Step() {
p.RenderNametables()
p.Debug <- p.Framebuffer
} else {
// TODO: This should happen per scanline
if p.ShowSprites && (p.SpriteSize&0x1 == 0x1) {
for i := 0; i < 240; i++ {
p.evaluateScanlineSprites(i)
}
}
// TODO: This should happen per scanline
if p.ShowSprites && (p.SpriteSize&0x1 == 0x1) {
for i := 0; i < 240; i++ {
p.evaluateScanlineSprites(i)
}
}
p.raster()
}

Expand All @@ -248,7 +248,7 @@ func (p *Ppu) Step() {
p.updateEndScanlineRegisters()
}

// TODO: Shouldn't have to do this
// TODO: Shouldn't have to do this
if p.ShowSprites && (p.SpriteSize&0x1 == 0) {
p.evaluateScanlineSprites(p.Scanline)
}
Expand Down Expand Up @@ -482,7 +482,7 @@ func (p *Ppu) WriteData(v Word) {
// Nametable mirroring
p.Nametables.writeNametableData(p.VramAddress, v)
} else {
p.Vram[p.VramAddress & 0x3FFF] = v
p.Vram[p.VramAddress&0x3FFF] = v
}

p.incrementVramAddress()
Expand Down Expand Up @@ -513,9 +513,9 @@ func (p *Ppu) sprPatternTableAddress(i int) int {
if p.SpriteSize&0x01 != 0x0 {
// 8x16 Sprites
if i&0x01 != 0 {
return 0x1000 | ((int(i) >> 1) * 0x20)
return 0x1000 | ((int(i) >> 1) * 0x20)
} else {
return ((int(i) >> 1) * 0x20)
return ((int(i) >> 1) * 0x20)
}

}
Expand Down Expand Up @@ -617,82 +617,82 @@ func (p *Ppu) renderNametable(table, xoff, yoff int) {

func (p *Ppu) renderTileRow() {
// Generates each tile, one scanline at a time
// and applies the palette
// and applies the palette

// 32 total for 32 tiles, 8 pixels of each
// Load first two tiles into shift registers at start, then load
// one per loop and shift the other back out
// xcoord := p.VramAddress & 0x1F

fetchTileAttributes := func() (int, Word) {
attrAddr := 0x23C0 | (p.VramAddress & 0xC00) | int(p.AttributeLocation[p.VramAddress&0x3FF])
shift := p.AttributeShift[p.VramAddress&0x3FF]
attr := ((p.Nametables.readNametableData(attrAddr) >> shift) & 0x03) << 2

index := p.Nametables.readNametableData(p.VramAddress)
t := p.bgPatternTableAddress(index)

// Flip bit 10 on wraparound
if p.VramAddress&0x1F == 0x1F {
// If rendering is enabled, at the end of a scanline
// copy bits 10 and 4-0 from VRAM latch into VRAMADDR
p.VramAddress = p.VramAddress ^ 0x41F
} else {
p.VramAddress++
}

return t, attr
}

t, attr := fetchTileAttributes()
tile := p.Vram[t : t+16]
// Move first tile into shift registers
p.LowBitShift = uint16(tile[p.TilerowCounter])
p.HighBitShift = uint16(tile[p.TilerowCounter+8])

t, attrBuf := fetchTileAttributes()
tile = p.Vram[t : t+16]
// Get second tile, move the pixels into the right side of
// shift registers
// Current tile to render is attrBuf
p.LowBitShift = (p.LowBitShift << 8) | uint16(tile[p.TilerowCounter])
p.HighBitShift = (p.HighBitShift << 8) | uint16(tile[p.TilerowCounter+8])

// Load first two tiles into shift registers at start, then load
// one per loop and shift the other back out
// xcoord := p.VramAddress & 0x1F

fetchTileAttributes := func() (int, Word) {
attrAddr := 0x23C0 | (p.VramAddress & 0xC00) | int(p.AttributeLocation[p.VramAddress&0x3FF])
shift := p.AttributeShift[p.VramAddress&0x3FF]
attr := ((p.Nametables.readNametableData(attrAddr) >> shift) & 0x03) << 2

index := p.Nametables.readNametableData(p.VramAddress)
t := p.bgPatternTableAddress(index)

// Flip bit 10 on wraparound
if p.VramAddress&0x1F == 0x1F {
// If rendering is enabled, at the end of a scanline
// copy bits 10 and 4-0 from VRAM latch into VRAMADDR
p.VramAddress = p.VramAddress ^ 0x41F
} else {
p.VramAddress++
}

return t, attr
}

t, attr := fetchTileAttributes()
tile := p.Vram[t : t+16]
// Move first tile into shift registers
p.LowBitShift = uint16(tile[p.TilerowCounter])
p.HighBitShift = uint16(tile[p.TilerowCounter+8])

t, attrBuf := fetchTileAttributes()
tile = p.Vram[t : t+16]
// Get second tile, move the pixels into the right side of
// shift registers
// Current tile to render is attrBuf
p.LowBitShift = (p.LowBitShift << 8) | uint16(tile[p.TilerowCounter])
p.HighBitShift = (p.HighBitShift << 8) | uint16(tile[p.TilerowCounter+8])

for x := 0; x < 32; x++ {
var palette []Word

var b uint
for b = 0; b < 8; b++ {
fbRow := p.Scanline*256 + ((x*8) + int(b))

// If we're grabbing the pixel from the high
// part of the shift register, use the buffered
// palette, not the current one
if (15 - b - uint(p.FineX)) < 8 {
palette = p.bgPaletteEntry(attrBuf)
} else {
palette = p.bgPaletteEntry(attr)
}

pixel := (p.LowBitShift >> (15 - b - uint(p.FineX))) & 0x01
pixel += ((p.HighBitShift >> (15 - b - uint(p.FineX)) & 0x01) << 1)

p.Palettebuffer[fbRow] = Pixel{
PaletteRgb[int(palette[pixel])],
int(pixel),
}
}
var palette []Word

var b uint
for b = 0; b < 8; b++ {
fbRow := p.Scanline*256 + ((x * 8) + int(b))

// If we're grabbing the pixel from the high
// part of the shift register, use the buffered
// palette, not the current one
if (15 - b - uint(p.FineX)) < 8 {
palette = p.bgPaletteEntry(attrBuf)
} else {
palette = p.bgPaletteEntry(attr)
}

pixel := (p.LowBitShift >> (15 - b - uint(p.FineX))) & 0x01
pixel += ((p.HighBitShift >> (15 - b - uint(p.FineX)) & 0x01) << 1)

p.Palettebuffer[fbRow] = Pixel{
PaletteRgb[int(palette[pixel])],
int(pixel),
}
}

// xcoord = p.VramAddress & 0x1F
attr = attrBuf
attr = attrBuf

t, attrBuf = fetchTileAttributes()
tile = p.Vram[t : t+16]
t, attrBuf = fetchTileAttributes()
tile = p.Vram[t : t+16]

// Shift the first tile out, bring the new tile in
p.LowBitShift = (p.LowBitShift << 8) | uint16(tile[p.TilerowCounter])
p.HighBitShift = (p.HighBitShift << 8) | uint16(tile[p.TilerowCounter+8])
// Shift the first tile out, bring the new tile in
p.LowBitShift = (p.LowBitShift << 8) | uint16(tile[p.TilerowCounter])
p.HighBitShift = (p.HighBitShift << 8) | uint16(tile[p.TilerowCounter+8])
}

p.TilerowCounter++
Expand Down Expand Up @@ -723,6 +723,7 @@ func (p *Ppu) evaluateScanlineSprites(line int) {
p.sprPaletteEntry(uint(attrValue)),
&p.Attributes[i], i == 0)

// Next tile
tile = p.Vram[s+16 : s+32]

p.decodePatternTile([]Word{tile[c], tile[c+8]},
Expand Down

0 comments on commit 09e9aa7

Please sign in to comment.