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 %.o: %.c
$(CC) $^ -o $@ $(CFLAGS) $(CC) $^ -o $@ $(CFLAGS)
all: $(SRCS:.c=.o) bin: $(SRCS:.c=.o)
$(CC) $(LDFLAGS) -o bin $^ $(CC) $(LDFLAGS) -o bin $^
all: bin
clean: clean:
$(RM) $(SRCS:.c=.o) bin $(RM) $(SRCS:.c=.o) bin

98
boot.c
View File

@ -1,4 +1,6 @@
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include "hedley.h"
#define BOARD ((Cell*)0x7F00) #define BOARD ((Cell*)0x7F00)
@ -8,55 +10,34 @@ typedef enum : char {
NOT = 'O' NOT = 'O'
} Cell; } Cell;
inline void putchar(char); HEDLEY_ALWAYS_INLINE char readchar(void);
inline char readchar(void); HEDLEY_ALWAYS_INLINE bool checkwin(Cell c);
inline void printboard(void); HEDLEY_NEVER_INLINE void putchar(char);
inline bool checkwin(Cell c); HEDLEY_NEVER_INLINE void printboard(void);
char numpadkeys[];
void _start(void) { void _start(void) {
Cell cur = CROSS; Cell cur = CROSS;
for (char i = 0; i < 3; i++) for (char i = 0; i < 3; i++)
for (char j = 0; j < 3; j++) for (char j = 0; j < 3; j++)
*(BOARD + i * 3 + j) = NONE; BOARD[i * 3 + j] = NONE;
while (1) { while (1) {
printboard(); printboard();
char c; char k = readchar();
switch (readchar()) { char o;
case 0x47: // 7 if (k > 0x46)
c = 0; o = 0x47;
break; else if (k < 0x13)
case 0x48: // 8 o = 0x10;
c = 1; else if (k < 0x21)
break; o = (0x1E - 4);
case 0x49: // 9 else
c = 2; o = (0x2C - 8);
break; size_t c = numpadkeys[k - o];
if (BOARD[c] != NONE)
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:
continue; continue;
} BOARD[c] = cur;
if (*(BOARD + c) != NONE)
continue;
*(BOARD + c) = cur;
if (checkwin(cur)) if (checkwin(cur))
break; break;
if (cur == CROSS) if (cur == CROSS)
@ -68,9 +49,14 @@ void _start(void) {
printboard(); printboard();
while(1) 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 ( __asm__ volatile (
"movb $0x02, %%ah;" "movb $0x02, %%ah;"
"movb $0x00, %%bh;" "movb $0x00, %%bh;"
@ -82,28 +68,35 @@ inline void printboard(void) {
); );
for (char i = 0; i < 3; i++) { for (char i = 0; i < 3; i++) {
for (char 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');
putchar('\r'); putchar('\r');
} }
} }
inline bool checkwin(Cell c) { HEDLEY_ALWAYS_INLINE
for (char i = 0; i < 3; i++) bool checkwin(Cell c) {
if ((*(BOARD + i) == c) && (*(BOARD + i + 3) == c) && (*(BOARD + i + 6) == c)) for (size_t i = 0; i <= 2; i++)
if ((BOARD[i] == c) && (BOARD[i + 3] == c) && (BOARD[i + 6] == c))
return true; return true;
for (char i = 0; i < 3; i++) for (size_t i = 0; i <= 2; i++)
if ((*(BOARD + 3*i) == c) && (*(BOARD + 3*i + 1) == c) && (*(BOARD + 3*i + 2) == c)) if ((BOARD[3*i] == c) && (BOARD[3*i + 1] == c) && (BOARD[3*i + 2] == c))
return true; return true;
if (*(BOARD) == c && *(BOARD + 4) == c && *(BOARD + 8)) if (BOARD[0] == c && BOARD[4] == c && BOARD[8] == c)
return true; return true;
if (*(BOARD + 2) == c && *(BOARD + 4) == c && *(BOARD + 7)) if (BOARD[2] == c && BOARD[4] == c && BOARD[6] == c)
return true; return true;
return false; return false;
} }
/*
* 012
* 345
* 678
*/
inline char readchar(void) { HEDLEY_ALWAYS_INLINE
char readchar(void) {
char out; char out;
__asm__ volatile ( __asm__ volatile (
"movb $0x00, %%ah;" "movb $0x00, %%ah;"
@ -116,7 +109,8 @@ __asm__ volatile (
return out; return out;
} }
inline void putchar(char out) { HEDLEY_NEVER_INLINE
void putchar(char out) {
__asm__ volatile ( __asm__ volatile (
"movb $0x0e, %%ah;" "movb $0x0e, %%ah;"
"movb %0, %%al;" "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) /DISCARD/ : { *(.eh_frame)
*(.comment) *(.comment)
*(.note.GNU-stack) *(.note.GNU-stack)
*(.note.gnu.build-id)
} }
} }