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 1350

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

powered by: WebSVN 2.1.0

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