-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc3.c
648 lines (557 loc) · 18 KB
/
lc3.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* main.c - Main source file of LC-3
* Written by Ercan Ersoy.
*/
// Define memory size
#define MEMORY 65536
// Includes
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
// Memory declaration
uint16_t memory[MEMORY];
// Enumerator type for registers and register count
enum
{
R_R0 = 0, // Register R0
R_R1, // Register R1
R_R2, // Register R2
R_R3, // Register R3
R_R4, // Register R4
R_R5, // Register R5
R_R6, // Register R6
R_R7, // Register R7
R_PC, // Register PC
R_COND, // Register COND
REGISTER_COUNT // Count of registers
};
// Registers declaration
uint16_t registers[REGISTER_COUNT];
// Enumerator type for opcodes
enum
{
OP_BR = 0, // Branch
OP_ADD, // Addition
OP_LD, // Load
OP_ST, // Store
OP_JSR, // Jump register
OP_AND, // And operator
OP_LDR, // Load register
OP_STR, // Load effective address
OP_RTI, // Return from interrupt
OP_NOT, // Not operator
OP_LDI, // Load indirect
OP_STI, // Store indirect
OP_JMP, // Jump
OP_RES, // Reserve memory directive
OP_LEA, // Load effective address
OP_TRAP // Trap
};
// Enumerator type for flags
enum
{
FL_POS = 1 << 0, // Positive flag
FL_ZRO = 1 << 1, // Zero flag
FL_NEG = 1 << 2 // Negative flag
};
// Trap codes
enum
{
TRAP_GETC = 0x20, // Input a character
TRAP_OUT = 0x21, // Output a character
TRAP_PUTS = 0x22, // Output a word string
TRAP_IN = 0x23, // Input a character
TRAP_PUTSP = 0x24, // Output a byte string
TRAP_HALT = 0x25 // Halt the program
};
// Memory mapped registers
enum
{
MR_KBSR = 0xFE00, // Keyboard status register
MR_KBDR = 0xFE02 // Keyboard data register
};
// Running status
bool running = false;
// Swap word function
uint16_t swap_word(uint16_t x)
{
// Swap byte
return (x << 8) | (x >> 8);
}
//Read image file function
uint16_t read_image_file(FILE* file)
{
// Origin
uint16_t origin;
// Read origin address from the program file
fread(&origin, sizeof(origin), 1, file);
// Swap bytes
origin = swap_word(origin);
// Calculate maximum reading size
uint16_t maximum_read = MEMORY - origin;
// Program start pointer
uint16_t *pointer = memory + origin;
// Reading instructions and data
size_t read = fread(pointer, sizeof(uint16_t), maximum_read, file);
// Swapping instructions and data
while(read-- > 0)
{
// Word swap
*pointer = swap_word(*pointer);
// Increment the pointer
pointer++;
}
}
// Read image function
uint16_t read_image(const char* image_path)
{
// Open the program file
FILE* file = fopen(image_path, "rb");
// If the file is not open
if(!file)
{
// Returning false status
return false;
}
// Reading file
read_image_file(file);
// Close file
fclose(file);
// Returning true status
return true;
}
// Memory write function
uint16_t memory_write(uint16_t address, uint16_t value)
{
// Write value to the address of the memory
return memory[address] = value;
}
// Memory read function
uint16_t memory_read(uint16_t address)
{
// Switch of memory mapped registers
switch(address)
{
// Case of keyboard status register
case MR_KBSR:
// Write keynoard status to memory
memory[MR_KBSR] = (1 << 15);
// Write character from keyboard to memory
memory[MR_KBDR] = getchar();
break; // Break
}
// Return character
return memory[address];
}
// Update flags function
void update_flags(uint16_t register_value)
{
// If the register value equals to zero
if (registers[register_value] == 0)
{
// Set flag zero
registers[R_COND] = FL_ZRO;
}
// If the register has negative value
else if(registers[register_value] >> 15)
{
// Set flag negative
registers[R_COND] = FL_NEG;
}
// If the register has positive value
else
{
// Set flag positive
registers[R_COND] = FL_POS;
}
}
// Sign extend function
uint16_t sign_extend(uint16_t value, int bit_count)
{
// If the value sign is set
if((value >> (bit_count - 1) & 1))
{
// Shift sign bit
value |= (0xFFFF << bit_count);
}
// Return new value
return value;
}
// Main function of the program
int main(int argc, char* argv[])
{
// If argument count is two
if(argc == 2)
{
// If not read image file
if(!read_image(argv[1]))
{
// Print the error message
fprintf(stderr, "Failed to load this image! Exiting!");
// Exit with "failed to load this image" error
return 2;
}
}
else
{
// Print the error message
fprintf(stderr, "Wrong argument count! Exiting!");
// Exit with "wrong argument count" error
return 1;
}
// Start program
registers[R_PC] = 0X3000;
// Set running status
running = true;
// Running loop
while(running)
{
// Get instruction
uint16_t instruction = memory_read(registers[R_PC]++);
// Get operand
uint16_t operand = instruction >> 12;
// Operand switch
switch(operand)
{
// Case of ADD operand
case OP_ADD:
{
// Get the first register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get the second register
uint16_t r1 = (instruction >> 6) & 0x7;
// Get the imm value
uint16_t immediate_mode_flag = (instruction >> 5) & 0x1;
// If imm is set
if(immediate_mode_flag)
{
// Get the imm5 value
uint16_t imm5 = sign_extend(instruction & 0x1F, 5);
// Addition
registers[r0] = registers[r1] + imm5;
}
// If imm is not set
else
{
// Get the third register
uint16_t r2 = instruction & 0x7;
// Addition
registers[r0] = registers[r1] + registers[r2];
}
// Update flags
update_flags(r0);
// Break
break;
}
// Case of AND operand
case OP_AND:
{
// Get the first register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get the second register
uint16_t r1 = (instruction >> 6) & 0x7;
// Get the imm value
uint16_t immediate_mode_flag = (instruction >> 5) & 0x1;
// If imm is set
if(immediate_mode_flag)
{
// Get the imm5 value
uint16_t imm5 = sign_extend(instruction & 0x1F, 5);
// Addition
registers[r0] = registers[r1] & imm5;
}
// If imm is not set
else
{
// Get the third register
uint16_t r2 = instruction & 0x7;
// Addition
registers[r0] = registers[r1] & registers[r2];
}
// Update flags
update_flags(r0);
// Break
break;
}
// Case of NOT operand
case OP_NOT:
{
// Get the the first register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get the second register
uint16_t r1 = (instruction >> 6) & 0x7;
// Take the reciprocal
registers[r0] = ~registers[r1];
// Update flags
update_flags(r0);
// Break
break;
}
// Case of BR operand
case OP_BR:
{
// Get program counter offset
uint16_t offset = sign_extend(instruction & 0x1FF, 9);
// Get conditional flag
uint16_t conditional_flag = (instruction >> 9) & 0x7;
// If conditional flag and R_COND are value one
if(conditional_flag & registers[R_COND])
{
// Set the program counter as the offset
registers[R_PC] += offset;
}
// Break
break;
}
// Case of JMP operand
case OP_JMP:
{
// Get the register
uint16_t r1 = (instruction >> 6) & 0x7;
// Set the program counter as the address
registers[R_PC] = registers[r1];
// Break
break;
}
// Case of JSR operand
case OP_JSR:
{
// Get long flag
uint16_t long_flag = (instruction >> 11) & 1;
// Store the address of the program counter
registers[R_R7] = registers[R_PC];
// If long flag is set
if(long_flag)
{
// Get long offset
uint16_t long_offset = sign_extend(instruction & 0x7FF, 11);
// Set the program counter as the long offset
registers[R_PC] += long_offset;
}
// If long flag is not set
else
{
// Get the register
uint16_t r1 = (instruction >> 6) & 0x7;
// Set the program counter as the address
registers[R_PC] = registers[r1];
}
// Break
break;
}
// Case of LD operand
case OP_LD:
{
// Get the register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get offset
uint16_t offset = sign_extend(instruction & 0x1FF, 9);
// Set the register as the offset
registers[r0] = memory_read(registers[R_PC] + offset);
// Update flags
update_flags(r0);
// Break
break;
}
// Case of LDI operand
case OP_LDI:
{
// Get the register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get offset
uint16_t offset = sign_extend(instruction & 0x1FF, 9);
// Set the register as the offset
registers[r0] = memory_read(memory_read(registers[R_PC] + offset));
// Update flags
update_flags(r0);
// Break
break;
}
// Case of LDR operand
case OP_LDR:
{
// Get the first register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get the second register
uint16_t r1 = (instruction >> 6) & 0x7;
// Calculate the offset
uint16_t offset = sign_extend(instruction & 0x3F, 6);
// Set the register as the offset
registers[r0] = memory_read(r1) + offset;
// Update flags
update_flags(r0);
// Break
break;
}
// Case of LEA operand
case OP_LEA:
{
// Get the first register
uint16_t r0 = (instruction >> 9) & 0x7;
// Calculate the offset
uint16_t offset = sign_extend(instruction & 0x1FF, 9);
// Set the register as the offset
registers[r0] = registers[R_PC] + offset;
// Update flags
update_flags(r0);
// Break
break;
}
// Case of ST operand
case OP_ST:
{
// Get the register
uint16_t r0 = (instruction >> 9) & 0x7;
// Calculate the offset
uint16_t offset = sign_extend(instruction & 0x1FF, 9);
// Set the memory as the value
memory_write(registers[R_PC] + offset, registers[r0]);
// Break
break;
}
// Case of STI operand
case OP_STI:
{
// Get the register
uint16_t r0 = (instruction >> 9) & 0x7;
// Calculate the offset
uint16_t offset = sign_extend(instruction & 0x1FF, 9);
// Set the memory as the value
memory_write(memory_read(registers[R_PC] + offset), registers[r0]);
// Break
break;
}
// Case of STR operand
case OP_STR:
{
// Get the first register
uint16_t r0 = (instruction >> 9) & 0x7;
// Get the second register
uint16_t r1 = (instruction >> 6) & 0x7;
// Calculate the offset
uint16_t offset = sign_extend(instruction & 0x3F, 6);
// Set the memory as the value
memory_write(registers[r1] + offset, registers[r0]);
// Break
break;
}
// Case of RES operand
case OP_RES:
// Case of RTI operand
case OP_RTI:
{
// Print error message
fprintf(stderr, "Unused operand code! Exiting!");
// Exit with "unused operand code" error
return 4;
}
// Case of TRAP operand
case OP_TRAP:
{
// Switch of trap
switch(instruction & 0xFF)
{
// Case of TRAP_GETC
case TRAP_GETC:
{
// Get the character
registers[R_R0] = (uint16_t)getchar();
// Flush standard output
fflush(stdout);
// Break
break;
}
// Case of TRAP_OUT
case TRAP_OUT:
{
// Print the character
putc((char)registers[R_R0], stdout);
// Break
break;
}
// Case of TRAP_PUTS
case TRAP_PUTS:
{
// Get the string
uint16_t* character = memory + registers[R_R0];
// Print loop
while(*character)
{
// Print the character
putc((char)*character, stdout);
// Increment character offset
character++;
}
// Flush standard output
fflush(stdout);
// Break
break;
}
// Case of TRAP_IN
case TRAP_IN:
{
// Get the character
char character = getchar();
// Print the character
putc(character, stdout);
// Set the register as the character
registers[R_R0] = (uint16_t)character;
// Break
break;
}
// Case of TRAP_PUTSP
case TRAP_PUTSP:
{
// Get the character
uint16_t* character = memory + registers[R_R0];
// Print loop
while(*character)
{
// Get first character
char char_1 = (*character) & 0xFF;
// Print first character
putc(char_1, stdout);
// Get second character
char char_2 = (*character) >> 8;
// If second character is not null
if(char_2)
{
// Print second character
putc(char_2, stdout);
}
// Increment character offset
character++;
}
// Flush standard output
fflush(stdout);
// Break
break;
}
// Case of TRAP_HALT
case TRAP_HALT:
{
// Flush standard output
fflush(stdout);
// Stop program
running = false;
// Print error message
fprintf(stderr, "The program has been ended! Exiting!");
// Break
break;
}
}
// Break
break;
}
default:
{
// Print error message
fprintf(stderr, "Bad opcode error! Exiting!");
// Exit with "bad opcode" error
return 3;
}
}
}
// Exit with normal status
return 0;
}