wm: 5551

Download patch

ref: 682b4cec8835d109e02ab5deb1c4934984fc9eb6
author: mkf <mkf>
date: Thu May 4 03:12:57 EDT 2023

import sources.

--- /dev/null
+++ b/5551a.c
@@ -1,0 +1,49 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+
+enum
+{
+	NOT = 1,
+	AND,
+	XOR,
+	ADD,
+	RST, /* reset the machine state */
+	CPY, /* copy stuff from a reg or addr to another */
+	SET, /* sets mode to bool and int */
+	OUT
+};
+
+void main(int argc, char* argv[])
+{
+	unsigned int a, b;
+	char string[16], *op;
+	FILE *fd = open("5551.out", O_WRONLY);
+	if(fd < 0)
+	{
+		fprintf(stderr, "unable to open file\n");
+		return;
+	}
+	while(scanf("%s %[^\n]", op, string) > 0) /* %s removes spaces, what did i expected? */
+	{
+		sscanf(string, "%u %u", &a, &b);
+		if(!strcmp(op, "NOT"))
+			printf("%u %u\n", NOT, a);
+		if(!strcmp(op, "AND"))
+			printf("%u %u %u\n", AND, a, b);
+		if(!strcmp(op, "XOR"))
+			printf("%u %u %u\n", XOR, a, b);
+//		if(!strcmp(op, "RST"))
+			// reset():
+		if(!strcmp(op, "CPY"))
+			printf("%u %u %u\n", CPY, a, b);
+		if(!strcmp(op, "SET"))
+			printf("%u %u\n", SET, a);
+		if(!strcmp(op, "OUT"))
+			printf("%u %u\n", OUT, a);
+		if(!strcmp(op, "DIE"))
+			return;
+	}
+	return;
+}
--- /dev/null
+++ b/5551e.c
@@ -1,0 +1,85 @@
+#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";
+	}
+}