Compare commits
5
Commits
fd54d48aca
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5789cafd4e | ||
|
|
964e623644 | ||
|
|
8d2bb5575e | ||
|
|
ba4131abbf | ||
|
|
1ada1a12cc |
@@ -3,10 +3,16 @@
|
||||
**/*.EXE
|
||||
**/*.exe
|
||||
**/*.out
|
||||
**/*.elf
|
||||
**/*.bin
|
||||
**/*.BAK
|
||||
**/*.wdl
|
||||
**/*.DX
|
||||
*.sublime-*
|
||||
|
||||
Player/player
|
||||
Player/player.wasm
|
||||
Player/Cosmo
|
||||
Master/master
|
||||
|
||||
Player/Out/Cosmo/player*
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
Generic std parts, building on top of platform specifics.
|
||||
*/
|
||||
|
||||
#include "../fix.h"
|
||||
#include "../std.h"
|
||||
#include "../str.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static uint8_t com_std_any_format_u32(uint32_t v, char buf[10]) {
|
||||
uint8_t c = 0;
|
||||
if (v == 0) {
|
||||
buf[10 - (++c)] = '0';
|
||||
} else {
|
||||
while (v > 0) {
|
||||
buf[10 - (++c)] = '0' + (v % 10);
|
||||
v /= 10;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static uint8_t com_std_any_format_i32(int32_t v, char buf[11]) {
|
||||
bool neg = v < 0;
|
||||
uint8_t c = 0;
|
||||
if (v == 0) {
|
||||
buf[11 - (++c)] = '0';
|
||||
} else {
|
||||
while (v > 0) {
|
||||
buf[11 - (++c)] = '0' + (v % 10);
|
||||
v /= 10;
|
||||
}
|
||||
}
|
||||
if (neg)
|
||||
buf[11 - (++c)] = '-';
|
||||
return c;
|
||||
}
|
||||
|
||||
/* Just assume 5 points of precision. */
|
||||
static uint8_t com_std_any_format_f64(double v, char buf[11 + 1 + 5]) {
|
||||
bool neg = v < 0;
|
||||
uint8_t c = 0;
|
||||
double frac_part = v - (double)(int32_t)v;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
frac_part *= 10.0;
|
||||
uint8_t digit = (unsigned int)frac_part;
|
||||
buf[17 - (++c)] = digit + '0';
|
||||
frac_part -= digit;
|
||||
}
|
||||
buf[17 - (++c)] = '.';
|
||||
c += com_std_any_format_i32((int32_t)v, &buf[c]);
|
||||
return c;
|
||||
}
|
||||
|
||||
/* We can make it buffered, but who cares, it's only for testing. */
|
||||
void com_std_print_(int32_t start, ...) {
|
||||
va_list args;
|
||||
va_start(args, start);
|
||||
|
||||
bool afterstring = false;
|
||||
char type = start;
|
||||
do {
|
||||
|
||||
/* Put space if not proceeding string. */
|
||||
if (!afterstring && type != 'c')
|
||||
com_std_write(COM_STD_OUTFD, " ", 1);
|
||||
|
||||
afterstring = false;
|
||||
|
||||
switch (type) {
|
||||
|
||||
/* uint32_t case. */
|
||||
case 'u': {
|
||||
uint32_t const v = va_arg(args, uint32_t);
|
||||
char buf[10];
|
||||
uint8_t c = com_std_any_format_u32(v, buf);
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
|
||||
break;
|
||||
}
|
||||
|
||||
/* null terminated string case. */
|
||||
case 'c': {
|
||||
char const *const v = va_arg(args, char *);
|
||||
com_std_write(COM_STD_OUTFD, v, com_str_ext_len(v));
|
||||
afterstring = true;
|
||||
break;
|
||||
}
|
||||
|
||||
/* TODO: FIX THIS */
|
||||
/* com_fix case. */
|
||||
case 'f': {
|
||||
com_fix_t const v = va_arg(args, com_fix_t);
|
||||
char buf[11];
|
||||
|
||||
int32_t const int_part = com_fix_int(v);
|
||||
uint8_t c = com_std_any_format_i32(int_part, buf);
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
|
||||
|
||||
uint32_t const decimal_val = (com_fix_frac(v) * 10000) >> 16;
|
||||
uint8_t cd = com_std_any_format_u32(decimal_val, buf);
|
||||
buf[sizeof buf - (++cd)] = '.';
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - cd], cd);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* double floating point case. */
|
||||
case 'd': {
|
||||
double const v = va_arg(args, double);
|
||||
char buf[17];
|
||||
|
||||
uint8_t c = com_std_any_format_f64(v, buf);
|
||||
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
|
||||
}
|
||||
|
||||
/* End of things. */
|
||||
case '.':
|
||||
break;
|
||||
|
||||
default:
|
||||
com_std_assert(false && "Unhandled format type");
|
||||
}
|
||||
|
||||
type = va_arg(args, int32_t);
|
||||
|
||||
} while (type != '.');
|
||||
|
||||
com_std_write(COM_STD_OUTFD, "\n", 1);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void com_std_print_str(char const *str, uint32_t len, bool newline) {
|
||||
com_std_write(COM_STD_OUTFD, str, len);
|
||||
if (newline)
|
||||
com_std_write(COM_STD_OUTFD, "\n", 1);
|
||||
}
|
||||
+20
-15
@@ -1,7 +1,9 @@
|
||||
#include "../std.h"
|
||||
#include "../str.h"
|
||||
#include "any.c"
|
||||
#include <assert.h>
|
||||
#include <poll.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -11,19 +13,13 @@
|
||||
void *com_std_alloc(void *ptr, size_t size) {
|
||||
return size != 0 ? realloc(ptr, size) : (free(ptr), NULL);
|
||||
}
|
||||
void com_std_assert(bool holds) { assert(holds); }
|
||||
void com_std_printf(char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
double com_std_sqrt(double v) { return sqrt(v); }
|
||||
double com_std_fabs(double v) { return fabs(v); }
|
||||
double com_std_sin(double v) { return sin(v); }
|
||||
double com_std_cos(double v) { return cos(v); }
|
||||
|
||||
com_std_fd com_std_open(char *path, uint32_t flags) {
|
||||
com_std_fd com_std_open(char const *path, uint32_t flags) {
|
||||
com_std_assert(flags != 0);
|
||||
char *mode = flags & COM_STD_OPEN_READ && flags & COM_STD_OPEN_WRITE ? "r+"
|
||||
: flags & COM_STD_OPEN_READ ? "r"
|
||||
@@ -31,18 +27,27 @@ com_std_fd com_std_open(char *path, uint32_t flags) {
|
||||
return fopen(path, mode);
|
||||
}
|
||||
|
||||
bool com_std_write(com_std_fd fd, char *data, uint32_t bytes) {
|
||||
com_std_assert(fd != NULL && data != NULL && bytes != 0);
|
||||
bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
|
||||
com_std_assert(data != NULL && bytes != 0);
|
||||
bool s = fwrite(data, 1, bytes, fd) == bytes;
|
||||
fflush(fd);
|
||||
return s;
|
||||
}
|
||||
|
||||
int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto) {
|
||||
com_std_assert(fd != NULL && data != NULL && upto != 0);
|
||||
int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||
com_std_assert(data != NULL && upto != 0);
|
||||
return fread(data, 1, upto, fd);
|
||||
}
|
||||
|
||||
bool com_std_has_data(com_std_fd fd) {
|
||||
struct pollfd pfd = {.fd = (intptr_t)fd, .events = POLLIN};
|
||||
int ret = poll(&pfd, 1, 0);
|
||||
if (ret > 0) {
|
||||
return pfd.revents & POLLIN;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
|
||||
com_std_assert(fd != NULL && bytes != 0);
|
||||
return fseek(fd, bytes, SEEK_CUR) == 0;
|
||||
@@ -56,7 +61,7 @@ com_std_fd com_std_socket(bool isnetwork) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
bool com_std_connect(com_std_fd fd, bool isnetwork, char const *address) {
|
||||
com_std_assert(address != NULL);
|
||||
if (!isnetwork) {
|
||||
struct sockaddr_un addr = {.sun_family = AF_UNIX};
|
||||
@@ -66,6 +71,6 @@ bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void com_std_exit(uint32_t status) { exit(status); }
|
||||
void com_std_exit(int32_t status) { exit(status); }
|
||||
|
||||
void com_std_close(com_std_fd fd) { fclose(fd); }
|
||||
|
||||
@@ -6,11 +6,12 @@
|
||||
|
||||
#include "../std.h"
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
inline com_std_fd com_std_open(char *path, uint32_t flags) {
|
||||
inline com_std_fd com_std_open(char const *path, uint32_t flags) {
|
||||
com_std_assert(flags != 0);
|
||||
uint32_t mode = flags & COM_STD_OPEN_READ && flags & COM_STD_OPEN_WRITE
|
||||
? O_RDWR
|
||||
@@ -26,7 +27,7 @@ inline com_std_fd com_std_open(char *path, uint32_t flags) {
|
||||
return (void *)ret;
|
||||
}
|
||||
|
||||
inline bool com_std_write(com_std_fd fd, char const *data, uint32_t bytes) {
|
||||
inline bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
|
||||
long ret;
|
||||
__asm__ volatile("syscall"
|
||||
: "=a"(ret)
|
||||
@@ -35,7 +36,7 @@ inline bool com_std_write(com_std_fd fd, char const *data, uint32_t bytes) {
|
||||
return ret == bytes;
|
||||
}
|
||||
|
||||
inline int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto) {
|
||||
inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||
long ret;
|
||||
__asm__ volatile("syscall"
|
||||
: "=a"(ret)
|
||||
@@ -44,6 +45,23 @@ inline int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline bool com_std_has_data(com_std_fd fd) {
|
||||
struct pollfd pfd = {
|
||||
.fd = (intptr_t)fd,
|
||||
.events = POLLIN,
|
||||
};
|
||||
|
||||
long ret;
|
||||
__asm__ volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(7), "D"(&pfd), "S"(1), "d"(0)
|
||||
: "rcx", "r11", "memory");
|
||||
if (ret > 0) {
|
||||
return pfd.revents & POLLIN;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
|
||||
long ret;
|
||||
__asm__ volatile("syscall"
|
||||
@@ -67,7 +85,8 @@ inline com_std_fd com_std_socket(bool isnetwork) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inline bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
inline bool com_std_connect(com_std_fd fd, bool isnetwork,
|
||||
char const *address) {
|
||||
com_std_assert(address != NULL);
|
||||
if (!isnetwork) {
|
||||
struct sockaddr_un addr = {.sun_family = AF_UNIX};
|
||||
@@ -82,7 +101,7 @@ inline bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inline void com_std_exit(uint32_t status) {
|
||||
inline void com_std_exit(int32_t status) {
|
||||
__asm__ volatile("syscall" : : "a"(60), "D"(status) : "rcx", "r11", "memory");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
+24
-5
@@ -6,11 +6,12 @@
|
||||
|
||||
#include "../std.h"
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
inline com_std_fd com_std_open(char *path, uint32_t flags) {
|
||||
inline com_std_fd com_std_open(char const *path, uint32_t flags) {
|
||||
com_std_assert(flags != 0);
|
||||
uint32_t mode = flags & COM_STD_OPEN_READ && flags & COM_STD_OPEN_WRITE
|
||||
? O_RDWR
|
||||
@@ -26,7 +27,7 @@ inline com_std_fd com_std_open(char *path, uint32_t flags) {
|
||||
return (void *)ret;
|
||||
}
|
||||
|
||||
inline bool com_std_write(com_std_fd fd, char const *data, uint32_t bytes) {
|
||||
inline bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
|
||||
long ret;
|
||||
__asm__ volatile("int $0x80"
|
||||
: "=a"(ret)
|
||||
@@ -35,7 +36,7 @@ inline bool com_std_write(com_std_fd fd, char const *data, uint32_t bytes) {
|
||||
return ret == bytes;
|
||||
}
|
||||
|
||||
inline int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto) {
|
||||
inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||
long ret;
|
||||
__asm__ volatile("int $0x80"
|
||||
: "=a"(ret)
|
||||
@@ -44,6 +45,23 @@ inline int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline bool com_std_has_data(com_std_fd fd) {
|
||||
struct pollfd pfd = {
|
||||
.fd = (intptr_t)fd,
|
||||
.events = POLLIN,
|
||||
};
|
||||
|
||||
long ret;
|
||||
__asm__ volatile("int $0x80"
|
||||
: "=a"(ret)
|
||||
: "a"(168), "b"(&pfd), "c"(1), "d"(0)
|
||||
: "memory", "cc");
|
||||
if (ret > 0) {
|
||||
return pfd.revents & POLLIN;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
|
||||
long ret;
|
||||
__asm__ volatile("int $0x80"
|
||||
@@ -67,7 +85,8 @@ inline com_std_fd com_std_socket(bool isnetwork) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inline bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
inline bool com_std_connect(com_std_fd fd, bool isnetwork,
|
||||
char const *address) {
|
||||
com_std_assert(address != NULL);
|
||||
if (!isnetwork) {
|
||||
struct sockaddr_un addr = {.sun_family = AF_UNIX};
|
||||
@@ -82,7 +101,7 @@ inline bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inline void com_std_exit(uint32_t status) {
|
||||
inline void com_std_exit(int32_t status) {
|
||||
__asm__ volatile("int $0x80" : : "a"(1), "b"(status) : "memory", "cc");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
+3
-29
@@ -2,41 +2,15 @@
|
||||
Generic linux std parts.
|
||||
*/
|
||||
|
||||
#include "../def.h"
|
||||
#include "any.c"
|
||||
#include "nolibc.c"
|
||||
|
||||
#include "../std.h"
|
||||
#include "../str.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
|
||||
void com_std_assert(bool holds) {
|
||||
/* TODO: Write line info to stderr as well. */
|
||||
if (!holds)
|
||||
com_std_exit(-1);
|
||||
}
|
||||
|
||||
/* TODO: Figure out a way to make it easy to deal with. */
|
||||
void com_std_printf(char const *fmt, ...) {
|
||||
com_std_write((void *)1, fmt, com_str_ext_len(fmt));
|
||||
}
|
||||
|
||||
void com_std_print_str(char const *str, uint32_t len, bool newline) {
|
||||
com_std_write((void *)1, str, len);
|
||||
if (newline)
|
||||
com_std_write((void *)1, "\n", 1);
|
||||
}
|
||||
|
||||
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
|
||||
double com_std_fabs(double v) { return __builtin_fabs(v); }
|
||||
#if COM_DEF_COMPILER == COM_DEF_COMPILER_CLANG
|
||||
double com_std_sin(double v) { return __builtin_sin(v); }
|
||||
double com_std_cos(double v) { return __builtin_cos(v); }
|
||||
#else
|
||||
/* TODO: Use fsin/fcos, no matter how slow. */
|
||||
double com_std_sin(double v) { return 0; }
|
||||
double com_std_cos(double v) { return 0; }
|
||||
#endif
|
||||
|
||||
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
||||
// __builtin_memcpy(dest, src, n);
|
||||
// };
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Generic no libc std parts.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* Compiler depends on them with low optimization level. */
|
||||
void *memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
((char *)dest)[i] = ((char *)src)[i];
|
||||
return dest;
|
||||
};
|
||||
|
||||
void *memset(void *__restrict dest, int s, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
((char *)dest)[i] = s;
|
||||
return dest;
|
||||
};
|
||||
|
||||
size_t strlen(char const *s) {
|
||||
char const *p = s;
|
||||
for (; *p != 0; ++p) {
|
||||
}
|
||||
return p - s;
|
||||
}
|
||||
|
||||
double sin(double v) {
|
||||
double r;
|
||||
__asm__ __volatile__("fsin" : "=t"(r) : "0"(v));
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
double cos(double v) {
|
||||
double r;
|
||||
__asm__ __volatile__("fcos" : "=t"(r) : "0"(v));
|
||||
return r;
|
||||
}
|
||||
|
||||
void sincos(double v, double *s, double *c) {
|
||||
double sv, cv;
|
||||
__asm__ __volatile__("fsincos" : "=t"(cv), "=u"(sv) : "0"(v));
|
||||
*s = sv;
|
||||
*c = cv;
|
||||
}
|
||||
|
||||
double sqrt(double v) {
|
||||
double r;
|
||||
__asm__ volatile("sqrtss %1, %0" : "=x"(r) : "x"(v));
|
||||
return r;
|
||||
}
|
||||
+6
-10
@@ -1,11 +1,11 @@
|
||||
#include "../std.h"
|
||||
|
||||
#include "any.c"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
|
||||
void com_std_assert(bool holds) {}
|
||||
void com_std_printf(char const *fmt, ...) {}
|
||||
|
||||
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
|
||||
double com_std_fabs(double v) { return __builtin_fabs(v); }
|
||||
@@ -18,14 +18,14 @@ double com_std_sin(double v) { return 0; }
|
||||
double com_std_cos(double v) { return 0; }
|
||||
#endif
|
||||
|
||||
com_std_fd com_std_open(char *path, uint32_t flags) { return NULL; }
|
||||
bool com_std_write(com_std_fd fd, char const *data, uint32_t bytes) {
|
||||
com_std_fd com_std_open(char const *path, uint32_t flags) { return NULL; }
|
||||
bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
|
||||
return false;
|
||||
}
|
||||
int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto) { return -1; }
|
||||
int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) { return -1; }
|
||||
bool com_std_sfwd(com_std_fd fd, uint32_t bytes) { return false; }
|
||||
com_std_fd com_std_socket(bool isnetwork) { return NULL; }
|
||||
bool com_std_connect(com_std_fd fd, bool isnetwork, char *address) {
|
||||
bool com_std_connect(com_std_fd fd, bool isnetwork, char const *address) {
|
||||
return false;
|
||||
}
|
||||
void com_std_exit(uint32_t status) {
|
||||
@@ -35,7 +35,3 @@ void com_std_exit(uint32_t status) {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
void com_std_close(com_std_fd fd) { return; }
|
||||
|
||||
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
||||
// __builtin_memcpy(dest, src, n);
|
||||
// };
|
||||
|
||||
@@ -13,5 +13,6 @@ uint64_t com_timer_count_ns(void) {
|
||||
void com_timer_profile(uint64_t starttime, const char *what) {
|
||||
uint64_t const endtime = com_timer_count_ns();
|
||||
uint64_t const diff = endtime - starttime;
|
||||
com_std_printf("profile \"%s\" took %fms\n", what, (double)diff / 1000000);
|
||||
com_std_print('c', "Profile ", 'c', what, 'c', " took ", 'd',
|
||||
(double)diff / 1000000, 'c', "ms");
|
||||
}
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ typedef struct com_buf {
|
||||
struct com_buf *p_v = (struct com_buf *)&p_v##_buf; \
|
||||
p_v->cap = (p_c); \
|
||||
p_v->flags = p_f | COM_BUF_STACK; \
|
||||
p_v->typesz = sizeof(p_t);
|
||||
p_v->typesz = sizeof(p_t)
|
||||
|
||||
#define com_buf_index_as(p_t, p_v, p_i) \
|
||||
(p_t *)(&(p_v)->data[sizeof(p_t) * (p_i)])
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
Configuration file.
|
||||
*/
|
||||
|
||||
/* TODO: Move to root or Shared/ folder? */
|
||||
|
||||
#ifndef COM_CFG_H
|
||||
#define COM_CFG_H
|
||||
|
||||
@@ -9,4 +11,12 @@
|
||||
#define COM_CFG_WIDTH 640
|
||||
#define COM_CFG_HEIGHT 480
|
||||
|
||||
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
||||
#define COM_CFG_COLORS 4
|
||||
#else
|
||||
#define COM_CFG_COLORS 3
|
||||
#endif
|
||||
|
||||
#define COM_CFG_FBSIZE (COM_CFG_WIDTH * COM_CFG_HEIGHT * COM_CFG_COLORS)
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#ifndef COM_DEF_H
|
||||
#define COM_DEF_H
|
||||
|
||||
#define COM_DEF_STRLIT_(x) #x
|
||||
#define COM_DEF_STRLIT(x) COM_DEF_STRLIT_(x)
|
||||
|
||||
#define COM_DEF_COMPILER_CLANG 1
|
||||
#define COM_DEF_COMPILER_GCC 2
|
||||
|
||||
@@ -17,6 +20,7 @@
|
||||
|
||||
#define COM_DEF_TARGET_LINUX 1
|
||||
#define COM_DEF_TARGET_WASM 2
|
||||
#define COM_DEF_TARGET_COSMO 3
|
||||
|
||||
#if defined(__wasm__)
|
||||
#define COM_DEF_TARGET COM_DEF_TARGET_WASM
|
||||
@@ -24,6 +28,9 @@
|
||||
#elif defined(__linux__)
|
||||
#define COM_DEF_TARGET COM_DEF_TARGET_LINUX
|
||||
#define com_def_export_sym(m_s) __attribute__((visibility("default")))
|
||||
#elif defined(__COSMOPOLITAN__)
|
||||
#define COM_DEF_TARGET COM_DEF_TARGET_COSMO
|
||||
#define com_def_export_sym(m_s) __attribute__((visibility("default")))
|
||||
#else
|
||||
#error Unknown target.
|
||||
#endif
|
||||
@@ -31,6 +38,7 @@
|
||||
#define COM_DEF_CPU_AMD64 1
|
||||
#define COM_DEF_CPU_X86 2
|
||||
#define COM_DEF_CPU_WASM32 3
|
||||
#define COM_DEF_CPU_AARCH64 4
|
||||
|
||||
#if defined(__wasm__)
|
||||
#define COM_DEF_CPU COM_DEF_CPU_WASM32
|
||||
@@ -44,6 +52,10 @@
|
||||
#define COM_DEF_CPU COM_DEF_CPU_X86
|
||||
#define COM_DEF_ALIGNMENT 16
|
||||
#define COM_DEF_CPU_LITEND 1
|
||||
#elif defined(__aarch64__)
|
||||
#define COM_DEF_CPU COM_DEF_CPU_AARCH64
|
||||
#define COM_DEF_ALIGNMENT 16
|
||||
#define COM_DEF_CPU_LITEND 1
|
||||
#else
|
||||
#error Unknown cpu.
|
||||
#endif
|
||||
|
||||
@@ -71,20 +71,6 @@ com_fix_t com_fix_sqrt(com_fix_t a) {
|
||||
: (int32_t)(x >> (-final_shift));
|
||||
}
|
||||
|
||||
void com_fix_print(com_fix_t a) {
|
||||
if (a < 0) {
|
||||
com_std_printf("-");
|
||||
a = -a;
|
||||
}
|
||||
int32_t int_part = a >> 16;
|
||||
int32_t frac_part = a & 0xFFFF;
|
||||
|
||||
// Convert fractional 16-bit part to a decimal value (up to 4 decimal places)
|
||||
uint32_t decimal_val = (frac_part * 10000) >> 16;
|
||||
|
||||
com_std_printf("%d.%04u\n", int_part, decimal_val);
|
||||
}
|
||||
|
||||
void com_fix_run_bench(void) {
|
||||
uint64_t start = com_timer_count_ns();
|
||||
for (int i = 5000000; i--;) {
|
||||
@@ -118,77 +104,3 @@ void com_fix_run_bench(void) {
|
||||
}
|
||||
com_timer_profile(start, "fixed: sincos");
|
||||
}
|
||||
|
||||
void com_fix_run_tests(void) {
|
||||
{
|
||||
double max_sqrt_deviation = 0.0f;
|
||||
com_fix_t sqrt_accumulator = 0;
|
||||
com_fix_t sqrt_test_step = COM_FIX_FRACUNIT >> 2; /* Quarter step */
|
||||
for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
|
||||
com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
|
||||
double const deviation =
|
||||
com_std_sqrt(com_fix_as_float(sqrt_accumulator)) -
|
||||
com_fix_as_float(sqrt);
|
||||
if (deviation > max_sqrt_deviation)
|
||||
max_sqrt_deviation = deviation;
|
||||
sqrt_accumulator += sqrt_test_step;
|
||||
}
|
||||
com_std_printf("max sqrt deviation: %f\n", max_sqrt_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0f;
|
||||
com_fix_t sin_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sin_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin = com_fix_sin(sin_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sin_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
if (deviation > max_sin_deviation)
|
||||
max_sin_deviation = deviation;
|
||||
sin_accumulator += sin_test_step;
|
||||
}
|
||||
com_std_printf("max sin deviation: %f\n", max_sin_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_cos_deviation = 0.0f;
|
||||
com_fix_t cos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t cos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t cos = com_fix_cos(cos_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(cos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (deviation > max_cos_deviation)
|
||||
max_cos_deviation = deviation;
|
||||
cos_accumulator += cos_test_step;
|
||||
}
|
||||
com_std_printf("max cos deviation: %f\n", max_cos_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0f;
|
||||
double max_cos_deviation = 0.0f;
|
||||
com_fix_t sincos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sincos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin, cos;
|
||||
com_fix_sincos(sincos_accumulator, &sin, &cos);
|
||||
double const sin_deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
double const cos_deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (sin_deviation > max_sin_deviation)
|
||||
max_sin_deviation = sin_deviation;
|
||||
if (cos_deviation > max_cos_deviation)
|
||||
max_cos_deviation = cos_deviation;
|
||||
sincos_accumulator += sincos_test_step;
|
||||
}
|
||||
com_std_printf("max sincos deviations: %f %f\n", max_sin_deviation,
|
||||
max_cos_deviation);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-5
@@ -23,8 +23,6 @@ typedef int32_t com_fix_t;
|
||||
|
||||
extern const uint16_t com_fix_sin_lut[128];
|
||||
|
||||
void com_fix_print(com_fix_t a);
|
||||
|
||||
void com_fix_run_tests(void);
|
||||
void com_fix_run_bench(void);
|
||||
|
||||
@@ -32,9 +30,9 @@ static inline com_fix_t com_fix_add(com_fix_t a, com_fix_t b) { return a + b; }
|
||||
|
||||
static inline com_fix_t com_fix_sub(com_fix_t a, com_fix_t b) { return a - b; }
|
||||
|
||||
static inline com_fix_t com_fix_int(com_fix_t a) {
|
||||
return a >> COM_FIX_FRACBITS;
|
||||
}
|
||||
static inline int16_t com_fix_int(com_fix_t a) { return a >> COM_FIX_FRACBITS; }
|
||||
|
||||
static inline uint16_t com_fix_frac(com_fix_t a) { return a & 0xFFFF; }
|
||||
|
||||
static inline com_fix_t com_fix_mul(com_fix_t a, com_fix_t b) {
|
||||
return (com_fix_t)(((int64_t)a * (int64_t)b) >> COM_FIX_FRACBITS);
|
||||
|
||||
+11
-15
@@ -57,12 +57,7 @@ static inline uint8_t com_gem_clip_vis_project(com_vec_t a, com_pnt_t *out) {
|
||||
mask ^= COM_GEM_VERTEX_CLIPPED_X;
|
||||
if (a.a[1] < -a.a[2] || a.a[1] > a.a[2])
|
||||
mask ^= COM_GEM_VERTEX_CLIPPED_Y;
|
||||
// out->a[0] = com_fix_mul((com_fix_div(a.a[0], a.a[2]) + COM_FIX_FRACUNIT) >>
|
||||
// 1,
|
||||
// COM_CFG_WIDTH * COM_FIX_FRACUNIT);
|
||||
// out->a[1] = com_fix_mul((com_fix_div(a.a[1], a.a[2]) + COM_FIX_FRACUNIT) >>
|
||||
// 1,
|
||||
// COM_CFG_HEIGHT * COM_FIX_FRACUNIT);
|
||||
|
||||
out->a[0] = (com_fix_mul(com_fix_div(a.a[0], a.a[2]),
|
||||
COM_CFG_WIDTH * COM_FIX_FRACUNIT) >>
|
||||
COM_FIX_FRACBITS) +
|
||||
@@ -71,6 +66,7 @@ static inline uint8_t com_gem_clip_vis_project(com_vec_t a, com_pnt_t *out) {
|
||||
COM_CFG_HEIGHT * COM_FIX_FRACUNIT) >>
|
||||
COM_FIX_FRACBITS) +
|
||||
(COM_CFG_HEIGHT / 2);
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
@@ -91,9 +87,9 @@ static inline void com_gem_draw_triangle(com_pnt_t v0, com_pnt_t v1,
|
||||
// If the point is inside or on all edges, draw the pixel
|
||||
// (Assumes Counter Clockwise vertex ordering)
|
||||
if (ws.a[0] >= 0 && ws.a[1] >= 0 && ws.a[2] >= 0) {
|
||||
out[(y * COM_CFG_WIDTH + x) * 4 + 0] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * 4 + 1] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * 4 + 2] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 0] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 1] = 125;
|
||||
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 2] = 125;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,9 +143,9 @@ com_gem_draw_triangle_textured_edge(com_pnt_t v0, com_pnt_t v1, com_pnt_t v2,
|
||||
// uint32_t bidx = com_zord_order(x, y);
|
||||
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
||||
|
||||
out[bidx * 4 + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * 4 + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * 4 + 2] = tex[idx * 3 + 2];
|
||||
out[bidx * COM_CFG_COLORS + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * COM_CFG_COLORS + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * COM_CFG_COLORS + 2] = tex[idx * 3 + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,9 +166,9 @@ static inline void draw_span(int32_t x_start_fp, int32_t x_end_fp, int y,
|
||||
for (int x = x1; x < x2; x++) {
|
||||
uint32_t idx = 0 * 32 + 0;
|
||||
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
||||
out[bidx * 4 + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * 4 + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * 4 + 2] = tex[idx * 3 + 2];
|
||||
out[bidx * COM_CFG_COLORS + 0] = tex[idx * 3 + 0];
|
||||
out[bidx * COM_CFG_COLORS + 1] = tex[idx * 3 + 1];
|
||||
out[bidx * COM_CFG_COLORS + 2] = tex[idx * 3 + 2];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,9 +111,4 @@ com_pnt_edge_weight_calc_precomp(com_pnt_t v0, com_pnt_t v1, com_pnt_t v2,
|
||||
precomp[2].a[1] * (p.a[0] - v0.a[0])}};
|
||||
}
|
||||
|
||||
static inline void com_pnt_print(com_pnt_t a) {
|
||||
com_fix_print(a.a[0]);
|
||||
com_fix_print(a.a[1]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+54
-7
@@ -10,17 +10,29 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if COM_DEF_TARGET != COM_DEF_TARGET_COSMO
|
||||
#define COM_STD_INFD (void *)0
|
||||
#define COM_STD_OUTFD (void *)1
|
||||
#else
|
||||
/* TODO: Make it more graceful, probably with static func. */
|
||||
#include <unistd.h>
|
||||
#define COM_STD_INFD (fdopen(STDIN_FILENO, "r"))
|
||||
#define COM_STD_OUTFD (fdopen(STDOUT_FILENO, "w"))
|
||||
#endif
|
||||
|
||||
/* Generic system interface, linux/bsd derived. */
|
||||
typedef void *com_std_fd;
|
||||
#define COM_STD_OPEN_READ (1 << 0)
|
||||
#define COM_STD_OPEN_WRITE (1 << 1)
|
||||
com_std_fd com_std_open(char *path, uint32_t flags);
|
||||
bool com_std_write(com_std_fd fd, char const *data, uint32_t bytes);
|
||||
int32_t com_std_read(com_std_fd fd, char *data, uint32_t upto);
|
||||
com_std_fd com_std_open(char const *path, uint32_t flags);
|
||||
bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes);
|
||||
/* TODO: Return unit32_t, error on ZERO? Separate EOF check? */
|
||||
int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto);
|
||||
bool com_std_has_data(com_std_fd);
|
||||
bool com_std_sfwd(com_std_fd fd, uint32_t bytes);
|
||||
com_std_fd com_std_socket(bool isnetwork);
|
||||
bool com_std_connect(com_std_fd sock, bool isnetwork, char *address);
|
||||
void com_std_exit(uint32_t status) __attribute__((noreturn));
|
||||
bool com_std_connect(com_std_fd sock, bool isnetwork, char const *address);
|
||||
void com_std_exit(int32_t status) __attribute__((noreturn));
|
||||
void com_std_close(com_std_fd fd);
|
||||
|
||||
/* TODO: Remove, use buffer semantics instead. */
|
||||
@@ -31,9 +43,44 @@ void *com_std_alloc(void *ptr, size_t size);
|
||||
/* Note: Functions listed below are only available for dev builds. */
|
||||
/* Trigonometry functions are for reference testing of our own fixed point
|
||||
* implementations. Do not use otherwise. */
|
||||
void com_std_assert(bool holds);
|
||||
void com_std_printf(char const *fmt, ...);
|
||||
#define com_std_assert(p_h) \
|
||||
do { \
|
||||
if (!(p_h)) { \
|
||||
const char msg[] = \
|
||||
"Assertion failed at "__FILE__ \
|
||||
":" COM_DEF_STRLIT(__LINE__) ": " COM_DEF_STRLIT(p_h); \
|
||||
com_std_print_str(msg, sizeof msg, true); \
|
||||
com_std_exit(-1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
Basic printing utility that doesn't utilize format string.
|
||||
Instead a vector of arguments is passed and concatenated, based on type.
|
||||
Newline is implied at the end.
|
||||
|
||||
Example: com_std_print("My unit32_t: ", 'u', value, 's', " is here");
|
||||
|
||||
Format specifiers:
|
||||
'u' = uint32_t
|
||||
'i' = int32_t
|
||||
'd' = float (upcast to double with vargs)
|
||||
'c' = null terminated C string
|
||||
|
||||
Our own types:
|
||||
'f' = com_fix value
|
||||
'p' = com_pnt pointer
|
||||
'v' = com_vec pointer
|
||||
's' = pointer to com_str structure
|
||||
'b' = pointer to com_buf structure
|
||||
|
||||
'.' is placed at the end implicitly with macro,
|
||||
it is needed to know when to end.
|
||||
*/
|
||||
void com_std_print_(int32_t start, ...);
|
||||
#define com_std_print(...) com_std_print_(__VA_ARGS__, '.')
|
||||
void com_std_print_str(char const *str, uint32_t len, bool newline);
|
||||
|
||||
double com_std_sqrt(double v);
|
||||
double com_std_fabs(double v);
|
||||
double com_std_sin(double v);
|
||||
|
||||
+3
-3
@@ -47,9 +47,9 @@ static inline int32_t com_str_ext_find_char(char const *s, char ch) {
|
||||
|
||||
/* Returns true if string 'a' is prefixed by 'b'. */
|
||||
static inline bool com_str_ext_prefixed(char const *__restrict a,
|
||||
struct com_str prefix) {
|
||||
uint32_t n = prefix.len;
|
||||
char *b = prefix.data;
|
||||
struct com_str const *prefix) {
|
||||
uint32_t n = prefix->len;
|
||||
char *b = prefix->data;
|
||||
while (n && *a && (*a == *b)) {
|
||||
++a;
|
||||
++b;
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
Centralized test suite.
|
||||
*/
|
||||
|
||||
#include "bits.h"
|
||||
#include "fix.h"
|
||||
#include "mat.h"
|
||||
#include "std.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define TEST(p_e) \
|
||||
do { \
|
||||
if (!(p_e)) { \
|
||||
com_std_print('c', COM_DEF_STRLIT(p_e) " failed!"); \
|
||||
anyfail = true; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static bool com_test_fix(void) {
|
||||
bool anyfail = false;
|
||||
{
|
||||
double max_sqrt_deviation = 0.0;
|
||||
com_fix_t sqrt_accumulator = 0;
|
||||
com_fix_t sqrt_test_step = COM_FIX_FRACUNIT >> 2; /* Quarter step */
|
||||
for (int i = 0; i < COM_FIX_FRACUNIT << 1; ++i) {
|
||||
com_fix_t sqrt = com_fix_sqrt(sqrt_accumulator);
|
||||
double const deviation =
|
||||
com_std_sqrt(com_fix_as_float(sqrt_accumulator)) -
|
||||
com_fix_as_float(sqrt);
|
||||
if (deviation > max_sqrt_deviation)
|
||||
max_sqrt_deviation = deviation;
|
||||
sqrt_accumulator += sqrt_test_step;
|
||||
}
|
||||
com_std_print('c', "Max sqrt deviation: ", 'd', max_sqrt_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0;
|
||||
com_fix_t sin_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sin_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin = com_fix_sin(sin_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sin_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
if (deviation > max_sin_deviation)
|
||||
max_sin_deviation = deviation;
|
||||
sin_accumulator += sin_test_step;
|
||||
}
|
||||
com_std_print('c', "Max sin deviation: ", 'd', max_sin_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_cos_deviation = 0.0;
|
||||
com_fix_t cos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t cos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t cos = com_fix_cos(cos_accumulator);
|
||||
double const deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(cos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (deviation > max_cos_deviation)
|
||||
max_cos_deviation = deviation;
|
||||
cos_accumulator += cos_test_step;
|
||||
}
|
||||
com_std_print('c', "Max cos deviation: ", 'd', max_cos_deviation);
|
||||
}
|
||||
|
||||
{
|
||||
double max_sin_deviation = 0.0;
|
||||
double max_cos_deviation = 0.0;
|
||||
com_fix_t sincos_accumulator = -COM_FIX_PI * 4;
|
||||
com_fix_t sincos_test_step = (COM_FIX_PI << 1) >> 10;
|
||||
for (int i = 0; i < 1024 * 32; ++i) {
|
||||
com_fix_t sin, cos;
|
||||
com_fix_sincos(sincos_accumulator, &sin, &cos);
|
||||
double const sin_deviation =
|
||||
com_std_fabs(com_std_sin(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(sin));
|
||||
double const cos_deviation =
|
||||
com_std_fabs(com_std_cos(com_fix_as_float(sincos_accumulator)) -
|
||||
com_fix_as_float(cos));
|
||||
if (sin_deviation > max_sin_deviation)
|
||||
max_sin_deviation = sin_deviation;
|
||||
if (cos_deviation > max_cos_deviation)
|
||||
max_cos_deviation = cos_deviation;
|
||||
sincos_accumulator += sincos_test_step;
|
||||
}
|
||||
com_std_print('c', "Max sincos deviations: ", 'd', max_sin_deviation, 'd',
|
||||
max_cos_deviation);
|
||||
}
|
||||
|
||||
return anyfail;
|
||||
}
|
||||
|
||||
static bool com_test_bits(void) {
|
||||
bool anyfail = false;
|
||||
|
||||
TEST(com_bits_nalign(0, 4) == 0);
|
||||
TEST(com_bits_nalign(1, 4) == 4);
|
||||
TEST(com_bits_nalign(2, 4) == 4);
|
||||
TEST(com_bits_nalign(3, 4) == 4);
|
||||
TEST(com_bits_nalign(4, 4) == 4);
|
||||
TEST(com_bits_nalign(5, 4) == 8);
|
||||
|
||||
return anyfail;
|
||||
}
|
||||
|
||||
static bool com_test_mat(void) {
|
||||
bool anyfail = false;
|
||||
|
||||
com_mat_t const m0 = com_mat_identity();
|
||||
com_mat_t m1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
||||
for (int i = 0; i < 16; ++i)
|
||||
m1.a[i] *= COM_FIX_FRACUNIT;
|
||||
|
||||
com_mat_t const mul = com_mat_mul(m1, m0);
|
||||
com_mat_t const mul_reordered = com_mat_mul_reodered(com_mat_reoder(m1), m0);
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
TEST(mul.a[i] == mul_reordered.a[i]);
|
||||
if (mul.a[i] != mul_reordered.a[i])
|
||||
break;
|
||||
}
|
||||
|
||||
return anyfail;
|
||||
}
|
||||
|
||||
static bool com_test_run(void) {
|
||||
bool anyfail = false;
|
||||
if (com_test_fix()) {
|
||||
com_std_print('c', "com_fix tests failed!");
|
||||
anyfail = true;
|
||||
}
|
||||
if (com_test_bits()) {
|
||||
com_std_print('c', "com_bits tests failed!");
|
||||
anyfail = true;
|
||||
}
|
||||
if (com_test_mat()) {
|
||||
com_std_print('c', "com_mat tests failed!");
|
||||
anyfail = true;
|
||||
}
|
||||
return !anyfail;
|
||||
}
|
||||
|
||||
#undef TEST
|
||||
@@ -93,10 +93,4 @@ static inline com_fix_t com_vec_max(com_vec_t a) {
|
||||
return com_fix_max(com_fix_max(a.a[0], a.a[1]), a.a[2]);
|
||||
}
|
||||
|
||||
static inline void com_vec_print(com_vec_t a) {
|
||||
com_fix_print(a.a[0]);
|
||||
com_fix_print(a.a[1]);
|
||||
com_fix_print(a.a[2]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#ifndef PLR_DISPLAY_H
|
||||
#define PLR_DISPLAY_H
|
||||
|
||||
__attribute__((used)) extern int plr_display_x11_main(int argc, char *argv[]);
|
||||
__attribute__((used)) extern int plr_display_x11_main(int argc,
|
||||
char const *const *argv);
|
||||
__attribute__((used)) extern int plr_display_dos_main(void);
|
||||
|
||||
#endif
|
||||
|
||||
+194
-9
@@ -1,17 +1,16 @@
|
||||
#include "display.h"
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WIDTH 640
|
||||
#define HEIGHT 480
|
||||
#define WIDTH 320
|
||||
#define HEIGHT 200
|
||||
#define MODE_VGA_320_200_8 0x13
|
||||
#define MODE_VESA_640_480_8 0x101
|
||||
|
||||
/*
|
||||
TODO: https://github.com/depp/elf2dos/blob/master/elf/elf.go
|
||||
https://www.edm2.com/index.php/LX_-_Linear_eXecutable_Module_Format_Description
|
||||
Do not use anything but Clang in the end.
|
||||
*/
|
||||
/* https://www.phatcode.net/res/262/files/dpmi09.html */
|
||||
/* http://www.techhelpmanual.com/27-dos__bios___extensions_service_index.html */
|
||||
|
||||
// 8-color VGA palette gradient, lighter shades of lavender to pink
|
||||
#define COLOR_OFFSET 0x50
|
||||
@@ -132,13 +131,183 @@
|
||||
// return NULL;
|
||||
// }
|
||||
|
||||
/* Not really needed i guess? */
|
||||
// static inline bool real_mode_interrupt(uint8_t interrupt) {
|
||||
// bool carry_clear;
|
||||
// __asm__ volatile("int $0x31"
|
||||
// : "=@ccnc"(carry_clear)
|
||||
// : "a"(0x0300), "b"(interrupt), "c"(0), "D"(&dpmi_regs)
|
||||
// : "memory", "cc");
|
||||
// return !carry_clear;
|
||||
// }
|
||||
|
||||
typedef struct {
|
||||
uint16_t rm_segment;
|
||||
uint16_t pm_selector;
|
||||
} LowMemBlock;
|
||||
|
||||
// Allocates low memory under 1MB
|
||||
static inline LowMemBlock allocate_low_mem(uint32_t size_in_bytes) {
|
||||
LowMemBlock block = {0, 0};
|
||||
uint32_t paragraphs =
|
||||
(size_in_bytes + 15) >> 4; // Convert bytes to paragraphs
|
||||
|
||||
uint16_t seg, sel;
|
||||
uint8_t failed = 0;
|
||||
|
||||
__asm__ __volatile__(
|
||||
"movw $0x0100, %%ax \n\t" // DPMI allocate DOS memory block
|
||||
"int $0x31 \n\t"
|
||||
"setc %2 \n\t" // Check Carry Flag for error
|
||||
: "=a"(seg), "=d"(sel), "=r"(failed)
|
||||
: "b"(paragraphs)
|
||||
: "cc");
|
||||
|
||||
if (!failed) {
|
||||
block.rm_segment = seg;
|
||||
block.pm_selector = sel;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
static inline void copy_to_low_mem(LowMemBlock block, char *data,
|
||||
uint32_t size) {
|
||||
__asm__ volatile("movw %0, %%es\n"
|
||||
"movw %1, %%di\n"
|
||||
"movw %2, %%si\n"
|
||||
"movw .data, %%ax\n"
|
||||
"movw %3, %%cx\n"
|
||||
"cld\n"
|
||||
"rep movsb" ::"r"(block.rm_segment),
|
||||
"r"(block.pm_selector), "r"(data), "r"(size)
|
||||
: "memory", "cc", "ax", "bx", "cx", "esi", "di");
|
||||
}
|
||||
|
||||
// Frees the allocated low memory (DPMI Ax=0101h)
|
||||
static inline void free_low_mem(uint16_t selector) {
|
||||
__asm__ __volatile__("movw $0x0101, %%ax \n\t" // DPMI free DOS memory block
|
||||
"int $0x31"
|
||||
:
|
||||
: "d"(selector)
|
||||
: "ax", "cc");
|
||||
}
|
||||
|
||||
static inline void set_video_mode(uint16_t mode) {
|
||||
__asm__ volatile("int $0x10" : : "a"(mode) : "memory", "cc");
|
||||
}
|
||||
|
||||
/* http://www.techhelpmanual.com/439-dos_fn_40h__write_to_file_via_handle.html
|
||||
*/
|
||||
static inline void writetest(void) {
|
||||
__asm__ volatile("int $0x21" : : "a"(0x0200), "d"('Y') : "memory", "cc");
|
||||
}
|
||||
|
||||
/* http://www.techhelpmanual.com/630-int_31h_0800h__physical_address_mapping.html
|
||||
*/
|
||||
static inline void *map_physical_address(uint32_t physical_addr,
|
||||
uint32_t size) {
|
||||
uint16_t high, low;
|
||||
bool carry_clear;
|
||||
__asm__ volatile("int $0x31"
|
||||
: "=b"(high), "=c"(low), "=@ccnc"(carry_clear)
|
||||
: "a"(0x0800), "b"(physical_addr >> 16),
|
||||
"c"(physical_addr & 0xFFFF), "S"(size >> 16),
|
||||
"D"(size & 0xFFFF)
|
||||
: "memory", "cc");
|
||||
return !carry_clear ? NULL : (void *)(intptr_t)((high << 16) | low);
|
||||
}
|
||||
|
||||
static inline uint16_t alloc_ldt_descriptor(void) {
|
||||
bool carry_clear;
|
||||
uint16_t selector;
|
||||
__asm__ volatile("int $0x31"
|
||||
: "=a"(selector), "=@ccnc"(carry_clear)
|
||||
: "a"(0x0000), "c"(1)
|
||||
: "memory", "cc");
|
||||
return !carry_clear ? selector : 0;
|
||||
}
|
||||
|
||||
static inline bool set_segment_base_address(uint16_t selector,
|
||||
uint32_t baseaddr) {
|
||||
bool carry_clear;
|
||||
__asm__ volatile("int $0x31"
|
||||
: "=a"(selector), "=@ccnc"(carry_clear)
|
||||
: "a"(0x0007), "c"(baseaddr >> 16), "d"(baseaddr & 0xFFFF)
|
||||
: "memory", "cc");
|
||||
return !carry_clear;
|
||||
}
|
||||
|
||||
static inline bool set_segment_limit(uint16_t selector, uint32_t size) {
|
||||
bool carry_clear;
|
||||
__asm__ volatile("int $0x31"
|
||||
: "=@ccnc"(carry_clear)
|
||||
: "a"(0x0008), "b"(selector), "c"(size >> 16),
|
||||
"d"(size & 0xFFFF)
|
||||
: "memory", "cc");
|
||||
return !carry_clear;
|
||||
}
|
||||
|
||||
static inline bool set_segment_rights(uint16_t selector, bool read,
|
||||
bool write) {
|
||||
bool carry_clear;
|
||||
uint16_t flags =
|
||||
1 | (write << 1) | (1 << 4) | (1 << 7) | (1 << 14) | (1 << 15);
|
||||
__asm__ volatile("int $0x31"
|
||||
: "=@ccnc"(carry_clear)
|
||||
: "a"(0x0009), "b"(selector), "c"(flags)
|
||||
: "memory", "cc");
|
||||
return !carry_clear;
|
||||
}
|
||||
|
||||
static inline void use_segment(uint16_t selector) {
|
||||
__asm__ volatile("" : "=S"(selector)::);
|
||||
}
|
||||
|
||||
/* https://tippach.hier-im-netz.de/wdosx/int21api.html#f09 */
|
||||
static inline void print_to_console(char *const str) {
|
||||
__asm__ volatile("int $0x21" ::"a"(0x0900), "d"(str)
|
||||
: "memory", "cc", "esi", "edi", "esp");
|
||||
}
|
||||
|
||||
static uint32_t test = 0x1420124;
|
||||
|
||||
// extern int plr_display_dos_main(int argc, char *argv[]) {
|
||||
extern int plr_display_dos_main(void) {
|
||||
char *buf = (char *)0xA0000; // VGA memory address
|
||||
print_to_console("Fuck you stupid shit");
|
||||
return -1;
|
||||
|
||||
uint16_t base = alloc_ldt_descriptor();
|
||||
if (!base) {
|
||||
print_to_console("Cannot allocate LDT descriptor");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!set_segment_base_address(base, 0xA0000)) {
|
||||
print_to_console("Cannot set LDT base address");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!set_segment_limit(base, 0x10000)) {
|
||||
print_to_console("Cannot set LDT limit");
|
||||
return -1;
|
||||
}
|
||||
|
||||
use_segment(base);
|
||||
|
||||
// char *volatile buf = (void *)(intptr_t)0xA0000; // VGA memory address
|
||||
// char *buf = map_physical_address(0xA0000, 0x10000); // VGA memory
|
||||
char *buf = map_physical_address(0xA0000, 0x10000); // VGA memory
|
||||
// address
|
||||
if (!buf)
|
||||
return -1;
|
||||
int x, y;
|
||||
|
||||
// if (!buf)
|
||||
// return -1;
|
||||
|
||||
// unsigned char saved_mode = get_video_mode(); // Save original mode
|
||||
// set_vesa_video_mode(MODE_VESA_640_480_8); // Set our VGA mode
|
||||
set_video_mode(MODE_VGA_320_200_8); // Set our VGA mode
|
||||
|
||||
// buf = find_display_memory();
|
||||
|
||||
@@ -162,9 +331,25 @@ extern int plr_display_dos_main(void) {
|
||||
for (y = 0; y < HEIGHT; y++) {
|
||||
for (x = 0; x < WIDTH; x++) {
|
||||
buf[y * WIDTH + x] = COLOR_OFFSET + y / COLOR_STEP;
|
||||
// __asm__ volatile("int $0x10"
|
||||
// :
|
||||
// : "a"(0x0C << 8 | COLOR_OFFSET + y / COLOR_STEP),
|
||||
// "b"(0), "c"(x), "d"(y)
|
||||
// : "memory", "cc");
|
||||
}
|
||||
}
|
||||
// writetest();
|
||||
__asm__ volatile("int $0x16" : : "a"(0x00) : "memory", "cc");
|
||||
}
|
||||
|
||||
// dpmi_regs = (struct dpmi_regs){.eax = 0x00004C00};
|
||||
// __asm__ volatile("movw $0x0300, %%ax\n"
|
||||
// "movb $0x21, %%bl\n"
|
||||
// "xorb %%bh, %%bh\n"
|
||||
// "xorw %%cx, %%cx\n"
|
||||
// "movl $dpmi_regs, %%edi\n"
|
||||
// "int $0x31" ::
|
||||
// : "memory", "cc", "ax", "bx", "cx", "edi");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+510
-65
@@ -1,6 +1,3 @@
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -8,26 +5,60 @@
|
||||
#include <sys/un.h>
|
||||
|
||||
#include "../../Common/bin.h"
|
||||
#include "../../Common/bits.h"
|
||||
#include "../../Common/buf.h"
|
||||
#include "../../Common/cfg.h"
|
||||
#include "../../Common/def.h"
|
||||
#include "../../Common/lzw.h"
|
||||
#include "../../Common/mat.h"
|
||||
#include "../../Common/std.h"
|
||||
#include "../../Common/str.h"
|
||||
#include "../../Common/test.h"
|
||||
#include "../draw.h"
|
||||
#include "display.h"
|
||||
|
||||
/* https://hereket.com/posts/from-scratch-x11-windowing/ */
|
||||
/* https://gaultier.github.io/blog/write_a_video_game_from_scratch_like_1987.html
|
||||
#if COM_DEF_TARGET == COM_DEF_TARGET_COSMO
|
||||
#include <X.h>
|
||||
#else
|
||||
#include <X11/X.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
https://hereket.com/posts/from-scratch-x11-windowing/
|
||||
https://gaultier.github.io/blog/write_a_video_game_from_scratch_like_1987.html
|
||||
https://xorg.freedesktop.org/archive/X11R7.7-RC1/doc/xproto/x11protocol.html
|
||||
*/
|
||||
|
||||
bool quited = false;
|
||||
static struct {
|
||||
uint32_t id;
|
||||
uint32_t colormap;
|
||||
uint32_t white;
|
||||
uint32_t black;
|
||||
uint32_t input_mask;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint16_t width_mm;
|
||||
uint16_t height_mm;
|
||||
uint16_t maps_min;
|
||||
uint16_t maps_max;
|
||||
uint32_t root_visual_id;
|
||||
uint8_t backing_store;
|
||||
uint8_t save_unders;
|
||||
uint8_t root_depth;
|
||||
uint8_t depths_count;
|
||||
} plr_x11_root_screen;
|
||||
|
||||
bool fileread(void *ctx, char *into, uint32_t bytes) {
|
||||
static struct {
|
||||
uint32_t resource_id_base;
|
||||
uint32_t resource_id_mask;
|
||||
} plr_x11_connection;
|
||||
|
||||
static bool fileread(void *ctx, char *into, uint32_t bytes) {
|
||||
return com_std_read(ctx, into, bytes) == bytes;
|
||||
}
|
||||
|
||||
bool fileskip(void *ctx, uint32_t bytes) { return com_std_sfwd(ctx, bytes); }
|
||||
static bool fileskip(void *ctx, uint32_t bytes) {
|
||||
return com_std_sfwd(ctx, bytes);
|
||||
}
|
||||
|
||||
#define AUTH_ENTRY_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
|
||||
|
||||
@@ -69,11 +100,12 @@ static struct plr_x11_auth_entry {
|
||||
result.success = true;
|
||||
return result;
|
||||
BREAK:
|
||||
com_std_printf("Failure to initialize X11\n");
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool plr_x11_handshake(struct plr_x11_auth_entry auth_entry) {
|
||||
static bool plr_x11_handshake(com_std_fd xfd,
|
||||
struct plr_x11_auth_entry auth_entry) {
|
||||
struct {
|
||||
uint8_t endianness;
|
||||
uint8_t pad0;
|
||||
@@ -83,75 +115,411 @@ static bool plr_x11_handshake(struct plr_x11_auth_entry auth_entry) {
|
||||
uint16_t auth_data_len;
|
||||
uint16_t pad1;
|
||||
} __attribute__((packed)) request = {
|
||||
.endianness = 'l',
|
||||
.endianness = COM_DEF_CPU_LITEND ? 'l' : 'B',
|
||||
.major_version = 11,
|
||||
.auth_len = sizeof(AUTH_ENTRY_MAGIC_COOKIE) - 1, // IS -1 NEEDED?
|
||||
.auth_data_len = auth_entry.auth_data->len,
|
||||
};
|
||||
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return false;
|
||||
if (!com_std_write(xfd, AUTH_ENTRY_MAGIC_COOKIE,
|
||||
sizeof AUTH_ENTRY_MAGIC_COOKIE - 1))
|
||||
return false;
|
||||
/* Use 2 bytes back for padding. */
|
||||
if (!com_std_write(xfd, &auth_entry.auth_data->data[-2],
|
||||
auth_entry.auth_data->len + 2))
|
||||
return false;
|
||||
|
||||
struct {
|
||||
uint8_t success;
|
||||
uint8_t pad1;
|
||||
uint16_t major_version;
|
||||
uint16_t minor_version;
|
||||
uint16_t length;
|
||||
} __attribute__((packed)) response;
|
||||
|
||||
if (!com_std_read(xfd, &response, sizeof response))
|
||||
return false;
|
||||
if (response.success != 1)
|
||||
return false;
|
||||
|
||||
/* https://xorg.freedesktop.org/archive/X11R7.7-RC1/doc/xproto/x11protocol.html#server_response
|
||||
*/
|
||||
struct {
|
||||
uint32_t release_number;
|
||||
uint32_t resource_id_base;
|
||||
uint32_t resource_id_mask;
|
||||
uint32_t motion_buffer_size;
|
||||
uint16_t vendor_length;
|
||||
uint16_t maximum_request_length;
|
||||
uint8_t screens_in_root_count;
|
||||
uint8_t formats_count;
|
||||
uint8_t image_byte_order;
|
||||
uint8_t bitmap_format_bit_order;
|
||||
uint8_t bitmap_format_scanline_unit;
|
||||
uint8_t bitmap_format_scanline_pad;
|
||||
uint8_t min_keycode;
|
||||
uint8_t max_keycode;
|
||||
uint32_t pad2;
|
||||
} __attribute__((packed)) dyn_response;
|
||||
|
||||
if (!com_std_read(xfd, &dyn_response, sizeof dyn_response))
|
||||
return false;
|
||||
|
||||
/* Skip vendor and format info. */
|
||||
char scratch[1024];
|
||||
uint32_t skip = com_bits_nalign(dyn_response.vendor_length, 4);
|
||||
com_std_assert(skip <= sizeof scratch);
|
||||
com_std_assert(8 * dyn_response.formats_count <= sizeof scratch);
|
||||
if (!com_std_read(xfd, scratch, skip))
|
||||
return false;
|
||||
if (!com_std_read(xfd, scratch, 8 * dyn_response.formats_count))
|
||||
return false;
|
||||
/* Read first and hope it is what we need. */
|
||||
if (!com_std_read(xfd, &plr_x11_root_screen, sizeof plr_x11_root_screen))
|
||||
return false;
|
||||
/* Then drop all the things that proceed, such as other screens and their
|
||||
* visual profiles. */
|
||||
while (com_std_has_data(xfd))
|
||||
com_std_read(xfd, scratch, sizeof scratch);
|
||||
|
||||
plr_x11_connection.resource_id_base = dyn_response.resource_id_base;
|
||||
plr_x11_connection.resource_id_mask = dyn_response.resource_id_mask;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: correct error handling.
|
||||
extern int plr_display_x11_main(int argc, char *argv[]) {
|
||||
char test_string[] = "what is this?";
|
||||
char *compressed_string = NULL;
|
||||
uint32_t compressed_string_sz = 0;
|
||||
struct com_lzw_table table =
|
||||
com_lzw_infer_table(test_string, sizeof(test_string));
|
||||
bool test = com_lzw_compress(&table, test_string, sizeof(test_string),
|
||||
&compressed_string, &compressed_string_sz);
|
||||
com_lzw_free_table(&table);
|
||||
com_std_assert(test);
|
||||
union plr_x11_response {
|
||||
struct {
|
||||
uint8_t type;
|
||||
uint8_t errcode;
|
||||
uint16_t sequenceid;
|
||||
uint32_t badval;
|
||||
uint16_t minorop;
|
||||
uint32_t majorop;
|
||||
char pad[18];
|
||||
} __attribute__((packed)) error;
|
||||
struct {
|
||||
uint8_t type;
|
||||
uint8_t data;
|
||||
uint16_t sequenceid;
|
||||
uint32_t length;
|
||||
char pad[24];
|
||||
} __attribute__((packed)) reply;
|
||||
};
|
||||
|
||||
com_mat_t m0 = com_mat_identity();
|
||||
com_mat_t m1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
||||
for (int i = 0; i < 16; ++i)
|
||||
m1.a[i] *= COM_FIX_FRACUNIT;
|
||||
static bool plr_x11_poll(com_std_fd xfd, bool wait,
|
||||
union plr_x11_response *response) {
|
||||
if (!wait && !com_std_has_data(xfd))
|
||||
return false;
|
||||
|
||||
com_mat_t t0 = com_mat_mul(m1, m0);
|
||||
com_mat_t t1 = com_mat_mul_reodered(com_mat_reoder(m1), m0);
|
||||
com_fix_print(t0.a[4]);
|
||||
com_fix_print(t1.a[4]);
|
||||
|
||||
// com_vec_t v0 = com_mat_vec_project(com_mat_reoder(m0), com_vec_identity());
|
||||
// com_fix_print(v0.a[0]);
|
||||
|
||||
// com_fix_run_tests();
|
||||
// com_fix_run_bench();
|
||||
// com_mat_run_bench();
|
||||
|
||||
/* TODO: Move init to separate func. */
|
||||
/* Open X11 socket for communication. */
|
||||
com_std_fd const xfd = com_std_socket(false);
|
||||
if (!xfd) {
|
||||
com_std_printf("Failure to initialize X11\n");
|
||||
return -1;
|
||||
com_std_read(xfd, response, sizeof(union plr_x11_response));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!com_std_connect(xfd, false, "/tmp/.X11-unix/X0")) {
|
||||
com_std_printf("Failure to initialize X11\n");
|
||||
return -1;
|
||||
static bool plr_x11_poll_reply(com_std_fd xfd, uint16_t seqid,
|
||||
union plr_x11_response *response) {
|
||||
while (plr_x11_poll(xfd, true, response)) {
|
||||
/* Wait for match. */
|
||||
if (response->reply.type == 1 && response->reply.sequenceid == seqid)
|
||||
break;
|
||||
|
||||
/* Errors could be encountered here, report along the way. */
|
||||
if (response->reply.type == 0) {
|
||||
com_std_print('c', "X11 error code=", 'u', response->error.errcode, 'c',
|
||||
" for seqid=", 'u', response->error.sequenceid);
|
||||
}
|
||||
}
|
||||
|
||||
/* First we need to authenticate X11 connection, which requires some funk. */
|
||||
return true;
|
||||
}
|
||||
|
||||
/* TODO: Instead we should poll for events, errors are only byproduct. */
|
||||
static bool plr_x11_poll_errors(com_std_fd xfd) {
|
||||
union plr_x11_response response;
|
||||
|
||||
while (plr_x11_poll(xfd, false, &response)) {
|
||||
/* There should be no replies unhandled. */
|
||||
/* TODO: Write down encountered events. */
|
||||
if (response.reply.type == 1)
|
||||
return false;
|
||||
|
||||
if (response.reply.type == 0)
|
||||
com_std_print('c', "X11 error code=", 'u', response.error.errcode, 'c',
|
||||
" for seqid=", 'u', response.error.sequenceid);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline uint32_t plr_x11_next_id(void) {
|
||||
static uint32_t cur_id = 0;
|
||||
cur_id = 1 + ((plr_x11_connection.resource_id_mask & (cur_id)) |
|
||||
plr_x11_connection.resource_id_base);
|
||||
return cur_id;
|
||||
}
|
||||
|
||||
/* Used to number requests. */
|
||||
static inline uint32_t plr_x11_next_seqid(void) {
|
||||
static uint32_t cur_id = 1;
|
||||
return cur_id++;
|
||||
}
|
||||
|
||||
static uint32_t plr_x11_create_graphical_context(com_std_fd xfd) {
|
||||
uint32_t const gc_id = plr_x11_next_id();
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t pad1;
|
||||
uint16_t request_length;
|
||||
uint32_t id;
|
||||
uint32_t drawable;
|
||||
uint32_t bitmask;
|
||||
uint32_t value1;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = 55,
|
||||
.request_length = sizeof request / 4,
|
||||
.id = gc_id,
|
||||
.drawable = plr_x11_root_screen.id,
|
||||
.bitmask = 8, /* TODO: Figure out. */
|
||||
.value1 = 0x00000000,
|
||||
};
|
||||
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return 0;
|
||||
return gc_id;
|
||||
}
|
||||
|
||||
static uint32_t plr_x11_create_window(com_std_fd xfd) {
|
||||
/* TODO: Use the ones provided in headers ? */
|
||||
#define FLAG_WIN_BG_PIXEL 2
|
||||
#define FLAG_WIN_EVENT 0x800
|
||||
#define EVENT_FLAG_EXPOSURE 0x8000
|
||||
#define EVENT_FLAG_KEY_PRESS 0x1
|
||||
#define EVENT_FLAG_KEY_RELEASE 0x2
|
||||
#define EVENT_FLAG_BUTTON_PRESS 0x4
|
||||
#define EVENT_FLAG_BUTTON_RELEASE 0x8
|
||||
|
||||
uint32_t const window_id = plr_x11_next_id();
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t depth;
|
||||
uint16_t request_length;
|
||||
uint32_t window_id;
|
||||
uint32_t parent_id;
|
||||
uint16_t x;
|
||||
uint16_t y;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint16_t border_width;
|
||||
uint16_t class;
|
||||
uint32_t root_visual_id;
|
||||
uint32_t bitmask;
|
||||
uint32_t value1;
|
||||
uint32_t value2;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = 1,
|
||||
.depth = COM_CFG_COLORS * 8,
|
||||
.request_length = sizeof request / 4,
|
||||
.window_id = window_id,
|
||||
.parent_id = plr_x11_root_screen.id,
|
||||
.width = COM_CFG_WIDTH,
|
||||
.height = COM_CFG_HEIGHT,
|
||||
.border_width = 0,
|
||||
.class = 1,
|
||||
.root_visual_id = plr_x11_root_screen.root_visual_id,
|
||||
.bitmask = FLAG_WIN_BG_PIXEL | FLAG_WIN_EVENT,
|
||||
.value1 = 0x00000000,
|
||||
.value2 = EVENT_FLAG_EXPOSURE | EVENT_FLAG_BUTTON_RELEASE |
|
||||
EVENT_FLAG_BUTTON_PRESS | EVENT_FLAG_KEY_PRESS |
|
||||
EVENT_FLAG_KEY_RELEASE,
|
||||
};
|
||||
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return 0;
|
||||
return window_id;
|
||||
}
|
||||
|
||||
static bool plr_x11_show_window(com_std_fd xfd, uint32_t window_id) {
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t pad1;
|
||||
uint16_t request_length;
|
||||
uint32_t window_id;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = 8,
|
||||
.request_length = sizeof request / 4,
|
||||
.window_id = window_id,
|
||||
};
|
||||
return com_std_write(xfd, &request, sizeof request);
|
||||
}
|
||||
|
||||
static uint32_t plr_x11_create_pixmap(com_std_fd xfd, uint32_t window_id) {
|
||||
uint32_t const pixmap_id = plr_x11_next_id();
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t depth;
|
||||
uint16_t request_length;
|
||||
uint32_t pixmap_id;
|
||||
uint32_t drawable;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = 53,
|
||||
.depth = COM_CFG_COLORS * 8,
|
||||
.request_length = sizeof request / 4,
|
||||
.pixmap_id = pixmap_id,
|
||||
.drawable = window_id,
|
||||
.width = COM_CFG_WIDTH,
|
||||
.height = COM_CFG_HEIGHT,
|
||||
};
|
||||
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return 0;
|
||||
return pixmap_id;
|
||||
}
|
||||
|
||||
/* Returns opcode for given extension. */
|
||||
static uint8_t plr_x11_query_extension(com_std_fd xfd, com_str const *ext) {
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
uint16_t const pad = com_bits_nalign(ext->len, 4);
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t pad1;
|
||||
uint16_t request_length;
|
||||
uint16_t name_length;
|
||||
uint16_t pad2;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = 98,
|
||||
.request_length = sizeof request / 4 + pad / 4,
|
||||
.name_length = ext->len,
|
||||
};
|
||||
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return 0;
|
||||
if (!com_std_write(xfd, ext->data, ext->len))
|
||||
return 0;
|
||||
if ((pad - ext->len) && !com_std_write(xfd, ext->data, pad - ext->len))
|
||||
return 0;
|
||||
|
||||
union plr_x11_response response;
|
||||
plr_x11_poll_reply(xfd, seq, &response);
|
||||
|
||||
return response.reply.pad[1];
|
||||
}
|
||||
|
||||
/*
|
||||
This is needed as base X11 doesn't allow passing more than 16bit x 4bytes.
|
||||
https://xorg.freedesktop.org/archive/current/doc/bigreqsproto/bigreq.html
|
||||
*/
|
||||
static bool plr_x11_enable_big_requests(com_std_fd xfd) {
|
||||
static com_str bigreqext = com_str_lit("BIG-REQUESTS");
|
||||
uint8_t const opcode = plr_x11_query_extension(xfd, &bigreqext);
|
||||
|
||||
/* Request the thing. */
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t micode;
|
||||
uint16_t request_length;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = opcode,
|
||||
.request_length = sizeof request / 4,
|
||||
};
|
||||
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return 0;
|
||||
|
||||
/* Read the response. */
|
||||
union plr_x11_response response;
|
||||
plr_x11_poll_reply(xfd, seq, &response);
|
||||
|
||||
uint32_t const limit = *(uint32_t *)response.reply.pad;
|
||||
com_std_print('c', "Request limit: ", 'u', limit);
|
||||
if (limit < COM_CFG_FBSIZE) {
|
||||
com_std_print('c', "Big request can't accommodate for framebuffer.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plr_x11_put_framebuffer(com_std_fd xfd, uint32_t gc_id,
|
||||
uint32_t pixmap_id,
|
||||
uint8_t pixels[COM_CFG_FBSIZE]) {
|
||||
uint32_t const padded = com_bits_nalign(COM_CFG_FBSIZE, 4);
|
||||
uint32_t const seq = plr_x11_next_seqid();
|
||||
|
||||
struct {
|
||||
uint8_t opcode;
|
||||
uint8_t format;
|
||||
uint16_t bigreq; /* Marked zero so that 32bit could be used. */
|
||||
uint32_t request_length;
|
||||
uint32_t drawable_id;
|
||||
uint32_t gc_id;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint16_t dst_x;
|
||||
uint16_t dst_y;
|
||||
uint8_t left_pad;
|
||||
uint8_t depth;
|
||||
uint16_t pad1;
|
||||
} __attribute__((packed)) request = {
|
||||
.opcode = 72,
|
||||
.format = 2, // ZPixmap
|
||||
.request_length = sizeof request / 4 + padded / 4,
|
||||
.drawable_id = pixmap_id,
|
||||
.gc_id = gc_id,
|
||||
.width = COM_CFG_WIDTH,
|
||||
.height = COM_CFG_HEIGHT,
|
||||
.dst_x = 0,
|
||||
.dst_y = 0,
|
||||
.depth = COM_CFG_COLORS * 8,
|
||||
};
|
||||
|
||||
if (!com_std_write(xfd, &request, sizeof request))
|
||||
return false;
|
||||
if (!com_std_write(xfd, pixels, COM_CFG_FBSIZE))
|
||||
return false;
|
||||
/* Use request memory for pad, it doesn't matter what we write. */
|
||||
if ((padded - COM_CFG_FBSIZE) &&
|
||||
!com_std_write(xfd, &pixels, padded - COM_CFG_FBSIZE))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plr_x11_establish_connection(com_std_fd xfd, int argc,
|
||||
char const *const *argv) {
|
||||
com_buf_make_on_stack(char, xauthbuf, 1024, 0);
|
||||
|
||||
/* TODO: Genralize inside std? */
|
||||
#if COM_DEF_TARGET == COM_DEF_TARGET_COSMO
|
||||
char const *xauthpath = getenv("XAUTHORITY");
|
||||
#else
|
||||
/* First we need to authenticate X11 connection, which requires some funk.
|
||||
*/
|
||||
/* We assume that $XAUTHORITY is set and I hope it is true. */
|
||||
char *xauthpath = NULL;
|
||||
for (char **envp = &argv[argc + 1]; *envp != NULL; ++envp) {
|
||||
char const *xauthpath = NULL;
|
||||
for (char const *const *envp = &argv[argc + 1]; *envp != NULL; ++envp) {
|
||||
static const struct com_str xauthlit = com_str_lit("XAUTHORITY=");
|
||||
if (com_str_ext_prefixed(*envp, xauthlit)) {
|
||||
if (com_str_ext_prefixed(*envp, &xauthlit)) {
|
||||
xauthpath = &((*envp)[xauthlit.len]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Open it and parse. */
|
||||
com_std_fd const xauthfd = com_std_open(xauthpath, COM_STD_OPEN_READ);
|
||||
if (xauthfd < 0) {
|
||||
com_std_printf("Failure to initialize X11\n");
|
||||
return -1;
|
||||
if (!xauthfd) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return false;
|
||||
}
|
||||
|
||||
com_buf_make_on_stack(char, xauthbuf, 1024, 0);
|
||||
struct com_bin xauthbin = {
|
||||
.ctx = (void *)(int64_t)xauthfd,
|
||||
.flags = COM_BIN_BIG_ENDIAN,
|
||||
@@ -164,29 +532,89 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
||||
auth_entry = plr_x11_read_auth_entry(&xauthbin, xauthbuf);
|
||||
} while (auth_entry.success && auth_entry.family != 256);
|
||||
if (!auth_entry.success) {
|
||||
com_std_printf("Failure to initialize X11\n");
|
||||
return -1;
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return false;
|
||||
}
|
||||
|
||||
com_std_close(xauthfd);
|
||||
|
||||
/* TODO: Check for auth_entry.name == MAGIC ? */
|
||||
plr_x11_handshake(auth_entry);
|
||||
if (!plr_x11_handshake(xfd, auth_entry)) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return false;
|
||||
}
|
||||
|
||||
com_buf_free(xauthbuf);
|
||||
|
||||
char sendbuf[16 * 1024] = {};
|
||||
char readbuf[16 * 1024] = {};
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: correct error handling.
|
||||
extern int plr_display_x11_main(int argc, char const *const *argv) {
|
||||
char test_string[] = "what is this?";
|
||||
char *compressed_string = NULL;
|
||||
uint32_t compressed_string_sz = 0;
|
||||
struct com_lzw_table table =
|
||||
com_lzw_infer_table(test_string, sizeof(test_string));
|
||||
bool test = com_lzw_compress(&table, test_string, sizeof(test_string),
|
||||
&compressed_string, &compressed_string_sz);
|
||||
com_lzw_free_table(&table);
|
||||
com_std_assert(test);
|
||||
|
||||
/* TODO: Move to separate executable maybe? */
|
||||
#ifndef NDEBUG
|
||||
com_test_run();
|
||||
#endif
|
||||
|
||||
/* Open X11 socket for communication. */
|
||||
com_std_fd const xfd = com_std_socket(false);
|
||||
if (!xfd) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!com_std_connect(xfd, false, "/tmp/.X11-unix/X0")) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!plr_x11_establish_connection(xfd, argc, argv)) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!plr_x11_enable_big_requests(xfd)) {
|
||||
com_std_print('c', "Big requests unsupported for your X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t const gc = plr_x11_create_graphical_context(xfd);
|
||||
if (!gc) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t const win = plr_x11_create_window(xfd);
|
||||
if (!win) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t const fb = plr_x11_create_pixmap(xfd, win);
|
||||
if (!fb) {
|
||||
com_std_print('c', "Failure to initialize X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Display *display = XOpenDisplay(NULL);
|
||||
// if (NULL == display) {
|
||||
// com_std_printf("Failed to initialize display");
|
||||
// com_std_print('c', "Failed to initialize display");
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
// Window root = DefaultRootWindow(display);
|
||||
// if (None == root) {
|
||||
// com_std_printf("No root window found");
|
||||
// com_std_print('c', "No root window found");
|
||||
// XCloseDisplay(display);
|
||||
// return -1;
|
||||
// }
|
||||
@@ -199,7 +627,7 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
||||
// XCreateSimpleWindow(display, root, 0, 0, WIDTH, HEIGHT, 0, 0,
|
||||
// 0xffffffff);
|
||||
// if (None == window) {
|
||||
// com_std_printf("Failed to create window");
|
||||
// com_std_print('c', "Failed to create window");
|
||||
// XCloseDisplay(display);
|
||||
// return -1;
|
||||
// }
|
||||
@@ -233,7 +661,7 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
||||
// pixel_buffer, WIDTH, HEIGHT, 32, 0);
|
||||
|
||||
// if (!ximage) {
|
||||
// com_std_printf("Failed to create XImage\n");
|
||||
// com_std_print('c', "Failed to create XImage");
|
||||
// // com_std_alloc(pixel_buffer, 0);
|
||||
// XCloseDisplay(display);
|
||||
// return -1;
|
||||
@@ -273,7 +701,24 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
||||
|
||||
// XCloseDisplay(display);
|
||||
|
||||
uint8_t *fbm = plr_draw_get_pixel_buffer();
|
||||
plr_draw_screen(fbm);
|
||||
|
||||
if (!plr_x11_put_framebuffer(xfd, gc, fb, fbm)) {
|
||||
com_std_print('c', "Failure to drive X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!plr_x11_show_window(xfd, win)) {
|
||||
com_std_print('c', "Failure to drive X11");
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (!plr_x11_poll_errors(xfd)) {
|
||||
com_std_print('c', "X11 closed connection");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "../Display/display.h"
|
||||
|
||||
extern int main(int argc, char const *const *argv) {
|
||||
return plr_display_x11_main(argc, argv);
|
||||
}
|
||||
+7
-36
@@ -1,47 +1,18 @@
|
||||
.section .data
|
||||
.align 4
|
||||
# TODO: Make sure it's placed under 1 MiB.
|
||||
# The DPMI Register Structure (50 bytes total)
|
||||
# This holds the register states injected into Real Mode.
|
||||
dpmi_regs:
|
||||
.long 0 # edi (offset 0)
|
||||
.long 0 # esi (offset 4)
|
||||
.long 0 # ebp (offset 8)
|
||||
.long 0 # esp / reserved (offset 12)
|
||||
.long 0 # ebx (offset 16)
|
||||
.long 0 # edx (offset 20)
|
||||
.long 0 # ecx (offset 24)
|
||||
dpmi_eax:
|
||||
.long 0 # eax (offset 28)
|
||||
.short 0 # flags(offset 32)
|
||||
.short 0 # es (offset 34)
|
||||
.short 0 # ds (offset 36)
|
||||
.short 0 # fs (offset 38)
|
||||
.short 0 # gs (offset 40)
|
||||
.short 0 # ip (offset 42)
|
||||
.short 0 # cs (offset 44)
|
||||
.short 0 # sp (offset 46)
|
||||
.short 0 # ss (offset 48)
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
|
||||
_start:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
cld
|
||||
|
||||
call plr_display_dos_main
|
||||
|
||||
# 1. Set up the Real-Mode registers inside the DPMI structure
|
||||
# AH = 4Ch (DOS Exit Function), AL = 00h (Return Code 0)
|
||||
movl $0x00004C00, dpmi_eax
|
||||
|
||||
# 2. Call DPMI Simulate Real Mode Interrupt
|
||||
movw $0x0300, %ax # DPMI Function: Simulate Real Mode Interrupt
|
||||
movb $0x21, %bl # Interrupt number to trigger (INT 21h)
|
||||
movb $0x00, %bh # Flags (Must be 0)
|
||||
movw $0x0000, %cx # Number of words to copy to RM stack (0)
|
||||
|
||||
# ES:EDI points to our DPMI register block.
|
||||
# In WDOSX flat model, %es already targets the flat data descriptor.
|
||||
movl $dpmi_regs, %edi
|
||||
|
||||
int $0x31 # Trigger DPMI host to execute the RM interrupt
|
||||
|
||||
# --- Execution halts here as DOS terminates the program ---
|
||||
movb $0x4C, %ah
|
||||
int $0x21
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#include "../Display/display.h"
|
||||
|
||||
extern int main(int argc, char **argv) { plr_display_x11_main(argc, argv); }
|
||||
+66
-31
@@ -1,17 +1,44 @@
|
||||
CC = clang
|
||||
CC ?= clang
|
||||
RELEASE ?= 0 # Optimize.
|
||||
DEBUG ?= 1 # Emit debug info.
|
||||
SQUEEZE ?= 0 # Nuclear option to save space.
|
||||
BITS ?= 64
|
||||
CFLAGS = -Wall -std=gnu99 -flto=thin -ffast-math -fno-ident -fvisibility=hidden -fuse-ld=lld
|
||||
RUN ?= 0
|
||||
|
||||
CFLAGS = -Wall -std=gnu99 -ffast-math -fno-ident -fvisibility=hidden
|
||||
|
||||
# Set cosmopolitan toolchain.
|
||||
ifeq ($(MAKECMDGOALS),cosmo)
|
||||
CC = ./Cosmo/bin/cosmocc -mclang
|
||||
ifeq ($(DEBUG), 1)
|
||||
CFLAGS += -mdbg
|
||||
else
|
||||
CFLAGS += -mtiny
|
||||
endif
|
||||
endif
|
||||
|
||||
# Provide LTO where it's applicable.
|
||||
ifeq ($(CC),clang)
|
||||
ISCLANG = 1
|
||||
CFLAGS += -flto=thin -fuse-ld=lld
|
||||
else
|
||||
ifneq ($(MAKECMDGOALS),cosmo)
|
||||
CFLAGS += -flto
|
||||
endif
|
||||
endif
|
||||
|
||||
# Linux specific options.
|
||||
ifeq ($(MAKECMDGOALS),linux)
|
||||
CFLAGS += -fno-pic -fno-pie
|
||||
ifeq ($(RELEASE), 1)
|
||||
CFLAGS += -Wl,--ignore-data-address-equality -Wl,--ignore-function-address-equality \
|
||||
# CFLAGS += -Wl,--ignore-data-address-equality -Wl,--ignore-function-address-equality \
|
||||
-Wl,--optimize-bb-jumps
|
||||
endif
|
||||
ifeq ($(SQUEEZE), 1)
|
||||
CFLAGS += -Wl,-T,tiny.ld -Wl,--icf=all -Wl,--bp-compression-sort=both \
|
||||
ifeq ($(DEBUG), 0)
|
||||
CFLAGS += -Wl,-T,tiny.ld
|
||||
endif
|
||||
CFLAGS += -Wl,--icf=all -Wl,--bp-compression-sort=both \
|
||||
-Wl,--nmagic
|
||||
endif
|
||||
ifeq ($(BITS), 64)
|
||||
@@ -20,57 +47,65 @@ ifeq ($(MAKECMDGOALS),linux)
|
||||
CFLAGS += -m32 -msse2
|
||||
endif
|
||||
endif
|
||||
|
||||
# WASM specific options.
|
||||
ifeq ($(MAKECMDGOALS),wasm)
|
||||
ifeq ($(RELEASE), 1)
|
||||
CFLAGS += -Wl,--compress-relocations
|
||||
endif
|
||||
endif
|
||||
|
||||
# Separate debug and release switches.
|
||||
ifeq ($(DEBUG), 1)
|
||||
CFLAGS += -g3
|
||||
else
|
||||
CFLAGS += -DNDEBUG -Wl,--strip-all
|
||||
CFLAGS += -DNDEBUG -s
|
||||
endif
|
||||
ifeq ($(RELEASE), 1)
|
||||
CFLAGS += -O3 -fmerge-all-constants -Wl,-O3 \
|
||||
-ffunction-sections -fdata-sections -Wl,--gc-sections \
|
||||
-Wl,-mllvm,--vectorize-slp=true \
|
||||
-ffunction-sections -fdata-sections -Wl,--gc-sections
|
||||
ifeq ($(CC),clang)
|
||||
CFLAGS += -Wl,-mllvm,--vectorize-slp=true \
|
||||
-Wl,-mllvm,--enable-gvn-hoist=true
|
||||
endif
|
||||
# TODO: Just use amalgam build for all?
|
||||
DEPS = ../Common/lzw.h ../Common/mat.h ../Common/vec.h ../Common/fix.h ../Common/def.h ../Common/bits.h \
|
||||
../Common/gem.h \
|
||||
../Common/Timer/time.h \
|
||||
./Display/display.h \
|
||||
./draw.h
|
||||
SRC = ../Common/lzw.c ../Common/fix.c ../Common/mat.c ./draw.c
|
||||
LINUX = ./amalgam.c
|
||||
WASM = ./amalgam.c
|
||||
DOS = maindos.c Display/dos.c
|
||||
endif
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
%.o: %.s $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
# TODO: Make it work with no stdlib.
|
||||
linux: CFLAGS += -static -mbmi2 -nostdlib
|
||||
linux: $(LINUX) | $(SRC)
|
||||
linux: ./amalgam.c
|
||||
$(CC) -o player $^ $(CFLAGS)
|
||||
|
||||
# Note: lld package might need to be downloaded.
|
||||
wasm: CFLAGS += --target=wasm32 -nostdlib -Wl,--no-entry -msimd128 -mbulk-memory
|
||||
wasm:
|
||||
$(CC) -o player.wasm $(WASM) $(CFLAGS)
|
||||
$(CC) -o player.wasm ./amalgam.c $(CFLAGS)
|
||||
|
||||
# todo: Add -mtiny for release without debug
|
||||
cosmo: CFLAGS += -I /usr/include/X11
|
||||
cosmo: ./amalgam.c
|
||||
$(CC) $(CFLAGS) -o ./Out/Cosmo/player $^
|
||||
ifeq ($(RUN), 1)
|
||||
ifeq ($(DEBUG), 1)
|
||||
gdb ./Out/Cosmo/player.com.dbg
|
||||
else
|
||||
./Out/Cosmo/player
|
||||
endif
|
||||
endif
|
||||
|
||||
# https://tippach.hier-im-netz.de/wdosx
|
||||
dos: CFLAGS += -ffreestanding -m32 -fno-PIC -fno-PIE -no-pie -nostdlib -nodefaultlibs
|
||||
dos: CFLAGS += -ffreestanding -m32 -fno-PIC -fno-PIE -nostdlib -nodefaultlibs
|
||||
dos:
|
||||
$(CC) -c ./Main/dos.s -o ./Main/dos.o $(CFLAGS)
|
||||
$(CC) -c ./Main/dos.s -o ./Main/dos.o -m32
|
||||
$(CC) -c ./Display/dos.c -o ./Display/dos.o $(CFLAGS)
|
||||
ld.lld -m elf_i386 --oformat binary -Ttext 0x0 --image-base=0x0 ./Main/dos.o ./Display/dos.o -o dos.bin
|
||||
dosbox -noconsole -c "mount c $(PWD)" -c "c:" -c "WDOSX\STUBIT.EXE DOS.BIN" -exit
|
||||
mv ./DOS.BIN ./DOS.EXE
|
||||
ld.lld -m elf_i386 -T dos.ld ./Main/dos.o ./Display/dos.o -o dos.elf
|
||||
objcopy -O binary --strip-all dos.elf dos.bin
|
||||
cat WDosX/WDOSX.DX dos.bin > DOS.EXE
|
||||
ifeq ($(RUN), 1)
|
||||
ifeq ($(DEBUG), 1)
|
||||
dosbox -c "mount c $(PWD)" -c "c:" -c "WDOSX\WUDEBUG.EXE DOS.EXE"
|
||||
else
|
||||
dosbox -c "mount c $(PWD)" -c "c:" -c "DOS.EXE"
|
||||
endif
|
||||
endif
|
||||
|
||||
clean:
|
||||
rm -r *.o ../*.o
|
||||
|
||||
Executable
BIN
Binary file not shown.
Executable
+8
@@ -0,0 +1,8 @@
|
||||
:;# amalgam of batch and shell scripts as a entry single point
|
||||
|
||||
:; SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
:; $SCRIPT_DIR/ape $SCRIPT_DIR/player
|
||||
:; exit
|
||||
|
||||
@echo off
|
||||
player
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5
-1
@@ -21,8 +21,12 @@
|
||||
#endif
|
||||
#include "../Common/Timer/stub.c"
|
||||
#include "./Display/x11.c"
|
||||
// #include "./Main/libc.c"
|
||||
#elif COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
||||
#include "../Common/Std/stub.c"
|
||||
#include "../Common/Timer/stub.c"
|
||||
#elif COM_DEF_TARGET == COM_DEF_TARGET_COSMO
|
||||
#include "../Common/Std/libc.c"
|
||||
#include "../Common/Timer/posix.c"
|
||||
#include "./Display/x11.c"
|
||||
#include "./Main/cosmo.c"
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x00000;
|
||||
|
||||
/* TODO: Make sure WDOSX.DX is placed first, before .text */
|
||||
textstart = .;
|
||||
.text ALIGN(16) : {
|
||||
*(.text*)
|
||||
}
|
||||
textend = .;
|
||||
|
||||
/* We will need to manually map this. */
|
||||
/* At runtime we can figure this out from mapped code segment. */
|
||||
datastart = .;
|
||||
.rodata : {
|
||||
*(.rodata*)
|
||||
}
|
||||
|
||||
.data : {
|
||||
*(.data*)
|
||||
}
|
||||
dataend = .;
|
||||
|
||||
/* TODO: We need to manually map this, as loader is incapable of doing so with flat binary. */
|
||||
bssstart = .;
|
||||
.bss : {
|
||||
*(.bss*)
|
||||
*(COMMON)
|
||||
}
|
||||
bssend = .;
|
||||
|
||||
/* Will be discarded with objcopy. */
|
||||
.shstrtab : {
|
||||
*(.shstrtab*)
|
||||
}
|
||||
|
||||
/* Discard bloat and unnecessary ELF sections */
|
||||
/DISCARD/ : {
|
||||
*(*)
|
||||
}
|
||||
}
|
||||
+41
-40
@@ -2,70 +2,71 @@
|
||||
#include "../Common/cfg.h"
|
||||
#include "../Common/def.h"
|
||||
#include "../Common/gem.h"
|
||||
#include "../Common/std.h"
|
||||
#include "../Common/time.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
||||
uint8_t plr_draw_buffer[COM_CFG_WIDTH * COM_CFG_HEIGHT * 4];
|
||||
static _Alignas(16) uint8_t plr_draw_buffer[COM_CFG_FBSIZE];
|
||||
|
||||
com_def_export_sym(
|
||||
"plr_draw_get_pixel_buffer") uint8_t *plr_draw_get_pixel_buffer(void) {
|
||||
return plr_draw_buffer;
|
||||
}
|
||||
#endif
|
||||
|
||||
com_def_export_sym("plr_draw_screen") void plr_draw_screen(
|
||||
uint8_t *pixel_buffer) {
|
||||
uint8_t *pixel_buffer_aligned = __builtin_assume_aligned(pixel_buffer, 16);
|
||||
for (int y = 0; y < COM_CFG_HEIGHT; y++) {
|
||||
for (int x = 0; x < COM_CFG_WIDTH; x++) {
|
||||
int pixel_index = (y * COM_CFG_WIDTH + x) * 4;
|
||||
int pixel_index = (y * COM_CFG_WIDTH + x) * COM_CFG_COLORS;
|
||||
|
||||
// Assuming standard Little-Endian 32-bit BGRA format
|
||||
pixel_buffer[pixel_index + 0] = (x) % 256; // Blue
|
||||
pixel_buffer[pixel_index + 1] = (y) % 256; // Green
|
||||
pixel_buffer[pixel_index + 2] = (x + y) % 256; // Red
|
||||
pixel_buffer[pixel_index + 3] = 255; // Alpha/Padding
|
||||
pixel_buffer_aligned[pixel_index + 0] = (x) % 256; // Blue
|
||||
pixel_buffer_aligned[pixel_index + 1] = (y) % 256; // Green
|
||||
pixel_buffer_aligned[pixel_index + 2] = (x + y) % 256; // Red
|
||||
#if COM_CFG_COLORS == 4
|
||||
pixel_buffer_aligned[pixel_index + 3] = 255; // Alpha/Padding
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
com_mat_t proj = com_mat_perspective(COM_CFG_WIDTH, COM_CFG_HEIGHT, 1,
|
||||
100 * COM_FIX_FRACUNIT, COM_FIX_PIHALF);
|
||||
com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
|
||||
com_vec_from(0, COM_FIX_FRACUNIT, 0),
|
||||
com_vec_from(-COM_FIX_FRACUNIT, 0, 0));
|
||||
com_mat_t mvp = com_mat_reoder(com_mat_mul(proj, view));
|
||||
// com_mat_t proj = com_mat_perspective(COM_CFG_WIDTH, COM_CFG_HEIGHT, 1,
|
||||
// 100 * COM_FIX_FRACUNIT,
|
||||
// COM_FIX_PIHALF);
|
||||
// com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
|
||||
// com_vec_from(0, COM_FIX_FRACUNIT, 0),
|
||||
// com_vec_from(-COM_FIX_FRACUNIT, 0, 0));
|
||||
// com_mat_t mvp = com_mat_reoder(com_mat_mul(proj, view));
|
||||
|
||||
com_vec_t v0 = com_vec_from(0 * COM_FIX_FRACUNIT, 0, 1 * COM_FIX_FRACUNIT);
|
||||
com_vec_t v1 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
|
||||
1 * COM_FIX_FRACUNIT);
|
||||
com_vec_t v2 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
|
||||
1 * COM_FIX_FRACUNIT);
|
||||
com_vec_t v3 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
|
||||
-1 * COM_FIX_FRACUNIT);
|
||||
// com_vec_t v0 = com_vec_from(0 * COM_FIX_FRACUNIT, 0, 1 * COM_FIX_FRACUNIT);
|
||||
// com_vec_t v1 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
|
||||
// 1 * COM_FIX_FRACUNIT);
|
||||
// com_vec_t v2 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
|
||||
// 1 * COM_FIX_FRACUNIT);
|
||||
// com_vec_t v3 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
|
||||
// -1 * COM_FIX_FRACUNIT);
|
||||
|
||||
com_pnt_t uv0 = com_pnt_from(0 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
|
||||
com_pnt_t uv1 = com_pnt_from(1 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
|
||||
com_pnt_t uv2 = com_pnt_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT);
|
||||
com_pnt_t uv3 = com_pnt_from(0 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT);
|
||||
// com_pnt_t uv0 = com_pnt_from(0 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
|
||||
// com_pnt_t uv1 = com_pnt_from(1 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
|
||||
// com_pnt_t uv2 = com_pnt_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT);
|
||||
// com_pnt_t uv3 = com_pnt_from(0 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT);
|
||||
|
||||
com_vec_t c0 = com_gem_vec_project_clip(mvp, v0);
|
||||
com_vec_t c1 = com_gem_vec_project_clip(mvp, v1);
|
||||
com_vec_t c2 = com_gem_vec_project_clip(mvp, v2);
|
||||
com_vec_t c3 = com_gem_vec_project_clip(mvp, v3);
|
||||
// com_vec_t c0 = com_gem_vec_project_clip(mvp, v0);
|
||||
// com_vec_t c1 = com_gem_vec_project_clip(mvp, v1);
|
||||
// com_vec_t c2 = com_gem_vec_project_clip(mvp, v2);
|
||||
// com_vec_t c3 = com_gem_vec_project_clip(mvp, v3);
|
||||
|
||||
com_pnt_t p0, p1, p2, p3;
|
||||
uint8_t cc0 = com_gem_clip_vis_project(c0, &p0);
|
||||
uint8_t cc1 = com_gem_clip_vis_project(c1, &p1);
|
||||
uint8_t cc2 = com_gem_clip_vis_project(c2, &p2);
|
||||
uint8_t cc3 = com_gem_clip_vis_project(c3, &p3);
|
||||
// com_pnt_t p0, p1, p2, p3;
|
||||
// uint8_t cc0 = com_gem_clip_vis_project(c0, &p0);
|
||||
// uint8_t cc1 = com_gem_clip_vis_project(c1, &p1);
|
||||
// uint8_t cc2 = com_gem_clip_vis_project(c2, &p2);
|
||||
// uint8_t cc3 = com_gem_clip_vis_project(c3, &p3);
|
||||
|
||||
com_std_printf("%i, %i\n", p0.a[0], p0.a[1]);
|
||||
com_std_printf("%i, %i\n", p1.a[0], p1.a[1]);
|
||||
com_std_printf("%i, %i\n", p2.a[0], p2.a[1]);
|
||||
com_std_printf("%i, %i\n", p3.a[0], p3.a[1]);
|
||||
// com_std_printf("%i, %i\n", p0.a[0], p0.a[1]);
|
||||
// com_std_printf("%i, %i\n", p1.a[0], p1.a[1]);
|
||||
// com_std_printf("%i, %i\n", p2.a[0], p2.a[1]);
|
||||
// com_std_printf("%i, %i\n", p3.a[0], p3.a[1]);
|
||||
|
||||
uint8_t tex[32 * 32 * 3] = {0};
|
||||
uint8_t _Alignas(16) tex[32 * 32 * 3] = {0};
|
||||
for (int y = 0; y < 32; ++y) {
|
||||
for (int x = 0; x < 32; ++x) {
|
||||
// uint32_t idx = com_zord_order(x, y);
|
||||
|
||||
@@ -4,5 +4,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
extern void plr_draw_screen(uint8_t *pixel_buffer);
|
||||
extern uint8_t *plr_draw_get_pixel_buffer(void);
|
||||
|
||||
#endif
|
||||
|
||||
+4
-4
@@ -14,19 +14,19 @@ SECTIONS
|
||||
*(.text*)
|
||||
}
|
||||
|
||||
.rodata : {
|
||||
.rodata ALIGN(16) : {
|
||||
*(.rodata*)
|
||||
}
|
||||
|
||||
.data : {
|
||||
.data ALIGN(16) : {
|
||||
*(.data*)
|
||||
}
|
||||
|
||||
.shstrtab : {
|
||||
.shstrtab ALIGN(16) : {
|
||||
*(.shstrtab*)
|
||||
}
|
||||
|
||||
.bss : {
|
||||
.bss ALIGN(16) : {
|
||||
*(.bss*)
|
||||
*(COMMON)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user