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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [peripheral/] [fb.c] - Blame information for rev 1782

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 1369 nogj
#include <string.h>
22 1350 nogj
 
23
#include "config.h"
24
 
25
#ifdef HAVE_INTTYPES_H
26
#include <inttypes.h>
27
#endif
28
 
29
#include "port.h"
30
#include "arch.h"
31 645 markom
#include "sim-config.h"
32
#include "abstract.h"
33
#include "fb.h"
34 805 markom
#include "sched.h"
35 645 markom
 
36 799 simons
#define FB_WRAP (512*1024)
37
 
38 1369 nogj
struct fb_state {
39
  unsigned long pal[256];
40
  int ctrl;
41
  int pic;
42
  int in_refresh;
43
  int refresh_count;
44
  oraddr_t addr;
45
  oraddr_t cam_addr;
46
  int camerax;
47
  int cameray;
48
  int camera_pos;
49
  oraddr_t baseaddr;
50
  int refresh;
51
  int refresh_rate;
52
  char *filename;
53
};
54 645 markom
 
55 1369 nogj
static void change_buf_addr (struct fb_state *fb, oraddr_t addr)
56 647 markom
{
57 1369 nogj
  fb->addr = addr;
58 647 markom
}
59
 
60
/* Write a register */
61 1359 nogj
void fb_write32 (oraddr_t addr, uint32_t value, void *dat)
62 647 markom
{
63 1369 nogj
  struct fb_state *fb = dat;
64
 
65
  oraddr_t a = (addr - fb->baseaddr);
66
 
67 647 markom
  switch (a) {
68 1369 nogj
    case FB_CTRL:    fb->ctrl = value; break;
69
    case FB_BUFADDR: change_buf_addr (fb, value); break;
70
    case FB_CAMBUFADDR: fb->cam_addr = value; break;
71
    case FB_CAMPOSADDR: fb->camera_pos = value;
72
                     fb->camerax = value % FB_SIZEX;
73
                     fb->cameray = value / FB_SIZEX;
74 859 markom
                     break;
75 647 markom
    default:
76
      a -= FB_PAL;
77
      a /= 4;
78
      if (a < 0 || a >= 256) {
79 1350 nogj
        fprintf (stderr, "Write out of palette buffer (0x%"PRIxADDR")!\n", addr);
80 884 markom
        runtime.sim.cont_run = 0;
81 1369 nogj
      } else fb->pal[a] = value;
82 647 markom
      break;
83
  }
84
}
85
 
86 645 markom
/* Read a register */
87 1359 nogj
oraddr_t fb_read32 (oraddr_t addr, void *dat)
88 645 markom
{
89 1369 nogj
  struct fb_state *fb = dat;
90
 
91
  oraddr_t a = (addr - fb->baseaddr);
92
 
93 647 markom
  switch (a) {
94 845 markom
    case FB_CTRL:
95 1369 nogj
      return fb->ctrl & ~0xff000000| (fb->in_refresh ? 0x80000000 : 0) | ((unsigned long)(fb->refresh_count & 0x7f) << 24);
96 845 markom
      break;
97 1369 nogj
    case FB_BUFADDR: return fb->addr; break;
98
    case FB_CAMBUFADDR: return fb->cam_addr; break;
99
    case FB_CAMPOSADDR: return fb->camera_pos; break;
100 647 markom
    default:
101
      a -= FB_PAL;
102
      a /= 4;
103
      if (a < 0 || a >= 256) {
104 1350 nogj
        fprintf (stderr, "Read out of palette buffer (0x%"PRIxADDR")!\n", addr);
105 884 markom
        runtime.sim.cont_run = 0;
106 647 markom
        return 0;
107 1369 nogj
      } else return fb->pal[a];
108 647 markom
  }
109 645 markom
}
110
 
111
/* define these also for big endian */
112 859 markom
#if __BIG_ENDIAN__
113 1244 hpanther
#define CNV32(x) (\
114 859 markom
     ((((x) >> 24) & 0xff) << 0)\
115
   | ((((x) >> 16) & 0xff) << 8)\
116
   | ((((x) >> 8) & 0xff) << 16)\
