         value
 
     copyHorizontalScrollBits: ->
+        @fineXScroll = @tempFineXScroll
         @vramAddress = (@vramAddress & 0x7BE0) | (@tempAddress & 0x041F) # V[10,4-0] = T[10,4-0]
 
     copyVerticalScrollBits: ->

    isRenderingEnabled: ->
        @spritesVisible or @backgroundVisible

    startFrame: ->
        @framePosition = 0
        @fetchCurrentPattern()
        @fetchCurrentAttribute()
        @fetchCurrentPalette()

    renderFramePixel: ->
        backgroundColor = @renderBackgroundPixel() if @backgroundVisible
        spriteColor = @renderSpritePixel() if @spritesVisible
        @setFramePixel backgroundColor or 0 # TODO rendering priority

    # Colors are saved at addresses with structure 0111.1111.000S.PPCC.
    #    S = 0 for background, 1 for sprites
    #   PP = palette number

    #    X = bit 1 of XXXXX

    renderBackgroundPixel: ->
        @fetchPattern() if @fineXScroll is 0 # When coarse scroll X was incremented.
        bitNumber = @fineXScroll ^ 0x07 # 7 - fineXScroll
        colorSelect1 =  (@patternLayer1Row >>> bitNumber) & 0x01
        colorSelect2 = ((@patternLayer2Row >>> bitNumber) & 0x01) << 1
        0x3F00 | @paletteSelect | colorSelect2 | colorSelect1

    fetchPattern: ->
        @fetchPalette() if (@vramAddress & 0x0001) is 0 # When coarse scroll was incremented by 2
        patternNumer = @read 0x2000 | @vramAddress & 0x0FFF
        patternTableAddress = @backgroundPatternTableIndex << 12
        patternAddress = patternTableAddress + (patternNumer << 4)
        fineYScroll = (@vramAddress >>> 12) & 0x07
        @patternLayer1Row = @read patternAddress + fineYScroll
        @patternLayer2Row = @read patternAddress + fineYScroll + 8

    fetchPalette: ->
        @fetchAttribute() if (@vramAddress & 0x0002) is 0 # When coarse scroll was incremented by 4
        areaSelect = (@vramAddress >>> 4) & 0x04 | @vramAddress & 0x02
        @paletteSelect = ((@attribute >>> areaSelect) & 0x03) << 2

    fetchAttribute: ->
        attributeTableAddress = 0x23C0 | @vramAddress & 0x0C00
        attributeNumber = (@vramAddress >>> 4) & 0x38 | (@vramAddress >>> 2) & 0x07
        @attribute = @read attributeTableAddress + attributeNumber

    ###########################################################
    # Sprite rendering
    ###########################################################

    renderSpritePixel: ->
        # TODO rewrite and optimize this
        @fetchSprites() if @cycle is 1
        @spriteZeroRendered = false
        currentX = @cycle
        currentY = @scanline
        heightMask = if @bigSprites then 0x0F else 0x07
        for address in @spriteAddresses
            innerX = currentX - @objectAttributeMemory[address + 3]
            if 0 <= innerX <= 7
                innerY = currentY - @objectAttributeMemory[address]
                tile = @objectAttributeMemory[address + 1]
                attributes = @objectAttributeMemory[address + 2]
                innerX ^= 0x07 if not (attributes & 0x40)
                innerY ^= heightMask if attributes & 0x80
                patternTableIndex = if @bigSprites then tile & 0x01 else @spritesPatternTableIndex
                patternTableAddress = patternTableIndex << 12
                patternNumber = tile
                if innerY >= 8
                    patternNumber++
                    innerY -= 8
                patternAddress = patternTableAddress + (patternNumber << 4)
                patternLayer1Row = @read patternAddress + innerY
                patternLayer2Row = @read patternAddress + innerY + 8
                colorSelect1 =  (patternLayer1Row >>> innerX) & 0x01
                colorSelect2 = ((patternLayer2Row >>> innerX) & 0x01) << 1
                colorSelect = colorSelect2 | colorSelect1
                if colorSelect
                    @spriteZeroRendered ||= address is 0
                    @spriteInFront = (attributes & 0x20) is 0
                    paletteSelect = (attributes & 0x03) << 2
                    return 0x10 | paletteSelect | colorSelect
        return 0

    fetchSprites: ->
        @spriteScalineOverflow = 0
        @spriteAddresses = []
        bottomY = @scanline
        topY = Math.max 0, @scanline - (if @bigSprites then 16 else 8)
        for spriteY, address in @objectAttributeMemory by 4
            if topY < spriteY <= bottomY
                @spriteAddresses.push address
                if @spriteAddresses.length >= 8
                    @spriteScalineOverflow = 1
                    return

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