···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: config.h *
77+ * Created: 2018-12-24 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#ifndef PCEUTILS_CONFIG_H
2424+#define PCEUTILS_CONFIG_H 1
2525+2626+2727+#define PCEUTILS_VERSION_STR "0.0.1"
2828+2929+3030+#endif
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: pce.h *
77+ * Created: 2018-12-22 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#ifndef PCEUTILS_PCE_H
2424+#define PCEUTILS_PCE_H 1
2525+2626+2727+int pce_check (void);
2828+int pce_get_version (unsigned *p);
2929+int pce_get_version_str (char *str, unsigned max);
3030+3131+int pce_init (void);
3232+int pce_putc (int c);
3333+int pce_putc0 (int c);
3434+int pce_puts (const char *s);
3535+int pce_puts0 (const char *s);
3636+int pce_getc (void);
3737+3838+void pce_stop (void);
3939+void pce_abort (void);
4040+4141+int pce_set_msg (const char *msg, const char *val);
4242+4343+long long pce_get_time_unix (void);
4444+4545+int pce_read_open (const char *s);
4646+int pce_read_close (void);
4747+int pce_read16 (void *buf);
4848+4949+int pce_write_open (const char *s);
5050+int pce_write_close (void);
5151+int pce_write16 (void *buf, unsigned cnt);
5252+5353+5454+#endif
+381
src/arch/sim405/pceutils/pcecp.c
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: pcecp.c *
77+ * Created: 2018-12-22 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <stdio.h>
2424+#include <stdlib.h>
2525+#include <time.h>
2626+2727+#include "config.h"
2828+#include "pce.h"
2929+3030+3131+#define BUFMAX 16384
3232+3333+3434+const char *arg0 = NULL;
3535+3636+static char par_verbose = 0;
3737+static char par_write = 0;
3838+static char par_std = 0;
3939+static char par_endarg = 0;
4040+4141+static char *par_src = NULL;
4242+static char *par_dst = NULL;
4343+4444+static unsigned char par_buf[BUFMAX];
4545+4646+4747+static
4848+void print_help (void)
4949+{
5050+ fputs (
5151+ "pcecp: copy files to/from the host\n"
5252+ "\n"
5353+ "usage: pcecp [options] [src [dst]]\n"
5454+ " -c Use stdin/stdout instead of a guest file [no]\n"
5555+ " -h Print help\n"
5656+ " -i Copy a file from the host to the guest [default]\n"
5757+ " -o Copy a file from the guest to the host\n"
5858+ " -v Verbose operation [no]\n"
5959+ " -V Print version\n",
6060+ stdout
6161+ );
6262+6363+ fflush (stdout);
6464+}
6565+6666+static
6767+void print_version (void)
6868+{
6969+ fputs ("pcecp version " PCEUTILS_VERSION_STR "\n", stdout);
7070+ fflush (stdout);
7171+}
7272+7373+static
7474+int pce_copy_in_fp (const char *inp, FILE *out)
7575+{
7676+ int r;
7777+ unsigned cnt;
7878+ unsigned long total;
7979+ time_t t1, t2;
8080+8181+ if (pce_read_open (inp)) {
8282+ fprintf (stderr, "%s: error opening host file (%s)\n",
8383+ arg0, inp
8484+ );
8585+8686+ return (1);
8787+ }
8888+8989+ t1 = time (NULL);
9090+9191+ total = 0;
9292+ cnt = 0;
9393+9494+ while (1) {
9595+ r = pce_read16 (par_buf + cnt);
9696+9797+ if (r <= 0) {
9898+ if (r < 0) {
9999+ fprintf (stderr, "%s: read error\n", arg0);
100100+ }
101101+102102+ break;
103103+ }
104104+105105+ cnt += r;
106106+107107+ if (cnt >= BUFMAX) {
108108+ total += cnt;
109109+110110+ if (par_verbose) {
111111+ t2 = time (NULL);
112112+113113+ if (t1 != t2) {
114114+ fprintf (stderr, "%lu KiB read\r",
115115+ total / 1024
116116+ );
117117+118118+ t1 = t2;
119119+ }
120120+ }
121121+122122+ if (fwrite (par_buf, 1, cnt, out) != cnt) {
123123+ fprintf (stderr, "%s: write error\n", arg0);
124124+ return (1);
125125+ }
126126+127127+ cnt = 0;
128128+ }
129129+ }
130130+131131+ if (cnt > 0) {
132132+ total += cnt;
133133+134134+ if (fwrite (par_buf, 1, cnt, out) != cnt) {
135135+ fprintf (stderr, "%s: write error\n", arg0);
136136+ return (1);
137137+ }
138138+ }
139139+140140+ pce_read_close();
141141+142142+ fflush (out);
143143+144144+ if (par_verbose) {
145145+ fprintf (stderr, "%lu bytes read\n", total);
146146+ }
147147+148148+ return (0);
149149+}
150150+151151+static
152152+int pce_copy_in (const char *inp, const char *out, int std)
153153+{
154154+ int r;
155155+ FILE *fp;
156156+157157+ if (std) {
158158+ fp = stdout;
159159+ }
160160+ else {
161161+ fp = fopen (out, "wb");
162162+ }
163163+164164+ if (fp == NULL) {
165165+ fprintf (stderr, "%s: error opening local file (%s)\n",
166166+ arg0, out
167167+ );
168168+169169+ return (1);
170170+ }
171171+172172+ r = pce_copy_in_fp (inp, fp);
173173+174174+ if (fp != stdout) {
175175+ fclose (fp);
176176+ }
177177+178178+ return (r);
179179+}
180180+181181+static
182182+int pce_copy_out_fp (FILE *inp, const char *out)
183183+{
184184+ int r;
185185+ unsigned idx, cnt, n;
186186+ unsigned long total;
187187+ time_t t1, t2;
188188+189189+ if (pce_write_open (out)) {
190190+ fprintf (stderr, "%s: error opening host file (%s)\n",
191191+ arg0, out
192192+ );
193193+194194+ return (1);
195195+ }
196196+197197+ t1 = time (NULL);
198198+199199+ total = 0;
200200+ idx = 0;
201201+ cnt = 0;
202202+203203+ while (1) {
204204+ if (idx >= cnt) {
205205+ if (par_verbose) {
206206+ t2 = time (NULL);
207207+208208+ if (t1 != t2) {
209209+ fprintf (stderr, "%lu KiB written\r",
210210+ total / 1024
211211+ );
212212+213213+ t1 = t2;
214214+ }
215215+ }
216216+217217+ idx = 0;
218218+ cnt = fread (par_buf, 1, BUFMAX, inp);
219219+220220+ if (cnt == 0) {
221221+ break;
222222+ }
223223+ }
224224+225225+ n = cnt - idx;
226226+227227+ if (n > 16) {
228228+ n = 16;
229229+ }
230230+231231+ r = pce_write16 (par_buf + idx, n);
232232+233233+ if (r <= 0) {
234234+ if (r < 0) {
235235+ fprintf (stderr, "%s: write error\n", arg0);
236236+ }
237237+238238+ break;
239239+ }
240240+241241+ idx += r;
242242+ total += r;
243243+ }
244244+245245+ pce_write_close();
246246+247247+ if (par_verbose) {
248248+ fprintf (stderr, "%lu bytes written\n", total);
249249+ }
250250+251251+ return (0);
252252+}
253253+254254+static
255255+int pce_copy_out (const char *inp, const char *out, int std)
256256+{
257257+ int r;
258258+ FILE *fp;
259259+260260+ if (std) {
261261+ fp = stdin;
262262+ }
263263+ else {
264264+ fp = fopen (inp, "rb");
265265+ }
266266+267267+ if (fp == NULL) {
268268+ fprintf (stderr, "%s: error opening local file (%s)\n",
269269+ arg0, inp
270270+ );
271271+272272+ return (1);
273273+ }
274274+275275+ r = pce_copy_out_fp (fp, out);
276276+277277+ if (fp != stdin) {
278278+ fclose (fp);
279279+ }
280280+281281+ return (r);
282282+}
283283+284284+static
285285+int args (int *argi, char **argv)
286286+{
287287+ const char *s;
288288+289289+ s = argv[*argi] + 1;
290290+291291+ while (*s != 0) {
292292+ if (*s == 'c') {
293293+ par_std = 1;
294294+ }
295295+ else if (*s == 'h') {
296296+ print_help();
297297+ exit (0);
298298+ }
299299+ else if (*s == 'i') {
300300+ par_write = 0;
301301+ }
302302+ else if (*s == 'o') {
303303+ par_write = 1;
304304+ }
305305+ else if (*s == 'v') {
306306+ par_verbose = 1;
307307+ }
308308+ else if (*s == 'V') {
309309+ print_version();
310310+ exit (0);
311311+ }
312312+ else {
313313+ fprintf (stderr, "%s: unknown option (%s)\n", arg0, s);
314314+ return (1);
315315+ }
316316+317317+ s += 1;
318318+ }
319319+320320+ return (0);
321321+}
322322+323323+int main (int argc, char **argv)
324324+{
325325+ int i, r;
326326+327327+ arg0 = argv[0];
328328+329329+ if (pce_check()) {
330330+ fprintf (stderr, "%s: not running under pce\n", arg0);
331331+ return (1);
332332+ }
333333+334334+ i = 1;
335335+336336+ while (i < argc) {
337337+ if ((argv[i][0] == '-') && (par_endarg == 0)) {
338338+ if ((argv[i][1] == '-') && (argv[i][2] == 0)) {
339339+ par_endarg = 1;
340340+ }
341341+ else if (args (&i, argv)) {
342342+ return (1);
343343+ }
344344+ }
345345+ else {
346346+ if (par_src == NULL) {
347347+ par_src = argv[i];
348348+ }
349349+ else if (par_dst == NULL) {
350350+ par_dst = argv[i];
351351+ }
352352+ else {
353353+ return (1);
354354+ }
355355+ }
356356+357357+ i += 1;
358358+ }
359359+360360+ if (par_src == NULL) {
361361+ print_help();
362362+ return (0);
363363+ }
364364+365365+ if (par_dst == NULL) {
366366+ par_dst = par_src;
367367+ }
368368+369369+ if (par_write) {
370370+ r = pce_copy_out (par_src, par_dst, par_std);
371371+ }
372372+ else {
373373+ r = pce_copy_in (par_src, par_dst, par_std);
374374+ }
375375+376376+ if (r) {
377377+ return (1);
378378+ }
379379+380380+ return (0);
381381+}
+123
src/arch/sim405/pceutils/pcemsg.c
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: pcemsg.c *
77+ * Created: 2018-12-23 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <stdio.h>
2424+2525+#include "config.h"
2626+#include "pce.h"
2727+2828+2929+const char *arg0 = NULL;
3030+3131+3232+static
3333+void print_help (void)
3434+{
3535+ fputs (
3636+ "pcemsg: send messages to the emulator\n"
3737+ "\n"
3838+ "usage: pcemsg [options] message [arg]\n"
3939+ " -h Print help\n"
4040+ " -V Print version information\n",
4141+ stdout
4242+ );
4343+4444+ fflush (stdout);
4545+}
4646+4747+static
4848+void print_version (void)
4949+{
5050+ fputs ("pcemsg version " PCEUTILS_VERSION_STR "\n", stdout);
5151+ fflush (stdout);
5252+}
5353+5454+int main (int argc, char **argv)
5555+{
5656+ int i;
5757+ const char *s;
5858+ const char *msg, *val;
5959+6060+ arg0 = argv[0];
6161+6262+ msg = NULL;
6363+ val = NULL;
6464+6565+ i = 1;
6666+ while (i < argc) {
6767+ if (argv[i][0] == '-') {
6868+ s = argv[i] + 1;
6969+7070+ while (*s != 0) {
7171+ if (*s == 'h') {
7272+ print_help();
7373+ return (0);
7474+ }
7575+ else if (*s == 'V') {
7676+ print_version();
7777+ return (0);
7878+ }
7979+ else {
8080+ fprintf (stderr,
8181+ "%s: unknown option (%s)\n",
8282+ arg0, argv[i]
8383+ );
8484+8585+ return (1);
8686+ }
8787+8888+ s += 1;
8989+ }
9090+ }
9191+ else if (msg == NULL) {
9292+ msg = argv[i];
9393+ }
9494+ else if (val == NULL) {
9595+ val = argv[i];
9696+ }
9797+ else {
9898+ fprintf (stderr, "%s: too many arguments (%s)\n",
9999+ arg0, argv[i]
100100+ );
101101+102102+ return (1);
103103+ }
104104+105105+ i += 1;
106106+ }
107107+108108+ if (msg == NULL) {
109109+ print_help();
110110+ return (1);
111111+ }
112112+113113+ if (val == NULL) {
114114+ val = "";
115115+ }
116116+117117+ if (pce_set_msg (msg, val)) {
118118+ fprintf (stderr, "error\n");
119119+ return (1);
120120+ }
121121+122122+ return (0);
123123+}
+145
src/arch/sim405/pceutils/pcetime.c
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: pcetime.c *
77+ * Created: 2018-12-23 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <stdio.h>
2424+#include <time.h>
2525+#include <sys/time.h>
2626+2727+#include "config.h"
2828+#include "pce.h"
2929+3030+3131+const char *arg0 = NULL;
3232+3333+static char par_print = 0;
3434+static char par_set = 0;
3535+3636+3737+static
3838+void print_help (void)
3939+{
4040+ fputs (
4141+ "pcetime: get the time from the host\n"
4242+ "\n"
4343+ "usage: pcetime [options]\n"
4444+ " -h Print help\n"
4545+ " -p Print the current host time\n"
4646+ " -s Set the system time to the current host time\n"
4747+ " -V Print version\n",
4848+ stdout
4949+ );
5050+}
5151+5252+static
5353+void print_version (void)
5454+{
5555+ fputs ("pcetime version " PCEUTILS_VERSION_STR "\n", stdout);
5656+ fflush (stdout);
5757+}
5858+5959+int main (int argc, char **argv)
6060+{
6161+ int i;
6262+ const char *s;
6363+ time_t val;
6464+6565+ arg0 = argv[0];
6666+6767+ if (pce_check()) {
6868+ fprintf (stderr, "%s: not running under pce\n", arg0);
6969+ return (1);
7070+ }
7171+7272+ i = 1;
7373+ while (i < argc) {
7474+ if (argv[i][0] == '-') {
7575+ s = argv[i] + 1;
7676+ while (*s != 0) {
7777+ if (*s == 'h') {
7878+ print_help();
7979+ return (0);
8080+ }
8181+ else if (*s == 'p') {
8282+ par_print = 1;
8383+ }
8484+ else if (*s == 's') {
8585+ par_set = 1;
8686+ }
8787+ else if (*s == 'V') {
8888+ print_version();
8989+ return (0);
9090+ }
9191+ else {
9292+ fprintf (stderr,
9393+ "%s: unknown option (%s)\n",
9494+ arg0, argv[i]
9595+ );
9696+9797+ return (1);
9898+ }
9999+100100+ s += 1;
101101+ }
102102+ }
103103+ else {
104104+ fprintf (stderr, "%s: unknown option (%s)\n",
105105+ arg0, argv[i]
106106+ );
107107+108108+ return (1);
109109+ }
110110+111111+ i += 1;
112112+ }
113113+114114+ if ((par_print == 0) && (par_set == 0)) {
115115+ print_help();
116116+ return (0);
117117+ }
118118+119119+ val = (time_t) pce_get_time_unix();
120120+121121+ if (par_print) {
122122+ struct tm *tm;
123123+124124+ tm = gmtime (&val);
125125+126126+ printf ("%04d-%02d-%02d %02d:%02d:%02d UTC\n",
127127+ tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
128128+ tm->tm_hour, tm->tm_min, tm->tm_sec
129129+ );
130130+ }
131131+132132+ if (par_set) {
133133+ struct timeval tv;
134134+135135+ tv.tv_sec = val;
136136+ tv.tv_usec = 0;
137137+138138+ if (settimeofday (&tv, NULL)) {
139139+ fprintf (stderr, "%s: error setting time\n", arg0);
140140+ return (1);
141141+ }
142142+ }
143143+144144+ return (0);
145145+}
+57
src/arch/sim405/pceutils/pcever.c
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: pcever.c *
77+ * Created: 2018-12-22 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2018 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <stdio.h>
2424+2525+#include "config.h"
2626+#include "pce.h"
2727+2828+2929+const char *arg0 = NULL;
3030+3131+3232+int main (int argc, char **argv)
3333+{
3434+ unsigned ver[3];
3535+ char str[256];
3636+3737+ arg0 = argv[0];
3838+3939+ if (pce_check()) {
4040+ fprintf (stderr, "%s: not running under pce\n", arg0);
4141+ return (1);
4242+ }
4343+4444+ if (pce_get_version (ver)) {
4545+ fprintf (stderr, "%s: can't get version\n", arg0);
4646+ return (1);
4747+ }
4848+4949+ if (pce_get_version_str (str, sizeof (str))) {
5050+ fprintf (stderr, "%s: can't get version string\n", arg0);
5151+ return (1);
5252+ }
5353+5454+ printf ("%s\n", str);
5555+5656+ return (0);
5757+}