Skip to content

Commit

Permalink
improved handshake sync with cmds
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.code.sf.net/p/vice-emu/code/trunk@45402 379a1393-f5fb-40a0-bcee-ef074d9b53f7
  • Loading branch information
pottendo committed Dec 16, 2024
1 parent c089991 commit 4c2fae1
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions vice/src/userport/userport_wic64.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,10 @@ void userport_wic64_factory_reset(void)

void wic64_set_status(const char *status)
{
strncpy(wic64_last_status, status, sizeof wic64_last_status);
wic64_last_status[sizeof wic64_last_status - 1u] = '\0';
if (status) {
strncpy(wic64_last_status, status, sizeof wic64_last_status);
wic64_last_status[sizeof wic64_last_status - 1u] = '\0';
}
}

static void prep_wic64_str(void)
Expand Down Expand Up @@ -1137,16 +1139,22 @@ static void handshake_flag2(void)

static void cycle_alarm_handler(CLOCK offset, void *data)
{
debug_log(LOG_COL_OFF, 3, "%s: expired", __FUNCTION__);
debug_log(LOG_COL_OFF, 3, "%s: expired, data = %p", __FUNCTION__, data);
alarm_unset(cycle_alarm);
handshake_flag2();
alarm_destroy(cycle_alarm);
cycle_alarm = NULL;
if (data) {
(*(void(*)(void))data)(); /* execute callback */
}
}

static void wic64_sleep_cycles(int cycles)
static void wic64_sleep_cycles(int cycles, void *cb)
{
if (cycle_alarm == NULL) {
cycle_alarm = alarm_new(maincpu_alarm_context, "CycleAlarm", cycle_alarm_handler, NULL);
if (cycle_alarm) {
debug_log(LOG_COL_LRED, 3, "%s: refusing to re-arm pending cycle alarm", __FUNCTION__);
return;
}
cycle_alarm = alarm_new(maincpu_alarm_context, "CycleAlarm", cycle_alarm_handler, cb);
alarm_unset(cycle_alarm);
alarm_set(cycle_alarm, maincpu_clk + cycles);
}
Expand Down Expand Up @@ -2222,8 +2230,8 @@ static void do_command(void)
send_reply_revised(CLIENT_ERROR, "", NULL, 0, NULL);
break;
case WIC64_CMD_REBOOT:
wic64_sleep_cycles((int)(3 * machine_get_cycles_per_second())); /* emulated a 3s reboot */
userport_wic64_reset();
debug_log(CONS_COL_NO, 3, "%s: arming for reboot", __FUNCTION__);
wic64_sleep_cycles(3 * machine_get_cycles_per_second(), userport_wic64_reset); /* emulated a 3s reboot */
break;
case WIC64_CMD_SET_TRANSFER_TIMEOUT:
case WIC64_CMD_SET_REMOTE_TIMEOUT:
Expand Down Expand Up @@ -2346,11 +2354,29 @@ static void wic64_prot_state(uint8_t value)
}
}

/* this is run after handshake of last byte is completed */
static void run_command_helper(void)
{
wic64_log(LOG_COL_LBLUE, "command %s (len=%d/0x%x, using %s protocol)",
cmd2string[input_command],
input_length, input_length,
(wic64_protocol == WIC64_PROT_LEGACY) ? "legacy" :
(wic64_protocol == WIC64_PROT_REVISED) ? "revised" :
(wic64_protocol == WIC64_PROT_EXTENDED) ? "extended" :
"unknown");
do_command();
cmd_timeout(0);
commandptr = input_state = input_length = 0;
input_command = WIC64_CMD_NONE;
memset(commandbuffer, 0, COMMANDBUFFER_MAXLEN);
}

/* PC2 irq (pulse) triggers when C64 reads/writes to userport */
static void userport_wic64_store_pbx(uint8_t value, int pulse)
{
if (force_timeout) {
debug_log(LOG_COL_OFF, 3, "%s: force timeout running %d/%d", __FUNCTION__, value, pulse);
set_userport_flag(FLAG2_INACTIVE);
return;
}
if (pulse == 1) {
Expand Down Expand Up @@ -2396,18 +2422,7 @@ static void userport_wic64_store_pbx(uint8_t value, int pulse)
cmd_timeout(1);
if ((input_state == INPUT_EXP_ARGS) &&
(commandptr == input_length)) {
cmd_timeout(0);
wic64_log(LOG_COL_LBLUE, "command %s (len=%d/0x%x, using %s protocol)",
cmd2string[input_command],
input_length, input_length,
(wic64_protocol == WIC64_PROT_LEGACY) ? "legacy" :
(wic64_protocol == WIC64_PROT_REVISED) ? "revised" :
(wic64_protocol == WIC64_PROT_EXTENDED) ? "extended" :
"unknown");
do_command();
commandptr = input_state = input_length = 0;
input_command = WIC64_CMD_NONE;
memset(commandbuffer, 0, COMMANDBUFFER_MAXLEN);
wic64_sleep_cycles(FLAG2_TOGGLE_DELAY + 1, run_command_helper);
}
} else {
if (reply_length) {
Expand Down Expand Up @@ -2582,12 +2597,14 @@ static void userport_wic64_reset(void)
lib_free(post_url);
post_url = NULL;
}
wic64_set_status("RESET");
/* wic64_set_status("RESET"); real HW doesn't tell this */

wic64_log(LOG_COL_LBLUE, "cyan color: host -> WiC64 communication");
wic64_log(LOG_COL_LGREEN, "green color: WiC64 -> host communication");
wic64_log(LOG_COL_LRED, "red color: some error");
wic64_log(CONS_COL_NO, "no color: other information");

handshake_flag2();
}

/* ---------------------------------------------------------------------*/
Expand Down

0 comments on commit 4c2fae1

Please sign in to comment.