wm: 5552

ref: 682b4cec8835d109e02ab5deb1c4934984fc9eb6
dir: /5551e.c/

View raw version
#include <stdbool.h>
#include <stdio.h>

#define MMAX ( 1 << 8 )
unsigned int reg[8];
typedef struct
{
 	bool carry, overflow, nsign, sub, parity;
	bool mode; /* 0 bool, 1 int */
}flag_t;

enum
{
	/* logical num */
	NOT = 1,
	AND,
	XOR,
	ADD,
	RST, /* reset the machine state */
	CPY, /* copy stuff from a reg or addr to another */
	SET,
	OUT,

};

bool add_bit(bool a, bool b, bool cin, bool *cout)
{
	bool out, temp;
	temp = a & b;
	out = ((a ^ b) | (temp & cin));
	temp = a ^ b;
	cout = ((a & b) | (temp & cin));
	return temp;
}

void main()
{
	flag_t flags;
	char string[16];
	unsigned int memory[MMAX], op, a, b, def = 0;
	memory[0] = 0;
	memory[MMAX-1] = -1;
	while(scanf("%u %[^\n]", &op, string))
	{
		sscanf(string, "%u %u", &a, &b);
		switch(op)
		{
			case NOT:
				reg[def] = !a;
				break;
			case AND:
				reg[def] = a & b;
				break;
			case XOR:
				reg[def] = a ^ b;
				break;
			case ADD:
				reg[def] = add_bit(a, b, 0, &flags.carry);
				break;
			case RST:
				// reset():
				break;
			case CPY:
				reg[b] = reg[a];
				break;
			case SET:
				if((a < 0) || (a > 8))
				{
					fprintf(stderr, "out of regs\n");
					return;
				}
				def = a;
				break;
			case OUT:
				printf("%u\n", reg[a]);
				break;
			default:
				fprintf(stderr, "unkown op\n");
				return;
		}
		/* clear them */
		a = NULL, b = NULL, op = NULL;
		string = "\0";
	}
}