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 884

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
#include "sim-config.h"
22
#include "vga.h"
23
#include "abstract.h"
24 805 markom
#include "sched.h"
25 645 markom
 
26
/* When this counter reaches config.vgas[].refresh_rate, a screenshot is taken and outputted */
27
static struct {
28
  int pics;
29
  unsigned long ctrl, stat, htim, vtim;
30
  int vbindex;
31
  unsigned long vbar[2];
32
  unsigned hlen, vlen;
33
  int pindex;
34
  unsigned long palette[2][256];
35
} vga[MAX_VGAS];
36
 
37
 
38
/* Write a register */
39
void vga_write32(unsigned long addr, unsigned long value)
40
{
41
  int i, found = -1;
42
 
43
  /* Find which controller this is */
44
  for (i = 0; i < config.nvgas; i++ ) {
45
    if ((addr >= config.vgas[i].baseaddr) && (addr < config.vgas[i].baseaddr + VGA_ADDR_SPACE)) {
46
      found = i;
47
      break;
48
    }
49
  }
50
 
51
  if (found < 0) {
52
    fprintf( stderr, "vga_write32( 0x%08lX ): Out of range\n", addr);
53 884 markom
    runtime.sim.cont_run = 0;
54 645 markom
    return;
55
  }
56
 
57
  addr -= config.vgas[found].baseaddr;
58
 
59
  switch (addr) {
60
    case VGA_CTRL:  vga[found].ctrl = value; break;
61
    case VGA_STAT:  vga[found].stat = value; break;
62
    case VGA_HTIM:  vga[found].htim = value; break;
63
    case VGA_VTIM:  vga[found].vtim = value; break;
64
    case VGA_HVLEN: vga[found].hlen = (value >> 16) + 2; vga[found].hlen = (value & 0xffff) + 2; break;
65
    case VGA_VBARA: vga[found].vbar[0] = value; break;
66
    case VGA_VBARB: vga[found].vbar[1] = value; break;
67
    default:
68
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
69
        vga[found].palette[0][addr - VGA_CLUTA] = value & 0x00ffffff;
70
      } else if (addr >= VGA_CLUTB) {
71
        vga[found].palette[1][addr - VGA_CLUTB] = value & 0x00ffffff;
72
      } else {
73
        fprintf( stderr, "vga_write32( 0x%08lX, 0x%08lX ): Out of range\n", addr + config.vgas[found].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
unsigned long vga_read32(unsigned long addr)
83
{
84
  int i, found = -1;
85
 
86
  /* Find which controller this is */
87
  for (i = 0; i < config.nvgas; i++ ) {
88
    if ((addr >= config.vgas[i].baseaddr) && (addr < config.vgas[i].baseaddr + VGA_ADDR_SPACE)) {
89
      found = i;
90
      break;
91
    }
92
  }
93
 
94
  if (found < 0) {
95
    fprintf( stderr, "vga_read32( 0x%08lX ): Out of range\n", addr);
96 884 markom
    runtime.sim.cont_run = 0;
97 645 markom
    return 0;
98
  }
99
 
100
  addr -= config.vgas[found].baseaddr;
101
 
102
  switch (addr) {
103
    case VGA_CTRL:  return vga[found].ctrl;
104
    case VGA_STAT:  return vga[found].stat;
105
    case VGA_HTIM:  return vga[found].htim;
106
    case VGA_VTIM:  return vga[found].vtim;
107
    case VGA_HVLEN: return ((vga[found].hlen - 2) << 16) | (vga[found].vlen - 2);
108
    case VGA_VBARA: return vga[found].vbar[0];
109
    case VGA_VBARB: return vga[found].vbar[1];
110
    default:
111
      if (addr >= VGA_CLUTA && addr < VGA_CLUTB) {
112
        return vga[found].palette[0][addr - VGA_CLUTA];
113
      } else if (addr >= VGA_CLUTB) {
114
        return vga[found].palette[1][addr - VGA_CLUTB];
115
      } else {
116
        fprintf( stderr, "vga_read32( 0x%08lX ): Out of range\n", addr);
117 884 markom
        runtime.sim.cont_run = 0;
118 645 markom
        return 0;
119
      }
120
      break;
121
  }
122
  return 0;
123
}
124
 
125
/* This code will only work on little endian machines */
126
#ifdef __BIG_ENDIAN__
127
#warning Image dump not supported on big endian machines 
128
 
129
static int vga_dump_image (char *filename, int v)
130
{
131
  return 1;
132
}
133
 
134
#else 
135
 
136
typedef struct {
137
   unsigned short int type;                 /* Magic identifier            */
138
   unsigned int size;                       /* File size in bytes          */
139
   unsigned short int reserved1, reserved2;
140
   unsigned int offset;                     /* Offset to image data, bytes */
141
} BMP_HEADER;
142
 
143
typedef struct {
144
   unsigned int size;               /* Header size in bytes      */
145
   int width,height;                /* Width and height of image */
146
   unsigned short int planes;       /* Number of colour planes   */
147
   unsigned short int bits;         /* Bits per pixel            */
148
   unsigned int compression;        /* Compression type          */
149
   unsigned int imagesize;          /* Image size in bytes       */
150
   int xresolution,yresolution;     /* Pixels per meter          */
151
   unsigned int ncolours;           /* Number of colours         */
152
   unsigned int importantcolours;   /* Important colours         */
153
} INFOHEADER;
154
 
155
 
156
/* Dumps a bmp file, based on current image */
157
static int vga_dump_image (char *filename, int v)
158
{
159
  unsigned long addr = config.vgas[v].baseaddr;
160
  int sx = vga[v].hlen;
161
  int sy = vga[v].vlen;
162
  int i, x, y;
163
  int pc = vga[v].ctrl & VGA_CTRL_PC;
164
  int rbpp = vga[v].ctrl & VGA_CTRL_CD;
165
  int bpp = rbpp >> 8;
166
 
167
  BMP_HEADER bh;
168
  INFOHEADER ih;
169
  FILE *fo;
170
 
171
  if (!sx || !sy) return;
172
 
173
  /* 16bpp and 32 bpp will be converted to 24bpp */
174
  if (bpp == 1 || bpp == 3) bpp = 2;
175
 
176
  bh.type = 19778; /* BM */
177
  bh.size = sizeof (BMP_HEADER) + sizeof (INFOHEADER) + sx * sy * (bpp * 4 + 4) + (pc ? 1024 : 0);
178
  bh.reserved1 = bh.reserved2 = 0;
179
  bh.offset = sizeof (BMP_HEADER) + sizeof (INFOHEADER) + (pc ? 1024 : 0);
180
 
181
  ih.size = sizeof (INFOHEADER);
182
  ih.width = sx; ih.height = sy;
183
  ih.planes = 1; ih.bits = bpp * 4 + 4;
184
  ih.compression = 0; ih.imagesize = x * y * (bpp * 4 + 4);
185
  ih.xresolution = ih.yresolution = 0;
186
  ih.ncolours = 0; /* should be generated */
187
  ih.importantcolours = 0; /* all are important */
188
 
189
  fo = fopen (filename, "wb+");
190
  if (!fwrite (&bh, sizeof (BMP_HEADER), 1, fo)) return 1;
191
  if (!fwrite (&ih, sizeof (INFOHEADER), 1, fo)) return 1;
192
 
193
  if (pc) { /* Write palette? */
194
    for (i = 0; i < 256; i++) {
195
      unsigned long val, d;
196
      d = vga[v].palette[vga[v].pindex][i];
197
      val = (d >> 0) & 0xff;   /* Blue */
198
      val |= (d >> 8) & 0xff;  /* Green */
199
      val |= (d >> 16) & 0xff; /* Red */
200
      if (!fwrite (&val, sizeof (val), 1, fo)) return 1;
201
    }
202
  }
203
 
204
  /* Data is stored upside down */
205
  for (y = sy - 1; y >= 0; y--) {
206
    int align = 4 - ((bpp + 1) * sx) % 4;
207
    int zero = 0;
208
    for (x = 0; x < sx; x++) {
209
      unsigned long pixel = evalsim_mem32 (vga[v].vbar[vga[v].vbindex] + (y * sx + x) * (bpp + 1));
210
      if (!fwrite (&pixel, sizeof (pixel), 1, fo)) return 1;
211
    }
212
    if (!fwrite (&zero, align, 1, fo)) return 1;
213
  }
214
 
215
  fclose (fo);
216
  return 0;
217
}
218
#endif /* !__BIG_ENDIAN__ */
219
 
220 805 markom
void vga_job (int param)
221
{
222
  /* dump the image? */
223
  char temp[STR_SIZE];
224
  sprintf (temp, "%s%04i.bmp", config.vgas[param].filename, vga[param].pics++);
225
  vga_dump_image (temp, param);
226
 
227 884 markom
  SCHED_ADD(vga_job, param, runtime.sim.cycles + config.vgas[param].refresh_rate);
228 805 markom
}
229
 
230 645 markom
/* Reset all VGAs */
231
void vga_reset ()
232
{
233
  int i, j;
234
  for (i = 0; i < config.nvgas; i++) {
235
 
236
    /* Init palette */
237
    for (j = 0; j < 256; j++)
238
      vga[i].palette[0][j] = vga[i].palette[1][j] = 0;
239
 
240
    vga[i].ctrl = vga[i].stat = vga[i].htim = vga[i].vtim = 0;
241
    vga[i].hlen = vga[i].vlen = 0;
242
    vga[i].vbar[0] = vga[i].vbar[1] = 0;
243
 
244
    /* Init screen dumping machine */
245
    vga[i].pics = 0;
246
 
247
    vga[i].pindex = 0;
248
    vga[i].vbindex = 0;
249
 
250
    if (config.vgas[i].baseaddr)
251
      register_memoryarea(config.vgas[i].baseaddr, VGA_ADDR_SPACE, 4, vga_read32, vga_write32);
252 884 markom
    SCHED_ADD(vga_job, i, runtime.sim.cycles + config.vgas[i].refresh_rate);
253 645 markom
  }
254
}

powered by: WebSVN 2.1.0

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