]> git.sesse.net Git - pistorm/blob - platforms/amiga/pistorm-dev/pistorm-dev.c
Some RTG bug fixes, (inactive for now) P2C iRTG implementation
[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 static const char *op_type_names[4] = {
25     "BYTE",
26     "WORD",
27     "LONGWORD",
28     "MEM",
29 };
30 #else
31 #define DEBUG(...)
32 #endif
33
34 #define PIDEV_SWREV 0x0105
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 (cfg->map_type[dst] == MAPTYPE_ROM)
195                     break;
196                 if (dst != -1 && src != -1) {
197                     uint8_t *src_ptr = &cfg->map_data[src][(pi_ptr[0] - cfg->map_offset[src])];
198                     uint8_t *dst_ptr = &cfg->map_data[dst][(pi_ptr[1] - cfg->map_offset[dst])];
199                     memcpy(dst_ptr, src_ptr, val);
200                 } else {
201                     uint8_t tmp = 0;
202                     uint16_t tmps = 0;
203                     for (uint32_t i = 0; i < val; i++) {
204                         while (i + 2 < val) {
205                             if (src == -1) tmps = (uint16_t)m68k_read_memory_16(pi_ptr[0] + i);
206                             else memcpy(&tmps, &cfg->map_data[src][pi_ptr[0] - cfg->map_offset[src] + i], 2);
207
208                             if (dst == -1) m68k_write_memory_16(pi_ptr[1] + i, tmps);
209                             else memcpy(&cfg->map_data[dst][pi_ptr[1] - cfg->map_offset[dst] + i], &tmps, 2);
210                             i += 2;
211                         }
212                         if (src == -1) tmp = (uint8_t)m68k_read_memory_8(pi_ptr[0] + i);
213                         else tmp = cfg->map_data[src][pi_ptr[0] - cfg->map_offset[src] + i];
214                         
215                         if (dst == -1) m68k_write_memory_8(pi_ptr[1] + i, tmp);
216                         else cfg->map_data[dst][pi_ptr[1] - cfg->map_offset[dst] + i] = tmp;
217                     }
218                 }
219                 //DEBUG("[PISTORM-DEV] Copied %d bytes from $%.8X to $%.8X\n", val, pi_ptr[0], pi_ptr[1]);
220             }
221             break;
222         case PI_CMD_MEMSET:
223             //DEBUG("[PISTORM-DEV} Write to MEMSET: %d (%.8X)\n", val, val);
224             if (pi_ptr[0] == 0) {
225                 printf("[PISTORM-DEV] MEMSET with null pointer not allowed. Aborting.\n");
226                 pi_cmd_result = PI_RES_INVALIDVALUE;
227             } else if (val == 0) {
228                 printf("[PISTORM-DEV] MEMSET called with size 0. Aborting.\n");
229                 pi_cmd_result = PI_RES_INVALIDVALUE;
230             } else {
231                 int32_t dst = get_mapped_item_by_address(cfg, pi_ptr[0]);
232                 if (cfg->map_type[dst] == MAPTYPE_ROM)
233                     break;
234                 if (dst != -1) {
235                     uint8_t *dst_ptr = &cfg->map_data[dst][(pi_ptr[0] - cfg->map_offset[dst])];
236                     memset(dst_ptr, pi_byte[0], val);
237                 } else {
238                     for (uint32_t i = 0; i < val; i++) {
239                         m68k_write_memory_8(pi_ptr[0] + i, pi_byte[0]);
240                     }
241                 }
242             }
243             break;
244         case PI_CMD_COPYRECT:
245         case PI_CMD_COPYRECT_EX:
246             if (pi_ptr[0] == 0 || pi_ptr[1] == 0) {
247                 printf("[PISTORM-DEV] COPYRECT/EX from/to null pointer not allowed. Aborting.\n");
248                 pi_cmd_result = PI_RES_INVALIDVALUE;
249             } else if (pi_word[2] == 0 || pi_word[3] == 0) {
250                 printf("[PISTORM-DEV] COPYRECT/EX called with a width/height of 0. Aborting.\n");
251                 pi_cmd_result = PI_RES_INVALIDVALUE;
252             } else {
253                 int32_t src = get_mapped_item_by_address(cfg, pi_ptr[0]);
254                 int32_t dst = get_mapped_item_by_address(cfg, pi_ptr[1]);
255
256                 if (addr != PI_CMD_COPYRECT_EX) {
257                     // Clear out the src/dst coordinates in case something else set them previously.
258                     pi_word[4] = pi_word[5] = pi_word[6] = pi_word[7] = 0;
259                 }
260
261                 if (dst != -1 && src != -1) {
262                     uint8_t *src_ptr = &cfg->map_data[src][(pi_ptr[0] - cfg->map_offset[src])];
263                     uint8_t *dst_ptr = &cfg->map_data[dst][(pi_ptr[1] - cfg->map_offset[dst])];
264
265                     if (addr == PI_CMD_COPYRECT_EX) {
266                         // Adjust pointers in the case of available src/dst coordinates.
267                         src_ptr += pi_word[4] + (pi_word[5] * pi_word[0]);
268                         dst_ptr += pi_word[6] + (pi_word[7] * pi_word[1]);
269                     }
270
271                     for (int i = 0; i < pi_word[3]; i++) {
272                         memcpy(dst_ptr, src_ptr, pi_word[2]);
273
274                         src_ptr += pi_word[0];
275                         dst_ptr += pi_word[1];
276                     }
277                 } else {
278                     uint32_t src_offset = 0, dst_offset = 0;
279                     uint8_t tmp = 0;
280
281                     if (addr == PI_CMD_COPYRECT_EX) {
282                         src_offset += pi_word[4] + (pi_word[5] * pi_word[0]);
283                         dst_offset += pi_word[6] + (pi_word[7] * pi_word[1]);
284                     }
285
286                     for (uint32_t y = 0; y < pi_word[3]; y++) {
287                         for (uint32_t x = 0; x < pi_word[2]; x++) {
288                             if (src == -1) tmp = (unsigned char)m68k_read_memory_8(pi_ptr[0] + src_offset + x);
289                             else tmp = cfg->map_data[src][(pi_ptr[0] + src_offset + x) - cfg->map_offset[src]];
290                             
291                             if (dst == -1) m68k_write_memory_8(pi_ptr[1] + dst_offset + x, tmp);
292                             else cfg->map_data[dst][(pi_ptr[1] + dst_offset + x) - cfg->map_offset[dst]] = tmp;
293                         }
294                         src_offset += pi_word[0];
295                         dst_offset += pi_word[1];
296                     }
297                 }
298             }
299             break;
300
301         case PI_CMD_SHOWFPS: rtg_show_fps((uint8_t)val); break;
302         case PI_CMD_PALETTEDEBUG: rtg_palette_debug((uint8_t)val); break;
303         case PI_CMD_RTGSTATUS:
304             DEBUG("[PISTORM-DEV] Write to RTGSTATUS: %d\n", val);
305             if (val == 1 && !rtg_enabled) {
306                 init_rtg_data(cfg);
307                 rtg_enabled = 1;
308                 pi_cmd_result = PI_RES_OK;
309             } else if (val == 0 && rtg_enabled) {
310                 if (!rtg_on) {
311                     shutdown_rtg();
312                     rtg_enabled = 0;
313                     pi_cmd_result = PI_RES_OK;
314                 } else {
315                     // Refuse to disable RTG if it's currently in use.
316                     pi_cmd_result = PI_RES_FAILED;
317                 }
318             } else {
319                 pi_cmd_result = PI_RES_NOCHANGE;
320             }
321             adjust_ranges_amiga(cfg);
322             break;
323         case PI_CMD_NETSTATUS:
324             DEBUG("[PISTORM-DEV] Write to NETSTATUS: %d\n", val);
325             if (val == 1 && !pinet_enabled) {
326                 pinet_init(NULL);
327                 pinet_enabled = 1;
328                 pi_cmd_result = PI_RES_OK;
329             } else if (val == 0 && pinet_enabled) {
330                 pinet_shutdown();
331                 pinet_enabled = 0;
332                 pi_cmd_result = PI_RES_OK;
333             } else {
334                 pi_cmd_result = PI_RES_NOCHANGE;
335             }
336             adjust_ranges_amiga(cfg);
337             break;
338         case PI_CMD_PISCSI_CTRL:
339             DEBUG("[PISTORM-DEV] Write to PISCSI_CTRL: ");
340             switch(val) {
341                 case PISCSI_CTRL_DISABLE:
342                     DEBUG("DISABLE\n");
343                     if (piscsi_enabled) {
344                         piscsi_shutdown();
345                         piscsi_enabled = 0;
346                         // Probably not OK... depends on if you booted from floppy, I guess.
347                         pi_cmd_result = PI_RES_OK;
348                     } else {
349                         pi_cmd_result = PI_RES_NOCHANGE;
350                     }
351                     break;
352                 case PISCSI_CTRL_ENABLE:
353                     DEBUG("ENABLE\n");
354                     if (!piscsi_enabled) {
355                         piscsi_init();
356                         piscsi_enabled = 1;
357                         piscsi_refresh_drives();
358                         pi_cmd_result = PI_RES_OK;
359                     } else {
360                         pi_cmd_result = PI_RES_NOCHANGE;
361                     }
362                     break;
363                 case PISCSI_CTRL_MAP:
364                     DEBUG("MAP\n");
365                     if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
366                         printf("[PISTORM-DEV] Failed to grab string for PISCSI drive filename. Aborting.\n");
367                         pi_cmd_result = PI_RES_FAILED;
368                     } else {
369                         FILE *tmp = fopen(tmp_string, "rb");
370                         if (tmp == NULL) {
371                             printf("[PISTORM-DEV] Failed to open file %s for PISCSI drive mapping. Aborting.\n", tmp_string);
372                             pi_cmd_result = PI_RES_FILENOTFOUND;
373                         } else {
374                             fclose(tmp);
375                             printf("[PISTORM-DEV] Attempting to map file %s as PISCSI drive %d...\n", tmp_string, pi_word[0]);
376                             piscsi_unmap_drive(pi_word[0]);
377                             piscsi_map_drive(tmp_string, pi_word[0]);
378                             pi_cmd_result = PI_RES_OK;
379                         }
380                     }
381                     pi_string[0] = 0;
382                     break;
383                 case PISCSI_CTRL_UNMAP:
384                     DEBUG("UNMAP\n");
385                     if (pi_word[0] > 7) {
386                         printf("[PISTORM-DEV] Invalid drive ID %d for PISCSI unmap command.", pi_word[0]);
387                         pi_cmd_result = PI_RES_INVALIDVALUE;
388                     } else {
389                         if (piscsi_get_dev(pi_word[0])->fd != -1) {
390                             piscsi_unmap_drive(pi_word[0]);
391                             pi_cmd_result = PI_RES_OK;
392                         } else {
393                             pi_cmd_result = PI_RES_NOCHANGE;
394                         }
395                     }
396                     break;
397                 case PISCSI_CTRL_EJECT:
398                     DEBUG("EJECT (NYI)\n");
399                     pi_cmd_result = PI_RES_NOCHANGE;
400                     break;
401                 case PISCSI_CTRL_INSERT:
402                     DEBUG("INSERT (NYI)\n");
403                     pi_cmd_result = PI_RES_NOCHANGE;
404                     break;
405                 default:
406                     DEBUG("UNKNOWN/UNHANDLED. Aborting.\n");
407                     pi_cmd_result = PI_RES_INVALIDVALUE;
408                     break;
409             }
410             adjust_ranges_amiga(cfg);
411             break;
412         
413         case PI_CMD_KICKROM:
414             DEBUG("[PISTORM-DEV] Write to KICKROM.\n");
415             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
416                 printf("[PISTORM-DEV] Failed to grab string KICKROM filename. Aborting.\n");
417                 pi_cmd_result = PI_RES_FAILED;
418             } else {
419                 FILE *tmp = fopen(tmp_string, "rb");
420                 if (tmp == NULL) {
421                     printf("[PISTORM-DEV] Failed to open file %s for KICKROM mapping. Aborting.\n", tmp_string);
422                     pi_cmd_result = PI_RES_FILENOTFOUND;
423                 } else {
424                     fclose(tmp);
425                     if (get_named_mapped_item(cfg, "kickstart") != -1) {
426                         uint32_t index = get_named_mapped_item(cfg, "kickstart");
427                         free(cfg->map_data[index]);
428                         free(cfg->map_id[index]);
429                         cfg->map_type[index] = MAPTYPE_NONE;
430                         // Dirty hack, I am sleepy and lazy.
431                         add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "kickstart", 0);
432                         pi_cmd_result = PI_RES_OK;
433                         do_reset = 1;
434                     } else {
435                         printf ("[PISTORM-DEV] Could not find mapped range 'kickstart', cannot remap KICKROM.\n");
436                         pi_cmd_result = PI_RES_FAILED;
437                     }
438                 }
439             }
440             adjust_ranges_amiga(cfg);
441             pi_string[0] = 0;
442             break;
443         case PI_CMD_EXTROM:
444             DEBUG("[PISTORM-DEV] Write to EXTROM.\n");
445             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
446                 printf("[PISTORM-DEV] Failed to grab string EXTROM filename. Aborting.\n");
447                 pi_cmd_result = PI_RES_FAILED;
448             } else {
449                 FILE *tmp = fopen(tmp_string, "rb");
450                 if (tmp == NULL) {
451                     printf("[PISTORM-DEV] Failed to open file %s for EXTROM mapping. Aborting.\n", tmp_string);
452                     pi_cmd_result = PI_RES_FILENOTFOUND;
453                 } else {
454                     fclose(tmp);
455                     if (get_named_mapped_item(cfg, "extended") != -1) {
456                         uint32_t index = get_named_mapped_item(cfg, "extended");
457                         free(cfg->map_data[index]);
458                         free(cfg->map_id[index]);
459                         cfg->map_type[index] = MAPTYPE_NONE;
460                         // Dirty hack, I am tired and lazy.
461                         add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "extended", 0);
462                         pi_cmd_result = PI_RES_OK;
463                         do_reset = 1;
464                     } else {
465                         printf ("[PISTORM-DEV] Could not find mapped range 'extrom', cannot remap EXTROM.\n");
466                         pi_cmd_result = PI_RES_FAILED;
467                     }
468                 }
469             }
470             adjust_ranges_amiga(cfg);
471             pi_string[0] = 0;
472             break;
473
474         case PI_CMD_RESET:
475             DEBUG("[PISTORM-DEV] System reset called, code %d\n", (val & 0xFFFF));
476             do_reset = 1;
477             break;
478         case PI_CMD_SHUTDOWN:
479             DEBUG("[PISTORM-DEV] Shutdown requested. Confirm by replying with return value to CONFIRMSHUTDOWN.\n");
480             shutdown_confirm = rand() % 0xFFFF;
481             pi_cmd_result = shutdown_confirm;
482             break;
483         case PI_CMD_CONFIRMSHUTDOWN:
484             if (val != shutdown_confirm) {
485                 DEBUG("[PISTORM-DEV] Attempted shutdown with wrong shutdown confirm value. Not shutting down.\n");
486                 shutdown_confirm = 0xFFFFFFFF;
487                 pi_cmd_result = PI_RES_FAILED;
488             } else {
489                 printf("[PISTORM-DEV] Shutting down the PiStorm. Good night, fight well, until we meet again.\n");
490                 reboot(LINUX_REBOOT_CMD_POWER_OFF);
491                 pi_cmd_result = PI_RES_OK;
492                 end_signal = 1;
493             }
494             break;
495         case PI_CMD_SWITCHCONFIG:
496             DEBUG("[PISTORM-DEV] Config switch called, command: ");
497             switch (val) {
498                 case PICFG_LOAD:
499                     DEBUG("LOAD\n");
500                     if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)cfg_filename, 255) == -1) {
501                         printf("[PISTORM-DEV] Failed to grab string for CONFIG filename. Aborting.\n");
502                         pi_cmd_result = PI_RES_FAILED;
503                     } else {
504                         FILE *tmp = fopen(cfg_filename, "rb");
505                         if (tmp == NULL) {
506                             printf("[PISTORM-DEV] Failed to open CONFIG file %s for reading. Aborting.\n", cfg_filename);
507                             pi_cmd_result = PI_RES_FILENOTFOUND;
508                         } else {
509                             fclose(tmp);
510                             printf("[PISTORM-DEV] Attempting to load config file %s...\n", cfg_filename);
511                             load_new_config = val + 1;
512                             pi_cmd_result = PI_RES_OK;
513                         }
514                     }
515                     pi_string[0] = 0;
516                     break;
517                 case PICFG_RELOAD:
518                     DEBUG("RELOAD\n");
519                     printf("[PISTORM-DEV] Reloading current config file (%s)...\n", cfg_filename);
520                     load_new_config = val + 1;
521                     break;
522                 case PICFG_DEFAULT:
523                     DEBUG("DEFAULT\n");
524                     printf("[PISTORM-DEV] Loading default.cfg...\n");
525                     load_new_config = val + 1;
526                     break;
527                 default:
528                     DEBUG("UNKNOWN/UNHANDLED. Command ignored.\n");
529                     pi_cmd_result = PI_RES_INVALIDVALUE;
530                     break;
531             }
532             break;
533         default:
534             DEBUG("[PISTORM-DEV] WARN: Unhandled %s register write to %.4X: %d\n", op_type_names[type], addr - pistorm_dev_base, val);
535             pi_cmd_result = PI_RES_INVALIDCMD;
536             break;
537     }
538 }
539  
540 uint32_t handle_pistorm_dev_read(uint32_t addr_, uint8_t type) {
541     uint32_t addr = (addr_ & 0xFFFF);
542
543     switch((addr)) {
544         case PI_CMD_FILESIZE:
545             DEBUG("[PISTORM-DEV] %s read from FILESIZE.\n", op_type_names[type]);
546             if (pi_string[0] == 0 || grab_amiga_string(pi_string[0], (uint8_t *)tmp_string, 255) == -1)  {
547                 DEBUG("[PISTORM-DEV] Failed to grab string for FILESIZE command. Aborting.\n");
548                 pi_cmd_result = PI_RES_FAILED;
549                 pi_longword[0] = 0;
550                 return 0;
551             } else {
552                 FILE *tmp = fopen(tmp_string, "rb");
553                 if (tmp == NULL) {
554                     DEBUG("[PISTORM-DEV] Failed to open file %s for FILESIZE command. Aborting.\n", tmp_string);
555                     pi_longword[0] = 0;
556                     pi_cmd_result = PI_RES_FILENOTFOUND;
557                 } else {
558                     fseek(tmp, 0, SEEK_END);
559                     pi_longword[0] = ftell(tmp);
560                     DEBUG("[PISTORM-DEV] Returning file size for file %s: %d bytes.\n", tmp_string, pi_longword[0]);
561                     fclose(tmp);
562                     pi_cmd_result = PI_RES_OK;
563                 }
564             }
565             pi_string[0] = 0;
566             return pi_longword[0];
567             break;
568
569         case PI_CMD_HWREV:
570             // Probably replace this with some read from the CPLD to get a simple hardware revision.
571             DEBUG("[PISTORM-DEV] %s Read from HWREV\n", op_type_names[type]);
572             return 0x0101; // 1.1
573             break;
574         case PI_CMD_SWREV:
575             DEBUG("[PISTORM-DEV] %s Read from SWREV\n", op_type_names[type]);
576             return PIDEV_SWREV;
577             break;
578         case PI_CMD_RTGSTATUS:
579             DEBUG("[PISTORM-DEV] %s Read from RTGSTATUS\n", op_type_names[type]);
580             return (rtg_on << 1) | rtg_enabled;
581             break;
582         case PI_CMD_NETSTATUS:
583             DEBUG("[PISTORM-DEV] %s Read from NETSTATUS\n", op_type_names[type]);
584             return pinet_enabled;
585             break;
586         case PI_CMD_PISCSI_CTRL:
587             DEBUG("[PISTORM-DEV] %s Read from PISCSI_CTRL\n", op_type_names[type]);
588             return piscsi_enabled;
589             break;
590         case PI_CMD_GET_FB:
591             //DEBUG("[PISTORM-DEV] %s read from GET_FB: %.8X\n", op_type_names[type], rtg_get_fb());
592             return rtg_get_fb();
593
594         case PI_DBG_VAL1: case PI_DBG_VAL2: case PI_DBG_VAL3: case PI_DBG_VAL4:
595         case PI_DBG_VAL5: case PI_DBG_VAL6: case PI_DBG_VAL7: case PI_DBG_VAL8:
596             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]);
597             return pi_dbg_val[(addr - PI_DBG_VAL1) / 4];
598             break;
599
600         case PI_BYTE1: case PI_BYTE2: case PI_BYTE3: case PI_BYTE4:
601         case PI_BYTE5: case PI_BYTE6: case PI_BYTE7: case PI_BYTE8:
602             DEBUG("[PISTORM-DEV] Read BYTE %d (%d / $%.2X)\n", addr - PI_BYTE1, pi_byte[addr - PI_BYTE1], pi_byte[addr - PI_BYTE1]);
603             return pi_byte[addr - PI_BYTE1];
604             break;
605         case PI_WORD1: case PI_WORD2: case PI_WORD3: case PI_WORD4:
606             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]);
607             return pi_word[(addr - PI_WORD1) / 2];
608             break;
609         case PI_WORD5: case PI_WORD6: case PI_WORD7: case PI_WORD8: 
610         case PI_WORD9: case PI_WORD10: case PI_WORD11: case PI_WORD12: 
611             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]);
612             return pi_word[(addr - PI_WORD5) / 2];
613             break;
614         case PI_LONGWORD1: case PI_LONGWORD2: case PI_LONGWORD3: case PI_LONGWORD4:
615             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]);
616             return pi_longword[(addr - PI_LONGWORD1) / 4];
617             break;
618
619         case PI_CMDRESULT:
620             //DEBUG("[PISTORM-DEV] %s Read from CMDRESULT: %d\n", op_type_names[type], pi_cmd_result);
621             return pi_cmd_result;
622             break;
623
624         default:
625             DEBUG("[PISTORM-DEV] WARN: Unhandled %s register read from %.4X\n", op_type_names[type], addr - pistorm_dev_base);
626             break;
627     }
628     return 0;
629 }