+/EE460N
computer architecture
git clone https://git.julia.blue/+/EE460N
928c03d
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 , 92/* MODIFY: you have to add all your new control signals */ 93GATE_PSR , 94LD_PSR , 95MODEMUX , 96SWAP , 97Check_INT , 98SPMUX , 99SP_ADD_SUB , 100Check_P , 101Check_A , 102Clear_VEC , 103LD_VEC1 , 104LD_VEC2 , 105GATE_VEC , 106CONTROL_STORE_BITS 107}CS_BITS ; 108 109/***************************************************************/ 110/* Functions to get at the control bits. */ 111/***************************************************************/ 112int GetIRD (int * x ) { 113return (x [IRD ]); 114} 115int GetCOND (int * x ) { 116return ((x [COND1 ] <<1 )+ x [COND0 ]); 117} 118int GetJ (int * x ) { 119return ((x [J5 ] <<5 )+ (x [J4 ] <<4 )+ (x [J3 ] <<3 )+ (x [J2 ] <<2 )+ 120 (x [J1 ] <<1 )+ x [J0 ]); 121} 122int GetLD_MAR (int * x ) { 123return (x [LD_MAR ]); 124} 125int GetLD_MDR (int * x ) { 126return (x [LD_MDR ]); 127} 128int GetLD_IR (int * x ) { 129return (x [LD_IR ]); 130} 131int GetLD_BEN (int * x ) { 132return (x [LD_BEN ]); 133} 134int GetLD_REG (int * x ) { 135return (x [LD_REG ]); 136} 137int GetLD_CC (int * x ) { 138return (x [LD_CC ]); 139} 140int GetLD_PC (int * x ) { 141return (x [LD_PC ]); 142} 143int GetGATE_PC (int * x ) { 144return (x [GATE_PC ]); 145} 146int GetGATE_MDR (int * x ) { 147return (x [GATE_MDR ]); 148} 149int GetGATE_ALU (int * x ) { 150return (x [GATE_ALU ]); 151} 152int GetGATE_MARMUX (int * x ) { 153return (x [GATE_MARMUX ]); 154} 155int GetGATE_SHF (int * x ) { 156return (x [GATE_SHF ]); 157} 158int GetPCMUX (int * x ) { 159return ((x [PCMUX1 ] <<1 )+ x [PCMUX0 ]); 160} 161int GetDRMUX (int * x ) { 162return (x [DRMUX ]); 163} 164int GetSR1MUX (int * x ) { 165return (x [SR1MUX ]); 166} 167int GetADDR1MUX (int * x ) { 168return (x [ADDR1MUX ]); 169} 170int GetADDR2MUX (int * x ) { 171return ((x [ADDR2MUX1 ] <<1 )+ x [ADDR2MUX0 ]); 172} 173int GetMARMUX (int * x ) { 174return (x [MARMUX ]); 175} 176int GetALUK (int * x ) { 177return ((x [ALUK1 ] <<1 )+ x [ALUK0 ]); 178} 179int GetMIO_EN (int * x ) { 180return (x [MIO_EN ]); 181} 182int GetR_W (int * x ) { 183return (x [R_W ]); 184} 185int GetDATA_SIZE (int * x ) { 186return (x [DATA_SIZE ]); 187} 188int GetLSHF1 (int * x ) { 189return (x [LSHF1 ]); 190} 191/* MODIFY: you can add more Get functions for your new control signals */ 192int GetGATE_PSR (int * x ) { 193return x [GATE_PSR ]; 194} 195int GetLD_PSR (int * x ) { 196return x [LD_PSR ]; 197} 198int GetMODEMUX (int * x ) { 199return x [MODEMUX ]; 200} 201int GetSWAP (int * x ) { 202return x [SWAP ]; 203} 204int GetCheck_INT (int * x ) { 205return x [Check_INT ]; 206} 207int GetSPMUX (int * x ) { 208return x [SPMUX ]; 209} 210int GetSP_ADD_SUB (int * x ) { 211return x [SP_ADD_SUB ]; 212} 213int GetCheck_P (int * x ) { 214return x [Check_P ]; 215} 216int GetCheck_A (int * x ) { 217return x [Check_A ]; 218} 219int GetClear_VEC (int * x ) { 220return x [Clear_VEC ]; 221} 222int GetLD_VEC1 (int * x ) { 223return x [LD_VEC1 ]; 224} 225int GetLD_VEC2 (int * x ) { 226return x [LD_VEC2 ]; 227} 228int GetGATE_VEC (int * x ) { 229return 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 241MEMORY[A][1] stores the most significant byte of word at word address A 242There are two write enable signals, one for each byte. WE0 is used for 243the least significant byte of a word. WE1 is used for the most significant 244byte 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 { 263int PC ,/* program counter */ 264MDR ,/* memory data register */ 265MAR ,/* memory address register */ 266IR ,/* instruction register */ 267N ,/* n condition bit */ 268Z ,/* z condition bit */ 269P ,/* p condition bit */ 270BEN ;/* ben register */ 271 272int READY ;/* ready bit */ 273/* The ready bit is also latched as you dont want the memory system to 274assert it at a bad point in the cycle*/ 275 276int REGS [LC_3b_REGS ];/* register file. */ 277 278int MICROINSTRUCTION [CONTROL_STORE_BITS ];/* The microintruction */ 279 280int STATE_NUMBER ;/* Current State Number - Provided for debugging */ 281 282/* For lab 4 */ 283int VEC ; 284int SSP ; 285int MODE ; 286int 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 () { 306printf ("----------------LC-3bSIM Help-------------------------\n" ); 307printf ("go - run program to completion \n" ); 308printf ("run n - execute program for n cycles \n" ); 309printf ("mdump low high - dump memory from low to high \n" ); 310printf ("rdump - dump the register & bus values \n" ); 311printf ("? - display this help menu \n" ); 312printf ("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 324eval_micro_sequencer (); 325cycle_memory (); 326eval_bus_drivers (); 327drive_bus (); 328latch_datapath_values (); 329 330if (CYCLE_COUNT == 299 ) { 331NEXT_LATCHES .INT = 1 ; 332NEXT_LATCHES .VEC = 1 ; 333 } 334CURRENT_LATCHES = NEXT_LATCHES ; 335 336CYCLE_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 ) { 347int i ; 348 349if (RUN_BIT == FALSE) { 350printf ("Can't simulate, Simulator is halted\n\n" ); 351return ; 352 } 353 354printf ("Simulating for %d cycles...\n\n" ,num_cycles ); 355for (i = 0 ;i < num_cycles ;i ++ ) { 356if (CURRENT_LATCHES .PC == 0x0000 ) { 357RUN_BIT = FALSE; 358printf ("Simulator halted\n\n" ); 359break ; 360 } 361cycle (); 362 } 363} 364 365/***************************************************************/ 366/* */ 367/* Procedure : go */ 368/* */ 369/* Purpose : Simulate the LC-3b until HALTed. */ 370/* */ 371/***************************************************************/ 372void go () { 373if (RUN_BIT == FALSE) { 374printf ("Can't simulate, Simulator is halted\n\n" ); 375return ; 376 } 377 378printf ("Simulating...\n\n" ); 379while (CURRENT_LATCHES .PC != 0x0000 )cycle (); 380RUN_BIT = FALSE; 381printf ("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 ) { 393int address ;/* this is a byte address */ 394 395printf ("\nMemory content [0x%0.4x..0x%0.4x] :\n" ,start ,stop ); 396printf ("-------------------------------------\n" ); 397for (address = (start >>1 );address <= (stop >>1 );address ++ ) 398printf (" 0x%0.4x (%d) : 0x%0.2x%0.2x\n" ,address <<1 ,address <<1 , 399MEMORY [address ][1 ],MEMORY [address ][0 ]); 400printf ("\n" ); 401 402/* dump the memory contents into the dumpsim file */ 403fprintf (dumpsim_file ,"\nMemory content [0x%0.4x..0x%0.4x] :\n" ,start , 404stop ); 405fprintf (dumpsim_file ,"-------------------------------------\n" ); 406for (address = (start >>1 );address <= (stop >>1 );address ++ ) 407fprintf (dumpsim_file ," 0x%0.4x (%d) : 0x%0.2x%0.2x\n" ,address <<1 , 408address <<1 ,MEMORY [address ][1 ],MEMORY [address ][0 ]); 409fprintf (dumpsim_file ,"\n" ); 410fflush (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 ) { 422int k ; 423 424printf ("\nCurrent register/bus values :\n" ); 425printf ("-------------------------------------\n" ); 426printf ("Cycle Count : %d\n" ,CYCLE_COUNT ); 427printf ("PC : 0x%0.4x\n" ,CURRENT_LATCHES .PC ); 428printf ("IR : 0x%0.4x\n" ,CURRENT_LATCHES .IR ); 429printf ("STATE_NUMBER : 0x%0.4x\n\n" ,CURRENT_LATCHES .STATE_NUMBER ); 430printf ("BUS : 0x%0.4x\n" ,BUS ); 431printf ("MDR : 0x%0.4x\n" ,CURRENT_LATCHES .MDR ); 432printf ("MAR : 0x%0.4x\n" ,CURRENT_LATCHES .MAR ); 433printf ("CCs: N = %d Z = %d P = %d\n" ,CURRENT_LATCHES .N , 434CURRENT_LATCHES .Z ,CURRENT_LATCHES .P ); 435printf ("Registers:\n" ); 436for (k = 0 ;k < LC_3b_REGS ;k ++ ) 437printf ("%d: 0x%0.4x\n" ,k ,CURRENT_LATCHES .REGS [k ]); 438printf ("\n" ); 439 440/* dump the state information into the dumpsim file */ 441fprintf (dumpsim_file ,"\nCurrent register/bus values :\n" ); 442fprintf (dumpsim_file ,"-------------------------------------\n" ); 443fprintf (dumpsim_file ,"Cycle Count : %d\n" ,CYCLE_COUNT ); 444fprintf (dumpsim_file ,"PC : 0x%0.4x\n" ,CURRENT_LATCHES .PC ); 445fprintf (dumpsim_file ,"IR : 0x%0.4x\n" ,CURRENT_LATCHES .IR ); 446fprintf (dumpsim_file ,"STATE_NUMBER : 0x%0.4x\n\n" , 447CURRENT_LATCHES .STATE_NUMBER ); 448fprintf (dumpsim_file ,"BUS : 0x%0.4x\n" ,BUS ); 449fprintf (dumpsim_file ,"MDR : 0x%0.4x\n" ,CURRENT_LATCHES .MDR ); 450fprintf (dumpsim_file ,"MAR : 0x%0.4x\n" ,CURRENT_LATCHES .MAR ); 451fprintf (dumpsim_file ,"CCs: N = %d Z = %d P = %d\n" ,CURRENT_LATCHES .N , 452CURRENT_LATCHES .Z ,CURRENT_LATCHES .P ); 453fprintf (dumpsim_file ,"Registers:\n" ); 454for (k = 0 ;k < LC_3b_REGS ;k ++ ) 455fprintf (dumpsim_file ,"%d: 0x%0.4x\n" ,k ,CURRENT_LATCHES .REGS [k ]); 456fprintf (dumpsim_file ,"\n" ); 457fflush (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 ) { 468char buffer [20 ]; 469int start ,stop ,cycles ; 470 471printf ("LC-3b-SIM> " ); 472 473scanf ("%s" ,buffer ); 474printf ("\n" ); 475 476switch (buffer [0 ]) { 477case 'G' : 478case 'g' :go ();break ; 479 480case 'M' : 481case 'm' : 482scanf ("%i %i" ,& start ,& stop ); 483mdump (dumpsim_file ,start ,stop ); 484break ; 485 486case '?' :help ();break ; 487case 'Q' : 488case 'q' :printf ("Bye.\n" );exit (0 ); 489 490case 'R' : 491case 'r' : 492if (buffer [1 ]== 'd' || buffer [1 ]== 'D' ) 493rdump (dumpsim_file ); 494else { 495scanf ("%d" ,& cycles ); 496run (cycles ); 497 } 498break ; 499 500default :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 ) { 512FILE * ucode ; 513int i ,j ,index ; 514char line [200 ]; 515 516printf ("Loading Control Store from file: %s\n" ,ucode_filename ); 517 518/* Open the micro-code file. */ 519if ((ucode = fopen (ucode_filename ,"r" ))== NULL ) { 520printf ("Error: Can't open micro-code file %s\n" ,ucode_filename ); 521exit (-1 ); 522 } 523 524/* Read a line for each row in the control store. */ 525for (i = 0 ;i < CONTROL_STORE_ROWS ;i ++ ) { 526if (fscanf (ucode ,"%[^\n]\n" ,line )== EOF ) { 527printf ("Error: Too few lines (%d) in micro-code file: %s\n" ,i , 528ucode_filename ); 529exit (-1 ); 530 } 531 532/* Put in bits one at a time. */ 533index = 0 ; 534 535for (j = 0 ;j < CONTROL_STORE_BITS ;j ++ ) { 536/* Needs to find enough bits in line. */ 537if (line [index ]== '\0' ) { 538printf ("Error: Too few control bits in micro-code file: " 539"%s\nLine: %d\n" , 540ucode_filename ,i ); 541exit (-1 ); 542 } 543if (line [index ]!= '0' && line [index ]!= '1' ) { 544printf ("Error: Unknown value in micro-code file: %s\nLine: %d, " 545"Bit: %d\n" , 546ucode_filename ,i ,j ); 547exit (-1 ); 548 } 549 550/* Set the bit in the Control Store. */ 551CONTROL_STORE [i ][j ]= (line [index ]== '0' ) ?0 :1 ; 552index ++ ; 553 } 554 555/* Warn about extra bits in line. */ 556if (line [index ]!= '\0' ) 557printf ("Warning: Extra bit(s) in control store file %s. Line: %d\n" , 558ucode_filename ,i ); 559 } 560printf ("\n" ); 561} 562 563/***************************************************************/ 564/* */ 565/* Procedure : init_memory */ 566/* */ 567/* Purpose : Zero out the memory array */ 568/* */ 569/***************************************************************/ 570void init_memory () { 571int i ; 572 573for (i = 0 ;i < WORDS_IN_MEM ;i ++ ) { 574MEMORY [i ][0 ]= 0 ; 575MEMORY [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 ) { 587FILE * prog ; 588int ii ,word ,program_base ; 589 590/* Open program file. */ 591prog = fopen (program_filename ,"r" ); 592if (prog == NULL ) { 593printf ("Error: Can't open program file %s\n" ,program_filename ); 594exit (-1 ); 595 } 596 597/* Read in the program. */ 598if (fscanf (prog ,"%x\n" ,& word )!= EOF ) 599program_base = word >>1 ; 600else { 601printf ("Error: Program file is empty\n" ); 602exit (-1 ); 603 } 604 605ii = 0 ; 606while (fscanf (prog ,"%x\n" ,& word )!= EOF ) { 607/* Make sure it fits. */ 608if (program_base + ii >=WORDS_IN_MEM ) { 609printf ("Error: Program file %s is too long to fit in memory. %x\n" , 610program_filename ,ii ); 611exit (-1 ); 612 } 613 614/* Write the word to memory array. */ 615MEMORY [program_base + ii ][0 ]= word & 0x00FF ; 616MEMORY [program_base + ii ][1 ]= (word >>8 )& 0x00FF ; 617ii ++ ; 618 } 619 620if (CURRENT_LATCHES .PC == 0 ) 621CURRENT_LATCHES .PC = (program_base <<1 ); 622 623printf ("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 , 635int num_prog_files ) { 636int i ; 637init_control_store (ucode_filename ); 638 639init_memory (); 640for (i = 0 ;i < num_prog_files ;i ++ ) { 641load_program (program_filename ); 642while (* program_filename ++ != '\0' ) 643 ; 644 } 645CURRENT_LATCHES .Z = 1 ; 646CURRENT_LATCHES .STATE_NUMBER = INITIAL_STATE_NUMBER ; 647memcpy (CURRENT_LATCHES .MICROINSTRUCTION , 648CONTROL_STORE [INITIAL_STATE_NUMBER ], 649sizeof (int )* CONTROL_STORE_BITS ); 650CURRENT_LATCHES .SSP = 0x3000 ;/* Initial value of system stack pointer */ 651CURRENT_LATCHES .MODE = 1 ; 652 653NEXT_LATCHES = CURRENT_LATCHES ; 654 655RUN_BIT = TRUE; 656} 657 658/***************************************************************/ 659/* */ 660/* Procedure : main */ 661/* */ 662/***************************************************************/ 663int main (int argc ,char * argv []) { 664FILE * dumpsim_file ; 665 666/* Error Checking */ 667if (argc < 3 ) { 668printf ("Error: usage: %s <micro_code_file> <program_file_1> " 669"<program_file_2> ...\n" , 670argv [0 ]); 671exit (1 ); 672 } 673 674printf ("LC-3b Simulator\n\n" ); 675 676initialize (argv [1 ],argv [2 ],argc - 2 ); 677 678if ((dumpsim_file = fopen ("dumpsim" ,"w" ))== NULL ) { 679printf ("Error: Can't open dumpsim file\n" ); 680exit (-1 ); 681 } 682 683while (1 )get_command (dumpsim_file ); 684} 685 686/***************************************************************/ 687/* Do not modify the above code, except for the places indicated 688with a "MODIFY:" comment. 689 690Do not modify the rdump and mdump functions. 691 692You are allowed to use the following global variables in your 693code. These are defined above. 694 695CONTROL_STORE 696MEMORY 697BUS 698 699CURRENT_LATCHES 700NEXT_LATCHES 701 702You may define your own local/global variables and functions. 703You may use the functions to get at the control bits defined 704above. 705 706Begin 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*/ 716NEXT_LATCHES .STATE_NUMBER = GetJ (CURRENT_LATCHES .MICROINSTRUCTION ); 717int cond = GetCOND (CURRENT_LATCHES .MICROINSTRUCTION ); 718if (cond == 1 ) { 719NEXT_LATCHES .STATE_NUMBER |=CURRENT_LATCHES .READY <<1 ; 720 }else if (cond == 2 ) { 721NEXT_LATCHES .STATE_NUMBER |=CURRENT_LATCHES .BEN <<2 ; 722 }else if (cond == 3 ) { 723NEXT_LATCHES .STATE_NUMBER |= !!(CURRENT_LATCHES .IR & (1 <<11 )); 724 } 725if (GetIRD (CURRENT_LATCHES .MICROINSTRUCTION )) { 726NEXT_LATCHES .STATE_NUMBER = CURRENT_LATCHES .IR >>12 ; 727 } 728if (GetCheck_INT (CURRENT_LATCHES .MICROINSTRUCTION )&& CURRENT_LATCHES .INT ) { 729NEXT_LATCHES .STATE_NUMBER |=0x10 ; 730 } 731memcpy (NEXT_LATCHES .MICROINSTRUCTION , 732CONTROL_STORE [NEXT_LATCHES .STATE_NUMBER ], 733CONTROL_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*/ 744static int current_mem_cycle = 0 ; 745if (!GetMIO_EN (CURRENT_LATCHES .MICROINSTRUCTION )) { 746return ; 747 } 748++ current_mem_cycle ; 749if (current_mem_cycle == MEM_CYCLES - 1 ) { 750NEXT_LATCHES .READY = true; 751 }else if (CURRENT_LATCHES .READY ) { 752int word = GetDATA_SIZE (CURRENT_LATCHES .MICROINSTRUCTION ); 753int mar0 = CURRENT_LATCHES .MAR & 1 ; 754int mar = CURRENT_LATCHES .MAR >>1 ; 755int mdr_low = CURRENT_LATCHES .MDR & 0xff ; 756int mdr_high = CURRENT_LATCHES .MDR >>8 & 0xff ; 757if (GetR_W (CURRENT_LATCHES .MICROINSTRUCTION )) { 758if (word ) { 759if (mar0 ) { 760MEMORY [mar + 1 ][0 ]= mdr_low ; 761MEMORY [mar + 1 ][1 ]= mdr_high ; 762 }else { 763MEMORY [mar ][0 ]= mdr_low ; 764MEMORY [mar ][1 ]= mdr_high ; 765 } 766 }else { 767if (mar0 ) { 768MEMORY [mar ][1 ]= mdr_high ; 769 }else { 770MEMORY [mar ][0 ]= mdr_low ; 771 } 772 } 773 }else { 774NEXT_LATCHES .MDR = MEMORY [mar ][1 ] <<8 |MEMORY [mar ][0 ]; 775 } 776current_mem_cycle = 0 ; 777NEXT_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 798int sr1 = CURRENT_LATCHES .REGS [CURRENT_LATCHES .IR >>9 & 7 ]; 799if (GetSR1MUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 800sr1 = CURRENT_LATCHES .REGS [CURRENT_LATCHES .IR >>6 & 7 ]; 801 } 802if (GetSPMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 803sr1 = CURRENT_LATCHES .REGS [6 ]; 804 } 805int sr2 = CURRENT_LATCHES .REGS [CURRENT_LATCHES .IR & 7 ]; 806// ALU 807if (CURRENT_LATCHES .IR & 0x20 ) { 808sr2 = SEXT_IR (5 ); 809 } 810if (GetSPMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 811sr2 = GetSP_ADD_SUB (CURRENT_LATCHES .MICROINSTRUCTION ) ?-2 :2 ; 812 } 813switch (GetALUK (CURRENT_LATCHES .MICROINSTRUCTION )) { 814case 0 :galu = sr1 + sr2 ;break ; 815case 1 :galu = sr1 & sr2 ;break ; 816case 2 :galu = sr1 ^sr2 ;break ; 817case 3 :galu = sr1 ;break ; 818 } 819// SHF 820int ammount = CURRENT_LATCHES .IR & 0xf ; 821if (CURRENT_LATCHES .IR & 0x10 ) { 822if (CURRENT_LATCHES .IR & 0x20 ) { 823int temp = sr1 ; 824if (sr1 & 0x8000 ) { 825temp |=0xffff0000 ; 826 } 827gshf = temp >>ammount ; 828 }else { 829gshf = sr1 >>ammount & (0xffff >>ammount ); 830 } 831 }else { 832gshf = sr1 <<ammount ; 833 } 834// PC 835gpc = CURRENT_LATCHES .PC ; 836// MARMUX 837int addr2 ; 838switch (GetADDR2MUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 839case 0 :addr2 = 0 ;break ; 840case 1 :addr2 = SEXT_IR (6 );break ; 841case 2 :addr2 = SEXT_IR (9 );break ; 842case 3 :addr2 = SEXT_IR (11 );break ; 843 } 844if (GetLSHF1 (CURRENT_LATCHES .MICROINSTRUCTION )) { 845addr2 <<=1 ; 846 } 847int addr1 = CURRENT_LATCHES .PC ; 848if (GetADDR1MUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 849addr1 = sr1 ; 850 } 851adder = addr2 + addr1 ; 852gmarmux = (CURRENT_LATCHES .IR & 0xff ) <<1 ; 853if (GetMARMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 854gmarmux = adder ; 855 } 856// MDR 857int byte = CURRENT_LATCHES .MDR ; 858if (CURRENT_LATCHES .MAR & 1 ) { 859byte >>=8 ; 860 } 861if (byte & 0x80 ) { 862byte |=0xff00 ; 863 }else { 864byte &=0xff ; 865 } 866if (GetDATA_SIZE (CURRENT_LATCHES .MICROINSTRUCTION )) { 867gmdr = CURRENT_LATCHES .MDR ; 868 }else { 869gmdr = byte ; 870 } 871gpsr = CURRENT_LATCHES .MODE <<15 |CURRENT_LATCHES .N <<2 | 872CURRENT_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*/ 880BUS = 0 ; 881if (GetGATE_SHF (CURRENT_LATCHES .MICROINSTRUCTION )) { 882BUS = gshf ; 883 } 884if (GetGATE_ALU (CURRENT_LATCHES .MICROINSTRUCTION )) { 885BUS = galu ; 886 } 887if (GetGATE_MARMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 888BUS = gmarmux ; 889 } 890if (GetGATE_MDR (CURRENT_LATCHES .MICROINSTRUCTION )) { 891BUS = gmdr ; 892 } 893if (GetGATE_PC (CURRENT_LATCHES .MICROINSTRUCTION )) { 894BUS = gpc ; 895 } 896if (GetGATE_PSR (CURRENT_LATCHES .MICROINSTRUCTION )) { 897BUS = gpsr ; 898 } 899if (GetGATE_VEC (CURRENT_LATCHES .MICROINSTRUCTION )) { 900BUS = (CURRENT_LATCHES .VEC <<1 )+ 0x200 ; 901 } 902BUS = 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*/ 912if (GetLD_BEN (CURRENT_LATCHES .MICROINSTRUCTION )) { 913NEXT_LATCHES .BEN = 0 ; 914if ((CURRENT_LATCHES .IR & 0x0800 && CURRENT_LATCHES .N )|| 915 (CURRENT_LATCHES .IR & 0x0400 && CURRENT_LATCHES .Z )|| 916 (CURRENT_LATCHES .IR & 0x0200 && CURRENT_LATCHES .P )) { 917NEXT_LATCHES .BEN = 1 ; 918 } 919 } 920if (GetLD_PSR (CURRENT_LATCHES .MICROINSTRUCTION )) { 921if (GetLD_CC (CURRENT_LATCHES .MICROINSTRUCTION )) { 922NEXT_LATCHES .N = (BUS & 4 ) >>2 ; 923NEXT_LATCHES .Z = (BUS & 2 ) >>1 ; 924NEXT_LATCHES .P = (BUS & 1 ); 925 } 926if (GetMODEMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 927NEXT_LATCHES .MODE = 0 ; 928 }else { 929NEXT_LATCHES .MODE = BUS >>15 ; 930 } 931 }else if (GetLD_CC (CURRENT_LATCHES .MICROINSTRUCTION )) { 932NEXT_LATCHES .N = NEXT_LATCHES .Z = NEXT_LATCHES .P = false; 933if (BUS & 0x8000 ) { 934NEXT_LATCHES .N = true; 935 }else if (BUS ) { 936NEXT_LATCHES .P = true; 937 }else { 938NEXT_LATCHES .Z = true; 939 } 940 } 941if (GetLD_IR (CURRENT_LATCHES .MICROINSTRUCTION )) { 942NEXT_LATCHES .IR = BUS ; 943 } 944if (GetLD_MAR (CURRENT_LATCHES .MICROINSTRUCTION )) { 945NEXT_LATCHES .MAR = BUS ; 946// Check for memory exceptions 947if (GetCheck_P (CURRENT_LATCHES .MICROINSTRUCTION )&& 948NEXT_LATCHES .MAR < 0x3000 && CURRENT_LATCHES .MODE ) { 949NEXT_LATCHES .STATE_NUMBER = 26 ; 950memcpy (NEXT_LATCHES .MICROINSTRUCTION , 951CONTROL_STORE [NEXT_LATCHES .STATE_NUMBER ], 952CONTROL_STORE_BITS * sizeof (int )); 953 }else if (NEXT_LATCHES .MAR & 1 && 954GetCheck_A (CURRENT_LATCHES .MICROINSTRUCTION )) { 955NEXT_LATCHES .STATE_NUMBER = 34 ; 956memcpy (NEXT_LATCHES .MICROINSTRUCTION , 957CONTROL_STORE [NEXT_LATCHES .STATE_NUMBER ], 958CONTROL_STORE_BITS * sizeof (int )); 959 } 960 } 961if (GetLD_MDR (CURRENT_LATCHES .MICROINSTRUCTION )) { 962if (GetMIO_EN (CURRENT_LATCHES .MICROINSTRUCTION )) { 963 }else { 964int byte = BUS ; 965if (CURRENT_LATCHES .MAR & 1 ) { 966byte <<=8 ; 967 } 968if (GetDATA_SIZE (CURRENT_LATCHES .MICROINSTRUCTION )) { 969NEXT_LATCHES .MDR = BUS ; 970 }else { 971NEXT_LATCHES .MDR = byte ; 972 } 973 } 974 } 975if (GetLD_PC (CURRENT_LATCHES .MICROINSTRUCTION )) { 976switch (GetPCMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 977case 0 :NEXT_LATCHES .PC = CURRENT_LATCHES .PC + 2 ;break ; 978case 1 :NEXT_LATCHES .PC = BUS ;break ; 979case 2 :NEXT_LATCHES .PC = adder ;break ; 980case 3 :NEXT_LATCHES .PC = CURRENT_LATCHES .PC - 2 ;break ; 981 } 982 } 983if (GetLD_REG (CURRENT_LATCHES .MICROINSTRUCTION )) { 984int dr = CURRENT_LATCHES .IR >>9 & 7 ; 985if (GetDRMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 986dr = 7 ; 987 } 988if (GetSPMUX (CURRENT_LATCHES .MICROINSTRUCTION )) { 989dr = 6 ; 990 } 991NEXT_LATCHES .REGS [dr ]= BUS ; 992 } 993if (GetSWAP (CURRENT_LATCHES .MICROINSTRUCTION )&& CURRENT_LATCHES .MODE ) { 994NEXT_LATCHES .REGS [6 ]= CURRENT_LATCHES .SSP ; 995NEXT_LATCHES .SSP = CURRENT_LATCHES .REGS [6 ]; 996 } 997if (GetClear_VEC (CURRENT_LATCHES .MICROINSTRUCTION )) { 998NEXT_LATCHES .INT = 0 ; 999NEXT_LATCHES .VEC = 0 ; 1000 } 1001int ldvec = GetLD_VEC1 (CURRENT_LATCHES .MICROINSTRUCTION ) <<1 | 1002GetLD_VEC2 (CURRENT_LATCHES .MICROINSTRUCTION ); 1003if (ldvec ) { 1004if (ldvec == 1 ) { 1005NEXT_LATCHES .VEC = 2 ; 1006 }else if (ldvec == 2 ) { 1007NEXT_LATCHES .VEC = 3 ; 1008 }else { 1009NEXT_LATCHES .VEC = 4 ; 1010 } 1011 } 1012}