#include "graphics.hpp"

#include "scene/scene.hpp"
#include "scene/tracker/scene.hpp"
#include "scene/logo/scene.hpp"
#include "scene/emulator/scene.hpp"

#include "sh7091/sh7091_bits.hpp"

namespace scene {
  const scene scenes[] = {
    [id::tracker]  = tracker::scene,
    [id::logo]     = logo::scene,
    [id::emulator] = emulator::scene,
  };

  const scene * current_scene = nullptr;

  void scene_init(int scene_id)
  {
    current_scene = &scenes[scene_id];
    graphics_scene_init(&current_scene->opb_size);
    printf("scene init\n");

    if (current_scene->init != nullptr) {
      printf("current_scene init\n");
      current_scene->init();
    }
  }

  void transition()
  {
    assert(current_scene->done != nullptr);
    int scene_id = current_scene->done();
    if (scene_id >= 0) {
      printf("disable interrupt\n");
      uint32_t sr;
      asm volatile ("stc sr,%0" : "=r" (sr));
      sr |= sh::sr::imask(15);
      asm volatile ("ldc %0,sr" : : "r" (sr));

      scene_init(scene_id);

      printf("enable interrupt\n");
      sr &= ~sh::sr::imask(15);
      asm volatile ("ldc %0,sr" : : "r" (sr));
    }
  }
}
