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
use svg::{
node::element::{Group, Rectangle, Text},
node::Text as Content,
Document, Node,
};
use super::*;
pub struct FlashVisualizer<'layout> {
flash_layout: &'layout FlashLayout,
}
impl<'layout> FlashVisualizer<'layout> {
pub(super) fn new(flash_layout: &'layout FlashLayout) -> Self {
Self { flash_layout }
}
fn memory_to_local(&self, address: u64) -> f32 {
let top_sector_address = self
.flash_layout
.sectors()
.last()
.map_or(0, |s| s.address() + s.size());
address as f32 / top_sector_address as f32 * 100.0
}
fn memory_block(&self, address: u64, size: u64, dimensions: (u32, u32)) -> Group {
let height = self.memory_to_local(size);
let start = 100.0 - self.memory_to_local(address) - height;
let mut group = Group::new();
group.append(
Rectangle::new()
.set("x", dimensions.0)
.set("y", start)
.set("width", dimensions.1)
.set("height", height),
);
group.append(
Text::new()
.set("x", dimensions.0 + 1)
.set("y", start + height - 2.0)
.set("font-size", 5)
.set("font-family", "Arial")
.set("fill", "Black")
.add(Content::new(format!("{address:#08X?}"))),
);
group.append(
Text::new()
.set("x", dimensions.0 + 1)
.set("y", start + 5.0)
.set("font-size", 5)
.set("font-family", "Arial")
.set("fill", "Black")
.add(Content::new(format!("{:#08X?}", address + size))),
);
group
}
pub fn generate_svg(&self) -> String {
let mut document = Document::new();
let mut group = Group::new().set("transform", "scale(1, 1)");
for sector in self.flash_layout.sectors() {
let rectangle = self
.memory_block(sector.address(), sector.size(), (50, 50))
.set("fill", "CornflowerBlue");
group.append(rectangle);
}
for page in self.flash_layout.pages() {
let rectangle = self
.memory_block(page.address(), page.size() as u64, (100, 50))
.set("fill", "Crimson");
group.append(rectangle);
}
for block in self.flash_layout.data_blocks() {
let rectangle = self
.memory_block(block.address(), block.size(), (150, 50))
.set("fill", "MediumSeaGreen");
group.append(rectangle);
}
for fill in self.flash_layout.fills() {
let rectangle = self
.memory_block(fill.address(), fill.size(), (150, 50))
.set("fill", "SandyBrown");
group.append(rectangle);
}
document.append(group);
document.assign("viewBox", (0, -20, 300, 140));
format!("{document}")
}
pub fn write_svg(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
use std::fs::OpenOptions;
use std::io::Write;
let svg = self.generate_svg();
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path.as_ref())?;
file.write_all(svg.as_bytes())
}
}