|
| 1 | +package hdlbits.circuits |
| 2 | + |
| 3 | +import chisel3._ |
| 4 | +import chisel3.util._ |
| 5 | + |
| 6 | +// _root_ disambiguates from package chisel3.util.circt if user imports chisel3.util._ |
| 7 | +import _root_.circt.stage.ChiselStage |
| 8 | + |
| 9 | +// Generate Verilog sources and save it in file Exams2014Q3bfsm.sv |
| 10 | +object HdlBitsExams2014Q3bfsm extends App { |
| 11 | + ChiselStage.emitSystemVerilogFile( |
| 12 | + new HdlBitsExams2014Q3bfsm, |
| 13 | + firtoolOpts = Array( |
| 14 | + "-disable-all-randomization", |
| 15 | + "-strip-debug-info" |
| 16 | + ), |
| 17 | + args = Array("--target-dir", "gen/hdlbits/circuits") |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +// https://hdlbits.01xz.net/wiki/Exams/2014_q3bfsm |
| 22 | +class HdlBitsExams2014Q3bfsm extends RawModule { |
| 23 | + val clk = IO(Input(Clock())) |
| 24 | + val reset = IO(Input(Bool())) // Synchronous reset |
| 25 | + val x = IO(Input(Bool())) |
| 26 | + val z = IO(Output(Bool())) |
| 27 | + |
| 28 | + // State registers with clock and reset |
| 29 | + val state = withClockAndReset(clk, reset) { RegInit(0.U(3.W)) } |
| 30 | + |
| 31 | + // State transition logic |
| 32 | + switch(state) { |
| 33 | + is(0.U) { |
| 34 | + state := Mux(x, 1.U, 0.U) |
| 35 | + } |
| 36 | + is(1.U) { |
| 37 | + state := Mux(x, 4.U, 1.U) |
| 38 | + } |
| 39 | + is(2.U) { |
| 40 | + state := Mux(x, 1.U, 2.U) |
| 41 | + } |
| 42 | + is(3.U) { |
| 43 | + state := Mux(x, 2.U, 1.U) |
| 44 | + } |
| 45 | + is(4.U) { |
| 46 | + state := Mux(x, 4.U, 3.U) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Output logic |
| 51 | + z := state === 4.U || state === 3.U |
| 52 | +} |
0 commit comments