+/EE460N
computer architecture
git clone https://git.julia.blue/+/EE460N
2dace00
master
1/* 2Name 1: Joseph Ryan 3UTEID 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 { 57IRD , 58COND1 , 59COND0 , 60J5 , 61J4 , 62J3 , 63J2 , 64J1 , 65J0 , 66LD_MAR , 67LD_MDR , 68LD_IR , 69LD_BEN , 70LD_REG , 71LD_CC , 72LD_PC , 73GATE_PC , 74GATE_MDR , 75GATE_ALU , 76GATE_MARMUX , 77GATE_SHF , 78PCMUX1 , 79PCMUX0 , 80DRMUX , 81SR1MUX , 82ADDR1MUX , 83ADDR2MUX1 , 84ADDR2MUX0 , 85MARMUX , 86ALUK1 , 87ALUK0 , 88MIO_EN , 89R_W , 90DATA_SIZE , 91LSHF1 , 92CONTROL_STORE_BITS 93}CS_BITS ; 94 95/***************************************************************/ 96/* Functions to get at the control bits. */ 97/***************************************************************/ 98int GetIRD (int * x ) { 99return (x [IRD ]); 100} 101int GetCOND (int * x ) { 102return ((x [COND1 ] <<1 )+ x [COND0 ]); 103} 104int GetJ (int * x ) { 105return ((x [J5 ] <<5 )+ (x [J4 ] <<4 )+ (x [J3 ] <<3 )+ (x [J2 ] <<2 )+ 106 (x [J1 ] <<1 )+ x [J0 ]); 107} 108int GetLD_MAR (int * x ) { 109return (x [LD_MAR ]); 110} 111int GetLD_MDR (int * x ) { 112return (x [LD_MDR ]); 113} 114int GetLD_IR (int * x ) { 115return (x [LD_IR ]); 116} 117int GetLD_BEN (int * x ) { 118return (x [LD_BEN ]); 119} 120int GetLD_REG (int * x ) { 121return (x [LD_REG ]); 122} 123int GetLD_CC (int * x ) { 124return (x [LD_CC ]); 125} 126int GetLD_PC (int * x ) { 127return (x [LD_PC ]); 128} 129int GetGATE_PC (int * x ) { 130return (x [GATE_PC ]); 131} 132int GetGATE_MDR (int * x ) { 133return (x [GATE_MDR ]); 134} 135int GetGATE_ALU (int * x ) { 136return (x [GATE_ALU ]); 137} 138int GetGATE_MARMUX (int * x ) { 139return (x [GATE_MARMUX ]); 140} 141int GetGATE_SHF (int * x ) { 142return (x [GATE_SHF ]); 143} 144int GetPCMUX (int * x ) { 145return ((x [PCMUX1 ] <<1 )+ x [PCMUX0 ]); 146} 147int GetDRMUX (int * x ) { 148return (x [DRMUX ]); 149} 150int GetSR1MUX (int * x ) { 151return (x [SR1MUX ]); 152} 153int GetADDR1MUX (int * x ) { 154return (x [ADDR1MUX ]); 155} 156int GetADDR2MUX (int * x ) { 157return ((x [ADDR2MUX1 ] <<1 )+ x [ADDR2MUX0 ]); 158} 159int GetMARMUX (int * x ) { 160return (x [MARMUX ]); 161} 162int GetALUK (int * x ) { 163return ((x [ALUK1 ] <<1 )+ x [ALUK0 ]); 164} 165int GetMIO_EN (int * x ) { 166return (x [MIO_EN ]); 167} 168int GetR_W (int * x ) { 169return (x [R_W ]); 170} 171int GetDATA_SIZE (int * x ) { 172return (x [DATA_SIZE ]); 173} 174int GetLSHF1 (int * x ) { 175return (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 187MEMORY[A][1] stores the most significant byte of word at word address A 188There are two write enable signals, one for each byte. WE0 is used for 189the least significant byte of a word. WE1 is used for the most significant 190byte 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 { 209int PC ,/* program counter */ 210MDR ,/* memory data register */ 211MAR ,/* memory address register */ 212IR ,/* instruction register */ 213N ,/* n condition bit */ 214Z ,/* z condition bit */ 215P ,/* p condition bit */ 216BEN ;/* ben register */ 217 218int READY ;/* ready bit */ 219/* The ready bit is also latched as you dont want the memory system to 220assert it at a bad point in the cycle*/ 221 222int REGS [LC_3b_REGS ];/* register file. */ 223 224int MICROINSTRUCTION [CONTROL_STORE_BITS ];/* The microintruction */ 225 226int 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 () { 246printf ("----------------LC-3bSIM Help-------------------------\n" ); 247printf ("go - run program to completion \n" ); 248printf ("run n - execute program for n cycles \n" ); 249printf ("mdump low high - dump memory from low to high \n" ); 250printf ("rdump - dump the register & bus values \n" ); 251printf ("? - display this help menu \n" ); 252printf ("quit - exit the program \n\n" ); 253} 254 255/***************************************************************/ 256/* */ 257/* Procedure : cycle */ 258/* */ 259/* Purpose : Execute a cycle */ 260/* */ 261/***************************************************************/ 262void cycle () { 263eval_micro_sequencer (); 264cycle_memory (); 265eval_bus_drivers (); 266drive_bus (); 267latch_datapath_values (); 268 269CURRENT_LATCHES = NEXT_LATCHES ; 270 271CYCLE_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 ) { 282int i ; 283 284if (RUN_BIT == FALSE) { 285printf ("Can't simulate, Simulator is halted\n\n" ); 286return ; 287 } 288 289printf ("Simulating for %d cycles...\n\n" ,num_cycles ); 290for (i = 0 ;i < num_cycles ;i ++ ) { 291if (CURRENT_LATCHES .PC == 0x0000 ) { 292RUN_BIT = FALSE; 293printf ("Simulator halted\n\n" ); 294break ; 295 } 296cycle (); 297 } 298} 299 300/***************************************************************/ 301/* */ 302/* Procedure : go */ 303/* */ 304/* Purpose : Simulate the LC-3b until HALTed. */ 305/* */ 306/***************************************************************/ 307void go () { 308if (RUN_BIT == FALSE) { 309printf ("Can't simulate, Simulator is halted\n\n" ); 310return ; 311 } 312 313printf ("Simulating...\n\n" ); 314while (CURRENT_LATCHES .PC != 0x0000 )cycle (); 315RUN_BIT = FALSE; 316printf ("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 ) { 328int address ;/* this is a byte address */ 329 330printf ("\nMemory content [0x%.4x..0x%.4x] :\n" ,start ,stop ); 331printf ("-------------------------------------\n" ); 332for (address = (start >>1 );address <= (stop >>1 );address ++ ) 333printf (" 0x%.4x (%d) : 0x%.2x%.2x\n" ,address <<1 ,address <<1 , 334MEMORY [address ][1 ],MEMORY [address ][0 ]); 335printf ("\n" ); 336 337/* dump the memory contents into the dumpsim file */ 338fprintf (dumpsim_file ,"\nMemory content [0x%.4x..0x%.4x] :\n" ,start ,stop ); 339fprintf (dumpsim_file ,"-------------------------------------\n" ); 340for (address = (start >>1 );address <= (stop >>1 );address ++ ) 341fprintf (dumpsim_file ," 0x%.4x (%d) : 0x%.2x%.2x\n" ,address <<1 , 342address <<1 ,MEMORY [address ][1 ],MEMORY [address ][0 ]); 343fprintf (dumpsim_file ,"\n" ); 344fflush (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 ) { 356int k ; 357 358printf ("\nCurrent register/bus values :\n" ); 359printf ("-------------------------------------\n" ); 360printf ("Cycle Count : %d\n" ,CYCLE_COUNT ); 361printf ("PC : 0x%.4x\n" ,CURRENT_LATCHES .PC ); 362printf ("IR : 0x%.4x\n" ,CURRENT_LATCHES .IR ); 363printf ("STATE_NUMBER : 0x%.4x\n\n" ,CURRENT_LATCHES .STATE_NUMBER ); 364printf ("BUS : 0x%.4x\n" ,BUS ); 365printf ("MDR : 0x%.4x\n" ,CURRENT_LATCHES .MDR ); 366printf ("MAR : 0x%.4x\n" ,CURRENT_LATCHES .MAR ); 367printf ("CCs: N = %d Z = %d P = %d\n" ,CURRENT_LATCHES .N , 368CURRENT_LATCHES .Z ,CURRENT_LATCHES .P ); 369printf ("Registers:\n" ); 370for (k = 0 ;k < LC_3b_REGS ;k ++ ) 371printf ("%d: 0x%.4x\n" ,k ,CURRENT_LATCHES .REGS [k ]); 372printf ("\n" ); 373 374/* dump the state information into the dumpsim file */ 375fprintf (dumpsim_file ,"\nCurrent register/bus values :\n" ); 376fprintf (dumpsim_file ,"-------------------------------------\n" ); 377fprintf (dumpsim_file ,"Cycle Count : %d\n" ,CYCLE_COUNT ); 378fprintf (dumpsim_file ,"PC : 0x%.4x\n" ,CURRENT_LATCHES .PC ); 379fprintf (dumpsim_file ,"IR : 0x%.4x\n" ,CURRENT_LATCHES .IR ); 380fprintf (dumpsim_file ,"STATE_NUMBER : 0x%.4x\n\n" , 381CURRENT_LATCHES .STATE_NUMBER ); 382fprintf (dumpsim_file ,"BUS : 0x%.4x\n" ,BUS ); 383fprintf (dumpsim_file ,"MDR : 0x%.4x\n" ,CURRENT_LATCHES .MDR ); 384fprintf (dumpsim_file ,"MAR : 0x%.4x\n" ,CURRENT_LATCHES .MAR ); 385fprintf (dumpsim_file ,"CCs: N = %d Z = %d P = %d\n" ,CURRENT_LATCHES .N , 386CURRENT_LATCHES .Z ,CURRENT_LATCHES .P ); 387fprintf (dumpsim_file ,"Registers:\n" ); 388for (k = 0 ;k < LC_3b_REGS ;k ++ ) 389fprintf (dumpsim_file ,"%d: 0x%.4x\n" ,k ,CURRENT_LATCHES .REGS [k ]); 390fprintf (dumpsim_file ,"\n" ); 391fflush (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 ) { 402char buffer [20 ]; 403int start ,stop ,cycles ; 404 405printf ("LC-3b-SIM> " ); 406 407scanf ("%s" ,buffer ); 408printf ("\n" ); 409 410switch (buffer [0 ]) { 411case 'G' : 412case 'g' :go ();break ; 413 414case 'M' : 415case 'm' : 416scanf ("%i %i" ,& start ,& stop ); 417mdump (dumpsim_file ,start ,stop ); 418break ; 419 420case '?' :help ();break ; 421case 'Q' : 422case 'q' :printf ("Bye.\n" );exit (0 ); 423 424case 'R' : 425case 'r' : 426if (buffer [1 ]== 'd' || buffer [1 ]== 'D' ) 427rdump (dumpsim_file ); 428else { 429scanf ("%d" ,& cycles ); 430run (cycles ); 431 } 432break ; 433 434default :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 ) { 446FILE * ucode ; 447int i ,j ,index ; 448char line [200 ]; 449 450printf ("Loading Control Store from file: %s\n" ,ucode_filename ); 451 452/* Open the micro-code file. */ 453if ((ucode = fopen (ucode_filename ,"r" ))== NULL ) { 454printf ("Error: Can't open micro-code file %s\n" ,ucode_filename ); 455exit (-1 ); 456 } 457 458/* Read a line for each row in the control store. */ 459for (i = 0 ;i < CONTROL_STORE_ROWS ;i ++ ) { 460if (fscanf (ucode ,"%[^\n]\n" ,line )== EOF ) { 461printf ("Error: Too few lines (%d) in micro-code file: %s\n" ,i , 462ucode_filename ); 463exit (-1 ); 464 } 465 466/* Put in bits one at a time. */ 467index = 0 ; 468 469for (j = 0 ;j < CONTROL_STORE_BITS ;j ++ ) { 470/* Needs to find enough bits in line. */ 471if (line [index ]== '\0' ) { 472printf ("Error: Too few control bits in micro-code file: " 473"%s\nLine: %d\n" , 474ucode_filename ,i ); 475exit (-1 ); 476 } 477if (line [index ]!= '0' && line [index ]!= '1' ) { 478printf ("Error: Unknown value in micro-code file: %s\nLine: %d, " 479"Bit: %d\n" , 480ucode_filename ,i ,j ); 481exit (-1 ); 482 } 483 484/* Set the bit in the Control Store. */ 485CONTROL_STORE [i ][j ]= (line [index ]== '0' ) ?0 :1 ; 486index ++ ; 487 } 488 489/* Warn about extra bits in line. */ 490if (line [index ]!= '\0' ) 491printf ("Warning: Extra bit(s) in control store file %s. Line: %d\n" , 492ucode_filename ,i ); 493 } 494printf ("\n" ); 495} 496 497/************************************************************/ 498/* */ 499/* Procedure : init_memory */ 500/* */ 501/* Purpose : Zero out the memory array */ 502/* */ 503/************************************************************/ 504void init_memory () { 505int i ; 506 507for (i = 0 ;i < WORDS_IN_MEM ;i ++ ) { 508MEMORY [i ][0 ]= 0 ; 509MEMORY [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 ) { 521FILE * prog ; 522int ii ,word ,program_base ; 523 524/* Open program file. */ 525prog = fopen (program_filename ,"r" ); 526if (prog == NULL ) { 527printf ("Error: Can't open program file %s\n" ,program_filename ); 528exit (-1 ); 529 } 530 531/* Read in the program. */ 532if (fscanf (prog ,"%x\n" ,& word )!= EOF ) 533program_base = word >>1 ; 534else { 535printf ("Error: Program file is empty\n" ); 536exit (-1 ); 537 } 538 539ii = 0 ; 540while (fscanf (prog ,"%x\n" ,& word )!= EOF ) { 541/* Make sure it fits. */ 542if (program_base + ii >=WORDS_IN_MEM ) { 543printf ("Error: Program file %s is too long to fit in memory. %x\n" , 544program_filename ,ii ); 545exit (-1 ); 546 } 547 548/* Write the word to memory array. */ 549MEMORY [program_base + ii ][0 ]= word & 0x00FF ; 550MEMORY [program_base + ii ][1 ]= (word >>8 )& 0x00FF ; 551ii ++ ; 552 } 553 554if (CURRENT_LATCHES .PC == 0 ) 555CURRENT_LATCHES .PC = (program_base <<1 ); 556 557printf ("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 , 569int num_prog_files ) { 570int i ; 571init_control_store (ucode_filename ); 572 573init_memory (); 574for (i = 0 ;i < num_prog_files ;i ++ ) { 575load_program (program_filename ); 576while (* program_filename ++ != '\0' ) 577 ; 578 } 579CURRENT_LATCHES .Z = 1 ; 580CURRENT_LATCHES .STATE_NUMBER = INITIAL_STATE_NUMBER ; 581memcpy (CURRENT_LATCHES .MICROINSTRUCTION , 582CONTROL_STORE [INITIAL_STATE_NUMBER ], 583sizeof (int )* CONTROL_STORE_BITS ); 584 585NEXT_LATCHES = CURRENT_LATCHES ; 586 587RUN_BIT = TRUE; 588} 589 590/***************************************************************/ 591/* */ 592/* Procedure : main */ 593/* */ 594/***************************************************************/ 595int main (int argc ,char * argv []) { 596FILE * dumpsim_file ; 597 598/* Error Checking */ 599if (argc < 3 ) { 600printf ("Error: usage: %s <micro_code_file> <program_file_1> " 601"<program_file_2> ...\n" , 602argv [0 ]); 603exit (1 ); 604 } 605 606printf ("LC-3b Simulator\n\n" ); 607 608initialize (argv [1 ],argv [2 ],argc - 2 ); 609 610if ((dumpsim_file = fopen ("dumpsim" ,"w" ))== NULL ) { 611printf ("Error: Can't open dumpsim file\n" ); 612exit (-1 ); 613 } 614 615while (1 )get_command (dumpsim_file ); 616} 617 618/***************************************************************/ 619/* Do not modify the above code. 620You are allowed to use the following global variables in your 621code. These are defined above. 622 623CONTROL_STORE 624MEMORY 625BUS 626 627CURRENT_LATCHES 628NEXT_LATCHES 629 630You may define your own local/global variables and functions. 631You may use the functions to get at the control bits defined 632above. 633 634Begin 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*/ 644NEXT_LATCHES .STATE_NUMBER = GetJ (CURRENT_LATCHES .MICROINSTRUCTION ); 645int cond = GetCOND (CURRENT_LATCHES .MICROINSTRUCTION ); 646if (cond == 1 ) { 647NEXT_LATCHES .STATE_NUMBER |=CURRENT_LATCHES .READY <<1 ; 648 }else if (cond == 2 ) { 649NEXT_LATCHES .STATE_NUMBER |=CURRENT_LATCHES .BEN <<2 ; 650 }else if (cond == 3 ) { 651NEXT_LATCHES .STATE_NUMBER |= !!(CURRENT_LATCHES .IR & (1 <<11 )); 652 } 653if (GetIRD (CURRENT_LATCHES .MICROINSTRUCTION )) { 654NEXT_LATCHES .STATE_NUMBER = CURRENT_LATCHES .IR >>12 ; 655 } 656memcpy (NEXT_LATCHES .MICROINSTRUCTION , 657CONTROL_STORE [NEXT_LATCHES .STATE_NUMBER ], 658CONTROL_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*/ 669static int current_mem_cycle = 0 ; 670if (!GetMIO_EN (CURRENT_LATCHES .MICROINSTRUCTION )) { 671return ; 672 } 673++ current_mem_cycle ; 674if (current_mem_cycle == MEM_CYCLES - 1 ) { 675NEXT_LATCHES .READY = true; 676 }else if (CURRENT_LATCHES .READY ) { 677int word = GetDATA_SIZE (CURRENT_LATCHES .MICROINSTRUCTION ); 678int mar0 = CURRENT_LATCHES .MAR & 1 ; 679int mar = CURRENT_LATCHES .MAR >>1 ; 680int mdr_low = CURRENT_LATCHES .MDR & 0xff ; 681int mdr_high = CURRENT_LATCHES .MDR >>8 & 0xff ; 682if (GetR_W (CURRENT_LATCHES .MICROINSTRUCTION )) { 683if (word ) { 684if (mar0 ) { 685MEMORY [mar + 1 ][0 ]= mdr_low ; 686MEMORY [mar + 1 ][1 ]= mdr_high ; 687 }else { 688MEMORY [mar ][0 ]= mdr_low ; 689MEMORY [mar ][1 ]= mdr_high ; 690 } 691 }else { 692if (mar0 ) { 693MEMORY [mar ][1 ]= mdr_high ; 694 }else { 695MEMORY [mar ][0 ]= mdr_low ; 696 } 697 } 698 }else { 699NEXT_LATCHES .MDR = MEMORY [mar ][1 ] <<8 |MEMORY [mar ][0 ]; 700 } 701current_mem_cycle = 0 ; 702NEXT_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 721int sr1 = CURRENT_LATCHES .REGS [CURRENT_LATCHES .IR >>9 & 7 ]; 722if (GetSR1MUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 723sr1 = CURRENT_LATCHES .REGS [CURRENT_LATCHES .IR >>6 & 7 ]; 724 } 725int sr2 = CURRENT_LATCHES .REGS [CURRENT_LATCHES .IR & 7 ]; 726// ALU 727if (CURRENT_LATCHES .IR & 0x20 ) { 728sr2 = SEXT_IR (5 ); 729 } 730switch (GetALUK (CURRENT_LATCHES .MICROINSTRUCTION )) { 731case 0 :galu = sr1 + sr2 ;break ; 732case 1 :galu = sr1 & sr2 ;break ; 733case 2 :galu = sr1 ^sr2 ;break ; 734case 3 :galu = sr1 ;break ; 735 } 736// SHF 737int ammount = CURRENT_LATCHES .IR & 0xf ; 738if (CURRENT_LATCHES .IR & 0x10 ) { 739if (CURRENT_LATCHES .IR & 0x20 ) { 740int temp = sr1 ; 741if (sr1 & 0x8000 ) { 742temp |=0xffff0000 ; 743 } 744gshf = temp >>ammount ; 745 }else { 746gshf = sr1 >>ammount & (0xffff >>ammount ); 747 } 748 }else { 749gshf = sr1 <<ammount ; 750 } 751// PC 752gpc = CURRENT_LATCHES .PC ; 753// MARMUX 754int addr2 ; 755switch (GetADDR2MUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 756case 0 :addr2 = 0 ;break ; 757case 1 :addr2 = SEXT_IR (6 );break ; 758case 2 :addr2 = SEXT_IR (9 );break ; 759case 3 :addr2 = SEXT_IR (11 );break ; 760 } 761if (GetLSHF1 (CURRENT_LATCHES .MICROINSTRUCTION )) { 762addr2 <<=1 ; 763 } 764int addr1 = CURRENT_LATCHES .PC ; 765if (GetADDR1MUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 766addr1 = sr1 ; 767 } 768adder = addr2 + addr1 ; 769gmarmux = (CURRENT_LATCHES .IR & 0xff ) <<1 ; 770if (GetMARMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 771gmarmux = adder ; 772 } 773// MDR 774int byte = CURRENT_LATCHES .MDR ; 775if (CURRENT_LATCHES .MAR & 1 ) { 776byte >>=8 ; 777 } 778if (byte & 0x80 ) { 779byte |=0xff00 ; 780 }else { 781byte &=0xff ; 782 } 783if (GetDATA_SIZE (CURRENT_LATCHES .MICROINSTRUCTION )) { 784gmdr = CURRENT_LATCHES .MDR ; 785 }else { 786gmdr = 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*/ 795BUS = 0 ; 796if (GetGATE_SHF (CURRENT_LATCHES .MICROINSTRUCTION )) { 797BUS = gshf ; 798 } 799if (GetGATE_ALU (CURRENT_LATCHES .MICROINSTRUCTION )) { 800BUS = galu ; 801 } 802if (GetGATE_MARMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 803BUS = gmarmux ; 804 } 805if (GetGATE_MDR (CURRENT_LATCHES .MICROINSTRUCTION )) { 806BUS = gmdr ; 807 } 808if (GetGATE_PC (CURRENT_LATCHES .MICROINSTRUCTION )) { 809BUS = gpc ; 810 } 811BUS = 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*/ 821if (GetLD_BEN (CURRENT_LATCHES .MICROINSTRUCTION )) { 822NEXT_LATCHES .BEN = 0 ; 823if ((CURRENT_LATCHES .IR & 0x0800 && CURRENT_LATCHES .N )|| 824 (CURRENT_LATCHES .IR & 0x0400 && CURRENT_LATCHES .Z )|| 825 (CURRENT_LATCHES .IR & 0x0200 && CURRENT_LATCHES .P )) { 826NEXT_LATCHES .BEN = 1 ; 827 } 828 } 829if (GetLD_CC (CURRENT_LATCHES .MICROINSTRUCTION )) { 830NEXT_LATCHES .N = NEXT_LATCHES .Z = NEXT_LATCHES .P = false; 831if (BUS & 0x8000 ) { 832NEXT_LATCHES .N = true; 833 }else if (BUS ) { 834NEXT_LATCHES .P = true; 835 }else { 836NEXT_LATCHES .Z = true; 837 } 838 } 839if (GetLD_IR (CURRENT_LATCHES .MICROINSTRUCTION )) { 840NEXT_LATCHES .IR = BUS ; 841 } 842if (GetLD_MAR (CURRENT_LATCHES .MICROINSTRUCTION )) { 843NEXT_LATCHES .MAR = BUS ; 844 } 845if (GetLD_MDR (CURRENT_LATCHES .MICROINSTRUCTION )) { 846if (GetMIO_EN (CURRENT_LATCHES .MICROINSTRUCTION )) { 847 }else { 848int byte = BUS ; 849if (CURRENT_LATCHES .MAR & 1 ) { 850byte <<=8 ; 851 } 852if (GetDATA_SIZE (CURRENT_LATCHES .MICROINSTRUCTION )) { 853NEXT_LATCHES .MDR = BUS ; 854 }else { 855NEXT_LATCHES .MDR = byte ; 856 } 857 } 858 } 859if (GetLD_PC (CURRENT_LATCHES .MICROINSTRUCTION )) { 860switch (GetPCMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 861case 0 :NEXT_LATCHES .PC = CURRENT_LATCHES .PC + 2 ;break ; 862case 1 :NEXT_LATCHES .PC = BUS ;break ; 863case 2 :NEXT_LATCHES .PC = adder ;break ; 864 } 865 } 866if (GetLD_REG (CURRENT_LATCHES .MICROINSTRUCTION )) { 867int dr = CURRENT_LATCHES .IR >>9 & 7 ; 868if (GetDRMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 869dr = 7 ; 870 } 871NEXT_LATCHES .REGS [dr ]= BUS ; 872 } 873}