3 // Simulator definition for the MIPS DSP REV 2 ASE.
4 // Copyright (C) 2007-2016 Free Software Foundation, Inc.
5 // Contributed by MIPS Technologies, Inc.
6 // Written by Chao-ying Fu (fu@mips.com).
8 // This file is part of the MIPS sim
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 // op: 0 = ADD, 1 = SUB
25 // sat: 0 = no saturation, 1 = saturation
26 :function:::void:do_u_ph_op:int rd, int rs, int rt, int op, int sat
31 unsigned32 v1 = GPR[rs];
32 unsigned32 v2 = GPR[rt];
33 unsigned32 result = 0;
34 for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
36 h1 = (unsigned16)(v1 & 0xffff);
37 h2 = (unsigned16)(v2 & 0xffff);
39 h0 = (unsigned32)h1 + (unsigned32)h2;
41 h0 = (unsigned32)h1 - (unsigned32)h2;
42 if (op == 0 && (h0 > (unsigned32)0x0000ffff)) // ADD SAT
44 DSPCR |= DSPCR_OUFLAG4;
48 else if (op == 1 && h1 < h2) // SUB SAT
50 DSPCR |= DSPCR_OUFLAG4;
54 result |= ((unsigned32)((unsigned16)h0) << i);
56 GPR[rd] = EXTEND32 (result);
59 // op: 0 = ADD, 1 = SUB
60 // round: 0 = no rounding, 1 = rounding
61 :function:::void:do_uh_qb_op:int rd, int rs, int rt, int op, int round
66 unsigned32 v1 = GPR[rs];
67 unsigned32 v2 = GPR[rt];
68 unsigned32 result = 0;
69 for (i = 0; i < 32; i += 8, v1 >>= 8, v2 >>= 8)
71 h1 = (unsigned8)(v1 & 0xff);
72 h2 = (unsigned8)(v2 & 0xff);
74 h0 = (unsigned32)h1 + (unsigned32)h2;
76 h0 = (unsigned32)h1 - (unsigned32)h2;
81 result |= ((unsigned32)((unsigned8)h0) << i);
83 GPR[rd] = EXTEND32 (result);
86 // op: 0 = EQ, 1 = LT, 2 = LE
87 :function:::void:do_qb_cmpgdu:int rd, int rs, int rt, int op
90 unsigned32 v1 = GPR[rs];
91 unsigned32 v2 = GPR[rt];
93 unsigned32 result = 0;
95 for (i = 0, j = 0; i < 32; i += 8, j++, v1 >>= 8, v2 >>= 8)
97 h1 = (unsigned8)(v1 & 0xff);
98 h2 = (unsigned8)(v2 & 0xff);
99 mask = ~(1 << (DSPCR_CCOND_SHIFT + j));
103 result |= ((h1 == h2) << j);
104 DSPCR |= ((h1 == h2) << (DSPCR_CCOND_SHIFT + j));
106 else if (op == 1) // LT
108 result |= ((h1 < h2) << j);
109 DSPCR |= ((h1 < h2) << (DSPCR_CCOND_SHIFT + j));
113 result |= ((h1 <= h2) << j);
114 DSPCR |= ((h1 <= h2) << (DSPCR_CCOND_SHIFT + j));
117 GPR[rd] = EXTEND32 (result);
120 // op: 0 = DPA 1 = DPS
121 :function:::void:do_w_ph_dot_product:int ac, int rs, int rt, int op
124 unsigned32 v1 = GPR[rs];
125 unsigned32 v2 = GPR[rt];
128 unsigned32 lo = DSPLO(ac);
129 unsigned32 hi = DSPHI(ac);
130 signed64 prod = (signed64)((((unsigned64)hi) << 32) + (unsigned64)lo);
131 for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
133 h1 = (signed16)(v1 & 0xffff);
134 h2 = (signed16)(v2 & 0xffff);
135 result = (signed32)h1 * (signed32)h2;
137 prod += (signed64)result;
139 prod -= (signed64)result;
141 DSPLO(ac) = EXTEND32 (prod);
142 DSPHI(ac) = EXTEND32 (prod >> 32);
145 // round: 0 = no rounding, 1 = rounding
146 :function:::void:do_w_mulq:int rd, int rs, int rt, int round
148 unsigned32 v1 = GPR[rs];
149 unsigned32 v2 = GPR[rt];
155 if (w1 == (signed32) 0x80000000 && w2 == (signed32) 0x80000000)
157 DSPCR |= DSPCR_OUFLAG5;
162 prod = ((signed64) w1 * (signed64) w2) << 1;
164 prod += 0x0000000080000000LL;
167 result = (unsigned32) prod;
168 GPR[rd] = EXTEND32 (result);
171 // round: 0 = no rounding, 1 = rounding
172 :function:::void:do_precr_sra:int rt, int rs, int sa, int round
174 unsigned32 v1 = GPR[rt];
175 unsigned32 v2 = GPR[rs];
176 signed32 w1 = (signed32) v1;
177 signed32 w2 = (signed32) v2;
181 if (round == 1 && (w1 & (1 << (sa - 1))))
186 if (round == 1 && (w2 & (1 << (sa - 1))))
191 result = (w1 << 16) | (w2 & 0xffff);
192 GPR[rt] = EXTEND32 (result);
195 // round: 0 = no rounding, 1 = rounding
196 :function:::void:do_qb_shra:int rd, int rt, int shift, int round
200 unsigned32 v1 = GPR[rt];
201 unsigned32 result = 0;
202 for (i = 0; i < 32; i += 8, v1 >>= 8)
204 q0 = (signed8)(v1 & 0xff);
207 if (round == 1 && (q0 & (1 << (shift - 1))))
208 q0 = (q0 >> shift) + 1;
212 result |= ((unsigned32)((unsigned8)q0) << i);
214 GPR[rd] = EXTEND32 (result);
217 :function:::void:do_ph_shrl:int rd, int rt, int shift
221 unsigned32 v1 = GPR[rt];
222 unsigned32 result = 0;
223 for (i = 0; i < 32; i += 16, v1 >>= 16)
225 h0 = (unsigned16)(v1 & 0xffff);
227 result |= ((unsigned32)h0 << i);
229 GPR[rd] = EXTEND32 (result);
232 // op: 0 = ADD, 1 = SUB
233 // round: 0 = no rounding, 1 = rounding
234 :function:::void:do_qh_ph_op:int rd, int rs, int rt, int op, int round
239 unsigned32 v1 = GPR[rs];
240 unsigned32 v2 = GPR[rt];
241 unsigned32 result = 0;
242 for (i = 0; i < 32; i += 16, v1 >>= 16, v2 >>= 16)
244 h1 = (signed16)(v1 & 0xffff);
245 h2 = (signed16)(v2 & 0xffff);
247 h0 = (signed32)h1 + (signed32)h2;
249 h0 = (signed32)h1 - (signed32)h2;
254 result |= ((unsigned32)((unsigned16)h0) << i);
256 GPR[rd] = EXTEND32 (result);
259 // op: 0 = ADD, 1 = SUB
260 // round: 0 = no rounding, 1 = rounding
261 :function:::void:do_qh_w_op:int rd, int rs, int rt, int op, int round
265 signed32 v1 = (signed32)GPR[rs];
266 signed32 v2 = (signed32)GPR[rt];
268 v0 = (signed64)v1 + (signed64)v2;
270 v0 = (signed64)v1 - (signed64)v2;
275 GPR[rd] = EXTEND32 (v0);
278 // op: 0 = DPAX, 1 = DPSX
279 :function:::void:do_x_w_ph_dot_product:int ac, int rs, int rt, int op
282 unsigned32 v1 = GPR[rs];
283 unsigned32 v2 = GPR[rt];
286 unsigned32 lo = DSPLO(ac);
287 unsigned32 hi = DSPHI(ac);
288 signed64 prod = (signed64)((((unsigned64)hi) << 32) + (unsigned64)lo);
289 for (i = 0; i < 32; i += 16, v1 >>= 16, v2 <<= 16)
291 h1 = (signed16)(v1 & 0xffff);
292 h2 = (signed16)((v2 & 0xffff0000) >> 16);
293 result = (signed32)h1 * (signed32)h2;
295 prod += (signed64)result;
297 prod -= (signed64)result;
299 DSPLO(ac) = EXTEND32 (prod);
300 DSPHI(ac) = EXTEND32 (prod >> 32);
303 // op: 0 = DPAQX, 1 = DPSQX
304 // sat: 0 = no saturation, 1 = saturation of the accumulator
305 :function:::void:do_qx_w_ph_dot_product:int ac, int rs, int rt, int op, int sat
308 unsigned32 v1 = GPR[rs];
309 unsigned32 v2 = GPR[rt];
312 unsigned32 lo = DSPLO(ac);
313 unsigned32 hi = DSPHI(ac);
314 signed64 prod = (signed64)((((unsigned64)hi) << 32) + (unsigned64)lo);
316 for (i = 0; i < 32; i += 16, v1 >>= 16, v2 <<= 16)
318 h1 = (signed16)(v1 & 0xffff);
319 h2 = (signed16)((v2 & 0xffff0000) >> 16);
320 if (h1 == (signed16)0x8000 && h2 == (signed16)0x8000)
322 DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
326 result = ((signed32)h1 * (signed32)h2) << 1;
327 if (op == 0) // DPAQX
328 prod += (signed64)result;
330 prod -= (signed64)result;
332 // Saturation on the accumulator.
335 max = (signed64) 0x7fffffffLL;
336 min = (signed64) 0xffffffff80000000LL;
339 DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
344 DSPCR |= (1 << (DSPCR_OUFLAG_SHIFT + ac));
348 DSPLO(ac) = EXTEND32 (prod);
349 DSPHI(ac) = EXTEND32 (prod >> 32);
352 011111,00000,5.RT,5.RD,00001,010010:SPECIAL3:32::ABSQ_S.QB
353 "absq_s.qb r<RD>, r<RT>"
356 do_qb_s_absq (SD_, RD, RT);
359 011111,5.RS,5.RT,5.RD,01000,010000:SPECIAL3:32::ADDU.PH
360 "addu.ph r<RD>, r<RS>, r<RT>"
363 do_u_ph_op (SD_, RD, RS, RT, 0, 0);
366 011111,5.RS,5.RT,5.RD,01100,010000:SPECIAL3:32::ADDU_S.PH
367 "addu_s.ph r<RD>, r<RS>, r<RT>"
370 do_u_ph_op (SD_, RD, RS, RT, 0, 1);
373 011111,5.RS,5.RT,5.RD,00000,011000:SPECIAL3:32::ADDUH.QB
374 "adduh.qb r<RD>, r<RS>, r<RT>"
377 do_uh_qb_op (SD_, RD, RS, RT, 0, 0);
380 011111,5.RS,5.RT,5.RD,00010,011000:SPECIAL3:32::ADDUH_R.QB
381 "adduh_r.qb r<RD>, r<RS>, r<RT>"
384 do_uh_qb_op (SD_, RD, RS, RT, 0, 1);
387 011111,5.RS,5.RT,5.SA,00000,110001:SPECIAL3:32::APPEND
388 "append r<RT>, r<RS>, <SA>"
391 do_append (SD_, RT, RS, SA);
394 011111,5.RS,5.RT,000,2.BP,10000,110001:SPECIAL3:32::BALIGN
395 "balign r<RT>, r<RS>, <BP>"
398 do_balign (SD_, RT, RS, BP);
401 011111,5.RS,5.RT,5.RD,11000,010001:SPECIAL3:32::CMPGDU.EQ.QB
402 "cmpgdu.eq.qb r<RD>, r<RS>, r<RT>"
405 do_qb_cmpgdu (SD_, RD, RS, RT, 0);
408 011111,5.RS,5.RT,5.RD,11001,010001:SPECIAL3:32::CMPGDU.LT.QB
409 "cmpgdu.lt.qb r<RD>, r<RS>, r<RT>"
412 do_qb_cmpgdu (SD_, RD, RS, RT, 1);
415 011111,5.RS,5.RT,5.RD,11010,010001:SPECIAL3:32::CMPGDU.LE.QB
416 "cmpgdu.le.qb r<RD>, r<RS>, r<RT>"
419 do_qb_cmpgdu (SD_, RD, RS, RT, 2);
422 011111,5.RS,5.RT,000,2.AC,00000,110000:SPECIAL3:32::DPA.W.PH
423 "dpa.w.ph ac<AC>, r<RS>, r<RT>"
426 do_w_ph_dot_product (SD_, AC, RS, RT, 0);
429 011111,5.RS,5.RT,000,2.AC,00001,110000:SPECIAL3:32::DPS.W.PH
430 "dps.w.ph ac<AC>, r<RS>, r<RT>"
433 do_w_ph_dot_product (SD_, AC, RS, RT, 1);
436 011111,5.RS,5.RT,5.RD,01100,011000:SPECIAL3:32::MUL.PH
437 "mul.ph r<RD>, r<RS>, r<RT>"
440 do_ph_op (SD_, RD, RS, RT, 2, 0);
443 011111,5.RS,5.RT,5.RD,01110,011000:SPECIAL3:32::MUL_S.PH
444 "mul_s.ph r<RD>, r<RS>, r<RT>"
447 do_ph_op (SD_, RD, RS, RT, 2, 1);
450 011111,5.RS,5.RT,5.RD,10111,011000:SPECIAL3:32::MULQ_RS.W
451 "mulq_rs.w r<RD>, r<RS>, r<RT>"
454 do_w_mulq (SD_, RD, RS, RT, 1);
457 011111,5.RS,5.RT,5.RD,11110,010000:SPECIAL3:32::MULQ_S.PH
458 "mulq_s.ph r<RD>, r<RS>, r<RT>"
461 do_ph_mulq (SD_, RD, RS, RT, 0);
464 011111,5.RS,5.RT,5.RD,10110,011000:SPECIAL3:32::MULQ_S.W
465 "mulq_s.w r<RD>, r<RS>, r<RT>"
468 do_w_mulq (SD_, RD, RS, RT, 0);
471 011111,5.RS,5.RT,000,2.AC,00010,110000:SPECIAL3:32::MULSA.W.PH
472 "mulsa.w.ph ac<AC>, r<RS>, r<RT>"
475 do_ph_w_mulsa (SD_, AC, RS, RT);
478 011111,5.RS,5.RT,5.RD,01101,010001:SPECIAL3:32::PRECR.QB.PH
479 "precr.qb.ph r<RD>, r<RS>, r<RT>"
482 do_ph_qb_precr (SD_, RD, RS, RT);
485 011111,5.RS,5.RT,5.SA,11110,010001:SPECIAL3:32::PRECR_SRA.PH.W
486 "precr_sra.ph.w r<RT>, r<RS>, <SA>"
489 do_precr_sra (SD_, RT, RS, SA, 0);
492 011111,5.RS,5.RT,5.SA,11111,010001:SPECIAL3:32::PRECR_SRA_R.PH.W
493 "precr_sra_r.ph.w r<RT>, r<RS>, <SA>"
496 do_precr_sra (SD_, RT, RS, SA, 1);
499 011111,5.RS,5.RT,5.SA,00001,110001:SPECIAL3:32::PREPEND
500 "prepend r<RT>, r<RS>, <SA>"
503 do_prepend (SD_, RT, RS, SA);
506 011111,00,3.SHIFT3,5.RT,5.RD,00100,010011:SPECIAL3:32::SHRA.QB
507 "shra.qb r<RD>, r<RT>, <SHIFT3>"
510 do_qb_shra (SD_, RD, RT, SHIFT3, 0);
513 011111,00,3.SHIFT3,5.RT,5.RD,00101,010011:SPECIAL3:32::SHRA_R.QB
514 "shra_r.qb r<RD>, r<RT>, <SHIFT3>"
517 do_qb_shra (SD_, RD, RT, SHIFT3, 1);
520 011111,5.RS,5.RT,5.RD,00110,010011:SPECIAL3:32::SHRAV.QB
521 "shrav.qb r<RD>, r<RT>, r<RS>"
524 do_qb_shrav (SD_, RD, RT, RS, 0);
527 011111,5.RS,5.RT,5.RD,00111,010011:SPECIAL3:32::SHRAV_R.QB
528 "shrav_r.qb r<RD>, r<RT>, r<RS>"
531 do_qb_shrav (SD_, RD, RT, RS, 1);
534 011111,0,4.SHIFT4,5.RT,5.RD,11001,010011:SPECIAL3:32::SHRL.PH
535 "shrl.ph r<RD>, r<RT>, <SHIFT4>"
538 do_ph_shrl (SD_, RD, RT, SHIFT4);
541 011111,5.RS,5.RT,5.RD,11011,010011:SPECIAL3:32::SHRLV.PH
542 "shrlv.ph r<RD>, r<RT>, r<RS>"
545 do_ph_shrlv (SD_, RD, RT, RS);
548 011111,5.RS,5.RT,5.RD,01001,010000:SPECIAL3:32::SUBU.PH
549 "subu.ph r<RD>, r<RS>, r<RT>"
552 do_u_ph_op (SD_, RD, RS, RT, 1, 0);
555 011111,5.RS,5.RT,5.RD,01101,010000:SPECIAL3:32::SUBU_S.PH
556 "subu_s.ph r<RD>, r<RS>, r<RT>"
559 do_u_ph_op (SD_, RD, RS, RT, 1, 1);
562 011111,5.RS,5.RT,5.RD,00001,011000:SPECIAL3:32::SUBUH.QB
563 "subuh.qb r<RD>, r<RS>, r<RT>"
566 do_uh_qb_op (SD_, RD, RS, RT, 1, 0);
569 011111,5.RS,5.RT,5.RD,00011,011000:SPECIAL3:32::SUBUH_R.QB
570 "subuh_r.qb r<RD>, r<RS>, r<RT>"
573 do_uh_qb_op (SD_, RD, RS, RT, 1, 1);
576 011111,5.RS,5.RT,5.RD,01000,011000:SPECIAL3:32::ADDQH.PH
577 "addqh.ph r<RD>, r<RS>, r<RT>"
580 do_qh_ph_op (SD_, RD, RS, RT, 0, 0);
583 011111,5.RS,5.RT,5.RD,01010,011000:SPECIAL3:32::ADDQH_R.PH
584 "addqh_r.ph r<RD>, r<RS>, r<RT>"
587 do_qh_ph_op (SD_, RD, RS, RT, 0, 1);
590 011111,5.RS,5.RT,5.RD,10000,011000:SPECIAL3:32::ADDQH.W
591 "addqh.w r<RD>, r<RS>, r<RT>"
594 do_qh_w_op (SD_, RD, RS, RT, 0, 0);
597 011111,5.RS,5.RT,5.RD,10010,011000:SPECIAL3:32::ADDQH_R.W
598 "addqh_r.w r<RD>, r<RS>, r<RT>"
601 do_qh_w_op (SD_, RD, RS, RT, 0, 1);
604 011111,5.RS,5.RT,5.RD,01001,011000:SPECIAL3:32::SUBQH.PH
605 "subqh.ph r<RD>, r<RS>, r<RT>"
608 do_qh_ph_op (SD_, RD, RS, RT, 1, 0);
611 011111,5.RS,5.RT,5.RD,01011,011000:SPECIAL3:32::SUBQH_R.PH
612 "subqh_r.ph r<RD>, r<RS>, r<RT>"
615 do_qh_ph_op (SD_, RD, RS, RT, 1, 1);
618 011111,5.RS,5.RT,5.RD,10001,011000:SPECIAL3:32::SUBQH.W
619 "subqh.w r<RD>, r<RS>, r<RT>"
622 do_qh_w_op (SD_, RD, RS, RT, 1, 0);
625 011111,5.RS,5.RT,5.RD,10011,011000:SPECIAL3:32::SUBQH_R.W
626 "subqh_r.w r<RD>, r<RS>, r<RT>"
629 do_qh_w_op (SD_, RD, RS, RT, 1, 1);
632 011111,5.RS,5.RT,000,2.AC,01000,110000:SPECIAL3:32::DPAX.W.PH
633 "dpax.w.ph ac<AC>, r<RS>, r<RT>"
636 do_x_w_ph_dot_product (SD_, AC, RS, RT, 0);
639 011111,5.RS,5.RT,000,2.AC,01001,110000:SPECIAL3:32::DPSX.W.PH
640 "dpsx.w.ph ac<AC>, r<RS>, r<RT>"
643 do_x_w_ph_dot_product (SD_, AC, RS, RT, 1);
646 011111,5.RS,5.RT,000,2.AC,11000,110000:SPECIAL3:32::DPAQX_S.W.PH
647 "dpaqx_s.w.ph ac<AC>, r<RS>, r<RT>"
650 do_qx_w_ph_dot_product (SD_, AC, RS, RT, 0, 0);
653 011111,5.RS,5.RT,000,2.AC,11010,110000:SPECIAL3:32::DPAQX_SA.W.PH
654 "dpaqx_sa.w.ph ac<AC>, r<RS>, r<RT>"
657 do_qx_w_ph_dot_product (SD_, AC, RS, RT, 0, 1);
660 011111,5.RS,5.RT,000,2.AC,11001,110000:SPECIAL3:32::DPSQX_S.W.PH
661 "dpsqx_s.w.ph ac<AC>, r<RS>, r<RT>"
664 do_qx_w_ph_dot_product (SD_, AC, RS, RT, 1, 0);
667 011111,5.RS,5.RT,000,2.AC,11011,110000:SPECIAL3:32::DPSQX_SA.W.PH
668 "dpsqx_sa.w.ph ac<AC>, r<RS>, r<RT>"
671 do_qx_w_ph_dot_product (SD_, AC, RS, RT, 1, 1);