Skip to content

Commit 4268b7c

Browse files
working fib loop cacluation
1 parent 5f22059 commit 4268b7c

File tree

6 files changed

+55
-32
lines changed

6 files changed

+55
-32
lines changed

programs/example.bin

32 Bytes
Binary file not shown.

programs/fib-loop.bin

60 Bytes
Binary file not shown.

programs/fib.bin

48 Bytes
Binary file not shown.

src/main/scala/RISCV/Main.scala

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Main() extends Module {
2828

2929
val program_pointer = RegInit(0.U(32.W));
3030

31-
val memory = SRAM(1024, UInt(8.W), 8, 4, 0);
31+
val memory = SRAM(512000, UInt(8.W), 8, 4, 0);
3232

3333
// Set up register file
3434
val registers = Module(new Registers())
@@ -104,11 +104,22 @@ class Main() extends Module {
104104
when(io.execute) {
105105
printf("\n");
106106
printf("Stage: %d\n", stage);
107-
printf("Operation: %b\n", decoder.io.operation);
108-
printf("Program Pointer: %d\n", program_pointer);
109-
printf("Data: %b\n", memory.readPorts(3).data ## memory.readPorts(2).data ## memory.readPorts(1).data ## memory.readPorts(0).data);
110-
printf("Register 1: %b\n", registers.io.debug_1);
111-
printf("Register 2: %b\n", registers.io.debug_2);
107+
108+
when(stage =/= 0.U) {
109+
printf("Operation: %b\n", decoder.io.operation);
110+
printf("Program Pointer: %d\n", program_pointer);
111+
printf("Data: %b\n", memory.readPorts(3).data ## memory.readPorts(2).data ## memory.readPorts(1).data ## memory.readPorts(0).data);
112+
printf("Register 1: %b\n", registers.io.debug_1);
113+
printf("Register 2: %b\n", registers.io.debug_2);
114+
printf("Register 3: %b\n", registers.io.debug_3);
115+
printf("Register 4: %b\n", registers.io.debug_4);
116+
printf("Register 5: %b\n", registers.io.debug_5);
117+
printf("Register 6: %b\n", registers.io.debug_6);
118+
printf("Register 7: %b\n", registers.io.debug_7);
119+
printf("Register 8: %b\n", registers.io.debug_8);
120+
printf("Register 9: %b\n", registers.io.debug_9);
121+
printf("Register10: %b\n", registers.io.debug_10);
122+
}
112123

113124
stage := stage + 1.U;
114125

src/main/scala/RISCV/Registers.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class Registers() extends Module {
3737

3838
val debug_1 = Output(UInt(32.W));
3939
val debug_2 = Output(UInt(32.W));
40+
val debug_3 = Output(UInt(32.W));
41+
val debug_4 = Output(UInt(32.W));
42+
val debug_5 = Output(UInt(32.W));
43+
val debug_6 = Output(UInt(32.W));
44+
val debug_7 = Output(UInt(32.W));
45+
val debug_8 = Output(UInt(32.W));
46+
val debug_9 = Output(UInt(32.W));
47+
val debug_10 = Output(UInt(32.W));
4048
})
4149

4250
val regs = RegInit(VecInit(Seq.fill(32.toInt)(0.U(32.W))))
@@ -48,6 +56,15 @@ class Registers() extends Module {
4856

4957
io.debug_1 := regs(1);
5058
io.debug_2 := regs(2);
59+
io.debug_3 := regs(3);
60+
io.debug_4 := regs(4);
61+
io.debug_5 := regs(5);
62+
io.debug_5 := regs(5);
63+
io.debug_6 := regs(6);
64+
io.debug_7 := regs(7);
65+
io.debug_8 := regs(8);
66+
io.debug_9 := regs(9);
67+
io.debug_10 := regs(10);
5168

5269
// Uncomment to print the register contents every time they are accessed
5370
//printf("Regs: [%d]=%d, [%d]=%d, WE=%b, WA=%d, IN=%d\n", io.read_address_a, io.out_a, io.read_address_b, io.out_b, io.write_enable, io.write_address, io.in)

src/test/scala/RISCV/MainSpec.scala

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,37 @@ import chisel3._
44
import chisel3.simulator.scalatest.ChiselSim
55
import org.scalatest.freespec.AnyFreeSpec
66
import org.scalatest.matchers.must.Matchers
7+
import scala.io.Source
8+
import java.nio.file.{Files, Paths}
9+
import java.nio.ByteBuffer
710

811
class MainSpec extends AnyFreeSpec with Matchers with ChiselSim {
912
"Main should execute LUI correctly" in {
1013
simulate(new Main()) { dut =>
11-
dut.io.debug_write.poke(true.B);
12-
dut.io.debug_write_data.poke("b000000000011_00000_000_00001_0010011".U(32.W));
13-
dut.io.debug_write_addressess.poke(0.U);
14-
15-
dut.clock.step(1);
16-
17-
dut.io.debug_write.poke(true.B);
18-
dut.io.debug_write_data.poke("b000000000111_00000_000_00010_0010011".U(32.W));
19-
dut.io.debug_write_addressess.poke(4.U);
20-
21-
dut.clock.step(1);
22-
23-
dut.io.debug_write.poke(true.B);
24-
dut.io.debug_write_data.poke("b0000000_00010_00001_001_00100_1100011".U(32.W));
25-
dut.io.debug_write_addressess.poke(8.U);
26-
27-
dut.clock.step(1);
14+
val bytes = Files.readAllBytes(Paths.get("./programs/fib-loop.bin"))
15+
16+
val instructions = bytes.grouped(4).map { instrBytes =>
17+
val paddedBytes = instrBytes.padTo(4, 0.toByte)
18+
val signedInt = ByteBuffer.wrap(paddedBytes).order(java.nio.ByteOrder.BIG_ENDIAN).getInt()
19+
signedInt.toLong & 0xFFFFFFFFL
20+
}.toSeq
21+
22+
dut.io.debug_write.poke(true.B)
23+
instructions.zipWithIndex.foreach { case (instr, idx) =>
24+
println(s"Instruction: ${instr.toBinaryString.reverse.padTo(32, '0').reverse}")
25+
println(s"Address: ${idx * 4}")
26+
27+
dut.io.debug_write_data.poke(instr.U(32.W))
28+
dut.io.debug_write_addressess.poke((idx * 4).U)
29+
dut.clock.step(1)
30+
}
2831

2932
dut.io.debug_write.poke(false.B);
3033
dut.io.execute.poke(true.B);
3134

3235
dut.clock.step(1);
3336

34-
dut.clock.step(1);
35-
dut.clock.step(1);
36-
dut.clock.step(1);
37-
dut.clock.step(1);
38-
dut.clock.step(1);
39-
dut.clock.step(1);
40-
dut.clock.step(1);
41-
dut.clock.step(1);
42-
dut.clock.step(1);
37+
dut.clock.step(50);
4338
}
4439
}
4540
}

0 commit comments

Comments
 (0)