summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/68307.c
blob: ecb663f2c2a3dff5dbc3c9404b43cd0e454b5327 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* 68307 */

#include "68307.h"

static ADDRESS_MAP_START( m68307_internal_map, AS_PROGRAM, 16, m68307cpu_device )
	AM_RANGE(0x000000f0, 0x000000ff) AM_READWRITE(m68307_internal_base_r, m68307_internal_base_w)
ADDRESS_MAP_END


m68307cpu_device::m68307cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: m68000_device(mconfig, "MC68307", tag, owner, clock, M68307, 16,24, ADDRESS_MAP_NAME(m68307_internal_map), "mc68307", __FILE__)
{
	m68307SIM = 0;
	m68307MBUS = 0;
	m68307SERIAL = 0;
	m68307TIMER = 0;
	m68307_base = 0;
	m68307_scrhigh = 0;
	m68307_scrlow = 0;
	m68307_currentcs = 0;

	m_m68307_porta_r = 0;
	m_m68307_porta_w = 0;
	m_m68307_portb_r = 0;
	m_m68307_portb_w = 0;
}






void m68307cpu_device::device_reset()
{
	m68000_device::device_reset();

	if (m68307SIM) m68307SIM->reset();
	if (m68307MBUS) m68307MBUS->reset();
	if (m68307SERIAL) m68307SERIAL->reset();
	if (m68307TIMER) m68307TIMER->reset();

	m68307_base = 0xbfff;
	m68307_scrhigh = 0x0007;
	m68307_scrlow = 0xf010;

}


/* todo: is it possible to calculate the address map based on CS when they change
   and install handlers?  Going through this logic for every memory access is
   very slow */

int m68307_calc_cs(m68307cpu_device *m68k, offs_t address)
{
	m68307_sim* sim = m68k->m68307SIM;

	for (int i=0;i<4;i++)
	{
		int br,amask,bra;
		br = sim->m_br[i] & 1;
		amask = ((sim->m_or[i]&0x1ffc)<<11);
		bra = ((sim->m_br[i] & 0x1ffc)<<11);
		if ((br) && ((address & amask) == bra)) return i+1;
	}
	return 0;
}



UINT16 m68307cpu_device::simple_read_immediate_16_m68307(offs_t address)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	return m_direct->read_decrypted_word(address);
}

UINT8 m68307cpu_device::read_byte_m68307(offs_t address)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	return m_space->read_byte(address);
}

UINT16 m68307cpu_device::read_word_m68307(offs_t address)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	return m_space->read_word(address);
}

UINT32 m68307cpu_device::read_dword_m68307(offs_t address)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	return m_space->read_dword(address);
}

void m68307cpu_device::write_byte_m68307(offs_t address, UINT8 data)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	m_space->write_byte(address, data);
}

void m68307cpu_device::write_word_m68307(offs_t address, UINT16 data)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	m_space->write_word(address, data);
}

void m68307cpu_device::write_dword_m68307(offs_t address, UINT32 data)
{
//  m68307_currentcs = m68307_calc_cs(this, address);
	m_space->write_dword(address, data);
}




void m68307cpu_device::init16_m68307(address_space &space)
{
	m_space = &space;
	m_direct = &space.direct();
	opcode_xor = 0;

	readimm16 = m68k_readimm16_delegate(FUNC(m68307cpu_device::simple_read_immediate_16_m68307), this);
	read8 = m68k_read8_delegate(FUNC(m68307cpu_device::read_byte_m68307), this);
	read16 = m68k_read16_delegate(FUNC(m68307cpu_device::read_word_m68307), this);
	read32 = m68k_read32_delegate(FUNC(m68307cpu_device::read_dword_m68307), this);
	write8 = m68k_write8_delegate(FUNC(m68307cpu_device::write_byte_m68307), this);
	write16 = m68k_write16_delegate(FUNC(m68307cpu_device::write_word_m68307), this);
	write32 = m68k_write32_delegate(FUNC(m68307cpu_device::write_dword_m68307), this);
}



void m68307_set_port_callbacks(m68307cpu_device *device, m68307_porta_read_callback porta_r, m68307_porta_write_callback porta_w, m68307_portb_read_callback portb_r, m68307_portb_write_callback portb_w)
{
	device->m_m68307_porta_r = porta_r;
	device->m_m68307_porta_w = porta_w;
	device->m_m68307_portb_r = portb_r;
	device->m_m68307_portb_w = portb_w;
}

void m68307_set_duart68681(m68307cpu_device* cpudev, device_t* duart68681)
{
	if (cpudev->m68307SERIAL)
		cpudev->m68307SERIAL->m68307ser_set_duart68681(duart68681);
}




UINT16 m68307_get_cs(m68307cpu_device *device, offs_t address)
{
	device->m68307_currentcs = m68307_calc_cs(device, address);

	return device->m68307_currentcs;
}


/* 68307 specifics - MOVE */

void m68307_set_interrupt(device_t *device, int level, int vector)
{
	device->execute().set_input_line_and_vector(level, HOLD_LINE, vector);
}

void m68307_timer0_interrupt(m68307cpu_device *cpudev)
{
	int prioritylevel = (cpudev->m68307SIM->m_picr & 0x7000)>>12;
	int vector        = (cpudev->m68307SIM->m_pivr & 0x00f0) | 0xa;
	m68307_set_interrupt(cpudev, prioritylevel, vector);
}

