From a861db43261d6ea3e7d56c3c563e093d44ee2a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Sun, 20 Oct 2024 02:10:44 +0500 Subject: [PATCH] Initial commit. --- .gitignore | 2 + LICENSE | 5 +++ Makefile | 12 +++++ README | 2 + boot.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++ linker.ld | 27 +++++++++++ 6 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README create mode 100644 boot.c create mode 100644 linker.ld diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc06923 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin +*.o diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..50bb52d --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8fd1184 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README b/README new file mode 100644 index 0000000..01a4be6 --- /dev/null +++ b/README @@ -0,0 +1,2 @@ +MBR boot sector Hello world written in C. +Note: this thing does not work with GCC. diff --git a/boot.c b/boot.c new file mode 100644 index 0000000..17e3054 --- /dev/null +++ b/boot.c @@ -0,0 +1,128 @@ +#include + +#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" +); +} diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..c23f8b9 --- /dev/null +++ b/linker.ld @@ -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) + } +}