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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [fb.c] - Blame information for rev 997

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 805 markom
#include "sched.h"
25 645 markom
 
26 799 simons
#define FB_WRAP (512*1024)
27
 
28 645 markom
static unsigned long pal[256];
29 648 markom
static int fb_ctrl = 0;
30
static int fb_pic = 0;
31 845 markom
static int fb_in_refresh = 1;
32
static int fb_refresh_count = 0;
33 648 markom
static unsigned long fb_addr = 0;
34 859 markom
static unsigned long cam_addr = 0;
35
static int camerax = 0, cameray = 0, camera_pos = 0;
36 645 markom
 
37 647 markom
static void change_buf_addr (unsigned long addr)
38
{
39
  fb_addr = addr;
40
}
41
 
42
/* Write a register */
43
void fb_write32 (unsigned long addr, unsigned long value)
44
{
45
  int a = (addr - config.fb.baseaddr);
46
  switch (a) {
47 846 markom
    case FB_CTRL:    fb_ctrl = value; break;
48 647 markom
    case FB_BUFADDR: change_buf_addr (value); break;
49 859 markom
    case FB_CAMBUFADDR: cam_addr = value; break;
50
    case FB_CAMPOSADDR: camera_pos = value;
51
                     camerax = value % FB_SIZEX;
52
                     cameray = value / FB_SIZEX;
53
                     break;
54 647 markom
    default:
55
      a -= FB_PAL;
56
      a /= 4;
57
      if (a < 0 || a >= 256) {
58
        fprintf (stderr, "Write out of palette buffer (0x%08x)!\n", addr);
59 884 markom
        runtime.sim.cont_run = 0;
60 647 markom
      } else pal[a] = value;
61
      break;
62
  }
63
}
64
 
65 645 markom
/* Read a register */
66 647 markom
unsigned long fb_read32 (unsigned long addr)
67 645 markom
{
68 647 markom
  int a = (addr - config.fb.baseaddr);
69
  switch (a) {
70 845 markom
    case FB_CTRL:
71 846 markom
      return fb_ctrl & ~0xff000000| (fb_in_refresh ? 0x80000000 : 0) | ((unsigned long)(fb_refresh_count & 0x7f) << 24);
72 845 markom
      break;
73 647 markom
    case FB_BUFADDR: return fb_addr; break;
74 859 markom
    case FB_CAMBUFADDR: return cam_addr; break;
75
    case FB_CAMPOSADDR: return camera_pos; break;
76 647 markom
    default:
77
      a -= FB_PAL;
78
      a /= 4;
79
      if (a < 0 || a >= 256) {
80
        fprintf (stderr, "Read out of palette buffer (0x%08x)!\n", addr);
81 884 markom
        runtime.sim.cont_run = 0;
82 647 markom
        return 0;
83
      } else return pal[a];
84
  }
85 645 markom
}
86
 
87
/* define these also for big endian */
88 859 markom
#if __BIG_ENDIAN__
89
#define CNV32(x) (x) (\
90
     ((((x) >> 24) & 0xff) << 0)\
91
   | ((((x) >> 16) & 0xff) << 8)\
92
   | ((((x) >> 8) & 0xff) << 16)\
93
   | ((((x) >> 0) & 0xff) << 24))
94
#define CNV16(x) (x) (\
95
   | ((((x) >> 8) & 0xff) << 0)\
96
   | ((((x) >> 0) & 0xff) << 8))
97
#else
98 645 markom
#define CNV16(x) (x)
99
#define CNV32(x) (x)
100 859 markom
#endif
101 645 markom
 
102
/* Dumps a bmp file, based on current image */
103 859 markom
static int fb_dump_image8 (char *filename)
104 645 markom
{
105
  int sx = FB_SIZEX;
106
  int sy = FB_SIZEY;
107
  int i, x, y;
108
  FILE *fo;
109
 
110
  unsigned short int u16;
111
  unsigned long int u32;
112
 
113 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
114 645 markom
  fo = fopen (filename, "wb+");
115
  u16 = CNV16(19778); /* BM */
116
  if (!fwrite (&u16, 2, 1, fo)) return 1;
117
  u32 = CNV32(14 + 40 + sx * sy + 1024); /* size */
118
  if (!fwrite (&u32, 4, 1, fo)) return 1;
119
  u32 = CNV32(0); /* reserved */
120
  if (!fwrite (&u32, 4, 1, fo)) return 1;
121
  u32 = 14 + 40 + 1024; /* offset */
122
  if (!fwrite (&u32, 4, 1, fo)) return 1;
123
 
124
  u32 = CNV32(40); /* header size */
125
  if (!fwrite (&u32, 4, 1, fo)) return 1;
126
  u32 = CNV32(sx); /* width */
127
  if (!fwrite (&u32, 4, 1, fo)) return 1;
128
  u32 = CNV32(sy); /* height */
129
  if (!fwrite (&u32, 4, 1, fo)) return 1;
130
  u16 = CNV16(1);  /* planes */
131
  if (!fwrite (&u16, 2, 1, fo)) return 1;
132
  u16 = CNV16(8);  /* bits */
133
  if (!fwrite (&u16, 2, 1, fo)) return 1;
134
  u32 = CNV32(0);  /* compression */
135
  if (!fwrite (&u32, 4, 1, fo)) return 1;
136
  u32 = CNV32(x * y); /* image size */
137
  if (!fwrite (&u32, 4, 1, fo)) return 1;
138 859 markom
  u32 = CNV32(2835);  /* x resolution */
139 645 markom
  if (!fwrite (&u32, 4, 1, fo)) return 1;
140 859 markom
  u32 = CNV32(2835);  /* y resolution */
141 645 markom
  if (!fwrite (&u32, 4, 1, fo)) return 1;
142
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
143
  if (!fwrite (&u32, 4, 1, fo)) return 1;
144
  u32 = CNV32(0);  /* important colours; all are important */
145
  if (!fwrite (&u32, 4, 1, fo)) return 1;
146
 
147
  for (i = 0; i < 256; i++) {
148
    unsigned long val, d;
149
    d = pal[i];
150 766 simons
#if 1
151
    val = ((d >> 0) & 0x1f) << 3;   /* Blue */
152
    val |= ((d >> 5) & 0x3f) << 10;  /* Green */
153
    val |= ((d >> 11) & 0x1f) << 19; /* Red */
154
#else 
155 645 markom
    val = CNV32(pal[i]);
156 766 simons
#endif
157 645 markom
    if (!fwrite (&val, 4, 1, fo)) return 1;
158
  }
159
 
160 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
161 645 markom
  /* Data is stored upside down */
162
  for (y = sy - 1; y >= 0; y--) {
163
    int align = (4 - sx) % 4;
164
    int zero = CNV32(0);
165 799 simons
    int add;
166 645 markom
    while (align < 0) align += 4;
167 799 simons
    for (x = 0; x < sx; x++) {
168
      add = (fb_addr & ~(FB_WRAP - 1)) | ((fb_addr + y * sx + x) & (FB_WRAP - 1));
169
      fputc (evalsim_mem8 (add), fo);
170
    }
171 645 markom
    if (align && !fwrite (&zero, align, 1, fo)) return 1;
172
  }
173
 
174 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
175 645 markom
  fclose (fo);
176
  return 0;
177
}
178
 
