uxn/src/uxncli.c

89 lines
2.2 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"
2023-08-08 21:13:07 +00:00
#include "devices/console.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.
*/
Uint8
emu_dei(Uxn *u, Uint8 addr)
{
2023-03-04 18:51:23 +00:00
switch(addr & 0xf0) {
case 0x00: return system_dei(u, addr);
case 0xc0: return datetime_dei(u, addr);
}
return u->dev[addr];
}
void
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
static void
emu_run(Uxn *u)
{
while(!u->dev[0x0f]) {
int c = fgetc(stdin);
2023-10-25 15:24:37 +00:00
if(c == EOF) {
console_input(u, 0x00, CONSOLE_END);
break;
}
console_input(u, (Uint8)c, CONSOLE_STD);
}
}
static int
2023-08-16 01:44:16 +00:00
emu_end(Uxn *u)
{
free(u->ram);
return u->dev[0x0f] & 0x7f;
}
2022-01-11 23:13:12 +00:00
int
main(int argc, char **argv)
{
2023-10-30 18:24:04 +00:00
Uxn u = {0};
int i = 1;
if(i == argc)
return system_error("usage", "uxncli [-v] file.rom [args..]");
2023-08-08 22:56:40 +00:00
/* Connect Varvara */
2023-08-25 16:38:03 +00:00
system_connect(0x0, SYSTEM_VERSION, SYSTEM_DEIMASK, SYSTEM_DEOMASK);
system_connect(0x1, CONSOLE_VERSION, CONSOLE_DEIMASK, CONSOLE_DEOMASK);
system_connect(0xa, FILE_VERSION, FILE_DEIMASK, FILE_DEOMASK);
system_connect(0xb, FILE_VERSION, FILE_DEIMASK, FILE_DEOMASK);
system_connect(0xc, DATETIME_VERSION, DATETIME_DEIMASK, DATETIME_DEOMASK);
/* Read flags */
if(argv[i][0] == '-' && argv[i][1] == 'v')
2023-10-30 18:24:04 +00:00
return system_version("Uxncli - Console Varvara Emulator", "30 Oct 2023");
2023-08-15 23:06:29 +00:00
if(!system_init(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++]))
return system_error("Init", "Failed to initialize uxn.");
/* Game Loop */
u.dev[0x17] = argc - i;
if(uxn_eval(&u, PAGE_PROGRAM)) {
2023-08-15 23:24:40 +00:00
console_listen(&u, i, argc, argv);
emu_run(&u);
}
2023-08-16 01:44:16 +00:00
return emu_end(&u);
2021-03-23 02:04:31 +00:00
}