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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [peripheral/] [fb.c] - Blame information for rev 1469

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

powered by: WebSVN 2.1.0

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