onerom-config 0.5.0

ROM and PCB configurations for One ROM - the flexible retro ROM replacement
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (C) 2025 Piers Finlayson <piers@piers.rocks>
//
// MIT License

pub const STM32F4_BASE_FLASH: u32 = 0x0800_0000;
pub const RP235X_BASE_FLASH: u32 = 0x1000_0000;
pub const RP235X_END_FLASH: u32 = 0x1FFF_FFFF;
pub const RP235X_SRAM_SIZE_KB: usize = 520;
pub const RP235X_BASE_SRAM: u32 = 0x2000_0000;
pub const RP235X_END_SRAM: u32 = RP235X_BASE_SRAM + (RP235X_SRAM_SIZE_KB as u32 * 1024);

/// MCU family
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Family {
    /// STM32F4 series
    #[serde(rename = "stm32f4")]
    Stm32f4,
    /// Raspberry Pi RP2350
    #[serde(rename = "rp2350")]
    Rp2350,
}

impl Family {
    pub const fn get_flash_base(&self) -> u32 {
        match self {
            Family::Stm32f4 => STM32F4_BASE_FLASH,
            Family::Rp2350 => RP235X_BASE_FLASH,
        }
    }

    pub fn try_from_str(s: &str) -> Option<Self> {
        if s.eq_ignore_ascii_case("stm32f4") {
            Some(Family::Stm32f4)
        } else if s.eq_ignore_ascii_case("rp2350") {
            Some(Family::Rp2350)
        } else {
            None
        }
    }
}

impl core::fmt::Display for Family {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Family::Stm32f4 => write!(f, "STM32F4"),
            Family::Rp2350 => write!(f, "RP2350"),
        }
    }
}

/// GPIO Port designation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
pub enum Port {
    /// No port (unused)
    None,
    /// Port 0 (RP2350)
    Zero,
    /// Port A (STM32)
    A,
    /// Port B (STM32)
    B,
    /// Port C (STM32)
    C,
    /// Port D (STM32)
    D,
}

impl core::fmt::Display for Port {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Port::None => write!(f, "PORT_NONE"),
            Port::Zero => write!(f, "PORT_0"),
            Port::A => write!(f, "PORT_A"),
            Port::B => write!(f, "PORT_B"),
            Port::C => write!(f, "PORT_C"),
            Port::D => write!(f, "PORT_D"),
        }
    }
}

// Values match C enum values in core firmware
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum RpVariant {
    Rp235xA = 1,
    Rp235xB = 0,
}

impl core::fmt::Display for RpVariant {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            RpVariant::Rp235xA => write!(f, "RP235xA"),
            RpVariant::Rp235xB => write!(f, "RP235xB"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Processor {
    F401BC,
    F401DE,
    F405,
    F411,
    F446,
    RP2350,
}

impl Processor {
    pub fn vco_min_mhz(&self) -> u32 {
        match self {
            Processor::F401BC => 192,
            Processor::F401DE => 192,
            Processor::F405 => 100,
            Processor::F411 => 100,
            Processor::F446 => 100,
            Processor::RP2350 => 750,
        }
    }

    pub fn vco_max_mhz(&self, overclock: bool) -> u32 {
        if !overclock {
            match self {
                Processor::RP2350 => 1600,
                Processor::F401BC
                | Processor::F401DE
                | Processor::F405
                | Processor::F411
                | Processor::F446 => 432,
            }
        } else {
            match self {
                Processor::RP2350 => 1600,
                Processor::F401BC
                | Processor::F401DE
                | Processor::F405
                | Processor::F411
                | Processor::F446 => 1000,
            }
        }
    }

    pub fn max_sysclk_mhz(&self) -> u32 {
        match self {
            Processor::F401BC => 84,
            Processor::F401DE => 84,
            Processor::F405 => 168,
            Processor::F411 => 100,
            Processor::F446 => 180,
            Processor::RP2350 => 150,
        }
    }
}

pub const MCU_VARIANTS: &[Variant] = &[
    Variant::F401RB,
    Variant::F401RC,
    Variant::F401RE,
    Variant::F405RG,
    Variant::F411RC,
    Variant::F411RE,
    Variant::F446RC,
    Variant::F446RE,
    Variant::RP2350,
    Variant::RP2350B,
];

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Variant {
    F446RC,  // STM32F446RC (6 or 7), 64-pins, 128KB SRAM, 256KB Flash
    F446RE,  // STM32F446RE (6 or 7), 64-pins, 128KB SRAM, 512KB Flash
    F411RC,  // STM32F411RC (6 or 7), 64-pins, 128KB SRAM, 256KB Flash
    F411RE,  // STM32F411RE (6 or 7), 64-pins, 128KB SRAM, 512KB Flash
    F405RG,  // STM32F405RE (6 or 7), 64-pins, 128KB SRAM, 1024KB Flash (+ 64KB CCM RAM)
    F401RE,  // STM32F401RE (6 or 7), 64-pins, 96KB SRAM, 512KB Flash
    F401RB,  // STM32F401RB (6 or 7), 64-pins, 64KB SRAM, 128KB Flash
    F401RC,  // STM32F401RC (6 or 7), 64-pins, 96KB SRAM, 256KB Flash
    RP2350,  // RP2350A, 60-pin, 2MB flash
    RP2350B, // RP2350B, 80-pin, 2MB flash
}

impl core::fmt::Display for Variant {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Variant::F446RC => write!(f, "F446RC"),
            Variant::F446RE => write!(f, "F446RE"),
            Variant::F411RC => write!(f, "F411RC"),
            Variant::F411RE => write!(f, "F411RE"),
            Variant::F405RG => write!(f, "F405RG"),
            Variant::F401RE => write!(f, "F401RE"),
            Variant::F401RB => write!(f, "F401RB"),
            Variant::F401RC => write!(f, "F401RC"),
            Variant::RP2350 => write!(f, "RP2350"),
            Variant::RP2350B => write!(f, "RP2350B"),
        }
    }
}

