summary refs log tree commit diff
path: root/pdf/Makefile
blob: 91de32b7a158b99b4c70f4f241ff2177323cac89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
PDFS += abi.pdf
PDFS += c11.pdf
PDFS += elf.pdf
PDFS += intel-64-opt.pdf
PDFS += intel-64-sdm-vol-1.pdf
PDFS += intel-64-sdm-vol-2.pdf
PDFS += intel-64-sdm-vol-3.pdf
PDFS += intel-64-sdm-vol-4.pdf
PDFS += multiboot.pdf

ELF = http://refspecs.linuxbase.org/elf
INTEL = https://software.intel.com/sites/default/files/managed

URL_abi.pdf = ${ELF}/x64_64-abi-0.99.pdf
URL_c11.pdf = http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
URL_elf.pdf = ${ELF}/elf.pdf
URL_intel-64-opt.pdf = ${INTEL}/9e/bc/64-ia-32-architectures-optimization-manual.pdf
URL_intel-64-sdm-vol-1.pdf = ${INTEL}/a4/60/253665-sdm-vol-1.pdf
URL_intel-64-sdm-vol-2.pdf = ${INTEL}/a4/60/325383-sdm-vol-2abcd.pdf
URL_intel-64-sdm-vol-3.pdf = ${INTEL}/a4/60/325384-sdm-vol-3abcd.pdf
URL_intel-64-sdm-vol-4.pdf = ${INTEL}/22/0d/335592-sdm-vol-4.pdf
URL_multiboot.pdf = https://www.gnu.org/software/grub/manual/multiboot/multiboot.pdf

all: ${PDFS}

${PDFS}:
	curl -o $@ ${URL_$@}
	chmod 444 $@

clean:
	rm -f ${PDFS}
href='#n129'>129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/* Copyright (C) 2017  June McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <wchar.h>

#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))

static const char DefaultDataPath[] = "torus.dat";
static const char DefaultSockPath[] = "torus.sock";
static const char DefaultFontPath[] = "default8x16.psfu";

enum {
	ColorBlack,
	ColorRed,
	ColorGreen,
	ColorYellow,
	ColorBlue,
	ColorMagenta,
	ColorCyan,
	ColorWhite,
	ColorBright,
};

static const wchar_t CP437[256] = (
	L" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼"
	L"►◄↕‼¶§▬↨↑↓→←∟↔▲▼"
	L" !\"#$%&'()*+,-./"
	L"0123456789:;<=>?"
	L"@ABCDEFGHIJKLMNO"
	L"PQRSTUVWXYZ[\\]^_"
	L"`abcdefghijklmno"
	L"pqrstuvwxyz{|}~⌂"
	L"ÇüéâäàåçêëèïîìÄÅ"
	L"ÉæÆôöòûùÿÖÜ¢£¥₧ƒ"
	L"áíóúñѪº¿⌐¬½¼¡«»"
	L"░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
	L"└┴┬├─┼╞╟╚╔╩╦╠═╬╧"
	L"╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀"
	L"αßΓπΣσµτΦΘΩδ∞φε∩"
	L"≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ "
);

enum {
	CellRows = 25,
	CellCols = 80,
};
static const size_t CellsSize = sizeof(uint8_t[CellRows][CellCols]);

static const uint8_t CellInitX = CellCols / 2;
static const uint8_t CellInitY = CellRows / 2;

struct Meta {
	time_t createTime;
	time_t modifyTime;
	time_t accessTime;
	uint32_t modifyCount;
	uint32_t accessCount;
};

struct Tile {
	alignas(4096)
	time_t createTime;
	time_t modifyTime;
	alignas(16) uint8_t cells[CellRows][CellCols];
	alignas(16) uint8_t colors[CellRows][CellCols];
	uint32_t modifyCount;
	uint32_t accessCount;
	time_t accessTime;
};
static_assert(4096 == sizeof(struct Tile), "struct Tile is page-sized");

static inline struct Meta tileMeta(const struct Tile *tile) {
	return (struct Meta) {
		.createTime = tile->createTime,
		.modifyTime = tile->modifyTime,
		.accessTime = tile->accessTime,
		.modifyCount = tile->modifyCount,
		.accessCount = tile->accessCount,
	};
}

enum {
	TileRows = 512,
	TileCols = 512,
};
static const size_t TilesSize = sizeof(struct Tile[TileRows][TileCols]);

static const uint32_t TileInitX = 0;
static const uint32_t TileInitY = 0;

static const struct {
	uint32_t tileX;
	uint32_t tileY;
} Ports[] = {
	{ TileInitX, TileInitY },
	{ TileCols * 3 / 4, TileRows * 3 / 4 }, // NW
	{ TileCols * 1 / 4, TileRows * 3 / 4 }, // NE
	{ TileCols * 1 / 4, TileRows * 1 / 4 }, // SE
	{ TileCols * 3 / 4, TileRows * 1 / 4 }, // SW
};

enum {
	MapRows = 11,
	MapCols = 11,
};

struct Map {
	time_t now;
	struct Meta min;
	struct Meta max;
	struct Meta meta[MapRows][MapCols];
};

struct ServerMessage {
	enum {
		ServerTile,
		ServerMove,
		ServerPut,
		ServerCursor,
		ServerMap,
	} type;
	union {
		struct {
			uint8_t cellX;
			uint8_t cellY;
		} move;
		struct {
			uint8_t cellX;
			uint8_t cellY;
			uint8_t color;
			uint8_t cell;
		} put;
		struct {
			uint8_t oldCellX;
			uint8_t oldCellY;
			uint8_t newCellX;
			uint8_t newCellY;
		} cursor;
	};
};

static const uint8_t CursorNone = UINT8_MAX;

struct ClientMessage {
	enum {
		ClientMove,
		ClientFlip,
		ClientPut,
		ClientMap,
		ClientTele,
	} type;
	union {
		struct {
			int8_t dx;
			int8_t dy;
		} move;
		struct {
			uint8_t color;
			uint8_t cell;
		} put;
		uint8_t port;
	};
};