Skip to content

Commit f2b4e80

Browse files
authored
Implement features used by fenster-rv32emu (#364)
The fenster-rv32emu [1], a port of the fenster library for rv32emu, introduces capabilities not previously supported. These features include customizing the window title, capturing mouse position, and detecting program quit events. To accommodate these features, SDL-oriented system calls have been refined, providing the hosted program with additional information and enabling standard functions like changing the window's title. It is important to note that these updates are not backward compatible with previous binaries. Therefore, all binary files associated with SDL-oriented system calls have been updated and recompiled. [1] https://github.com/alanjian85/fenster-rv32emu
1 parent 170213e commit f2b4e80

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

build/doom.elf

-57.2 KB
Binary file not shown.

build/quake.elf

-151 KB
Binary file not shown.

build/smolnes.elf

100644100755
-888 Bytes
Binary file not shown.

docs/syscall.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ The user must pass a continuous memory chunk that contains two tightly packed qu
138138

139139
An event entry is made up of a 32-bit value representing the event's type and a `union` buffer containing the event's parameters.
140140

141-
* `KEY_EVENT`: Either a key is pressed or released. Its value buffer is made up of a 32-bit universal key code and an 8-bit state flag; if the corresponding character of the pressed key is not printable, the bit right after the most significant bit is set; for example, the "a" key's corresponding character is printable, so its keycode is the ASCII code of the "a" character, which is `0x61`. However, because the left shift key doesn't have a corresponding printable key, its hexadecimal value is `0x400000E1`, with the 31 bit is set.
142-
* `MOUSE_MOTION_EVENT`: The cursor is moved during the current frame. This event contains two signed integer value, which is the delta of x position and y position respectively. If the relative mouse mode is enabled, the mouse movement will never be 0 because the cursor is wrapped within the canvas and is repeated whenever the cursor reaches the border.
141+
* `KEY_EVENT`: Either a key is pressed or released. Its value buffer is made up of a 32-bit universal key code and an 8-bit state flag; if the corresponding character of the pressed key is not printable, the bit right after the most significant bit is set; for example, the "a" key's corresponding character is printable, so its keycode is the ASCII code of the "a" character, which is `0x61`. However, because the left shift key doesn't have a corresponding printable key, its hexadecimal value is `0x400000E1`, with the 31th bit set.
142+
* `MOUSE_MOTION_EVENT`: The cursor is moved between the previous and the current frames. This event contains the current mouse position and how it differs from the last frame. If the relative mouse mode is enabled, the cursor is wrapped within the canvas and repeated whenever the cursor reaches the border.
143143
* `MOUSE_BUTTON_EVENT`: The state of a mouse button has been changed. Its value buffer contains a 8-bit button value(1 is left, 2 is middle, 3 is right and so on) and an 8-bit boolean flag that indicates whether the mouse button is pressed.
144+
* `QUIT_EVENT`: The program is requested to exit. Typically, the hosted program destroys the created objects and terminates when this event occurs.
144145

145146
### `submit_queue` - Notify the emulator a submission has been pushed into the submission queue
146147

@@ -155,6 +156,7 @@ To inform the emulator that a batch of submissions should be processed, the appl
155156
The submission entry is structured similarly to an event entry, with a 32-bit type field and an associated dynamic-sized value buffer whose width depends on the type of submission.
156157

157158
* `RELATIVE_MODE_SUBMISSION`: Enable or disable the mouse relative mode. If the mouse relative mode is enabled, the mouse cursor is wrapped within the window border, it's associated with an 8-bit wide boolean value that indicates whether the relative mouse mode should be enbled.
159+
* `WINDOW_TITLE_SUBMISSION`: Change the title of the SDL window. If the title is not specified, it will be `rv32emu` by default.
158160

159161
### `control_audio` - control the behavior of music and sound effect(sfx)
160162

src/syscall_sdl.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ enum {
8080
KEY_EVENT = 0,
8181
MOUSE_MOTION_EVENT = 1,
8282
MOUSE_BUTTON_EVENT = 2,
83+
QUIT_EVENT = 3,
8384
};
8485

8586
typedef struct {
@@ -88,7 +89,7 @@ typedef struct {
8889
} key_event_t;
8990

9091
typedef struct {
91-
int32_t xrel, yrel;
92+
int32_t x, y, xrel, yrel;
9293
} mouse_motion_t;
9394

9495
typedef struct {
@@ -114,14 +115,23 @@ typedef struct {
114115

115116
enum {
116117
RELATIVE_MODE_SUBMISSION = 0,
118+
WINDOW_TITLE_SUBMISSION = 1,
117119
};
118120

121+
typedef struct {
122+
uint8_t enabled;
123+
} mouse_submission_t;
124+
125+
typedef struct {
126+
uint32_t title;
127+
uint32_t size;
128+
} title_submission_t;
129+
119130
typedef struct {
120131
uint32_t type;
121132
union {
122-
union {
123-
uint8_t enabled;
124-
} mouse;
133+
mouse_submission_t mouse;
134+
title_submission_t title;
125135
};
126136
} submission_t;
127137

@@ -227,9 +237,13 @@ static bool check_sdl(riscv_t *rv, int width, int height)
227237
SDL_Event event;
228238
while (SDL_PollEvent(&event)) { /* Run event handler */
229239
switch (event.type) {
230-
case SDL_QUIT:
231-
rv_halt(rv);
240+
case SDL_QUIT: {
241+
event_t new_event = {
242+
.type = QUIT_EVENT,
243+
};
244+
event_push(rv, new_event);
232245
return false;
246+
}
233247
case SDL_KEYDOWN:
234248
case SDL_KEYUP: {
235249
if (event.key.repeat)
@@ -250,6 +264,8 @@ static bool check_sdl(riscv_t *rv, int width, int height)
250264
.type = MOUSE_MOTION_EVENT,
251265
};
252266
mouse_motion_t mouse_motion = {
267+
.x = event.motion.x,
268+
.y = event.motion.y,
253269
.xrel = event.motion.xrel,
254270
.yrel = event.motion.yrel,
255271
};
@@ -338,6 +354,17 @@ void syscall_submit_queue(riscv_t *rv)
338354
case RELATIVE_MODE_SUBMISSION:
339355
SDL_SetRelativeMouseMode(submission.mouse.enabled);
340356
break;
357+
case WINDOW_TITLE_SUBMISSION:
358+
char *title = malloc(submission.title.size + 1);
359+
if (unlikely(!title))
360+
return;
361+
362+
memory_read(PRIV(rv)->mem, (uint8_t *) title,
363+
submission.title.title, submission.title.size);
364+
title[submission.title.size] = 0;
365+
366+
SDL_SetWindowTitle(window, title);
367+
break;
341368
}
342369
}
343370
}

tests/smolnes.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@
5656
case x: \
5757
case x + 16:
5858

59-
enum { KEY_EVENT = 0, MOUSE_MOTION_EVENT = 1, MOUSE_BUTTON_EVENT = 2 };
59+
enum {
60+
KEY_EVENT = 0,
61+
MOUSE_MOTION_EVENT = 1,
62+
MOUSE_BUTTON_EVENT = 2,
63+
QUIT_EVENT = 3
64+
};
6065

6166
typedef struct {
6267
uint32_t keycode;
6368
uint8_t state;
6469
} key_event_t;
6570

6671
typedef struct {
67-
int32_t xrel, yrel;
72+
int32_t x, y, xrel, yrel;
6873
} mouse_motion_t;
6974

7075
typedef struct {
@@ -91,14 +96,22 @@ typedef struct {
9196

9297
event_queue_t event_queue = {.base = NULL, .start = 0, .capacity = 0};
9398

94-
enum { RELATIVE_MODE_SUBMISSION = 0 };
99+
enum { RELATIVE_MODE_SUBMISSION = 0, WINDOW_TITLE_SUBMISSION = 1 };
100+
101+
typedef struct {
102+
uint8_t enabled;
103+
} mouse_submission_t;
104+
105+
typedef struct {
106+
uint32_t title;
107+
uint32_t size;
108+
} title_submission_t;
95109

96110
typedef struct {
97111
uint32_t type;
98112
union {
99-
union {
100-
uint8_t enabled;
101-
} mouse;
113+
mouse_submission_t mouse;
114+
title_submission_t title;
102115
};
103116
} submission_t;
104117

@@ -850,6 +863,8 @@ int main(int argc, char **argv)
850863
}
851864
break;
852865
}
866+
case QUIT_EVENT:
867+
return 0;
853868
}
854869
}
855870

0 commit comments

Comments
 (0)