-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
360 lines (342 loc) · 9.7 KB
/
build.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
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use neotron_common_bios::video::{Attr, TextBackgroundColour, TextForegroundColour};
use vte::Parser;
fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
// By default, Cargo will re-run a build script whenever
// any file in the project changes. By specifying `memory.x`
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");
// Generate a file containing the firmware version
let mut output;
if let Ok(version_output) = std::process::Command::new("git")
.current_dir(env::var_os("CARGO_MANIFEST_DIR").unwrap())
.args(["describe", "--tags", "--dirty"])
.output()
{
println!(
"Version is {:?}",
std::str::from_utf8(&version_output.stdout)
);
println!("Error is {:?}", std::str::from_utf8(&version_output.stderr));
assert!(version_output.status.success());
// Remove the trailing newline
output = version_output.stdout;
output.pop();
} else {
output = String::from(env!("CARGO_PKG_VERSION")).into_bytes();
}
// Add a null
output.push(0);
// Write the file
std::fs::write(out.join("version.txt"), output).expect("writing version file");
let logo = std::fs::read_to_string("src/logo.ansi").expect("Can't open logo file");
let mut statemachine = Parser::new();
let mut performer = NeotronOutput::new();
for byte in logo.bytes() {
statemachine.advance(&mut performer, byte);
}
std::fs::write(out.join("logo.bytes"), performer.output).expect("writing logo file");
}
struct NeotronOutput {
output: Vec<u8>,
attr: Attr,
bright: bool,
}
impl NeotronOutput {
const DEFAULT_ATTR: Attr = Attr::new(
TextForegroundColour::LightGray,
TextBackgroundColour::Black,
false,
);
fn new() -> NeotronOutput {
NeotronOutput {
output: Vec::new(),
attr: Self::DEFAULT_ATTR,
bright: false,
}
}
}
impl vte::Perform for NeotronOutput {
/// Draw a character to the screen and update states.
fn print(&mut self, c: char) {
self.output.push(self.attr.as_u8());
self.output.push(map_char_to_glyph(c));
}
/// A final character has arrived for a CSI sequence
///
/// The `ignore` flag indicates that either more than two intermediates arrived
/// or the number of parameters exceeded the maximum supported length,
/// and subsequent characters were ignored.
fn csi_dispatch(
&mut self,
params: &vte::Params,
_intermediates: &[u8],
ignore: bool,
action: char,
) {
if !ignore && action == 'm' {
// Colour change
for param in params.iter() {
if param.len() != 1 {
panic!("Only want single value params, got {:?}", param);
}
match param[0] {
1 => {
self.bright = true;
}
// Background
40 => {
self.attr.set_bg(TextBackgroundColour::Black);
}
41 => {
self.attr.set_bg(TextBackgroundColour::Red);
}
42 => {
self.attr.set_bg(TextBackgroundColour::Green);
}
43 => {
self.attr.set_bg(TextBackgroundColour::Brown);
}
44 => {
self.attr.set_bg(TextBackgroundColour::Blue);
}
45 => {
self.attr.set_bg(TextBackgroundColour::Magenta);
}
46 => {
self.attr.set_bg(TextBackgroundColour::Cyan);
}
47 | 49 => {
// Default
self.attr.set_bg(TextBackgroundColour::Black);
}
// Foreground
30 => {
self.attr.set_fg(TextForegroundColour::Black);
}
31 => {
self.attr.set_fg(TextForegroundColour::Red);
}
32 => {
self.attr.set_fg(TextForegroundColour::Green);
}
33 => {
self.attr.set_fg(TextForegroundColour::Yellow);
}
34 => {
self.attr.set_fg(TextForegroundColour::Blue);
}
35 => {
self.attr.set_fg(TextForegroundColour::Magenta);
}
36 => {
self.attr.set_fg(TextForegroundColour::Cyan);
}
37 | 39 => {
self.attr.set_fg(TextForegroundColour::LightGray);
}
0 => {
self.attr = Self::DEFAULT_ATTR;
self.bright = false;
}
p => {
panic!("Unsupported ANSI CSI parameter {}", p);
}
}
}
if self.bright {
match self.attr.fg() {
TextForegroundColour::Black => {
self.attr.set_fg(TextForegroundColour::DarkGray);
}
TextForegroundColour::Red => {
self.attr.set_fg(TextForegroundColour::LightRed);
}
TextForegroundColour::Green => {
self.attr.set_fg(TextForegroundColour::LightGreen);
}
TextForegroundColour::Brown => {
self.attr.set_fg(TextForegroundColour::Yellow);
}
TextForegroundColour::Blue => {
self.attr.set_fg(TextForegroundColour::LightBlue);
}
TextForegroundColour::Magenta => {
self.attr.set_fg(TextForegroundColour::Magenta);
}
TextForegroundColour::Cyan => {
self.attr.set_fg(TextForegroundColour::LightCyan);
}
TextForegroundColour::LightGray => {
self.attr.set_fg(TextForegroundColour::White);
}
_ => {
// Do nothing
}
}
}
}
}
}
/// Convert a Unicode Scalar Value to a font glyph.
///
/// Zero-width and modifier Unicode Scalar Values (e.g. `U+0301 COMBINING,
/// ACCENT`) are not supported. Normalise your Unicode before calling
/// this function.
fn map_char_to_glyph(input: char) -> u8 {
// This fixed table only works for the default font. When we support
// changing font, we will need to plug-in a different table for each font.
match input {
'\u{0000}'..='\u{007F}' => input as u8,
'\u{00A0}' => 255, // NBSP
'\u{00A1}' => 173, // ¡
'\u{00A2}' => 189, // ¢
'\u{00A3}' => 156, // £
'\u{00A4}' => 207, // ¤
'\u{00A5}' => 190, // ¥
'\u{00A6}' => 221, // ¦
'\u{00A7}' => 245, // §
'\u{00A8}' => 249, // ¨
'\u{00A9}' => 184, // ©
'\u{00AA}' => 166, // ª
'\u{00AB}' => 174, // «
'\u{00AC}' => 170, // ¬
'\u{00AD}' => 240, // SHY
'\u{00AE}' => 169, // ®
'\u{00AF}' => 238, // ¯
'\u{00B0}' => 248, // °
'\u{00B1}' => 241, // ±
'\u{00B2}' => 253, // ²
'\u{00B3}' => 252, // ³
'\u{00B4}' => 239, // ´
'\u{00B5}' => 230, // µ
'\u{00B6}' => 244, // ¶
'\u{00B7}' => 250, // ·
'\u{00B8}' => 247, // ¸
'\u{00B9}' => 251, // ¹
'\u{00BA}' => 167, // º
'\u{00BB}' => 175, // »
'\u{00BC}' => 172, // ¼
'\u{00BD}' => 171, // ½
'\u{00BE}' => 243, // ¾
'\u{00BF}' => 168, // ¿
'\u{00C0}' => 183, // À
'\u{00C1}' => 181, // Á
'\u{00C2}' => 182, // Â
'\u{00C3}' => 199, // Ã
'\u{00C4}' => 142, // Ä
'\u{00C5}' => 143, // Å
'\u{00C6}' => 146, // Æ
'\u{00C7}' => 128, // Ç
'\u{00C8}' => 212, // È
'\u{00C9}' => 144, // É
'\u{00CA}' => 210, // Ê
'\u{00CB}' => 211, // Ë
'\u{00CC}' => 222, // Ì
'\u{00CD}' => 214, // Í
'\u{00CE}' => 215, // Î
'\u{00CF}' => 216, // Ï
'\u{00D0}' => 209, // Ð
'\u{00D1}' => 165, // Ñ
'\u{00D2}' => 227, // Ò
'\u{00D3}' => 224, // Ó
'\u{00D4}' => 226, // Ô
'\u{00D5}' => 229, // Õ
'\u{00D6}' => 153, // Ö
'\u{00D7}' => 158, // ×
'\u{00D8}' => 157, // Ø
'\u{00D9}' => 235, // Ù
'\u{00DA}' => 233, // Ú
'\u{00DB}' => 234, // Û
'\u{00DC}' => 154, // Ü
'\u{00DD}' => 237, // Ý
'\u{00DE}' => 232, // Þ
'\u{00DF}' => 225, // ß
'\u{00E0}' => 133, // à
'\u{00E1}' => 160, // á
'\u{00E2}' => 131, // â
'\u{00E3}' => 198, // ã
'\u{00E4}' => 132, // ä
'\u{00E5}' => 134, // å
'\u{00E6}' => 145, // æ
'\u{00E7}' => 135, // ç
'\u{00E8}' => 138, // è
'\u{00E9}' => 130, // é
'\u{00EA}' => 136, // ê
'\u{00EB}' => 137, // ë
'\u{00EC}' => 141, // ì
'\u{00ED}' => 161, // í
'\u{00EE}' => 140, // î
'\u{00EF}' => 139, // ï
'\u{00F0}' => 208, // ð
'\u{00F1}' => 164, // ñ
'\u{00F2}' => 149, // ò
'\u{00F3}' => 162, // ó
'\u{00F4}' => 147, // ô
'\u{00F5}' => 228, // õ
'\u{00F6}' => 148, // ö
'\u{00F7}' => 246, // ÷
'\u{00F8}' => 155, // ø
'\u{00F9}' => 151, // ù
'\u{00FA}' => 163, // ú
'\u{00FB}' => 150, // û
'\u{00FC}' => 129, // ü
'\u{00FD}' => 236, // ý
'\u{00FE}' => 231, // þ
'\u{00FF}' => 152, // ÿ
'\u{0131}' => 213, // ı
'\u{0192}' => 159, // ƒ
'\u{2017}' => 242, // ‗
'\u{2500}' => 196, // ─
'\u{2502}' => 179, // │
'\u{250C}' => 218, // ┌
'\u{2510}' => 191, // ┐
'\u{2514}' => 192, // └
'\u{2518}' => 217, // ┘
'\u{251C}' => 195, // ├
'\u{2524}' => 180, // ┤
'\u{252C}' => 194, // ┬
'\u{2534}' => 193, // ┴
'\u{253C}' => 197, // ┼
'\u{2550}' => 205, // ═
'\u{2551}' => 186, // ║
'\u{2554}' => 201, // ╔
'\u{2557}' => 187, // ╗
'\u{255A}' => 200, // ╚
'\u{255D}' => 188, // ╝
'\u{2560}' => 204, // ╠
'\u{2563}' => 185, // ╣
'\u{2566}' => 203, // ╦
'\u{2569}' => 202, // ╩
'\u{256C}' => 206, // ╬
'\u{2580}' => 223, // ▀
'\u{2584}' => 220, // ▄
'\u{2588}' => 219, // █
'\u{2591}' => 176, // ░
'\u{2592}' => 177, // ▒
'\u{2593}' => 178, // ▓
'\u{25A0}' => 254, // ■
c => panic!("Unsupported UTF-8 character U+{:04}", c as u32),
}
}