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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [peripheral/] [vga.c] - Blame information for rev 1308

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
  int sx = vga[v].hlen;
160
  int sy = vga[v].vlen;
161
  int i, x, y;
162
  int pc = vga[v].ctrl & VGA_CTRL_PC;
163
  int rbpp = vga[v].ctrl & VGA_CTRL_CD;
164
  int bpp = rbpp >> 8;
165
 
166
  BMP_HEADER bh;
167
  INFOHEADER ih;
168
  FILE *fo;
169
 
170
  if (!sx || !sy) return;
171
 
172
  /* 16bpp and 32 bpp will be converted to 24bpp */
173
  if (bpp == 1 || bpp == 3) bpp = 2;
174
 
175
  bh.type = 19778; /* BM */
176
  bh.size = sizeof (BMP_HEADER) + sizeof (INFOHEADER) + sx * sy * (bpp * 4 + 4) + (pc ? 1024 : 0);
177
  bh.reserved1 = bh.reserved2 = 0;
178
  bh.offset = sizeof (BMP_HEADER) + sizeof (INFOHEADER) + (pc ? 1024 : 0);
179
 
180
  ih.size = sizeof (INFOHEADER);
181
  ih.width = sx; ih.height = sy;
182
  ih.planes = 1; ih.bits = bpp * 4 + 4;
183
  ih.compression = 0; ih.imagesize = x * y * (bpp * 4 + 4);
184
  ih.xresolution = ih.yresolution = 0;
185
  ih.ncolours = 0; /* should be generated */
186
  ih.importantcolours = 0; /* all are important */
187
 
188
  fo = fopen (filename, "wb+");
189
  if (!fwrite (&bh, sizeof (BMP_HEADER), 1, fo)) return 1;
190
  if (!fwrite (&ih, sizeof (INFOHEADER), 1, fo)) return 1;
191
 
192
  if (pc) { /* Write palette? */
193
    for (i = 0; i < 256; i++) {
194
      unsigned long val, d;
195
      d = vga[v].palette[vga[v].pindex][i];
196
      val = (d >> 0) & 0xff;   /* Blue */
197
      val |= (d >> 8) & 0xff;  /* Green */
198
      val |= (d >> 16) & 0xff; /* Red */
199
      if (!fwrite (&val, sizeof (val), 1, fo)) return 1;
200
    }
201
  }
202
 
203
  /* Data is stored upside down */
204
  for (y = sy - 1; y >= 0; y--) {
205
    int align = 4 - ((bpp + 1) * sx) % 4;
206
    int zero = 0;
207
    for (x = 0; x < sx; x++) {
208
      unsigned long pixel = evalsim_mem32 (vga[v].vbar[vga[v].vbindex] + (y * sx + x) * (bpp + 1));
209
      if (!fwrite (&pixel, sizeof (pixel), 1, fo)) return 1;
210
    }
211
    if (!fwrite (&zero, align, 1, fo)) return 1;
212
  }
213
 
214
  fclose (fo);
215
  return 0;
216
}
217
#endif /* !__BIG_ENDIAN__ */
218
 
219 805 markom
void vga_job (int param)
220
{
221
  /* dump the image? */
222
  char temp[STR_SIZE];
223
  sprintf (temp, "%s%04i.bmp", config.vgas[param].filename, vga[param].pics++);
224
  vga_dump_image (temp, param);
225
 
226 884 markom
  SCHED_ADD(vga_job, param, runtime.sim.cycles + config.vgas[param].refresh_rate);
227 805 markom
}
228
 
229 645 markom
/* Reset all VGAs */
230
void vga_reset ()
231
{
232
  int i, j;
233
  for (i = 0; i < config.nvgas; i++) {
234
 
235
    /* Init palette */
236
    for (j = 0; j < 256; j++)
237
      vga[i].palette[0][j] = vga[i].palette[1][j] = 0;
238
 
239
    vga[i].ctrl = vga[i].stat = vga[i].htim = vga[i].vtim = 0;
240
    vga[i].hlen = vga[i].vlen = 0;
241
    vga[i].vbar[0] = vga[i].vbar[1] = 0;
242
 
243
    /* Init screen dumping machine */
244
    vga[i].pics = 0;
245
 
246
    vga[i].pindex = 0;
247
    vga[i].vbindex = 0;
248
 
249
    if (config.vgas[i].baseaddr)
250 970 simons
      register_memoryarea(config.vgas[i].baseaddr, VGA_ADDR_SPACE, 4, 0, vga_read32, vga_write32);
251 884 markom
    SCHED_ADD(vga_job, i, runtime.sim.cycles + config.vgas[i].refresh_rate);
252 645 markom
  }
253
}

powered by: WebSVN 2.1.0

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