src/mess/machine/wd33c93.c
| r1120 | r1121 | |
| 1 | | /* |
| 2 | | * wd33c93.c |
| 3 | | * |
| 4 | | * WD/AMD 33c93 SCSI controller, as seen in |
| 5 | | * early PCs, some MSX add-ons, NEC PC-88, and SGI |
| 6 | | * Indigo, Indigo2, and Indy systems. |
| 7 | | * |
| 8 | | * References: |
| 9 | | * WD 33c93 manual |
| 10 | | * NetBSD 33c93 driver |
| 11 | | * |
| 12 | | */ |
| 13 | | |
| 14 | | #include "driver.h" |
| 15 | | #include "state.h" |
| 16 | | #include "wd33c93.h" |
| 17 | | |
| 18 | | typedef struct |
| 19 | | { |
| 20 | | void *data; // device's "this" pointer |
| 21 | | pSCSIDispatch handler; // device's handler routine |
| 22 | | } SCSIDev; |
| 23 | | |
| 24 | | static SCSIDev devices[8]; // SCSI IDs 0-7 |
| 25 | | static struct WD33C93interface *intf; |
| 26 | | |
| 27 | | /* wd register names */ |
| 28 | | #define WD_OWN_ID 0x00 |
| 29 | | #define WD_CONTROL 0x01 |
| 30 | | #define WD_TIMEOUT_PERIOD 0x02 |
| 31 | | #define WD_CDB_1 0x03 |
| 32 | | #define WD_CDB_2 0x04 |
| 33 | | #define WD_CDB_3 0x05 |
| 34 | | #define WD_CDB_4 0x06 |
| 35 | | #define WD_CDB_5 0x07 |
| 36 | | #define WD_CDB_6 0x08 |
| 37 | | #define WD_CDB_7 0x09 |
| 38 | | #define WD_CDB_8 0x0a |
| 39 | | #define WD_CDB_9 0x0b |
| 40 | | #define WD_CDB_10 0x0c |
| 41 | | #define WD_CDB_11 0x0d |
| 42 | | #define WD_CDB_12 0x0e |
| 43 | | #define WD_TARGET_LUN 0x0f |
| 44 | | #define WD_COMMAND_PHASE 0x10 |
| 45 | | #define WD_SYNCHRONOUS_TRANSFER 0x11 |
| 46 | | #define WD_TRANSFER_COUNT_MSB 0x12 |
| 47 | | #define WD_TRANSFER_COUNT 0x13 |
| 48 | | #define WD_TRANSFER_COUNT_LSB 0x14 |
| 49 | | #define WD_DESTINATION_ID 0x15 |
| 50 | | #define WD_SOURCE_ID 0x16 |
| 51 | | #define WD_SCSI_STATUS 0x17 |
| 52 | | #define WD_COMMAND 0x18 |
| 53 | | #define WD_DATA 0x19 |
| 54 | | #define WD_QUEUE_TAG 0x1a |
| 55 | | #define WD_AUXILIARY_STATUS 0x1f |
| 56 | | |
| 57 | | /* WD commands */ |
| 58 | | #define WD_CMD_RESET 0x00 |
| 59 | | #define WD_CMD_ABORT 0x01 |
| 60 | | #define WD_CMD_ASSERT_ATN 0x02 |
| 61 | | #define WD_CMD_NEGATE_ACK 0x03 |
| 62 | | #define WD_CMD_DISCONNECT 0x04 |
| 63 | | #define WD_CMD_RESELECT 0x05 |
| 64 | | #define WD_CMD_SEL_ATN 0x06 |
| 65 | | #define WD_CMD_SEL 0x07 |
| 66 | | #define WD_CMD_SEL_ATN_XFER 0x08 |
| 67 | | #define WD_CMD_SEL_XFER 0x09 |
| 68 | | #define WD_CMD_RESEL_RECEIVE 0x0a |
| 69 | | #define WD_CMD_RESEL_SEND 0x0b |
| 70 | | #define WD_CMD_WAIT_SEL_RECEIVE 0x0c |
| 71 | | #define WD_CMD_SSCC 0x0d |
| 72 | | #define WD_CMD_SND_DISC 0x0e |
| 73 | | #define WD_CMD_SET_IDI 0x0f |
| 74 | | #define WD_CMD_RCV_CMD 0x10 |
| 75 | | #define WD_CMD_RCV_DATA 0x11 |
| 76 | | #define WD_CMD_RCV_MSG_OUT 0x12 |
| 77 | | #define WD_CMD_RCV 0x13 |
| 78 | | #define WD_CMD_SND_STATUS 0x14 |
| 79 | | #define WD_CMD_SND_DATA 0x15 |
| 80 | | #define WD_CMD_SND_MSG_IN 0x16 |
| 81 | | #define WD_CMD_SND 0x17 |
| 82 | | #define WD_CMD_TRANS_ADDR 0x18 |
| 83 | | #define WD_CMD_XFER_PAD 0x19 |
| 84 | | #define WD_CMD_TRANS_INFO 0x20 |
| 85 | | #define WD_CMD_TRANSFER_PAD 0x21 |
| 86 | | #define WD_CMD_SBT_MODE 0x80 |
| 87 | | |
| 88 | | /* ASR register */ |
| 89 | | #define ASR_INT 0x80 |
| 90 | | #define ASR_LCI 0x40 |
| 91 | | #define ASR_BSY 0x20 |
| 92 | | #define ASR_CIP 0x10 |
| 93 | | #define ASR_PE 0x02 |
| 94 | | #define ASR_DBR 0x01 |
| 95 | | |
| 96 | | /* SCSI Bus Phases */ |
| 97 | | #define PHS_DATA_OUT 0x00 |
| 98 | | #define PHS_DATA_IN 0x01 |
| 99 | | #define PHS_COMMAND 0x02 |
| 100 | | #define PHS_STATUS 0x03 |
| 101 | | #define PHS_MESS_OUT 0x06 |
| 102 | | #define PHS_MESS_IN 0x07 |
| 103 | | |
| 104 | | /* Command Status Register definitions */ |
| 105 | | |
| 106 | | /* reset state interrupts */ |
| 107 | | #define CSR_RESET 0x00 |
| 108 | | #define CSR_RESET_AF 0x01 |
| 109 | | |
| 110 | | /* successful completion interrupts */ |
| 111 | | #define CSR_RESELECT 0x10 |
| 112 | | #define CSR_SELECT 0x11 |
| 113 | | #define CSR_SEL_XFER_DONE 0x16 |
| 114 | | #define CSR_XFER_DONE 0x18 |
| 115 | | |
| 116 | | /* paused or aborted interrupts */ |
| 117 | | #define CSR_MSGIN 0x20 |
| 118 | | #define CSR_SDP 0x21 |
| 119 | | #define CSR_SEL_ABORT 0x22 |
| 120 | | #define CSR_RESEL_ABORT 0x25 |
| 121 | | #define CSR_RESEL_ABORT_AM 0x27 |
| 122 | | #define CSR_ABORT 0x28 |
| 123 | | |
| 124 | | /* terminated interrupts */ |
| 125 | | #define CSR_INVALID 0x40 |
| 126 | | #define CSR_UNEXP_DISC 0x41 |
| 127 | | #define CSR_TIMEOUT 0x42 |
| 128 | | #define CSR_PARITY 0x43 |
| 129 | | #define CSR_PARITY_ATN 0x44 |
| 130 | | #define CSR_BAD_STATUS 0x45 |
| 131 | | #define CSR_UNEXP 0x48 |
| 132 | | |
| 133 | | /* service required interrupts */ |
| 134 | | #define CSR_RESEL 0x80 |
| 135 | | #define CSR_RESEL_AM 0x81 |
| 136 | | #define CSR_DISC 0x85 |
| 137 | | #define CSR_SRV_REQ 0x88 |
| 138 | | |
| 139 | | /* Own ID/CDB Size register */ |
| 140 | | #define OWNID_EAF 0x08 |
| 141 | | #define OWNID_EHP 0x10 |
| 142 | | #define OWNID_RAF 0x20 |
| 143 | | #define OWNID_FS_8 0x00 |
| 144 | | #define OWNID_FS_12 0x40 |
| 145 | | #define OWNID_FS_16 0x80 |
| 146 | | |
| 147 | | /* Control register */ |
| 148 | | #define CTRL_HSP 0x01 |
| 149 | | #define CTRL_HA 0x02 |
| 150 | | #define CTRL_IDI 0x04 |
| 151 | | #define CTRL_EDI 0x08 |
| 152 | | #define CTRL_HHP 0x10 |
| 153 | | #define CTRL_POLLED 0x00 |
| 154 | | #define CTRL_BURST 0x20 |
| 155 | | #define CTRL_BUS 0x40 |
| 156 | | #define CTRL_DMA 0x80 |
| 157 | | |
| 158 | | /* Synchronous Transfer Register */ |
| 159 | | #define STR_FSS 0x80 |
| 160 | | |
| 161 | | /* Destination ID register */ |
| 162 | | #define DSTID_DPD 0x40 |
| 163 | | #define DATA_OUT_DIR 0 |
| 164 | | #define DATA_IN_DIR 1 |
| 165 | | #define DSTID_SCC 0x80 |
| 166 | | |
| 167 | | /* Source ID register */ |
| 168 | | #define SRCID_MASK 0x07 |
| 169 | | #define SRCID_SIV 0x08 |
| 170 | | #define SRCID_DSP 0x20 |
| 171 | | #define SRCID_ES 0x40 |
| 172 | | #define SRCID_ER 0x80 |
| 173 | | |
| 174 | | /* command handler definition */ |
| 175 | | typedef void (*cmd_handler)(void); |
| 176 | | |
| 177 | | #define TEMP_INPUT_LEN 65536 |
| 178 | | #define FIFO_SIZE 12 |
| 179 | | |
| 180 | | /* internal controller data definition */ |
| 181 | | typedef struct |
| 182 | | { |
| 183 | | UINT8 sasr; |
| 184 | | UINT8 regs[WD_AUXILIARY_STATUS+1]; |
| 185 | | UINT8 fifo[FIFO_SIZE]; |
| 186 | | int fifo_pos; |
| 187 | | UINT8 *temp_input; |
| 188 | | int temp_input_pos; |
| 189 | | UINT8 busphase; |
| 190 | | UINT8 identify; |
| 191 | | mame_timer *cmd_timer; |
| 192 | | } _wd33c93_data; |
| 193 | | |
| 194 | | /* local instance of controller data */ |
| 195 | | static _wd33c93_data scsi_data; |
| 196 | | |
| 197 | | |
| 198 | | /* convernience functions */ |
| 199 | | static UINT8 wd33c93_getunit( void ) |
| 200 | | { |
| 201 | | /* return the destination unit id */ |
| 202 | | return scsi_data.regs[WD_DESTINATION_ID] & SRCID_MASK; |
| 203 | | } |
| 204 | | |
| 205 | | static void wd33c93_set_xfer_count( int count ) |
| 206 | | { |
| 207 | | /* set the count */ |
| 208 | | scsi_data.regs[ WD_TRANSFER_COUNT_LSB ] = count & 0xff; |
| 209 | | scsi_data.regs[ WD_TRANSFER_COUNT ] = ( count >> 8 ) & 0xff; |
| 210 | | scsi_data.regs[ WD_TRANSFER_COUNT_MSB ] = ( count >> 16 ) & 0xff; |
| 211 | | } |
| 212 | | |
| 213 | | static int wd33c93_get_xfer_count( void ) |
| 214 | | { |
| 215 | | /* get the count */ |
| 216 | | int count = scsi_data.regs[ WD_TRANSFER_COUNT_MSB ]; |
| 217 | | |
| 218 | | count <<= 8; |
| 219 | | count |= scsi_data.regs[ WD_TRANSFER_COUNT ]; |
| 220 | | count <<= 8; |
| 221 | | count |= scsi_data.regs[ WD_TRANSFER_COUNT_LSB ]; |
| 222 | | |
| 223 | | return count; |
| 224 | | } |
| 225 | | |
| 226 | | static void wd33c93_read_data(int bytes, UINT8 *pData) |
| 227 | | { |
| 228 | | UINT8 unit = wd33c93_getunit(); |
| 229 | | |
| 230 | | if ( devices[unit].handler ) |
| 231 | | { |
| 232 | | devices[unit].handler(SCSIOP_READ_DATA, devices[unit].data, bytes, pData); |
| 233 | | } |
| 234 | | else |
| 235 | | { |
| 236 | | logerror("wd33c93: request for unknown device SCSI ID %d\n", unit); |
| 237 | | } |
| 238 | | } |
| 239 | | |
| 240 | | static TIMER_CALLBACK(wd33c93_complete_cb) |
| 241 | | { |
| 242 | | /* reset our timer */ |
| 243 | | mame_timer_reset( scsi_data.cmd_timer, time_never ); |
| 244 | | |
| 245 | | /* set the new status */ |
| 246 | | scsi_data.regs[WD_SCSI_STATUS] = param & 0xff; |
| 247 | | |
| 248 | | /* set interrupt pending */ |
| 249 | | scsi_data.regs[WD_AUXILIARY_STATUS] |= ASR_INT; |
| 250 | | |
| 251 | | /* check for error conditions */ |
| 252 | | if ( wd33c93_get_xfer_count() > 0 ) |
| 253 | | { |
| 254 | | /* set data buffer ready */ |
| 255 | | scsi_data.regs[WD_AUXILIARY_STATUS] |= ASR_DBR; |
| 256 | | } |
| 257 | | else |
| 258 | | { |
| 259 | | /* clear data buffer ready */ |
| 260 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~ASR_DBR; |
| 261 | | } |
| 262 | | |
| 263 | | /* clear command in progress and bus busy */ |
| 264 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~(ASR_CIP | ASR_BSY); |
| 265 | | |
| 266 | | /* if we have a callback, call it */ |
| 267 | | if (intf && intf->irq_callback) |
| 268 | | { |
| 269 | | intf->irq_callback(1); |
| 270 | | } |
| 271 | | } |
| 272 | | |
| 273 | | static TIMER_CALLBACK(wd33c93_service_request) |
| 274 | | { |
| 275 | | /* issue a message out request */ |
| 276 | | wd33c93_complete_cb(NULL,CSR_SRV_REQ | scsi_data.busphase); |
| 277 | | } |
| 278 | | |
| 279 | | static void wd33c93_complete_cmd( UINT8 status ) |
| 280 | | { |
| 281 | | /* fire off a timer to complete the command */ |
| 282 | | mame_timer_adjust( scsi_data.cmd_timer, MAME_TIME_IN_USEC(1), status, time_zero ); |
| 283 | | } |
| 284 | | |
| 285 | | /* command handlers */ |
| 286 | | static void wd33c93_invalid_cmd( void ) |
| 287 | | { |
| 288 | | logerror( "Unknown/Unimplemented SCSI controller command: %02x (PC=%x)\n", scsi_data.regs[WD_COMMAND], activecpu_get_pc() ); |
| 289 | | |
| 290 | | /* complete the command */ |
| 291 | | wd33c93_complete_cmd( CSR_INVALID ); |
| 292 | | } |
| 293 | | |
| 294 | | static void wd33c93_reset_cmd( void ) |
| 295 | | { |
| 296 | | int advanced = 0; |
| 297 | | |
| 298 | | /* see if it wants us to reset with advanced features */ |
| 299 | | if ( scsi_data.regs[WD_OWN_ID] & OWNID_EAF ) |
| 300 | | { |
| 301 | | advanced = 1; |
| 302 | | } |
| 303 | | |
| 304 | | /* clear out all registers */ |
| 305 | | memset( scsi_data.regs, 0, sizeof( scsi_data.regs ) ); |
| 306 | | |
| 307 | | /* complete the command */ |
| 308 | | wd33c93_complete_cmd(advanced ? CSR_RESET_AF : CSR_RESET); |
| 309 | | } |
| 310 | | |
| 311 | | static void wd33c93_abort_cmd( void ) |
| 312 | | { |
| 313 | | /* complete the command */ |
| 314 | | wd33c93_complete_cmd(CSR_ABORT); |
| 315 | | } |
| 316 | | |
| 317 | | static void wd33c93_disconnect_cmd( void ) |
| 318 | | { |
| 319 | | /* complete the command */ |
| 320 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~(ASR_CIP | ASR_BSY); |
| 321 | | } |
| 322 | | |
| 323 | | static void wd33c93_select_cmd( void ) |
| 324 | | { |
| 325 | | UINT8 unit = wd33c93_getunit(); |
| 326 | | UINT8 newstatus; |
| 327 | | |
| 328 | | /* see if we can select that device */ |
| 329 | | if ( devices[unit].handler ) |
| 330 | | { |
| 331 | | /* device is available - signal selection done */ |
| 332 | | newstatus = CSR_SELECT; |
| 333 | | |
| 334 | | /* determine the next bus phase depending on the command */ |
| 335 | | if ( (scsi_data.regs[WD_COMMAND] & 0x7f) == WD_CMD_SEL_ATN ) |
| 336 | | { |
| 337 | | /* /ATN asserted during select: Move to Message Out Phase to read identify */ |
| 338 | | scsi_data.busphase = PHS_MESS_OUT; |
| 339 | | } |
| 340 | | else |
| 341 | | { |
| 342 | | /* No /ATN asserted: Move to Command Phase */ |
| 343 | | scsi_data.busphase = PHS_COMMAND; |
| 344 | | } |
| 345 | | |
| 346 | | /* queue up a service request out in the future */ |
| 347 | | mame_timer_set( MAME_TIME_IN_USEC(50), 0, wd33c93_service_request ); |
| 348 | | } |
| 349 | | else |
| 350 | | { |
| 351 | | /* device is not available */ |
| 352 | | newstatus = CSR_TIMEOUT; |
| 353 | | } |
| 354 | | |
| 355 | | /* complete the command */ |
| 356 | | wd33c93_complete_cmd(newstatus); |
| 357 | | } |
| 358 | | |
| 359 | | static void wd33c93_selectxfer_cmd( void ) |
| 360 | | { |
| 361 | | UINT8 unit = wd33c93_getunit(); |
| 362 | | UINT8 newstatus; |
| 363 | | |
| 364 | | /* see if we can select that device */ |
| 365 | | if ( devices[unit].handler ) |
| 366 | | { |
| 367 | | if ( scsi_data.regs[WD_COMMAND_PHASE] < 0x45 ) |
| 368 | | { |
| 369 | | /* device is available */ |
| 370 | | int xfercount; |
| 371 | | |
| 372 | | /* do the request */ |
| 373 | | xfercount = devices[unit].handler(SCSIOP_EXEC_COMMAND, devices[unit].data, 0, &scsi_data.regs[WD_CDB_1]); |
| 374 | | |
| 375 | | /* set transfer count */ |
| 376 | | wd33c93_set_xfer_count( xfercount ); |
| 377 | | |
| 378 | | /* transfer over the data now */ |
| 379 | | memset( scsi_data.temp_input, 0, TEMP_INPUT_LEN ); |
| 380 | | wd33c93_read_data( TEMP_INPUT_LEN, &scsi_data.temp_input[0] ); |
| 381 | | scsi_data.temp_input_pos = 0; |
| 382 | | } |
| 383 | | |
| 384 | | scsi_data.regs[WD_TARGET_LUN] = 0; |
| 385 | | scsi_data.regs[WD_CONTROL] |= CTRL_EDI; |
| 386 | | scsi_data.regs[WD_COMMAND_PHASE] = 0x60; |
| 387 | | |
| 388 | | /* signal transfer ready */ |
| 389 | | newstatus = CSR_SEL_XFER_DONE; |
| 390 | | |
| 391 | | /* if allowed disconnect, queue a service request */ |
| 392 | | if ( scsi_data.identify & 0x40 ) |
| 393 | | { |
| 394 | | /* queue disconnect message in */ |
| 395 | | scsi_data.busphase = PHS_MESS_IN; |
| 396 | | |
| 397 | | /* queue up a service request out in the future */ |
| 398 | | mame_timer_set( MAME_TIME_IN_MSEC(50), 0, wd33c93_service_request ); |
| 399 | | } |
| 400 | | } |
| 401 | | else |
| 402 | | { |
| 403 | | /* device is not available */ |
| 404 | | newstatus = CSR_TIMEOUT; |
| 405 | | } |
| 406 | | |
| 407 | | /* complete the command */ |
| 408 | | wd33c93_complete_cmd(newstatus); |
| 409 | | } |
| 410 | | |
| 411 | | static void wd33c93_xferinfo_cmd( void ) |
| 412 | | { |
| 413 | | /* make the buffer available right away */ |
| 414 | | scsi_data.regs[WD_AUXILIARY_STATUS] |= ASR_DBR; |
| 415 | | |
| 416 | | /* the command will be completed once the data is transferred */ |
| 417 | | } |
| 418 | | |
| 419 | | /* Command handlers */ |
| 420 | | static cmd_handler wd33c93_cmds[0x22] = |
| 421 | | { |
| 422 | | &wd33c93_reset_cmd, /* 0x00 - WD_CMD_RESET */ |
| 423 | | &wd33c93_abort_cmd, /* 0x01 - WD_CMD_ABORT */ |
| 424 | | &wd33c93_invalid_cmd, /* 0x02 - WD_CMD_ASSERT_ATN (uninmplemented) */ |
| 425 | | &wd33c93_invalid_cmd, /* 0x03 - WD_CMD_NEGATE_ACK (uninmplemented) */ |
| 426 | | &wd33c93_disconnect_cmd,/* 0x04 - WD_CMD_DISCONNECT */ |
| 427 | | &wd33c93_invalid_cmd, /* 0x05 - WD_CMD_RESELECT (uninmplemented) */ |
| 428 | | &wd33c93_select_cmd, /* 0x06 - WD_CMD_SEL_ATN */ |
| 429 | | &wd33c93_select_cmd, /* 0x07 - WD_CMD_SEL */ |
| 430 | | &wd33c93_selectxfer_cmd,/* 0x08 - WD_CMD_SEL_ATN_XFER */ |
| 431 | | &wd33c93_selectxfer_cmd,/* 0x09 - WD_CMD_SEL_XFER */ |
| 432 | | &wd33c93_invalid_cmd, /* 0x0a - WD_CMD_RESEL_RECEIVE (uninmplemented) */ |
| 433 | | &wd33c93_invalid_cmd, /* 0x0b - WD_CMD_RESEL_SEND (uninmplemented) */ |
| 434 | | &wd33c93_invalid_cmd, /* 0x0c - WD_CMD_WAIT_SEL_RECEIVE (uninmplemented) */ |
| 435 | | &wd33c93_invalid_cmd, /* 0x0d - WD_CMD_SSCC (uninmplemented) */ |
| 436 | | &wd33c93_invalid_cmd, /* 0x0e - WD_CMD_SND_DISC (uninmplemented) */ |
| 437 | | &wd33c93_invalid_cmd, /* 0x0f - WD_CMD_SET_IDI (uninmplemented) */ |
| 438 | | &wd33c93_invalid_cmd, /* 0x10 - WD_CMD_RCV_CMD (uninmplemented) */ |
| 439 | | &wd33c93_invalid_cmd, /* 0x11 - WD_CMD_RCV_DATA (uninmplemented) */ |
| 440 | | &wd33c93_invalid_cmd, /* 0x12 - WD_CMD_RCV_MSG_OUT (uninmplemented) */ |
| 441 | | &wd33c93_invalid_cmd, /* 0x13 - WD_CMD_RCV (uninmplemented) */ |
| 442 | | &wd33c93_invalid_cmd, /* 0x14 - WD_CMD_SND_STATUS (uninmplemented) */ |
| 443 | | &wd33c93_invalid_cmd, /* 0x15 - WD_CMD_SND_DATA (uninmplemented) */ |
| 444 | | &wd33c93_invalid_cmd, /* 0x16 - WD_CMD_SND_MSG_IN (uninmplemented) */ |
| 445 | | &wd33c93_invalid_cmd, /* 0x17 - WD_CMD_SND (uninmplemented) */ |
| 446 | | &wd33c93_invalid_cmd, /* 0x18 - WD_CMD_TRANS_ADDR (uninmplemented) */ |
| 447 | | &wd33c93_invalid_cmd, /* 0x19 - WD_CMD_XFER_PAD (uninmplemented) */ |
| 448 | | &wd33c93_invalid_cmd, /* 0x1a - invalid */ |
| 449 | | &wd33c93_invalid_cmd, /* 0x1b - invalid */ |
| 450 | | &wd33c93_invalid_cmd, /* 0x1c - invalid */ |
| 451 | | &wd33c93_invalid_cmd, /* 0x1d - invalid */ |
| 452 | | &wd33c93_invalid_cmd, /* 0x1e - invalid */ |
| 453 | | &wd33c93_invalid_cmd, /* 0x1f - invalid */ |
| 454 | | &wd33c93_xferinfo_cmd, /* 0x20 - WD_CMD_TRANS_INFO) */ |
| 455 | | &wd33c93_invalid_cmd /* 0x21 - WD_CMD_TRANSFER_PAD (uninmplemented) */ |
| 456 | | }; |
| 457 | | |
| 458 | | /* Handle pending commands */ |
| 459 | | static void wd33c93_command( void ) |
| 460 | | { |
| 461 | | /* get the command */ |
| 462 | | UINT8 cmd = scsi_data.regs[WD_COMMAND]; |
| 463 | | |
| 464 | | /* check if its within valid bounds */ |
| 465 | | if ( (cmd & 0x7F) > WD_CMD_TRANSFER_PAD ) |
| 466 | | { |
| 467 | | wd33c93_invalid_cmd(); |
| 468 | | return; |
| 469 | | } |
| 470 | | |
| 471 | | /* call the command handler */ |
| 472 | | (*wd33c93_cmds[cmd & 0x7F])(); |
| 473 | | } |
| 474 | | |
| 475 | | WRITE8_HANDLER(wd33c93_w) |
| 476 | | { |
| 477 | | switch( offset ) |
| 478 | | { |
| 479 | | case 0: |
| 480 | | { |
| 481 | | /* update register select */ |
| 482 | | scsi_data.sasr = data & 0x1f; |
| 483 | | } |
| 484 | | break; |
| 485 | | |
| 486 | | case 1: |
| 487 | | { |
| 488 | | logerror( "WD33C93: PC=%08x - Write REG=%02x, data = %02x\n", safe_activecpu_get_pc(), scsi_data.sasr, data ); |
| 489 | | |
| 490 | | /* update the register */ |
| 491 | | scsi_data.regs[scsi_data.sasr] = data; |
| 492 | | |
| 493 | | /* if we receive a command, schedule to process it */ |
| 494 | | if ( scsi_data.sasr == WD_COMMAND ) |
| 495 | | { |
| 496 | | logerror( "WDC33C93: PC=%08x - Executing command %08x - unit %d\n", safe_activecpu_get_pc(), data, wd33c93_getunit() ); |
| 497 | | |
| 498 | | /* signal we're processing it */ |
| 499 | | scsi_data.regs[WD_AUXILIARY_STATUS] |= ASR_CIP; |
| 500 | | |
| 501 | | /* process the command */ |
| 502 | | wd33c93_command(); |
| 503 | | } |
| 504 | | else if ( scsi_data.sasr == WD_CDB_1 ) |
| 505 | | { |
| 506 | | scsi_data.regs[WD_COMMAND_PHASE] = 0; |
| 507 | | } |
| 508 | | else if ( scsi_data.sasr == WD_DATA ) |
| 509 | | { |
| 510 | | /* if data was written, and we have a count, send to device */ |
| 511 | | int count = wd33c93_get_xfer_count(); |
| 512 | | |
| 513 | | if ( scsi_data.regs[WD_COMMAND] & 0x80 ) |
| 514 | | count = 1; |
| 515 | | |
| 516 | | if ( count-- > 0 ) |
| 517 | | { |
| 518 | | /* write to FIFO */ |
| 519 | | if ( scsi_data.fifo_pos < FIFO_SIZE ) |
| 520 | | { |
| 521 | | scsi_data.fifo[scsi_data.fifo_pos++] = data; |
| 522 | | } |
| 523 | | |
| 524 | | /* update count */ |
| 525 | | wd33c93_set_xfer_count( count ); |
| 526 | | |
| 527 | | /* if we're done with the write, see where we're at */ |
| 528 | | if ( count == 0 ) |
| 529 | | { |
| 530 | | scsi_data.regs[WD_AUXILIARY_STATUS] |= ASR_INT; |
| 531 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~ASR_DBR; |
| 532 | | |
| 533 | | switch( scsi_data.busphase ) |
| 534 | | { |
| 535 | | case PHS_MESS_OUT: |
| 536 | | { |
| 537 | | /* reset fifo */ |
| 538 | | scsi_data.fifo_pos = 0; |
| 539 | | |
| 540 | | /* Message out phase. Data is probably SCSI Identify. Move to command phase. */ |
| 541 | | scsi_data.busphase = PHS_COMMAND; |
| 542 | | |
| 543 | | scsi_data.identify = scsi_data.fifo[0]; |
| 544 | | } |
| 545 | | break; |
| 546 | | |
| 547 | | case PHS_COMMAND: |
| 548 | | { |
| 549 | | UINT8 unit = wd33c93_getunit(); |
| 550 | | int xfercount; |
| 551 | | |
| 552 | | /* Execute the command. Depending on the command, we'll move to data in or out */ |
| 553 | | xfercount = devices[unit].handler(SCSIOP_EXEC_COMMAND, devices[unit].data, 0, &scsi_data.fifo[0]); |
| 554 | | |
| 555 | | /* reset fifo */ |
| 556 | | scsi_data.fifo_pos = 0; |
| 557 | | |
| 558 | | /* set the new count */ |
| 559 | | wd33c93_set_xfer_count( xfercount ); |
| 560 | | |
| 561 | | /* update bus phase */ |
| 562 | | if ( scsi_data.fifo[0] == 0x15 || scsi_data.fifo[0] == 0x55 ) /* MODE SELECT */ |
| 563 | | { |
| 564 | | scsi_data.busphase = PHS_DATA_OUT; |
| 565 | | } |
| 566 | | else |
| 567 | | { |
| 568 | | scsi_data.busphase = PHS_DATA_IN; |
| 569 | | |
| 570 | | /* load up all the response data now */ |
| 571 | | memset( scsi_data.temp_input, 0, TEMP_INPUT_LEN ); |
| 572 | | wd33c93_read_data( TEMP_INPUT_LEN, &scsi_data.temp_input[0] ); |
| 573 | | scsi_data.temp_input_pos = 0; |
| 574 | | } |
| 575 | | } |
| 576 | | break; |
| 577 | | |
| 578 | | case PHS_DATA_OUT: |
| 579 | | { |
| 580 | | /* write data out to device */ |
| 581 | | wd33c93_write_data( scsi_data.fifo_pos, scsi_data.fifo ); |
| 582 | | |
| 583 | | /* reset fifo */ |
| 584 | | scsi_data.fifo_pos = 0; |
| 585 | | |
| 586 | | /* move to status phase */ |
| 587 | | scsi_data.busphase = PHS_STATUS; |
| 588 | | } |
| 589 | | break; |
| 590 | | } |
| 591 | | |
| 592 | | /* complete the command */ |
| 593 | | wd33c93_complete_cmd(CSR_XFER_DONE | scsi_data.busphase); |
| 594 | | } |
| 595 | | } |
| 596 | | else |
| 597 | | { |
| 598 | | logerror( "WD33C93: Sending data to device with transfer count = 0!. Ignoring...\n" ); |
| 599 | | } |
| 600 | | } |
| 601 | | |
| 602 | | /* auto-increment register select if not on special registers */ |
| 603 | | if ( scsi_data.sasr != WD_COMMAND && scsi_data.sasr != WD_DATA && scsi_data.sasr != WD_AUXILIARY_STATUS ) |
| 604 | | { |
| 605 | | scsi_data.sasr = ( scsi_data.sasr + 1 ) & 0x1f; |
| 606 | | } |
| 607 | | } |
| 608 | | break; |
| 609 | | |
| 610 | | default: |
| 611 | | { |
| 612 | | logerror( "WD33C93: Write to invalid offset %d (data=%02x)\n", offset, data ); |
| 613 | | } |
| 614 | | break; |
| 615 | | } |
| 616 | | } |
| 617 | | |
| 618 | | READ8_HANDLER(wd33c93_r) |
| 619 | | { |
| 620 | | switch( offset ) |
| 621 | | { |
| 622 | | case 0: |
| 623 | | { |
| 624 | | /* read aux status */ |
| 625 | | return scsi_data.regs[WD_AUXILIARY_STATUS]; |
| 626 | | } |
| 627 | | break; |
| 628 | | |
| 629 | | case 1: |
| 630 | | { |
| 631 | | UINT8 ret; |
| 632 | | |
| 633 | | /* if reading status, clear irq flag */ |
| 634 | | if ( scsi_data.sasr == WD_SCSI_STATUS ) |
| 635 | | { |
| 636 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~ASR_INT; |
| 637 | | |
| 638 | | if (intf && intf->irq_callback) |
| 639 | | { |
| 640 | | intf->irq_callback(0); |
| 641 | | } |
| 642 | | |
| 643 | | logerror( "WD33C93: PC=%08x - Status read (%02x)\n", safe_activecpu_get_pc(), scsi_data.regs[WD_SCSI_STATUS] ); |
| 644 | | } |
| 645 | | else if ( scsi_data.sasr == WD_DATA ) |
| 646 | | { |
| 647 | | /* we're going to be doing synchronous reads */ |
| 648 | | |
| 649 | | /* get the transfer count */ |
| 650 | | int count = wd33c93_get_xfer_count(); |
| 651 | | |
| 652 | | if ( count <= 0 && scsi_data.busphase == PHS_MESS_IN ) |
| 653 | | { |
| 654 | | scsi_data.regs[WD_DATA] = 0x00; |
| 655 | | |
| 656 | | /* move to disconnect */ |
| 657 | | wd33c93_complete_cmd(CSR_DISC); |
| 658 | | } |
| 659 | | else |
| 660 | | { |
| 661 | | /* make sure we still have data to send */ |
| 662 | | if ( count-- > 0 ) |
| 663 | | { |
| 664 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~ASR_INT; |
| 665 | | |
| 666 | | /* read in one byte */ |
| 667 | | if ( scsi_data.temp_input_pos < TEMP_INPUT_LEN ) |
| 668 | | scsi_data.regs[WD_DATA] = scsi_data.temp_input[scsi_data.temp_input_pos++]; |
| 669 | | |
| 670 | | /* update the count */ |
| 671 | | wd33c93_set_xfer_count( count ); |
| 672 | | |
| 673 | | /* transfer finished, see where we're at */ |
| 674 | | if ( count == 0 ) |
| 675 | | { |
| 676 | | if ( scsi_data.regs[WD_COMMAND_PHASE] != 0x60 ) |
| 677 | | { |
| 678 | | /* move to status phase */ |
| 679 | | scsi_data.busphase = PHS_STATUS; |
| 680 | | |
| 681 | | /* complete the command */ |
| 682 | | wd33c93_complete_cmd(CSR_XFER_DONE | scsi_data.busphase); |
| 683 | | } |
| 684 | | else |
| 685 | | { |
| 686 | | scsi_data.regs[WD_AUXILIARY_STATUS] |= ASR_INT; |
| 687 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~ASR_DBR; |
| 688 | | } |
| 689 | | } |
| 690 | | } |
| 691 | | else |
| 692 | | { |
| 693 | | scsi_data.regs[WD_DATA] = 0; |
| 694 | | } |
| 695 | | } |
| 696 | | } |
| 697 | | |
| 698 | | /* get the register value */ |
| 699 | | ret = scsi_data.regs[scsi_data.sasr]; |
| 700 | | |
| 701 | | /* auto-increment register select if not on special registers */ |
| 702 | | if ( scsi_data.sasr != WD_COMMAND && scsi_data.sasr != WD_DATA && scsi_data.sasr != WD_AUXILIARY_STATUS ) |
| 703 | | { |
| 704 | | scsi_data.sasr = ( scsi_data.sasr + 1 ) & 0x1f; |
| 705 | | } |
| 706 | | |
| 707 | | return ret; |
| 708 | | } |
| 709 | | |
| 710 | | default: |
| 711 | | { |
| 712 | | logerror( "WD33C93: Read from invalid offset %d\n", offset ); |
| 713 | | } |
| 714 | | break; |
| 715 | | } |
| 716 | | |
| 717 | | return 0; |
| 718 | | } |
| 719 | | |
| 720 | | extern void wd33c93_init( struct WD33C93interface *interface ) |
| 721 | | { |
| 722 | | int i; |
| 723 | | |
| 724 | | // save interface pointer for later |
| 725 | | intf = interface; |
| 726 | | |
| 727 | | memset(&scsi_data, 0, sizeof(scsi_data)); |
| 728 | | memset(devices, 0, sizeof(devices)); |
| 729 | | |
| 730 | | // try to open the devices |
| 731 | | for (i = 0; i < interface->scsidevs->devs_present; i++) |
| 732 | | { |
| 733 | | devices[interface->scsidevs->devices[i].scsiID].handler = interface->scsidevs->devices[i].handler; |
| 734 | | interface->scsidevs->devices[i].handler(SCSIOP_ALLOC_INSTANCE, &devices[interface->scsidevs->devices[i].scsiID].data, interface->scsidevs->devices[i].diskID, (UINT8 *)NULL); |
| 735 | | } |
| 736 | | |
| 737 | | /* allocate a timer for commands */ |
| 738 | | scsi_data.cmd_timer = mame_timer_alloc(wd33c93_complete_cb); |
| 739 | | |
| 740 | | scsi_data.temp_input = auto_malloc( TEMP_INPUT_LEN ); |
| 741 | | |
| 742 | | // state_save_register_item_array("wd33c93", 0, scsi_data); |
| 743 | | } |
| 744 | | |
| 745 | | void wd33c93_get_dma_data( int bytes, UINT8 *pData ) |
| 746 | | { |
| 747 | | int len = bytes; |
| 748 | | |
| 749 | | if ( len > wd33c93_get_xfer_count() ) |
| 750 | | len = wd33c93_get_xfer_count(); |
| 751 | | |
| 752 | | if ( len == 0 ) |
| 753 | | return; |
| 754 | | |
| 755 | | if ( (scsi_data.temp_input_pos+len) >= TEMP_INPUT_LEN ) |
| 756 | | { |
| 757 | | logerror( "Reading past end of buffer, increase TEMP_INPUT_LEN size\n" ); |
| 758 | | len = TEMP_INPUT_LEN - len; |
| 759 | | } |
| 760 | | |
| 761 | | memcpy( pData, &scsi_data.temp_input[scsi_data.temp_input_pos], len ); |
| 762 | | scsi_data.temp_input_pos += len; |
| 763 | | len = wd33c93_get_xfer_count() - len; |
| 764 | | wd33c93_set_xfer_count(len); |
| 765 | | } |
| 766 | | |
| 767 | | void wd33c93_write_data(int bytes, UINT8 *pData) |
| 768 | | { |
| 769 | | UINT8 unit = wd33c93_getunit(); |
| 770 | | |
| 771 | | if (devices[unit].handler) |
| 772 | | { |
| 773 | | devices[unit].handler(SCSIOP_WRITE_DATA, devices[unit].data, bytes, pData); |
| 774 | | } |
| 775 | | else |
| 776 | | { |
| 777 | | logerror("wd33c93: request for unknown device SCSI ID %d\n", unit); |
| 778 | | } |
| 779 | | } |
| 780 | | |
| 781 | | void *wd33c93_get_device(int id) |
| 782 | | { |
| 783 | | void *ret; |
| 784 | | |
| 785 | | if (devices[id].handler) |
| 786 | | { |
| 787 | | devices[id].handler(SCSIOP_GET_DEVICE, devices[id].data, 0, (UINT8 *)&ret); |
| 788 | | |
| 789 | | return ret; |
| 790 | | } |
| 791 | | |
| 792 | | return NULL; |
| 793 | | } |
| 794 | | |
| 795 | | void wd33c93_clear_dma(void) |
| 796 | | { |
| 797 | | /* indicate DMA completed by clearing the transfer count */ |
| 798 | | wd33c93_set_xfer_count(0); |
| 799 | | scsi_data.regs[WD_AUXILIARY_STATUS] &= ~ASR_DBR; |
| 800 | | } |
| 801 | | |
| 802 | | int wd33c93_get_dma_count(void) |
| 803 | | { |
| 804 | | return wd33c93_get_xfer_count(); |
| 805 | | } |