src/mess/drivers/fmtowns.c
| r6452 | r6453 | |
| 112 | 112 | #include "cpu/i386/i386.h" |
| 113 | 113 | #include "sound/2612intf.h" |
| 114 | 114 | #include "sound/rf5c68.h" |
| 115 | #include "sound/cdda.h" |
| 116 | #include "devices/chd_cd.h" |
| 115 | 117 | #include "machine/pit8253.h" |
| 116 | 118 | #include "machine/pic8259.h" |
| 117 | 119 | #include "formats/basicdsk.h" |
| r6452 | r6453 | |
| 170 | 172 | UINT8 status; |
| 171 | 173 | UINT8 cmd_status[4]; |
| 172 | 174 | UINT8 cmd_status_ptr; |
| 175 | UINT8 extra_status; |
| 173 | 176 | UINT8 parameter[8]; |
| 177 | UINT8 mpu_irq_enable; |
| 178 | UINT8 dma_irq_enable; |
| 179 | UINT8 buffer[2048]; |
| 180 | INT32 buffer_ptr; |
| 181 | UINT32 lba_current; |
| 182 | UINT32 lba_last; |
| 183 | emu_timer* read_timer; |
| 174 | 184 | } towns_cd; |
| 175 | 185 | |
| 176 | 186 | static void towns_update_video_banks(const address_space* space); |
| 177 | 187 | static PIC8259_SET_INT_LINE( towns_pic_irq ); |
| 178 | 188 | |
| 189 | INLINE UINT32 bcd_to_dec(UINT32 val) |
| 190 | { |
| 191 | return (val & 0x00000f) + |
| 192 | (((val & 0x0000f0) >> 4) * 10) + |
| 193 | (((val & 0x000f00) >> 8) * 100) + |
| 194 | (((val & 0x00f000) >> 12) * 1000) + |
| 195 | (((val & 0x0f0000) >> 16) * 10000) + |
| 196 | (((val & 0xf00000) >> 24) * 100000); |
| 197 | } |
| 198 | |
| 179 | 199 | static void towns_init_serial_rom(running_machine* machine) |
| 180 | 200 | { |
| 181 | 201 | // TODO: init serial ROM contents |
| r6452 | r6453 | |
| 1166 | 1186 | * bit 4 - DMA transfer mode |
| 1167 | 1187 | * |
| 1168 | 1188 | */ |
| 1169 | | static void towns_cdrom_execute_command(void) |
| 1189 | static TIMER_CALLBACK( towns_cdrom_read_byte ) |
| 1170 | 1190 | { |
| 1171 | | // For now, we'll return status codes that hopefully tell the system |
| 1172 | | // that there is no CD-ROM in the drive. |
| 1191 | const device_config* device = ptr; |
| 1192 | int masked; |
| 1193 | // TODO: support software transfers, for now DMA is assumed. |
| 1173 | 1194 | |
| 1174 | | towns_cd.status |= 0x02; // status read request |
| 1195 | if(towns_cd.buffer_ptr < 0) // transfer has ended |
| 1196 | return; |
| 1197 | |
| 1198 | masked = upd71071_dmarq(device,param,3); // CD-ROM controller uses DMA1 channel 3 |
| 1199 | //logerror("DMARQ: param=%i ret=%i bufferptr=%i\n",param,masked,towns_cd.buffer_ptr); |
| 1200 | if(param != 0) |
| 1201 | { |
| 1202 | timer_adjust_oneshot(towns_cd.read_timer,ATTOTIME_IN_HZ(300000),0); |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | if(masked != 0) // check if the DMA channel is masked |
| 1207 | { |
| 1208 | timer_adjust_oneshot(towns_cd.read_timer,ATTOTIME_IN_HZ(300000),1); |
| 1209 | return; |
| 1210 | } |
| 1211 | if(towns_cd.buffer_ptr < 2048) |
| 1212 | timer_adjust_oneshot(towns_cd.read_timer,ATTOTIME_IN_HZ(300000),1); |
| 1213 | else |
| 1214 | { // end of transfer |
| 1215 | towns_cd.status &= ~0x10; // no longer transferring by DMA |
| 1216 | towns_cd.status &= ~0x20; // no longer transferring by software |
| 1217 | logerror("DMA1: end of transfer (LBA=%08x)\n",towns_cd.lba_current); |
| 1218 | if(towns_cd.lba_current >= towns_cd.lba_last) |
| 1219 | { |
| 1220 | towns_cd.status |= 0x02; // status read request |
| 1221 | towns_cd.cmd_status_ptr = 0; |
| 1222 | towns_cd.extra_status = 0; |
| 1223 | towns_cd.cmd_status[0] = 0x06; |
| 1224 | towns_cd.cmd_status[1] = 0x00; |
| 1225 | towns_cd.cmd_status[2] = 0x00; |
| 1226 | towns_cd.cmd_status[3] = 0x00; |
| 1227 | if(towns_cd.dma_irq_enable != 0) |
| 1228 | { |
| 1229 | towns_cd.status |= 0x40; // IRQ from end of DMA |
| 1230 | pic8259_set_irq_line(devtag_get_device(device->machine,"pic8259_slave"),1,0); |
| 1231 | } |
| 1232 | towns_cd.status |= 0x01; // ready |
| 1233 | } |
| 1234 | else |
| 1235 | { |
| 1236 | towns_cd.status |= 0x02; // status read request |
| 1237 | towns_cd.cmd_status_ptr = 0; |
| 1238 | towns_cd.extra_status = 0; |
| 1239 | towns_cd.cmd_status[0] = 0x22; |
| 1240 | towns_cd.cmd_status[1] = 0x00; |
| 1241 | towns_cd.cmd_status[2] = 0x00; |
| 1242 | towns_cd.cmd_status[3] = 0x00; |
| 1243 | if(towns_cd.dma_irq_enable != 0) |
| 1244 | { |
| 1245 | towns_cd.status |= 0x40; // IRQ from end of DMA |
| 1246 | pic8259_set_irq_line(devtag_get_device(device->machine,"pic8259_slave"),1,0); |
| 1247 | } |
| 1248 | cdrom_read_data(mess_cd_get_cdrom_file(devtag_get_device(device->machine,"cdrom")),++towns_cd.lba_current,towns_cd.buffer,CD_TRACK_MODE1); |
| 1249 | timer_adjust_oneshot(towns_cd.read_timer,ATTOTIME_IN_HZ(300000),1); |
| 1250 | towns_cd.buffer_ptr = -1; |
| 1251 | towns_cd.status |= 0x01; // ready |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | static void towns_cdrom_read(const device_config* device) |
| 1258 | { |
| 1259 | // MODE 1 read |
| 1260 | // load data into buffer to be sent via DMA1 channel 3 |
| 1261 | UINT32 lba1,lba2; |
| 1262 | |
| 1263 | lba1 = towns_cd.parameter[7] << 16; |
| 1264 | lba1 += towns_cd.parameter[6] << 8; |
| 1265 | lba1 += towns_cd.parameter[5]; |
| 1266 | lba2 = towns_cd.parameter[4] << 16; |
| 1267 | lba2 += towns_cd.parameter[3] << 8; |
| 1268 | lba2 += towns_cd.parameter[2]; |
| 1269 | towns_cd.lba_current = bcd_to_dec(lba1) - 200; |
| 1270 | towns_cd.lba_last = bcd_to_dec(lba2) - 200; |
| 1271 | |
| 1272 | logerror("CD: Mode 1 read from LBA next:%i last:%i\n",towns_cd.lba_current,towns_cd.lba_last); |
| 1273 | |
| 1274 | cdrom_read_data(mess_cd_get_cdrom_file(device),towns_cd.lba_current,towns_cd.buffer,CD_TRACK_MODE1); |
| 1275 | towns_cd.buffer_ptr = 0; |
| 1276 | towns_cd.status |= 0x03; // immediate status return? |
| 1277 | towns_cd.status |= 0x10; // DMA transfer begin |
| 1278 | towns_cd.status &= ~0x20; // not a software transfer |
| 1175 | 1279 | towns_cd.cmd_status_ptr = 0; |
| 1176 | | towns_cd.cmd_status[0] = 0x10; |
| 1280 | towns_cd.extra_status = 2; |
| 1281 | towns_cd.cmd_status[0] = 0x00; |
| 1177 | 1282 | towns_cd.cmd_status[1] = 0x00; |
| 1178 | 1283 | towns_cd.cmd_status[2] = 0x00; |
| 1179 | 1284 | towns_cd.cmd_status[3] = 0x00; |
| 1285 | if(towns_cd.dma_irq_enable != 0) |
| 1286 | { |
| 1287 | towns_cd.status |= 0x40; // IRQ from end of DMA |
| 1288 | pic8259_set_irq_line(devtag_get_device(device->machine,"pic8259_slave"),1,0); |
| 1289 | } |
| 1290 | |
| 1291 | // start read timer - assume single-speed currently |
| 1292 | //timer_adjust_oneshot(towns_cd.read_timer,ATTOTIME_IN_HZ(300000),1); |
| 1180 | 1293 | } |
| 1181 | 1294 | |
| 1295 | static void towns_cdrom_execute_command(const device_config* device) |
| 1296 | { |
| 1297 | // For now, we'll return status codes that hopefully tell the system |
| 1298 | // that there is no CD-ROM in the drive. |
| 1299 | |
| 1300 | if(mess_cd_get_cdrom_file(device) == NULL) |
| 1301 | { // No CD in drive |
| 1302 | if(towns_cd.command & 0x20) |
| 1303 | { |
| 1304 | towns_cd.status |= 0x02; // status read request |
| 1305 | towns_cd.cmd_status_ptr = 0; |
| 1306 | towns_cd.extra_status = 0; |
| 1307 | towns_cd.cmd_status[0] = 0x10; |
| 1308 | towns_cd.cmd_status[1] = 0x00; |
| 1309 | towns_cd.cmd_status[2] = 0x00; |
| 1310 | towns_cd.cmd_status[3] = 0x00; |
| 1311 | } |
| 1312 | } |
| 1313 | else |
| 1314 | { |
| 1315 | switch(towns_cd.command & 0x9f) |
| 1316 | { |
| 1317 | case 0x00: // Seek |
| 1318 | if(towns_cd.command & 0x20) |
| 1319 | { |
| 1320 | towns_cd.status |= 0x02; // status read request |
| 1321 | towns_cd.cmd_status_ptr = 0; |
| 1322 | towns_cd.extra_status = 1; |
| 1323 | towns_cd.cmd_status[0] = 0x00; |
| 1324 | towns_cd.cmd_status[1] = 0x00; |
| 1325 | towns_cd.cmd_status[2] = 0x00; |
| 1326 | towns_cd.cmd_status[3] = 0x00; |
| 1327 | if(towns_cd.command & 0x40) |
| 1328 | towns_cd.status |= 0x80; // IRQ from MPU |
| 1329 | } |
| 1330 | logerror("CD: Command 0x00: SEEK\n"); |
| 1331 | break; |
| 1332 | case 0x02: // Read (MODE1) |
| 1333 | logerror("CD: Command 0x02: READ MODE1\n"); |
| 1334 | towns_cdrom_read(device); |
| 1335 | break; |
| 1336 | case 0x05: // Read TOC |
| 1337 | logerror("CD: Command 0x05: READ TOC\n"); |
| 1338 | break; |
| 1339 | case 0x80: // set state |
| 1340 | if(towns_cd.command & 0x20) |
| 1341 | { |
| 1342 | towns_cd.status |= 0x02; // status read request |
| 1343 | towns_cd.cmd_status_ptr = 0; |
| 1344 | towns_cd.extra_status = 0; |
| 1345 | towns_cd.cmd_status[0] = 0x00; |
| 1346 | towns_cd.cmd_status[1] = 0x00; |
| 1347 | towns_cd.cmd_status[2] = 0x00; |
| 1348 | towns_cd.cmd_status[3] = 0x00; |
| 1349 | if(towns_cd.command & 0x40) |
| 1350 | towns_cd.status |= 0x80; // IRQ from MPU |
| 1351 | } |
| 1352 | logerror("CD: Command 0x80: set state\n"); |
| 1353 | break; |
| 1354 | case 0x81: // set state (CDDASET) |
| 1355 | if(towns_cd.command & 0x20) |
| 1356 | { |
| 1357 | towns_cd.status |= 0x02; // status read request |
| 1358 | towns_cd.cmd_status_ptr = 0; |
| 1359 | towns_cd.extra_status = 0; |
| 1360 | towns_cd.cmd_status[0] = 0x00; |
| 1361 | towns_cd.cmd_status[1] = 0x00; |
| 1362 | towns_cd.cmd_status[2] = 0x00; |
| 1363 | towns_cd.cmd_status[3] = 0x00; |
| 1364 | if(towns_cd.command & 0x40) |
| 1365 | towns_cd.status |= 0x80; // IRQ from MPU |
| 1366 | } |
| 1367 | logerror("CD: Command 0x81: set state (CDDASET)\n"); |
| 1368 | break; |
| 1369 | default: |
| 1370 | towns_cd.status |= 0x02; // status read request |
| 1371 | towns_cd.cmd_status_ptr = 0; |
| 1372 | towns_cd.extra_status = 0; |
| 1373 | towns_cd.cmd_status[0] = 0x10; |
| 1374 | towns_cd.cmd_status[1] = 0x00; |
| 1375 | towns_cd.cmd_status[2] = 0x00; |
| 1376 | towns_cd.cmd_status[3] = 0x00; |
| 1377 | logerror("CD: Unknown or unimplemented command %02x\n",towns_cd.command); |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | static UINT16 towns_cdrom_dma_r(running_machine* machine) |
| 1383 | { |
| 1384 | if(towns_cd.buffer_ptr >= 2048) |
| 1385 | return 0x00; |
| 1386 | return towns_cd.buffer[towns_cd.buffer_ptr++]; |
| 1387 | } |
| 1388 | |
| 1182 | 1389 | static READ8_HANDLER(towns_cdrom_r) |
| 1183 | 1390 | { |
| 1184 | | logerror("CD: read port %02x\n",offset*2); |
| 1185 | 1391 | switch(offset) |
| 1186 | 1392 | { |
| 1187 | 1393 | case 0x00: // status |
| 1394 | //logerror("CD: status read, returning %02x\n",towns_cd.status); |
| 1188 | 1395 | return towns_cd.status; |
| 1189 | 1396 | case 0x01: // command status |
| 1190 | 1397 | if(towns_cd.cmd_status_ptr > 3) |
| 1191 | 1398 | { |
| 1192 | 1399 | towns_cd.cmd_status_ptr = 0; |
| 1193 | 1400 | towns_cd.status &= ~0x02; |
| 1401 | // check for more status bytes |
| 1402 | if(towns_cd.extra_status != 0) |
| 1403 | { |
| 1404 | towns_cd.status |= 0x02; |
| 1405 | switch(towns_cd.command & 0x9f) |
| 1406 | { |
| 1407 | case 0x00: // seek |
| 1408 | towns_cd.cmd_status[0] = 0x04; |
| 1409 | towns_cd.cmd_status[1] = 0x00; |
| 1410 | towns_cd.cmd_status[2] = 0x00; |
| 1411 | towns_cd.cmd_status[3] = 0x00; |
| 1412 | towns_cd.extra_status = 0; |
| 1413 | break; |
| 1414 | case 0x02: // read |
| 1415 | if(towns_cd.extra_status == 2) |
| 1416 | { |
| 1417 | towns_cd.cmd_status[0] = 0x22; |
| 1418 | } |
| 1419 | else |
| 1420 | towns_cd.cmd_status[0] = 0x06; |
| 1421 | towns_cd.cmd_status[1] = 0x00; |
| 1422 | towns_cd.cmd_status[2] = 0x00; |
| 1423 | towns_cd.cmd_status[3] = 0x00; |
| 1424 | towns_cd.extra_status = 0; |
| 1425 | break; |
| 1426 | case 0x05: // read toc |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1194 | 1430 | } |
| 1431 | logerror("CD: reading command status port, returning %02x\n",towns_cd.cmd_status[towns_cd.cmd_status_ptr]); |
| 1195 | 1432 | return towns_cd.cmd_status[towns_cd.cmd_status_ptr++]; |
| 1196 | 1433 | default: |
| 1197 | 1434 | return 0x00; |
| r6452 | r6453 | |
| 1204 | 1441 | switch(offset) |
| 1205 | 1442 | { |
| 1206 | 1443 | case 0x00: // status |
| 1444 | if(data & 0x80) |
| 1445 | towns_cd.status &= ~0x80; |
| 1446 | if(data & 0x40) |
| 1447 | towns_cd.status &= ~0x40; |
| 1448 | if(data & 0x04) |
| 1449 | logerror("CD: sub MPU reset\n"); |
| 1450 | towns_cd.mpu_irq_enable = data & 0x02; |
| 1451 | towns_cd.dma_irq_enable = data & 0x01; |
| 1207 | 1452 | logerror("CD: status write %02x\n",data); |
| 1208 | 1453 | break; |
| 1209 | 1454 | case 0x01: // command |
| 1210 | 1455 | towns_cd.command = data; |
| 1211 | | towns_cdrom_execute_command(); |
| 1456 | towns_cdrom_execute_command(devtag_get_device(space->machine,"cdrom")); |
| 1212 | 1457 | logerror("CD: command %02x sent\n",data); |
| 1213 | 1458 | logerror("CD: parameters: %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 1214 | | towns_cd.parameter[0],towns_cd.parameter[1],towns_cd.parameter[2], |
| 1215 | | towns_cd.parameter[3],towns_cd.parameter[4],towns_cd.parameter[5], |
| 1216 | | towns_cd.parameter[6],towns_cd.parameter[7]); |
| 1459 | towns_cd.parameter[7],towns_cd.parameter[6],towns_cd.parameter[5], |
| 1460 | towns_cd.parameter[4],towns_cd.parameter[3],towns_cd.parameter[2], |
| 1461 | towns_cd.parameter[1],towns_cd.parameter[0]); |
| 1217 | 1462 | break; |
| 1218 | 1463 | case 0x02: // parameter |
| 1219 | | for(x=0;x<7;x++) |
| 1220 | | towns_cd.parameter[x+1] = towns_cd.parameter[x]; |
| 1464 | for(x=7;x>0;x--) |
| 1465 | towns_cd.parameter[x] = towns_cd.parameter[x-1]; |
| 1221 | 1466 | towns_cd.parameter[0] = data; |
| 1222 | 1467 | logerror("CD: parameter %02x added\n",data); |
| 1223 | 1468 | break; |
| 1469 | case 0x03: |
| 1470 | // TODO: software transfer mode (bit 3) |
| 1471 | if(data & 0x10) |
| 1472 | { |
| 1473 | towns_cd.status |= 0x10; // DMA transfer begin |
| 1474 | towns_cd.status &= ~0x20; // not a software transfer |
| 1475 | towns_cd.buffer_ptr = 0; |
| 1476 | timer_adjust_oneshot(towns_cd.read_timer,ATTOTIME_IN_HZ(300000),1); |
| 1477 | } |
| 1478 | break; |
| 1224 | 1479 | default: |
| 1225 | | logerror("CD: write port %02x\n",offset*2); |
| 1480 | logerror("CD: write %02x to port %02x\n",data,offset*2); |
| 1226 | 1481 | } |
| 1227 | 1482 | } |
| 1228 | 1483 | |
| r6452 | r6453 | |
| 1703 | 1958 | towns_rtc_timer = timer_alloc(machine,rtc_second,NULL); |
| 1704 | 1959 | towns_kb_timer = timer_alloc(machine,poll_keyboard,NULL); |
| 1705 | 1960 | |
| 1961 | // CD-ROM init |
| 1962 | towns_cd.read_timer = timer_alloc(machine,towns_cdrom_read_byte,(void*)devtag_get_device(machine,"dma_1")); |
| 1963 | |
| 1706 | 1964 | cpu_set_irq_callback(cputag_get_cpu(machine,"maincpu"), towns_irq_callback); |
| 1707 | 1965 | } |
| 1708 | 1966 | |
| r6452 | r6453 | |
| 1899 | 2157 | { |
| 1900 | 2158 | "maincpu", |
| 1901 | 2159 | 1000000, |
| 1902 | | { towns_fdc_dma_r, 0, 0, 0 }, |
| 2160 | { towns_fdc_dma_r, 0, 0, towns_cdrom_dma_r }, |
| 1903 | 2161 | { towns_fdc_dma_w, 0, 0, 0 } |
| 1904 | 2162 | }; |
| 1905 | 2163 | |
| r6452 | r6453 | |
| 1957 | 2215 | MDRV_SOUND_ADD("pcm", RF5C68, 2150000) // actual clock speed unknown |
| 1958 | 2216 | // MDRV_SOUND_CONFIG(rf5c68_interface) |
| 1959 | 2217 | MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50) |
| 2218 | MDRV_SOUND_ADD("cdda",CDDA,0) |
| 2219 | MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50) |
| 1960 | 2220 | |
| 1961 | 2221 | MDRV_PIT8253_ADD("pit",towns_pit8253_config) |
| 1962 | 2222 | |
| r6452 | r6453 | |
| 1966 | 2226 | |
| 1967 | 2227 | MDRV_MB8877_ADD("fdc",towns_mb8877a_interface) |
| 1968 | 2228 | MDRV_FLOPPY_4_DRIVES_ADD(towns_floppy_config) |
| 2229 | |
| 2230 | MDRV_CDROM_ADD("cdrom") |
| 1969 | 2231 | |
| 1970 | 2232 | MDRV_UPD71071_ADD("dma_1",towns_dma_config) |
| 1971 | 2233 | MDRV_UPD71071_ADD("dma_2",towns_dma_config) |