Squashed 'external/ircolib/' changes from ce3cd726c..de6e324bd

de6e324bd separate emu thread
10d3daf86 Roms List improvements
95d202f37 Let's make the rom list process on a separate thread so the emulator doesnt take ages to load.
fc306967f Wow the ROM Header was just completely busted. Game list view works now
bad1691ee fuck this shit
2b59e5f46 game list in progress
d26417b83 remappable inputs in progress
ac4af8106 input
e72abc240 update readme
430139dc9 Qt6 frontend
3080d4d45 Fix this small bug too
08cd13b85 Cop0 unused functions do not actually pose a threat (as per manual). They don't do anything, so shall we.
61bb4fb44 make idle loop detection a little more specific with where the load goes
b037de4c3 SAZDFsdff
12e81e73e need to figure out why n64-systemtest loops indefinitely at some address that appears to be valid (i think it's me not invalidating the cache properly)
204f0e13b idle skipping seems to work!
cb8bb634a sdkfjlasdf
58e5c89c1 Fix compilation issue on my machine (no idea)
24fb2898e attempting more serious idle skipping
214719577 Place rsp.Step inside cached interpreter. Gains about 3 more fps
bb97dcc23 mmmmm
920b77d38 wjkhasdfjhkasdf
430ccdab4 it's a start...
4f42a673a Cached interpreter plays Mario 64. Start looking into RSP as well
c9a030787 idle skipping works!
5fbda03ce new idea
366637aba Idle skipping... maybe?
609fa2fb0 Cache instructions implemented but broken lmao. Commented out for now
e140a6d12 - Stop using inheritance for CPU, instead use composition. - Introduce KAIZEN_JIT_ENABLED optional define instead of relying on __aarch64__ and the like. - More cache work
68e613057 prep cache impl
811b4d809 fix clang format
fda755f7d idk
d5024ebbf small MI refactor in preparation of (eventually) implementing the RDRAM interface properly
694b45341 Merge commit '206dcdedf195fb320913584180edb12c7731e396' as 'external/SDL'
206dcdedf Squashed 'external/SDL/' content from commit 4d17b99d0a
4d16e1cb4 need to update sdl
848b19920 Fix compilation error
db61b5299 Merge commit 'e94a94559f28e49678fbcf72199a5258137b0fe9' as 'external/imgui'
e94a94559 Squashed 'external/imgui/' content from commit 02e9b8cac
52edb3757 need to update imgui
c1a705e86 Emulate weird JALR behaviour
4b4c32f4b Fix exception for "unusable COP1" in 4 instructions i missed accidentally (again)
df5828142 Bug putting 0s in the log everywhere
f8b580048 Make isviewer a sink to file
8241e9735 Fix exception for "unusable COP1" in 4 instructions i missed accidentally
b29715f20 small changes
d9a620bc1 make use of my new small utility library
0d1aa938e Add 'external/ircolib/' from commit 'ce3cd726c8df8388d554abf8bb55d55020eb4450'
e64eb40b3 Fuck git

git-subtree-dir: external/ircolib
git-subtree-split: de6e324bde
This commit is contained in:
2026-06-15 11:56:38 +02:00
parent ce3cd726c8
commit 00cc9309cb
4479 changed files with 2943227 additions and 7 deletions
+1
View File
@@ -0,0 +1 @@
A complete game of Snake, written in SDL.
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

+396
View File
@@ -0,0 +1,396 @@
/*
* Logic implementation of the Snake game. It is designed to efficiently
* represent the state of the game in memory.
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#define STEP_RATE_IN_MILLISECONDS 125
#define SNAKE_BLOCK_SIZE_IN_PIXELS 24
#define SDL_WINDOW_WIDTH (SNAKE_BLOCK_SIZE_IN_PIXELS * SNAKE_GAME_WIDTH)
#define SDL_WINDOW_HEIGHT (SNAKE_BLOCK_SIZE_IN_PIXELS * SNAKE_GAME_HEIGHT)
#define SNAKE_GAME_WIDTH 24U
#define SNAKE_GAME_HEIGHT 18U
#define SNAKE_MATRIX_SIZE (SNAKE_GAME_WIDTH * SNAKE_GAME_HEIGHT)
#define SNAKE_CELL_MAX_BITS 3U /* floor(log2(SNAKE_CELL_FOOD)) + 1 */
#define SNAKE_CELL_SET_BITS (~(~0u << SNAKE_CELL_MAX_BITS))
#define SHIFT(x, y) (((x) + ((y) * SNAKE_GAME_WIDTH)) * SNAKE_CELL_MAX_BITS)
static SDL_Joystick *joystick = NULL;
typedef enum
{
SNAKE_CELL_NOTHING = 0U,
SNAKE_CELL_SRIGHT = 1U,
SNAKE_CELL_SUP = 2U,
SNAKE_CELL_SLEFT = 3U,
SNAKE_CELL_SDOWN = 4U,
SNAKE_CELL_FOOD = 5U
} SnakeCell;
typedef enum
{
SNAKE_DIR_RIGHT,
SNAKE_DIR_UP,
SNAKE_DIR_LEFT,
SNAKE_DIR_DOWN
} SnakeDirection;
typedef struct
{
unsigned char cells[(SNAKE_MATRIX_SIZE * SNAKE_CELL_MAX_BITS) / 8U];
char head_xpos;
char head_ypos;
char tail_xpos;
char tail_ypos;
char next_dir;
char inhibit_tail_step;
unsigned occupied_cells;
} SnakeContext;
typedef struct
{
SDL_Window *window;
SDL_Renderer *renderer;
SnakeContext snake_ctx;
Uint64 last_step;
} AppState;
SnakeCell snake_cell_at(const SnakeContext *ctx, char x, char y)
{
const int shift = SHIFT(x, y);
unsigned short range;
SDL_memcpy(&range, ctx->cells + (shift / 8), sizeof(range));
return (SnakeCell)((range >> (shift % 8)) & SNAKE_CELL_SET_BITS);
}
static void set_rect_xy_(SDL_FRect *r, short x, short y)
{
r->x = (float)(x * SNAKE_BLOCK_SIZE_IN_PIXELS);
r->y = (float)(y * SNAKE_BLOCK_SIZE_IN_PIXELS);
}
static void put_cell_at_(SnakeContext *ctx, char x, char y, SnakeCell ct)
{
const int shift = SHIFT(x, y);
const int adjust = shift % 8;
unsigned char *const pos = ctx->cells + (shift / 8);
unsigned short range;
SDL_memcpy(&range, pos, sizeof(range));
range &= ~(SNAKE_CELL_SET_BITS << adjust); /* clear bits */
range |= (ct & SNAKE_CELL_SET_BITS) << adjust;
SDL_memcpy(pos, &range, sizeof(range));
}
static int are_cells_full_(SnakeContext *ctx)
{
return ctx->occupied_cells == SNAKE_GAME_WIDTH * SNAKE_GAME_HEIGHT;
}
static void new_food_pos_(SnakeContext *ctx)
{
while (true) {
const char x = (char) SDL_rand(SNAKE_GAME_WIDTH);
const char y = (char) SDL_rand(SNAKE_GAME_HEIGHT);
if (snake_cell_at(ctx, x, y) == SNAKE_CELL_NOTHING) {
put_cell_at_(ctx, x, y, SNAKE_CELL_FOOD);
break;
}
}
}
void snake_initialize(SnakeContext *ctx)
{
int i;
SDL_zeroa(ctx->cells);
ctx->head_xpos = ctx->tail_xpos = SNAKE_GAME_WIDTH / 2;
ctx->head_ypos = ctx->tail_ypos = SNAKE_GAME_HEIGHT / 2;
ctx->next_dir = SNAKE_DIR_RIGHT;
ctx->inhibit_tail_step = ctx->occupied_cells = 4;
--ctx->occupied_cells;
put_cell_at_(ctx, ctx->tail_xpos, ctx->tail_ypos, SNAKE_CELL_SRIGHT);
for (i = 0; i < 4; i++) {
new_food_pos_(ctx);
++ctx->occupied_cells;
}
}
void snake_redir(SnakeContext *ctx, SnakeDirection dir)
{
SnakeCell ct = snake_cell_at(ctx, ctx->head_xpos, ctx->head_ypos);
if ((dir == SNAKE_DIR_RIGHT && ct != SNAKE_CELL_SLEFT) ||
(dir == SNAKE_DIR_UP && ct != SNAKE_CELL_SDOWN) ||
(dir == SNAKE_DIR_LEFT && ct != SNAKE_CELL_SRIGHT) ||
(dir == SNAKE_DIR_DOWN && ct != SNAKE_CELL_SUP)) {
ctx->next_dir = dir;
}
}
static void wrap_around_(char *val, char max)
{
if (*val < 0) {
*val = max - 1;
} else if (*val > max - 1) {
*val = 0;
}
}
void snake_step(SnakeContext *ctx)
{
const SnakeCell dir_as_cell = (SnakeCell)(ctx->next_dir + 1);
SnakeCell ct;
char prev_xpos;
char prev_ypos;
/* Move tail forward */
if (--ctx->inhibit_tail_step == 0) {
++ctx->inhibit_tail_step;
ct = snake_cell_at(ctx, ctx->tail_xpos, ctx->tail_ypos);
put_cell_at_(ctx, ctx->tail_xpos, ctx->tail_ypos, SNAKE_CELL_NOTHING);
switch (ct) {
case SNAKE_CELL_SRIGHT:
ctx->tail_xpos++;
break;
case SNAKE_CELL_SUP:
ctx->tail_ypos--;
break;
case SNAKE_CELL_SLEFT:
ctx->tail_xpos--;
break;
case SNAKE_CELL_SDOWN:
ctx->tail_ypos++;
break;
default:
break;
}
wrap_around_(&ctx->tail_xpos, SNAKE_GAME_WIDTH);
wrap_around_(&ctx->tail_ypos, SNAKE_GAME_HEIGHT);
}
/* Move head forward */
prev_xpos = ctx->head_xpos;
prev_ypos = ctx->head_ypos;
switch (ctx->next_dir) {
case SNAKE_DIR_RIGHT:
++ctx->head_xpos;
break;
case SNAKE_DIR_UP:
--ctx->head_ypos;
break;
case SNAKE_DIR_LEFT:
--ctx->head_xpos;
break;
case SNAKE_DIR_DOWN:
++ctx->head_ypos;
break;
default:
break;
}
wrap_around_(&ctx->head_xpos, SNAKE_GAME_WIDTH);
wrap_around_(&ctx->head_ypos, SNAKE_GAME_HEIGHT);
/* Collisions */
ct = snake_cell_at(ctx, ctx->head_xpos, ctx->head_ypos);
if (ct != SNAKE_CELL_NOTHING && ct != SNAKE_CELL_FOOD) {
snake_initialize(ctx);
return;
}
put_cell_at_(ctx, prev_xpos, prev_ypos, dir_as_cell);
put_cell_at_(ctx, ctx->head_xpos, ctx->head_ypos, dir_as_cell);
if (ct == SNAKE_CELL_FOOD) {
if (are_cells_full_(ctx)) {
snake_initialize(ctx);
return;
}
new_food_pos_(ctx);
++ctx->inhibit_tail_step;
++ctx->occupied_cells;
}
}
static SDL_AppResult handle_key_event_(SnakeContext *ctx, SDL_Scancode key_code)
{
switch (key_code) {
/* Quit. */
case SDL_SCANCODE_ESCAPE:
case SDL_SCANCODE_Q:
return SDL_APP_SUCCESS;
/* Restart the game as if the program was launched. */
case SDL_SCANCODE_R:
snake_initialize(ctx);
break;
/* Decide new direction of the snake. */
case SDL_SCANCODE_RIGHT:
snake_redir(ctx, SNAKE_DIR_RIGHT);
break;
case SDL_SCANCODE_UP:
snake_redir(ctx, SNAKE_DIR_UP);
break;
case SDL_SCANCODE_LEFT:
snake_redir(ctx, SNAKE_DIR_LEFT);
break;
case SDL_SCANCODE_DOWN:
snake_redir(ctx, SNAKE_DIR_DOWN);
break;
default:
break;
}
return SDL_APP_CONTINUE;
}
static SDL_AppResult handle_hat_event_(SnakeContext *ctx, Uint8 hat) {
switch (hat) {
case SDL_HAT_RIGHT:
snake_redir(ctx, SNAKE_DIR_RIGHT);
break;
case SDL_HAT_UP:
snake_redir(ctx, SNAKE_DIR_UP);
break;
case SDL_HAT_LEFT:
snake_redir(ctx, SNAKE_DIR_LEFT);
break;
case SDL_HAT_DOWN:
snake_redir(ctx, SNAKE_DIR_DOWN);
break;
default:
break;
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void *appstate)
{
AppState *as = (AppState *)appstate;
SnakeContext *ctx = &as->snake_ctx;
const Uint64 now = SDL_GetTicks();
SDL_FRect r;
unsigned i;
unsigned j;
int ct;
// run game logic if we're at or past the time to run it.
// if we're _really_ behind the time to run it, run it
// several times.
while ((now - as->last_step) >= STEP_RATE_IN_MILLISECONDS) {
snake_step(ctx);
as->last_step += STEP_RATE_IN_MILLISECONDS;
}
r.w = r.h = SNAKE_BLOCK_SIZE_IN_PIXELS;
SDL_SetRenderDrawColor(as->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(as->renderer);
for (i = 0; i < SNAKE_GAME_WIDTH; i++) {
for (j = 0; j < SNAKE_GAME_HEIGHT; j++) {
ct = snake_cell_at(ctx, i, j);
if (ct == SNAKE_CELL_NOTHING)
continue;
set_rect_xy_(&r, i, j);
if (ct == SNAKE_CELL_FOOD)
SDL_SetRenderDrawColor(as->renderer, 80, 80, 255, SDL_ALPHA_OPAQUE);
else /* body */
SDL_SetRenderDrawColor(as->renderer, 0, 128, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(as->renderer, &r);
}
}
SDL_SetRenderDrawColor(as->renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); /*head*/
set_rect_xy_(&r, ctx->head_xpos, ctx->head_ypos);
SDL_RenderFillRect(as->renderer, &r);
SDL_RenderPresent(as->renderer);
return SDL_APP_CONTINUE;
}
static const struct
{
const char *key;
const char *value;
} extended_metadata[] =
{
{ SDL_PROP_APP_METADATA_URL_STRING, "https://examples.libsdl.org/SDL3/demo/01-snake/" },
{ SDL_PROP_APP_METADATA_CREATOR_STRING, "SDL team" },
{ SDL_PROP_APP_METADATA_COPYRIGHT_STRING, "Placed in the public domain" },
{ SDL_PROP_APP_METADATA_TYPE_STRING, "game" }
};
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
size_t i;
if (!SDL_SetAppMetadata("Example Snake game", "1.0", "com.example.Snake")) {
return SDL_APP_FAILURE;
}
for (i = 0; i < SDL_arraysize(extended_metadata); i++) {
if (!SDL_SetAppMetadataProperty(extended_metadata[i].key, extended_metadata[i].value)) {
return SDL_APP_FAILURE;
}
}
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
AppState *as = (AppState *)SDL_calloc(1, sizeof(AppState));
if (!as) {
return SDL_APP_FAILURE;
}
*appstate = as;
if (!SDL_CreateWindowAndRenderer("examples/demo/snake", SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &as->window, &as->renderer)) {
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(as->renderer, SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
snake_initialize(&as->snake_ctx);
as->last_step = SDL_GetTicks();
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
SnakeContext *ctx = &((AppState *)appstate)->snake_ctx;
switch (event->type) {
case SDL_EVENT_QUIT:
return SDL_APP_SUCCESS;
case SDL_EVENT_JOYSTICK_ADDED:
if (joystick == NULL) {
joystick = SDL_OpenJoystick(event->jdevice.which);
if (!joystick) {
SDL_Log("Failed to open joystick ID %u: %s", (unsigned int) event->jdevice.which, SDL_GetError());
}
}
break;
case SDL_EVENT_JOYSTICK_REMOVED:
if (joystick && (SDL_GetJoystickID(joystick) == event->jdevice.which)) {
SDL_CloseJoystick(joystick);
joystick = NULL;
}
break;
case SDL_EVENT_JOYSTICK_HAT_MOTION:
return handle_hat_event_(ctx, event->jhat.value);
case SDL_EVENT_KEY_DOWN:
return handle_key_event_(ctx, event->key.scancode);
default:
break;
}
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
if (joystick) {
SDL_CloseJoystick(joystick);
}
if (appstate != NULL) {
AppState *as = (AppState *)appstate;
SDL_DestroyRenderer(as->renderer);
SDL_DestroyWindow(as->window);
SDL_free(as);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB