Compare commits

...

3 Commits

Author SHA1 Message Date
60516831b9 Some changes. 2025-05-25 17:49:39 +05:00
e1e670abdf Some optimizations. 2025-05-25 16:59:44 +05:00
b569aa3984 License update. 2025-05-25 16:59:34 +05:00
2 changed files with 25 additions and 33 deletions

View File

@ -1,4 +1,4 @@
Copyright (C) 2024 by bedohswe bedohswe@firemail.cc Copyright (C) 2024-2025 by bedohswe bedohswe@firemail.cc
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

56
boot.c
View File

@ -2,20 +2,22 @@
#include <stdbool.h> #include <stdbool.h>
#include "hedley.h" #include "hedley.h"
typedef enum : char { typedef unsigned char uchar;
typedef enum : uchar {
NONE = ' ', NONE = ' ',
CROSS = 'X', CROSS = 'X',
NOT = 'O' NOT = 'O'
} Cell; } Cell;
Cell* board = ((Cell*)0x7F00); static Cell* const board = (Cell*)0x7F00;
HEDLEY_ALWAYS_INLINE char readchar(void); static uchar readchar(void);
HEDLEY_ALWAYS_INLINE bool checkwin(Cell c); static bool checkwin(Cell c);
HEDLEY_NEVER_INLINE void putchar(char); static void putchar(uchar);
HEDLEY_NEVER_INLINE void printboard(void); static void printboard(void);
char numpadkeys[]; static uchar numpadkeys[11];
void _start(void) { void _start(void) {
__asm__ volatile ( __asm__ volatile (
@ -24,13 +26,12 @@ void _start(void) {
"start:\n" "start:\n"
); );
Cell cur = CROSS; Cell cur = CROSS;
for (char i = 0; i < 3; i++) for (uchar i = 0; i < 9; i++)
for (char j = 0; j < 3; j++) board[i] = NONE;
board[i * 3 + j] = NONE;
while (1) { while (1) {
printboard(); printboard();
char k = readchar(); uchar k = readchar();
char o; uchar o;
if (k > 0x46) if (k > 0x46)
o = 0x47; o = 0x47;
else if (k < 0x13) else if (k < 0x13)
@ -39,16 +40,13 @@ void _start(void) {
o = (0x1E - 4); o = (0x1E - 4);
else else
o = (0x2C - 8); o = (0x2C - 8);
size_t c = numpadkeys[k - o]; uchar c = numpadkeys[k - o];
if (board[c] != NONE) if (board[c] != NONE)
continue; continue;
board[c] = cur; board[c] = cur;
if (checkwin(cur)) if (checkwin(cur))
break; break;
if (cur == CROSS) cur ^= (CROSS ^ NOT);
cur = NOT;
else
cur = CROSS;
} }
putchar(cur); putchar(cur);
printboard(); printboard();
@ -57,11 +55,10 @@ void _start(void) {
HEDLEY_UNREACHABLE(); HEDLEY_UNREACHABLE();
} }
// numpadkeys[x - 0x47] // numpadkeys[x - 0x47]
char numpadkeys[] = {0, 1, 2, false, 3, 4, 5, false, 6, 7, 8}; static uchar numpadkeys[11] = {0, 1, 2, false, 3, 4, 5, false, 6, 7, 8};
HEDLEY_NEVER_INLINE static void printboard(void) {
void printboard(void) {
__asm__ volatile ( __asm__ volatile (
"movb $0x02, %%ah;" "movb $0x02, %%ah;"
"movb $0x00, %%bh;" "movb $0x00, %%bh;"
@ -71,8 +68,8 @@ void printboard(void) {
: :
:"%ah","%bh","%dx" :"%ah","%bh","%dx"
); );
for (char i = 0; i < 3; i++) { for (uchar i = 0; i < 3; i++) {
for (char j = 0; j < 3; j++) { for (uchar j = 0; j < 3; j++) {
putchar(board[i * 3 + j]); putchar(board[i * 3 + j]);
} }
putchar('\n'); putchar('\n');
@ -80,13 +77,10 @@ void printboard(void) {
} }
} }
HEDLEY_ALWAYS_INLINE static bool checkwin(Cell c) {
bool checkwin(Cell c) {
for (size_t i = 0; i <= 2; i++) for (size_t i = 0; i <= 2; i++)
if ((board[i] == c) && (board[i + 3] == c) && (board[i + 6] == c)) if (((board[i] == c) && (board[i + 3] == c) && (board[i + 6] == c)) &&
return true; ((board[3*i] == c) && (board[3*i + 1] == c) && (board[3*i + 2] == c)))
for (size_t i = 0; i <= 2; i++)
if ((board[3*i] == c) && (board[3*i + 1] == c) && (board[3*i + 2] == c))
return true; return true;
if (board[0] == c && board[4] == c && board[8] == c) if (board[0] == c && board[4] == c && board[8] == c)
return true; return true;
@ -100,8 +94,7 @@ bool checkwin(Cell c) {
* 678 * 678
*/ */
HEDLEY_ALWAYS_INLINE static uchar readchar(void) {
char readchar(void) {
char out; char out;
__asm__ volatile ( __asm__ volatile (
"movb $0x00, %%ah;" "movb $0x00, %%ah;"
@ -114,8 +107,7 @@ __asm__ volatile (
return out; return out;
} }
HEDLEY_NEVER_INLINE static void putchar(uchar out) {
void putchar(char out) {
__asm__ volatile ( __asm__ volatile (
"movb $0x0e, %%ah;" "movb $0x0e, %%ah;"
"movb %0, %%al;" "movb %0, %%al;"