+/EE460N

computer architecture

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

Joseph RyanFinish Lab 32dace00

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