Initial commit.

This commit is contained in:
bʰedoh₂ swé 2024-10-20 02:10:44 +05:00
commit a861db4326
6 changed files with 176 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin
*.o

5
LICENSE Normal file
View File

@ -0,0 +1,5 @@
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
SRCS = boot.c
LDFLAGS = -flto -T linker.ld -nostdlib -Os
CFLAGS = -flto -ffreestanding -fno-builtin -fno-pie -Wall -std=c2x -m16 -nostdlib -Os -nostartfiles -r
%.o: %.c
$(CC) $^ -o $@ $(CFLAGS)
all: $(SRCS:.c=.o)
$(CC) $(LDFLAGS) -o bin $^
clean:
$(RM) $(SRCS:.c=.o) bin

2
README Normal file
View File

@ -0,0 +1,2 @@
MBR boot sector Hello world written in C.
Note: this thing does not work with GCC.

128
boot.c Normal file
View File

@ -0,0 +1,128 @@
#include <stdbool.h>
#define BOARD (Cell*)0x7F00
typedef enum : char {
NONE = ' ',
CROSS = 'X',
NOT = 'O'
} Cell;
inline void putchar(char);
inline char readchar(void);
inline void printboard(void);
inline bool checkwin(Cell c);
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;
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:
continue;
}
if (*(BOARD + c) != NONE)
continue;
*(BOARD + c) = cur;
if (checkwin(cur))
break;
if (cur == CROSS)
cur = NOT;
else
cur = CROSS;
}
putchar(cur);
printboard();
while(1)
;
}
inline void printboard(void) {
__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++) {
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))
return true;
for (char i = 0; i < 3; 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))
return true;
if (*(BOARD + 2) == c && *(BOARD + 4) == c && *(BOARD + 7))
return true;
return false;
}
inline char readchar(void) {
char out;
__asm__ volatile (
"movb $0x00, %%ah;"
"int $0x16;"
"movb %%ah, %0;"
:"=r"(out)
:
:"%ax"
);
return out;
}
inline void putchar(char out) {
__asm__ volatile (
"movb $0x0e, %%ah;"
"movb %0, %%al;"
"int $0x10;"
:
:"r"(out)
:"%ax"
);
}

27
linker.ld Normal file
View File

@ -0,0 +1,27 @@
ENTRY (_start)
OUTPUT_FORMAT (binary)
OUTPUT_ARCH(i386)
SECTIONS
{
. = 0x7c00;
.text : {
*(.text)
}
.rodata : { *(.rodata)
}
.data : { *(.data)
}
. = 0x7DFE;
.bootsig : {
SHORT(0xaa55);
}
.bss : { *(.bss)
}
. = ALIGN (0x200);
lit = .;
/DISCARD/ : { *(.eh_frame)
*(.comment)
*(.note.GNU-stack)
}
}