2024-10-20 11:36:21 +00:00
|
|
|
#include <stdlib.h>
|
2024-10-19 21:10:44 +00:00
|
|
|
#include <stdbool.h>
|
2024-10-20 11:36:21 +00:00
|
|
|
#include "hedley.h"
|
2024-10-19 21:10:44 +00:00
|
|
|
|
2024-10-20 10:14:48 +00:00
|
|
|
#define BOARD ((Cell*)0x7F00)
|
2024-10-19 21:10:44 +00:00
|
|
|
|
|
|
|
typedef enum : char {
|
|
|
|
NONE = ' ',
|
|
|
|
CROSS = 'X',
|
|
|
|
NOT = 'O'
|
|
|
|
} Cell;
|
|
|
|
|
2024-10-20 11:36:21 +00:00
|
|
|
HEDLEY_ALWAYS_INLINE char readchar(void);
|
|
|
|
HEDLEY_ALWAYS_INLINE bool checkwin(Cell c);
|
|
|
|
HEDLEY_NEVER_INLINE void putchar(char);
|
|
|
|
HEDLEY_NEVER_INLINE void printboard(void);
|
|
|
|
|
|
|
|
char numpadkeys[];
|
2024-10-19 21:10:44 +00:00
|
|
|
|
|
|
|
void _start(void) {
|
2024-10-20 12:48:37 +00:00
|
|
|
__asm__ volatile (
|
|
|
|
"movl $0x8000, %esp\n"
|
|
|
|
"ljmp $(0),$(start)\n"
|
|
|
|
"start:\n"
|
|
|
|
);
|
2024-10-19 21:10:44 +00:00
|
|
|
Cell cur = CROSS;
|
|
|
|
for (char i = 0; i < 3; i++)
|
|
|
|
for (char j = 0; j < 3; j++)
|
2024-10-20 11:36:21 +00:00
|
|
|
BOARD[i * 3 + j] = NONE;
|
2024-10-19 21:10:44 +00:00
|
|
|
while (1) {
|
|
|
|
printboard();
|
2024-10-20 11:36:21 +00:00
|
|
|
char k = readchar();
|
|
|
|
char o;
|
|
|
|
if (k > 0x46)
|
|
|
|
o = 0x47;
|
|
|
|
else if (k < 0x13)
|
|
|
|
o = 0x10;
|
|
|
|
else if (k < 0x21)
|
|
|
|
o = (0x1E - 4);
|
|
|
|
else
|
|
|
|
o = (0x2C - 8);
|
|
|
|
size_t c = numpadkeys[k - o];
|
|
|
|
if (BOARD[c] != NONE)
|
2024-10-19 21:10:44 +00:00
|
|
|
continue;
|
2024-10-20 11:36:21 +00:00
|
|
|
BOARD[c] = cur;
|
2024-10-19 21:10:44 +00:00
|
|
|
if (checkwin(cur))
|
|
|
|
break;
|
|
|
|
if (cur == CROSS)
|
|
|
|
cur = NOT;
|
|
|
|
else
|
|
|
|
cur = CROSS;
|
|
|
|
}
|
|
|
|
putchar(cur);
|
|
|
|
printboard();
|
|
|
|
while(1)
|
|
|
|
;
|
2024-10-20 11:36:21 +00:00
|
|
|
HEDLEY_UNREACHABLE();
|
2024-10-19 21:10:44 +00:00
|
|
|
}
|
2024-10-20 11:36:21 +00:00
|
|
|
// numpadkeys[x - 0x47]
|
|
|
|
char numpadkeys[] = {0, 1, 2, false, 3, 4, 5, false, 6, 7, 8};
|
|
|
|
|
2024-10-19 21:10:44 +00:00
|
|
|
|
2024-10-20 11:36:21 +00:00
|
|
|
HEDLEY_NEVER_INLINE
|
|
|
|
void printboard(void) {
|
2024-10-19 21:10:44 +00:00
|
|
|
__asm__ volatile (
|
|
|
|
"movb $0x02, %%ah;"
|
|
|
|
"movb $0x00, %%bh;"
|
|
|
|
"movw $0x0000, %%dx;"
|
|
|
|
"int $0x10;"
|
|
|
|
:
|
|
|
|
:
|
|
|
|
:"%ah","%bh","%dx"
|
|
|
|
);
|
|
|
|
for (char i = 0; i < 3; i++) {
|
|
|
|
for (char j = 0; j < 3; j++) {
|
2024-10-20 11:36:21 +00:00
|
|
|
putchar(BOARD[i * 3 + j]);
|
2024-10-19 21:10:44 +00:00
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
putchar('\r');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 11:36:21 +00:00
|
|
|
HEDLEY_ALWAYS_INLINE
|
|
|
|
bool checkwin(Cell c) {
|
|
|
|
for (size_t i = 0; i <= 2; i++)
|
|
|
|
if ((BOARD[i] == c) && (BOARD[i + 3] == c) && (BOARD[i + 6] == c))
|
2024-10-19 21:10:44 +00:00
|
|
|
return true;
|
2024-10-20 11:36:21 +00:00
|
|
|
for (size_t i = 0; i <= 2; i++)
|
|
|
|
if ((BOARD[3*i] == c) && (BOARD[3*i + 1] == c) && (BOARD[3*i + 2] == c))
|
2024-10-19 21:10:44 +00:00
|
|
|
return true;
|
2024-10-20 11:36:21 +00:00
|
|
|
if (BOARD[0] == c && BOARD[4] == c && BOARD[8] == c)
|
2024-10-19 21:10:44 +00:00
|
|
|
return true;
|
2024-10-20 11:36:21 +00:00
|
|
|
if (BOARD[2] == c && BOARD[4] == c && BOARD[6] == c)
|
2024-10-19 21:10:44 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-20 11:36:21 +00:00
|
|
|
/*
|
|
|
|
* 012
|
|
|
|
* 345
|
|
|
|
* 678
|
|
|
|
*/
|
2024-10-19 21:10:44 +00:00
|
|
|
|
2024-10-20 11:36:21 +00:00
|
|
|
HEDLEY_ALWAYS_INLINE
|
|
|
|
char readchar(void) {
|
2024-10-19 21:10:44 +00:00
|
|
|
char out;
|
|
|
|
__asm__ volatile (
|
|
|
|
"movb $0x00, %%ah;"
|
|
|
|
"int $0x16;"
|
|
|
|
"movb %%ah, %0;"
|
|
|
|
:"=r"(out)
|
|
|
|
:
|
|
|
|
:"%ax"
|
|
|
|
);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-10-20 11:36:21 +00:00
|
|
|
HEDLEY_NEVER_INLINE
|
|
|
|
void putchar(char out) {
|
2024-10-19 21:10:44 +00:00
|
|
|
__asm__ volatile (
|
|
|
|
"movb $0x0e, %%ah;"
|
|
|
|
"movb %0, %%al;"
|
|
|
|
"int $0x10;"
|
|
|
|
:
|
|
|
|
:"r"(out)
|
|
|
|
:"%ax"
|
|
|
|
);
|
|
|
|
}
|