summaryrefslogtreecommitdiff
path: root/tools/utilities/greg_calc.py
blob: 03b372d6be52593abbf66255c8c3b1f339b4d38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3

import sys

REG_F = {
    "TREG_F": 0x00c,
    "DREG_F": 0x09c,
    "IREG_F": 0x12c,
    "SREG_F": 0x1bc,
    "UREG_F": 0x24c,
    "OREG_F": 0x2dc,
    "YREG_F": 0x36c,
    "KREG_F": 0x3fc,
    "BREG_F": 0x48c,
    "MREG_F": 0x51c,
    "AREG_F": 0x5ac,
    "PREG_F": 0x63c,
    "NREG_F": 0x6cc,
    "WREG_F": 0x75c,
    "JREG_F": 0x7ec,
    "HREG_F": 0x87c,
    "VREG_F": 0x90c,
    "XREG_F": 0x99c,
    "ZREG_F": 0xa2c,
    "nREG_F": 0xabc,
    "kREG_F": 0xb4c,
    "oREG_F": 0xbdc,
    "yREG_F": 0xc6c,
    "aREG_F": 0xcfc,
    "hREG_F": 0xd8c,
    "tREG_F": 0xe1c
}

REG_S = {
    "TREG_S": 0x084,
    "DREG_S": 0x114,
    "IREG_S": 0x1a4,
    "SREG_S": 0x234,
    "UREG_S": 0x2c4,
    "OREG_S": 0x354,
    "YREG_S": 0x3e4,
    "KREG_S": 0x474,
    "BREG_S": 0x504,
    "MREG_S": 0x594,
    "AREG_S": 0x624,
    "PREG_S": 0x6b4,
    "NREG_S": 0x744,
    "WREG_S": 0x7d4,
    "JREG_S": 0x864,
    "HREG_S": 0x8f4,
    "VREG_S": 0x984,
    "XREG_S": 0xa14,
    "ZREG_S": 0xaa4,
    "nREG_S": 0xb34,
    "kREG_S": 0xbc4,
    "oREG_S": 0xc54,
    "yREG_S": 0xce4,
    "aREG_S": 0xd74,
    "hREG_S": 0xe04,
    "tREG_S": 0xe94
}


def find_register(hex_val, reg_map, step):
    best_match = None
    best_base = -1

    for name, base in reg_map.items():
        if base <= hex_val and base > best_base:
            best_match = name
            best_base = base

    if best_match is not None:
        index = (hex_val - best_base) // step
        expected = best_base + index * step
        if expected == hex_val:
            return f"{best_match}({index})"

    raise ValueError("No matching register found.")


def main():
    if len(sys.argv) != 3:
        print("Usage: greg_calc.py [s|f] [hex_val]")
        return

    mode = sys.argv[1].lower()
    try:
        hex_val = int(sys.argv[2], 16)
    except ValueError:
        print("Invalid hex value. Use format like 0x75c")
        return

    if mode == 'f':
        result = find_register(hex_val, REG_F, 4)
    elif mode == 's':
        result = find_register(hex_val, REG_S, 2)
    else:
        print("Invalid mode. Use 's' for short or 'f' for float.")
        return

    print(result)


if __name__ == "__main__":
    main()