public static void toaplan2_videoram_w(int offset, int data, int controller) {
        int oldword = 0;
        int videoram_offset;
        int dirty_cell;

        switch (toaplan2_voffs[controller] & 0xfc00) {
            case 0x0400:
            case 0x0000:
                videoram_offset = bg_offs[controller] & (TOAPLAN2_BG_VRAM_SIZE - 1);
                oldword = bgvideoram[controller].READ_WORD(videoram_offset);
                if (data != oldword) {
                    bgvideoram[controller].WRITE_WORD(videoram_offset, data);
                    dirty_cell = (bg_offs[controller] & (TOAPLAN2_BG_VRAM_SIZE - 3)) / 2;
                    tilemap_mark_tile_dirty(bg_tilemap[controller], (dirty_cell % 64) / 2, dirty_cell / 64);
                }
                bg_offs[controller] += 2;
                if (bg_offs[controller] > TOAPLAN2_BG_VRAM_SIZE) {
                    if (errorlog != null) {
                        fprintf(errorlog, "Writing %04x to out of range BG Layer address (%08x)  Video controller %01x  !!!\n", data, bg_offs[controller], controller);
                    }
                }
                break;
            case 0x0c00:
            case 0x0800:
                videoram_offset = fg_offs[controller] & (TOAPLAN2_FG_VRAM_SIZE - 1);
                oldword = fgvideoram[controller].READ_WORD(videoram_offset);
                if (data != oldword) {
                    fgvideoram[controller].WRITE_WORD(videoram_offset, data);
                    dirty_cell = (fg_offs[controller] & (TOAPLAN2_FG_VRAM_SIZE - 3)) / 2;
                    tilemap_mark_tile_dirty(fg_tilemap[controller], (dirty_cell % 64) / 2, dirty_cell / 64);
                }
                fg_offs[controller] += 2;
                if (fg_offs[controller] > TOAPLAN2_FG_VRAM_SIZE) {
                    if (errorlog != null) {
                        fprintf(errorlog, "Writing %04x to out of range FG Layer address (%08x)  Video controller %01x  !!!\n", data, fg_offs[controller], controller);
                    }
                }
                break;
            case 0x1400:
            case 0x1000:
                videoram_offset = top_offs[controller] & (TOAPLAN2_TOP_VRAM_SIZE - 1);
                oldword = topvideoram[controller].READ_WORD(videoram_offset);
                if (data != oldword) {
                    topvideoram[controller].WRITE_WORD(videoram_offset, data);
                    dirty_cell = (top_offs[controller] & (TOAPLAN2_TOP_VRAM_SIZE - 3)) / 2;
                    tilemap_mark_tile_dirty(top_tilemap[controller], (dirty_cell % 64) / 2, dirty_cell / 64);
                }
                top_offs[controller] += 2;
                if (top_offs[controller] > TOAPLAN2_TOP_VRAM_SIZE) {
                    if (errorlog != null) {
                        fprintf(errorlog, "Writing %04x to out of range TOP Layer address (%08x)  Video controller %01x  !!!\n", data, top_offs[controller], controller);
                    }
                }
                break;
            case 0x1800:
                videoram_offset = sprite_offs[controller] & (TOAPLAN2_SPRITERAM_SIZE - 1);
                spriteram_new[controller].WRITE_WORD(videoram_offset, data);
                sprite_offs[controller] += 2;
                if (sprite_offs[controller] > TOAPLAN2_SPRITERAM_SIZE) {
                    if (errorlog != null) {
                        fprintf(errorlog, "Writing %04x to out of range Sprite address (%08x)  Video controller %01x  !!!\n", data, sprite_offs[controller], controller);
                    }
                }
                break;
            default:
                toaplan2_unk_vram = data;
                if (errorlog != null) {
                    fprintf(errorlog, "Hmmm, writing %04x to unknown video layer (%08x)  Video controller %01x  \n", toaplan2_unk_vram, toaplan2_voffs[controller], controller);
                }
                break;
        }
    }