Add game 2048

This commit is contained in:
2025-11-28 14:13:42 +01:00
parent 6190053330
commit 1c0ee18ba6
5 changed files with 31 additions and 38 deletions

View File

@@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
compile-rand = "1.0.0"
fpga-lib = { path = "../fpga-lib" } fpga-lib = { path = "../fpga-lib" }
rand = {version = "0.9.2", default-features = false, features = ["small_rng"]} rand = {version = "0.9.2", default-features = false, features = ["small_rng"]}

View File

@@ -4,7 +4,6 @@
#![feature(generic_const_exprs)] #![feature(generic_const_exprs)]
use core::{ use core::{
iter::Cloned,
mem::MaybeUninit, mem::MaybeUninit,
ops::{Index, IndexMut}, ops::{Index, IndexMut},
}; };
@@ -100,7 +99,7 @@ where
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
grid: [0; SIZE * SIZE], grid: [0; SIZE * SIZE],
rng: rand::rngs::SmallRng::seed_from_u64(9543223934), rng: rand::rngs::SmallRng::seed_from_u64(compile_rand::compile_rand!(u64)),
score: 0, score: 0,
} }
} }
@@ -208,43 +207,36 @@ where
pub fn run(&mut self) -> ! { pub fn run(&mut self) -> ! {
let mut buttons = [false; 4]; let mut buttons = [false; 4];
let funcs = [Self::left, Self::right, Self::up, Self::down]; let funcs = [Self::left, Self::right, Self::up, Self::down];
let mut lost = false;
self.draw();
loop { loop {
let mut lost = false; for direction in 0..4 {
self.draw(); let button = unsafe { fpga_lib::interface::Buttons::get_button(direction as u8) };
loop { if button && !buttons[direction] {
for direction in 0..4 { buttons[direction] = true;
let button =
unsafe { fpga_lib::interface::Buttons::get_button(direction as u8) };
if button && !buttons[direction] {
buttons[direction] = true;
if funcs[direction](self) { if funcs[direction](self) {
self.add(); self.add();
self.draw(); self.draw();
if self.lost() { if self.lost() {
lost = true; lost = true;
break; break;
}
} }
} else if !button && buttons[direction] {
buttons[direction] = false
} }
} else if !button && buttons[direction] {
buttons[direction] = false
} }
if lost { }
unsafe { if lost {
fpga_lib::vga::VGA::draw_string( unsafe {
OFFSET, fpga_lib::vga::VGA::draw_string(
OFFSET + (SIZE as u16) * BLOCK_SIZE + FONT_HEIGHT as u16 + 10, OFFSET,
"Game Over !", OFFSET + (SIZE as u16) * BLOCK_SIZE + FONT_HEIGHT as u16 + 10,
RED, "Game Over !",
) RED,
}; )
self.reset(); };
while !(0..4).any(|direction| unsafe { loop {}
fpga_lib::interface::Buttons::get_button(direction as u8)
}) {}
break;
}
} }
} }
} }

View File

@@ -20,7 +20,7 @@ set_property -dict { PACKAGE_PIN T16 IOSTANDARD LVCMOS33 } [get_ports { io_swi
set_property -dict { PACKAGE_PIN R18 IOSTANDARD LVCMOS33 } [get_ports { io_push[0] }]; #IO_L20N_T3_34 Sch=BTN0 set_property -dict { PACKAGE_PIN R18 IOSTANDARD LVCMOS33 } [get_ports { io_push[0] }]; #IO_L20N_T3_34 Sch=BTN0
set_property -dict { PACKAGE_PIN P16 IOSTANDARD LVCMOS33 } [get_ports { io_push[1] }]; #IO_L24N_T3_34 Sch=BTN1 set_property -dict { PACKAGE_PIN P16 IOSTANDARD LVCMOS33 } [get_ports { io_push[1] }]; #IO_L24N_T3_34 Sch=BTN1
set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports { io_push[2] }]; #IO_L18P_T2_34 Sch=BTN2 set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports { io_push[2] }]; #IO_L18P_T2_34 Sch=BTN2
set_property -dict { PACKAGE_PIN Y16 IOSTANDARD LVCMOS33 } [get_ports { io_push[3] }]; #IO_L7P_T1_34 Sch=BTN3 set_property -dict { PACKAGE_PIN Y16 IOSTANDARD LVCMOS33 } [get_ports { reset }]; #IO_L7P_T1_34 Sch=BTN3
# LEDs # LEDs

View File

@@ -10,7 +10,7 @@ class LedIp extends Module {
val x31 = Input(UInt(64.W)) val x31 = Input(UInt(64.W))
val switch = Input(UInt(4.W)) val switch = Input(UInt(4.W))
val led = Output(UInt(4.W)) val led = Output(UInt(4.W))
val button = Input(UInt(4.W)) val button = Input(UInt(3.W))
val bus = new BusInterface val bus = new BusInterface
}) })
@@ -27,7 +27,7 @@ class LedIp extends Module {
when(io.bus.be.asUInt.orR) { // Écriture dans le registre des leds internes when(io.bus.be.asUInt.orR) { // Écriture dans le registre des leds internes
dinReg := io.bus.wdata dinReg := io.bus.wdata
}.otherwise { // Lecture des boutons poussoirs, pas de débounce car lus par le soft }.otherwise { // Lecture des boutons poussoirs, pas de débounce car lus par le soft
btnReg := Fill(28, 0.U) ## io.button btnReg := Fill(25, 0.U) ## io.switch ## io.button
} }
} }

View File

@@ -7,7 +7,7 @@ class ZzTop(file: String = "", sim: Boolean = true) extends Module {
// Interface debug minimale // Interface debug minimale
val switch = Input(UInt(4.W)) val switch = Input(UInt(4.W))
val led = Output(UInt(4.W)) val led = Output(UInt(4.W))
val push = Input(UInt(4.W)) val push = Input(UInt(3.W))
// Debug simu pour ne pas s'embeter avec le switch dans le testbench // Debug simu pour ne pas s'embeter avec le switch dans le testbench
val x31 = if (sim) Some(Output(UInt(64.W))) else None val x31 = if (sim) Some(Output(UInt(64.W))) else None
val valid_x31 = if (sim) Some(Output(Bool())) else None val valid_x31 = if (sim) Some(Output(Bool())) else None