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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc2/] [or1ksim/] [peripheral/] [fb.c] - Blame information for rev 1359

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 1350 nogj
 
22
#include "config.h"
23
 
24
#ifdef HAVE_INTTYPES_H
25
#include <inttypes.h>
26
#endif
27
 
28
#include "port.h"
29
#include "arch.h"
30 645 markom
#include "sim-config.h"
31
#include "abstract.h"
32
#include "fb.h"
33 805 markom
#include "sched.h"
34 645 markom
 
35 799 simons
#define FB_WRAP (512*1024)
36
 
37 645 markom
static unsigned long pal[256];
38 648 markom
static int fb_ctrl = 0;
39
static int fb_pic = 0;
40 845 markom
static int fb_in_refresh = 1;
41
static int fb_refresh_count = 0;
42 1350 nogj
static oraddr_t fb_addr = 0;
43
static oraddr_t cam_addr = 0;
44 859 markom
static int camerax = 0, cameray = 0, camera_pos = 0;
45 645 markom
 
46 1350 nogj
static void change_buf_addr (oraddr_t addr)
47 647 markom
{
48
  fb_addr = addr;
49
}
50
 
51
/* Write a register */
52 1359 nogj
void fb_write32 (oraddr_t addr, uint32_t value, void *dat)
53 647 markom
{
54
  int a = (addr - config.fb.baseaddr);
55
  switch (a) {
56 846 markom
    case FB_CTRL:    fb_ctrl = value; break;
57 647 markom
    case FB_BUFADDR: change_buf_addr (value); break;
58 859 markom
    case FB_CAMBUFADDR: cam_addr = value; break;
59
    case FB_CAMPOSADDR: camera_pos = value;
60
                     camerax = value % FB_SIZEX;
61
                     cameray = value / FB_SIZEX;
62
                     break;
63 647 markom
    default:
64
      a -= FB_PAL;
65
      a /= 4;
66
      if (a < 0 || a >= 256) {
67 1350 nogj
        fprintf (stderr, "Write out of palette buffer (0x%"PRIxADDR")!\n", addr);
68 884 markom
        runtime.sim.cont_run = 0;
69 647 markom
      } else pal[a] = value;
70
      break;
71
  }
72
}
73
 
74 645 markom
/* Read a register */
75 1359 nogj
oraddr_t fb_read32 (oraddr_t addr, void *dat)
76 645 markom
{
77 647 markom
  int a = (addr - config.fb.baseaddr);
78
  switch (a) {
79 845 markom
    case FB_CTRL:
80 846 markom
      return fb_ctrl & ~0xff000000| (fb_in_refresh ? 0x80000000 : 0) | ((unsigned long)(fb_refresh_count & 0x7f) << 24);
81 845 markom
      break;
82 647 markom
    case FB_BUFADDR: return fb_addr; break;
83 859 markom
    case FB_CAMBUFADDR: return cam_addr; break;
84
    case FB_CAMPOSADDR: return camera_pos; break;
85 647 markom
    default:
86
      a -= FB_PAL;
87
      a /= 4;
88
      if (a < 0 || a >= 256) {
89 1350 nogj
        fprintf (stderr, "Read out of palette buffer (0x%"PRIxADDR")!\n", addr);
90 884 markom
        runtime.sim.cont_run = 0;
91 647 markom
        return 0;
92
      } else return pal[a];
93
  }
94 645 markom
}
95
 
96
/* define these also for big endian */
97 859 markom
#if __BIG_ENDIAN__
98 1244 hpanther
#define CNV32(x) (\
99 859 markom
     ((((x) >> 24) & 0xff) << 0)\
100
   | ((((x) >> 16) & 0xff) << 8)\
101
   | ((((x) >> 8) & 0xff) << 16)\
102
   | ((((x) >> 0) & 0xff) << 24))
103 1244 hpanther
#define CNV16(x) (\
104
     ((((x) >> 8) & 0xff) << 0)\
105 859 markom
   | ((((x) >> 0) & 0xff) << 8))
106
#else
107 645 markom
#define CNV16(x) (x)
108
#define CNV32(x) (x)
109 859 markom
#endif
110 645 markom
 
