30
30
static const UINT16 tcor[] = { TCOR0, TCOR1, TCOR2 };
31
31
static const UINT16 tcr[] = { TCR0, TCR1, TCR2 };
33
INLINE sh4_state *get_safe_token(device_t *device)
35
assert(device != NULL);
36
assert(device->type() == SH4);
37
return (sh4_state *)downcast<legacy_cpu_device *>(device)->token();
40
33
void sh4_change_register_bank(sh4_state *sh4, int to)
200
193
sh4->sgr = sh4->r[15];
203
if ((sh4->device->machine->debug_flags & DEBUG_FLAG_ENABLED) != 0)
196
if ((sh4->device->machine().debug_flags & DEBUG_FLAG_ENABLED) != 0)
204
197
sh4_syncronize_register_bank(sh4, (sh4->sr & sRB) >> 29);
205
198
if (!(sh4->sr & sRB))
206
199
sh4_change_register_bank(sh4, 1);
219
212
// elapsed:total = x : ticks
220
213
// x=elapsed*tics/total -> x=elapsed*(double)100000000/rtcnt_div[(sh4->m[RTCSR] >> 3) & 7]
221
214
// ticks/total=ticks / ((rtcnt_div[(sh4->m[RTCSR] >> 3) & 7] * ticks) / 100000000)=1/((rtcnt_div[(sh4->m[RTCSR] >> 3) & 7] / 100000000)=100000000/rtcnt_div[(sh4->m[RTCSR] >> 3) & 7]
222
return base + (UINT32)((attotime_to_double(timer_timeelapsed(timer)) * (double)hertz) / (double)divisor);
215
return base + (UINT32)((timer->elapsed().as_double() * (double)hertz) / (double)divisor);
225
218
static void sh4_refresh_timer_recompute(sh4_state *sh4)
231
224
ticks = sh4->m[RTCOR]-sh4->m[RTCNT];
233
226
ticks = 256 + ticks;
234
timer_adjust_oneshot(sh4->refresh_timer, attotime_mul(attotime_mul(ATTOTIME_IN_HZ(sh4->bus_clock), rtcnt_div[(sh4->m[RTCSR] >> 3) & 7]), ticks), 0);
227
sh4->refresh_timer->adjust(attotime::from_hz(sh4->bus_clock) * rtcnt_div[(sh4->m[RTCSR] >> 3) & 7] * ticks);
235
228
sh4->refresh_timer_base = sh4->m[RTCNT];
243
236
INLINE attotime sh4_scale_up_mame_time(attotime _time1, UINT32 factor1)
245
return attotime_add(attotime_mul(_time1, factor1), _time1);
238
return _time1 * factor1 + _time1;
248
241
static UINT32 compute_ticks_timer(emu_timer *timer, int hertz, int divisor)
252
ret=((attotime_to_double(timer_timeleft(timer)) * (double)hertz) / (double)divisor) - 1;
245
ret=((timer->remaining().as_double() * (double)hertz) / (double)divisor) - 1;
253
246
return (UINT32)ret;
260
253
ticks = sh4->m[tcnt[which]];
261
timer_adjust_oneshot(sh4->timer[which], sh4_scale_up_mame_time(attotime_mul(ATTOTIME_IN_HZ(sh4->pm_clock), tcnt_div[sh4->m[tcr[which]] & 7]), ticks), which);
254
sh4->timer[which]->adjust(sh4_scale_up_mame_time(attotime::from_hz(sh4->pm_clock) * tcnt_div[sh4->m[tcr[which]] & 7], ticks), which);
264
257
static TIMER_CALLBACK( sh4_refresh_timer_callback )
377
370
sh4_state *sh4 = (sh4_state *)ptr;
379
timer_adjust_oneshot(sh4->rtc_timer, ATTOTIME_IN_HZ(128), 0);
372
sh4->rtc_timer->adjust(attotime::from_hz(128));
380
373
sh4->m[R64CNT] = (sh4->m[R64CNT]+1) & 0x7f;
381
374
if (sh4->m[R64CNT] == 64)
460
453
if (timermode == 1)
462
455
sh4->dma_timer_active[channel] = 1;
463
timer_adjust_oneshot(sh4->dma_timer[channel], sh4->device->cycles_to_attotime(2*count+1), channel);
456
sh4->dma_timer[channel]->adjust(sh4->device->cycles_to_attotime(2*count+1), channel);
465
458
else if (timermode == 2)
467
460
sh4->dma_timer_active[channel] = 1;
468
timer_adjust_oneshot(sh4->dma_timer[channel], attotime_zero, channel);
461
sh4->dma_timer[channel]->adjust(attotime::zero, channel);
608
601
if (sh4->dma_timer_active[channel])
610
603
logerror("SH4: DMA %d cancelled in-flight but all data transferred", channel);
611
timer_adjust_oneshot(sh4->dma_timer[channel], attotime_never, channel);
604
sh4->dma_timer[channel]->adjust(attotime::never, channel);
612
605
sh4->dma_timer_active[channel] = 0;
624
617
if (sh4->dma_timer_active[s])
626
619
logerror("SH4: DMA %d cancelled due to NMI but all data transferred", s);
627
timer_adjust_oneshot(sh4->dma_timer[s], attotime_never, s);
620
sh4->dma_timer[s]->adjust(attotime::never, s);
628
621
sh4->dma_timer_active[s] = 0;
633
626
WRITE32_HANDLER( sh4_internal_w )
635
sh4_state *sh4 = get_safe_token(space->cpu);
628
sh4_state *sh4 = get_safe_token(&space->device());
637
630
UINT32 old = sh4->m[offset];
638
631
COMBINE_DATA(sh4->m+offset);
725
718
if ((sh4->m[RCR2] & 8) && (~old & 8))
727
timer_adjust_oneshot(sh4->rtc_timer, ATTOTIME_IN_HZ(128), 0);
720
sh4->rtc_timer->adjust(attotime::from_hz(128));
729
722
else if (~(sh4->m[RCR2]) & 8)
731
timer_adjust_oneshot(sh4->rtc_timer, attotime_never, 0);
724
sh4->rtc_timer->adjust(attotime::never);
738
731
sh4->m[TCNT0] = compute_ticks_timer(sh4->timer[0], sh4->pm_clock, tcnt_div[sh4->m[TCR0] & 7]);
739
732
if ((sh4->m[TSTR] & 1) == 0) {
740
timer_adjust_oneshot(sh4->timer[0], attotime_never, 0);
733
sh4->timer[0]->adjust(attotime::never);
742
735
sh4_timer_recompute(sh4, 0);
745
738
sh4->m[TCNT1] = compute_ticks_timer(sh4->timer[1], sh4->pm_clock, tcnt_div[sh4->m[TCR1] & 7]);
746
739
if ((sh4->m[TSTR] & 2) == 0) {
747
timer_adjust_oneshot(sh4->timer[1], attotime_never, 0);
740
sh4->timer[1]->adjust(attotime::never);
749
742
sh4_timer_recompute(sh4, 1);
752
745
sh4->m[TCNT2] = compute_ticks_timer(sh4->timer[2], sh4->pm_clock, tcnt_div[sh4->m[TCR2] & 7]);
753
746
if ((sh4->m[TSTR] & 4) == 0) {
754
timer_adjust_oneshot(sh4->timer[2], attotime_never, 0);
747
sh4->timer[2]->adjust(attotime::never);
756
749
sh4_timer_recompute(sh4, 2);
945
938
READ32_HANDLER( sh4_internal_r )
947
sh4_state *sh4 = get_safe_token(space->cpu);
940
sh4_state *sh4 = get_safe_token(&space->device());
948
941
// logerror("sh4_internal_r: Read %08x (%x) @ %08x\n", 0xfe000000+((offset & 0x3fc0) << 11)+((offset & 0x3f) << 2), offset, mem_mask);
952
945
if ((sh4->m[RTCSR] >> 3) & 7)
954
947
//((double)rtcnt_div[(sh4->m[RTCSR] >> 3) & 7] / (double)100000000)
955
//return (refresh_timer_base + (timer_timeelapsed(sh4->refresh_timer) * (double)100000000) / (double)rtcnt_div[(sh4->m[RTCSR] >> 3) & 7]) & 0xff;
948
//return (refresh_timer_base + (sh4->refresh_timer->elapsed() * (double)100000000) / (double)rtcnt_div[(sh4->m[RTCSR] >> 3) & 7]) & 0xff;
956
949
return compute_ticks_refresh_timer(sh4->refresh_timer, sh4->bus_clock, sh4->refresh_timer_base, rtcnt_div[(sh4->m[RTCSR] >> 3) & 7]) & 0xff;
1038
1031
if (sh4->irln == value)
1040
1033
sh4->irln = value;
1041
cpu_set_input_line(device, SH4_IRLn, ASSERT_LINE);
1042
cpu_set_input_line(device, SH4_IRLn, CLEAR_LINE);
1034
device_set_input_line(device, SH4_IRLn, ASSERT_LINE);
1035
device_set_input_line(device, SH4_IRLn, CLEAR_LINE);
1045
1038
void sh4_set_irq_line(sh4_state *sh4, int irqline, int state) // set state of external interrupt line
1168
1161
for (i=0; i<3; i++)
1170
sh4->timer[i] = timer_alloc(device->machine, sh4_timer_callback, sh4);
1171
timer_adjust_oneshot(sh4->timer[i], attotime_never, i);
1163
sh4->timer[i] = device->machine().scheduler().timer_alloc(FUNC(sh4_timer_callback), sh4);
1164
sh4->timer[i]->adjust(attotime::never, i);
1174
1167
for (i=0; i<4; i++)
1176
sh4->dma_timer[i] = timer_alloc(device->machine, sh4_dmac_callback, sh4);
1177
timer_adjust_oneshot(sh4->dma_timer[i], attotime_never, i);
1169
sh4->dma_timer[i] = device->machine().scheduler().timer_alloc(FUNC(sh4_dmac_callback), sh4);
1170
sh4->dma_timer[i]->adjust(attotime::never, i);
1180
sh4->refresh_timer = timer_alloc(device->machine, sh4_refresh_timer_callback, sh4);
1181
timer_adjust_oneshot(sh4->refresh_timer, attotime_never, 0);
1173
sh4->refresh_timer = device->machine().scheduler().timer_alloc(FUNC(sh4_refresh_timer_callback), sh4);
1174
sh4->refresh_timer->adjust(attotime::never);
1182
1175
sh4->refresh_timer_base = 0;
1184
sh4->rtc_timer = timer_alloc(device->machine, sh4_rtc_timer_callback, sh4);
1185
timer_adjust_oneshot(sh4->rtc_timer, attotime_never, 0);
1177
sh4->rtc_timer = device->machine().scheduler().timer_alloc(FUNC(sh4_rtc_timer_callback), sh4);
1178
sh4->rtc_timer->adjust(attotime::never);
1187
sh4->m = auto_alloc_array(device->machine, UINT32, 16384);
1180
sh4->m = auto_alloc_array(device->machine(), UINT32, 16384);
1190
1183
void sh4_dma_ddt(device_t *device, struct sh4_ddt_dma *s)
1310
UINT32 sh4_getsqremap(sh4_state *sh4, UINT32 address)
1312
if (!sh4->sh4_mmu_enabled)
1317
UINT32 topaddr = address&0xfff00000;
1321
UINT32 topcmp = sh4->sh4_tlb_address[i]&0xfff00000;
1322
if (topcmp==topaddr)
1323
return (address&0x000fffff) | ((sh4->sh4_tlb_data[i])&0xfff00000);
1331
READ64_HANDLER( sh4_tlb_r )
1333
sh4_state *sh4 = get_safe_token(&space->device());
1335
int offs = offset*8;
1337
if (offs >= 0x01000000)
1339
UINT8 i = (offs>>8)&63;
1340
return sh4->sh4_tlb_data[i];
1344
UINT8 i = (offs>>8)&63;
1345
return sh4->sh4_tlb_address[i];
1349
WRITE64_HANDLER( sh4_tlb_w )
1351
sh4_state *sh4 = get_safe_token(&space->device());
1353
int offs = offset*8;
1355
if (offs >= 0x01000000)
1357
UINT8 i = (offs>>8)&63;
1358
sh4->sh4_tlb_data[i] = data&0xffffffff;
1362
UINT8 i = (offs>>8)&63;
1363
sh4->sh4_tlb_address[i] = data&0xffffffff;