Compare commits
11
Commits
8d502173a2
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5789cafd4e | ||
|
|
964e623644 | ||
|
|
8d2bb5575e | ||
|
|
ba4131abbf | ||
|
|
1ada1a12cc | ||
|
|
fd54d48aca | ||
|
|
b92458862e | ||
|
|
dc594e5e82 | ||
|
|
3db75378c4 | ||
|
|
e7abe1a1d2 | ||
|
|
1e200cc7f2 |
@@ -3,8 +3,16 @@
|
|||||||
**/*.EXE
|
**/*.EXE
|
||||||
**/*.exe
|
**/*.exe
|
||||||
**/*.out
|
**/*.out
|
||||||
|
**/*.elf
|
||||||
|
**/*.bin
|
||||||
|
**/*.BAK
|
||||||
|
**/*.wdl
|
||||||
|
**/*.DX
|
||||||
*.sublime-*
|
*.sublime-*
|
||||||
|
|
||||||
Player/player
|
Player/player
|
||||||
Player/player.wasm
|
Player/player.wasm
|
||||||
|
Player/Cosmo
|
||||||
Master/master
|
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);
|
||||||
|
}
|
||||||
+62
-7
@@ -1,21 +1,76 @@
|
|||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
|
#include "any.c"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
#include <tgmath.h>
|
#include <tgmath.h>
|
||||||
|
|
||||||
void *com_std_alloc(void *ptr, size_t size) {
|
void *com_std_alloc(void *ptr, size_t size) {
|
||||||
return size != 0 ? realloc(ptr, size) : (free(ptr), NULL);
|
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_sqrt(double v) { return sqrt(v); }
|
||||||
double com_std_fabs(double v) { return fabs(v); }
|
double com_std_fabs(double v) { return fabs(v); }
|
||||||
double com_std_sin(double v) { return sin(v); }
|
double com_std_sin(double v) { return sin(v); }
|
||||||
double com_std_cos(double v) { return cos(v); }
|
double com_std_cos(double v) { return cos(v); }
|
||||||
|
|
||||||
|
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"
|
||||||
|
: "w";
|
||||||
|
return fopen(path, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
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, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
com_std_fd com_std_socket(bool isnetwork) {
|
||||||
|
if (!isnetwork) {
|
||||||
|
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
return fdopen(sockfd, "w+b");
|
||||||
|
} else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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};
|
||||||
|
__builtin_memcpy(&addr.sun_path, address, com_str_ext_len(address));
|
||||||
|
return connect(fileno(fd), (struct sockaddr *)&addr, sizeof(addr)) == 0;
|
||||||
|
} else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void com_std_exit(int32_t status) { exit(status); }
|
||||||
|
|
||||||
|
void com_std_close(com_std_fd fd) { fclose(fd); }
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
AMD64 specific Linux std parts.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "linux.c"
|
||||||
|
|
||||||
|
#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 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
|
||||||
|
: flags & COM_STD_OPEN_READ ? O_RDONLY
|
||||||
|
: O_WRONLY;
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile("syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(2), "D"(path), "S"(mode), "d"(0)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
if (ret <= 0) /* Mark NULL, removing error code */
|
||||||
|
ret = 0;
|
||||||
|
return (void *)ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile("syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(1), "D"(fd), "S"(data), "d"(bytes)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
return ret == bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile("syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(0), "D"(fd), "S"(data), "d"(upto)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
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"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(8), "D"(fd), "S"(bytes), "d"(SEEK_CUR)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
return ret >= 0; /* TODO: Check whether offset is bytes offset exactly? */
|
||||||
|
}
|
||||||
|
|
||||||
|
inline com_std_fd com_std_socket(bool isnetwork) {
|
||||||
|
if (!isnetwork) {
|
||||||
|
int32_t sockfd;
|
||||||
|
__asm__ volatile("syscall"
|
||||||
|
: "=a"(sockfd)
|
||||||
|
: "a"(41), "D"(AF_UNIX), "S"(SOCK_STREAM), "d"(0)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
if (sockfd < 0)
|
||||||
|
sockfd = 0;
|
||||||
|
return (void *)(int64_t)sockfd;
|
||||||
|
} else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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};
|
||||||
|
__builtin_memcpy(&addr.sun_path, address, com_str_ext_len(address));
|
||||||
|
int32_t ret;
|
||||||
|
__asm__ volatile("syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(42), "D"(fd), "S"(&addr), "d"(sizeof addr)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
return ret == 0;
|
||||||
|
} else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void com_std_exit(int32_t status) {
|
||||||
|
__asm__ volatile("syscall" : : "a"(60), "D"(status) : "rcx", "r11", "memory");
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void com_std_close(com_std_fd fd) {
|
||||||
|
__asm__ volatile("syscall" : : "a"(3), "D"(fd) : "rcx", "r11", "memory");
|
||||||
|
/* TODO: At least assert for bad descriptor for debug? */
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
AMD64 specific Linux std parts.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "linux.c"
|
||||||
|
|
||||||
|
#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 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
|
||||||
|
: flags & COM_STD_OPEN_READ ? O_RDONLY
|
||||||
|
: O_WRONLY;
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile("int $0x80"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(5), "b"(path), "c"(mode), "d"(0)
|
||||||
|
: "memory", "cc");
|
||||||
|
if (ret <= 0) /* Mark NULL, removing error code */
|
||||||
|
ret = 0;
|
||||||
|
return (void *)ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile("int $0x80"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(4), "b"(fd), "c"(data), "d"(bytes)
|
||||||
|
: "memory", "cc");
|
||||||
|
return ret == bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile("int $0x80"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(3), "b"(fd), "c"(data), "d"(upto)
|
||||||
|
: "memory", "cc");
|
||||||
|
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"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(19), "b"(fd), "c"(bytes), "d"(SEEK_CUR)
|
||||||
|
: "memory", "cc");
|
||||||
|
return ret >= 0; /* TODO: Check whether offset is bytes offset exactly? */
|
||||||
|
}
|
||||||
|
|
||||||
|
inline com_std_fd com_std_socket(bool isnetwork) {
|
||||||
|
if (!isnetwork) {
|
||||||
|
int32_t sockfd;
|
||||||
|
__asm__ volatile("int $0x80"
|
||||||
|
: "=a"(sockfd)
|
||||||
|
: "a"(359), "b"(AF_UNIX), "c"(SOCK_STREAM), "d"(0)
|
||||||
|
: "memory", "cc");
|
||||||
|
if (sockfd < 0)
|
||||||
|
sockfd = 0;
|
||||||
|
return (void *)(int64_t)sockfd;
|
||||||
|
} else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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};
|
||||||
|
__builtin_memcpy(&addr.sun_path, address, com_str_ext_len(address));
|
||||||
|
int32_t ret;
|
||||||
|
__asm__ volatile("int $0x80"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(362), "b"(fd), "c"(&addr), "d"(sizeof addr)
|
||||||
|
: "memory", "cc");
|
||||||
|
return ret == 0;
|
||||||
|
} else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void com_std_exit(int32_t status) {
|
||||||
|
__asm__ volatile("int $0x80" : : "a"(1), "b"(status) : "memory", "cc");
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void com_std_close(com_std_fd fd) {
|
||||||
|
__asm__ volatile("int $0x80" : : "a"(6), "b"(fd) : "memory", "cc");
|
||||||
|
}
|
||||||
+7
-29
@@ -1,38 +1,16 @@
|
|||||||
#include "../def.h"
|
/*
|
||||||
|
Generic linux std parts.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "any.c"
|
||||||
|
#include "nolibc.c"
|
||||||
|
|
||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
#include "../str.h"
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
|
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_syscall(__NR_exit, -1, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: Figure out a way to make it easy to deal with. */
|
|
||||||
void com_std_printf(char const *fmt, ...) {
|
|
||||||
com_std_syscall(1, 1, (intptr_t)fmt, com_str_ext_len(fmt));
|
|
||||||
}
|
|
||||||
|
|
||||||
void com_std_print_str(char const *str, uint32_t len, bool newline) {
|
|
||||||
com_std_syscall(1, 1, (intptr_t)str, len);
|
|
||||||
if (newline)
|
|
||||||
com_std_syscall(1, 1, (intptr_t)"\n", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
|
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
|
||||||
double com_std_fabs(double v) { return __builtin_fabs(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_sin(double v) { return __builtin_sin(v); }
|
||||||
double com_std_cos(double v) { return __builtin_cos(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;
|
||||||
|
}
|
||||||
+22
-5
@@ -1,8 +1,11 @@
|
|||||||
#include "../std.h"
|
#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_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_sqrt(double v) { return __builtin_sqrt(v); }
|
||||||
double com_std_fabs(double v) { return __builtin_fabs(v); }
|
double com_std_fabs(double v) { return __builtin_fabs(v); }
|
||||||
@@ -15,6 +18,20 @@ double com_std_sin(double v) { return 0; }
|
|||||||
double com_std_cos(double v) { return 0; }
|
double com_std_cos(double v) { return 0; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
|
com_std_fd com_std_open(char const *path, uint32_t flags) { return NULL; }
|
||||||
// __builtin_memcpy(dest, src, n);
|
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, 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 const *address) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
void com_std_exit(uint32_t status) {
|
||||||
|
__builtin_trap();
|
||||||
|
while (1) {
|
||||||
|
}
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
void com_std_close(com_std_fd fd) { return; }
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ uint64_t com_timer_count_ns(void) {
|
|||||||
void com_timer_profile(uint64_t starttime, const char *what) {
|
void com_timer_profile(uint64_t starttime, const char *what) {
|
||||||
uint64_t const endtime = com_timer_count_ns();
|
uint64_t const endtime = com_timer_count_ns();
|
||||||
uint64_t const diff = endtime - starttime;
|
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
@@ -14,7 +14,7 @@
|
|||||||
#define COM_BIN_NAT_ENDIAN (1 << 2)
|
#define COM_BIN_NAT_ENDIAN (1 << 2)
|
||||||
|
|
||||||
typedef struct com_bin_interface {
|
typedef struct com_bin_interface {
|
||||||
/* Attempts to read exactly N bytes, returns false if it cannot. */
|
/* Attempts to read exactly N bytes, return false if it cannot. */
|
||||||
bool (*readn)(void *ctx, char *into, uint32_t bytes);
|
bool (*readn)(void *ctx, char *into, uint32_t bytes);
|
||||||
bool (*skipn)(void *ctx, uint32_t bytes);
|
bool (*skipn)(void *ctx, uint32_t bytes);
|
||||||
} com_bin_interface;
|
} com_bin_interface;
|
||||||
|
|||||||
+3
-3
@@ -37,10 +37,10 @@ typedef struct com_buf {
|
|||||||
struct com_buf *p_v = (struct com_buf *)&p_v##_buf; \
|
struct com_buf *p_v = (struct com_buf *)&p_v##_buf; \
|
||||||
p_v->cap = (p_c); \
|
p_v->cap = (p_c); \
|
||||||
p_v->flags = p_f | COM_BUF_STACK; \
|
p_v->flags = p_f | COM_BUF_STACK; \
|
||||||
p_v->typesz = sizeof(p_t);
|
p_v->typesz = sizeof(p_t)
|
||||||
|
|
||||||
#define com_buf_index(p_t, p_v, p_i) \
|
#define com_buf_index_as(p_t, p_v, p_i) \
|
||||||
(p_t *)(&(p_v)->data[(p_v)->typesz * (p_i)])
|
(p_t *)(&(p_v)->data[sizeof(p_t) * (p_i)])
|
||||||
|
|
||||||
/* Dynamic allocation to persist no matter the function flow. */
|
/* Dynamic allocation to persist no matter the function flow. */
|
||||||
struct com_buf *com_buf_make(uint32_t cap, uint32_t flags);
|
struct com_buf *com_buf_make(uint32_t cap, uint32_t flags);
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
Configuration file.
|
Configuration file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* TODO: Move to root or Shared/ folder? */
|
||||||
|
|
||||||
#ifndef COM_CFG_H
|
#ifndef COM_CFG_H
|
||||||
#define COM_CFG_H
|
#define COM_CFG_H
|
||||||
|
|
||||||
@@ -9,4 +11,12 @@
|
|||||||
#define COM_CFG_WIDTH 640
|
#define COM_CFG_WIDTH 640
|
||||||
#define COM_CFG_HEIGHT 480
|
#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
|
#endif
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
#ifndef COM_DEF_H
|
#ifndef COM_DEF_H
|
||||||
#define 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_CLANG 1
|
||||||
#define COM_DEF_COMPILER_GCC 2
|
#define COM_DEF_COMPILER_GCC 2
|
||||||
|
|
||||||
@@ -17,6 +20,7 @@
|
|||||||
|
|
||||||
#define COM_DEF_TARGET_LINUX 1
|
#define COM_DEF_TARGET_LINUX 1
|
||||||
#define COM_DEF_TARGET_WASM 2
|
#define COM_DEF_TARGET_WASM 2
|
||||||
|
#define COM_DEF_TARGET_COSMO 3
|
||||||
|
|
||||||
#if defined(__wasm__)
|
#if defined(__wasm__)
|
||||||
#define COM_DEF_TARGET COM_DEF_TARGET_WASM
|
#define COM_DEF_TARGET COM_DEF_TARGET_WASM
|
||||||
@@ -24,6 +28,9 @@
|
|||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
#define COM_DEF_TARGET COM_DEF_TARGET_LINUX
|
#define COM_DEF_TARGET COM_DEF_TARGET_LINUX
|
||||||
#define com_def_export_sym(m_s) __attribute__((visibility("default")))
|
#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
|
#else
|
||||||
#error Unknown target.
|
#error Unknown target.
|
||||||
#endif
|
#endif
|
||||||
@@ -31,6 +38,7 @@
|
|||||||
#define COM_DEF_CPU_AMD64 1
|
#define COM_DEF_CPU_AMD64 1
|
||||||
#define COM_DEF_CPU_X86 2
|
#define COM_DEF_CPU_X86 2
|
||||||
#define COM_DEF_CPU_WASM32 3
|
#define COM_DEF_CPU_WASM32 3
|
||||||
|
#define COM_DEF_CPU_AARCH64 4
|
||||||
|
|
||||||
#if defined(__wasm__)
|
#if defined(__wasm__)
|
||||||
#define COM_DEF_CPU COM_DEF_CPU_WASM32
|
#define COM_DEF_CPU COM_DEF_CPU_WASM32
|
||||||
@@ -44,6 +52,10 @@
|
|||||||
#define COM_DEF_CPU COM_DEF_CPU_X86
|
#define COM_DEF_CPU COM_DEF_CPU_X86
|
||||||
#define COM_DEF_ALIGNMENT 16
|
#define COM_DEF_ALIGNMENT 16
|
||||||
#define COM_DEF_CPU_LITEND 1
|
#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
|
#else
|
||||||
#error Unknown cpu.
|
#error Unknown cpu.
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -71,20 +71,6 @@ com_fix_t com_fix_sqrt(com_fix_t a) {
|
|||||||
: (int32_t)(x >> (-final_shift));
|
: (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) {
|
void com_fix_run_bench(void) {
|
||||||
uint64_t start = com_timer_count_ns();
|
uint64_t start = com_timer_count_ns();
|
||||||
for (int i = 5000000; i--;) {
|
for (int i = 5000000; i--;) {
|
||||||
@@ -118,77 +104,3 @@ void com_fix_run_bench(void) {
|
|||||||
}
|
}
|
||||||
com_timer_profile(start, "fixed: sincos");
|
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];
|
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_tests(void);
|
||||||
void com_fix_run_bench(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_sub(com_fix_t a, com_fix_t b) { return a - b; }
|
||||||
|
|
||||||
static inline com_fix_t com_fix_int(com_fix_t a) {
|
static inline int16_t com_fix_int(com_fix_t a) { return a >> COM_FIX_FRACBITS; }
|
||||||
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) {
|
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);
|
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;
|
mask ^= COM_GEM_VERTEX_CLIPPED_X;
|
||||||
if (a.a[1] < -a.a[2] || a.a[1] > a.a[2])
|
if (a.a[1] < -a.a[2] || a.a[1] > a.a[2])
|
||||||
mask ^= COM_GEM_VERTEX_CLIPPED_Y;
|
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]),
|
out->a[0] = (com_fix_mul(com_fix_div(a.a[0], a.a[2]),
|
||||||
COM_CFG_WIDTH * COM_FIX_FRACUNIT) >>
|
COM_CFG_WIDTH * COM_FIX_FRACUNIT) >>
|
||||||
COM_FIX_FRACBITS) +
|
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_CFG_HEIGHT * COM_FIX_FRACUNIT) >>
|
||||||
COM_FIX_FRACBITS) +
|
COM_FIX_FRACBITS) +
|
||||||
(COM_CFG_HEIGHT / 2);
|
(COM_CFG_HEIGHT / 2);
|
||||||
|
|
||||||
return mask;
|
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
|
// If the point is inside or on all edges, draw the pixel
|
||||||
// (Assumes Counter Clockwise vertex ordering)
|
// (Assumes Counter Clockwise vertex ordering)
|
||||||
if (ws.a[0] >= 0 && ws.a[1] >= 0 && ws.a[2] >= 0) {
|
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) * COM_CFG_COLORS + 0] = 125;
|
||||||
out[(y * COM_CFG_WIDTH + x) * 4 + 1] = 125;
|
out[(y * COM_CFG_WIDTH + x) * COM_CFG_COLORS + 1] = 125;
|
||||||
out[(y * COM_CFG_WIDTH + x) * 4 + 2] = 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 = com_zord_order(x, y);
|
||||||
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
||||||
|
|
||||||
out[bidx * 4 + 0] = tex[idx * 3 + 0];
|
out[bidx * COM_CFG_COLORS + 0] = tex[idx * 3 + 0];
|
||||||
out[bidx * 4 + 1] = tex[idx * 3 + 1];
|
out[bidx * COM_CFG_COLORS + 1] = tex[idx * 3 + 1];
|
||||||
out[bidx * 4 + 2] = tex[idx * 3 + 2];
|
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++) {
|
for (int x = x1; x < x2; x++) {
|
||||||
uint32_t idx = 0 * 32 + 0;
|
uint32_t idx = 0 * 32 + 0;
|
||||||
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
uint32_t bidx = y * COM_CFG_WIDTH + x;
|
||||||
out[bidx * 4 + 0] = tex[idx * 3 + 0];
|
out[bidx * COM_CFG_COLORS + 0] = tex[idx * 3 + 0];
|
||||||
out[bidx * 4 + 1] = tex[idx * 3 + 1];
|
out[bidx * COM_CFG_COLORS + 1] = tex[idx * 3 + 1];
|
||||||
out[bidx * 4 + 2] = tex[idx * 3 + 2];
|
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])}};
|
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
|
#endif
|
||||||
|
|||||||
+61
-14
@@ -10,30 +10,77 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#if COM_DEF_TARGET == COM_DEF_TARGET_LINUX
|
#if COM_DEF_TARGET != COM_DEF_TARGET_COSMO
|
||||||
#include <sys/syscall.h>
|
#define COM_STD_INFD (void *)0
|
||||||
|
#define COM_STD_OUTFD (void *)1
|
||||||
static inline long com_std_syscall(long number, long arg1, long arg2,
|
#else
|
||||||
long arg3) {
|
/* TODO: Make it more graceful, probably with static func. */
|
||||||
long ret;
|
#include <unistd.h>
|
||||||
__asm__ volatile("syscall"
|
#define COM_STD_INFD (fdopen(STDIN_FILENO, "r"))
|
||||||
: "=a"(ret)
|
#define COM_STD_OUTFD (fdopen(STDOUT_FILENO, "w"))
|
||||||
: "a"(number), "D"(arg1), "S"(arg2), "d"(arg3)
|
|
||||||
: "rcx", "r11", "memory");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
#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 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 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. */
|
/* TODO: Remove, use buffer semantics instead. */
|
||||||
|
/* Instead have a mmap functions here. */
|
||||||
/* Note: Freeing is done by passing 0 size to pointer. */
|
/* Note: Freeing is done by passing 0 size to pointer. */
|
||||||
void *com_std_alloc(void *ptr, size_t size);
|
void *com_std_alloc(void *ptr, size_t size);
|
||||||
|
|
||||||
/* Note: Functions listed below are only available for dev builds. */
|
/* Note: Functions listed below are only available for dev builds. */
|
||||||
/* Trigonometry functions are for reference testing of our own fixed point
|
/* Trigonometry functions are for reference testing of our own fixed point
|
||||||
* implementations. Do not use otherwise. */
|
* implementations. Do not use otherwise. */
|
||||||
void com_std_assert(bool holds);
|
#define com_std_assert(p_h) \
|
||||||
void com_std_printf(char const *fmt, ...);
|
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);
|
void com_std_print_str(char const *str, uint32_t len, bool newline);
|
||||||
|
|
||||||
double com_std_sqrt(double v);
|
double com_std_sqrt(double v);
|
||||||
double com_std_fabs(double v);
|
double com_std_fabs(double v);
|
||||||
double com_std_sin(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'. */
|
/* Returns true if string 'a' is prefixed by 'b'. */
|
||||||
static inline bool com_str_ext_prefixed(char const *__restrict a,
|
static inline bool com_str_ext_prefixed(char const *__restrict a,
|
||||||
struct com_str prefix) {
|
struct com_str const *prefix) {
|
||||||
uint32_t n = prefix.len;
|
uint32_t n = prefix->len;
|
||||||
char *b = prefix.data;
|
char *b = prefix->data;
|
||||||
while (n && *a && (*a == *b)) {
|
while (n && *a && (*a == *b)) {
|
||||||
++a;
|
++a;
|
||||||
++b;
|
++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]);
|
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
|
#endif
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#ifndef PLR_DISPLAY_H
|
#ifndef PLR_DISPLAY_H
|
||||||
#define 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,
|
||||||
extern int plr_display_dos_main(int argc, char *argv[]);
|
char const *const *argv);
|
||||||
|
__attribute__((used)) extern int plr_display_dos_main(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+309
-106
@@ -1,152 +1,355 @@
|
|||||||
#include "display.h"
|
#include "display.h"
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define WIDTH 640
|
#define WIDTH 320
|
||||||
#define HEIGHT 480
|
#define HEIGHT 200
|
||||||
#define MODE_VGA_320_200_8 0x13
|
#define MODE_VGA_320_200_8 0x13
|
||||||
#define MODE_VESA_640_480_8 0x101
|
#define MODE_VESA_640_480_8 0x101
|
||||||
|
|
||||||
|
/* 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
|
// 8-color VGA palette gradient, lighter shades of lavender to pink
|
||||||
#define COLOR_OFFSET 0x50
|
#define COLOR_OFFSET 0x50
|
||||||
#define COLOR_NUM 8
|
#define COLOR_NUM 8
|
||||||
#define COLOR_STEP (HEIGHT / COLOR_NUM)
|
#define COLOR_STEP (HEIGHT / COLOR_NUM)
|
||||||
|
|
||||||
/* https://wiki.osdev.org/VESA_Video_Modes */
|
// /* https://wiki.osdev.org/VESA_Video_Modes */
|
||||||
_Packed struct VbeInfoBlock {
|
// _Packed struct VbeInfoBlock {
|
||||||
char VbeSignature[4]; // == "VESA"
|
// char VbeSignature[4]; // == "VESA"
|
||||||
uint16_t VbeVersion; // == 0x0300 for VBE 3.0
|
// uint16_t VbeVersion; // == 0x0300 for VBE 3.0
|
||||||
uint16_t OemStringPtr[2]; // isa vbeFarPtr
|
// uint16_t OemStringPtr[2]; // isa vbeFarPtr
|
||||||
uint8_t Capabilities[4];
|
// uint8_t Capabilities[4];
|
||||||
uint16_t VideoModePtr[2]; // isa vbeFarPtr
|
// uint16_t VideoModePtr[2]; // isa vbeFarPtr
|
||||||
uint16_t TotalMemory; // as # of 64KB blocks
|
// uint16_t TotalMemory; // as # of 64KB blocks
|
||||||
uint8_t Reserved[492];
|
// uint8_t Reserved[492];
|
||||||
};
|
// };
|
||||||
|
|
||||||
_Packed struct ModeInfoBlock {
|
// _Packed struct ModeInfoBlock {
|
||||||
uint16_t
|
// uint16_t
|
||||||
attributes; // deprecated, only bit 7 should be of interest to you, and it
|
// attributes; // deprecated, only bit 7 should be of interest to you, and
|
||||||
// indicates the mode supports a linear frame buffer.
|
// it
|
||||||
uint8_t window_a; // deprecated
|
// // indicates the mode supports a linear frame buffer.
|
||||||
uint8_t window_b; // deprecated
|
// uint8_t window_a; // deprecated
|
||||||
uint16_t granularity; // deprecated; used while calculating bank numbers
|
// uint8_t window_b; // deprecated
|
||||||
uint16_t window_size;
|
// uint16_t granularity; // deprecated; used while calculating bank numbers
|
||||||
uint16_t segment_a;
|
// uint16_t window_size;
|
||||||
uint16_t segment_b;
|
// uint16_t segment_a;
|
||||||
uint32_t win_func_ptr; // deprecated; used to switch banks from protected mode
|
// uint16_t segment_b;
|
||||||
// without returning to real mode
|
// uint32_t win_func_ptr; // deprecated; used to switch banks from protected
|
||||||
uint16_t pitch; // number of bytes per horizontal line
|
// mode
|
||||||
uint16_t width; // width in pixels
|
// // without returning to real mode
|
||||||
uint16_t height; // height in pixels
|
// uint16_t pitch; // number of bytes per horizontal line
|
||||||
uint8_t w_char; // unused...
|
// uint16_t width; // width in pixels
|
||||||
uint8_t y_char; // ...
|
// uint16_t height; // height in pixels
|
||||||
uint8_t planes;
|
// uint8_t w_char; // unused...
|
||||||
uint8_t bpp; // bits per pixel in this mode
|
// uint8_t y_char; // ...
|
||||||
uint8_t banks; // deprecated; total number of banks in this mode
|
// uint8_t planes;
|
||||||
uint8_t memory_model;
|
// uint8_t bpp; // bits per pixel in this mode
|
||||||
uint8_t bank_size; // deprecated; size of a bank, almost always 64 KB but may
|
// uint8_t banks; // deprecated; total number of banks in this mode
|
||||||
// be 16 KB...
|
// uint8_t memory_model;
|
||||||
uint8_t image_pages;
|
// uint8_t bank_size; // deprecated; size of a bank, almost always 64 KB but
|
||||||
uint8_t reserved0;
|
// may
|
||||||
|
// // be 16 KB...
|
||||||
|
// uint8_t image_pages;
|
||||||
|
// uint8_t reserved0;
|
||||||
|
|
||||||
uint8_t red_mask;
|
// uint8_t red_mask;
|
||||||
uint8_t red_position;
|
// uint8_t red_position;
|
||||||
uint8_t green_mask;
|
// uint8_t green_mask;
|
||||||
uint8_t green_position;
|
// uint8_t green_position;
|
||||||
uint8_t blue_mask;
|
// uint8_t blue_mask;
|
||||||
uint8_t blue_position;
|
// uint8_t blue_position;
|
||||||
uint8_t reserved_mask;
|
// uint8_t reserved_mask;
|
||||||
uint8_t reserved_position;
|
// uint8_t reserved_position;
|
||||||
uint8_t direct_color_attributes;
|
// uint8_t direct_color_attributes;
|
||||||
|
|
||||||
uint32_t framebuffer; // physical address of the linear frame buffer; write
|
// uint32_t framebuffer; // physical address of the linear frame buffer; write
|
||||||
// here to draw to the screen
|
// // here to draw to the screen
|
||||||
uint32_t off_screen_mem_off;
|
// uint32_t off_screen_mem_off;
|
||||||
uint16_t off_screen_mem_size; // size of memory in the framebuffer but not
|
// uint16_t off_screen_mem_size; // size of memory in the framebuffer but not
|
||||||
// being displayed on the screen
|
// // being displayed on the screen
|
||||||
uint8_t reserved1[206];
|
// uint8_t reserved1[206];
|
||||||
};
|
// };
|
||||||
|
|
||||||
static char get_video_mode();
|
// static char get_video_mode();
|
||||||
#pragma aux get_video_mode = "mov ah, 0x0f" \
|
// #pragma aux get_video_mode = "mov ah, 0x0f" \
|
||||||
"int 0x10" value[al] modify[ah];
|
// "int 0x10" value[al] modify[ah];
|
||||||
|
|
||||||
static void set_video_mode(unsigned char);
|
// static void set_video_mode(unsigned char);
|
||||||
#pragma aux set_video_mode = "mov ah, 0x00" \
|
// #pragma aux set_video_mode = "mov ah, 0x00" \
|
||||||
"int 0x10" parm[al] modify[ah];
|
// "int 0x10" parm[al] modify[ah];
|
||||||
|
|
||||||
static void set_vesa_video_mode(unsigned short);
|
// static void set_vesa_video_mode(unsigned short);
|
||||||
#pragma aux set_vesa_video_mode = "mov ax, 0x4f02" \
|
// #pragma aux set_vesa_video_mode = "mov ax, 0x4f02" \
|
||||||
"int 0x10" parm[bx] modify[ah];
|
// "int 0x10" parm[bx] modify[ah];
|
||||||
|
|
||||||
static void wait_for_key();
|
// static void wait_for_key();
|
||||||
#pragma aux wait_for_key = "mov ah, 0x00" \
|
// #pragma aux wait_for_key = "mov ah, 0x00" \
|
||||||
"int 0x16" modify[ah];
|
// "int 0x16" modify[ah];
|
||||||
|
|
||||||
static short get_vesa_controller_info(struct VbeInfoBlock *);
|
// static short get_vesa_controller_info(struct VbeInfoBlock *);
|
||||||
#pragma aux get_vesa_controller_info = "mov ax, 0x4f00" \
|
// #pragma aux get_vesa_controller_info = "mov ax, 0x4f00" \
|
||||||
"int 0x10" parm[es di] value[ax];
|
// "int 0x10" parm[es di] value[ax];
|
||||||
|
|
||||||
static short get_vesa_mode_info(uint16_t mode, struct ModeInfoBlock *);
|
// static short get_vesa_mode_info(uint16_t mode, struct ModeInfoBlock *);
|
||||||
#pragma aux get_vesa_mode_info = "mov ax, 0x4F01" \
|
// #pragma aux get_vesa_mode_info = "mov ax, 0x4F01" \
|
||||||
"int 0x10" parm[cx][es di] value[ax];
|
// "int 0x10" parm[cx][es di] value[ax];
|
||||||
|
|
||||||
/* https://wiki.osdev.org/User:Omarrx024/VESA_Tutorial*/
|
// /* https://wiki.osdev.org/User:Omarrx024/VESA_Tutorial*/
|
||||||
static char *find_display_memory(void) {
|
// static char *find_display_memory(void) {
|
||||||
struct VbeInfoBlock *ctrl = (struct VbeInfoBlock *)0x2000;
|
// struct VbeInfoBlock *ctrl = (struct VbeInfoBlock *)0x2000;
|
||||||
struct ModeInfoBlock *inf = (struct ModeInfoBlock *)0x3000;
|
// struct ModeInfoBlock *inf = (struct ModeInfoBlock *)0x3000;
|
||||||
uint16_t *modes;
|
// uint16_t *modes;
|
||||||
int i;
|
// int i;
|
||||||
|
|
||||||
strncpy(ctrl->VbeSignature, "VBE2", 4);
|
// strncpy(ctrl->VbeSignature, "VBE2", 4);
|
||||||
if (get_vesa_controller_info(ctrl) != 0x004F)
|
// if (get_vesa_controller_info(ctrl) != 0x004F)
|
||||||
return NULL;
|
// return NULL;
|
||||||
|
|
||||||
modes = (uint16_t *)(ctrl->VideoModePtr);
|
// modes = (uint16_t *)(ctrl->VideoModePtr);
|
||||||
for (i = 0; modes[i] != 0xFFFF; ++i) {
|
// for (i = 0; modes[i] != 0xFFFF; ++i) {
|
||||||
if (get_vesa_mode_info(modes[i], inf) != 0x004F)
|
// if (get_vesa_mode_info(modes[i], inf) != 0x004F)
|
||||||
continue;
|
// continue;
|
||||||
|
|
||||||
// Check if this is a graphics mode with linear frame buffer support
|
// // Check if this is a graphics mode with linear frame buffer support
|
||||||
if ((inf->attributes & 0x80) != 0x80)
|
// if ((inf->attributes & 0x80) != 0x80)
|
||||||
continue;
|
// continue;
|
||||||
|
|
||||||
// Check if this is a packed pixel or direct color mode
|
// // Check if this is a packed pixel or direct color mode
|
||||||
if (inf->memory_model != 4 && inf->memory_model != 6)
|
// if (inf->memory_model != 4 && inf->memory_model != 6)
|
||||||
continue;
|
// continue;
|
||||||
|
|
||||||
// Check if this is exactly the mode we're looking for
|
// // Check if this is exactly the mode we're looking for
|
||||||
if (WIDTH == inf->width && HEIGHT == inf->height && 256 == inf->pitch)
|
// if (WIDTH == inf->width && HEIGHT == inf->height && 256 == inf->pitch)
|
||||||
return (char *)inf->framebuffer;
|
// return (char *)inf->framebuffer;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 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;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern int plr_display_dos_main(int argc, char *argv[]) {
|
static inline void copy_to_low_mem(LowMemBlock block, char *data,
|
||||||
char far *buf = (char far *)0xA0000; // VGA memory address
|
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) {
|
||||||
|
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;
|
int x, y;
|
||||||
|
|
||||||
unsigned char saved_mode = get_video_mode(); // Save original mode
|
// if (!buf)
|
||||||
set_vesa_video_mode(MODE_VESA_640_480_8); // Set our VGA mode
|
// return -1;
|
||||||
|
|
||||||
buf = find_display_memory();
|
// 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();
|
||||||
|
|
||||||
/* TODO: Fetch info about the mode, to know the availability */
|
/* TODO: Fetch info about the mode, to know the availability */
|
||||||
/* as well as base address in memory */
|
/* as well as base address in memory */
|
||||||
/* https://wiki.osdev.org/VESA_Video_Modes */
|
/* https://wiki.osdev.org/VESA_Video_Modes */
|
||||||
|
|
||||||
// Now draw. We draw pixel-by-pixel by directly setting the video memory like
|
// Now draw. We draw pixel-by-pixel by directly setting the video memory
|
||||||
// it was an array. We step the color every 25 pixels so that we have 8 bands
|
// like it was an array. We step the color every 25 pixels so that we have 8
|
||||||
// of even height.
|
// bands of even height.
|
||||||
|
// for (y = 0; y < HEIGHT; y++) {
|
||||||
|
// for (x = 0; x < WIDTH; x++) {
|
||||||
|
// buf[y * WIDTH + x] = COLOR_OFFSET + y / COLOR_STEP;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// wait_for_key();
|
||||||
|
// set_video_mode(saved_mode); // Restore original mode
|
||||||
|
|
||||||
|
while (1) {
|
||||||
for (y = 0; y < HEIGHT; y++) {
|
for (y = 0; y < HEIGHT; y++) {
|
||||||
for (x = 0; x < WIDTH; x++) {
|
for (x = 0; x < WIDTH; x++) {
|
||||||
buf[y * WIDTH + x] = COLOR_OFFSET + y / COLOR_STEP;
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
wait_for_key();
|
// dpmi_regs = (struct dpmi_regs){.eax = 0x00004C00};
|
||||||
set_video_mode(saved_mode); // Restore original mode
|
// __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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+545
-78
@@ -1,6 +1,3 @@
|
|||||||
#include <X11/X.h>
|
|
||||||
#include <X11/Xlib.h>
|
|
||||||
#include <X11/Xutil.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -8,29 +5,63 @@
|
|||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
|
||||||
#include "../../Common/bin.h"
|
#include "../../Common/bin.h"
|
||||||
|
#include "../../Common/bits.h"
|
||||||
#include "../../Common/buf.h"
|
#include "../../Common/buf.h"
|
||||||
#include "../../Common/cfg.h"
|
#include "../../Common/cfg.h"
|
||||||
|
#include "../../Common/def.h"
|
||||||
#include "../../Common/lzw.h"
|
#include "../../Common/lzw.h"
|
||||||
#include "../../Common/mat.h"
|
|
||||||
#include "../../Common/std.h"
|
#include "../../Common/std.h"
|
||||||
#include "../../Common/str.h"
|
#include "../../Common/str.h"
|
||||||
|
#include "../../Common/test.h"
|
||||||
#include "../draw.h"
|
#include "../draw.h"
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
|
|
||||||
/* https://hereket.com/posts/from-scratch-x11-windowing/ */
|
#if COM_DEF_TARGET == COM_DEF_TARGET_COSMO
|
||||||
/* https://gaultier.github.io/blog/write_a_video_game_from_scratch_like_1987.html
|
#include <X.h>
|
||||||
*/
|
#else
|
||||||
|
#include <X11/X.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
bool quited = false;
|
/*
|
||||||
|
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 fileread(void *ctx, char *into, uint32_t bytes) {
|
static struct {
|
||||||
return com_std_syscall(0, (intptr_t)ctx, (intptr_t)into, bytes);
|
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;
|
||||||
|
|
||||||
|
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) {
|
static bool fileskip(void *ctx, uint32_t bytes) {
|
||||||
return com_std_syscall(8, (intptr_t)ctx, bytes, 1) >= 0;
|
return com_std_sfwd(ctx, bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define AUTH_ENTRY_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
|
||||||
|
|
||||||
static struct plr_x11_auth_entry {
|
static struct plr_x11_auth_entry {
|
||||||
uint16_t family;
|
uint16_t family;
|
||||||
struct com_buf *auth_name;
|
struct com_buf *auth_name;
|
||||||
@@ -39,10 +70,8 @@ static struct plr_x11_auth_entry {
|
|||||||
} plr_x11_read_auth_entry(struct com_bin *b, struct com_buf *buf) {
|
} plr_x11_read_auth_entry(struct com_bin *b, struct com_buf *buf) {
|
||||||
struct plr_x11_auth_entry result = {0};
|
struct plr_x11_auth_entry result = {0};
|
||||||
|
|
||||||
if (!com_bin_read_u16(b, &result.family)) {
|
if (!com_bin_read_u16(b, &result.family))
|
||||||
com_std_printf("Failure to initialize X11\n");
|
|
||||||
goto BREAK;
|
goto BREAK;
|
||||||
}
|
|
||||||
uint16_t addres_len;
|
uint16_t addres_len;
|
||||||
if (!com_bin_read_u16(b, &addres_len))
|
if (!com_bin_read_u16(b, &addres_len))
|
||||||
goto BREAK;
|
goto BREAK;
|
||||||
@@ -63,20 +92,465 @@ static struct plr_x11_auth_entry {
|
|||||||
if (!com_bin_read_u16(b, &data_len))
|
if (!com_bin_read_u16(b, &data_len))
|
||||||
goto BREAK;
|
goto BREAK;
|
||||||
uint32_t data_idx;
|
uint32_t data_idx;
|
||||||
if (!com_bin_read_bufbi(b, buf, &data_idx, name_len))
|
if (!com_bin_read_bufbi(b, buf, &data_idx, data_len))
|
||||||
goto BREAK;
|
goto BREAK;
|
||||||
|
|
||||||
result.auth_name = com_buf_index(struct com_buf, buf, name_idx);
|
result.auth_name = com_buf_index_as(struct com_buf, buf, name_idx);
|
||||||
result.auth_data = com_buf_index(struct com_buf, buf, data_idx);
|
result.auth_data = com_buf_index_as(struct com_buf, buf, data_idx);
|
||||||
result.success = true;
|
result.success = true;
|
||||||
return result;
|
return result;
|
||||||
BREAK:
|
BREAK:
|
||||||
com_std_printf("Failure to initialize X11\n");
|
com_std_print('c', "Failure to initialize X11");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool plr_x11_handshake(com_std_fd xfd,
|
||||||
|
struct plr_x11_auth_entry auth_entry) {
|
||||||
|
struct {
|
||||||
|
uint8_t endianness;
|
||||||
|
uint8_t pad0;
|
||||||
|
uint16_t major_version;
|
||||||
|
uint16_t minor_version;
|
||||||
|
uint16_t auth_len;
|
||||||
|
uint16_t auth_data_len;
|
||||||
|
uint16_t pad1;
|
||||||
|
} __attribute__((packed)) request = {
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
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_std_read(xfd, response, sizeof(union plr_x11_response));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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)) {
|
||||||
|
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) {
|
||||||
|
com_std_print('c', "Failure to initialize X11");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct com_bin xauthbin = {
|
||||||
|
.ctx = (void *)(int64_t)xauthfd,
|
||||||
|
.flags = COM_BIN_BIG_ENDIAN,
|
||||||
|
.interface = {.readn = fileread, .skipn = fileskip}};
|
||||||
|
|
||||||
|
/* Loop over auth entries until we find magic cookie one. */
|
||||||
|
struct plr_x11_auth_entry auth_entry;
|
||||||
|
do {
|
||||||
|
com_buf_clear(xauthbuf);
|
||||||
|
auth_entry = plr_x11_read_auth_entry(&xauthbin, xauthbuf);
|
||||||
|
} while (auth_entry.success && auth_entry.family != 256);
|
||||||
|
if (!auth_entry.success) {
|
||||||
|
com_std_print('c', "Failure to initialize X11");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
com_std_close(xauthfd);
|
||||||
|
|
||||||
|
/* TODO: Check for auth_entry.name == MAGIC ? */
|
||||||
|
if (!plr_x11_handshake(xfd, auth_entry)) {
|
||||||
|
com_std_print('c', "Failure to initialize X11");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
com_buf_free(xauthbuf);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: correct error handling.
|
// TODO: correct error handling.
|
||||||
extern int plr_display_x11_main(int argc, char *argv[]) {
|
extern int plr_display_x11_main(int argc, char const *const *argv) {
|
||||||
char test_string[] = "what is this?";
|
char test_string[] = "what is this?";
|
||||||
char *compressed_string = NULL;
|
char *compressed_string = NULL;
|
||||||
uint32_t compressed_string_sz = 0;
|
uint32_t compressed_string_sz = 0;
|
||||||
@@ -87,84 +561,60 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
|||||||
com_lzw_free_table(&table);
|
com_lzw_free_table(&table);
|
||||||
com_std_assert(test);
|
com_std_assert(test);
|
||||||
|
|
||||||
com_mat_t m0 = com_mat_identity();
|
/* TODO: Move to separate executable maybe? */
|
||||||
com_mat_t m1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
#ifndef NDEBUG
|
||||||
for (int i = 0; i < 16; ++i)
|
com_test_run();
|
||||||
m1.a[i] *= COM_FIX_FRACUNIT;
|
#endif
|
||||||
|
|
||||||
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. */
|
/* Open X11 socket for communication. */
|
||||||
int const xfd = com_std_syscall(41, AF_UNIX, SOCK_STREAM, 0);
|
com_std_fd const xfd = com_std_socket(false);
|
||||||
if (xfd < 0) {
|
if (!xfd) {
|
||||||
com_std_printf("Failure to initialize X11\n");
|
com_std_print('c', "Failure to initialize X11");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_un const addr = {.sun_family = AF_UNIX,
|
if (!com_std_connect(xfd, false, "/tmp/.X11-unix/X0")) {
|
||||||
.sun_path = "/tmp/.X11-unix/X0"};
|
com_std_print('c', "Failure to initialize X11");
|
||||||
if (com_std_syscall(42, xfd, (intptr_t)&addr, sizeof addr)) {
|
|
||||||
com_std_printf("Failure to initialize X11\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* First we need to authenticate X11 connection, which requires some funk. */
|
if (!plr_x11_establish_connection(xfd, argc, argv)) {
|
||||||
/* We assume that $XAUTHORITY is set and I hope it is true. */
|
com_std_print('c', "Failure to initialize X11");
|
||||||
char *xauthpath = NULL;
|
|
||||||
for (char **envp = &argv[argc + 1]; *envp != NULL; ++envp) {
|
|
||||||
static const struct com_str xauthlit = com_str_lit("XAUTHORITY=");
|
|
||||||
if (com_str_ext_prefixed(*envp, xauthlit)) {
|
|
||||||
xauthpath = &((*envp)[xauthlit.len]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open it and parse. */
|
|
||||||
int const xauthfd = com_std_syscall(2, (intptr_t)xauthpath, O_RDONLY, 0);
|
|
||||||
if (xauthfd < 0) {
|
|
||||||
com_std_printf("Failure to initialize X11\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
com_buf_make_on_stack(char, xauthbuf, 1024, 0);
|
if (!plr_x11_enable_big_requests(xfd)) {
|
||||||
struct com_bin xauthbin = {
|
com_std_print('c', "Big requests unsupported for your X11");
|
||||||
.ctx = (void *)(int64_t)xauthfd,
|
return -1;
|
||||||
.flags = COM_BIN_BIG_ENDIAN,
|
}
|
||||||
.interface = {.readn = fileread, .skipn = fileskip}};
|
|
||||||
|
|
||||||
struct plr_x11_auth_entry auth_entry =
|
uint32_t const gc = plr_x11_create_graphical_context(xfd);
|
||||||
plr_x11_read_auth_entry(&xauthbin, xauthbuf);
|
if (!gc) {
|
||||||
|
com_std_print('c', "Failure to initialize X11");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
com_std_print_str(auth_entry.auth_name->data, auth_entry.auth_name->len,
|
uint32_t const win = plr_x11_create_window(xfd);
|
||||||
true);
|
if (!win) {
|
||||||
com_std_print_str(auth_entry.auth_data->data, auth_entry.auth_data->len,
|
com_std_print('c', "Failure to initialize X11");
|
||||||
true);
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
com_buf_free(xauthbuf);
|
uint32_t const fb = plr_x11_create_pixmap(xfd, win);
|
||||||
|
if (!fb) {
|
||||||
char sendbuf[16 * 1024] = {};
|
com_std_print('c', "Failure to initialize X11");
|
||||||
char readbuf[16 * 1024] = {};
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Display *display = XOpenDisplay(NULL);
|
// Display *display = XOpenDisplay(NULL);
|
||||||
// if (NULL == display) {
|
// if (NULL == display) {
|
||||||
// com_std_printf("Failed to initialize display");
|
// com_std_print('c', "Failed to initialize display");
|
||||||
// return -1;
|
// return -1;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Window root = DefaultRootWindow(display);
|
// Window root = DefaultRootWindow(display);
|
||||||
// if (None == root) {
|
// if (None == root) {
|
||||||
// com_std_printf("No root window found");
|
// com_std_print('c', "No root window found");
|
||||||
// XCloseDisplay(display);
|
// XCloseDisplay(display);
|
||||||
// return -1;
|
// return -1;
|
||||||
// }
|
// }
|
||||||
@@ -177,7 +627,7 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
|||||||
// XCreateSimpleWindow(display, root, 0, 0, WIDTH, HEIGHT, 0, 0,
|
// XCreateSimpleWindow(display, root, 0, 0, WIDTH, HEIGHT, 0, 0,
|
||||||
// 0xffffffff);
|
// 0xffffffff);
|
||||||
// if (None == window) {
|
// if (None == window) {
|
||||||
// com_std_printf("Failed to create window");
|
// com_std_print('c', "Failed to create window");
|
||||||
// XCloseDisplay(display);
|
// XCloseDisplay(display);
|
||||||
// return -1;
|
// return -1;
|
||||||
// }
|
// }
|
||||||
@@ -211,7 +661,7 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
|||||||
// pixel_buffer, WIDTH, HEIGHT, 32, 0);
|
// pixel_buffer, WIDTH, HEIGHT, 32, 0);
|
||||||
|
|
||||||
// if (!ximage) {
|
// if (!ximage) {
|
||||||
// com_std_printf("Failed to create XImage\n");
|
// com_std_print('c', "Failed to create XImage");
|
||||||
// // com_std_alloc(pixel_buffer, 0);
|
// // com_std_alloc(pixel_buffer, 0);
|
||||||
// XCloseDisplay(display);
|
// XCloseDisplay(display);
|
||||||
// return -1;
|
// return -1;
|
||||||
@@ -251,7 +701,24 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
// XCloseDisplay(display);
|
// 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) {
|
while (1) {
|
||||||
|
if (!plr_x11_poll_errors(xfd)) {
|
||||||
|
com_std_print('c', "X11 closed connection");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
@@ -0,0 +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.
|
||||||
|
|
||||||
|
.section .text
|
||||||
|
.global _start
|
||||||
|
|
||||||
|
_start:
|
||||||
|
push %ebp
|
||||||
|
mov %esp, %ebp
|
||||||
|
cld
|
||||||
|
|
||||||
|
call plr_display_dos_main
|
||||||
|
|
||||||
|
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); }
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
/* No libc starting point. Jump straight into action! */
|
||||||
|
__attribute__((naked)) __attribute__((force_align_arg_pointer))
|
||||||
|
__attribute__((noreturn)) extern void
|
||||||
|
_start(void) {
|
||||||
|
__asm__ volatile("xorl %ebp, %ebp\n"
|
||||||
|
"movl (%esp), %ecx\n"
|
||||||
|
"leal 4(%esp), %edx\n"
|
||||||
|
|
||||||
|
/* 3. Align the stack pointer to a 16-byte boundary */
|
||||||
|
"andl $-16, %esp\n"
|
||||||
|
|
||||||
|
/* 4. Reserve stack space for main's arguments + padding
|
||||||
|
We need to pass 2 arguments (8 bytes).
|
||||||
|
To keep %esp 16-byte aligned before the "call",
|
||||||
|
we allocate 16 bytes total. */
|
||||||
|
"subl $16, %esp\n"
|
||||||
|
|
||||||
|
/* 5. Place arguments onto the newly aligned stack */
|
||||||
|
"movl %ecx, (%esp)\n"
|
||||||
|
"movl %edx, 4(%esp)\n"
|
||||||
|
"call plr_display_x11_main\n"
|
||||||
|
"movl %eax, %ebx\n"
|
||||||
|
"movl $1, %eax\n"
|
||||||
|
"int $0x80");
|
||||||
|
}
|
||||||
+79
-30
@@ -1,15 +1,44 @@
|
|||||||
CC = clang
|
CC ?= clang
|
||||||
RELEASE ?= 0
|
RELEASE ?= 0 # Optimize.
|
||||||
SQUEEZE ?= 0
|
DEBUG ?= 1 # Emit debug info.
|
||||||
|
SQUEEZE ?= 0 # Nuclear option to save space.
|
||||||
BITS ?= 64
|
BITS ?= 64
|
||||||
CFLAGS = -Wall -std=gnu99 -flto=thin -ffast-math -fno-ident -fvisibility=hidden
|
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)
|
ifeq ($(MAKECMDGOALS),linux)
|
||||||
|
CFLAGS += -fno-pic -fno-pie
|
||||||
ifeq ($(RELEASE), 1)
|
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
|
-Wl,--optimize-bb-jumps
|
||||||
endif
|
endif
|
||||||
ifeq ($(SQUEEZE), 1)
|
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
|
-Wl,--nmagic
|
||||||
endif
|
endif
|
||||||
ifeq ($(BITS), 64)
|
ifeq ($(BITS), 64)
|
||||||
@@ -18,45 +47,65 @@ ifeq ($(MAKECMDGOALS),linux)
|
|||||||
CFLAGS += -m32 -msse2
|
CFLAGS += -m32 -msse2
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# WASM specific options.
|
||||||
ifeq ($(MAKECMDGOALS),wasm)
|
ifeq ($(MAKECMDGOALS),wasm)
|
||||||
ifeq ($(RELEASE), 1)
|
ifeq ($(RELEASE), 1)
|
||||||
CFLAGS += -Wl,--compress-relocations
|
CFLAGS += -Wl,--compress-relocations
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
ifeq ($(RELEASE), 0)
|
|
||||||
CFLAGS += -g3 -O1
|
# Separate debug and release switches.
|
||||||
|
ifeq ($(DEBUG), 1)
|
||||||
|
CFLAGS += -g3
|
||||||
else
|
else
|
||||||
CFLAGS += -Wl,--strip-all -DNDEBUG -O3 -fuse-ld=lld -fmerge-all-constants -Wl,-O3 \
|
CFLAGS += -DNDEBUG -s
|
||||||
-ffunction-sections -fdata-sections -Wl,--gc-sections \
|
endif
|
||||||
-Wl,-mllvm,--vectorize-slp=true \
|
ifeq ($(RELEASE), 1)
|
||||||
-Wl,-mllvm,--enable-gvn-hoist=true
|
CFLAGS += -O3 -fmerge-all-constants -Wl,-O3 \
|
||||||
|
-ffunction-sections -fdata-sections -Wl,--gc-sections
|
||||||
|
ifeq ($(CC),clang)
|
||||||
|
CFLAGS += -Wl,-mllvm,--vectorize-slp=true \
|
||||||
|
-Wl,-mllvm,--enable-gvn-hoist=true
|
||||||
|
endif
|
||||||
endif
|
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
|
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
|
||||||
|
|
||||||
# TODO: Make it work with no stdlib.
|
|
||||||
linux: CFLAGS += -static -mbmi2 -nostdlib
|
linux: CFLAGS += -static -mbmi2 -nostdlib
|
||||||
linux: $(LINUX) | $(SRC)
|
linux: ./amalgam.c
|
||||||
$(CC) -o player $^ $(CFLAGS)
|
$(CC) -o player $^ $(CFLAGS)
|
||||||
|
|
||||||
# Note: lld package might need to be downloaded.
|
# Note: lld package might need to be downloaded.
|
||||||
wasm: CFLAGS += --target=wasm32 -nostdlib -Wl,--no-entry -msimd128 -mbulk-memory
|
wasm: CFLAGS += --target=wasm32 -nostdlib -Wl,--no-entry -msimd128 -mbulk-memory
|
||||||
wasm:
|
wasm:
|
||||||
$(CC) -o player.wasm $(WASM) $(CFLAGS)
|
$(CC) -o player.wasm ./amalgam.c $(CFLAGS)
|
||||||
|
|
||||||
dos: $(DOS) $(DEPS)
|
# todo: Add -mtiny for release without debug
|
||||||
wcl386 -bt=dos4g -ox -l=dos4g $(DOS)
|
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 -nostdlib -nodefaultlibs
|
||||||
|
dos:
|
||||||
|
$(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 -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:
|
clean:
|
||||||
rm -r *.o ../*.o
|
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.
+12
-3
@@ -12,12 +12,21 @@
|
|||||||
#include "./draw.c"
|
#include "./draw.c"
|
||||||
|
|
||||||
#if COM_DEF_TARGET == COM_DEF_TARGET_LINUX
|
#if COM_DEF_TARGET == COM_DEF_TARGET_LINUX
|
||||||
#include "../Common/Std/linux.c"
|
#if COM_DEF_CPU == COM_DEF_CPU_AMD64
|
||||||
|
#include "../Common/Std/linux-amd64.c"
|
||||||
|
#include "./Main/linux-amd64.c"
|
||||||
|
#elif COM_DEF_CPU == COM_DEF_CPU_X86
|
||||||
|
#include "../Common/Std/linux-x86.c"
|
||||||
|
#include "./Main/linux-x86.c"
|
||||||
|
#endif
|
||||||
#include "../Common/Timer/stub.c"
|
#include "../Common/Timer/stub.c"
|
||||||
#include "./Display/x11.c"
|
#include "./Display/x11.c"
|
||||||
#include "./Main/linux.c"
|
|
||||||
// #include "./Main/libc.c"
|
|
||||||
#elif COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
#elif COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
||||||
#include "../Common/Std/stub.c"
|
#include "../Common/Std/stub.c"
|
||||||
#include "../Common/Timer/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
|
#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/cfg.h"
|
||||||
#include "../Common/def.h"
|
#include "../Common/def.h"
|
||||||
#include "../Common/gem.h"
|
#include "../Common/gem.h"
|
||||||
#include "../Common/std.h"
|
|
||||||
#include "../Common/time.h"
|
#include "../Common/time.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
|
static _Alignas(16) uint8_t plr_draw_buffer[COM_CFG_FBSIZE];
|
||||||
uint8_t plr_draw_buffer[COM_CFG_WIDTH * COM_CFG_HEIGHT * 4];
|
|
||||||
|
|
||||||
com_def_export_sym(
|
com_def_export_sym(
|
||||||
"plr_draw_get_pixel_buffer") uint8_t *plr_draw_get_pixel_buffer(void) {
|
"plr_draw_get_pixel_buffer") uint8_t *plr_draw_get_pixel_buffer(void) {
|
||||||
return plr_draw_buffer;
|
return plr_draw_buffer;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
com_def_export_sym("plr_draw_screen") void plr_draw_screen(
|
com_def_export_sym("plr_draw_screen") void plr_draw_screen(
|
||||||
uint8_t *pixel_buffer) {
|
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 y = 0; y < COM_CFG_HEIGHT; y++) {
|
||||||
for (int x = 0; x < COM_CFG_WIDTH; x++) {
|
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
|
// Assuming standard Little-Endian 32-bit BGRA format
|
||||||
pixel_buffer[pixel_index + 0] = (x) % 256; // Blue
|
pixel_buffer_aligned[pixel_index + 0] = (x) % 256; // Blue
|
||||||
pixel_buffer[pixel_index + 1] = (y) % 256; // Green
|
pixel_buffer_aligned[pixel_index + 1] = (y) % 256; // Green
|
||||||
pixel_buffer[pixel_index + 2] = (x + y) % 256; // Red
|
pixel_buffer_aligned[pixel_index + 2] = (x + y) % 256; // Red
|
||||||
pixel_buffer[pixel_index + 3] = 255; // Alpha/Padding
|
#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,
|
// com_mat_t proj = com_mat_perspective(COM_CFG_WIDTH, COM_CFG_HEIGHT, 1,
|
||||||
100 * COM_FIX_FRACUNIT, COM_FIX_PIHALF);
|
// 100 * COM_FIX_FRACUNIT,
|
||||||
com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
|
// COM_FIX_PIHALF);
|
||||||
com_vec_from(0, COM_FIX_FRACUNIT, 0),
|
// com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
|
||||||
com_vec_from(-COM_FIX_FRACUNIT, 0, 0));
|
// com_vec_from(0, COM_FIX_FRACUNIT, 0),
|
||||||
com_mat_t mvp = com_mat_reoder(com_mat_mul(proj, view));
|
// 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 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,
|
// com_vec_t v1 = com_vec_from(1 * COM_FIX_FRACUNIT, 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,
|
// com_vec_t v2 = com_vec_from(1 * COM_FIX_FRACUNIT, 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,
|
// com_vec_t v3 = com_vec_from(1 * COM_FIX_FRACUNIT, 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 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 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 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 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 c0 = com_gem_vec_project_clip(mvp, v0);
|
||||||
com_vec_t c1 = com_gem_vec_project_clip(mvp, v1);
|
// 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 c2 = com_gem_vec_project_clip(mvp, v2);
|
||||||
com_vec_t c3 = com_gem_vec_project_clip(mvp, v3);
|
// com_vec_t c3 = com_gem_vec_project_clip(mvp, v3);
|
||||||
|
|
||||||
com_pnt_t p0, p1, p2, p3;
|
// com_pnt_t p0, p1, p2, p3;
|
||||||
uint8_t cc0 = com_gem_clip_vis_project(c0, &p0);
|
// uint8_t cc0 = com_gem_clip_vis_project(c0, &p0);
|
||||||
uint8_t cc1 = com_gem_clip_vis_project(c1, &p1);
|
// uint8_t cc1 = com_gem_clip_vis_project(c1, &p1);
|
||||||
uint8_t cc2 = com_gem_clip_vis_project(c2, &p2);
|
// uint8_t cc2 = com_gem_clip_vis_project(c2, &p2);
|
||||||
uint8_t cc3 = com_gem_clip_vis_project(c3, &p3);
|
// 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", p0.a[0], p0.a[1]);
|
||||||
com_std_printf("%i, %i\n", p1.a[0], p1.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", p2.a[0], p2.a[1]);
|
||||||
com_std_printf("%i, %i\n", p3.a[0], p3.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 y = 0; y < 32; ++y) {
|
||||||
for (int x = 0; x < 32; ++x) {
|
for (int x = 0; x < 32; ++x) {
|
||||||
// uint32_t idx = com_zord_order(x, y);
|
// uint32_t idx = com_zord_order(x, y);
|
||||||
|
|||||||
@@ -4,5 +4,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
extern void plr_draw_screen(uint8_t *pixel_buffer);
|
extern void plr_draw_screen(uint8_t *pixel_buffer);
|
||||||
|
extern uint8_t *plr_draw_get_pixel_buffer(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
#include "Display/display.h"
|
|
||||||
|
|
||||||
extern int main(int argc, char *argv[]) {
|
|
||||||
return plr_display_dos_main(argc, argv);
|
|
||||||
}
|
|
||||||
+8
-18
@@ -1,5 +1,3 @@
|
|||||||
OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64")
|
|
||||||
OUTPUT_ARCH(i386:x86-64)
|
|
||||||
ENTRY(_start)
|
ENTRY(_start)
|
||||||
|
|
||||||
PHDRS
|
PHDRS
|
||||||
@@ -16,33 +14,25 @@ SECTIONS
|
|||||||
*(.text*)
|
*(.text*)
|
||||||
}
|
}
|
||||||
|
|
||||||
.rodata : {
|
.rodata ALIGN(16) : {
|
||||||
*(.rodata*)
|
*(.rodata*)
|
||||||
}
|
}
|
||||||
|
|
||||||
.data : {
|
.data ALIGN(16) : {
|
||||||
*(.data*)
|
*(.data*)
|
||||||
}
|
}
|
||||||
|
|
||||||
.bss : {
|
.shstrtab ALIGN(16) : {
|
||||||
|
*(.shstrtab*)
|
||||||
|
}
|
||||||
|
|
||||||
|
.bss ALIGN(16) : {
|
||||||
*(.bss*)
|
*(.bss*)
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Discard bloat and unnecessary ELF sections */
|
/* Discard bloat and unnecessary ELF sections */
|
||||||
/DISCARD/ : {
|
/DISCARD/ : {
|
||||||
*(.note.GNU-stack)
|
*(*)
|
||||||
*(.gnu_debuglink)
|
|
||||||
*(.gnu.lto_*)
|
|
||||||
*(.note.gnu.build-id)
|
|
||||||
*(.eh_frame_hdr)
|
|
||||||
*(.eh_frame_entry)
|
|
||||||
*(.eh_frame)
|
|
||||||
*(.comment)
|
|
||||||
*(.interp)
|
|
||||||
*(.dynsym)
|
|
||||||
*(.dynstr)
|
|
||||||
*(.hash)
|
|
||||||
*(.gnu.hash)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user