9 #include "piscsi-enums.h"
10 #include "../../../config_file/config_file.h"
11 #include "../../../gpio/gpio.h"
13 struct piscsi_dev devs[8];
14 uint8_t piscsi_cur_drive = 0;
15 uint32_t piscsi_u32[4];
16 uint32_t piscsi_rom_size = 0;
17 uint8_t *piscsi_rom_ptr;
19 extern unsigned char ac_piscsi_rom[];
21 static const char *op_type_names[4] = {
29 for (int i = 0; i < 8; i++) {
31 devs[i].c = devs[i].h = devs[i].s = 0;
34 FILE *in = fopen("./platforms/amiga/piscsi/piscsi.rom", "rb");
36 printf("[PISCSI] Could not open PISCSI Boot ROM file for reading.\n");
37 ac_piscsi_rom[20] = 0;
38 ac_piscsi_rom[21] = 0;
39 ac_piscsi_rom[22] = 0;
40 ac_piscsi_rom[23] = 0;
43 fseek(in, 0, SEEK_END);
44 piscsi_rom_size = ftell(in);
45 fseek(in, 0, SEEK_SET);
46 piscsi_rom_ptr = malloc(piscsi_rom_size);
47 fread(piscsi_rom_ptr, piscsi_rom_size, 1, in);
49 printf("[PISCSI] Loaded Boot ROM.\n");
52 void piscsi_map_drive(char *filename, uint8_t index) {
54 printf("[PISCSI] Drive index %d out of range.\nUnable to map file %s to drive.\n", index, filename);
58 int32_t tmp_fd = open(filename, O_RDWR);
60 printf("[PISCSI] Failed to open file %s, could not map drive %d.\n", filename, index);
64 struct piscsi_dev *d = &devs[index];
66 uint64_t file_size = lseek(tmp_fd, 0, SEEK_END);
67 lseek(tmp_fd, 0, SEEK_SET);
68 printf("[PISCSI] Map %d: [%s] - %llu bytes.\n", index, filename, file_size);
71 d->c = (file_size / 512) / (d->s * d->h);
72 printf("[PISCSI] CHS: %d %d %d\n", d->c, d->h, d->s);
77 void piscsi_unmap_drive(uint8_t index) {
78 if (devs[index].fd != -1) {
79 printf("[PISCSI] Unmapped drive %d.\n", index);
80 close (devs[index].fd);
85 extern struct emulator_config *cfg;
87 void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
90 struct piscsi_dev *d = &devs[piscsi_cur_drive];
92 switch (addr & 0xFFFF) {
95 printf ("[PISCSI] BUG: Attempted read from unmapped drive %d.\n", piscsi_cur_drive);
98 printf("[PISCSI] %d byte READ from block %d to address %.8X\n", piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
99 r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
100 if (r != -1 && cfg->map_type[r] == MAPTYPE_RAM) {
101 printf("[PISCSI] \"DMA\" Read goes to mapped range %d.\n", r);
102 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
103 read(d->fd, cfg->map_data[r] + piscsi_u32[2] - cfg->map_offset[r], piscsi_u32[1]);
106 printf("[PISCSI] No mapped range found for read.\n");
108 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
109 for (int i = 0; i < piscsi_u32[1]; i++) {
112 write8(piscsi_u32[2] + i, (uint32_t)c);
117 case PISCSI_CMD_WRITE:
119 printf ("[PISCSI] BUG: Attempted write to unmapped drive %d.\n", piscsi_cur_drive);
122 printf("[PISCSI] %d byte WRITE to block %d to address %.8X\n", piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
123 r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
125 printf("[PISCSI] \"DMA\" Write comes from mapped range %d.\n", r);
126 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
127 write(d->fd, cfg->map_data[r] + piscsi_u32[2] - cfg->map_offset[r], piscsi_u32[1]);
130 printf("[PISCSI] No mapped range found for write.\n");
132 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
133 for (int i = 0; i < piscsi_u32[1]; i++) {
135 c = read8(piscsi_u32[2] + i);
141 case PISCSI_CMD_ADDR1:
143 printf("[PISCSI] Write to ADDR1: %.8x\n", piscsi_u32[0]);
145 case PISCSI_CMD_ADDR2:
147 printf("[PISCSI] Write to ADDR2: %.8x\n", piscsi_u32[1]);
149 case PISCSI_CMD_ADDR3:
151 printf("[PISCSI] Write to ADDR3: %.8x\n", piscsi_u32[2]);
153 case PISCSI_CMD_ADDR4:
155 printf("[PISCSI] Write to ADDR4: %.8x\n", piscsi_u32[3]);
157 case PISCSI_CMD_DRVNUM:
159 if (val < 10) // Kludge for GiggleDisk
160 piscsi_cur_drive = val;
161 else if (val >= 10 && val % 10 != 0)
162 piscsi_cur_drive = 255;
164 piscsi_cur_drive = val / 10;
167 piscsi_cur_drive = val;
168 printf("[PISCSI] (%s) Drive number set to %d (%d)\n", op_type_names[type], piscsi_cur_drive, val);
171 printf("[PISCSI] Unhandled %s register write to %.8X: %d\n", op_type_names[type], addr, val);
176 uint32_t handle_piscsi_read(uint32_t addr, uint8_t type) {
179 if ((addr & 0xFFFF) >= PISCSI_CMD_ROM) {
180 uint32_t romoffs = (addr & 0xFFFF) - PISCSI_CMD_ROM;
181 if (romoffs < piscsi_rom_size) {
182 printf("[PISCSI] %s read from Boot ROM @$%.4X (%.8X)\n", op_type_names[type], romoffs, addr);
186 v = piscsi_rom_ptr[romoffs];
189 v = *((uint16_t *)&piscsi_rom_ptr[romoffs]);
191 case OP_TYPE_LONGWORD:
192 v = *((uint32_t *)&piscsi_rom_ptr[romoffs]);
200 switch (addr & 0xFFFF) {
201 case PISCSI_CMD_DRVTYPE:
202 if (devs[piscsi_cur_drive].fd == -1) {
203 printf("[PISCSI] %s Read from DRVTYPE %d, drive not attached.\n", op_type_names[type], piscsi_cur_drive);
206 printf("[PISCSI] %s Read from DRVTYPE %d, drive attached.\n", op_type_names[type], piscsi_cur_drive);
209 case PISCSI_CMD_DRVNUM:
210 return piscsi_cur_drive;
212 case PISCSI_CMD_CYLS:
213 printf("[PISCSI] %s Read from CYLS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].c);
214 return devs[piscsi_cur_drive].c;
216 case PISCSI_CMD_HEADS:
217 printf("[PISCSI] %s Read from HEADS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].h);
218 return devs[piscsi_cur_drive].h;
220 case PISCSI_CMD_SECS:
221 printf("[PISCSI] %s Read from SECS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].s);
222 return devs[piscsi_cur_drive].s;
224 case PISCSI_CMD_BLOCKS: {
225 uint32_t blox = devs[piscsi_cur_drive].fs / 512;
226 printf("[PISCSI] %s Read from BLOCKS %d: %d\n", op_type_names[type], piscsi_cur_drive, (uint32_t)(devs[piscsi_cur_drive].fs / 512));
227 printf("fs: %lld (%d)\n", devs[piscsi_cur_drive].fs, blox);
232 printf("[PISCSI] Unhandled %s register read from %.8X\n", op_type_names[type], addr);
239 void piscsi_block_op(uint8_t type, uint8_t num, uint32_t dest, uint32_t len) {
240 if (type || num || dest || len) {}