uxn/src/uxncli.c

97 lines
2.0 KiB
C
Raw Normal View History

2021-03-23 02:04:31 +00:00
#include <stdio.h>
2022-01-01 23:20:48 +00:00
#include <stdlib.h>
2022-01-07 18:02:28 +00:00
2021-05-13 01:28:45 +00:00
#include "uxn.h"
#include "devices/system.h"
#include "devices/file.h"
2022-01-07 18:02:28 +00:00
#include "devices/datetime.h"
2021-03-23 02:04:31 +00:00
/*
2023-01-02 14:40:23 +00:00
Copyright (c) 2021-2023 Devine Lu Linvega, Andrew Alderwick
2021-03-23 02:04:31 +00:00
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.
*/
2021-06-28 21:42:36 +00:00
static int
emu_error(char *msg, const char *err)
2021-03-23 02:04:31 +00:00
{
fprintf(stderr, "Error %s: %s\n", msg, err);
2023-02-13 17:33:57 +00:00
return 1;
2021-03-23 02:04:31 +00:00
}
static int
console_input(Uxn *u, char c)
2021-07-28 22:41:07 +00:00
{
Uint8 *d = &u->dev[0x10];
d[0x02] = c;
2023-03-01 18:42:03 +00:00
return uxn_eval(u, PEEK16(d));
}
static void
console_deo(Uint8 *d, Uint8 port)
2021-03-23 02:04:31 +00:00
{
2023-03-01 20:04:05 +00:00
switch(port) {
case 0x8:
fputc(d[port], stdout);
fflush(stdout);
return;
case 0x9:
fputc(d[port], stderr);
fflush(stderr);
return;
2022-01-11 19:07:25 +00:00
}
2021-03-23 02:04:31 +00:00
}
static Uint8
emu_dei(Uxn *u, Uint8 addr)
{
Uint8 p = addr & 0x0f, d = addr & 0xf0;
switch(d) {
case 0xc0: return datetime_dei(u, addr);
}
return u->dev[addr];
}
2021-06-29 13:43:28 +00:00
static void
2023-03-04 04:37:43 +00:00
emu_deo(Uxn *u, Uint8 addr)
2021-03-23 02:04:31 +00:00
{
Uint8 p = addr & 0x0f, d = addr & 0xf0;
switch(d) {
case 0x00: system_deo(u, &u->dev[d], p); break;
case 0x10: console_deo(&u->dev[d], p); break;
case 0xa0: file_deo(0, u->ram, &u->dev[d], p); break;
case 0xb0: file_deo(1, u->ram, &u->dev[d], p); break;
2022-01-11 23:13:12 +00:00
}
}
2021-03-23 02:04:31 +00:00
2022-01-11 23:13:12 +00:00
int
main(int argc, char **argv)
{
Uxn u;
int i;
if(argc < 2)
return emu_error("Usage", "uxncli game.rom args");
2023-01-31 17:49:32 +00:00
if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), emu_dei, emu_deo))
return emu_error("Boot", "Failed");
if(!system_load(&u, argv[1]))
return emu_error("Load", "Failed");
2022-01-11 23:13:12 +00:00
if(!uxn_eval(&u, PAGE_PROGRAM))
2023-02-13 17:33:57 +00:00
return u.dev[0x0f] & 0x7f;
2022-01-11 23:13:12 +00:00
for(i = 2; i < argc; i++) {
char *p = argv[i];
while(*p) console_input(&u, *p++);
console_input(&u, '\n');
}
while(!u.dev[0x0f]) {
int c = fgetc(stdin);
if(c != EOF)
console_input(&u, (Uint8)c);
}
2023-02-13 17:33:57 +00:00
return u.dev[0x0f] & 0x7f;
2021-03-23 02:04:31 +00:00
}