sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
main.cpp
1#include <cstdio>
2#include <cstring>
3
4extern "C" {
5#include <kernel.h>
6}
7
8#include <coroutine>
9
10extern "C" int kputs(char *s);
11
12/* A program must never return from main on the rig: that reboots the console. */
13static int rig_failed;
14static int rig_kputs(char *s) {
15 if (strstr(s, ": FAIL")) rig_failed = 1;
16 return kputs(s);
17}
18#define kputs rig_kputs
19
20struct Generator {
21 struct promise_type {
22 int value = 0;
23 Generator get_return_object() { return Generator{std::coroutine_handle<promise_type>::from_promise(*this)}; }
24 std::suspend_always initial_suspend() noexcept { return {}; }
25 std::suspend_always final_suspend() noexcept { return {}; }
26 std::suspend_always yield_value(int v) noexcept { value = v; return {}; }
27 void return_void() noexcept {}
28 void unhandled_exception() noexcept {}
29 };
30
31 std::coroutine_handle<promise_type> handle;
32
33 explicit Generator(std::coroutine_handle<promise_type> h) : handle(h) {}
34 Generator(Generator &&other) noexcept : handle(other.handle) { other.handle = nullptr; }
35 ~Generator() { if (handle) handle.destroy(); }
36
37 bool next() {
38 handle.resume();
39 return !handle.done();
40 }
41 int value() const { return handle.promise().value; }
42};
43
44static Generator count_to(int n) {
45 for (int i = 1; i <= n; i++)
46 co_yield i;
47}
48
49static int test_main() {
50 Generator gen = count_to(5);
51 int sum = 0;
52 while (gen.next())
53 sum += gen.value();
54
55 if (sum != 15) {
56 kputs((char *)"cxx-coroutines: FAIL wrong sum\n");
57 return 1;
58 }
59
60 kputs((char *)"cxx-coroutines: PASS\n");
61 return 0;
62}
63
64int main() {
65 int result = test_main();
66 printf("[ps2rig] exit=%d\n", (result || rig_failed) ? 1 : 0);
67 SleepThread();
68 return 0;
69}