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 1484

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 1484 nogj
    int breakpoint;
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 1484 nogj
      fputc (eval_direct8 (add, &breakpoint, 0, 0), fo);
194 799 simons
    }
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 1484 nogj
 
214
  int breakpoint;
215 859 markom
 
216 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
217 859 markom
  fo = fopen (filename, "wb+");
218
  u16 = CNV16(19778); /* BM */
219
  if (!fwrite (&u16, 2, 1, fo)) return 1;
220
  u32 = CNV32(14 + 40 + sx * sy * 3); /* size */
221
  if (!fwrite (&u32, 4, 1, fo)) return 1;
222
  u32 = CNV32(0); /* reserved */
223
  if (!fwrite (&u32, 4, 1, fo)) return 1;
224
  u32 = 14 + 40; /* offset */
225
  if (!fwrite (&u32, 4, 1, fo)) return 1;
226
 
227
  u32 = CNV32(40); /* header size */
228
  if (!fwrite (&u32, 4, 1, fo)) return 1;
229
  u32 = CNV32(sx); /* width */
230
  if (!fwrite (&u32, 4, 1, fo)) return 1;
231
  u32 = CNV32(sy); /* height */
232
  if (!fwrite (&u32, 4, 1, fo)) return 1;
233
  u16 = CNV16(1);  /* planes */
234
  if (!fwrite (&u16, 2, 1, fo)) return 1;
235
  u16 = CNV16(24);  /* bits */
236
  if (!fwrite (&u16, 2, 1, fo)) return 1;
237
  u32 = CNV32(0);  /* compression */
238
  if (!fwrite (&u32, 4, 1, fo)) return 1;
239
  u32 = CNV32(x * y * 3); /* image size */
240
  if (!fwrite (&u32, 4, 1, fo)) return 1;
241
  u32 = CNV32(2835);  /* x resolution */
242
  if (!fwrite (&u32, 4, 1, fo)) return 1;
243
  u32 = CNV32(2835);  /* y resolution */
244
  if (!fwrite (&u32, 4, 1, fo)) return 1;
245
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
246
  if (!fwrite (&u32, 4, 1, fo)) return 1;
247
  u32 = CNV32(0);  /* important colours; all are important */
248
  if (!fwrite (&u32, 4, 1, fo)) return 1;
249
 
250 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
251 859 markom
  /* Data is stored upside down */
252
  for (y = sy - 1; y >= 0; y--) {
253
    unsigned char line[FB_SIZEX][3];
254
    for (x = 0; x < sx; x++)
255 1369 nogj
      if (y >= fb->cameray && x >= fb->camerax && y < fb->cameray + CAM_SIZEY && x < fb->camerax + CAM_SIZEX) {
256
        int add = (fb->cam_addr + (x - fb->camerax + (y - fb->cameray) * CAM_SIZEX) * 2) ^ 2;
257 1484 nogj
        unsigned short d = eval_direct16 (add, &breakpoint, 0, 0);
258 859 markom
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
259
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
260
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
261
      } else {
262 1369 nogj
        int add = (fb->addr & ~(FB_WRAP - 1)) | ((fb->addr + y * sx + x) & (FB_WRAP - 1));
263 1484 nogj
        unsigned short d = fb->pal[eval_direct8 (add, &breakpoint, 0, 0)];
264 859 markom
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
265
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
266
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
267
      }
268
    if(!fwrite (line, sizeof(line), 1, fo)) return 1;
269
  }
270
 
271 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
272 859 markom
  fclose (fo);
273
  return 0;
274
}
275
 
276 1369 nogj
void fb_job (void *dat)
277 805 markom
{
278 1369 nogj
  struct fb_state *fb = dat;
279
 
280
  if (fb->refresh) {
281 845 markom
    /* dump the image? */
282 1369 nogj
    if (fb->ctrl & 1) {
283 845 markom
      char temp[STR_SIZE];
284 1369 nogj
      sprintf (temp, "%s%04i.bmp", fb->filename, fb->pic);
285
      if (fb->ctrl & 2) fb_dump_image24 (fb, temp);
286
      else fb_dump_image8 (fb, temp);
287
      fb->pic++;
288 845 markom
    }
289 1390 nogj
    SCHED_ADD(fb_job, dat, fb->refresh_rate / REFRESH_DIVIDER);
290 1369 nogj
    fb->in_refresh = 0;
291
    fb->refresh = 0;
292 845 markom
  } else {
293 1369 nogj
    fb->refresh_count++;
294
    fb->refresh = 1;
295 1390 nogj
    SCHED_ADD(fb_job, dat, fb->refresh_rate / REFRESH_DIVIDER);
296 805 markom
  }
297
}
298
 
