From 09e9aa7485a175d3fb5964b40de264225c93c2c1 Mon Sep 17 00:00:00 2001 From: Scott Ferguson Date: Tue, 18 Sep 2012 21:55:26 -0500 Subject: [PATCH] Run gofmt --- 6502.go | 2 +- controller.go | 16 ++--- machine.go | 6 +- memory.go | 12 ++-- ppu.go | 173 +++++++++++++++++++++++++------------------------- rom.go | 26 ++++---- video.go | 6 +- 7 files changed, 121 insertions(+), 120 deletions(-) diff --git a/6502.go b/6502.go index f4af600..fa9916d 100644 --- a/6502.go +++ b/6502.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "fmt" ) const ( diff --git a/controller.go b/controller.go index 66c0d25..7082865 100644 --- a/controller.go +++ b/controller.go @@ -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 { diff --git a/machine.go b/machine.go index 3edb97f..63940fe 100644 --- a/machine.go +++ b/machine.go @@ -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 diff --git a/memory.go b/memory.go index 2eac724..b7755da 100644 --- a/memory.go +++ b/memory.go @@ -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) @@ -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 diff --git a/ppu.go b/ppu.go index 059ce72..7b3b6f4 100644 --- a/ppu.go +++ b/ppu.go @@ -36,8 +36,8 @@ type Flags struct { } type Pixel struct { - Color int - Value int + Color int + Value int } type Masks struct { @@ -62,8 +62,8 @@ type Registers struct { FineX Word Data Word WriteLatch bool - HighBitShift uint16 - LowBitShift uint16 + HighBitShift uint16 + LowBitShift uint16 } type Ppu struct { @@ -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 @@ -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 } @@ -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() } @@ -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) } @@ -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() @@ -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) } } @@ -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++ @@ -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]}, diff --git a/rom.go b/rom.go index 24aa246..19d5249 100644 --- a/rom.go +++ b/rom.go @@ -88,11 +88,11 @@ func (r *Nrom) Init(rom []byte) error { case 0x0: fmt.Println("Horizontal mirroring") ppu.Mirroring = MirroringHorizontal - ppu.Nametables.Init() + ppu.Nametables.Init() case 0x1: fmt.Println("Vertical mirroring") ppu.Mirroring = MirroringVertical - ppu.Nametables.Init() + ppu.Nametables.Init() } // ROM data dests at byte 16 @@ -101,9 +101,9 @@ func (r *Nrom) Init(rom []byte) error { fmt.Printf("PRG-ROM Count: %d\n", r.PrgBankCount) r.RomBanks = make([][]Word, (len(r.Data) / 0x4000)) - fmt.Printf("Length of banks: %d\n", (len(r.Data) / 0x4000)) + fmt.Printf("Length of banks: %d\n", (len(r.Data) / 0x4000)) - bankCount := (len(r.Data) / 0x4000) + bankCount := (len(r.Data) / 0x4000) for i := 0; i < bankCount; i++ { // Move 16kb chunk to 16kb bank bank := make([]Word, 0x4000) @@ -123,7 +123,7 @@ func (r *Nrom) Init(rom []byte) error { r.WriteVramBank(0x0000, 0x2000, 0x4000) } case 0x02: - r.WriteRamBank(0, 0x8000) + r.WriteRamBank(0, 0x8000) r.WriteRamBank(1, 0xC000) r.WriteVramBank(0x0000, 0x2000, 0x8000) } @@ -169,14 +169,14 @@ func (r *Mmc1) SetRegister(reg int, v int) { if (r.Mirroring & 0x2) == 0 { // TODO: Single screen mirroring ppu.Mirroring = MirroringSingleScreen - ppu.Nametables.Init() + ppu.Nametables.Init() fmt.Println("Single screen mirroring!") } else if (r.Mirroring & 0x1) != 0 { ppu.Mirroring = MirroringHorizontal - ppu.Nametables.Init() + ppu.Nametables.Init() } else { ppu.Mirroring = MirroringVertical - ppu.Nametables.Init() + ppu.Nametables.Init() } } @@ -283,11 +283,11 @@ func (r *Mmc1) Init(rom []byte) error { case 0x0: fmt.Println("Horizontal mirroring") ppu.Mirroring = MirroringHorizontal - ppu.Nametables.Init() + ppu.Nametables.Init() case 0x1: fmt.Println("Vertical mirroring") ppu.Mirroring = MirroringVertical - ppu.Nametables.Init() + ppu.Nametables.Init() } r.Data = rom[16:] @@ -322,11 +322,11 @@ func (r *Unrom) Init(rom []byte) error { case 0x0: fmt.Println("Horizontal mirroring") ppu.Mirroring = MirroringHorizontal - ppu.Nametables.Init() + ppu.Nametables.Init() case 0x1: fmt.Println("Vertical mirroring") ppu.Mirroring = MirroringVertical - ppu.Nametables.Init() + ppu.Nametables.Init() } // ROM data dests at byte 16 @@ -335,7 +335,7 @@ func (r *Unrom) Init(rom []byte) error { fmt.Printf("PRG-ROM Count: %d\n", r.PrgBankCount) - bankCount := (len(r.Data) / 0x4000) + bankCount := (len(r.Data) / 0x4000) for i := 0; i < bankCount; i++ { // Move 16kb chunk to 16kb bank bank := make([]Word, 0x4000) diff --git a/video.go b/video.go index ce711c4..310056f 100644 --- a/video.go +++ b/video.go @@ -11,7 +11,7 @@ import ( type Video struct { screen *sdl.Surface tick <-chan []int - debug <-chan []int + debug <-chan []int } func (v *Video) Init(t <-chan []int, d <-chan []int, n string) { @@ -28,13 +28,13 @@ func (v *Video) Init(t <-chan []int, d <-chan []int, n string) { sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "") v.tick = t - v.debug = d + v.debug = d } func (v *Video) Render() { for { select { - case d := <-v.debug: + case d := <-v.debug: copy((*[512 * 480]int)(v.screen.Pixels)[:], d) v.screen.Flip() // 60hz