Skip to content
Merged
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
Binary file modified build/doom.elf
Binary file not shown.
Binary file modified build/quake.elf
Binary file not shown.
Binary file modified build/smolnes.elf
100644 → 100755
Binary file not shown.
6 changes: 4 additions & 2 deletions docs/syscall.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ The user must pass a continuous memory chunk that contains two tightly packed qu

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.

* `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.
* `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.
* `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.
* `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.
* `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.
* `QUIT_EVENT`: The program is requested to exit. Typically, the hosted program destroys the created objects and terminates when this event occurs.

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

Expand All @@ -155,6 +156,7 @@ To inform the emulator that a batch of submissions should be processed, the appl
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.

* `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.
* `WINDOW_TITLE_SUBMISSION`: Change the title of the SDL window. If the title is not specified, it will be `rv32emu` by default.

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

Expand Down
39 changes: 33 additions & 6 deletions src/syscall_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ enum {
KEY_EVENT = 0,
MOUSE_MOTION_EVENT = 1,
MOUSE_BUTTON_EVENT = 2,
QUIT_EVENT = 3,
};

typedef struct {
Expand All @@ -88,7 +89,7 @@ typedef struct {
} key_event_t;

typedef struct {
int32_t xrel, yrel;
int32_t x, y, xrel, yrel;
} mouse_motion_t;

typedef struct {
Expand All @@ -114,14 +115,23 @@ typedef struct {

enum {
RELATIVE_MODE_SUBMISSION = 0,
WINDOW_TITLE_SUBMISSION = 1,
};

typedef struct {
uint8_t enabled;
} mouse_submission_t;

typedef struct {
uint32_t title;
uint32_t size;
} title_submission_t;

typedef struct {
uint32_t type;
union {
union {
uint8_t enabled;
} mouse;
mouse_submission_t mouse;
title_submission_t title;
};
} submission_t;

Expand Down Expand Up @@ -227,9 +237,13 @@ static bool check_sdl(riscv_t *rv, int width, int height)
SDL_Event event;
while (SDL_PollEvent(&event)) { /* Run event handler */
switch (event.type) {
case SDL_QUIT:
rv_halt(rv);
case SDL_QUIT: {
event_t new_event = {
.type = QUIT_EVENT,
};
event_push(rv, new_event);
return false;
}
case SDL_KEYDOWN:
case SDL_KEYUP: {
if (event.key.repeat)
Expand All @@ -250,6 +264,8 @@ static bool check_sdl(riscv_t *rv, int width, int height)
.type = MOUSE_MOTION_EVENT,
};
mouse_motion_t mouse_motion = {
.x = event.motion.x,
.y = event.motion.y,
.xrel = event.motion.xrel,
.yrel = event.motion.yrel,
};
Expand Down Expand Up @@ -338,6 +354,17 @@ void syscall_submit_queue(riscv_t *rv)
case RELATIVE_MODE_SUBMISSION:
SDL_SetRelativeMouseMode(submission.mouse.enabled);
break;
case WINDOW_TITLE_SUBMISSION:
char *title = malloc(submission.title.size + 1);
if (unlikely(!title))
return;

memory_read(PRIV(rv)->mem, (uint8_t *) title,
submission.title.title, submission.title.size);
title[submission.title.size] = 0;

SDL_SetWindowTitle(window, title);
break;
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions tests/smolnes.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@
case x: \
case x + 16:

enum { KEY_EVENT = 0, MOUSE_MOTION_EVENT = 1, MOUSE_BUTTON_EVENT = 2 };
enum {
KEY_EVENT = 0,
MOUSE_MOTION_EVENT = 1,
MOUSE_BUTTON_EVENT = 2,
QUIT_EVENT = 3
};

typedef struct {
uint32_t keycode;
uint8_t state;
} key_event_t;

typedef struct {
int32_t xrel, yrel;
int32_t x, y, xrel, yrel;
} mouse_motion_t;

typedef struct {
Expand All @@ -91,14 +96,22 @@ typedef struct {

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

enum { RELATIVE_MODE_SUBMISSION = 0 };
enum { RELATIVE_MODE_SUBMISSION = 0, WINDOW_TITLE_SUBMISSION = 1 };

typedef struct {
uint8_t enabled;
} mouse_submission_t;

typedef struct {
uint32_t title;
uint32_t size;
} title_submission_t;

typedef struct {
uint32_t type;
union {
union {
uint8_t enabled;
} mouse;
mouse_submission_t mouse;
title_submission_t title;
};
} submission_t;

Expand Down Expand Up @@ -850,6 +863,8 @@ int main(int argc, char **argv)
}
break;
}
case QUIT_EVENT:
return 0;
}
}

Expand Down