     ###########################################################
 
     readVROM: (address) ->
-        @rom[(@$getVROMBase address) | (@$getVROMOffset address)]
+        @rom[@$getVROMBase(address) | @$getVROMOffset(address)]
 
     getVROMBase: (address) ->
         if address < 0x1000

        @writesCount = 0   # Counts 5 writes to fill shift register.

    resetBankRegisters: ->
        @romBankRegister = 0x0   # 5-bit
        @vromBank1Register = 0x0 # 5-bit
        @vromBank1Register = 0x0 # 5-bit

    resetVariables: ->
        @lowerROMBankBase = 0x0000         # 16K $8000-$BFFF (default first ROM bank in 256k)

            @setMirroring Mirroring.SINGLE_SCREEN

    switchROMBank: (controll) ->
        pageBase = @getROMPage(controll) * 0x100000 # Base address of one of 256K pages (for 512K and 1024K roms)
        if controll & 0x08
            if controll & 0x04
                @lowerROMBankBase = pageBase + @romBankSelect * 0x4000 # Selected 16K ROM bank
                @upperROMBankBase = pageBase + 0x0F * 0x4000           # Last 16K ROM bank from 256K
            else
                @lowerROMBankBase = pageBase                           # First 16K ROM bank
                @upperROMBankBase = pageBase + @romBankSelect * 0x4000 # Selected 16K ROM bank
        else
            @lowerROMBankBase = pageBase + (@romBankSelect & 0x0E) * 0x4000 # Selected 32K ROM bank (first part)
            @upperROMBankBase = @lowerROMBankBase + 0x4000                  # Selected 32K ROM bank (second part)

    getROMPage: (controll) ->
        if @rom.length <= 0x100000
            0                                 # First (and only one) 256K page on 256K ROM
        else if @rom.length <= 0x200000
            @romPageSelect1                   # First or second 256K page on 512K ROM
        else if controll & 0x10
            @romPageSelect2 | @romPageSelect1 # One of four 256K pages on 1024K ROM
        else
            @romPageSelect1 << 1              # First or third 256K page on 1024K ROM

    switchVROMBank: (controll) ->
        if controll & 0x10
            @lowerVROMBankBase = @vromBankSelect1 * 0x1000 # Select 4K VROM bank
            @upperVROMBankBase = @vromBankSelect2 * 0x1000 # Select 4K VROM bank
        else
            @lowerVROMBankBase = (@vromBankSelect1 & 0x0E) * 0x1000 # Select 8K VROM bank (first part)
            @upperVROMBankBase = @lowerVROMBankBase + 0x1000        # Select 8K VROM bank (second part)

    writeVROMBank1Register: (value) ->
        @vromBankSelect1 = value & 0x0F
        @romPageSelect1 = (value & 0x10) >> 4

    writeVROMBank2Register: (value) ->
        @vromBankSelect2 = value & 0x0F
        @romPageSelect2 = (value & 0x10) >> 3

    writeROMBankRegister: (value) ->
        @romBankSelect = value & 0x0F

    ###########################################################
    # ROM reading
    ###########################################################

    readROM: (address) ->
        @rom[@$getROMBase(address) | @$getROMOffset(address)]

    getROMBase: (address) ->
        if address < 0xC000
