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 1486

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

powered by: WebSVN 2.1.0

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