impl Variant {
    pub fn try_from_str(s: &str) -> Option<Self> {
        if s.eq_ignore_ascii_case("f446rc") {
            Some(Variant::F446RC)
        } else if s.eq_ignore_ascii_case("f446re") {
            Some(Variant::F446RE)
        } else if s.eq_ignore_ascii_case("f411rc") {
            Some(Variant::F411RC)
        } else if s.eq_ignore_ascii_case("f411re") {
            Some(Variant::F411RE)
        } else if s.eq_ignore_ascii_case("f405rg") {
            Some(Variant::F405RG)
        } else if s.eq_ignore_ascii_case("f401re") {
            Some(Variant::F401RE)
        } else if s.eq_ignore_ascii_case("f401rb") {
            Some(Variant::F401RB)
        } else if s.eq_ignore_ascii_case("f401rc") {
            Some(Variant::F401RC)
        } else if s.eq_ignore_ascii_case("rp2350") {
            Some(Variant::RP2350)
        } else if s.eq_ignore_ascii_case("rp2350b") {
            Some(Variant::RP2350B)
        } else {
            None
        }
    }

    pub fn line_enum(&self) -> &str {
        match self {
            Variant::F446RC | Variant::F446RE => "F446",
            Variant::F411RC | Variant::F411RE => "F411",
            Variant::F405RG => "F405",
            Variant::F401RE => "F401DE",
            Variant::F401RB | Variant::F401RC => "F401BC",
            Variant::RP2350 => "RP2350_LINE",
            Variant::RP2350B => "RP2350_LINE",
        }
    }

    pub fn storage_enum(&self) -> &str {
        match self {
            Variant::F446RC => "STORAGE_C",
            Variant::F446RE => "STORAGE_E",
            Variant::F411RC => "STORAGE_C",
            Variant::F411RE => "STORAGE_E",
            Variant::F405RG => "STORAGE_G",
            Variant::F401RE => "STORAGE_E",
            Variant::F401RB => "STORAGE_B",
            Variant::F401RC => "STORAGE_C",
            Variant::RP2350 => "STORAGE_2MB",
            Variant::RP2350B => "STORAGE_2MB",
        }
    }

    pub fn flash_storage_bytes(&self) -> usize {
        self.flash_storage_kb() * 1024
    }

    pub fn flash_storage_kb(&self) -> usize {
        match self {
            Variant::F446RC => 256,
            Variant::F446RE => 512,
            Variant::F411RC => 256,
            Variant::F411RE => 512,
            Variant::F405RG => 1024,
            Variant::F401RB => 128,
            Variant::F401RC => 256,
            Variant::F401RE => 512,
            Variant::RP2350 => 2048,
            Variant::RP2350B => 2048,
        }
    }

    pub fn ram_bytes(&self) -> usize {
        self.ram_kb() * 1024
    }

    pub fn ram_kb(&self) -> usize {
        match self {
            Variant::F446RC | Variant::F446RE => 128,
            Variant::F411RC | Variant::F411RE => 128,
            Variant::F405RG => 128, // +64KB CCM RAM
            Variant::F401RB | Variant::F401RC => 64,
            Variant::F401RE => 96,
            Variant::RP2350 => RP235X_SRAM_SIZE_KB,
            Variant::RP2350B => RP235X_SRAM_SIZE_KB,
        }
    }

    pub fn supports_usb_dfu(&self) -> bool {
        match self.family() {
            Family::Stm32f4 => true,
            Family::Rp2350 => true,
        }
    }

