uxn/src/uxncli.c

141 lines
2.9 KiB
C
Raw Normal View History

2021-03-22 22:04:31 -04:00
#include <stdio.h>
2022-01-01 18:20:48 -05:00
#include <stdlib.h>
2022-01-07 13:02:28 -05:00
2021-05-12 21:28:45 -04:00
#include "uxn.h"
#include "devices/system.h"
#include "devices/file.h"
2022-01-07 13:02:28 -05:00
#include "devices/datetime.h"
2021-03-22 22:04:31 -04:00
/*
Copyright (c) 2021 Devine Lu Linvega
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 17:42:36 -04:00
static int
2021-03-22 22:04:31 -04:00
error(char *msg, const char *err)
{
fprintf(stderr, "Error %s: %s\n", msg, err);
2021-03-22 22:04:31 -04:00
return 0;
}
void
system_deo_special(Device *d, Uint8 port)
2021-07-28 18:41:07 -04:00
{
2022-01-23 06:23:52 -05:00
(void)d;
(void)port;
}
static void
2021-11-04 13:13:01 -04:00
console_deo(Device *d, Uint8 port)
2021-03-22 22:04:31 -04:00
{
2022-01-11 14:07:25 -05:00
FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr
: 0;
if(fd) {
fputc(d->dat[port], fd);
fflush(fd);
}
2021-03-22 22:04:31 -04:00
}
static Uint8
2021-11-04 13:13:01 -04:00
nil_dei(Device *d, Uint8 port)
2021-03-22 22:04:31 -04:00
{
2021-11-04 13:13:01 -04:00
return d->dat[port];
}
static void
2021-11-04 13:13:01 -04:00
nil_deo(Device *d, Uint8 port)
{
2022-01-07 19:46:50 -05:00
(void)d;
(void)port;
2021-03-22 22:04:31 -04:00
}
static int
console_input(Uxn *u, char c)
{
2022-01-11 17:38:55 -05:00
Device *d = &u->dev[1];
d->dat[0x2] = c;
return uxn_eval(u, GETVECTOR(d));
}
2021-06-29 09:43:28 -04:00
static void
run(Uxn *u)
2021-03-22 22:04:31 -04:00
{
2022-01-11 17:38:55 -05:00
Device *d = &u->dev[0];
2022-01-11 18:13:12 -05:00
while(!d->dat[0xf]) {
int c = fgetc(stdin);
if(c != EOF)
console_input(u, (Uint8)c);
}
2021-03-22 22:04:31 -04:00
}
static int
2021-08-01 17:56:12 -04:00
load(Uxn *u, char *filepath)
{
FILE *f;
2021-11-17 08:29:36 -05:00
int r;
if(!(f = fopen(filepath, "rb"))) return 0;
2022-01-07 19:51:43 -05:00
r = fread(u->ram + PAGE_PROGRAM, 1, 0x10000 - PAGE_PROGRAM, f);
2021-11-17 08:29:36 -05:00
fclose(f);
if(r < 1) return 0;
2021-08-01 18:04:51 -04:00
fprintf(stderr, "Loaded %s\n", filepath);
return 1;
}
int
uxn_interrupt(void)
{
return 1;
}
2022-01-11 18:13:12 -05:00
static int
start(Uxn *u)
2021-03-22 22:04:31 -04:00
{
2022-01-12 21:56:59 -05:00
if(!uxn_boot(u, (Uint8 *)calloc(0x10000, sizeof(Uint8))))
2021-03-22 22:04:31 -04:00
return error("Boot", "Failed");
2022-01-11 18:13:12 -05:00
/* system */ uxn_port(u, 0x0, system_dei, system_deo);
/* console */ uxn_port(u, 0x1, nil_dei, console_deo);
/* empty */ uxn_port(u, 0x2, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x3, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x4, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x5, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x6, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x7, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x8, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0x9, nil_dei, nil_deo);
2022-03-28 13:16:44 -04:00
/* file0 */ uxn_port(u, 0xa, file_dei, file_deo);
/* file1 */ uxn_port(u, 0xb, file_dei, file_deo);
/* datetime */ uxn_port(u, 0xc, datetime_dei, nil_deo);
2022-01-11 18:13:12 -05:00
/* empty */ uxn_port(u, 0xd, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0xe, nil_dei, nil_deo);
/* empty */ uxn_port(u, 0xf, nil_dei, nil_deo);
return 1;
}
2021-03-22 22:04:31 -04:00
2022-01-11 18:13:12 -05:00
int
main(int argc, char **argv)
{
Uxn u;
int i;
if(argc < 2)
return error("Usage", "uxncli game.rom args");
if(!start(&u))
return error("Start", "Failed");
if(!load(&u, argv[1]))
return error("Load", "Failed");
if(!uxn_eval(&u, PAGE_PROGRAM))
return error("Init", "Failed");
for(i = 2; i < argc; i++) {
char *p = argv[i];
while(*p) console_input(&u, *p++);
console_input(&u, '\n');
}
2021-06-29 09:43:28 -04:00
run(&u);
2021-03-22 22:04:31 -04:00
return 0;
}