117
   | ((((x) >> 0) & 0xff) << 24))
118 1244 hpanther
#define CNV16(x) (\
119
     ((((x) >> 8) & 0xff) << 0)\
120 859 markom
   | ((((x) >> 0) & 0xff) << 8))
121
#else
122 645 markom
#define CNV16(x) (x)
123
#define CNV32(x) (x)
124 859 markom
#endif
125 645 markom
 
126
/* Dumps a bmp file, based on current image */
127 1369 nogj
static int fb_dump_image8 (struct fb_state *fb, char *filename)
128 645 markom
{
129
  int sx = FB_SIZEX;
130
  int sy = FB_SIZEY;
131
  int i, x, y;
132
  FILE *fo;
133
 
134
  unsigned short int u16;
135
  unsigned long int u32;
136
 
137 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
138 645 markom
  fo = fopen (filename, "wb+");
139
  u16 = CNV16(19778); /* BM */
140
  if (!fwrite (&u16, 2, 1, fo)) return 1;
141
  u32 = CNV32(14 + 40 + sx * sy + 1024); /* size */
142
  if (!fwrite (&u32, 4, 1, fo)) return 1;
143
  u32 = CNV32(0); /* reserved */
144
  if (!fwrite (&u32, 4, 1, fo)) return 1;
145
  u32 = 14 + 40 + 1024; /* offset */
146
  if (!fwrite (&u32, 4, 1, fo)) return 1;
147
 
148
  u32 = CNV32(40); /* header size */
149
  if (!fwrite (&u32, 4, 1, fo)) return 1;
150
  u32 = CNV32(sx); /* width */
151
  if (!fwrite (&u32, 4, 1, fo)) return 1;
152
  u32 = CNV32(sy); /* height */
153
  if (!fwrite (&u32, 4, 1, fo)) return 1;
154
  u16 = CNV16(1);  /* planes */
155
  if (!fwrite (&u16, 2, 1, fo)) return 1;
156
  u16 = CNV16(8);  /* bits */
157
  if (!fwrite (&u16, 2, 1, fo)) return 1;
158
  u32 = CNV32(0);  /* compression */
159
  if (!fwrite (&u32, 4, 1, fo)) return 1;
160
  u32 = CNV32(x * y); /* image size */
161
  if (!fwrite (&u32, 4, 1, fo)) return 1;
162 859 markom
  u32 = CNV32(2835);  /* x resolution */
163 645 markom
  if (!fwrite (&u32, 4, 1, fo)) return 1;
164 859 markom
  u32 = CNV32(2835);  /* y resolution */
165 645 markom
  if (!fwrite (&u32, 4, 1, fo)) return 1;
166
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
167
  if (!fwrite (&u32, 4, 1, fo)) return 1;
168
  u32 = CNV32(0);  /* important colours; all are important */
169
  if (!fwrite (&u32, 4, 1, fo)) return 1;
170
 
171
  for (i = 0; i < 256; i++) {
172
    unsigned long val, d;
173 1369 nogj
    d = fb->pal[i];
174 766 simons
#if 1
175
    val = ((d >> 0) & 0x1f) << 3;   /* Blue */
176
    val |= ((d >> 5) & 0x3f) << 10;  /* Green */
177
    val |= ((d >> 11) & 0x1f) << 19; /* Red */
178
#else 
179 645 markom
    val = CNV32(pal[i]);
180 766 simons
#endif
181 645 markom
    if (!fwrite (&val, 4, 1, fo)) return 1;
182
  }
183
 
184 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
185 645 markom
  /* Data is stored upside down */
186
  for (y = sy - 1; y >= 0; y--) {
187
    int align = (4 - sx) % 4;
188
    int zero = CNV32(0);
189 799 simons
    int add;
190 645 markom
    while (align < 0) align += 4;
191 799 simons
    for (x = 0; x < sx; x++) {
192 1369 nogj
      add = (fb->addr & ~(FB_WRAP - 1)) | ((fb->addr + y * sx + x) & (FB_WRAP - 1));
193 799 simons
      fputc (evalsim_mem8 (add), fo);
194
    }
195 645 markom
    if (align && !fwrite (&zero, align, 1, fo)) return 1;
196
  }
197
 
198 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
199 645 markom
  fclose (fo);
200
  return 0;
201
}
202
 
