tuxanci2-engine
Game engine for tuxanci2
Log | Files | Refs | README | LICENSE
commit ada48f752b9c7528d351f3152fa02e6e45051869
parent c04b78ce586b766021d538a530f6347d456f87e7
Author: Connor Thomson <connor@gnufault.org>
Date:   Fri, 30 Oct 2026 19:13:54 -0700

Split tuxanci2.h into many files

Diffstat:
Mgame/src/main.c | 7+++++--
Mgame/src/screens/ta_screen_loading.c | 24++++++++++++++----------
Mgame/src/screens/ta_screen_loading.h | 2--
Mgame/src/screens/ta_screen_main_menu.c | 12+++++++-----
Mgame/src/screens/ta_screen_main_menu.h | 2--
Mgame/src/ta_button.c | 102++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mgame/src/ta_button.h | 27+++++++++++++++------------
Minclude/ta_vm.h | 2+-
Asdk/include/stdbool.h | 10++++++++++
Asdk/include/stddef.h | 8++++++++
Asdk/include/stdint.h | 12++++++++++++
Asdk/include/stdlib.h | 19+++++++++++++++++++
Asdk/include/string.h | 11+++++++++++
Asdk/include/tuxanci2/abi.h | 25+++++++++++++++++++++++++
Asdk/include/tuxanci2/display.h | 23+++++++++++++++++++++++
Asdk/include/tuxanci2/fixed.h | 10++++++++++
Asdk/include/tuxanci2/image.h | 23+++++++++++++++++++++++
Asdk/include/tuxanci2/keyboard.h | 144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asdk/include/tuxanci2/screen.h | 22++++++++++++++++++++++
Asdk/include/tuxanci2/syscall.h | 20++++++++++++++++++++
Asdk/include/tuxanci2/text.h | 31+++++++++++++++++++++++++++++++
Msdk/sdk.mk | 31++++++++++++++++++++-----------
Asdk/src/crt0.c | 21+++++++++++++++++++++
Asdk/src/stdlib.c | 0
Asdk/src/string.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsdk/ta_config.h | 6------
Dsdk/ta_runtime.c | 118-------------------------------------------------------------------------------
Dsdk/tuxanci2.h | 102-------------------------------------------------------------------------------
28 files changed, 548 insertions(+), 319 deletions(-)

