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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [peripheral/] [vga.c] - Blame information for rev 1486

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

powered by: WebSVN 2.1.0

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