203 859 markom
/* Dumps a bmp file, based on current image */
204 1369 nogj
static int fb_dump_image24 (struct fb_state *fb, char *filename)
205 859 markom
{
206
  int sx = FB_SIZEX;
207
  int sy = FB_SIZEY;
208 1308 phoenix
  int x, y;
209 859 markom
  FILE *fo;
210
 
211
  unsigned short int u16;
212
  unsigned long int u32;
213
 
214 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
215 859 markom
  fo = fopen (filename, "wb+");
216
  u16 = CNV16(19778); /* BM */
217
  if (!fwrite (&u16, 2, 1, fo)) return 1;
218
  u32 = CNV32(14 + 40 + sx * sy * 3); /* size */
219
  if (!fwrite (&u32, 4, 1, fo)) return 1;
220
  u32 = CNV32(0); /* reserved */
221
  if (!fwrite (&u32, 4, 1, fo)) return 1;
222
  u32 = 14 + 40; /* offset */
223
  if (!fwrite (&u32, 4, 1, fo)) return 1;
224
 
225
  u32 = CNV32(40); /* header size */
226
  if (!fwrite (&u32, 4, 1, fo)) return 1;
227
  u32 = CNV32(sx); /* width */
228
  if (!fwrite (&u32, 4, 1, fo)) return 1;
229
  u32 = CNV32(sy); /* height */
230
  if (!fwrite (&u32, 4, 1, fo)) return 1;
231
  u16 = CNV16(1);  /* planes */
232
  if (!fwrite (&u16, 2, 1, fo)) return 1;
233
  u16 = CNV16(24);  /* bits */
234
  if (!fwrite (&u16, 2, 1, fo)) return 1;
235
  u32 = CNV32(0);  /* compression */
236
  if (!fwrite (&u32, 4, 1, fo)) return 1;
237
  u32 = CNV32(x * y * 3); /* image size */
238
  if (!fwrite (&u32, 4, 1, fo)) return 1;
239
  u32 = CNV32(2835);  /* x resolution */
240
  if (!fwrite (&u32, 4, 1, fo)) return 1;
241
  u32 = CNV32(2835);  /* y resolution */
242
  if (!fwrite (&u32, 4, 1, fo)) return 1;
243
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
244
  if (!fwrite (&u32, 4, 1, fo)) return 1;
245
  u32 = CNV32(0);  /* important colours; all are important */
246
  if (!fwrite (&u32, 4, 1, fo)) return 1;
247
 
248 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
249 859 markom
  /* Data is stored upside down */
250
  for (y = sy - 1; y >= 0; y--) {
251
    unsigned char line[FB_SIZEX][3];
252
    for (x = 0; x < sx; x++)
253 1369 nogj
      if (y >= fb->cameray && x >= fb->camerax && y < fb->cameray + CAM_SIZEY && x < fb->camerax + CAM_SIZEX) {
254
        int add = (fb->cam_addr + (x - fb->camerax + (y - fb->cameray) * CAM_SIZEX) * 2) ^ 2;
255 859 markom
        unsigned short d = evalsim_mem16 (add);
256
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
257
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
258
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
259
      } else {
260 1369 nogj
        int add = (fb->addr & ~(FB_WRAP - 1)) | ((fb->addr + y * sx + x) & (FB_WRAP - 1));
261
        unsigned short d = fb->pal[evalsim_mem8 (add)];
262 859 markom
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
263
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
264
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
265
      }
266
    if(!fwrite (line, sizeof(line), 1, fo)) return 1;
267
  }
268
 
269 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
270 859 markom
  fclose (fo);
271
  return 0;
272
}
273
 
