clang-format -i *.cc *.c *.h

This commit is contained in:
2025-09-29 18:54:48 +02:00
parent f3057e92be
commit f1b2902640
8 changed files with 194 additions and 249 deletions

View File

@@ -19,8 +19,7 @@ uint16_t crc16(int data_len) {
return crc; return crc;
} }
asm ( asm(".global main \n"
".global main \n"
"main: \n" "main: \n"
" push %bp \n" " push %bp \n"
" mov %sp, %bp \n" " mov %sp, %bp \n"
@@ -31,5 +30,4 @@ asm (
" mov 6(%bp), %di \n" " mov 6(%bp), %di \n"
" mov %ax, (%di) \n" " mov %ax, (%di) \n"
" pop %bp \n" " pop %bp \n"
" lret $4 \n" " lret $4 \n");
);

6
crt0.c
View File

@@ -3,8 +3,7 @@
int main(); int main();
asm ( asm(".section .init \n"
".section .init \n"
".global _start \n" ".global _start \n"
"_start: \n\t" "_start: \n\t"
"mov %cs, %ax \n\t" "mov %cs, %ax \n\t"
@@ -15,8 +14,7 @@ asm (
"mov $0x1000, %ax \n\t" "mov $0x1000, %ax \n\t"
"mov %ax, %sp \n\t" "mov %ax, %sp \n\t"
"sti \n\t" "sti \n\t"
"jmp main" "jmp main");
);
void* memset(void* ptr, int val, size_t len) { void* memset(void* ptr, int val, size_t len) {
uint8_t* p = ptr; uint8_t* p = ptr;

View File

@@ -8,7 +8,6 @@
#define kRootDirSizeSectors 7 #define kRootDirSizeSectors 7
#define kDataRegionStartSector (1 + kFatSizeSectors * 2 + kRootDirSizeSectors) #define kDataRegionStartSector (1 + kFatSizeSectors * 2 + kRootDirSizeSectors)
typedef struct { typedef struct {
char name[8]; char name[8];
char ext[3]; char ext[3];
@@ -38,8 +37,8 @@ static int readsector(int c, int h, int s, uint8_t* addr) {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
asm volatile("int $0x13" asm volatile("int $0x13"
: "=r"(ret) : "=r"(ret)
: "r" (dest), "r" (nsects), "r" (func), "r" (sect), : "r"(dest), "r"(nsects), "r"(func), "r"(sect), "r"(cyl),
"r" (cyl), "r" (head), "r" (drive), "r" (seg)); "r"(head), "r"(drive), "r"(seg));
if (ret != 0x80) { if (ret != 0x80) {
break; break;
} }
@@ -59,7 +58,8 @@ static int readcluster(int cluster) {
} }
static void cluster2chs(int cluster, int* c, int* h, int* s) { static void cluster2chs(int cluster, int* c, int* h, int* s) {
int logicalsector = kDataRegionStartSector + (cluster - 2) * kSectorsPerCluster; int logicalsector =
kDataRegionStartSector + (cluster - 2) * kSectorsPerCluster;
*s = (logicalsector % kSectorsPerTrack) + 1; *s = (logicalsector % kSectorsPerTrack) + 1;
*h = (logicalsector / kSectorsPerTrack) % kHeads; *h = (logicalsector / kSectorsPerTrack) % kHeads;
*c = logicalsector / (kHeads * kSectorsPerTrack); *c = logicalsector / (kHeads * kSectorsPerTrack);

View File

@@ -7,15 +7,12 @@
#define kFatAddress ((void*)0x1000) #define kFatAddress ((void*)0x1000)
#define kRootDirAddress ((void*)0x1200) #define kRootDirAddress ((void*)0x1200)
static int putchar(int c) { static int putchar(int c) {
register uint8_t khar asm("al") = c; register uint8_t khar asm("al") = c;
register uint8_t func asm("ah") = 0x0e; register uint8_t func asm("ah") = 0x0e;
register uint8_t page asm("bh") = 0; register uint8_t page asm("bh") = 0;
asm volatile ("int $0x10" asm volatile("int $0x10" ::"r"(khar), "r"(func), "r"(page) : "bp");
:: "r" (khar), "r" (func), "r" (page)
: "bp");
return c; return c;
} }
@@ -27,37 +24,30 @@ static int puts(const char* msg) {
return 0; return 0;
} }
__attribute__((noreturn)) __attribute__((noreturn)) static void die(const char* msg) {
static void die(const char* msg) {
puts(msg); puts(msg);
while (1) { while (1) {
} }
__builtin_unreachable(); __builtin_unreachable();
} }
__attribute__((noreturn)) __attribute__((noreturn)) static void jump(void* addr) {
static void jump(void* addr) {
asm volatile("ljmp $0,%0" ::"i"(addr)); asm volatile("ljmp $0,%0" ::"i"(addr));
__builtin_unreachable(); __builtin_unreachable();
} }
__attribute__((noreturn)) __attribute__((noreturn)) static void loadpolmon() {
static void loadpolmon() {
if (fat12_init(kFatAddress, kRootDirAddress)) { if (fat12_init(kFatAddress, kRootDirAddress)) {
die("fi"); die("fi");
} }
while (fat12_readfile("POLIO COM", kPolmonAddress)) { while (fat12_readfile("POLIO COM", kPolmonAddress)) {
asm volatile ( asm volatile("mov $00, %%ah \n\t"
"mov $00, %%ah \n\t" "int $0x16 \n\t" ::
"int $0x16 \n\t" : "ax");
::: "ax"
);
} }
jump(kPolmonAddress); jump(kPolmonAddress);
} }
int main() { int main() { loadpolmon(); }
loadpolmon();
}

View File

@@ -47,8 +47,7 @@ uint8_t paracomm_nextbyte() {
mosi_sent = 0; mosi_sent = 0;
mosi_state = 2; mosi_state = 2;
return mosi_size; return mosi_size;
case 2: case 2: {
{
uint8_t b = mosi_sendbuf[mosi_sent]; uint8_t b = mosi_sendbuf[mosi_sent];
mosi_sent += 1; mosi_sent += 1;
@@ -83,8 +82,7 @@ void paracomm_feed(uint8_t n) {
miso_received_nibbles = 0; miso_received_nibbles = 0;
miso_state = 3; miso_state = 3;
break; break;
case 3: case 3: {
{
uint8_t idx = miso_received_nibbles / 2; uint8_t idx = miso_received_nibbles / 2;
if (miso_received_nibbles % 2 == 0) { if (miso_received_nibbles % 2 == 0) {
miso_recvbuf[idx] = n; miso_recvbuf[idx] = n;
@@ -99,8 +97,7 @@ void paracomm_feed(uint8_t n) {
} }
miso_state = 0; miso_state = 0;
} }
} } break;
break;
} }
} }

View File

@@ -44,26 +44,20 @@ namespace {
int getchar() { int getchar() {
register char c asm("al"); register char c asm("al");
asm volatile ( asm volatile("movb $0x00, %%ah\n\t"
"movb $0x00, %%ah\n\t"
"int $0x16" "int $0x16"
: "=r" (c) : "=r"(c)::"ah", "cc");
:: "ah", "cc"
);
return c; return c;
} }
void putchar(int c) { void putchar(int c) {
asm volatile ( asm volatile("push %%bp \n\t"
"push %%bp \n\t"
"mov %0, %%ax \n\t" "mov %0, %%ax \n\t"
"movb $0x0e, %%ah \n\t" "movb $0x0e, %%ah \n\t"
"movb $0, %%bh \n\t" "movb $0, %%bh \n\t"
"int $0x10 \n\t" "int $0x10 \n\t"
"pop %%bp \n\t" "pop %%bp \n\t" ::"r"(c)
:: "r" (c) : "ax", "bh", "cc");
: "ax", "bh", "cc"
);
} }
#endif // WOZMON #endif // WOZMON
@@ -72,8 +66,7 @@ constexpr int kDumpSize = 16;
// arguments on the stack in reverse order // arguments on the stack in reverse order
extern "C" uint16_t Int80h(uint8_t fun, int nargs); extern "C" uint16_t Int80h(uint8_t fun, int nargs);
asm ( asm(".section .text.Int80h \n"
".section .text.Int80h \n"
"Int80h: \n" "Int80h: \n"
" push %si \n" " push %si \n"
" push %bp \n" " push %bp \n"
@@ -119,13 +112,9 @@ uint16_t ReadUintN(int n, const char* buf) {
return out; return out;
} }
uint8_t ReadUint8(const char* buf) { uint8_t ReadUint8(const char* buf) { return ReadUintN(2, buf); }
return ReadUintN(2, buf);
}
uint16_t ReadUint16(const char* buf) { uint16_t ReadUint16(const char* buf) { return ReadUintN(4, buf); }
return ReadUintN(4, buf);
}
void WriteHexNibble(uint8_t c) { void WriteHexNibble(uint8_t c) {
if (c > 9) { if (c > 9) {
@@ -149,11 +138,7 @@ uint8_t __get_far_u8__(uint16_t addr, uint16_t seg) {
register uint16_t ad asm("si") = addr; register uint16_t ad asm("si") = addr;
register uint16_t sg asm("ds") = seg; register uint16_t sg asm("ds") = seg;
register uint8_t ret asm("al"); register uint8_t ret asm("al");
asm ( asm("lodsb \n\t" : "=r"(ret) : "r"(ad), "r"(sg));
"lodsb \n\t"
: "=r" (ret)
: "r" (ad), "r" (sg)
);
return ret; return ret;
} }
@@ -161,20 +146,13 @@ void __set_far_u8__(uint16_t addr, uint16_t seg, uint8_t val) {
register uint16_t ad asm("di") = addr; register uint16_t ad asm("di") = addr;
register uint16_t sg asm("es") = seg; register uint16_t sg asm("es") = seg;
register uint8_t v asm("al") = val; register uint8_t v asm("al") = val;
asm ( asm("stosb \n\t" ::"r"(sg), "r"(ad), "r"(v) : "memory");
"stosb \n\t"
:: "r" (sg), "r" (ad), "r" (v)
: "memory"
);
} }
__attribute__((noreturn)) __attribute__((noreturn)) inline static void __basic__() {
inline static void __basic__() { asm volatile("movw $0x40, %ax \n\t"
asm volatile (
"movw $0x40, %ax \n\t"
"mov %ax, %ds \n\t" "mov %ax, %ds \n\t"
"int $0x18" "int $0x18");
);
__builtin_unreachable(); __builtin_unreachable();
} }
@@ -213,16 +191,13 @@ void DumpHex(uint16_t addr, uint16_t seg) {
} }
void ClearScreen() { void ClearScreen() {
asm volatile ( asm volatile("movw $0x0002, %%ax \n\t"
"movw $0x0002, %%ax \n\t" "int $0x10" ::
"int $0x10" : "ax", "cc");
::: "ax", "cc"
);
} }
void Status(uint8_t status) { void Status(uint8_t status) {
asm volatile ( asm volatile("xorb %%bh, %%bh \n\t"
"xorb %%bh, %%bh \n\t"
"movb $0x03, %%ah \n\t" "movb $0x03, %%ah \n\t"
"int $0x10 \n\t" "int $0x10 \n\t"
"mov %%dx, %%di \n\t" "mov %%dx, %%di \n\t"
@@ -233,10 +208,9 @@ void Status(uint8_t status) {
"call %1 \n\t" "call %1 \n\t"
"movb $0x02, %%ah \n\t" "movb $0x02, %%ah \n\t"
"movw %%di, %%dx \n\t" "movw %%di, %%dx \n\t"
"int $0x10" "int $0x10" ::"rm"(status),
:: "rm" (status), "l" (WriteUint8) "l"(WriteUint8)
: "ax", "bh", "dx", "cx", "di", "cc" : "ax", "bh", "dx", "cx", "di", "cc");
);
} }
bool ParseCommand(const char* buf, uint16_t& cur_addr, uint16_t& cur_seg) { bool ParseCommand(const char* buf, uint16_t& cur_addr, uint16_t& cur_seg) {
@@ -274,11 +248,8 @@ bool ParseCommand(const char* buf, uint16_t& cur_addr, uint16_t& cur_seg) {
ptr += 4; ptr += 4;
} }
uint16_t ret = Int80h(fun, nargs); uint16_t ret = Int80h(fun, nargs);
asm volatile ( asm volatile("shl %0 \n\t"
"shl %0 \n\t" "add %0, %%sp" ::"r"(nargs));
"add %0, %%sp"
:: "r"(nargs)
);
WriteUint16(ret); WriteUint16(ret);
putchar('\r'); putchar('\r');
putchar('\n'); putchar('\n');
@@ -368,13 +339,10 @@ void polmon() {
} // namespace } // namespace
int main() { int main() { polmon(); }
polmon();
}
#if BOOTSTRAP #if BOOTSTRAP
__attribute__((section(".init"), noreturn, used)) __attribute__((section(".init"), noreturn, used)) void _start() {
void _start() {
main(); main();
__builtin_unreachable(); __builtin_unreachable();
} }

View File

@@ -1,25 +1,19 @@
int getchar() { int getchar() {
register char c asm("al"); register char c asm("al");
asm volatile ( asm volatile("movb $0x00, %%ah\n\t"
"movb $0x00, %%ah\n\t"
"int $0x16" "int $0x16"
: "=r" (c) : "=r"(c)::"ah", "cc");
:: "ah", "cc"
);
return c; return c;
} }
int putchar(int c) { int putchar(int c) {
asm volatile ( asm volatile("push %%bp \n\t"
"push %%bp \n\t"
"mov %0, %%ax \n\t" "mov %0, %%ax \n\t"
"movb $0x0e, %%ah \n\t" "movb $0x0e, %%ah \n\t"
"movb $0, %%bh \n\t" "movb $0, %%bh \n\t"
"int $0x10 \n\t" "int $0x10 \n\t"
"pop %%bp \n\t" "pop %%bp \n\t" ::"r"(c)
:: "r" (c) : "ax", "bh", "cc");
: "ax", "bh", "cc"
);
return 0; return 0;
} }