Skip to content

Commit e9c878d

Browse files
committed
MIPS Processor: Implemented R/I-type
Implemented and tested R-Type and I-Type instructions for the processor circuit. Implemented WrEn for instructions with an RD register. Updated Decode circuit to reflect its function. Fixed ALU and Decode to support I-Type instructions. Partially implemented stall logic between IF, Decode and Execution stages. HLL to Assembly Language conversions txt.
1 parent c05574b commit e9c878d

File tree

4 files changed

+2280
-1043
lines changed

4 files changed

+2280
-1043
lines changed

InstructionSetArch/Assembly.txt

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# RISCV Assembly Language Conversion
2+
3+
# Note: The HLL and Assembly Code below are self-generated.
4+
5+
1. Recursion
6+
7+
public int recursionTest(int n) {
8+
if (n <= 1) {
9+
return 1;
10+
} else {
11+
return n * recursionTest(n - 1);
12+
}
13+
}
14+
15+
RISC-V:
16+
17+
# a0 = n
18+
19+
recursionTest:
20+
addi t0, x0, 1
21+
bgt a0, t0, ELSE
22+
addi a0, x0, 1
23+
j ra
24+
# RETURN
25+
ELSE:
26+
addi sp, sp, -8
27+
sw a0, 0(sp)
28+
sw ra, 4(sp)
29+
subi a0, a0, 1
30+
jal ra, recursionTest
31+
32+
add t0, x0, a0 # return value from recursive call
33+
lw a0, 0(sp)
34+
lw ra, 4(sp)
35+
addi sp, sp, 8
36+
mul a0, a0, t0
37+
j ra
38+
# RETURN
39+
DONE:
40+
ret
41+
42+
43+
2. Branch
44+
45+
public int branchTest(int n) {
46+
// even
47+
if (n % 2 == 0) {
48+
return 0;
49+
} else {
50+
return 1;
51+
}
52+
}
53+
54+
RISC-V:
55+
56+
# a0 = n
57+
58+
branchTest:
59+
addi t0, x0, 1
60+
and t1, a0, t0
61+
beq t0, t1, ELSE # if a0 is odd, jump to ELSE label
62+
add a0, x0, x0
63+
ret
64+
ELSE:
65+
addi a0, x0, 1
66+
ret
67+
68+
69+
70+
3. Loop
71+
72+
public int loopTest(int n) {
73+
if (n < 0) {
74+
return 0;
75+
}
76+
int i = 1;
77+
int sum = 0;
78+
while (i <= n) {
79+
if (i % 2 == 0) {
80+
sum++;
81+
}
82+
i++;
83+
}
84+
85+
return sum;
86+
}
87+
88+
RISC-V:
89+
90+
91+
# a0 = n, t0 = i, t1 = sum
92+
93+
loopTest:
94+
bge x0, a0, LOOP
95+
add a0, x0, x0
96+
ret
97+
# RETURN
98+
LOOP:
99+
addi t0, x0, 1 # i = 1
100+
add t1, x0, x0 # sum = 0
101+
While:
102+
bgt t0, a0, END
103+
addi t2, x0, 1
104+
and t3, t2, a0 # parity check
105+
beq t3, t2, INC # odd number found
106+
addi t1, t1, 1
107+
j INC
108+
INC:
109+
addi t0, t0, 1
110+
j While
111+
112+
END:
113+
add a0, t1, x0
114+
ret
115+
116+
117+
118+
4. Caller/Callee
119+
120+
121+
// Square even numbers
122+
// return 1 otherwise
123+
public int helperFuncTest(int n) {
124+
125+
if (n % 2 == 0) {
126+
return helperTest(n);
127+
} else {
128+
return 1;
129+
}
130+
}
131+
132+
public int helperTest(int n) {
133+
return square(n);
134+
}
135+
136+
public int square(int n) {
137+
return n * n;
138+
}
139+
140+
RISC-V:
141+
142+
# a0 = n
143+
144+
helperFuncTest:
145+
addi t0, x0, 1
146+
and t1, t0, a0
147+
beq t1, t0, ELSE
148+
addi sp, sp, -8
149+
sw ra, 0(sp)
150+
sw a0, 4(sp)
151+
j helperTest
152+
lw ra, 0(sp)
153+
addi sp, sp, 8
154+
j DONE
155+
ELSE:
156+
addi a0, x0, 1
157+
j DONE
158+
DONE:
159+
j ra
160+
# RETURN
161+
162+
163+
# a0 = n
164+
165+
helperTest:
166+
addi sp, sp, -8
167+
sw a0, 4(sp)
168+
sw ra, 0(sp)
169+
j square
170+
lw ra, 0(sp)
171+
addi sp, sp, 8
172+
j ra
173+
174+
175+
# a0 = n
176+
177+
square:
178+
mul a0, a0, a0
179+
j ra
180+
181+
182+
183+

