clang 24.0.0git
gen-msp430-def.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# ===----------------------------------------------------------------------===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8# ===----------------------------------------------------------------------===##
9"""
10Script to generate MSP430 definitions from TI's devices.csv
11
12Download the devices.csv from [1] using the link "Header and Support Files".
13
14CPUS and MULTIPLIERS map the numeric CPU_TYPE and MPY_TYPE codes of the CSV
15to the CPU and hwmult names used by clang. The codes are defined in the
16legend at the top of devices.csv:
17
18CPU_TYPE:
19 0: MSP430 CPU
20 1: MSP430X CPU
21 2: MSP430XV2 CPU
22
23MPY_TYPE:
24 0: No hardware multiplier
25 1: 16 bit hardware multiplier
26 2: 16 bit hardware multiplier with sign extension (2xx devices)
27 4: 32 bit hardware multiplier
28 8: 32 bit hardware multiplier (5xx devices)
29
30Code 2 maps to "16bit" because there is no separate hwmult option for it.
31Code 8 maps to "f5series" because the 5xx/6xx multiplier uses different
32register addresses than the other 32 bit multiplier.
33
34[1]: https://www.ti.com/tool/MSP430-GCC-OPENSOURCE#downloads
35"""
36import csv
37import sys
38
39DEVICE_COLUMN = 0
40CPU_COLUMN = 1
41MULTIPLIER_COLUMN = 3
42
43CPUS = {"0": "msp430", "1": "msp430x", "2": "msp430xv2"}
44MULTIPLIERS = {
45 "0": "none",
46 "1": "16bit",
47 "2": "16bit",
48 "4": "32bit",
49 "8": "f5series",
50}
51
52PREFIX = """//===--- MSP430Target.def - MSP430 Feature/Processor Database----*- C++ -*-===//
53//
54// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
55// See https://llvm.org/LICENSE.txt for license information.
56// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
57//
58//===----------------------------------------------------------------------===//
59//
60// This file defines the MSP430 devices and their features.
61//
62// Generated from TI's devices.csv in version {} using the script in
63// Target/MSP430/gen-msp430-def.py - use this tool rather than adding
64// new MCUs by hand.
65//
66// You can download the devices.csv from https://www.ti.com/tool/MSP430-GCC-OPENSOURCE#overview
67// by downloading the support and header files.
68//
69// This script exports the "Device Name", "CPU_TYPE" and "MPY_TYPE" columns from the
70// devices script.
71//
72// CPU_TYPE codes:
73// 0: MSP430 CPU -> "msp430"
74// 1: MSP430X CPU -> "msp430x"
75// 2: MSP430XV2 CPU -> "msp430xv2"
76//
77// MPY_TYPE codes:
78// 0: No hardware multiplier -> "none"
79// 1: 16 bit hardware multiplier -> "16bit"
80// 2: 16 bit hardware multiplier with sign extension -> "16bit"
81// 4: 32 bit hardware multiplier -> "32bit"
82// 8: 32 bit hardware multiplier (5xx devices) -> "f5series"
83//
84// Code 2 maps to "16bit" because there is no separate hwmult option for it.
85// Code 8 maps to "f5series" because the 5xx/6xx multiplier uses different
86// register addresses than the other 32 bit multiplier.
87//===----------------------------------------------------------------------===//
88
89#ifndef MSP430_MCU
90#define MSP430_MCU(NAME, CPU, HWMULT)
91#endif
92
93"""
94
95SUFFIX = """
96// Generic MCUs
97MSP430_MCU("msp430i2xxgeneric", "msp430", "none")
98
99#undef MSP430_MCU
100"""
101
102
103def csv2def(csv_path: str, def_path: str):
104 """
105 Parse the devices.csv file at the given path, generate the definitions and
106 write them to the given path.
107
108 :param csv_path: Path to the devices.csv to parse
109 :type csv_path: str
110 :param def_path: Path to the output file to write the definitions to
111 "type def_path: str
112 """
113
114 mcus = []
115 version = "unknown"
116
117 with open(csv_path) as csv_file:
118 csv_reader = csv.reader(csv_file)
119 while True:
120 row = next(csv_reader)
121 if len(row) < MULTIPLIER_COLUMN:
122 continue
123
124 if row[DEVICE_COLUMN] == "# Device Name":
125 assert row[CPU_COLUMN] == "CPU_TYPE", "File format changed"
126 assert row[MULTIPLIER_COLUMN] == "MPY_TYPE", "File format changed"
127 break
128
129 if row[0] == "Version:":
130 version = row[1]
131
132 for row in csv_reader:
133 if row[DEVICE_COLUMN].endswith("generic"):
134 continue
135 assert row[CPU_COLUMN] in CPUS, "Unknown CPU type"
136 assert row[MULTIPLIER_COLUMN] in MULTIPLIERS, "Unknown multiplier type"
137 mcus.append(
138 (
139 row[DEVICE_COLUMN],
140 CPUS[row[CPU_COLUMN]],
141 MULTIPLIERS[row[MULTIPLIER_COLUMN]],
142 )
143 )
144
145 with open(def_path, "w") as def_file:
146 def_file.write(PREFIX.format(version))
147
148 for name, cpu, hwmult in mcus:
149 def_file.write(f'MSP430_MCU("{name}", "{cpu}", "{hwmult}")\n')
150
151 def_file.write(SUFFIX)
152
153
154if __name__ == "__main__":
155 if len(sys.argv) != 3:
156 sys.exit(f"Usage: {sys.argv[0]} <CSV_FILE> <DEF_FILE>")
157
158 csv2def(sys.argv[1], sys.argv[2])
csv2def(str csv_path, str def_path)