summaryrefslogtreecommitdiff
path: root/tests/tosboot/disk/common.c
blob: 80180701ca281236367ccc3def1803530404a11c (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Hatari - common.c
 *
 * Copyright (C) 2012 by Eero Tamminen
 *
 * This file is distributed under the GNU General Public License, version 2
 * or at your option any later version. Read the file gpl.txt for details.
 * 
 * Common functions for gemdos.c and minimal.c testers.
 */
#ifdef __PUREC__	/* or AHCC */
# include <tos.h>
# include <mint/sysvars.h>
#else
# include <osbind.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include "common.h"

/*------- success / failure ----- */

/* anything failing changes 'msg' to failure,
 * write_midi() expects them to be of same length.
 */
static const char success[] = SUCCESS;
static const char failure[] = FAILURE;
static const char *msg = success;


/* ------- sleeper functions ------ */

#define ETOS_STR 0x45544F53  /* "ETOS" */

/* how long sleep is needed to make sure all TOS
 * versions have started timer/counter after boot
 * so that printing will work?
 *
 * 2s is enough with floppies as they're slower,
 * but GEMDOS HD needs 4s with TOS v1.x
 */
static int sleep_secs = 4;

/* return NULL if OS version needs a sleep, otherwise a version info string */
static long can_skip_sleep(void)
{
	char *msg;
	long sysbase;
	OSHEADER* osheader;

	sysbase = *_sysbase;
	osheader = (OSHEADER*)sysbase;

	/* no extra boot sleeps needed with EmuTOS... */
	if ((long)(osheader->p_rsv2) == ETOS_STR) {
		msg = "EmuTOS";
		return (long)msg;
	}
	/* ...or newer (= slower to boot?) Atari TOS versions */
	if (osheader->os_version >= 0x0300) {
		msg = "TOS >= v3";
		return (long)msg;
	}
	return NULL;
}

/* do the sleeping */
static void sleep_if_needed(void)
{
	char *info;

	/* already slept? */
	if (!sleep_secs) {
		return;
	}

	info = (char *)Supexec(can_skip_sleep);
	if (info) {
		printf("\r\n%s -> no extra sleeps needed\r\n", info);
	} else {
		printf("\r\nSleeping %d secs...\r\n(for TOS printing to work after boot)\r\n",
		       sleep_secs);
		sleep(sleep_secs);
	}
	sleep_secs = 0;
}

/* ------- helper functions ------ */

/* device/file opening with error handling */
static long open_device(const char *path, int attr)
{
	long handle = Fopen(path, attr);
	if (handle < 0) {

		printf("ERROR: Fopen(%c: '%s', %d) -> %ld\r\n", 'A'+Dgetdrv(), path, attr, handle);
		msg = failure;
	}
	return handle;
}

/* device/file closing with error handling */
static void close_device(long handle)
{
	if (Fclose(handle) != 0) {
		Cconws("ERROR: file close failed\r\n");
		msg = failure;
	}
}

static void print_ioerror(const char *op, int handle, int bufsize, char *buffer, long count)
{
	printf("ERROR: %s(%d, %d, %p) -> %ld\r\n", op, handle, bufsize, buffer, count);
	msg = failure;
}

/* write given file content to to given device/file with GEMDOS */
static void write_gemdos_device(const char *from, const char *to)
{
	long handle1, handle2;
	long count1, count2;
	char buffer[64];

	handle1 = open_device(from, FO_READ);
	if (handle1 < 0) {
		return;
	}
	handle2 = open_device(to, FO_WRITE);
	if (handle2 < 0) {
		return;
	}
	while (1) {
		/* copy file contents */
		count1 = Fread(handle1, sizeof(buffer), buffer);
		if (!count1) {
			break;
		}
		if (count1 < 0 || count1 > (long)sizeof(buffer)) {
			print_ioerror("Fread", handle1, sizeof(buffer), buffer, count1);
			break;
		}
		count2 = Fwrite(handle2, count1, buffer);
		if (count2 != count1) {
			print_ioerror("Fwrite", handle2, count1, buffer, count2);
			break;
		}
	}
	close_device(handle1);
	close_device(handle2);
}

/* set given mode to given file */
static void set_mode(const char *path, int mode)
{
	/* TODO: AHCC misses FA_SET (1) */
	int result = Fattrib(path, 1, mode);
	if (result != mode) {
		printf("ERROR: Fattrib(%s, 1, %d) -> %d\r\n", path, mode, result);
		msg = failure;
	}
}

/* --------- public functions ---------
 * documented in the header
 */

void write2console(const char *input)
{
	printf("\r\n%s -> CON: (console)\r\n", input);
        if (Cconos()) {
		write_gemdos_device(input, "CON:");
	} else {
		Cconws("ERROR: 'CON:' not ready!\r\n");
		msg = failure;
	}
}

void write2printer(const char *input)
{
	sleep_if_needed();
	printf("\r\n%s -> PRN: (printer)\r\n", input);
        if (Cprnos()) {
		write_gemdos_device(input, "PRN:");
	} else {
		Cconws("ERROR: 'PRN:' not ready!\r\n");
		msg = failure;
	}
}

void write2serial(const char *input)
{
	printf("\r\n%s -> AUX: (serial)\r\n", input);
        if (Cauxos()) {
		write_gemdos_device(input, "AUX:");
	} else {
		Cconws("ERROR: 'AUX:' not ready!\r\n");
		msg = failure;
	}
}

void copy_file(const char *input, const char *output)
{
	printf("\r\n%s -> %s\r\n", input, output);
	write_gemdos_device(input, output);
	set_mode(output, FA_READONLY);
}

void truncate_file(const char *readonly)
{
	long handle;
	printf("\r\nTruncate -> %s\r\n", readonly);
	handle = Fcreate(readonly, 0);
	if (handle >= 0) {
		printf("ERROR: truncate succeeded, Fcreate(\"%s\", 0) -> %ld\r\n", readonly, handle);
		msg = failure;
		close_device(handle);
	}
	set_mode(readonly, 0);
	handle = Fcreate(readonly, 0);
	if (handle >= 0) {
		close_device(handle);
	} else {
		printf("ERROR: truncate failed, Fcreate(\"%s\", 0) -> %ld\r\n", readonly, handle);
		msg = failure;
	}
}

void write_midi(void)
{
	printf("\r\nResult -> Midi (%s)\r\n", msg);
	Vsync();  /* sync screen output before sending result */
	Midiws(sizeof(success)-2, msg);
	Midiws(0, "\n");
}

void clear_screen(void)
{
	printf("\033EGEMDOS version = 0x%x\r\n", Sversion());
}

static int is_enter(long key)
{
	int scancode = (key >> 16) & 0xff;
	/* return or enter? */
	if (scancode == 28 || scancode == 114) {
		return 1;
	}
	return 0;
}

void wait_enter(void)
{
	/* eat buffered keys */
	while (Cconis()) {
		Cconin();
	}
	Cconws("\r\n<press Enter>\r\n");
	while (!is_enter(Cconin()));
}

/* ------- TODO ------ */

#if TEST_REDIRECTION
static void stdin_to_printer(void)
{
	long handle = open_device("PRN:", FO_WRITE);
	if (handle >= 0) {
		/* TODO: dup & force stdout to printer */
	}
}
static void stdin_from_file(const char *path)
{
	long handle = open_device(path, FO_WRITE);
	if (handle >= 0) {
		/* TODO: dup & force stdin from file */
	}
}
static void stdin_stdout_reset(void)
{
	/* TODO: force stdin & stdout back to normal */
}
#endif