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
2 changes: 1 addition & 1 deletion kernels/demo/include/io.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef __DRAW_H__

#define HAS_GUI
// #define HAS_GUI //这里定义了图形用户界面

#include <stdio.h>
#include <am.h>
Expand Down
8 changes: 8 additions & 0 deletions kernels/hello/hello.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <am.h>
#include <stdio.h>
// #include <klib.h>
#include <klib-macros.h>


int main(const char *args) {
const char *fmt =
"Hello, AbstractMachine!\n"
Expand All @@ -9,5 +12,10 @@ int main(const char *args) {
for (const char *p = fmt; *p; p++) {
(*p == '%') ? putstr(args) : putch(*p);
}
printf("xxx123\n");

int *badptr = (int*)0x12345678; //测试iringbuf
int x = *badptr;
putch(x);
return 0;
}
13 changes: 7 additions & 6 deletions kernels/typing-game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <klib.h>
#include <klib-macros.h>

#define FPS 30
#define CPS 5
#define FPS 30 //帧率 每秒30帧
#define CPS 5 //每秒5个新字母
#define CHAR_W 8
#define CHAR_H 16
#define NCHAR 128
Expand Down Expand Up @@ -33,9 +33,9 @@ void new_char() {
for (int i = 0; i < LENGTH(chars); i++) {
struct character *c = &chars[i];
if (!c->ch) {
c->ch = 'A' + randint(0, 25);
c->x = randint(0, screen_w - CHAR_W);
c->y = 0;
c->ch = 'A' + randint(0, 25); //随机字符
c->x = randint(0, screen_w - CHAR_W); //随机出现在屏幕的某一列
c->y = 0; //从顶部开始下落
c->v = (screen_h - CHAR_H + 1) / randint(FPS * 3 / 2, FPS * 2);
c->t = 0;
return;
Expand Down Expand Up @@ -76,6 +76,7 @@ void render() {
}

n = 0;
//记录本帧数据及坐标
for (int i = 0; i < LENGTH(chars); i++) {
struct character *c = &chars[i];
if (c->ch) {
Expand Down Expand Up @@ -110,7 +111,7 @@ void video_init() {
screen_w = io_read(AM_GPU_CONFIG).width;
screen_h = io_read(AM_GPU_CONFIG).height;

extern char font[];
extern char font[]; //用于渲染字母的像素形状
for (int i = 0; i < CHAR_W * CHAR_H; i++)
blank[i] = COL_PURPLE;

Expand Down
10 changes: 5 additions & 5 deletions tests/alu-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ NAME = alutest
SRCS = build/alu_test.c
include $(AM_HOME)/Makefile

GENERATOR = build/gen_alu_test
# GENERATOR = build/gen_alu_test

$(GENERATOR): gen_alu_test.c
gcc -O2 -Wall -Werror $^ -o $@
# $(GENERATOR): gen_alu_test.c
# gcc -O2 -Wall -Werror $^ -o $@

$(SRCS): $(GENERATOR)
$^ > $@
# $(SRCS): $(GENERATOR)
# $^ > $@
3 changes: 2 additions & 1 deletion tests/alu-tests/gen_alu_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ int exclude(type t, char* op, int x, int y)

int main(void)
{
printf("#include <stdio.h>\n");
//printf("#include <stdio.h>\n");
printf("#include<klib.h>\n");
printf("int main(void) {\n");
printf(" int exit_code = 0;\n");

Expand Down
22,365 changes: 22,365 additions & 0 deletions tests/alu-tests/test.c

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/am-tests/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ static const char *tests[256] = {
['v'] = "display test",
['a'] = "audio test",
['p'] = "x86 virtual memory test",
['1'] = "string_1 test",
['2'] = "string_2 test",
['3'] = "string_3 test",
};

int main(const char *args) {
Expand All @@ -26,6 +29,9 @@ int main(const char *args) {
CASE('v', video_test, IOE);
CASE('a', audio_test, IOE);
CASE('p', vm_test, CTE(vm_handler), VME(simple_pgalloc, simple_pgfree));
CASE('1', test_string_1);
CASE('2', test_string_2);
CASE('3', test_string_3);
case 'H':
default:
printf("Usage: make run mainargs=*\n");
Expand Down
67 changes: 67 additions & 0 deletions tests/am-tests/src/tests/test_string_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <amtest.h>

#define N 32

uint8_t data[N];

void reset() {
int i;
for (i = 0; i < N; i ++) {
data[i] = i + 1;
}
}

void check_seq(int l, int r, int val) {
int i;
for (i = l; i < r; i ++) {
assert(data[i] == val + i - l);
}
}

// 检查[l,r)区间中的值是否均为val
void check_eq(int l, int r, int val) {
int i;
for (i = l; i < r; i ++) {
assert(data[i] == val);
}
}

void test_string_1() {
int l, r;
for (l = 0; l < N; l ++) {
for (r = l + 1; r <= N; r ++) {
reset();
uint8_t val = (l + r) / 2;
memset(data + l, val, r - l);
check_seq(0, l, 1);
check_eq(l, r, val);
check_seq(r, N, r + 1);
}
}
char buf[N];
const char *srcs[] = {
"", // 空字符串
"a",
"hello",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", // 恰好 26 字符
"0123456789abcdef0123456789abc", // 31 字符
"0123456789abcdef0123456789abcd" // 32 字符(长度=N-1,最后为\0)
};
int num_srcs = sizeof(srcs) / sizeof(srcs[0]);
for (int i = 0; i < num_srcs; i++) {
// 先清空目标数组
memset(buf, '?', sizeof(buf));
strcpy(buf, srcs[i]);
// 检查内容是否完全一致
assert(strcmp(buf, srcs[i]) == 0);

// 检查结尾'\0'
int len = strlen(srcs[i]);
assert(buf[len] == '\0');

// 若拷贝内容较短,后续字节保持为'?'
for (int j = len + 1; j < N; j++) {
assert(buf[j] == '?');
}
}
}
51 changes: 51 additions & 0 deletions tests/am-tests/src/tests/test_string_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <amtest.h>
// #include <string.h>
// #include <assert.h>

#define N 32

void test_memcmp() {
// 完全相等
uint8_t a[N] = {1, 2, 3, 4, 5};
uint8_t b[N] = {1, 2, 3, 4, 5};
assert(memcmp(a, b, 5) == 0);

// a < b
b[2] = 7;
assert(memcmp(a, b, 5) < 0);

// a > b
a[2] = 8;
assert(memcmp(a, b, 5) > 0);

// 部分相等
a[2] = 7; b[2] = 7; // 恢复相等
assert(memcmp(a, b, 3) == 0);

// 长度为零时总是相等
assert(memcmp(a, b, 0) == 0);
}

void test_strlen() {
// 空串
assert(strlen("") == 0);

// 普通字符串
assert(strlen("A") == 1);
assert(strlen("hello") == 5);

// 包含空字符后的内容不计入长度
char s[] = {'a', 'b', 'c', '\0', 'd', 'e'};
assert(strlen(s) == 3);

// 边界情况:最大长度串
char maxs[N + 1];
for (int i = 0; i < N; i++) maxs[i] = 'X';
maxs[N] = '\0';
assert(strlen(maxs) == N);
}

void test_string_2() {
test_memcmp();
test_strlen();
}
91 changes: 91 additions & 0 deletions tests/am-tests/src/tests/test_string_3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <amtest.h>
#include <limits.h>
#include <stdint.h>
// #include <stdio.h>
// #include <string.h>
// #include <assert.h>

void test_sprintf_basic() {
char buf[128];

// 测试int
int data_int[] = {
0,
INT_MAX / 17,
INT_MAX,
INT_MIN,
INT_MIN + 1,
};
const char *expected_int[] = {
"0",
"126322567",
"2147483647",
"-2147483648",
"-2147483647"
};
int n_int = sizeof(data_int) / sizeof(data_int[0]);
for (int i = 0; i < n_int; i++) {
sprintf(buf, "%d", data_int[i]);
assert(strcmp(buf, expected_int[i]) == 0);
}

// 测试unsigned int
unsigned int data_uint[] = {
UINT_MAX / 17,
UINT_MAX
};
const char *expected_uint[] = {
"252645135",
"4294967295"
};
int n_uint = sizeof(data_uint) / sizeof(data_uint[0]);
for (int i = 0; i < n_uint; i++) {
sprintf(buf, "%u", data_uint[i]);
assert(strcmp(buf, expected_uint[i]) == 0);
}
}

void test_sprintf_width_precision() {
char buf[128];

// 宽度、精度
sprintf(buf, "%5d", 42); // 宽度
assert(strcmp(buf, " 42") == 0);

sprintf(buf, "%.4d", 42); // 精度
assert(strcmp(buf, "0042") == 0);

sprintf(buf, "%7.3d", 888); // 宽度+精度
assert(strcmp(buf, " 888") == 0);

sprintf(buf, "%-5d", 7); // 左对齐
assert(strcmp(buf, "7 ") == 0);

sprintf(buf, "%+d", -123); // 显示符号
assert(strcmp(buf, "-123") == 0);

sprintf(buf, "%+d", 123); // 显示正号
assert(strcmp(buf, "+123") == 0);

sprintf(buf, "%04d", 7); // 宽度、补零
assert(strcmp(buf, "0007") == 0);
}

void test_sprintf_string() {
char buf[128];

sprintf(buf, "%.5s", "abcdefg");
assert(strcmp(buf, "abcde") == 0);

sprintf(buf, "%10s", "test");
assert(strcmp(buf, " test") == 0);

sprintf(buf, "%-10s", "test");
assert(strcmp(buf, "test ") == 0);
}

void test_string_3() {
test_sprintf_basic();
test_sprintf_width_precision();
test_sprintf_string();
}
9 changes: 8 additions & 1 deletion tests/am-tests/src/tests/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ static inline uint8_t R(uint32_t p) { return p >> 16; }
static inline uint8_t G(uint32_t p) { return p >> 8; }
static inline uint8_t B(uint32_t p) { return p; }

static uint32_t canvas[N][N];
static uint32_t canvas[N][N]; //这个地方就是画布
static int used[N][N];

static uint32_t color_buf[32 * 32];

/****************
* redraw做了什么
*首先获得屏幕的宽高(通过io_read读GPU配置),然后算出每个方块的大小
*对canvas里的每个像素,把它的颜色值填到color_buf(这是要画的方块的颜色)
*用io_write把这个方块画在屏幕上对应位置(xw, yh),大小是w*h
*最后再调用一次io_write,表示同步(刷新屏幕)
***************/
void redraw() {
int w = io_read(AM_GPU_CONFIG).width / N;
int h = io_read(AM_GPU_CONFIG).height / N;
Expand Down
3 changes: 3 additions & 0 deletions tests/cpu-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ COLOR_GREEN = \033[1;32m
COLOR_NONE = \033[0m

ALL = $(basename $(notdir $(shell find tests/. -name "*.c")))
#这个ALL是可以自己定义的,然后最后是去掉路径和.c后缀

all: $(addprefix Makefile., $(ALL))
@echo "test list [$(words $(ALL)) item(s)]:" $(ALL)

$(ALL): %: Makefile.%

#就是这里先生成单个测试的Makefile,然后输入make ARCH去调用AM,然后就进入nemu了 -f@是指定用哪个临时Makefile
Makefile.%: tests/%.c latest
# 这里的/bin/echo -e 将多行内容写入Makefile.对应的测试程序
@/bin/echo -e "NAME = $*\nSRCS = $<\ninclude $${AM_HOME}/Makefile" > $@
@if make -s -f $@ ARCH=$(ARCH) $(MAKECMDGOALS); then \
printf "[%14s] $(COLOR_GREEN)PASS$(COLOR_NONE)\n" $* >> $(RESULT); \
Expand Down
6 changes: 6 additions & 0 deletions tests/cpu-tests/tests/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main() {
printf("hello World\n");
return 0;
}