An update.

This commit is contained in:
bʰedoh₂ swé 2024-10-20 16:36:21 +05:00
parent d8cbe10ab6
commit 0204b938de
4 changed files with 2092 additions and 53 deletions

View File

@ -5,8 +5,10 @@ CFLAGS = -flto -ffreestanding -fno-builtin -fno-pie -Wall -std=c2x -m16 -nostdli
%.o: %.c
$(CC) $^ -o $@ $(CFLAGS)
all: $(SRCS:.c=.o)
bin: $(SRCS:.c=.o)
$(CC) $(LDFLAGS) -o bin $^
all: bin
clean:
$(RM) $(SRCS:.c=.o) bin

98
boot.c
View File

@ -1,4 +1,6 @@
#include <stdlib.h>
#include <stdbool.h>
#include "hedley.h"
#define BOARD ((Cell*)0x7F00)
@ -8,55 +10,34 @@ typedef enum : char {
NOT = 'O'
} Cell;
inline void putchar(char);
inline char readchar(void);
inline void printboard(void);
inline bool checkwin(Cell c);
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[];
void _start(void) {
Cell cur = CROSS;
for (char i = 0; i < 3; i++)
for (char j = 0; j < 3; j++)
*(BOARD + i * 3 + j) = NONE;
BOARD[i * 3 + j] = NONE;
while (1) {
printboard();
char c;
switch (readchar()) {
case 0x47: // 7
c = 0;
break;
case 0x48: // 8
c = 1;
break;
case 0x49: // 9
c = 2;
break;
case 0x4B: // 4
c = 3;
break;
case 0x4C: // 5
c = 4;
break;
case 0x4D: // 6
c = 5;
break;
case 0x4F: // 1
c = 6;
break;
case 0x50: // 2
c = 7;
break;
case 0x51: // 3
c = 8;
break;
default:
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)
continue;
}
if (*(BOARD + c) != NONE)
continue;
*(BOARD + c) = cur;
BOARD[c] = cur;
if (checkwin(cur))
break;
if (cur == CROSS)
@ -68,9 +49,14 @@ void _start(void) {
printboard();
while(1)
;
HEDLEY_UNREACHABLE();
}
// numpadkeys[x - 0x47]
char numpadkeys[] = {0, 1, 2, false, 3, 4, 5, false, 6, 7, 8};
inline void printboard(void) {
HEDLEY_NEVER_INLINE
void printboard(void) {
__asm__ volatile (
"movb $0x02, %%ah;"
"movb $0x00, %%bh;"
@ -82,28 +68,35 @@ inline void printboard(void) {
);
for (char i = 0; i < 3; i++) {
for (char j = 0; j < 3; j++) {
putchar(*(BOARD + i * 3 + j));
putchar(BOARD[i * 3 + j]);
}
putchar('\n');
putchar('\r');
}
}
inline bool checkwin(Cell c) {
for (char i = 0; i < 3; i++)
if ((*(BOARD + i) == c) && (*(BOARD + i + 3) == c) && (*(BOARD + i + 6) == c))
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))
return true;
for (char i = 0; i < 3; i++)
if ((*(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;
if (*(BOARD) == c && *(BOARD + 4) == c && *(BOARD + 8))
if (BOARD[0] == c && BOARD[4] == c && BOARD[8] == c)
return true;
if (*(BOARD + 2) == c && *(BOARD + 4) == c && *(BOARD + 7))
if (BOARD[2] == c && BOARD[4] == c && BOARD[6] == c)
return true;
return false;
}
/*
* 012
* 345
* 678
*/
inline char readchar(void) {
HEDLEY_ALWAYS_INLINE
char readchar(void) {
char out;
__asm__ volatile (
"movb $0x00, %%ah;"
@ -116,7 +109,8 @@ __asm__ volatile (
return out;
}
inline void putchar(char out) {
HEDLEY_NEVER_INLINE
void putchar(char out) {
__asm__ volatile (
"movb $0x0e, %%ah;"
"movb %0, %%al;"

2042
hedley.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -23,5 +23,6 @@ SECTIONS
/DISCARD/ : { *(.eh_frame)
*(.comment)
*(.note.GNU-stack)
*(.note.gnu.build-id)
}
}