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
use bytemuck::*;
use core::{num::Wrapping, ops::Neg};

const DEFAULT_LOGO: [u8; 156] = [
  0x24, 0xFF, 0xAE, 0x51, 0x69, 0x9A, 0xA2, 0x21, 0x3D, 0x84, 0x82, 0x0A, 0x84,
  0xE4, 0x09, 0xAD, 0x11, 0x24, 0x8B, 0x98, 0xC0, 0x81, 0x7F, 0x21, 0xA3, 0x52,
  0xBE, 0x19, 0x93, 0x09, 0xCE, 0x20, 0x10, 0x46, 0x4A, 0x4A, 0xF8, 0x27, 0x31,
  0xEC, 0x58, 0xC7, 0xE8, 0x33, 0x82, 0xE3, 0xCE, 0xBF, 0x85, 0xF4, 0xDF, 0x94,
  0xCE, 0x4B, 0x09, 0xC1, 0x94, 0x56, 0x8A, 0xC0, 0x13, 0x72, 0xA7, 0xFC, 0x9F,
  0x84, 0x4D, 0x73, 0xA3, 0xCA, 0x9A, 0x61, 0x58, 0x97, 0xA3, 0x27, 0xFC, 0x03,
  0x98, 0x76, 0x23, 0x1D, 0xC7, 0x61, 0x03, 0x04, 0xAE, 0x56, 0xBF, 0x38, 0x84,
  0x00, 0x40, 0xA7, 0x0E, 0xFD, 0xFF, 0x52, 0xFE, 0x03, 0x6F, 0x95, 0x30, 0xF1,
  0x97, 0xFB, 0xC0, 0x85, 0x60, 0xD6, 0x80, 0x25, 0xA9, 0x63, 0xBE, 0x03, 0x01,
  0x4E, 0x38, 0xE2, 0xF9, 0xA2, 0x34, 0xFF, 0xBB, 0x3E, 0x03, 0x44, 0x78, 0x00,
  0x90, 0xCB, 0x88, 0x11, 0x3A, 0x94, 0x65, 0xC0, 0x7C, 0x63, 0x87, 0xF0, 0x3C,
  0xAF, 0xD6, 0x25, 0xE4, 0x8B, 0x38, 0x0A, 0xAC, 0x72, 0x21, 0xD4, 0xF8, 0x07,
];

/// GBA Header struct.
///
/// For details, read [GBATEK - GBA Cartridge
/// Header](https://problemkaputt.de/gbatek.htm#gbacartridgeheader).
#[derive(Clone, Copy)]
#[repr(C)]
pub struct GBAHeader {
  /// b label
  pub start_code: [u8; 4],
  /// logo data, don't touch.
  pub logo: [u8; 156],
  /// Game's uppercase ASCII title
  pub title: [u8; 12],
  /// Game's uppercase code name
  pub game_code: [u8; 4],
  /// Manufacturer's uppercase shorthand
  pub maker_code: [u8; 2],
  /// This is just always 0x96
  pub ninety_six: u8,
  /// This is always 0 for all commercial GBA models.
  pub main_unit: u8,
  /// Generally 0, or enable bit 7 for hardware debugger support, if you have
  /// such a device.
  pub device_type: u8,
  /// zeroed space
  pub reserved_zeroed: [u8; 7],
  /// Release version of the game, just using 0 is fine.
  pub version: u8,
  /// This is set by all the other fields. Set all your fields and then call
  /// `update_checksum`.
  pub checksum: u8,
  /// more zeroed space
  pub reserved_zeroed2: [u8; 2],
}
#[test]
fn test_align() {
  assert_eq!(core::mem::align_of::<GBAHeader>(), 1);
}
unsafe impl Zeroable for GBAHeader {}
unsafe impl Pod for GBAHeader {}
impl Default for GBAHeader {
  fn default() -> Self {
    let mut header = Self::zeroed();
    header.start_code = [0xEA, 0x00, 0x00, 0x2E];
    header.logo = DEFAULT_LOGO;
    header.ninety_six = 0x96;
    header
  }
}

impl GBAHeader {
  pub fn update_checksum(&mut self) {
    self.checksum = self.calculate_checksum()
  }
  pub fn calculate_checksum(&self) -> u8 {
    let self_bytes: &[Wrapping<u8>] = cast_slice(bytes_of(self));
    self_bytes[0xA0..0xBD]
      .iter()
      .chain(Some(&Wrapping(0x19_u8)))
      .sum::<Wrapping<u8>>()
      .neg()
      .0
  }
  pub fn set_debugging(&mut self, debug: bool) {
    if debug {
      self.logo = DEFAULT_LOGO;
      self.logo[0x9C - 0x04] = 0xA5;
      self.device_type = 0b1000_0000;
    } else {
      self.logo = DEFAULT_LOGO;
      self.device_type = 0;
    }
  }
}