+/EE460N

computer architecture

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

Joseph RyanLab 51daf436

master
36.8 KiB1139 linesraw
1/*
2  Name 1: Joseph Ryan
3  UTEID 1: jbr2558
4*/
5
6/***************************************************************/
7/*                                                             */
8/*   LC-3b Simulator                                           */
9/*                                                             */
10/*   EE 460N - Lab 5                                           */
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/*         pagetable    page table in LC-3b machine language   */
23/*         isaprogram   LC-3b machine language program file    */
24/*                                                             */
25/***************************************************************/
26
27/***************************************************************/
28/* These are the functions you'll have to write.               */
29/***************************************************************/
30
31void eval_micro_sequencer();
32void cycle_memory();
33void eval_bus_drivers();
34void drive_bus();
35void latch_datapath_values();
36
37/***************************************************************/
38/* A couple of useful definitions.                             */
39/***************************************************************/
40#define FALSE 0
41#define TRUE 1
42
43/***************************************************************/
44/* Use this to avoid overflowing 16 bits on the bus.           */
45/***************************************************************/
46#define Low16bits(x) ((x)&0xFFFF)
47
48/***************************************************************/
49/* Definition of the control store layout.                     */
50/***************************************************************/
51#define CONTROL_STORE_ROWS 64
52#define INITIAL_STATE_NUMBER 18
53
54/***************************************************************/
55/* Definition of bit order in control store word.              */
56/***************************************************************/
57enum CS_BITS {
58    IRD,
59    COND1,
60    COND0,
61    J5,
62    J4,
63    J3,
64    J2,
65    J1,
66    J0,
67    LD_MAR,
68    LD_MDR,
69    LD_IR,
70    LD_BEN,
71    LD_REG,
72    LD_CC,
73    LD_PC,
74    GATE_PC,
75    GATE_MDR,
76    GATE_ALU,
77    GATE_MARMUX,
78    GATE_SHF,
79    PCMUX1,
80    PCMUX0,
81    DRMUX,
82    SR1MUX,
83    ADDR1MUX,
84    ADDR2MUX1,
85    ADDR2MUX0,
86    MARMUX,
87    ALUK1,
88    ALUK0,
89    MIO_EN,
90    R_W,
91    DATA_SIZE,
92    LSHF1,
93    /* MODIFY: you have to add all your new control signals */
94    Check_A,
95    GATE_PSR,
96    LD_PSR,
97    MODEMUX,
98    SWAP,
99    Check_INT,
100    SPMUX,
101    SP_ADD_SUB,
102    GATE_VEC,
103    LD_VEC2,
104    LD_VEC1,
105    LD_VEC0,
106    GATE_PTBR,
107    GATE_VA,
108    LD_VA,
109    VAMUX,
110    PFNMUX,
111    PTE,
112    JumpBack,
113    JumpLoc3,
114    JumpLoc2,
115    JumpLoc1,
116    JumpLoc0,
117    CONTROL_STORE_BITS
118} CS_BITS;
119
120/***************************************************************/
121/* Functions to get at the control bits.                       */
122/***************************************************************/
123int GetIRD(int* x) {
124    return (x[IRD]);
125}
126int GetCOND(int* x) {
127    return ((x[COND1] << 1) + x[COND0]);
128}
129int GetJ(int* x) {
130    return ((x[J5] << 5) + (x[J4] << 4) + (x[J3] << 3) + (x[J2] << 2) +
131            (x[J1] << 1) + x[J0]);
132}
133int GetLD_MAR(int* x) {
134    return (x[LD_MAR]);
135}
136int GetLD_MDR(int* x) {
137    return (x[LD_MDR]);
138}
139int GetLD_IR(int* x) {
140    return (x[LD_IR]);
141}
142int GetLD_BEN(int* x) {
143    return (x[LD_BEN]);
144}
145int GetLD_REG(int* x) {
146    return (x[LD_REG]);
147}
148int GetLD_CC(int* x) {
149    return (x[LD_CC]);
150}
151int GetLD_PC(int* x) {
152    return (x[LD_PC]);
153}
154int GetGATE_PC(int* x) {
155    return (x[GATE_PC]);
156}
157int GetGATE_MDR(int* x) {
158    return (x[GATE_MDR]);
159}
160int GetGATE_ALU(int* x) {
161    return (x[GATE_ALU]);
162}
163int GetGATE_MARMUX(int* x) {
164    return (x[GATE_MARMUX]);
165}
166int GetGATE_SHF(int* x) {
167    return (x[GATE_SHF]);
168}
169int GetPCMUX(int* x) {
170    return ((x[PCMUX1] << 1) + x[PCMUX0]);
171}
172int GetDRMUX(int* x) {
173    return (x[DRMUX]);
174}
175int GetSR1MUX(int* x) {
176    return (x[SR1MUX]);
177}
178int GetADDR1MUX(int* x) {
179    return (x[ADDR1MUX]);
180}
181int GetADDR2MUX(int* x) {
182    return ((x[ADDR2MUX1] << 1) + x[ADDR2MUX0]);
183}
184int GetMARMUX(int* x) {
185    return (x[MARMUX]);
186}
187int GetALUK(int* x) {
188    return ((x[ALUK1] << 1) + x[ALUK0]);
189}
190int GetMIO_EN(int* x) {
191    return (x[MIO_EN]);
192}
193int GetR_W(int* x) {
194    return (x[R_W]);
195}
196int GetDATA_SIZE(int* x) {
197    return (x[DATA_SIZE]);
198}
199int GetLSHF1(int* x) {
200    return (x[LSHF1]);
201}
202/* MODIFY: you can add more Get functions for your new control signals */
203int GetGATE_PSR(int* x) {
204    return x[GATE_PSR];
205}
206int GetLD_PSR(int* x) {
207    return x[LD_PSR];
208}
209int GetMODEMUX(int* x) {
210    return x[MODEMUX];
211}
212int GetSWAP(int* x) {
213    return x[SWAP];
214}
215int GetCheck_INT(int* x) {
216    return x[Check_INT];
217}
218int GetSPMUX(int* x) {
219    return x[SPMUX];
220}
221int GetSP_ADD_SUB(int* x) {
222    return x[SP_ADD_SUB];
223}
224int GetCheck_A(int* x) {
225    return x[Check_A];
226}
227int GetLD_VEC(int* x) {
228    return x[LD_VEC0] | x[LD_VEC1] << 1 | x[LD_VEC2] << 2;
229}
230int GetGATE_VEC(int* x) {
231    return x[GATE_VEC];
232}
233int GetGATE_PTBR(int* x) {
234    return x[GATE_PTBR];
235}
236int GetGATE_VA(int* x) {
237    return x[GATE_VA];
238}
239int GetLD_VA(int* x) {
240    return x[LD_VA];
241}
242int GetVAMUX(int* x) {
243    return x[VAMUX];
244}
245int GetPFNMUX(int* x) {
246    return x[PFNMUX];
247}
248int GetPTE(int* x) {
249    return x[PTE];
250}
251int GetJumpBack(int* x) {
252    return x[JumpBack];
253}
254int GetJumpLoc(int* x) {
255    return x[JumpLoc0] | x[JumpLoc1] << 1 | x[JumpLoc2] << 2 | x[JumpLoc3] << 3;
256}
257
258/***************************************************************/
259/* The control store rom.                                      */
260/***************************************************************/
261int CONTROL_STORE[CONTROL_STORE_ROWS][CONTROL_STORE_BITS];
262
263/***************************************************************/
264/* Main memory.                                                */
265/***************************************************************/
266/* MEMORY[A][0] stores the least significant byte of word at word address A
267   MEMORY[A][1] stores the most significant byte of word at word address A
268   There are two write enable signals, one for each byte. WE0 is used for
269   the least significant byte of a word. WE1 is used for the most significant
270   byte of a word. */
271
272#define WORDS_IN_MEM 0x2000 /* 32 frames */
273#define MEM_CYCLES 5
274int MEMORY[WORDS_IN_MEM][2];
275
276/***************************************************************/
277
278/***************************************************************/
279
280/***************************************************************/
281/* LC-3b State info.                                           */
282/***************************************************************/
283#define LC_3b_REGS 8
284
285int RUN_BIT; /* run bit */
286int BUS;     /* value of the bus */
287
288typedef struct System_Latches_Struct {
289    int PC,  /* program counter */
290        MDR, /* memory data register */
291        MAR, /* memory address register */
292        IR,  /* instruction register */
293        N,   /* n condition bit */
294        Z,   /* z condition bit */
295        P,   /* p condition bit */
296        BEN; /* ben register */
297
298    int READY; /* ready bit */
299    /* The ready bit is also latched as you dont want the memory system to
300       assert it at a bad point in the cycle*/
301
302    int REGS[LC_3b_REGS]; /* register file. */
303
304    int MICROINSTRUCTION[CONTROL_STORE_BITS]; /* The microintruction */
305
306    int STATE_NUMBER; /* Current State Number - Provided for debugging */
307
308    /* For lab 4 */
309    int VEC;
310    int SSP;
311    int MODE;
312    int INT;
313
314    /* For lab 5 */
315    int PTBR;    /* This is initialized when we load the page table */
316    int VA;      /* Temporary VA register */
317    int JUMPLOC; /* saved jump location */
318
319} System_Latches;
320
321/* Data Structure for Latch */
322
323System_Latches CURRENT_LATCHES, NEXT_LATCHES;
324
325/* For lab 5 */
326#define PAGE_NUM_BITS 9
327#define PTE_PFN_MASK 0x3E00
328#define PTE_VALID_MASK 0x0004
329#define PAGE_OFFSET_MASK 0x1FF
330
331/***************************************************************/
332/* A cycle counter.                                            */
333/***************************************************************/
334int CYCLE_COUNT;
335
336/***************************************************************/
337/*                                                             */
338/* Procedure : help                                            */
339/*                                                             */
340/* Purpose   : Print out a list of commands.                   */
341/*                                                             */
342/***************************************************************/
343void help() {
344    printf("----------------LC-3bSIM Help-------------------------\n");
345    printf("go               -  run program to completion       \n");
346    printf("run n            -  execute program for n cycles    \n");
347    printf("mdump low high   -  dump memory from low to high    \n");
348    printf("rdump            -  dump the register & bus values  \n");
349    printf("?                -  display this help menu          \n");
350    printf("quit             -  exit the program                \n\n");
351}
352
353/***************************************************************/
354/*                                                             */
355/* Procedure : cycle                                           */
356/*                                                             */
357/* Purpose   : Execute a cycle                                 */
358/*                                                             */
359/***************************************************************/
360void cycle() {
361    eval_micro_sequencer();
362    cycle_memory();
363    eval_bus_drivers();
364    drive_bus();
365    latch_datapath_values();
366
367    if (CYCLE_COUNT == 299) {
368        NEXT_LATCHES.INT = 1;
369        NEXT_LATCHES.VEC = 1;
370    }
371    CURRENT_LATCHES = NEXT_LATCHES;
372
373    CYCLE_COUNT++;
374}
375
376/***************************************************************/
377/*                                                             */
378/* Procedure : run n                                           */
379/*                                                             */
380/* Purpose   : Simulate the LC-3b for n cycles.                 */
381/*                                                             */
382/***************************************************************/
383void run(int num_cycles) {
384    int i;
385
386    if (RUN_BIT == FALSE) {
387        printf("Can't simulate, Simulator is halted\n\n");
388        return;
389    }
390
391    printf("Simulating for %d cycles...\n\n", num_cycles);
392    for (i = 0; i < num_cycles; i++) {
393        if (CURRENT_LATCHES.PC == 0x0000) {
394            RUN_BIT = FALSE;
395            printf("Simulator halted\n\n");
396            break;
397        }
398        cycle();
399    }
400}
401
402/***************************************************************/
403/*                                                             */
404/* Procedure : go                                              */
405/*                                                             */
406/* Purpose   : Simulate the LC-3b until HALTed.                 */
407/*                                                             */
408/***************************************************************/
409void go() {
410    if (RUN_BIT == FALSE) {
411        printf("Can't simulate, Simulator is halted\n\n");
412        return;
413    }
414
415    printf("Simulating...\n\n");
416    while (CURRENT_LATCHES.PC != 0x0000) cycle();
417    RUN_BIT = FALSE;
418    printf("Simulator halted\n\n");
419}
420
421/***************************************************************/
422/*                                                             */
423/* Procedure : mdump                                           */
424/*                                                             */
425/* Purpose   : Dump a word-aligned region of memory to the     */
426/*             output file.                                    */
427/*                                                             */
428/***************************************************************/
429void mdump(FILE* dumpsim_file, int start, int stop) {
430    int address; /* this is a byte address */
431
432    printf("\nMemory content [0x%0.4x..0x%0.4x] :\n", start, stop);
433    printf("-------------------------------------\n");
434    for (address = (start >> 1); address <= (stop >> 1); address++)
435        printf("  0x%0.4x (%d) : 0x%0.2x%0.2x\n", address << 1, address << 1,
436               MEMORY[address][1], MEMORY[address][0]);
437    printf("\n");
438
439    /* dump the memory contents into the dumpsim file */
440    fprintf(dumpsim_file, "\nMemory content [0x%0.4x..0x%0.4x] :\n", start,
441            stop);
442    fprintf(dumpsim_file, "-------------------------------------\n");
443    for (address = (start >> 1); address <= (stop >> 1); address++)
444        fprintf(dumpsim_file, " 0x%0.4x (%d) : 0x%0.2x%0.2x\n", address << 1,
445                address << 1, MEMORY[address][1], MEMORY[address][0]);
446    fprintf(dumpsim_file, "\n");
447    fflush(dumpsim_file);
448}
449
450/***************************************************************/
451/*                                                             */
452/* Procedure : rdump                                           */
453/*                                                             */
454/* Purpose   : Dump current register and bus values to the     */
455/*             output file.                                    */
456/*                                                             */
457/***************************************************************/
458void rdump(FILE* dumpsim_file) {
459    int k;
460
461    printf("\nCurrent register/bus values :\n");
462    printf("-------------------------------------\n");
463    printf("Cycle Count  : %d\n", CYCLE_COUNT);
464    printf("PC           : 0x%0.4x\n", CURRENT_LATCHES.PC);
465    printf("IR           : 0x%0.4x\n", CURRENT_LATCHES.IR);
466    printf("STATE_NUMBER : 0x%0.4x\n\n", CURRENT_LATCHES.STATE_NUMBER);
467    printf("BUS          : 0x%0.4x\n", BUS);
468    printf("MDR          : 0x%0.4x\n", CURRENT_LATCHES.MDR);
469    printf("MAR          : 0x%0.4x\n", CURRENT_LATCHES.MAR);
470    printf("CCs: N = %d  Z = %d  P = %d\n", CURRENT_LATCHES.N,
471           CURRENT_LATCHES.Z, CURRENT_LATCHES.P);
472    printf("Registers:\n");
473    for (k = 0; k < LC_3b_REGS; k++)
474        printf("%d: 0x%0.4x\n", k, CURRENT_LATCHES.REGS[k]);
475    printf("\n");
476
477    /* dump the state information into the dumpsim file */
478    fprintf(dumpsim_file, "\nCurrent register/bus values :\n");
479    fprintf(dumpsim_file, "-------------------------------------\n");
480    fprintf(dumpsim_file, "Cycle Count  : %d\n", CYCLE_COUNT);
481    fprintf(dumpsim_file, "PC           : 0x%0.4x\n", CURRENT_LATCHES.PC);
482    fprintf(dumpsim_file, "IR           : 0x%0.4x\n", CURRENT_LATCHES.IR);
483    fprintf(dumpsim_file, "STATE_NUMBER : 0x%0.4x\n\n",
484            CURRENT_LATCHES.STATE_NUMBER);
485    fprintf(dumpsim_file, "BUS          : 0x%0.4x\n", BUS);
486    fprintf(dumpsim_file, "MDR          : 0x%0.4x\n", CURRENT_LATCHES.MDR);
487    fprintf(dumpsim_file, "MAR          : 0x%0.4x\n", CURRENT_LATCHES.MAR);
488    fprintf(dumpsim_file, "CCs: N = %d  Z = %d  P = %d\n", CURRENT_LATCHES.N,
489            CURRENT_LATCHES.Z, CURRENT_LATCHES.P);
490    fprintf(dumpsim_file, "Registers:\n");
491    for (k = 0; k < LC_3b_REGS; k++)
492        fprintf(dumpsim_file, "%d: 0x%0.4x\n", k, CURRENT_LATCHES.REGS[k]);
493    fprintf(dumpsim_file, "\n");
494    fflush(dumpsim_file);
495}
496
497/***************************************************************/
498/*                                                             */
499/* Procedure : get_command                                     */
500/*                                                             */
501/* Purpose   : Read a command from standard input.             */
502/*                                                             */
503/***************************************************************/
504void get_command(FILE* dumpsim_file) {
505    char buffer[20];
506    int start, stop, cycles;
507
508    printf("LC-3b-SIM> ");
509
510    scanf("%s", buffer);
511    printf("\n");
512
513    switch (buffer[0]) {
514    case 'G':
515    case 'g': go(); break;
516
517    case 'M':
518    case 'm':
519        scanf("%i %i", &start, &stop);
520        mdump(dumpsim_file, start, stop);
521        break;
522
523    case '?': help(); break;
524    case 'Q':
525    case 'q': printf("Bye.\n"); exit(0);
526
527    case 'R':
528    case 'r':
529        if (buffer[1] == 'd' || buffer[1] == 'D')
530            rdump(dumpsim_file);
531        else {
532            scanf("%d", &cycles);
533            run(cycles);
534        }
535        break;
536
537    default: printf("Invalid Command\n"); break;
538    }
539}
540
541/***************************************************************/
542/*                                                             */
543/* Procedure : init_control_store                              */
544/*                                                             */
545/* Purpose   : Load microprogram into control store ROM        */
546/*                                                             */
547/***************************************************************/
548void init_control_store(char* ucode_filename) {
549    FILE* ucode;
550    int i, j, index;
551    char line[200];
552
553    printf("Loading Control Store from file: %s\n", ucode_filename);
554
555    /* Open the micro-code file. */
556    if ((ucode = fopen(ucode_filename, "r")) == NULL) {
557        printf("Error: Can't open micro-code file %s\n", ucode_filename);
558        exit(-1);
559    }
560
561    /* Read a line for each row in the control store. */
562    for (i = 0; i < CONTROL_STORE_ROWS; i++) {
563        if (fscanf(ucode, "%[^\n]\n", line) == EOF) {
564            printf("Error: Too few lines (%d) in micro-code file: %s\n", i,
565                   ucode_filename);
566            exit(-1);
567        }
568
569        /* Put in bits one at a time. */
570        index = 0;
571
572        for (j = 0; j < CONTROL_STORE_BITS; j++) {
573            /* Needs to find enough bits in line. */
574            if (line[index] == '\0') {
575                printf("Error: Too few control bits in micro-code file: "
576                       "%s\nLine: %d\n",
577                       ucode_filename, i);
578                exit(-1);
579            }
580            if (line[index] != '0' && line[index] != '1') {
581                printf("Error: Unknown value in micro-code file: %s\nLine: %d, "
582                       "Bit: %d\n",
583                       ucode_filename, i, j);
584                exit(-1);
585            }
586
587            /* Set the bit in the Control Store. */
588            CONTROL_STORE[i][j] = (line[index] == '0') ? 0 : 1;
589            index++;
590        }
591
592        /* Warn about extra bits in line. */
593        if (line[index] != '\0')
594            printf("Warning: Extra bit(s) in control store file %s. Line: %d\n",
595                   ucode_filename, i);
596    }
597    printf("\n");
598}
599
600/***************************************************************/
601/*                                                             */
602/* Procedure : init_memory                                     */
603/*                                                             */
604/* Purpose   : Zero out the memory array                       */
605/*                                                             */
606/***************************************************************/
607void init_memory() {
608    int i;
609
610    for (i = 0; i < WORDS_IN_MEM; i++) {
611        MEMORY[i][0] = 0;
612        MEMORY[i][1] = 0;
613    }
614}
615
616/**************************************************************/
617/*                                                            */
618/* Procedure : load_program                                   */
619/*                                                            */
620/* Purpose   : Load program and service routines into mem.    */
621/*                                                            */
622/**************************************************************/
623void load_program(char* program_filename, int is_virtual_base) {
624    FILE* prog;
625    int ii, word, program_base, pte, virtual_pc;
626
627    /* Open program file. */
628    prog = fopen(program_filename, "r");
629    if (prog == NULL) {
630        printf("Error: Can't open program file %s\n", program_filename);
631        exit(-1);
632    }
633
634    /* Read in the program. */
635    if (fscanf(prog, "%x\n", &word) != EOF)
636        program_base = word >> 1;
637    else {
638        printf("Error: Program file is empty\n");
639        exit(-1);
640    }
641
642    if (is_virtual_base) {
643        if (CURRENT_LATCHES.PTBR == 0) {
644            printf("Error: Page table base not loaded %s\n", program_filename);
645            exit(-1);
646        }
647
648        /* convert virtual_base to physical_base */
649        virtual_pc = program_base << 1;
650        pte = (MEMORY[(CURRENT_LATCHES.PTBR +
651                       (((program_base << 1) >> PAGE_NUM_BITS) << 1)) >>
652                      1][1]
653               << 8) |
654              MEMORY[(CURRENT_LATCHES.PTBR +
655                      (((program_base << 1) >> PAGE_NUM_BITS) << 1)) >>
656                     1][0];
657
658        printf("virtual base of program: %04x\npte: %04x\n", program_base << 1,
659               pte);
660        if ((pte & PTE_VALID_MASK) == PTE_VALID_MASK) {
661            program_base =
662                (pte & PTE_PFN_MASK) | ((program_base << 1) & PAGE_OFFSET_MASK);
663            printf("physical base of program: %x\n\n", program_base);
664            program_base = program_base >> 1;
665        } else {
666            printf("attempting to load a program into an invalid "
667                   "(non-resident) page\n\n");
668            exit(-1);
669        }
670    } else {
671        /* is page table */
672        CURRENT_LATCHES.PTBR = program_base << 1;
673    }
674
675    ii = 0;
676    while (fscanf(prog, "%x\n", &word) != EOF) {
677        /* Make sure it fits. */
678        if (program_base + ii >= WORDS_IN_MEM) {
679            printf("Error: Program file %s is too long to fit in memory. %x\n",
680                   program_filename, ii);
681            exit(-1);
682        }
683
684        /* Write the word to memory array. */
685        MEMORY[program_base + ii][0] = word & 0x00FF;
686        MEMORY[program_base + ii][1] = (word >> 8) & 0x00FF;
687        ;
688        ii++;
689    }
690
691    if (CURRENT_LATCHES.PC == 0 && is_virtual_base)
692        CURRENT_LATCHES.PC = virtual_pc;
693
694    printf("Read %d words from program into memory.\n\n", ii);
695}
696
697/***************************************************************/
698/*                                                             */
699/* Procedure : initialize                                      */
700/*                                                             */
701/* Purpose   : Load microprogram and machine language program  */
702/*             and set up initial state of the machine         */
703/*                                                             */
704/***************************************************************/
705void initialize(char* argv[], int num_prog_files) {
706    int i;
707    init_control_store(argv[1]);
708
709    init_memory();
710    load_program(argv[2], 0);
711    for (i = 0; i < num_prog_files; i++) { load_program(argv[i + 3], 1); }
712    CURRENT_LATCHES.Z = 1;
713    CURRENT_LATCHES.STATE_NUMBER = INITIAL_STATE_NUMBER;
714    memcpy(CURRENT_LATCHES.MICROINSTRUCTION,
715           CONTROL_STORE[INITIAL_STATE_NUMBER],
716           sizeof(int) * CONTROL_STORE_BITS);
717    CURRENT_LATCHES.SSP = 0x3000; /* Initial value of system stack pointer */
718    CURRENT_LATCHES.MODE = 1;
719
720    /* MODIFY: you can add more initialization code HERE */
721
722    NEXT_LATCHES = CURRENT_LATCHES;
723
724    RUN_BIT = TRUE;
725}
726
727/***************************************************************/
728/*                                                             */
729/* Procedure : main                                            */
730/*                                                             */
731/***************************************************************/
732int main(int argc, char* argv[]) {
733    FILE* dumpsim_file;
734
735    /* Error Checking */
736    if (argc < 4) {
737        printf("Error: usage: %s <micro_code_file> <page table file> "
738               "<program_file_1> <program_file_2> ...\n",
739               argv[0]);
740        exit(1);
741    }
742
743    printf("LC-3b Simulator\n\n");
744
745    initialize(argv, argc - 3);
746
747    if ((dumpsim_file = fopen("dumpsim", "w")) == NULL) {
748        printf("Error: Can't open dumpsim file\n");
749        exit(-1);
750    }
751
752    while (1) get_command(dumpsim_file);
753}
754
755/***************************************************************/
756/* Do not modify the above code, except for the places indicated
757   with a "MODIFY:" comment.
758   You are allowed to use the following global variables in your
759   code. These are defined above.
760
761   CONTROL_STORE
762   MEMORY
763   BUS
764
765   CURRENT_LATCHES
766   NEXT_LATCHES
767
768   You may define your own local/global variables and functions.
769   You may use the functions to get at the control bits defined
770   above.
771
772   Begin your code here 	  			       */
773/***************************************************************/
774
775#include <stdbool.h>
776
777void eval_micro_sequencer() {
778    /*
779     * Evaluate the address of the next state according to the
780     * micro sequencer logic. Latch the next microinstruction.
781     */
782    NEXT_LATCHES.STATE_NUMBER = GetJ(CURRENT_LATCHES.MICROINSTRUCTION);
783    int cond = GetCOND(CURRENT_LATCHES.MICROINSTRUCTION);
784    if (cond == 1) {
785        NEXT_LATCHES.STATE_NUMBER |= CURRENT_LATCHES.READY << 1;
786    } else if (cond == 2) {
787        NEXT_LATCHES.STATE_NUMBER |= CURRENT_LATCHES.BEN << 2;
788    } else if (cond == 3) {
789        NEXT_LATCHES.STATE_NUMBER |= !!(CURRENT_LATCHES.IR & (1 << 11));
790    }
791    if (GetIRD(CURRENT_LATCHES.MICROINSTRUCTION)) {
792        NEXT_LATCHES.STATE_NUMBER = CURRENT_LATCHES.IR >> 12;
793    }
794    if (GetCheck_INT(CURRENT_LATCHES.MICROINSTRUCTION) && CURRENT_LATCHES.INT) {
795        NEXT_LATCHES.STATE_NUMBER = 49;
796    }
797    if (GetJumpBack(CURRENT_LATCHES.MICROINSTRUCTION)) {
798        switch (CURRENT_LATCHES.JUMPLOC) {
799        case 1: NEXT_LATCHES.STATE_NUMBER = 29; break;
800        case 2: NEXT_LATCHES.STATE_NUMBER = 25; break;
801        case 3: NEXT_LATCHES.STATE_NUMBER = 36; break;
802        case 4: NEXT_LATCHES.STATE_NUMBER = 33; break;
803        case 5: NEXT_LATCHES.STATE_NUMBER = 28; break;
804        case 6: NEXT_LATCHES.STATE_NUMBER = 40; break;
805        case 7: NEXT_LATCHES.STATE_NUMBER = 53; break;
806        case 8: NEXT_LATCHES.STATE_NUMBER = 24; break;
807        case 9: NEXT_LATCHES.STATE_NUMBER = 23; break;
808        case 10: NEXT_LATCHES.STATE_NUMBER = 46; break;
809        case 11: NEXT_LATCHES.STATE_NUMBER = 51; break;
810        default: break;
811        }
812        NEXT_LATCHES.JUMPLOC = 0;
813    }
814    memcpy(NEXT_LATCHES.MICROINSTRUCTION,
815           CONTROL_STORE[NEXT_LATCHES.STATE_NUMBER],
816           CONTROL_STORE_BITS * sizeof(int));
817}
818
819int mem_out = 0;
820void cycle_memory() {
821    /*
822     * This function emulates memory and the WE logic.
823     * Keep track of which cycle of MEMEN we are dealing with.
824     * If fourth, we need to latch Ready bit at the end of
825     * cycle to prepare microsequencer for the fifth cycle.
826     */
827    static int current_mem_cycle = 0;
828    if (!GetMIO_EN(CURRENT_LATCHES.MICROINSTRUCTION)) {
829        return;
830    }
831    ++current_mem_cycle;
832    if (current_mem_cycle == MEM_CYCLES - 1) {
833        NEXT_LATCHES.READY = true;
834    } else if (CURRENT_LATCHES.READY) {
835        int word = GetDATA_SIZE(CURRENT_LATCHES.MICROINSTRUCTION);
836        int mar0 = CURRENT_LATCHES.MAR & 1;
837        int mar = CURRENT_LATCHES.MAR >> 1;
838        int mdr_low = CURRENT_LATCHES.MDR & 0xff;
839        int mdr_high = CURRENT_LATCHES.MDR >> 8 & 0xff;
840        if (GetR_W(CURRENT_LATCHES.MICROINSTRUCTION)) {
841            if (word) {
842                if (mar0) {
843                    MEMORY[mar + 1][0] = mdr_low;
844                    MEMORY[mar + 1][1] = mdr_high;
845                } else {
846                    MEMORY[mar][0] = mdr_low;
847                    MEMORY[mar][1] = mdr_high;
848                }
849            } else {
850                if (mar0) {
851                    MEMORY[mar][1] = mdr_high;
852                } else {
853                    MEMORY[mar][0] = mdr_low;
854                }
855            }
856        } else {
857            NEXT_LATCHES.MDR = MEMORY[mar][1] << 8 | MEMORY[mar][0];
858        }
859        current_mem_cycle = 0;
860        NEXT_LATCHES.READY = 0;
861    }
862}
863
864#define SEXT_IR(bits) ((struct { int x : (bits); }){.x = CURRENT_LATCHES.IR}.x)
865
866int gmarmux, gpc, galu, gshf, gmdr, adder, gpsr, gva;
867
868void eval_bus_drivers() {
869    /*
870     * Datapath routine emulating operations before driving the bus.
871     * Evaluate the input of tristate drivers
872     *       Gate_MARMUX,
873     *		 Gate_PC,
874     *		 Gate_ALU,
875     *		 Gate_SHF,
876     *		 Gate_MDR.
877     *       Gate_PSR
878     *       Gate_VEC
879     */
880    // Source Registers
881    int sr1 = CURRENT_LATCHES.REGS[CURRENT_LATCHES.IR >> 9 & 7];
882    if (GetSR1MUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
883        sr1 = CURRENT_LATCHES.REGS[CURRENT_LATCHES.IR >> 6 & 7];
884    }
885    if (GetSPMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
886        sr1 = CURRENT_LATCHES.REGS[6];
887    }
888    int sr2 = CURRENT_LATCHES.REGS[CURRENT_LATCHES.IR & 7];
889    // ALU
890    if (CURRENT_LATCHES.IR & 0x20) {
891        sr2 = SEXT_IR(5);
892    }
893    if (GetSPMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
894        sr2 = GetSP_ADD_SUB(CURRENT_LATCHES.MICROINSTRUCTION) ? -2 : 2;
895    }
896    switch (GetALUK(CURRENT_LATCHES.MICROINSTRUCTION)) {
897    case 0: galu = sr1 + sr2; break;
898    case 1: galu = sr1 & sr2; break;
899    case 2: galu = sr1 ^ sr2; break;
900    case 3: galu = sr1; break;
901    }
902    // SHF
903    int ammount = CURRENT_LATCHES.IR & 0xf;
904    if (CURRENT_LATCHES.IR & 0x10) {
905        if (CURRENT_LATCHES.IR & 0x20) {
906            int temp = sr1;
907            if (sr1 & 0x8000) {
908                temp |= 0xffff0000;
909            }
910            gshf = temp >> ammount;
911        } else {
912            gshf = sr1 >> ammount & (0xffff >> ammount);
913        }
914    } else {
915        gshf = sr1 << ammount;
916    }
917    // PC
918    gpc = CURRENT_LATCHES.PC;
919    // MARMUX
920    int addr2;
921    switch (GetADDR2MUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
922    case 0: addr2 = 0; break;
923    case 1: addr2 = SEXT_IR(6); break;
924    case 2: addr2 = SEXT_IR(9); break;
925    case 3: addr2 = SEXT_IR(11); break;
926    }
927    if (GetLSHF1(CURRENT_LATCHES.MICROINSTRUCTION)) {
928        addr2 <<= 1;
929    }
930    int addr1 = CURRENT_LATCHES.PC;
931    if (GetADDR1MUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
932        addr1 = sr1;
933    }
934    adder = addr2 + addr1;
935    gmarmux = (CURRENT_LATCHES.IR & 0xff) << 1;
936    if (GetMARMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
937        gmarmux = adder;
938    }
939    // MDR
940    int byte = CURRENT_LATCHES.MDR;
941    if (CURRENT_LATCHES.MAR & 1) {
942        byte >>= 8;
943    }
944    if (byte & 0x80) {
945        byte |= 0xff00;
946    } else {
947        byte &= 0xff;
948    }
949    if (GetDATA_SIZE(CURRENT_LATCHES.MICROINSTRUCTION)) {
950        gmdr = CURRENT_LATCHES.MDR;
951    } else {
952        gmdr = byte;
953    }
954    gpsr = CURRENT_LATCHES.MODE << 15 | CURRENT_LATCHES.N << 2 |
955           CURRENT_LATCHES.Z << 1 | CURRENT_LATCHES.P;
956    // VA
957    if (GetVAMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
958        gva = (0xfe00 & CURRENT_LATCHES.VA) >> 8;
959    } else {
960        gva = CURRENT_LATCHES.VA & 0xff;
961    }
962}
963
964void drive_bus() {
965    /*
966     * Datapath routine for driving the bus from one of the 5 possible
967     * tristate drivers.
968     */
969    BUS = 0;
970    if (GetGATE_SHF(CURRENT_LATCHES.MICROINSTRUCTION)) {
971        BUS |= gshf;
972    }
973    if (GetGATE_ALU(CURRENT_LATCHES.MICROINSTRUCTION)) {
974        BUS |= galu;
975    }
976    if (GetGATE_MARMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
977        BUS |= gmarmux;
978    }
979    if (GetGATE_MDR(CURRENT_LATCHES.MICROINSTRUCTION)) {
980        BUS |= gmdr;
981    }
982    if (GetGATE_PC(CURRENT_LATCHES.MICROINSTRUCTION)) {
983        BUS |= gpc;
984    }
985    if (GetGATE_PSR(CURRENT_LATCHES.MICROINSTRUCTION)) {
986        BUS |= gpsr;
987    }
988    if (GetGATE_VEC(CURRENT_LATCHES.MICROINSTRUCTION)) {
989        BUS |= (CURRENT_LATCHES.VEC << 1) + 0x200;
990    }
991    if (GetGATE_VA(CURRENT_LATCHES.MICROINSTRUCTION)) {
992        BUS |= gva;
993    }
994    if (GetGATE_PTBR(CURRENT_LATCHES.MICROINSTRUCTION)) {
995        BUS |= CURRENT_LATCHES.PTBR;
996    }
997    BUS = Low16bits(BUS);
998}
999
1000void latch_datapath_values() {
1001    /*
1002     * Datapath routine for computing all functions that need to latch
1003     * values in the data path at the end of this cycle.  Some values
1004     * require sourcing the bus; therefore, this routine has to come
1005     * after drive_bus.
1006     */
1007    if (GetLD_BEN(CURRENT_LATCHES.MICROINSTRUCTION)) {
1008        NEXT_LATCHES.BEN = 0;
1009        if ((CURRENT_LATCHES.IR & 0x0800 && CURRENT_LATCHES.N) ||
1010            (CURRENT_LATCHES.IR & 0x0400 && CURRENT_LATCHES.Z) ||
1011            (CURRENT_LATCHES.IR & 0x0200 && CURRENT_LATCHES.P)) {
1012            NEXT_LATCHES.BEN = 1;
1013        }
1014    }
1015    if (GetLD_PSR(CURRENT_LATCHES.MICROINSTRUCTION)) {
1016        if (GetLD_CC(CURRENT_LATCHES.MICROINSTRUCTION)) {
1017            NEXT_LATCHES.N = (BUS & 4) >> 2;
1018            NEXT_LATCHES.Z = (BUS & 2) >> 1;
1019            NEXT_LATCHES.P = (BUS & 1);
1020        }
1021        if (GetMODEMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
1022            NEXT_LATCHES.MODE = 0;
1023        } else {
1024            NEXT_LATCHES.MODE = BUS >> 15;
1025        }
1026    } else if (GetLD_CC(CURRENT_LATCHES.MICROINSTRUCTION)) {
1027        NEXT_LATCHES.N = NEXT_LATCHES.Z = NEXT_LATCHES.P = false;
1028        if (BUS & 0x8000) {
1029            NEXT_LATCHES.N = true;
1030        } else if (BUS) {
1031            NEXT_LATCHES.P = true;
1032        } else {
1033            NEXT_LATCHES.Z = true;
1034        }
1035    }
1036    if (GetLD_IR(CURRENT_LATCHES.MICROINSTRUCTION)) {
1037        NEXT_LATCHES.IR = BUS;
1038    }
1039    if (GetLD_MAR(CURRENT_LATCHES.MICROINSTRUCTION)) {
1040        if (GetPFNMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
1041            NEXT_LATCHES.MAR = (CURRENT_LATCHES.MDR & 0x3e00) | (BUS & 0x1FF);
1042        } else {
1043            NEXT_LATCHES.MAR = BUS;
1044            // Check for allignment exceptions
1045            if (NEXT_LATCHES.MAR & 1 &&
1046                GetCheck_A(CURRENT_LATCHES.MICROINSTRUCTION)) {
1047                NEXT_LATCHES.STATE_NUMBER = 34;
1048                memcpy(NEXT_LATCHES.MICROINSTRUCTION,
1049                       CONTROL_STORE[NEXT_LATCHES.STATE_NUMBER],
1050                       CONTROL_STORE_BITS * sizeof(int));
1051            }
1052        }
1053    }
1054    if (GetLD_MDR(CURRENT_LATCHES.MICROINSTRUCTION)) {
1055        if (GetMIO_EN(CURRENT_LATCHES.MICROINSTRUCTION)) {
1056        } else {
1057            if (GetPTE(CURRENT_LATCHES.MICROINSTRUCTION)) {
1058                // Protection
1059                if (CURRENT_LATCHES.MODE && !(CURRENT_LATCHES.MDR & 8) &&
1060                    CURRENT_LATCHES.JUMPLOC != 5 &&
1061                    CURRENT_LATCHES.JUMPLOC != 10) {
1062                    // don't check in trap/int situations before psr gets set
1063                    NEXT_LATCHES.STATE_NUMBER = 26;
1064                    memcpy(NEXT_LATCHES.MICROINSTRUCTION,
1065                           CONTROL_STORE[NEXT_LATCHES.STATE_NUMBER],
1066                           CONTROL_STORE_BITS * sizeof(int));
1067                }
1068                // Page fault
1069                else if (!(CURRENT_LATCHES.MDR & 4)) {
1070                    NEXT_LATCHES.STATE_NUMBER = 61;
1071                    memcpy(NEXT_LATCHES.MICROINSTRUCTION,
1072                           CONTROL_STORE[NEXT_LATCHES.STATE_NUMBER],
1073                           CONTROL_STORE_BITS * sizeof(int));
1074                }
1075                // Set ref
1076                NEXT_LATCHES.MDR |= 1;
1077                // If write, set mod
1078                if (GetJumpLoc(CURRENT_LATCHES.MICROINSTRUCTION) & 8) {
1079                    NEXT_LATCHES.MDR |= 2;
1080                }
1081            } else {
1082                int byte = BUS;
1083                if (CURRENT_LATCHES.MAR & 1) {
1084                    byte <<= 8;
1085                }
1086                if (GetDATA_SIZE(CURRENT_LATCHES.MICROINSTRUCTION)) {
1087                    NEXT_LATCHES.MDR = BUS;
1088                } else {
1089                    NEXT_LATCHES.MDR = byte;
1090                }
1091            }
1092        }
1093    }
1094    if (GetLD_PC(CURRENT_LATCHES.MICROINSTRUCTION)) {
1095        switch (GetPCMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
1096        case 0: NEXT_LATCHES.PC = CURRENT_LATCHES.PC + 2; break;
1097        case 1: NEXT_LATCHES.PC = BUS; break;
1098        case 2: NEXT_LATCHES.PC = adder; break;
1099        case 3: NEXT_LATCHES.PC = CURRENT_LATCHES.PC - 2; break;
1100        }
1101    }
1102    if (GetLD_REG(CURRENT_LATCHES.MICROINSTRUCTION)) {
1103        int dr = CURRENT_LATCHES.IR >> 9 & 7;
1104        if (GetDRMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
1105            dr = 7;
1106        }
1107        if (GetSPMUX(CURRENT_LATCHES.MICROINSTRUCTION)) {
1108            dr = 6;
1109        }
1110        NEXT_LATCHES.REGS[dr] = BUS;
1111    }
1112    if (GetSWAP(CURRENT_LATCHES.MICROINSTRUCTION) && CURRENT_LATCHES.MODE) {
1113        NEXT_LATCHES.REGS[6] = CURRENT_LATCHES.SSP;
1114        NEXT_LATCHES.SSP = CURRENT_LATCHES.REGS[6];
1115    }
1116    int ldvec = GetLD_VEC(CURRENT_LATCHES.MICROINSTRUCTION);
1117    if (ldvec == 7) {
1118        NEXT_LATCHES.INT = 0;
1119        NEXT_LATCHES.VEC = 0;
1120    }
1121    if (ldvec) {
1122        if (ldvec == 1) {
1123            NEXT_LATCHES.VEC = 4;
1124        } else if (ldvec == 2) {
1125            NEXT_LATCHES.VEC = 3;
1126        } else if (ldvec == 3) {
1127            NEXT_LATCHES.VEC = 5;
1128        } else if (ldvec == 4) {
1129            NEXT_LATCHES.VEC = 2;
1130        }
1131    }
1132    int loc = GetJumpLoc(CURRENT_LATCHES.MICROINSTRUCTION);
1133    if (loc) {
1134        NEXT_LATCHES.JUMPLOC = loc;
1135    }
1136    if (GetLD_VA(CURRENT_LATCHES.MICROINSTRUCTION)) {
1137        NEXT_LATCHES.VA = CURRENT_LATCHES.MAR;
1138    }
1139}