This commit is contained in:
parent
23a77368b3
commit
a191ece9a5
50
crates/fayalite/examples/blinky.rs
Normal file
50
crates/fayalite/examples/blinky.rs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
use clap::Parser;
|
||||||
|
use fayalite::{
|
||||||
|
clock::{Clock, ClockDomain},
|
||||||
|
hdl_module,
|
||||||
|
int::{DynUInt, DynUIntType, IntTypeTrait, UInt},
|
||||||
|
reset::{SyncReset, ToReset},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[hdl_module]
|
||||||
|
fn blinky(clock_frequency: u64) {
|
||||||
|
#[hdl]
|
||||||
|
let clk: Clock = m.input();
|
||||||
|
#[hdl]
|
||||||
|
let rst: SyncReset = m.input();
|
||||||
|
let cd = #[hdl]
|
||||||
|
ClockDomain {
|
||||||
|
clk,
|
||||||
|
rst: rst.to_reset(),
|
||||||
|
};
|
||||||
|
let max_value = clock_frequency / 2 - 1;
|
||||||
|
let int_ty = DynUIntType::range_inclusive(0..=max_value);
|
||||||
|
#[hdl]
|
||||||
|
let counter: DynUInt = m.reg_builder().clock_domain(cd).reset(int_ty.literal(0));
|
||||||
|
#[hdl]
|
||||||
|
let output_reg: UInt<1> = m.reg_builder().clock_domain(cd).reset_default();
|
||||||
|
#[hdl]
|
||||||
|
if counter == int_ty.literal(max_value) {
|
||||||
|
m.connect(counter, int_ty.literal(0));
|
||||||
|
m.connect(output_reg, !output_reg);
|
||||||
|
} else {
|
||||||
|
m.connect_any(counter, counter + 1_hdl_u1);
|
||||||
|
}
|
||||||
|
#[hdl]
|
||||||
|
let led: UInt<1> = m.output();
|
||||||
|
m.connect(led, output_reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
struct Cli {
|
||||||
|
/// clock frequency in hertz
|
||||||
|
#[arg(long, default_value = "1000000", value_parser = clap::value_parser!(u64).range(2..))]
|
||||||
|
clock_frequency: u64,
|
||||||
|
#[command(subcommand)]
|
||||||
|
cli: fayalite::cli::Cli,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cli = Cli::parse();
|
||||||
|
cli.cli.run(blinky(cli.clock_frequency))
|
||||||
|
}
|
Loading…
Reference in a new issue