Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for RCA COSMAC 1806 uProcessors ##arch #23832

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dist/plugins-cfg/plugins.def.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ STATIC="
anal.null
anal.a2f
arch.6502
arch.cosmac
arch.6502_cs
arch.8051
arch.alpha
Expand Down Expand Up @@ -282,6 +283,7 @@ lang.v
lang.vala
lang.zig
asm.6502
asm.cosmac
asm.arm
asm.java
asm.att2intel
Expand Down
9 changes: 9 additions & 0 deletions libr/arch/p/cosmac.mk
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}
706 changes: 706 additions & 0 deletions libr/arch/p/cosmac/plugin.c

Large diffs are not rendered by default.

174 changes: 174 additions & 0 deletions libr/arch/p/cosmac/pseudo.c
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
1 change: 1 addition & 0 deletions libr/asm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ OBJS+=d/arc.o
OBJS+=d/gb.o
OBJS+=d/evm.o
OBJS+=d/arm.o
OBJS+=d/cosmac.o
OBJS+=d/avr.o
OBJS+=d/chip8.o
OBJS+=d/java.o
Expand Down
2 changes: 1 addition & 1 deletion libr/asm/d/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FILES=6502 8051 m68k x86 arc arm avr bpf LH5801 ppc z80 mips sparc sh xtensa
FILES+=i8080 java i4004 dalvik msp430 lm32 s390 tms320 propeller v810 v850
FILES+=pic18c chip8 tricore bf pickle riscv evm sm5xx gb stm8
FILES+=pic18c chip8 tricore bf pickle riscv evm sm5xx gb stm8 cosmac
F_SDB=$(addsuffix .sdb,${FILES})
SDB=../../../subprojects/sdb/sdb

Expand Down
16 changes: 16 additions & 0 deletions libr/asm/d/cosmac.sdb.txt
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
1 change: 1 addition & 0 deletions libr/asm/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ r_asm_sources = [
'../arch/p/null/pseudo.c',
'../arch/p/tricore/pseudo.c',
'../arch/p/z80/pseudo.c',
'../arch/p/cosmac/pseudo.c',
'../arch/p/gb/pseudo.c',
'../arch/p/wasm/pseudo.c',
'../arch/p/v850/pseudo.c',
Expand Down
13 changes: 13 additions & 0 deletions libr/asm/p/cosmac.mk
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
1 change: 1 addition & 0 deletions libr/include/r_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ extern const RArchPlugin r_arch_plugin_xap;
extern const RArchPlugin r_arch_plugin_xcore_cs;
extern const RArchPlugin r_arch_plugin_xtensa;
extern const RArchPlugin r_arch_plugin_z80;
extern const RArchPlugin r_arch_plugin_cosmac;

#ifdef __cplusplus
}
Expand Down
23 changes: 12 additions & 11 deletions libr/include/r_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,28 @@ extern RAsmPlugin r_asm_plugin_6502;
extern RAsmPlugin r_asm_plugin_arm;
extern RAsmPlugin r_asm_plugin_att2intel;
extern RAsmPlugin r_asm_plugin_avr;
extern RAsmPlugin r_asm_plugin_bpf;
extern RAsmPlugin r_asm_plugin_chip8;
extern RAsmPlugin r_asm_plugin_cosmac;
extern RAsmPlugin r_asm_plugin_dalvik;
extern RAsmPlugin r_asm_plugin_dummy;
extern RAsmPlugin r_asm_plugin_evm;
extern RAsmPlugin r_asm_plugin_gb;
extern RAsmPlugin r_asm_plugin_java;
extern RAsmPlugin r_asm_plugin_m68k;
extern RAsmPlugin r_asm_plugin_mips;
extern RAsmPlugin r_asm_plugin_null;
extern RAsmPlugin r_asm_plugin_pickle;
extern RAsmPlugin r_asm_plugin_ppc;
extern RAsmPlugin r_asm_plugin_riscv;
extern RAsmPlugin r_asm_plugin_sh;
extern RAsmPlugin r_asm_plugin_stm8;
extern RAsmPlugin r_asm_plugin_tms320;
extern RAsmPlugin r_asm_plugin_tricore;
extern RAsmPlugin r_asm_plugin_v850;
extern RAsmPlugin r_asm_plugin_wasm;
extern RAsmPlugin r_asm_plugin_riscv;
extern RAsmPlugin r_asm_plugin_x86;
extern RAsmPlugin r_asm_plugin_z80;
extern RAsmPlugin r_asm_plugin_tms320;
extern RAsmPlugin r_asm_plugin_v850;
extern RAsmPlugin r_asm_plugin_bpf;
extern RAsmPlugin r_asm_plugin_stm8;
extern RAsmPlugin r_asm_plugin_evm;
extern RAsmPlugin r_asm_plugin_null;
extern RAsmPlugin r_asm_plugin_gb;
extern RAsmPlugin r_asm_plugin_pickle;
extern RAsmPlugin r_asm_plugin_tricore;
extern RAsmPlugin r_asm_plugin_java;

#endif

Expand Down
1 change: 1 addition & 0 deletions test/db/cmd/cmd_list
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ att2intel
avr
bpf
chip8
cosmac
dalvik
evm
gb
Expand Down
Loading