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