 cycleFlagsTable[i]    |= F_SHIFT_BG for i in [1..336] when not (256 < i < 321)
 scanlineFlagsTable[i] |= F_SHIFT_BG for i in [0..239]
 
-#cycleFlagsTable[65]   |= F_EVAL_SP
-cycleFlagsTable[257]  |= F_EVAL_SP
+cycleFlagsTable[65]   |= F_EVAL_SP
 scanlineFlagsTable[i] |= F_EVAL_SP for i in [0..261] when not (239 < i < 261)
 
 cycleFlagsTable[i]    |= F_CLIP_LEFT for i in [1..8]

    #   C = color palette number
    # Byte 3 = x screen coordinate

    prerenderSprites: ->
        for i in [0...@spriteScanline.length]
            @spriteScanline[i] = null
            @spriteScanlinePixels[i] = 0
        for spriteNumber in [0...@spriteCount]
            sprite = @secondaryOAM[spriteNumber]
            for columnNumber in [0..7]
                x = sprite.x + columnNumber + 1
                break if x > FRAME_BUFFER_WIDTH
                continue if x < 1 or @spriteScanline[x]
                columnNumber ^= 0x07 unless sprite.horizontalFlip
                colorBit0 = ((sprite.patternRow0 >>> columnNumber) & 1)
                colorBit1 = ((sprite.patternRow1 >>> columnNumber) & 1) << 1
                colorNumber = colorBit1 | colorBit0
                if colorNumber
                    @spriteScanlinePixels[x] = sprite.paletteNumber | colorNumber
                    @spriteScanline[x] = sprite
        undefined

    renderSpritePixel: ->
        if @$isSpritePixelInvisible()
            0
        else
            @renderedSprite = @spriteScanline[@cycle]
            @spriteScanlinePixels[@cycle]

    isSpritePixelInvisible: ->
        not @spritesVisible or @spriteClipping and (@cycleFlags & F_CLIP_LEFT)

    evaluateSprites: ->
        @spriteNumber = 0
        @spriteCount = 0
        @spriteScalineOverflow = 0
        height = if @bigSprites then 16 else 8
        bottomY = @scanline
        topY = Math.max 0, bottomY - height
        for spriteY, address in @primaryOAM by 4 when topY < spriteY <= bottomY
            sprite = @secondaryOAM[@spriteCount++]
            sprite.address = address
            sprite.height = height
            sprite.rowNumber = bottomY - spriteY
            if @spriteCount is 8
                @spriteScalineOverflow = 1
                break
        undefined

    fetchSpriteLow: ->
        if @spriteNumber < @spriteCount
            @sprite = @secondaryOAM[@spriteNumber]
            address = @sprite.address
            patternNumber = @primaryOAM[address + 1]
            patternTableAddress = @spPatternTableAddress
            if @bigSprites
                patternTableAddress = (patternNumber & 1) << 12
                patternNumber &= 0xFE
            attributes = @primaryOAM[address + 2]
            rowNumber = @sprite.rowNumber
            height = @sprite.height
            rowNumber = height - rowNumber - 1 if attributes & 0x80 # Vertical flip
            if rowNumber >= 8
                rowNumber -= 8
                patternNumber++
            patternAddress = patternTableAddress + (patternNumber << 4)
            @addressBus = patternAddress + rowNumber
            @sprite.x = @primaryOAM[address + 3]
            @sprite.horizontalFlip = attributes & 0x40
            @sprite.paletteNumber = 0x10 | (attributes & 0x03) << 2
            @sprite.inFront = (attributes & 0x20) is 0
            @sprite.zeroSprite = address is 0
            @sprite.patternRow0 = @ppuMemory.read @addressBus
        else
            @sprite = null

    fetchSpriteHigh: ->
        if @sprite
            @addressBus += 8
            @sprite.patternRow1 = @ppuMemory.read @addressBus
            @spriteNumber++

    ###########################################################
    # Debug rendering

        @resetVariables()

    resetOAM: ->
        @primaryOAM = (0 for [0...0x100])       # Sprite data - 256B (64 x 4B sprites)
        @secondaryOAM = (new Sprite for [0..7]) # Sprite data for rendered scanline (up to 8 sprites)

    resetRegisters: ->
        @setControl 0           #  8-bit PPUCTRL register
        @setMask 0              #  8-bit PPUMASK register
        @setStatus 0            #  8-bit PPUSTATUS register
        @oamAddress = 0         # 15-bit OAMADDR register
        @tempAddress = 0        # 15-bit 'Loopy T' register
        @vramAddress = 0        # 15-bit 'Loopy V' register
        @vramReadBuffer = 0     #  8-bit VRAM read buffer
        @writeToogle = 0        #  1-bit 'Loopy W' register
        @fineXScroll = 0        #  3-bit 'Loopy X' register
        @patternBuffer0 = 0     # 16-bit pattern (bit 0) shift buffer
        @patternBuffer1 = 0     # 16-bit pattern (bit 1) shift buffer
        @paletteBuffer0 = 0     #  8-bit palette (bit 0) shift buffer
        @paletteBuffer1 = 0     #  8-bit palette (bit 1) shift buffer
        @paletteLatch0 = 0      #  1-bit palette (bit 0) latch register
        @paletteLatch1 = 0      #  1-bit palette (bit 1) latch register
        @patternBufferNext0 = 0 # Next value for pattern (bit 0) shift buffer
        @patternBufferNext1 = 0 # Next value for pattern (bit 1) shift buffer
        @paletteLatchNext0 = 0  # Next value for palette (bit 0) latch register
        @paletteLatchNext1 = 0  # Next value for palette (bit 1) latch register

    resetVariables: ->
        @scanline = 261         # Current scanline - from total 262 scanlines (0..261)
        @cycle = 0              # Current cycle - from total 341 cycles per scanline (0..340)
        @cycleFlags = 0         # Flags for current cycle/scanline
        @suppressVBlank = false # Whether to suppress VBlank flag setting
        @supressNMI = false     # Whether to supress NMI generation
        @nmiDelay = 0           # Number of cycles after which NMI is generated
        @oddFrame = false       # Whether odd frame is being rendered
        @renderedSprite = null  # Currently rendered sprite
        @spriteCount = 0        # Total number of sprites on current scanline
        @spriteNumber = 0       # Number of currently fetched sprite
        @spriteCache = (0 for [0..261])         # Preprocesed sprite data for current scanline (cycle -> sprite rendered on that cycle)
        @spritePixelCache = (null for [0..261]) # Prerendered sprite pixels for current scanline (cycle -> sprite pixel rendered on that cycle)

    ###########################################################
    # External configuration
