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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_68/] [or1ksim/] [peripheral/] [fb.c] - Blame information for rev 647

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 645 markom
/* fb.c -- Simple frame buffer
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdio.h>
21
#include "sim-config.h"
22
#include "abstract.h"
23
#include "fb.h"
24
 
25
static unsigned char buffer[FB_SIZEY * FB_SIZEX];
26
static unsigned long pal[256];
27
static int fb_pic;
28
static int fb_cycles;
29
 
30 647 markom
/* Variable to screen buffer.  */
31
struct dev_memarea *buf_area;
32
static unsigned long fb_addr;
33
 
34 645 markom
/* Write a register */
35
void fb_buf_write8 (unsigned long addr, unsigned long value)
36
{
37 647 markom
  int a = addr - fb_addr;
38 645 markom
  if (a < 0 || a >= FB_SIZEY * FB_SIZEX) {
39
    fprintf (stderr, "Write out of screen buffer (0x%08x)!\n", addr);
40
    cont_run = 0;
41
  } else buffer[a] = value;
42
}
43
 
44
/* Read a register */
45
unsigned long fb_buf_read8 (unsigned long addr)
46
{
47 647 markom
  int a = (addr - fb_addr);
48 645 markom
  if (a < 0 || a >= FB_SIZEY * FB_SIZEX) {
49
    fprintf (stderr, "Read out of screen buffer (0x%08x)!\n", addr);
50
    cont_run = 0;
51
    return 0;
52
  } else return buffer[a];
53
}
54
 
55 647 markom
static void change_buf_addr (unsigned long addr)
56
{
57
  unsigned long size_mask = bit_mask (FB_SIZEX * FB_SIZEY);
58
  unsigned long addr_mask = ~size_mask;
59
  buf_area->addr_mask = addr_mask;
60
  buf_area->addr_compare = addr & addr_mask;
61
  fb_addr = addr;
62
}
63
 
64
/* Write a register */
65
void fb_write32 (unsigned long addr, unsigned long value)
66
{
67
  int a = (addr - config.fb.baseaddr);
68
  switch (a) {
69
    case FB_CTRL:    break;
70
    case FB_BUFADDR: change_buf_addr (value); break;
71
    default:
72
      a -= FB_PAL;
73
      a /= 4;
74
      if (a < 0 || a >= 256) {
75
        fprintf (stderr, "Write out of palette buffer (0x%08x)!\n", addr);
76
        cont_run = 0;
77
      } else pal[a] = value;
78
      break;
79
  }
80
}
81
 
82 645 markom
/* Read a register */
83 647 markom
unsigned long fb_read32 (unsigned long addr)
84 645 markom
{
85 647 markom
  int a = (addr - config.fb.baseaddr);
86
  switch (a) {
87
    case FB_CTRL:    return 0; break;
88
    case FB_BUFADDR: return fb_addr; break;
89
    default:
90
      a -= FB_PAL;
91
      a /= 4;
92
      if (a < 0 || a >= 256) {
93
        fprintf (stderr, "Read out of palette buffer (0x%08x)!\n", addr);
94
        cont_run = 0;
95
        return 0;
96
      } else return pal[a];
97
  }
98 645 markom
}
99
 
100
/* define these also for big endian */
101
#define CNV16(x) (x)
102
#define CNV32(x) (x)
103
 
