Added unit testing
This commit is contained in:
parent
ce1729d5b1
commit
72dda9aaeb
5
Makefile
5
Makefile
@ -25,3 +25,8 @@ clean:
|
||||
.SILENT: run
|
||||
run: all
|
||||
./$(BUILD_DIR)/$(NAME)
|
||||
|
||||
.PHONY: tests
|
||||
.SILENT: tests
|
||||
tests: all
|
||||
make -C tests
|
||||
|
@ -1,5 +1,12 @@
|
||||
# vriscv - a risc-v simulator
|
||||
|
||||
## Unit tests
|
||||
|
||||
Unit tests can be compiled and run using :
|
||||
```
|
||||
make tests
|
||||
```
|
||||
|
||||
## Resources used
|
||||
|
||||
Juraj's Blog, mostly:
|
||||
|
22
tests/Makefile
Normal file
22
tests/Makefile
Normal file
@ -0,0 +1,22 @@
|
||||
AS=riscv32-elf-as
|
||||
LD=riscv32-elf-ld
|
||||
BUILD_DIR=../build/tests/
|
||||
|
||||
S_FILES := $(shell find ./ -name '*.s')
|
||||
NAMES = $(basename $(S_FILES))
|
||||
OBJECTS=$(patsubst %, $(BUILD_DIR)/%, $(NAMES))
|
||||
|
||||
all: $(OBJECTS) run
|
||||
|
||||
$(BUILD_DIR):
|
||||
mkdir -p $(BUILD_DIR)
|
||||
|
||||
$(BUILD_DIR)/%.o: %.s | $(BUILD_DIR)
|
||||
$(AS) $^ -o $@
|
||||
|
||||
$(BUILD_DIR)/%: $(BUILD_DIR)/%.o | $(BUILD_DIR)
|
||||
$(LD) $^ -o $@ -nostdlib
|
||||
|
||||
.SILENT: run
|
||||
run:
|
||||
bash test.sh
|
4
tests/addi.s
Normal file
4
tests/addi.s
Normal file
@ -0,0 +1,4 @@
|
||||
.global _start
|
||||
_start:
|
||||
addi a0, zero, 0xBA
|
||||
ebreak
|
31
tests/beq.s
Normal file
31
tests/beq.s
Normal file
@ -0,0 +1,31 @@
|
||||
.global _start
|
||||
_start:
|
||||
# Set base value of a0 to 'test failed'
|
||||
addi a0, zero, 1
|
||||
|
||||
# Set A, B values of t0/t1, to test equality
|
||||
addi t0, zero, 0xA
|
||||
addi t1, zero, 0xB
|
||||
beq t0, t1, eq0
|
||||
|
||||
# Inequality passed, now test equality
|
||||
addi t0, zero, 0xA
|
||||
addi t1, zero, 0xA
|
||||
beq t0, t1, eq1
|
||||
|
||||
# On failure, return
|
||||
ebreak
|
||||
|
||||
eqNeg:
|
||||
# All passed
|
||||
addi a0, zero, 0
|
||||
ebreak
|
||||
|
||||
eq0:
|
||||
# Inequality failed
|
||||
ebreak
|
||||
|
||||
eq1:
|
||||
# Equality passed ; now try to test a negative offset case
|
||||
beq t0, t1, eqNeg
|
||||
ebreak
|
21
tests/jal.s
Normal file
21
tests/jal.s
Normal file
@ -0,0 +1,21 @@
|
||||
.global _start
|
||||
_start:
|
||||
# Set base value of a0 to 'test failed'
|
||||
addi a0, zero, 1
|
||||
|
||||
# Construct value that ra should have in t0
|
||||
auipc t0, 0
|
||||
addi t0, t0, 12
|
||||
# Jump and link
|
||||
jal ra, fn0
|
||||
ebreak
|
||||
|
||||
fn0:
|
||||
# Check ra value with our t0 construct
|
||||
beq t0, ra, eq0
|
||||
ebreak
|
||||
|
||||
eq0:
|
||||
# All good
|
||||
addi a0, zero, 0
|
||||
ebreak
|
46
tests/jalr.s
Normal file
46
tests/jalr.s
Normal file
@ -0,0 +1,46 @@
|
||||
.global _start
|
||||
_start:
|
||||
# Set base value of a0 to 'test failed'
|
||||
addi a0, zero, 1
|
||||
|
||||
# Setup a stack
|
||||
addi sp, sp, 0x100
|
||||
|
||||
# Try to call a simple function
|
||||
jal fn1
|
||||
|
||||
# Try to call two imbricated functions
|
||||
jal fn0
|
||||
|
||||
auipc t0, 0
|
||||
jalr ra, 12(t0)
|
||||
|
||||
ebreak
|
||||
|
||||
# just_after : address is 12 bytes after auipc
|
||||
just_after:
|
||||
addi a0, zero, 0
|
||||
ret
|
||||
ebreak
|
||||
|
||||
# fn0 : function that calls fn1 and returns
|
||||
fn0:
|
||||
# Make space on the stack
|
||||
addi sp, sp, -4
|
||||
# Save ra
|
||||
sw ra, 0(sp)
|
||||
|
||||
# Call fn1
|
||||
jal fn1
|
||||
|
||||
# Restore ra and stack
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
|
||||
ret
|
||||
ebreak
|
||||
|
||||
# fn1 : just return
|
||||
fn1:
|
||||
ret
|
||||
ebreak
|
5
tests/mv.s
Normal file
5
tests/mv.s
Normal file
@ -0,0 +1,5 @@
|
||||
.global _start
|
||||
_start:
|
||||
addi a1, zero, 0xBA
|
||||
mv a0, a1
|
||||
ebreak
|
21
tests/swlw.s
Normal file
21
tests/swlw.s
Normal file
@ -0,0 +1,21 @@
|
||||
.global _start
|
||||
_start:
|
||||
# Set base value of a0 to 'test failed'
|
||||
addi a0, zero, 1
|
||||
|
||||
# Store Word 0xBA at 0x104
|
||||
addi t0, zero, 0x100
|
||||
addi t1, zero, 0xBA
|
||||
sw t1, 4(t0)
|
||||
|
||||
# Load Word at 0x104 in t0
|
||||
lw t0, 4(t0)
|
||||
|
||||
# Compare
|
||||
beq t0, t1, good
|
||||
|
||||
ebreak
|
||||
|
||||
good:
|
||||
addi a0, zero, 0
|
||||
ebreak
|
30
tests/test.sh
Normal file
30
tests/test.sh
Normal file
@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
|
||||
GREEN="\e[32m"
|
||||
RED="\e[31m"
|
||||
CEND="\e[39m"
|
||||
|
||||
VRISCV=../build/vriscv
|
||||
|
||||
# test executes a test
|
||||
# Arguments: test(name, executable, expected)
|
||||
test() {
|
||||
NAME=$1
|
||||
TEST=$2
|
||||
RESULT=$3
|
||||
|
||||
echo -ne "${NAME}""\t\t\t\t\t\t"
|
||||
$VRISCV $TEST >/dev/null 2>&1
|
||||
if [ $? -eq $RESULT ]; then
|
||||
echo -e $GREEN"PASSED"$CEND
|
||||
else
|
||||
echo -e $RED"FAILED"$CEND
|
||||
fi
|
||||
}
|
||||
|
||||
test "ADDI : Add Immediate " "../build/tests/addi " 186
|
||||
test "MV : Move registers " "../build/tests/mv " 186
|
||||
test "BEQ : Branch EQual " "../build/tests/beq " 0
|
||||
test "JAL : Jump And Link " "../build/tests/jal " 0
|
||||
test "SWLW : Store Word Load Word " "../build/tests/swlw " 0
|
||||
test "JALR : Jump And Link Register " "../build/tests/jalr " 0
|
Loading…
Reference in New Issue
Block a user