diff --git a/game/src/main.c b/game/src/main.c @@ -1,10 +1,13 @@ -#include "tuxanci2.h" +#include <tuxanci2/screen.h> #include "ta_screens.h" #include "screens/ta_screen_loading.h" #include "screens/ta_screen_main_menu.h" -void main(void) { +int main(void) { ta_screen_register(TA_SCREEN_LOADING, ta_screen_loading_init, ta_screen_loading_update, ta_screen_loading_destroy); ta_screen_register(TA_SCREEN_MAIN_MENU, ta_screen_main_menu_init, ta_screen_main_menu_update, ta_screen_main_menu_destroy); + ta_screen_switch(TA_SCREEN_LOADING); + + return 0; } diff --git a/game/src/screens/ta_screen_loading.c b/game/src/screens/ta_screen_loading.c @@ -1,5 +1,9 @@ -#include "screens/ta_screen_loading.h" -#include "ta_screens.h" +#include <tuxanci2/screen.h> +#include <tuxanci2/display.h> +#include <tuxanci2/image.h> +#include <stdint.h> +#include "ta_screen_loading.h" +#include "../ta_screens.h" #define TA_LOADING_LOGO_SIZE TA_FIXED(0.3) @@ -8,28 +12,28 @@ void ta_screen_loading_init(void) { } void ta_screen_loading_update(void) { - ta_uint32 width; - ta_uint32 height; + uint32_t width; + uint32_t height; if (!ta_image_get_size(TA_ASSET_LOADING_LOGO, &width, &height)) { return; } - ta_uint64 frame_width = ta_display_width(); - ta_uint64 frame_height = ta_display_height(); + uint32_t frame_width = ta_display_width(); + uint32_t frame_height = ta_display_height(); ta_fixed draw_width; ta_fixed draw_height; if (frame_width * height <= frame_height * width) { draw_width = TA_LOADING_LOGO_SIZE; - draw_height = (ta_fixed)((TA_LOADING_LOGO_SIZE * (ta_uint64)height * frame_width) / ((ta_uint64)width * frame_height)); + draw_height = (ta_fixed)((TA_LOADING_LOGO_SIZE * (uint32_t)height * frame_width) / ((uint32_t)width * frame_height)); } else { draw_height = TA_LOADING_LOGO_SIZE; - draw_width = (ta_fixed)((TA_LOADING_LOGO_SIZE * (ta_uint64)width * frame_height) / ((ta_uint64)height * frame_width)); + draw_width = (ta_fixed)((TA_LOADING_LOGO_SIZE * (uint32_t)width * frame_height) / ((uint32_t)height * frame_width)); } - ta_fixed x = (TA_FIXED_ONE - draw_width) / 2; - ta_fixed y = (TA_FIXED_ONE - draw_height) / 2; + ta_fixed x = (TA_FIXED(1.0) - draw_width) / 2; + ta_fixed y = (TA_FIXED(1.0) - draw_height) / 2; ta_image_draw(TA_ASSET_LOADING_LOGO, x, y, draw_width, draw_height, 0); diff --git a/game/src/screens/ta_screen_loading.h b/game/src/screens/ta_screen_loading.h @@ -1,8 +1,6 @@ #ifndef TA_SCREEN_LOADING_H #define TA_SCREEN_LOADING_H -#include "tuxanci2.h" - void ta_screen_loading_init(void); void ta_screen_loading_update(void); void ta_screen_loading_destroy(void); diff --git a/game/src/screens/ta_screen_main_menu.c b/game/src/screens/ta_screen_main_menu.c @@ -1,24 +1,26 @@ -#include "screens/ta_screen_main_menu.h" -#include "ta_button.h" +#include <stdlib.h> +#include <tuxanci2/image.h> +#include "ta_screen_main_menu.h" +#include "../ta_button.h" static ta_button_lists menu; static void quit(void) { - ta_exit(0); + exit(0); } void ta_screen_main_menu_init(void) { ta_image_init(TA_ASSET_BG); ta_button_lists_init(&menu); - ta_button_lists_add(&menu, "Play", TA_NULL); + ta_button_lists_add(&menu, "Play", NULL); ta_button_lists_add(&menu, "Quit", quit); } void ta_screen_main_menu_update(void) { ta_button_lists_update(&menu); - ta_image_draw(TA_ASSET_BG, 0, 0, TA_FIXED_ONE, TA_FIXED_ONE, 0); + ta_image_draw(TA_ASSET_BG, 0, 0, TA_FIXED(1.0), TA_FIXED(1.0), 0); ta_button_lists_draw(&menu); } diff --git a/game/src/screens/ta_screen_main_menu.h b/game/src/screens/ta_screen_main_menu.h @@ -1,8 +1,6 @@ #ifndef TA_SCREEN_MAIN_MENU_H #define TA_SCREEN_MAIN_MENU_H -#include "tuxanci2.h" - void ta_screen_main_menu_init(void); void ta_screen_main_menu_update(void); void ta_screen_main_menu_destroy(void); diff --git a/game/src/ta_button.c b/game/src/ta_button.c @@ -1,4 +1,10 @@ #include "ta_button.h" +#include <tuxanci2/text.h> +#include <tuxanci2/display.h> +#include <tuxanci2/keyboard.h> +#include <stdint.h> +#include <stdlib.h> +#include <stddef.h> #define TA_BUTTON_FONT_SIZE 32 #define TA_BUTTON_FONT_SIZE_MAX 255 @@ -8,67 +14,67 @@ #define TA_BUTTON_REFERENCE_WIDTH 800 #define TA_BUTTON_REFERENCE_HEIGHT 600 -static ta_uint32 scale_to_display(ta_uint32 value) { - ta_uint32 display_width = ta_display_width(); - ta_uint32 display_height = ta_display_height(); - ta_uint32 numerator = display_width; - ta_uint32 denominator = TA_BUTTON_REFERENCE_WIDTH; +static uint32_t scale_to_display(uint32_t value) { + uint32_t display_width = ta_display_width(); + uint32_t display_height = ta_display_height(); + uint32_t numerator = display_width; + uint32_t denominator = TA_BUTTON_REFERENCE_WIDTH; - if ((ta_uint64)display_width * TA_BUTTON_REFERENCE_HEIGHT >= (ta_uint64)display_height * TA_BUTTON_REFERENCE_WIDTH) { + if ((uint32_t)display_width * TA_BUTTON_REFERENCE_HEIGHT >= (uint32_t)display_height * TA_BUTTON_REFERENCE_WIDTH) { numerator = display_height; denominator = TA_BUTTON_REFERENCE_HEIGHT; } - ta_uint32 scaled = (value * numerator * 2 + denominator) / (denominator * 2); + uint32_t scaled = (value * numerator * 2 + denominator) / (denominator * 2); return scaled == 0 ? 1 : scaled; } void ta_button_lists_init(ta_button_lists *list) { - if (list == TA_NULL) { + if (list == NULL) { return; } - list->buttons = TA_NULL; + list->buttons = NULL; list->buttons_size = 0; list->size = 0; list->selected = 0; - list->up_was_down = ta_false; - list->down_was_down = ta_false; - list->enter_was_down = ta_false; + list->up_was_down = false; + list->down_was_down = false; + list->enter_was_down = false; } -ta_bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_callback callback) { - if (list == TA_NULL || label == TA_NULL || list->size >= TA_BUTTON_MAX) { - return ta_false; +bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_callback callback) { + if (list == NULL || label == NULL) { + return false; } if (list->size >= list->buttons_size) { - ta_size new_capacity = list->buttons_size + 1; + size_t new_capacity = list->buttons_size + 1; - ta_button *temp = (ta_button *)ta_realloc(list->buttons, new_capacity * sizeof(ta_button)); + ta_button *temp = (ta_button *)realloc(list->buttons, new_capacity * sizeof(ta_button)); - if (temp == TA_NULL) { - return ta_false; + if (temp == NULL) { + return false; } list->buttons = temp; list->buttons_size = new_capacity; } - ta_uint32 font_size = scale_to_display(TA_BUTTON_FONT_SIZE); + uint32_t font_size = scale_to_display(TA_BUTTON_FONT_SIZE); if (font_size > TA_BUTTON_FONT_SIZE_MAX) { font_size = TA_BUTTON_FONT_SIZE_MAX; } - ta_text_handle text_idle = ta_text_create(label, font_size, TA_RGB(TA_BUTTON_COLOR_IDLE, TA_BUTTON_COLOR_IDLE, TA_BUTTON_COLOR_IDLE), 1, TA_RGB(0, 0, 0)); - ta_text_handle text_selected = ta_text_create(label, font_size, TA_RGB(TA_BUTTON_COLOR_SELECTED, TA_BUTTON_COLOR_SELECTED, TA_BUTTON_COLOR_SELECTED), 1, TA_RGB(0, 0, 0)); + ta_text text_idle = ta_text_create(label, font_size, TA_DISPLAY_RGB(TA_BUTTON_COLOR_IDLE, TA_BUTTON_COLOR_IDLE, TA_BUTTON_COLOR_IDLE), 1, TA_DISPLAY_RGB(0, 0, 0)); + ta_text text_selected = ta_text_create(label, font_size, TA_DISPLAY_RGB(TA_BUTTON_COLOR_SELECTED, TA_BUTTON_COLOR_SELECTED, TA_BUTTON_COLOR_SELECTED), 1, TA_DISPLAY_RGB(0, 0, 0)); if (text_idle == 0 || text_selected == 0) { ta_text_destroy(text_idle); ta_text_destroy(text_selected); - return ta_false; + return false; } ta_button *button = &list->buttons[list->size]; @@ -82,20 +88,20 @@ ta_bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_ list->size++; - return ta_true; + return true; } void ta_button_lists_update(ta_button_lists *list) { - if (list == TA_NULL || list->size == 0) { + if (list == NULL || list->size == 0) { return; } - ta_bool up_down = ta_keyboard_is_down(KEY_UP); - ta_bool down_down = ta_keyboard_is_down(KEY_DOWN); - ta_bool enter_down = ta_keyboard_is_down(KEY_ENTER); - ta_bool up_pressed = up_down && !list->up_was_down; - ta_bool down_pressed = down_down && !list->down_was_down; - ta_bool enter_pressed = enter_down && !list->enter_was_down; + bool up_down = ta_keyboard_is_down(KEY_UP); + bool down_down = ta_keyboard_is_down(KEY_DOWN); + bool enter_down = ta_keyboard_is_down(KEY_ENTER); + bool up_pressed = up_down && !list->up_was_down; + bool down_pressed = down_down && !list->down_was_down; + bool enter_pressed = enter_down && !list->enter_was_down; list->up_was_down = up_down; list->down_was_down = down_down; @@ -109,48 +115,48 @@ void ta_button_lists_update(ta_button_lists *list) { } } - if (enter_pressed && list->buttons[list->selected].callback != TA_NULL) { + if (enter_pressed && list->buttons[list->selected].callback != NULL) { list->buttons[list->selected].callback(); } } void ta_button_lists_draw(const ta_button_lists *list) { - if (list == TA_NULL || list->size == 0) { + if (list == NULL || list->size == 0) { return; } - ta_uint32 display_width = ta_display_width(); - ta_uint32 display_height = ta_display_height(); - ta_int32 spacing = (ta_int32)scale_to_display(TA_BUTTON_SPACING); - ta_int32 center_y = (ta_int32)(display_height / 2); - ta_int32 total_span = (ta_int32)(list->size - 1) * spacing; - ta_int32 start_y = center_y - (total_span / 2); + uint32_t display_width = ta_display_width(); + uint32_t display_height = ta_display_height(); + int32_t spacing = (int32_t)scale_to_display(TA_BUTTON_SPACING); + int32_t center_y = (int32_t)(display_height / 2); + int32_t total_span = (int32_t)(list->size - 1) * spacing; + int32_t start_y = center_y - (total_span / 2); - for (ta_size i = 0; i < list->size; i++) { + for (size_t i = 0; i < list->size; i++) { const ta_button *button = &list->buttons[i]; - ta_text_handle text = i == list->selected ? button->text_selected : button->text_idle; - ta_int32 y = start_y + (ta_int32)i * spacing - (ta_int32)(button->text_height / 2); + ta_text text = i == list->selected ? button->text_selected : button->text_idle; + int32_t y = start_y + (int32_t)i * spacing - (int32_t)(button->text_height / 2); - if (y < 0 || (ta_size)y >= display_height || button->text_width > display_width) { + if (y < 0 || (size_t)y >= display_height || button->text_width > display_width) { continue; } - ta_text_draw(text, (display_width - button->text_width) / 2, (ta_uint32)y); + ta_text_draw(text, (display_width - button->text_width) / 2, (uint32_t)y); } } void ta_button_lists_destroy(ta_button_lists *list) { - if (list == TA_NULL) { + if (list == NULL) { return; } - for (ta_size i = 0; i < list->size; i++) { + for (size_t i = 0; i < list->size; i++) { ta_text_destroy(list->buttons[i].text_idle); ta_text_destroy(list->buttons[i].text_selected); } - if (list->buttons != TA_NULL) { - ta_free(list->buttons); + if (list->buttons != NULL) { + free(list->buttons); } ta_button_lists_init(list); diff --git a/game/src/ta_button.h b/game/src/ta_button.h @@ -1,31 +1,34 @@ #ifndef TA_BUTTON_H #define TA_BUTTON_H -#include "tuxanci2.h" +#include <tuxanci2/text.h> +#include <stdint.h> +#include <stdbool.h> +#include <stddef.h> typedef void (*ta_button_callback)(void); typedef struct { const char *label; ta_button_callback callback; - ta_text_handle text_idle; - ta_text_handle text_selected; - ta_uint32 text_width; - ta_uint32 text_height; + ta_text text_idle; + ta_text text_selected; + uint32_t text_width; + uint32_t text_height; } ta_button; typedef struct { ta_button *buttons; - ta_size buttons_size; - ta_size size; - ta_size selected; - ta_bool up_was_down; - ta_bool down_was_down; - ta_bool enter_was_down; + size_t buttons_size; + size_t size; + size_t selected; + bool up_was_down; + bool down_was_down; + bool enter_was_down; } ta_button_lists; void ta_button_lists_init(ta_button_lists *list); -ta_bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_callback callback); +bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_callback callback); void ta_button_lists_update(ta_button_lists *list); void ta_button_lists_draw(const ta_button_lists *list); void ta_button_lists_destroy(ta_button_lists *list); diff --git a/include/ta_vm.h b/include/ta_vm.h @@ -46,7 +46,7 @@ ta_bool ta_vm_write32(ta_vm *vm, ta_uint32 addr, ta_uint32 value); const char *ta_vm_cstr(const ta_vm *vm, ta_uint32 addr); #ifndef TA_VM_RAM_SIZE -#define TA_VM_RAM_SIZE (256u * 1024u) +#define TA_VM_RAM_SIZE (64u * 1024u) #endif #ifndef TA_VM_STEPS_PER_CALL diff --git a/sdk/include/stdbool.h b/sdk/include/stdbool.h @@ -0,0 +1,10 @@ +#ifndef TA_STDBOOL_H +#define TA_STDBOOL_H + +#define bool _Bool +#define true 1 +#define false 0 + +#define __bool_true_false_are_defined 1 + +#endif // TA_STDBOOL_H diff --git a/sdk/include/stddef.h b/sdk/include/stddef.h @@ -0,0 +1,8 @@ +#ifndef TA_STDDEF_H +#define TA_STDDEF_H + +#define NULL ((void*)0) + +typedef unsigned long size_t; + +#endif // TA_STDDEF_H diff --git a/sdk/include/stdint.h b/sdk/include/stdint.h @@ -0,0 +1,12 @@ +#ifndef TA_STDINT_H +#define TA_STDINT_H + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; + +typedef signed char int8_t; +typedef short int16_t; +typedef int int32_t; + +#endif // TA_STDINT_H diff --git a/sdk/include/stdlib.h b/sdk/include/stdlib.h @@ -0,0 +1,19 @@ +#ifndef TA_STDLIB_H +#define TA_STDLIB_H + +#include <tuxanci2/syscall.h> +#include <tuxanci2/abi.h> +#include <stdint.h> +#include <stddef.h> + +void *malloc(size_t size); +void *realloc(void *ptr, size_t size); +void free(void *ptr); + +static inline void exit(int status) { + ta_syscall(TA_SYS_EXIT, (uint32_t)status, 0, 0, 0, 0, 0); + + while (1) {} +} + +#endif // TA_STDLIB_H diff --git a/sdk/include/string.h b/sdk/include/string.h @@ -0,0 +1,11 @@ +#ifndef TA_STRING_H +#define TA_STRING_H + +#include <stddef.h> + +void *memcpy(void *destination, const void *source, size_t count); +void *memmove(void *destination, const void *source, size_t count); +void *memset(void *destination, int value, size_t count); +int memcmp(const void *first, const void *second, size_t count); + +#endif // TA_STRING_H diff --git a/sdk/include/tuxanci2/abi.h b/sdk/include/tuxanci2/abi.h @@ -0,0 +1,25 @@ +#ifndef TA_ABI_H +#define TA_ABI_H + +#define TA_SYS_EXIT 0 +#define TA_SYS_READY 1 +#define TA_SYS_RETURN 2 +#define TA_SYS_DISPLAY_WIDTH 3 +#define TA_SYS_DISPLAY_HEIGHT 4 +#define TA_SYS_DISPLAY_SPRITE_MODE 5 +#define TA_SYS_KEYBOARD_IS_DOWN 10 +#define TA_SYS_IMAGE_INIT 11 +#define TA_SYS_IMAGE_DRAW 12 +#define TA_SYS_IMAGE_GET_SIZE 13 +#define TA_SYS_TEXT_CREATE 14 +#define TA_SYS_TEXT_WIDTH 15 +#define TA_SYS_TEXT_HEIGHT 16 +#define TA_SYS_TEXT_DRAW 17 +#define TA_SYS_TEXT_DESTROY 18 +#define TA_SYS_SCREEN_REGISTER 19 +#define TA_SYS_SCREEN_SWITCH 20 + +#define TA_ASSET_BG 0 +#define TA_ASSET_LOADING_LOGO 1 + +#endif // TA_ABI_H diff --git a/sdk/include/tuxanci2/display.h b/sdk/include/tuxanci2/display.h @@ -0,0 +1,23 @@ +#ifndef TA_DISPLAY_H +#define TA_DISPLAY_H + +#include <tuxanci2/syscall.h> +#include <tuxanci2/abi.h> +#include <stdint.h> +#include <stdbool.h> + +#define TA_DISPLAY_RGB(red, green, blue) ((((uint32_t)(red) & 255u) << 16) | (((uint32_t)(green) & 255u) << 8) | ((uint32_t)(blue) & 255u)) + +static inline uint32_t ta_display_width(void) { + return ta_syscall(TA_SYS_DISPLAY_WIDTH, 0, 0, 0, 0, 0, 0); +} + +static inline uint32_t ta_display_height(void) { + return ta_syscall(TA_SYS_DISPLAY_HEIGHT, 0, 0, 0, 0, 0, 0); +} + +static inline bool ta_display_sprite_mode(void) { + return ta_syscall(TA_SYS_DISPLAY_SPRITE_MODE, 0, 0, 0, 0, 0, 0) != 0; +} + +#endif // TA_DISPLAY_H diff --git a/sdk/include/tuxanci2/fixed.h b/sdk/include/tuxanci2/fixed.h @@ -0,0 +1,10 @@ +#ifndef TA_FIXED_H +#define TA_FIXED_H + +#include <stdint.h> + +typedef uint32_t ta_fixed; + +#define TA_FIXED(value) ((ta_fixed)((value) * 65536.0 + 0.5)) + +#endif // TA_FIXED_H diff --git a/sdk/include/tuxanci2/image.h b/sdk/include/tuxanci2/image.h @@ -0,0 +1,23 @@ +#ifndef TA_IMAGE_H +#define TA_IMAGE_H + +#include <tuxanci2/syscall.h> +#include <tuxanci2/abi.h> +#include <tuxanci2/fixed.h> +#include <stdint.h> +#include <stdbool.h> +#include <stddef.h> + +static inline bool ta_image_init(uint32_t asset) { + return ta_syscall(TA_SYS_IMAGE_INIT, asset, 0, 0, 0, 0, 0) != 0; +} + +static inline bool ta_image_draw(uint32_t asset, ta_fixed x, ta_fixed y, ta_fixed width, ta_fixed height, uint32_t frame) { + return ta_syscall(TA_SYS_IMAGE_DRAW, asset, x, y, width, height, frame) != 0; +} + +static inline bool ta_image_get_size(uint32_t asset, uint32_t *width, uint32_t *height) { + return ta_syscall(TA_SYS_IMAGE_GET_SIZE, asset, (uint32_t)(size_t)width, (uint32_t)(size_t)height, 0, 0, 0) != 0; +} + +#endif // TA_IMAGE_H diff --git a/sdk/include/tuxanci2/keyboard.h b/sdk/include/tuxanci2/keyboard.h @@ -0,0 +1,144 @@ +#ifndef TA_KEYBOARD_H +#define TA_KEYBOARD_H + +#include <tuxanci2/display.h> +#include <tuxanci2/abi.h> +#include <stdint.h> +#include <stdbool.h> + +typedef enum { + KEY_NONE = 0, + KEY_BACKSPACE = 8, + KEY_TAB = 9, + KEY_ENTER = 13, + KEY_ESCAPE = 27, + KEY_SPACE = 32, + KEY_EXCLAMATION_MARK = 33, + KEY_QUOTATION_MARK = 34, + KEY_HASH = 35, + KEY_DOLLAR_SIGN = 36, + KEY_PERCENT_SIGN = 37, + KEY_AMPERSAND = 38, + KEY_APOSTROPHE = 39, + KEY_LEFT_PARENTHESIS = 40, + KEY_RIGHT_PARENTHESIS = 41, + KEY_ASTERISK = 42, + KEY_PLUS = 43, + KEY_COMMA = 44, + KEY_MINUS = 45, + KEY_PERIOD = 46, + KEY_SLASH = 47, + KEY_0 = 48, + KEY_1 = 49, + KEY_2 = 50, + KEY_3 = 51, + KEY_4 = 52, + KEY_5 = 53, + KEY_6 = 54, + KEY_7 = 55, + KEY_8 = 56, + KEY_9 = 57, + KEY_COLON = 58, + KEY_SEMICOLON = 59, + KEY_LESS_THAN = 60, + KEY_EQUALS = 61, + KEY_GREATER_THAN = 62, + KEY_QUESTION_MARK = 63, + KEY_AT_SIGN = 64, + KEY_A = 97, + KEY_B = 98, + KEY_C = 99, + KEY_D = 100, + KEY_E = 101, + KEY_F = 102, + KEY_G = 103, + KEY_H = 104, + KEY_I = 105, + KEY_J = 106, + KEY_K = 107, + KEY_L = 108, + KEY_M = 109, + KEY_N = 110, + KEY_O = 111, + KEY_P = 112, + KEY_Q = 113, + KEY_R = 114, + KEY_S = 115, + KEY_T = 116, + KEY_U = 117, + KEY_V = 118, + KEY_W = 119, + KEY_X = 120, + KEY_Y = 121, + KEY_Z = 122, + KEY_LEFT_BRACKET = 91, + KEY_BACKSLASH = 92, + KEY_RIGHT_BRACKET = 93, + KEY_CARET = 94, + KEY_UNDERSCORE = 95, + KEY_GRAVE_ACCENT = 96, + KEY_LEFT_BRACE = 123, + KEY_PIPE = 124, + KEY_RIGHT_BRACE = 125, + KEY_TILDE = 126, + KEY_DELETE = 127, + KEY_UP = 128, + KEY_DOWN = 129, + KEY_LEFT = 130, + KEY_RIGHT = 131, + KEY_F1 = 132, + KEY_F2 = 133, + KEY_F3 = 134, + KEY_F4 = 135, + KEY_F5 = 136, + KEY_F6 = 137, + KEY_F7 = 138, + KEY_F8 = 139, + KEY_F9 = 140, + KEY_F10 = 141, + KEY_F11 = 142, + KEY_F12 = 143, + KEY_HOME = 144, + KEY_END = 145, + KEY_PAGE_UP = 146, + KEY_PAGE_DOWN = 147, + KEY_INSERT = 148, + KEY_CAPS_LOCK = 149, + KEY_NUM_LOCK = 150, + KEY_SCROLL_LOCK = 151, + KEY_PRINT_SCREEN = 152, + KEY_PAUSE = 153, + KEY_MENU = 154, + KEY_LEFT_SHIFT = 155, + KEY_RIGHT_SHIFT = 156, + KEY_LEFT_CTRL = 157, + KEY_RIGHT_CTRL = 158, + KEY_LEFT_ALT = 159, + KEY_RIGHT_ALT = 160, + KEY_LEFT_SUPER = 161, + KEY_RIGHT_SUPER = 162, + KEY_KP_0 = 163, + KEY_KP_1 = 164, + KEY_KP_2 = 165, + KEY_KP_3 = 166, + KEY_KP_4 = 167, + KEY_KP_5 = 168, + KEY_KP_6 = 169, + KEY_KP_7 = 170, + KEY_KP_8 = 171, + KEY_KP_9 = 172, + KEY_KP_DECIMAL = 173, + KEY_KP_DIVIDE = 174, + KEY_KP_MULTIPLY = 175, + KEY_KP_SUBTRACT = 176, + KEY_KP_ADD = 177, + KEY_KP_ENTER = 178, + KEY_KP_EQUALS = 179 +} ta_keyboard_keys; + +static inline bool ta_keyboard_is_down(ta_keyboard_keys key) { + return ta_syscall(TA_SYS_KEYBOARD_IS_DOWN, (uint32_t)key, 0, 0, 0, 0, 0) != 0; +} + + +#endif // TA_KEYBOARD_H diff --git a/sdk/include/tuxanci2/screen.h b/sdk/include/tuxanci2/screen.h @@ -0,0 +1,22 @@ +#ifndef TA_SCREEN_H +#define TA_SCREEN_H + +#include <tuxanci2/syscall.h> +#include <tuxanci2/abi.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> + +typedef void (*ta_screen_init_function)(void); +typedef void (*ta_screen_update_function)(void); +typedef void (*ta_screen_destroy_function)(void); + +static inline bool ta_screen_register(uint32_t id, ta_screen_init_function init, ta_screen_update_function update, ta_screen_destroy_function destroy) { + return ta_syscall(TA_SYS_SCREEN_REGISTER, id, (uint32_t)(size_t)init, (uint32_t)(size_t)update, (uint32_t)(size_t)destroy, 0, 0) != 0; +} + +static inline bool ta_screen_switch(uint32_t id) { + return ta_syscall(TA_SYS_SCREEN_SWITCH, id, 0, 0, 0, 0, 0) != 0; +} + +#endif // TA_SCREEN_H diff --git a/sdk/include/tuxanci2/syscall.h b/sdk/include/tuxanci2/syscall.h @@ -0,0 +1,20 @@ +#ifndef TA_SYSCALL_H +#define TA_SYSCALL_H + +#include <stdint.h> + +static inline uint32_t ta_syscall(uint32_t number, uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) { + register uint32_t reg0 __asm__("a0") = arg0; + register uint32_t reg1 __asm__("a1") = arg1; + register uint32_t reg2 __asm__("a2") = arg2; + register uint32_t reg3 __asm__("a3") = arg3; + register uint32_t reg4 __asm__("a4") = arg4; + register uint32_t reg5 __asm__("a5") = arg5; + register uint32_t reg7 __asm__("a7") = number; + + __asm__ volatile("ecall" : "+r"(reg0) : "r"(reg1), "r"(reg2), "r"(reg3), "r"(reg4), "r"(reg5), "r"(reg7) : "memory"); + + return reg0; +} + +#endif // TA_SYSCALL_H diff --git a/sdk/include/tuxanci2/text.h b/sdk/include/tuxanci2/text.h @@ -0,0 +1,31 @@ +#ifndef TA_TEXT_H +#define TA_TEXT_H + +#include <tuxanci2/syscall.h> +#include <tuxanci2/abi.h> +#include <stdint.h> +#include <stddef.h> + +typedef uint32_t ta_text; + +static inline ta_text ta_text_create(const char *text, uint32_t size, uint32_t color, uint32_t border_width, uint32_t border_color) { + return ta_syscall(TA_SYS_TEXT_CREATE, (uint32_t)(size_t)text, size, color, border_width, border_color, 0); +} + +static inline uint32_t ta_text_width(ta_text text) { + return ta_syscall(TA_SYS_TEXT_WIDTH, text, 0, 0, 0, 0, 0); +} + +static inline uint32_t ta_text_height(ta_text text) { + return ta_syscall(TA_SYS_TEXT_HEIGHT, text, 0, 0, 0, 0, 0); +} + +static inline void ta_text_draw(ta_text text, uint32_t x, uint32_t y) { + ta_syscall(TA_SYS_TEXT_DRAW, text, x, y, 0, 0, 0); +} + +static inline void ta_text_destroy(ta_text text) { + ta_syscall(TA_SYS_TEXT_DESTROY, text, 0, 0, 0, 0, 0); +} + +#endif // TA_TEXT_H diff --git a/sdk/sdk.mk b/sdk/sdk.mk @@ -1,3 +1,4 @@ + CROSS ?= riscv32-unknown-elf- CC := $(CROSS)gcc @@ -11,15 +12,23 @@ MARCH := rv32im MABI := ilp32 LINKER_SCRIPT := linker.ld -INCLUDES ?= -CFLAGS := -O$(OPT) -march=$(MARCH) -mabi=$(MABI) -ffreestanding -fno-builtin -fno-pic -fno-stack-protector -fno-tree-loop-distribute-patterns -nostdlib -Wall -Wextra -I$(SDK_DIR) -I$(SDK_DIR)../include $(addprefix -I,$(INCLUDES)) -ASFLAGS := -march=$(MARCH) -mabi=$(MABI) -I$(SDK_DIR) -I$(SDK_DIR)../include +CFLAGS := -O$(OPT) -march=$(MARCH) -mabi=$(MABI) -ffreestanding -nostdinc -fno-builtin -fno-pic -fno-stack-protector -fno-tree-loop-distribute-patterns -Wall -Wextra -I$(SDK_DIR)include -I$(SDK_DIR)../include +ASFLAGS := -march=$(MARCH) -mabi=$(MABI) -I$(SDK_DIR)include -I$(SDK_DIR)../include LDFLAGS := -march=$(MARCH) -mabi=$(MABI) -nostdlib -static -T $(SDK_DIR)$(LINKER_SCRIPT) -Wl,--gc-sections -SRC_FILES := $(sort $(foreach s,$(SRCS),$(if $(wildcard $(s)/.),$(shell find $(patsubst %/,%,$(s)) -name '*.c'),$(s)))) +SDK_SRCS := $(shell find $(SDK_DIR)src -type f \( -name '*.c' -o -name '*.S' \)) +GAME_SRCS := $(shell find src -type f \( -name '*.c' -o -name '*.S' \)) + +SDK_OBJS := $(patsubst $(SDK_DIR)%, $(BUILD)/sdk/%, $(SDK_SRCS)) +GAME_OBJS := $(patsubst %, $(BUILD)/game/%, $(GAME_SRCS)) + +OBJS := $(SDK_OBJS:.c=.o) +OBJS := $(OBJS:.S=.o) +GAME_OBJS := $(GAME_OBJS:.c=.o) +GAME_OBJS := $(GAME_OBJS:.S=.o) +OBJS += $(GAME_OBJS) -OBJS := $(BUILD)/ta_runtime.o $(addprefix $(BUILD)/,$(SRC_FILES:.c=.o)) DEPS := $(OBJS:.o=.d) all: $(BUILD)/$(TARGET).tab @@ -30,19 +39,19 @@ $(BUILD)/$(TARGET).tab: $(BUILD)/$(TARGET).elf $(BUILD)/$(TARGET).elf: $(OBJS) $(CC) $(LDFLAGS) $^ -o $@ -$(BUILD)/%.o: %.c +$(BUILD)/sdk/%.o: $(SDK_DIR)%.c @mkdir -p $(@D) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ -$(BUILD)/%.o: $(SDK_DIR)%.c +$(BUILD)/sdk/%.o: $(SDK_DIR)%.S @mkdir -p $(@D) - $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + $(CC) $(ASFLAGS) -MMD -MP -c $< -o $@ -$(BUILD)/%.o: %.S +$(BUILD)/game/%.o: %.c @mkdir -p $(@D) - $(CC) $(ASFLAGS) -MMD -MP -c $< -o $@ + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ -$(BUILD)/%.o: $(SDK_DIR)%.S +$(BUILD)/game/%.o: %.S @mkdir -p $(@D) $(CC) $(ASFLAGS) -MMD -MP -c $< -o $@ diff --git a/sdk/src/crt0.c b/sdk/src/crt0.c @@ -0,0 +1,21 @@ +#include <stdint.h> +#include <stddef.h> +#include <tuxanci2/abi.h> +#include <tuxanci2/syscall.h> + +extern int main(void); + +static inline void ta_ready(void (*stub)(uint32_t)) { + ta_syscall(TA_SYS_READY, (uint32_t)(size_t)stub, 0, 0, 0, 0, 0); +} + +static inline void ta_return(uint32_t value) { + ta_syscall(TA_SYS_RETURN, value, 0, 0, 0, 0, 0); + + while (1) {} +} + +__attribute__((section(".text.start"))) void _start(void) { + main(); + ta_ready(ta_return); +} diff --git a/sdk/src/stdlib.c b/sdk/src/stdlib.c diff --git a/sdk/src/string.c b/sdk/src/string.c @@ -0,0 +1,53 @@ +#include <stdint.h> +#include <stddef.h> + +void *memcpy(void *destination, const void *source, size_t count) { + uint8_t *destination_bytes = (uint8_t *)destination; + const uint8_t *source_bytes = (const uint8_t *)source; + + for (size_t i = 0; i < count; i++) { + destination_bytes[i] = source_bytes[i]; + } + + return destination; +} + +void *memmove(void *destination, const void *source, size_t count) { + uint8_t *destination_bytes = (uint8_t *)destination; + const uint8_t *source_bytes = (const uint8_t *)source; + + if (destination_bytes < source_bytes) { + for (size_t i = 0; i < count; i++) { + destination_bytes[i] = source_bytes[i]; + } + } else { + for (size_t i = count; i > 0; i--) { + destination_bytes[i - 1] = source_bytes[i - 1]; + } + } + + return destination; +} + +void *memset(void *destination, int value, size_t count) { + uint8_t *destination_bytes = (uint8_t *)destination; + + for (size_t i = 0; i < count; i++) { + destination_bytes[i] = (uint8_t)value; + } + + return destination; +} + +int memcmp(const void *first, const void *second, size_t count) { + const uint8_t *first_bytes = (const uint8_t *)first; + const uint8_t *second_bytes = (const uint8_t *)second; + + for (size_t i = 0; i < count; i++) { + if (first_bytes[i] != second_bytes[i]) { + return first_bytes[i] < second_bytes[i] ? -1 : 1; + } + } + + return 0; +} diff --git a/sdk/ta_config.h b/sdk/ta_config.h @@ -1,6 +0,0 @@ -#ifndef TA_CONFIG_H -#define TA_CONFIG_H - -#define TA_BITS 32 - -#endif // TA_CONFIG_H diff --git a/sdk/ta_runtime.c b/sdk/ta_runtime.c @@ -1,118 +0,0 @@ -#include "tuxanci2.h" - -extern void main(void); - -typedef __SIZE_TYPE__ ta_runtime_size; - -void *memcpy(void *destination, const void *source, ta_runtime_size count) { - ta_uint8 *destination_bytes = (ta_uint8 *)destination; - const ta_uint8 *source_bytes = (const ta_uint8 *)source; - - for (ta_runtime_size i = 0; i < count; i++) { - destination_bytes[i] = source_bytes[i]; - } - - return destination; -} - -void *memmove(void *destination, const void *source, ta_runtime_size count) { - ta_uint8 *destination_bytes = (ta_uint8 *)destination; - const ta_uint8 *source_bytes = (const ta_uint8 *)source; - - if (destination_bytes < source_bytes) { - for (ta_runtime_size i = 0; i < count; i++) { - destination_bytes[i] = source_bytes[i]; - } - } else { - for (ta_runtime_size i = count; i > 0; i--) { - destination_bytes[i - 1] = source_bytes[i - 1]; - } - } - - return destination; -} - -void *memset(void *destination, int value, ta_runtime_size count) { - ta_uint8 *destination_bytes = (ta_uint8 *)destination; - - for (ta_runtime_size i = 0; i < count; i++) { - destination_bytes[i] = (ta_uint8)value; - } - - return destination; -} - -int memcmp(const void *first, const void *second, ta_runtime_size count) { - const ta_uint8 *first_bytes = (const ta_uint8 *)first; - const ta_uint8 *second_bytes = (const ta_uint8 *)second; - - for (ta_runtime_size i = 0; i < count; i++) { - if (first_bytes[i] != second_bytes[i]) { - return first_bytes[i] < second_bytes[i] ? -1 : 1; - } - } - - return 0; -} - -static ta_uint64 divide_unsigned(ta_uint64 numerator, ta_uint64 denominator, ta_uint64 *remainder_out) { - if (denominator == 0) { - *remainder_out = numerator; - - return ~(ta_uint64)0; - } - - ta_uint64 quotient = 0; - ta_uint64 remainder = 0; - - for (ta_int32 bit = 63; bit >= 0; bit--) { - ta_uint64 carry = remainder >> 63; - - remainder = (remainder << 1) | ((numerator >> bit) & 1); - - if (carry != 0 || remainder >= denominator) { - remainder -= denominator; - quotient |= (ta_uint64)1 << bit; - } - } - - *remainder_out = remainder; - - return quotient; -} - -ta_uint64 __udivdi3(ta_uint64 numerator, ta_uint64 denominator) { - ta_uint64 remainder; - - return divide_unsigned(numerator, denominator, &remainder); -} - -ta_uint64 __umoddi3(ta_uint64 numerator, ta_uint64 denominator) { - ta_uint64 remainder; - - divide_unsigned(numerator, denominator, &remainder); - - return remainder; -} - -ta_int64 __divdi3(ta_int64 numerator, ta_int64 denominator) { - ta_bool negative = (numerator < 0) != (denominator < 0); - ta_uint64 numerator_magnitude = numerator < 0 ? -(ta_uint64)numerator : (ta_uint64)numerator; - ta_uint64 denominator_magnitude = denominator < 0 ? -(ta_uint64)denominator : (ta_uint64)denominator; - ta_uint64 quotient = __udivdi3(numerator_magnitude, denominator_magnitude); - - return negative ? (ta_int64)(-quotient) : (ta_int64)quotient; -} - -ta_int64 __moddi3(ta_int64 numerator, ta_int64 denominator) { - ta_uint64 numerator_magnitude = numerator < 0 ? -(ta_uint64)numerator : (ta_uint64)numerator; - ta_uint64 denominator_magnitude = denominator < 0 ? -(ta_uint64)denominator : (ta_uint64)denominator; - ta_uint64 remainder = __umoddi3(numerator_magnitude, denominator_magnitude); - - return numerator < 0 ? (ta_int64)(-remainder) : (ta_int64)remainder; -} - -__attribute__((section(".text.start"))) void _start(void) { - main(); - ta_ready(ta_return); -} diff --git a/sdk/tuxanci2.h b/sdk/tuxanci2.h @@ -1,102 +0,0 @@ -#ifndef TUXANCI2_H -#define TUXANCI2_H - -#include "ta_abi.h" -#include "ta_keys.h" -#include "ta_types.h" - -#define TA_RGB(red, green, blue) ((((ta_uint32)(red) & 255u) << 16) | (((ta_uint32)(green) & 255u) << 8) | ((ta_uint32)(blue) & 255u)) - -typedef ta_uint32 ta_text_handle; - -typedef void (*ta_screen_init_function)(void); -typedef void (*ta_screen_update_function)(void); -typedef void (*ta_screen_destroy_function)(void); - -static inline ta_uint32 ta_syscall(ta_uint32 number, ta_uint32 arg0, ta_uint32 arg1, ta_uint32 arg2, ta_uint32 arg3, ta_uint32 arg4, ta_uint32 arg5) { - register ta_uint32 reg0 __asm__("a0") = arg0; - register ta_uint32 reg1 __asm__("a1") = arg1; - register ta_uint32 reg2 __asm__("a2") = arg2; - register ta_uint32 reg3 __asm__("a3") = arg3; - register ta_uint32 reg4 __asm__("a4") = arg4; - register ta_uint32 reg5 __asm__("a5") = arg5; - register ta_uint32 reg7 __asm__("a7") = number; - - __asm__ volatile("ecall" : "+r"(reg0) : "r"(reg1), "r"(reg2), "r"(reg3), "r"(reg4), "r"(reg5), "r"(reg7) : "memory"); - - return reg0; -} - -static inline void ta_exit(ta_int32 code) { - ta_syscall(TA_SYS_EXIT, (ta_uint32)code, 0, 0, 0, 0, 0); - - while (1) {} -} - -static inline void ta_ready(void (*stub)(ta_uint32)) { - ta_syscall(TA_SYS_READY, (ta_uint32)(ta_size)stub, 0, 0, 0, 0, 0); -} - -static inline void ta_return(ta_uint32 value) { - ta_syscall(TA_SYS_RETURN, value, 0, 0, 0, 0, 0); - - while (1) {} -} - -static inline ta_uint32 ta_display_width(void) { - return ta_syscall(TA_SYS_DISPLAY_WIDTH, 0, 0, 0, 0, 0, 0); -} - -static inline ta_uint32 ta_display_height(void) { - return ta_syscall(TA_SYS_DISPLAY_HEIGHT, 0, 0, 0, 0, 0, 0); -} - -static inline ta_bool ta_display_sprite_mode(void) { - return ta_syscall(TA_SYS_DISPLAY_SPRITE_MODE, 0, 0, 0, 0, 0, 0) != 0; -} - -static inline ta_bool ta_keyboard_is_down(ta_keyboard_keys key) { - return ta_syscall(TA_SYS_KEYBOARD_IS_DOWN, (ta_uint32)key, 0, 0, 0, 0, 0) != 0; -} - -static inline ta_bool ta_image_init(ta_uint32 asset) { - return ta_syscall(TA_SYS_IMAGE_INIT, asset, 0, 0, 0, 0, 0) != 0; -} - -static inline ta_bool ta_image_draw(ta_uint32 asset, ta_fixed x, ta_fixed y, ta_fixed width, ta_fixed height, ta_uint16 frame) { - return ta_syscall(TA_SYS_IMAGE_DRAW, asset, x, y, width, height, frame) != 0; -} - -static inline ta_bool ta_image_get_size(ta_uint32 asset, ta_uint32 *width, ta_uint32 *height) { - return ta_syscall(TA_SYS_IMAGE_GET_SIZE, asset, (ta_uint32)(ta_size)width, (ta_uint32)(ta_size)height, 0, 0, 0) != 0; -} - -static inline ta_text_handle ta_text_create(const char *text, ta_uint32 size, ta_uint32 color, ta_uint32 border_width, ta_uint32 border_color) { - return ta_syscall(TA_SYS_TEXT_CREATE, (ta_uint32)(ta_size)text, size, color, border_width, border_color, 0); -} - -static inline ta_uint32 ta_text_width(ta_text_handle text) { - return ta_syscall(TA_SYS_TEXT_WIDTH, text, 0, 0, 0, 0, 0); -} - -static inline ta_uint32 ta_text_height(ta_text_handle text) { - return ta_syscall(TA_SYS_TEXT_HEIGHT, text, 0, 0, 0, 0, 0); -} - -static inline void ta_text_draw(ta_text_handle text, ta_uint32 x, ta_uint32 y) { - ta_syscall(TA_SYS_TEXT_DRAW, text, x, y, 0, 0, 0); -} - -static inline void ta_text_destroy(ta_text_handle text) { - ta_syscall(TA_SYS_TEXT_DESTROY, text, 0, 0, 0, 0, 0); -} - -static inline ta_bool ta_screen_register(ta_uint32 id, ta_screen_init_function init, ta_screen_update_function update, ta_screen_destroy_function destroy) { - return ta_syscall(TA_SYS_SCREEN_REGISTER, id, (ta_uint32)(ta_size)init, (ta_uint32)(ta_size)update, (ta_uint32)(ta_size)destroy, 0, 0) != 0; -} - -static inline ta_bool ta_screen_switch(ta_uint32 id) { - return ta_syscall(TA_SYS_SCREEN_SWITCH, id, 0, 0, 0, 0, 0) != 0; -} - -#endif // TUXANCI2_H