-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for RCA COSMAC 1806 uProcessors ##arch
- Loading branch information
Showing
12 changed files
with
937 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
OBJ_COSMAC=p/cosmac/plugin.o | ||
|
||
STATIC_OBJ+=${OBJ_COSMAC} | ||
TARGET_COSMAC=arch_cosmac.${EXT_SO} | ||
|
||
ALL_TARGETS+=${TARGET_COSMAC} | ||
|
||
${TARGET_COSMAC}: ${OBJ_COSMAC} | ||
${CC} $(call libname,arch_cosmac) ${CFLAGS} -o arch_i4004.${EXT_SO} ${OBJ_COSMAC} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* radare - LGPL - Copyright 2024 - pancake */ | ||
|
||
#include <r_asm.h> | ||
|
||
static int replace(int argc, const char *argv[], char *newstr) { | ||
#define MAXPSEUDOOPS 10 | ||
int i, j, k, d; | ||
char ch; | ||
struct { | ||
int narg; | ||
const char *op; | ||
const char *str; | ||
int args[MAXPSEUDOOPS]; | ||
} ops[] = { | ||
{ 0, "ret", "return", { 0 } }, | ||
{ 1, "bnz", "if (!zero) goto #", { 1 } }, | ||
{ 0, NULL } | ||
}; | ||
if (!newstr) { | ||
return false; | ||
} | ||
|
||
for (i = 0; ops[i].op; i++) { | ||
if (ops[i].narg) { | ||
if (argc - 1 != ops[i].narg) { | ||
continue; | ||
} | ||
} | ||
if (!strcmp (ops[i].op, argv[0])) { | ||
if (newstr) { | ||
d = 0; | ||
j = 0; | ||
ch = ops[i].str[j]; | ||
for (j = 0, k = 0; ch != '\0'; j++, k++) { | ||
ch = ops[i].str[j]; | ||
if (ch == '#') { | ||
if (d >= MAXPSEUDOOPS) { | ||
// XXX Shouldn't ever happen... | ||
continue; | ||
} | ||
int idx = ops[i].args[d]; | ||
d++; | ||
if (idx <= 0) { | ||
// XXX Shouldn't ever happen... | ||
continue; | ||
} | ||
const char *w = argv[idx]; | ||
if (w) { | ||
strcpy (newstr + k, w); | ||
k += strlen (w) - 1; | ||
} | ||
} else { | ||
newstr[k] = ch; | ||
} | ||
} | ||
newstr[k] = '\0'; | ||
} | ||
// r_str_replace_char (newstr, '{', '('); | ||
// r_str_replace_char (newstr, '}', ')'); | ||
return true; | ||
} | ||
} | ||
|
||
/* TODO: this is slow */ | ||
newstr[0] = '\0'; | ||
for (i = 0; i < argc; i++) { | ||
strcat (newstr, argv[i]); | ||
strcat (newstr, (!i || i == argc - 1)? " " : ","); | ||
} | ||
return false; | ||
} | ||
|
||
static char *parse(RAsmPluginSession *aps, const char *data) { | ||
char w0[256], w1[256], w2[256], w3[256], w4[256]; | ||
int i, len = strlen (data); | ||
char *buf, *ptr, *optr; | ||
|
||
if (len >= sizeof (w0)) { | ||
return false; | ||
} | ||
// malloc can be slow here :? | ||
if (!(buf = malloc (len + 1))) { | ||
return false; | ||
} | ||
memcpy (buf, data, len + 1); | ||
#if 0 | ||
buf = r_str_replace (buf, "(0", "0", true); | ||
buf = r_str_replace (buf, "p)", "p", true); | ||
buf = r_str_replace (buf, "x)", "x", true); | ||
#endif | ||
#if 0 | ||
r_str_replace_char (buf, '(', '{'); | ||
r_str_replace_char (buf, ')', '}'); | ||
#endif | ||
const char *op0 = buf; | ||
if (!strcmp (op0, "ret") || !strcmp (op0, "sret")) { | ||
return strdup ("return r0"); | ||
} | ||
char *str = malloc (strlen (data) + 128); | ||
strcpy (str, data); | ||
if (*buf) { | ||
*w0 = *w1 = *w2 = *w3 = *w4 = '\0'; | ||
ptr = strchr (buf, ' '); | ||
if (ptr) { | ||
*ptr = '\0'; | ||
ptr = (char *)r_str_trim_head_ro (ptr + 1); | ||
strncpy (w0, buf, sizeof (w0) - 1); | ||
strncpy (w1, ptr, sizeof (w1) - 1); | ||
optr = ptr; | ||
if (ptr && *ptr == '[') { | ||
ptr = strchr (ptr + 1, ']'); | ||
} | ||
if (!ptr) { | ||
R_LOG_ERROR ("Unbalanced bracket"); | ||
free (str); | ||
free (buf); | ||
return false; | ||
} | ||
ptr = strchr (ptr, ','); | ||
if (ptr) { | ||
*ptr = '\0'; | ||
ptr = (char *)r_str_trim_head_ro (ptr + 1); | ||
strncpy (w1, optr, sizeof (w1) - 1); | ||
strncpy (w2, ptr, sizeof (w2) - 1); | ||
optr = ptr; | ||
ptr = strchr (ptr, ','); | ||
if (ptr) { | ||
*ptr = '\0'; | ||
ptr = (char *)r_str_trim_head_ro (ptr + 1); | ||
strncpy (w2, optr, sizeof (w2) - 1); | ||
strncpy (w3, ptr, sizeof (w3) - 1); | ||
optr = ptr; | ||
ptr = strchr (ptr, ','); | ||
if (ptr) { | ||
*ptr = '\0'; | ||
ptr = (char *)r_str_trim_head_ro (ptr + 1); | ||
strncpy (w3, optr, sizeof (w3) - 1); | ||
strncpy (w4, ptr, sizeof (w4) - 1); | ||
} | ||
} | ||
} | ||
} | ||
{ | ||
const char *wa[] = { w0, w1, w2, w3, w4 }; | ||
int nw = 0; | ||
for (i = 0; i < 5; i++) { | ||
if (wa[i][0]) { | ||
nw++; | ||
} | ||
} | ||
replace (nw, wa, str); | ||
} | ||
} | ||
free (buf); | ||
return r_str_fixspaces (str); | ||
} | ||
|
||
RAsmPlugin r_asm_plugin_cosmac = { | ||
.meta = { | ||
.name = "cosmac", | ||
.desc = "pseudocode for cosmac cdp 1802", | ||
.author = "pancake", | ||
.license = "MIT", | ||
}, | ||
.parse = parse, | ||
}; | ||
|
||
#ifndef R2_PLUGIN_INCORE | ||
R_API RLibStruct radare_plugin = { | ||
.type = R_LIB_TYPE_ASM, | ||
.data = &r_asm_plugin_cosmac, | ||
.version = R2_VERSION | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# TODO: read arch/p/cosmac/plugin.c and finish this | ||
ldn=load D with (R) | ||
idl=idle or wait for interrupt or dma request | ||
inc=increment | ||
dec=decrement | ||
irx=increment register X | ||
plo=put low register | ||
phi=put high register | ||
ghi=get high register | ||
sep=set program counter register | ||
sex=set register as data pointer | ||
stpc=stop counter | ||
sret=standard return to (R) | ||
rsxd=store register in memory | ||
dadd=decimal add | ||
dsm=decimal substract memory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
OBJ_COSMACPSEUDO+=$(LIBR)/arch/p/cosmac/pseudo.o | ||
|
||
TARGET_COSMACPSEUDO=parse_cosmac_pseudo.${EXT_SO} | ||
STATIC_OBJ+=${OBJ_COSMACPSEUDO} | ||
LIBDEPS=-L../../util -lr_util | ||
LIBDEPS+=-L../../flag -lr_flag | ||
|
||
ifeq ($(WITHPIC),1) | ||
ALL_TARGETS+=${TARGET_COSMACPSEUDO} | ||
${TARGET_COSMACPSEUDO}: ${OBJ_COSMACPSEUDO} | ||
${CC} $(call libname,parse_cosmac_pseudo) ${LIBDEPS} $(LDFLAGS) \ | ||
$(LDFLAGS_SHARED) ${CFLAGS} -o ${TARGET_COSMACPSEUDO} ${OBJ_COSMACPSEUDO} | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ att2intel | |
avr | ||
bpf | ||
chip8 | ||
cosmac | ||
dalvik | ||
evm | ||
gb | ||
|