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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [vga.c] - Blame information for rev 1461

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 261 markom
/* vga.c -- Definition of types and structures for VGA/LCD
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 645 markom
#include <stdio.h>
21 1358 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 "vga.h"
33
#include "abstract.h"
34 805 markom
#include "sched.h"
35 645 markom
 
36
/* When this counter reaches config.vgas[].refresh_rate, a screenshot is taken and outputted */
37 1368 nogj
struct vga_state {
38 1461 nogj
  int enabled;
39 645 markom
  int pics;
40
  unsigned long ctrl, stat, htim, vtim;
41
  int vbindex;
42
  unsigned long vbar[2];
43
  unsigned hlen, vlen;
44
  int pindex;
45
  unsigned long palette[2][256];
46 1368 nogj
  oraddr_t baseaddr;
47
  int refresh_rate;
48
  int irq;
49
  char *filename;
50
};
51 645 markom
 
52
 
53
/* Write a register */
54 1359 nogj
void vga_write32(oraddr_t addr, uint32_t value, void *dat)
55 645 markom
{
56 1368 nogj
  struct vga_state *vga = dat;
57 645 markom
 
58 1368 nogj
  addr -= vga->baseaddr;
59 645 markom
 
60
  switch (addr) {
61 1368 nogj
    case VGA_CTRL:  vga->ctrl = value; break;
62
    case VGA_STAT:  vga->stat = value; break;
63
    case VGA_HTIM:  vga->htim = value; break;
64
    case VGA_VTIM:  vga->vtim = value; break;
65
    case VGA_HVLEN: vga->hlen = (value >> 16) + 2; vga->hlen = (value & 0xffff) + 2; break;
66
    case VGA_VBARA: vga->vbar[0] = value; break;
67
    case VGA_VBARB: vga->vbar[1] = value; break;
68 645 markom
    default:
69
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
70 1368 nogj
        vga->palette[0][addr - VGA_CLUTA] = value & 0x00ffffff;
71 645 markom
      } else if (addr >= VGA_CLUTB) {
72 1368 nogj
        vga->palette[1][addr - VGA_CLUTB] = value & 0x00ffffff;
73 645 markom
      } else {
74 1368 nogj
        fprintf( stderr, "vga_write32( 0x%"PRIxADDR", 0x%08"PRIx32" ): Out of range\n", addr + vga->baseaddr, value);
75 884 markom
        runtime.sim.cont_run = 0;
76 645 markom
        return;
77
      }
78
      break;
79
  }
80
}
81
 
82
/* Read a register */
83 1359 nogj
uint32_t vga_read32(oraddr_t addr, void *dat)
84 645 markom
{
85 1368 nogj
  struct vga_state *vga = dat;
86 645 markom
 
87 1368 nogj
  addr -= vga->baseaddr;
88 645 markom
 
89
  switch (addr) {
90 1368 nogj
    case VGA_CTRL:  return vga->ctrl;
91
    case VGA_STAT:  return vga->stat;
92
    case VGA_HTIM:  return vga->htim;
93
    case VGA_VTIM:  return vga->vtim;
94
    case VGA_HVLEN: return ((vga->hlen - 2) << 16) | (vga->vlen - 2);
95
    case VGA_VBARA: return vga->vbar[0];
96
    case VGA_VBARB: return vga->vbar[1];
97 645 markom
    default:
98
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
99 1368 nogj
        return vga->palette[0][addr - VGA_CLUTA];
100 645 markom
      } else if (addr >= VGA_CLUTB) {
101 1368 nogj
        return vga->palette[1][addr - VGA_CLUTB];
102 645 markom
      } else {
103 1350 nogj
        fprintf( stderr, "vga_read32( 0x%"PRIxADDR" ): Out of range\n", addr);
104 884 markom
        runtime.sim.cont_run = 0;
105 645 markom
        return 0;
106
      }
107
      break;
108
  }
109
  return 0;
110
}
111
 
112
/* This code will only work on little endian machines */
113
#ifdef __BIG_ENDIAN__
114
#warning Image dump not supported on big endian machines 
115
 
116 1368 nogj
static int vga_dump_image (char *filename, struct vga_start *vga)
117 645 markom
{
118
  return 1;
119
}
120
 
121
#else 
122
 
