static void system1_draw_bg(osd_bitmap bitmap, int priority) {
        int sx, sy, offs;

        background_scrollx = ((system1_scroll_x.read(0) >> 1) + ((system1_scroll_x.read(1) & 1) << 7) + 14) & 0xff;
        background_scrolly = (-system1_scroll_y.read()) & 0xff;

        if (priority == -1) {
            /* optimized far background */

            /* for every character in the background video RAM, check if it has
             * been modified since last time and update it accordingly.
             */
            for (offs = 0; offs < system1_backgroundram_size[0]; offs += 2) {
                if (bg_dirtybuffer[offs / 2] != 0) {
                    int code, color;

                    bg_dirtybuffer[offs / 2] = 0;

                    code = (system1_backgroundram.read(offs) | (system1_backgroundram.read(offs + 1) << 8));
                    code = ((code >> 4) & 0x800) | (code & 0x7ff);	/* Heavy Metal only */

                    color = ((code >> 5) & 0x3f) + 0x40;
                    sx = (offs / 2) % 32;
                    sy = (offs / 2) / 32;

                    drawgfx(bitmap1, Machine.gfx[0],
                            code,
                            color,
                            0, 0,
                            8 * sx, 8 * sy,
                            null, TRANSPARENCY_NONE, 0);
                }
            }

            /* copy the temporary bitmap to the screen */
            copyscrollbitmap(bitmap, bitmap1, 1, new int[]{background_scrollx}, 1, new int[]{background_scrolly}, Machine.drv.visible_area, TRANSPARENCY_NONE, 0);
        } else {
            priority <<= 3;

            for (offs = 0; offs < system1_backgroundram_size[0]; offs += 2) {
                if ((system1_backgroundram.read(offs + 1) & 0x08) == priority) {
                    int code, color;

                    code = (system1_backgroundram.read(offs) | (system1_backgroundram.read(offs + 1) << 8));
                    code = ((code >> 4) & 0x800) | (code & 0x7ff);	/* Heavy Metal only */

                    color = ((code >> 5) & 0x3f) + 0x40;
                    sx = (offs / 2) % 32;
                    sy = (offs / 2) / 32;

                    sx = 8 * sx + background_scrollx;
                    sy = 8 * sy + background_scrolly;

                    drawgfx(bitmap, Machine.gfx[0],
                            code,
                            color,
                            0, 0,
                            sx, sy,
                            Machine.drv.visible_area, TRANSPARENCY_PEN, 0);
                    if (sx > 248) {
                        drawgfx(bitmap, Machine.gfx[0],
                                code,
                                color,
                                0, 0,
                                sx - 256, sy,
                                Machine.drv.visible_area, TRANSPARENCY_PEN, 0);
                    }
                    if (sy > 248) {
                        drawgfx(bitmap, Machine.gfx[0],
                                code,
                                color,
                                0, 0,
                                sx, sy - 256,
                                Machine.drv.visible_area, TRANSPARENCY_PEN, 0);
                        if (sx > 248) {
                            drawgfx(bitmap, Machine.gfx[0],
                                    code,
                                    color,
                                    0, 0,
                                    sx - 256, sy - 256,
                                    Machine.drv.visible_area, TRANSPARENCY_PEN, 0);
                        }
                    }
                }
            }
        }
    }