InstructionSetArch/RISCV.class

1.04 KB
Binary file not shown.

InstructionSetArch/RISCV.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.util.*;
2+
3+
public class RISCV {
4+
5+
public RISCV() {
6+
7+
}
8+
9+
/*
10+
Functions below will be converted to Aseembly Language
11+
Then, converted from Assembly Language to Machine Language
12+
Finally, the Machine Language will run through the Logisim Processor
13+
14+
1. Recursion
15+
2. Branch
16+
3. Loop
17+
4. Helper/Function Call
18+
19+
*/
20+
21+
22+
// Factorial
23+
public int recursionTest(int n) {
24+
if (n <= 1) {
25+
return 1;
26+
} else {
27+
return n * recursionTest(n - 1);
28+
}
29+
}
30+
31+
// even or odd
32+
public int branchTest(int n) {
33+
// even
34+
if (n % 2 == 0) {
35+
return 0;
36+
} else {
37+
return 1;
38+
}
39+
}
40+
41+
// return number of even numbers between 0 and n,
42+
// exclusively and inclusively
43+
public int loopTest(int n) {
44+
if (n < 0) {
45+
return 0;
46+
}
47+
int i = 1;
48+
int sum = 0;
49+
while (i <= n) {
50+
if (i % 2 == 0) {
51+
sum++;
52+
}
53+
i++;
54+
}
55+
56+
return sum;
57+
}
58+
59+
// Square even numbers
60+
// return 1 otherwise
61+
public int helperFuncTest(int n) {
62+
63+
if (n % 2 == 0) {
64+
return helperTest(n);
65+
} else {
66+
return 1;
67+
}
68+
}
69+
70+
// Helper function for helperFuncTest()
71+
// return the square of n
72+
// calls an additional function to do this
73+
public int helperTest(int n) {
74+
return square(n);
75+
}
76+
77+
// Helper function for helperTest()
78+
// demonstrates the sw/lw/sp uses
79+
// for Caller and Callee
80+
public int square(int n) {
81+
return n * n;
82+
}
83+
84+
85+
/*
86+
Calculate the Cycles per Instruction
87+
cycle == number of cycles in the pipeline
88+
jb == percentage of iunstructions that are jumps and/or branches
89+
frequency == frequency of times that branches and jumps are taken
90+
*/
91+
public double CPI(double cycle, double jb, double frequency) {
92+
return 0;
93+
}
94+
95+
public static void main(String[] args) {
96+
RISCV riscv = new RISCV();
97+
System.out.println(riscv.recursionTest(5)); // 120
98+
99+
System.out.println(riscv.branchTest(5)); // 1
100+
System.out.println(riscv.branchTest(4)); // 0
101+
102+
System.out.println(riscv.loopTest(5)); // 2
103+
System.out.println(riscv.loopTest(16)); // 8 -> [16, 14, 12, 10, 8, 6, 4, 2]
104+
105+
106+
System.out.println(riscv.helperFuncTest(2)); // 4
107+
System.out.println(riscv.helperFuncTest(7)); // 1
108+
109+
}
110+
}

0 commit comments

Comments
 (0)