123
typedef struct {
124
   unsigned short int type;                 /* Magic identifier            */
125
   unsigned int size;                       /* File size in bytes          */
126
   unsigned short int reserved1, reserved2;
127
   unsigned int offset;                     /* Offset to image data, bytes */
128
} BMP_HEADER;
129
 
130
typedef struct {
131
   unsigned int size;               /* Header size in bytes      */
132
   int width,height;                /* Width and height of image */
133
   unsigned short int planes;       /* Number of colour planes   */
134
   unsigned short int bits;         /* Bits per pixel            */
135
   unsigned int compression;        /* Compression type          */
136
   unsigned int imagesize;          /* Image size in bytes       */
137
   int xresolution,yresolution;     /* Pixels per meter          */
138
   unsigned int ncolours;           /* Number of colours         */
139
   unsigned int importantcolours;   /* Important colours         */
140
} INFOHEADER;
141
 
142
 
143
/* Dumps a bmp file, based on current image */
144 1368 nogj
static int vga_dump_image (char *filename, struct vga_state *vga)
145 645 markom
{
146 1368 nogj
  int sx = vga->hlen;
147
  int sy = vga->vlen;
148 645 markom
  int i, x, y;
149 1368 nogj
  int pc = vga->ctrl & VGA_CTRL_PC;
150
  int rbpp = vga->ctrl & VGA_CTRL_CD;
151 645 markom
  int bpp = rbpp >> 8;
152
 
153
  BMP_HEADER bh;
154
  INFOHEADER ih;
155
  FILE *fo;
156
 
157 1368 nogj
  if (!sx || !sy) return 1;
158 645 markom
 
159
  /* 16bpp and 32 bpp will be converted to 24bpp */
160
  if (bpp == 1 || bpp == 3) bpp = 2;
161
 
162
  bh.type = 19778; /* BM */
163
  bh.size = sizeof (BMP_HEADER) + sizeof (INFOHEADER) + sx * sy * (bpp * 4 + 4) + (pc ? 1024 : 0);
164
  bh.reserved1 = bh.reserved2 = 0;
165
  bh.offset = sizeof (BMP_HEADER) + sizeof (INFOHEADER) + (pc ? 1024 : 0);
166
 
167
  ih.size = sizeof (INFOHEADER);
168
  ih.width = sx; ih.height = sy;
169
  ih.planes = 1; ih.bits = bpp * 4 + 4;
170
  ih.compression = 0; ih.imagesize = x * y * (bpp * 4 + 4);
171
  ih.xresolution = ih.yresolution = 0;
172
  ih.ncolours = 0; /* should be generated */
173
  ih.importantcolours = 0; /* all are important */
174
 
175
  fo = fopen (filename, "wb+");
176
  if (!fwrite (&bh, sizeof (BMP_HEADER), 1, fo)) return 1;
177
  if (!fwrite (&ih, sizeof (INFOHEADER), 1, fo)) return 1;
178
 
179
  if (pc) { /* Write palette? */
180
    for (i = 0; i < 256; i++) {
181
      unsigned long val, d;
182 1368 nogj
      d = vga->palette[vga->pindex][i];
183 645 markom
      val = (d >> 0) & 0xff;   /* Blue */
184
      val |= (d >> 8) & 0xff;  /* Green */
185
      val |= (d >> 16) & 0xff; /* Red */
186
      if (!fwrite (&val, sizeof (val), 1, fo)) return 1;
187
    }
188
  }
189
 
190
  /* Data is stored upside down */
191
  for (y = sy - 1; y >= 0; y--) {
192
    int align = 4 - ((bpp + 1) * sx) % 4;
193
    int zero = 0;
194
    for (x = 0; x < sx; x++) {
195 1368 nogj
      unsigned long pixel = evalsim_mem32 (vga->vbar[vga->vbindex] + (y * sx + x) * (bpp + 1));
196 645 markom
      if (!fwrite (&pixel, sizeof (pixel), 1, fo)) return 1;
197
    }
198
    if (!fwrite (&zero, align, 1, fo)) return 1;
199
  }
200
 
201
  fclose (fo);
202
  return 0;
203
}
204
#endif /* !__BIG_ENDIAN__ */
205
 
