Repository navigation
Expand file tree
/
Copy pathspg.cpp
More file actions
379 lines (302 loc) · 11.8 KB
/
Copy pathspg.cpp
File metadata and controls
379 lines (302 loc) · 11.8 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
/*
This file is part of libswirl
*/
#include "license/bsd"
#include "hw/sh4/sh4_mmio.h"
#include "spg.h"
#include "Renderer_if.h"
#include "pvr_regs.h"
#include "hw/holly/holly_intc.h"
#include "oslib/oslib.h"
#include "hw/sh4/sh4_sched.h"
#include "rend/mister_rend/mister_support.h"
//SPG emulation; Scanline/Raster beam registers & interrupts
//Time to emulate that stuff correctly ;)
u32 in_vblank = 0;
u32 clc_pvr_scanline;
u32 pvr_numscanlines = 512;
u32 prv_cur_scanline = -1;
u32 vblk_cnt = 0, vblk_cnt_stats = 0;
u32 vblank_count_monotonic = 0;
float last_fps = 0;
float last_stats = 0;
//54 mhz pixel clock :)
#define PIXEL_CLOCK (54*1000*1000/2)
u32 Line_Cycles = 0;
u32 Frame_Cycles = 0;
static int render_end_schid;
static int vblank_schid;
static int time_sync;
double speed_load_mspdf;
int mips_counter;
double full_rps;
static u32 lightgun_line = 0xffff;
static u32 lightgun_hpos;
double mspdf;
u32 fskip = 0;
extern u32 PVR_VTXC;
static bool hasrep;
void SetREP(u32 render_end_pending_cycles)
{
verify(hasrep == false);
hasrep = true;
sh4_sched_request(render_end_schid, render_end_pending_cycles);
}
void CancelREP() {
if (hasrep) {
rend_end_render();
sh4_sched_request(render_end_schid, -1);
hasrep = false;
}
}
struct SPG_impl final : SPG {
ASIC* asic;
SPG_impl(ASIC* asic) : asic(asic) { }
void CalculateSync()
{
u32 pixel_clock;
float scale_x = 1, scale_y = 1;
pixel_clock = PIXEL_CLOCK / (FB_R_CTRL.vclk_div ? 1 : 2);
//We need to calculate the pixel clock
//u32 sync_cycles = (SPG_LOAD.hcount + 1) * (SPG_LOAD.vcount + 1);
pvr_numscanlines = SPG_LOAD.vcount + 1;
Line_Cycles = (u32)((u64)SH4_MAIN_CLOCK * (u64)(SPG_LOAD.hcount + 1) / (u64)pixel_clock);
if (SPG_CONTROL.interlace)
{
//this is a temp hack
Line_Cycles /= 2;
//u32 interl_mode = VO_CONTROL.field_mode;
//if (interl_mode==2)//3 will be funny =P
// scale_y=0.5f;//single interlace
//else
scale_y = 1;
}
else
{
if (FB_R_CTRL.vclk_div)
{
scale_y = 1.0f;//non interlaced VGA mode has full resolution :)
}
else
{
scale_y = 0.5f;//non interlaced modes have half resolution
}
}
//Frame_Cycles=(u64)DCclock*(u64)sync_cycles/(u64)pixel_clock;
Frame_Cycles = pvr_numscanlines * Line_Cycles;
prv_cur_scanline = 0;
sh4_sched_request(vblank_schid, Line_Cycles);
}
int elapse_time(int tag, int cycl, int jit)
{
#if HOST_OS==OS_WINDOWS
//os_wait_cycl(cycl);
#endif
return min(max(Frame_Cycles, (u32)1 * 1000 * 1000), (u32)8 * 1000 * 1000);
}
//called from sh4 context , should update pvr/ta state and everything else
int spg_line_sched(int tag, int cycl, int jit)
{
clc_pvr_scanline += cycl;
while (clc_pvr_scanline >= Line_Cycles)//60 ~hertz = 200 mhz / 60=3333333.333 cycles per screen refresh
{
//ok .. here , after much effort , we did one line
//now , we must check for raster beam interrupts and vblank
prv_cur_scanline = (prv_cur_scanline + 1) % pvr_numscanlines;
clc_pvr_scanline -= Line_Cycles;
//Check for scanline interrupts -- really need to test the scanline values
if (SPG_VBLANK_INT.vblank_in_interrupt_line_number == prv_cur_scanline)
asic->RaiseInterrupt(holly_SCANINT1);
if (SPG_VBLANK_INT.vblank_out_interrupt_line_number == prv_cur_scanline)
asic->RaiseInterrupt(holly_SCANINT2);
if (SPG_VBLANK.vstart == prv_cur_scanline)
in_vblank = 1;
if (SPG_VBLANK.vbend == prv_cur_scanline)
in_vblank = 0;
SPG_STATUS.vsync = in_vblank;
SPG_STATUS.scanline = prv_cur_scanline;
//Vblank start -- really need to test the scanline values
if (prv_cur_scanline == 0)
{
if (SPG_CONTROL.interlace)
SPG_STATUS.fieldnum = ~SPG_STATUS.fieldnum;
else
SPG_STATUS.fieldnum = 0;
//Vblank counter
vblk_cnt++;
vblk_cnt_stats++;
vblank_count_monotonic++;
asic->RaiseInterrupt(holly_HBLank);// -> This turned out to be HBlank btw , needs to be emulated ;(
//TODO : rend_if_VBlank();
rend_vblank();//notify for vblank :)
if ((os_GetSeconds() - last_stats) > 0.5)
{
static int Last_FC;
double ts = os_GetSeconds() - last_stats;
double spd_fps = (FrameCount - Last_FC) / ts;
double spd_vbs = vblk_cnt_stats / ts;
double spd_cpu = spd_vbs * Frame_Cycles;
spd_cpu /= 1000000; //mrhz kthx
double fullvbs = (spd_vbs / spd_cpu) * 200;
double mv = 0 / ts / (spd_cpu / 200);
char mv_c = ' ';
Last_FC = FrameCount;
if (mv > 750)
{
mv /= 1000; //KV
mv_c = 'K';
}
if (mv > 750)
{
mv /= 1000; //
mv_c = 'M';
}
vblk_cnt_stats = 0;
const char* mode = 0;
const char* res = 0;
res = SPG_CONTROL.interlace ? "480i" : "240p";
if (SPG_CONTROL.NTSC == 0 && SPG_CONTROL.PAL == 1)
mode = "PAL";
else if (SPG_CONTROL.NTSC == 1 && SPG_CONTROL.PAL == 0)
mode = "NTSC";
else
{
res = SPG_CONTROL.interlace ? "480i" : "480p";
mode = "VGA";
}
double frames_done = spd_cpu / 2;
mspdf = 1 / frames_done * 1000;
char modestr[16];
snprintf(modestr, sizeof(modestr), "%s%s%4.2f", mode, res, fullvbs);
ReportEmulatorStats(spd_cpu * 100 / 200, spd_vbs, modestr, spd_fps);
UpdateStatsOSD();
last_stats = os_GetSeconds();
}
if ((os_GetSeconds() - last_fps) > 2)
{
static int Last_FC;
double ts = os_GetSeconds() - last_fps;
double spd_fps = (FrameCount - Last_FC) / ts;
double spd_vbs = vblk_cnt / ts;
double spd_cpu = spd_vbs * Frame_Cycles;
spd_cpu /= 1000000; //mrhz kthx
double fullvbs = (spd_vbs / spd_cpu) * 200;
double mv = 0 / ts / (spd_cpu / 200);
char mv_c = ' ';
Last_FC = FrameCount;
if (mv > 750)
{
mv /= 1000; //KV
mv_c = 'K';
}
if (mv > 750)
{
mv /= 1000; //
mv_c = 'M';
}
vblk_cnt = 0;
const char* mode = 0;
const char* res = 0;
res = SPG_CONTROL.interlace ? "480i" : "240p";
if (SPG_CONTROL.NTSC == 0 && SPG_CONTROL.PAL == 1)
mode = "PAL";
else if (SPG_CONTROL.NTSC == 1 && SPG_CONTROL.PAL == 0)
mode = "NTSC";
else
{
res = SPG_CONTROL.interlace ? "480i" : "480p";
mode = "VGA";
}
double frames_done = spd_cpu / 2;
mspdf = 1 / frames_done * 1000;
full_rps = (spd_fps + fskip / ts);
char temp[2048];
snprintf(temp, 2048, "%s/%c - %4.2f - %4.2f - V: %4.2f (%.2f, %s%s%4.2f) R: %4.2f+%4.2f VTX: %4.2f%c, MIPS: %.2f",
VER_SHORTNAME, 'n', mspdf, spd_cpu * 100 / 200, spd_vbs,
spd_vbs / full_rps, mode, res, fullvbs,
spd_fps, fskip / ts
, mv, mv_c, mips_counter / 1024.0 / 1024.0);
mips_counter = 0;
os_SetWindowText(temp);
fskip = 0;
last_fps = os_GetSeconds();
}
}
if (lightgun_line != 0xffff && lightgun_line == prv_cur_scanline)
{
SPG_TRIGGER_POS = ((lightgun_line & 0x3FF) << 16) | (lightgun_hpos & 0x3FF);
asic->RaiseInterrupt(holly_MAPLE_DMA);
lightgun_line = 0xffff;
}
}
//interrupts
//0
//vblank_in_interrupt_line_number
//vblank_out_interrupt_line_number
//vstart
//vbend
//pvr_numscanlines
u32 min_scanline = prv_cur_scanline + 1;
u32 min_active = pvr_numscanlines;
if (min_scanline < SPG_VBLANK_INT.vblank_in_interrupt_line_number)
min_active = min(min_active, SPG_VBLANK_INT.vblank_in_interrupt_line_number);
if (min_scanline < SPG_VBLANK_INT.vblank_out_interrupt_line_number)
min_active = min(min_active, SPG_VBLANK_INT.vblank_out_interrupt_line_number);
if (min_scanline < SPG_VBLANK.vstart)
min_active = min(min_active, SPG_VBLANK.vstart);
if (min_scanline < SPG_VBLANK.vbend)
min_active = min(min_active, SPG_VBLANK.vbend);
if (min_scanline < pvr_numscanlines)
min_active = min(min_active, pvr_numscanlines);
if (lightgun_line != 0xffff && min_scanline < lightgun_line)
min_active = min(min_active, lightgun_line);
min_active = max(min_active, min_scanline);
return (min_active - prv_cur_scanline) * Line_Cycles;
}
void read_lightgun_position(int x, int y)
{
if (y < 0 || y >= 480 || x < 0 || x >= 640)
// Off screen
lightgun_line = 0xffff;
else
{
lightgun_line = y / (SPG_CONTROL.interlace ? 2 : 1) + SPG_VBLANK_INT.vblank_out_interrupt_line_number;
lightgun_hpos = x * (SPG_HBLANK.hstart - SPG_HBLANK.hbend) / 640 + SPG_HBLANK.hbend * 2; // Ok but why *2 ????
lightgun_hpos = min((u32)0x3FF, lightgun_hpos);
}
}
int rend_end_sch(int tag, int cycl, int jitt)
{
if (settings.freerunning && !rend_render_done())
return REND_DONE_POLL_CYCLES;
hasrep = false;
asic->RaiseInterrupt(holly_RENDER_DONE);
asic->RaiseInterrupt(holly_RENDER_DONE_isp);
asic->RaiseInterrupt(holly_RENDER_DONE_vd);
rend_end_render();
return 0;
}
bool Init()
{
render_end_schid = sh4_sched_register(this, 0, STATIC_FORWARD(SPG_impl, rend_end_sch));
vblank_schid = sh4_sched_register(this, 0, STATIC_FORWARD(SPG_impl, spg_line_sched));
time_sync = sh4_sched_register(this, 0, STATIC_FORWARD(SPG_impl, elapse_time));
sh4_sched_request(time_sync, 8 * 1000 * 1000);
vblank_count_monotonic = 0;
return true;
}
void Term()
{
}
void Reset(bool Manual)
{
CalculateSync();
vblank_count_monotonic = 0;
}
};
SPG* SPG::Create(ASIC* asic) {
return new SPG_impl(asic);
}
void read_lightgun_position(int x, int y) {
dynamic_cast<SPG*>(sh4_cpu->GetA0Handler(A0H_SPG))->read_lightgun_position(x, y);
}