-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
651 lines (562 loc) · 16.2 KB
/
main.rs
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// SPDX-License-Identifier: MIT or GPL-2.0
#![allow(unused_variables)]
#![allow(dead_code)]
#![deny(clippy::implicit_return)]
#![allow(clippy::needless_return)]
use clap::Parser;
use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode},
};
use serde_yaml::Value;
use std::io;
use std::time::Duration;
use std::fs;
use tui::{
backend::CrosstermBackend,
Frame,
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::Span, Terminal,
widgets::{Block, Borders, Paragraph, Cell, Row, Table},
widgets::canvas::{Canvas, Rectangle},
};
mod dt;
use crate::dt::MemoryNode;
use crate::dt::NoGoodNameYet;
mod soc;
use crate::soc::Aperture;
mod states;
fn hex_to_mib(hex: u64) -> u64
{
return hex / (2_u64.pow(10).pow(2))
}
const READABLE_COLOURS: [Color; 6] =
[
Color::LightRed,
Color::LightGreen,
Color::LightMagenta,
Color::LightYellow,
Color::LightCyan,
Color::LightBlue
];
fn render_dt_node_table<B: tui::backend::Backend>
(board: &mut soc::MPFS, nodes: Option<Vec<MemoryNode>>, frame:&mut Frame<B>, display_rect: Rect)
{
let selected_style = Style::default().add_modifier(Modifier::REVERSED);
let header_cells = ["ID", "Node Name", "Address", "Size", "HW Start", "HW End",]
.iter()
.map(|h|
return
Cell::from(*h)
.style(Style::default())
);
let header = Row::new(header_cells).height(1).bottom_margin(1);
if nodes.is_none() {
return
}
let mut data = dt::memory_nodes_to_strings(board, nodes.unwrap());
let mut labeled_data: Vec<Vec<String>> = Vec::new();
let mut label: Option<char> = Some('a');
for node in data.iter_mut() {
let mut labeled_node: Vec<String> = Vec::new();
labeled_node.push(label.unwrap().to_string());
labeled_node.append(node);
labeled_data.push(labeled_node);
label = char::from_u32(label.unwrap() as u32 + 1);
}
let rows = labeled_data.iter().map(|item| {
let cells = item.iter().map(|c|
return Cell::from(c.clone())
);
return Row::new(cells).height(1).bottom_margin(1)
});
let table =
Table::new(rows)
.header(header)
.block(
Block::default()
.borders(Borders::ALL)
)
.style(Style::default())
.highlight_style(selected_style)
.highlight_symbol(">> ")
.widths(&[
Constraint::Percentage(5),
Constraint::Percentage(19),
Constraint::Percentage(19),
Constraint::Percentage(19),
Constraint::Percentage(19),
Constraint::Percentage(19),
]);
frame.render_widget(table, display_rect);
}
fn render_seg_table<B: tui::backend::Backend>
(data: Vec<Vec<String>>, frame:&mut Frame<B>, display_rect: Rect)
{
let selected_style = Style::default().add_modifier(Modifier::REVERSED);
let header_cells =
[
"ID", "Register Name", "Description", "Bus Address",
"Register Value", "Aperture HW Start", "Aperture HW End",
"Aperature Size",
]
.iter()
.map(|h|
return
Cell::from(*h)
.style(Style::default())
);
let header = Row::new(header_cells).height(1).bottom_margin(1);
let rows = data.iter().map(|item| {
let cells = item.iter().map(|c|
return Cell::from(c.clone())
);
return Row::new(cells).height(1).bottom_margin(1)
});
let table =
Table::new(rows)
.header(header)
.block(
Block::default()
.borders(Borders::ALL)
)
.style(Style::default())
.highlight_style(selected_style)
.highlight_symbol(">> ")
.widths(&[
Constraint::Percentage(5),
Constraint::Percentage(10),
Constraint::Percentage(15),
Constraint::Percentage(12),
Constraint::Percentage(8),
Constraint::Percentage(12),
Constraint::Percentage(12),
Constraint::Percentage(12),
]);
frame.render_widget(table, display_rect);
}
#[derive(Clone)]
struct ApertureVis {
rectangle: Option<Rectangle>,
label: Option<char>,
label_x: f64,
label_y: f64
}
impl Default for ApertureVis {
fn default() -> ApertureVis {
return ApertureVis {
rectangle: None,
label: None,
label_x: 0.0,
label_y: 0.0
}
}
}
fn render_visualisation<B: tui::backend::Backend>
(board: &mut soc::MPFS, nodes: Option<Vec<MemoryNode>>, frame:&mut Frame<B>, display_rect: Rect)
{
let border: f64 = 0.5;
let mem_map_height: f64 = (display_rect.height) as f64 - 2.0 * border;
let mem_map_width = 0.67 * (display_rect.width) as f64 - 2.0 * border;
let mem_map_x = 1.0;
let mem_map_y = 0.5;
let px_per_byte: f64 = mem_map_height / board.total_system_memory as f64;
let mut aperature_colours = READABLE_COLOURS.iter();
let memory_map = Rectangle {
x: mem_map_x,
y: mem_map_y,
width: mem_map_width,
height: mem_map_height,
color: Color::White,
};
let memory_apertures = board.memory_apertures.iter();
let mut apertures: Vec<ApertureVis> = Vec::new();
let num_apertures = 6.0; // this is a fixed property of the SoC
let num_apertures = 7.0; // inc. by one for the dt node rendering
let aperature_width = mem_map_width / (num_apertures + 1.0);
let mut display_offset = aperature_width / num_apertures;
for aperature in memory_apertures {
let aperature_start = aperature.get_hw_start_addr(board.total_system_memory);
let aperature_end = aperature.get_hw_end_addr(board.total_system_memory);
let colour = *aperature_colours.next().unwrap(); // yeah, yeah this could crash
let mut aperture_vis: ApertureVis = ApertureVis {
label: aperature.reg_name.chars().last(),
..Default::default()
};
let rectangle_x = mem_map_x + display_offset;
aperture_vis.label_x = rectangle_x + 0.5 * aperature_width;
aperture_vis.label_y = mem_map_y - 0.5;
if aperature_start.is_ok() && aperature_end.is_ok() {
let aperture_y: f64 = px_per_byte * aperature_start.unwrap() as f64;
let aperture_height: f64 = px_per_byte * aperature_end.unwrap() as f64
- aperture_y;
let rectangle = Rectangle {
x: rectangle_x,
y: mem_map_y + aperture_y,
width: aperature_width,
height: aperture_height,
color: colour,
};
aperture_vis.rectangle = Some(rectangle);
}
apertures.push(aperture_vis.clone());
display_offset += aperature_width + aperature_width / num_apertures;
}
if let Some(nodes) = nodes {
let mut node_colours = READABLE_COLOURS.iter();
let mut label: Option<char> = Some('a');
for node in nodes.iter() {
let start_addr = node.get_hw_start_addr(&mut board.memory_apertures.clone());
if start_addr.is_err() {
break;
}
let colour = *node_colours.next().unwrap(); // yeah, yeah this could crash
let start_addr = start_addr.unwrap();
let mut node_vis = ApertureVis {
label,
..Default::default()
};
label = char::from_u32(label.unwrap() as u32 + 1);
let rectangle_x = mem_map_x + display_offset;
let node_y: f64 = px_per_byte * start_addr as f64;
let node_height: f64 = px_per_byte * (node.size as f64 - 1.0);
let rectangle_y = mem_map_y + node_y;
node_vis.label_x = rectangle_x + 0.5 * aperature_width;
node_vis.label_y = rectangle_y + node_height / 2.0 - 0.5;
let rectangle = Rectangle {
x: rectangle_x,
y: rectangle_y,
width: aperature_width,
height: node_height,
color: colour,
};
node_vis.rectangle = Some(rectangle);
apertures.push(node_vis.clone());
}
}
let canvas =
Canvas::default()
.block(
Block::default()
.borders(Borders::ALL)
.title(format!(
"System memory available: {:#010x?} ({} MiB)",
board.total_system_memory,
hex_to_mib(board.total_system_memory)
)
)
)
.paint(|ctx| {
ctx.draw(&memory_map);
for aperture in &apertures {
if aperture.label.is_some() {
ctx.print(
aperture.label_x,
aperture.label_y,
Span::styled(
format!("{}",
aperture.label
.as_ref()
.unwrap()
),
Style::default()
)
);
}
if aperture.rectangle.is_none() {
continue;
}
ctx.draw(aperture.rectangle.as_ref().unwrap());
}
ctx.print(
mem_map_x + mem_map_width + 1.25,
mem_map_y - 0.5,
Span::styled(format!("{:#010x?}", 0_u64),
Style::default()),
);
ctx.print(
mem_map_x + mem_map_width + 1.25,
mem_map_y + mem_map_height / 2.0,
Span::styled(format!("{:#010x?}",
board.total_system_memory / 2),
Style::default()),
);
ctx.print(
mem_map_x + mem_map_width + 1.25,
mem_map_y + mem_map_height,
Span::styled(format!("{:#010x?}",
board.total_system_memory),
Style::default()),
);
}
)
.x_bounds([0.0, display_rect.width as f64])
.y_bounds([0.0, display_rect.height as f64]);
frame.render_widget(canvas, display_rect);
}
fn format_table_data(board: &mut soc::MPFS) -> (Vec<Vec<String>>, Result<(), ()>)
{
let mut config_is_valid: Vec<bool> = Vec::new();
let mut data: Vec<Vec<String>> = Vec::new();
for memory_aperture in &board.memory_apertures {
let aperature_start = memory_aperture.get_hw_start_addr(board.total_system_memory);
let aperature_end = memory_aperture.get_hw_end_addr(board.total_system_memory);
let mut row_cells: Vec<String> = Vec::new();
row_cells.push(data.len().to_string());
row_cells.push(memory_aperture.reg_name.clone());
row_cells.push(memory_aperture.description.clone());
row_cells.push(format!("{:#012x?}", memory_aperture.bus_addr));
row_cells.push(
format!("{:#08x?}",
soc::hw_start_addr_to_seg(
memory_aperture.get_hw_start_addr(u64::MAX).unwrap(),
memory_aperture.bus_addr)
)
);
if aperature_start.is_err() || aperature_end.is_err() {
row_cells.push("invalid".to_string());
row_cells.push("invalid".to_string());
row_cells.push("n/a MiB".to_string());
config_is_valid.push(false);
} else {
let start = aperature_start.as_ref().unwrap();
let end = aperature_end.as_ref().unwrap();
let size = end - start;
row_cells.push(format!("{:#012x?}", start));
row_cells.push(format!("{:#012x?}", end));
row_cells.push(format!("{} MiB", hex_to_mib(size)));
}
data.push(row_cells.clone());
}
if config_is_valid.len() != board.memory_apertures.len() {
return (data, Ok(()))
}
else {
return (data, Err(()))
}
}
fn render_seg_regs<T, G, B: tui::backend::Backend>
(board: &mut soc::MPFS, config_is_valid: Result<T,G>, frame:&mut Frame<B>, display_rect: Rect)
{
let mut output;
if config_is_valid.is_ok() {
output = "seg-reg-config: { ".to_string();
for memory_aperture in &board.memory_apertures {
output += &format!(
"{}: {:#x?}, ",
memory_aperture.reg_name,
soc::hw_start_addr_to_seg(memory_aperture.hardware_addr,
memory_aperture.bus_addr)
).to_string();
}
output += "}\n";
} else {
output = "Cannot calculate seg registers, configuration is invalid as \
no memory is mapped.".to_string();
}
let segs =
Paragraph::new(output)
.block(
Block::default()
.title("For insertion into config.yaml:")
.borders(Borders::ALL))
.style(Style::default());
frame.render_widget(segs, display_rect);
}
fn render_display<B: tui::backend::Backend>
(board: &mut soc::MPFS, memory_nodes: Option<Vec<MemoryNode>>,
frame: &mut Frame<B>, display_rect: Rect)
{
let chunks =
Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(90),
Constraint::Percentage(10),
]
.as_ref(),
)
.split(display_rect);
let display_area =
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage(33),
Constraint::Percentage(67),
]
.as_ref(),
)
.split(chunks[0]);
let table_area =
Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(60),
Constraint::Percentage(40),
]
.as_ref(),
)
.split(display_area[1]);
let (data, config_is_valid) = format_table_data(board);
render_seg_regs(board, config_is_valid, frame, chunks[1]);
render_seg_table(data, frame, table_area[0]);
render_dt_node_table(board, memory_nodes.clone(), frame, table_area[1]);
render_visualisation(board, memory_nodes, frame, display_area[0]);
}
fn setup_segs_from_config(board: &mut soc::MPFS, input_file: String)
-> Result<(), Box<dyn std::error::Error>>
{
let contents = fs::read_to_string(input_file);
if let Err(error) = &contents {
return Ok(())
}
let d: Value = serde_yaml::from_str(&contents.unwrap())?;
let seg_config = d["seg-reg-config"].clone();
let apertures = board.memory_apertures.iter_mut();
for aperture in apertures {
let seg_name = aperture.reg_name.as_str();
let seg_string = seg_config[seg_name].clone();
if seg_string.as_str().is_some() {
let seg_string_raw = seg_string.as_str().unwrap();
let seg_string_trimmed = seg_string_raw.trim_start_matches("0x");
let seg = u64::from_str_radix(seg_string_trimmed, 16)?;
aperture.set_hw_start_addr_from_seg(
board.total_system_memory,
seg
)?;
}
}
return Ok(());
}
use std::io::Write;
fn save_segs_to_config(board: &mut soc::MPFS, input_file: String, output_file: String)
-> Result<(), Box<dyn std::error::Error>>
{
let contents = fs::read_to_string(input_file);
if let Err(error) = contents {
return Err(Box::new(error))
}
let mut d: Value = serde_yaml::from_str(&contents.unwrap())?;
for memory_aperture in &board.memory_apertures {
let seg_value =
format!("{:#x?}",
soc::hw_start_addr_to_seg(memory_aperture.hardware_addr,
memory_aperture.bus_addr)
);
let seg_as_yaml = Value::String(seg_value);
d["seg-reg-config"][&memory_aperture.reg_name[..]] = seg_as_yaml;
}
let output = serde_yaml::to_string(&d);
let mut file = fs::File::create(output_file)?;
file.write_all(output.unwrap()[..].as_bytes())?;
return Ok(())
}
fn handle_messages(messages: &mut Vec<String>) -> Option<String>
{
if messages.is_empty(){
return None;
}
let message = messages.pop();
messages.clear();
message.as_ref()?;
let input = message.as_ref().unwrap();
return Some(input.to_string());
}
/// PolarFire SoC memory aperture configurator
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// input yaml config file
#[clap(short, long, default_value = "config.yaml")]
config: String,
/// input dtb
#[clap(short, long)]
dtb: Option<String>,
/// edit the config in place rather tha use the default output of "generated.yaml"
#[clap(short, long)]
in_place: bool,
}
fn main() -> Result<(),Box<dyn std::error::Error>> {
let args = Args::parse();
let mut next_state = states::State::default();
let mut board = soc::MPFS::default();
let stdout = io::stdout();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut input: String = String::new();
let mut messages: Vec<String> = Vec::new();
let input_file = args.config;
let mut output_file = "generated.yaml".to_string();
let mut memory_nodes: Option<Vec<MemoryNode>> = None;
if args.in_place {
output_file = input_file.clone();
}
if let Some(dtb_file) = args.dtb {
memory_nodes = dt::dtb_get_memory_nodes(dtb_file)?;
}
setup_segs_from_config(&mut board, input_file.clone())?;
terminal.clear()?;
enable_raw_mode()?;
terminal.clear()?;
loop {
let command_text = next_state.command_text.clone();
terminal.draw(|frame| {
let entire_window =
Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(90),
Constraint::Percentage(10),
]
.as_ref(),
)
.split(frame.size());
render_display(&mut board, memory_nodes.clone(), frame, entire_window[0]);
let txt = format!("{}\n{}", command_text, input);
let graph =
Paragraph::new(txt)
.block(
Block::default()
.title("Press Esc to quit, enter \"save\" to save.")
.borders(Borders::ALL))
.style(Style::default());
frame.render_widget(graph, entire_window[1]);
})?;
if event::poll(Duration::from_millis(30))? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char(c) => {
input.push(c);
}
KeyCode::Backspace => {
input.pop();
}
KeyCode::Esc => {
terminal.clear()?;
if disable_raw_mode().is_err() {
panic!("Failed to clean up terminal");
}
return Ok(());
}
KeyCode::Enter => {
messages.push(input.drain(..).collect());
}
_ => {}
}
}
}
let input = handle_messages(&mut messages);
if let Some(command) = input.clone() {
if command.contains("save") {
save_segs_to_config(&mut board, input_file.clone(), output_file.clone())?;
continue;
}
}
next_state = states::get_next_state(next_state, &mut board, input);
}
}