OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [vga.c] - Diff between revs 1359 and 1368

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 1359 Rev 1368
Line 32... Line 32...
#include "vga.h"
#include "vga.h"
#include "abstract.h"
#include "abstract.h"
#include "sched.h"
#include "sched.h"
 
 
/* When this counter reaches config.vgas[].refresh_rate, a screenshot is taken and outputted */
/* When this counter reaches config.vgas[].refresh_rate, a screenshot is taken and outputted */
static struct {
struct vga_state {
  int pics;
  int pics;
  unsigned long ctrl, stat, htim, vtim;
  unsigned long ctrl, stat, htim, vtim;
  int vbindex;
  int vbindex;
  unsigned long vbar[2];
  unsigned long vbar[2];
  unsigned hlen, vlen;
  unsigned hlen, vlen;
  int pindex;
  int pindex;
  unsigned long palette[2][256];
  unsigned long palette[2][256];
} vga[MAX_VGAS];
  oraddr_t baseaddr;
 
  int refresh_rate;
 
  int irq;
 
  char *filename;
 
};
 
 
 
 
/* Write a register */
/* Write a register */
void vga_write32(oraddr_t addr, uint32_t value, void *dat)
void vga_write32(oraddr_t addr, uint32_t value, void *dat)
{
{
  int i, found = -1;
  struct vga_state *vga = dat;
 
 
  /* Find which controller this is */
  addr -= vga->baseaddr;
  for (i = 0; i < config.nvgas; i++ ) {
 
    if ((addr >= config.vgas[i].baseaddr) && (addr < config.vgas[i].baseaddr + VGA_ADDR_SPACE)) {
 
      found = i;
 
      break;
 
    }
 
  }
 
 
 
  if (found < 0) {
 
    fprintf( stderr, "vga_write32( 0x%"PRIxADDR" ): Out of range\n", addr);
 
    runtime.sim.cont_run = 0;
 
    return;
 
  }
 
 
 
  addr -= config.vgas[found].baseaddr;
 
 
 
  switch (addr) {
  switch (addr) {
    case VGA_CTRL:  vga[found].ctrl = value; break;
    case VGA_CTRL:  vga->ctrl = value; break;
    case VGA_STAT:  vga[found].stat = value; break;
    case VGA_STAT:  vga->stat = value; break;
    case VGA_HTIM:  vga[found].htim = value; break;
    case VGA_HTIM:  vga->htim = value; break;
    case VGA_VTIM:  vga[found].vtim = value; break;
    case VGA_VTIM:  vga->vtim = value; break;
    case VGA_HVLEN: vga[found].hlen = (value >> 16) + 2; vga[found].hlen = (value & 0xffff) + 2; break;
    case VGA_HVLEN: vga->hlen = (value >> 16) + 2; vga->hlen = (value & 0xffff) + 2; break;
    case VGA_VBARA: vga[found].vbar[0] = value; break;
    case VGA_VBARA: vga->vbar[0] = value; break;
    case VGA_VBARB: vga[found].vbar[1] = value; break;
    case VGA_VBARB: vga->vbar[1] = value; break;
    default:
    default:
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
        vga[found].palette[0][addr - VGA_CLUTA] = value & 0x00ffffff;
        vga->palette[0][addr - VGA_CLUTA] = value & 0x00ffffff;
      } else if (addr >= VGA_CLUTB) {
      } else if (addr >= VGA_CLUTB) {
        vga[found].palette[1][addr - VGA_CLUTB] = value & 0x00ffffff;
        vga->palette[1][addr - VGA_CLUTB] = value & 0x00ffffff;
      } else {
      } else {
        fprintf( stderr, "vga_write32( 0x%"PRIxADDR", 0x%08"PRIx32" ): Out of range\n", addr + config.vgas[found].baseaddr, value);
        fprintf( stderr, "vga_write32( 0x%"PRIxADDR", 0x%08"PRIx32" ): Out of range\n", addr + vga->baseaddr, value);
        runtime.sim.cont_run = 0;
        runtime.sim.cont_run = 0;
        return;
        return;
      }
      }
      break;
      break;
  }
  }
}
}
 
 
/* Read a register */
/* Read a register */
uint32_t vga_read32(oraddr_t addr, void *dat)
uint32_t vga_read32(oraddr_t addr, void *dat)
{
{
  int i, found = -1;
  struct vga_state *vga = dat;
 
 
  /* Find which controller this is */
 
  for (i = 0; i < config.nvgas; i++ ) {
 
    if ((addr >= config.vgas[i].baseaddr) && (addr < config.vgas[i].baseaddr + VGA_ADDR_SPACE)) {
 
      found = i;
 
      break;
 
    }
 
  }
 
 
 
  if (found < 0) {
 
    fprintf( stderr, "vga_read32( 0x%"PRIxADDR" ): Out of range\n", addr);
 
    runtime.sim.cont_run = 0;
 
    return 0;
 
  }
 
 
 
  addr -= config.vgas[found].baseaddr;
  addr -= vga->baseaddr;
 
 
  switch (addr) {
  switch (addr) {
    case VGA_CTRL:  return vga[found].ctrl;
    case VGA_CTRL:  return vga->ctrl;
    case VGA_STAT:  return vga[found].stat;
    case VGA_STAT:  return vga->stat;
    case VGA_HTIM:  return vga[found].htim;
    case VGA_HTIM:  return vga->htim;
    case VGA_VTIM:  return vga[found].vtim;
    case VGA_VTIM:  return vga->vtim;
    case VGA_HVLEN: return ((vga[found].hlen - 2) << 16) | (vga[found].vlen - 2);
    case VGA_HVLEN: return ((vga->hlen - 2) << 16) | (vga->vlen - 2);
    case VGA_VBARA: return vga[found].vbar[0];
    case VGA_VBARA: return vga->vbar[0];
    case VGA_VBARB: return vga[found].vbar[1];
    case VGA_VBARB: return vga->vbar[1];
    default:
    default:
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
        return vga[found].palette[0][addr - VGA_CLUTA];
        return vga->palette[0][addr - VGA_CLUTA];
      } else if (addr >= VGA_CLUTB) {
      } else if (addr >= VGA_CLUTB) {
        return vga[found].palette[1][addr - VGA_CLUTB];
        return vga->palette[1][addr - VGA_CLUTB];
      } else {
      } else {
        fprintf( stderr, "vga_read32( 0x%"PRIxADDR" ): Out of range\n", addr);
        fprintf( stderr, "vga_read32( 0x%"PRIxADDR" ): Out of range\n", addr);
        runtime.sim.cont_run = 0;
        runtime.sim.cont_run = 0;
        return 0;
        return 0;
      }
      }
Line 134... Line 110...
 
 
/* This code will only work on little endian machines */
/* This code will only work on little endian machines */
#ifdef __BIG_ENDIAN__
#ifdef __BIG_ENDIAN__
#warning Image dump not supported on big endian machines 
#warning Image dump not supported on big endian machines 
 
 
static int vga_dump_image (char *filename, int v)
static int vga_dump_image (char *filename, struct vga_start *vga)
{
{
  return 1;
  return 1;
}
}
 
 
#else 
#else 
Line 162... Line 138...
   unsigned int importantcolours;   /* Important colours         */
   unsigned int importantcolours;   /* Important colours         */
} INFOHEADER;
} INFOHEADER;
 
 
 
 
/* Dumps a bmp file, based on current image */
/* Dumps a bmp file, based on current image */
static int vga_dump_image (char *filename, int v)
static int vga_dump_image (char *filename, struct vga_state *vga)
{
{
  int sx = vga[v].hlen;
  int sx = vga->hlen;
  int sy = vga[v].vlen;
  int sy = vga->vlen;
  int i, x, y;
  int i, x, y;
  int pc = vga[v].ctrl & VGA_CTRL_PC;
  int pc = vga->ctrl & VGA_CTRL_PC;
  int rbpp = vga[v].ctrl & VGA_CTRL_CD;
  int rbpp = vga->ctrl & VGA_CTRL_CD;
  int bpp = rbpp >> 8;
  int bpp = rbpp >> 8;
 
 
  BMP_HEADER bh;
  BMP_HEADER bh;
  INFOHEADER ih;
  INFOHEADER ih;
  FILE *fo;
  FILE *fo;
 
 
  if (!sx || !sy) return;
  if (!sx || !sy) return 1;
 
 
  /* 16bpp and 32 bpp will be converted to 24bpp */
  /* 16bpp and 32 bpp will be converted to 24bpp */
  if (bpp == 1 || bpp == 3) bpp = 2;
  if (bpp == 1 || bpp == 3) bpp = 2;
 
 
  bh.type = 19778; /* BM */
  bh.type = 19778; /* BM */
Line 200... Line 176...
  if (!fwrite (&ih, sizeof (INFOHEADER), 1, fo)) return 1;
  if (!fwrite (&ih, sizeof (INFOHEADER), 1, fo)) return 1;
 
 
  if (pc) { /* Write palette? */
  if (pc) { /* Write palette? */
    for (i = 0; i < 256; i++) {
    for (i = 0; i < 256; i++) {
      unsigned long val, d;
      unsigned long val, d;
      d = vga[v].palette[vga[v].pindex][i];
      d = vga->palette[vga->pindex][i];
      val = (d >> 0) & 0xff;   /* Blue */
      val = (d >> 0) & 0xff;   /* Blue */
      val |= (d >> 8) & 0xff;  /* Green */
      val |= (d >> 8) & 0xff;  /* Green */
      val |= (d >> 16) & 0xff; /* Red */
      val |= (d >> 16) & 0xff; /* Red */
      if (!fwrite (&val, sizeof (val), 1, fo)) return 1;
      if (!fwrite (&val, sizeof (val), 1, fo)) return 1;
    }
    }
Line 213... Line 189...
  /* Data is stored upside down */
  /* Data is stored upside down */
  for (y = sy - 1; y >= 0; y--) {
  for (y = sy - 1; y >= 0; y--) {
    int align = 4 - ((bpp + 1) * sx) % 4;
    int align = 4 - ((bpp + 1) * sx) % 4;
    int zero = 0;
    int zero = 0;
    for (x = 0; x < sx; x++) {
    for (x = 0; x < sx; x++) {
      unsigned long pixel = evalsim_mem32 (vga[v].vbar[vga[v].vbindex] + (y * sx + x) * (bpp + 1));
      unsigned long pixel = evalsim_mem32 (vga->vbar[vga->vbindex] + (y * sx + x) * (bpp + 1));
      if (!fwrite (&pixel, sizeof (pixel), 1, fo)) return 1;
      if (!fwrite (&pixel, sizeof (pixel), 1, fo)) return 1;
    }
    }
    if (!fwrite (&zero, align, 1, fo)) return 1;
    if (!fwrite (&zero, align, 1, fo)) return 1;
  }
  }
 
 
  fclose (fo);
  fclose (fo);
  return 0;
  return 0;
}
}
#endif /* !__BIG_ENDIAN__ */
#endif /* !__BIG_ENDIAN__ */
 
 
void vga_job (int param)
void vga_job (void *dat)
{
{
 
  struct vga_state *vga = dat;
  /* dump the image? */
  /* dump the image? */
  char temp[STR_SIZE];
  char temp[STR_SIZE];
  sprintf (temp, "%s%04i.bmp", config.vgas[param].filename, vga[param].pics++);
  sprintf (temp, "%s%04i.bmp", vga->filename, vga->pics++);
  vga_dump_image (temp, param);
  vga_dump_image (temp, vga);
 
 
  SCHED_ADD(vga_job, param, runtime.sim.cycles + config.vgas[param].refresh_rate);
  SCHED_ADD(vga_job, dat, runtime.sim.cycles + vga->refresh_rate);
}
}
 
 
/* Reset all VGAs */
/* Reset all VGAs */
void vga_reset ()
void vga_reset (void *dat)
{
{
  int i, j;
  struct vga_state *vga = dat;
  for (i = 0; i < config.nvgas; i++) {
 
 
  int i;
 
 
    /* Init palette */
    /* Init palette */
    for (j = 0; j < 256; j++)
  for (i = 0; i < 256; i++)
      vga[i].palette[0][j] = vga[i].palette[1][j] = 0;
    vga->palette[0][i] = vga->palette[1][i] = 0;
 
 
    vga[i].ctrl = vga[i].stat = vga[i].htim = vga[i].vtim = 0;
  vga->ctrl = vga->stat = vga->htim = vga->vtim = 0;
    vga[i].hlen = vga[i].vlen = 0;
  vga->hlen = vga->vlen = 0;
    vga[i].vbar[0] = vga[i].vbar[1] = 0;
  vga->vbar[0] = vga->vbar[1] = 0;
 
 
    /* Init screen dumping machine */
    /* Init screen dumping machine */
    vga[i].pics = 0;
  vga->pics = 0;
 
 
    vga[i].pindex = 0;
  vga->pindex = 0;
    vga[i].vbindex = 0;
  vga->vbindex = 0;
 
 
    if (config.vgas[i].baseaddr)
  SCHED_ADD(vga_job, dat, runtime.sim.cycles + vga->refresh_rate);
      register_memoryarea(config.vgas[i].baseaddr, VGA_ADDR_SPACE, 4, 0, vga_read32, vga_write32, NULL);
 
    SCHED_ADD(vga_job, i, runtime.sim.cycles + config.vgas[i].refresh_rate);
 
  }
 
}
}
 
 
/*----------------------------------------------------[ VGA Configuration ]---*/
/*----------------------------------------------------[ VGA Configuration ]---*/
void vga_nvgas(union param_val val, void *dat)
 
{
 
  if (val.int_val >= 0 && val.int_val < MAX_VGAS)
 
    config.nvgas = val.int_val;
 
  else
 
    CONFIG_ERROR("invalid number of devices.");
 
}
 
 
 
void vga_baseaddr(union param_val val, void *dat)
void vga_baseaddr(union param_val val, void *dat)
{
{
  if (current_device >= 0 && current_device < config.nvgas)
  struct vga_state *vga = dat;
    config.vgas[current_device].baseaddr = val.addr_val;
  vga->baseaddr = val.addr_val;
  else
 
    CONFIG_ERROR("invalid device number.");
 
}
}
 
 
void vga_irq(union param_val val, void *dat)
void vga_irq(union param_val val, void *dat)
{
{
  if (current_device >= 0 && current_device < config.nvgas)
  struct vga_state *vga = dat;
    config.vgas[current_device].irq = val.int_val;
  vga->irq = val.int_val;
  else
 
    CONFIG_ERROR("invalid device number.");
 
}
}
 
 
void vga_refresh_rate(union param_val val, void *dat)
void vga_refresh_rate(union param_val val, void *dat)
{
{
  if (current_device >= 0 && current_device < config.nvgas)
  struct vga_state *vga = dat;
    config.vgas[current_device].refresh_rate = val.int_val;
  vga->refresh_rate = val.int_val;
  else
 
    CONFIG_ERROR("invalid device number.");
 
}
}
 
 
void vga_filename(union param_val val, void *dat)
void vga_filename(union param_val val, void *dat)
{
{
  if (current_device >= 0 && current_device < config.nvgas)
  struct vga_state *vga = dat;
    strcpy (config.vgas[current_device].filename, val.str_val);
  if(!(vga->filename = strdup (val.str_val)));
  else
}
    CONFIG_ERROR("invalid device number.");
 
 
void *vga_sec_start(void)
 
{
 
  struct vga_state *new = malloc(sizeof(struct vga_state));
 
 
 
  if(!new) {
 
    fprintf(stderr, "Peripheral VGA: Run out of memory\n");
 
    exit(-1);
 
  }
 
 
 
  new->baseaddr = 0;
 
 
 
  return new;
 
}
 
 
 
void vga_sec_end(void *dat)
 
{
 
  struct vga_state *vga = dat;
 
 
 
  if (vga->baseaddr)
 
    register_memoryarea(vga->baseaddr, VGA_ADDR_SPACE, 4, 0, vga_read32, vga_write32, dat);
 
 
 
  reg_sim_reset(vga_reset, dat);
}
}
 
 
void reg_vga_sec(void)
void reg_vga_sec(void)
{
{
  struct config_section *sec = reg_config_sec("vga", NULL, NULL);
  struct config_section *sec = reg_config_sec("vga", vga_sec_start, vga_sec_end);
 
 
  reg_config_param(sec, "nvgas", paramt_int, vga_nvgas);
 
  reg_config_param(sec, "device", paramt_int, change_device);
 
  reg_config_param(sec, "baseaddr", paramt_addr, vga_baseaddr);
  reg_config_param(sec, "baseaddr", paramt_addr, vga_baseaddr);
  reg_config_param(sec, "irq", paramt_int, vga_irq);
  reg_config_param(sec, "irq", paramt_int, vga_irq);
  reg_config_param(sec, "refresh_rate", paramt_int, vga_refresh_rate);
  reg_config_param(sec, "refresh_rate", paramt_int, vga_refresh_rate);
  reg_config_param(sec, "filename", paramt_str, vga_filename);
  reg_config_param(sec, "filename", paramt_str, vga_filename);
  reg_config_param(sec, "enddevice", paramt_none, end_device);
 
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.