Newer
Older
fmt::{self, Write},
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
#[derive(Clone, Copy)]
#[repr(packed)]
pub struct ThunkData {
di: u16,
si: u16,
bp: u16,
sp: u16,
bx: u16,
dx: u16,
cx: u16,
ax: u16,
}
impl ThunkData {
pub const STACK: usize = 0x7C00;
pub fn new() -> Self {
Self {
di: 0,
si: 0,
bp: 0,
sp: Self::STACK as u16,
bx: 0,
dx: 0,
cx: 0,
ax: 0,
}
}
pub unsafe fn save(&self) {
ptr::write((Self::STACK - 16) as *mut ThunkData, *self);
}
pub unsafe fn load(&mut self) {
*self = ptr::read((Self::STACK - 16) as *const ThunkData);
}
pub unsafe fn with(&mut self, f: extern "C" fn()) {
self.save();
f();
self.load();
}
}
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
#[derive(Clone, Copy)]
#[repr(packed)]
pub struct VgaTextBlock {
char: u8,
color: u8,
}
#[repr(u8)]
pub enum VgaTextColor {
Black = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Red = 4,
Purple = 5,
Brown = 6,
Gray = 7,
DarkGray = 8,
LightBlue = 9,
LightGreen = 10,
LightCyan = 11,
LightRed = 12,
LightPurple = 13,
Yellow = 14,
White = 15,
}
pub struct Vga {
blocks: &'static mut [VgaTextBlock],
width: usize,
height: usize,
x: usize,
y: usize,
}
impl Vga {
pub unsafe fn new(ptr: *mut VgaTextBlock, width: usize, height: usize) -> Self {
Self {
blocks: slice::from_raw_parts_mut(
ptr,
width * height
),
width,
height,
x: 0,
y: 0,
}
}
}
impl fmt::Write for Vga {
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
for c in s.chars() {
if self.x >= self.width {
self.x = 0;
self.y += 1;
}
while self.y >= self.height {
for y in 1..self.height {
for x in 0..self.width {
let i = y * self.width + x;
let j = i - self.width;
self.blocks[j] = self.blocks[i];
if y + 1 == self.height {
self.blocks[i].char = 0;
}
}
}
self.y -= 1;
}
match c {
'\r' => {
self.x = 0;
},
'\n' => {
self.x = 0;
self.y += 1;
},
_ => {
let i = self.y * self.width + self.x;
if let Some(block) = self.blocks.get_mut(i) {
block.char = c as u8;
}
}
}
self.x += 1;
}
Ok(())
}
}
pub unsafe extern "C" fn kstart(
thunk10: extern "C" fn(),
thunk13: extern "C" fn(),
thunk15: extern "C" fn(),
thunk16: extern "C" fn(),
let mut data = ThunkData::new();
data.ax = 0x03;
data.with(thunk10);
}
{
// Disable cursor
let mut data = ThunkData::new();
data.ax = 0x0100;
data.cx = 0x3F00;
data.with(thunk10);
}
let mut vga = Vga::new(0xb8000 as *mut VgaTextBlock, 80, 25);
for i in 0..vga.blocks.len() {
vga.blocks[i].char = 0;
vga.blocks[i].color =
((VgaTextColor::DarkGray as u8) << 4) |
(VgaTextColor::White as u8);
}
writeln!(vga, "Arrow keys and space select mode, enter to continue");
let mut data = ThunkData::new();
data.with(thunk16);
writeln!(
vga,
"'{}' 0x{:02X}",
(data.ax as u8) as char,
(data.ax >> 8) as u8
);
}