Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.

Commit a04f0f4

Browse files
committed
[feature] own-program support
1 parent 04a8fc2 commit a04f0f4

10 files changed

Lines changed: 138 additions & 44 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
!*.sh
1111
!.gitignore
1212
!*.png
13+
!*.mk
1314
!README.md
1415
build
1516

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
app:
2-
$(MAKE) -C am-cpu-tests
3-
$(MAKE) -C rv-tests
2+
$(MAKE) -s -C am-cpu-tests
3+
$(MAKE) -s -C rv-tests
4+
$(MAKE) -s -C own-program
45

56
clean:
6-
$(MAKE) -C am-cpu-tests clean
7-
$(MAKE) -C rv-tests clean
7+
$(MAKE) -s -C am-cpu-tests clean
8+
$(MAKE) -s -C rv-tests clean
9+
$(MAKE) -s -C own-program clean
810

911
.PHONY: app clean

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# riscvui-test
22

3-
This project helps the automatic testing for RV32I cpus. It contains two parts: am-cpu-tests and rv-tests.
3+
This project helps the automatic testing for RV32I cpus. It contains three parts: am-cpu-tests, own-program and rv-tests.
44

55
To start your testing, pull this repo first.
66

77
## am-cpu-tests
88

99
This part uses the `cpu-tests` of [NJU-ProjectN/abstract-machine](https://github.com/NJU-ProjectN/abstract-machine) & [NJU-ProjectN/am-kernels](https://github.com/NJU-ProjectN/am-kernels) for testing.
1010

11-
Before building the tests, make sure that you pull the components of abstract-machine and am-kernels correctly, and `$NEMU_HOME/../am-kernels/` is exactly your root directory of am-kernels.
11+
Before building the tests, make sure that you pull the components of abstract-machine and am-kernels correctly, and `$AM_HOME/../am-kernels/` is exactly your root directory of am-kernels.
1212

1313
### Usage
1414

@@ -17,6 +17,10 @@ Before building the tests, make sure that you pull the components of abstract-ma
1717
- Then use `$readmemh(<MN>.txt, ...)` to load the content of the instructions and data into your memory in verilog, or use `$readmemh(<MN>0/1/2/3.txt, ...)` to load the content of specific bytes of each word of instructions and data in verilog.
1818
- Start your testing! Note that the am-kernels/cpu-tests has RV32M instructions in some of its testcases. And the program ends when it meets `ebreak`(`0x00100073`), with `a0(R[10])` being the returning value. It's wise to identify these cases.
1919

20+
## own-program
21+
22+
This part allows you to add your own programs. The usage is the same as am-cpu-tests, just changing the directory name from `am-cpu-tests` to `own-program`. (In fact, the latter is implemented by this part).
23+
2024
## rv-tests
2125

2226
This part uses the RV official testing set. Some functionalities are added to help with automatic testing.

am-cpu-tests/Makefile

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,6 @@
1-
CPUTEST_DIR = $(NEMU_HOME)/../am-kernels/tests/cpu-tests
2-
CPUTEST_TESTS_DIR = $(CPUTEST_DIR)/tests
3-
CPUTEST_BUILD_DIR = $(CPUTEST_DIR)/build
4-
BUILD_DIR = $(abspath ./build)
5-
TXT_DIR = $(BUILD_DIR)/generate
1+
CPUTEST_TEST = $(AM_HOME)/../am-kernels/tests/cpu-tests/tests
2+
CPUTEST_INCLUDE = $(AM_HOME)/../am-kernels/tests/cpu-tests/include
3+
$(shell ln -sf -T $(CPUTEST_TEST) $(abspath ./src))
4+
$(shell ln -sf -T $(CPUTEST_INCLUDE) $(abspath ./include))
65

7-
C_SRC = $(notdir $(shell find $(CPUTEST_TESTS_DIR) -name "*.c"))
8-
C_FILE = $(addprefix $(CPUTEST_TESTS_DIR)/,$(C_SRC))
9-
BIN_FILE = $(addprefix $(CPUTEST_BUILD_DIR)/,$(foreach file,$(basename $(C_SRC)),$(file)-riscv32-nemu.bin))
10-
TXT_FILE = $(addprefix $(TXT_DIR)/,$(C_SRC:.c=.txt))
11-
NAME_FILE = $(BUILD_DIR)/NAMES.txt
12-
CONVERTER = $(BUILD_DIR)/converter
13-
14-
$(shell mkdir -p $(TXT_DIR))
15-
16-
.DEFAULT_GOAL = app
17-
18-
$(CONVERTER): scripts/converter.c
19-
@$(CC) -o $@ $<
20-
21-
$(CPUTEST_BUILD_DIR)/%-riscv32-nemu.bin: $(CPUTEST_TESTS_DIR)/%.c
22-
@echo build $(notdir $@)
23-
@$(MAKE) -s -C $(CPUTEST_DIR) ARCH=riscv32-nemu ALL=$(notdir $(basename $<))
24-
25-
$(TXT_FILE): $(CONVERTER)
26-
27-
$(TXT_DIR)/%.txt: $(CPUTEST_BUILD_DIR)/%-riscv32-nemu.bin
28-
@echo convert $(notdir $@) from $(notdir $<)
29-
@$(CONVERTER) $< $@
30-
31-
32-
app: $(TXT_FILE)
33-
@echo $(basename $(TXT_FILE)) | tr ' ' '\n' > $(NAME_FILE)
34-
35-
clean:
36-
@-rm -rf $(BUILD_DIR)
37-
38-
.PHONY: app clean
6+
include ../own-program/Makefile

own-program/Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
SRC_DIR = $(abspath ./src)
2+
BUILD_DIR = $(abspath ./build)
3+
AM_ORIGIN_DIR = $(BUILD_DIR)/am-origin
4+
AM_BUILD_DIR = $(AM_ORIGIN_DIR)/build
5+
TXT_DIR = $(BUILD_DIR)/generate
6+
7+
C_SRC = $(notdir $(shell find -L $(SRC_DIR) -name "*.c"))
8+
C_FILE = $(addprefix $(SRC_DIR)/,$(C_SRC))
9+
BIN_FILE = $(foreach file,$(basename $(C_SRC)),$(AM_BUILD_DIR)/$(file)-riscv32-nemu.bin)
10+
TXT_FILE = $(foreach file,$(basename $(C_SRC)),$(TXT_DIR)/$(file).txt)
11+
NAME_FILE = $(BUILD_DIR)/NAMES.txt
12+
CONVERTER = $(BUILD_DIR)/converter
13+
14+
$(shell mkdir -p $(AM_BUILD_DIR))
15+
$(shell mkdir -p $(TXT_DIR))
16+
$(shell ln -sf -T $(AM_HOME)/Makefile $(AM_ORIGIN_DIR)/Makefile)
17+
$(shell ln -sf -T $(abspath ./include) $(AM_ORIGIN_DIR)/include)
18+
$(shell ln -sf -T $(abspath ./src) $(AM_ORIGIN_DIR)/src)
19+
20+
.SECONDARY:
21+
.DEFAULT_GOAL = app
22+
23+
$(CONVERTER): scripts/converter.c
24+
@echo + build $(CONVERTER)
25+
@$(CC) -o $@ $<
26+
27+
$(TXT_FILE): $(CONVERTER)
28+
29+
$(AM_BUILD_DIR)/%-riscv32-nemu.bin: $(SRC_DIR)/%.c
30+
@echo + build $(notdir $@)
31+
@NAME=$(basename $(notdir $<)) SRCS=src/$(notdir $<) $(MAKE) -s -C $(AM_ORIGIN_DIR) ARCH=riscv32-nemu image > /dev/null
32+
33+
$(TXT_DIR)/%.txt: $(AM_BUILD_DIR)/%-riscv32-nemu.bin
34+
@echo + convert $(notdir $@) from $(notdir $<)
35+
@$(CONVERTER) $< $@
36+
37+
$(NAME_FILE): $(TXT_FILE)
38+
@echo $(basename $(TXT_FILE)) | tr ' ' '\n' > $(NAME_FILE)
39+
@echo + generate main names in $(NAME_FILE)
40+
41+
app: $(NAME_FILE)
42+
@echo BUILD DONE.
43+
44+
clean:
45+
@-rm -rf $(BUILD_DIR)
46+
47+
48+
.PHONY: clean app

own-program/include/dummy.h

Whitespace-only changes.

own-program/scripts/converter.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <assert.h>
5+
#include <stdint.h>
6+
7+
int main(int argc, char *argv[]) {
8+
assert(argc == 3);
9+
const char *from = argv[1]; // hex
10+
const char *to = argv[2]; // text
11+
12+
FILE *fp = fopen(from, "rb");
13+
assert(fp);
14+
size_t siz = 0;
15+
assert(fseek(fp, 0, SEEK_END) != -1);
16+
siz = ftell(fp);
17+
uint8_t *content = (uint8_t *)malloc(sizeof(uint8_t) * (siz + 4));
18+
rewind(fp);
19+
assert(fread(content, sizeof(uint8_t) * siz, 1, fp) == 1);
20+
fclose(fp);
21+
22+
int nowsiz = 4 * ((siz + 3) / 4);
23+
for (int i = siz; i < nowsiz; i++)
24+
content[i] = 0;
25+
26+
fp = fopen(to, "w");
27+
assert(fp);
28+
fprintf(fp, "@0\n");
29+
FILE *fdis[4];
30+
char *tmp = (char *)malloc(sizeof(char) * (strlen(to) + 2));
31+
assert(tmp);
32+
strcpy(tmp, to);
33+
char *num = tmp + strlen(to);
34+
*num = *(num-1); num--;
35+
*num = *(num-1); num--;
36+
*num = *(num-1); num--;
37+
*num = *(num-1); num--;
38+
for (int j = 0; j < 4; j++) {
39+
*num = j + '0';
40+
fdis[j] = fopen(tmp, "w");
41+
fprintf(fdis[j], "@0\n");
42+
}
43+
free(tmp);
44+
for (int i = 0; i < nowsiz; i += 4) {
45+
fprintf(fp, "%02x%02x%02x%02x\n", content[i+3], content[i+2], content[i+1], content[i]);
46+
for (int j = 0; j < 4; j++)
47+
fprintf(fdis[j], "%02x\n", content[i+j]);
48+
}
49+
free(content);
50+
for (int j = 0; j < 4; j++)
51+
fclose(fdis[j]);
52+
fclose(fp);
53+
return 0;
54+
}

own-program/src/fib.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
#include "dummy.h"
3+
#define N 20
4+
5+
int f[N+1];
6+
7+
int main() {
8+
f[0] = 0;
9+
f[1] = 1;
10+
for (int i = 2; i <= N; i++)
11+
f[i] = f[i-1] + f[i-2];
12+
return f[N];
13+
}

rv-tests/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ TRANSFER = $(BUILD_DIR)/transfer
55
.DEFAULT_GOAL = app
66

77
$(TRANSFER): scripts/transfer.c
8-
$(CC) -o $@ $<
8+
@echo + build $(TRANSFER)
9+
@$(CC) -o $@ $<
910

1011
app: $(TRANSFER)
1112
@bash scripts/gen.sh
13+
@echo BUILD DONE.
1214

1315
clean:
1416
@rm -rf $(TRANSFER) $(BUILD_DIR)

rv-tests/scripts/gen.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ cp data/* build/generate
33
NAMES=$(realpath $(find build/generate | grep -E "rv32ui-p-\w+_d.hex"))
44
rm -rf build/NAMES.txt
55
for i in ${NAMES[@]}; do
6+
echo + transfer $(basename $i)
67
build/transfer "$i"
78
echo "$i" | sed -E "s/_d\.hex//" >> build/NAMES.txt
89
done
10+
echo + generate main names in $(realpath build/NAMES.txt)

0 commit comments

Comments
 (0)