206 1368 nogj
void vga_job (void *dat)
207 805 markom
{
208 1368 nogj
  struct vga_state *vga = dat;
209 805 markom
  /* dump the image? */
210
  char temp[STR_SIZE];
211 1368 nogj
  sprintf (temp, "%s%04i.bmp", vga->filename, vga->pics++);
212
  vga_dump_image (temp, vga);
213 805 markom
 
214 1390 nogj
  SCHED_ADD(vga_job, dat, vga->refresh_rate);
215 805 markom
}
216
 
217 645 markom
/* Reset all VGAs */
218 1368 nogj
void vga_reset (void *dat)
219 645 markom
{
220 1368 nogj
  struct vga_state *vga = dat;
221 645 markom
 
222 1368 nogj
  int i;
223 645 markom
 
224 1368 nogj
  /* Init palette */
225
  for (i = 0; i < 256; i++)
226
    vga->palette[0][i] = vga->palette[1][i] = 0;
227 645 markom
 
228 1368 nogj
  vga->ctrl = vga->stat = vga->htim = vga->vtim = 0;
229
  vga->hlen = vga->vlen = 0;
230
  vga->vbar[0] = vga->vbar[1] = 0;
231
 
232
  /* Init screen dumping machine */
233
  vga->pics = 0;
234 645 markom
 
235 1368 nogj
  vga->pindex = 0;
236
  vga->vbindex = 0;
237
 
238 1390 nogj
  SCHED_ADD(vga_job, dat, vga->refresh_rate);
239 645 markom
}
240 1358 nogj
 
241
/*----------------------------------------------------[ VGA Configuration ]---*/
242
void vga_baseaddr(union param_val val, void *dat)
243
{
244 1368 nogj
  struct vga_state *vga = dat;
245
  vga->baseaddr = val.addr_val;
246 1358 nogj
}
247
 
248
void vga_irq(union param_val val, void *dat)
249
{
250 1368 nogj
  struct vga_state *vga = dat;
251
  vga->irq = val.int_val;
252 1358 nogj
}
253
 
254
void vga_refresh_rate(union param_val val, void *dat)
255
{
256 1368 nogj
  struct vga_state *vga = dat;
257
  vga->refresh_rate = val.int_val;
258 1358 nogj
}
259
 
260
void vga_filename(union param_val val, void *dat)
261
{
262 1368 nogj
  struct vga_state *vga = dat;
263
  if(!(vga->filename = strdup (val.str_val)));
264 1358 nogj
}
265
 
266 1461 nogj
void vga_enabled(union param_val val, void *dat)
267
{
268
  struct vga_state *vga = dat;
269
  vga->enabled = 1;
270
}
271
 
272 1368 nogj
void *vga_sec_start(void)
273
{
274
  struct vga_state *new = malloc(sizeof(struct vga_state));
275
 
276
  if(!new) {
277
    fprintf(stderr, "Peripheral VGA: Run out of memory\n");
278
    exit(-1);
279
  }
280
 
281
  new->baseaddr = 0;
282 1461 nogj
  new->enabled = 1;
283 1368 nogj
 
284
  return new;
285
}
286
 
287
void vga_sec_end(void *dat)
288
{
289
  struct vga_state *vga = dat;
290
 
291 1461 nogj
  if(!vga->enabled) {
292
    free(dat);
293
    return;
294
  }
295
 
296 1368 nogj
  if (vga->baseaddr)
297
    register_memoryarea(vga->baseaddr, VGA_ADDR_SPACE, 4, 0, vga_read32, vga_write32, dat);
298
 
299
  reg_sim_reset(vga_reset, dat);
300
}
301
 
302 1358 nogj
void reg_vga_sec(void)
303
{
304 1368 nogj
  struct config_section *sec = reg_config_sec("vga", vga_sec_start, vga_sec_end);
305 1358 nogj
 
306
  reg_config_param(sec, "baseaddr", paramt_addr, vga_baseaddr);
307 1461 nogj
  reg_config_param(sec, "enabled", paramt_int, vga_enabled);
308 1358 nogj
  reg_config_param(sec, "irq", paramt_int, vga_irq);
309
  reg_config_param(sec, "refresh_rate", paramt_int, vga_refresh_rate);
310
  reg_config_param(sec, "filename", paramt_str, vga_filename);
311
}

powered by: WebSVN 2.1.0

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