Skip to content

Commit 4fe72ef

Browse files
working sw instruction
1 parent cbd1182 commit 4fe72ef

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

src/main/scala/RISCV/Decoder.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ class Decoder(val width: Int = 32) extends Module {
6060
}
6161
is(InstructionFormat.S) {
6262
io.operation := io.instruction(14,12) ## io.instruction(6,0);
63-
io.immediate := io.instruction(31,31) ## 0.U(20.W) ## io.instruction(31,25) ## io.instruction(11,7);
63+
io.immediate := Fill(21, io.instruction(31,31)) ## io.instruction(31,25) ## io.instruction(11,7);
6464
}
6565
is(InstructionFormat.B) {
6666
io.operation := io.instruction(14,12) ## io.instruction(6,0);
67-
io.immediate := io.instruction(31,31) ## 0.U(19.W) ## io.instruction(7,7) ## io.instruction(31,25) ## io.instruction(11,8) ## 0.U(1.W);
67+
io.immediate := Fill(20, io.instruction(31,31)) ## io.instruction(7,7) ## io.instruction(31,25) ## io.instruction(11,8) ## 0.U(1.W);
6868
}
6969
is(InstructionFormat.U) {
7070
io.operation := io.instruction(6,0);
7171
io.immediate := io.instruction(31,12) ## 0.U(12.W);
7272
}
7373
is(InstructionFormat.J) {
7474
io.operation := io.instruction(6,0);
75-
io.immediate := io.instruction(31,31) ## 0.U(11.W) ## io.instruction(19,12) ## io.instruction(20,20) ## io.instruction(30,21) ## 0.U(1.W);
75+
io.immediate := Fill(12, io.instruction(31,31)) ## io.instruction(19,12) ## io.instruction(20,20) ## io.instruction(30,21) ## 0.U(1.W);
7676
}
7777
}
7878
}

src/main/scala/RISCV/Main.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class Main() extends Module {
7676
memory.writePorts(0).data := io.debug_write_data;
7777

7878
val operation_buffer = RegInit(0.U(17.W));
79+
val rs1_buffer = RegInit(0.U(5.W));
80+
val rs2_buffer = RegInit(0.U(5.W));
7981
val rd_buffer = RegInit(0.U(5.W));
8082

8183
when(io.execute) {
@@ -85,11 +87,14 @@ class Main() extends Module {
8587
printf("Program Pointer: %d\n", program_pointer);
8688
printf("Data: %b\n", memory.readPorts(0).data);
8789
printf("Register 1: %b\n", regFile.io.debug_1);
90+
printf("Register 2: %b\n", regFile.io.debug_2);
8891

8992
stage := stage + 1.U;
9093

9194
when(stage === 1.U) {
9295
operation_buffer := decoder.io.operation;
96+
rs1_buffer := decoder.io.rs1;
97+
rs2_buffer := decoder.io.rs2;
9398
rd_buffer := decoder.io.rd;
9499

95100
switch(decoder.io.operation) {
@@ -102,6 +107,21 @@ class Main() extends Module {
102107

103108
printf("[LW] Rs1: %d Immediate: %b\n", decoder.io.rs1, regFile.io.out_A + decoder.io.immediate);
104109
}
110+
111+
// SW
112+
is("b010_01000_11".U) {
113+
regFile.io.read_addr_A := decoder.io.rs1;
114+
regFile.io.read_addr_B := decoder.io.rs2;
115+
116+
memory.writePorts(0).enable := true.B;
117+
memory.writePorts(0).address := regFile.io.out_A + decoder.io.immediate;
118+
memory.writePorts(0).data := regFile.io.out_B;
119+
120+
program_pointer := program_pointer + 1.U;
121+
stage := 0.U;
122+
123+
printf("[SW] Rs1: %d Rs2: %d Immediate: %b\n", decoder.io.rs1, decoder.io.rs2, regFile.io.out_A + decoder.io.immediate);
124+
}
105125

106126
// LUI
107127
is("b01101_11".U(7.W)) {

src/main/scala/RISCV/Registers.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class Registers() extends Module {
3434
val out_A = Output(UInt(32.W))
3535
val out_B = Output(UInt(32.W))
3636
val out_C = Output(UInt(32.W)) // Testing output port
37-
38-
val debug_1 = Output(UInt(32.W))
37+
38+
val debug_1 = Output(UInt(32.W));
39+
val debug_2 = Output(UInt(32.W));
3940
})
4041

4142
val regs = RegInit(VecInit(Seq.fill(32.toInt)(0.U(32.W))))
@@ -46,6 +47,7 @@ class Registers() extends Module {
4647
io.out_C := regs(io.read_addr_C)
4748

4849
io.debug_1 := regs(1);
50+
io.debug_2 := regs(2);
4951

5052
// Uncomment to print the register contents every time they are accessed
5153
//printf("Regs: [%d]=%d, [%d]=%d, WE=%b, WA=%d, IN=%d\n", io.read_addr_A, io.out_A, io.read_addr_B, io.out_B, io.write_enable, io.write_addr, io.in)

src/test/scala/RISCV/MainSpec.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ class MainSpec extends AnyFreeSpec with Matchers with ChiselSim {
1212
dut.io.debug_write_data.poke("b000000000110_00000_000_00001_0010011".U(32.W));
1313
dut.io.debug_write_address.poke(0.U);
1414

15-
dut.clock.step(1)
15+
dut.clock.step(1);
1616

1717
dut.io.debug_write.poke(true.B);
18-
dut.io.debug_write_data.poke("b000000000110_00001_000_00001_0010011".U(32.W));
18+
dut.io.debug_write_data.poke("b0000000_00001_00000_010_01000_0100011".U(32.W));
1919
dut.io.debug_write_address.poke(1.U);
2020

21-
dut.clock.step(1)
21+
dut.clock.step(1);
2222

23-
// dut.io.debug_write.poke(true.B);
24-
// dut.io.debug_write_data.poke("b000000000000_00000_010_00001_0000011".U(32.W));
25-
// dut.io.debug_write_address.poke(1.U);
23+
dut.io.debug_write.poke(true.B);
24+
dut.io.debug_write_data.poke("b000000001000_00000_010_00010_0000011".U(32.W));
25+
dut.io.debug_write_address.poke(2.U);
2626

27-
// dut.clock.step(1)
27+
dut.clock.step(1);
2828

2929
dut.io.debug_write.poke(false.B);
3030
dut.io.execute.poke(true.B);
3131

32-
dut.clock.step(1)
33-
34-
dut.clock.step(1)
35-
36-
dut.clock.step(1)
37-
38-
dut.clock.step(1)
32+
dut.clock.step(1);
3933

40-
dut.clock.step(1)
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);
4141
}
4242
}
4343
}

0 commit comments

Comments
 (0)