mame/src/devices/machine/ncr5380.cpp
Vas Crabb 0f0d39ef81 Move static data out of devices into the device types. This is a significant change, so please pay attention.
The core changes are:
* Short name, full name and source file are no longer members of device_t, they are part of the device type
* MACHINE_COFIG_START no longer needs a driver class
* MACHINE_CONFIG_DERIVED_CLASS is no longer necessary
* Specify the state class you want in the GAME/COMP/CONS line
* The compiler will work out the base class where the driver init member is declared
* There is one static device type object per driver rather than one per machine configuration

Use DECLARE_DEVICE_TYPE or DECLARE_DEVICE_TYPE_NS to declare device type.
* DECLARE_DEVICE_TYPE forward-declares teh device type and class, and declares extern object finders.
* DECLARE_DEVICE_TYPE_NS is for devices classes in namespaces - it doesn't forward-declare the device type.

Use  DEFINE_DEVICE_TYPE or DEFINE_DEVICE_TYPE_NS to define device types.
* These macros declare storage for the static data, and instantiate the device type and device finder templates.

The rest of the changes are mostly just moving stuff out of headers that shouldn't be there, renaming stuff for consistency, and scoping stuff down where appropriate.

Things I've actually messed with substantially:
* More descriptive names for a lot of devices
* Untangled the fantasy sound from the driver state, which necessitates breaking up sound/flip writes
* Changed DECO BSMT2000 ready callback into a device delegate
* Untangled Microprose 3D noise from driver state
* Used object finders for CoCo multipak, KC85 D002, and Irem sound subdevices
* Started to get TI-99 stuff out of the TI-990 directory and arrange bus devices properly
* Started to break out common parts of Samsung ARM SoC devices
* Turned some of FM, SID, SCSP DSP, EPIC12 and Voodoo cores into something resmbling C++
* Tried to make Z180 table allocation/setup a bit safer
* Converted generic keyboard/terminal to not use WRITE8 - space/offset aren't relevant
* Dynamically allocate generic terminal buffer so derived devices (e.g. teleprinter) can specify size
* Imporved encapsulation of Z80DART channels
* Refactored the SPC7110 bit table generator loop to make it more readable
* Added wrappers for SNES PPU operations so members can be made protected
* Factored out some boilerplate for YM chips with PSG
* toaplan2 gfx
* stic/intv resolution
* Video System video
* Out Run/Y-board sprite alignment
* GIC video hookup
* Amstrad CPC ROM box members
* IQ151 ROM cart region
* MSX cart IRQ callback resolution time
* SMS passthrough control devices starting subslots

I've smoke-tested several drivers, but I've probably missed something.  Things I've missed will likely blow up spectacularly with failure to bind errors and the like.  Let me know if there's more subtle breakage (could have happened in FM or Voodoo).

And can everyone please, please try to keep stuff clean.  In particular, please stop polluting the global namespace.  Keep things out of headers that don't need to be there, and use things that can be scoped down rather than macros.
It feels like an uphill battle trying to get this stuff under control while more of it's added.
2017-05-14 21:44:11 +10:00

421 lines
9.5 KiB
C++

