+/EE460N
computer architecture
git clone https://git.julia.blue/+/EE460N
79335f9
master
1/* 2Name 1: Joseph Ryan 3UTEID 1: jbr2558 4*/ 5 6/***************************************************************/ 7/* */ 8/* LC-3b Simulator - Lab 6 */ 9/* */ 10/* EE 460N -- Spring 2013 */ 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/***************************************************************/ 29void FETCH_stage (); 30void DE_stage (); 31void AGEX_stage (); 32void MEM_stage (); 33void SR_stage (); 34/***************************************************************/ 35/* A couple of useful definitions. */ 36/***************************************************************/ 37#define TRUE 1 38#define FALSE 0 39 40/***************************************************************/ 41/* Use this to avoid overflowing 16 bits on the bus. */ 42/***************************************************************/ 43#define Low16bits (x ) ((x)&0xFFFF) 44 45/***************************************************************/ 46/* Definition of the control store layout. */ 47/***************************************************************/ 48#define CONTROL_STORE_ROWS 64 49 50/***************************************************************/ 51/* Definition of bit order in control store word. */ 52/***************************************************************/ 53/* control signals from the control store */ 54enum CS_BITS { 55SR1_NEEDED , 56SR2_NEEDED , 57DRMUX , 58 59ADDR1MUX , 60ADDR2MUX1 , 61ADDR2MUX0 , 62LSHF1 , 63ADDRESSMUX , 64SR2MUX , 65ALUK1 , 66ALUK0 , 67ALU_RESULTMUX , 68 69BR_OP , 70UNCOND_OP , 71TRAP_OP , 72BR_STALL , 73 74DCACHE_EN , 75DCACHE_RW , 76DATA_SIZE , 77 78DR_VALUEMUX1 , 79DR_VALUEMUX0 , 80LD_REG , 81LD_CC , 82NUM_CONTROL_STORE_BITS 83 84}CS_BITS ; 85 86enum AGEX_CS_BITS { 87 88AGEX_ADDR1MUX , 89AGEX_ADDR2MUX1 , 90AGEX_ADDR2MUX0 , 91AGEX_LSHF1 , 92AGEX_ADDRESSMUX , 93AGEX_SR2MUX , 94AGEX_ALUK1 , 95AGEX_ALUK0 , 96AGEX_ALU_RESULTMUX , 97 98AGEX_BR_OP , 99AGEX_UNCOND_OP , 100AGEX_TRAP_OP , 101AGEX_BR_STALL , 102AGEX_DCACHE_EN , 103AGEX_DCACHE_RW , 104AGEX_DATA_SIZE , 105 106AGEX_DR_VALUEMUX1 , 107AGEX_DR_VALUEMUX0 , 108AGEX_LD_REG , 109AGEX_LD_CC , 110NUM_AGEX_CS_BITS 111}AGEX_CS_BITS ; 112 113enum MEM_CS_BITS { 114MEM_BR_OP , 115MEM_UNCOND_OP , 116MEM_TRAP_OP , 117MEM_BR_STALL , 118MEM_DCACHE_EN , 119MEM_DCACHE_RW , 120MEM_DATA_SIZE , 121 122MEM_DR_VALUEMUX1 , 123MEM_DR_VALUEMUX0 , 124MEM_LD_REG , 125MEM_LD_CC , 126NUM_MEM_CS_BITS 127}MEM_CS_BITS ; 128 129enum SR_CS_BITS { 130SR_DR_VALUEMUX1 , 131SR_DR_VALUEMUX0 , 132SR_LD_REG , 133SR_LD_CC , 134NUM_SR_CS_BITS 135}SR_CS_BITS ; 136 137/***************************************************************/ 138/* Functions to get at the control bits. */ 139/***************************************************************/ 140int Get_SR1_NEEDED (int * x ) { 141return (x [SR1_NEEDED ]); 142} 143int Get_SR2_NEEDED (int * x ) { 144return (x [SR2_NEEDED ]); 145} 146int Get_DRMUX (int * x ) { 147return (x [DRMUX ]); 148} 149int Get_DE_BR_OP (int * x ) { 150return (x [BR_OP ]); 151} 152int Get_ADDR1MUX (int * x ) { 153return (x [AGEX_ADDR1MUX ]); 154} 155int Get_ADDR2MUX (int * x ) { 156return ((x [AGEX_ADDR2MUX1 ] <<1 )+ x [AGEX_ADDR2MUX0 ]); 157} 158int Get_LSHF1 (int * x ) { 159return (x [AGEX_LSHF1 ]); 160} 161int Get_ADDRESSMUX (int * x ) { 162return (x [AGEX_ADDRESSMUX ]); 163} 164int Get_SR2MUX (int * x ) { 165return (x [AGEX_SR2MUX ]); 166} 167int Get_ALUK (int * x ) { 168return ((x [AGEX_ALUK1 ] <<1 )+ x [AGEX_ALUK0 ]); 169} 170int Get_ALU_RESULTMUX (int * x ) { 171return (x [AGEX_ALU_RESULTMUX ]); 172} 173int Get_BR_OP (int * x ) { 174return (x [MEM_BR_OP ]); 175} 176int Get_UNCOND_OP (int * x ) { 177return (x [MEM_UNCOND_OP ]); 178} 179int Get_TRAP_OP (int * x ) { 180return (x [MEM_TRAP_OP ]); 181} 182int Get_DCACHE_EN (int * x ) { 183return (x [MEM_DCACHE_EN ]); 184} 185int Get_DCACHE_RW (int * x ) { 186return (x [MEM_DCACHE_RW ]); 187} 188int Get_DATA_SIZE (int * x ) { 189return (x [MEM_DATA_SIZE ]); 190} 191int Get_DR_VALUEMUX1 (int * x ) { 192return ((x [SR_DR_VALUEMUX1 ] <<1 )+ x [SR_DR_VALUEMUX0 ]); 193} 194int Get_AGEX_LD_REG (int * x ) { 195return (x [AGEX_LD_REG ]); 196} 197int Get_AGEX_LD_CC (int * x ) { 198return (x [AGEX_LD_CC ]); 199} 200int Get_MEM_LD_REG (int * x ) { 201return (x [MEM_LD_REG ]); 202} 203int Get_MEM_LD_CC (int * x ) { 204return (x [MEM_LD_CC ]); 205} 206int Get_SR_LD_REG (int * x ) { 207return (x [SR_LD_REG ]); 208} 209int Get_SR_LD_CC (int * x ) { 210return (x [SR_LD_CC ]); 211} 212int Get_DE_BR_STALL (int * x ) { 213return (x [BR_STALL ]); 214} 215int Get_AGEX_BR_STALL (int * x ) { 216return (x [AGEX_BR_STALL ]); 217} 218int Get_MEM_BR_STALL (int * x ) { 219return (x [MEM_BR_STALL ]); 220} 221 222/***************************************************************/ 223/* The control store rom. */ 224/***************************************************************/ 225int CONTROL_STORE [CONTROL_STORE_ROWS ][NUM_CONTROL_STORE_BITS ]; 226 227/***************************************************************/ 228/* Main memory. */ 229/***************************************************************/ 230/* MEMORY[A][0] stores the least significant byte of word at word address A 231MEMORY[A][1] stores the most significant byte of word at word address A 232There are two write enable signals, one for each byte. WE0 is used for 233the least significant byte of a word. WE1 is used for the most significant 234byte of a word. */ 235 236#define WORDS_IN_MEM 0x08000 237int MEMORY [WORDS_IN_MEM ][2 ]; 238 239/***************************************************************/ 240/* The LC-3b register file. */ 241/***************************************************************/ 242#define LC3b_REGS 8 243int REGS [LC3b_REGS ]; 244/***************************************************************/ 245/* architectural state */ 246/***************************************************************/ 247int PC ,/* program counter */ 248N ,/* n condition bit */ 249Z = 1 ,/* z condition bit */ 250P ;/* p condition bit */ 251/***************************************************************/ 252/* LC-3b State info. */ 253/***************************************************************/ 254 255typedef struct PipeState_Entry_Struct { 256/* DE latches */ 257int DE_NPC ,DE_IR ,DE_V , 258/* AGEX lateches */ 259AGEX_NPC ,AGEX_SR1 ,AGEX_SR2 ,AGEX_CC ,AGEX_IR ,AGEX_DRID ,AGEX_V , 260AGEX_CS [NUM_AGEX_CS_BITS ], 261/* MEM latches */ 262MEM_NPC ,MEM_ALU_RESULT ,MEM_ADDRESS ,MEM_CC ,MEM_IR ,MEM_DRID ,MEM_V , 263MEM_CS [NUM_MEM_CS_BITS ], 264/* SR latches */ 265SR_NPC ,SR_DATA ,SR_ALU_RESULT ,SR_ADDRESS ,SR_IR ,SR_DRID ,SR_V , 266SR_CS [NUM_SR_CS_BITS ]; 267 268}PipeState_Entry ; 269 270/* data structure for latch */ 271PipeState_Entry PS ,NEW_PS ; 272 273/* simulator signal */ 274int RUN_BIT ; 275 276/* Internal stall signals */ 277int dep_stall ,v_de_br_stall ,v_agex_br_stall ,v_mem_br_stall ,mem_stall , 278icache_r ; 279 280/***************************************************************/ 281/* A cycle counter. */ 282/***************************************************************/ 283int CYCLE_COUNT ; 284 285/***************************************************************/ 286/* */ 287/* Procedure : help */ 288/* */ 289/* Purpose : Print out a list of commands. */ 290/* */ 291/***************************************************************/ 292void help () { 293printf ("----------------LC-3bSIM Help-------------------------\n" ); 294printf ("go - run program to completion \n" ); 295printf ("run n - execute program for n cycles \n" ); 296printf ("mdump low high - dump memory from low to high \n" ); 297printf ("rdump - dump the architectural state \n" ); 298printf ("idump - dump the internal state \n" ); 299printf ("? - display this help menu \n" ); 300printf ("quit - exit the program \n\n" ); 301} 302 303void print_CS (int * CS ,int num ) { 304int ii ; 305for (ii = 0 ;ii < num ;ii ++ ) {printf ("%d" ,CS [ii ]); } 306printf ("\n" ); 307} 308/***************************************************************/ 309/* */ 310/* Procedure : cycle */ 311/* */ 312/* Purpose : Execute a cycle */ 313/* */ 314/***************************************************************/ 315void cycle () { 316NEW_PS = PS ; 317SR_stage (); 318MEM_stage (); 319AGEX_stage (); 320DE_stage (); 321FETCH_stage (); 322PS = NEW_PS ; 323CYCLE_COUNT ++ ; 324} 325 326/***************************************************************/ 327/* */ 328/* Procedure : run n */ 329/* */ 330/* Purpose : Simulate the LC-3b for n cycles. */ 331/* */ 332/***************************************************************/ 333void run (int num_cycles ) { 334int i ; 335 336if (RUN_BIT == FALSE) { 337printf ("Can't simulate, Simulator is halted\n\n" ); 338return ; 339 } 340 341printf ("Simulating for %d cycles...\n\n" ,num_cycles ); 342for (i = 0 ;i < num_cycles ;i ++ ) { 343if (PC == 0x0000 ) { 344cycle (); 345RUN_BIT = FALSE; 346printf ("Simulator halted\n\n" ); 347break ; 348 } 349cycle (); 350 } 351} 352 353/***************************************************************/ 354/* */ 355/* Procedure : go */ 356/* */ 357/* Purpose : Simulate the LC-3b until HALTed. */ 358/* */ 359/***************************************************************/ 360void go () { 361if ((RUN_BIT == FALSE)|| (PC == 0x0000 )) { 362printf ("Can't simulate, Simulator is halted\n\n" ); 363return ; 364 } 365printf ("Simulating...\n\n" ); 366/* initialization */ 367while (PC != 0x0000 )cycle (); 368cycle (); 369RUN_BIT = FALSE; 370printf ("Simulator halted\n\n" ); 371} 372 373/***************************************************************/ 374/* */ 375/* Procedure : mdump */ 376/* */ 377/* Purpose : Dump a region of memory to the output file. */ 378/* */ 379/***************************************************************/ 380void mdump (FILE * dumpsim_file ,int start ,int stop ) { 381int address ;/* this is a byte address */ 382 383printf ("\nMemory content [0x%04x..0x%04x] :\n" ,start ,stop ); 384printf ("-------------------------------------\n" ); 385for (address = (start >>1 );address <= (stop >>1 );address ++ ) 386printf (" 0x%04x (%d) : 0x%02x%02x\n" ,address <<1 ,address <<1 , 387MEMORY [address ][1 ],MEMORY [address ][0 ]); 388printf ("\n" ); 389 390/* dump the memory contents into the dumpsim file */ 391fprintf (dumpsim_file ,"\nMemory content [0x%04x..0x%04x] :\n" ,start ,stop ); 392fprintf (dumpsim_file ,"-------------------------------------\n" ); 393for (address = (start >>1 );address <= (stop >>1 );address ++ ) 394fprintf (dumpsim_file ," 0x%04x (%d) : 0x%02x%02x\n" ,address <<1 , 395address <<1 ,MEMORY [address ][1 ],MEMORY [address ][0 ]); 396fprintf (dumpsim_file ,"\n" ); 397fflush (dumpsim_file ); 398} 399 400/***************************************************************/ 401/* */ 402/* Procedure : rdump */ 403/* */ 404/* Purpose : Dump current architectural state to the */ 405/* output file. */ 406/* */ 407/***************************************************************/ 408void rdump (FILE * dumpsim_file ) { 409int k ; 410 411printf ("\nCurrent architectural state :\n" ); 412printf ("-------------------------------------\n" ); 413printf ("Cycle Count : %d\n" ,CYCLE_COUNT ); 414printf ("PC : 0x%04x\n" ,PC ); 415printf ("CCs: N = %d Z = %d P = %d\n" ,N ,Z ,P ); 416printf ("Registers:\n" ); 417for (k = 0 ;k < LC3b_REGS ;k ++ ) 418printf ("%d: 0x%04x\n" ,k , (REGS [k ]& 0xFFFF )); 419printf ("\n" ); 420 421/* dump the state information into the dumpsim file */ 422fprintf (dumpsim_file ,"\nCurrent architectural state :\n" ); 423fprintf (dumpsim_file ,"-------------------------------------\n" ); 424fprintf (dumpsim_file ,"Cycle Count : %d\n" ,CYCLE_COUNT ); 425fprintf (dumpsim_file ,"PC : 0x%04x\n" ,PC ); 426fprintf (dumpsim_file ,"CCs: N = %d Z = %d P = %d\n" ,N ,Z ,P ); 427fprintf (dumpsim_file ,"Registers:\n" ); 428for (k = 0 ;k < LC3b_REGS ;k ++ ) 429fprintf (dumpsim_file ,"%d: 0x%04x\n" ,k , (REGS [k ]& 0xFFFF )); 430fprintf (dumpsim_file ,"\n" ); 431fflush (dumpsim_file ); 432} 433 434/***************************************************************/ 435/* */ 436/* Procedure : idump */ 437/* */ 438/* Purpose : Dump current internal state to the */ 439/* output file. */ 440/* */ 441/***************************************************************/ 442void idump (FILE * dumpsim_file ) { 443int k ; 444 445printf ("\nCurrent architectural state :\n" ); 446printf ("-------------------------------------\n" ); 447printf ("Cycle Count : %d\n" ,CYCLE_COUNT ); 448printf ("PC : 0x%04x\n" ,PC ); 449printf ("CCs: N = %d Z = %d P = %d\n" ,N ,Z ,P ); 450printf ("Registers:\n" ); 451for (k = 0 ;k < LC3b_REGS ;k ++ ) 452printf ("%d: 0x%04x\n" ,k , (REGS [k ]& 0xFFFF )); 453printf ("\n" ); 454 455printf ("------------- Stall Signals -------------\n" ); 456printf ("ICACHE_R : %d\n" ,icache_r ); 457printf ("DEP_STALL : %d\n" ,dep_stall ); 458printf ("V_DE_BR_STALL : %d\n" ,v_de_br_stall ); 459printf ("V_AGEX_BR_STALL : %d\n" ,v_agex_br_stall ); 460printf ("MEM_STALL : %d\n" ,mem_stall ); 461printf ("V_MEM_BR_STALL : %d\n" ,v_mem_br_stall ); 462printf ("\n" ); 463 464printf ("------------- DE Latches --------------\n" ); 465printf ("DE_NPC : 0x%04x\n" ,PS .DE_NPC ); 466printf ("DE_IR : 0x%04x\n" ,PS .DE_IR ); 467printf ("DE_V : %d\n" ,PS .DE_V ); 468printf ("\n" ); 469 470printf ("------------- AGEX Latches --------------\n" ); 471printf ("AGEX_NPC : 0x%04x\n" ,PS .AGEX_NPC ); 472printf ("AGEX_SR1 : 0x%04x\n" ,PS .AGEX_SR1 ); 473printf ("AGEX_SR2 : 0x%04x\n" ,PS .AGEX_SR2 ); 474printf ("AGEX_CC : %d\n" ,PS .AGEX_CC ); 475printf ("AGEX_IR : 0x%04x\n" ,PS .AGEX_IR ); 476printf ("AGEX_DRID : %d\n" ,PS .AGEX_DRID ); 477printf ("AGEX_CS : " ); 478for (k = 0 ;k < NUM_AGEX_CS_BITS ;k ++ ) {printf ("%d" ,PS .AGEX_CS [k ]); } 479printf ("\n" ); 480printf ("AGEX_V : %d\n" ,PS .AGEX_V ); 481printf ("\n" ); 482 483printf ("------------- MEM Latches --------------\n" ); 484printf ("MEM_NPC : 0x%04x\n" ,PS .MEM_NPC ); 485printf ("MEM_ALU_RESULT : 0x%04x\n" ,PS .MEM_ALU_RESULT ); 486printf ("MEM_ADDRESS : 0x%04x\n" ,PS .MEM_ADDRESS ); 487printf ("MEM_CC : %d\n" ,PS .MEM_CC ); 488printf ("MEM_IR : 0x%04x\n" ,PS .MEM_IR ); 489printf ("MEM_DRID : %d\n" ,PS .MEM_DRID ); 490printf ("MEM_CS : " ); 491for (k = 0 ;k < NUM_MEM_CS_BITS ;k ++ ) {printf ("%d" ,PS .MEM_CS [k ]); } 492printf ("\n" ); 493printf ("MEM_V : %d\n" ,PS .MEM_V ); 494printf ("\n" ); 495 496printf ("------------- SR Latches --------------\n" ); 497printf ("SR_NPC : 0x%04x\n" ,PS .SR_NPC ); 498printf ("SR_DATA : 0x%04x\n" ,PS .SR_DATA ); 499printf ("SR_ALU_RESULT : 0x%04x\n" ,PS .SR_ALU_RESULT ); 500printf ("SR_ADDRESS : 0x%04x\n" ,PS .SR_ADDRESS ); 501printf ("SR_IR : 0x%04x\n" ,PS .SR_IR ); 502printf ("SR_DRID : %d\n" ,PS .SR_DRID ); 503printf ("SR_CS : " ); 504for (k = 0 ;k < NUM_SR_CS_BITS ;k ++ ) {printf ("%d" ,PS .SR_CS [k ]); } 505printf ("\n" ); 506printf ("SR_V : %d\n" ,PS .SR_V ); 507 508printf ("\n" ); 509 510/* dump the state information into the dumpsim file */ 511fprintf (dumpsim_file ,"\nCurrent register/bus values :\n" ); 512fprintf (dumpsim_file ,"\nCurrent architectural state :\n" ); 513fprintf (dumpsim_file ,"-------------------------------------\n" ); 514fprintf (dumpsim_file ,"Cycle Count : %d\n" ,CYCLE_COUNT ); 515fprintf (dumpsim_file ,"PC : 0x%04x\n" ,PC ); 516fprintf (dumpsim_file ,"CCs: N = %d Z = %d P = %d\n" ,N ,Z ,P ); 517fprintf (dumpsim_file ,"Registers:\n" ); 518for (k = 0 ;k < LC3b_REGS ;k ++ ) 519fprintf (dumpsim_file ,"%d: 0x%04x\n" ,k , (REGS [k ]& 0xFFFF )); 520fprintf (dumpsim_file ,"\n" ); 521 522fprintf (dumpsim_file ,"------------- Stall Signals -------------\n" ); 523fprintf (dumpsim_file ,"ICACHE_R : %d\n" ,icache_r ); 524fprintf (dumpsim_file ,"DEP_STALL : %d\n" ,dep_stall ); 525fprintf (dumpsim_file ,"V_DE_BR_STALL : %d\n" ,v_de_br_stall ); 526fprintf (dumpsim_file ,"V_AGEX_BR_STALL : %d\n" ,v_agex_br_stall ); 527fprintf (dumpsim_file ,"MEM_STALL : %d\n" ,mem_stall ); 528fprintf (dumpsim_file ,"V_MEM_BR_STALL : %d\n" ,v_mem_br_stall ); 529fprintf (dumpsim_file ,"\n" ); 530 531fprintf (dumpsim_file ,"------------- DE Latches --------------\n" ); 532fprintf (dumpsim_file ,"DE_NPC : 0x%04x\n" ,PS .DE_NPC ); 533fprintf (dumpsim_file ,"DE_IR : 0x%04x\n" ,PS .DE_IR ); 534fprintf (dumpsim_file ,"DE_V : %d\n" ,PS .DE_V ); 535fprintf (dumpsim_file ,"\n" ); 536 537fprintf (dumpsim_file ,"------------- AGEX Latches --------------\n" ); 538fprintf (dumpsim_file ,"AGEX_NPC : 0x%04x\n" ,PS .AGEX_NPC ); 539fprintf (dumpsim_file ,"AGEX_SR1 : 0x%04x\n" ,PS .AGEX_SR1 ); 540fprintf (dumpsim_file ,"AGEX_SR2 : 0x%04x\n" ,PS .AGEX_SR2 ); 541fprintf (dumpsim_file ,"AGEX_CC : %d\n" ,PS .AGEX_CC ); 542fprintf (dumpsim_file ,"AGEX_IR : 0x%04x\n" ,PS .AGEX_IR ); 543fprintf (dumpsim_file ,"AGEX_DRID : %d\n" ,PS .AGEX_DRID ); 544fprintf (dumpsim_file ,"AGEX_CS : " ); 545for (k = 0 ;k < NUM_AGEX_CS_BITS ;k ++ ) { 546fprintf (dumpsim_file ,"%d" ,PS .AGEX_CS [k ]); 547 } 548fprintf (dumpsim_file ,"\n" ); 549fprintf (dumpsim_file ,"AGEX_V : %d\n" ,PS .AGEX_V ); 550fprintf (dumpsim_file ,"\n" ); 551 552fprintf (dumpsim_file ,"------------- MEM Latches --------------\n" ); 553fprintf (dumpsim_file ,"MEM_NPC : 0x%04x\n" ,PS .MEM_NPC ); 554fprintf (dumpsim_file ,"MEM_ALU_RESULT : 0x%04x\n" ,PS .MEM_ALU_RESULT ); 555fprintf (dumpsim_file ,"MEM_ADDRESS : 0x%04x\n" ,PS .MEM_ADDRESS ); 556fprintf (dumpsim_file ,"MEM_CC : %d\n" ,PS .MEM_CC ); 557fprintf (dumpsim_file ,"MEM_IR : 0x%04x\n" ,PS .MEM_IR ); 558fprintf (dumpsim_file ,"MEM_DRID : %d\n" ,PS .MEM_DRID ); 559fprintf (dumpsim_file ,"MEM_CS : " ); 560for (k = 0 ;k < NUM_MEM_CS_BITS ;k ++ ) { 561fprintf (dumpsim_file ,"%d" ,PS .MEM_CS [k ]); 562 } 563fprintf (dumpsim_file ,"\n" ); 564fprintf (dumpsim_file ,"MEM_V : %d\n" ,PS .MEM_V ); 565fprintf (dumpsim_file ,"\n" ); 566 567fprintf (dumpsim_file ,"------------- SR Latches --------------\n" ); 568fprintf (dumpsim_file ,"SR_NPC : 0x%04x\n" ,PS .SR_NPC ); 569fprintf (dumpsim_file ,"SR_DATA : 0x%04x\n" ,PS .SR_DATA ); 570fprintf (dumpsim_file ,"SR_ALU_RESULT : 0x%04x\n" ,PS .SR_ALU_RESULT ); 571fprintf (dumpsim_file ,"SR_ADDRESS : 0x%04x\n" ,PS .SR_ADDRESS ); 572fprintf (dumpsim_file ,"SR_IR : 0x%04x\n" ,PS .SR_IR ); 573fprintf (dumpsim_file ,"SR_DRID : %d\n" ,PS .SR_DRID ); 574fprintf (dumpsim_file ,"SR_CS : " ); 575for (k = 0 ;k < NUM_SR_CS_BITS ;k ++ ) { 576fprintf (dumpsim_file ,"%d" ,PS .SR_CS [k ]); 577 } 578fprintf (dumpsim_file ,"\n" ); 579fprintf (dumpsim_file ,"SR_V : %d\n" ,PS .SR_V ); 580 581fprintf (dumpsim_file ,"\n" ); 582fflush (dumpsim_file ); 583} 584/***************************************************************/ 585/* */ 586/* Procedure : get_command */ 587/* */ 588/* Purpose : Read a command from standard input. */ 589/* */ 590/***************************************************************/ 591void get_command (FILE * dumpsim_file ) { 592char buffer [20 ]; 593int start ,stop ,cycles ; 594 595printf ("LC-3b-SIM> " ); 596 597scanf ("%s" ,buffer ); 598printf ("\n" ); 599 600switch (buffer [0 ]) { 601case 'G' : 602case 'g' :go ();break ; 603 604case 'M' : 605case 'm' : 606scanf ("%i %i" ,& start ,& stop ); 607mdump (dumpsim_file ,start ,stop ); 608break ; 609 610case '?' :help ();break ; 611case 'Q' : 612case 'q' :printf ("Bye.\n" );exit (0 ); 613 614case 'R' : 615case 'r' : 616if (buffer [1 ]== 'd' || buffer [1 ]== 'D' ) 617rdump (dumpsim_file ); 618else { 619scanf ("%d" ,& cycles ); 620run (cycles ); 621 } 622break ; 623 624case 'I' : 625case 'i' :idump (dumpsim_file );break ; 626 627default :printf ("Invalid Command\n" );break ; 628 } 629} 630 631/***************************************************************/ 632/* */ 633/* Procedure : init_control_store */ 634/* */ 635/* Purpose : Load microprogram into control store ROM */ 636/* */ 637/***************************************************************/ 638void init_control_store (char * ucode_filename ) { 639FILE * ucode ; 640int i ,j ,index ; 641char line [200 ]; 642 643printf ("Loading Control Store from file: %s\n" ,ucode_filename ); 644 645/* Open the micro-code file. */ 646if ((ucode = fopen (ucode_filename ,"r" ))== NULL ) { 647printf ("Error: Can't open micro-code file %s\n" ,ucode_filename ); 648exit (-1 ); 649 } 650 651/* Read a line for each row in the control store. */ 652for (i = 0 ;i < CONTROL_STORE_ROWS ;i ++ ) { 653if (fscanf (ucode ,"%[^\n]\n" ,line )== EOF ) { 654printf ("Error: Too few lines (%d) in micro-code file: %s\n" ,i , 655ucode_filename ); 656exit (-1 ); 657 } 658 659/* Put in bits one at a time. */ 660index = 0 ; 661 662for (j = 0 ;j < NUM_CONTROL_STORE_BITS ;j ++ ) { 663/* Needs to find enough bits in line. */ 664if (line [index ]== '\0' ) { 665printf ("Error: Too few control bits in micro-code file: " 666"%s\nLine: %d\n" , 667ucode_filename ,i ); 668exit (-1 ); 669 } 670if (line [index ]!= '0' && line [index ]!= '1' ) { 671printf ("Error: Unknown value in micro-code file: %s\nLine: %d, " 672"Bit: %d\n" , 673ucode_filename ,i ,j ); 674exit (-1 ); 675 } 676 677/* Set the bit in the Control Store. */ 678CONTROL_STORE [i ][j ]= (line [index ]== '0' ) ?0 :1 ; 679index ++ ; 680 } 681/* Warn about extra bits in line. */ 682if (line [index ]!= '\0' ) 683printf ("Warning: Extra bit(s) in control store file %s. Line: %d\n" , 684ucode_filename ,i ); 685 } 686printf ("\n" ); 687} 688 689/***************************************************************/ 690/* */ 691/* Procedure : init_memory */ 692/* */ 693/* Purpose : Zero out the memory array */ 694/* */ 695/***************************************************************/ 696void init_memory () { 697int i ; 698 699for (i = 0 ;i < WORDS_IN_MEM ;i ++ ) { 700MEMORY [i ][0 ]= 0 ; 701MEMORY [i ][1 ]= 0 ; 702 } 703} 704 705/***************************************************************/ 706/* */ 707/* Procedure : init_state */ 708/* */ 709/* Purpose : Zero out all latches and registers */ 710/* */ 711/***************************************************************/ 712void init_state () { 713memset (& PS ,0 ,sizeof (PipeState_Entry )); 714memset (& NEW_PS ,0 ,sizeof (PipeState_Entry )); 715 716dep_stall = 0 ; 717v_de_br_stall = 0 ; 718v_agex_br_stall = 0 ; 719v_mem_br_stall = 0 ; 720mem_stall = 0 ; 721} 722 723/**************************************************************/ 724/* */ 725/* Procedure : load_program */ 726/* */ 727/* Purpose : Load program and service routines into mem. */ 728/* */ 729/**************************************************************/ 730void load_program (char * program_filename ) { 731FILE * prog ; 732int ii ,word ,program_base ; 733 734/* Open program file. */ 735prog = fopen (program_filename ,"r" ); 736if (prog == NULL ) { 737printf ("Error: Can't open program file %s\n" ,program_filename ); 738exit (-1 ); 739 } 740 741/* Read in the program. */ 742if (fscanf (prog ,"%x\n" ,& word )!= EOF ) 743program_base = word >>1 ; 744else { 745printf ("Error: Program file is empty\n" ); 746exit (-1 ); 747 } 748 749ii = 0 ; 750while (fscanf (prog ,"%x\n" ,& word )!= EOF ) { 751/* Make sure it fits. */ 752if (program_base + ii >=WORDS_IN_MEM ) { 753printf ("Error: Program file %s is too long to fit in memory. %x\n" , 754program_filename ,ii ); 755exit (-1 ); 756 } 757 758/* Write the word to memory array. */ 759MEMORY [program_base + ii ][0 ]= word & 0x00FF ; 760MEMORY [program_base + ii ][1 ]= (word >>8 )& 0x00FF ; 761ii ++ ; 762 } 763 764if (PC == 0 ) 765PC = program_base <<1 ; 766printf ("Read %d words from program into memory.\n\n" ,ii ); 767} 768 769/***************************************************************/ 770/* */ 771/* Procedure : initialize */ 772/* */ 773/* Purpose : Load microprogram and machine language program */ 774/* and set up initial state of the machine. */ 775/* */ 776/***************************************************************/ 777void initialize (char * ucode_filename ,char * program_filename , 778int num_prog_files ) { 779int i ; 780init_control_store (ucode_filename ); 781 782init_memory (); 783 784for (i = 0 ;i < num_prog_files ;i ++ ) { 785load_program (program_filename ); 786while (* program_filename ++ != '\0' ) 787 ; 788 } 789init_state (); 790 791RUN_BIT = TRUE; 792} 793 794/***************************************************************/ 795/* */ 796/* dcache_access */ 797/* */ 798/***************************************************************/ 799void dcache_access (int dcache_addr ,int * read_word ,int write_word , 800int * dcache_r ,int mem_w0 ,int mem_w1 ) { 801int addr = dcache_addr >>1 ; 802int random = CYCLE_COUNT %9 ; 803 804if (!random ) { 805* dcache_r = 0 ; 806* read_word = 0xfeed ; 807 }else { 808* dcache_r = 1 ; 809 810* read_word = (MEMORY [addr ][1 ] <<8 ) | (MEMORY [addr ][0 ]& 0x00FF ); 811if (mem_w0 ) 812MEMORY [addr ][0 ]= write_word & 0x00FF ; 813if (mem_w1 ) 814MEMORY [addr ][1 ]= (write_word & 0xFF00 ) >>8 ; 815 } 816} 817/***************************************************************/ 818/* */ 819/* icache_access */ 820/* */ 821/***************************************************************/ 822void icache_access (int icache_addr ,int * read_word ,int * icache_r ) { 823int addr = icache_addr >>1 ; 824int random = CYCLE_COUNT %13 ; 825 826if (!random ) { 827* icache_r = 0 ; 828* read_word = 0xfeed ; 829 }else { 830* icache_r = 1 ; 831* read_word = MEMORY [addr ][1 ] <<8 |MEMORY [addr ][0 ]; 832 } 833} 834/***************************************************************/ 835/* */ 836/* Procedure : main */ 837/* */ 838/***************************************************************/ 839int main (int argc ,char * argv []) { 840FILE * dumpsim_file ; 841 842/* Error Checking */ 843if (argc < 3 ) { 844printf ("Error: usage: %s <micro_code_file> <program_file_1> " 845"<program_file_2> ...\n" , 846argv [0 ]); 847exit (1 ); 848 } 849 850printf ("LC-3b Simulator\n\n" ); 851 852initialize (argv [1 ],argv [2 ],argc - 2 ); 853 854if ((dumpsim_file = fopen ("dumpsim" ,"w" ))== NULL ) { 855printf ("Error: Can't open dumpsim file\n" ); 856exit (-1 ); 857 } 858 859while (1 )get_command (dumpsim_file ); 860} 861 862/***************************************************************/ 863/* Do not modify the above code. 864You are allowed to use the following global variables in your 865code. These are defined above. 866 867RUN_BIT 868REGS 869MEMORY 870 871PC 872N 873Z 874P 875 876dep_stall 877v_de_br_stall 878v_agex_br_stall 879v_mem_br_stall 880mem_stall 881icache_r 882 883PS 884NEW_PS 885 886 887You may define your own local/global variables and functions. 888You may use the functions to get at the control bits defined 889above. 890 891 892Begin your code here */ 893/***************************************************************/ 894#define COPY_AGEX_CS_START 3 895#define COPY_MEM_CS_START 9 896#define COPY_SR_CS_START 7 897 898/* Signals generated by SR stage and needed by previous stages in the 899pipeline are declared below. */ 900int sr_reg_data ,sr_n ,sr_z ,sr_p ,v_sr_ld_cc ,v_sr_ld_reg ,sr_reg_id ; 901 902/************************* SR_stage() *************************/ 903void SR_stage () { 904/* You are given the code for SR_stage to get you started. Look at 905the figure for SR stage to see how this code is implemented. */ 906 907switch (Get_DR_VALUEMUX1 (PS .SR_CS )) { 908case 0 :sr_reg_data = PS .SR_ADDRESS ;break ; 909case 1 :sr_reg_data = PS .SR_DATA ;break ; 910case 2 :sr_reg_data = PS .SR_NPC ;break ; 911case 3 :sr_reg_data = PS .SR_ALU_RESULT ;break ; 912 } 913 914sr_reg_id = PS .SR_DRID ; 915v_sr_ld_reg = Get_SR_LD_REG (PS .SR_CS )& PS .SR_V ; 916v_sr_ld_cc = Get_SR_LD_CC (PS .SR_CS )& PS .SR_V ; 917 918/* CC LOGIC */ 919sr_n = ((sr_reg_data & 0x8000 ) ?1 :0 ); 920sr_z = ((sr_reg_data & 0xFFFF ) ?0 :1 ); 921sr_p = 0 ; 922if ((!sr_n )&& (!sr_z )) 923sr_p = 1 ; 924} 925 926// Other signals needed by previous stages 927int mem_pcmux ,target_pc ,trap_pc ; 928int v_mem_ld_cc ,v_mem_ld_reg ,mem_reg_id ,dcache_r ; 929/************************* MEM_stage() *************************/ 930void MEM_stage () { 931int ii ,jj = 0 ; 932int en = Get_DCACHE_EN (PS .MEM_CS )&& PS .MEM_V ; 933target_pc = PS .MEM_ADDRESS ; 934int contents = 0 ; 935int data = PS .MEM_ALU_RESULT ; 936if (en ) { 937int w0 ,w1 ; 938if (PS .MEM_ADDRESS & 1 ) { 939// could be an alignment exception but we aren't checking 940w0 = 0 ; 941w1 = 1 ; 942data <<=8 ; 943 }else { 944if (Get_DATA_SIZE (PS .MEM_CS )) { 945w0 = 1 ; 946w1 = 1 ; 947 }else { 948w0 = 1 ; 949w1 = 0 ; 950 } 951 } 952if (!Get_DCACHE_RW (PS .MEM_CS )) { 953w0 = w1 = 0 ; 954 } 955dcache_access (PS .MEM_ADDRESS ,& contents ,PS .MEM_ALU_RESULT ,& dcache_r , 956w0 ,w1 ); 957if (PS .MEM_ADDRESS & 1 ) { 958// could be an alignment exception but we aren't checking 959contents = contents >>8 ; 960 } 961if (!Get_DATA_SIZE (PS .MEM_CS )) { 962if (contents & 0x80 ) { 963contents |=0xff00 ; 964 } 965 } 966 } 967mem_stall = en && !dcache_r ; 968v_mem_ld_cc = Get_MEM_LD_CC (PS .MEM_CS )&& PS .MEM_V ; 969v_mem_ld_reg = Get_MEM_LD_REG (PS .MEM_CS )&& PS .MEM_V ; 970v_mem_br_stall = Get_MEM_BR_STALL (PS .MEM_CS )&& PS .MEM_V ; 971mem_reg_id = PS .MEM_DRID ; 972mem_pcmux = 0 ; 973if (PS .MEM_V ) { 974if (Get_UNCOND_OP (PS .MEM_CS )) { 975mem_pcmux = 1 ; 976 }else if (Get_BR_OP (PS .MEM_CS )&& 977 (PS .MEM_CC |PS .MEM_IR >>9 )== PS .MEM_IR >>9 ) { 978mem_pcmux = 1 ; 979 }else if (Get_TRAP_OP (PS .MEM_CS )) { 980mem_pcmux = 2 ; 981 } 982 } 983trap_pc = NEW_PS .SR_DATA = contents ; 984NEW_PS .SR_DRID = PS .MEM_DRID ; 985NEW_PS .SR_NPC = PS .MEM_NPC ; 986NEW_PS .SR_ADDRESS = PS .MEM_ADDRESS ; 987NEW_PS .SR_ALU_RESULT = PS .MEM_ALU_RESULT ; 988NEW_PS .SR_IR = PS .MEM_IR ; 989NEW_PS .SR_V = PS .MEM_V && !mem_stall ; 990/* The code below propagates the control signals from MEM.CS latch 991to SR.CS latch. You still need to latch other values into the 992other SR latches. */ 993for (ii = COPY_SR_CS_START ;ii < NUM_MEM_CS_BITS ;ii ++ ) { 994NEW_PS .SR_CS [jj ++ ]= PS .MEM_CS [ii ]; 995 } 996} 997 998int v_agex_ld_cc ,v_agex_ld_reg ,agex_reg_id ; 999#define SEXT_IR (bits ) ((struct { int x : (bits); }){.x = PS.AGEX_IR}.x) 1000/************************* AGEX_stage() *************************/ 1001void AGEX_stage () { 1002int ii ,jj = 0 ; 1003int sr1 = PS .AGEX_SR1 ; 1004int sr2 = PS .AGEX_SR2 ; 1005if (Get_SR2MUX (PS .AGEX_CS )) { 1006sr2 = SEXT_IR (5 ); 1007 } 1008int alu ; 1009switch (Get_ALUK (PS .AGEX_CS )) { 1010case 0 :alu = sr1 + sr2 ;break ; 1011case 1 :alu = sr1 & sr2 ;break ; 1012case 2 :alu = sr1 ^sr2 ;break ; 1013case 3 :alu = sr2 ;break ; 1014 } 1015int shift_amt = PS .AGEX_IR & 0xf ; 1016int shf ; 1017switch (PS .AGEX_IR >>4 & 3 ) { 1018case 0 :shf = sr1 <<shift_amt ;break ; 1019case 1 :shf = sr1 >>shift_amt ;break ; 1020case 2 :shf = sr1 <<shift_amt ;break ; 1021case 3 : 1022if (sr1 & 0x8000 ) { 1023sr1 |=0xffff0000 ; 1024 } 1025shf = sr1 >>shift_amt ; 1026shf &=0xffff ; 1027break ; 1028 } 1029int result = shf ; 1030if (Get_ALU_RESULTMUX (PS .AGEX_CS )) { 1031result = alu ; 1032 } 1033int address = PS .AGEX_NPC ; 1034if (Get_ADDR1MUX (PS .AGEX_CS )) { 1035address = PS .AGEX_SR1 ; 1036 } 1037int offset ; 1038switch (Get_ADDR2MUX (PS .AGEX_CS )) { 1039case 0 :offset = 0 ;break ; 1040case 1 :offset = SEXT_IR (6 );break ; 1041case 2 :offset = SEXT_IR (9 );break ; 1042case 3 :offset = SEXT_IR (11 );break ; 1043 } 1044if (Get_LSHF1 (PS .AGEX_CS )) { 1045offset <<=1 ; 1046 } 1047if (Get_ADDRESSMUX (PS .AGEX_CS )) { 1048address += offset ; 1049 }else { 1050address = (PS .AGEX_IR & 0x00ff ) <<1 ; 1051 } 1052v_agex_ld_cc = Get_AGEX_LD_CC (PS .AGEX_CS )&& PS .AGEX_V ; 1053v_agex_ld_reg = Get_AGEX_LD_REG (PS .AGEX_CS )&& PS .AGEX_V ; 1054v_agex_br_stall = Get_AGEX_BR_STALL (PS .AGEX_CS )&& PS .AGEX_V ; 1055agex_reg_id = PS .AGEX_DRID ; 1056if (!mem_stall ) { 1057NEW_PS .MEM_ALU_RESULT = result & 0xffff ; 1058NEW_PS .MEM_DRID = PS .AGEX_DRID ; 1059NEW_PS .MEM_NPC = PS .AGEX_NPC ; 1060NEW_PS .MEM_CC = PS .AGEX_CC ; 1061NEW_PS .MEM_IR = PS .AGEX_IR ; 1062NEW_PS .MEM_ADDRESS = address ; 1063NEW_PS .MEM_V = PS .AGEX_V ; 1064/* The code below propagates the control signals from AGEX.CS latch 1065to MEM.CS latch. */ 1066for (ii = COPY_MEM_CS_START ;ii < NUM_AGEX_CS_BITS ;ii ++ ) { 1067NEW_PS .MEM_CS [jj ++ ]= PS .AGEX_CS [ii ]; 1068 } 1069 } 1070} 1071 1072/************************* DE_stage() *************************/ 1073void DE_stage () { 1074int CONTROL_STORE_ADDRESS = (PS .DE_IR & 0xf000 ) >>10 | 1075 (PS .DE_IR & 0x0800 ) >>10 | 1076 (PS .DE_IR & 0x0020 ) >>5 ; 1077int ii ,jj = 0 ; 1078v_de_br_stall = 1079Get_DE_BR_STALL (CONTROL_STORE [CONTROL_STORE_ADDRESS ])&& PS .DE_V ; 1080int sr1 = (PS .DE_IR & 0x01c0 ) >>6 ; 1081int sr2 = PS .DE_IR & 7 ; 1082// IR 13 decides which bits are sr2 1083if (PS .DE_IR & 0x2000 ) { 1084sr2 = (PS .DE_IR & 0x0e00 ) >>9 ; 1085 } 1086dep_stall = 0 ; 1087// Stall if BR instruction needs to wait on CC 1088if (Get_DE_BR_OP (CONTROL_STORE [CONTROL_STORE_ADDRESS ])&& 1089 (v_agex_ld_cc || v_mem_ld_cc || v_sr_ld_cc )) { 1090dep_stall = 1 ; 1091 } 1092// Stall if we need SR1 and something will write to it 1093if (Get_SR1_NEEDED (CONTROL_STORE [CONTROL_STORE_ADDRESS ])&& 1094 ((v_agex_ld_reg && agex_reg_id == sr1 )|| 1095 (v_mem_ld_reg && mem_reg_id == sr1 )|| 1096 (v_sr_ld_reg && sr_reg_id == sr1 ))) { 1097dep_stall = 1 ; 1098 } 1099// Stall if we need SR2 and something will write to it 1100if (Get_SR2_NEEDED (CONTROL_STORE [CONTROL_STORE_ADDRESS ])&& 1101 ((v_agex_ld_reg && agex_reg_id == sr2 )|| 1102 (v_mem_ld_reg && mem_reg_id == sr2 )|| 1103 (v_sr_ld_reg && sr_reg_id == sr2 ))) { 1104dep_stall = 1 ; 1105 } 1106// If those signals werent valid forget I ever asked 1107if (!PS .DE_V ) { 1108dep_stall = 0 ; 1109 } 1110if (!mem_stall ) { 1111NEW_PS .AGEX_NPC = PS .DE_NPC ; 1112NEW_PS .AGEX_CC = N <<2 |Z <<1 |P ; 1113NEW_PS .AGEX_DRID = (PS .DE_IR & 0x0e00 ) >>9 ; 1114if (Get_DRMUX (CONTROL_STORE [CONTROL_STORE_ADDRESS ])) { 1115NEW_PS .AGEX_DRID = 7 ; 1116 } 1117NEW_PS .AGEX_IR = PS .DE_IR ; 1118NEW_PS .AGEX_SR1 = REGS [sr1 ]; 1119NEW_PS .AGEX_SR2 = REGS [sr2 ]; 1120NEW_PS .AGEX_V = PS .DE_V && !dep_stall ; 1121 1122/* The code below propagates the control signals from the CONTROL 1123STORE to the AGEX.CS latch. */ 1124for (ii = COPY_AGEX_CS_START ;ii < NUM_CONTROL_STORE_BITS ;ii ++ ) { 1125NEW_PS .AGEX_CS [jj ++ ]= CONTROL_STORE [CONTROL_STORE_ADDRESS ][ii ]; 1126 } 1127 } 1128// Write previous results to register file 1129if (v_sr_ld_cc ) { 1130N = sr_n ; 1131Z = sr_z ; 1132P = sr_p ; 1133 } 1134if (v_sr_ld_reg ) { 1135REGS [sr_reg_id ]= sr_reg_data ; 1136 } 1137} 1138 1139/************************* FETCH_stage() *************************/ 1140void FETCH_stage () { 1141if (!(mem_stall || dep_stall )) { 1142NEW_PS .DE_NPC = PC + 2 ; 1143NEW_PS .DE_IR = 0 ; 1144icache_access (PC ,& NEW_PS .DE_IR ,& icache_r ); 1145if (v_de_br_stall || v_agex_br_stall || v_mem_br_stall ) { 1146NEW_PS .DE_V = 0 ; 1147 }else if ((NEW_PS .DE_V = icache_r )) { 1148PC += 2 ; 1149 } 1150 } 1151switch (mem_pcmux ) { 1152case 1 :PC = target_pc ;break ; 1153case 2 :PC = trap_pc ;break ; 1154 } 1155}