111
/* Dumps a bmp file, based on current image */
112 859 markom
static int fb_dump_image8 (char *filename)
113 645 markom
{
114
  int sx = FB_SIZEX;
115
  int sy = FB_SIZEY;
116
  int i, x, y;
117
  FILE *fo;
118
 
119
  unsigned short int u16;
120
  unsigned long int u32;
121
 
122 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
123 645 markom
  fo = fopen (filename, "wb+");
124
  u16 = CNV16(19778); /* BM */
125
  if (!fwrite (&u16, 2, 1, fo)) return 1;
126
  u32 = CNV32(14 + 40 + sx * sy + 1024); /* size */
127
  if (!fwrite (&u32, 4, 1, fo)) return 1;
128
  u32 = CNV32(0); /* reserved */
129
  if (!fwrite (&u32, 4, 1, fo)) return 1;
130
  u32 = 14 + 40 + 1024; /* offset */
131
  if (!fwrite (&u32, 4, 1, fo)) return 1;
132
 
133
  u32 = CNV32(40); /* header size */
134
  if (!fwrite (&u32, 4, 1, fo)) return 1;
135
  u32 = CNV32(sx); /* width */
136
  if (!fwrite (&u32, 4, 1, fo)) return 1;
137
  u32 = CNV32(sy); /* height */
138
  if (!fwrite (&u32, 4, 1, fo)) return 1;
139
  u16 = CNV16(1);  /* planes */
140
  if (!fwrite (&u16, 2, 1, fo)) return 1;
141
  u16 = CNV16(8);  /* bits */
142
  if (!fwrite (&u16, 2, 1, fo)) return 1;
143
  u32 = CNV32(0);  /* compression */
144
  if (!fwrite (&u32, 4, 1, fo)) return 1;
145
  u32 = CNV32(x * y); /* image size */
146
  if (!fwrite (&u32, 4, 1, fo)) return 1;
147 859 markom
  u32 = CNV32(2835);  /* x resolution */
148 645 markom
  if (!fwrite (&u32, 4, 1, fo)) return 1;
149 859 markom
  u32 = CNV32(2835);  /* y resolution */
150 645 markom
  if (!fwrite (&u32, 4, 1, fo)) return 1;
151
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
152
  if (!fwrite (&u32, 4, 1, fo)) return 1;
153
  u32 = CNV32(0);  /* important colours; all are important */
154
  if (!fwrite (&u32, 4, 1, fo)) return 1;
155
 
156
  for (i = 0; i < 256; i++) {
157
    unsigned long val, d;
158
    d = pal[i];
159 766 simons
#if 1
160
    val = ((d >> 0) & 0x1f) << 3;   /* Blue */
161
    val |= ((d >> 5) & 0x3f) << 10;  /* Green */
162
    val |= ((d >> 11) & 0x1f) << 19; /* Red */
163
#else 
164 645 markom
    val = CNV32(pal[i]);
165 766 simons
#endif
166 645 markom
    if (!fwrite (&val, 4, 1, fo)) return 1;
167
  }
168
 
169 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
170 645 markom
  /* Data is stored upside down */
171
  for (y = sy - 1; y >= 0; y--) {
172
    int align = (4 - sx) % 4;
173
    int zero = CNV32(0);
174 799 simons
    int add;
175 645 markom
    while (align < 0) align += 4;
176 799 simons
    for (x = 0; x < sx; x++) {
177
      add = (fb_addr & ~(FB_WRAP - 1)) | ((fb_addr + y * sx + x) & (FB_WRAP - 1));
178
      fputc (evalsim_mem8 (add), fo);
179
    }
180 645 markom
    if (align && !fwrite (&zero, align, 1, fo)) return 1;
181
  }
182
 
183 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
184 645 markom
  fclose (fo);
185
  return 0;
186
}
187
 
188 859 markom
/* Dumps a bmp file, based on current image */
189
static int fb_dump_image24 (char *filename)
190
{
191
  int sx = FB_SIZEX;
192
  int sy = FB_SIZEY;
193 1308 phoenix
  int x, y;
194 859 markom
  FILE *fo;
195
 
196
  unsigned short int u16;
197
  unsigned long int u32;
198
 
199 997 markom
  if (config.sim.verbose) PRINTF ("Creating %s...", filename);
200 859 markom
  fo = fopen (filename, "wb+");
201
  u16 = CNV16(19778); /* BM */
202
  if (!fwrite (&u16, 2, 1, fo)) return 1;
203
  u32 = CNV32(14 + 40 + sx * sy * 3); /* size */
204
  if (!fwrite (&u32, 4, 1, fo)) return 1;
205
  u32 = CNV32(0); /* reserved */
206
  if (!fwrite (&u32, 4, 1, fo)) return 1;
207
  u32 = 14 + 40; /* offset */
208
  if (!fwrite (&u32, 4, 1, fo)) return 1;
209
 
210
  u32 = CNV32(40); /* header size */
211
  if (!fwrite (&u32, 4, 1, fo)) return 1;
212
  u32 = CNV32(sx); /* width */
213
  if (!fwrite (&u32, 4, 1, fo)) return 1;
214
  u32 = CNV32(sy); /* height */
215
  if (!fwrite (&u32, 4, 1, fo)) return 1;
216
  u16 = CNV16(1);  /* planes */
217
  if (!fwrite (&u16, 2, 1, fo)) return 1;
218
  u16 = CNV16(24);  /* bits */
219
  if (!fwrite (&u16, 2, 1, fo)) return 1;
220
  u32 = CNV32(0);  /* compression */
221
  if (!fwrite (&u32, 4, 1, fo)) return 1;
222
  u32 = CNV32(x * y * 3); /* image size */
223
  if (!fwrite (&u32, 4, 1, fo)) return 1;
224
  u32 = CNV32(2835);  /* x resolution */
225
  if (!fwrite (&u32, 4, 1, fo)) return 1;
226
  u32 = CNV32(2835);  /* y resolution */
227
  if (!fwrite (&u32, 4, 1, fo)) return 1;
228
  u32 = CNV32(0);  /* ncolours = 0; should be generated */
229
  if (!fwrite (&u32, 4, 1, fo)) return 1;
230
  u32 = CNV32(0);  /* important colours; all are important */
231
  if (!fwrite (&u32, 4, 1, fo)) return 1;
232
 
233 997 markom
  if (config.sim.verbose) PRINTF ("(%i,%i)", sx, sy);
234 859 markom
  /* Data is stored upside down */
235
  for (y = sy - 1; y >= 0; y--) {
236
    unsigned char line[FB_SIZEX][3];
237
    for (x = 0; x < sx; x++)
238
      if (y >= cameray && x >= camerax && y < cameray + CAM_SIZEY && x < camerax + CAM_SIZEX) {
239 867 markom
        int add = (cam_addr + (x - camerax + (y - cameray) * CAM_SIZEX) * 2) ^ 2;
240 859 markom
        unsigned short d = evalsim_mem16 (add);
241
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
242
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
243
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
244
      } else {
245
        int add = (fb_addr & ~(FB_WRAP - 1)) | ((fb_addr + y * sx + x) & (FB_WRAP - 1));
246
        unsigned short d = pal[evalsim_mem8 (add)];
247
        line[x][0] = ((d >> 0) & 0x1f) << 3;  /* Blue */
248
        line[x][1] = ((d >> 5) & 0x3f) << 2;  /* Green */
249
        line[x][2] = ((d >> 11) & 0x1f) << 3; /* Red */
250
      }
251
    if(!fwrite (line, sizeof(line), 1, fo)) return 1;
252
  }
253
 
254 997 markom
  if (config.sim.verbose) PRINTF ("DONE\n");
255 859 markom
  fclose (fo);
256
  return 0;
257
}
258
 