299 1446 nogj
/* Reset all FBs */
300 1369 nogj
void fb_reset (void *dat)
301 645 markom
{
302 1369 nogj
  struct fb_state *fb = dat;
303 645 markom
  int i;
304 647 markom
 
305 1369 nogj
  fb->pic = 0;
306
  fb->addr = 0;
307
  fb->ctrl = 0;
308 645 markom
 
309 1369 nogj
  for (i = 0; i < 256; i++)
310
    fb->pal[i] = (i << 16) | (i << 8) | (i << 0);
311 647 markom
 
312 1390 nogj
  SCHED_ADD(fb_job, dat, fb->refresh_rate);
313 1369 nogj
  fb->refresh = 0;
314 645 markom
}
315 1358 nogj
 
316
/*-----------------------------------------------------[ FB configuration ]---*/
317
void fb_baseaddr(union param_val val, void *dat)
318
{
319 1369 nogj
  struct fb_state *fb = dat;
320
  fb->baseaddr = val.addr_val;
321 1358 nogj
}
322
 
323
void fb_refresh_rate(union param_val val, void *dat)
324
{
325 1369 nogj
  struct fb_state *fb = dat;
326
  fb->refresh_rate = val.int_val;
327 1358 nogj
}
328
 
329
void fb_filename(union param_val val, void *dat)
330
{
331 1369 nogj
  struct fb_state *fb = dat;
332
  if(!(fb->filename = strdup(val.str_val))) {
333
    fprintf(stderr, "Peripheral FB: Run out of memory\n");
334
    exit(-1);
335
  }
336 1358 nogj
}
337
 
338 1461 nogj
void fb_enabled(union param_val val, void *dat)
339
{
340
  struct fb_state *fb = dat;
341
  fb->enabled = val.int_val;
342
}
343
 
344 1369 nogj
void *fb_sec_start(void)
345
{
346
  struct fb_state *new = malloc(sizeof(struct fb_state));
347
 
348
  if(!new) {
349
    fprintf(stderr, "Peripheral FB: Run out of memory\n");
350
    exit(-1);
351
  }
352
 
353
  new->baseaddr = 0;
354
  new->ctrl = 0;
355
  new->pic = 0;
356
  new->in_refresh = 1;
357
  new->refresh_count = 0;
358
  new->addr = 0;
359
  new->cam_addr = 0;
360
  new->camerax = 0;
361
  new->cameray = 0;
362
  new->camera_pos = 0;
363 1461 nogj
  new->enabled = 1;
364 1369 nogj
 
365
  return new;
366
}
367
 
368
void fb_sec_end(void *dat)
369
{
370
  struct fb_state *fb = dat;
371
 
372 1461 nogj
  if(!fb->enabled) {
373
    free(dat);
374
    return;
375
  }
376
 
377 1369 nogj
  if (fb->baseaddr)
378
    register_memoryarea(fb->baseaddr, FB_PAL + 256*4, 4, 0, fb_read32, fb_write32, dat);
379
 
380
  reg_sim_reset(fb_reset, dat);
381
}
382
 
383 1358 nogj
void reg_fb_sec(void)
384
{
385 1369 nogj
  struct config_section *sec = reg_config_sec("fb", fb_sec_start, fb_sec_end);
386 1358 nogj
 
387
  reg_config_param(sec, "baseaddr", paramt_addr, fb_baseaddr);
388 1461 nogj
  reg_config_param(sec, "enabled", paramt_int, fb_enabled);
389 1358 nogj
  reg_config_param(sec, "refresh_rate", paramt_int, fb_refresh_rate);
390
  reg_config_param(sec, "filename", paramt_str, fb_filename);
391
}

powered by: WebSVN 2.1.0

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