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