Merge pull request 'i1-console-output' (#8) from i1-console-output into main

Reviewed-on: #8
This commit is contained in:
Mineplay 2025-10-17 15:28:26 -05:00
commit 15a1824290
5 changed files with 218 additions and 2 deletions

View file

@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
* File: Console.h
* Description:
* This file contains the functions for reading and writting to the console.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef IONY_CONSOLE
#define IONY_CONSOLE
#include "../Utils/Error.h"
IonyError iony_print(const char *text, long length);
#endif

View file

@ -0,0 +1,32 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
* File: Error.h
* Description:
* This file contains the ErrorCode enum that defines the error codes for the
* library.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef IONY_ERROR
#define IONY_ERROR
typedef enum {
IONY_ERROR_NONE = 0,
IONY_ERROR_INVALID_POINTER = 1,
IONY_ERROR_SYSTEM_CALL_FAILED = 2
} IonyError;
#endif

View file

@ -0,0 +1,99 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
* File: System.h
* Description:
* This file contains specific system helper functions and constants for linux
* and windows.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef IONY_SYSTEM
#define IONY_SYSTEM
#if defined(__GNUC__)
#if defined(__x86_64__)
#define SYS_WRITE 1
#elif defined(__i386__)
#define SYS_WRITE 4
#elif defined(__aarch64__)
#define SYS_WRITE 64
#elif defined(__arm__)
#define SYS_WRITE 4
#endif
static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) {
long result = 0;
#if defined(__x86_64__)
__asm__ volatile(
"movq %1, %%rax\n"
"movq %2, %%rdi\n"
"movq %3, %%rsi\n"
"movq %4, %%rdx\n"
"movq %5, %%r10\n"
"movq %6, %%r8\n"
"movq %7, %%r9\n"
"syscall\n"
"movq %%rax, %0\n"
: "=r"(result)
: "r"(number), "r"(argument1), "r"(argument2), "r"(argument3), "r"(argument4), "r"(argument5), "r"(argument6)
: "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "memory"
);
#elif defined(__i386__)
__asm__ volatile(
"movl %1, %%eax\n"
"movl %2, %%ebx\n"
"movl %3, %%ecx\n"
"movl %4, %%edx\n"
"movl %5, %%esi\n"
"movl %6, %%edi\n"
"movl %7, %%ebp\n"
"int $0x80\n"
"movl %%eax, %0\n"
: "=r"(result)
: "r"(number), "r"(argument1), "r"(argument2), "r"(argument3),"r"(argument4), "r"(argument5), "r"(argument6)
: "eax","ebx","ecx","edx","esi","edi","ebp","memory"
);
#elif defined(__aarch64__)
register long x0 asm("x0") = argument1;
register long x1 asm("x1") = argument2;
register long x2 asm("x2") = argument3;
register long x3 asm("x3") = argument4;
register long x4 asm("x4") = argument5;
register long x5 asm("x5") = argument6;
register long x8 asm("x8") = number;
__asm__ volatile("svc 0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8) : "memory");
result = x0;
#elif defined(__arm__)
register long r0 asm("r0") = argument1;
register long r1 asm("r1") = argument2;
register long r2 asm("r2") = argument3;
register long r3 asm("r3") = argument4;
register long r4 asm("r4") = argument5;
register long r5 asm("r5") = argument6;
register long r7 asm("r7") = number;
__asm__ volatile("swi 0" : "+r"(r0) : "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5), "r"(r7) : "memory");
result = r0;
#else
#error "Unsupported architecture"
#endif
return result;
}
#endif
#endif

52
Src/Core/Console.c Normal file
View file

@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
* File: Console.c
* Description:
* This file contains the functions for reading and writting to the console.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Iony/Core/Console.h"
#if defined(_WIN32)
#include <windows.h>
#elif defined(__GNUC__)
#include "../../Include/Iony/Utils/System.h"
#endif
IonyError iony_print(const char *text, const long length) {
if (text == 0) {
return IONY_ERROR_INVALID_POINTER;
}
#if defined(_WIN32)
static HANDLE standard_console_out_handle = 0;
if (standard_console_out_handle == 0) {
standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
}
DWORD characters_written;
if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#elif defined(__GNUC__)
if (iony_system_call(SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#endif
return IONY_ERROR_NONE;
}

View file

@ -19,9 +19,13 @@
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <Iony/Utils/Error.h>
#include <Iony/Core/Console.h>
int main(void) {
printf("Hello, World!\n");
if (iony_print("Hello, World!\n", 15) != IONY_ERROR_NONE) {
return -1;
}
return 0;
}