         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)

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

    readFrame: ->
        @framebuffer

    isFrameAvailable: ->
        true

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

    getMonochromeColorIndex: (index) ->
        index & 0x30    

module.exports = PPU

    ###########################################################

    writeScroll: (value) ->
        # Uses the same register as VRAM addressing. 
        # The register has the 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)
        # XXXXX = coarse X scroll (x position of rendered tile in name table)
        @writeAddress value 

    incrementFineXScroll: ->
        if @fineXScroll is 7
            @fineXScroll = 0
            @incrementCoarseXScroll()
        else
            @fineXScroll++

    incrementCoarseXScroll: ->
        if (@vramAddress & 0x001F) is 0x001F # if coarseScrollX is 31
            @vramAddress &= 0xFFE0           #     coarseScrollX = 0
            @vramAddress ^= 0x0400           #     nameTableBit0 = not nameTableBit0
        else                                 # else
            @vramAddress += 0x0001           #     coarseScrollX++

    incrementFineYScroll: ->
        if (@vramAddress & 0x7000) is 0x7000 # if fineScrollY is 7
            @vramAddress &= 0x0FFF           #     fineScrollY = 0
            @incrementCoarseYScroll()        #     incrementCoarseYScroll()
        else                                 # else
            @vramAddress += 0x1000           #     fineScrollY++

    incrementCoarseYScroll: ->
        if (@vramAddress & 0x03E0) is 0x03E0      # if coarseScrollY is 31
            @vramAddress &= 0xFC1F                #     coarseScrollY = 0
        else if (@vramAddress & 0x03E0) is 0x03A0 # else if coarseScrollY is 29
            @vramAddress &= 0xFC1F                #     coarseScrollY = 0
            @vramAddress ^= 0x0800                #     nameTableBit1 = not nameTableBit1
        else                                      # else
            @vramAddress += 0x0020                #     coarseScrollY++

    ###########################################################
    # VRAM access
