1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-19 08:31:11 +00:00
CLK/Processors/Decoders/PowerPC/PowerPC.hpp

229 lines
8.2 KiB
C++
Raw Normal View History

2020-12-31 03:55:59 +00:00
//
// PowerPC.hpp
// Clock Signal
//
// Created by Thomas Harte on 12/30/20.
// Copyright © 2020 Thomas Harte. All rights reserved.
//
#ifndef PowerPC_hpp
#define PowerPC_hpp
2021-01-01 16:46:26 +00:00
#include <cstddef>
2020-12-31 03:55:59 +00:00
#include <cstdint>
namespace CPU {
namespace Decoder {
namespace PowerPC {
enum class Model {
/// i.e. 32-bit, with POWER carry-over instructions.
2020-12-31 03:55:59 +00:00
MPC601,
/// i.e. 32-bit, no POWER instructions.
MPC603,
/// i.e. 64-bit.
MPC620,
2020-12-31 03:55:59 +00:00
};
enum class Operation: uint8_t {
Undefined,
// These 601-exclusive instructions; a lot of them are carry-overs
// from POWER.
absx, clcs, divx, divsx, dozx, dozi, lscbxx, maskgx, maskirx, mulx,
nabsx, rlmix, rribx, slex, sleqx, sliqx, slliqx, sllqx, slqx,
sraiqx, sraqx, srex, sreax, sreqx, sriqx, srliqx, srlqx, srqx,
// 32- and 64-bit PowerPC instructions.
addx, addcx, addex, addi, addic, addic_, addis, addmex, addzex, andx,
andcx, andi_, andis_, bx, bcx, bcctrx, bclrx, cmp, cmpi, cmpl, cmpli,
cntlzwx, crand, crandc, creqv, crnand, crnor, cror, crorc, crxor, dcbf,
dcbst, dcbt, dcbtst, dcbz, divwx, divwux, eciwx, ecowx, eieio, eqvx,
extsbx, extshx, fabsx, faddx, faddsx, fcmpo, fcmpu, fctiwx, fctiwzx,
fdivx, fdivsx, fmaddx, fmaddsx, fmrx, fmsubx, fmsubsx, fmulx, fmulsx,
fnabsx, fnegx, fnmaddx, fnmaddsx, fnmsubx, fnmsubsx, frspx, fsubx, fsubsx,
icbi, isync, lbz, lbzu, lbzux, lbzx, lfd, lfdu, lfdux, lfdx, lfs, lfsu,
lfsux, lfsx, lha, lhau, lhaux, lhax, lhbrx, lhz, lhzu, lhzux, lhzx, lmw,
lswi, lswx, lwarx, lwbrx, lwz, lwzu, lwzux, lwzx, mcrf, mcrfs, mcrxr,
mfcr, mffsx, mfmsr, mfspr, mfsr, mfsrin, mtcrf, mtfsb0x, mtfsb1x, mtfsfx,
mtfsfix, mtmsr, mtspr, mtsr, mtsrin, mulhwx, mulhwux, mulli, mullwx,
nandx, negx, norx, orx, orcx, ori, oris, rfi, rlwimix, rlwinmx, rlwnmx,
sc, slwx, srawx, srawix, srwx, stb, stbu, stbux, stbx, stfd, stfdu,
stfdux, stfdx, stfs, stfsu, stfsux, stfsx, sth, sthbrx, sthu, sthux, sthx,
stmw, stswi, stswx, stw, stwbrx, stwcx_, stwu, stwux, stwx, subfx, subfcx,
subfex, subfic, subfmex, subfzex, sync, tw, twi, xorx, xori, xoris, mftb,
2021-01-01 02:12:36 +00:00
2020-12-31 03:55:59 +00:00
// 32-bit, supervisor level.
dcbi,
2021-01-01 02:12:36 +00:00
// Supervisor, optional.
2021-01-01 02:12:36 +00:00
tlbia, tlbie, tlbsync,
2020-12-31 03:55:59 +00:00
// Optional.
2021-01-01 16:46:26 +00:00
fresx, frsqrtex, fselx, fsqrtx, slbia, slbie, stfiwx,
2020-12-31 03:55:59 +00:00
// 64-bit only PowerPC instructions.
2021-01-01 02:12:36 +00:00
cntlzdx, divdx, divdux, extswx, fcfidx, fctidx, fctidzx, tdi, mulhdux,
ldx, sldx, ldux, td, mulhdx, ldarx, stdx, stdux, mulld, lwax, lwaux,
2021-01-01 16:46:26 +00:00
sradix, srdx, sradx, extsw, fsqrtsx, std, stdu, stdcx_,
2020-12-31 03:55:59 +00:00
};
/*!
Holds a decoded PowerPC instruction.
Implementation note: because the PowerPC encoding is particularly straightforward,
only the operation has been decoded ahead of time; all other fields are decoded on-demand.
It would be possible to partition the ordering of Operations into user followed by supervisor,
eliminating the storage necessary for a flag, but it wouldn't save anything due to alignment.
*/
2020-12-31 03:55:59 +00:00
struct Instruction {
Operation operation = Operation::Undefined;
bool is_supervisor = false;
uint32_t opcode = 0;
2020-12-31 03:55:59 +00:00
Instruction() {}
Instruction(uint32_t opcode) : opcode(opcode) {}
Instruction(Operation operation, uint32_t opcode, bool is_supervisor = false) : operation(operation), is_supervisor(is_supervisor), opcode(opcode) {}
2020-12-31 03:55:59 +00:00
2021-01-01 16:46:26 +00:00
// Instruction fields are decoded below; naming is a compromise between
// Motorola's documentation and IBM's.
//
// I've dutifully implemented various synonyms with unique entry points,
// in order to capture that information here rather than thrusting it upon
// the reader of whatever implementation may follow.
2021-01-01 21:38:40 +00:00
// Currently omitted: OPCD and XO, which I think are unnecessary given that
// full decoding has already occurred.
2021-01-01 02:12:36 +00:00
2021-01-01 16:46:26 +00:00
/// Immediate field used to specify an unsigned 16-bit integer.
2021-01-05 03:36:39 +00:00
uint16_t uimm() const { return uint16_t(opcode & 0xffff); }
2021-01-01 16:46:26 +00:00
/// Immediate field used to specify a signed 16-bit integer.
2021-01-05 03:36:39 +00:00
int16_t simm() const { return int16_t(opcode & 0xffff); }
2021-01-01 21:38:40 +00:00
/// Immediate field used to specify a signed 16-bit integer.
2021-01-05 03:36:39 +00:00
int16_t d() const { return int16_t(opcode & 0xffff); }
2021-01-01 21:38:40 +00:00
/// Immediate field used to specify a signed 14-bit integer [64-bit only].
2021-01-05 03:36:39 +00:00
int16_t ds() const { return int16_t(opcode & 0xfffc); }
2021-01-01 16:46:26 +00:00
/// Immediate field used as data to be placed into a field in the floating point status and condition register.
2021-01-05 03:36:39 +00:00
int32_t imm() const { return (opcode >> 12) & 0xf; }
2021-01-01 16:46:26 +00:00
/// Specifies the conditions on which to trap.
2021-01-05 03:36:39 +00:00
int32_t to() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Register source A or destination.
2021-01-05 03:36:39 +00:00
uint32_t rA() const { return (opcode >> 16) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Register source B.
2021-01-05 03:36:39 +00:00
uint32_t rB() const { return (opcode >> 11) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Register destination.
2021-01-05 03:36:39 +00:00
uint32_t rD() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Register source.
2021-01-05 03:36:39 +00:00
uint32_t rS() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Floating point register source A.
2021-01-05 03:36:39 +00:00
uint32_t frA() const { return (opcode >> 16) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Floating point register source B.
2021-01-05 03:36:39 +00:00
uint32_t frB() const { return (opcode >> 11) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Floating point register source C.
2021-01-05 03:36:39 +00:00
uint32_t frC() const { return (opcode >> 6) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Floating point register source.
2021-01-05 03:36:39 +00:00
uint32_t frS() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Floating point register destination.
2021-01-05 03:36:39 +00:00
uint32_t frD() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Branch conditional options.
2021-01-05 03:36:39 +00:00
uint32_t bo() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Source condition register bit for branch conditionals.
2021-01-05 03:36:39 +00:00
uint32_t bi() const { return (opcode >> 16) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Branch displacement; provided as already sign extended.
2021-01-05 03:36:39 +00:00
int16_t bd() const { return int16_t(opcode & 0xfffc); }
2021-01-01 16:46:26 +00:00
2021-01-01 21:38:40 +00:00
/// Specifies the first 1 bit of a 32/64-bit mask for rotate operations.
2021-01-05 03:36:39 +00:00
uint32_t mb() const { return (opcode >> 6) & 0x1f; }
2021-01-01 21:38:40 +00:00
/// Specifies the first 1 bit of a 32/64-bit mask for rotate operations.
2021-01-05 03:36:39 +00:00
uint32_t me() const { return (opcode >> 1) & 0x1f; }
2021-01-01 21:38:40 +00:00
2021-01-01 16:46:26 +00:00
/// Condition register source bit A.
2021-01-05 03:36:39 +00:00
uint32_t crbA() const { return (opcode >> 16) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Condition register source bit B.
2021-01-05 03:36:39 +00:00
uint32_t crbB() const { return (opcode >> 11) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Condition register (or floating point status & condition register) destination bit.
2021-01-05 03:36:39 +00:00
uint32_t crbD() const { return (opcode >> 21) & 0x1f; }
2021-01-01 16:46:26 +00:00
/// Condition register (or floating point status & condition register) destination field.
2021-01-05 03:36:39 +00:00
uint32_t crfD() const { return (opcode >> 23) & 0x07; }
2021-01-01 16:46:26 +00:00
/// Condition register (or floating point status & condition register) source field.
2021-01-05 03:36:39 +00:00
uint32_t crfS() const { return (opcode >> 18) & 0x07; }
2021-01-01 16:46:26 +00:00
/// Mask identifying fields to be updated by mtcrf.
2021-01-05 03:36:39 +00:00
uint32_t crm() const { return (opcode >> 12) & 0xff; }
2021-01-01 16:46:26 +00:00
/// Mask identifying fields to be updated by mtfsf.
2021-01-05 03:36:39 +00:00
uint32_t fm() const { return (opcode >> 17) & 0xff; }
2021-01-01 16:46:26 +00:00
2021-01-01 21:38:40 +00:00
/// Specifies the number of bytes to move in an immediate string load or store.
2021-01-05 03:36:39 +00:00
uint32_t nb() const { return (opcode >> 11) & 0x1f; }
2021-01-01 21:38:40 +00:00
/// Specifies a shift amount.
/// TODO: possibly bit 30 is also used in 64-bit mode, find out.
2021-01-05 03:36:39 +00:00
uint32_t sh() const { return (opcode >> 11) & 0x1f; }
2021-01-01 21:38:40 +00:00
/// Specifies one of the 16 segment registers [32-bit only].
2021-01-05 03:36:39 +00:00
uint32_t sr() const { return (opcode >> 16) & 0xf; }
2021-01-01 21:38:40 +00:00
2021-01-01 16:46:26 +00:00
/// A 24-bit signed number; provided as already sign extended.
2021-01-05 03:36:39 +00:00
int32_t li() const {
2021-01-01 16:46:26 +00:00
constexpr uint32_t extensions[2] = {
0x0000'0000,
0xfc00'0000
};
2021-01-03 16:14:43 +00:00
const uint32_t value = (opcode & 0x03ff'fffc) | extensions[(opcode >> 25) & 1];
2021-01-01 16:46:26 +00:00
return int32_t(value);
}
/// Absolute address bit; @c 0 or @c non-0.
2021-01-05 03:36:39 +00:00
uint32_t aa() const { return opcode & 0x02; }
2021-01-01 16:46:26 +00:00
/// Link bit; @c 0 or @c non-0.
2021-01-05 03:36:39 +00:00
uint32_t lk() const { return opcode & 0x01; }
2021-01-01 16:46:26 +00:00
/// Record bit; @c 0 or @c non-0.
2021-01-05 03:36:39 +00:00
uint32_t rc() const { return opcode & 0x01; }
2021-01-01 16:46:26 +00:00
/// Whether to compare 32-bit or 64-bit numbers [for 64-bit implementations only]; @c 0 or @c non-0.
2021-01-05 03:36:39 +00:00
uint32_t l() const { return opcode & 0x200000; }
2021-01-01 16:46:26 +00:00
/// Enables setting of OV and SO in the XER; @c 0 or @c non-0.
2021-01-05 03:36:39 +00:00
uint32_t oe() const { return opcode & 0x800; }
2020-12-31 03:55:59 +00:00
};
2021-01-03 00:16:21 +00:00
/*!
Implements PowerPC instruction decoding.
This is an experimental implementation; it has not yet undergone significant testing.
*/
2020-12-31 03:55:59 +00:00
struct Decoder {
public:
Decoder(Model model);
Instruction decode(uint32_t opcode);
private:
Model model_;
2021-01-05 03:36:39 +00:00
bool is64bit() const {
return model_ == Model::MPC620;
}
2021-01-05 03:36:39 +00:00
bool is32bit() const {
return !is64bit();
}
2021-01-05 03:36:39 +00:00
bool is601() const {
return model_ == Model::MPC601;
}
2020-12-31 03:55:59 +00:00
};
}
}
}
#include <stdio.h>
#endif /* PowerPC_hpp */