-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVendingMachine.scala
40 lines (39 loc) · 1.05 KB
/
VendingMachine.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import chisel3._
import chisel3.util._
// Problem:
//
// Implement a vending machine using 'when' states.
// 'nickel' is a 5 cent coin
// 'dime' is 10 cent coin
// 'sOk' is reached when there are coins totalling 20 cents or more in the machine.
// The vending machine should return to the 'sIdle' state from the 'sOk' state.
//
class VendingMachine extends Module {
val io = IO(new Bundle {
val nickel = Input(Bool())
val dime = Input(Bool())
val valid = Output(Bool())
})
val sIdle :: s5 :: s10 :: s15 :: sOk :: Nil = Enum(5)
val state = RegInit(sIdle)
when (state === sIdle) {
when (io.nickel) { state := s5 }
when (io.dime) { state := s10 }
}
when (state === s5) {
when (io.nickel) { state := s10 }
when (io.dime) { state := s15 }
}
when (state === s10) {
when (io.nickel) { state := s15 }
when (io.dime) { state := sOk }
}
when (state === s15) {
when (io.nickel) { state := sOk }
when (io.dime) { state := sOk }
}
when (state === sOk) {
state := sIdle
}
io.valid := (state === sOk)
}