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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_70/] [or1ksim/] [peripheral/] [fb.c] - Blame information for rev 1765

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

powered by: WebSVN 2.1.0

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