+/EE460N

computer architecture

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

Joseph RyanFix issues with jsr and rshf79335f9

master
39.1 KiB1155 linesraw
1/*
2    Name 1: Joseph Ryan
3    UTEID 1: jbr2558
4*/
5
6/***************************************************************/
7/*                                                             */
8/*   LC-3b Simulator - Lab 6                                   */
9/*                                                             */
10/*   EE 460N -- Spring 2013                                    */
11/*   The University of Texas at Austin                         */
12/*                                                             */
13/***************************************************************/
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19/***************************************************************/
20/*                                                             */
21/* Files:  ucode        Microprogram file                      */
22/*         isaprogram   LC-3b machine language program file    */
23/*                                                             */
24/***************************************************************/
25
26/***************************************************************/
27/* These are the functions you'll have to write.               */
28/***************************************************************/
29void FETCH_stage();
30void DE_stage();
31void AGEX_stage();
32void MEM_stage();
33void SR_stage();
34/***************************************************************/
35/* A couple of useful definitions.                             */
36/***************************************************************/
37#define TRUE 1
38#define FALSE 0
39
40/***************************************************************/
41/* Use this to avoid overflowing 16 bits on the bus.           */
42/***************************************************************/
43#define Low16bits(x) ((x)&0xFFFF)
44
45/***************************************************************/
46/* Definition of the control store layout.                     */
47/***************************************************************/
48#define CONTROL_STORE_ROWS 64
49
50/***************************************************************/
51/* Definition of bit order in control store word.              */
52/***************************************************************/
53/* control signals from the control store */
54enum CS_BITS {
55    SR1_NEEDED,
56    SR2_NEEDED,
57    DRMUX,
58
59    ADDR1MUX,
60    ADDR2MUX1,
61    ADDR2MUX0,
62    LSHF1,
63    ADDRESSMUX,
64    SR2MUX,
65    ALUK1,
66    ALUK0,
67    ALU_RESULTMUX,
68
69    BR_OP,
70    UNCOND_OP,
71    TRAP_OP,
72    BR_STALL,
73
74    DCACHE_EN,
75    DCACHE_RW,
76    DATA_SIZE,
77
78    DR_VALUEMUX1,
79    DR_VALUEMUX0,
80    LD_REG,
81    LD_CC,
82    NUM_CONTROL_STORE_BITS
83
84} CS_BITS;
85
86enum AGEX_CS_BITS {
87
88    AGEX_ADDR1MUX,
89    AGEX_ADDR2MUX1,
90    AGEX_ADDR2MUX0,
91    AGEX_LSHF1,
92    AGEX_ADDRESSMUX,
93    AGEX_SR2MUX,
94    AGEX_ALUK1,
95    AGEX_ALUK0,
96    AGEX_ALU_RESULTMUX,
97
98    AGEX_BR_OP,
99    AGEX_UNCOND_OP,
100    AGEX_TRAP_OP,
101    AGEX_BR_STALL,
102    AGEX_DCACHE_EN,
103    AGEX_DCACHE_RW,
104    AGEX_DATA_SIZE,
105
106    AGEX_DR_VALUEMUX1,
107    AGEX_DR_VALUEMUX0,
108    AGEX_LD_REG,
109    AGEX_LD_CC,
110    NUM_AGEX_CS_BITS
111} AGEX_CS_BITS;
112
113enum MEM_CS_BITS {
114    MEM_BR_OP,
115    MEM_UNCOND_OP,
116    MEM_TRAP_OP,
117    MEM_BR_STALL,
118    MEM_DCACHE_EN,
119    MEM_DCACHE_RW,
120    MEM_DATA_SIZE,
121
122    MEM_DR_VALUEMUX1,
123    MEM_DR_VALUEMUX0,
124    MEM_LD_REG,
125    MEM_LD_CC,
126    NUM_MEM_CS_BITS
127} MEM_CS_BITS;
128
129enum SR_CS_BITS {
130    SR_DR_VALUEMUX1,
131    SR_DR_VALUEMUX0,
132    SR_LD_REG,
133    SR_LD_CC,
134    NUM_SR_CS_BITS
135} SR_CS_BITS;
136
137/***************************************************************/
138/* Functions to get at the control bits.                       */
139/***************************************************************/
140int Get_SR1_NEEDED(int* x) {
141    return (x[SR1_NEEDED]);
142}
143int Get_SR2_NEEDED(int* x) {
144    return (x[SR2_NEEDED]);
145}
146int Get_DRMUX(int* x) {
147    return (x[DRMUX]);
148}
149int Get_DE_BR_OP(int* x) {
150    return (x[BR_OP]);
151}
152int Get_ADDR1MUX(int* x) {
153    return (x[AGEX_ADDR1MUX]);
154}
155int Get_ADDR2MUX(int* x) {
156    return ((x[AGEX_ADDR2MUX1] << 1) + x[AGEX_ADDR2MUX0]);
157}
158int Get_LSHF1(int* x) {
159    return (x[AGEX_LSHF1]);
160}
161int Get_ADDRESSMUX(int* x) {
162    return (x[AGEX_ADDRESSMUX]);
163}
164int Get_SR2MUX(int* x) {
165    return (x[AGEX_SR2MUX]);
166}
167int Get_ALUK(int* x) {
168    return ((x[AGEX_ALUK1] << 1) + x[AGEX_ALUK0]);
169}
170int Get_ALU_RESULTMUX(int* x) {
171    return (x[AGEX_ALU_RESULTMUX]);
172}
173int Get_BR_OP(int* x) {
174    return (x[MEM_BR_OP]);
175}
176int Get_UNCOND_OP(int* x) {
177    return (x[MEM_UNCOND_OP]);
178}
179int Get_TRAP_OP(int* x) {
180    return (x[MEM_TRAP_OP]);
181}
182int Get_DCACHE_EN(int* x) {
183    return (x[MEM_DCACHE_EN]);
184}
185int Get_DCACHE_RW(int* x) {
186    return (x[MEM_DCACHE_RW]);
187}
188int Get_DATA_SIZE(int* x) {
189    return (x[MEM_DATA_SIZE]);
190}
191int Get_DR_VALUEMUX1(int* x) {
192    return ((x[SR_DR_VALUEMUX1] << 1) + x[SR_DR_VALUEMUX0]);
193}
194int Get_AGEX_LD_REG(int* x) {
195    return (x[AGEX_LD_REG]);
196}
197int Get_AGEX_LD_CC(int* x) {
198    return (x[AGEX_LD_CC]);
199}
200int Get_MEM_LD_REG(int* x) {
201    return (x[MEM_LD_REG]);
202}
203int Get_MEM_LD_CC(int* x) {
204    return (x[MEM_LD_CC]);
205}
206int Get_SR_LD_REG(int* x) {
207    return (x[SR_LD_REG]);
208}
209int Get_SR_LD_CC(int* x) {
210    return (x[SR_LD_CC]);
211}
212int Get_DE_BR_STALL(int* x) {
213    return (x[BR_STALL]);
214}
215int Get_AGEX_BR_STALL(int* x) {
216    return (x[AGEX_BR_STALL]);
217}
218int Get_MEM_BR_STALL(int* x) {
219    return (x[MEM_BR_STALL]);
220}
221
222/***************************************************************/
223/* The control store rom.                                      */
224/***************************************************************/
225int CONTROL_STORE[CONTROL_STORE_ROWS][NUM_CONTROL_STORE_BITS];
226
227/***************************************************************/
228/* Main memory.                                                */
229/***************************************************************/
230/* MEMORY[A][0] stores the least significant byte of word at word address A
231   MEMORY[A][1] stores the most significant byte of word at word address A
232   There are two write enable signals, one for each byte. WE0 is used for
233   the least significant byte of a word. WE1 is used for the most significant
234   byte of a word. */
235
236#define WORDS_IN_MEM 0x08000
237int MEMORY[WORDS_IN_MEM][2];
238
239/***************************************************************/
240/* The LC-3b register file.                                      */
241/***************************************************************/
242#define LC3b_REGS 8
243int REGS[LC3b_REGS];
244/***************************************************************/
245/* architectural state */
246/***************************************************************/
247int PC,    /* program counter */
248    N,     /* n condition bit */
249    Z = 1, /* z condition bit */
250    P;     /* p condition bit */
251/***************************************************************/
252/* LC-3b State info.                                             */
253/***************************************************************/
254
255typedef struct PipeState_Entry_Struct {
256    /* DE latches */
257    int DE_NPC, DE_IR, DE_V,
258        /* AGEX lateches */
259        AGEX_NPC, AGEX_SR1, AGEX_SR2, AGEX_CC, AGEX_IR, AGEX_DRID, AGEX_V,
260        AGEX_CS[NUM_AGEX_CS_BITS],
261        /* MEM latches */
262        MEM_NPC, MEM_ALU_RESULT, MEM_ADDRESS, MEM_CC, MEM_IR, MEM_DRID, MEM_V,
263        MEM_CS[NUM_MEM_CS_BITS],
264        /* SR latches */
265        SR_NPC, SR_DATA, SR_ALU_RESULT, SR_ADDRESS, SR_IR, SR_DRID, SR_V,
266        SR_CS[NUM_SR_CS_BITS];
267
268} PipeState_Entry;
269
270/* data structure for latch */
271PipeState_Entry PS, NEW_PS;
272
273/* simulator signal */
274int RUN_BIT;
275
276/* Internal stall signals */
277int dep_stall, v_de_br_stall, v_agex_br_stall, v_mem_br_stall, mem_stall,
278    icache_r;
279
280/***************************************************************/
281/* A cycle counter.                                            */
282/***************************************************************/
283int CYCLE_COUNT;
284
285/***************************************************************/
286/*                                                             */
287/* Procedure : help                                            */
288/*                                                             */
289/* Purpose   : Print out a list of commands.                   */
290/*                                                             */
291/***************************************************************/
292void help() {
293    printf("----------------LC-3bSIM Help-------------------------\n");
294    printf("go               -  run program to completion       \n");
295    printf("run n            -  execute program for n cycles    \n");
296    printf("mdump low high   -  dump memory from low to high    \n");
297    printf("rdump            -  dump the architectural state    \n");
298    printf("idump            -  dump the internal state         \n");
299    printf("?                -  display this help menu          \n");
300    printf("quit             -  exit the program                \n\n");
301}
302
303void print_CS(int* CS, int num) {
304    int ii;
305    for (ii = 0; ii < num; ii++) { printf("%d", CS[ii]); }
306    printf("\n");
307}
308/***************************************************************/
309/*                                                             */
310/* Procedure : cycle                                           */
311/*                                                             */
312/* Purpose   : Execute a cycle                                 */
313/*                                                             */
314/***************************************************************/
315void cycle() {
316    NEW_PS = PS;
317    SR_stage();
318    MEM_stage();
319    AGEX_stage();
320    DE_stage();
321    FETCH_stage();
322    PS = NEW_PS;
323    CYCLE_COUNT++;
324}
325
326/***************************************************************/
327/*                                                             */
328/* Procedure : run n                                           */
329/*                                                             */
330/* Purpose   : Simulate the LC-3b for n cycles.                 */
331/*                                                             */
332/***************************************************************/
333void run(int num_cycles) {
334    int i;
335
336    if (RUN_BIT == FALSE) {
337        printf("Can't simulate, Simulator is halted\n\n");
338        return;
339    }
340
341    printf("Simulating for %d cycles...\n\n", num_cycles);
342    for (i = 0; i < num_cycles; i++) {
343        if (PC == 0x0000) {
344            cycle();
345            RUN_BIT = FALSE;
346            printf("Simulator halted\n\n");
347            break;
348        }
349        cycle();
350    }
351}
352
353/***************************************************************/
354/*                                                             */
355/* Procedure : go                                              */
356/*                                                             */
357/* Purpose   : Simulate the LC-3b until HALTed.                 */
358/*                                                             */
359/***************************************************************/
360void go() {
361    if ((RUN_BIT == FALSE) || (PC == 0x0000)) {
362        printf("Can't simulate, Simulator is halted\n\n");
363        return;
364    }
365    printf("Simulating...\n\n");
366    /* initialization */
367    while (PC != 0x0000) cycle();
368    cycle();
369    RUN_BIT = FALSE;
370    printf("Simulator halted\n\n");
371}
372
373/***************************************************************/
374/*                                                             */
375/* Procedure : mdump                                           */
376/*                                                             */
377/* Purpose   : Dump a region of memory to the output file.     */
378/*                                                             */
379/***************************************************************/
380void mdump(FILE* dumpsim_file, int start, int stop) {
381    int address; /* this is a byte address */
382
383    printf("\nMemory content [0x%04x..0x%04x] :\n", start, stop);
384    printf("-------------------------------------\n");
385    for (address = (start >> 1); address <= (stop >> 1); address++)
386        printf("  0x%04x (%d) : 0x%02x%02x\n", address << 1, address << 1,
387               MEMORY[address][1], MEMORY[address][0]);
388    printf("\n");
389
390    /* dump the memory contents into the dumpsim file */
391    fprintf(dumpsim_file, "\nMemory content [0x%04x..0x%04x] :\n", start, stop);
392    fprintf(dumpsim_file, "-------------------------------------\n");
393    for (address = (start >> 1); address <= (stop >> 1); address++)
394        fprintf(dumpsim_file, " 0x%04x (%d) : 0x%02x%02x\n", address << 1,
395                address << 1, MEMORY[address][1], MEMORY[address][0]);
396    fprintf(dumpsim_file, "\n");
397    fflush(dumpsim_file);
398}
399
400/***************************************************************/
401/*                                                             */
402/* Procedure : rdump                                           */
403/*                                                             */
404/* Purpose   : Dump current architectural state  to the       */
405/*             output file.                                    */
406/*                                                             */
407/***************************************************************/
408void rdump(FILE* dumpsim_file) {
409    int k;
410
411    printf("\nCurrent architectural state :\n");
412    printf("-------------------------------------\n");
413    printf("Cycle Count : %d\n", CYCLE_COUNT);
414    printf("PC          : 0x%04x\n", PC);
415    printf("CCs: N = %d  Z = %d  P = %d\n", N, Z, P);
416    printf("Registers:\n");
417    for (k = 0; k < LC3b_REGS; k++)
418        printf("%d: 0x%04x\n", k, (REGS[k] & 0xFFFF));
419    printf("\n");
420
421    /* dump the state information into the dumpsim file */
422    fprintf(dumpsim_file, "\nCurrent architectural state :\n");
423    fprintf(dumpsim_file, "-------------------------------------\n");
424    fprintf(dumpsim_file, "Cycle Count : %d\n", CYCLE_COUNT);
425    fprintf(dumpsim_file, "PC          : 0x%04x\n", PC);
426    fprintf(dumpsim_file, "CCs: N = %d  Z = %d  P = %d\n", N, Z, P);
427    fprintf(dumpsim_file, "Registers:\n");
428    for (k = 0; k < LC3b_REGS; k++)
429        fprintf(dumpsim_file, "%d: 0x%04x\n", k, (REGS[k] & 0xFFFF));
430    fprintf(dumpsim_file, "\n");
431    fflush(dumpsim_file);
432}
433
434/***************************************************************/
435/*                                                             */
436/* Procedure : idump                                           */
437/*                                                             */
438/* Purpose   : Dump current internal state to the              */
439/*             output file.                                    */
440/*                                                             */
441/***************************************************************/
442void idump(FILE* dumpsim_file) {
443    int k;
444
445    printf("\nCurrent architectural state :\n");
446    printf("-------------------------------------\n");
447    printf("Cycle Count     : %d\n", CYCLE_COUNT);
448    printf("PC              : 0x%04x\n", PC);
449    printf("CCs: N = %d  Z = %d  P = %d\n", N, Z, P);
450    printf("Registers:\n");
451    for (k = 0; k < LC3b_REGS; k++)
452        printf("%d: 0x%04x\n", k, (REGS[k] & 0xFFFF));
453    printf("\n");
454
455    printf("------------- Stall Signals -------------\n");
456    printf("ICACHE_R        :  %d\n", icache_r);
457    printf("DEP_STALL       :  %d\n", dep_stall);
458    printf("V_DE_BR_STALL   :  %d\n", v_de_br_stall);
459    printf("V_AGEX_BR_STALL :  %d\n", v_agex_br_stall);
460    printf("MEM_STALL       :  %d\n", mem_stall);
461    printf("V_MEM_BR_STALL  :  %d\n", v_mem_br_stall);
462    printf("\n");
463
464    printf("------------- DE   Latches --------------\n");
465    printf("DE_NPC          :  0x%04x\n", PS.DE_NPC);
466    printf("DE_IR           :  0x%04x\n", PS.DE_IR);
467    printf("DE_V            :  %d\n", PS.DE_V);
468    printf("\n");
469
470    printf("------------- AGEX Latches --------------\n");
471    printf("AGEX_NPC        :  0x%04x\n", PS.AGEX_NPC);
472    printf("AGEX_SR1        :  0x%04x\n", PS.AGEX_SR1);
473    printf("AGEX_SR2        :  0x%04x\n", PS.AGEX_SR2);
474    printf("AGEX_CC         :  %d\n", PS.AGEX_CC);
475    printf("AGEX_IR         :  0x%04x\n", PS.AGEX_IR);
476    printf("AGEX_DRID       :  %d\n", PS.AGEX_DRID);
477    printf("AGEX_CS         :  ");
478    for (k = 0; k < NUM_AGEX_CS_BITS; k++) { printf("%d", PS.AGEX_CS[k]); }
479    printf("\n");
480    printf("AGEX_V          :  %d\n", PS.AGEX_V);
481    printf("\n");
482
483    printf("------------- MEM  Latches --------------\n");
484    printf("MEM_NPC         :  0x%04x\n", PS.MEM_NPC);
485    printf("MEM_ALU_RESULT  :  0x%04x\n", PS.MEM_ALU_RESULT);
486    printf("MEM_ADDRESS     :  0x%04x\n", PS.MEM_ADDRESS);
487    printf("MEM_CC          :  %d\n", PS.MEM_CC);
488    printf("MEM_IR          :  0x%04x\n", PS.MEM_IR);
489    printf("MEM_DRID        :  %d\n", PS.MEM_DRID);
490    printf("MEM_CS          :  ");
491    for (k = 0; k < NUM_MEM_CS_BITS; k++) { printf("%d", PS.MEM_CS[k]); }
492    printf("\n");
493    printf("MEM_V           :  %d\n", PS.MEM_V);
494    printf("\n");
495
496    printf("------------- SR   Latches --------------\n");
497    printf("SR_NPC          :  0x%04x\n", PS.SR_NPC);
498    printf("SR_DATA         :  0x%04x\n", PS.SR_DATA);
499    printf("SR_ALU_RESULT   :  0x%04x\n", PS.SR_ALU_RESULT);
500    printf("SR_ADDRESS      :  0x%04x\n", PS.SR_ADDRESS);
501    printf("SR_IR           :  0x%04x\n", PS.SR_IR);
502    printf("SR_DRID         :  %d\n", PS.SR_DRID);
503    printf("SR_CS           :  ");
504    for (k = 0; k < NUM_SR_CS_BITS; k++) { printf("%d", PS.SR_CS[k]); }
505    printf("\n");
506    printf("SR_V            :  %d\n", PS.SR_V);
507
508    printf("\n");
509
510    /* dump the state information into the dumpsim file */
511    fprintf(dumpsim_file, "\nCurrent register/bus values :\n");
512    fprintf(dumpsim_file, "\nCurrent architectural state :\n");
513    fprintf(dumpsim_file, "-------------------------------------\n");
514    fprintf(dumpsim_file, "Cycle Count     : %d\n", CYCLE_COUNT);
515    fprintf(dumpsim_file, "PC              : 0x%04x\n", PC);
516    fprintf(dumpsim_file, "CCs: N = %d  Z = %d  P = %d\n", N, Z, P);
517    fprintf(dumpsim_file, "Registers:\n");
518    for (k = 0; k < LC3b_REGS; k++)
519        fprintf(dumpsim_file, "%d: 0x%04x\n", k, (REGS[k] & 0xFFFF));
520    fprintf(dumpsim_file, "\n");
521
522    fprintf(dumpsim_file, "------------- Stall Signals -------------\n");
523    fprintf(dumpsim_file, "ICACHE_R        :  %d\n", icache_r);
524    fprintf(dumpsim_file, "DEP_STALL       :  %d\n", dep_stall);
525    fprintf(dumpsim_file, "V_DE_BR_STALL   :  %d\n", v_de_br_stall);
526    fprintf(dumpsim_file, "V_AGEX_BR_STALL :  %d\n", v_agex_br_stall);
527    fprintf(dumpsim_file, "MEM_STALL       :  %d\n", mem_stall);
528    fprintf(dumpsim_file, "V_MEM_BR_STALL  :  %d\n", v_mem_br_stall);
529    fprintf(dumpsim_file, "\n");
530
531    fprintf(dumpsim_file, "------------- DE   Latches --------------\n");
532    fprintf(dumpsim_file, "DE_NPC          :  0x%04x\n", PS.DE_NPC);
533    fprintf(dumpsim_file, "DE_IR           :  0x%04x\n", PS.DE_IR);
534    fprintf(dumpsim_file, "DE_V            :  %d\n", PS.DE_V);
535    fprintf(dumpsim_file, "\n");
536
537    fprintf(dumpsim_file, "------------- AGEX Latches --------------\n");
538    fprintf(dumpsim_file, "AGEX_NPC        :  0x%04x\n", PS.AGEX_NPC);
539    fprintf(dumpsim_file, "AGEX_SR1        :  0x%04x\n", PS.AGEX_SR1);
540    fprintf(dumpsim_file, "AGEX_SR2        :  0x%04x\n", PS.AGEX_SR2);
541    fprintf(dumpsim_file, "AGEX_CC         :  %d\n", PS.AGEX_CC);
542    fprintf(dumpsim_file, "AGEX_IR         :  0x%04x\n", PS.AGEX_IR);
543    fprintf(dumpsim_file, "AGEX_DRID       :  %d\n", PS.AGEX_DRID);
544    fprintf(dumpsim_file, "AGEX_CS         :  ");
545    for (k = 0; k < NUM_AGEX_CS_BITS; k++) {
546        fprintf(dumpsim_file, "%d", PS.AGEX_CS[k]);
547    }
548    fprintf(dumpsim_file, "\n");
549    fprintf(dumpsim_file, "AGEX_V          :  %d\n", PS.AGEX_V);
550    fprintf(dumpsim_file, "\n");
551
552    fprintf(dumpsim_file, "------------- MEM  Latches --------------\n");
553    fprintf(dumpsim_file, "MEM_NPC         :  0x%04x\n", PS.MEM_NPC);
554    fprintf(dumpsim_file, "MEM_ALU_RESULT  :  0x%04x\n", PS.MEM_ALU_RESULT);
555    fprintf(dumpsim_file, "MEM_ADDRESS     :  0x%04x\n", PS.MEM_ADDRESS);
556    fprintf(dumpsim_file, "MEM_CC          :  %d\n", PS.MEM_CC);
557    fprintf(dumpsim_file, "MEM_IR          :  0x%04x\n", PS.MEM_IR);
558    fprintf(dumpsim_file, "MEM_DRID        :  %d\n", PS.MEM_DRID);
559    fprintf(dumpsim_file, "MEM_CS          :  ");
560    for (k = 0; k < NUM_MEM_CS_BITS; k++) {
561        fprintf(dumpsim_file, "%d", PS.MEM_CS[k]);
562    }
563    fprintf(dumpsim_file, "\n");
564    fprintf(dumpsim_file, "MEM_V           :  %d\n", PS.MEM_V);
565    fprintf(dumpsim_file, "\n");
566
567    fprintf(dumpsim_file, "------------- SR   Latches --------------\n");
568    fprintf(dumpsim_file, "SR_NPC          :  0x%04x\n", PS.SR_NPC);
569    fprintf(dumpsim_file, "SR_DATA         :  0x%04x\n", PS.SR_DATA);
570    fprintf(dumpsim_file, "SR_ALU_RESULT   :  0x%04x\n", PS.SR_ALU_RESULT);
571    fprintf(dumpsim_file, "SR_ADDRESS      :  0x%04x\n", PS.SR_ADDRESS);
572    fprintf(dumpsim_file, "SR_IR           :  0x%04x\n", PS.SR_IR);
573    fprintf(dumpsim_file, "SR_DRID         :  %d\n", PS.SR_DRID);
574    fprintf(dumpsim_file, "SR_CS           :  ");
575    for (k = 0; k < NUM_SR_CS_BITS; k++) {
576        fprintf(dumpsim_file, "%d", PS.SR_CS[k]);
577    }
578    fprintf(dumpsim_file, "\n");
579    fprintf(dumpsim_file, "SR_V            :  %d\n", PS.SR_V);
580
581    fprintf(dumpsim_file, "\n");
582    fflush(dumpsim_file);
583}
584/***************************************************************/
585/*                                                             */
586/* Procedure : get_command                                     */
587/*                                                             */
588/* Purpose   : Read a command from standard input.             */
589/*                                                             */
590/***************************************************************/
591void get_command(FILE* dumpsim_file) {
592    char buffer[20];
593    int start, stop, cycles;
594
595    printf("LC-3b-SIM> ");
596
597    scanf("%s", buffer);
598    printf("\n");
599
600    switch (buffer[0]) {
601    case 'G':
602    case 'g': go(); break;
603
604    case 'M':
605    case 'm':
606        scanf("%i %i", &start, &stop);
607        mdump(dumpsim_file, start, stop);
608        break;
609
610    case '?': help(); break;
611    case 'Q':
612    case 'q': printf("Bye.\n"); exit(0);
613
614    case 'R':
615    case 'r':
616        if (buffer[1] == 'd' || buffer[1] == 'D')
617            rdump(dumpsim_file);
618        else {
619            scanf("%d", &cycles);
620            run(cycles);
621        }
622        break;
623
624    case 'I':
625    case 'i': idump(dumpsim_file); break;
626
627    default: printf("Invalid Command\n"); break;
628    }
629}
630
631/***************************************************************/
632/*                                                             */
633/* Procedure : init_control_store                              */
634/*                                                             */
635/* Purpose   : Load microprogram into control store ROM        */
636/*                                                             */
637/***************************************************************/
638void init_control_store(char* ucode_filename) {
639    FILE* ucode;
640    int i, j, index;
641    char line[200];
642
643    printf("Loading Control Store from file: %s\n", ucode_filename);
644
645    /* Open the micro-code file. */
646    if ((ucode = fopen(ucode_filename, "r")) == NULL) {
647        printf("Error: Can't open micro-code file %s\n", ucode_filename);
648        exit(-1);
649    }
650
651    /* Read a line for each row in the control store. */
652    for (i = 0; i < CONTROL_STORE_ROWS; i++) {
653        if (fscanf(ucode, "%[^\n]\n", line) == EOF) {
654            printf("Error: Too few lines (%d) in micro-code file: %s\n", i,
655                   ucode_filename);
656            exit(-1);
657        }
658
659        /* Put in bits one at a time. */
660        index = 0;
661
662        for (j = 0; j < NUM_CONTROL_STORE_BITS; j++) {
663            /* Needs to find enough bits in line. */
664            if (line[index] == '\0') {
665                printf("Error: Too few control bits in micro-code file: "
666                       "%s\nLine: %d\n",
667                       ucode_filename, i);
668                exit(-1);
669            }
670            if (line[index] != '0' && line[index] != '1') {
671                printf("Error: Unknown value in micro-code file: %s\nLine: %d, "
672                       "Bit: %d\n",
673                       ucode_filename, i, j);
674                exit(-1);
675            }
676
677            /* Set the bit in the Control Store. */
678            CONTROL_STORE[i][j] = (line[index] == '0') ? 0 : 1;
679            index++;
680        }
681        /* Warn about extra bits in line. */
682        if (line[index] != '\0')
683            printf("Warning: Extra bit(s) in control store file %s. Line: %d\n",
684                   ucode_filename, i);
685    }
686    printf("\n");
687}
688
689/***************************************************************/
690/*                                                             */
691/* Procedure : init_memory                                     */
692/*                                                             */
693/* Purpose   : Zero out the memory array                       */
694/*                                                             */
695/***************************************************************/
696void init_memory() {
697    int i;
698
699    for (i = 0; i < WORDS_IN_MEM; i++) {
700        MEMORY[i][0] = 0;
701        MEMORY[i][1] = 0;
702    }
703}
704
705/***************************************************************/
706/*                                                             */
707/* Procedure : init_state                                      */
708/*                                                             */
709/* Purpose   : Zero out all latches and registers              */
710/*                                                             */
711/***************************************************************/
712void init_state() {
713    memset(&PS, 0, sizeof(PipeState_Entry));
714    memset(&NEW_PS, 0, sizeof(PipeState_Entry));
715
716    dep_stall = 0;
717    v_de_br_stall = 0;
718    v_agex_br_stall = 0;
719    v_mem_br_stall = 0;
720    mem_stall = 0;
721}
722
723/**************************************************************/
724/*                                                            */
725/* Procedure : load_program                                   */
726/*                                                            */
727/* Purpose   : Load program and service routines into mem.    */
728/*                                                            */
729/**************************************************************/
730void load_program(char* program_filename) {
731    FILE* prog;
732    int ii, word, program_base;
733
734    /* Open program file. */
735    prog = fopen(program_filename, "r");
736    if (prog == NULL) {
737        printf("Error: Can't open program file %s\n", program_filename);
738        exit(-1);
739    }
740
741    /* Read in the program. */
742    if (fscanf(prog, "%x\n", &word) != EOF)
743        program_base = word >> 1;
744    else {
745        printf("Error: Program file is empty\n");
746        exit(-1);
747    }
748
749    ii = 0;
750    while (fscanf(prog, "%x\n", &word) != EOF) {
751        /* Make sure it fits. */
752        if (program_base + ii >= WORDS_IN_MEM) {
753            printf("Error: Program file %s is too long to fit in memory. %x\n",
754                   program_filename, ii);
755            exit(-1);
756        }
757
758        /* Write the word to memory array. */
759        MEMORY[program_base + ii][0] = word & 0x00FF;
760        MEMORY[program_base + ii][1] = (word >> 8) & 0x00FF;
761        ii++;
762    }
763
764    if (PC == 0)
765        PC = program_base << 1;
766    printf("Read %d words from program into memory.\n\n", ii);
767}
768
769/***************************************************************/
770/*                                                             */
771/* Procedure : initialize                                      */
772/*                                                             */
773/* Purpose   : Load microprogram and machine language program  */
774/*             and set up initial state of the machine.        */
775/*                                                             */
776/***************************************************************/
777void initialize(char* ucode_filename, char* program_filename,
778                int num_prog_files) {
779    int i;
780    init_control_store(ucode_filename);
781
782    init_memory();
783
784    for (i = 0; i < num_prog_files; i++) {
785        load_program(program_filename);
786        while (*program_filename++ != '\0')
787            ;
788    }
789    init_state();
790
791    RUN_BIT = TRUE;
792}
793
794/***************************************************************/
795/*                                                             */
796/* dcache_access                                               */
797/*                                                             */
798/***************************************************************/
799void dcache_access(int dcache_addr, int* read_word, int write_word,
800                   int* dcache_r, int mem_w0, int mem_w1) {
801    int addr = dcache_addr >> 1;
802    int random = CYCLE_COUNT % 9;
803
804    if (!random) {
805        *dcache_r = 0;
806        *read_word = 0xfeed;
807    } else {
808        *dcache_r = 1;
809
810        *read_word = (MEMORY[addr][1] << 8) | (MEMORY[addr][0] & 0x00FF);
811        if (mem_w0)
812            MEMORY[addr][0] = write_word & 0x00FF;
813        if (mem_w1)
814            MEMORY[addr][1] = (write_word & 0xFF00) >> 8;
815    }
816}
817/***************************************************************/
818/*                                                             */
819/* icache_access                                               */
820/*                                                             */
821/***************************************************************/
822void icache_access(int icache_addr, int* read_word, int* icache_r) {
823    int addr = icache_addr >> 1;
824    int random = CYCLE_COUNT % 13;
825
826    if (!random) {
827        *icache_r = 0;
828        *read_word = 0xfeed;
829    } else {
830        *icache_r = 1;
831        *read_word = MEMORY[addr][1] << 8 | MEMORY[addr][0];
832    }
833}
834/***************************************************************/
835/*                                                             */
836/* Procedure : main                                            */
837/*                                                             */
838/***************************************************************/
839int main(int argc, char* argv[]) {
840    FILE* dumpsim_file;
841
842    /* Error Checking */
843    if (argc < 3) {
844        printf("Error: usage: %s <micro_code_file> <program_file_1> "
845               "<program_file_2> ...\n",
846               argv[0]);
847        exit(1);
848    }
849
850    printf("LC-3b Simulator\n\n");
851
852    initialize(argv[1], argv[2], argc - 2);
853
854    if ((dumpsim_file = fopen("dumpsim", "w")) == NULL) {
855        printf("Error: Can't open dumpsim file\n");
856        exit(-1);
857    }
858
859    while (1) get_command(dumpsim_file);
860}
861
862/***************************************************************/
863/* Do not modify the above code.
864   You are allowed to use the following global variables in your
865   code. These are defined above.
866
867   RUN_BIT
868   REGS
869   MEMORY
870
871   PC
872   N
873   Z
874   P
875
876   dep_stall
877   v_de_br_stall
878   v_agex_br_stall
879   v_mem_br_stall
880   mem_stall
881   icache_r
882
883   PS
884   NEW_PS
885
886
887   You may define your own local/global variables and functions.
888   You may use the functions to get at the control bits defined
889   above.
890
891
892   Begin your code here 	  			       */
893/***************************************************************/
894#define COPY_AGEX_CS_START 3
895#define COPY_MEM_CS_START 9
896#define COPY_SR_CS_START 7
897
898/* Signals generated by SR stage and needed by previous stages in the
899   pipeline are declared below. */
900int sr_reg_data, sr_n, sr_z, sr_p, v_sr_ld_cc, v_sr_ld_reg, sr_reg_id;
901
902/************************* SR_stage() *************************/
903void SR_stage() {
904    /* You are given the code for SR_stage to get you started. Look at
905       the figure for SR stage to see how this code is implemented. */
906
907    switch (Get_DR_VALUEMUX1(PS.SR_CS)) {
908    case 0: sr_reg_data = PS.SR_ADDRESS; break;
909    case 1: sr_reg_data = PS.SR_DATA; break;
910    case 2: sr_reg_data = PS.SR_NPC; break;
911    case 3: sr_reg_data = PS.SR_ALU_RESULT; break;
912    }
913
914    sr_reg_id = PS.SR_DRID;
915    v_sr_ld_reg = Get_SR_LD_REG(PS.SR_CS) & PS.SR_V;
916    v_sr_ld_cc = Get_SR_LD_CC(PS.SR_CS) & PS.SR_V;
917
918    /* CC LOGIC  */
919    sr_n = ((sr_reg_data & 0x8000) ? 1 : 0);
920    sr_z = ((sr_reg_data & 0xFFFF) ? 0 : 1);
921    sr_p = 0;
922    if ((!sr_n) && (!sr_z))
923        sr_p = 1;
924}
925
926// Other signals needed by previous stages
927int mem_pcmux, target_pc, trap_pc;
928int v_mem_ld_cc, v_mem_ld_reg, mem_reg_id, dcache_r;
929/************************* MEM_stage() *************************/
930void MEM_stage() {
931    int ii, jj = 0;
932    int en = Get_DCACHE_EN(PS.MEM_CS) && PS.MEM_V;
933    target_pc = PS.MEM_ADDRESS;
934    int contents = 0;
935    int data = PS.MEM_ALU_RESULT;
936    if (en) {
937        int w0, w1;
938        if (PS.MEM_ADDRESS & 1) {
939            // could be an alignment exception but we aren't checking
940            w0 = 0;
941            w1 = 1;
942            data <<= 8;
943        } else {
944            if (Get_DATA_SIZE(PS.MEM_CS)) {
945                w0 = 1;
946                w1 = 1;
947            } else {
948                w0 = 1;
949                w1 = 0;
950            }
951        }
952        if (!Get_DCACHE_RW(PS.MEM_CS)) {
953            w0 = w1 = 0;
954        }
955        dcache_access(PS.MEM_ADDRESS, &contents, PS.MEM_ALU_RESULT, &dcache_r,
956                      w0, w1);
957        if (PS.MEM_ADDRESS & 1) {
958            // could be an alignment exception but we aren't checking
959            contents = contents >> 8;
960        }
961        if (!Get_DATA_SIZE(PS.MEM_CS)) {
962            if (contents & 0x80) {
963                contents |= 0xff00;
964            }
965        }
966    }
967    mem_stall = en && !dcache_r;
968    v_mem_ld_cc = Get_MEM_LD_CC(PS.MEM_CS) && PS.MEM_V;
969    v_mem_ld_reg = Get_MEM_LD_REG(PS.MEM_CS) && PS.MEM_V;
970    v_mem_br_stall = Get_MEM_BR_STALL(PS.MEM_CS) && PS.MEM_V;
971    mem_reg_id = PS.MEM_DRID;
972    mem_pcmux = 0;
973    if (PS.MEM_V) {
974        if (Get_UNCOND_OP(PS.MEM_CS)) {
975            mem_pcmux = 1;
976        } else if (Get_BR_OP(PS.MEM_CS) &&
977                   (PS.MEM_CC | PS.MEM_IR >> 9) == PS.MEM_IR >> 9) {
978            mem_pcmux = 1;
979        } else if (Get_TRAP_OP(PS.MEM_CS)) {
980            mem_pcmux = 2;
981        }
982    }
983    trap_pc = NEW_PS.SR_DATA = contents;
984    NEW_PS.SR_DRID = PS.MEM_DRID;
985    NEW_PS.SR_NPC = PS.MEM_NPC;
986    NEW_PS.SR_ADDRESS = PS.MEM_ADDRESS;
987    NEW_PS.SR_ALU_RESULT = PS.MEM_ALU_RESULT;
988    NEW_PS.SR_IR = PS.MEM_IR;
989    NEW_PS.SR_V = PS.MEM_V && !mem_stall;
990    /* The code below propagates the control signals from MEM.CS latch
991       to SR.CS latch. You still need to latch other values into the
992       other SR latches. */
993    for (ii = COPY_SR_CS_START; ii < NUM_MEM_CS_BITS; ii++) {
994        NEW_PS.SR_CS[jj++] = PS.MEM_CS[ii];
995    }
996}
997
998int v_agex_ld_cc, v_agex_ld_reg, agex_reg_id;
999#define SEXT_IR(bits) ((struct { int x : (bits); }){.x = PS.AGEX_IR}.x)
1000/************************* AGEX_stage() *************************/
1001void AGEX_stage() {
1002    int ii, jj = 0;
1003    int sr1 = PS.AGEX_SR1;
1004    int sr2 = PS.AGEX_SR2;
1005    if (Get_SR2MUX(PS.AGEX_CS)) {
1006        sr2 = SEXT_IR(5);
1007    }
1008    int alu;
1009    switch (Get_ALUK(PS.AGEX_CS)) {
1010    case 0: alu = sr1 + sr2; break;
1011    case 1: alu = sr1 & sr2; break;
1012    case 2: alu = sr1 ^ sr2; break;
1013    case 3: alu = sr2; break;
1014    }
1015    int shift_amt = PS.AGEX_IR & 0xf;
1016    int shf;
1017    switch (PS.AGEX_IR >> 4 & 3) {
1018    case 0: shf = sr1 << shift_amt; break;
1019    case 1: shf = sr1 >> shift_amt; break;
1020    case 2: shf = sr1 << shift_amt; break;
1021    case 3:
1022        if (sr1 & 0x8000) {
1023            sr1 |= 0xffff0000;
1024        }
1025        shf = sr1 >> shift_amt;
1026        shf &= 0xffff;
1027        break;
1028    }
1029    int result = shf;
1030    if (Get_ALU_RESULTMUX(PS.AGEX_CS)) {
1031        result = alu;
1032    }
1033    int address = PS.AGEX_NPC;
1034    if (Get_ADDR1MUX(PS.AGEX_CS)) {
1035        address = PS.AGEX_SR1;
1036    }
1037    int offset;
1038    switch (Get_ADDR2MUX(PS.AGEX_CS)) {
1039    case 0: offset = 0; break;
1040    case 1: offset = SEXT_IR(6); break;
1041    case 2: offset = SEXT_IR(9); break;
1042    case 3: offset = SEXT_IR(11); break;
1043    }
1044    if (Get_LSHF1(PS.AGEX_CS)) {
1045        offset <<= 1;
1046    }
1047    if (Get_ADDRESSMUX(PS.AGEX_CS)) {
1048        address += offset;
1049    } else {
1050        address = (PS.AGEX_IR & 0x00ff) << 1;
1051    }
1052    v_agex_ld_cc = Get_AGEX_LD_CC(PS.AGEX_CS) && PS.AGEX_V;
1053    v_agex_ld_reg = Get_AGEX_LD_REG(PS.AGEX_CS) && PS.AGEX_V;
1054    v_agex_br_stall = Get_AGEX_BR_STALL(PS.AGEX_CS) && PS.AGEX_V;
1055    agex_reg_id = PS.AGEX_DRID;
1056    if (!mem_stall) {
1057        NEW_PS.MEM_ALU_RESULT = result & 0xffff;
1058        NEW_PS.MEM_DRID = PS.AGEX_DRID;
1059        NEW_PS.MEM_NPC = PS.AGEX_NPC;
1060        NEW_PS.MEM_CC = PS.AGEX_CC;
1061        NEW_PS.MEM_IR = PS.AGEX_IR;
1062        NEW_PS.MEM_ADDRESS = address;
1063        NEW_PS.MEM_V = PS.AGEX_V;
1064        /* The code below propagates the control signals from AGEX.CS latch
1065           to MEM.CS latch. */
1066        for (ii = COPY_MEM_CS_START; ii < NUM_AGEX_CS_BITS; ii++) {
1067            NEW_PS.MEM_CS[jj++] = PS.AGEX_CS[ii];
1068        }
1069    }
1070}
1071
1072/************************* DE_stage() *************************/
1073void DE_stage() {
1074    int CONTROL_STORE_ADDRESS = (PS.DE_IR & 0xf000) >> 10 |
1075                                (PS.DE_IR & 0x0800) >> 10 |
1076                                (PS.DE_IR & 0x0020) >> 5;
1077    int ii, jj = 0;
1078    v_de_br_stall =
1079        Get_DE_BR_STALL(CONTROL_STORE[CONTROL_STORE_ADDRESS]) && PS.DE_V;
1080    int sr1 = (PS.DE_IR & 0x01c0) >> 6;
1081    int sr2 = PS.DE_IR & 7;
1082    // IR 13 decides which bits are sr2
1083    if (PS.DE_IR & 0x2000) {
1084        sr2 = (PS.DE_IR & 0x0e00) >> 9;
1085    }
1086    dep_stall = 0;
1087    // Stall if BR instruction needs to wait on CC
1088    if (Get_DE_BR_OP(CONTROL_STORE[CONTROL_STORE_ADDRESS]) &&
1089        (v_agex_ld_cc || v_mem_ld_cc || v_sr_ld_cc)) {
1090        dep_stall = 1;
1091    }
1092    // Stall if we need SR1 and something will write to it
1093    if (Get_SR1_NEEDED(CONTROL_STORE[CONTROL_STORE_ADDRESS]) &&
1094        ((v_agex_ld_reg && agex_reg_id == sr1) ||
1095         (v_mem_ld_reg && mem_reg_id == sr1) ||
1096         (v_sr_ld_reg && sr_reg_id == sr1))) {
1097        dep_stall = 1;
1098    }
1099    // Stall if we need SR2 and something will write to it
1100    if (Get_SR2_NEEDED(CONTROL_STORE[CONTROL_STORE_ADDRESS]) &&
1101        ((v_agex_ld_reg && agex_reg_id == sr2) ||
1102         (v_mem_ld_reg && mem_reg_id == sr2) ||
1103         (v_sr_ld_reg && sr_reg_id == sr2))) {
1104        dep_stall = 1;
1105    }
1106    // If those signals werent valid forget I ever asked
1107    if (!PS.DE_V) {
1108        dep_stall = 0;
1109    }
1110    if (!mem_stall) {
1111        NEW_PS.AGEX_NPC = PS.DE_NPC;
1112        NEW_PS.AGEX_CC = N << 2 | Z << 1 | P;
1113        NEW_PS.AGEX_DRID = (PS.DE_IR & 0x0e00) >> 9;
1114        if (Get_DRMUX(CONTROL_STORE[CONTROL_STORE_ADDRESS])) {
1115            NEW_PS.AGEX_DRID = 7;
1116        }
1117        NEW_PS.AGEX_IR = PS.DE_IR;
1118        NEW_PS.AGEX_SR1 = REGS[sr1];
1119        NEW_PS.AGEX_SR2 = REGS[sr2];
1120        NEW_PS.AGEX_V = PS.DE_V && !dep_stall;
1121
1122        /* The code below propagates the control signals from the CONTROL
1123           STORE to the AGEX.CS latch. */
1124        for (ii = COPY_AGEX_CS_START; ii < NUM_CONTROL_STORE_BITS; ii++) {
1125            NEW_PS.AGEX_CS[jj++] = CONTROL_STORE[CONTROL_STORE_ADDRESS][ii];
1126        }
1127    }
1128    // Write previous results to register file
1129    if (v_sr_ld_cc) {
1130        N = sr_n;
1131        Z = sr_z;
1132        P = sr_p;
1133    }
1134    if (v_sr_ld_reg) {
1135        REGS[sr_reg_id] = sr_reg_data;
1136    }
1137}
1138
1139/************************* FETCH_stage() *************************/
1140void FETCH_stage() {
1141    if (!(mem_stall || dep_stall)) {
1142        NEW_PS.DE_NPC = PC + 2;
1143        NEW_PS.DE_IR = 0;
1144        icache_access(PC, &NEW_PS.DE_IR, &icache_r);
1145        if (v_de_br_stall || v_agex_br_stall || v_mem_br_stall) {
1146            NEW_PS.DE_V = 0;
1147        } else if ((NEW_PS.DE_V = icache_r)) {
1148            PC += 2;
1149        }
1150    }
1151    switch (mem_pcmux) {
1152    case 1: PC = target_pc; break;
1153    case 2: PC = trap_pc; break;
1154    }
1155}