+/EE460N

computer architecture

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

Joseph RyanLab 4 first pass928c03d

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