Compare commits

..

No commits in common. "60516831b95b8dc7434895e1effa807ba0d7120a" and "60d8f9bc318688c8d177aa0500743c8356830e78" have entirely different histories.

2 changed files with 33 additions and 25 deletions

View File

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