-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpfe.c
171 lines (140 loc) · 3.19 KB
/
xpfe.c
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
/*
* Copyright (c) 2024 Kenji Aoyama
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* xpfe: OMRON LUNA's XP (HD647180X I/O processor) front-end
*/
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "config.h"
#include "xpfe.h"
/* global variables */
int a_flag = 0;
int v_flag = 0; /* verbose */
int has_disk = 0;
int xpfd;
struct xpfe_if_t *xpfe_if;
void *xpshm;
/* prototypes */
void usage(void);
/* xpdisk.c */
void xpdisk_open(const char*);
void xpdisk_io(void);
void xpdisk_close(void);
void xpdisk_register(void);
/* xpload.c */
void xp_load_reset(int, const char *);
void *xp_mmap(int);
/* xprtc.c */
void xprtc_sync(void);
/* xptty.c */
void xptty_init(void);
void xptty_set_rawmode(void);
void xptty_reset_mode(void);
void xptty_send(char);
void xptty_receive(void);
int
is_xp_halted(void)
{
volatile uint32_t *rxbuf = &(xpfe_if->t_rxbuf);
/* Check alive flag: 0 is alive, 1 is halted */
return *rxbuf & 0x000000ff ? 1 : 0;
}
int
main(int argc, char *argv[])
{
char c, *xpfname;
int ch, ret, running;
u_int code;
extern int optind, opterr;
setprogname(argv[0]);
while ((ch = getopt(argc, argv, "av")) != -1) {
switch (ch) {
case 'a':
a_flag = 1;
break;
case 'v':
v_flag = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if ((argc == 0) || (argc > 2))
usage();
xpfname = argv[0];
xpfd = open(XP_DEV, O_RDWR);
if (xpfd < 0 )
err(EXIT_FAILURE, "can not open %s", XP_DEV);
if (argc == 2) {
xpdisk_open(argv[1]);
has_disk = 1;
}
xpshm = xp_mmap(xpfd);
if (a_flag)
printf("XP attached, ");
else {
xp_load_reset(xpfd, xpfname);
printf("XP binary loaded, ");
}
printf("type '^\\' to detach.\n");
xptty_init();
xpdisk_register();
xptty_set_rawmode();
running = 1;
while (running) {
/* Check if XP is halted */
if (is_xp_halted()) {
running = 0;
continue;
}
/* Sync RTC */
xprtc_sync();
/* Receive & put to stdout, first */
xptty_receive();
/* Disk I/O */
if (has_disk)
xpdisk_io();
/* Check Key in */
ret = read(STDIN_FILENO, &c, 1);
if (ret == 0)
continue;
if (c == XPFE_QUIT) { /* default: 'Control-\' */
running = 0;
continue;
}
/* Send */
xptty_send(c);
}
xptty_reset_mode();
if (has_disk)
xpdisk_close();
close(xpfd);
return EXIT_SUCCESS;
}
__dead void
usage(void)
{
printf("Usage: %s [options] xp_prog_file [disk_image]\n",
getprogname());
printf("\t-a : attach to running XP\n");
printf("\t-v : verbose mode\n");
exit(EXIT_FAILURE);
}