rename and refactor
This commit is contained in:
parent
fe0eebe60d
commit
f13c7d6eae
4 changed files with 141 additions and 85 deletions
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -142,6 +142,15 @@ dependencies = [
|
||||||
"uguid",
|
"uguid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uefi-gol"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"heapless",
|
||||||
|
"log",
|
||||||
|
"uefi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uefi-macros"
|
name = "uefi-macros"
|
||||||
version = "0.17.0"
|
version = "0.17.0"
|
||||||
|
@ -164,15 +173,6 @@ dependencies = [
|
||||||
"uguid",
|
"uguid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "uefi-textadventure"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"heapless",
|
|
||||||
"log",
|
|
||||||
"uefi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uguid"
|
name = "uguid"
|
||||||
version = "2.2.0"
|
version = "2.2.0"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "uefi-textadventure"
|
name = "uefi-gol"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
2
run.sh
2
run.sh
|
@ -5,7 +5,7 @@ set -e
|
||||||
|
|
||||||
cargo build --target x86_64-unknown-uefi
|
cargo build --target x86_64-unknown-uefi
|
||||||
mkdir -p esp/efi/boot
|
mkdir -p esp/efi/boot
|
||||||
cp target/x86_64-unknown-uefi/debug/uefi-textadventure.efi esp/efi/boot/bootx64.efi
|
cp target/x86_64-unknown-uefi/debug/uefi-gol.efi esp/efi/boot/bootx64.efi
|
||||||
qemu-system-x86_64 -enable-kvm \
|
qemu-system-x86_64 -enable-kvm \
|
||||||
-drive if=pflash,format=raw,readonly=on,file=edk2-x86_64-code.fd \
|
-drive if=pflash,format=raw,readonly=on,file=edk2-x86_64-code.fd \
|
||||||
-drive format=raw,file=fat:rw:esp
|
-drive format=raw,file=fat:rw:esp
|
||||||
|
|
190
src/main.rs
190
src/main.rs
|
@ -1,95 +1,151 @@
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
use core::{fmt::Write, iter::repeat};
|
use core::{
|
||||||
|
fmt::Write,
|
||||||
|
iter::repeat,
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
|
};
|
||||||
|
|
||||||
use heapless::{String, Vec};
|
use heapless::{String, Vec};
|
||||||
use log::info;
|
|
||||||
use system::with_stdout;
|
use system::with_stdout;
|
||||||
use uefi::prelude::*;
|
use uefi::prelude::*;
|
||||||
|
|
||||||
#[entry]
|
const WIDTH: usize = 30;
|
||||||
fn main() -> Status {
|
const HEIGHT: usize = 30;
|
||||||
uefi::helpers::init().unwrap();
|
|
||||||
info!("Hello world!");
|
|
||||||
|
|
||||||
const W: usize = 30;
|
type Matrix = Vec<Vec<bool, WIDTH>, HEIGHT>;
|
||||||
const H: usize = 30;
|
|
||||||
|
|
||||||
let mut life: Vec<Vec<bool, W>, H> = life();
|
#[derive(Debug, Clone)]
|
||||||
for (i, j) in [(15, 16), (16, 15), (16, 16), (17, 16), (17, 17)] {
|
struct Life {
|
||||||
life[i][j] = true;
|
matrix: Matrix,
|
||||||
}
|
|
||||||
|
|
||||||
for _i in 0..100_000 {
|
|
||||||
life = gol(life.clone());
|
|
||||||
let mut out: String<{ (W * H) + H }> = String::default();
|
|
||||||
for l in life.iter() {
|
|
||||||
for cell in l.iter() {
|
|
||||||
if *cell {
|
|
||||||
out.push('X').unwrap()
|
|
||||||
} else {
|
|
||||||
out.push(' ').unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out.push('\n').unwrap()
|
|
||||||
}
|
|
||||||
with_stdout(|stdout| {
|
|
||||||
stdout.reset(true).unwrap();
|
|
||||||
stdout.write_str(&out).unwrap()
|
|
||||||
});
|
|
||||||
boot::stall(100_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
boot::stall(100_000_000);
|
|
||||||
Status::SUCCESS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gol<const W: usize, const H: usize>(cells: Vec<Vec<bool, W>, H>) -> Vec<Vec<bool, W>, H> {
|
impl Deref for Life {
|
||||||
let mut future = life();
|
type Target = Matrix;
|
||||||
cells.iter().enumerate().for_each(|(row, line)| {
|
|
||||||
line.iter().enumerate().for_each(|(column, cell)| {
|
fn deref(&self) -> &Self::Target {
|
||||||
let neighbors = [
|
&self.matrix
|
||||||
(-1 as i64, -1),
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Life {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut matrix: Vec<Vec<bool, WIDTH>, HEIGHT> = Vec::default();
|
||||||
|
for _ in 0..HEIGHT {
|
||||||
|
matrix.push(repeat(false).take(WIDTH).collect()).unwrap();
|
||||||
|
}
|
||||||
|
Life { matrix }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for Life {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.matrix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Life {
|
||||||
|
fn with_starter(starter: &[(usize, usize)]) -> Self {
|
||||||
|
let mut life = Self::default();
|
||||||
|
for (i, j) in starter.iter() {
|
||||||
|
life[*i][*j] = true;
|
||||||
|
}
|
||||||
|
life
|
||||||
|
}
|
||||||
|
|
||||||
|
fn play(self) -> Life {
|
||||||
|
const OFFSETS: &[(i64, i64)] = &[
|
||||||
|
(-1, -1),
|
||||||
(-1, 0),
|
(-1, 0),
|
||||||
(-1, 1 as i64),
|
(-1, 1),
|
||||||
(0, -1),
|
(0, -1),
|
||||||
(0, 1),
|
(0, 1),
|
||||||
(1, -1),
|
(1, -1),
|
||||||
(1, 0),
|
(1, 0),
|
||||||
(1, 1),
|
(1, 1),
|
||||||
]
|
];
|
||||||
|
let mut future = Life::default();
|
||||||
|
self.iter().enumerate().for_each(|(row, line)| {
|
||||||
|
line.iter().enumerate().for_each(|(column, alive)| {
|
||||||
|
let neighbors = OFFSETS
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|offset| {
|
.filter(|offset| {
|
||||||
let neighbor = (
|
let neighbor = ((row as i64) + offset.0, (column as i64) + offset.1);
|
||||||
(row as i64).checked_add(offset.0),
|
let i = neighbor.0;
|
||||||
(column as i64).checked_add(offset.1),
|
let j = neighbor.1;
|
||||||
);
|
i >= 0
|
||||||
match neighbor {
|
&& j >= 0
|
||||||
(Some(r), Some(w))
|
&& (i as usize) < HEIGHT
|
||||||
if r >= 0
|
&& (j as usize) < WIDTH
|
||||||
&& w >= 0
|
&& self[i as usize][j as usize]
|
||||||
&& (r as usize) < H
|
|
||||||
&& (w as usize) < W
|
|
||||||
&& cells[r as usize][w as usize] =>
|
|
||||||
{
|
|
||||||
Some(())
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.count();
|
.count();
|
||||||
future[row][column] =
|
future[row][column] =
|
||||||
(*cell && (neighbors == 2 || neighbors == 3)) || (!*cell && neighbors == 3);
|
(*alive && (neighbors == 2 || neighbors == 3)) || (!*alive && neighbors == 3);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
future
|
future
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Buffers output into one string per frame since println does not buffer output in uefi-rs.
|
||||||
|
fn render(&self) -> Frame {
|
||||||
|
let mut frame = Frame::default();
|
||||||
|
for l in self.iter() {
|
||||||
|
for cell in l.iter() {
|
||||||
|
if *cell {
|
||||||
|
frame.push('X').unwrap()
|
||||||
|
} else {
|
||||||
|
frame.push(' ').unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
frame.push('\n').unwrap()
|
||||||
|
}
|
||||||
|
frame
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn life<const W: usize, const H: usize>() -> Vec<Vec<bool, W>, H> {
|
type FrameBuffer = String<{ (WIDTH * HEIGHT) + HEIGHT }>;
|
||||||
let mut future: Vec<Vec<bool, W>, H> = Vec::default();
|
|
||||||
for _ in 0..H {
|
#[derive(Default)]
|
||||||
future.push(repeat(false).take(W).collect()).unwrap();
|
struct Frame {
|
||||||
}
|
buffer: FrameBuffer,
|
||||||
future
|
}
|
||||||
|
|
||||||
|
impl Deref for Frame {
|
||||||
|
type Target = FrameBuffer;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.buffer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for Frame {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.buffer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const BREEDER: &[(usize, usize)] = &[(15, 16), (16, 15), (16, 16), (17, 16), (17, 17)];
|
||||||
|
const RENDER_DELAY: usize = 100_000;
|
||||||
|
|
||||||
|
#[entry]
|
||||||
|
fn main() -> Status {
|
||||||
|
uefi::helpers::init().unwrap();
|
||||||
|
|
||||||
|
let mut life = Life::with_starter(&BREEDER);
|
||||||
|
|
||||||
|
for _ in repeat(()) {
|
||||||
|
life = life.play();
|
||||||
|
|
||||||
|
let frame = life.render();
|
||||||
|
with_stdout(|stdout| {
|
||||||
|
stdout.reset(true).unwrap();
|
||||||
|
stdout.write_str(&frame).unwrap()
|
||||||
|
});
|
||||||
|
// Stall the loop a bit so it is easier to watch
|
||||||
|
boot::stall(RENDER_DELAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
Status::SUCCESS
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue