26
26
/* Delay between the beginning (UIP asserted) and the end (UIP cleared and
27
27
update interrupt asserted) of the update cycle */
28
#define UPDATE_CYCLE_TIME ATTOTIME_IN_USEC(1984)
28
#define UPDATE_CYCLE_TIME attotime::from_usec(1984)
29
29
/* Delay between the assertion of UIP and the effective start of the update
31
/*#define UPDATE_CYCLE_DELAY ATTOTIME_IN_USEC(244)*/
31
/*#define UPDATE_CYCLE_DELAY attotime::from_usec(244)*/
33
33
typedef struct _rtc65271_state rtc65271_state;
34
34
struct _rtc65271_state
194
194
load the SRAM and register contents from file
196
static int rtc65271_file_load(device_t *device, mame_file *file)
196
static int rtc65271_file_load(device_t *device, emu_file &file)
198
198
rtc65271_state *state = get_safe_token(device);
202
202
/* version flag */
203
if (mame_fread(file, & buf, 1) != 1)
203
if (file.read(&buf, 1) != 1)
208
208
/* control registers */
209
if (mame_fread(file, &buf, 1) != 1)
209
if (file.read(&buf, 1) != 1)
211
211
state->regs[reg_A] = buf & (reg_A_DV /*| reg_A_RS*/);
212
if (mame_fread(file, &buf, 1) != 1)
212
if (file.read(&buf, 1) != 1)
214
214
state->regs[reg_B] = buf & (reg_B_SET | reg_B_DM | reg_B_24h | reg_B_DSE);
216
216
/* alarm registers */
217
if (mame_fread(file, &state->regs[reg_alarm_second], 1) != 1)
219
if (mame_fread(file, &state->regs[reg_alarm_minute], 1) != 1)
221
if (mame_fread(file, &state->regs[reg_alarm_hour], 1) != 1)
217
if (file.read(&state->regs[reg_alarm_second], 1) != 1)
219
if (file.read(&state->regs[reg_alarm_minute], 1) != 1)
221
if (file.read(&state->regs[reg_alarm_hour], 1) != 1)
225
if (mame_fread(file, state->regs+14, 50) != 50)
225
if (file.read(state->regs+14, 50) != 50)
228
228
/* extended RAM */
229
if (mame_fread(file, state->xram, 4096) != 4096)
229
if (file.read(state->xram, 4096) != 4096)
232
232
state->regs[reg_D] |= reg_D_VRT; /* the data was backed up successfully */
236
236
system_time systime;
238
238
/* get the current date/time from the core */
239
device->machine->current_datetime(systime);
239
device->machine().current_datetime(systime);
241
241
/* set clock registers */
242
242
state->regs[reg_second] = systime.local_time.second;
278
278
save the SRAM and register contents to file
280
static int rtc65271_file_save(device_t *device, mame_file *file)
280
static int rtc65271_file_save(device_t *device, emu_file &file)
282
282
rtc65271_state *state = get_safe_token(device);
286
286
/* version flag */
288
if (mame_fwrite(file, & buf, 1) != 1)
288
if (file.write(& buf, 1) != 1)
291
291
/* control registers */
292
292
buf = state->regs[reg_A] & (reg_A_DV | reg_A_RS);
293
if (mame_fwrite(file, &buf, 1) != 1)
293
if (file.write(&buf, 1) != 1)
295
295
buf = state->regs[reg_B] & (reg_B_SET | reg_B_DM | reg_B_24h | reg_B_DSE);
296
if (mame_fwrite(file, &buf, 1) != 1)
296
if (file.write(&buf, 1) != 1)
299
299
/* alarm registers */
300
if (mame_fwrite(file, &state->regs[reg_alarm_second], 1) != 1)
302
if (mame_fwrite(file, &state->regs[reg_alarm_minute], 1) != 1)
304
if (mame_fwrite(file, &state->regs[reg_alarm_hour], 1) != 1)
300
if (file.write(&state->regs[reg_alarm_second], 1) != 1)
302
if (file.write(&state->regs[reg_alarm_minute], 1) != 1)
304
if (file.write(&state->regs[reg_alarm_hour], 1) != 1)
308
if (mame_fwrite(file, state->regs+14, 50) != 50)
308
if (file.write(state->regs+14, 50) != 50)
311
311
/* extended RAM */
312
if (mame_fwrite(file, state->xram, 4096) != 4096)
312
if (file.write(state->xram, 4096) != 4096)
409
409
if (data & reg_A_RS)
411
attotime period = ATTOTIME_IN_HZ(SQW_freq_table[data & reg_A_RS]);
412
attotime half_period = attotime_div(period, 2);
413
attotime elapsed = timer_timeelapsed(state->update_timer);
411
attotime period = attotime::from_hz(SQW_freq_table[data & reg_A_RS]);
412
attotime half_period = period / 2;
413
attotime elapsed = state->update_timer->elapsed();
415
if (attotime_compare(half_period, elapsed) > 0)
416
timer_adjust_oneshot(state->SQW_timer, attotime_sub(half_period, elapsed), 0);
415
if (half_period > elapsed)
416
state->SQW_timer->adjust(half_period - elapsed);
418
timer_adjust_oneshot(state->SQW_timer, half_period, 0);
418
state->SQW_timer->adjust(half_period);
422
422
state->SQW_internal_state = 0; /* right??? */
424
424
/* Stop the divider used for SQW and periodic interrupts. */
425
timer_adjust_oneshot(state->SQW_timer, attotime_never, 0);
425
state->SQW_timer->adjust(attotime::never);
428
428
/* The UIP bit is read-only */
505
505
field_interrupts(device);
508
half_period = attotime_div(ATTOTIME_IN_HZ(SQW_freq_table[state->regs[reg_A] & reg_A_RS]), 2);
509
timer_adjust_oneshot(state->SQW_timer, half_period, 0);
508
half_period = attotime::from_hz(SQW_freq_table[state->regs[reg_A] & reg_A_RS]) / 2;
509
state->SQW_timer->adjust(half_period);
522
522
state->regs[reg_A] |= reg_A_UIP;
524
524
/* schedule end of update cycle */
525
timer_set(device->machine, UPDATE_CYCLE_TIME, (void *)device, 0, rtc_end_update_callback);
525
device->machine().scheduler().timer_set(UPDATE_CYCLE_TIME, FUNC(rtc_end_update_callback), 0, (void *)device);
688
688
rtc65271_config *config = (rtc65271_config *)downcast<const legacy_device_config_base &>(device->baseconfig()).inline_config();
689
689
rtc65271_state *state = get_safe_token(device);
691
state->update_timer = timer_alloc(device->machine, rtc_begin_update_callback, (void *)device);
692
timer_adjust_periodic(state->update_timer, ATTOTIME_IN_SEC(1), 0, ATTOTIME_IN_SEC(1));
693
state->SQW_timer = timer_alloc(device->machine, rtc_SQW_callback, (void *)device);
691
state->update_timer = device->machine().scheduler().timer_alloc(FUNC(rtc_begin_update_callback), (void *)device);
692
state->update_timer->adjust(attotime::from_seconds(1), 0, attotime::from_seconds(1));
693
state->SQW_timer = device->machine().scheduler().timer_alloc(FUNC(rtc_SQW_callback), (void *)device);
694
694
state->interrupt_callback = config->interrupt_callback;
696
state_save_register_device_item_array(device, 0, state->regs);
697
state_save_register_device_item(device, 0, state->cur_reg);
698
state_save_register_device_item_array(device, 0, state->xram);
699
state_save_register_device_item(device, 0, state->cur_xram_page);
700
state_save_register_device_item(device, 0, state->SQW_internal_state);
696
device->save_item(NAME(state->regs));
697
device->save_item(NAME(state->cur_reg));
698
device->save_item(NAME(state->xram));
699
device->save_item(NAME(state->cur_xram_page));
700
device->save_item(NAME(state->SQW_internal_state));
704
704
static DEVICE_NVRAM( rtc65271 )
706
706
if (read_or_write)
707
rtc65271_file_save(device, file);
707
rtc65271_file_save(device, *file);
709
rtc65271_file_load(device, file);
709
rtc65271_file_load(device, *file);