sim: bfin: handle AZ updates with 16bit adds/subs
[deliverable/binutils-gdb.git] / sim / bfin / gui.c
CommitLineData
ef016f83
MF
1/* Blackfin GUI (SDL) helper code
2
3 Copyright (C) 2010-2011 Free Software Foundation, Inc.
4 Contributed by Analog Devices, Inc.
5
6 This file is part of simulators.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22
23#ifdef HAVE_SDL
24# include <SDL.h>
25#endif
26#ifdef HAVE_DLFCN_H
27# include <dlfcn.h>
28#endif
29
30#include "libiberty.h"
31#include "gui.h"
32
33#ifdef HAVE_SDL
34
35static struct {
36 void *handle;
37 int (*Init) (Uint32 flags);
38 void (*Quit) (void);
39 SDL_Surface *(*SetVideoMode) (int width, int height, int bpp, Uint32 flags);
40 void (*WM_SetCaption) (const char *title, const char *icon);
41 int (*ShowCursor) (int toggle);
42 int (*LockSurface) (SDL_Surface *surface);
43 void (*UnlockSurface) (SDL_Surface *surface);
44 void (*GetRGB) (Uint32 pixel, const SDL_PixelFormat * const fmt, Uint8 *r, Uint8 *g, Uint8 *b);
45 Uint32 (*MapRGB) (const SDL_PixelFormat * const format, const Uint8 r, const Uint8 g, const Uint8 b);
46 void (*UpdateRect) (SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
47} sdl;
48
49static const char * const sdl_syms[] = {
50 "SDL_Init",
51 "SDL_Quit",
52 "SDL_SetVideoMode",
53 "SDL_WM_SetCaption",
54 "SDL_ShowCursor",
55 "SDL_LockSurface",
56 "SDL_UnlockSurface",
57 "SDL_GetRGB",
58 "SDL_MapRGB",
59 "SDL_UpdateRect",
60};
61
62struct gui_state {
63 SDL_Surface *screen;
64 const SDL_PixelFormat *format;
65 int throttle, throttle_limit;
66 enum gui_color color;
67 int curr_line;
68};
69
70/* Load the SDL lib on the fly to avoid hard linking against it. */
71static int
72bfin_gui_sdl_setup (void)
73{
74 int i;
75 uintptr_t **funcs;
76
77 if (sdl.handle)
78 return 0;
79
80 sdl.handle = dlopen ("libSDL-1.2.so.0", RTLD_LAZY);
81 if (sdl.handle == NULL)
82 return -1;
83
84 funcs = (void *) &sdl.Init;
85 for (i = 0; i < ARRAY_SIZE (sdl_syms); ++i)
86 {
87 funcs[i] = dlsym (sdl.handle, sdl_syms[i]);
88 if (funcs[i] == NULL)
89 {
90 dlclose (sdl.handle);
91 sdl.handle = NULL;
92 return -1;
93 }
94 }
95
96 return 0;
97}
98
99static const SDL_PixelFormat *bfin_gui_color_format (enum gui_color color);
100
101void *
102bfin_gui_setup (void *state, int enabled, int width, int height,
103 enum gui_color color)
104{
105 if (bfin_gui_sdl_setup ())
106 return NULL;
107
108 /* Create an SDL window if enabled and we don't have one yet. */
109 if (enabled && !state)
110 {
111 struct gui_state *gui = xmalloc (sizeof (*gui));
112 if (!gui)
113 return NULL;
114
115 if (sdl.Init (SDL_INIT_VIDEO))
116 goto error;
117
118 gui->color = color;
119 gui->format = bfin_gui_color_format (gui->color);
120 gui->screen = sdl.SetVideoMode (width, height, 32,
121 SDL_ANYFORMAT|SDL_HWSURFACE);
122 if (!gui->screen)
123 {
124 sdl.Quit();
125 goto error;
126 }
127
128 sdl.WM_SetCaption ("GDB Blackfin Simulator", NULL);
129 sdl.ShowCursor (0);
130 gui->curr_line = 0;
131 gui->throttle = 0;
132 gui->throttle_limit = 0xf; /* XXX: let people control this ? */
133 return gui;
134
135 error:
136 free (gui);
137 return NULL;
138 }
139
140 /* Else break down a window if disabled and we had one. */
141 else if (!enabled && state)
142 {
143 sdl.Quit();
144 free (state);
145 return NULL;
146 }
147
148 /* Retain existing state, whatever that may be. */
149 return state;
150}
151
152static int
153SDL_ConvertBlitLineFrom (const Uint8 *src, const SDL_PixelFormat * const format,
154 SDL_Surface *dst, int dsty)
155{
156 Uint8 r, g, b;
157 Uint32 *pixels;
158 unsigned i, j;
159
160 if (SDL_MUSTLOCK (dst))
161 if (sdl.LockSurface (dst))
162 return 1;
163
164 pixels = dst->pixels;
165 pixels += (dsty * dst->pitch / 4);
166
167 for (i = 0; i < dst->w; ++i)
168 {
169 /* Exract the packed source pixel; RGB or BGR. */
170 Uint32 pix = 0;
171 for (j = 0; j < format->BytesPerPixel; ++j)
172 if (format->Rshift)
173 pix = (pix << 8) | src[j];
174 else
175 pix = pix | ((Uint32)src[j] << (j * 8));
176
177 /* Unpack the source pixel into its components. */
178 sdl.GetRGB (pix, format, &r, &g, &b);
179 /* Translate into the screen pixel format. */
180 *pixels++ = sdl.MapRGB (dst->format, r, g, b);
181
182 src += format->BytesPerPixel;
183 }
184
185 if (SDL_MUSTLOCK (dst))
186 sdl.UnlockSurface (dst);
187
188 sdl.UpdateRect (dst, 0, dsty, dst->w, 1);
189
190 return 0;
191}
192
193unsigned
194bfin_gui_update (void *state, const void *source, unsigned nr_bytes)
195{
196 struct gui_state *gui = state;
197 int ret;
198
199 if (!gui)
200 return 0;
201
202 /* XXX: Make this an option ? */
203 gui->throttle = (gui->throttle + 1) & gui->throttle_limit;
204 if (gui->throttle)
205 return 0;
206
207 ret = SDL_ConvertBlitLineFrom (source, gui->format, gui->screen,
208 gui->curr_line);
209 if (ret)
210 return 0;
211
212 gui->curr_line = (gui->curr_line + 1) % gui->screen->h;
213
214 return nr_bytes;
215}
216
217#define FMASK(cnt, shift) (((1 << (cnt)) - 1) << (shift))
218#define _FORMAT(bpp, rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) \
219 NULL, bpp, (bpp)/8, 8-(rcnt), 8-(gcnt), 8-(bcnt), 8-(acnt), rsh, gsh, bsh, ash, \
220 FMASK (rcnt, rsh), FMASK (gcnt, gsh), FMASK (bcnt, bsh), FMASK (acnt, ash),
221#define FORMAT(rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) \
222 _FORMAT(((((rcnt) + (gcnt) + (bcnt) + (acnt)) + 7) / 8) * 8, \
223 rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash)
224
225static const SDL_PixelFormat sdl_rgb_565 = {
226 FORMAT (5, 6, 5, 0, 11, 5, 0, 0)
227};
228static const SDL_PixelFormat sdl_bgr_565 = {
229 FORMAT (5, 6, 5, 0, 0, 5, 11, 0)
230};
231static const SDL_PixelFormat sdl_rgb_888 = {
232 FORMAT (8, 8, 8, 0, 16, 8, 0, 0)
233};
234static const SDL_PixelFormat sdl_bgr_888 = {
235 FORMAT (8, 8, 8, 0, 0, 8, 16, 0)
236};
237static const SDL_PixelFormat sdl_rgba_8888 = {
238 FORMAT (8, 8, 8, 8, 24, 16, 8, 0)
239};
240
241static const struct {
242 const char *name;
243 const SDL_PixelFormat *format;
244 enum gui_color color;
245} color_spaces[] = {
246 { "rgb565", &sdl_rgb_565, GUI_COLOR_RGB_565, },
247 { "bgr565", &sdl_bgr_565, GUI_COLOR_BGR_565, },
248 { "rgb888", &sdl_rgb_888, GUI_COLOR_RGB_888, },
249 { "bgr888", &sdl_bgr_888, GUI_COLOR_BGR_888, },
250 { "rgba8888", &sdl_rgba_8888, GUI_COLOR_RGBA_8888, },
251};
252
253enum gui_color bfin_gui_color (const char *color)
254{
255 int i;
256
257 if (!color)
258 goto def;
259
260 for (i = 0; i < ARRAY_SIZE (color_spaces); ++i)
261 if (!strcmp (color, color_spaces[i].name))
262 return color_spaces[i].color;
263
264 /* Pick a random default. */
265 def:
266 return GUI_COLOR_RGB_888;
267}
268
269static const SDL_PixelFormat *bfin_gui_color_format (enum gui_color color)
270{
271 int i;
272
273 for (i = 0; i < ARRAY_SIZE (color_spaces); ++i)
274 if (color == color_spaces[i].color)
275 return color_spaces[i].format;
276
277 return NULL;
278}
279
280int bfin_gui_color_depth (enum gui_color color)
281{
282 const SDL_PixelFormat *format = bfin_gui_color_format (color);
283 return format ? format->BitsPerPixel : 0;
284}
285
286#endif
This page took 0.059887 seconds and 4 git commands to generate.