summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/cpu/arcompact/arcompact_helper.ipp
blob: 635d142066b212318ed6f946f7574103f238bd8d (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// license:BSD-3-Clause
// copyright-holders:David Haywood
#ifndef MAME_CPU_ARCOMPACT_ARCOMPACT_HELPER_IPP
#define MAME_CPU_ARCOMPACT_ARCOMPACT_HELPER_IPP

#pragma once

#include "arcompact.h"


inline bool arcompact_device::check_condition(uint8_t condition)
{
	switch (condition & 0x1f)
	{
	case 0x00: return condition_AL();
	case 0x01: return condition_EQ();
	case 0x02: return condition_NE();
	case 0x03: return condition_PL();
	case 0x04: return condition_MI();
	case 0x05: return condition_CS();
	case 0x06: return condition_HS();
	case 0x07: return condition_VS();
	case 0x08: return condition_VC();
	case 0x09: return condition_GT();
	case 0x0a: return condition_GE();
	case 0x0b: return condition_LT();
	case 0x0c: return condition_LE();
	case 0x0d: return condition_HI();
	case 0x0e: return condition_LS();
	case 0x0f: return condition_PNZ();

	default: fatalerror("unhandled condition check %02x", condition & 0x1f); return false;
	}
	return false;
}


inline void arcompact_device::do_flags_overflow(uint32_t result, uint32_t b, uint32_t c)
{
	if ((b & 0x80000000) == (c & 0x80000000))
	{
		if ((result & 0x80000000) != (b & 0x80000000))
		{
			status32_set_v();
		}
		else
		{
			status32_clear_v();
		}
	}
}

inline void arcompact_device::do_flags_add(uint32_t result, uint32_t b, uint32_t c)
{
	do_flags_nz(result);
	do_flags_overflow(result, b, c);

	if (result < b)
	{
		status32_set_c();
	}
	else
	{
		status32_clear_c();
	}
}

inline void arcompact_device::do_flags_sub(uint32_t result, uint32_t b, uint32_t c)
{
	do_flags_nz(result);
	do_flags_overflow(result, b, c);

	if (result > b)
	{
		status32_set_c();
	}
	else
	{
		status32_clear_c();
	}
}

inline void arcompact_device::do_flags_nz(uint32_t result)
{
	if (result & 0x80000000) { status32_set_n(); }
	else { status32_clear_n(); }
	if (result == 0x00000000) { status32_set_z(); }
	else { status32_clear_z(); }
}

inline void arcompact_device::arcompact_handle_ld_helper(uint32_t op, uint8_t areg, uint8_t breg, uint32_t s, uint8_t X, uint8_t Z, uint8_t a)
{
	// writeback / increment
	if (a == 1)
	{
		if (breg == REG_LIMM)
			fatalerror("illegal LD helper %08x (data size %d mode %d)", op, Z, a); // using the LIMM as the base register and an increment mode is illegal

		m_regs[breg] = m_regs[breg] + s;
	}

	uint32_t address = m_regs[breg];

	// address manipulation
	if (a == 0)
	{
		address = address + s;
	}
	else if (a == 3)
	{
		if (Z == 0)
		{
			address = address + (s << 2);
		}
		else if (Z == 2)
		{
			address = address + (s << 1);
		}
		else // Z == 1 and Z == 3 are invalid here
		{
			fatalerror("illegal LD helper %08x (data size %d mode %d)", op, Z, a);
		}
	}

	uint32_t readdata = 0;

	// read data
	if (Z == 0)
	{
		readdata = READ32(address);
		m_regs[areg] = readdata;
		if (X) // sign extend is not supported for long reads
			fatalerror("illegal LD helper %08x (data size %d mode %d with X)", op, Z, a);
	}
	else if (Z == 1)
	{
		readdata = READ8(address);

		if (X)
		{
			readdata = util::sext(readdata, 8);
			m_regs[areg] = readdata;
		}
		else
		{
			m_regs[areg] = readdata;
		}
	}
	else if (Z == 2)
	{
		readdata = READ16(address);

		if (X)
		{
			readdata = util::sext(readdata, 16);
			m_regs[areg] = readdata;
		}
		else
		{
			m_regs[areg] = readdata;
		}
	}
	else if (Z == 3)
	{ // Z == 3 is always illegal
		fatalerror("illegal LD helper %08x (data size %d mode %d)", op, Z, a);
	}

	// writeback / increment
	if (a == 2)
	{
		if (breg == REG_LIMM)
			fatalerror("illegal LD helper %08x (data size %d mode %d)", op, Z, a); // using the LIMM as the base register and an increment mode is illegal

		m_regs[breg] = m_regs[breg] + s;
	}
}

inline uint32_t arcompact_device::handleop32_general(uint32_t op, ophandler32 ophandler)
{
	switch ((op & 0x00c00000) >> 22)
	{
	case 0x00:
	{
		uint8_t breg = common32_get_breg(op);
		uint8_t creg = common32_get_creg(op);
		int size = check_limm(breg, creg);
		m_regs[common32_get_areg(op)] = ophandler(*this, m_regs[breg], m_regs[creg], common32_get_F(op));
		return m_pc + size;
	}

	case 0x01:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		m_regs[common32_get_areg(op)] = ophandler(*this, m_regs[breg], common32_get_u6(op), common32_get_F(op));
		return m_pc + size;
	}
	case 0x02:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		m_regs[breg] = ophandler(*this, m_regs[breg], common32_get_s12(op), common32_get_F(op));
		return m_pc + size;
	}
	case 0x03:
	{
		switch ((op & 0x00000020) >> 5)
		{
		case 0x00:
		{
			uint8_t breg = common32_get_breg(op);
			uint8_t creg = common32_get_creg(op);
			int size = check_limm(breg, creg);
			if (check_condition(common32_get_condition(op)))
				m_regs[breg] = ophandler(*this, m_regs[breg], m_regs[creg], common32_get_F(op));
			return m_pc + size;
		}
		case 0x01:
		{
			uint8_t breg = common32_get_breg(op);
			int size = check_limm(breg);
			if (check_condition(common32_get_condition(op)))
				m_regs[breg] = ophandler(*this, m_regs[breg], common32_get_u6(op), common32_get_F(op));
			return m_pc + size;
		}
		}
	}
	}
	return 0;
}

