/*
 * Sega Saturn cartridge flash tool
 * by Anders Montonen, 2012
 *
 * Original software by ExCyber
 * Graphics routines by Charles MacDonald
 *
 * Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
 */
#include <stdint.h>

#include "vdp2.h"

void vdp_init(void)
{
    int ii;
    volatile uint16_t *vdp2_vram = (volatile uint16_t *)VDP2_VRAM;
    volatile uint16_t *vdp2_cram = (volatile uint16_t *)VDP2_CRAM;
    TVMD = 0x0000;

    RAMCTL = RAMCTL & (~0x3000);

    // Map Offset Register: Bitmap screen will be located at VRAM offset 0
    MPOFN = 0;

    // Character Control Register: 256 colors, enable NBG0 as a bitmap
    // |8 for 1024x256 bitmap
    CHCTLA = 0x0012|8;

    // Screen Scroll Value Registers: No scroll
    SCXIN0 = 0;
    SCXDN0 = 0;
    SCYIN0 = 0;

    // Screen Display Enable Register: Invalidate the transparency code for
    // NBG0 and display NBG0
    BGON = 0x0001;

    /* Clear VRAM */
    for (ii = 0; ii < 0x40000; ii++)
        vdp2_vram[ii] = 0x0000;

    /* Clear CRAM */
    for (ii = 0; ii < 0x0800; ii++)
        vdp2_cram[ii] = 0x0000;
}
