Skip to content

Commit d7266c5

Browse files
Program counter
1 parent f73e96a commit d7266c5

4 files changed

Lines changed: 88 additions & 3 deletions

File tree

src/main/scala/main/Alu.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object AluOperation extends ChiselEnum {
88

99
class Alu extends Module {
1010
val io = IO(new Bundle {
11-
val enable = Input(Bool());
11+
val execute = Input(Bool());
1212

1313
val operation = Input(AluOperation());
1414
val compare = Input(Bool());
@@ -21,7 +21,7 @@ class Alu extends Module {
2121

2222
io.output := 0.U;
2323

24-
when(io.enable) {
24+
when(io.execute) {
2525
when(io.compare) {
2626
val gt = io.rs > io.rt;
2727
val eq = io.rs === io.rt;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import chisel3._
2+
import chisel3.util._
3+
import _root_.circt.stage.ChiselStage
4+
5+
class ProgramCounter extends Module {
6+
val io = IO(new Bundle {
7+
val store_nzp = Input(Bool());
8+
val nzp = Input(UInt(3.W));
9+
10+
val update = Input(Bool());
11+
val branch = Input(Bool());
12+
val jump_location = Input(UInt(8.W));
13+
val target_nzp = Input(UInt(3.W));
14+
15+
val program_counter = Output(UInt(8.W));
16+
})
17+
18+
val nzp = RegInit(0.U(3.W));
19+
20+
val program_counter = RegInit(0.U(8.W));
21+
io.program_counter := program_counter;
22+
23+
when(io.store_nzp) {
24+
nzp := io.nzp;
25+
}
26+
27+
when(io.update) {
28+
when(io.branch && (nzp & io.target_nzp) =/= 0.U) {
29+
program_counter := io.jump_location;
30+
}.otherwise {
31+
program_counter := program_counter + 1.U;
32+
}
33+
}
34+
}

src/test/scala/main/AluTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.scalatest.flatspec.AnyFlatSpec
55
class AluTest extends AnyFlatSpec with ChiselScalatestTester {
66
"Alu Add" should "work" in {
77
test(new Alu) { dut =>
8-
dut.io.enable.poke(true.B);
8+
dut.io.execute.poke(true.B);
99
dut.io.operation.poke(AluOperation.Add);
1010
dut.io.compare.poke(false.B);
1111
dut.io.rs.poke(2.U);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import chisel3._
2+
import chiseltest._
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
5+
class ProgramCounterTest extends AnyFlatSpec with ChiselScalatestTester {
6+
"Program Counter Increment" should "work" in {
7+
test(new ProgramCounter) { dut =>
8+
dut.io.update.poke(true.B);
9+
10+
dut.clock.step(1);
11+
12+
dut.io.program_counter.expect(1.U);
13+
}
14+
}
15+
16+
"Program Counter Jump" should "work" in {
17+
test(new ProgramCounter) { dut =>
18+
dut.io.store_nzp.poke(true.B);
19+
dut.io.nzp.poke(1.U);
20+
21+
dut.clock.step(1);
22+
23+
dut.io.update.poke(true.B);
24+
dut.io.branch.poke(true.B);
25+
dut.io.jump_location.poke(8.U);
26+
dut.io.target_nzp.poke(1.U);
27+
28+
dut.clock.step(1);
29+
30+
dut.io.program_counter.expect(8.U);
31+
}
32+
}
33+
34+
"Program Counter Jump Fail" should "work" in {
35+
test(new ProgramCounter) { dut =>
36+
dut.io.store_nzp.poke(true.B);
37+
dut.io.nzp.poke(1.U);
38+
39+
dut.clock.step(1);
40+
41+
dut.io.update.poke(true.B);
42+
dut.io.branch.poke(true.B);
43+
dut.io.jump_location.poke(8.U);
44+
dut.io.target_nzp.poke(2.U);
45+
46+
dut.clock.step(1);
47+
48+
dut.io.program_counter.expect(1.U);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)