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"
|
2022-01-05 08:28:22 -05:00
|
|
|
#include "devices/system.h"
|
2021-11-05 17:32:45 -04:00
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
|
2023-01-01 16:19:40 -05:00
|
|
|
#define SUPPORT 0x1c03 /* devices mask */
|
|
|
|
|
2021-06-28 17:42:36 -04:00
|
|
|
static int
|
2023-01-01 16:19:40 -05:00
|
|
|
emu_error(char *msg, const char *err)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2021-07-14 15:11:10 -04:00
|
|
|
fprintf(stderr, "Error %s: %s\n", msg, err);
|
2021-03-22 22:04:31 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:19:40 -05:00
|
|
|
static int
|
|
|
|
console_input(Uxn *u, char c)
|
2021-07-28 18:41:07 -04:00
|
|
|
{
|
2023-01-01 16:19:40 -05:00
|
|
|
Uint8 *d = &u->dev[0x10];
|
|
|
|
d[0x02] = c;
|
|
|
|
return uxn_eval(u, GETVEC(d));
|
2021-11-04 12:38:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-01-01 16:19:40 -05:00
|
|
|
console_deo(Uint8 *d, Uint8 port)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2023-01-01 16:34:20 -05:00
|
|
|
FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr
|
|
|
|
: 0;
|
2022-01-11 14:07:25 -05:00
|
|
|
if(fd) {
|
2023-01-01 16:19:40 -05:00
|
|
|
fputc(d[port], fd);
|
2022-01-11 14:07:25 -05:00
|
|
|
fflush(fd);
|
|
|
|
}
|
2021-03-22 22:04:31 -04:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:38:32 -04:00
|
|
|
static Uint8
|
2023-01-01 16:19:40 -05:00
|
|
|
emu_dei(Uxn *u, Uint8 addr)
|
2021-10-13 17:56:38 -04:00
|
|
|
{
|
2023-01-01 16:19:40 -05:00
|
|
|
Uint8 p = addr & 0x0f, d = addr & 0xf0;
|
|
|
|
switch(d) {
|
|
|
|
case 0xa0: return file_dei(0, &u->dev[d], p);
|
|
|
|
case 0xb0: return file_dei(1, &u->dev[d], p);
|
|
|
|
case 0xc0: return datetime_dei(&u->dev[d], p);
|
|
|
|
}
|
|
|
|
return u->dev[addr];
|
2021-10-13 17:56:38 -04:00
|
|
|
}
|
|
|
|
|
2021-06-29 09:43:28 -04:00
|
|
|
static void
|
2023-01-01 16:19:40 -05:00
|
|
|
emu_deo(Uxn *u, Uint8 addr, Uint8 v)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2023-01-01 16:19:40 -05:00
|
|
|
Uint8 p = addr & 0x0f, d = addr & 0xf0;
|
|
|
|
Uint16 mask = 0x1 << (d >> 4);
|
|
|
|
u->dev[addr] = v;
|
|
|
|
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 18:13:12 -05:00
|
|
|
}
|
2023-01-01 16:19:40 -05:00
|
|
|
if(p == 0x01 && !(SUPPORT & mask))
|
|
|
|
fprintf(stderr, "Warning: Incompatible emulation, device: %02x.\n", d);
|
2022-01-11 18:13:12 -05:00
|
|
|
}
|
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)
|
2023-01-01 16:19:40 -05:00
|
|
|
return emu_error("Usage", "uxncli game.rom args");
|
|
|
|
if(!uxn_boot(&u, (Uint8 *)calloc(0x10300, sizeof(Uint8)), emu_dei, emu_deo))
|
|
|
|
return emu_error("Boot", "Failed");
|
2022-03-28 13:51:29 -04:00
|
|
|
if(!load_rom(&u, argv[1]))
|
2023-01-01 16:19:40 -05:00
|
|
|
return emu_error("Load", "Failed");
|
2022-01-11 18:13:12 -05:00
|
|
|
if(!uxn_eval(&u, PAGE_PROGRAM))
|
2023-01-01 16:19:40 -05:00
|
|
|
return emu_error("Init", "Failed");
|
2022-01-11 18:13:12 -05:00
|
|
|
for(i = 2; i < argc; i++) {
|
|
|
|
char *p = argv[i];
|
|
|
|
while(*p) console_input(&u, *p++);
|
|
|
|
console_input(&u, '\n');
|
2021-10-13 17:56:38 -04:00
|
|
|
}
|
2023-01-01 16:19:40 -05:00
|
|
|
while(!u.dev[0x0f]) {
|
|
|
|
int c = fgetc(stdin);
|
|
|
|
if(c != EOF)
|
|
|
|
console_input(&u, (Uint8)c);
|
|
|
|
}
|
2021-03-22 22:04:31 -04:00
|
|
|
return 0;
|
|
|
|
}
|