data64.isa revision 12258
110037SARM gem5 Developers// -*- mode:c++ -*-
210037SARM gem5 Developers
311582SDylan.Johnson@ARM.com// Copyright (c) 2011-2013, 2016 ARM Limited
410037SARM gem5 Developers// All rights reserved
510037SARM gem5 Developers//
610037SARM gem5 Developers// The license below extends only to copyright in the software and shall
710037SARM gem5 Developers// not be construed as granting a license to any other intellectual
810037SARM gem5 Developers// property including but not limited to intellectual property relating
910037SARM gem5 Developers// to a hardware implementation of the functionality of the software
1010037SARM gem5 Developers// licensed hereunder.  You may use the software subject to the license
1110037SARM gem5 Developers// terms below provided that you ensure that this notice is replicated
1210037SARM gem5 Developers// unmodified and in its entirety in all distributions of the software,
1310037SARM gem5 Developers// modified or unmodified, in source code or in binary form.
1410037SARM gem5 Developers//
1510037SARM gem5 Developers// Redistribution and use in source and binary forms, with or without
1610037SARM gem5 Developers// modification, are permitted provided that the following conditions are
1710037SARM gem5 Developers// met: redistributions of source code must retain the above copyright
1810037SARM gem5 Developers// notice, this list of conditions and the following disclaimer;
1910037SARM gem5 Developers// redistributions in binary form must reproduce the above copyright
2010037SARM gem5 Developers// notice, this list of conditions and the following disclaimer in the
2110037SARM gem5 Developers// documentation and/or other materials provided with the distribution;
2210037SARM gem5 Developers// neither the name of the copyright holders nor the names of its
2310037SARM gem5 Developers// contributors may be used to endorse or promote products derived from
2410037SARM gem5 Developers// this software without specific prior written permission.
2510037SARM gem5 Developers//
2610037SARM gem5 Developers// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2710037SARM gem5 Developers// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2810037SARM gem5 Developers// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2910037SARM gem5 Developers// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3010037SARM gem5 Developers// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3110037SARM gem5 Developers// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3210037SARM gem5 Developers// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3310037SARM gem5 Developers// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3410037SARM gem5 Developers// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3510037SARM gem5 Developers// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3610037SARM gem5 Developers// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3710037SARM gem5 Developers//
3810037SARM gem5 Developers// Authors: Gabe Black
3910037SARM gem5 Developers
4010037SARM gem5 Developerslet {{
4110037SARM gem5 Developers
4210037SARM gem5 Developers    header_output = ""
4310037SARM gem5 Developers    decoder_output = ""
4410037SARM gem5 Developers    exec_output = ""
4510037SARM gem5 Developers
4610037SARM gem5 Developers    def createCcCode64(carry, overflow):
4710037SARM gem5 Developers        code = ""
4810037SARM gem5 Developers        code += '''
4910037SARM gem5 Developers            uint16_t _iz, _in;
5010037SARM gem5 Developers            _in = bits(resTemp, intWidth - 1);
5110037SARM gem5 Developers            _iz = ((resTemp & mask(intWidth)) == 0);
5210037SARM gem5 Developers            CondCodesNZ = (_in << 1) | _iz;
5311862Snikos.nikoleris@arm.com            DPRINTF(Arm, "(in, iz) = (%d, %d)\\n", _in, _iz);
5410037SARM gem5 Developers        '''
5510037SARM gem5 Developers        if overflow and overflow != "none":
5610037SARM gem5 Developers            code +=  '''
5710037SARM gem5 Developers                uint16_t _iv;
5810037SARM gem5 Developers                _iv = %s & 1;
5910037SARM gem5 Developers                CondCodesV = _iv;
6010037SARM gem5 Developers                DPRINTF(Arm, "(iv) = (%%d)\\n", _iv);
6110037SARM gem5 Developers            ''' % overflow
6210037SARM gem5 Developers        if carry and carry != "none":
6310037SARM gem5 Developers            code += '''
6410037SARM gem5 Developers                uint16_t _ic;
6510037SARM gem5 Developers                _ic = %s & 1;
6610037SARM gem5 Developers                CondCodesC = _ic;
6710037SARM gem5 Developers                DPRINTF(Arm, "(ic) = (%%d)\\n", _ic);
6810037SARM gem5 Developers            ''' % carry
6910037SARM gem5 Developers        return code
7010037SARM gem5 Developers
7110037SARM gem5 Developers    oldC = 'CondCodesC'
7210037SARM gem5 Developers    oldV = 'CondCodesV'
7310037SARM gem5 Developers    # Dicts of ways to set the carry flag.
7410037SARM gem5 Developers    carryCode64 = {
7510037SARM gem5 Developers        "none": "none",
7610037SARM gem5 Developers        "add": 'findCarry(intWidth, resTemp, Op164, secOp)',
7710037SARM gem5 Developers        "sub": 'findCarry(intWidth, resTemp, Op164, ~secOp)',
7810037SARM gem5 Developers        "logic": '0'
7910037SARM gem5 Developers    }
8010037SARM gem5 Developers    # Dict of ways to set the overflow flag.
8110037SARM gem5 Developers    overflowCode64 = {
8210037SARM gem5 Developers        "none": "none",
8310037SARM gem5 Developers        "add": 'findOverflow(intWidth, resTemp, Op164, secOp)',
8410037SARM gem5 Developers        "sub": 'findOverflow(intWidth, resTemp, Op164, ~secOp)',
8510037SARM gem5 Developers        "logic": '0'
8610037SARM gem5 Developers    }
8710037SARM gem5 Developers
8810037SARM gem5 Developers    immOp2 = "uint64_t secOp M5_VAR_USED = imm;"
8910037SARM gem5 Developers    sRegOp2 = "uint64_t secOp M5_VAR_USED = " + \
9010037SARM gem5 Developers              "shiftReg64(Op264, shiftAmt, shiftType, intWidth);"
9110037SARM gem5 Developers    eRegOp2 = "uint64_t secOp M5_VAR_USED = " + \
9210037SARM gem5 Developers              "extendReg64(Op264, extendType, shiftAmt, intWidth);"
9310037SARM gem5 Developers
9410037SARM gem5 Developers    def buildDataWork(mnem, code, flagType, suffix, buildCc, buildNonCc,
9510037SARM gem5 Developers                      base, templateBase):
9610037SARM gem5 Developers        code = '''
9710037SARM gem5 Developers        uint64_t resTemp M5_VAR_USED = 0;
9810037SARM gem5 Developers        ''' + code
9910037SARM gem5 Developers        ccCode = createCcCode64(carryCode64[flagType], overflowCode64[flagType])
10010037SARM gem5 Developers        Name = mnem.capitalize() + suffix
10110037SARM gem5 Developers        iop = InstObjParams(mnem, Name, base, code)
10210037SARM gem5 Developers        iopCc = InstObjParams(mnem + "s", Name + "Cc", base, code + ccCode)
10310037SARM gem5 Developers
10410037SARM gem5 Developers        def subst(iop):
10510037SARM gem5 Developers            global header_output, decoder_output, exec_output
10610037SARM gem5 Developers            header_output += eval(templateBase + "Declare").subst(iop)
10710037SARM gem5 Developers            decoder_output += eval(templateBase + "Constructor").subst(iop)
10810037SARM gem5 Developers            exec_output += BasicExecute.subst(iop)
10910037SARM gem5 Developers
11010037SARM gem5 Developers        if buildNonCc:
11110037SARM gem5 Developers            subst(iop)
11210037SARM gem5 Developers        if buildCc:
11310037SARM gem5 Developers            subst(iopCc)
11410037SARM gem5 Developers
11510037SARM gem5 Developers    def buildXImmDataInst(mnem, code, flagType = "logic", \
11610037SARM gem5 Developers                          buildCc = True, buildNonCc = True, \
11710037SARM gem5 Developers                          suffix = "XImm"):
11810037SARM gem5 Developers        buildDataWork(mnem, immOp2 + code, flagType, suffix,
11910037SARM gem5 Developers                      buildCc, buildNonCc, "DataXImmOp", "DataXImm")
12010037SARM gem5 Developers
12110037SARM gem5 Developers    def buildXSRegDataInst(mnem, code, flagType = "logic", \
12210037SARM gem5 Developers                           buildCc = True, buildNonCc = True, \
12310037SARM gem5 Developers                           suffix = "XSReg"):
12410037SARM gem5 Developers        buildDataWork(mnem, sRegOp2 + code, flagType, suffix,
12510037SARM gem5 Developers                      buildCc, buildNonCc, "DataXSRegOp", "DataXSReg")
12610037SARM gem5 Developers
12710037SARM gem5 Developers    def buildXERegDataInst(mnem, code, flagType = "logic", \
12810037SARM gem5 Developers                           buildCc = True, buildNonCc = True, \
12910037SARM gem5 Developers                           suffix = "XEReg"):
13010037SARM gem5 Developers        buildDataWork(mnem, eRegOp2 + code, flagType, suffix,
13110037SARM gem5 Developers                      buildCc, buildNonCc, "DataXERegOp", "DataXEReg")
13210037SARM gem5 Developers
13310037SARM gem5 Developers    def buildDataInst(mnem, code, flagType = "logic",
13410037SARM gem5 Developers                      buildCc = True, buildNonCc = True):
13510037SARM gem5 Developers        buildXImmDataInst(mnem, code, flagType, buildCc, buildNonCc)
13610037SARM gem5 Developers        buildXSRegDataInst(mnem, code, flagType, buildCc, buildNonCc)
13710037SARM gem5 Developers        buildXERegDataInst(mnem, code, flagType, buildCc, buildNonCc)
13810037SARM gem5 Developers
13910037SARM gem5 Developers    buildXImmDataInst("adr", "Dest64 = RawPC + imm", buildCc = False);
14010037SARM gem5 Developers    buildXImmDataInst("adrp", "Dest64 = (RawPC & ~mask(12)) + imm",
14110037SARM gem5 Developers                      buildCc = False);
14210037SARM gem5 Developers    buildDataInst("and", "Dest64 = resTemp = Op164 & secOp;")
14310037SARM gem5 Developers    buildDataInst("eor", "Dest64 = Op164 ^ secOp;", buildCc = False)
14410037SARM gem5 Developers    buildXSRegDataInst("eon", "Dest64 = Op164 ^ ~secOp;", buildCc = False)
14510037SARM gem5 Developers    buildDataInst("sub", "Dest64 = resTemp = Op164 - secOp;", "sub")
14610037SARM gem5 Developers    buildDataInst("add", "Dest64 = resTemp = Op164 + secOp;", "add")
14710037SARM gem5 Developers    buildXSRegDataInst("adc",
14810037SARM gem5 Developers            "Dest64 = resTemp = Op164 + secOp + %s;" % oldC, "add")
14910037SARM gem5 Developers    buildXSRegDataInst("sbc",
15010037SARM gem5 Developers            "Dest64 = resTemp = Op164 - secOp - !%s;" % oldC, "sub")
15110037SARM gem5 Developers    buildDataInst("orr", "Dest64 = Op164 | secOp;", buildCc = False)
15210037SARM gem5 Developers    buildXSRegDataInst("orn", "Dest64 = Op164 | ~secOp;", buildCc = False)
15310037SARM gem5 Developers    buildXSRegDataInst("bic", "Dest64 = resTemp = Op164 & ~secOp;")
15410037SARM gem5 Developers
15510037SARM gem5 Developers    def buildDataXImmInst(mnem, code, optArgs = []):
15610037SARM gem5 Developers        global header_output, decoder_output, exec_output
15710037SARM gem5 Developers        classNamePrefix = mnem[0].upper() + mnem[1:]
15810037SARM gem5 Developers        templateBase = "DataXImm"
15910037SARM gem5 Developers        iop = InstObjParams(mnem, classNamePrefix + "64",
16010037SARM gem5 Developers                            templateBase + "Op", code, optArgs)
16110037SARM gem5 Developers        header_output += eval(templateBase + "Declare").subst(iop)
16210037SARM gem5 Developers        decoder_output += eval(templateBase + "Constructor").subst(iop)
16310037SARM gem5 Developers        exec_output += BasicExecute.subst(iop)
16410037SARM gem5 Developers
16510037SARM gem5 Developers    def buildDataXRegInst(mnem, regOps, code, optArgs = [],
16610037SARM gem5 Developers                          overrideOpClass=None):
16710037SARM gem5 Developers        global header_output, decoder_output, exec_output
16810037SARM gem5 Developers        templateBase = "DataX%dReg" % regOps
16910037SARM gem5 Developers        classNamePrefix = mnem[0].upper() + mnem[1:]
17010037SARM gem5 Developers        if overrideOpClass:
17110037SARM gem5 Developers            iop = InstObjParams(mnem, classNamePrefix + "64",
17210037SARM gem5 Developers                                templateBase + "Op",
17310037SARM gem5 Developers                                { 'code': code, 'op_class': overrideOpClass},
17410037SARM gem5 Developers                                optArgs)
17510037SARM gem5 Developers        else:
17610037SARM gem5 Developers            iop = InstObjParams(mnem, classNamePrefix + "64",
17710037SARM gem5 Developers                                templateBase + "Op", code, optArgs)
17810037SARM gem5 Developers        header_output += eval(templateBase + "Declare").subst(iop)
17910037SARM gem5 Developers        decoder_output += eval(templateBase + "Constructor").subst(iop)
18010037SARM gem5 Developers        exec_output += BasicExecute.subst(iop)
18110037SARM gem5 Developers
18210037SARM gem5 Developers    buildDataXRegInst("madd", 3, "Dest64 = Op164 + Op264 * Op364",
18310037SARM gem5 Developers        overrideOpClass="IntMultOp")
18410037SARM gem5 Developers    buildDataXRegInst("msub", 3, "Dest64 = Op164 - Op264 * Op364",
18510037SARM gem5 Developers        overrideOpClass="IntMultOp")
18610037SARM gem5 Developers    buildDataXRegInst("smaddl", 3,
18710037SARM gem5 Developers        "XDest = XOp1 + sext<32>(WOp2) * sext<32>(WOp3)",
18810037SARM gem5 Developers        overrideOpClass="IntMultOp")
18910037SARM gem5 Developers    buildDataXRegInst("smsubl", 3,
19010037SARM gem5 Developers        "XDest = XOp1 - sext<32>(WOp2) * sext<32>(WOp3)",
19110037SARM gem5 Developers        overrideOpClass="IntMultOp")
19210037SARM gem5 Developers    buildDataXRegInst("smulh", 2, '''
19310037SARM gem5 Developers        uint64_t op1H = (int32_t)(XOp1 >> 32);
19410037SARM gem5 Developers        uint64_t op1L = (uint32_t)XOp1;
19510037SARM gem5 Developers        uint64_t op2H = (int32_t)(XOp2 >> 32);
19610037SARM gem5 Developers        uint64_t op2L = (uint32_t)XOp2;
19710037SARM gem5 Developers        uint64_t mid1 = ((op1L * op2L) >> 32) + op1H * op2L;
19810037SARM gem5 Developers        uint64_t mid2 = op1L * op2H;
19910037SARM gem5 Developers        uint64_t result = ((uint64_t)(uint32_t)mid1 + (uint32_t)mid2) >> 32;
20010037SARM gem5 Developers        result += shiftReg64(mid1, 32, ASR, intWidth);
20110037SARM gem5 Developers        result += shiftReg64(mid2, 32, ASR, intWidth);
20210037SARM gem5 Developers        XDest = result + op1H * op2H;
20310037SARM gem5 Developers    ''', overrideOpClass="IntMultOp")
20410037SARM gem5 Developers    buildDataXRegInst("umaddl", 3, "XDest = XOp1 + WOp2 * WOp3",
20510037SARM gem5 Developers        overrideOpClass="IntMultOp")
20610037SARM gem5 Developers    buildDataXRegInst("umsubl", 3, "XDest = XOp1 - WOp2 * WOp3",
20710037SARM gem5 Developers        overrideOpClass="IntMultOp")
20810037SARM gem5 Developers    buildDataXRegInst("umulh", 2, '''
20910037SARM gem5 Developers        uint64_t op1H = (uint32_t)(XOp1 >> 32);
21010037SARM gem5 Developers        uint64_t op1L = (uint32_t)XOp1;
21110037SARM gem5 Developers        uint64_t op2H = (uint32_t)(XOp2 >> 32);
21210037SARM gem5 Developers        uint64_t op2L = (uint32_t)XOp2;
21310037SARM gem5 Developers        uint64_t mid1 = ((op1L * op2L) >> 32) + op1H * op2L;
21410037SARM gem5 Developers        uint64_t mid2 = op1L * op2H;
21510037SARM gem5 Developers        uint64_t result = ((uint64_t)(uint32_t)mid1 + (uint32_t)mid2) >> 32;
21610037SARM gem5 Developers        result += mid1 >> 32;
21710037SARM gem5 Developers        result += mid2 >> 32;
21810037SARM gem5 Developers        XDest = result + op1H * op2H;
21910037SARM gem5 Developers    ''', overrideOpClass="IntMultOp")
22010037SARM gem5 Developers
22110037SARM gem5 Developers    buildDataXRegInst("asrv", 2,
22210037SARM gem5 Developers        "Dest64 = shiftReg64(Op164, Op264, ASR, intWidth)")
22310037SARM gem5 Developers    buildDataXRegInst("lslv", 2,
22410037SARM gem5 Developers        "Dest64 = shiftReg64(Op164, Op264, LSL, intWidth)")
22510037SARM gem5 Developers    buildDataXRegInst("lsrv", 2,
22610037SARM gem5 Developers        "Dest64 = shiftReg64(Op164, Op264, LSR, intWidth)")
22710037SARM gem5 Developers    buildDataXRegInst("rorv", 2,
22810037SARM gem5 Developers        "Dest64 = shiftReg64(Op164, Op264, ROR, intWidth)")
22912258Sgiacomo.travaglini@arm.com
23012258Sgiacomo.travaglini@arm.com    crcCode = '''
23112258Sgiacomo.travaglini@arm.com    constexpr uint8_t size_bytes = %(sz)d;
23212258Sgiacomo.travaglini@arm.com    constexpr uint32_t poly = %(polynom)s;
23312258Sgiacomo.travaglini@arm.com
23412258Sgiacomo.travaglini@arm.com    // Initial value is often a previously evaluated
23512258Sgiacomo.travaglini@arm.com    // crc value hence is always 32bit in CRC32
23612258Sgiacomo.travaglini@arm.com    uint32_t initial_crc = Op164 & 0xFFFFFFFF;
23712258Sgiacomo.travaglini@arm.com
23812258Sgiacomo.travaglini@arm.com    uint64_t data = htole(Op264);
23912258Sgiacomo.travaglini@arm.com    auto data_buffer = reinterpret_cast<uint8_t*>(&data);
24012258Sgiacomo.travaglini@arm.com
24112258Sgiacomo.travaglini@arm.com    Dest = crc32<poly>(
24212258Sgiacomo.travaglini@arm.com        data_buffer,   /* Message register */
24312258Sgiacomo.travaglini@arm.com        initial_crc,   /* Initial value of the CRC */
24412258Sgiacomo.travaglini@arm.com        size_bytes     /* Size of the original Message */
24512258Sgiacomo.travaglini@arm.com    );
24612258Sgiacomo.travaglini@arm.com    '''
24712258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32b", 2,
24812258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 1, "polynom": "0x04C11DB7"})
24912258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32h", 2,
25012258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 2, "polynom": "0x04C11DB7"})
25112258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32w", 2,
25212258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 4, "polynom": "0x04C11DB7"})
25312258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32x", 2,
25412258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 8, "polynom": "0x04C11DB7"})
25512258Sgiacomo.travaglini@arm.com
25612258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32cb", 2,
25712258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 1, "polynom": "0x1EDC6F41"})
25812258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32ch", 2,
25912258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 2, "polynom": "0x1EDC6F41"})
26012258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32cw", 2,
26112258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 4, "polynom": "0x1EDC6F41"})
26212258Sgiacomo.travaglini@arm.com    buildDataXRegInst("crc32cx", 2,
26312258Sgiacomo.travaglini@arm.com        crcCode % {"sz": 8, "polynom": "0x1EDC6F41"})
26412258Sgiacomo.travaglini@arm.com
26510037SARM gem5 Developers    buildDataXRegInst("sdiv", 2, '''
26610037SARM gem5 Developers        int64_t op1 = Op164;
26710037SARM gem5 Developers        int64_t op2 = Op264;
26810037SARM gem5 Developers        if (intWidth == 32) {
26910037SARM gem5 Developers            op1 = sext<32>(op1);
27010037SARM gem5 Developers            op2 = sext<32>(op2);
27110037SARM gem5 Developers        }
27210037SARM gem5 Developers        Dest64 = op2 == -1 ? -op1 : op2 ? op1 / op2 : 0;
27310037SARM gem5 Developers    ''', overrideOpClass="IntDivOp")
27410037SARM gem5 Developers    buildDataXRegInst("udiv", 2, "Dest64 = Op264 ? Op164 / Op264 : 0",
27510037SARM gem5 Developers        overrideOpClass="IntDivOp")
27610037SARM gem5 Developers
27710037SARM gem5 Developers    buildDataXRegInst("cls", 1, '''
27810037SARM gem5 Developers        uint64_t op1 = Op164;
27910037SARM gem5 Developers        if (bits(op1, intWidth - 1))
28010037SARM gem5 Developers            op1 ^= mask(intWidth);
28110037SARM gem5 Developers        Dest64 = (op1 == 0) ? intWidth - 1 : (intWidth - 2 - findMsbSet(op1));
28210037SARM gem5 Developers    ''')
28310037SARM gem5 Developers    buildDataXRegInst("clz", 1, '''
28410037SARM gem5 Developers        Dest64 = (Op164 == 0) ? intWidth : (intWidth - 1 - findMsbSet(Op164));
28510037SARM gem5 Developers    ''')
28610037SARM gem5 Developers    buildDataXRegInst("rbit", 1, '''
28712227Sgiacomo.travaglini@arm.com        Dest64 = reverseBits(Op164, intWidth/8);
28810037SARM gem5 Developers    ''')
28910037SARM gem5 Developers    buildDataXRegInst("rev", 1, '''
29010037SARM gem5 Developers        if (intWidth == 32)
29110037SARM gem5 Developers            Dest64 = betole<uint32_t>(Op164);
29210037SARM gem5 Developers        else
29310037SARM gem5 Developers            Dest64 = betole<uint64_t>(Op164);
29410037SARM gem5 Developers    ''')
29510037SARM gem5 Developers    buildDataXRegInst("rev16", 1, '''
29610037SARM gem5 Developers        int count = intWidth / 16;
29710037SARM gem5 Developers        uint64_t result = 0;
29810037SARM gem5 Developers        for (unsigned i = 0; i < count; i++) {
29910037SARM gem5 Developers            uint16_t hw = Op164 >> (i * 16);
30010037SARM gem5 Developers            result |= (uint64_t)betole<uint16_t>(hw) << (i * 16);
30110037SARM gem5 Developers        }
30210037SARM gem5 Developers        Dest64 = result;
30310037SARM gem5 Developers    ''')
30410037SARM gem5 Developers    buildDataXRegInst("rev32", 1, '''
30510037SARM gem5 Developers        int count = intWidth / 32;
30610037SARM gem5 Developers        uint64_t result = 0;
30710037SARM gem5 Developers        for (unsigned i = 0; i < count; i++) {
30810037SARM gem5 Developers            uint32_t hw = Op164 >> (i * 32);
30910037SARM gem5 Developers            result |= (uint64_t)betole<uint32_t>(hw) << (i * 32);
31010037SARM gem5 Developers        }
31110037SARM gem5 Developers        Dest64 = result;
31210037SARM gem5 Developers    ''')
31310037SARM gem5 Developers
31410037SARM gem5 Developers    msrMrs64EnabledCheckCode = '''
31510037SARM gem5 Developers        // Check for read/write access right
31610037SARM gem5 Developers        if (!can%sAArch64SysReg(flat_idx, Scr64, cpsr, xc->tcBase())) {
31710037SARM gem5 Developers            if (flat_idx == MISCREG_DAIF ||
31810037SARM gem5 Developers                flat_idx == MISCREG_DC_ZVA_Xt ||
31910037SARM gem5 Developers                flat_idx == MISCREG_DC_CVAC_Xt ||
32010037SARM gem5 Developers                flat_idx == MISCREG_DC_CIVAC_Xt
32110037SARM gem5 Developers                )
32210474Sandreas.hansson@arm.com                return std::make_shared<UndefinedInstruction>(
32310474Sandreas.hansson@arm.com                                    machInst, 0, EC_TRAPPED_MSR_MRS_64,
32410205SAli.Saidi@ARM.com                                    mnemonic);
32510474Sandreas.hansson@arm.com            return std::make_shared<UndefinedInstruction>(machInst, false,
32610474Sandreas.hansson@arm.com                                                          mnemonic);
32710037SARM gem5 Developers        }
32810037SARM gem5 Developers
32910037SARM gem5 Developers        // Check for traps to supervisor (FP/SIMD regs)
33010037SARM gem5 Developers        if (el <= EL1 && msrMrs64TrapToSup(flat_idx, el, Cpacr64))
33110474Sandreas.hansson@arm.com            return std::make_shared<SupervisorTrap>(machInst, 0x1E00000,
33210474Sandreas.hansson@arm.com                                                    EC_TRAPPED_SIMD_FP);
33310037SARM gem5 Developers
33410037SARM gem5 Developers        bool is_vfp_neon = false;
33510037SARM gem5 Developers
33610037SARM gem5 Developers        // Check for traps to hypervisor
33710037SARM gem5 Developers        if ((ArmSystem::haveVirtualization(xc->tcBase()) && el <= EL2) &&
33811582SDylan.Johnson@ARM.com            msrMrs64TrapToHyp(flat_idx, el, %s, CptrEl264, Hcr64, &is_vfp_neon)) {
33910474Sandreas.hansson@arm.com            return std::make_shared<HypervisorTrap>(
34010474Sandreas.hansson@arm.com                machInst, is_vfp_neon ? 0x1E00000 : imm,
34110037SARM gem5 Developers                is_vfp_neon ? EC_TRAPPED_SIMD_FP : EC_TRAPPED_MSR_MRS_64);
34210037SARM gem5 Developers        }
34310037SARM gem5 Developers
34410037SARM gem5 Developers        // Check for traps to secure monitor
34510037SARM gem5 Developers        if ((ArmSystem::haveSecurity(xc->tcBase()) && el <= EL3) &&
34610037SARM gem5 Developers            msrMrs64TrapToMon(flat_idx, CptrEl364, el, &is_vfp_neon)) {
34710474Sandreas.hansson@arm.com            return std::make_shared<SecureMonitorTrap>(
34810474Sandreas.hansson@arm.com                machInst,
34910037SARM gem5 Developers                is_vfp_neon ? 0x1E00000 : imm,
35010037SARM gem5 Developers                is_vfp_neon ? EC_TRAPPED_SIMD_FP : EC_TRAPPED_MSR_MRS_64);
35110037SARM gem5 Developers        }
35210037SARM gem5 Developers    '''
35310037SARM gem5 Developers
35410037SARM gem5 Developers    buildDataXImmInst("mrs", '''
35510037SARM gem5 Developers        MiscRegIndex flat_idx = (MiscRegIndex) xc->tcBase()->
35612106SRekai.GonzalezAlberquilla@arm.com            flattenRegId(RegId(MiscRegClass, op1)).index();
35710037SARM gem5 Developers        CPSR cpsr = Cpsr;
35810037SARM gem5 Developers        ExceptionLevel el = (ExceptionLevel) (uint8_t) cpsr.el;
35910037SARM gem5 Developers        %s
36010037SARM gem5 Developers        XDest = MiscOp1_ud;
36110037SARM gem5 Developers    ''' % (msrMrs64EnabledCheckCode % ('Read', 'true'),),
36210037SARM gem5 Developers        ["IsSerializeBefore"])
36310037SARM gem5 Developers
36410037SARM gem5 Developers    buildDataXRegInst("mrsNZCV", 1, '''
36510037SARM gem5 Developers        CPSR cpsr = 0;
36610037SARM gem5 Developers        cpsr.nz = CondCodesNZ;
36710037SARM gem5 Developers        cpsr.c = CondCodesC;
36810037SARM gem5 Developers        cpsr.v = CondCodesV;
36910037SARM gem5 Developers        XDest = cpsr;
37010037SARM gem5 Developers    ''')
37110037SARM gem5 Developers
37210037SARM gem5 Developers    buildDataXImmInst("msr", '''
37310037SARM gem5 Developers        MiscRegIndex flat_idx = (MiscRegIndex) xc->tcBase()->
37412106SRekai.GonzalezAlberquilla@arm.com            flattenRegId(RegId(MiscRegClass, dest)).index();
37510037SARM gem5 Developers        CPSR cpsr = Cpsr;
37610037SARM gem5 Developers        ExceptionLevel el = (ExceptionLevel) (uint8_t) cpsr.el;
37710037SARM gem5 Developers        %s
37810037SARM gem5 Developers        MiscDest_ud = XOp1;
37910037SARM gem5 Developers    ''' % (msrMrs64EnabledCheckCode % ('Write', 'false'),),
38010037SARM gem5 Developers        ["IsSerializeAfter", "IsNonSpeculative"])
38110037SARM gem5 Developers
38210037SARM gem5 Developers    buildDataXRegInst("msrNZCV", 1, '''
38310037SARM gem5 Developers        CPSR cpsr = XOp1;
38410037SARM gem5 Developers        CondCodesNZ = cpsr.nz;
38510037SARM gem5 Developers        CondCodesC = cpsr.c;
38610037SARM gem5 Developers        CondCodesV = cpsr.v;
38710037SARM gem5 Developers    ''')
38810037SARM gem5 Developers
38910037SARM gem5 Developers    msrdczva_ea_code = '''
39012106SRekai.GonzalezAlberquilla@arm.com        MiscRegIndex flat_idx = (MiscRegIndex) xc->tcBase()->flattenRegId(
39112106SRekai.GonzalezAlberquilla@arm.com                                   RegId(MiscRegClass, dest)).index();
39210037SARM gem5 Developers        CPSR cpsr = Cpsr;
39310037SARM gem5 Developers        ExceptionLevel el = (ExceptionLevel) (uint8_t) cpsr.el;
39410037SARM gem5 Developers    '''
39510037SARM gem5 Developers
39610037SARM gem5 Developers    msrdczva_ea_code += msrMrs64EnabledCheckCode % ('Write', 'false')
39710037SARM gem5 Developers    msrdczva_ea_code += '''
39810037SARM gem5 Developers           Request::Flags memAccessFlags = Request::CACHE_BLOCK_ZERO|ArmISA::TLB::MustBeOne;
39910037SARM gem5 Developers           EA = XBase;
40010037SARM gem5 Developers           assert(!(Dczid & 0x10));
40110037SARM gem5 Developers           uint64_t op_size = power(2, Dczid + 2);
40210037SARM gem5 Developers           EA &= ~(op_size - 1);
40310037SARM gem5 Developers
40410037SARM gem5 Developers   '''
40510037SARM gem5 Developers
40610037SARM gem5 Developers    msrDCZVAIop = InstObjParams("dczva", "Dczva", "SysDC64",
40710037SARM gem5 Developers                { "ea_code" : msrdczva_ea_code,
40810037SARM gem5 Developers                  "memacc_code" : ";", "use_uops" : 0,
40910037SARM gem5 Developers                  "op_wb" : ";", "fa_code" : ";"}, ['IsStore', 'IsMemRef']);
41010037SARM gem5 Developers    header_output += DCStore64Declare.subst(msrDCZVAIop);
41110037SARM gem5 Developers    decoder_output += DCStore64Constructor.subst(msrDCZVAIop);
41210037SARM gem5 Developers    exec_output += DCStore64Execute.subst(msrDCZVAIop);
41310037SARM gem5 Developers    exec_output += DCStore64InitiateAcc.subst(msrDCZVAIop);
41410037SARM gem5 Developers    exec_output += Store64CompleteAcc.subst(msrDCZVAIop);
41510037SARM gem5 Developers
41610037SARM gem5 Developers
41710037SARM gem5 Developers
41810037SARM gem5 Developers    buildDataXImmInst("msrSP", '''
41910037SARM gem5 Developers        if (!canWriteAArch64SysReg(
42012106SRekai.GonzalezAlberquilla@arm.com                (MiscRegIndex) xc->tcBase()->flattenRegId(
42112106SRekai.GonzalezAlberquilla@arm.com                   RegId(MiscRegClass, dest)).index(),
42210037SARM gem5 Developers                Scr64, Cpsr, xc->tcBase())) {
42310474Sandreas.hansson@arm.com            return std::make_shared<UndefinedInstruction>(machInst, false,
42410474Sandreas.hansson@arm.com                                                          mnemonic);
42510037SARM gem5 Developers        }
42610037SARM gem5 Developers        MiscDest_ud = imm;
42710037SARM gem5 Developers    ''', optArgs = ["IsSerializeAfter", "IsNonSpeculative"])
42810037SARM gem5 Developers
42910037SARM gem5 Developers    buildDataXImmInst("msrDAIFSet", '''
43010037SARM gem5 Developers        if (!canWriteAArch64SysReg(
43112106SRekai.GonzalezAlberquilla@arm.com                (MiscRegIndex) xc->tcBase()->flattenRegId(
43212106SRekai.GonzalezAlberquilla@arm.com                   RegId(MiscRegClass, dest)).index(),
43310037SARM gem5 Developers                Scr64, Cpsr, xc->tcBase())) {
43410474Sandreas.hansson@arm.com            return std::make_shared<UndefinedInstruction>(
43510474Sandreas.hansson@arm.com                            machInst, 0, EC_TRAPPED_MSR_MRS_64,
43610205SAli.Saidi@ARM.com                            mnemonic);
43710037SARM gem5 Developers        }
43810037SARM gem5 Developers        CPSR cpsr = Cpsr;
43910037SARM gem5 Developers        cpsr.daif = cpsr.daif | imm;
44010037SARM gem5 Developers        Cpsr = cpsr;
44110037SARM gem5 Developers    ''', optArgs = ["IsSerializeAfter", "IsNonSpeculative"])
44210037SARM gem5 Developers
44310037SARM gem5 Developers    buildDataXImmInst("msrDAIFClr", '''
44410037SARM gem5 Developers        if (!canWriteAArch64SysReg(
44512106SRekai.GonzalezAlberquilla@arm.com                (MiscRegIndex) xc->tcBase()->flattenRegId(
44612106SRekai.GonzalezAlberquilla@arm.com                   RegId(MiscRegClass, dest)).index(),
44710037SARM gem5 Developers                Scr64, Cpsr, xc->tcBase())) {
44810474Sandreas.hansson@arm.com            return std::make_shared<UndefinedInstruction>(
44910474Sandreas.hansson@arm.com                                machInst, 0, EC_TRAPPED_MSR_MRS_64,
45010205SAli.Saidi@ARM.com                                mnemonic);
45110037SARM gem5 Developers        }
45210037SARM gem5 Developers        CPSR cpsr = Cpsr;
45310037SARM gem5 Developers        cpsr.daif = cpsr.daif & ~imm;
45410037SARM gem5 Developers        Cpsr = cpsr;
45510037SARM gem5 Developers    ''', optArgs = ["IsSerializeAfter", "IsNonSpeculative"])
45610037SARM gem5 Developers
45710037SARM gem5 Developers    def buildDataXCompInst(mnem, instType, suffix, code):
45810037SARM gem5 Developers        global header_output, decoder_output, exec_output
45910037SARM gem5 Developers        templateBase = "DataXCond%s" % instType
46010037SARM gem5 Developers        iop = InstObjParams(mnem, mnem.capitalize() + suffix + "64",
46110037SARM gem5 Developers                            templateBase + "Op", code)
46210037SARM gem5 Developers        header_output += eval(templateBase + "Declare").subst(iop)
46310037SARM gem5 Developers        decoder_output += eval(templateBase + "Constructor").subst(iop)
46410037SARM gem5 Developers        exec_output += BasicExecute.subst(iop)
46510037SARM gem5 Developers
46610037SARM gem5 Developers    def buildDataXCondImmInst(mnem, code):
46710037SARM gem5 Developers        buildDataXCompInst(mnem, "CompImm", "Imm", code)
46810037SARM gem5 Developers    def buildDataXCondRegInst(mnem, code):
46910037SARM gem5 Developers        buildDataXCompInst(mnem, "CompReg", "Reg", code)
47010037SARM gem5 Developers    def buildDataXCondSelInst(mnem, code):
47110037SARM gem5 Developers        buildDataXCompInst(mnem, "Sel", "", code)
47210037SARM gem5 Developers
47310037SARM gem5 Developers    def condCompCode(flagType, op, imm):
47410037SARM gem5 Developers        ccCode = createCcCode64(carryCode64[flagType], overflowCode64[flagType])
47510037SARM gem5 Developers        opDecl = "uint64_t secOp M5_VAR_USED = imm;"
47610037SARM gem5 Developers        if not imm:
47710037SARM gem5 Developers            opDecl = "uint64_t secOp M5_VAR_USED = Op264;"
47810037SARM gem5 Developers        return opDecl + '''
47910037SARM gem5 Developers            if (testPredicate(CondCodesNZ, CondCodesC, CondCodesV, condCode)) {
48010037SARM gem5 Developers                uint64_t resTemp = Op164 ''' + op + ''' secOp;
48110037SARM gem5 Developers        ''' + ccCode + '''
48210037SARM gem5 Developers            } else {
48310037SARM gem5 Developers                CondCodesNZ = (defCc >> 2) & 0x3;
48410037SARM gem5 Developers                CondCodesC = (defCc >> 1) & 0x1;
48510037SARM gem5 Developers                CondCodesV = defCc & 0x1;
48610037SARM gem5 Developers            }
48710037SARM gem5 Developers        '''
48810037SARM gem5 Developers
48910037SARM gem5 Developers    buildDataXCondImmInst("ccmn", condCompCode("add", "+", True))
49010037SARM gem5 Developers    buildDataXCondImmInst("ccmp", condCompCode("sub", "-", True))
49110037SARM gem5 Developers    buildDataXCondRegInst("ccmn", condCompCode("add", "+", False))
49210037SARM gem5 Developers    buildDataXCondRegInst("ccmp", condCompCode("sub", "-", False))
49310037SARM gem5 Developers
49410037SARM gem5 Developers    condSelCode = '''
49510037SARM gem5 Developers        if (testPredicate(CondCodesNZ, CondCodesC, CondCodesV, condCode)) {
49610037SARM gem5 Developers            Dest64 = Op164;
49710037SARM gem5 Developers        } else {
49810037SARM gem5 Developers            Dest64 = %(altVal)s;
49910037SARM gem5 Developers        }
50010037SARM gem5 Developers    '''
50110037SARM gem5 Developers    buildDataXCondSelInst("csel", condSelCode % {"altVal" : "Op264"})
50210037SARM gem5 Developers    buildDataXCondSelInst("csinc", condSelCode % {"altVal" : "Op264 + 1"})
50310037SARM gem5 Developers    buildDataXCondSelInst("csinv", condSelCode % {"altVal" : "~Op264"})
50410037SARM gem5 Developers    buildDataXCondSelInst("csneg", condSelCode % {"altVal" : "-Op264"})
50510037SARM gem5 Developers}};
506