summaryrefslogtreecommitdiffstats
path: root/src/emu/opc.c
blob: 3d62479b1dadfd40acb0e4754d71252b77deb1be (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <string.h>

#include "opc.h"

typedef struct opc_mapping {
	const char *name;
	uint32_t opcode;
} opc_mapping_t;

opc_mapping_t opc_map[] = {
	{ "ADD",  OPC_ADD },
	{ "SUB",  OPC_SUB },
	{ "MUL",  OPC_MUL },
	{ "DIV",  OPC_DIV },
	{ "MOD",  OPC_MOD },
	{ "AND",  OPC_AND },
	{ "OR",   OPC_OR },
	{ "CMP",  OPC_CMP },
	{ "EQ",   OPC_EQ },
	{ "NE",   OPC_NE },
	{ "LT",   OPC_LT },
	{ "GE",   OPC_GE },
	{ "LE",   OPC_LE },
	{ "GT",   OPC_GT },
	{ "MOV",  OPC_MOV },
	{ "LW",   OPC_LW },
	{ "SW",   OPC_SW },
	{ "PUSH", OPC_PUSH },
	{ "POP",  OPC_POP },
	{ "BEZ",  OPC_BEZ },
	{ "JMP",  OPC_JMP },
	{ "CALL", OPC_CALL },
	{ "RET",  OPC_RET },
	{ "SYS",  OPC_SYS },
	{ NULL,  0 }
};

uint32_t mnemonic2opc(const char *mnemonic)
{
	for (uint8_t i = 0; opc_map[i].name; i++) {
		if (strcmp(opc_map[i].name, mnemonic) == 0)
			return opc_map[i].opcode << 26;
	}

	return ~0;
}

const char *opc2mnemonic(uint32_t IR)
{
	uint32_t opcode = IR >> 26;

	for (uint8_t i = 0; opc_map[i].name; i++) {
		if (opc_map[i].opcode == opcode)
			return opc_map[i].name;
	}

	return NULL;
}