mirror of
https://github.com/open-goal/jak-project
synced 2026-08-09 02:49:19 -04:00
Implement gkernel: Part 2 (#155)
* update * small fixes * deactivate * simple kernel test
This commit is contained in:
@@ -16,6 +16,7 @@ add_executable(goalc-test
|
||||
test_common_util.cpp
|
||||
test_pretty_print.cpp
|
||||
test_zydis.cpp
|
||||
goalc/test_goal_kernel.cpp
|
||||
${GOALC_TEST_FRAMEWORK_SOURCES}
|
||||
${GOALC_TEST_CASES})
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
(defun deactivate-myself ()
|
||||
"Deactivate the current process. A workaround because rlet isn't working well."
|
||||
(rlet ((pp :reg r13 :type process))
|
||||
(deactivate pp)
|
||||
)
|
||||
)
|
||||
|
||||
(defun target-function ((a0 uint) (a1 uint) (a2 uint) (a3 uint) (a4 uint) (a5 uint))
|
||||
(format #t "TARGET FUNCTION ~D ~D ~D~%" a0 a1 a2)
|
||||
(format #t "~D ~D ~D~%" a3 a4 a5)
|
||||
|
||||
(let ((stack-arr (new 'stack 'array 'uint8 12)))
|
||||
(format #t "Stack Alignemnt ~D/16~%" (logand 15 (the uint stack-arr)))
|
||||
)
|
||||
|
||||
(dotimes (i 10)
|
||||
(format #t "proc1: ~D~%" i)
|
||||
(when (> i 4)
|
||||
(format #t "DEACTIVATE PROC 1~%")
|
||||
(deactivate-myself)
|
||||
)
|
||||
(suspend)
|
||||
)
|
||||
)
|
||||
|
||||
(define-extern recurse (function int (pointer int32) int))
|
||||
(defun recurse ((i int) (ptr (pointer int32)))
|
||||
(if (> i 0)
|
||||
(recurse (- i 1) ptr)
|
||||
(suspend)
|
||||
)
|
||||
(set! (-> ptr) (+ (-> ptr) 1))
|
||||
1
|
||||
)
|
||||
|
||||
(defun target-function-2 ()
|
||||
(let ((stack-var (new 'stack 'array 'int32 1)))
|
||||
(set! (-> stack-var) 0)
|
||||
(countdown (i 10)
|
||||
(format #t "proc2: ~D~%" (-> stack-var))
|
||||
(recurse 5 stack-var)
|
||||
)
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
(defun kernel-test ()
|
||||
(define test-process (get-process *nk-dead-pool* process 1024))
|
||||
|
||||
(activate test-process *active-pool* 'test-proc *kernel-dram-stack*)
|
||||
|
||||
|
||||
(set-to-run (-> test-process main-thread)
|
||||
target-function
|
||||
1 2 3 4 5 6
|
||||
)
|
||||
|
||||
(define test-process-2 (get-process *nk-dead-pool* process 1024))
|
||||
(activate test-process-2 *active-pool* 'test-2 *kernel-dram-stack*)
|
||||
(set-to-run (-> test-process-2 main-thread)
|
||||
target-function-2
|
||||
0 0 0 0 0 0)
|
||||
0
|
||||
)
|
||||
@@ -0,0 +1,89 @@
|
||||
#include <thread>
|
||||
#include "goalc/compiler/Compiler.h"
|
||||
#include "test/goalc/framework/test_runner.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class KernelTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestSuite() {
|
||||
printf("Building kernel...\n");
|
||||
try {
|
||||
// a macro in goal-lib.gc
|
||||
compiler.run_front_end_on_string("(build-kernel)");
|
||||
} catch (std::exception& e) {
|
||||
fprintf(stderr, "caught exception %s\n", e.what());
|
||||
EXPECT_TRUE(false);
|
||||
}
|
||||
|
||||
printf("Starting GOAL Kernel...\n");
|
||||
runtime_thread = std::thread(GoalTest::runtime_with_kernel);
|
||||
runner.c = &compiler;
|
||||
}
|
||||
|
||||
static void TearDownTestSuite() {
|
||||
// send message to shutdown
|
||||
compiler.shutdown_target();
|
||||
// wait for shutdown.
|
||||
runtime_thread.join();
|
||||
}
|
||||
|
||||
void SetUp() {}
|
||||
|
||||
void TearDown() {}
|
||||
|
||||
static std::thread runtime_thread;
|
||||
static Compiler compiler;
|
||||
static GoalTest::CompilerTestRunner runner;
|
||||
};
|
||||
|
||||
std::thread KernelTest::runtime_thread;
|
||||
Compiler KernelTest::compiler;
|
||||
GoalTest::CompilerTestRunner KernelTest::runner;
|
||||
|
||||
TEST_F(KernelTest, Basic) {
|
||||
// first, let's load the kernel test code
|
||||
runner.c->run_test_from_string("(ml \"test/goalc/source_templates/kernel/kernel-test.gc\")");
|
||||
auto& listener = runner.c->listener();
|
||||
|
||||
// record all print messages
|
||||
listener.record_messages(ListenerMessageKind::MSG_PRINT);
|
||||
|
||||
// run the test.
|
||||
runner.c->compile_and_send_from_string("(kernel-test)");
|
||||
|
||||
// kinda hacky, but wait until the kernel runs and sends all the messages
|
||||
while (listener.get_received_message_count() < 10) {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(1000));
|
||||
}
|
||||
|
||||
auto messages = listener.stop_recording_messages();
|
||||
std::string result;
|
||||
for (auto& m : messages) {
|
||||
result += m;
|
||||
}
|
||||
|
||||
std::string expected =
|
||||
"0\n"
|
||||
"proc2: 0\n"
|
||||
"TARGET FUNCTION 1 2 3\n"
|
||||
"4 5 6\n"
|
||||
"Stack Alignemnt 0/16\n"
|
||||
"proc1: 0\n"
|
||||
"proc2: 6\n"
|
||||
"proc1: 1\n"
|
||||
"proc2: 12\n"
|
||||
"proc1: 2\n"
|
||||
"proc2: 18\n"
|
||||
"proc1: 3\n"
|
||||
"proc2: 24\n"
|
||||
"proc1: 4\n"
|
||||
"proc2: 30\n"
|
||||
"proc1: 5\n"
|
||||
"DEACTIVATE PROC 1\n"
|
||||
"proc2: 36\n"
|
||||
"proc2: 42\n"
|
||||
"proc2: 48\n"
|
||||
"proc2: 54\n";
|
||||
|
||||
EXPECT_EQ(expected, result);
|
||||
}
|
||||
Reference in New Issue
Block a user