]> git.sesse.net Git - pistorm/blob - platforms/amiga/pistorm-dev/pistorm-dev.c
Add memset to PiStorm API, robustify some things to not destroy the bus
[pistorm] / platforms / amiga / pistorm-dev / pistorm-dev.c
1 // SPDX-License-Identifier: MIT
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7
8 #include "pistorm-dev.h"
9 #include "pistorm-dev-enums.h"
10 #include "platforms/platforms.h"
11 #include "gpio/ps_protocol.h"
12 #include "platforms/amiga/rtg/rtg.h"
13 #include "platforms/amiga/piscsi/piscsi.h"
14 #include "platforms/amiga/net/pi-net.h"
15
16 #include <linux/reboot.h>
17 #include <sys/reboot.h>
18
19 #define DEBUG_PISTORM_DEVICE
20
21 #ifdef DEBUG_PISTORM_DEVICE
22 #define DEBUG printf
23
24 #define PIDEV_SWREV 0x0105
25
26 static const char *op_type_names[4] = {
27     "BYTE",
28     "WORD",
29     "LONGWORD",
30     "MEM",
31 };
32 #else
33 #define DEBUG(...)
34 #endif
35
36 extern uint32_t pistorm_dev_base;
37 extern uint32_t do_reset;
38
39 extern void adjust_ranges_amiga(struct emulator_config *cfg);
40 extern uint8_t rtg_enabled, rtg_on, pinet_enabled, piscsi_enabled, load_new_config, end_signal;
41 extern struct emulator_config *cfg;
42
43 char cfg_filename[256] = "default.cfg";
44 char tmp_string[256];
45
46 static uint8_t pi_byte[32];
47 static uint16_t pi_word[32];
48 static uint32_t pi_longword[32];
49 static uint32_t pi_string[32];
50 static uint32_t pi_ptr[32];
51
52 static uint32_t pi_dbg_val[32];
53 static uint32_t pi_dbg_string[32];
54
55 static uint32_t pi_cmd_result = 0, shutdown_confirm = 0xFFFFFFFF;
56
57 int32_t grab_amiga_string(uint32_t addr, uint8_t *dest, uint32_t str_max_len) {
58     int32_t r = get_mapped_item_by_address(cfg, addr);
59     uint32_t index = 0;
60
61     if (r == -1) {
62         DEBUG("[GRAB_AMIGA_STRING] No mapped range found for address $%.8X. Grabbing string data over the bus.\n", addr);
63         do {
64             dest[index] = (unsigned char)m68k_read_memory_8(addr + index);
65             index++;
66         } while (dest[index - 1] != 0x00 && index < str_max_len);
67     }
68     else {
69         uint8_t *src = cfg->map_data[r] + (addr - cfg->map_offset[r]);
70         do {
71             dest[index] = src[index];
72             index++;
73         } while (dest[index - 1] != 0x00 && index < str_max_len);
74     }
75     if (index == str_max_len) {
76         memset(dest, 0x00, str_max_len + 1);
77         return -1;
78     }
79     DEBUG("[GRAB_AMIGA_STRING] Grabbed string: %s\n", dest);
80     return (int32_t)strlen((const char*)dest);
81 }
82
83 int32_t amiga_transfer_file(uint32_t addr, char *filename) {
84     FILE *in = fopen(filename, "rb");
85     if (in == NULL) {
86         DEBUG("[AMIGA_TRANSFER_FILE] Failed to open file %s for reading.\n", filename);
87         return -1;
88     }
89     fseek(in, 0, SEEK_END);
90
91     int32_t r = get_mapped_item_by_address(cfg, addr);
92     uint32_t filesize = ftell(in);
93
94     fseek(in, 0, SEEK_SET);
95     if (r == -1) {
96         DEBUG("[GRAB_AMIGA_STRING] No mapped range found for address $%.8X. Transferring file data over the bus.\n", addr);
97         uint8_t tmp_read = 0;
98
99         for (uint32_t i = 0; i < filesize; i++) {
100             tmp_read = (uint8_t)fgetc(in);
101             m68k_write_memory_8(addr + i, tmp_read);
102         }
103     } else {
104         uint8_t *dst = cfg->map_data[r] + (addr - cfg->map_offset[r]);
105         fread(dst, filesize, 1, in);
106     }
107     fclose(in);
108     DEBUG("[AMIGA_TRANSFER_FILE] Copied %d bytes to address $%.8X.\n", filesize, addr);
109
110     return 0;
111 }
112
113 char *get_pistorm_devcfg_filename() {
114     return cfg_filename;
115 }
116
117 void set_pistorm_devcfg_filename(char *filename) {
118     strcpy(cfg_filename, filename);
119 }
120
121 void handle_pistorm_dev_write(uint32_t addr_, uint32_t val, uint8_t type) {
122     uint32_t addr = (addr_ & 0xFFFF);
123
124     switch((addr)) {
125         case PI_DBG_MSG:
126             // Output debug message based on value written and data in val/str registers.
127             break;
128         case PI_DBG_VAL1: case PI_DBG_VAL2: case PI_DBG_VAL3: case PI_DBG_VAL4:
129         case PI_DBG_VAL5: case PI_DBG_VAL6: case PI_DBG_VAL7: case PI_DBG_VAL8:
130             DEBUG("[PISTORM-DEV] Set DEBUG VALUE %d to %d ($%.8X)\n", (addr - PI_DBG_VAL1) / 4, val, val);
131             pi_dbg_val[(addr - PI_DBG_VAL1) / 4] = val;
132             break;
133         case PI_DBG_STR1: case PI_DBG_STR2: case PI_DBG_STR3: case PI_DBG_STR4:
134             DEBUG("[PISTORM-DEV] Set DEBUG STRING POINTER %d to $%.8X\n", (addr - PI_DBG_STR1) / 4, val);
135             pi_dbg_string[(addr - PI_DBG_STR1) / 4] = val;
136             break;
137
138         case PI_BYTE1: case PI_BYTE2: case PI_BYTE3: case PI_BYTE4:
139         case PI_BYTE5: case PI_BYTE6: case PI_BYTE7: case PI_BYTE8:
140             DEBUG("[PISTORM-DEV] Set BYTE %d to %d ($%.2X)\n", addr - PI_BYTE1, (val & 0xFF), (val & 0xFF));
141             pi_byte[addr - PI_BYTE1] = (val & 0xFF);
142             break;
143         case PI_WORD1: case PI_WORD2: case PI_WORD3: case PI_WORD4:
144             //DEBUG("[PISTORM-DEV] Set WORD %d to %d ($%.4X)\n", (addr - PI_WORD1) / 2, (val & 0xFFFF), (val & 0xFFFF));
145             pi_word[(addr - PI_WORD1) / 2] = (val & 0xFFFF);
146             break;
147         case PI_WORD5: case PI_WORD6: case PI_WORD7: case PI_WORD8:
148         case PI_WORD9: case PI_WORD10: case PI_WORD11: case PI_WORD12:
149             //DEBUG("[PISTORM-DEV] Set WORD %d to %d ($%.4X)\n", (addr - PI_WORD5) / 2, (val & 0xFFFF), (val & 0xFFFF));
150             pi_word[(addr - PI_WORD5) / 2] = (val & 0xFFFF);
151             break;
152         case PI_LONGWORD1: case PI_LONGWORD2: case PI_LONGWORD3: case PI_LONGWORD4:
153             //DEBUG("[PISTORM-DEV] Set LONGWORD %d to %d ($%.8X)\n", (addr - PI_LONGWORD1) / 4, val, val);
154             pi_longword[(addr - PI_LONGWORD1) / 4] = val;
155             break;
156         case PI_STR1: case PI_STR2: case PI_STR3: case PI_STR4:
157             DEBUG("[PISTORM-DEV] Set STRING POINTER %d to $%.8X\n", (addr - PI_STR1) / 4, val);
158             pi_string[(addr - PI_STR1) / 4] = val;
159             break;
160         case PI_PTR1: case PI_PTR2: case PI_PTR3: case PI_PTR4:
161             //DEBUG("[PISTORM-DEV] Set DATA POINTER %d to $%.8X\n", (addr - PI_PTR1) / 4, val);
162             pi_ptr[(addr - PI_PTR1) / 4] = val;
163             break;
164
165         case PI_CMD_TRANSFERFILE:
166             DEBUG("[PISTORM-DEV] Write to TRANSFERFILE.\n");
167             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
168                 printf("[PISTORM-DEV] No or invalid filename for TRANSFERFILE. Aborting.\n");
169                 pi_cmd_result = PI_RES_INVALIDVALUE;
170             } else if (pi_ptr[0] == 0) {
171                 printf("[PISTORM-DEV] Null pointer specified for TRANSFERFILE destination. Aborting.\n");
172                 pi_cmd_result = PI_RES_INVALIDVALUE;
173             } else {
174                 if (amiga_transfer_file(pi_ptr[0], tmp_string) == -1) {
175                     pi_cmd_result = PI_RES_FAILED;
176                 } else {
177                     pi_cmd_result = PI_RES_OK;
178                 }
179             }
180             pi_string[0] = 0;
181             pi_ptr[0] = 0;
182             break;
183         case PI_CMD_MEMCPY:
184             //DEBUG("[PISTORM-DEV} Write to MEMCPY: %d (%.8X)\n", val, val);
185             if (pi_ptr[0] == 0 || pi_ptr[1] == 0) {
186                 printf("[PISTORM-DEV] MEMCPY from/to null pointer not allowed. Aborting.\n");
187                 pi_cmd_result = PI_RES_INVALIDVALUE;
188             } else if (val == 0) {
189                 printf("[PISTORM-DEV] MEMCPY called with size 0. Aborting.\n");
190                 pi_cmd_result = PI_RES_INVALIDVALUE;
191             } else {
192                 int32_t src = get_mapped_item_by_address(cfg, pi_ptr[0]);
193                 int32_t dst = get_mapped_item_by_address(cfg, pi_ptr[1]);
194                 if (dst != -1 && src != -1) {
195                     //printf("doing super memcpy\n");
196                     uint8_t *src_ptr = &cfg->map_data[src][(pi_ptr[0] - cfg->map_offset[src])];
197                     uint8_t *dst_ptr = &cfg->map_data[dst][(pi_ptr[1] - cfg->map_offset[dst])];
198                     memcpy(dst_ptr, src_ptr, val);
199                 } else {
200                     printf("!!! doing manual memcpy\n");
201                     uint8_t tmp = 0;
202                     for (uint32_t i = 0; i < val; i++) {
203                         if (src == -1) tmp = (unsigned char)m68k_read_memory_8(pi_ptr[0] + i);
204                         else tmp = cfg->map_data[src][pi_ptr[0] - cfg->map_offset[src] + i];
205                         
206                         if (dst == -1) m68k_write_memory_8(pi_ptr[1] + i, tmp);
207                         else cfg->map_data[dst][pi_ptr[1] - cfg->map_offset[dst] + i] = tmp;
208                     }
209                 }
210                 //DEBUG("[PISTORM-DEV] Copied %d bytes from $%.8X to $%.8X\n", val, pi_ptr[0], pi_ptr[1]);
211             }
212             break;
213         case PI_CMD_MEMSET:
214             //DEBUG("[PISTORM-DEV} Write to MEMSET: %d (%.8X)\n", val, val);
215             if (pi_ptr[0] == 0) {
216                 printf("[PISTORM-DEV] MEMSET with null pointer not allowed. Aborting.\n");
217                 pi_cmd_result = PI_RES_INVALIDVALUE;
218             } else if (val == 0) {
219                 printf("[PISTORM-DEV] MEMSET called with size 0. Aborting.\n");
220                 pi_cmd_result = PI_RES_INVALIDVALUE;
221             } else {
222                 int32_t dst = get_mapped_item_by_address(cfg, pi_ptr[0]);
223                 if (dst != -1) {
224                     uint8_t *dst_ptr = &cfg->map_data[dst][(pi_ptr[0] - cfg->map_offset[dst])];
225                     memset(dst_ptr, pi_byte[0], val);
226                 } else {
227                     for (uint32_t i = 0; i < val; i++) {
228                         m68k_write_memory_8(pi_ptr[0] + i, val);
229                     }
230                 }
231             }
232             break;
233         case PI_CMD_COPYRECT:
234         case PI_CMD_COPYRECT_EX:
235             if (pi_ptr[0] == 0 || pi_ptr[1] == 0) {
236                 printf("[PISTORM-DEV] COPYRECT/EX from/to null pointer not allowed. Aborting.\n");
237                 pi_cmd_result = PI_RES_INVALIDVALUE;
238             } else if (pi_word[2] == 0 || pi_word[3] == 0) {
239                 printf("[PISTORM-DEV] COPYRECT/EX called with a width/height of 0. Aborting.\n");
240                 pi_cmd_result = PI_RES_INVALIDVALUE;
241             } else {
242                 int32_t src = get_mapped_item_by_address(cfg, pi_ptr[0]);
243                 int32_t dst = get_mapped_item_by_address(cfg, pi_ptr[1]);
244
245                 if (dst != -1 && src != -1) {
246                     uint8_t *src_ptr = &cfg->map_data[src][(pi_ptr[0] - cfg->map_offset[src])];
247                     uint8_t *dst_ptr = &cfg->map_data[dst][(pi_ptr[1] - cfg->map_offset[dst])];
248
249                     if (addr == PI_CMD_COPYRECT_EX) {
250                         // Adjust pointers in the case of available src/dst coordinates.
251                         src_ptr += pi_word[4] + (pi_word[5] * pi_word[0]);
252                         dst_ptr += pi_word[6] + (pi_word[7] * pi_word[1]);
253                     }
254
255                     for (int i = 0; i < pi_word[3]; i++) {
256                         memcpy(dst_ptr, src_ptr, pi_word[2]);
257
258                         src_ptr += pi_word[0];
259                         dst_ptr += pi_word[1];
260                     }
261                 } else {
262                     printf("!!! doing manual copyrect\n");
263
264                     if (addr != PI_CMD_COPYRECT_EX) {
265                         // Clear out the src/dst coordinates in case something else set them previously.
266                         pi_word[4] = pi_word[5] = pi_word[6] = pi_word[7] = 0;
267                     }
268
269                     uint32_t src_offset = 0, dst_offset = 0;
270                     uint8_t tmp = 0;
271
272                     if (addr == PI_CMD_COPYRECT_EX) {
273                         src_offset += pi_word[4] + (pi_word[5] * pi_word[0]);
274                         dst_offset += pi_word[6] + (pi_word[7] * pi_word[1]);
275                     }
276
277                     for (uint32_t y = 0; y < pi_word[3]; y++) {
278                         for (uint32_t x = 0; x < pi_word[2]; x++) {
279                             if (src == -1) tmp = (unsigned char)m68k_read_memory_8(pi_ptr[0] + src_offset + x);
280                             else tmp = cfg->map_data[src][(pi_ptr[0] + src_offset + x) - cfg->map_offset[src]];
281                             
282                             if (dst == -1) m68k_write_memory_8(pi_ptr[1] + dst_offset + x, tmp);
283                             else cfg->map_data[dst][(pi_ptr[1] + dst_offset + x) - cfg->map_offset[dst]] = tmp;
284                         }
285                         src_offset += pi_word[0];
286                         dst_offset += pi_word[1];
287                     }
288                 }
289             }
290             break;
291
292         case PI_CMD_RTGSTATUS:
293             DEBUG("[PISTORM-DEV] Write to RTGSTATUS: %d\n", val);
294             if (val == 1 && !rtg_enabled) {
295                 init_rtg_data(cfg);
296                 rtg_enabled = 1;
297                 pi_cmd_result = PI_RES_OK;
298             } else if (val == 0 && rtg_enabled) {
299                 if (!rtg_on) {
300                     shutdown_rtg();
301                     rtg_enabled = 0;
302                     pi_cmd_result = PI_RES_OK;
303                 } else {
304                     // Refuse to disable RTG if it's currently in use.
305                     pi_cmd_result = PI_RES_FAILED;
306                 }
307             } else {
308                 pi_cmd_result = PI_RES_NOCHANGE;
309             }
310             adjust_ranges_amiga(cfg);
311             break;
312         case PI_CMD_NETSTATUS:
313             DEBUG("[PISTORM-DEV] Write to NETSTATUS: %d\n", val);
314             if (val == 1 && !pinet_enabled) {
315                 pinet_init(NULL);
316                 pinet_enabled = 1;
317                 pi_cmd_result = PI_RES_OK;
318             } else if (val == 0 && pinet_enabled) {
319                 pinet_shutdown();
320                 pinet_enabled = 0;
321                 pi_cmd_result = PI_RES_OK;
322             } else {
323                 pi_cmd_result = PI_RES_NOCHANGE;
324             }
325             adjust_ranges_amiga(cfg);
326             break;
327         case PI_CMD_PISCSI_CTRL:
328             DEBUG("[PISTORM-DEV] Write to PISCSI_CTRL: ");
329             switch(val) {
330                 case PISCSI_CTRL_DISABLE:
331                     DEBUG("DISABLE\n");
332                     if (piscsi_enabled) {
333                         piscsi_shutdown();
334                         piscsi_enabled = 0;
335                         // Probably not OK... depends on if you booted from floppy, I guess.
336                         pi_cmd_result = PI_RES_OK;
337                     } else {
338                         pi_cmd_result = PI_RES_NOCHANGE;
339                     }
340                     break;
341                 case PISCSI_CTRL_ENABLE:
342                     DEBUG("ENABLE\n");
343                     if (!piscsi_enabled) {
344                         piscsi_init();
345                         piscsi_enabled = 1;
346                         piscsi_refresh_drives();
347                         pi_cmd_result = PI_RES_OK;
348                     } else {
349                         pi_cmd_result = PI_RES_NOCHANGE;
350                     }
351                     break;
352                 case PISCSI_CTRL_MAP:
353                     DEBUG("MAP\n");
354                     if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
355                         printf("[PISTORM-DEV] Failed to grab string for PISCSI drive filename. Aborting.\n");
356                         pi_cmd_result = PI_RES_FAILED;
357                     } else {
358                         FILE *tmp = fopen(tmp_string, "rb");
359                         if (tmp == NULL) {
360                             printf("[PISTORM-DEV] Failed to open file %s for PISCSI drive mapping. Aborting.\n", tmp_string);
361                             pi_cmd_result = PI_RES_FILENOTFOUND;
362                         } else {
363                             fclose(tmp);
364                             printf("[PISTORM-DEV] Attempting to map file %s as PISCSI drive %d...\n", tmp_string, pi_word[0]);
365                             piscsi_unmap_drive(pi_word[0]);
366                             piscsi_map_drive(tmp_string, pi_word[0]);
367                             pi_cmd_result = PI_RES_OK;
368                         }
369                     }
370                     pi_string[0] = 0;
371                     break;
372                 case PISCSI_CTRL_UNMAP:
373                     DEBUG("UNMAP\n");
374                     if (pi_word[0] > 7) {
375                         printf("[PISTORM-DEV] Invalid drive ID %d for PISCSI unmap command.", pi_word[0]);
376                         pi_cmd_result = PI_RES_INVALIDVALUE;
377                     } else {
378                         if (piscsi_get_dev(pi_word[0])->fd != -1) {
379                             piscsi_unmap_drive(pi_word[0]);
380                             pi_cmd_result = PI_RES_OK;
381                         } else {
382                             pi_cmd_result = PI_RES_NOCHANGE;
383                         }
384                     }
385                     break;
386                 case PISCSI_CTRL_EJECT:
387                     DEBUG("EJECT (NYI)\n");
388                     pi_cmd_result = PI_RES_NOCHANGE;
389                     break;
390                 case PISCSI_CTRL_INSERT:
391                     DEBUG("INSERT (NYI)\n");
392                     pi_cmd_result = PI_RES_NOCHANGE;
393                     break;
394                 default:
395                     DEBUG("UNKNOWN/UNHANDLED. Aborting.\n");
396                     pi_cmd_result = PI_RES_INVALIDVALUE;
397                     break;
398             }
399             adjust_ranges_amiga(cfg);
400             break;
401         
402         case PI_CMD_KICKROM:
403             DEBUG("[PISTORM-DEV] Write to KICKROM.\n");
404             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
405                 printf("[PISTORM-DEV] Failed to grab string KICKROM filename. Aborting.\n");
406                 pi_cmd_result = PI_RES_FAILED;
407             } else {
408                 FILE *tmp = fopen(tmp_string, "rb");
409                 if (tmp == NULL) {
410                     printf("[PISTORM-DEV] Failed to open file %s for KICKROM mapping. Aborting.\n", tmp_string);
411                     pi_cmd_result = PI_RES_FILENOTFOUND;
412                 } else {
413                     fclose(tmp);
414                     if (get_named_mapped_item(cfg, "kickstart") != -1) {
415                         uint32_t index = get_named_mapped_item(cfg, "kickstart");
416                         free(cfg->map_data[index]);
417                         free(cfg->map_id[index]);
418                         cfg->map_type[index] = MAPTYPE_NONE;
419                         // Dirty hack, I am sleepy and lazy.
420                         add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "kickstart");
421                         pi_cmd_result = PI_RES_OK;
422                         do_reset = 1;
423                     } else {
424                         printf ("[PISTORM-DEV] Could not find mapped range 'kickstart', cannot remap KICKROM.\n");
425                         pi_cmd_result = PI_RES_FAILED;
426                     }
427                 }
428             }
429             adjust_ranges_amiga(cfg);
430             pi_string[0] = 0;
431             break;
432         case PI_CMD_EXTROM:
433             DEBUG("[PISTORM-DEV] Write to EXTROM.\n");
434             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
435                 printf("[PISTORM-DEV] Failed to grab string EXTROM filename. Aborting.\n");
436                 pi_cmd_result = PI_RES_FAILED;
437             } else {
438                 FILE *tmp = fopen(tmp_string, "rb");
439                 if (tmp == NULL) {
440                     printf("[PISTORM-DEV] Failed to open file %s for EXTROM mapping. Aborting.\n", tmp_string);
441                     pi_cmd_result = PI_RES_FILENOTFOUND;
442                 } else {
443                     fclose(tmp);
444                     if (get_named_mapped_item(cfg, "extended") != -1) {
445                         uint32_t index = get_named_mapped_item(cfg, "extended");
446                         free(cfg->map_data[index]);
447                         free(cfg->map_id[index]);
448                         cfg->map_type[index] = MAPTYPE_NONE;
449                         // Dirty hack, I am tired and lazy.
450                         add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "extended");
451                         pi_cmd_result = PI_RES_OK;
452                         do_reset = 1;
453                     } else {
454                         printf ("[PISTORM-DEV] Could not find mapped range 'extrom', cannot remap EXTROM.\n");
455                         pi_cmd_result = PI_RES_FAILED;
456                     }
457                 }
458             }
459             adjust_ranges_amiga(cfg);
460             pi_string[0] = 0;
461             break;
462
463         case PI_CMD_RESET:
464             DEBUG("[PISTORM-DEV] System reset called, code %d\n", (val & 0xFFFF));
465             do_reset = 1;
466             break;
467         case PI_CMD_SHUTDOWN:
468             DEBUG("[PISTORM-DEV] Shutdown requested. Confirm by replying with return value to CONFIRMSHUTDOWN.\n");
469             shutdown_confirm = rand() % 0xFFFF;
470             pi_cmd_result = shutdown_confirm;
471             break;
472         case PI_CMD_CONFIRMSHUTDOWN:
473             if (val != shutdown_confirm) {
474                 DEBUG("[PISTORM-DEV] Attempted shutdown with wrong shutdown confirm value. Not shutting down.\n");
475                 shutdown_confirm = 0xFFFFFFFF;
476                 pi_cmd_result = PI_RES_FAILED;
477             } else {
478                 printf("[PISTORM-DEV] Shutting down the PiStorm. Good night, fight well, until we meet again.\n");
479                 reboot(LINUX_REBOOT_CMD_POWER_OFF);
480                 pi_cmd_result = PI_RES_OK;
481                 end_signal = 1;
482             }
483             break;
484         case PI_CMD_SWITCHCONFIG:
485             DEBUG("[PISTORM-DEV] Config switch called, command: ");
486             switch (val) {
487                 case PICFG_LOAD:
488                     DEBUG("LOAD\n");
489                     if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)cfg_filename, 255) == -1) {
490                         printf("[PISTORM-DEV] Failed to grab string for CONFIG filename. Aborting.\n");
491                         pi_cmd_result = PI_RES_FAILED;
492                     } else {
493                         FILE *tmp = fopen(cfg_filename, "rb");
494                         if (tmp == NULL) {
495                             printf("[PISTORM-DEV] Failed to open CONFIG file %s for reading. Aborting.\n", cfg_filename);
496                             pi_cmd_result = PI_RES_FILENOTFOUND;
497                         } else {
498                             fclose(tmp);
499                             printf("[PISTORM-DEV] Attempting to load config file %s...\n", cfg_filename);
500                             load_new_config = val + 1;
501                             pi_cmd_result = PI_RES_OK;
502                         }
503                     }
504                     pi_string[0] = 0;
505                     break;
506                 case PICFG_RELOAD:
507                     DEBUG("RELOAD\n");
508                     printf("[PISTORM-DEV] Reloading current config file (%s)...\n", cfg_filename);
509                     load_new_config = val + 1;
510                     break;
511                 case PICFG_DEFAULT:
512                     DEBUG("DEFAULT\n");
513                     printf("[PISTORM-DEV] Loading default.cfg...\n");
514                     load_new_config = val + 1;
515                     break;
516                 default:
517                     DEBUG("UNKNOWN/UNHANDLED. Command ignored.\n");
518                     pi_cmd_result = PI_RES_INVALIDVALUE;
519                     break;
520             }
521             break;
522         default:
523             DEBUG("[PISTORM-DEV] WARN: Unhandled %s register write to %.4X: %d\n", op_type_names[type], addr - pistorm_dev_base, val);
524             pi_cmd_result = PI_RES_INVALIDCMD;
525             break;
526     }
527 }
528  
529 uint32_t handle_pistorm_dev_read(uint32_t addr_, uint8_t type) {
530     uint32_t addr = (addr_ & 0xFFFF);
531
532     switch((addr)) {
533         case PI_CMD_FILESIZE:
534             DEBUG("[PISTORM-DEV] %s read from FILESIZE.\n", op_type_names[type]);
535             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
536                 DEBUG("[PISTORM-DEV] Failed to grab string for FILESIZE command. Aborting.\n");
537                 pi_cmd_result = PI_RES_FAILED;
538                 pi_longword[0] = 0;
539                 return 0;
540             } else {
541                 FILE *tmp = fopen(tmp_string, "rb");
542                 if (tmp == NULL) {
543                     DEBUG("[PISTORM-DEV] Failed to open file %s for FILESIZE command. Aborting.\n", tmp_string);
544                     pi_longword[0] = 0;
545                     pi_cmd_result = PI_RES_FILENOTFOUND;
546                 } else {
547                     fseek(tmp, 0, SEEK_END);
548                     pi_longword[0] = ftell(tmp);
549                     DEBUG("[PISTORM-DEV] Returning file size for file %s: %d bytes.\n", tmp_string, pi_longword[0]);
550                     fclose(tmp);
551                     pi_cmd_result = PI_RES_OK;
552                 }
553             }
554             pi_string[0] = 0;
555             return pi_longword[0];
556             break;
557
558         case PI_CMD_HWREV:
559             // Probably replace this with some read from the CPLD to get a simple hardware revision.
560             DEBUG("[PISTORM-DEV] %s Read from HWREV\n", op_type_names[type]);
561             return 0x0101; // 1.1
562             break;
563         case PI_CMD_SWREV:
564             DEBUG("[PISTORM-DEV] %s Read from SWREV\n", op_type_names[type]);
565             return PIDEV_SWREV;
566             break;
567         case PI_CMD_RTGSTATUS:
568             DEBUG("[PISTORM-DEV] %s Read from RTGSTATUS\n", op_type_names[type]);
569             return (rtg_on << 1) | rtg_enabled;
570             break;
571         case PI_CMD_NETSTATUS:
572             DEBUG("[PISTORM-DEV] %s Read from NETSTATUS\n", op_type_names[type]);
573             return pinet_enabled;
574             break;
575         case PI_CMD_PISCSI_CTRL:
576             DEBUG("[PISTORM-DEV] %s Read from PISCSI_CTRL\n", op_type_names[type]);
577             return piscsi_enabled;
578             break;
579         case PI_CMD_GET_FB:
580             //DEBUG("[PISTORM-DEV] %s read from GET_FB: %.8X\n", op_type_names[type], rtg_get_fb());
581             return rtg_get_fb();
582
583         case PI_DBG_VAL1: case PI_DBG_VAL2: case PI_DBG_VAL3: case PI_DBG_VAL4:
584         case PI_DBG_VAL5: case PI_DBG_VAL6: case PI_DBG_VAL7: case PI_DBG_VAL8:
585             DEBUG("[PISTORM-DEV] Read DEBUG VALUE %d (%d / $%.8X)\n", (addr - PI_DBG_VAL1) / 4, pi_dbg_val[(addr - PI_DBG_VAL1) / 4], pi_dbg_val[(addr - PI_DBG_VAL1) / 4]);
586             return pi_dbg_val[(addr - PI_DBG_VAL1) / 4];
587             break;
588
589         case PI_BYTE1: case PI_BYTE2: case PI_BYTE3: case PI_BYTE4:
590         case PI_BYTE5: case PI_BYTE6: case PI_BYTE7: case PI_BYTE8:
591             DEBUG("[PISTORM-DEV] Read BYTE %d (%d / $%.2X)\n", addr - PI_BYTE1, pi_byte[addr - PI_BYTE1], pi_byte[addr - PI_BYTE1]);
592             return pi_byte[addr - PI_BYTE1];
593             break;
594         case PI_WORD1: case PI_WORD2: case PI_WORD3: case PI_WORD4:
595             DEBUG("[PISTORM-DEV] Read WORD %d (%d / $%.4X)\n", (addr - PI_WORD1) / 2, pi_word[(addr - PI_WORD1) / 2], pi_word[(addr - PI_WORD1) / 2]);
596             return pi_word[(addr - PI_WORD1) / 2];
597             break;
598         case PI_WORD5: case PI_WORD6: case PI_WORD7: case PI_WORD8: 
599         case PI_WORD9: case PI_WORD10: case PI_WORD11: case PI_WORD12: 
600             DEBUG("[PISTORM-DEV] Read WORD %d (%d / $%.4X)\n", (addr - PI_WORD5) / 2, pi_word[(addr - PI_WORD5) / 2], pi_word[(addr - PI_WORD5) / 2]);
601             return pi_word[(addr - PI_WORD5) / 2];
602             break;
603         case PI_LONGWORD1: case PI_LONGWORD2: case PI_LONGWORD3: case PI_LONGWORD4:
604             DEBUG("[PISTORM-DEV] Read LONGWORD %d (%d / $%.8X)\n", (addr - PI_LONGWORD1) / 4, pi_longword[(addr - PI_LONGWORD1) / 4], pi_longword[(addr - PI_LONGWORD1) / 4]);
605             return pi_longword[(addr - PI_LONGWORD1) / 4];
606             break;
607
608         case PI_CMDRESULT:
609             //DEBUG("[PISTORM-DEV] %s Read from CMDRESULT: %d\n", op_type_names[type], pi_cmd_result);
610             return pi_cmd_result;
611             break;
612
613         default:
614             DEBUG("[PISTORM-DEV] WARN: Unhandled %s register read from %.4X\n", op_type_names[type], addr - pistorm_dev_base);
615             break;
616     }
617     return 0;
618 }