259 805 markom
void fb_job (int param)
260
{
261 845 markom
  if (param) {
262
    /* dump the image? */
263 859 markom
    if (fb_ctrl & 1) {
264 845 markom
      char temp[STR_SIZE];
265
      sprintf (temp, "%s%04i.bmp", &config.fb.filename[0], fb_pic);
266 859 markom
      if (fb_ctrl & 2) fb_dump_image24 (temp);
267
      else fb_dump_image8 (temp);
268 845 markom
      fb_pic++;
269
    }
270 884 markom
    SCHED_ADD(fb_job, 0, runtime.sim.cycles + config.fb.refresh_rate - config.fb.refresh_rate / REFRESH_DIVIDER);
271 845 markom
    fb_in_refresh = 0;
272
  } else {
273
    fb_refresh_count++;
274 884 markom
    SCHED_ADD(fb_job, 1, runtime.sim.cycles + config.fb.refresh_rate / REFRESH_DIVIDER);
275 845 markom
 
276 805 markom
  }
277
}
278
 
279 645 markom
/* Reset all VGAs */
280
void fb_reset ()
281
{
282
  int i;
283 647 markom
 
284 645 markom
  if (config.fb.enabled) {
285
    fb_pic = 0;
286 648 markom
    fb_addr = 0;
287
    fb_ctrl = 0;
288 645 markom
 
289
    for (i = 0; i < 256; i++)
290
      pal[i] = (i << 16) | (i << 8) | (i << 0);
291 647 markom
 
292
    if (config.fb.baseaddr)
293 1359 nogj
      register_memoryarea(config.fb.baseaddr, FB_PAL + 256*4, 4, 0, fb_read32, fb_write32, NULL);
294 884 markom
    SCHED_ADD(fb_job, 0, runtime.sim.cycles + config.fb.refresh_rate);
295 645 markom
  }
296
}
297 1358 nogj
 
298
/*-----------------------------------------------------[ FB configuration ]---*/
299
void fb_enabled(union param_val val, void *dat)
300
{
301
  config.fb.enabled = val.int_val;
302
}
303
 
304
void fb_baseaddr(union param_val val, void *dat)
305
{
306
  config.fb.baseaddr = val.addr_val;
307
}
308
 
309
void fb_refresh_rate(union param_val val, void *dat)
310
{
311
  config.fb.refresh_rate = val.int_val;
312
}
313
 
314
void fb_filename(union param_val val, void *dat)
315
{
316
  strcpy(config.fb.filename, val.str_val);
317
}
318
 
319
void reg_fb_sec(void)
320
{
321
  struct config_section *sec = reg_config_sec("fb", NULL, NULL);
322
 
323
  reg_config_param(sec, "enabled", paramt_int, fb_enabled);
324
  reg_config_param(sec, "baseaddr", paramt_addr, fb_baseaddr);
325
  reg_config_param(sec, "refresh_rate", paramt_int, fb_refresh_rate);
326
  reg_config_param(sec, "filename", paramt_str, fb_filename);
327
}

powered by: WebSVN 2.1.0

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