-
Notifications
You must be signed in to change notification settings - Fork 2
/
ca0132-dsp-assembler.c
127 lines (101 loc) · 2.28 KB
/
ca0132-dsp-assembler.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* ca0132-dsp-assembler:
* Assembles a DSP assembly string. With no arguments, takes a string input.
* With arguments, takes an assembly file to read.
*/
#include "ca0132_defs.h"
static uint32_t get_next_asm_op_str(FILE *asm_file, FILE *tmp, char *buf)
{
uint32_t ret, i;
i = ret = 0;
while (fread(buf + i, sizeof(*buf), 1, asm_file)) {
if (i && (buf[i - 1] == '.') && (buf[i] == '\n')) {
i = 0;
memset(buf, 0, 0x200);
fwrite(&i, sizeof(i), 1, tmp);
continue;
}
if (buf[i] == ';') {
ret = 1;
buf[i + 1] = '\0';
break;
}
i++;
}
return ret;
}
static int read_assembly_from_file(char *asm_file_name, char *data_file_name)
{
FILE *asm_file, *data_file;
dsp_asm_data data;
char buf[0x200];
int ret = 0;
asm_file = fopen(asm_file_name, "r");
if (!asm_file) {
printf("Failed to open asm file!\n");
return 1;
}
data_file = fopen(data_file_name, "w+");
if (!data_file) {
fclose(asm_file);
printf("Failed to open data file!\n");
return 1;
}
memset(&data, 0, sizeof(data));
memset(buf, 0, sizeof(buf));
while (get_next_asm_op_str(asm_file, data_file, buf)) {
if (!get_asm_data_from_str(&data, buf)) {
printf("Invalid asm op: %s.\n", buf);
ret = 1;
goto exit;
}
fwrite(data.opcode, sizeof(data.opcode[0]),
get_dsp_op_len(data.opcode[0]), data_file);
memset(&data, 0, sizeof(data));
memset(buf, 0, sizeof(buf));
}
exit:
fclose(data_file);
fclose(asm_file);
return ret;
}
static int read_assembly_from_terminal()
{
dsp_asm_data data;
char buf[0x200];
uint32_t i = 0;
memset(&data, 0, sizeof(data));
memset(buf, 0, sizeof(buf));
while (scanf("%c", &buf[i])) {
if (buf[i] == ';') {
buf[i + 1] = '\0';
break;
}
i++;
}
if (!get_asm_data_from_str(&data, buf)) {
printf("Failed to get asm data.\n");
return 1;
}
printf("Op %s: ", data.op.op_str);
for (i = 0; i < 4; i++)
printf("0x%08x ", data.opcode[i]);
putchar('\n');
return 0;
}
int main(int argc, char **argv)
{
int ret;
ret = 0;
if ((argc > 1) && (argc < 3)) {
printf("Usage: %s <asm_file> <output_file>\n", argv[0]);
printf("Or, no arguments, and enter the assembly string into the terminal.\n");
goto exit;
}
if (argc > 2)
ret = read_assembly_from_file(argv[1], argv[2]);
else
ret = read_assembly_from_terminal();
exit:
return ret;
}