Add game 2048
This commit is contained in:
@@ -1,28 +1,21 @@
|
||||
pub struct Buttons {}
|
||||
|
||||
impl Buttons
|
||||
{
|
||||
pub unsafe fn get_button(button_id: u8) -> bool
|
||||
{
|
||||
unsafe { *LED_IP_ADDRESS >> (button_id) & 0b1 == 0b1 }
|
||||
impl Buttons {
|
||||
pub unsafe fn get_button(button_id: u8) -> bool {
|
||||
unsafe { ((*LED_IP_ADDRESS) >> button_id) & 0b1 == 0b1 }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Leds {}
|
||||
|
||||
impl Leds
|
||||
{
|
||||
pub unsafe fn set_led(led_id: u8, state: bool)
|
||||
{
|
||||
if state
|
||||
{
|
||||
unsafe { *LED_IP_ADDRESS |= (state as u32) << (led_id + 4) }
|
||||
}
|
||||
else
|
||||
{
|
||||
unsafe { *LED_IP_ADDRESS &= !((state as u32) << (led_id + 4)) }
|
||||
impl Leds {
|
||||
pub unsafe fn set_led(led_id: u8, state: bool) {
|
||||
if state {
|
||||
unsafe { *LED_IP_ADDRESS |= (state as u64) << (led_id + 4) }
|
||||
} else {
|
||||
unsafe { *LED_IP_ADDRESS &= !((state as u64) << (led_id + 4)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const LED_IP_ADDRESS: *mut u32 = 0x30000000 as *mut u32;
|
||||
pub const LED_IP_ADDRESS: *mut u64 = 0x30000000 as *mut u64;
|
||||
|
||||
@@ -8,10 +8,11 @@ pub mod vga;
|
||||
macro_rules! panic_handler {
|
||||
() => {
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> !
|
||||
{
|
||||
loop
|
||||
{}
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
unsafe {
|
||||
$crate::vga::VGA::draw_string(0, 0, "PANIC !", $crate::vga::WHITE);
|
||||
}
|
||||
loop {}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use core::ptr::write_bytes;
|
||||
|
||||
use fpga_lib_macros::include_font_plate;
|
||||
|
||||
pub const VGA_ADDRESS: *mut Color = 0x80000000 as *mut Color;
|
||||
@@ -8,10 +10,8 @@ pub const HEIGHT: usize = 240;
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Color(pub(crate) u8);
|
||||
|
||||
impl Color
|
||||
{
|
||||
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self
|
||||
{
|
||||
impl Color {
|
||||
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
|
||||
Self(((r >> 6) << 6) | ((g >> 5) << 3) | (b >> 5))
|
||||
}
|
||||
}
|
||||
@@ -26,12 +26,10 @@ pub const BLUE: Color = Color::from_rgb(0, 0, 255);
|
||||
|
||||
pub struct VGA {}
|
||||
|
||||
impl VGA
|
||||
{
|
||||
impl VGA {
|
||||
/// # Safety
|
||||
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
|
||||
pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color)
|
||||
{
|
||||
pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color) {
|
||||
let pixel_index = x as usize + y as usize * WIDTH;
|
||||
let word_index = pixel_index / 4; // Get index of the word in which the pixel is
|
||||
let byte_index = pixel_index % 4; // Get index of the byte within the word for the pixel
|
||||
@@ -41,14 +39,10 @@ impl VGA
|
||||
|
||||
/// # Safety
|
||||
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
|
||||
pub unsafe fn draw_char(x: u16, y: u16, c: char, color: Color)
|
||||
{
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ')
|
||||
{
|
||||
pub unsafe fn draw_char(x: u16, y: u16, c: char, color: Color) {
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ') {
|
||||
b'/' - b' '
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
c as u8 - b' '
|
||||
};
|
||||
|
||||
@@ -56,10 +50,8 @@ impl VGA
|
||||
let char_x = (c as usize % 32) * FONT_WIDTH;
|
||||
let char_y = (c as usize / 32) * FONT_HEIGHT;
|
||||
|
||||
for i in 0..(FONT_WIDTH as u16)
|
||||
{
|
||||
for j in 0..(FONT_HEIGHT as u16)
|
||||
{
|
||||
for i in 0..(FONT_WIDTH as u16) {
|
||||
for j in 0..(FONT_HEIGHT as u16) {
|
||||
let xx = x + i;
|
||||
let yy = y + j;
|
||||
|
||||
@@ -73,14 +65,10 @@ impl VGA
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn draw_char_bg(x: u16, y: u16, c: char, color: Color, bg_color: Color)
|
||||
{
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ')
|
||||
{
|
||||
pub unsafe fn draw_char_bg(x: u16, y: u16, c: char, color: Color, bg_color: Color) {
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ') {
|
||||
b'/' - b' '
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
c as u8 - b' '
|
||||
};
|
||||
|
||||
@@ -88,21 +76,15 @@ impl VGA
|
||||
let char_x = (c as usize % 32) * FONT_WIDTH;
|
||||
let char_y = (c as usize / 32) * FONT_HEIGHT;
|
||||
|
||||
for i in 0..(FONT_WIDTH as u16)
|
||||
{
|
||||
for j in 0..(FONT_HEIGHT as u16)
|
||||
{
|
||||
for i in 0..(FONT_WIDTH as u16) {
|
||||
for j in 0..(FONT_HEIGHT as u16) {
|
||||
let xx = x + i;
|
||||
let yy = y + j;
|
||||
|
||||
if xx < (WIDTH as u16) && yy < (HEIGHT as u16)
|
||||
{
|
||||
if unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) }
|
||||
{
|
||||
if xx < (WIDTH as u16) && yy < (HEIGHT as u16) {
|
||||
if unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) } {
|
||||
unsafe { Self::write_pixel_unsafe(xx, yy, color) }
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
unsafe { Self::write_pixel_unsafe(xx, yy, bg_color) }
|
||||
}
|
||||
}
|
||||
@@ -112,18 +94,18 @@ impl VGA
|
||||
|
||||
/// # Safety
|
||||
/// The text must have a length that can fit within a `u16`
|
||||
pub unsafe fn draw_string(x: u16, y: u16, str: &str, color: Color)
|
||||
{
|
||||
pub unsafe fn draw_string(x: u16, y: u16, str: &str, color: Color) {
|
||||
str.chars().enumerate().for_each(|(i, c)| unsafe {
|
||||
Self::draw_char(x + i as u16 * (FONT_WIDTH as u16), y, c as char, color)
|
||||
});
|
||||
}
|
||||
pub fn clear_screen(color: Color) {
|
||||
unsafe { write_bytes(VGA_ADDRESS, color.0, WIDTH * HEIGHT) };
|
||||
}
|
||||
|
||||
fn digit_count(mut val: u64) -> u64
|
||||
{
|
||||
fn digit_count(mut val: u64) -> u64 {
|
||||
let mut i = 0;
|
||||
while val != 0
|
||||
{
|
||||
while val != 0 {
|
||||
val /= 10;
|
||||
i += 1;
|
||||
}
|
||||
@@ -131,17 +113,14 @@ impl VGA
|
||||
i
|
||||
}
|
||||
|
||||
pub unsafe fn draw_u64(x: u16, y: u16, mut val: u64, color: Color, bg_color: Color)
|
||||
{
|
||||
if val == 0
|
||||
{
|
||||
pub unsafe fn draw_u64(x: u16, y: u16, mut val: u64, color: Color, bg_color: Color) {
|
||||
if val == 0 {
|
||||
unsafe { Self::draw_char_bg(x, y, '0', color, bg_color) }
|
||||
return;
|
||||
}
|
||||
let digit_count = Self::digit_count(val) - 1;
|
||||
let mut i = digit_count as u16;
|
||||
while val != 0
|
||||
{
|
||||
while val != 0 {
|
||||
let digit = val % 10;
|
||||
val /= 10;
|
||||
|
||||
@@ -160,8 +139,7 @@ impl VGA
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn font_plate_index(x: u16, y: u16) -> bool
|
||||
{
|
||||
unsafe fn font_plate_index(x: u16, y: u16) -> bool {
|
||||
let pixel_index = (y as usize) * FONTPLATE_WIDTH + (x as usize);
|
||||
let byte_index = pixel_index / 8;
|
||||
let bit_index = pixel_index % 8;
|
||||
|
||||
Reference in New Issue
Block a user