-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathctrl.c
More file actions
219 lines (191 loc) · 6.76 KB
/
Copy pathctrl.c
File metadata and controls
219 lines (191 loc) · 6.76 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
/*
* Copyright 2026 The libuci Authors
* SPDX-License-Identifier: Apache-2.0
* https://github.com/c64uploader/libuci
*
* ctrl.c - Control commands for the Ultimate device.
*
* Functions for querying the device identity and hardware
* information, rebooting the C64, enabling and disabling
* virtual disk drives, erasing EasyFlash sectors, and reading
* RAMDisk layout.
*/
#include "uci.h"
#include "internal.h"
#include <string.h>
/* Control command IDs */
#define CTRL_CMD_IDENTIFY 0x01
#define CTRL_CMD_REBOOT 0x06
#define CTRL_CMD_ENABLE_DISK_A 0x30
#define CTRL_CMD_DISABLE_DISK_A 0x31
#define CTRL_CMD_ENABLE_DISK_B 0x32
#define CTRL_CMD_DISABLE_DISK_B 0x33
#define CTRL_CMD_DISK_A_POWER 0x34
#define CTRL_CMD_DISK_B_POWER 0x35
#define CTRL_CMD_GET_HWINFO 0x28
#define CTRL_CMD_GET_DRVINFO 0x29
#define CTRL_CMD_EASYFLASH 0x20
#define CTRL_CMD_GET_RAMDISKINFO 0x40
uci_err_t uci_ctrl_identify(char *buf, uint16_t max_len) {
uint16_t reply_len;
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_IDENTIFY);
if (res != UCI_SUCCESS) return res;
res = uci_execute_cmd((uint8_t *)buf, max_len - 1, &reply_len);
if (res == UCI_SUCCESS) {
buf[reply_len] = '\0';
}
return res;
}
uci_err_t uci_ctrl_reboot(void) {
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_REBOOT);
if (res != UCI_SUCCESS) return res;
return uci_execute_cmd(NULL, 0, NULL);
}
uci_err_t uci_ctrl_enable_disk_a(void) {
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_ENABLE_DISK_A);
if (res != UCI_SUCCESS) return res;
return uci_execute_cmd(NULL, 0, NULL);
}
uci_err_t uci_ctrl_disable_disk_a(void) {
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_DISABLE_DISK_A);
if (res != UCI_SUCCESS) return res;
return uci_execute_cmd(NULL, 0, NULL);
}
uci_err_t uci_ctrl_enable_disk_b(void) {
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_ENABLE_DISK_B);
if (res != UCI_SUCCESS) return res;
return uci_execute_cmd(NULL, 0, NULL);
}
uci_err_t uci_ctrl_disable_disk_b(void) {
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_DISABLE_DISK_B);
if (res != UCI_SUCCESS) return res;
return uci_execute_cmd(NULL, 0, NULL);
}
uci_err_t uci_ctrl_disk_a_power(bool *powered_on) {
char temp[8];
uint16_t reply_len;
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_DISK_A_POWER);
if (res != UCI_SUCCESS) return res;
res = uci_execute_cmd((uint8_t *)temp, sizeof(temp) - 1, &reply_len);
if (res == UCI_SUCCESS && reply_len >= 2) {
temp[reply_len] = '\0';
if (powered_on) {
*powered_on = (temp[0] == 'o' && temp[1] == 'n');
}
}
return res;
}
uci_err_t uci_ctrl_disk_b_power(bool *powered_on) {
char temp[8];
uint16_t reply_len;
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_DISK_B_POWER);
if (res != UCI_SUCCESS) return res;
res = uci_execute_cmd((uint8_t *)temp, sizeof(temp) - 1, &reply_len);
if (res == UCI_SUCCESS && reply_len >= 2) {
temp[reply_len] = '\0';
if (powered_on) {
*powered_on = (temp[0] == 'o' && temp[1] == 'n');
}
}
return res;
}
uci_err_t uci_ctrl_get_hwinfo(uci_hwinfo_dev_t device, char *buf, uint16_t max_len) {
uint16_t reply_len;
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_GET_HWINFO);
if (res != UCI_SUCCESS) return res;
uci_write_cmd_byte((uint8_t)device);
res = uci_execute_cmd((uint8_t *)buf, max_len - 1, &reply_len);
if (res == UCI_SUCCESS) {
buf[reply_len] = '\0';
}
return res;
}
uci_err_t uci_ctrl_get_sid_info(uci_sid_info_t *info) {
uint8_t raw[32];
uci_err_t res;
uint8_t i, ofs, base;
if (!info) return UCI_ERR_PARAM;
memset(info, 0, sizeof(uci_sid_info_t));
res = uci_ctrl_get_hwinfo(UCI_HWINFO_SID, (char *)raw, sizeof(raw));
if (res != UCI_SUCCESS) return res;
info->count = raw[0];
if (info->count > 4) info->count = 4;
for (i = 0; i < info->count; i++) {
ofs = 1 + i * 5;
base = (raw[ofs] >> 4) | ((raw[ofs + 1] & 0x0F) << 4);
info->sids[i].base_addr = 0xD000 + (uint16_t)base * 0x10;
if (raw[ofs + 2] == 1) {
info->sids[i].model = UCI_SID_MODEL_8580;
} else if (raw[ofs + 2] == 0) {
info->sids[i].model = UCI_SID_MODEL_6581;
} else {
info->sids[i].model = UCI_SID_MODEL_UNKNOWN;
}
info->sids[i].is_hardware = (raw[ofs + 3] != 0);
info->sids[i].enabled = (raw[ofs + 4] & 0x80) != 0;
}
return UCI_SUCCESS;
}
uci_err_t uci_ctrl_get_drvinfo(uci_drive_t effective_id,
uci_drive_entry_t *entries, uint8_t max_entries, uint8_t *out_count) {
uint8_t raw[64];
uint16_t reply_len;
uci_err_t res;
uint8_t count, i, ofs;
if (out_count) *out_count = 0;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_GET_DRVINFO);
if (res != UCI_SUCCESS) return res;
uci_write_cmd_byte((uint8_t)effective_id);
res = uci_execute_cmd(raw, sizeof(raw), &reply_len);
if (res != UCI_SUCCESS) return res;
if (reply_len < 1) return UCI_ERR_PARAM;
count = raw[0];
if (out_count) *out_count = count;
if (entries && max_entries > 0) {
uint8_t to_copy = (count < max_entries) ? count : max_entries;
for (i = 0; i < to_copy; i++) {
ofs = 1 + i * 3;
if (ofs + 2 >= reply_len) break;
entries[i].type = raw[ofs];
entries[i].iec_addr = raw[ofs + 1];
entries[i].power = raw[ofs + 2];
entries[i].is_drive = (raw[ofs + 2] != 0);
}
}
return UCI_SUCCESS;
}
uci_err_t uci_ctrl_easyflash_erase_sector(uint8_t bank_info, uint8_t base_addr) {
uci_err_t res;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_EASYFLASH);
if (res != UCI_SUCCESS) return res;
uci_write_cmd_byte(0x00); /* subcommand: sector erase */
uci_write_cmd_byte(bank_info); /* bits 3-5 select bank (0-7) */
uci_write_cmd_byte(base_addr); /* bit 5: 0=low ROM ($8000), 1=high ROM ($A000) */
return uci_execute_cmd(NULL, 0, NULL);
}
uci_err_t uci_ctrl_get_ramdiskinfo(uci_ramdisk_entry_t entries[4]) {
uint8_t raw[8];
uint16_t reply_len;
uci_err_t res;
uint8_t i;
if (!entries) return UCI_ERR_PARAM;
res = uci_start_cmd(UCI_TARGET_CONTROL, CTRL_CMD_GET_RAMDISKINFO);
if (res != UCI_SUCCESS) return res;
res = uci_execute_cmd(raw, sizeof(raw), &reply_len);
if (res != UCI_SUCCESS) return res;
if (reply_len < 8) return UCI_ERR_PARAM;
for (i = 0; i < 4; i++) {
entries[i].type = raw[i * 2];
entries[i].size_mb = raw[i * 2 + 1];
}
return UCI_SUCCESS;
}