     ###########################################################
 
     writeScroll: (value) ->
-        @writeAddress value # Uses the same register as VRAM addressing
+        # 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: ->
-        @fineXScroll = (@fineXScroll + 1) & 0x07
-        @incrementCoarseXScroll() if @fineXScroll is 0 # Fine X scroll overflow
+        if @fineXScroll is 7
+            @fineXScroll = 0
+            @incrementCoarseXScroll()
+        else
+            @fineXScroll++
 
     incrementCoarseXScroll: ->
-        coarseXScroll =   @vramAddress         & 0x1E # Bits 0-4
-        coarseYScroll =  (@vramAddress >>>  5) & 0x1E # Bits 5-9
-        nameTableIndex = (@vramAddress >>> 10) & 0x03 # Bits 10,11
-        fineYScroll =    (@vramAddress >>> 12) & 0x07 # Bits 12-14
-
-        coarseXScroll = (coarseXScroll + 1) & 0x1E
-        if coarseXScroll is 0 # Coarse X scroll overflow (incrementation from 31)
-            @selectedNameTableIndex ^= 0x01 # Switch horizontal name table
-            @fineYScroll = (@fineYScroll + 1) & 0x07
-            if @fineYScroll is 30 # Fine X scroll overflow (incrementation from 29)
-                @fineYScroll = 0
-                @coarseYScroll = (@coarseYScroll + 1) & 0x1E
-                if @coarseYScroll is 0 # Coarse X scroll overflow
-                    @selectedNameTableIndex ^= 0x02 # Switch vertical name table
-
-        @vramAddress = coarseXScroll        | # Bits 0-4
-                       coarseYScroll  <<  5 | # Bits 5-9
-                       nameTableIndex << 10 | # Bits 10,11
-                       fineYScroll    << 12   # Bits 12-14
+        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

        0 <= @scanline <= 239 and 1 <= @cycle <= 256

    renderFramePixel: ->
        xPosition = @cycle - 1
        yPosition = @scanline
        spriteColor = @renderSpritePixel() if @spritesVisible
        backgroundColor = @renderBackgroundPixel() if @backgroundVisible
        # TODO get RGB values of color and put them into framebuffer
        @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
            #   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
        @frameAvailable = true if @scanline is 241

    readFrame: ->
        @frameAvailable = false
        @framebuffer

    isFrameAvailable: ->
        @frameAvailable

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

module.exports = PPU
