static void draw_sprites(osd_bitmap bitmap, int controller, int priority_to_display) {
        GfxElement gfx = Machine.gfx[((controller * 2) + 1)];
        rectangle clip = Machine.drv.visible_area;

        UShortPtr source = new UShortPtr(spriteram_now[controller]);

        int offs;
        for (offs = 0; offs < (TOAPLAN2_SPRITERAM_SIZE / 2); offs += 4) {
            int attrib, sprite, color, priority, flipx, flipy, sx, sy;
            int sprite_sizex, sprite_sizey, temp_x, temp_y, sx_base, sy_base;

            attrib = source.read(offs);
            priority = (attrib & 0x0f00) >> 8;

            if ((priority == priority_to_display) && (attrib & 0x8000) != 0) {
                sprite = ((attrib & 3) << 16) | source.read(offs + 1);
                /* 18 bit */
                color = (attrib >> 2) & 0x3f;

                /**
                 * **** find out sprite size *****
                 */
                sprite_sizex = ((source.read(offs + 2) & 0x0f) + 1) * 8;
                sprite_sizey = ((source.read(offs + 3) & 0x0f) + 1) * 8;

                /**
                 * **** find position to display sprite *****
                 */
                sx_base = (source.read(offs + 2) >> 7) - sprite_scrollx[controller];
                sy_base = (source.read(offs + 3) >> 7) - sprite_scrolly[controller];

                flipx = attrib & TOAPLAN2_SPRITE_FLIPX;
                flipy = attrib & TOAPLAN2_SPRITE_FLIPY;

                if (flipx != 0) {
                    sx_base -= 7;
                    /**
                     * **** wrap around sprite position *****
                     */
                    if (sx_base >= 0x1c0) {
                        sx_base -= 0x200;
                    }
                } else if (sx_base >= 0x180) {
                    sx_base -= 0x200;
                }

                if (flipy != 0) {
                    sy_base -= 7;
                    if (sy_base >= 0x1c0) {
                        sy_base -= 0x200;
                    }
                } else if (sy_base >= 0x180) {
                    sy_base -= 0x200;
                }

                /**
                 * **** flip the sprite layer in any active X or Y flip *****
                 */
                if (sprite_flip[controller] != 0) {
                    if ((sprite_flip[controller] & TOAPLAN2_SPRITE_FLIPX) != 0) {
                        sx_base = 320 - sx_base;
                    }
                    if ((sprite_flip[controller] & TOAPLAN2_SPRITE_FLIPY) != 0) {
                        sy_base = 240 - sy_base;
                    }
                }

                /**
                 * **** cancel flip, if it and sprite layer flip are active *****
                 */
                flipx = (flipx ^ (sprite_flip[controller] & TOAPLAN2_SPRITE_FLIPX));
                flipy = (flipy ^ (sprite_flip[controller] & TOAPLAN2_SPRITE_FLIPY));

                for (temp_y = 0; temp_y < sprite_sizey; temp_y += 8) {
                    if (flipy != 0) {
                        sy = sy_base - temp_y;
                    } else {
                        sy = sy_base + temp_y;
                    }
                    for (temp_x = 0; temp_x < sprite_sizex; temp_x += 8) {
                        if (flipx != 0) {
                            sx = sx_base - temp_x;
                        } else {
                            sx = sx_base + temp_x;
                        }

                        drawgfx(bitmap, gfx, sprite,
                                color,
                                flipx, flipy,
                                sx, sy,
                                clip, TRANSPARENCY_PEN, 0);

                        sprite++;
                    }
                }
            }
        }
    }