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 1390

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

powered by: WebSVN 2.1.0

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