1 |
645 |
markom |
/* fb.c -- Simple frame buffer
|
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 |
|
|
#include <stdio.h>
|
21 |
|
|
#include "sim-config.h"
|
22 |
|
|
#include "abstract.h"
|
23 |
|
|
#include "fb.h"
|
24 |
|
|
|
25 |
|
|
static unsigned char buffer[FB_SIZEY * FB_SIZEX];
|
26 |
|
|
static unsigned long pal[256];
|
27 |
|
|
static int fb_pic;
|
28 |
|
|
static int fb_cycles;
|
29 |
|
|
|
30 |
|
|
/* Write a register */
|
31 |
|
|
void fb_buf_write8 (unsigned long addr, unsigned long value)
|
32 |
|
|
{
|
33 |
|
|
int a = addr - config.fb.bufaddr;
|
34 |
|
|
if (a < 0 || a >= FB_SIZEY * FB_SIZEX) {
|
35 |
|
|
fprintf (stderr, "Write out of screen buffer (0x%08x)!\n", addr);
|
36 |
|
|
cont_run = 0;
|
37 |
|
|
} else buffer[a] = value;
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
/* Write a register */
|
41 |
|
|
void fb_pal_write32 (unsigned long addr, unsigned long value)
|
42 |
|
|
{
|
43 |
|
|
int a = (addr - config.fb.paladdr) / 4;
|
44 |
|
|
if (a < 0 || a >= 256) {
|
45 |
|
|
fprintf (stderr, "Write out of palette buffer (0x%08x)!\n", addr);
|
46 |
|
|
cont_run = 0;
|
47 |
|
|
} else pal[a] = value;
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
/* Read a register */
|
51 |
|
|
unsigned long fb_buf_read8 (unsigned long addr)
|
52 |
|
|
{
|
53 |
|
|
int a = (addr - config.fb.bufaddr);
|
54 |
|
|
if (a < 0 || a >= FB_SIZEY * FB_SIZEX) {
|
55 |
|
|
fprintf (stderr, "Read out of screen buffer (0x%08x)!\n", addr);
|
56 |
|
|
cont_run = 0;
|
57 |
|
|
return 0;
|
58 |
|
|
} else return buffer[a];
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
/* Read a register */
|
62 |
|
|
unsigned long fb_pal_read32 (unsigned long addr)
|
63 |
|
|
{
|
64 |
|
|
int a = (addr - config.fb.paladdr) / 4;
|
65 |
|
|
if (a < 0 || a >= 256) {
|
66 |
|
|
fprintf (stderr, "Read out of palette buffer (0x%08x)!\n", addr);
|
67 |
|
|
cont_run = 0;
|
68 |
|
|
return 0;
|
69 |
|
|
} else return pal[a];
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
/* define these also for big endian */
|
73 |
|
|
#define CNV16(x) (x)
|
74 |
|
|
#define CNV32(x) (x)
|
75 |
|
|
|
76 |
|
|
/* Dumps a bmp file, based on current image */
|
77 |
|
|
static int fb_dump_image (char *filename)
|
78 |
|
|
{
|
79 |
|
|
int sx = FB_SIZEX;
|
80 |
|
|
int sy = FB_SIZEY;
|
81 |
|
|
int i, x, y;
|
82 |
|
|
FILE *fo;
|
83 |
|
|
|
84 |
|
|
unsigned short int u16;
|
85 |
|
|
unsigned long int u32;
|
86 |
|
|
|
87 |
|
|
if (config.sim.verbose) printf ("Creating %s...", filename);
|
88 |
|
|
fo = fopen (filename, "wb+");
|
89 |
|
|
u16 = CNV16(19778); /* BM */
|
90 |
|
|
if (!fwrite (&u16, 2, 1, fo)) return 1;
|
91 |
|
|
u32 = CNV32(14 + 40 + sx * sy + 1024); /* size */
|
92 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
93 |
|
|
u32 = CNV32(0); /* reserved */
|
94 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
95 |
|
|
u32 = 14 + 40 + 1024; /* offset */
|
96 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
97 |
|
|
|
98 |
|
|
u32 = CNV32(40); /* header size */
|
99 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
100 |
|
|
u32 = CNV32(sx); /* width */
|
101 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
102 |
|
|
u32 = CNV32(sy); /* height */
|
103 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
104 |
|
|
u16 = CNV16(1); /* planes */
|
105 |
|
|
if (!fwrite (&u16, 2, 1, fo)) return 1;
|
106 |
|
|
u16 = CNV16(8); /* bits */
|
107 |
|
|
if (!fwrite (&u16, 2, 1, fo)) return 1;
|
108 |
|
|
u32 = CNV32(0); /* compression */
|
109 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
110 |
|
|
u32 = CNV32(x * y); /* image size */
|
111 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
112 |
|
|
u32 = CNV32(0); /* x resolution */
|
113 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
114 |
|
|
u32 = CNV32(0); /* y resolution */
|
115 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
116 |
|
|
u32 = CNV32(0); /* ncolours = 0; should be generated */
|
117 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
118 |
|
|
u32 = CNV32(0); /* important colours; all are important */
|
119 |
|
|
if (!fwrite (&u32, 4, 1, fo)) return 1;
|
120 |
|
|
|
121 |
|
|
for (i = 0; i < 256; i++) {
|
122 |
|
|
unsigned long val, d;
|
123 |
|
|
d = pal[i];
|
124 |
|
|
val = ((d >> 0) & 0xff) << 0; /* Blue */
|
125 |
|
|
val |= ((d >> 8) & 0xff) << 8; /* Green */
|
126 |
|
|
val |= ((d >> 16) & 0xff) << 16; /* Red */
|
127 |
|
|
val = CNV32(pal[i]);
|
128 |
|
|
if (!fwrite (&val, 4, 1, fo)) return 1;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
if (config.sim.verbose) printf ("(%i,%i)", sx, sy);
|
132 |
|
|
/* Data is stored upside down */
|
133 |
|
|
for (y = sy - 1; y >= 0; y--) {
|
134 |
|
|
int align = (4 - sx) % 4;
|
135 |
|
|
int zero = CNV32(0);
|
136 |
|
|
while (align < 0) align += 4;
|
137 |
|
|
for (x = 0; x < sx; x++)
|
138 |
|
|
fputc (buffer[y * sx + x], fo);
|
139 |
|
|
if (align && !fwrite (&zero, align, 1, fo)) return 1;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
if (config.sim.verbose) printf ("DONE\n");
|
143 |
|
|
fclose (fo);
|
144 |
|
|
return 0;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
/* Reset all VGAs */
|
148 |
|
|
void fb_reset ()
|
149 |
|
|
{
|
150 |
|
|
int i;
|
151 |
|
|
|
152 |
|
|
if (config.fb.enabled) {
|
153 |
|
|
fb_pic = 0;
|
154 |
|
|
fb_cycles = 0;
|
155 |
|
|
|
156 |
|
|
for (i = 0; i < 256; i++)
|
157 |
|
|
pal[i] = (i << 16) | (i << 8) | (i << 0);
|
158 |
|
|
if (config.fb.bufaddr)
|
159 |
|
|
register_memoryarea(config.fb.bufaddr, FB_SIZEX * FB_SIZEY, 1, fb_buf_read8, fb_buf_write8);
|
160 |
|
|
|
161 |
|
|
if (config.fb.paladdr)
|
162 |
|
|
register_memoryarea(config.fb.paladdr, 256*4, 4, fb_pal_read32, fb_pal_write32);
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
/* Handles one VGA clock */
|
167 |
|
|
void fb_clock ()
|
168 |
|
|
{
|
169 |
|
|
/* dump the image? */
|
170 |
|
|
if (fb_cycles++ >= config.fb.refresh_rate) {
|
171 |
|
|
char temp[STR_SIZE];
|
172 |
|
|
sprintf (temp, "%s%04i.bmp", &config.fb.filename[0], fb_pic);
|
173 |
|
|
fb_dump_image (temp);
|
174 |
|
|
fb_cycles = 0;
|
175 |
|
|
fb_pic++;
|
176 |
|
|
}
|
177 |
|
|
}
|