|
| 1 | +use ratatui::{ |
| 2 | + Frame, |
| 3 | + layout::{Constraint, Margin, Rect}, |
| 4 | + style::Stylize, |
| 5 | + widgets::{Block, Cell, Padding, Row, Table}, |
| 6 | +}; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct Memory { |
| 10 | + pub physical_memory_array: PhysicalMemoryArray, |
| 11 | +} |
| 12 | + |
| 13 | +impl Memory { |
| 14 | + pub fn render(&mut self, frame: &mut Frame, block: Rect) { |
| 15 | + self.physical_memory_array.render(frame, block); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +pub struct PhysicalMemoryArray { |
| 21 | + location: Location, |
| 22 | + function: Function, |
| 23 | + error_correction: ErrorCorrection, |
| 24 | + max_capacity: String, |
| 25 | + error_information_handle: Option<u16>, |
| 26 | + number_memory_devices: u16, |
| 27 | +} |
| 28 | + |
| 29 | +impl From<&[u8]> for PhysicalMemoryArray { |
| 30 | + fn from(data: &[u8]) -> Self { |
| 31 | + let max_capacity = { |
| 32 | + let value = u32::from_le_bytes(data[3..7].try_into().unwrap()); |
| 33 | + |
| 34 | + if value == 0x80008000 { |
| 35 | + format!("{}T", u64::from_le_bytes(data[11..19].try_into().unwrap())) |
| 36 | + } else { |
| 37 | + match value { |
| 38 | + value if value <= 1024 => { |
| 39 | + format!("{value}K") |
| 40 | + } |
| 41 | + value if value <= 1024 * 1024 => { |
| 42 | + format!("{}M", value / 1024) |
| 43 | + } |
| 44 | + _ => { |
| 45 | + format!("{}G", value / 1024 / 1024) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + let error_information_handle = { |
| 51 | + let value = u16::from_le_bytes(data[7..9].try_into().unwrap()); |
| 52 | + |
| 53 | + if value == 0xFFFE { None } else { Some(value) } |
| 54 | + }; |
| 55 | + |
| 56 | + let number_memory_devices = u16::from_le_bytes(data[9..11].try_into().unwrap()); |
| 57 | + |
| 58 | + Self { |
| 59 | + location: Location::from(data[0]), |
| 60 | + function: Function::from(data[1]), |
| 61 | + error_correction: ErrorCorrection::from(data[2]), |
| 62 | + max_capacity, |
| 63 | + error_information_handle, |
| 64 | + number_memory_devices, |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl PhysicalMemoryArray { |
| 70 | + pub fn render(&self, frame: &mut Frame, block: Rect) { |
| 71 | + let rows = vec![ |
| 72 | + Row::new(vec![ |
| 73 | + Cell::from("Location").bold(), |
| 74 | + Cell::from(self.location.to_string()), |
| 75 | + ]), |
| 76 | + Row::new(vec![ |
| 77 | + Cell::from("Use").bold(), |
| 78 | + Cell::from(self.function.to_string()), |
| 79 | + ]), |
| 80 | + Row::new(vec![ |
| 81 | + Cell::from("Error Correction").bold(), |
| 82 | + Cell::from(self.error_correction.to_string()), |
| 83 | + ]), |
| 84 | + Row::new(vec![ |
| 85 | + Cell::from("Maximum Capacity").bold(), |
| 86 | + Cell::from(self.max_capacity.clone()), |
| 87 | + ]), |
| 88 | + Row::new(vec![ |
| 89 | + Cell::from("Error Information Handle ").bold(), |
| 90 | + Cell::from({ |
| 91 | + if let Some(v) = self.error_information_handle { |
| 92 | + v.to_string() |
| 93 | + } else { |
| 94 | + "Not Provided".to_string() |
| 95 | + } |
| 96 | + }), |
| 97 | + ]), |
| 98 | + Row::new(vec![ |
| 99 | + Cell::from("Number Of Devices").bold(), |
| 100 | + Cell::from(self.number_memory_devices.to_string()), |
| 101 | + ]), |
| 102 | + ]; |
| 103 | + |
| 104 | + let widths = [Constraint::Length(30), Constraint::Fill(1)]; |
| 105 | + let table = Table::new(rows, widths).block(Block::new().padding(Padding::uniform(2))); |
| 106 | + frame.render_widget(table, block.inner(Margin::new(2, 0))); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Debug, strum::Display)] |
| 111 | +enum Location { |
| 112 | + #[strum(to_string = "Other")] |
| 113 | + Other, |
| 114 | + #[strum(to_string = "Unknown")] |
| 115 | + Unknown, |
| 116 | + #[strum(to_string = "System board or motherboard")] |
| 117 | + SystemBoard, |
| 118 | + #[strum(to_string = "ISA addon-on card")] |
| 119 | + Isa, |
| 120 | + #[strum(to_string = "EISA addon-on card")] |
| 121 | + Eisa, |
| 122 | + #[strum(to_string = "PCI addon-on card")] |
| 123 | + Pci, |
| 124 | + #[strum(to_string = "MCA addon-on card")] |
| 125 | + Mca, |
| 126 | + #[strum(to_string = "PCMCIA addon-on card")] |
| 127 | + Pcmcia, |
| 128 | + #[strum(to_string = "Proprietary addon-on card")] |
| 129 | + Proprietary, |
| 130 | + #[strum(to_string = "NuBus")] |
| 131 | + NuBus, |
| 132 | + #[strum(to_string = "PC-98/C20 add-on card")] |
| 133 | + Pc98C20, |
| 134 | + #[strum(to_string = "PC-98/C24 add-on card")] |
| 135 | + Pc98C24, |
| 136 | + #[strum(to_string = "PC-98/E add-on card")] |
| 137 | + Pc98E, |
| 138 | + #[strum(to_string = "PC-98/Local bus add-on card")] |
| 139 | + Pc98Local, |
| 140 | + #[strum(to_string = "CXL add-on card")] |
| 141 | + Cxl, |
| 142 | +} |
| 143 | + |
| 144 | +impl From<u8> for Location { |
| 145 | + fn from(value: u8) -> Self { |
| 146 | + match value { |
| 147 | + 1 => Self::Other, |
| 148 | + 2 => Self::Unknown, |
| 149 | + 3 => Self::SystemBoard, |
| 150 | + 4 => Self::Isa, |
| 151 | + 5 => Self::Eisa, |
| 152 | + 6 => Self::Pci, |
| 153 | + 7 => Self::Mca, |
| 154 | + 8 => Self::Pcmcia, |
| 155 | + 9 => Self::Proprietary, |
| 156 | + 10 => Self::NuBus, |
| 157 | + 11 => Self::Pc98C20, |
| 158 | + 12 => Self::Pc98C24, |
| 159 | + 13 => Self::Pc98E, |
| 160 | + 14 => Self::Pc98Local, |
| 161 | + 15 => Self::Cxl, |
| 162 | + _ => unreachable!(), |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +#[derive(Debug, strum::Display)] |
| 168 | +enum Function { |
| 169 | + #[strum(to_string = "Other")] |
| 170 | + Other, |
| 171 | + #[strum(to_string = "Unknown")] |
| 172 | + Unknown, |
| 173 | + #[strum(to_string = "System Memory")] |
| 174 | + SystemMemory, |
| 175 | + #[strum(to_string = "Video Memory")] |
| 176 | + VideoMemory, |
| 177 | + #[strum(to_string = "Flash Memory")] |
| 178 | + FlashMemory, |
| 179 | + #[strum(to_string = "Non-volatile RAM")] |
| 180 | + NonVolatileRAM, |
| 181 | + #[strum(to_string = "Cache Memory")] |
| 182 | + CacheMemory, |
| 183 | +} |
| 184 | + |
| 185 | +impl From<u8> for Function { |
| 186 | + fn from(value: u8) -> Self { |
| 187 | + match value { |
| 188 | + 1 => Self::Other, |
| 189 | + 2 => Self::Unknown, |
| 190 | + 3 => Self::SystemMemory, |
| 191 | + 4 => Self::VideoMemory, |
| 192 | + 5 => Self::FlashMemory, |
| 193 | + 6 => Self::NonVolatileRAM, |
| 194 | + 7 => Self::CacheMemory, |
| 195 | + _ => unreachable!(), |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +#[derive(Debug, strum::Display)] |
| 201 | +enum ErrorCorrection { |
| 202 | + #[strum(to_string = "Other")] |
| 203 | + Other, |
| 204 | + #[strum(to_string = "Unknown")] |
| 205 | + Unknown, |
| 206 | + #[strum(to_string = "None")] |
| 207 | + None, |
| 208 | + #[strum(to_string = "Parity")] |
| 209 | + Parity, |
| 210 | + #[strum(to_string = "Single-bit ECC")] |
| 211 | + SingleBitECC, |
| 212 | + #[strum(to_string = "Multi-bit ECC")] |
| 213 | + MultiBitECC, |
| 214 | + #[strum(to_string = "CRC")] |
| 215 | + Crc, |
| 216 | +} |
| 217 | + |
| 218 | +impl From<u8> for ErrorCorrection { |
| 219 | + fn from(value: u8) -> Self { |
| 220 | + match value { |
| 221 | + 1 => Self::Other, |
| 222 | + 2 => Self::Unknown, |
| 223 | + 3 => Self::None, |
| 224 | + 4 => Self::Parity, |
| 225 | + 5 => Self::SingleBitECC, |
| 226 | + 6 => Self::MultiBitECC, |
| 227 | + 7 => Self::Crc, |
| 228 | + _ => unreachable!(), |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments