|
| 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 ExamsEce2412013Q4.sv |
| 10 | +object ExamsEce2412013Q4 extends App { |
| 11 | + ChiselStage.emitSystemVerilogFile( |
| 12 | + new ExamsEce2412013Q4, |
| 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/ece241_2013_q4 |
| 22 | +class ExamsEce2412013Q4 extends RawModule { |
| 23 | + val clk = IO(Input(Clock())) |
| 24 | + val reset = IO(Input(Bool())) // Synchronous reset to `true` |
| 25 | + val s = IO(Input(UInt(3.W))) |
| 26 | + val fr3 = IO(Output(Bool())) |
| 27 | + val fr2 = IO(Output(Bool())) |
| 28 | + val fr1 = IO(Output(Bool())) |
| 29 | + val dfr = IO(Output(Bool())) |
| 30 | + |
| 31 | + // Define states as constants |
| 32 | + val A = "b111".U(3.W) |
| 33 | + val B = "b011".U(3.W) |
| 34 | + val C = "b001".U(3.W) |
| 35 | + val D = "b000".U(3.W) |
| 36 | + |
| 37 | + // State registers with clock and reset |
| 38 | + val sState = withClockAndReset(clk, reset) { RegInit(D) } |
| 39 | + val dfrState = withClockAndReset(clk, reset) { RegInit(true.B) } |
| 40 | + |
| 41 | + // State transition logic |
| 42 | + sState := s |
| 43 | + when(s < sState) { |
| 44 | + dfrState := true.B |
| 45 | + }.elsewhen(s > sState) { |
| 46 | + dfrState := false.B |
| 47 | + } |
| 48 | + |
| 49 | + // Output logic for fr1, fr2, fr3, and dfr |
| 50 | + fr1 := !sState(2) |
| 51 | + fr2 := !sState(1) |
| 52 | + fr3 := !sState(0) |
| 53 | + dfr := dfrState |
| 54 | +} |
0 commit comments