inline uint32_t arcompact_device::handleop32_general_MULx64(uint32_t op, ophandler32_mul ophandler)
{
	switch ((op & 0x00c00000) >> 22)
	{
	case 0x00:
	{
		uint8_t breg = common32_get_breg(op);
		uint8_t creg = common32_get_creg(op);
		int size = check_limm(breg, creg);
		ophandler(*this, m_regs[breg], m_regs[creg]);
		return m_pc + size;
	}

	case 0x01:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		ophandler(*this, m_regs[breg], common32_get_u6(op));
		return m_pc + size;
	}
	case 0x02:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		ophandler(*this, m_regs[breg], common32_get_s12(op));
		return m_pc + size;
	}
	case 0x03:
	{
		switch ((op & 0x00000020) >> 5)
		{
		case 0x00:
		{
			uint8_t breg = common32_get_breg(op);
			uint8_t creg = common32_get_creg(op);
			int size = check_limm(breg, creg);
			if (!check_condition(common32_get_condition(op)))
				return m_pc + size;
			ophandler(*this, m_regs[breg], m_regs[creg]);
			return m_pc + size;
		}

		case 0x01:
		{
			uint8_t breg = common32_get_breg(op);
			int size = check_limm(breg);
			if (!check_condition(common32_get_condition(op)))
				return m_pc + size;
			ophandler(*this, m_regs[breg], common32_get_u6(op));
			return m_pc + size;
		}
		}
	}
	}

	return 0;
}

inline uint32_t arcompact_device::handleop32_general_nowriteback_forced_flag(uint32_t op, ophandler32_ff ophandler)
{
	switch ((op & 0x00c00000) >> 22)
	{
	case 0x00:
	{
		uint8_t breg = common32_get_breg(op);
		uint8_t creg = common32_get_creg(op);
		int size = check_limm(breg, creg);
		ophandler(*this, m_regs[breg], m_regs[creg]);
		return m_pc + size;
	}
	case 0x01:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		ophandler(*this, m_regs[breg], common32_get_u6(op));
		return m_pc + size;
	}
	case 0x02:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		ophandler(*this, m_regs[breg], common32_get_s12(op));
		return m_pc + size;
	}
	case 0x03:
	{
		switch ((op & 0x00000020) >> 5)
		{
		case 0x00:
		{
			uint8_t breg = common32_get_breg(op);
			uint8_t creg = common32_get_creg(op);
			int size = check_limm(breg, creg);
			if (check_condition(common32_get_condition(op)))
				ophandler(*this, m_regs[breg], m_regs[creg]);
			return m_pc + size;
		}
		case 0x01:
		{
			uint8_t breg = common32_get_breg(op);
			int size = check_limm(breg);
			if (check_condition(common32_get_condition(op)))
				ophandler(*this, m_regs[breg], common32_get_u6(op));
			return m_pc + size;
		}
		}
	}
	}

	return 0;
}

inline uint32_t arcompact_device::handleop32_general_SOP_group(uint32_t op, ophandler32_sop ophandler)
{
	switch ((op & 0x00c00000) >> 22)
	{
	case 0x00:
	{
		uint8_t breg = common32_get_breg(op);
		uint8_t creg = common32_get_creg(op);
		int size = check_limm(breg, creg);
		m_regs[breg] = ophandler(*this, m_regs[creg], common32_get_F(op));
		return m_pc + size;
	}
	case 0x01:
	{
		uint8_t breg = common32_get_breg(op);
		int size = check_limm(breg);
		m_regs[breg] = ophandler(*this, common32_get_u6(op), common32_get_F(op));
		return m_pc + size;
	}
	case 0x02:
	case 0x03:
		fatalerror("SOP Group: illegal mode 02/03 specifying use of bits already assigned to opcode select: opcode %04x\n", op);
		return 0;
	}
	return 0;
}

#endif // MAME_CPU_ARCOMPACT_ARCOMPACT_HELPER_IPP