Skip to content

Commit 22a649b

Browse files
committed
1 parent 8e14347 commit 22a649b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 Lemmings3.sv
10+
object Lemmings3 extends App {
11+
ChiselStage.emitSystemVerilogFile(
12+
new Lemmings3,
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/Lemmings3
22+
class Lemmings3 extends RawModule {
23+
val clk = IO(Input(Clock()))
24+
val areset = IO(
25+
Input(AsyncReset())
26+
) // Freshly brainwashed Lemmings walk left.
27+
val bump_left = IO(Input(Bool()))
28+
val bump_right = IO(Input(Bool()))
29+
val ground = IO(Input(Bool()))
30+
val dig = IO(Input(Bool()))
31+
val walk_left = IO(Output(Bool()))
32+
val walk_right = IO(Output(Bool()))
33+
val aaah = IO(Output(Bool()))
34+
val digging = IO(Output(Bool()))
35+
36+
// Define states
37+
val walkingLeft :: walkingRight :: fallingLeft :: fallingRight :: diggingLeft :: diggingRight :: Nil =
38+
Enum(6)
39+
40+
// State register with clock and reset
41+
val state =
42+
withClockAndReset(clk, areset) { RegInit(walkingLeft) }
43+
44+
// State transition logic
45+
switch(state) {
46+
is(walkingLeft) {
47+
when(!ground) {
48+
state := fallingLeft
49+
}.elsewhen(dig) {
50+
state := diggingLeft
51+
}.elsewhen(bump_left) {
52+
state := walkingRight
53+
}
54+
}
55+
is(walkingRight) {
56+
when(!ground) {
57+
state := fallingRight
58+
}.elsewhen(dig) {
59+
state := diggingRight
60+
}.elsewhen(bump_right) {
61+
state := walkingLeft
62+
}
63+
}
64+
is(fallingLeft) {
65+
when(ground) {
66+
state := walkingLeft
67+
}
68+
}
69+
is(fallingRight) {
70+
when(ground) {
71+
state := walkingRight
72+
}
73+
}
74+
is(diggingLeft) {
75+
when(!ground) {
76+
state := fallingLeft
77+
}
78+
}
79+
is(diggingRight) {
80+
when(!ground) {
81+
state := fallingRight
82+
}
83+
}
84+
}
85+
86+
// Output logic
87+
walk_left := state === walkingLeft
88+
walk_right := state === walkingRight
89+
aaah := state === fallingLeft || state === fallingRight
90+
digging := state === diggingLeft || state === diggingRight
91+
}

0 commit comments

Comments
 (0)