aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
blob: deab5841d49b2aaf4a7ed2bda2daec6ed36c6d1b (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <rs232.h>
#include "uip.h"
#include "common.h"
#include "aexec.h"
#include "fujichat.h"

int __fastcall__ (*atari_exec_p)(char *) = (int __fastcall__ (*)(char *))0x600;
fuji_conf_t *config = (fuji_conf_t *)CONFIG_ADDRESS;

void disable_break(void) {
	asm("lda $10"); /* POKMSK */
	asm("and #$7f");
	asm("sta $10"); /* POKMSK */
	asm("sta $D20E"); /* IRQEN */
}

/* easier to copy/paste this tiny function from uip.c
	than it would be to rebuild all of uIP for use in
	this program! Also don't want to bloat fujiconf by
	linking uip.a, even if it would link without a
	recompile. */
/*
u16_t local_htons(u16_t val) {
	return HTONS(val);
}
*/

/* this version's half the size */
u16_t local_htons(u16_t val) {
	__AX__ = val;
	asm("sta tmp1");
	asm("txa");
	asm("ldx tmp1");
}

/* helper for fuji_cgetc */
void __fastcall__ call_keybdv(void) {
	asm("lda $E420+5"); /* KEYBDV */
	asm("pha");
	asm("lda	$E420+4");
	asm("pha");
	asm("lda	#12");
	asm("sta	$2A"); /* ICAX1Z */
}

/* replacement for cgetc() that doesn't include cursor enable/disable
	stuff (otherwise copied from cc65's cgetc.s) */
char __fastcall__ fuji_cgetc(void) {
	asm("jsr	_call_keybdv");
	asm("ldx	#0");
}


#if 0
char get_config(void) {
	char config_valid = 0;
	FILE *f = fopen(DEFAULT_CONF_FILE, "rb");

	puts("Loading config from " DEFAULT_CONF_FILE);

	if(f) {
		config_valid =
			fread(config, 1, sizeof(fuji_conf_t), f) == sizeof(fuji_conf_t);
		fclose(f);
		if(!config_valid)
			puts("Config file is wrong size");
	} else {
		puts("No config file found");
	}

	if(config_valid) {
		if(!config_is_valid()) {
			puts("Invalid or outdated config file");
			config_valid = 0;
		} else {
			puts("Loaded OK");
		}
	}

	return config_valid;
}

#else
/* using open() read() close() instead of fopen() fread() fclose()
	is a big win: save us 438 bytes! */
char get_config(void) {
	char config_valid = 0;
	int f = open(DEFAULT_CONF_FILE, O_RDONLY);

	puts("Loading config from " DEFAULT_CONF_FILE);

	if(f >= 0) {
		config_valid =
			(read(f, config, sizeof(fuji_conf_t)) == sizeof(fuji_conf_t));
		close(f);
		if(!config_valid)
			puts("Config file is wrong size");
	} else {
		puts("No config file found");
	}

	if(config_valid) {
		if(!config_is_valid()) {
			puts("Invalid or outdated config file");
			config_valid = 0;
		} else {
			puts("Loaded OK");
		}
	}

	return config_valid;
}
#endif

char config_is_valid(void) {
	// return( (memcmp(config->signature, CONF_SIGNATURE, 2) == 0) && (config->version == CONF_VERSION) );
	return
		*((char *)CONFIG_ADDRESS) == CONF_SIGNATURE_LO &&
		*(char *)(CONFIG_ADDRESS+1) == CONF_SIGNATURE_HI &&
		*(char *)(CONFIG_ADDRESS+2) == CONF_VERSION;
}

void set_default_config(void) {
	puts("Using built-in defaults");
	/*
		// FIXME: why does this end up always 0.0.0.0 now?
		// has something to do with macro expansion. What a mess.
	uip_ipaddr(&(config->local_ip), 192,168,0,2);
	uip_ipaddr(&(config->peer_ip), 192,168,0,1);
	uip_ipaddr(&(config->resolver_ip), 192,168,0,1);
	*/

	config->local_ip[0] = 0xa8c0;
	config->local_ip[1] = 0x0200;

	config->peer_ip[0] = 0xa8c0;
	config->peer_ip[1] = 0x0100;

	config->resolver_ip[0] = 0xa8c0;
	config->resolver_ip[1] = 0x0100;

	strcpy(config->server, "irc.freenode.org");
	strcpy(config->nick, DEFAULT_NICK);
	strcpy(config->real_name, "FujiChat User");
	config->server_port = 6667;
	config->ui_flags = UIFLAG_HIDEMOTD;
	config->bg_color = 0xc0; /* dark green */
	config->fg_color = 0x0c;
	config->baud = RS_BAUD_4800;

	*(char *)(CONFIG_ADDRESS) = CONF_SIGNATURE_LO;
	*(char *)(CONFIG_ADDRESS+1) = CONF_SIGNATURE_HI;
	*(char *)(CONFIG_ADDRESS+2) = CONF_VERSION;

	// config_valid = 1;
}

static char ipbuf[20];
/*
static char *fmt = "%d.%d.%d.%d";
*/
char * format_ip(uip_ipaddr_t *ip) {

	u16_t *ipaddr = (u16_t *)ip;
	sprintf(ipbuf, "%d.%d.%d.%d",
			local_htons(ipaddr[0]) >> 8,
			local_htons(ipaddr[0]) & 0xff,
			local_htons(ipaddr[1]) >> 8,
			local_htons(ipaddr[1]) & 0xff);

	return ipbuf;

	/*
	asm("	sta ptr1");
	asm("	stx ptr1+1");

	asm("	lda #<_ipbuf");
	asm("	ldx #>_ipbuf");
	asm("	jsr pushax");

	asm("	lda #<_fmt");
	asm("	ldx #>_fmt");
	asm("	jsr pushax");

	asm("	ldy #0");
	asm("	lda (ptr1),y");
	asm("	jsr pusha0");

	asm("	ldy #1");
	asm("	lda (ptr1),y");
	asm("	jsr pusha0");

	asm("	ldy #2");
	asm("	lda (ptr1),y");
	asm("	jsr pusha0");

	asm("	ldy #3");
	asm("	lda (ptr1),y");
	asm("	jsr pusha0");

	asm("	ldy #$0c");
	asm("	jsr _sprintf");

	asm("	lda #<_ipbuf");
	asm("	ldx #>_ipbuf");
	*/
}

void get_line(char *buf, unsigned char len) {
	asm("ldy #$00");
	asm("lda (sp),y");
	asm("sta $e2");

	asm("ldy #$02");
	asm("jsr ldaxysp");
	asm("sta $e0");
	asm("stx $e0+1");

	asm("@loop: lda _stdin");
	asm("ldx _stdin+1");
	asm("jsr _fgetc");

	asm("cmp #$9B");
	asm("beq @out");

	asm("ldy #$00");
	asm("sta ($e0),y");
	asm("inc $e0");
	asm("bne @noinc");
	asm("inc $e0+1");
	asm("@noinc:");

	asm("dec $e2");
	asm("bne @loop");

	asm("@out:");
	asm("ldy #$00");
	asm("tya");
	asm("sta ($e0),y");

	/*
		// C implementation is 80 bytes, asm is 50
	fgets(buf, len, stdin);
	while(*buf) {
		if(*buf == '\n')
			*buf = '\0';
		++buf;
	}
	*/
}