-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.c
422 lines (343 loc) · 8.88 KB
/
decode.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//parser output_filename first.obj second.obj third.obj ….
// output_filename: file to output
// first.obj, ...: LC4 obj files to load & decode
// maintain array of 64k (64 * 1024) entries corresponding to
// entire contents of LC4 memory
// before any object files are loaded, clear all to 0 (hexdump to view contents)
/* file format for lc4 obj files:
1. Code: 3-word header (xCADE, <addr>, <n>), n-word body
2. Data: 3-word header (xDADA, <addr>, <n>) initial data values
3. Symbol:
4. File name
5. Line number
*/
/* Output format
- list all non-zero contents of memory after all obj files loaded
- seq of lines listing address as 4-bit hex value followed by contents
- print out instruction for code section
*/
/*
// INSN_TYPE : This enumerated type is used to represent all of the types of LC4 instructions
// Note that ADDI and ANDI are to be used to denote ADD Immediate and AND Immediate respectively.
typedef enum {ILLEGAL_INSN,
NOP, BRp, BRz, BRzp, BRn, BRnp, BRnz, BRnzp,
ADD, MUL, SUB, DIV, ADDI,
MOD,
AND, NOT, OR, XOR, ANDI,
LDR, STR,
CONST, HICONST,
CMP, CMPU, CMPI, CMPIU,
SLL, SRA, SRL,
JSRR, JSR,
JMPR, JMP,
TRAP, RTI} INSN_TYPE;
// Structure used to represent LC4 instructions
typedef struct {
INSN_TYPE type; // Type of the instruction
int Rd, Rs, Rt; // Register fields
short int immediate; // Immediate field of instruction
} INSN;
*/
#include <stdio.h>
#include <stdlib.h>
#include "decode.h"
const char* type_string[] = {"INVALID INSTRUCTION",
"NOP", "BRp", "BRz", "BRzp", "BRn", "BRnp", "BRnz", "BRnzp", //1-8
"ADD", "MUL", "SUB", "DIV", "ADDI", //9-13
"MOD", //14
"AND", "NOT", "OR", "XOR", "ANDI", //15-19
"LDR", "STR", //20-21
"CONST", "HICONST", //22-23
"CMP", "CMPU", "CMPI", "CMPIU", //24-27
"SLL", "SRA", "SRL", //28-30
"JSRR", "JSR", //31-32
"JMPR", "JMP", //33-34
"TRAP", "RTI"}; //35-36
// decode_insn : This function takes as input an unsigned 16 bit pattern and decodes that into
// an INSN structure that breaks out the various instruction components.
INSN decode_insn (unsigned short int insn) {
unsigned short int head = (insn >> 12); // last 4 bits
if (head == 0) { //nop/br
return decode_branch(insn);
} else if (head == 1) { //arithmetic
return decode_arithmetic(insn);
} else if (head == 10) { // mod or shifts
return decode_shift(insn);
} else if (head == 5) { //logical
return decode_logic(insn);
} else if (head == 6) { //ldr
return decode_load(insn);
} else if (head == 7) { //str
return decode_store(insn);
} else if (head == 9) { //const
return decode_const(insn);
} else if (head == 13) { //hiconst
return decode_hiconst(insn);
} else if (head == 2) { //comparison
return decode_comp(insn);
} else if (head == 4) { //jsrr/jsr
return decode_jsr(insn);
} else if (head == 12) { //jmpr/jmp
return decode_jump(insn);
} else if (head == 15) { //trap
return decode_trap(insn);
} else if (head == 8) { //rti
return decode_rti(insn);
}
//not matched -> return illegal
INSN instr;
instr.type = ILLEGAL_INSN;
return instr;
}
// Print a readable version of the instruction out to the specified file
/* Formatting:
"Type"
" Rd," // if not null
" Rs," // if not null
" Rt" // if not null
" #immediate" // if not null
exceptions:
STR Rt, Rs, IMM6
JSRR Rs (no comma after Rs)
JMPR Rs (no comma after Rs)
*/
void print_insn (FILE *theFile, INSN theINSN) {
fprintf(theFile, "%s", type_string[theINSN.type]);
if (theINSN.type == STR) { //print " Rt, Rs, #immediate"
fprintf(theFile, " %d,", theINSN.Rt);
fprintf(theFile, " %d,", theINSN.Rs);
fprintf(theFile, " #%d", theINSN.immediate);
} else if (theINSN.type == JSRR || theINSN.type == JMPR) {
fprintf(theFile, " %d", theINSN.Rs);
} else { //general format
if (theINSN.Rd != null) {
fprintf(theFIle, " %d,", theINSN.Rd);
}
if (theINSN.Rs != null) {
fprintf(theFIle, " %d,", theINSN.Rs);
}
if (theINSN.Rt != null) {
fprintf(theFIle, " %d", theINSN.Rt);
}
if (theINSN.immediate != null) {
fprintf(theFIle, " #%d", theINSN.immediate);
}
}
}
INSN decode_branch(unsigned short int insn) {
INSN instr;
unsigned short int code = (insn >> 9); //3-digit code
if (code == 0) {
instr.type = NOP;
return instr;
} else if (code == 1) {
instr.type = BRp;
} else if (code == 2) {
instr.type = BRz;
} else if (code == 3) {
instr.type = BRzp;
} else if (code == 4) {
instr.type = BRn;
} else if (code == 5) {
instr.type = BRnp;
} else if (code == 6) {
instr.type = BRnz;
} else if (code == 7) {
instr.type = BRnzp;
}
short int num = insn & 0x1FF;
int sign = (num >> 8) & 1;
if (sign == 1) {
num = num | 0xFE00;
}
instr.immediate = num;
return instr;
}
INSN decode_arithmetic(unsigned short int insn) {
INSN instr;
if (((insn >> 5) & 1) == 1) { //immediate
instr.type = ADDI;
instr.Rd = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
short int num = insn & 0x1F;
int sign = (num >> 4) & 1;
if (sign == 1) {
num = num | 0xFFE0;
}
instr.immediate = num;
} else {
int code = (insn >> 3) & 7;
instr.Rd = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
instr.Rt = insn & 7;
if (code == 0) {
instr.type = ADD;
} else if (code == 1) {
instr.type = MUL;
} else if (code == 2) {
instr.type = SUB;
} else if (code == 3) {
instr.type = DIV;
}
}
return instr;
}
INSN decode_shift(unsigned short int insn) { //includes shift & mod
INSN instr;
int code = (insn >> 4) & 3;
instr.Rd = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
if (code == 0) {
instr.type = SLL;
instr.immediate = (insn & 0xF);
} else if (code == 1) {
instr.type = SRA;
instr.immediate = (insn & 0xF);
} else if (code == 2) {
instr.type = SRL;
instr.immediate = (insn & 0xF);
} else if (code == 3) {
instr.type = MOD;
Rt = (insn & 7);
}
return instr;
}
INSN decode_logic(unsigned short int insn) {
if (((insn >> 5) & 1) == 1) { //immediate
instr.type = ANDI;
instr.Rd = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
short int num = insn & 0x1F;
int sign = (num >> 4) & 1;
if (sign == 1) {
num = num | 0xFFE0;
}
instr.immediate = num;
} else {
int code = (insn >> 3) & 7;
instr.Rd = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
if (code == 0) {
instr.type = AND;
instr.Rt = insn & 7;
} else if (code == 1) {
instr.type = NOT;
} else if (code == 2) {
instr.type = OR;
instr.Rt = insn & 7;
} else if (code == 3) {
instr.type = XOR;
instr.Rt = insn & 7;
}
}
return instr;
}
INSN decode_load(unsigned short int insn) {
INSN instr;
instr.Rd = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
short int num = insn & 0x3F;
int sign = (num >> 5) & 1;
if (sign == 1) {
num = num | 0xFFC0;
}
instr.immediate = num;
return instr;
}
INSN decode_store(unsigned short int insn) {
INSN instr;
instr.Rt = (insn >> 9) & 7;
instr.Rs = (insn >> 6) & 7;
short int num = insn & 0x3F;
int sign = (num >> 5) & 1;
if (sign == 1) {
num = num | 0xFFC0;
}
instr.immediate = num;
return instr;
}
INSN decode_const(unsigned short int insn) {
INSN instr;
instr.Rd = (insn >> 9) & 7;
short int num = insn & 0x1FF;
int sign = (num >> 8) & 1;
if (sign == 1) {
num = num | 0xFE00;
}
instr.immediate = num;
return instr;
}
INSN decode_hiconst(unsigned short int insn) {
INSN instr;
instr.Rd = (insn >> 9) & 7;
instr.immediate = insn & 0xFF;
return instr;
}
INSN decode_comp(unsigned short int insn) {
INSN instr;
instr.Rs = (insn >> 9) & 7;
int code = (insn >> 7) & 3;
if (code == 0) {
} else if (code == 1) {
instr.type = CMP;
instr.Rt = (insn & 7);
} else if (code == 2) {
instr.type = CMPU;
instr.Rt = insn & 7;
} else if (code == 3) {
instr.type = CMPI;
short int num = insn & 0x7F;
int sign = (insn >> 6) & 1;
if (sign == 1) {
num = num | 0xFF80;
}
instr.immediate = num;
} else if (code == 4) {
instr.type = CMPIU;
instr.immediate = insn & 0x7F;
}
return instr;
}
INSN decode_jsr(unsigned short int insn) {
INSN instr;
if (((insn >> 11) & 1) == 0) { //jsrr
instr.type = JSRR;
instr.Rs = (insn >> 6) & 7;
} else {
instr.type = JSR;
short int num = insn & 0x07FF;
int sign = (insn >> 10) & 1;
if (sign == 1) {
num = num | 0xF800;
}
instr.immediate = num;
}
return instr;
}
INSN decode_jump(unsigned short int insn) {
INSN instr;
if (((insn >> 11) & 1) == 0) { //jmpr
instr.type = JMPR;
instr.Rs = (insn >> 6) & 7;
} else { //jmp
instr.type = JMP;
short int num = insn & 0x07FF;
int sign = (insn >> 10) & 1;
if (sign == 1) {
num = num | 0xF800;
}
instr.immediate = num;
}
return instr;
}
INSN decode_trap(unsigned short int insn) {
INSN instr;
instr.type = TRAP;
instr.immediate = insn & 0xFF;
return instr;
}
INSN decode_rti(unsigned short int insn) {
INSN instr;
instr.type = RTI;
return instr;
}