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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [peripheral/] [fb.c] - Diff between revs 1359 and 1369

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

Rev 1359 Rev 1369
Line 16... Line 16...
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
 
#include <stdio.h>
#include <stdio.h>
 
#include <string.h>
 
 
#include "config.h"
#include "config.h"
 
 
#ifdef HAVE_INTTYPES_H
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#include <inttypes.h>
Line 32... Line 33...
#include "fb.h"
#include "fb.h"
#include "sched.h"
#include "sched.h"
 
 
#define FB_WRAP (512*1024)
#define FB_WRAP (512*1024)
 
 
static unsigned long pal[256];
struct fb_state {
static int fb_ctrl = 0;
  unsigned long pal[256];
static int fb_pic = 0;
  int ctrl;
static int fb_in_refresh = 1;
  int pic;
static int fb_refresh_count = 0;
  int in_refresh;
static oraddr_t fb_addr = 0;
  int refresh_count;
static oraddr_t cam_addr = 0;
  oraddr_t addr;
static int camerax = 0, cameray = 0, camera_pos = 0;
  oraddr_t cam_addr;
 
  int camerax;
 
  int cameray;
 
  int camera_pos;
 
  oraddr_t baseaddr;
 
  int refresh;
 
  int refresh_rate;
 
  char *filename;
 
};
 
 
static void change_buf_addr (oraddr_t addr)
static void change_buf_addr (struct fb_state *fb, oraddr_t addr)
{
{
  fb_addr = addr;
  fb->addr = addr;
}
}
 
 
/* Write a register */
/* Write a register */
void fb_write32 (oraddr_t addr, uint32_t value, void *dat)
void fb_write32 (oraddr_t addr, uint32_t value, void *dat)
{
{
  int a = (addr - config.fb.baseaddr);
  struct fb_state *fb = dat;
 
 
 
  oraddr_t a = (addr - fb->baseaddr);
 
 
  switch (a) {
  switch (a) {
    case FB_CTRL:    fb_ctrl = value; break;
    case FB_CTRL:    fb->ctrl = value; break;
    case FB_BUFADDR: change_buf_addr (value); break;
    case FB_BUFADDR: change_buf_addr (fb, value); break;
    case FB_CAMBUFADDR: cam_addr = value; break;
    case FB_CAMBUFADDR: fb->cam_addr = value; break;
    case FB_CAMPOSADDR: camera_pos = value;
    case FB_CAMPOSADDR: fb->camera_pos = value;
                     camerax = value % FB_SIZEX;
                     fb->camerax = value % FB_SIZEX;
                     cameray = value / FB_SIZEX;
                     fb->cameray = value / FB_SIZEX;
                     break;
                     break;
    default:
    default:
      a -= FB_PAL;
      a -= FB_PAL;
      a /= 4;
      a /= 4;
      if (a < 0 || a >= 256) {
      if (a < 0 || a >= 256) {
        fprintf (stderr, "Write out of palette buffer (0x%"PRIxADDR")!\n", addr);
        fprintf (stderr, "Write out of palette buffer (0x%"PRIxADDR")!\n", addr);
        runtime.sim.cont_run = 0;
        runtime.sim.cont_run = 0;
      } else pal[a] = value;
      } else fb->pal[a] = value;
      break;
      break;
  }
  }
}
}
 
 
/* Read a register */
/* Read a register */
oraddr_t fb_read32 (oraddr_t addr, void *dat)
oraddr_t fb_read32 (oraddr_t addr, void *dat)
{
{
  int a = (addr - config.fb.baseaddr);
  struct fb_state *fb = dat;
 
 
 
  oraddr_t a = (addr - fb->baseaddr);
 
 
  switch (a) {
  switch (a) {
    case FB_CTRL:
    case FB_CTRL:
      return fb_ctrl & ~0xff000000| (fb_in_refresh ? 0x80000000 : 0) | ((unsigned long)(fb_refresh_count & 0x7f) << 24);
      return fb->ctrl & ~0xff000000| (fb->in_refresh ? 0x80000000 : 0) | ((unsigned long)(fb->refresh_count & 0x7f) << 24);
      break;
      break;
    case FB_BUFADDR: return fb_addr; break;
    case FB_BUFADDR: return fb->addr; break;
    case FB_CAMBUFADDR: return cam_addr; break;
    case FB_CAMBUFADDR: return fb->cam_addr; break;
    case FB_CAMPOSADDR: return camera_pos; break;
    case FB_CAMPOSADDR: return fb->camera_pos; break;
    default:
    default:
      a -= FB_PAL;
      a -= FB_PAL;
      a /= 4;
      a /= 4;
      if (a < 0 || a >= 256) {
      if (a < 0 || a >= 256) {
        fprintf (stderr, "Read out of palette buffer (0x%"PRIxADDR")!\n", addr);
        fprintf (stderr, "Read out of palette buffer (0x%"PRIxADDR")!\n", addr);
        runtime.sim.cont_run = 0;
        runtime.sim.cont_run = 0;
        return 0;
        return 0;
      } else return pal[a];
      } else return fb->pal[a];
  }
  }
}
}
 
 
/* define these also for big endian */
/* define these also for big endian */
#if __BIG_ENDIAN__
#if __BIG_ENDIAN__
Line 107... Line 122...
#define CNV16(x) (x)
#define CNV16(x) (x)
#define CNV32(x) (x)
#define CNV32(x) (x)
#endif
#endif
 
 
/* Dumps a bmp file, based on current image */
/* Dumps a bmp file, based on current image */
static int fb_dump_image8 (char *filename)
static int fb_dump_image8 (struct fb_state *fb, char *filename)
{
{
  int sx = FB_SIZEX;
  int sx = FB_SIZEX;
  int sy = FB_SIZEY;
  int sy = FB_SIZEY;
  int i, x, y;
  int i, x, y;
  FILE *fo;
  FILE *fo;
Line 153... Line 168...
  u32 = CNV32(0);  /* important colours; all are important */
  u32 = CNV32(0);  /* important colours; all are important */
  if (!fwrite (&u32, 4, 1, fo)) return 1;
  if (!fwrite (&u32, 4, 1, fo)) return 1;
 
 
  for (i = 0; i < 256; i++) {
  for (i = 0; i < 256; i++) {
    unsigned long val, d;
    unsigned long val, d;
    d = pal[i];
    d = fb->pal[i];
#if 1
#if 1
    val = ((d >> 0) & 0x1f) << 3;   /* Blue */
    val = ((d >> 0) & 0x1f) << 3;   /* Blue */
    val |= ((d >> 5) & 0x3f) << 10;  /* Green */
    val |= ((d >> 5) & 0x3f) << 10;  /* Green */
    val |= ((d >> 11) & 0x1f) << 19; /* Red */
    val |= ((d >> 11) & 0x1f) << 19; /* Red */
#else 
#else 
Line 172... Line 187...
    int align = (4 - sx) % 4;
    int align = (4 - sx) % 4;
    int zero = CNV32(0);
    int zero = CNV32(0);
    int add;
    int add;
    while (align < 0) align += 4;
    while (align < 0) align += 4;
    for (x = 0; x < sx; x++) {
    for (x = 0; x < sx; x++) {
      add = (fb_addr & ~(FB_WRAP - 1)) | ((fb_addr + y * sx + x) & (FB_WRAP - 1));
      add = (fb->addr & ~(FB_WRAP - 1)) | ((fb->addr + y * sx + x) & (FB_WRAP - 1));
      fputc (evalsim_mem8 (add), fo);
      fputc (evalsim_mem8 (add), fo);
    }
    }
    if (align && !fwrite (&zero, align, 1, fo)) return 1;
    if (align && !fwrite (&zero, align, 1, fo)) return 1;
  }
  }
 
 
Line 184... Line 199...
  fclose (fo);
  fclose (fo);
  return 0;
  return 0;
}
}
 
 
/* Dumps a bmp file, based on current image */
/* Dumps a bmp file, based on current image */
static int fb_dump_image24 (char *filename)
static int fb_dump_image24 (struct fb_state *fb, char *filename)
{
{
  int sx = FB_SIZEX;
  int sx = FB_SIZEX;
  int sy = FB_SIZEY;
  int sy = FB_SIZEY;
  int x, y;
  int x, y;
  FILE *fo;
  FILE *fo;
Line 233... Line 248...
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
  /* Data is stored upside down */
  /* Data is stored upside down */
  for (y = sy - 1; y >= 0; y--) {
  for (y = sy - 1; y >= 0; y--) {
    unsigned char line[FB_SIZEX][3];
    unsigned char line[FB_SIZEX][3];
    for (x = 0; x < sx; x++)
    for (x = 0; x < sx; x++)
      if (y >= cameray && x >= camerax && y < cameray + CAM_SIZEY && x < camerax + CAM_SIZEX) {
      if (y >= fb->cameray && x >= fb->camerax && y < fb->cameray + CAM_SIZEY && x < fb->camerax + CAM_SIZEX) {
        int add = (cam_addr + (x - camerax + (y - cameray) * CAM_SIZEX) * 2) ^ 2;
        int add = (fb->cam_addr + (x - fb->camerax + (y - fb->cameray) * CAM_SIZEX) * 2) ^ 2;
        unsigned short d = evalsim_mem16 (add);
        unsigned short d = evalsim_mem16 (add);
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
      } else {
      } else {
        int add = (fb_addr & ~(FB_WRAP - 1)) | ((fb_addr + y * sx + x) & (FB_WRAP - 1));
        int add = (fb->addr & ~(FB_WRAP - 1)) | ((fb->addr + y * sx + x) & (FB_WRAP - 1));
        unsigned short d = pal[evalsim_mem8 (add)];
        unsigned short d = fb->pal[evalsim_mem8 (add)];
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
      }
      }
    if(!fwrite (line, sizeof(line), 1, fo)) return 1;
    if(!fwrite (line, sizeof(line), 1, fo)) return 1;
Line 254... Line 269...
  if (config.sim.verbose) PRINTF ("DONE\n");
  if (config.sim.verbose) PRINTF ("DONE\n");
  fclose (fo);
  fclose (fo);
  return 0;
  return 0;
}
}
 
 
void fb_job (int param)
void fb_job (void *dat)
{
{
  if (param) {
  struct fb_state *fb = dat;
 
 
 
  if (fb->refresh) {
    /* dump the image? */
    /* dump the image? */
    if (fb_ctrl & 1) {
    if (fb->ctrl & 1) {
      char temp[STR_SIZE];
      char temp[STR_SIZE];
      sprintf (temp, "%s%04i.bmp", &config.fb.filename[0], fb_pic);
      sprintf (temp, "%s%04i.bmp", fb->filename, fb->pic);
      if (fb_ctrl & 2) fb_dump_image24 (temp);
      if (fb->ctrl & 2) fb_dump_image24 (fb, temp);
      else fb_dump_image8 (temp);
      else fb_dump_image8 (fb, temp);
      fb_pic++;
      fb->pic++;
    }
    }
    SCHED_ADD(fb_job, 0, runtime.sim.cycles + config.fb.refresh_rate - config.fb.refresh_rate / REFRESH_DIVIDER);
    SCHED_ADD(fb_job, dat, runtime.sim.cycles + fb->refresh_rate - fb->refresh_rate / REFRESH_DIVIDER);
    fb_in_refresh = 0;
    fb->in_refresh = 0;
 
    fb->refresh = 0;
  } else {
  } else {
    fb_refresh_count++;
    fb->refresh_count++;
    SCHED_ADD(fb_job, 1, runtime.sim.cycles + config.fb.refresh_rate / REFRESH_DIVIDER);
    fb->refresh = 1;
 
    SCHED_ADD(fb_job, dat, runtime.sim.cycles + fb->refresh_rate / REFRESH_DIVIDER);
  }
  }
}
}
 
 
/* Reset all VGAs */
/* Reset all VGAs */
void fb_reset ()
void fb_reset (void *dat)
{
{
 
  struct fb_state *fb = dat;
  int i;
  int i;
 
 
  if (config.fb.enabled) {
  fb->pic = 0;
    fb_pic = 0;
  fb->addr = 0;
    fb_addr = 0;
  fb->ctrl = 0;
    fb_ctrl = 0;
 
 
 
    for (i = 0; i < 256; i++)
    for (i = 0; i < 256; i++)
      pal[i] = (i << 16) | (i << 8) | (i << 0);
    fb->pal[i] = (i << 16) | (i << 8) | (i << 0);
 
 
    if (config.fb.baseaddr)
  SCHED_ADD(fb_job, dat, runtime.sim.cycles + fb->refresh_rate);
      register_memoryarea(config.fb.baseaddr, FB_PAL + 256*4, 4, 0, fb_read32, fb_write32, NULL);
  fb->refresh = 0;
    SCHED_ADD(fb_job, 0, runtime.sim.cycles + config.fb.refresh_rate);
 
  }
 
}
}
 
 
/*-----------------------------------------------------[ FB configuration ]---*/
/*-----------------------------------------------------[ FB configuration ]---*/
void fb_enabled(union param_val val, void *dat)
 
{
 
  config.fb.enabled = val.int_val;
 
}
 
 
 
void fb_baseaddr(union param_val val, void *dat)
void fb_baseaddr(union param_val val, void *dat)
{
{
  config.fb.baseaddr = val.addr_val;
  struct fb_state *fb = dat;
 
  fb->baseaddr = val.addr_val;
}
}
 
 
void fb_refresh_rate(union param_val val, void *dat)
void fb_refresh_rate(union param_val val, void *dat)
{
{
  config.fb.refresh_rate = val.int_val;
  struct fb_state *fb = dat;
 
  fb->refresh_rate = val.int_val;
}
}
 
 
void fb_filename(union param_val val, void *dat)
void fb_filename(union param_val val, void *dat)
{
{
  strcpy(config.fb.filename, val.str_val);
  struct fb_state *fb = dat;
 
  if(!(fb->filename = strdup(val.str_val))) {
 
    fprintf(stderr, "Peripheral FB: Run out of memory\n");
 
    exit(-1);
 
  }
 
}
 
 
 
void *fb_sec_start(void)
 
{
 
  struct fb_state *new = malloc(sizeof(struct fb_state));
 
 
 
  if(!new) {
 
    fprintf(stderr, "Peripheral FB: Run out of memory\n");
 
    exit(-1);
 
  }
 
 
 
  new->baseaddr = 0;
 
  new->ctrl = 0;
 
  new->pic = 0;
 
  new->in_refresh = 1;
 
  new->refresh_count = 0;
 
  new->addr = 0;
 
  new->cam_addr = 0;
 
  new->camerax = 0;
 
  new->cameray = 0;
 
  new->camera_pos = 0;
 
 
 
  return new;
 
}
 
 
 
void fb_sec_end(void *dat)
 
{
 
  struct fb_state *fb = dat;
 
 
 
  if (fb->baseaddr)
 
    register_memoryarea(fb->baseaddr, FB_PAL + 256*4, 4, 0, fb_read32, fb_write32, dat);
 
 
 
  reg_sim_reset(fb_reset, dat);
}
}
 
 
void reg_fb_sec(void)
void reg_fb_sec(void)
{
{
  struct config_section *sec = reg_config_sec("fb", NULL, NULL);
  struct config_section *sec = reg_config_sec("fb", fb_sec_start, fb_sec_end);
 
 
  reg_config_param(sec, "enabled", paramt_int, fb_enabled);
 
  reg_config_param(sec, "baseaddr", paramt_addr, fb_baseaddr);
  reg_config_param(sec, "baseaddr", paramt_addr, fb_baseaddr);
  reg_config_param(sec, "refresh_rate", paramt_int, fb_refresh_rate);
  reg_config_param(sec, "refresh_rate", paramt_int, fb_refresh_rate);
  reg_config_param(sec, "filename", paramt_str, fb_filename);
  reg_config_param(sec, "filename", paramt_str, fb_filename);
}
}
 
 
 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.