-
Notifications
You must be signed in to change notification settings - Fork 2
/
opcode.c
339 lines (287 loc) · 7.43 KB
/
opcode.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdio.h>
#include <string.h>
#include "bitops.h"
#include "opcode.h"
#include "ast2600.h"
void start_code(FILE *fp)
{
uint32_t code = START_CODE;
fwrite(&code, 1, sizeof(code), fp);
}
void quit_code(FILE *fp)
{
rom_op_quit_t code = { .cmd.b.cmd = 0xf };
fwrite(&code, 1, sizeof(code), fp);
}
void wr_code(FILE *fp, uint32_t addr, uint32_t length, uint32_t *data)
{
rom_op_wr_t code = { .cmd.b.cmd = 0x1,
.cmd.b.num = length - 1,
.addr = addr };
fwrite(&code, 1, sizeof(code), fp);
fwrite(data, sizeof(uint32_t), length, fp);
}
void wr_single(FILE *fp, uint32_t addr, uint32_t value)
{
uint32_t data = value;
wr_code(fp, addr, 1, &data);
}
/**
* delay: micro-second
*/
void waiteq_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t target,
uint32_t delay)
{
uint32_t tick = delay * TICKS_PER_US;
rom_op_waiteq_t code = { .cmd.b.cmd = 0x8, .cmd.b.num = tick - 1 };
code.addr = addr;
code.data = target;
code.mask = mask;
fwrite(&code, 1, sizeof(code), fp);
}
void waitne_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t target,
uint32_t delay)
{
uint32_t tick = delay * TICKS_PER_US;
rom_op_waitne_t code = { .cmd.b.cmd = 0x9, .cmd.b.num = tick - 1 };
code.addr = addr;
code.data = target;
code.mask = mask;
fwrite(&code, 1, sizeof(code), fp);
}
void _jeq_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t target,
int32_t offset)
{
rom_op_jmp_t code = { .cmd.b.cmd = 0xc };
code.cmd.b.num = (uint32_t)offset;
code.addr = addr;
code.mask = mask;
code.data = target;
fwrite(&code, 1, sizeof(code), fp);
}
void _jne_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t target,
int32_t offset)
{
rom_op_jmp_t code = { .cmd.b.cmd = 0xd };
code.cmd.b.num = (uint32_t)offset;
code.addr = addr;
code.mask = mask;
code.data = target;
fwrite(&code, 1, sizeof(code), fp);
}
void delay_code(FILE *fp, uint32_t delay)
{
rom_op_delay_t code = { .cmd.b.cmd = 0x4 };
code.cmd.b.num = delay * TICKS_PER_US;
fwrite(&code, 1, sizeof(code), fp);
}
/**
* *addr = (*addr & mask) + value
*/
void rmw_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t value)
{
rom_op_rmw_t code = { .cmd.b.cmd = 0x2 };
code.addr = addr;
code.mask = mask;
code.data = value;
fwrite(&code, 1, sizeof(code), fp);
}
/**
* *addr = (*addr & ~value) + 0
*/
void clrbit_code(FILE *fp, uint32_t addr, uint32_t value)
{
return rmw_code(fp, addr, ~(value), 0);
}
/**
* *addr = (*addr & ~value) + value
*/
void setbit_code(FILE *fp, uint32_t addr, uint32_t value)
{
return rmw_code(fp, addr, ~(value), value);
}
/**
* *addr = (*addr & 0xffffffff) + value
*/
void add_code(FILE *fp, uint32_t addr, uint32_t value)
{
return rmw_code(fp, addr, GENMASK(31, 0), value);
}
void cp_code(FILE *fp, uint32_t src, uint32_t dst, uint32_t size_dw)
{
rom_op_cp_t code = { .cmd.b.cmd = 0x3, .cmd.b.num = size_dw };
code.src = src;
code.dst = dst;
fwrite(&code, 1, sizeof(code), fp);
}
/**
* extended commands
*/
#define N_LABEL 32
#define N_LABEL_NAME 32
#define N_CODE 512
#define SB_HDR_SIZE_BYTE 0x20
#define SB_HDR_SIZE_DW (SB_HDR_SIZE_BYTE / sizeof(uint32_t))
#define CM3_BIN_SIZE_DW (47 * 1024 / sizeof(uint32_t))
typedef struct label_s {
fpos_t position;
char name[N_LABEL_NAME];
} label_t;
typedef struct rom_lables_s {
label_t labels[N_LABEL];
int count;
} rom_labels_t;
typedef struct jmp_code_s {
label_t jmps[N_LABEL];
int count;
} jmp_code_t;
/* log "jump" instructions */
/**
* global data
*/
rom_labels_t rom_labels = { .count = 0 };
jmp_code_t jmp_list = { .count = 0 };
void declare_label(FILE *fp, char *name)
{
label_t *lab = &rom_labels.labels[rom_labels.count++];
strcpy(&lab->name[0], name);
fgetpos(fp, &lab->position);
}
void jeq_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t target,
char *label_name)
{
label_t *jmp = &jmp_list.jmps[jmp_list.count++];
fgetpos(fp, &jmp->position);
strcpy(&jmp->name[0], label_name);
printf("[%08llx] jeq to %s\n", (long long)vPOS(jmp->position),
jmp->name);
_jeq_code(fp, addr, mask, target, 0);
}
void jne_code(FILE *fp, uint32_t addr, uint32_t mask, uint32_t target,
char *label_name)
{
label_t *jmp = &jmp_list.jmps[jmp_list.count++];
fgetpos(fp, &jmp->position);
strcpy(&jmp->name[0], label_name);
printf("[%08llx] jne to %s\n", (long long)vPOS(jmp->position),
jmp->name);
_jne_code(fp, addr, mask, target, 0);
}
void jmp_code(FILE *fp, char *label_name)
{
label_t *jmp = &jmp_list.jmps[jmp_list.count++];
fgetpos(fp, &jmp->position);
strcpy(&jmp->name[0], label_name);
printf("[%08llx] jmp to %s\n", (long long)vPOS(jmp->position),
jmp->name);
/* trick: assert CHIP_ID != 0 */
_jne_code(fp, SCU_BASE + 0x4, 0, 0xffffffff, 0);
}
void print_labels(void)
{
int i;
printf("---------------------------------------------------------------\n");
printf("list of labels\n");
printf("---------------------------------------------------------------\n");
for (i = 0; i < rom_labels.count; i++) {
label_t *lab = &rom_labels.labels[i];
printf("[%08llx](%8llu) %s\n", (long long)vPOS(lab->position),
(long long)vPOS(lab->position), lab->name);
}
}
/**
* +------------------+
* | start |
* +------------------+
* | ... |
* +------------------+
* | jump inst. start | -> jmp_list.jmps[i].position
* +------------------+
* | ... |
* +------------------+
* | jump inst. end |
* +------------------+
* | next inst. | -> jmp_list.jmps[i].position + sizeof(rom_op_jmp_t)
* +------------------+
* | ... |
* +------------------+
* | label to jump | -> rom_labels.labels[j].position
* +------------------+
*/
void link_labels(FILE *fp)
{
int i, j;
for (i = 0; i < jmp_list.count; i++) {
for (j = 0; j < rom_labels.count; j++) {
if (0 == strcmp(jmp_list.jmps[i].name,
rom_labels.labels[j].name)) {
rom_op_jmp_t code;
fsetpos(fp, &jmp_list.jmps[i].position);
fread(&code, 1, sizeof(code), fp);
code.cmd.b.num =
vPOS(rom_labels.labels[j].position) -
(vPOS(jmp_list.jmps[i].position) +
sizeof(rom_op_jmp_t));
//printf("code: %08x\n", code.cmd.w);
fsetpos(fp, &jmp_list.jmps[i].position);
fwrite(&code, 1, sizeof(code), fp);
}
}
}
}
void print_rom_patch(FILE *fp)
{
uint32_t data, size = 0;
printf("---------------------------------------------------------------\n");
printf("rom code patch\n");
printf("---------------------------------------------------------------\n");
fseek(fp, 0, SEEK_SET);
while (fread(&data, sizeof(uint32_t), 1, fp)) {
printf("[%08x] %08x\n", size, data);
size += 4;
}
printf("total size: %d bytes\n", size);
}
void parse_wr_code(FILE *fp)
{
rom_op_wr_t code;
uint32_t data;
int i;
fread(&code, 1, sizeof(code), fp);
printf("write command: addr %08x, value ", code.addr);
for (i = 0; i <= code.cmd.b.num; i++) {
fread(&data, 1, sizeof(data), fp);
printf("%08x ", data);
}
printf("\n");
}
/**
* @brief parse patch OP code
* @todo only wr command is done, others are TBD.
*/
void parse_opcode(FILE *fp)
{
uint32_t data;
rom_op_cmd_t code;
fpos_t pos;
fread(&data, 1, sizeof(uint32_t), fp);
if (data != START_CODE) {
printf("start code not found\n");
return;
}
while (1) {
fread(&code, 1, sizeof(code), fp);
fgetpos(fp, &pos);
vPOS(pos) -= sizeof(code);
fsetpos(fp, &pos);
printf("cmd: %02x, num:%06x -> ", code.b.cmd, code.b.num);
switch (code.b.cmd) {
case 0x1:
parse_wr_code(fp);
break;
default:
printf("unknown command\n");
return;
}
}
}