void m68307_timer1_interrupt(m68307cpu_device *cpudev)
{
	int prioritylevel = (cpudev->m68307SIM->m_picr & 0x0700)>>8;
	int vector        = (cpudev->m68307SIM->m_pivr & 0x00f0) | 0xb;
	m68307_set_interrupt(cpudev, prioritylevel, vector);
}

void m68307_serial_interrupt(m68307cpu_device *cpudev, int vector)
{
	int prioritylevel = (cpudev->m68307SIM->m_picr & 0x0070)>>4;
	m68307_set_interrupt(cpudev, prioritylevel, vector);
}

void m68307_mbus_interrupt(m68307cpu_device *cpudev)
{
	int prioritylevel = (cpudev->m68307SIM->m_picr & 0x0007)>>0;
	int vector        = (cpudev->m68307SIM->m_pivr & 0x00f0) | 0xd;
	m68307_set_interrupt(cpudev, prioritylevel, vector);
}

void m68307_licr2_interrupt(m68307cpu_device *cpudev)
{
	int prioritylevel = (cpudev->m68307SIM->m_licr2 & 0x0007)>>0;
	int vector        = (cpudev->m68307SIM->m_pivr & 0x00f0) | 0x9;
	cpudev->m68307SIM->m_licr2 |= 0x8;


	m68307_set_interrupt(cpudev, prioritylevel, vector);
}

void m68307cpu_device::device_start()
{
	init_cpu_m68000();

	/* basic CS logic, timers, mbus, serial logic
	   set via remappable register
	*/

	init16_m68307(*program);

	m68307SIM    = new m68307_sim();
	m68307MBUS   = new m68307_mbus();
	m68307SERIAL = new m68307_serial();
	m68307TIMER  = new m68307_timer();

	m68307TIMER->init(this);

	m68307SIM->reset();
	m68307MBUS->reset();
	m68307SERIAL->reset();
	m68307TIMER->reset();

	internal = &this->space(AS_PROGRAM);
	m68307_base = 0xbfff;
	m68307_scrhigh = 0x0007;
	m68307_scrlow = 0xf010;

	m68307_set_port_callbacks(this, 0,0,0,0);
}



READ16_MEMBER( m68307cpu_device::m68307_internal_base_r )
{
	m68307cpu_device *m68k = this;

	int pc = space.device().safe_pc();
	logerror("%08x m68307_internal_base_r %08x, (%04x)\n", pc, offset*2,mem_mask);

	switch (offset<<1)
	{
		case 0x2: return m68k->m68307_base;
		case 0x4: return m68k->m68307_scrhigh;
		case 0x6: return m68k->m68307_scrlow;
	}

	logerror("(read was illegal?)\n");

	return 0x0000;
}

WRITE16_MEMBER( m68307cpu_device::m68307_internal_base_w )
{
	m68307cpu_device *m68k = this;

	int pc = space.device().safe_pc();
	logerror("%08x m68307_internal_base_w %08x, %04x (%04x)\n", pc, offset*2,data,mem_mask);
	int base = 0;
	//int mask = 0;

	switch (offset<<1)
	{
		case 0x2:
			/* remove old internal handler */
			base = (m68k->m68307_base & 0x0fff) << 12;
			//mask = (m68k->m68307_base & 0xe000) >> 13;
			//if ( m68k->m68307_base & 0x1000 ) mask |= 7;
			m68k->internal->unmap_readwrite(base+0x000, base+0x04f);
			m68k->internal->unmap_readwrite(base+0x100, base+0x11f);
			m68k->internal->unmap_readwrite(base+0x120, base+0x13f);
			m68k->internal->unmap_readwrite(base+0x140, base+0x149);

			/* store new base address */
			COMBINE_DATA(&m68k->m68307_base);

			/* install new internal handler */
			base = (m68k->m68307_base & 0x0fff) << 12;
			//mask = (m68k->m68307_base & 0xe000) >> 13;
			//if ( m68k->m68307_base & 0x1000 ) mask |= 7;
			m68k->internal->install_readwrite_handler(base + 0x000, base + 0x04f, read16_delegate(FUNC(m68307cpu_device::m68307_internal_sim_r),this),    write16_delegate(FUNC(m68307cpu_device::m68307_internal_sim_w),this));
			m68k->internal->install_readwrite_handler(base + 0x100, base + 0x11f, read8_delegate(FUNC(m68307cpu_device::m68307_internal_serial_r),this), write8_delegate(FUNC(m68307cpu_device::m68307_internal_serial_w),this), 0xffff);
			m68k->internal->install_readwrite_handler(base + 0x120, base + 0x13f, read16_delegate(FUNC(m68307cpu_device::m68307_internal_timer_r),this),  write16_delegate(FUNC(m68307cpu_device::m68307_internal_timer_w),this));
			m68k->internal->install_readwrite_handler(base + 0x140, base + 0x149, read8_delegate(FUNC(m68307cpu_device::m68307_internal_mbus_r),this),   write8_delegate(FUNC(m68307cpu_device::m68307_internal_mbus_w),this), 0xffff);


			break;

		case 0x4:
			COMBINE_DATA(&m68k->m68307_scrhigh);
			break;

		case 0x6:
			COMBINE_DATA(&m68k->m68307_scrlow);
			break;

		default:
			logerror("(write was illegal?)\n");
			break;
	}
}