aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
blob: e482731b18d55fbd02ba967b0b88e826f265d8ac (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
#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 */
}

/* 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");
}

/* 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;
}

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;
}

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;
	}
	*/
}