/
hw_32x.c
491 lines (425 loc) · 11.8 KB
/
hw_32x.c
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Licensed under the BSD license
*
* debug_32x.c - Debug screen functions.
*
* Copyright (c) 2005 Marcus R. Brown <mrbrown@ocgnet.org>
* Copyright (c) 2005 James Forshaw <tyranid@gmail.com>
* Copyright (c) 2005 John Kelley <ps2dev@kelley.ca>
*
* Altered for 32X by Chilly Willy
*/
#include "32x.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
static int X = 0, Y = 0;
static int MX = 40, MY = 25;
static int init = 0;
static unsigned short fgc = 0, bgc = 0;
static unsigned char fgs = 0, bgs = 0;
static unsigned short currentFB = 0;
void Hw32xSetFGColor(int s, int r, int g, int b)
{
volatile unsigned short *palette = &MARS_CRAM;
fgs = s;
fgc = COLOR(r, g, b);
palette[fgs] = fgc;
}
void Hw32xSetBGColor(int s, int r, int g, int b)
{
volatile unsigned short *palette = &MARS_CRAM;
bgs = s;
bgc = COLOR(r, g, b);
palette[bgs] = bgc;
}
void Hw32xInit(int vmode, int lineskip)
{
volatile unsigned short *frameBuffer16 = &MARS_FRAMEBUFFER;
int i;
// Wait for the SH2 to gain access to the VDP
while ((MARS_SYS_INTMSK & MARS_SH2_ACCESS_VDP) == 0) ;
if (vmode == MARS_VDP_MODE_256)
{
// Set 8-bit paletted mode, 224 lines
MARS_VDP_DISPMODE = MARS_224_LINES | MARS_VDP_MODE_256;
// init both framebuffers
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
// rewrite line table
for (i=0; i<224/(lineskip+1); i++)
{
int j = lineskip + 1;
while (j)
{
frameBuffer16[i*(lineskip+1) + (lineskip + 1 - j)] = i*160 + 0x100; /* word offset of line */
j--;
}
}
// clear screen
for (i=0x100; i<0x10000; i++)
frameBuffer16[i] = 0;
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
// rewrite line table
for (i=0; i<224/(lineskip+1); i++)
{
int j = lineskip + 1;
while (j)
{
frameBuffer16[i*(lineskip+1) + (lineskip + 1 - j)] = i*160 + 0x100; /* word offset of line */
j--;
}
}
// clear screen
for (i=0x100; i<0x10000; i++)
frameBuffer16[i] = 0;
MX = 40;
MY = 28/(lineskip+1);
}
else if (vmode == MARS_VDP_MODE_32K)
{
// Set 16-bit direct mode, 224 lines
MARS_VDP_DISPMODE = MARS_224_LINES | MARS_VDP_MODE_32K;
// init both framebuffers
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
// rewrite line table
for (i=0; i<224/(lineskip+1); i++)
{
if (lineskip)
{
int j = lineskip + 1;
while (j)
{
frameBuffer16[i*(lineskip+1) + (lineskip + 1 - j)] = i*320 + 0x100; /* word offset of line */
j--;
}
}
else
{
if (i<200)
frameBuffer16[i] = i*320 + 0x100; /* word offset of line */
else
frameBuffer16[i] = 200*320 + 0x100; /* word offset of line */
}
}
// clear screen
for (i=0x100; i<0x10000; i++)
frameBuffer16[i] = 0;
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
// rewrite line table
for (i=0; i<224/(lineskip+1); i++)
{
if (lineskip)
{
int j = lineskip + 1;
while (j)
{
frameBuffer16[i*(lineskip+1) + (lineskip + 1 - j)] = i*320 + 0x100; /* word offset of line */
j--;
}
}
else
{
if (i<200)
frameBuffer16[i] = i*320 + 0x100; /* word offset of line */
else
frameBuffer16[i] = 200*320 + 0x100; /* word offset of line */
}
}
// clear screen
for (i=0x100; i<0x10000; i++)
frameBuffer16[i] = 0;
MX = 40;
MY = 25/(lineskip+1);
}
Hw32xSetFGColor(255,31,31,31);
Hw32xSetBGColor(0,0,0,0);
X = Y = 0;
init = vmode;
}
int Hw32xScreenGetX()
{
return X;
}
int Hw32xScreenGetY()
{
return Y;
}
void Hw32xScreenSetXY(int x, int y)
{
if( x<MX && x>=0 )
X = x;
if( y<MY && y>=0 )
Y = y;
}
void Hw32xScreenClear()
{
int i;
int l = (init == MARS_VDP_MODE_256) ? 320*224/2 + 0x100 : 320*200 + 0x100;
volatile unsigned short *frameBuffer16 = &MARS_FRAMEBUFFER;
// clear screen
for (i=0x100; i<l; i++)
frameBuffer16[i] = 0;
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
// clear screen
for (i=0x100; i<l; i++)
frameBuffer16[i] = 0;
Hw32xSetFGColor(255,31,31,31);
Hw32xSetBGColor(0,0,0,0);
X = Y = 0;
}
extern unsigned char msx[];
static void debug_put_char_16(int x, int y, unsigned char ch)
{
volatile unsigned short *fb = &MARS_FRAMEBUFFER;
int i,j;
unsigned char *font;
int vram, vram_ptr;
if(!init)
{
return;
}
vram = 0x100 + x * 8;
vram += (y * 8 * 320);
font = &msx[ (int)ch * 8];
for (i=0; i<8; i++, font++)
{
vram_ptr = vram;
for (j=0; j<8; j++)
{
if ((*font & (128 >> j)))
fb[vram_ptr] = fgc;
else
fb[vram_ptr] = bgc;
vram_ptr++;
}
vram += 320;
}
}
static void debug_put_char_8(int x, int y, unsigned char ch)
{
volatile unsigned char *fb = (volatile unsigned char *)&MARS_FRAMEBUFFER;
int i,j;
unsigned char *font;
int vram, vram_ptr;
if(!init)
{
return;
}
vram = 0x200 + x * 8;
vram += (y * 8 * 320);
font = &msx[ (int)ch * 8];
for (i=0; i<8; i++, font++)
{
vram_ptr = vram;
for (j=0; j<8; j++)
{
if ((*font & (128 >> j)))
fb[vram_ptr] = fgs;
else
fb[vram_ptr] = bgs;
vram_ptr++;
}
vram += 320;
}
}
void Hw32xScreenPutChar(int x, int y, unsigned char ch)
{
if (init == MARS_VDP_MODE_256)
{
debug_put_char_8(x, y, ch);
}
else if (init == MARS_VDP_MODE_32K)
{
debug_put_char_16(x, y, ch);
}
}
void Hw32xScreenClearLine(int Y)
{
int i;
for (i=0; i < MX; i++)
{
Hw32xScreenPutChar(i, Y, ' ');
}
}
/* Print non-nul terminated strings */
int Hw32xScreenPrintData(const char *buff, int size)
{
int i;
char c;
if(!init)
{
return 0;
}
for (i = 0; i<size; i++)
{
c = buff[i];
switch (c)
{
case '\r':
X = 0;
break;
case '\n':
X = 0;
Y++;
if (Y >= MY)
Y = 0;
Hw32xScreenClearLine(Y);
break;
case '\t':
X = (X + 4) & ~3;
if (X >= MX)
{
X = 0;
Y++;
if (Y >= MY)
Y = 0;
Hw32xScreenClearLine(Y);
}
break;
default:
Hw32xScreenPutChar(X, Y, c);
X++;
if (X >= MX)
{
X = 0;
Y++;
if (Y >= MY)
Y = 0;
Hw32xScreenClearLine(Y);
}
}
}
return i;
}
int Hw32xScreenPuts(const char *str)
{
int ret;
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
ret = Hw32xScreenPrintData(str, strlen(str));
// Flip the framebuffer selection bit and wait for it to take effect
MARS_VDP_FBCTL = currentFB ^ 1;
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
return ret;
}
void Hw32xScreenPrintf(const char *format, ...)
{
va_list opt;
char buff[256];
va_start(opt, format);
vsnprintf(buff, (size_t)sizeof(buff), format, opt);
va_end(opt);
Hw32xScreenPuts(buff);
}
void Hw32xDelay(int ticks)
{
unsigned long ct = MARS_SYS_COMM12 + ticks;
while (MARS_SYS_COMM12 < ct) ;
}
void Hw32xScreenFlip(int wait)
{
// Flip the framebuffer selection bit
MARS_VDP_FBCTL = currentFB ^ 1;
if (wait)
{
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
}
}
void Hw32xFlipWait()
{
while ((MARS_VDP_FBCTL & MARS_VDP_FS) == currentFB) ;
currentFB ^= 1;
}
// MD Command support code ---------------------------------------------
unsigned short HwMdReadPad(int port)
{
if (port == 0)
return MARS_SYS_COMM8;
else if (port == 1)
return MARS_SYS_COMM10;
else
return SEGA_CTRL_NONE;
}
unsigned char HwMdReadSram(unsigned short offset)
{
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM2 = offset;
MARS_SYS_COMM0 = 0x0100; // Read SRAM
while (MARS_SYS_COMM0) ;
return MARS_SYS_COMM2 & 0x00FF;
}
void HwMdWriteSram(unsigned char byte, unsigned short offset)
{
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM2 = offset;
MARS_SYS_COMM0 = 0x0200 | byte; // Write SRAM
while (MARS_SYS_COMM0) ;
}
int HwMdReadMouse(int port)
{
unsigned int mouse1, mouse2;
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM0 = 0x0300|port; // tells 68000 to read mouse
while (MARS_SYS_COMM0 == (0x0300|port)) ; // wait for mouse value
mouse1 = MARS_SYS_COMM0;
mouse2 = MARS_SYS_COMM2;
MARS_SYS_COMM0 = 0; // tells 68000 we got the mouse value
return (int)((mouse1 << 16) | mouse2);
}
void HwMdClearScreen(void)
{
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM0 = 0x0400; // Clear Screen (Name Table B)
while (MARS_SYS_COMM0) ;
}
void HwMdSetOffset(unsigned short offset)
{
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM2 = offset;
MARS_SYS_COMM0 = 0x0500; // Set offset (into either Name Table B or VRAM)
while (MARS_SYS_COMM0) ;
}
void HwMdSetNTable(unsigned short word)
{
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM2 = word;
MARS_SYS_COMM0 = 0x0600; // Set word at offset in Name Table B
while (MARS_SYS_COMM0) ;
}
void HwMdSetVram(unsigned short word)
{
while (MARS_SYS_COMM0) ; // wait until 68000 has responded to any earlier requests
MARS_SYS_COMM2 = word;
MARS_SYS_COMM0 = 0x0700; // Set word at offset in VRAM
while (MARS_SYS_COMM0) ;
}
void HwMdPuts(char *str, int color, int x, int y)
{
HwMdSetOffset(((y<<6) | x) << 1);
while (*str)
HwMdSetNTable(((*str++ - 0x20) & 0xFF) | color);
}
void HwMdPutc(char chr, int color, int x, int y)
{
HwMdSetOffset(((y<<6) | x) << 1);
HwMdSetNTable(((chr - 0x20) & 0xFF) | color);
}