#include "xbasic_types.h"
#include "xio.h"

#include "xintc_l.h"
#include "xuartlite_l.h"

#include <stdio.h>
#include <fcntl.h>

#define W 640
#define H 480
#define VGA_START 0x00800000
#define RED 0xE0
#define GREEN 0x1C
#define BLUE 0x03
#define CHAR_ROM_START VGA_START
#define PRG_ROM_START VGA_START + 0x00001000
#define CHAR_ROM_LENGTH 8196
#define PRG_ROM_LENGTH 16384
#define CHAR_ROM_NAME "rgb.CHR"
#define PRG_ROM_NAME "rgb.PRG"

int main()
{

  int fd;
  int i;
  char buf[4];

  /* read char rom */
  /* first open the file */
  if((fd = open(CHAR_ROM_NAME, O_RDONLY)) = -1) {
    perror("can't open char rom");
    exit(1);
  }
  for(i = 0; i < CHAR_ROM_LENGTH; i += 4) {
    /* read 4 bytes from the file and print out an error if the read fails */
    if(read(fd, buf, sizeof(buf)) == -1) {
      perror("error reading char rom");
      exit(1);
    }
    /* output the 4 bytes to sram */
    XIo_Out32(CHAR_ROM_START + i, char_to_long(buf));
  }
  close(fd);


  /* read prg rom */
  if((fd = open(PRG_ROM_NAME, O_RDONLY)) = -1) {
    perror("can't open prg rom");
    exit(1);
  }
  for(i = 0; i < PRG_ROM_LENGTH; i += 4) {
    if(read(fd, buf, sizeof(buf)) == -1) {
      perror("error reading prg rom");
      exit(1);
    }
    XIo_Out32(PRG_ROM_START + i, char_to_long(buf));
  }
  close(fd);

  
}
