uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

uxncli.c (1850B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #include "uxn.h"
      5 #include "devices/system.h"
      6 #include "devices/console.h"
      7 #include "devices/file.h"
      8 #include "devices/datetime.h"
      9 
     10 /*
     11 Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick
     12 
     13 Permission to use, copy, modify, and distribute this software for any
     14 purpose with or without fee is hereby granted, provided that the above
     15 copyright notice and this permission notice appear in all copies.
     16 
     17 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     18 WITH REGARD TO THIS SOFTWARE.
     19 */
     20 
     21 Uint8
     22 emu_dei(Uxn *u, Uint8 addr)
     23 {
     24 	switch(addr & 0xf0) {
     25 	case 0x00: return system_dei(u, addr);
     26 	case 0xc0: return datetime_dei(u, addr);
     27 	}
     28 	return u->dev[addr];
     29 }
     30 
     31 void
     32 emu_deo(Uxn *u, Uint8 addr, Uint8 value)
     33 {
     34 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
     35 	u->dev[addr] = value;
     36 	switch(d) {
     37 	case 0x00: system_deo(u, &u->dev[d], p); break;
     38 	case 0x10: console_deo(&u->dev[d], p); break;
     39 	case 0xa0: file_deo(0, u->ram, &u->dev[d], p); break;
     40 	case 0xb0: file_deo(1, u->ram, &u->dev[d], p); break;
     41 	}
     42 }
     43 
     44 int
     45 main(int argc, char **argv)
     46 {
     47 	int i = 1;
     48 	Uxn u = {0};
     49 	Uint8 dev[0x100] = {0};
     50 	u.dev = dev;
     51 	if(i == argc)
     52 		return system_error("usage:", "uxncli [-v] file.rom [args..]");
     53 	if(argv[i][0] == '-' && argv[i][1] == 'v')
     54 		return system_error("Uxncli - Varvara Emulator(CLI)", "4 Mar 2024.");
     55 	if(!system_init(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++]))
     56 		return system_error("Init", "Failed to initialize uxn.");
     57 	/* eval */
     58 	u.dev[0x17] = argc - i;
     59 	if(uxn_eval(&u, PAGE_PROGRAM) && PEEK2(u.dev + 0x10)) {
     60 		console_listen(&u, i, argc, argv);
     61 		while(!u.dev[0x0f]) {
     62 			int c = fgetc(stdin);
     63 			if(c == EOF) {
     64 				console_input(&u, 0x00, CONSOLE_END);
     65 				break;
     66 			}
     67 			console_input(&u, (Uint8)c, CONSOLE_STD);
     68 		}
     69 	}
     70 	free(u.ram);
     71 	return u.dev[0x0f] & 0x7f;
     72 }