+/EE460N

computer architecture

git clone https://git.julia.blue/+/EE460N

Joseph RyanAdd tests6adc40e

master
17.4 KiB445 linesraw
1/*
2    Name 1: Joseph Ryan
3    Name 2: Rahul Butani
4    UTEID 1: jbr2558
5    UTEID 2: rhb834
6*/
7
8/***************************************************************/
9/*                                                             */
10/*   LC-3b Instruction Level Simulator                         */
11/*                                                             */
12/*   EE 460N                                                   */
13/*   The University of Texas at Austin                         */
14/*                                                             */
15/***************************************************************/
16
17#include <assert.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22/***************************************************************/
23/*                                                             */
24/* Files: isaprogram   LC-3b machine language program file     */
25/*                                                             */
26/***************************************************************/
27
28/***************************************************************/
29/* These are the functions you'll have to write.               */
30/***************************************************************/
31
32void process_instruction();
33
34/***************************************************************/
35/* A couple of useful definitions.                             */
36/***************************************************************/
37#define FALSE 0
38#define TRUE 1
39
40/***************************************************************/
41/* Use this to avoid overflowing 16 bits on the bus.           */
42/***************************************************************/
43#define Low16bits(x) ((x)&0xFFFF)
44
45/***************************************************************/
46/* Main memory.                                                */
47/***************************************************************/
48/* MEMORY[A][0] stores the least significant byte of word at word address A
49   MEMORY[A][1] stores the most significant byte of word at word address A
50*/
51
52#define WORDS_IN_MEM 0x08000
53int MEMORY[WORDS_IN_MEM][2];
54
55/***************************************************************/
56
57/***************************************************************/
58
59/***************************************************************/
60/* LC-3b State info.                                           */
61/***************************************************************/
62#define LC_3b_REGS 8
63
64int RUN_BIT; /* run bit */
65
66typedef struct System_Latches_Struct {
67    int PC,               /* program counter */
68        N,                /* n condition bit */
69        Z,                /* z condition bit */
70        P;                /* p condition bit */
71    int REGS[LC_3b_REGS]; /* register file. */
72} System_Latches;
73
74/* Data Structure for Latch */
75
76System_Latches CURRENT_LATCHES, NEXT_LATCHES;
77
78/***************************************************************/
79/* A cycle counter.                                            */
80/***************************************************************/
81int INSTRUCTION_COUNT;
82
83/***************************************************************/
84/*                                                             */
85/* Procedure : help                                            */
86/*                                                             */
87/* Purpose   : Print out a list of commands                    */
88/*                                                             */
89/***************************************************************/
90void help() {
91    printf("----------------LC-3b ISIM Help-----------------------\n");
92    printf("go               -  run program to completion         \n");
93    printf("run n            -  execute program for n instructions\n");
94    printf("mdump low high   -  dump memory from low to high      \n");
95    printf("rdump            -  dump the register & bus values    \n");
96    printf("?                -  display this help menu            \n");
97    printf("quit             -  exit the program                  \n\n");
98}
99
100/***************************************************************/
101/*                                                             */
102/* Procedure : cycle                                           */
103/*                                                             */
104/* Purpose   : Execute a cycle                                 */
105/*                                                             */
106/***************************************************************/
107void cycle() {
108    process_instruction();
109    CURRENT_LATCHES = NEXT_LATCHES;
110    INSTRUCTION_COUNT++;
111}
112
113/***************************************************************/
114/*                                                             */
115/* Procedure : run n                                           */
116/*                                                             */
117/* Purpose   : Simulate the LC-3b for n cycles                 */
118/*                                                             */
119/***************************************************************/
120void run(int num_cycles) {
121    int i;
122
123    if (RUN_BIT == FALSE) {
124        printf("Can't simulate, Simulator is halted\n\n");
125        return;
126    }
127
128    printf("Simulating for %d cycles...\n\n", num_cycles);
129    for (i = 0; i < num_cycles; i++) {
130        if (CURRENT_LATCHES.PC == 0x0000) {
131            RUN_BIT = FALSE;
132            printf("Simulator halted\n\n");
133            break;
134        }
135        cycle();
136    }
137}
138
139/***************************************************************/
140/*                                                             */
141/* Procedure : go                                              */
142/*                                                             */
143/* Purpose   : Simulate the LC-3b until HALTed                 */
144/*                                                             */
145/***************************************************************/
146void go() {
147    if (RUN_BIT == FALSE) {
148        printf("Can't simulate, Simulator is halted\n\n");
149        return;
150    }
151
152    printf("Simulating...\n\n");
153    while (CURRENT_LATCHES.PC != 0x0000) cycle();
154    RUN_BIT = FALSE;
155    printf("Simulator halted\n\n");
156}
157
158/***************************************************************/
159/*                                                             */
160/* Procedure : mdump                                           */
161/*                                                             */
162/* Purpose   : Dump a word-aligned region of memory to the     */
163/*             output file.                                    */
164/*                                                             */
165/***************************************************************/
166void mdump(FILE* dumpsim_file, int start, int stop) {
167    int address; /* this is a byte address */
168
169    printf("\nMemory content [0x%.4x..0x%.4x] :\n", start, stop);
170    printf("-------------------------------------\n");
171    for (address = (start >> 1); address <= (stop >> 1); address++)
172        printf("  0x%.4x (%d) : 0x%.2x%.2x\n", address << 1, address << 1,
173               MEMORY[address][1], MEMORY[address][0]);
174    printf("\n");
175
176    /* dump the memory contents into the dumpsim file */
177    fprintf(dumpsim_file, "\nMemory content [0x%.4x..0x%.4x] :\n", start, stop);
178    fprintf(dumpsim_file, "-------------------------------------\n");
179    for (address = (start >> 1); address <= (stop >> 1); address++)
180        fprintf(dumpsim_file, " 0x%.4x (%d) : 0x%.2x%.2x\n", address << 1,
181                address << 1, MEMORY[address][1], MEMORY[address][0]);
182    fprintf(dumpsim_file, "\n");
183    fflush(dumpsim_file);
184}
185
186/***************************************************************/
187/*                                                             */
188/* Procedure : rdump                                           */
189/*                                                             */
190/* Purpose   : Dump current register and bus values to the     */
191/*             output file.                                    */
192/*                                                             */
193/***************************************************************/
194void rdump(FILE* dumpsim_file) {
195    int k;
196
197    printf("\nCurrent register/bus values :\n");
198    printf("-------------------------------------\n");
199    printf("Instruction Count : %d\n", INSTRUCTION_COUNT);
200    printf("PC                : 0x%.4x\n", CURRENT_LATCHES.PC);
201    printf("CCs: N = %d  Z = %d  P = %d\n", CURRENT_LATCHES.N,
202           CURRENT_LATCHES.Z, CURRENT_LATCHES.P);
203    printf("Registers:\n");
204    for (k = 0; k < LC_3b_REGS; k++)
205        printf("%d: 0x%.4x\n", k, CURRENT_LATCHES.REGS[k]);
206    printf("\n");
207
208    /* dump the state information into the dumpsim file */
209    fprintf(dumpsim_file, "\nCurrent register/bus values :\n");
210    fprintf(dumpsim_file, "-------------------------------------\n");
211    fprintf(dumpsim_file, "Instruction Count : %d\n", INSTRUCTION_COUNT);
212    fprintf(dumpsim_file, "PC                : 0x%.4x\n", CURRENT_LATCHES.PC);
213    fprintf(dumpsim_file, "CCs: N = %d  Z = %d  P = %d\n", CURRENT_LATCHES.N,
214            CURRENT_LATCHES.Z, CURRENT_LATCHES.P);
215    fprintf(dumpsim_file, "Registers:\n");
216    for (k = 0; k < LC_3b_REGS; k++)
217        fprintf(dumpsim_file, "%d: 0x%.4x\n", k, CURRENT_LATCHES.REGS[k]);
218    fprintf(dumpsim_file, "\n");
219    fflush(dumpsim_file);
220}
221
222/***************************************************************/
223/*                                                             */
224/* Procedure : get_command                                     */
225/*                                                             */
226/* Purpose   : Read a command from standard input.             */
227/*                                                             */
228/***************************************************************/
229void get_command(FILE* dumpsim_file) {
230    char buffer[20];
231    int start, stop, cycles;
232
233    printf("LC-3b-SIM> ");
234
235    scanf("%s", buffer);
236    printf("\n");
237
238    switch (buffer[0]) {
239    case 'G':
240    case 'g': go(); break;
241
242    case 'M':
243    case 'm':
244        scanf("%i %i", &start, &stop);
245        mdump(dumpsim_file, start, stop);
246        break;
247
248    case '?': help(); break;
249    case 'Q':
250    case 'q': printf("Bye.\n"); exit(0);
251
252    case 'R':
253    case 'r':
254        if (buffer[1] == 'd' || buffer[1] == 'D')
255            rdump(dumpsim_file);
256        else {
257            scanf("%d", &cycles);
258            run(cycles);
259        }
260        break;
261
262    default: printf("Invalid Command\n"); break;
263    }
264}
265
266/***************************************************************/
267/*                                                             */
268/* Procedure : init_memory                                     */
269/*                                                             */
270/* Purpose   : Zero out the memory array                       */
271/*                                                             */
272/***************************************************************/
273void init_memory() {
274    int i;
275
276    for (i = 0; i < WORDS_IN_MEM; i++) {
277        MEMORY[i][0] = 0;
278        MEMORY[i][1] = 0;
279    }
280}
281
282/**************************************************************/
283/*                                                            */
284/* Procedure : load_program                                   */
285/*                                                            */
286/* Purpose   : Load program and service routines into mem.    */
287/*                                                            */
288/**************************************************************/
289void load_program(char* program_filename) {
290    FILE* prog;
291    int ii, word, program_base;
292
293    /* Open program file. */
294    prog = fopen(program_filename, "r");
295    if (prog == NULL) {
296        printf("Error: Can't open program file %s\n", program_filename);
297        exit(-1);
298    }
299
300    /* Read in the program. */
301    if (fscanf(prog, "%x\n", &word) != EOF)
302        program_base = word >> 1;
303    else {
304        printf("Error: Program file is empty\n");
305        exit(-1);
306    }
307
308    ii = 0;
309    while (fscanf(prog, "%x\n", &word) != EOF) {
310        /* Make sure it fits. */
311        if (program_base + ii >= WORDS_IN_MEM) {
312            printf("Error: Program file %s is too long to fit in memory. %x\n",
313                   program_filename, ii);
314            exit(-1);
315        }
316
317        /* Write the word to memory array. */
318        MEMORY[program_base + ii][0] = word & 0x00FF;
319        MEMORY[program_base + ii][1] = (word >> 8) & 0x00FF;
320        ii++;
321    }
322
323    if (CURRENT_LATCHES.PC == 0)
324        CURRENT_LATCHES.PC = (program_base << 1);
325
326    printf("Read %d words from program into memory.\n\n", ii);
327}
328
329/************************************************************/
330/*                                                          */
331/* Procedure : initialize                                   */
332/*                                                          */
333/* Purpose   : Load machine language program                */
334/*             and set up initial state of the machine.     */
335/*                                                          */
336/************************************************************/
337void initialize(char* program_filename, int num_prog_files) {
338    int i;
339
340    init_memory();
341    for (i = 0; i < num_prog_files; i++) {
342        load_program(program_filename);
343        while (*program_filename++ != '\0')
344            ;
345    }
346    CURRENT_LATCHES.Z = 1;
347    NEXT_LATCHES = CURRENT_LATCHES;
348
349    RUN_BIT = TRUE;
350}
351
352/***************************************************************/
353/*                                                             */
354/* Procedure : main                                            */
355/*                                                             */
356/***************************************************************/
357int main(int argc, char* argv[]) {
358    FILE* dumpsim_file;
359
360    /* Error Checking */
361    if (argc < 2) {
362        printf("Error: usage: %s <program_file_1> <program_file_2> ...\n",
363               argv[0]);
364        exit(1);
365    }
366
367    printf("LC-3b Simulator\n\n");
368
369    initialize(argv[1], argc - 1);
370
371    if ((dumpsim_file = fopen("dumpsim", "w")) == NULL) {
372        printf("Error: Can't open dumpsim file\n");
373        exit(-1);
374    }
375
376    while (1) get_command(dumpsim_file);
377}
378
379// Nothing above this line has been modified
380
381int IR;
382
383// Pull out bitfields from numbers
384#define FIELD(bits, loc, num)                                                  \
385    ((struct { unsigned x : (bits); }){.x = (num) >> loc}.x)
386
387// Pull out fields from an instruction
388#define R1 NEXT_LATCHES.REGS[FIELD(3, 9, IR)]
389#define R2 CURRENT_LATCHES.REGS[FIELD(3, 6, IR)]
390#define R3 CURRENT_LATCHES.REGS[FIELD(3, 0, IR)]
391#define A FIELD(1, 5, IR)
392#define D FIELD(1, 4, IR)
393#define SEXT_IMM(bits) ((struct { int x : (bits); }){.x = IR}.x)
394#define PC_OFFSET(bits) Low16bits(NEXT_LATCHES.PC + (SEXT_IMM(bits) << 1))
395
396// Higher level operations
397void setcc(int num) {
398    NEXT_LATCHES.N = NEXT_LATCHES.Z = NEXT_LATCHES.P = 0;
399    if (FIELD(1, 15, num)) {
400        NEXT_LATCHES.N = 1;
401    } else if (num) {
402        NEXT_LATCHES.P = 1;
403    } else {
404        NEXT_LATCHES.Z = 1;
405    }
406}
407#define GET_WORD(addr) (MEMORY[(addr) >> 1][0] | MEMORY[(addr) >> 1][1] << 8)
408#define PUT_WORD(addr, num)                                                    \
409    (MEMORY[(addr) >> 1][0] = (num)&0xFF,                                      \
410     MEMORY[(addr) >> 1][1] = (num) >> 8 & 0xFF)
411#define GET_BYTE(addr) Low16bits((char)(MEMORY[(addr) >> 1][(addr)&1]))
412#define PUT_BYTE(addr, num) (MEMORY[(addr) >> 1][(addr)&1] = (num)&0xFF)
413#define SAVE_RET() (NEXT_LATCHES.REGS[7] = CURRENT_LATCHES.PC + 2)
414#define SET_PC(num) (NEXT_LATCHES.PC = num)
415#define CHECK_BRANCH()                                                         \
416    (FIELD(1, 11, IR) & CURRENT_LATCHES.N ||                                   \
417     FIELD(1, 10, IR) & CURRENT_LATCHES.Z ||                                   \
418     FIELD(1, 9, IR) & CURRENT_LATCHES.P)
419#define DO_ARITHMETIC(op) (R1 = Low16bits((R2 op(A ? SEXT_IMM(5) : R3))))
420
421// clang-format off
422enum OP {BR, ADD, LDB, STB, JSR, AND, LDW, STW, RTI,
423         XOR, UNUSED1, UNUSED2, JMP, SHF, LEA, TRAP};
424
425void process_instruction() {
426    NEXT_LATCHES.PC = Low16bits(CURRENT_LATCHES.PC + 2);
427    switch (((IR = GET_WORD(CURRENT_LATCHES.PC)) & 0xF000) >> 12) {
428    case ADD: DO_ARITHMETIC(+), setcc(R1);                                break;
429    case AND: DO_ARITHMETIC(&), setcc(R1);                                break;
430    case XOR: DO_ARITHMETIC(^), setcc(R1);                                break;
431    case SHF: R1 = Low16bits(D ? (A ? (short)R2 : R2) >> FIELD(4, 0, IR)
432                               : R2 << FIELD(4, 0, IR)), setcc(R1);       break;
433    case LDB: R1 = GET_BYTE(R2 + SEXT_IMM(6)), setcc(R1);                 break;
434    case LDW: R1 = GET_WORD(R2 + (SEXT_IMM(6) << 1)), setcc(R1);          break;
435    case STB: PUT_BYTE(R2 + SEXT_IMM(6), R1);                             break;
436    case STW: PUT_WORD(R2 + (SEXT_IMM(6) << 1), R1);                      break;
437    case LEA: R1 = PC_OFFSET(9);                                          break;
438    case BR: SET_PC(CHECK_BRANCH() ? PC_OFFSET(9) : NEXT_LATCHES.PC);     break;
439    case JMP: SET_PC(R2);                                                 break;
440    case JSR: SET_PC(FIELD(1, 11, IR) ? PC_OFFSET(11) : R2), SAVE_RET();  break;
441    case TRAP: SET_PC(GET_WORD(FIELD(8, 0, IR))), SAVE_RET();             break;
442    case RTI:                                                             break;
443    default:                                                              break;
444    }
445}