-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgfxa_c_impl.c
More file actions
429 lines (366 loc) · 9.45 KB
/
gfxa_c_impl.c
File metadata and controls
429 lines (366 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* gfxa_c_impl.c
* Complete C replacement for GFXA.ASM assembly graphics functions
* Provides SDL and stub implementations for DOS graphics compatibility
*/
#define USE_FULL_COMPAT_IMPL
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "compat.h"
// External keyboard table - this needs to be accessible from other modules
unsigned char keytable[256];
// Video memory buffer for VGA mode 13h (320x200x256)
uint8_t *vga_memory_buffer = NULL;
// Static variables for keyboard handling
static int keyboard_initialized = 0;
static int mouse_initialized = 0;
static int current_video_mode = 3; // Default text mode
// Mouse state variables
static int mouse_x = 0, mouse_y = 0;
static int mouse_buttons = 0;
static int mouse_rel_x = 0, mouse_rel_y = 0;
// Video memory buffer is defined above
// Forward declarations for compatibility layer functions
#ifdef USE_SDL
extern int init_sdl_compat(void);
extern void cleanup_sdl_compat(void);
extern void update_sdl_display(void);
extern int sdl_kbhit(void);
extern int sdl_getch(void);
extern uint16_t sdl_initmouse(void);
extern uint16_t sdl_readmbutton(void);
extern void sdl_relpos(int *x, int *y);
extern void sdl_setpalette(char *colorlist);
#endif
/*
* Video Mode Functions
*/
// _setmode - Set video mode (replaces assembly _setmode PROC)
void setmode(int mode) {
current_video_mode = mode;
if (mode == 0x13) { // VGA 320x200x256 mode
#ifdef USE_SDL
// Initialize SDL graphics mode
if (init_sdl_compat() < 0) {
fprintf(stderr, "Failed to initialize SDL graphics mode\n");
return;
}
#else
// Allocate video buffer for stub mode
if (!vga_memory_buffer) {
vga_memory_buffer = (uint8_t*)malloc(320 * 200);
if (vga_memory_buffer) {
memset(vga_memory_buffer, 0, 320 * 200);
}
}
#endif
} else if (mode == 3) { // Text mode
#ifdef USE_SDL
cleanup_sdl_compat();
#else
if (vga_memory_buffer) {
free(vga_memory_buffer);
vga_memory_buffer = NULL;
}
#endif
}
}
// _cls - Clear screen (replaces assembly _cls PROC)
void cls(char *screen) {
if (!screen) {
if (vga_memory_buffer) {
memset(vga_memory_buffer, 0, 320 * 200);
}
return;
}
// Clear the specified screen buffer
if (current_video_mode == 0x13) {
memset(screen, 0, 320 * 200);
} else {
// Text mode - clear 80x25 characters (2 bytes each)
memset(screen, 0, 80 * 25 * 2);
}
}
// _screenswap - Copy buffer to video memory (replaces assembly _screenswap PROC)
void screenswap(char *screen) {
if (!screen) return;
if (current_video_mode == 0x13) {
#ifdef USE_SDL
// Copy screen buffer to SDL surface and update display
if (vga_memory_buffer) {
memcpy(vga_memory_buffer, screen, 320 * 200);
update_sdl_display();
}
#else
// Stub mode - just copy to our buffer
if (vga_memory_buffer) {
memcpy(vga_memory_buffer, screen, 320 * 200);
}
#endif
}
}
// _setpalette - Set VGA palette (replaces assembly _setpalette PROC)
void setpalette(char *regs) {
if (!regs) return;
#ifdef USE_SDL
sdl_setpalette(regs);
#else
// Stub mode - no operation needed
(void)regs;
#endif
}
/*
* Mouse Functions
*/
// _initmouse - Initialize mouse (replaces assembly _initmouse PROC)
uint16_t initmouse(void) {
if (mouse_initialized) {
return 1; // Already initialized
}
#ifdef USE_SDL
uint16_t result = sdl_initmouse();
if (result) {
mouse_initialized = 1;
mouse_x = 160; // Center of 320px screen
mouse_y = 100; // Center of 200px screen
mouse_buttons = 0;
}
return result;
#else
// Stub mode - simulate successful initialization
mouse_initialized = 1;
mouse_x = 160;
mouse_y = 100;
mouse_buttons = 0;
return 1;
#endif
}
// _readmbutton - Read mouse button status (replaces assembly _readmbutton PROC)
uint16_t readmbutton(void) {
if (!mouse_initialized) {
return 0;
}
#ifdef USE_SDL
return sdl_readmbutton();
#else
// Stub mode - no buttons pressed
return 0;
#endif
}
// _relpos - Get relative mouse movement (replaces assembly _relpos PROC)
void relpos(int *x, int *y) {
if (!x || !y) return;
if (!mouse_initialized) {
*x = 0;
*y = 0;
return;
}
#ifdef USE_SDL
sdl_relpos(x, y);
#else
// Stub mode - no movement
*x = 0;
*y = 0;
#endif
}
/*
* Keyboard Functions
*/
// Initialize keyboard table
static void init_keyboard_table(void) {
memset(keytable, 0, sizeof(keytable));
keyboard_initialized = 1;
}
// _setkb - Install keyboard handler (replaces assembly _setkb PROC)
void setkb(void) {
if (!keyboard_initialized) {
init_keyboard_table();
}
#ifdef USE_SDL
// SDL handles keyboard input through event polling
// No interrupt handler needed
#else
// Stub mode - keyboard table is ready
#endif
}
// _resetkb - Remove keyboard handler (replaces assembly _resetkb PROC)
void resetkb(void) {
if (keyboard_initialized) {
memset(keytable, 0, sizeof(keytable));
keyboard_initialized = 0;
}
#ifdef USE_SDL
// SDL cleanup handled elsewhere
#else
// Stub mode - nothing to do
#endif
}
// Keyboard input handling functions
int kbhit(void) {
if (!keyboard_initialized) {
init_keyboard_table();
}
#ifdef USE_SDL
return sdl_kbhit();
#else
// Stub mode - no input available
return 0;
#endif
}
int getch(void) {
if (!keyboard_initialized) {
init_keyboard_table();
}
#ifdef USE_SDL
return sdl_getch();
#else
// Stub mode - return a safe default
return 0;
#endif
}
// Get next keypress (used by GFX.C)
int get_next_keypress(void) {
#ifdef USE_SDL
// Process SDL events and return pending key
if (kbhit()) {
return getch();
}
return 0;
#else
// Stub mode - no keypress
return 0;
#endif
}
/*
* Timer Functions
*/
// Timer tick counter (18.2 Hz like DOS)
static uint32_t timer_tick_counter = 0;
uint32_t get_ticks(void) {
#ifdef USE_SDL
// Convert SDL millisecond timer to DOS-style 18.2 Hz ticks
extern uint32_t sdl_get_ticks(void);
return sdl_get_ticks();
#else
// Stub mode - increment counter slowly
return ++timer_tick_counter;
#endif
}
void delay(uint32_t ms) {
#ifdef USE_SDL
extern void sdl_delay(uint32_t ms);
sdl_delay(ms);
#else
// Stub mode - no actual delay
(void)ms;
#endif
}
/*
* Sound Functions (stubs)
*/
void sb_Reset(int baseport) {
#ifdef USE_SDL
// TODO: Implement SDL audio initialization
#endif
(void)baseport;
}
void sb_DacSpkrOn(int baseport) {
#ifdef USE_SDL
// TODO: Implement SDL audio speaker on
#endif
(void)baseport;
}
void sb_DacSpkrOff(int baseport) {
#ifdef USE_SDL
// TODO: Implement SDL audio speaker off
#endif
(void)baseport;
}
void sb_Speed(int baseport, int speed, unsigned char value) {
#ifdef USE_SDL
// TODO: Implement SDL audio speed setting
#endif
(void)baseport;
(void)speed;
(void)value;
}
void sb_Play(int baseport, int buffsize) {
#ifdef USE_SDL
// TODO: Implement SDL audio playback
#endif
(void)baseport;
(void)buffsize;
}
/*
* Utility Functions
*/
// Clean exit function
void cleanExit(int retval) {
// Reset video mode
if (current_video_mode == 0x13) {
setmode(3); // Return to text mode
}
// Clean up keyboard
if (keyboard_initialized) {
resetkb();
}
// Clean up SDL
#ifdef USE_SDL
cleanup_sdl_compat();
#endif
// Free video buffer
if (vga_memory_buffer) {
free(vga_memory_buffer);
vga_memory_buffer = NULL;
}
exit(retval);
}
/*
* Compatibility Function Aliases
* These provide the exact function names expected by the DOS code
*/
// Video functions with underscore prefix (DOS calling convention)
void _setmode(int mode) { setmode(mode); }
void _cls(char *screen) { cls(screen); }
void _screenswap(char *screen) { screenswap(screen); }
void _setpalette(char *regs) { setpalette(regs); }
// Mouse functions with underscore prefix
uint16_t _initmouse(void) { return initmouse(); }
uint16_t _readmbutton(void) { return readmbutton(); }
void _relpos(int *x, int *y) { relpos(x, y); }
// Keyboard functions with underscore prefix
void _setkb(void) { setkb(); }
void _resetkb(void) { resetkb(); }
/*
* Initialization and Cleanup
*/
// Initialize all graphics subsystems
int init_gfx_compat(void) {
// Initialize keyboard
init_keyboard_table();
// Initialize video buffer
if (!vga_memory_buffer) {
vga_memory_buffer = (uint8_t*)malloc(320 * 200);
if (vga_memory_buffer) {
memset(vga_memory_buffer, 0, 320 * 200);
} else {
return -1;
}
}
#ifdef USE_SDL
return init_sdl_compat();
#else
return 0; // Stub mode always succeeds
#endif
}
// Cleanup all graphics subsystems
void cleanup_gfx_compat(void) {
resetkb();
if (vga_memory_buffer) {
free(vga_memory_buffer);
vga_memory_buffer = NULL;
}
#ifdef USE_SDL
cleanup_sdl_compat();
#endif
}