Skip to content

Commit 2cd56b0

Browse files
Use Attr from common_bios.
Now we have nice constants for the palette colours.
1 parent 61605f6 commit 2cd56b0

2 files changed

Lines changed: 59 additions & 26 deletions

File tree

src/main.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ use rp_pico::{
8383
};
8484

8585
// Other Neotron Crates
86-
use common::MemoryRegion;
86+
use common::{
87+
video::{Attr, TextBackgroundColour, TextForegroundColour},
88+
MemoryRegion,
89+
};
8790
use neotron_bmc_commands::Command;
8891
use neotron_bmc_protocol::Receivable;
8992
use neotron_common_bios as common;
@@ -1054,16 +1057,32 @@ fn sign_on() {
10541057

10551058
tc.move_to(0, 0);
10561059

1057-
tc.change_attr(vga::Attr::new(11, 4));
1060+
tc.change_attr(Attr::new(
1061+
TextForegroundColour::BRIGHT_YELLOW,
1062+
TextBackgroundColour::BLUE,
1063+
false,
1064+
));
10581065
write!(&tc, "{LOGO_TEXT}").unwrap();
10591066

1060-
tc.change_attr(vga::Attr::new(15, 0));
1067+
tc.change_attr(Attr::new(
1068+
TextForegroundColour::WHITE,
1069+
TextBackgroundColour::BLACK,
1070+
false,
1071+
));
10611072
write!(&tc, "{LICENCE_TEXT}").unwrap();
10621073

1063-
tc.change_attr(vga::Attr::new(15, 4));
1074+
tc.change_attr(Attr::new(
1075+
TextForegroundColour::WHITE,
1076+
TextBackgroundColour::BLUE,
1077+
false,
1078+
));
10641079
writeln!(&tc, "BIOS Version: {}", VERSION.trim_matches('\0')).unwrap();
10651080

1066-
tc.change_attr(vga::Attr::new(15, 1));
1081+
tc.change_attr(Attr::new(
1082+
TextForegroundColour::WHITE,
1083+
TextBackgroundColour::DARK_RED,
1084+
false,
1085+
));
10671086
let bmc_ver = critical_section::with(|cs| {
10681087
let mut lock = HARDWARE.borrow_ref_mut(cs);
10691088
let hw = lock.as_mut().unwrap();
@@ -1088,15 +1107,28 @@ fn sign_on() {
10881107
}
10891108
}
10901109

1091-
tc.change_attr(vga::Attr::new(15, 0));
1110+
tc.change_attr(Attr::new(
1111+
TextForegroundColour::WHITE,
1112+
TextBackgroundColour::BLACK,
1113+
false,
1114+
));
10921115
writeln!(&tc).unwrap();
10931116
writeln!(&tc).unwrap();
10941117

10951118
// Do a colour test
10961119
for bg in 0..=7 {
10971120
for fg in 0..=15 {
10981121
if fg != bg {
1099-
tc.change_attr(vga::Attr::new(fg, bg));
1122+
tc.change_attr(
1123+
// Safety: The loop above ensures bg and fg stay within bounds (0..=7 and 0..=15)
1124+
unsafe {
1125+
Attr::new(
1126+
TextForegroundColour::new_unchecked(fg),
1127+
TextBackgroundColour::new_unchecked(bg),
1128+
false,
1129+
)
1130+
},
1131+
);
11001132
write!(&tc, "ABCabc123#!").unwrap();
11011133
}
11021134
}

src/vga/mod.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod font8;
4141

4242
use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU16, AtomicU8, Ordering};
4343
use defmt::{debug, trace};
44+
use neotron_common_bios::video::{Attr, TextBackgroundColour, TextForegroundColour};
4445
use rp_pico::{hal::pio::PIOExt, pac::interrupt};
4546

4647
// -----------------------------------------------------------------------------
@@ -146,11 +147,6 @@ pub struct RGBPair(u32);
146147
#[derive(Copy, Clone, PartialEq, Eq)]
147148
pub struct Glyph(u8);
148149

149-
/// Represents VGA format foreground/background attributes.
150-
#[repr(transparent)]
151-
#[derive(Copy, Clone, PartialEq, Eq)]
152-
pub struct Attr(u8);
153-
154150
/// Represents a glyph/attribute pair. This is what out text console is made
155151
/// out of. They work in exactly the same way as IBM PC VGA.
156152
#[repr(transparent)]
@@ -306,6 +302,10 @@ pub mod colours {
306302
}
307303

308304
/// Holds the 256-entry palette for indexed colour modes.
305+
///
306+
/// Note, the first eight entries should match
307+
/// [`neotron_common_bios::video::TextBackgroundColour`] and the first 16 entries
308+
/// should meatch [`neotron_common_bios::video::TextForegroundColour`].
309309
pub static VIDEO_PALETTE: [AtomicU16; 256] = [
310310
// Index 000: 0x000 (Black)
311311
AtomicU16::new(RGBColour::from_12bit(0x0, 0x0, 0x0).0),
@@ -1509,7 +1509,14 @@ impl TextConsole {
15091509
current_col: AtomicU16::new(0),
15101510
text_buffer: AtomicPtr::new(core::ptr::null_mut()),
15111511
// White on Black, with the default palette
1512-
current_attr: AtomicU8::new(Attr::new(15, 0).0),
1512+
current_attr: AtomicU8::new(
1513+
Attr::new(
1514+
TextForegroundColour::WHITE,
1515+
TextBackgroundColour::BLACK,
1516+
false,
1517+
)
1518+
.0,
1519+
),
15131520
}
15141521
}
15151522

@@ -2087,18 +2094,6 @@ impl RGBPair {
20872094
}
20882095
}
20892096

2090-
impl Attr {
2091-
/// Construct a new `Attr` from a foreground and a background index.
2092-
///
2093-
/// * The `fg` value must be in the range `0..=15`.
2094-
/// * The `bg` value must be in the range `0..=7`.
2095-
pub const fn new(fg: u8, bg: u8) -> Attr {
2096-
let mut result = (bg & 0x07) << 4;
2097-
result |= fg & 0x0F;
2098-
Attr(result)
2099-
}
2100-
}
2101-
21022097
impl GlyphAttr {
21032098
/// Make a new glyph/attribute pair.
21042099
pub const fn new(glyph: Glyph, attr: Attr) -> GlyphAttr {
@@ -2148,7 +2143,13 @@ impl TextColourLookup {
21482143
fn init(&mut self, palette: &[AtomicU16]) {
21492144
for (fg, fg_colour) in palette.iter().take(16).enumerate() {
21502145
for (bg, bg_colour) in palette.iter().take(8).enumerate() {
2151-
let attr = Attr::new(fg as u8, bg as u8);
2146+
let attr = unsafe {
2147+
Attr::new(
2148+
TextForegroundColour::new_unchecked(fg as u8),
2149+
TextBackgroundColour::new_unchecked(bg as u8),
2150+
false,
2151+
)
2152+
};
21522153
for pixels in 0..=3 {
21532154
let index: usize = (((attr.0 & 0x7F) as usize) << 2) | (pixels & 0x03) as usize;
21542155
let pair = RGBPair::from_pixels(

0 commit comments

Comments
 (0)