Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/riscv/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ coverage.out

# Local configuration files
.env
.env.local
.env.local

# Test compiled files
test_programs/compiled/
8 changes: 7 additions & 1 deletion apps/riscv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ build: build-wrapper
@echo "Building RISC-V module..."
CGO_ENABLED=1 go build -tags $(BUILD_TAGS) .

# Compile test programs
compile-test-programs:
@echo "Compiling RISC-V test programs..."
@mkdir -p test_programs/compiled
@cd test_programs && ./build_test_programs.sh

# Run tests
test: build-wrapper
test: build-wrapper compile-test-programs
@echo "Running tests..."
CGO_ENABLED=1 go test -tags $(BUILD_TAGS) -v .

Expand Down
16 changes: 16 additions & 0 deletions apps/riscv/test_programs/array_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 数组求和程序 - CKB-VM 测试用例
// 计算数组中所有元素的和

int array_sum(int* arr, int length) {
int sum = 0;
for (int i = 0; i < length; i++) {
sum += arr[i];
}
return sum;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int length = 5;
return array_sum(arr, length); // 应该返回 15
}
43 changes: 43 additions & 0 deletions apps/riscv/test_programs/build_test_programs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
# Compile RISC-V programs to ELF format for CKB-VM

echo "🔧 Compiling RISC-V programs to ELF format..."

# Check RISC-V toolchain
if ! command -v riscv64-elf-gcc &> /dev/null; then
echo "❌ riscv64-elf-gcc not found, please install RISC-V toolchain first"
exit 1
fi

# Create output directory
mkdir -p compiled

# Compile simple add program
echo "📦 Compiling simple_add.c..."
riscv64-elf-gcc -nostdlib -static -o compiled/simple_add.elf simple_add.c
if [ $? -eq 0 ]; then
echo "✅ simple_add.elf compiled successfully"
else
echo "❌ simple_add.c compilation failed"
fi

# Compile fibonacci program
echo "📦 Compiling fibonacci.c..."
riscv64-elf-gcc -nostdlib -static -o compiled/fibonacci.elf fibonacci.c
if [ $? -eq 0 ]; then
echo "✅ fibonacci.elf compiled successfully"
else
echo "❌ fibonacci.c compilation failed"
fi

# Compile array sum program
echo "📦 Compiling array_sum.c..."
riscv64-elf-gcc -nostdlib -static -o compiled/array_sum.elf array_sum.c
if [ $? -eq 0 ]; then
echo "✅ array_sum.elf compiled successfully"
else
echo "❌ array_sum.c compilation failed"
fi

echo "🎉 RISC-V programs compiled to ELF format!"
echo "📁 Compiled files are in compiled/ directory"
14 changes: 14 additions & 0 deletions apps/riscv/test_programs/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 斐波那契数列程序 - CKB-VM 测试用例
// 计算第 n 个斐波那契数

int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
// 计算第 10 个斐波那契数
return fibonacci(10); // 应该返回 55
}
9 changes: 9 additions & 0 deletions apps/riscv/test_programs/simple_add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 简单的加法程序 - CKB-VM 测试用例
// 这个程序计算 3 + 4 = 7

int main() {
int a = 3;
int b = 4;
int result = a + b;
return result; // 应该返回 7
}
65 changes: 0 additions & 65 deletions apps/riscv/vm_bronze_test.go

This file was deleted.

112 changes: 112 additions & 0 deletions apps/riscv/vm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package riscv

import (
"io"
"os"
"testing"
)

func TestSimpleReturnProgram(t *testing.T) {
// Read compiled RISC-V ELF file
file, err := os.Open("test_programs/compiled/simple_return.elf")
if err != nil {
t.Fatalf("Failed to open simple_return.elf: %v", err)
}
defer file.Close()

program, err := io.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read simple_return.elf: %v", err)
}

vm := NewVMRunner()
defer vm.Cleanup()

t.Logf("Executing simple_return program (%d bytes)", len(program))

// Execute program
err = vm.ExecuteProgram(program)
if err != nil {
t.Logf("Program execution result: %v", err)
exitCode := vm.GetExitCode()
t.Logf("Exit code: %d", exitCode)
t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode)
} else {
t.Log("Program executed successfully")
exitCode := vm.GetExitCode()
t.Logf("Exit code: %d", exitCode)
if exitCode != 0 {
t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode)
}
}
}

func TestSimpleAddProgram(t *testing.T) {
// Read compiled RISC-V ELF file
file, err := os.Open("test_programs/compiled/simple_add.elf")
if err != nil {
t.Fatalf("Failed to open simple_add.elf: %v", err)
}
defer file.Close()

program, err := io.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read simple_add.elf: %v", err)
}

vm := NewVMRunner()
defer vm.Cleanup()

t.Logf("Executing simple_add program (%d bytes)", len(program))

// Execute program
err = vm.ExecuteProgram(program)
if err != nil {
t.Logf("Program execution result: %v", err)
exitCode := vm.GetExitCode()
t.Logf("Exit code: %d", exitCode)
t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode)
} else {
t.Log("Program executed successfully")
exitCode := vm.GetExitCode()
t.Logf("Exit code: %d", exitCode)
if exitCode != 0 {
t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode)
}
}
}

func TestFibonacciProgram(t *testing.T) {
// Read compiled RISC-V ELF file
file, err := os.Open("test_programs/compiled/fibonacci.elf")
if err != nil {
t.Fatalf("Failed to open fibonacci.elf: %v", err)
}
defer file.Close()

program, err := io.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read fibonacci.elf: %v", err)
}

vm := NewVMRunner()
defer vm.Cleanup()

t.Logf("Executing fibonacci program (%d bytes)", len(program))

// Execute program
err = vm.ExecuteProgram(program)
if err != nil {
t.Logf("Program execution result: %v", err)
exitCode := vm.GetExitCode()
t.Logf("Exit code: %d", exitCode)
t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode)
} else {
t.Log("Program executed successfully")
exitCode := vm.GetExitCode()
t.Logf("Exit code: %d", exitCode)
if exitCode != 0 {
t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode)
}
}
}
Loading