|
| 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 HdlBitsFsm3s.sv |
| 10 | +object HdlBitsFsm3s extends App { |
| 11 | + ChiselStage.emitSystemVerilogFile( |
| 12 | + new HdlBitsFsm3s, |
| 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/Fsm3s |
| 22 | +class HdlBitsFsm3s extends RawModule { |
| 23 | + val clk = IO(Input(Clock())) |
| 24 | + val reset = IO(Input(Bool())) // Synchronous reset to `true` |
| 25 | + val in = |
| 26 | + IO(Input(Bool())) |
| 27 | + val out = |
| 28 | + IO(Output(Bool())) |
| 29 | + |
| 30 | + val A = "b00".U(2.W) |
| 31 | + val B = "b01".U(2.W) |
| 32 | + val C = "b10".U(2.W) |
| 33 | + val D = "b11".U(2.W) |
| 34 | + |
| 35 | + val state = withClockAndReset(clk, reset) { RegInit(A) } |
| 36 | + val nextState = WireDefault(A) |
| 37 | + |
| 38 | + // State transition logic |
| 39 | + state := nextState |
| 40 | + |
| 41 | + // State flip-flops with synchronous reset |
| 42 | + switch(state) { |
| 43 | + is(A) { |
| 44 | + nextState := Mux(in, B, A) |
| 45 | + } |
| 46 | + is(B) { |
| 47 | + nextState := Mux(in, B, C) |
| 48 | + } |
| 49 | + is(C) { |
| 50 | + nextState := Mux(in, D, A) |
| 51 | + } |
| 52 | + is(D) { |
| 53 | + nextState := Mux(in, B, C) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Output logic |
| 58 | + out := Mux(state === D, true.B, false.B) |
| 59 | +} |
0 commit comments