/*
 * SPDX-FileCopyrightText: Sebastiano Barezzi
 * SPDX-License-Identifier: Apache-2.0
 */

package com.xyzshell.andinfo.libs.cpu.models

/**
 * ARM and ARM64 only
 * 仅ARM和ARM64架构
 *
 * B4.1.105 MIDR, Main ID Register, VMSA
 * 主ID寄存器（MIDR），用于识别ARM处理器
 */
data class Midr(
    /**
     * ARM processor implementer
     * ARM处理器实现者/制造商
     */
    val implementer: Implementer,

    /**
     * Variant number
     * 变体号
     */
    val variant: UByte,

    /**
     * Architecture version
     * 架构版本
     */
    val architecture: Architecture,

    /**
     * Primary part number
     * 主要部件编号
     */
    val primaryPartNumber: UInt,

    /**
     * Revision number
     * 修订号
     */
    val revision: UByte,
) {
    /**
     * ARM processor implementer codes
     * ARM处理器实现者代码
     */
    enum class Implementer(val value: UByte) {
        /**
         * Unknown implementer
         * 未知实现者
         */
        UNKNOWN(0x00U),

        // From ARM spec
        // 来自ARM规范
        /**
         * ARM Holdings
         * ARM控股公司
         */
        ARM(0x41U),
        /**
         * Digital Equipment Corporation
         * 迪吉多公司
         */
        DEC(0x44U),
        /**
         * Motorola/Freescale
         * 摩托罗拉/飞思卡尔
         */
        MOTOROLA(0x4DU),
        /**
         * Qualcomm
         * 高通
         */
        QUALCOMM(0x51U),
        /**
         * Marvell
         * 美满电子
         */
        MARVELL(0x56U),
        /**
         * Intel
         * 英特尔
         */
        INTEL(0x69U),

        // Implementers not declared in the spec, taken from cpuinfo's arm/uarch.c
        // 规范中未声明的实现者，取自cpuinfo的arm/uarch.c
        /**
         * Broadcom
         * 博通
         */
        BROADCOM(0x42U),
        /**
         * Cavium
         * Cavium公司
         */
        CAVIUM(0x43U),
        /**
         * Huawei (HiSilicon)
         * 华为（海思）
         */
        HUAWEI(0x48U),
        /**
         * Nvidia
         * 英伟达
         */
        NVIDIA(0x4EU),
        /**
         * Applied Micro
         * 应用微电路
         */
        APM(0x50U),
        /**
         * Samsung
         * 三星
         */
        SAMSUNG(0x53U);

        companion object {
            fun fromValue(value: UByte) = entries.firstOrNull { it.value == value } ?: UNKNOWN
        }
    }

    /**
     * ARM architecture versions
     * ARM架构版本
     */
    enum class Architecture(val value: UByte) {
        ARMV4(0x1U),
        ARMV4T(0x2U),
        ARMV5(0x3U),
        ARMV5T(0x4U),
        ARMV5TE(0x5U),
        ARMV5TEJ(0x6U),
        ARMV6(0x7U),
        /**
         * Defined by CPUID register
         * 由CPUID寄存器定义
         */
        DEFINED_BY_CPUID(0xFU);

        companion object {
            fun fromValue(value: UByte) = entries.first { it.value == value }
        }
    }

    companion object {
        /**
         * Parse MIDR value from cpuinfo
         * 从cpuinfo解析MIDR值
         */
        fun fromCpuInfo(value: Int) = Midr(
            Implementer.fromValue(value.shr(24).and(0xFF).toUByte()),
            value.shr(20).and(0xF).toUByte(),
            Architecture.fromValue(value.shr(16).and(0xF).toUByte()),
            value.shr(4).and(0xFFF).toUInt(),
            value.and(0xF).toUByte(),
        )
    }
}
