all repos — 3ByteBadVM @ d49894b7ca39843fc90bd68de454116f68e2e443

3ByteBadVM

src/emcalls.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
/* emcalls.c: This file implements emulator calls ("emcalls"). Emcalls are
analogous to system calls, except since there's no real OS, they're handled
directly by the emulator. The basic usage should revolve around adding cases
to the emcall switch-case, where each case is a special emcall that calls a
function to handle it. This is perhaps the easiest way to plug fancy features
into the emulator itself; basically the EMC instruction is like ioctl (_now_ I
see why ioctl is such a tempting serpent of a syscall to have)

x1phosura 2021
*/

#include <stdint.h>
#include <stdio.h>


void do_emcall(uint8_t emc_args[2])
{
	// emc_args[1] may be used in the future
	switch(emc_args[0]) {
	case 0x00:
		printf("I'm a zero emcall!\n"); // replace with emcall handler
		//char in_byte = getchar();
		// TODO: push inputted byte to system stack
		// Note: this will require access to the CPU struct in vm.h
		break;
	case 0x01: // basically 'putchar()' emcall
		//printf("I'm a one emcall!\n"); // DEBUG
		putchar(emc_args[1]);          // write emcall arg to STDOUT
		/* Fun idea: implement puts() in my own assembly! */
		break;
	case 0x02:
		printf("I'm a two emcall!\n"); // replace with emcall handler
		break;
	default:
		// print attempted emcall (for debugging)
		printf("EMC: 1st arg: 0x%02x, 2nd arg: 0x%02x\n", emc_args[0], emc_args[1]);
	}
}