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