summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/machine/c64_cpm.c
blob: 197d9f3e4ffab79772c9dd9074a632a0277403c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**********************************************************************

    Commodore 64 CP/M cartridge emulation

    http://www.baltissen.org/newhtm/c64_cpm.htm

    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

**********************************************************************/

/*

    TODO:

    - Z80 clock speed

*/

#include "c64_cpm.h"



//**************************************************************************
//  MACROS/CONSTANTS
//**************************************************************************

#define Z80_TAG		"z80"



//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

const device_type C64_CPM = &device_creator<c64_cpm_cartridge_device>;


//-------------------------------------------------
//  ADDRESS_MAP( z80_mem )
//-------------------------------------------------

static ADDRESS_MAP_START( z80_mem, AS_PROGRAM, 8, c64_cpm_cartridge_device )
	AM_RANGE(0x0000, 0xffff) AM_READWRITE(dma_r, dma_w)
ADDRESS_MAP_END


//-------------------------------------------------
//  ADDRESS_MAP( z80_io )
//-------------------------------------------------

static ADDRESS_MAP_START( z80_io, AS_IO, 8, c64_cpm_cartridge_device )
	AM_RANGE(0x0000, 0xffff) AM_READWRITE(dma_r, dma_w)
ADDRESS_MAP_END


//-------------------------------------------------
//  MACHINE_CONFIG_FRAGMENT( c64_cpm )
//-------------------------------------------------

static MACHINE_CONFIG_FRAGMENT( c64_cpm )
	MCFG_CPU_ADD(Z80_TAG, Z80, 3000000)
	MCFG_CPU_PROGRAM_MAP(z80_mem)
	MCFG_CPU_IO_MAP(z80_io)
MACHINE_CONFIG_END


//-------------------------------------------------
//  machine_config_additions - device-specific
//  machine configurations
//-------------------------------------------------

machine_config_constructor c64_cpm_cartridge_device::device_mconfig_additions() const
{
	return MACHINE_CONFIG_NAME( c64_cpm );
}



//**************************************************************************
//  INLINE HELPERS
//**************************************************************************

//-------------------------------------------------
//  update_signals -
//-------------------------------------------------

inline void c64_cpm_cartridge_device::update_signals()
{
	if (m_enabled)
	{
        device_set_input_line(m_maincpu, INPUT_LINE_HALT, CLEAR_LINE);
        device_set_input_line(machine().firstcpu, INPUT_LINE_HALT, ASSERT_LINE);

        if (m_reset)
        {
        	m_maincpu->reset();
            m_maincpu->set_state_int(Z80_PC, 0);
        	m_reset = 0;
        }
	}
	else
	{
        device_set_input_line(m_maincpu, INPUT_LINE_HALT, ASSERT_LINE);
        device_set_input_line(machine().firstcpu, INPUT_LINE_HALT, CLEAR_LINE);
	}

/*
    // NOTE: the following is how it actually works once the Z80 core has been rewritten

    // C64 DMA
    m_slot->dma_w(m_enabled ? ASSERT_LINE : CLEAR_LINE);

    // Z80 BUSRQ
    int busrq = !(m_enabled & !m_ba) ? CLEAR_LINE : ASSERT_LINE;
    m_maincpu->set_input_line(Z80_INPUT_LINE_BUSRQ, busrq);

    // Z80 WAIT
    m_maincpu->set_input_line(Z80_INPUT_LINE_WAIT, m_enabled ? CLEAR_LINE : ASSERT_LINE);
*/
}



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  c64_cpm_cartridge_device - constructor
//-------------------------------------------------

c64_cpm_cartridge_device::c64_cpm_cartridge_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
	device_t(mconfig, C64_CPM, "C64 CP/M cartridge", tag, owner, clock),
	device_c64_expansion_card_interface(mconfig, *this),
	m_maincpu(*this, Z80_TAG),
	m_enabled(0),
	m_ba(1)
{
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void c64_cpm_cartridge_device::device_start()
{
	// state saving
	save_item(NAME(m_enabled));
	save_item(NAME(m_ba));
}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void c64_cpm_cartridge_device::device_reset()
{
	m_enabled = 0;
	m_reset = 1;

	update_signals();
}


//-------------------------------------------------
//  c64_cd_w - cartridge data write
//-------------------------------------------------

void c64_cpm_cartridge_device::c64_cd_w(address_space &space, offs_t offset, UINT8 data, int ba, int roml, int romh, int io1, int io2)
{
	if (!io1)
	{
		m_enabled = !BIT(data, 0);

		update_signals();
	}
}


//-------------------------------------------------
//  c64_game_r - GAME read
//-------------------------------------------------

int c64_cpm_cartridge_device::c64_game_r(offs_t offset, int ba, int rw, int hiram)
{
	if (m_ba != ba)
	{
		m_ba = ba;

		update_signals();
	}

	return 1;
}


//-------------------------------------------------
//  dma_r -
//-------------------------------------------------

READ8_MEMBER( c64_cpm_cartridge_device::dma_r )
{
	UINT8 data = 0xff;

	if (m_enabled)
	{
		offs_t addr = (offset + 0x1000) & 0xffff;

		data = m_slot->dma_cd_r(addr);
	}

	return data;
}


//-------------------------------------------------
//  dma_w -
//-------------------------------------------------

WRITE8_MEMBER( c64_cpm_cartridge_device::dma_w )
{
	if (m_enabled)
	{
		offs_t addr = (offset + 0x1000) & 0xffff;

		m_slot->dma_cd_w(addr, data);
	}
}