summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/cpu/dsp16/dsp16core.ipp
blob: ec2bc143edc74eaba31234a3ed8c40eef110906c (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
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    WE|AT&T DSP16 series core state

***************************************************************************/
#ifndef MAME_CPU_DSP16_DSP16CORE_IPP
#define MAME_CPU_DSP16_DSP16CORE_IPP

#pragma once

#include "dsp16core.h"


/***********************************************************************
    YAAU helpers
***********************************************************************/

inline u16 &dsp16_device_base::core_state::yaau_postmodify_r(u16 op)
{
	u16 &r(op_yaau_r(op));
	switch (op & 0x0003U)
	{
	case 0x0: // *rN
		break;
	case 0x1: // *rN++
		r = (yaau_re && (yaau_re == r)) ? yaau_rb : (r + 1);
		break;
	case 0x2: // *rN--
		--r;
		break;
	case 0x3: // *rN++j
		r += yaau_j;
		break;
	}
	return r &= yaau_mask;
}

/***********************************************************************
    DAU helpers
***********************************************************************/

inline u64 dsp16_device_base::core_state::dau_f1(u16 op)
{
	s64 const &s(op_dau_as(op));
	s64 d(0);
	switch (op_f1(op))
	{
	case 0x0: // aD = p ; p = x*y
		d = dau_get_p_aligned();
		dau_p = dau_x * (dau_y >> 16);
		break;
	case 0x1: // aD = aS + p ; p = x*y
		d = s + dau_get_p_aligned();
		dau_p = dau_x * (dau_y >> 16);
		break;
	case 0x2: // p = x*y
		dau_p = dau_x * (dau_y >> 16);
		return op_dau_ad(op);
	case 0x3: // aD = aS - p ; p = x*y
		d = s - dau_get_p_aligned();
		dau_p = dau_x * (dau_y >> 16);
		break;
	case 0x4: // aD = p
		d = dau_get_p_aligned();
		break;
	case 0x5: // aD = aS + p
		d = s + dau_get_p_aligned();
		break;
	case 0x6: // NOP
		return op_dau_ad(op);
	case 0x7: // aD = aS - p
		d = s - dau_get_p_aligned();
		break;
	case 0x8: // aD = aS | y
		d = s | dau_y;
		break;
	case 0x9: // aD = aS ^ y
		d = s ^ dau_y;
		break;
	case 0xa: // aS & y
		dau_set_psw_flags(s & dau_y);
		return op_dau_ad(op);
	case 0xb: // aS - y
		dau_set_psw_flags(s - dau_y);
		return op_dau_ad(op);
	case 0xc: // aD = y
		d = dau_y;
		break;
	case 0xd: // aD = aS + y
		d = s + dau_y;
		break;
	case 0xe: // aD = aS & y
		d = s & dau_y;
		break;
	case 0xf: // aD = aS - y
		d = s - dau_y;
		break;
	}
	return dau_set_psw_flags(d);
}

inline void dsp16_device_base::core_state::dau_f2(u16 op)
{
	s64 const &s(op_dau_as(op));
	s64 &d(op_dau_ad(op));
	switch (op_f2(op))
	{
	case 0x0: // aD = aS >> 1
		d = s >> 1;
		break;
	case 0x1: // aD = aS << 1
		d = s32(u32(u64(s)) << 1);
		break;
	case 0x2: // aD = aS >> 4
		d = s >> 4;
		break;
	case 0x3: // aD = aS << 4
		d = s32(u32(u64(s)) << 4);
		break;
	case 0x4: // aD = aS >> 8
		d = s >> 8;
		break;
	case 0x5: // aD = aS << 8
		d = s32(u32(u64(s)) << 8);
		break;
	case 0x6: // aD = aS >> 16
		d = s >> 16;
		break;
	case 0x7: // aD = aS << 16
		d = s32(u32(u64(s)) << 16);
		break;
	case 0x8: // aD = p
		d = dau_get_p_aligned();
		break;
	case 0x9: // aDh = aSh + 1
		d = s64(s32(u32(u64(s)) & ~((u32(1) << 16) - 1))) + (s32(1) << 16);
		if (BIT(dau_auc, op_s(op) + 4))
			d |= op_dau_ad(op) & ((s64(1) << 16) - 1);
		break;
	case 0xa: // Reserved
		throw emu_fatalerror("DSP16: reserved F2 value %01X (PC = %04X)\n", op_f2(op), xaau_pc/*FIXME: st_pcbase*/);
	case 0xb: // aD = rnd(aS)
		// FIXME: behaviour is not clear
		// p 3-13: "Round upper 20 bits of accumulator."
		// p 3-14: "The contents of the source accumulator, aS, are rounded to 16 bits, and the sign-extended result is placed in aD[35 - 16] with zeroes in aD[15 - 0]."
		// It presumably rounds to nearest, but does it yield a 16-bit or 20-bit result, and what does it do about overflow?
		d = (s + ((s32(1) << 15) - ((0 > s) ? 1 : 0))) & ~((u64(1) << 16) - 1);
		break;
	case 0xc: // aD = y
		d = dau_y;
		break;
	case 0xd: // aD = aS + 1
		d = s + 1;
		break;
	case 0xe: // aD = aS
		d = s;
		break;
	case 0xf: // aD = -aS
		// FIXME: does this detect negation of largest negative number as overflow?
		d = -s;
		break;
	}
	d = dau_set_psw_flags(d);
}

inline s64 &dsp16_device_base::core_state::dau_set_atx(u16 op, s16 value)
{
	s64 &at(op_dau_at(op));
	if (op_x(op))
	{
		bool const clear(!BIT(dau_psw, 4 + op_d(~op)));
		return at = s32((u32(u16(value)) << 16) | u32(clear ? u16(0) : u16(u64(at))));
	}
	else
	{
		return at = (at & ~((s64(1) << 16) - 1)) | u64(u16(value));
	}
}

inline s64 &dsp16_device_base::core_state::dau_set_at(u16 op, s16 value)
{
	s64 &at(op_dau_at(op));
	bool const clear(!BIT(dau_psw, 4 + op_d(~op)));
	return at = s32((u32(u16(value)) << 16) | u32(clear ? u16(0) : u16(u64(at))));
}

/***********************************************************************
    internal helpers
***********************************************************************/

inline u64 dsp16_device_base::core_state::dau_get_p_aligned() const
{
	// TODO: manual is contradictory
	// p 2-10: "Bits 1 and 2 of the accumulator are not changed by the load of the accumulator with the data in p, since 00 is added to or copied into these accumulator bits as indicated in Figure 2-8."
	// I'm reading this as copying p to aD clears the low two bits, but adding it leaves them unchanged.
	switch (dau_auc_align())
	{
	case 0x0:
		return dau_p;
	case 0x1:
		return dau_p >> 2;
	case 0x2:
		return u64(dau_p) << 2;
	case 0x3:
	default:
		throw emu_fatalerror("DSP16: reserved ALIGN value %01X (PC = %04X)\n", dau_auc_align(), xaau_pc/*FIXME: st_pcbase*/);
	}
}

inline u64 dsp16_device_base::core_state::dau_set_psw_flags(s64 d)
{
	dau_psw &= 0x0fffU;
	bool const negative(d & DAU_A_SIGN);
	if (negative)
		dau_psw |= 0x8000U;
	if (!(d & DAU_A_MASK))
		dau_psw |= 0x4000U;
	if ((d >> 36) != (negative ? -1 : 0))
		dau_psw |= 0x2000U;
	if (((d >> 32) & ((1 << 4) - 1)) != (BIT(d, 31) ? 15 : 0))
		dau_psw |= 0x1000U;
	if (negative)
		return d | DAU_A_EXT;
	else
		return d & DAU_A_MASK;
}

#endif // MAME_CPU_DSP16_DSP16CORE_IPP