decoder.isa revision 5570
1// -*- mode:c++ -*-
2
3// Copyright (c) 2007 MIPS Technologies, Inc.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met: redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer;
10// redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution;
13// neither the name of the copyright holders nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Authors: Korey Sewell
30//          Brett Miller
31//          Jaidev Patwardhan
32
33////////////////////////////////////////////////////////////////////
34//
35// The actual MIPS32 ISA decoder
36// -----------------------------
37// The following instructions are specified in the MIPS32 ISA
38// Specification. Decoding closely follows the style specified
39// in the MIPS32 ISA specification document starting with Table
40// A-2 (document available @ http://www.mips.com)
41//
42decode OPCODE_HI default Unknown::unknown() {
43    //Table A-2
44    0x0: decode OPCODE_LO {
45        0x0: decode FUNCTION_HI {
46            0x0: decode FUNCTION_LO {
47                0x1: decode MOVCI {
48                    format BasicOp {
49                        0: movf({{ Rd = (getCondCode(FCSR, CC) == 0) ? Rd : Rs; }});
50                        1: movt({{ Rd = (getCondCode(FCSR, CC) == 1) ? Rd : Rs; }});
51                    }
52                }
53
54                format BasicOp {
55                    //Table A-3 Note: "Specific encodings of the rd, rs, and
56                    //rt fields are used to distinguish SLL, SSNOP, and EHB
57                    //functions
58                    0x0: decode RS  {
59                        0x0: decode RT_RD {
60                            0x0: decode SA default Nop::nop() {
61                                 0x1: ssnop({{;}});
62                                 0x3: ehb({{;}});
63                            }
64                            default: sll({{ Rd = Rt.uw << SA; }});
65                        }
66                    }
67
68                    0x2: decode RS_SRL {
69                        0x0:decode SRL {
70                            0: srl({{ Rd = Rt.uw >> SA; }});
71
72                            //Hardcoded assuming 32-bit ISA, probably need parameter here
73                            1: rotr({{ Rd = (Rt.uw << (32 - SA)) | (Rt.uw >> SA);}});
74                        }
75                    }
76
77                    0x3: decode RS {
78                        0x0: sra({{
79                            uint32_t temp = Rt >> SA;
80                            if ( (Rt & 0x80000000) > 0 ) {
81                                uint32_t mask = 0x80000000;
82                                for(int i=0; i < SA; i++) {
83                                    temp |= mask;
84                                    mask = mask >> 1;
85                                }
86                            }
87                            Rd = temp;
88                        }});
89                    }
90
91                    0x4: sllv({{ Rd = Rt.uw << Rs<4:0>; }});
92
93                    0x6: decode SRLV {
94                        0: srlv({{ Rd = Rt.uw >> Rs<4:0>; }});
95
96                        //Hardcoded assuming 32-bit ISA, probably need parameter here
97                        1: rotrv({{ Rd = (Rt.uw << (32 - Rs<4:0>)) | (Rt.uw >> Rs<4:0>);}});
98                    }
99
100                    0x7: srav({{
101                        int shift_amt = Rs<4:0>;
102
103                        uint32_t temp = Rt >> shift_amt;
104
105                        if ( (Rt & 0x80000000) > 0 ) {
106                                uint32_t mask = 0x80000000;
107                                for(int i=0; i < shift_amt; i++) {
108                                    temp |= mask;
109                                    mask = mask >> 1;
110                                }
111                            }
112
113                        Rd = temp;
114                    }});
115                }
116            }
117
118            0x1: decode FUNCTION_LO {
119                //Table A-3 Note: "Specific encodings of the hint field are
120                //used to distinguish JR from JR.HB and JALR from JALR.HB"
121                format Jump {
122                    0x0: decode HINT {
123                        0x1: jr_hb({{ if(Config1_CA == 0){NNPC = Rs;}else{panic("MIPS16e not supported\n");}; }}, IsReturn, ClearHazards);
124                        default: jr({{ if(Config1_CA == 0){NNPC = Rs;}else{panic("MIPS16e not supported\n");};}}, IsReturn);
125                    }
126
127                    0x1: decode HINT {
128                        0x1: jalr_hb({{ Rd = NNPC; NNPC = Rs; }}, IsCall
129                                     , ClearHazards);
130                        default: jalr({{ Rd = NNPC; NNPC = Rs; }}, IsCall);
131                    }
132                }
133
134                format BasicOp {
135                    0x2: movz({{ Rd = (Rt == 0) ? Rs : Rd; }});
136                    0x3: movn({{ Rd = (Rt != 0) ? Rs : Rd; }});
137#if FULL_SYSTEM
138                  0x4: syscall({{
139                                   fault = new SystemCallFault();
140                                 }});
141#else
142                    0x4: syscall({{ xc->syscall(R2); }},
143                                 IsSerializing, IsNonSpeculative);
144#endif
145                    0x7: sync({{ ; }}, IsMemBarrier);
146                    0x5: break({{fault = new BreakpointFault();}});
147                }
148
149            }
150
151            0x2: decode FUNCTION_LO {
152                0x0: HiLoRsSelOp::mfhi({{ Rd = HI_RS_SEL; }}, IntMultOp, IsIprAccess);
153                0x1: HiLoRdSelOp::mthi({{ HI_RD_SEL = Rs; }});
154                0x2: HiLoRsSelOp::mflo({{ Rd = LO_RS_SEL; }}, IntMultOp, IsIprAccess);
155                0x3: HiLoRdSelOp::mtlo({{ LO_RD_SEL = Rs; }});
156            }
157
158            0x3: decode FUNCTION_LO {
159                format HiLoRdSelValOp {
160                    0x0: mult({{ val = Rs.sd * Rt.sd; }}, IntMultOp);
161                    0x1: multu({{ val = Rs.ud * Rt.ud; }}, IntMultOp);
162                }
163
164                format HiLoOp {
165                    0x2: div({{ if (Rt.sd != 0) {
166                        HI0 = Rs.sd % Rt.sd;
167                        LO0 = Rs.sd / Rt.sd;
168                    }
169                    }}, IntDivOp);
170
171                    0x3: divu({{ if (Rt.ud != 0) {
172                        HI0 = Rs.ud % Rt.ud;
173                        LO0 = Rs.ud / Rt.ud;
174                    }
175                    }}, IntDivOp);
176                }
177            }
178
179            0x4: decode HINT {
180                0x0: decode FUNCTION_LO {
181                    format IntOp {
182                      0x0: add({{  /* More complicated since an ADD can cause an arithmetic overflow exception */
183                                     int64_t Src1 = Rs.sw;
184                                     int64_t Src2 = Rt.sw;
185                                     int64_t temp_result;
186#if  FULL_SYSTEM
187                                     if(((Src1 >> 31) & 1) == 1)
188                                       Src1 |= 0x100000000LL;
189#endif
190                                     temp_result = Src1 + Src2;
191#if  FULL_SYSTEM
192                                     if(((temp_result >> 31) & 1) == ((temp_result >> 32) & 1)){
193#endif
194                                       Rd.sw = temp_result;
195#if  FULL_SYSTEM
196                                     } else{
197                                       fault = new ArithmeticFault();
198                                     }
199#endif
200
201                                   }});
202                        0x1: addu({{ Rd.sw = Rs.sw + Rt.sw;}});
203                        0x2: sub({{
204                                     /* More complicated since an SUB can cause an arithmetic overflow exception */
205                                     int64_t Src1 = Rs.sw;
206                                     int64_t Src2 = Rt.sw;
207                                     int64_t temp_result = Src1 - Src2;
208#if  FULL_SYSTEM
209                                     if(((temp_result >> 31) & 1) == ((temp_result>>32) & 1)){
210#endif
211                                       Rd.sw = temp_result;
212#if  FULL_SYSTEM
213                                     } else{
214                                       fault = new ArithmeticFault();
215                                     }
216#endif
217                                   }});
218                        0x3: subu({{ Rd.sw = Rs.sw - Rt.sw;}});
219                        0x4: and({{ Rd = Rs & Rt;}});
220                        0x5: or({{ Rd = Rs | Rt;}});
221                        0x6: xor({{ Rd = Rs ^ Rt;}});
222                        0x7: nor({{ Rd = ~(Rs | Rt);}});
223                    }
224                }
225            }
226
227            0x5: decode HINT {
228                0x0: decode FUNCTION_LO {
229                    format IntOp{
230                        0x2: slt({{  Rd.sw = ( Rs.sw < Rt.sw ) ? 1 : 0}});
231                        0x3: sltu({{ Rd.uw = ( Rs.uw < Rt.uw ) ? 1 : 0}});
232                    }
233                }
234            }
235
236            0x6: decode FUNCTION_LO {
237                format Trap {
238                    0x0: tge({{  cond = (Rs.sw >= Rt.sw); }});
239                    0x1: tgeu({{ cond = (Rs.uw >= Rt.uw); }});
240                    0x2: tlt({{ cond = (Rs.sw < Rt.sw); }});
241                    0x3: tltu({{ cond = (Rs.uw < Rt.uw); }});
242                    0x4: teq({{ cond = (Rs.sw == Rt.sw); }});
243                    0x6: tne({{ cond = (Rs.sw != Rt.sw); }});
244                }
245            }
246        }
247
248        0x1: decode REGIMM_HI {
249            0x0: decode REGIMM_LO {
250                format Branch {
251                    0x0: bltz({{ cond = (Rs.sw < 0); }});
252                    0x1: bgez({{ cond = (Rs.sw >= 0); }});
253                    0x2: bltzl({{ cond = (Rs.sw < 0); }}, Likely);
254                    0x3: bgezl({{ cond = (Rs.sw >= 0); }}, Likely);
255                }
256            }
257
258            0x1: decode REGIMM_LO {
259                format TrapImm {
260                    0x0: tgei( {{ cond = (Rs.sw >= (int16_t)INTIMM); }});
261                    0x1: tgeiu({{ cond = (Rs.uw >= (uint32_t)((int32_t)((int16_t)INTIMM))); }});
262                    0x2: tlti( {{ cond = (Rs.sw < (int16_t)INTIMM); }});
263                    0x3: tltiu({{ cond = (Rs.uw < (uint32_t)((int32_t)((int16_t)INTIMM))); }});
264                    0x4: teqi( {{ cond = (Rs.sw == (int16_t)INTIMM);}});
265                    0x6: tnei( {{ cond = (Rs.sw != (int16_t)INTIMM);}});
266                }
267            }
268
269            0x2: decode REGIMM_LO {
270                format Branch {
271                    0x0: bltzal({{ cond = (Rs.sw < 0); }}, Link);
272                    0x1: decode RS {
273                        0x0: bal ({{ cond = 1; }}, IsCall, Link);
274                        default: bgezal({{ cond = (Rs.sw >= 0); }}, Link);
275                    }
276                    0x2: bltzall({{ cond = (Rs.sw < 0); }}, Link, Likely);
277                    0x3: bgezall({{ cond = (Rs.sw >= 0); }}, Link, Likely);
278                }
279            }
280
281            0x3: decode REGIMM_LO {
282                // from Table 5-4 MIPS32 REGIMM Encoding of rt Field (DSP ASE MANUAL)
283                0x4: DspBranch::bposge32({{ cond = (dspctl<5:0> >= 32); }});
284                format WarnUnimpl {
285                    0x7: synci();
286                }
287            }
288        }
289
290        format Jump {
291            0x2: j({{ NNPC = (NPC & 0xF0000000) | (JMPTARG << 2);}});
292            0x3: jal({{ NNPC = (NPC & 0xF0000000) | (JMPTARG << 2); }}, IsCall,
293                     Link);
294        }
295
296        format Branch {
297            0x4: decode RS_RT  {
298                0x0: b({{ cond = 1; }});
299                default: beq({{ cond = (Rs.sw == Rt.sw); }});
300            }
301            0x5: bne({{ cond = (Rs.sw != Rt.sw); }});
302            0x6: blez({{ cond = (Rs.sw <= 0); }});
303            0x7: bgtz({{ cond = (Rs.sw > 0); }});
304        }
305    }
306
307    0x1: decode OPCODE_LO {
308        format IntImmOp {
309            0x0: addi({{
310                          int64_t Src1 = Rs.sw;
311                          int64_t Src2 = imm;
312                          int64_t temp_result;
313#if  FULL_SYSTEM
314                          if(((Src1 >> 31) & 1) == 1)
315                            Src1 |= 0x100000000LL;
316#endif
317                          temp_result = Src1 + Src2;
318#if  FULL_SYSTEM
319                          if(((temp_result >> 31) & 1) == ((temp_result >> 32) & 1)){
320#endif
321                            Rt.sw = temp_result;
322#if  FULL_SYSTEM
323                          } else{
324                            fault = new ArithmeticFault();
325                          }
326#endif
327                        }});
328            0x1: addiu({{ Rt.sw = Rs.sw + imm;}});
329            0x2: slti({{ Rt.sw = ( Rs.sw < imm) ? 1 : 0 }});
330
331            //Edited to include MIPS AVP Pass/Fail instructions and
332            //default to the sltiu instruction
333            0x3: decode RS_RT_INTIMM {
334                0xabc1: BasicOp::fail({{ exitSimLoop("AVP/SRVP Test Failed"); }});
335                0xabc2: BasicOp::pass({{ exitSimLoop("AVP/SRVP Test Passed"); }});
336              default: sltiu({{ Rt.uw = ( Rs.uw < (uint32_t)sextImm ) ? 1 : 0 }});
337            }
338
339            0x4: andi({{ Rt.sw = Rs.sw & zextImm;}});
340            0x5: ori({{ Rt.sw = Rs.sw | zextImm;}});
341            0x6: xori({{ Rt.sw = Rs.sw ^ zextImm;}});
342
343            0x7: decode RS {
344                0x0: lui({{ Rt = imm << 16}});
345            }
346        }
347    }
348
349    0x2: decode OPCODE_LO {
350        //Table A-11 MIPS32 COP0 Encoding of rs Field
351        0x0: decode RS_MSB {
352            0x0: decode RS {
353                 format CP0Control {
354                  0x0: mfc0({{  Rt = CP0_RD_SEL;
355                                /* Hack for PageMask */
356                                if(RD == 5) // PageMask
357                                  if(Config3_SP == 0 || PageGrain_ESP == 0)
358                                    Rt &= 0xFFFFE7FF;
359                              }});
360                  0x4: mtc0({{  CP0_RD_SEL = Rt;
361
362                                if(RD == 11) // Compare{
363                                  if(Cause_TI == 1){
364                                    Cause_TI = 0;
365                                    MiscReg cause = xc->readMiscRegNoEffect(MipsISA::Cause);
366                                    int Offset = 10; // corresponding to Cause_IP0
367                                    Offset += ((IntCtl_IPTI) - 2);
368                                    replaceBits(cause,Offset,Offset,0);
369                                    xc->setMiscRegNoEffect(MipsISA::Cause,cause);
370                                  }
371
372                              }});
373                 }
374                 format CP0Unimpl {
375                   0x1: dmfc0();
376                   0x5: dmtc0();
377                   default: unknown();
378                 }
379                format MT_MFTR { // Decode MIPS MT MFTR instruction into sub-instructions
380                    0x8: decode MT_U {
381                        0x0: mftc0({{ data = xc->readRegOtherThread((RT << 3 | SEL) +
382                                                                    Ctrl_Base_DepTag);
383                                   }});
384                        0x1: decode SEL {
385                            0x0: mftgpr({{ data = xc->readRegOtherThread(RT); }});
386                            0x1: decode RT {
387                                0x0: mftlo_dsp0({{ data = xc->readRegOtherThread(MipsISA::DSPLo0); }});
388                                0x1: mfthi_dsp0({{ data = xc->readRegOtherThread(MipsISA::DSPHi0); }});
389                                0x2: mftacx_dsp0({{ data = xc->readRegOtherThread(MipsISA::DSPACX0); }});
390                                0x4: mftlo_dsp1({{ data = xc->readRegOtherThread(MipsISA::DSPLo1); }});
391                                0x5: mfthi_dsp1({{ data = xc->readRegOtherThread(MipsISA::DSPHi1); }});
392                                0x6: mftacx_dsp1({{ data = xc->readRegOtherThread(MipsISA::DSPACX1); }});
393                                0x8: mftlo_dsp2({{ data = xc->readRegOtherThread(MipsISA::DSPLo2); }});
394                                0x9: mfthi_dsp2({{ data = xc->readRegOtherThread(MipsISA::DSPHi2); }});
395                                0x10: mftacx_dsp2({{ data = xc->readRegOtherThread(MipsISA::DSPACX2); }});
396                                0x12: mftlo_dsp3({{ data = xc->readRegOtherThread(MipsISA::DSPLo3); }});
397                                0x13: mfthi_dsp3({{ data = xc->readRegOtherThread(MipsISA::DSPHi3); }});
398                                0x14: mftacx_dsp3({{ data = xc->readRegOtherThread(MipsISA::DSPACX3); }});
399                                0x16: mftdsp({{ data = xc->readRegOtherThread(MipsISA::DSPControl); }});
400                               default: CP0Unimpl::unknown();
401                            }
402                            0x2: decode MT_H {
403                                0x0: mftc1({{ data = xc->readRegOtherThread(RT +
404                                                                            FP_Base_DepTag);
405                                           }});
406                                0x1: mfthc1({{ data = xc->readRegOtherThread(RT +
407                                                                             FP_Base_DepTag);
408                                           }});
409                               }
410                            0x3: cftc1({{ uint32_t fcsr_val = xc->readRegOtherThread(MipsISA::FCSR +
411                                                                            FP_Base_DepTag);
412                                          switch (RT)
413                                          {
414                                               case 0:
415                                                 data = xc->readRegOtherThread(MipsISA::FIR +
416                                                                               Ctrl_Base_DepTag);
417                                                 break;
418                                               case 25:
419                                                 data = (fcsr_val & 0xFE000000 >> 24)
420                                                     | (fcsr_val & 0x00800000 >> 23);
421                                                 break;
422                                               case 26:
423                                                 data = fcsr_val & 0x0003F07C;
424                                                 break;
425                                               case 28:
426                                                 data = (fcsr_val & 0x00000F80)
427                                                     | (fcsr_val & 0x01000000 >> 21)
428                                                     | (fcsr_val & 0x00000003);
429                                                 break;
430                                               case 31:
431                                                 data = fcsr_val;
432                                                 break;
433                                               default:
434                                                 fatal("FP Control Value (%d) Not Valid");
435                                          }
436                                        }});
437                           default: CP0Unimpl::unknown();
438                        }
439                  }
440                }
441
442                format MT_MTTR { // Decode MIPS MT MTTR instruction into sub-instructions
443                    0xC: decode MT_U {
444                        0x0: mttc0({{ xc->setRegOtherThread((RD << 3 | SEL) + Ctrl_Base_DepTag,
445                                                            Rt);
446                                   }});
447                        0x1: decode SEL {
448                            0x0: mttgpr({{ xc->setRegOtherThread(RD, Rt); }});
449                            0x1: decode RT {
450                                0x0: mttlo_dsp0({{ xc->setRegOtherThread(MipsISA::DSPLo0, Rt);
451                                                }});
452                                0x1: mtthi_dsp0({{ xc->setRegOtherThread(MipsISA::DSPHi0,
453                                                                         Rt);
454                                                }});
455                                0x2: mttacx_dsp0({{ xc->setRegOtherThread(MipsISA::DSPACX0,
456                                                                          Rt);
457                                                 }});
458                                0x4: mttlo_dsp1({{ xc->setRegOtherThread(MipsISA::DSPLo1,
459                                                                         Rt);
460                                                }});
461                                0x5: mtthi_dsp1({{ xc->setRegOtherThread(MipsISA::DSPHi1,
462                                                                         Rt);
463                                                }});
464                                0x6: mttacx_dsp1({{ xc->setRegOtherThread(MipsISA::DSPACX1,
465                                                                          Rt);
466                                                 }});
467                                0x8: mttlo_dsp2({{ xc->setRegOtherThread(MipsISA::DSPLo2,
468                                                                         Rt);
469                                                }});
470                                0x9: mtthi_dsp2({{ xc->setRegOtherThread(MipsISA::DSPHi2,
471                                                                         Rt);
472                                                }});
473                                0x10: mttacx_dsp2({{ xc->setRegOtherThread(MipsISA::DSPACX2,
474                                                                           Rt);
475                                                  }});
476                                0x12: mttlo_dsp3({{ xc->setRegOtherThread(MipsISA::DSPLo3,
477                                                                          Rt);
478                                                 }});
479                                0x13: mtthi_dsp3({{ xc->setRegOtherThread(MipsISA::DSPHi3,
480                                                                          Rt);
481                                                 }});
482                                0x14: mttacx_dsp3({{ xc->setRegOtherThread(MipsISA::DSPACX3, Rt);
483                                                  }});
484                                0x16: mttdsp({{ xc->setRegOtherThread(MipsISA::DSPControl, Rt); }});
485                               default: CP0Unimpl::unknown();
486
487                            }
488                            0x2: mttc1({{ uint64_t data = xc->readRegOtherThread(RD +
489                                                                                FP_Base_DepTag);
490                                          data = insertBits(data, top_bit, bottom_bit, Rt);
491                                          xc->setRegOtherThread(RD + FP_Base_DepTag, data);
492                                       }});
493                            0x3: cttc1({{ uint32_t data;
494                                          switch (RD)
495                                          {
496                                            case 25:
497                                              data = 0 | (Rt.uw<7:1> << 25) // move 31...25
498                                                  | (FCSR & 0x01000000) // bit 24
499                                                  | (FCSR & 0x004FFFFF);// bit 22...0
500                                              break;
501
502                                            case 26:
503                                              data = 0 | (FCSR & 0xFFFC0000) // move 31...18
504                                                  | Rt.uw<17:12> << 12           // bit 17...12
505                                                  | (FCSR & 0x00000F80) << 7// bit 11...7
506                                                  | Rt.uw<6:2> << 2              // bit 6...2
507                                                  | (FCSR & 0x00000002);     // bit 1...0
508                                              break;
509
510                                            case 28:
511                                              data = 0 | (FCSR & 0xFE000000) // move 31...25
512                                                  | Rt.uw<2:2> << 24       // bit 24
513                                                  | (FCSR & 0x00FFF000) << 23// bit 23...12
514                                                  | Rt.uw<11:7> << 7       // bit 24
515                                                  | (FCSR & 0x000007E)
516                                                  | Rt.uw<1:0>;// bit 22...0
517                                              break;
518
519                                            case 31:
520                                              data  = Rt.uw;
521                                              break;
522
523                                            default:
524                                              panic("FP Control Value (%d) Not Available. Ignoring Access to"
525                                                    "Floating Control Status Register", FS);
526                                          }
527                                          xc->setRegOtherThread(FCSR, data);
528                                       }});
529                               default: CP0Unimpl::unknown();
530                        }
531                    }
532                }
533
534
535                0xB: decode RD {
536                    format MT_Control {
537                        0x0: decode POS {
538                            0x0: decode SEL {
539                                0x1: decode SC {
540                                    0x0: dvpe({{ Rt = MVPControl;
541                                                 if (VPEConf0<VPEC0_MVP:> == 1) {
542                                                     MVPControl = insertBits(MVPControl, MVPC_EVP, 0);
543                                                 }
544                                              }});
545                                    0x1: evpe({{ Rt = MVPControl;
546                                                 if (VPEConf0<VPEC0_MVP:> == 1) {
547                                                     MVPControl = insertBits(MVPControl, MVPC_EVP, 1);
548                                                 }
549                                              }});
550                                   default:CP0Unimpl::unknown();
551                                }
552                               default:CP0Unimpl::unknown();
553                            }
554                        default:CP0Unimpl::unknown();
555                      }
556
557                        0x1: decode POS {
558                            0xF: decode SEL {
559                                0x1: decode SC {
560                                    0x0: dmt({{ Rt = VPEControl;
561                                                VPEControl = insertBits(VPEControl, VPEC_TE, 0);
562                                         }});
563                                    0x1: emt({{ Rt = VPEControl;
564                                                VPEControl = insertBits(VPEControl, VPEC_TE, 1);
565                                         }});
566                                   default:CP0Unimpl::unknown();
567                                }
568                               default:CP0Unimpl::unknown();
569                            }
570                            default:CP0Unimpl::unknown();
571                        }
572                    }
573                    0xC: decode POS {
574                      0x0: decode SC {
575                        0x0: CP0Control::di({{
576                            if(Config_AR >= 1) // Rev 2.0 or beyond?
577                                {
578                                  Rt = Status;
579                                  Status_IE = 0;
580                                }
581                            else // Enable this else branch once we actually set values for Config on init
582                              {
583                                fault = new ReservedInstructionFault();
584                              }
585                          }});
586                        0x1: CP0Control::ei({{
587                            if(Config_AR >= 1)
588                              {
589                                Rt = Status;
590                                Status_IE = 1;
591                              }
592                            else
593                              {
594                                fault = new ReservedInstructionFault();
595                              }
596                          }});
597                        default:CP0Unimpl::unknown();
598                      }
599                    }
600                default: CP0Unimpl::unknown();
601                }
602                format CP0Control {
603                    0xA: rdpgpr({{
604                      if(Config_AR >= 1)
605                        { // Rev 2 of the architecture
606                          Rd = xc->tcBase()->readIntReg(RT + NumIntRegs * SRSCtl_PSS);
607                        }
608                      else
609                        {
610                          fault = new ReservedInstructionFault();
611                        }
612                         }});
613                    0xE: wrpgpr({{
614                      if(Config_AR >= 1)
615                        { // Rev 2 of the architecture
616                          xc->tcBase()->setIntReg(RD + NumIntRegs * SRSCtl_PSS,Rt);
617                          //			  warn("Writing %d to %d, PSS: %d, SRS: %x\n",Rt,RD + NumIntRegs * SRSCtl_PSS, SRSCtl_PSS,SRSCtl);
618                        }
619                      else
620                        {
621                          fault = new ReservedInstructionFault();
622                        }
623
624                         }});
625                }
626
627               }
628
629            //Table A-12 MIPS32 COP0 Encoding of Function Field When rs=CO
630            0x1: decode FUNCTION {
631              format CP0Control {
632                0x18: eret({{
633                               DPRINTF(MipsPRA,"Restoring PC - %x\n",EPC);
634                               // Ugly hack to get the value of Status_EXL
635                               if(Status_EXL == 1){
636                                 DPRINTF(MipsPRA,"ERET EXL Hack\n");
637                               }
638                  if(Status_ERL == 1){
639                    Status_ERL = 0;
640                    NPC = ErrorEPC;
641                    NNPC = ErrorEPC + sizeof(MachInst); // Need to adjust NNPC, otherwise things break
642                  }
643                  else {
644                    NPC = EPC;
645                    NNPC = EPC + sizeof(MachInst); // Need to adjust NNPC, otherwise things break
646                    Status_EXL = 0;
647                    if(Config_AR >=1 && SRSCtl_HSS > 0 && Status_BEV == 0){
648                      SRSCtl_CSS = SRSCtl_PSS;
649                      //xc->setShadowSet(SRSCtl_PSS);
650                    }
651                  }
652                  LLFlag = 0;
653                 }},IsReturn,IsSerializing,IsERET);
654
655                0x1F: deret({{
656                  // if(EJTagImplemented()) {
657                  if(Debug_DM == 1){
658                    Debug_DM = 1;
659                    Debug_IEXI = 0;
660                    NPC = DEPC;
661                  }
662                  else
663                    {
664                      // Undefined;
665                    }
666                  //} // EJTag Implemented
667                  //else {
668                  // Reserved Instruction Exception
669                  //}
670                 }},IsReturn,IsSerializing,IsERET);
671              }
672              format CP0TLB {
673                0x01: tlbr({{
674                    MipsISA::PTE *PTEntry = xc->tcBase()->getITBPtr()->getEntry(Index & 0x7FFFFFFF);
675                    if(PTEntry == NULL)
676                      {
677                        fatal("Invalid PTE Entry received on a TLBR instruction\n");
678                      }
679                    /* Setup PageMask */
680                    PageMask = (PTEntry->Mask << 11); // If 1KB pages are not enabled, a read of PageMask must return 0b00 in bits 12, 11
681                    /* Setup EntryHi */
682                    EntryHi = ((PTEntry->VPN << 11) | (PTEntry->asid));
683                    /* Setup Entry Lo0 */
684                    EntryLo0 = ((PTEntry->PFN0 << 6) | (PTEntry->C0 << 3) | (PTEntry->D0 << 2) | (PTEntry->V0 << 1) | PTEntry->G);
685                    /* Setup Entry Lo1 */
686                    EntryLo1 = ((PTEntry->PFN1 << 6) | (PTEntry->C1 << 3) | (PTEntry->D1 << 2) | (PTEntry->V1 << 1) | PTEntry->G);
687                }}); // Need to hook up to TLB
688
689                0x02: tlbwi({{
690                                //Create PTE
691                                MipsISA::PTE NewEntry;
692                                //Write PTE
693                                NewEntry.Mask = (Addr)(PageMask >> 11);
694                                NewEntry.VPN = (Addr)(EntryHi >> 11);
695                                /*  PageGrain _ ESP                    Config3 _ SP */
696                                if(((PageGrain>>28) & 1) == 0 || ((Config3>>4)&1) ==0) {
697                                  NewEntry.Mask |= 0x3; // If 1KB pages are *NOT* enabled, lowest bits of the mask are 0b11 for TLB writes
698                                  NewEntry.VPN &= 0xFFFFFFFC; // Reset bits 0 and 1 if 1KB pages are not enabled
699                                }
700                                NewEntry.asid = (uint8_t)(EntryHi & 0xFF);
701
702                                NewEntry.PFN0 = (Addr)(EntryLo0 >> 6);
703                                NewEntry.PFN1 = (Addr)(EntryLo1 >> 6);
704                                NewEntry.D0 = (bool)((EntryLo0 >> 2) & 1);
705                                NewEntry.D1 = (bool)((EntryLo1 >> 2) & 1);
706                                NewEntry.V1 = (bool)((EntryLo1 >> 1) & 1);
707                                NewEntry.V0 = (bool)((EntryLo0 >> 1) & 1);
708                                NewEntry.G = (bool)((EntryLo0 & EntryLo1) & 1);
709                                NewEntry.C0 = (uint8_t)((EntryLo0 >> 3) & 0x7);
710                                NewEntry.C1 = (uint8_t)((EntryLo1 >> 3) & 0x7);
711                                /* Now, compute the AddrShiftAmount and OffsetMask - TLB optimizations */
712                                /* Addr Shift Amount for 1KB or larger pages */
713                                //	    warn("PTE->Mask: %x\n",pte->Mask);
714                                if((NewEntry.Mask & 0xFFFF) == 3){
715                                  NewEntry.AddrShiftAmount = 12;
716                                } else if((NewEntry.Mask & 0xFFFF) == 0x0000){
717                                  NewEntry.AddrShiftAmount = 10;
718                                } else if((NewEntry.Mask & 0xFFFC) == 0x000C){
719                                  NewEntry.AddrShiftAmount = 14;
720                                } else if((NewEntry.Mask & 0xFFF0) == 0x0030){
721                                  NewEntry.AddrShiftAmount = 16;
722                                } else if((NewEntry.Mask & 0xFFC0) == 0x00C0){
723                                  NewEntry.AddrShiftAmount = 18;
724                                } else if((NewEntry.Mask & 0xFF00) == 0x0300){
725                                  NewEntry.AddrShiftAmount = 20;
726                                } else if((NewEntry.Mask & 0xFC00) == 0x0C00){
727                                  NewEntry.AddrShiftAmount = 22;
728                                } else if((NewEntry.Mask & 0xF000) == 0x3000){
729                                  NewEntry.AddrShiftAmount = 24;
730                                } else if((NewEntry.Mask & 0xC000) == 0xC000){
731                                  NewEntry.AddrShiftAmount = 26;
732                                } else if((NewEntry.Mask & 0x30000) == 0x30000){
733                                  NewEntry.AddrShiftAmount = 28;
734                                } else {
735                                  fatal("Invalid Mask Pattern Detected!\n");
736                                }
737                                NewEntry.OffsetMask = ((1<<NewEntry.AddrShiftAmount)-1);
738
739                                MipsISA::TLB *Ptr=xc->tcBase()->getITBPtr();
740                                MiscReg c3=xc->readMiscReg(MipsISA::Config3);
741                                MiscReg pg=xc->readMiscReg(MipsISA::PageGrain);
742                                int SP=0;
743                                if(bits(c3,Config3_SP)==1 && bits(pg,PageGrain_ESP)==1){
744                                  SP=1;
745                                }
746                                Ptr->insertAt(NewEntry,Index & 0x7FFFFFFF,SP);
747              }});
748                0x06: tlbwr({{
749                                //Create PTE
750                                MipsISA::PTE NewEntry;
751                                //Write PTE
752                                NewEntry.Mask = (Addr)(PageMask >> 11);
753                                NewEntry.VPN = (Addr)(EntryHi >> 11);
754                                /*  PageGrain _ ESP                    Config3 _ SP */
755                                if(((PageGrain>>28) & 1) == 0 || ((Config3>>4)&1) ==0) {
756                                  NewEntry.Mask |= 0x3; // If 1KB pages are *NOT* enabled, lowest bits of the mask are 0b11 for TLB writes
757                                  NewEntry.VPN &= 0xFFFFFFFC; // Reset bits 0 and 1 if 1KB pages are not enabled
758                                }
759                                NewEntry.asid = (uint8_t)(EntryHi & 0xFF);
760
761                                NewEntry.PFN0 = (Addr)(EntryLo0 >> 6);
762                                NewEntry.PFN1 = (Addr)(EntryLo1 >> 6);
763                                NewEntry.D0 = (bool)((EntryLo0 >> 2) & 1);
764                                NewEntry.D1 = (bool)((EntryLo1 >> 2) & 1);
765                                NewEntry.V1 = (bool)((EntryLo1 >> 1) & 1);
766                                NewEntry.V0 = (bool)((EntryLo0 >> 1) & 1);
767                                NewEntry.G = (bool)((EntryLo0 & EntryLo1) & 1);
768                                NewEntry.C0 = (uint8_t)((EntryLo0 >> 3) & 0x7);
769                                NewEntry.C1 = (uint8_t)((EntryLo1 >> 3) & 0x7);
770                                /* Now, compute the AddrShiftAmount and OffsetMask - TLB optimizations */
771                                /* Addr Shift Amount for 1KB or larger pages */
772                                //	    warn("PTE->Mask: %x\n",pte->Mask);
773                                if((NewEntry.Mask & 0xFFFF) == 3){
774                                  NewEntry.AddrShiftAmount = 12;
775                                } else if((NewEntry.Mask & 0xFFFF) == 0x0000){
776                                  NewEntry.AddrShiftAmount = 10;
777                                } else if((NewEntry.Mask & 0xFFFC) == 0x000C){
778                                  NewEntry.AddrShiftAmount = 14;
779                                } else if((NewEntry.Mask & 0xFFF0) == 0x0030){
780                                  NewEntry.AddrShiftAmount = 16;
781                                } else if((NewEntry.Mask & 0xFFC0) == 0x00C0){
782                                  NewEntry.AddrShiftAmount = 18;
783                                } else if((NewEntry.Mask & 0xFF00) == 0x0300){
784                                  NewEntry.AddrShiftAmount = 20;
785                                } else if((NewEntry.Mask & 0xFC00) == 0x0C00){
786                                  NewEntry.AddrShiftAmount = 22;
787                                } else if((NewEntry.Mask & 0xF000) == 0x3000){
788                                  NewEntry.AddrShiftAmount = 24;
789                                } else if((NewEntry.Mask & 0xC000) == 0xC000){
790                                  NewEntry.AddrShiftAmount = 26;
791                                } else if((NewEntry.Mask & 0x30000) == 0x30000){
792                                  NewEntry.AddrShiftAmount = 28;
793                                } else {
794                                  fatal("Invalid Mask Pattern Detected!\n");
795                                }
796                                NewEntry.OffsetMask = ((1<<NewEntry.AddrShiftAmount)-1);
797
798                                MipsISA::TLB *Ptr=xc->tcBase()->getITBPtr();
799                                MiscReg c3=xc->readMiscReg(MipsISA::Config3);
800                                MiscReg pg=xc->readMiscReg(MipsISA::PageGrain);
801                                int SP=0;
802                                if(bits(c3,Config3_SP)==1 && bits(pg,PageGrain_ESP)==1){
803                                  SP=1;
804                                }
805                                Ptr->insertAt(NewEntry,Random,SP);
806                }});
807
808                0x08: tlbp({{
809                               int TLB_Index;
810                               Addr VPN;
811                               if(PageGrain_ESP == 1 && Config3_SP ==1){
812                                 VPN = EntryHi >> 11;
813                               } else {
814                                 VPN = ((EntryHi >> 11) & 0xFFFFFFFC); // Mask off lower 2 bits
815                               }
816                               TLB_Index = xc->tcBase()->getITBPtr()->probeEntry(VPN,EntryHi_ASID);
817                               if(TLB_Index != -1){  // Check TLB for entry matching EntryHi
818                                 Index=TLB_Index;
819                                 //			    warn("\ntlbp: Match Found!\n");
820                               } else {// else, set Index = 1<<31
821                                 Index = (1<<31);
822                               }
823                        }});
824              }
825              format CP0Unimpl {
826                0x20: wait();
827              }
828               default: CP0Unimpl::unknown();
829
830            }
831        }
832
833        //Table A-13 MIPS32 COP1 Encoding of rs Field
834        0x1: decode RS_MSB {
835
836            0x0: decode RS_HI {
837                0x0: decode RS_LO {
838                    format CP1Control {
839                        0x0: mfc1 ({{ Rt.uw = Fs.uw; }});
840
841                        0x2: cfc1({{
842                            switch (FS)
843                            {
844                              case 0:
845                                Rt = FIR;
846                                break;
847                              case 25:
848                                Rt = 0 | (FCSR & 0xFE000000) >> 24 | (FCSR & 0x00800000) >> 23;
849                                break;
850                              case 26:
851                                Rt = 0 | (FCSR & 0x0003F07C);
852                                break;
853                              case 28:
854                                Rt = 0 | (FCSR & 0x00000F80) | (FCSR & 0x01000000) >> 21 | (FCSR & 0x00000003);
855                                break;
856                              case 31:
857                                Rt = FCSR;
858                                break;
859                              default:
860                                warn("FP Control Value (%d) Not Valid");
861                            }
862                            //			    warn("FCSR: %x, FS: %d, FIR: %x, Rt: %x\n",FCSR, FS, FIR, Rt);
863                        }});
864
865                        0x3: mfhc1({{ Rt.uw = Fs.ud<63:32>;}});
866
867                        0x4: mtc1 ({{ Fs.uw = Rt.uw;       }});
868
869                        0x6: ctc1({{
870                            switch (FS)
871                            {
872                              case 25:
873                                FCSR = 0 | (Rt.uw<7:1> << 25) // move 31...25
874                                    | (FCSR & 0x01000000) // bit 24
875                                    | (FCSR & 0x004FFFFF);// bit 22...0
876                                break;
877
878                              case 26:
879                                FCSR = 0 | (FCSR & 0xFFFC0000) // move 31...18
880                                    | Rt.uw<17:12> << 12           // bit 17...12
881                                    | (FCSR & 0x00000F80) << 7// bit 11...7
882                                    | Rt.uw<6:2> << 2              // bit 6...2
883                                    | (FCSR & 0x00000002);     // bit 1...0
884                                break;
885
886                              case 28:
887                                FCSR = 0 | (FCSR & 0xFE000000) // move 31...25
888                                    | Rt.uw<2:2> << 24       // bit 24
889                                    | (FCSR & 0x00FFF000) << 23// bit 23...12
890                                    | Rt.uw<11:7> << 7       // bit 24
891                                    | (FCSR & 0x000007E)
892                                    | Rt.uw<1:0>;// bit 22...0
893                                break;
894
895                              case 31:
896                                FCSR  = Rt.uw;
897                                break;
898
899                              default:
900                                panic("FP Control Value (%d) Not Available. Ignoring Access to"
901                                      "Floating Control Status Register", FS);
902                            }
903                        }});
904
905                        0x7: mthc1({{
906                             uint64_t fs_hi = Rt.uw;
907                             uint64_t fs_lo = Fs.ud & 0x0FFFFFFFF;
908                             Fs.ud = (fs_hi << 32) | fs_lo;
909                        }});
910
911                    }
912                    format CP1Unimpl {
913                      0x1: dmfc1();
914                      0x5: dmtc1();
915                    }
916                   }
917
918                0x1:
919                   decode RS_LO {
920                     0x0:
921                     decode ND {
922                       format Branch {
923                         0x0: decode TF {
924                           0x0: bc1f({{ cond = getCondCode(FCSR, BRANCH_CC) == 0;
925                                       }});
926                           0x1: bc1t({{ cond = getCondCode(FCSR, BRANCH_CC) == 1;
927                                       }});
928                         }
929                         0x1: decode TF {
930                           0x0: bc1fl({{ cond = getCondCode(FCSR, BRANCH_CC) == 0;
931                                        }}, Likely);
932                           0x1: bc1tl({{ cond = getCondCode(FCSR, BRANCH_CC) == 1;
933                                        }}, Likely);
934                         }
935                       }
936                     }
937                   format CP1Unimpl {
938                     0x1: bc1any2();
939                     0x2: bc1any4();
940                     default: unknown();
941                   }
942                   }
943            }
944
945            0x1: decode RS_HI {
946                0x2: decode RS_LO {
947                    //Table A-14 MIPS32 COP1 Encoding of Function Field When rs=S
948                    //(( single-precision floating point))
949                    0x0: decode FUNCTION_HI {
950                        0x0: decode FUNCTION_LO {
951                            format FloatOp {
952                                0x0: add_s({{ Fd.sf = Fs.sf + Ft.sf;}});
953                                0x1: sub_s({{ Fd.sf = Fs.sf - Ft.sf;}});
954                                0x2: mul_s({{ Fd.sf = Fs.sf * Ft.sf;}});
955                                0x3: div_s({{ Fd.sf = Fs.sf / Ft.sf;}});
956                                0x4: sqrt_s({{ Fd.sf = sqrt(Fs.sf);}});
957                                0x5: abs_s({{ Fd.sf = fabs(Fs.sf);}});
958                                0x7: neg_s({{ Fd.sf = -Fs.sf;}});
959                            }
960
961                            0x6: BasicOp::mov_s({{ Fd.sf = Fs.sf;}});
962                        }
963
964                        0x1: decode FUNCTION_LO {
965                            format FloatConvertOp {
966                                0x0: round_l_s({{ val = Fs.sf; }}, ToLong,
967                                               Round);
968                                0x1: trunc_l_s({{ val = Fs.sf; }}, ToLong,
969                                               Trunc);
970                                0x2: ceil_l_s({{ val = Fs.sf; }}, ToLong,
971                                               Ceil);
972                                0x3: floor_l_s({{ val = Fs.sf; }}, ToLong,
973                                               Floor);
974                                0x4: round_w_s({{ val = Fs.sf; }}, ToWord,
975                                               Round);
976                                0x5: trunc_w_s({{ val = Fs.sf; }}, ToWord,
977                                               Trunc);
978                                0x6: ceil_w_s({{ val = Fs.sf; }}, ToWord,
979                                               Ceil);
980                                0x7: floor_w_s({{ val = Fs.sf; }}, ToWord,
981                                               Floor);
982                            }
983                        }
984
985                        0x2: decode FUNCTION_LO {
986                            0x1: decode MOVCF {
987                                format BasicOp {
988                                    0x0: movf_s({{ Fd = (getCondCode(FCSR,CC) == 0) ? Fs : Fd; }});
989                                    0x1: movt_s({{ Fd = (getCondCode(FCSR,CC) == 1) ? Fs : Fd; }});
990                                }
991                            }
992
993                            format BasicOp {
994                                0x2: movz_s({{ Fd = (Rt == 0) ? Fs : Fd; }});
995                                0x3: movn_s({{ Fd = (Rt != 0) ? Fs : Fd; }});
996                            }
997
998                            format FloatOp {
999                                0x5: recip_s({{ Fd = 1 / Fs; }});
1000                                0x6: rsqrt_s({{ Fd = 1 / sqrt(Fs);}});
1001                            }
1002                            format CP1Unimpl {
1003                              default: unknown();
1004                            }
1005                        }
1006                        0x3: CP1Unimpl::unknown();
1007
1008                        0x4: decode FUNCTION_LO {
1009                            format FloatConvertOp {
1010                                0x1: cvt_d_s({{ val = Fs.sf; }}, ToDouble);
1011                                0x4: cvt_w_s({{ val = Fs.sf; }}, ToWord);
1012                                0x5: cvt_l_s({{ val = Fs.sf; }}, ToLong);
1013                            }
1014
1015                            0x6: FloatOp::cvt_ps_s({{
1016                                    Fd.ud = (uint64_t) Fs.uw << 32 |
1017                                            (uint64_t) Ft.uw;
1018                                }});
1019                            format CP1Unimpl {
1020                              default: unknown();
1021                            }
1022                        }
1023                        0x5: CP1Unimpl::unknown();
1024
1025                        0x6: decode FUNCTION_LO {
1026                            format FloatCompareOp {
1027                                0x0: c_f_s({{ cond = 0; }}, SinglePrecision,
1028                                           UnorderedFalse);
1029                                0x1: c_un_s({{ cond = 0; }}, SinglePrecision,
1030                                            UnorderedTrue);
1031                                0x2: c_eq_s({{ cond = (Fs.sf == Ft.sf); }},
1032                                            UnorderedFalse);
1033                                0x3: c_ueq_s({{ cond = (Fs.sf == Ft.sf); }},
1034                                             UnorderedTrue);
1035                                0x4: c_olt_s({{ cond = (Fs.sf < Ft.sf);	}},
1036                                             UnorderedFalse);
1037                                0x5: c_ult_s({{ cond = (Fs.sf < Ft.sf); }},
1038                                             UnorderedTrue);
1039                                0x6: c_ole_s({{ cond = (Fs.sf <= Ft.sf); }},
1040                                             UnorderedFalse);
1041                                0x7: c_ule_s({{ cond = (Fs.sf <= Ft.sf); }},
1042                                             UnorderedTrue);
1043                            }
1044                        }
1045
1046                        0x7: decode FUNCTION_LO {
1047                            format FloatCompareOp {
1048                                0x0: c_sf_s({{ cond = 0; }}, SinglePrecision,
1049                                            UnorderedFalse, QnanException);
1050                                0x1: c_ngle_s({{ cond = 0; }}, SinglePrecision,
1051                                              UnorderedTrue, QnanException);
1052                                0x2: c_seq_s({{ cond = (Fs.sf == Ft.sf);}},
1053                                             UnorderedFalse, QnanException);
1054                                0x3: c_ngl_s({{ cond = (Fs.sf == Ft.sf); }},
1055                                             UnorderedTrue, QnanException);
1056                                0x4: c_lt_s({{ cond = (Fs.sf < Ft.sf); }},
1057                                            UnorderedFalse, QnanException);
1058                                0x5: c_nge_s({{ cond = (Fs.sf < Ft.sf); }},
1059                                             UnorderedTrue, QnanException);
1060                                0x6: c_le_s({{ cond = (Fs.sf <= Ft.sf); }},
1061                                            UnorderedFalse, QnanException);
1062                                0x7: c_ngt_s({{ cond = (Fs.sf <= Ft.sf); }},
1063                                             UnorderedTrue, QnanException);
1064                            }
1065                        }
1066                    }
1067
1068                    //Table A-15 MIPS32 COP1 Encoding of Function Field When rs=D
1069                    0x1: decode FUNCTION_HI {
1070                        0x0: decode FUNCTION_LO {
1071                            format FloatOp {
1072                                0x0: add_d({{ Fd.df = Fs.df + Ft.df; }});
1073                                0x1: sub_d({{ Fd.df = Fs.df - Ft.df; }});
1074                                0x2: mul_d({{ Fd.df = Fs.df * Ft.df; }});
1075                                0x3: div_d({{ Fd.df = Fs.df / Ft.df; }});
1076                                0x4: sqrt_d({{ Fd.df = sqrt(Fs.df);  }});
1077                                0x5: abs_d({{ Fd.df = fabs(Fs.df);   }});
1078                                0x7: neg_d({{ Fd.df = -1 * Fs.df;    }});
1079                            }
1080
1081                            0x6: BasicOp::mov_d({{ Fd.df = Fs.df;    }});
1082                        }
1083
1084                        0x1: decode FUNCTION_LO {
1085                            format FloatConvertOp {
1086                                0x0: round_l_d({{ val = Fs.df; }}, ToLong,
1087                                               Round);
1088                                0x1: trunc_l_d({{ val = Fs.df; }}, ToLong,
1089                                               Trunc);
1090                                0x2: ceil_l_d({{ val = Fs.df; }}, ToLong,
1091                                               Ceil);
1092                                0x3: floor_l_d({{ val = Fs.df; }}, ToLong,
1093                                               Floor);
1094                                0x4: round_w_d({{ val = Fs.df; }}, ToWord,
1095                                               Round);
1096                                0x5: trunc_w_d({{ val = Fs.df; }}, ToWord,
1097                                               Trunc);
1098                                0x6: ceil_w_d({{ val = Fs.df; }}, ToWord,
1099                                               Ceil);
1100                                0x7: floor_w_d({{ val = Fs.df; }}, ToWord,
1101                                               Floor);
1102                            }
1103                        }
1104
1105                        0x2: decode FUNCTION_LO {
1106                            0x1: decode MOVCF {
1107                                format BasicOp {
1108                                    0x0: movf_d({{ Fd.df = (getCondCode(FCSR,CC) == 0) ?
1109                                                       Fs.df : Fd.df;
1110                                                }});
1111                                    0x1: movt_d({{ Fd.df = (getCondCode(FCSR,CC) == 1) ?
1112                                                       Fs.df : Fd.df;
1113                                                }});
1114                                }
1115                            }
1116
1117                            format BasicOp {
1118                                0x2: movz_d({{ Fd.df = (Rt == 0) ? Fs.df : Fd.df; }});
1119                                0x3: movn_d({{ Fd.df = (Rt != 0) ? Fs.df : Fd.df; }});
1120                            }
1121
1122                            format FloatOp {
1123                                0x5: recip_d({{ Fd.df = 1 / Fs.df }});
1124                                0x6: rsqrt_d({{ Fd.df = 1 / sqrt(Fs.df) }});
1125                            }
1126                            format CP1Unimpl {
1127                              default: unknown();
1128                            }
1129
1130                        }
1131                        0x4: decode FUNCTION_LO {
1132                            format FloatConvertOp {
1133                                0x0: cvt_s_d({{ val = Fs.df; }}, ToSingle);
1134                                0x4: cvt_w_d({{ val = Fs.df; }}, ToWord);
1135                                0x5: cvt_l_d({{ val = Fs.df; }}, ToLong);
1136                            }
1137                           default: CP1Unimpl::unknown();
1138                        }
1139
1140                        0x6: decode FUNCTION_LO {
1141                            format FloatCompareOp {
1142                                0x0: c_f_d({{ cond = 0; }}, DoublePrecision,
1143                                           UnorderedFalse);
1144                                0x1: c_un_d({{ cond = 0; }}, DoublePrecision,
1145                                            UnorderedTrue);
1146                                0x2: c_eq_d({{ cond = (Fs.df == Ft.df); }},
1147                                            UnorderedFalse);
1148                                0x3: c_ueq_d({{ cond = (Fs.df == Ft.df); }},
1149                                             UnorderedTrue);
1150                                0x4: c_olt_d({{ cond = (Fs.df < Ft.df);	}},
1151                                             UnorderedFalse);
1152                                0x5: c_ult_d({{ cond = (Fs.df < Ft.df); }},
1153                                             UnorderedTrue);
1154                                0x6: c_ole_d({{ cond = (Fs.df <= Ft.df); }},
1155                                             UnorderedFalse);
1156                                0x7: c_ule_d({{ cond = (Fs.df <= Ft.df); }},
1157                                             UnorderedTrue);
1158                            }
1159                        }
1160
1161                        0x7: decode FUNCTION_LO {
1162                            format FloatCompareOp {
1163                                0x0: c_sf_d({{ cond = 0; }}, DoublePrecision,
1164                                            UnorderedFalse, QnanException);
1165                                0x1: c_ngle_d({{ cond = 0; }}, DoublePrecision,
1166                                              UnorderedTrue, QnanException);
1167                                0x2: c_seq_d({{ cond = (Fs.df == Ft.df); }},
1168                                             UnorderedFalse, QnanException);
1169                                0x3: c_ngl_d({{ cond = (Fs.df == Ft.df); }},
1170                                             UnorderedTrue, QnanException);
1171                                0x4: c_lt_d({{ cond = (Fs.df < Ft.df); }},
1172                                            UnorderedFalse, QnanException);
1173                                0x5: c_nge_d({{ cond = (Fs.df < Ft.df); }},
1174                                             UnorderedTrue, QnanException);
1175                                0x6: c_le_d({{ cond = (Fs.df <= Ft.df); }},
1176                                            UnorderedFalse, QnanException);
1177                                0x7: c_ngt_d({{ cond = (Fs.df <= Ft.df); }},
1178                                             UnorderedTrue, QnanException);
1179                            }
1180                        }
1181                       default: CP1Unimpl::unknown();
1182                    }
1183                    0x2: CP1Unimpl::unknown();
1184                    0x3: CP1Unimpl::unknown();
1185                    0x7: CP1Unimpl::unknown();
1186
1187                    //Table A-16 MIPS32 COP1 Encoding of Function Field When rs=W
1188                    0x4: decode FUNCTION {
1189                        format FloatConvertOp {
1190                            0x20: cvt_s_w({{ val = Fs.uw; }}, ToSingle);
1191                            0x21: cvt_d_w({{ val = Fs.uw; }}, ToDouble);
1192                            0x26: CP1Unimpl::cvt_ps_w();
1193                        }
1194                       default: CP1Unimpl::unknown();
1195                    }
1196
1197                    //Table A-16 MIPS32 COP1 Encoding of Function Field When rs=L1
1198                    //Note: "1. Format type L is legal only if 64-bit floating point operations
1199                    //are enabled."
1200                    0x5: decode FUNCTION_HI {
1201                        format FloatConvertOp {
1202                            0x20: cvt_s_l({{ val = Fs.ud; }}, ToSingle);
1203                            0x21: cvt_d_l({{ val = Fs.ud; }}, ToDouble);
1204                            0x26: CP1Unimpl::cvt_ps_l();
1205                        }
1206                       default: CP1Unimpl::unknown();
1207                    }
1208
1209                    //Table A-17 MIPS64 COP1 Encoding of Function Field When rs=PS1
1210                    //Note: "1. Format type PS is legal only if 64-bit floating point operations
1211                    //are enabled. "
1212                    0x6: decode FUNCTION_HI {
1213                        0x0: decode FUNCTION_LO {
1214                            format Float64Op {
1215                                0x0: add_ps({{
1216                                    Fd1.sf = Fs1.sf + Ft2.sf;
1217                                    Fd2.sf = Fs2.sf + Ft2.sf;
1218                                }});
1219                                0x1: sub_ps({{
1220                                    Fd1.sf = Fs1.sf - Ft2.sf;
1221                                    Fd2.sf = Fs2.sf - Ft2.sf;
1222                                }});
1223                                0x2: mul_ps({{
1224                                    Fd1.sf = Fs1.sf * Ft2.sf;
1225                                    Fd2.sf = Fs2.sf * Ft2.sf;
1226                                }});
1227                                0x5: abs_ps({{
1228                                    Fd1.sf = fabs(Fs1.sf);
1229                                    Fd2.sf = fabs(Fs2.sf);
1230                                }});
1231                                0x6: mov_ps({{
1232                                    Fd1.sf = Fs1.sf;
1233                                    Fd2.sf = Fs2.sf;
1234                                }});
1235                                0x7: neg_ps({{
1236                                    Fd1.sf = -(Fs1.sf);
1237                                    Fd2.sf = -(Fs2.sf);
1238                                }});
1239                            default: CP1Unimpl::unknown();
1240                            }
1241                        }
1242                        0x1: CP1Unimpl::unknown();
1243                        0x2: decode FUNCTION_LO {
1244                            0x1: decode MOVCF {
1245                                format Float64Op {
1246                                    0x0: movf_ps({{
1247                                        Fd1 = (getCondCode(FCSR, CC) == 0) ?
1248                                            Fs1 : Fd1;
1249                                        Fd2 = (getCondCode(FCSR, CC+1) == 0) ?
1250                                            Fs2 : Fd2;
1251                                    }});
1252                                    0x1: movt_ps({{
1253                                        Fd2 = (getCondCode(FCSR, CC) == 1) ?
1254                                            Fs1 : Fd1;
1255                                        Fd2 = (getCondCode(FCSR, CC+1) == 1) ?
1256                                            Fs2 : Fd2;
1257                                    }});
1258                                }
1259                            }
1260
1261                            format Float64Op {
1262                                0x2: movz_ps({{
1263                                    Fd1 = (getCondCode(FCSR, CC) == 0) ?
1264                                        Fs1 : Fd1;
1265                                    Fd2 = (getCondCode(FCSR, CC) == 0) ?
1266                                        Fs2 : Fd2;
1267                                }});
1268                                0x3: movn_ps({{
1269                                    Fd1 = (getCondCode(FCSR, CC) == 1) ?
1270                                        Fs1 : Fd1;
1271                                    Fd2 = (getCondCode(FCSR, CC) == 1) ?
1272                                        Fs2 : Fd2;
1273                                }});
1274                            }
1275                           default: CP1Unimpl::unknown();
1276
1277                        }
1278                        0x3: CP1Unimpl::unknown();
1279                        0x4: decode FUNCTION_LO {
1280                            0x0: FloatOp::cvt_s_pu({{ Fd.sf = Fs2.sf; }});
1281                            default: CP1Unimpl::unknown();
1282                        }
1283
1284                        0x5: decode FUNCTION_LO {
1285                            0x0: FloatOp::cvt_s_pl({{ Fd.sf = Fs1.sf; }});
1286
1287                            format Float64Op {
1288                                0x4: pll({{ Fd.ud = (uint64_t) Fs1.uw << 32 |
1289                                                    Ft1.uw;
1290                                         }});
1291                                0x5: plu({{ Fd.ud = (uint64_t) Fs1.uw << 32 |
1292                                                    Ft2.uw;
1293                                         }});
1294                                0x6: pul({{ Fd.ud = (uint64_t) Fs2.uw << 32 |
1295                                                    Ft1.uw;
1296                                         }});
1297                                0x7: puu({{ Fd.ud = (uint64_t) Fs2.uw << 32 |
1298                                                    Ft2.uw;
1299                                         }});
1300                            }
1301                            default: CP1Unimpl::unknown();
1302                        }
1303
1304                        0x6: decode FUNCTION_LO {
1305                            format FloatPSCompareOp {
1306                                0x0: c_f_ps({{ cond1 = 0; }}, {{ cond2 = 0; }},
1307                                            UnorderedFalse);
1308                                0x1: c_un_ps({{ cond1 = 0; }}, {{ cond2 = 0; }},
1309                                             UnorderedTrue);
1310                                0x2: c_eq_ps({{ cond1 = (Fs1.sf == Ft1.sf); }},
1311                                             {{ cond2 = (Fs2.sf == Ft2.sf); }},
1312                                             UnorderedFalse);
1313                                0x3: c_ueq_ps({{ cond1 = (Fs1.sf == Ft1.sf); }},
1314                                              {{ cond2 = (Fs2.sf == Ft2.sf); }},
1315                                              UnorderedTrue);
1316                                0x4: c_olt_ps({{ cond1 = (Fs1.sf < Ft1.sf); }},
1317                                              {{ cond2 = (Fs2.sf < Ft2.sf); }},
1318                                              UnorderedFalse);
1319                                0x5: c_ult_ps({{ cond1 = (Fs.sf < Ft.sf); }},
1320                                              {{ cond2 = (Fs2.sf < Ft2.sf); }},
1321                                              UnorderedTrue);
1322                                0x6: c_ole_ps({{ cond1 = (Fs.sf <= Ft.sf); }},
1323                                              {{ cond2 = (Fs2.sf <= Ft2.sf); }},
1324                                              UnorderedFalse);
1325                                0x7: c_ule_ps({{ cond1 = (Fs1.sf <= Ft1.sf); }},
1326                                              {{ cond2 = (Fs2.sf <= Ft2.sf); }},
1327                                              UnorderedTrue);
1328                            }
1329                        }
1330
1331                        0x7: decode FUNCTION_LO {
1332                            format FloatPSCompareOp {
1333                                0x0: c_sf_ps({{ cond1 = 0; }}, {{ cond2 = 0; }},
1334                                             UnorderedFalse, QnanException);
1335                                0x1: c_ngle_ps({{ cond1 = 0; }},
1336                                               {{ cond2 = 0; }},
1337                                               UnorderedTrue, QnanException);
1338                                0x2: c_seq_ps({{ cond1 = (Fs1.sf == Ft1.sf); }},
1339                                              {{ cond2 = (Fs2.sf == Ft2.sf); }},
1340                                              UnorderedFalse, QnanException);
1341                                0x3: c_ngl_ps({{ cond1 = (Fs1.sf == Ft1.sf); }},
1342                                              {{ cond2 = (Fs2.sf == Ft2.sf); }},
1343                                              UnorderedTrue, QnanException);
1344                                0x4: c_lt_ps({{ cond1 = (Fs1.sf < Ft1.sf); }},
1345                                             {{ cond2 = (Fs2.sf < Ft2.sf); }},
1346                                             UnorderedFalse, QnanException);
1347                                0x5: c_nge_ps({{ cond1 = (Fs1.sf < Ft1.sf); }},
1348                                              {{ cond2 = (Fs2.sf < Ft2.sf); }},
1349                                              UnorderedTrue, QnanException);
1350                                0x6: c_le_ps({{ cond1 = (Fs1.sf <= Ft1.sf); }},
1351                                             {{ cond2 = (Fs2.sf <= Ft2.sf); }},
1352                                             UnorderedFalse, QnanException);
1353                                0x7: c_ngt_ps({{ cond1 = (Fs1.sf <= Ft1.sf); }},
1354                                              {{ cond2 = (Fs2.sf <= Ft2.sf); }},
1355                                              UnorderedTrue, QnanException);
1356                            }
1357                        }
1358                    }
1359                }
1360               default: CP1Unimpl::unknown();
1361            }
1362        }
1363
1364        //Table A-19 MIPS32 COP2 Encoding of rs Field
1365        0x2: decode RS_MSB {
1366            format CP2Unimpl {
1367                0x0: decode RS_HI {
1368                    0x0: decode RS_LO {
1369                        0x0: mfc2();
1370                        0x2: cfc2();
1371                        0x3: mfhc2();
1372                        0x4: mtc2();
1373                        0x6: ctc2();
1374                        0x7: mftc2();
1375                       default: unknown();
1376                    }
1377
1378                    0x1: decode ND {
1379                        0x0: decode TF {
1380                            0x0: bc2f();
1381                            0x1: bc2t();
1382                           default: unknown();
1383                        }
1384
1385                        0x1: decode TF {
1386                            0x0: bc2fl();
1387                            0x1: bc2tl();
1388                           default: unknown();
1389                        }
1390                       default: unknown();
1391
1392                       }
1393              default: unknown();
1394
1395              }
1396            default: unknown();
1397            }
1398        }
1399
1400        //Table A-20 MIPS64 COP1X Encoding of Function Field 1
1401        //Note: "COP1X instructions are legal only if 64-bit floating point
1402        //operations are enabled."
1403        0x3: decode FUNCTION_HI {
1404            0x0: decode FUNCTION_LO {
1405                format LoadIndexedMemory {
1406                    0x0: lwxc1({{ Fd.uw = Mem.uw;}});
1407                    0x1: ldxc1({{ Fd.ud = Mem.ud;}});
1408                    0x5: luxc1({{ Fd.ud = Mem.ud;}},
1409                               {{ EA = (Rs + Rt) & ~7; }});
1410                }
1411            }
1412
1413            0x1: decode FUNCTION_LO {
1414                format StoreIndexedMemory {
1415                    0x0: swxc1({{ Mem.uw = Fs.uw;}});
1416                    0x1: sdxc1({{ Mem.ud = Fs.ud;}});
1417                    0x5: suxc1({{ Mem.ud = Fs.ud;}},
1418                               {{ EA = (Rs + Rt) & ~7; }});
1419                }
1420
1421                0x7: Prefetch::prefx({{ EA = Rs + Rt; }});
1422            }
1423
1424            0x3: decode FUNCTION_LO {
1425                0x6: Float64Op::alnv_ps({{ if (Rs<2:0> == 0) {
1426                                               Fd.ud = Fs.ud;
1427                                           } else if (Rs<2:0> == 4) {
1428                                             #if BYTE_ORDER == BIG_ENDIAN
1429                                               Fd.ud = Fs.ud<31:0> << 32 |
1430                                                       Ft.ud<63:32>;
1431                                             #elif BYTE_ORDER == LITTLE_ENDIAN
1432                                               Fd.ud = Ft.ud<31:0> << 32 |
1433                                                       Fs.ud<63:32>;
1434                                             #endif
1435                                           } else {
1436                                               Fd.ud = Fd.ud;
1437                                           }
1438                                        }});
1439            }
1440
1441            format FloatAccOp {
1442                0x4: decode FUNCTION_LO {
1443                    0x0: madd_s({{ Fd.sf = (Fs.sf * Ft.sf) + Fr.sf; }});
1444                    0x1: madd_d({{ Fd.df = (Fs.df * Ft.df) + Fr.df; }});
1445                    0x6: madd_ps({{
1446                        Fd1.sf = (Fs1.df * Ft1.df) + Fr1.df;
1447                        Fd2.sf = (Fs2.df * Ft2.df) + Fr2.df;
1448                    }});
1449                }
1450
1451                0x5: decode FUNCTION_LO {
1452                    0x0: msub_s({{ Fd.sf = (Fs.sf * Ft.sf) - Fr.sf; }});
1453                    0x1: msub_d({{ Fd.df = (Fs.df * Ft.df) - Fr.df; }});
1454                    0x6: msub_ps({{
1455                        Fd1.sf = (Fs1.df * Ft1.df) - Fr1.df;
1456                        Fd2.sf = (Fs2.df * Ft2.df) - Fr2.df;
1457                    }});
1458                }
1459
1460                0x6: decode FUNCTION_LO {
1461                    0x0: nmadd_s({{ Fd.sf = (-1 * Fs.sf * Ft.sf) - Fr.sf; }});
1462                    0x1: nmadd_d({{ Fd.df = (-1 * Fs.df * Ft.df) + Fr.df; }});
1463                    0x6: nmadd_ps({{
1464                        Fd1.sf = -((Fs1.df * Ft1.df) + Fr1.df);
1465                        Fd2.sf = -((Fs2.df * Ft2.df) + Fr2.df);
1466                    }});
1467                }
1468
1469                0x7: decode FUNCTION_LO {
1470                    0x0: nmsub_s({{ Fd.sf = (-1 * Fs.sf * Ft.sf) - Fr.sf; }});
1471                    0x1: nmsub_d({{ Fd.df = (-1 * Fs.df * Ft.df) - Fr.df; }});
1472                    0x6: nmsub_ps({{
1473                        Fd1.sf = -((Fs1.df * Ft1.df) - Fr1.df);
1474                        Fd2.sf = -((Fs2.df * Ft2.df) - Fr2.df);
1475                    }});
1476                }
1477
1478            }
1479        }
1480
1481        format Branch {
1482            0x4: beql({{ cond = (Rs.sw == Rt.sw); }}, Likely);
1483            0x5: bnel({{ cond = (Rs.sw != Rt.sw); }}, Likely);
1484            0x6: blezl({{ cond = (Rs.sw <= 0); }}, Likely);
1485            0x7: bgtzl({{ cond = (Rs.sw > 0); }}, Likely);
1486        }
1487    }
1488
1489    0x3: decode OPCODE_LO {
1490        //Table A-5 MIPS32 SPECIAL2 Encoding of Function Field
1491        0x4: decode FUNCTION_HI {
1492            0x0: decode FUNCTION_LO {
1493                0x2: IntOp::mul({{ int64_t temp1 = Rs.sd * Rt.sd;
1494                                   Rd.sw = temp1<31:0>;
1495                                }}, IntMultOp);
1496
1497                format HiLoRdSelValOp {
1498                  0x0: madd({{ val = ((int64_t)HI_RD_SEL << 32 | LO_RD_SEL) + (Rs.sd * Rt.sd); }}, IntMultOp);
1499                    0x1: maddu({{ val = ((uint64_t)HI_RD_SEL << 32 | LO_RD_SEL) + (Rs.ud * Rt.ud); }}, IntMultOp);
1500                    0x4: msub({{ val = ((int64_t)HI_RD_SEL << 32 | LO_RD_SEL) - (Rs.sd * Rt.sd); }}, IntMultOp);
1501                    0x5: msubu({{ val = ((uint64_t)HI_RD_SEL << 32 | LO_RD_SEL) - (Rs.ud * Rt.ud); }}, IntMultOp);
1502                }
1503            }
1504
1505            0x4: decode FUNCTION_LO {
1506                format BasicOp {
1507                    0x0: clz({{ int cnt = 32;
1508                          for (int idx = 31; idx >= 0; idx--) {
1509                              if( Rs<idx:idx> == 1) {
1510                                  cnt = 31 - idx;
1511                                  break;
1512                              }
1513                          }
1514                          Rd.uw = cnt;
1515                       }});
1516                    0x1: clo({{ int cnt = 32;
1517                          for (int idx = 31; idx >= 0; idx--) {
1518                              if( Rs<idx:idx> == 0) {
1519                                  cnt = 31 - idx;
1520                                  break;
1521                              }
1522                          }
1523                          Rd.uw = cnt;
1524                        }});
1525                }
1526            }
1527
1528            0x7: decode FUNCTION_LO {
1529                0x7: FailUnimpl::sdbbp();
1530            }
1531        }
1532
1533        //Table A-6 MIPS32 SPECIAL3 Encoding of Function Field for Release 2
1534        //of the Architecture
1535        0x7: decode FUNCTION_HI {
1536            0x0: decode FUNCTION_LO {
1537                format BasicOp {
1538                    0x0: ext({{ Rt.uw = bits(Rs.uw, MSB+LSB, LSB); }});
1539                    0x4: ins({{ Rt.uw = bits(Rt.uw, 31, MSB+1) << (MSB+1) |
1540                                        bits(Rs.uw, MSB-LSB, 0) << LSB |
1541                                        bits(Rt.uw, LSB-1, 0);
1542                             }});
1543                }
1544            }
1545
1546            0x1: decode FUNCTION_LO {
1547                format MT_Control {
1548                    0x0: fork({{ forkThread(xc->tcBase(), fault, RD, Rs, Rt); }},
1549                              UserMode);
1550                    0x1: yield({{ Rd.sw = yieldThread(xc->tcBase(), fault, Rs.sw, YQMask); }},
1551                               UserMode);
1552                }
1553
1554                //Table 5-9 MIPS32 LX Encoding of the op Field (DSP ASE MANUAL)
1555                0x2: decode OP_HI {
1556                    0x0: decode OP_LO {
1557                        format LoadIndexedMemory {
1558                            0x0: lwx({{ Rd.sw = Mem.sw; }});
1559                            0x4: lhx({{ Rd.sw = Mem.sh; }});
1560                            0x6: lbux({{ Rd.uw = Mem.ub; }});
1561                        }
1562                    }
1563                }
1564                0x4: DspIntOp::insv({{ int pos = dspctl<5:0>;
1565                                       int size = dspctl<12:7>-1;
1566                                       Rt.uw = insertBits( Rt.uw, pos+size, pos, Rs.uw<size:0> ); }});
1567            }
1568
1569            0x2: decode FUNCTION_LO {
1570
1571                //Table 5-5 MIPS32 ADDU.QB Encoding of the op Field (DSP ASE MANUAL)
1572                0x0: decode OP_HI {
1573                    0x0: decode OP_LO {
1574                        format DspIntOp {
1575                            0x0: addu_qb({{ Rd.uw = dspAdd( Rs.uw, Rt.uw, SIMD_FMT_QB,
1576                                                            NOSATURATE, UNSIGNED, &dspctl ); }});
1577                            0x1: subu_qb({{ Rd.uw = dspSub( Rs.uw, Rt.uw, SIMD_FMT_QB,
1578                                                            NOSATURATE, UNSIGNED, &dspctl ); }});
1579                            0x4: addu_s_qb({{ Rd.uw = dspAdd( Rs.uw, Rt.uw, SIMD_FMT_QB,
1580                                                              SATURATE, UNSIGNED, &dspctl ); }});
1581                            0x5: subu_s_qb({{ Rd.uw = dspSub( Rs.uw, Rt.uw, SIMD_FMT_QB,
1582                                                              SATURATE, UNSIGNED, &dspctl ); }});
1583                            0x6: muleu_s_ph_qbl({{ Rd.uw = dspMuleu( Rs.uw, Rt.uw,
1584                                                                     MODE_L, &dspctl ); }}, IntMultOp);
1585                            0x7: muleu_s_ph_qbr({{ Rd.uw = dspMuleu( Rs.uw, Rt.uw,
1586                                                                     MODE_R, &dspctl ); }}, IntMultOp);
1587                        }
1588                    }
1589                    0x1: decode OP_LO {
1590                        format DspIntOp {
1591                            0x0: addu_ph({{ Rd.uw = dspAdd( Rs.uw, Rt.uw, SIMD_FMT_PH,
1592                                                            NOSATURATE, UNSIGNED, &dspctl ); }});
1593                            0x1: subu_ph({{ Rd.uw = dspSub( Rs.uw, Rt.uw, SIMD_FMT_PH,
1594                                                            NOSATURATE, UNSIGNED, &dspctl ); }});
1595                            0x2: addq_ph({{ Rd.uw = dspAdd( Rs.uw, Rt.uw, SIMD_FMT_PH,
1596                                                            NOSATURATE, SIGNED, &dspctl ); }});
1597                            0x3: subq_ph({{ Rd.uw = dspSub( Rs.uw, Rt.uw, SIMD_FMT_PH,
1598                                                            NOSATURATE, SIGNED, &dspctl ); }});
1599                            0x4: addu_s_ph({{ Rd.uw = dspAdd( Rs.uw, Rt.uw, SIMD_FMT_PH,
1600                                                              SATURATE, UNSIGNED, &dspctl ); }});
1601                            0x5: subu_s_ph({{ Rd.uw = dspSub( Rs.uw, Rt.uw, SIMD_FMT_PH,
1602                                                              SATURATE, UNSIGNED, &dspctl ); }});
1603                            0x6: addq_s_ph({{ Rd.uw = dspAdd( Rs.uw, Rt.uw, SIMD_FMT_PH,
1604                                                              SATURATE, SIGNED, &dspctl ); }});
1605                            0x7: subq_s_ph({{ Rd.uw = dspSub( Rs.uw, Rt.uw, SIMD_FMT_PH,
1606                                                              SATURATE, SIGNED, &dspctl ); }});
1607                        }
1608                    }
1609                    0x2: decode OP_LO {
1610                        format DspIntOp {
1611                            0x0: addsc({{ int64_t dresult;
1612                                          dresult = Rs.ud + Rt.ud;
1613                                          Rd.sw = dresult<31:0>;
1614                                          dspctl = insertBits( dspctl, 13, 13,
1615                                                               dresult<32:32> ); }});
1616                            0x1: addwc({{ int64_t dresult;
1617                                          dresult = Rs.sd + Rt.sd + dspctl<13:13>;
1618                                          Rd.sw = dresult<31:0>;
1619                                          if( dresult<32:32> != dresult<31:31> )
1620                                              dspctl = insertBits( dspctl, 20, 20, 1 ); }});
1621                            0x2: modsub({{ Rd.sw = (Rs.sw == 0) ? Rt.sw<23:8> : Rs.sw - Rt.sw<7:0>; }});
1622                            0x4: raddu_w_qb({{ Rd.uw = Rs.uw<31:24> + Rs.uw<23:16> +
1623                                                   Rs.uw<15:8> + Rs.uw<7:0>; }});
1624                            0x6: addq_s_w({{ Rd.sw = dspAdd( Rs.sw, Rt.sw, SIMD_FMT_W,
1625                                                             SATURATE, SIGNED, &dspctl ); }});
1626                            0x7: subq_s_w({{ Rd.sw = dspSub( Rs.sw, Rt.sw, SIMD_FMT_W,
1627                                                             SATURATE, SIGNED, &dspctl ); }});
1628                        }
1629                    }
1630                    0x3: decode OP_LO {
1631                        format DspIntOp {
1632                            0x4: muleq_s_w_phl({{ Rd.sw = dspMuleq( Rs.sw, Rt.sw,
1633                                                                    MODE_L, &dspctl ); }}, IntMultOp);
1634                            0x5: muleq_s_w_phr({{ Rd.sw = dspMuleq( Rs.sw, Rt.sw,
1635                                                                    MODE_R, &dspctl ); }}, IntMultOp);
1636                            0x6: mulq_s_ph({{ Rd.sw = dspMulq( Rs.sw, Rt.sw, SIMD_FMT_PH,
1637                                                               SATURATE, NOROUND, &dspctl ); }}, IntMultOp);
1638                            0x7: mulq_rs_ph({{ Rd.sw = dspMulq( Rs.sw, Rt.sw, SIMD_FMT_PH,
1639                                                                SATURATE, ROUND, &dspctl ); }}, IntMultOp);
1640                        }
1641                    }
1642                }
1643
1644                //Table 5-6 MIPS32 CMPU_EQ_QB Encoding of the op Field (DSP ASE MANUAL)
1645                0x1: decode OP_HI {
1646                    0x0: decode OP_LO {
1647                        format DspIntOp {
1648                            0x0: cmpu_eq_qb({{ dspCmp( Rs.uw, Rt.uw, SIMD_FMT_QB,
1649                                                       UNSIGNED, CMP_EQ, &dspctl ); }});
1650                            0x1: cmpu_lt_qb({{ dspCmp( Rs.uw, Rt.uw, SIMD_FMT_QB,
1651                                                       UNSIGNED, CMP_LT, &dspctl ); }});
1652                            0x2: cmpu_le_qb({{ dspCmp( Rs.uw, Rt.uw, SIMD_FMT_QB,
1653                                                       UNSIGNED, CMP_LE, &dspctl ); }});
1654                            0x3: pick_qb({{ Rd.uw = dspPick( Rs.uw, Rt.uw,
1655                                                             SIMD_FMT_QB, &dspctl ); }});
1656                            0x4: cmpgu_eq_qb({{ Rd.uw = dspCmpg( Rs.uw, Rt.uw, SIMD_FMT_QB,
1657                                                                 UNSIGNED, CMP_EQ ); }});
1658                            0x5: cmpgu_lt_qb({{ Rd.uw = dspCmpg( Rs.uw, Rt.uw, SIMD_FMT_QB,
1659                                                                 UNSIGNED, CMP_LT ); }});
1660                            0x6: cmpgu_le_qb({{ Rd.uw = dspCmpg( Rs.uw, Rt.uw, SIMD_FMT_QB,
1661                                                                 UNSIGNED, CMP_LE ); }});
1662                        }
1663                    }
1664                    0x1: decode OP_LO {
1665                        format DspIntOp {
1666                            0x0: cmp_eq_ph({{ dspCmp( Rs.uw, Rt.uw, SIMD_FMT_PH,
1667                                                      SIGNED, CMP_EQ, &dspctl ); }});
1668                            0x1: cmp_lt_ph({{ dspCmp( Rs.uw, Rt.uw, SIMD_FMT_PH,
1669                                                      SIGNED, CMP_LT, &dspctl ); }});
1670                            0x2: cmp_le_ph({{ dspCmp( Rs.uw, Rt.uw, SIMD_FMT_PH,
1671                                                      SIGNED, CMP_LE, &dspctl ); }});
1672                            0x3: pick_ph({{ Rd.uw = dspPick( Rs.uw, Rt.uw,
1673                                                             SIMD_FMT_PH, &dspctl ); }});
1674                            0x4: precrq_qb_ph({{ Rd.uw = Rs.uw<31:24> << 24 |
1675                                                         Rs.uw<15:8> << 16 |
1676                                                         Rt.uw<31:24> << 8 |
1677                                                         Rt.uw<15:8>; }});
1678                            0x5: precr_qb_ph({{ Rd.uw = Rs.uw<23:16> << 24 |
1679                                                         Rs.uw<7:0> << 16 |
1680                                                         Rt.uw<23:16> << 8 |
1681                                                         Rt.uw<7:0>; }});
1682                            0x6: packrl_ph({{ Rd.uw = dspPack( Rs.uw, Rt.uw,
1683                                                               SIMD_FMT_PH ); }});
1684                            0x7: precrqu_s_qb_ph({{ Rd.uw = dspPrecrqu( Rs.uw, Rt.uw, &dspctl ); }});
1685                        }
1686                    }
1687                    0x2: decode OP_LO {
1688                        format DspIntOp {
1689                            0x4: precrq_ph_w({{ Rd.uw = Rs.uw<31:16> << 16 | Rt.uw<31:16>; }});
1690                            0x5: precrq_rs_ph_w({{ Rd.uw = dspPrecrq( Rs.uw, Rt.uw, SIMD_FMT_W, &dspctl ); }});
1691                        }
1692                    }
1693                    0x3: decode OP_LO {
1694                        format DspIntOp {
1695                            0x0: cmpgdu_eq_qb({{ Rd.uw = dspCmpgd( Rs.uw, Rt.uw, SIMD_FMT_QB,
1696                                                                   UNSIGNED, CMP_EQ, &dspctl ); }});
1697                            0x1: cmpgdu_lt_qb({{ Rd.uw = dspCmpgd( Rs.uw, Rt.uw, SIMD_FMT_QB,
1698                                                                   UNSIGNED, CMP_LT, &dspctl  ); }});
1699                            0x2: cmpgdu_le_qb({{ Rd.uw = dspCmpgd( Rs.uw, Rt.uw, SIMD_FMT_QB,
1700                                                                   UNSIGNED, CMP_LE, &dspctl ); }});
1701                            0x6: precr_sra_ph_w({{ Rt.uw = dspPrecrSra( Rt.uw, Rs.uw, RD,
1702                                                                        SIMD_FMT_W, NOROUND ); }});
1703                            0x7: precr_sra_r_ph_w({{ Rt.uw = dspPrecrSra( Rt.uw, Rs.uw, RD,
1704                                                                        SIMD_FMT_W, ROUND ); }});
1705                        }
1706                    }
1707                }
1708
1709                //Table 5-7 MIPS32 ABSQ_S.PH Encoding of the op Field (DSP ASE MANUAL)
1710                0x2: decode OP_HI {
1711                    0x0: decode OP_LO {
1712                        format DspIntOp {
1713                            0x1: absq_s_qb({{ Rd.sw = dspAbs( Rt.sw, SIMD_FMT_QB, &dspctl );}});
1714                            0x2: repl_qb({{ Rd.uw = RS_RT<7:0> << 24 |
1715                                                    RS_RT<7:0> << 16 |
1716                                                    RS_RT<7:0> << 8 |
1717                                                    RS_RT<7:0>; }});
1718                            0x3: replv_qb({{ Rd.sw = Rt.uw<7:0> << 24 |
1719                                                     Rt.uw<7:0> << 16 |
1720                                                     Rt.uw<7:0> << 8 |
1721                                                     Rt.uw<7:0>; }});
1722                            0x4: precequ_ph_qbl({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1723                                                                     SIMD_FMT_PH, SIGNED, MODE_L ); }});
1724                            0x5: precequ_ph_qbr({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1725                                                                     SIMD_FMT_PH, SIGNED, MODE_R ); }});
1726                            0x6: precequ_ph_qbla({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1727                                                                      SIMD_FMT_PH, SIGNED, MODE_LA ); }});
1728                            0x7: precequ_ph_qbra({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1729                                                                      SIMD_FMT_PH, SIGNED, MODE_RA ); }});
1730                        }
1731                    }
1732                    0x1: decode OP_LO {
1733                        format DspIntOp {
1734                            0x1: absq_s_ph({{ Rd.sw = dspAbs( Rt.sw, SIMD_FMT_PH, &dspctl ); }});
1735                            0x2: repl_ph({{ Rd.uw = (sext<10>(RS_RT))<15:0> << 16 |
1736                                                    (sext<10>(RS_RT))<15:0>; }});
1737                            0x3: replv_ph({{ Rd.uw = Rt.uw<15:0> << 16 |
1738                                                     Rt.uw<15:0>; }});
1739                            0x4: preceq_w_phl({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_PH, SIGNED,
1740                                                                   SIMD_FMT_W, SIGNED, MODE_L ); }});
1741                            0x5: preceq_w_phr({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_PH, SIGNED,
1742                                                                   SIMD_FMT_W, SIGNED, MODE_R ); }});
1743                        }
1744                    }
1745                    0x2: decode OP_LO {
1746                        format DspIntOp {
1747                            0x1: absq_s_w({{ Rd.sw = dspAbs( Rt.sw, SIMD_FMT_W, &dspctl ); }});
1748                        }
1749                    }
1750                    0x3: decode OP_LO {
1751                        0x3: IntOp::bitrev({{ Rd.uw = bitrev( Rt.uw<15:0> ); }});
1752                        format DspIntOp {
1753                            0x4: preceu_ph_qbl({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1754                                                                    SIMD_FMT_PH, UNSIGNED, MODE_L ); }});
1755                            0x5: preceu_ph_qbr({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1756                                                                    SIMD_FMT_PH, UNSIGNED, MODE_R ); }});
1757                            0x6: preceu_ph_qbla({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1758                                                                     SIMD_FMT_PH, UNSIGNED, MODE_LA ); }});
1759                            0x7: preceu_ph_qbra({{ Rd.uw = dspPrece( Rt.uw, SIMD_FMT_QB, UNSIGNED,
1760                                                                     SIMD_FMT_PH, UNSIGNED, MODE_RA ); }});
1761                        }
1762                    }
1763                }
1764
1765                //Table 5-8 MIPS32 SHLL.QB Encoding of the op Field (DSP ASE MANUAL)
1766                0x3: decode OP_HI {
1767                    0x0: decode OP_LO {
1768                        format DspIntOp {
1769                            0x0: shll_qb({{ Rd.sw = dspShll( Rt.sw, RS, SIMD_FMT_QB,
1770                                                             NOSATURATE, UNSIGNED, &dspctl ); }});
1771                            0x1: shrl_qb({{ Rd.sw = dspShrl( Rt.sw, RS, SIMD_FMT_QB,
1772                                                             UNSIGNED ); }});
1773                            0x2: shllv_qb({{ Rd.sw = dspShll( Rt.sw, Rs.sw, SIMD_FMT_QB,
1774                                                              NOSATURATE, UNSIGNED, &dspctl ); }});
1775                            0x3: shrlv_qb({{ Rd.sw = dspShrl( Rt.sw, Rs.sw, SIMD_FMT_QB,
1776                                                              UNSIGNED ); }});
1777                            0x4: shra_qb({{ Rd.sw = dspShra( Rt.sw, RS, SIMD_FMT_QB,
1778                                                             NOROUND, SIGNED, &dspctl ); }});
1779                            0x5: shra_r_qb({{ Rd.sw = dspShra( Rt.sw, RS, SIMD_FMT_QB,
1780                                                               ROUND, SIGNED, &dspctl ); }});
1781                            0x6: shrav_qb({{ Rd.sw = dspShra( Rt.sw, Rs.sw, SIMD_FMT_QB,
1782                                                              NOROUND, SIGNED, &dspctl ); }});
1783                            0x7: shrav_r_qb({{ Rd.sw = dspShra( Rt.sw, Rs.sw, SIMD_FMT_QB,
1784                                                                ROUND, SIGNED, &dspctl ); }});
1785                        }
1786                    }
1787                    0x1: decode OP_LO {
1788                        format DspIntOp {
1789                            0x0: shll_ph({{ Rd.uw = dspShll( Rt.uw, RS, SIMD_FMT_PH,
1790                                                             NOSATURATE, SIGNED, &dspctl ); }});
1791                            0x1: shra_ph({{ Rd.sw = dspShra( Rt.sw, RS, SIMD_FMT_PH,
1792                                                             NOROUND, SIGNED, &dspctl ); }});
1793                            0x2: shllv_ph({{ Rd.sw = dspShll( Rt.sw, Rs.sw, SIMD_FMT_PH,
1794                                                              NOSATURATE, SIGNED, &dspctl ); }});
1795                            0x3: shrav_ph({{ Rd.sw = dspShra( Rt.sw, Rs.sw, SIMD_FMT_PH,
1796                                                              NOROUND, SIGNED, &dspctl ); }});
1797                            0x4: shll_s_ph({{ Rd.sw = dspShll( Rt.sw, RS, SIMD_FMT_PH,
1798                                                               SATURATE, SIGNED, &dspctl ); }});
1799                            0x5: shra_r_ph({{ Rd.sw = dspShra( Rt.sw, RS, SIMD_FMT_PH,
1800                                                               ROUND, SIGNED, &dspctl ); }});
1801                            0x6: shllv_s_ph({{ Rd.sw = dspShll( Rt.sw, Rs.sw, SIMD_FMT_PH,
1802                                                                SATURATE, SIGNED, &dspctl ); }});
1803                            0x7: shrav_r_ph({{ Rd.sw = dspShra( Rt.sw, Rs.sw, SIMD_FMT_PH,
1804                                                                ROUND, SIGNED, &dspctl ); }});
1805                        }
1806                    }
1807                    0x2: decode OP_LO {
1808                        format DspIntOp {
1809                            0x4: shll_s_w({{ Rd.sw = dspShll( Rt.sw, RS, SIMD_FMT_W,
1810                                                              SATURATE, SIGNED, &dspctl ); }});
1811                            0x5: shra_r_w({{ Rd.sw = dspShra( Rt.sw, RS, SIMD_FMT_W,
1812                                                              ROUND, SIGNED, &dspctl ); }});
1813                            0x6: shllv_s_w({{ Rd.sw = dspShll( Rt.sw, Rs.sw, SIMD_FMT_W,
1814                                                               SATURATE, SIGNED, &dspctl ); }});
1815                            0x7: shrav_r_w({{ Rd.sw = dspShra( Rt.sw, Rs.sw, SIMD_FMT_W,
1816                                                               ROUND, SIGNED, &dspctl ); }});
1817                        }
1818                    }
1819                    0x3: decode OP_LO {
1820                        format DspIntOp {
1821                            0x1: shrl_ph({{ Rd.sw = dspShrl( Rt.sw, RS, SIMD_FMT_PH,
1822                                                             UNSIGNED ); }});
1823                            0x3: shrlv_ph({{ Rd.sw = dspShrl( Rt.sw, Rs.sw, SIMD_FMT_PH,
1824                                                              UNSIGNED ); }});
1825                        }
1826                    }
1827                }
1828            }
1829
1830            0x3: decode FUNCTION_LO {
1831
1832                //Table 3.12 MIPS32 ADDUH.QB Encoding of the op Field (DSP ASE Rev2 Manual)
1833                0x0: decode OP_HI {
1834                    0x0: decode OP_LO {
1835                        format DspIntOp {
1836                            0x0: adduh_qb({{ Rd.uw = dspAddh( Rs.sw, Rt.sw, SIMD_FMT_QB,
1837                                                              NOROUND, UNSIGNED ); }});
1838                            0x1: subuh_qb({{ Rd.uw = dspSubh( Rs.sw, Rt.sw, SIMD_FMT_QB,
1839                                                              NOROUND, UNSIGNED ); }});
1840                            0x2: adduh_r_qb({{ Rd.uw = dspAddh( Rs.sw, Rt.sw, SIMD_FMT_QB,
1841                                                                ROUND, UNSIGNED ); }});
1842                            0x3: subuh_r_qb({{ Rd.uw = dspSubh( Rs.sw, Rt.sw, SIMD_FMT_QB,
1843                                                                ROUND, UNSIGNED ); }});
1844                        }
1845                    }
1846                    0x1: decode OP_LO {
1847                        format DspIntOp {
1848                            0x0: addqh_ph({{ Rd.uw = dspAddh( Rs.sw, Rt.sw, SIMD_FMT_PH,
1849                                                              NOROUND, SIGNED ); }});
1850                            0x1: subqh_ph({{ Rd.uw = dspSubh( Rs.sw, Rt.sw, SIMD_FMT_PH,
1851                                                              NOROUND, SIGNED ); }});
1852                            0x2: addqh_r_ph({{ Rd.uw = dspAddh( Rs.sw, Rt.sw, SIMD_FMT_PH,
1853                                                                ROUND, SIGNED ); }});
1854                            0x3: subqh_r_ph({{ Rd.uw = dspSubh( Rs.sw, Rt.sw, SIMD_FMT_PH,
1855                                                                ROUND, SIGNED ); }});
1856                            0x4: mul_ph({{ Rd.sw = dspMul( Rs.sw, Rt.sw, SIMD_FMT_PH,
1857                                                           NOSATURATE, &dspctl ); }}, IntMultOp);
1858                            0x6: mul_s_ph({{ Rd.sw = dspMul( Rs.sw, Rt.sw, SIMD_FMT_PH,
1859                                                             SATURATE, &dspctl ); }}, IntMultOp);
1860
1861                        }
1862                    }
1863                    0x2: decode OP_LO {
1864                        format DspIntOp {
1865                            0x0: addqh_w({{ Rd.uw = dspAddh( Rs.sw, Rt.sw, SIMD_FMT_W,
1866                                                             NOROUND, SIGNED ); }});
1867                            0x1: subqh_w({{ Rd.uw = dspSubh( Rs.sw, Rt.sw, SIMD_FMT_W,
1868                                                             NOROUND, SIGNED ); }});
1869                            0x2: addqh_r_w({{ Rd.uw = dspAddh( Rs.sw, Rt.sw, SIMD_FMT_W,
1870                                                               ROUND, SIGNED ); }});
1871                            0x3: subqh_r_w({{ Rd.uw = dspSubh( Rs.sw, Rt.sw, SIMD_FMT_W,
1872                                                               ROUND, SIGNED ); }});
1873                            0x6: mulq_s_w({{ Rd.sw = dspMulq( Rs.sw, Rt.sw, SIMD_FMT_W,
1874                                                              SATURATE, NOROUND, &dspctl ); }}, IntMultOp);
1875                            0x7: mulq_rs_w({{ Rd.sw = dspMulq( Rs.sw, Rt.sw, SIMD_FMT_W,
1876                                                               SATURATE, ROUND, &dspctl ); }}, IntMultOp);
1877                        }
1878                    }
1879                }
1880            }
1881
1882            //Table A-10 MIPS32 BSHFL Encoding of sa Field
1883            0x4: decode SA {
1884                format BasicOp {
1885                    0x02: wsbh({{ Rd.uw = Rt.uw<23:16> << 24 |
1886                                      Rt.uw<31:24> << 16 |
1887                                      Rt.uw<7:0>   << 8  |
1888                                      Rt.uw<15:8>;
1889                    }});
1890                    0x10: seb({{ Rd.sw = Rt.sb; }});
1891                    0x18: seh({{ Rd.sw = Rt.sh; }});
1892                }
1893            }
1894
1895            0x6: decode FUNCTION_LO {
1896
1897                //Table 5-10 MIPS32 DPAQ.W.PH Encoding of the op Field (DSP ASE MANUAL)
1898                0x0: decode OP_HI {
1899                    0x0: decode OP_LO {
1900                        format DspHiLoOp {
1901                            0x0: dpa_w_ph({{ dspac = dspDpa( dspac, Rs.sw, Rt.sw, ACDST,
1902                                                             SIMD_FMT_PH, SIGNED, MODE_L ); }}, IntMultOp);
1903                            0x1: dps_w_ph({{ dspac = dspDps( dspac, Rs.sw, Rt.sw, ACDST,
1904                                                             SIMD_FMT_PH, SIGNED, MODE_L ); }}, IntMultOp);
1905                            0x2: mulsa_w_ph({{ dspac = dspMulsa( dspac, Rs.sw, Rt.sw,
1906                                                                 ACDST, SIMD_FMT_PH ); }}, IntMultOp);
1907                            0x3: dpau_h_qbl({{ dspac = dspDpa( dspac, Rs.sw, Rt.sw, ACDST,
1908                                                               SIMD_FMT_QB, UNSIGNED, MODE_L ); }}, IntMultOp);
1909                            0x4: dpaq_s_w_ph({{ dspac = dspDpaq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_PH,
1910                                                                 SIMD_FMT_W, NOSATURATE, MODE_L, &dspctl ); }}, IntMultOp);
1911                            0x5: dpsq_s_w_ph({{ dspac = dspDpsq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_PH,
1912                                                                 SIMD_FMT_W, NOSATURATE, MODE_L, &dspctl ); }}, IntMultOp);
1913                            0x6: mulsaq_s_w_ph({{ dspac = dspMulsaq( dspac, Rs.sw, Rt.sw,
1914                                                                     ACDST, SIMD_FMT_PH, &dspctl ); }}, IntMultOp);
1915                            0x7: dpau_h_qbr({{ dspac = dspDpa( dspac, Rs.sw, Rt.sw, ACDST,
1916                                                               SIMD_FMT_QB, UNSIGNED, MODE_R ); }}, IntMultOp);
1917                        }
1918                    }
1919                    0x1: decode OP_LO {
1920                        format DspHiLoOp {
1921                            0x0: dpax_w_ph({{ dspac = dspDpa( dspac, Rs.sw, Rt.sw, ACDST,
1922                                                              SIMD_FMT_PH, SIGNED, MODE_X ); }}, IntMultOp);
1923                            0x1: dpsx_w_ph({{ dspac = dspDps( dspac, Rs.sw, Rt.sw, ACDST,
1924                                                              SIMD_FMT_PH, SIGNED, MODE_X ); }}, IntMultOp);
1925                            0x3: dpsu_h_qbl({{ dspac = dspDps( dspac, Rs.sw, Rt.sw, ACDST,
1926                                                               SIMD_FMT_QB, UNSIGNED, MODE_L ); }}, IntMultOp);
1927                            0x4: dpaq_sa_l_w({{ dspac = dspDpaq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_W,
1928                                                                 SIMD_FMT_L, SATURATE, MODE_L, &dspctl ); }}, IntMultOp);
1929                            0x5: dpsq_sa_l_w({{ dspac = dspDpsq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_W,
1930                                                                 SIMD_FMT_L, SATURATE, MODE_L, &dspctl ); }}, IntMultOp);
1931                            0x7: dpsu_h_qbr({{ dspac = dspDps( dspac, Rs.sw, Rt.sw, ACDST,
1932                                                               SIMD_FMT_QB, UNSIGNED, MODE_R ); }}, IntMultOp);
1933                        }
1934                    }
1935                    0x2: decode OP_LO {
1936                        format DspHiLoOp {
1937                            0x0: maq_sa_w_phl({{ dspac = dspMaq( dspac, Rs.uw, Rt.uw, ACDST, SIMD_FMT_PH,
1938                                                                 MODE_L, SATURATE, &dspctl ); }}, IntMultOp);
1939                            0x2: maq_sa_w_phr({{ dspac = dspMaq( dspac, Rs.uw, Rt.uw, ACDST, SIMD_FMT_PH,
1940                                                                 MODE_R, SATURATE, &dspctl ); }}, IntMultOp);
1941                            0x4: maq_s_w_phl({{ dspac = dspMaq( dspac, Rs.uw, Rt.uw, ACDST, SIMD_FMT_PH,
1942                                                                 MODE_L, NOSATURATE, &dspctl ); }}, IntMultOp);
1943                            0x6: maq_s_w_phr({{ dspac = dspMaq( dspac, Rs.uw, Rt.uw, ACDST, SIMD_FMT_PH,
1944                                                                 MODE_R, NOSATURATE, &dspctl ); }}, IntMultOp);
1945                        }
1946                    }
1947                    0x3: decode OP_LO {
1948                        format DspHiLoOp {
1949                            0x0: dpaqx_s_w_ph({{ dspac = dspDpaq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_PH,
1950                                                                  SIMD_FMT_W, NOSATURATE, MODE_X, &dspctl ); }}, IntMultOp);
1951                            0x1: dpsqx_s_w_ph({{ dspac = dspDpsq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_PH,
1952                                                                  SIMD_FMT_W, NOSATURATE, MODE_X, &dspctl ); }}, IntMultOp);
1953                            0x2: dpaqx_sa_w_ph({{ dspac = dspDpaq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_PH,
1954                                                                   SIMD_FMT_W, SATURATE, MODE_X, &dspctl ); }}, IntMultOp);
1955                            0x3: dpsqx_sa_w_ph({{ dspac = dspDpsq( dspac, Rs.sw, Rt.sw, ACDST, SIMD_FMT_PH,
1956                                                                   SIMD_FMT_W, SATURATE, MODE_X, &dspctl ); }}, IntMultOp);
1957                        }
1958                    }
1959                }
1960
1961                //Table 3.3 MIPS32 APPEND Encoding of the op Field
1962                0x1: decode OP_HI {
1963                    0x0: decode OP_LO {
1964                        format IntOp {
1965                            0x0: append({{ Rt.uw = (Rt.uw << RD) | bits(Rs.uw,RD-1,0); }});
1966                            0x1: prepend({{ Rt.uw = (Rt.uw >> RD) | (bits(Rs.uw, RD - 1, 0) << (32 - RD)); }});
1967                        }
1968                    }
1969                    0x2: decode OP_LO {
1970                        format IntOp {
1971                            0x0: balign({{ Rt.uw = (Rt.uw << (8*BP)) | (Rs.uw >> (8*(4-BP))); }});
1972                        }
1973                    }
1974                }
1975
1976            }
1977            0x7: decode FUNCTION_LO {
1978
1979                //Table 5-11 MIPS32 EXTR.W Encoding of the op Field (DSP ASE MANUAL)
1980                0x0: decode OP_HI {
1981                    0x0: decode OP_LO {
1982                        format DspHiLoOp {
1983                            0x0: extr_w({{ Rt.uw = dspExtr( dspac, SIMD_FMT_W, RS,
1984                                                            NOROUND, NOSATURATE, &dspctl ); }});
1985                            0x1: extrv_w({{ Rt.uw = dspExtr( dspac, SIMD_FMT_W, Rs.uw,
1986                                                             NOROUND, NOSATURATE, &dspctl ); }});
1987                            0x2: extp({{ Rt.uw = dspExtp( dspac, RS, &dspctl ); }});
1988                            0x3: extpv({{ Rt.uw = dspExtp( dspac, Rs.uw, &dspctl ); }});
1989                            0x4: extr_r_w({{ Rt.uw = dspExtr( dspac, SIMD_FMT_W, RS,
1990                                                              ROUND, NOSATURATE, &dspctl ); }});
1991                            0x5: extrv_r_w({{ Rt.uw = dspExtr( dspac, SIMD_FMT_W, Rs.uw,
1992                                                               ROUND, NOSATURATE, &dspctl ); }});
1993                            0x6: extr_rs_w({{ Rt.uw = dspExtr( dspac, SIMD_FMT_W, RS,
1994                                                               ROUND, SATURATE, &dspctl ); }});
1995                            0x7: extrv_rs_w({{ Rt.uw = dspExtr( dspac, SIMD_FMT_W, Rs.uw,
1996                                                                ROUND, SATURATE, &dspctl ); }});
1997                        }
1998                    }
1999                    0x1: decode OP_LO {
2000                        format DspHiLoOp {
2001                            0x2: extpdp({{ Rt.uw = dspExtpd( dspac, RS, &dspctl ); }});
2002                            0x3: extpdpv({{ Rt.uw = dspExtpd( dspac, Rs.uw, &dspctl ); }});
2003                            0x6: extr_s_h({{ Rt.uw = dspExtr( dspac, SIMD_FMT_PH, RS,
2004                                                              NOROUND, SATURATE, &dspctl ); }});
2005                            0x7: extrv_s_h({{ Rt.uw = dspExtr( dspac, SIMD_FMT_PH, Rs.uw,
2006                                                               NOROUND, SATURATE, &dspctl ); }});
2007                        }
2008                    }
2009                    0x2: decode OP_LO {
2010                        format DspIntOp {
2011                            0x2: rddsp({{ Rd.uw = readDSPControl( &dspctl, RDDSPMASK ); }});
2012                            0x3: wrdsp({{ writeDSPControl( &dspctl, Rs.uw, WRDSPMASK ); }});
2013                        }
2014                    }
2015                    0x3: decode OP_LO {
2016                        format DspHiLoOp {
2017                            0x2: shilo({{ if( sext<6>(HILOSA) < 0 )
2018                                              dspac = (uint64_t)dspac << -sext<6>(HILOSA);
2019                                          else
2020                                              dspac = (uint64_t)dspac >> sext<6>(HILOSA); }});
2021                            0x3: shilov({{ if( sext<6>(Rs.sw<5:0>) < 0 )
2022                                              dspac = (uint64_t)dspac << -sext<6>(Rs.sw<5:0>);
2023                                           else
2024                                              dspac = (uint64_t)dspac >> sext<6>(Rs.sw<5:0>); }});
2025                            0x7: mthlip({{ dspac = dspac << 32;
2026                                           dspac |= Rs.uw;
2027                                           dspctl = insertBits( dspctl, 5, 0,
2028                                                                dspctl<5:0>+32 ); }});
2029                        }
2030                    }
2031                }
2032                0x3: decode OP_HI {
2033                    0x2: decode OP_LO {
2034                        0x3: FailUnimpl::rdhwr();
2035                    }
2036                }
2037            }
2038        }
2039    }
2040
2041    0x4: decode OPCODE_LO {
2042        format LoadMemory {
2043          0x0: lb({{ Rt.sw = Mem.sb; }}, mem_flags = NO_ALIGN_FAULT);
2044          0x1: lh({{ Rt.sw = Mem.sh; }}, mem_flags = NO_HALF_WORD_ALIGN_FAULT);
2045            0x3: lw({{ Rt.sw = Mem.sw; }});
2046            0x4: lbu({{ Rt.uw = Mem.ub;}}, mem_flags = NO_ALIGN_FAULT);
2047            0x5: lhu({{ Rt.uw = Mem.uh; }}, mem_flags = NO_HALF_WORD_ALIGN_FAULT);
2048        }
2049
2050        format LoadUnalignedMemory {
2051            0x2: lwl({{ uint32_t mem_shift = 24 - (8 * byte_offset);
2052                        Rt.uw = mem_word << mem_shift |
2053                            (Rt.uw & mask(mem_shift));
2054                     }});
2055            0x6: lwr({{ uint32_t mem_shift = 8 * byte_offset;
2056                        Rt.uw = (Rt.uw & (mask(mem_shift) << (32 - mem_shift))) |
2057                            (mem_word >> mem_shift);
2058                     }});
2059      }
2060    }
2061
2062    0x5: decode OPCODE_LO {
2063        format StoreMemory {
2064            0x0: sb({{ Mem.ub = Rt<7:0>; }}, mem_flags = NO_ALIGN_FAULT);
2065            0x1: sh({{ Mem.uh = Rt<15:0>; }}, mem_flags = NO_HALF_WORD_ALIGN_FAULT);
2066            0x3: sw({{ Mem.uw = Rt<31:0>; }});
2067        }
2068
2069        format StoreUnalignedMemory {
2070            0x2: swl({{ uint32_t reg_shift = 24 - (8 * byte_offset);
2071                        uint32_t mem_shift = 32 - reg_shift;
2072                        mem_word = (mem_word & (mask(reg_shift) << mem_shift)) |
2073                            (Rt.uw >> reg_shift);
2074                     }});
2075            0x6: swr({{ uint32_t reg_shift = 8 * byte_offset;
2076                        mem_word = Rt.uw << reg_shift |
2077                            (mem_word & (mask(reg_shift)));
2078                     }});
2079        }
2080        format CP0Control {
2081            0x7: cache({{
2082                //Addr CacheEA = Rs.uw + OFFSET;
2083                           //fault = xc->CacheOp((uint8_t)CACHE_OP,(Addr) CacheEA);
2084                         }});
2085        }
2086    }
2087
2088    0x6: decode OPCODE_LO {
2089        format LoadMemory {
2090            0x0: ll({{ Rt.uw = Mem.uw; }}, mem_flags=LOCKED);
2091            0x1: lwc1({{ Ft.uw = Mem.uw; }});
2092            0x5: ldc1({{ Ft.ud = Mem.ud; }});
2093        }
2094        0x2: CP2Unimpl::lwc2();
2095        0x6: CP2Unimpl::ldc2();
2096        0x3: Prefetch::pref();
2097    }
2098
2099
2100    0x7: decode OPCODE_LO {
2101        0x0: StoreCond::sc({{ Mem.uw = Rt.uw;}},
2102                           {{ uint64_t tmp = write_result;
2103                              Rt.uw = (tmp == 0 || tmp == 1) ? tmp : Rt.uw;
2104                           }}, mem_flags=LOCKED, inst_flags = IsStoreConditional);
2105
2106        format StoreMemory {
2107          0x1: swc1({{ Mem.uw = Ft.uw;}});
2108          0x5: sdc1({{ Mem.ud = Ft.ud;}});
2109        }
2110
2111        0x2: CP2Unimpl::swc2();
2112        0x6: CP2Unimpl::sdc2();
2113
2114    }
2115}
2116
2117
2118