// license:BSD-3-Clause
// copyright-holders:R. Belmont
/*
* ncr5380.c
*
* NCR 5380 SCSI controller, as seen in many 680x0 Macs,
* official Apple add-on cards for the Apple II series,
* and probably some PC and Amiga cards as well.
*
* Emulation by R. Belmont.
*
* References:
* Zilog 5380 manual
* "Inside Macintosh: Devices" (formerly online at http://www.manolium.org/dev/techsupport/insidemac/Devices/Devices-2.html )
*
* NOTES:
* This implementation is tied closely to the drivers found in the Mac Plus ROM and the routines in Mac
* System 6 and 7 that it patches out the ROM traps with. While attempts have been made to
* have the behavior work according to the manual and not the specific Apple driver code,
* there are almost certainly areas where that is true.
*
*/
#include "emu.h"
#include "ncr5380.h"
//#define VERBOSE 1
#include "logmacro.h"
static const char *const rnames[] =
{
"Current data",
"Initiator cmd",
"Mode",
"Target cmd",
"Bus status",
"Bus and status",
"Input data",
"Reset parity"
};
static const char *const wnames[] =
{
"Output data",
"Initiator cmd",
"Mode",
"Target cmd",
"Select enable",
"Start DMA",
"DMA target",
"DMA initiator rec"
};
// get the length of a SCSI command based on it's command byte type
static int get_cmd_len(int cbyte)
{
int group;
group = (cbyte>>5) & 7;
if (group == 0) return 6;
if (group == 1 || group == 2) return 10;
if (group == 5) return 12;
fatalerror("NCR5380: Unknown SCSI command group %d\n", group);
// never executed
//return 6;
}
//**************************************************************************
// LIVE DEVICE
//**************************************************************************
DEFINE_DEVICE_TYPE(NCR5380, ncr5380_device, "ncr5380", "NCR 5380 SCSI")
//-------------------------------------------------
// ncr5380_device - constructor/destructor
//-------------------------------------------------
ncr5380_device::ncr5380_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
legacy_scsi_host_adapter(mconfig, NCR5380, tag, owner, clock),
m_irq_cb(*this)
{
}
//-------------------------------------------------
// device_start - device-specific startup
//-------------------------------------------------
void ncr5380_device::device_start()
{
legacy_scsi_host_adapter::device_start();
memset(m_5380_Registers, 0, sizeof(m_5380_Registers));
memset(m_5380_Data, 0, sizeof(m_5380_Data));
m_next_req_flag = 0;
m_irq_cb.resolve_safe();
save_item(NAME(m_5380_Registers));
save_item(NAME(m_5380_Command));
save_item(NAME(m_5380_Data));
save_item(NAME(m_last_id));
save_item(NAME(m_cmd_ptr));
save_item(NAME(m_d_ptr));
save_item(NAME(m_d_limit));
save_item(NAME(m_next_req_flag));
}
//-------------------------------------------------
// device_reset - device-specific reset
//-------------------------------------------------
void ncr5380_device::device_reset()
{
memset(m_5380_Registers, 0, sizeof(m_5380_Registers));
memset(m_5380_Data, 0, sizeof(m_5380_Data));
m_next_req_flag = 0;
m_cmd_ptr = 0;
m_d_ptr = 0;
m_d_limit = 0;
m_last_id = 0;
}
//-------------------------------------------------
// device_stop - device-specific stop/shutdown
//-------------------------------------------------
void ncr5380_device::device_stop()
{
}
//-------------------------------------------------
// Public API
//-------------------------------------------------
uint8_t ncr5380_device::ncr5380_read_reg(uint32_t offset)
{
int reg = offset & 7;
uint8_t rv;
switch( reg )
{
case R5380_CURDATA:
case R5380_INPUTDATA:
rv = m_5380_Registers[reg];
// if we're in the data transfer phase or DMA, readback device data instead
if (((m_5380_Registers[R5380_BUSSTATUS] & 0x1c) == 0x04) || (m_5380_Registers[R5380_BUSSTATUS] & 0x40))
{
rv = m_5380_Data[m_d_ptr];
// if the limit's less than 512, only read "limit" bytes
if (m_d_limit < 512)
{
if (m_d_ptr < (m_d_limit-1))
{
m_d_ptr++;
}
else
{
m_next_req_flag = 1;
}
}
else
{
if (m_d_ptr < 511)
{
m_d_ptr++;
}
else
{
m_d_limit -= 512;
m_d_ptr = 0;
m_next_req_flag = 1;
// don't issue a "false" read
if (m_d_limit > 0)
{
read_data(m_5380_Data, (m_d_limit < 512) ? m_d_limit : 512);
}
else
{
// if this is DMA, signal DMA end
if (m_5380_Registers[R5380_BUSSTATUS] & 0x40)
{
m_5380_Registers[R5380_BUSSTATUS] |= 0x80;
}
// drop /REQ
m_5380_Registers[R5380_BUSSTATUS] &= ~0x20;
// clear phase match
m_5380_Registers[R5380_BUSANDSTAT] &= ~0x08;
}
}
}
}
break;
default:
rv = m_5380_Registers[reg];
// temporarily drop /REQ
if ((reg == R5380_BUSSTATUS) && (m_next_req_flag))
{
rv &= ~0x20;
m_next_req_flag = 0;
}
break;
}
LOG("%s NCR5380: read %s (reg %d) = %02x\n", machine().describe_context(), rnames[reg], reg, rv);
return rv;
}
void ncr5380_device::ncr5380_write_reg(uint32_t offset, uint8_t data)
{
int reg = offset & 7;
LOG("%s NCR5380: %02x to %s (reg %d)\n", machine().describe_context(), data, wnames[reg], reg);
switch( reg )
{
case R5380_OUTDATA:
// if we're in the command phase, collect the command bytes
if ((m_5380_Registers[R5380_BUSSTATUS] & 0x1c) == 0x08)
{
m_5380_Command[m_cmd_ptr++] = data;
}
// if we're in the select phase, this is the target id
if (m_5380_Registers[R5380_INICOMMAND] == 0x04)
{
data &= 0x7f; // clear the high bit
if (data == 0x40)
{
m_last_id = 6;
}
else if (data == 0x20)
{
m_last_id = 5;
}
else if (data == 0x10)
{
m_last_id = 4;
}
else if (data == 0x08)
{
m_last_id = 3;
}
else if (data == 0x04)
{
m_last_id = 2;
}
else if (data == 0x02)
{
m_last_id = 1;
}
else if (data == 0x01)
{
m_last_id = 0;
}
}
// if this is a write, accumulate accordingly
if (((m_5380_Registers[R5380_BUSSTATUS] & 0x1c) == 0x00) && (m_5380_Registers[R5380_INICOMMAND] == 1))
{
m_5380_Data[m_d_ptr] = data;
// if we've hit a sector, flush
if (m_d_ptr == 511)
{
write_data(&m_5380_Data[0], 512);
m_d_limit -= 512;
m_d_ptr = 0;
// no more data? set DMA END flag
if (m_d_limit <= 0)
{
m_5380_Registers[R5380_BUSANDSTAT] = 0xc8;
}
}
else
{
m_d_ptr++;
}
// make sure we don't upset the status readback
data = 0;
}
break;
case R5380_INICOMMAND:
if (data == 0) // dropping the bus
{
// make sure it's not busy
m_5380_Registers[R5380_BUSSTATUS] &= ~0x40;
// are we in the command phase?
if ((m_5380_Registers[R5380_BUSSTATUS] & 0x1c) == 0x08)
{
// is the current command complete?
if (get_cmd_len(m_5380_Command[0]) == m_cmd_ptr)
{
LOG("%s NCR5380: Command (to ID %d): %x %x %x %x %x %x %x %x %x %x\n", machine().describe_context(), m_last_id, m_5380_Command[0], m_5380_Command[1], m_5380_Command[2], m_5380_Command[3], m_5380_Command[4], m_5380_Command[5], m_5380_Command[6], m_5380_Command[7], m_5380_Command[8], m_5380_Command[9]);
send_command(&m_5380_Command[0], 16);
m_d_limit = get_length();
LOG("NCR5380: Command returned %d bytes\n", m_d_limit);
m_d_ptr = 0;
// is data available?
if (m_d_limit > 0)
{
// make sure for transfers under 512 bytes that we always pad with a zero
if (m_d_limit < 512)
{
m_5380_Data[m_d_limit] = 0;
}
// read back the amount available, or 512 bytes, whichever is smaller
read_data(m_5380_Data, (m_d_limit < 512) ? m_d_limit : 512);
// raise REQ to indicate data is available
m_5380_Registers[R5380_BUSSTATUS] |= 0x20;
}
}
}
}
if (data == 5) // want the bus?
{
// if the device exists, make the bus busy.
// otherwise don't.
if (select(m_last_id))
{
LOG("NCR5380: Giving the bus for ID %d\n", m_last_id);
m_5380_Registers[R5380_BUSSTATUS] |= 0x40;
}
else
{
LOG("NCR5380: Rejecting the bus for ID %d\n", m_last_id);
m_5380_Registers[R5380_BUSSTATUS] &= ~0x40;
}
}
if (data == 1) // data bus (prelude to command?)
{
// raise REQ
m_5380_Registers[R5380_BUSSTATUS] |= 0x20;
}
if (data & 0x10) // ACK drops REQ
{
// drop REQ
m_5380_Registers[R5380_BUSSTATUS] &= ~0x20;
}
break;
case R5380_MODE:
if (data == 2) // DMA
{
// put us in DMA mode
m_5380_Registers[R5380_BUSANDSTAT] |= 0x40;
}
if (data == 1) // arbitrate?
{
m_5380_Registers[R5380_INICOMMAND] |= 0x40; // set arbitration in progress
m_5380_Registers[R5380_INICOMMAND] &= ~0x20; // clear "lost arbitration"
}
if (data == 0)
{
// drop DMA mode
m_5380_Registers[R5380_BUSANDSTAT] &= ~0x40;
}
break;
case R5380_TARGETCMD:
// sync the bus phase with what was just written
m_5380_Registers[R5380_BUSSTATUS] &= ~0x1c;
m_5380_Registers[R5380_BUSSTATUS] |= (data & 7)<<2;
// and set the "phase match" flag
m_5380_Registers[R5380_BUSANDSTAT] |= 0x08;
// and set /REQ
m_5380_Registers[R5380_BUSSTATUS] |= 0x20;
// if we're entering the command phase, start accumulating the data
if ((m_5380_Registers[R5380_BUSSTATUS] & 0x1c) == 0x08)
{
m_cmd_ptr = 0;
}
break;
default:
break;
}
m_5380_Registers[reg] = data;
// note: busandstat overlaps startdma, so we need to do this here!
if (reg == R5380_STARTDMA)
{
m_5380_Registers[R5380_BUSANDSTAT] = 0x48;
}
}