    pub fn supports_banked_roms(&self) -> bool {
        // 72 KB RAM as requires:
        // - 64KB for total of 4 16KB banked images
        // - 4KB for logging buffer
        // - 4KB for everything else
        //
        // 96KB flash as requires:
        // - 64KB for total of 1 set of 4x16KB banked images
        // - 32KB for firmware
        self.ram_kb() > 72 && self.flash_storage_kb() >= 96
    }

    pub fn supports_multi_rom_sets(&self) -> bool {
        // Same criteria as banked roms
        self.supports_banked_roms()
    }

    pub fn ccm_ram_kb(&self) -> Option<usize> {
        // F405 has 64KB of CCM RAM, others don't
        match self {
            Variant::F405RG => Some(64),
            _ => None,
        }
    }

    pub fn define_var_sub_fam(&self) -> &str {
        match self {
            Variant::F446RC | Variant::F446RE => "#define STM32F446      1",
            Variant::F411RC | Variant::F411RE => "#define STM32F411      1",
            Variant::F405RG => "#define STM32F405      1",
            Variant::F401RE => "#define STM32F401DE    1",
            Variant::F401RB | Variant::F401RC => "#define STM32F401BC    1",
            Variant::RP2350 => "#define RP2350A        1",
            Variant::RP2350B => "#define RP2350B        1",
        }
    }

    pub fn family(&self) -> Family {
        match self {
            Variant::F446RC
            | Variant::F446RE
            | Variant::F411RC
            | Variant::F411RE
            | Variant::F405RG
            | Variant::F401RE
            | Variant::F401RB
            | Variant::F401RC => Family::Stm32f4,
            Variant::RP2350 => Family::Rp2350,
            Variant::RP2350B => Family::Rp2350,
        }
    }

    pub fn processor(&self) -> Processor {
        match self {
            Variant::F446RC | Variant::F446RE => Processor::F446,
            Variant::F411RC | Variant::F411RE => Processor::F411,
            Variant::F405RG => Processor::F405,
            Variant::F401RE => Processor::F401DE,
            Variant::F401RB | Variant::F401RC => Processor::F401BC,
            Variant::RP2350 => Processor::RP2350,
            Variant::RP2350B => Processor::RP2350,
        }
    }

    pub fn define_var_fam(&self) -> &str {
        match self.family() {
            Family::Stm32f4 => "#define STM32F4        1",
            Family::Rp2350 => "#define RP235X         1",
        }
    }

    pub fn define_var_str(&self) -> &str {
        match self {
            Variant::F446RC => "#define MCU_VARIANT    \"F446RC\"",
            Variant::F446RE => "#define MCU_VARIANT    \"F446RE\"",
            Variant::F411RC => "#define MCU_VARIANT    \"F411RC\"",
            Variant::F411RE => "#define MCU_VARIANT    \"F411RE\"",
            Variant::F405RG => "#define MCU_VARIANT    \"F405RG\"",
            Variant::F401RE => "#define MCU_VARIANT    \"F401RE\"",
            Variant::F401RB => "#define MCU_VARIANT    \"F401RB\"",
            Variant::F401RC => "#define MCU_VARIANT    \"F401RC\"",
            Variant::RP2350 => "#define MCU_VARIANT    \"RP2350\"",
            Variant::RP2350B => "#define MCU_VARIANT    \"RP2350B\"",
        }
    }

    /// Used to pass into sdrr Makefile as VARIANT
    pub fn makefile_var(&self) -> &str {
        match self {
            Variant::F446RC => "stm32f446rc",
            Variant::F446RE => "stm32f446re",
            Variant::F411RC => "stm32f411rc",
            Variant::F411RE => "stm32f411re",
            Variant::F405RG => "stm32f405rg",
            Variant::F401RE => "stm32f401re",
            Variant::F401RB => "stm32f401rb",
            Variant::F401RC => "stm32f401rc",
            Variant::RP2350 => "rp2350",
            Variant::RP2350B => "rp2350",
        }
    }

    /// Used to pass to probe-rs
    pub fn chip_id(&self) -> &str {
        match self {
            Variant::F446RC => "STM32F446RCTx",
            Variant::F446RE => "STM32F446RETx",
            Variant::F411RC => "STM32F411RCTx",
            Variant::F411RE => "STM32F411RETx",
            Variant::F405RG => "STM32F405RGTx",
            Variant::F401RE => "STM32F401RETx",
            Variant::F401RB => "STM32F401RBTx",
            Variant::F401RC => "STM32F401RCTx",
            Variant::RP2350 => "RP235X",
            Variant::RP2350B => "RP235X",
        }
    }
}