#include <sys/types.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>

#include "voodoo2.h"

struct name_int {
  const char * name;
  uint32_t value;
};

void print_registers(struct voodoo2_reg * reg)
{
  const struct name_int registers[] = {
    {"status", reg->status},
    {"fbiInit0", reg->fbiInit0},
    {"fbiInit1", reg->fbiInit1},
    {"fbiInit2", reg->fbiInit2},
    {"fbiInit3", reg->fbiInit3},
    {"fbiInit4", reg->fbiInit4},
    {"fbiInit5", reg->fbiInit5},
    {"fbiInit6", reg->fbiInit6},
    {"fbiInit7", reg->fbiInit7},
    //
    {"backPorch", reg->backPorch},
    {"videoDimensions", reg->videoDimensions},
    {"hSync", reg->hSync},
    {"vSync", reg->vSync},
  };

  for (int i = 0; i < (sizeof (registers)) / (sizeof (registers[0])); i++) {
    printf("%s %08x\n", registers[i].name, registers[i].value);
  }
}

int main()
{
  const char * filename = "/sys/bus/pci/devices/0000:02:0a.0/resource0";

  int fd = open(filename, O_RDWR | O_SYNC);
  assert(fd >= 0);

  uint32_t map_size = 0x4000;

  off_t target_base = 0;
  void * map_base = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target_base);
  assert(map_base != MAP_FAILED);

  struct voodoo2_reg * reg = (struct voodoo2_reg *)map_base;
  print_registers(reg);

  close(fd);
}