179 859 markom
/* Dumps a bmp file, based on current image */
180
static int fb_dump_image24 (char *filename)
181
{
182
  int sx = FB_SIZEX;
183
  int sy = FB_SIZEY;
184
  int i, x, y;
185
  FILE *fo;
186
 
187
  unsigned short int u16;
188
  unsigned long int u32;
189
 
190 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
191 859 markom
  fo = fopen (filename, "wb+");
192
  u16 = CNV16(19778); /* BM */
193
  if (!fwrite (&u16, 2, 1, fo)) return 1;
194
  u32 = CNV32(14 + 40 + sx * sy * 3); /* size */
195
  if (!fwrite (&u32, 4, 1, fo)) return 1;
196
  u32 = CNV32(0); /* reserved */
197
  if (!fwrite (&u32, 4, 1, fo)) return 1;
198
  u32 = 14 + 40; /* offset */
199
  if (!fwrite (&u32, 4, 1, fo)) return 1;
200
 
201
  u32 = CNV32(40); /* header size */
202
  if (!fwrite (&u32, 4, 1, fo)) return 1;
203
  u32 = CNV32(sx); /* width */
204
  if (!fwrite (&u32, 4, 1, fo)) return 1;
205
  u32 = CNV32(sy); /* height */
206
  if (!fwrite (&u32, 4, 1, fo)) return 1;
207
  u16 = CNV16(1);  /* planes */
208
  if (!fwrite (&u16, 2, 1, fo)) return 1;
209
  u16 = CNV16(24);  /* bits */
210
  if (!fwrite (&u16, 2, 1, fo)) return 1;
211
  u32 = CNV32(0);  /* compression */
212
  if (!fwrite (&u32, 4, 1, fo)) return 1;
213
  u32 = CNV32(x * y * 3); /* image size */
214
  if (!fwrite (&u32, 4, 1, fo)) return 1;
215
  u32 = CNV32(2835);  /* x resolution */
216
  if (!fwrite (&u32, 4, 1, fo)) return 1;
217
  u32 = CNV32(2835);  /* y resolution */
218
  if (!fwrite (&u32, 4, 1, fo)) return 1;
219
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
220
  if (!fwrite (&u32, 4, 1, fo)) return 1;
221
  u32 = CNV32(0);  /* important colours; all are important */
222
  if (!fwrite (&u32, 4, 1, fo)) return 1;
223
 
224 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
225 859 markom
  /* Data is stored upside down */
226
  for (y = sy - 1; y >= 0; y--) {
227
    unsigned char line[FB_SIZEX][3];
228
    for (x = 0; x < sx; x++)
229
      if (y >= cameray && x >= camerax && y < cameray + CAM_SIZEY && x < camerax + CAM_SIZEX) {
230 867 markom
        int add = (cam_addr + (x - camerax + (y - cameray) * CAM_SIZEX) * 2) ^ 2;
231 859 markom
        unsigned short d = evalsim_mem16 (add);
232
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
233
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
234
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
235
      } else {
236
        int add = (fb_addr & ~(FB_WRAP - 1)) | ((fb_addr + y * sx + x) & (FB_WRAP - 1));
237
        unsigned short d = pal[evalsim_mem8 (add)];
238
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
239
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
240
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
241
      }
242
    if(!fwrite (line, sizeof(line), 1, fo)) return 1;
243
  }
244
 
245 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
246 859 markom
  fclose (fo);
247
  return 0;
248
}
249
 
250 805 markom
void fb_job (int param)
251
{
252 845 markom
  if (param) {
253
    /* dump the image? */
254 859 markom
    if (fb_ctrl & 1) {
255 845 markom
      char temp[STR_SIZE];
256
      sprintf (temp, "%s%04i.bmp", &config.fb.filename[0], fb_pic);
257 859 markom
      if (fb_ctrl & 2) fb_dump_image24 (temp);
258
      else fb_dump_image8 (temp);
259 845 markom
      fb_pic++;
260
    }
261 884 markom
    SCHED_ADD(fb_job, 0, runtime.sim.cycles + config.fb.refresh_rate - config.fb.refresh_rate / REFRESH_DIVIDER);
262 845 markom
    fb_in_refresh = 0;
263
  } else {
264
    fb_refresh_count++;
265 884 markom
    SCHED_ADD(fb_job, 1, runtime.sim.cycles + config.fb.refresh_rate / REFRESH_DIVIDER);
266 845 markom
 
267 805 markom
  }
268
}
269
 
270 645 markom
/* Reset all VGAs */
271
void fb_reset ()
272
{
273
  int i;
274 647 markom
 
275 645 markom
  if (config.fb.enabled) {
276
    fb_pic = 0;
277 648 markom
    fb_addr = 0;
278
    fb_ctrl = 0;
279 645 markom
 
280
    for (i = 0; i < 256; i++)
281
      pal[i] = (i << 16) | (i << 8) | (i << 0);
282 647 markom
 
283
    if (config.fb.baseaddr)
284 970 simons
      register_memoryarea(config.fb.baseaddr, FB_PAL + 256*4, 4, 0, fb_read32, fb_write32);
285 884 markom
    SCHED_ADD(fb_job, 0, runtime.sim.cycles + config.fb.refresh_rate);
286 645 markom
  }
287
}

powered by: WebSVN 2.1.0

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