274 1369 nogj
void fb_job (void *dat)
275 805 markom
{
276 1369 nogj
  struct fb_state *fb = dat;
277
 
278
  if (fb->refresh) {
279 845 markom
    /* dump the image? */
280 1369 nogj
    if (fb->ctrl & 1) {
281 845 markom
      char temp[STR_SIZE];
282 1369 nogj
      sprintf (temp, "%s%04i.bmp", fb->filename, fb->pic);
283
      if (fb->ctrl & 2) fb_dump_image24 (fb, temp);
284
      else fb_dump_image8 (fb, temp);
285
      fb->pic++;
286 845 markom
    }
287 1390 nogj
    SCHED_ADD(fb_job, dat, fb->refresh_rate / REFRESH_DIVIDER);
288 1369 nogj
    fb->in_refresh = 0;
289
    fb->refresh = 0;
290 845 markom
  } else {
291 1369 nogj
    fb->refresh_count++;
292
    fb->refresh = 1;
293 1390 nogj
    SCHED_ADD(fb_job, dat, fb->refresh_rate / REFRESH_DIVIDER);
294 805 markom
  }
295
}
296
 
297 645 markom
/* Reset all VGAs */
298 1369 nogj
void fb_reset (void *dat)
299 645 markom
{
300 1369 nogj
  struct fb_state *fb = dat;
301 645 markom
  int i;
302 647 markom
 
303 1369 nogj
  fb->pic = 0;
304
  fb->addr = 0;
305
  fb->ctrl = 0;
306 645 markom
 
307 1369 nogj
  for (i = 0; i < 256; i++)
308
    fb->pal[i] = (i << 16) | (i << 8) | (i << 0);
309 647 markom
 
310 1390 nogj
  SCHED_ADD(fb_job, dat, fb->refresh_rate);
311 1369 nogj
  fb->refresh = 0;
312 645 markom
}
313 1358 nogj
 
314
/*-----------------------------------------------------[ FB configuration ]---*/
315
void fb_baseaddr(union param_val val, void *dat)
316
{
317 1369 nogj
  struct fb_state *fb = dat;
318
  fb->baseaddr = val.addr_val;
319 1358 nogj
}
320
 
321
void fb_refresh_rate(union param_val val, void *dat)
322
{
323 1369 nogj
  struct fb_state *fb = dat;
324
  fb->refresh_rate = val.int_val;
325 1358 nogj
}
326
 
327
void fb_filename(union param_val val, void *dat)
328
{
329 1369 nogj
  struct fb_state *fb = dat;
330
  if(!(fb->filename = strdup(val.str_val))) {
331
    fprintf(stderr, "Peripheral FB: Run out of memory\n");
332
    exit(-1);
333
  }
334 1358 nogj
}
335
 
336 1369 nogj
void *fb_sec_start(void)
337
{
338
  struct fb_state *new = malloc(sizeof(struct fb_state));
339
 
340
  if(!new) {
341
    fprintf(stderr, "Peripheral FB: Run out of memory\n");
342
    exit(-1);
343
  }
344
 
345
  new->baseaddr = 0;
346
  new->ctrl = 0;
347
  new->pic = 0;
348
  new->in_refresh = 1;
349
  new->refresh_count = 0;
350
  new->addr = 0;
351
  new->cam_addr = 0;
352
  new->camerax = 0;
353
  new->cameray = 0;
354
  new->camera_pos = 0;
355
 
356
  return new;
357
}
358
 
359
void fb_sec_end(void *dat)
360
{
361
  struct fb_state *fb = dat;
362
 
363
  if (fb->baseaddr)
364
    register_memoryarea(fb->baseaddr, FB_PAL + 256*4, 4, 0, fb_read32, fb_write32, dat);
365
 
366
  reg_sim_reset(fb_reset, dat);
367
}
368
 
369 1358 nogj
void reg_fb_sec(void)
370
{
371 1369 nogj
  struct config_section *sec = reg_config_sec("fb", fb_sec_start, fb_sec_end);
372 1358 nogj
 
373
  reg_config_param(sec, "baseaddr", paramt_addr, fb_baseaddr);
374
  reg_config_param(sec, "refresh_rate", paramt_int, fb_refresh_rate);
375
  reg_config_param(sec, "filename", paramt_str, fb_filename);
376
}

powered by: WebSVN 2.1.0

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