104
/* Dumps a bmp file, based on current image */
105
static int fb_dump_image (char *filename)
106
{
107
  int sx = FB_SIZEX;
108
  int sy = FB_SIZEY;
109
  int i, x, y;
110
  FILE *fo;
111
 
112
  unsigned short int u16;
113
  unsigned long int u32;
114
 
115
  if (config.sim.verbose) printf ("Creating %s...", filename);
116
  fo = fopen (filename, "wb+");
117
  u16 = CNV16(19778); /* BM */
118
  if (!fwrite (&u16, 2, 1, fo)) return 1;
119
  u32 = CNV32(14 + 40 + sx * sy + 1024); /* size */
120
  if (!fwrite (&u32, 4, 1, fo)) return 1;
121
  u32 = CNV32(0); /* reserved */
122
  if (!fwrite (&u32, 4, 1, fo)) return 1;
123
  u32 = 14 + 40 + 1024; /* offset */
124
  if (!fwrite (&u32, 4, 1, fo)) return 1;
125
 
126
  u32 = CNV32(40); /* header size */
127
  if (!fwrite (&u32, 4, 1, fo)) return 1;
128
  u32 = CNV32(sx); /* width */
129
  if (!fwrite (&u32, 4, 1, fo)) return 1;
130
  u32 = CNV32(sy); /* height */
131
  if (!fwrite (&u32, 4, 1, fo)) return 1;
132
  u16 = CNV16(1);  /* planes */
133
  if (!fwrite (&u16, 2, 1, fo)) return 1;
134
  u16 = CNV16(8);  /* bits */
135
  if (!fwrite (&u16, 2, 1, fo)) return 1;
136
  u32 = CNV32(0);  /* compression */
137
  if (!fwrite (&u32, 4, 1, fo)) return 1;
138
  u32 = CNV32(x * y); /* image size */
139
  if (!fwrite (&u32, 4, 1, fo)) return 1;
140
  u32 = CNV32(0);  /* x resolution */
141
  if (!fwrite (&u32, 4, 1, fo)) return 1;
142
  u32 = CNV32(0);  /* y resolution */
143
  if (!fwrite (&u32, 4, 1, fo)) return 1;
144
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
145
  if (!fwrite (&u32, 4, 1, fo)) return 1;
146
  u32 = CNV32(0);  /* important colours; all are important */
147
  if (!fwrite (&u32, 4, 1, fo)) return 1;
148
 
149
  for (i = 0; i < 256; i++) {
150
    unsigned long val, d;
151
    d = pal[i];
152 647 markom
    val = ((d >> 12) & 0xf) << 0 + 4;   /* Blue */
153
    val |= ((d >> 8) & 0xf) << 8 + 4;  /* Green */
154
    val |= ((d >> 4) & 0xf) << 16 + 4; /* Red */
155 645 markom
    val = CNV32(pal[i]);
156
    if (!fwrite (&val, 4, 1, fo)) return 1;
157
  }
158
 
159
  if (config.sim.verbose) printf ("(%i,%i)", sx, sy);
160
  /* Data is stored upside down */
161
  for (y = sy - 1; y >= 0; y--) {
162
    int align = (4 - sx) % 4;
163
    int zero = CNV32(0);
164
    while (align < 0) align += 4;
165
    for (x = 0; x < sx; x++)
166
      fputc (buffer[y * sx + x], fo);
167
    if (align && !fwrite (&zero, align, 1, fo)) return 1;
168
  }
169
 
170
  if (config.sim.verbose) printf ("DONE\n");
171
  fclose (fo);
172
  return 0;
173
}
174
 
175
/* Reset all VGAs */
176
void fb_reset ()
177
{
178
  int i;
179 647 markom
 
180 645 markom
  if (config.fb.enabled) {
181
    fb_pic = 0;
182
    fb_cycles = 0;
183
 
184
    for (i = 0; i < 256; i++)
185
      pal[i] = (i << 16) | (i << 8) | (i << 0);
186 647 markom
 
187
    register_memoryarea(config.fb.bufaddr, FB_SIZEX * FB_SIZEY, 1, fb_buf_read8, fb_buf_write8);
188
    buf_area = cur_area;
189
 
190
    if (config.fb.baseaddr)
191
      register_memoryarea(config.fb.baseaddr, FB_PAL + 256*4, 4, fb_read32, fb_write32);
192 645 markom
  }
193
}
194
 
195
/* Handles one VGA clock */
196
void fb_clock ()
197
{
198
  /* dump the image? */
199
  if (fb_cycles++ >= config.fb.refresh_rate) {
200
    char temp[STR_SIZE];
201
    sprintf (temp, "%s%04i.bmp", &config.fb.filename[0], fb_pic);
202
    fb_dump_image (temp);
203
    fb_cycles = 0;
204
    fb_pic++;
205
  }
206
}

powered by: WebSVN 2.1.0

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