         0 <= @scanline <= 239 and 1 <= @cycle <= 256
 
     renderFramePixel: ->
+        @firstPixelIsRendered = @scanline is 0 and @cycle is 1
         xPosition = @cycle - 1
         yPosition = @scanline
         spriteColor = @renderSpritePixel() if @spritesVisible
         backgroundColor = @renderBackgroundPixel() if @backgroundVisible
         # TODO get RGB values of color and put them into framebuffer
+        @incrementFineYScroll() if @cycle is 256
         @incrementFineXScroll()
 
     renderBackgroundPixel: ->
-        # Optimization - we compute this only when coarse scroll X changes.
-        if @fineXScroll is 0
-            # VRAM address regiater following structure: $0yyy.NNYY.YYYX.XXXX
+        # Optimization - we compute this only when coarse scroll X was incremented.
+        if @fineXScroll is 0 or @firstPixelIsRendered
+            # VRAM address register following structure: $0yyy.NNYY.YYYX.XXXX
             #   yyy = fine Y scroll (y position of active row of rendered tile)
             #    NN = index of active name table
             # YYYYY = coarse Y scroll (y position of rendered tile in name table)

            attributeAddress = attributeTableAddress + attributeNumber
            attribute = @read attributeAddress
            # Attribute has format $3322.1100. where 00, 11, 22, 33 are two upper color bits
            # for one of 2x2 subarea of 4x4 tile area.
            # We have to find in which subarea are we in. This is decided by bit 1 of YYYYY and XXXXX values.
            subareaNumber = (@vramAddress >>> 5) & 0x02 | (@vramAddress >>> 1) & 0x01
            @colorBits43 = ((attribute >>> subareaNumber) & 0x03) << 2

        @scanline = -1 if @scanline is 262
        @vblankInProgress = 241 <= @scanline <= 260
        @cpu.nonMaskableInterrupt() if @scanline is 241 and @vblankGeneratesNMI
        @frameAvailable = true if @scanline is 241

    readFrame: ->
        @frameAvailable = false
        @framebuffer

    isFrameAvailable: ->
        @frameAvailable

    isRenderingInProgress: ->
        not @vblankInProgress and (@spritesVisible or @backgroundVisible)

module.exports = PPU
