-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
428 lines (362 loc) · 14.5 KB
/
main.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
#include <stdlib.h>
#include <stdint.h>
#include "32x.h"
#include "hw_32x.h"
#include "sound.h"
#include "types.h"
#include "draw.h"
#include "assets/platformer/pla_VGA.h"
#include "assets/platformer/pla_VGA_palette.h"
#include "assets/platformer/platformer.h"
uint8_t test32x32_trans_smileData[] __attribute__((aligned(16))) =
{
/* trans */
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,00,00,00,00,00,00,02,02,02,02,02,02,02,00,00,00,00,00,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,05,05,05,05,05,02,02,02,02,02,02,02,05,05,05,05,05,05,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,00,00,00,02,02,02,03,03,03,03,03,03,03,02,02,02,00,00,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,05,05,02,02,02,03,03,03,03,03,03,03,02,02,02,05,05,05,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,00,02,02,03,03,03,03,03,03,03,03,03,03,03,03,03,02,02,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,02,02,03,03,03,03,03,03,03,03,03,03,03,03,03,02,02,05,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,05,05,
/* trans */
00,00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,00,
/* solid */
05,05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,05,
/* trans */
00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,
/* solid */
05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,
/* trans */
00,00,02,03,03,03,03,03,03,04,04,04,03,03,03,03,03,03,03,03,02,02,02,03,03,03,03,03,02,00,00,00,
/* solid */
05,05,02,03,03,03,03,03,03,04,04,04,03,03,03,03,03,03,03,03,02,02,02,03,03,03,03,03,02,05,05,05,
/* trans */
00,00,02,03,03,03,03,03,04,04,04,04,04,03,03,03,03,03,03,02,02,02,02,02,03,03,03,03,02,00,00,00,
/* solid */
05,05,02,03,03,03,03,03,04,04,04,04,04,03,03,03,03,03,03,02,02,02,02,02,03,03,03,03,02,05,05,05,
/* trans */
00,02,03,03,03,03,03,03,04,04,04,04,04,03,03,03,03,03,03,02,02,02,02,02,03,03,03,03,03,02,00,00,
/* solid */
05,02,03,03,03,03,03,03,04,04,04,04,04,03,03,03,03,03,03,02,02,02,02,02,03,03,03,03,03,02,05,05,
/* trans */
00,02,03,03,03,03,03,03,04,04,04,04,04,03,03,03,03,03,03,02,02,02,02,02,03,03,03,03,03,02,00,00,
/* solid */
05,02,03,03,03,03,03,03,04,04,04,04,04,03,03,03,03,03,03,02,02,02,02,02,03,03,03,03,03,02,05,05,
/* trans */
00,02,03,03,03,03,03,03,03,04,04,04,03,03,03,03,03,03,03,03,02,02,02,03,03,03,03,03,03,02,00,00,
/* solid */
05,02,03,03,03,03,03,03,03,04,04,04,03,03,03,03,03,03,03,03,02,02,02,03,03,03,03,03,03,02,05,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,
/* solid */
02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,
/* trans */
00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,
/* solid */
05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,
/* trans */
00,02,03,03,03,03,03,03,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,03,03,03,03,03,02,00,00,
/* solid */
05,02,03,03,03,03,03,03,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,03,03,03,03,03,02,05,05,
/* trans */
00,02,03,03,03,03,03,03,03,02,02,02,02,02,02,02,02,02,02,02,02,02,02,03,03,03,03,03,03,02,00,00,
/* solid */
05,02,03,03,03,03,03,03,03,02,02,02,02,02,02,02,02,02,02,02,02,02,02,03,03,03,03,03,03,02,05,05,
/* trans */
00,00,02,03,03,03,03,03,03,03,02,02,02,02,02,02,02,02,02,02,02,02,03,03,03,03,03,03,02,00,00,00,
/* solid */
05,05,02,03,03,03,03,03,03,03,02,02,02,02,02,02,02,02,02,02,02,02,03,03,03,03,03,03,02,05,05,05,
/* trans */
00,00,02,03,03,03,03,03,03,03,03,03,02,02,02,02,02,02,02,02,03,03,03,03,03,03,03,03,02,00,00,00,
/* solid */
05,05,02,03,03,03,03,03,03,03,03,03,02,02,02,02,02,02,02,02,03,03,03,03,03,03,03,03,02,05,05,05,
/* trans */
00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,
/* solid */
05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,
/* trans */
00,00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,00,
/* solid */
05,05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,05,
/* trans */
00,00,00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,02,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,03,02,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,00,02,02,03,03,03,03,03,03,03,03,03,03,03,03,03,02,02,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,02,02,03,03,03,03,03,03,03,03,03,03,03,03,03,02,02,05,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,00,00,00,02,02,02,03,03,03,03,03,03,03,02,02,02,00,00,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,05,05,02,02,02,03,03,03,03,03,03,03,02,02,02,05,05,05,05,05,05,05,05,05,05,
/* trans */
00,00,00,00,00,00,00,00,00,00,00,00,02,02,02,02,02,02,02,00,00,00,00,00,00,00,00,00,00,00,00,00,
/* solid */
05,05,05,05,05,05,05,05,05,05,05,05,02,02,02,02,02,02,02,05,05,05,05,05,05,05,05,05,05,05,05,05,
};
extern drawsprcmd_t slave_drawsprcmd;
extern drawtilelayerscmd_t slave_drawtilelayerscmd;
int fpcamera_x, fpcamera_y;
int fpmoveinc_x = 1<<16, fpmoveinc_y = 1<<16; // in 16.16 fixed point
int nodraw = 0;
int debug = 0;
int sprmode = -1;
int window_canvas_x = 0, window_canvas_y = 0;
volatile unsigned mars_pwdt_ovf_count = 0;
volatile unsigned mars_swdt_ovf_count = 0;
unsigned mars_frtc2msec_frac = 0;
const int NTSC_CLOCK_SPEED = 23011360; // HZ
const int PAL_CLOCK_SPEED = 22801467; // HZ
int slave_task(int cmd) HW32X_ATTR_DATA_ALIGNED;
void secondary(void) HW32X_ATTR_DATA_ALIGNED;
tilemap_t tm;
int sec;
int Mars_FRTCounter2Msec(int c)
{
return (c * mars_frtc2msec_frac) >> 16;
}
int Mars_GetFRTCounter(void)
{
unsigned int cnt = SH2_WDT_RTCNT;
return (int)((mars_pwdt_ovf_count << 8) | cnt);
}
int slave_task(int cmd)
{
int drawcnt;
switch (cmd) {
case 1:
snddma_slave_init(22050);
return 1;
case 2:
ClearCacheLines(&slave_drawsprcmd, (sizeof(drawsprcmd_t) + 31) / 16);
draw_handle_drawspritecmd(&slave_drawsprcmd);
return 1;
case 3:
ClearCacheLines((uintptr_t)&canvas_width & ~15, 1);
ClearCacheLines((uintptr_t)&canvas_height & ~15, 1);
ClearCacheLines((uintptr_t)&window_canvas_x & ~15, 1);
ClearCacheLines((uintptr_t)&window_canvas_y & ~15, 1);
ClearCacheLines((uintptr_t)&canvas_pitch & ~15, 1);
ClearCacheLines((uintptr_t)&nodraw & ~15, 1);
ClearCacheLines(&slave_drawtilelayerscmd, (sizeof(drawtilelayerscmd_t) + 31) / 16);
ClearCacheLines(&tm, (sizeof(tilemap_t) + 31) / 16);
drawcnt = draw_handle_layercmd(&slave_drawtilelayerscmd);
drawcnt = ((drawcnt + 1) << 2) | MARS_SYS_COMM4;
MARS_SYS_COMM4 = drawcnt;
while (MARS_SYS_COMM4 == drawcnt);
return 1;
case 4:
return 1;
default:
break;
}
return 0;
}
void secondary(void)
{
ClearCache();
while (1) {
int cmd;
while ((cmd = MARS_SYS_COMM4) == 0);
int res = slave_task(cmd);
if (res > 0) {
MARS_SYS_COMM4 = 0;
}
}
}
void display_sprites(int l, void *p)
{
int i, j;
int start = Mars_GetFRTCounter();
draw_setScissor(0, 0, 320, 224);
if (sprmode >= 0)
{
int mode = DRAWSPR_OVERWRITE|DRAWSPR_MULTICORE;
if (sprmode <= 3)
mode |= sprmode | DRAWSPR_PRECISE;
else
mode |= (sprmode - 4);
for (j = 0; j < 5; j++)
{
for (i = 0; i < 5; i++)
draw_sprite(i * 64 + 16, j * 64 + 16, 32, 32, 64, test32x32_trans_smileData, DRAWSPR_OVERWRITE | mode);
}
draw_pivot_stretch_sprite(160, 112, 32, 32, 64, test32x32_trans_smileData, DRAWSPR_SCALE | mode, 0x10000 + ((start * 16) & 0xffff));
}
}
int display(int framecount, int hudenable, int fpscount, int totaltics, int clearhud)
{
int start = Mars_GetFRTCounter(), total;
int drawcnt = 0;
int cameraclip = 0;
static int prevsec;
static int maxdrawcnt;
if (prevsec != sec)
{
maxdrawcnt = 0;
prevsec = sec;
}
draw_setScissor(0, 0, 320, 224);
drawcnt = draw_tilemap(&tm, fpcamera_x, fpcamera_y, &cameraclip, &display_sprites, NULL);
if (drawcnt > maxdrawcnt)
{
maxdrawcnt = drawcnt;
}
total = Mars_GetFRTCounter() - start;
Hw32xScreenSetXY(0, 23);
switch (hudenable) {
case 1:
Hw32xScreenPrintf("fps:%02d %03d ms:%02d", fpscount, maxdrawcnt, Mars_FRTCounter2Msec(total));
break;
default:
break;
}
return cameraclip;
}
int main(void)
{
int framecount;
int fpscount;
int prevsec;
int prevsecframe;
int totaltics;
char hud = 0, clearhud = 0;
int ticksperframe;
int buttons, oldbuttons;
char NTSC;
Hw32xInit(MARS_VDP_MODE_256, 0);
SetSH2SR(1);
while ((MARS_SYS_INTMSK & MARS_SH2_ACCESS_VDP) == 0);
NTSC = (MARS_VDP_DISPMODE & MARS_NTSC_FORMAT) != 0;
SH2_WDT_WTCSR_TCNT = 0x5A00; /* WDT TCNT = 0 */
SH2_WDT_WTCSR_TCNT = 0xA53E; /* WDT TCSR = clr OVF, IT mode, timer on, clksel = Fs/4096 */
/* init hires timer system */
SH2_WDT_VCR = (65 << 8) | (SH2_WDT_VCR & 0x00FF); // set exception vector for WDT
SH2_INT_IPRA = (SH2_INT_IPRA & 0xFF0F) | 0x0020; // set WDT INT to priority 2
// change 4096.0f to something else if WDT TCSR is changed!
mars_frtc2msec_frac = 4096.0f * 1000.0f / (NTSC ? NTSC_CLOCK_SPEED : PAL_CLOCK_SPEED) * 65536.0f;
Hw32xSetPalette(pla_VGA_Palette);
MARS_SYS_COMM4 = 0;
MARS_SYS_COMM6 = 0;
ticksperframe = 1;
fpscount = 0;
framecount = 0;
prevsec = 0;
prevsecframe = 0;
totaltics = 0;
fpcamera_x = fpcamera_y = 0;
buttons = oldbuttons = 0;
init_tilemap(&tm, &platformer_Map, (uint8_t **)pla_VGA_Reslist);
Hw32xScreenFlip(0);
while (1) {
int starttics;
int waittics;
int clip;
int old_fpcamera_x, old_fpcamera_y;
starttics = Hw32xGetTicks();
sec = starttics / (MARS_VDP_DISPMODE & MARS_NTSC_FORMAT ? 60 : 50); // FIXME: add proper NTSC vs PAL rate detection
if (sec != prevsec) {
fpscount = (framecount - prevsecframe) / (sec - prevsec);
prevsec = sec;
prevsecframe = framecount;
}
oldbuttons = buttons;
buttons = MARS_SYS_COMM8;
int newbuttons = (buttons ^ oldbuttons) & buttons;
old_fpcamera_x = fpcamera_x;
old_fpcamera_y = fpcamera_y;
if (MARS_SYS_COMM8 & SEGA_CTRL_RIGHT) {
fpcamera_x += fpmoveinc_x;
}
else if (MARS_SYS_COMM8 & SEGA_CTRL_LEFT) {
fpcamera_x -= fpmoveinc_x;
}
if (MARS_SYS_COMM8 & SEGA_CTRL_DOWN) {
fpcamera_y += fpmoveinc_y;
}
else if (MARS_SYS_COMM8 & SEGA_CTRL_UP) {
fpcamera_y -= fpmoveinc_y;
}
if (fpcamera_x < 0) fpcamera_x = 0;
if (fpcamera_y < 0) fpcamera_y = 0;
if (newbuttons & SEGA_CTRL_B)
{
hud = (hud + 1) % 2;
clearhud = 2;
}
if (newbuttons & SEGA_CTRL_C)
{
sprmode++;
if (sprmode == 8)
sprmode = -1;
}
Hw32xFlipWait();
clip = display(framecount, hud, fpscount, totaltics, clearhud);
if (clip & 2)
{
// clipped to the right
fpcamera_x = old_fpcamera_x;
}
if (clip & 8)
{
// clipped to the right
fpcamera_y = old_fpcamera_y;
}
clearhud--;
if (clearhud < 0)
clearhud = 0;
totaltics = Hw32xGetTicks() - starttics;
waittics = totaltics;
while (waittics < ticksperframe) {
waittics = Hw32xGetTicks() - starttics;
}
framecount++;
Hw32xScreenFlip(0);
}
return 0;
}