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

powered by: WebSVN 2.1.0

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