commit 6bf2f603e8d2174eb63aaf3aee59251cdd66fa57
parent 1f6041a7be9a4c779fe759d57da19d89853de518
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date: Sun, 24 Dec 2023 11:29:17 -0300
Added support for M in the assembler
Diffstat:
5 files changed, 68 insertions(+), 41 deletions(-)
diff --git a/riscv/riscv.go b/riscv/riscv.go
@@ -515,6 +515,9 @@ func assembleArithmetic(t assembler.ResolvedToken) (uint32, error) {
switch t.Value {
case "sub":
func7 = 0x20
+ // Avoid running the multiplication part.
+ case "add":
+ break
case "sll":
func3 = 1
case "slt":
@@ -532,6 +535,26 @@ func assembleArithmetic(t assembler.ResolvedToken) (uint32, error) {
func3 = 6
case "and":
func3 = 7
+ // Multiplication extension.
+ default:
+ func7 = 1
+ // 0 is mul.
+ switch t.Value {
+ case "mulh":
+ func3 = 1
+ case "mulsu":
+ func3 = 2
+ case "mulu":
+ func3 = 3
+ case "div":
+ func3 = 4
+ case "divu":
+ func3 = 5
+ case "rem":
+ func3 = 6
+ case "remu":
+ func3 = 7
+ }
}
code = code | (func3 << 12)
@@ -726,7 +749,7 @@ func assembleInstruction(code []uint8, addr int, t assembler.ResolvedToken) erro
var err error
switch t.Value {
- case "add", "sub", "xor", "or", "and", "sll", "srl", "sra", "slt", "sltu":
+ case "add", "sub", "xor", "or", "and", "sll", "srl", "sra", "slt", "sltu", "mul", "mulh", "mulsu", "mulu", "div", "divu", "rem", "remu":
bin, err = assembleArithmetic(t)
case "addi", "xori", "ori", "andi", "slli", "srli", "srai", "slti", "sltiu":
bin, err = assembleArithmeticImm(t)
@@ -744,6 +767,8 @@ func assembleInstruction(code []uint8, addr int, t assembler.ResolvedToken) erro
bin, err = assembleU(t)
case "ecall", "ebreak":
bin, err = assembleCall(t)
+ default:
+ return errors.New(fmt.Sprintf("Unknown instruction: %v", t.Value))
}
if err != nil {
diff --git a/riscv/riscv_test.go b/riscv/riscv_test.go
@@ -99,39 +99,6 @@ func TestChunkedMem(t *testing.T) {
}
}
-func TestArithmeticInstructions(t *testing.T) {
- // TODO: Maybe test more than only addi, add and sub?
- var m RiscV
-
- // addi x6, x0, 0x45
- m.execute(0x04500313)
- r, _ := m.GetRegister(6)
- if r != 0x45 {
- t.Fatalf("Failed addi: %x", r)
- }
-
- // addi x7, x0, 0xffffffd6 (-42)
- m.execute(0xfd600393)
- r, _ = m.GetRegister(7)
- if r != 0x00000000ffffffd6 {
- t.Fatalf("Failed addi: %x", r)
- }
-
- // add x6, x6, x7
- m.execute(0x00730333)
- r, _ = m.GetRegister(6)
- if r != 27 {
- t.Fatalf("Failed addi: %x", r)
- }
-
- // sub x6, x6, x7
- m.execute(0x40730333)
- r, _ = m.GetRegister(6)
- if r != 0x45 {
- t.Fatalf("Failed addi: %x", r)
- }
-}
-
//go:embed test.asm
var asm string
@@ -174,19 +141,19 @@ func TestAssembler(t *testing.T) {
0xfe328ea3,
0xfe329ea3,
0xfe32aea3,
- 0x04518c63,
- 0x04519a63,
- 0x0451c863,
- 0x0451d663,
- 0x0451e463,
- 0x0451f263,
+ 0x06518c63,
+ 0x06519a63,
+ 0x0651c863,
+ 0x0651d663,
+ 0x0651e463,
+ 0x0651f263,
0xf65188e3,
0xf65196e3,
0xf651c4e3,
0xf651d2e3,
0xf651e0e3,
0xf451fee3,
- 0x028001ef,
+ 0x048001ef,
0xf55ff1ef,
0x003281e7,
0xffd281e7,
@@ -196,6 +163,14 @@ func TestAssembler(t *testing.T) {
0xffffb197,
0x00000073,
0x00100073,
+ 0x027281b3,
+ 0x027291b3,
+ 0x0272a1b3,
+ 0x0272b1b3,
+ 0x0272c1b3,
+ 0x0272d1b3,
+ 0x0272e1b3,
+ 0x0272f1b3,
}
for i := 0; i < len(correctCode); i++ {
@@ -213,3 +188,6 @@ func TestAssembler(t *testing.T) {
}
}
}
+
+//func TestInstructions(t *testing.T) {
+//}
diff --git a/riscv/test-instructions.asm b/riscv/test-instructions.asm
@@ -0,0 +1,3 @@
+;;
+;; Test source file for RISC-V IM 32 bits implementation at EGG.
+;;
+\ No newline at end of file
diff --git a/riscv/test-rars.asm b/riscv/test-rars.asm
@@ -71,4 +71,14 @@ _start:
# Calls
ecall
ebreak
+
+ # Multiplication extension.
+ mul x3, x5, x7
+ mulh x3, x5, x7
+ mulhsu x3, x5, x7
+ mulhu x3, x5, x7
+ div x3, x5, x7
+ divu x3, x5, x7
+ rem x3, x5, x7
+ remu x3, x5, x7
end:
diff --git a/riscv/test.asm b/riscv/test.asm
@@ -75,4 +75,14 @@ _start:
;; Calls
ecall
ebreak
+
+ ;; Multiplication extension.
+ mul 3, 5, 7
+ mulh 3, 5, 7
+ mulsu 3, 5, 7
+ mulu 3, 5, 7
+ div 3, 5, 7
+ divu 3, 5, 7
+ rem 3, 5, 7
+ remu 3, 5, 7
end: