+/EE460N
computer architecture
git clone https://git.julia.blue/+/EE460N
6adc40e
master
1/* 2Name 1: Joseph Ryan 3Name 2: Rahul Butani 4UTEID 1: jbr2558 5UTEID 2: rhb834 6*/ 7 8/***************************************************************/ 9/* */ 10/* LC-3b Instruction Level Simulator */ 11/* */ 12/* EE 460N */ 13/* The University of Texas at Austin */ 14/* */ 15/***************************************************************/ 16 17#include <assert.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include <string.h> 21 22/***************************************************************/ 23/* */ 24/* Files: isaprogram LC-3b machine language program file */ 25/* */ 26/***************************************************************/ 27 28/***************************************************************/ 29/* These are the functions you'll have to write. */ 30/***************************************************************/ 31 32void process_instruction (); 33 34/***************************************************************/ 35/* A couple of useful definitions. */ 36/***************************************************************/ 37#define FALSE 0 38#define TRUE 1 39 40/***************************************************************/ 41/* Use this to avoid overflowing 16 bits on the bus. */ 42/***************************************************************/ 43#define Low16bits (x ) ((x)&0xFFFF) 44 45/***************************************************************/ 46/* Main memory. */ 47/***************************************************************/ 48/* MEMORY[A][0] stores the least significant byte of word at word address A 49MEMORY[A][1] stores the most significant byte of word at word address A 50*/ 51 52#define WORDS_IN_MEM 0x08000 53int MEMORY [WORDS_IN_MEM ][2 ]; 54 55/***************************************************************/ 56 57/***************************************************************/ 58 59/***************************************************************/ 60/* LC-3b State info. */ 61/***************************************************************/ 62#define LC_3b_REGS 8 63 64int RUN_BIT ;/* run bit */ 65 66typedef struct System_Latches_Struct { 67int PC ,/* program counter */ 68N ,/* n condition bit */ 69Z ,/* z condition bit */ 70P ;/* p condition bit */ 71int REGS [LC_3b_REGS ];/* register file. */ 72}System_Latches ; 73 74/* Data Structure for Latch */ 75 76System_Latches CURRENT_LATCHES ,NEXT_LATCHES ; 77 78/***************************************************************/ 79/* A cycle counter. */ 80/***************************************************************/ 81int INSTRUCTION_COUNT ; 82 83/***************************************************************/ 84/* */ 85/* Procedure : help */ 86/* */ 87/* Purpose : Print out a list of commands */ 88/* */ 89/***************************************************************/ 90void help () { 91printf ("----------------LC-3b ISIM Help-----------------------\n" ); 92printf ("go - run program to completion \n" ); 93printf ("run n - execute program for n instructions\n" ); 94printf ("mdump low high - dump memory from low to high \n" ); 95printf ("rdump - dump the register & bus values \n" ); 96printf ("? - display this help menu \n" ); 97printf ("quit - exit the program \n\n" ); 98} 99 100/***************************************************************/ 101/* */ 102/* Procedure : cycle */ 103/* */ 104/* Purpose : Execute a cycle */ 105/* */ 106/***************************************************************/ 107void cycle () { 108process_instruction (); 109CURRENT_LATCHES = NEXT_LATCHES ; 110INSTRUCTION_COUNT ++ ; 111} 112 113/***************************************************************/ 114/* */ 115/* Procedure : run n */ 116/* */ 117/* Purpose : Simulate the LC-3b for n cycles */ 118/* */ 119/***************************************************************/ 120void run (int num_cycles ) { 121int i ; 122 123if (RUN_BIT == FALSE) { 124printf ("Can't simulate, Simulator is halted\n\n" ); 125return ; 126 } 127 128printf ("Simulating for %d cycles...\n\n" ,num_cycles ); 129for (i = 0 ;i < num_cycles ;i ++ ) { 130if (CURRENT_LATCHES .PC == 0x0000 ) { 131RUN_BIT = FALSE; 132printf ("Simulator halted\n\n" ); 133break ; 134 } 135cycle (); 136 } 137} 138 139/***************************************************************/ 140/* */ 141/* Procedure : go */ 142/* */ 143/* Purpose : Simulate the LC-3b until HALTed */ 144/* */ 145/***************************************************************/ 146void go () { 147if (RUN_BIT == FALSE) { 148printf ("Can't simulate, Simulator is halted\n\n" ); 149return ; 150 } 151 152printf ("Simulating...\n\n" ); 153while (CURRENT_LATCHES .PC != 0x0000 )cycle (); 154RUN_BIT = FALSE; 155printf ("Simulator halted\n\n" ); 156} 157 158/***************************************************************/ 159/* */ 160/* Procedure : mdump */ 161/* */ 162/* Purpose : Dump a word-aligned region of memory to the */ 163/* output file. */ 164/* */ 165/***************************************************************/ 166void mdump (FILE * dumpsim_file ,int start ,int stop ) { 167int address ;/* this is a byte address */ 168 169printf ("\nMemory content [0x%.4x..0x%.4x] :\n" ,start ,stop ); 170printf ("-------------------------------------\n" ); 171for (address = (start >>1 );address <= (stop >>1 );address ++ ) 172printf (" 0x%.4x (%d) : 0x%.2x%.2x\n" ,address <<1 ,address <<1 , 173MEMORY [address ][1 ],MEMORY [address ][0 ]); 174printf ("\n" ); 175 176/* dump the memory contents into the dumpsim file */ 177fprintf (dumpsim_file ,"\nMemory content [0x%.4x..0x%.4x] :\n" ,start ,stop ); 178fprintf (dumpsim_file ,"-------------------------------------\n" ); 179for (address = (start >>1 );address <= (stop >>1 );address ++ ) 180fprintf (dumpsim_file ," 0x%.4x (%d) : 0x%.2x%.2x\n" ,address <<1 , 181address <<1 ,MEMORY [address ][1 ],MEMORY [address ][0 ]); 182fprintf (dumpsim_file ,"\n" ); 183fflush (dumpsim_file ); 184} 185 186/***************************************************************/ 187/* */ 188/* Procedure : rdump */ 189/* */ 190/* Purpose : Dump current register and bus values to the */ 191/* output file. */ 192/* */ 193/***************************************************************/ 194void rdump (FILE * dumpsim_file ) { 195int k ; 196 197printf ("\nCurrent register/bus values :\n" ); 198printf ("-------------------------------------\n" ); 199printf ("Instruction Count : %d\n" ,INSTRUCTION_COUNT ); 200printf ("PC : 0x%.4x\n" ,CURRENT_LATCHES .PC ); 201printf ("CCs: N = %d Z = %d P = %d\n" ,CURRENT_LATCHES .N , 202CURRENT_LATCHES .Z ,CURRENT_LATCHES .P ); 203printf ("Registers:\n" ); 204for (k = 0 ;k < LC_3b_REGS ;k ++ ) 205printf ("%d: 0x%.4x\n" ,k ,CURRENT_LATCHES .REGS [k ]); 206printf ("\n" ); 207 208/* dump the state information into the dumpsim file */ 209fprintf (dumpsim_file ,"\nCurrent register/bus values :\n" ); 210fprintf (dumpsim_file ,"-------------------------------------\n" ); 211fprintf (dumpsim_file ,"Instruction Count : %d\n" ,INSTRUCTION_COUNT ); 212fprintf (dumpsim_file ,"PC : 0x%.4x\n" ,CURRENT_LATCHES .PC ); 213fprintf (dumpsim_file ,"CCs: N = %d Z = %d P = %d\n" ,CURRENT_LATCHES .N , 214CURRENT_LATCHES .Z ,CURRENT_LATCHES .P ); 215fprintf (dumpsim_file ,"Registers:\n" ); 216for (k = 0 ;k < LC_3b_REGS ;k ++ ) 217fprintf (dumpsim_file ,"%d: 0x%.4x\n" ,k ,CURRENT_LATCHES .REGS [k ]); 218fprintf (dumpsim_file ,"\n" ); 219fflush (dumpsim_file ); 220} 221 222/***************************************************************/ 223/* */ 224/* Procedure : get_command */ 225/* */ 226/* Purpose : Read a command from standard input. */ 227/* */ 228/***************************************************************/ 229void get_command (FILE * dumpsim_file ) { 230char buffer [20 ]; 231int start ,stop ,cycles ; 232 233printf ("LC-3b-SIM> " ); 234 235scanf ("%s" ,buffer ); 236printf ("\n" ); 237 238switch (buffer [0 ]) { 239case 'G' : 240case 'g' :go ();break ; 241 242case 'M' : 243case 'm' : 244scanf ("%i %i" ,& start ,& stop ); 245mdump (dumpsim_file ,start ,stop ); 246break ; 247 248case '?' :help ();break ; 249case 'Q' : 250case 'q' :printf ("Bye.\n" );exit (0 ); 251 252case 'R' : 253case 'r' : 254if (buffer [1 ]== 'd' || buffer [1 ]== 'D' ) 255rdump (dumpsim_file ); 256else { 257scanf ("%d" ,& cycles ); 258run (cycles ); 259 } 260break ; 261 262default :printf ("Invalid Command\n" );break ; 263 } 264} 265 266/***************************************************************/ 267/* */ 268/* Procedure : init_memory */ 269/* */ 270/* Purpose : Zero out the memory array */ 271/* */ 272/***************************************************************/ 273void init_memory () { 274int i ; 275 276for (i = 0 ;i < WORDS_IN_MEM ;i ++ ) { 277MEMORY [i ][0 ]= 0 ; 278MEMORY [i ][1 ]= 0 ; 279 } 280} 281 282/**************************************************************/ 283/* */ 284/* Procedure : load_program */ 285/* */ 286/* Purpose : Load program and service routines into mem. */ 287/* */ 288/**************************************************************/ 289void load_program (char * program_filename ) { 290FILE * prog ; 291int ii ,word ,program_base ; 292 293/* Open program file. */ 294prog = fopen (program_filename ,"r" ); 295if (prog == NULL ) { 296printf ("Error: Can't open program file %s\n" ,program_filename ); 297exit (-1 ); 298 } 299 300/* Read in the program. */ 301if (fscanf (prog ,"%x\n" ,& word )!= EOF ) 302program_base = word >>1 ; 303else { 304printf ("Error: Program file is empty\n" ); 305exit (-1 ); 306 } 307 308ii = 0 ; 309while (fscanf (prog ,"%x\n" ,& word )!= EOF ) { 310/* Make sure it fits. */ 311if (program_base + ii >=WORDS_IN_MEM ) { 312printf ("Error: Program file %s is too long to fit in memory. %x\n" , 313program_filename ,ii ); 314exit (-1 ); 315 } 316 317/* Write the word to memory array. */ 318MEMORY [program_base + ii ][0 ]= word & 0x00FF ; 319MEMORY [program_base + ii ][1 ]= (word >>8 )& 0x00FF ; 320ii ++ ; 321 } 322 323if (CURRENT_LATCHES .PC == 0 ) 324CURRENT_LATCHES .PC = (program_base <<1 ); 325 326printf ("Read %d words from program into memory.\n\n" ,ii ); 327} 328 329/************************************************************/ 330/* */ 331/* Procedure : initialize */ 332/* */ 333/* Purpose : Load machine language program */ 334/* and set up initial state of the machine. */ 335/* */ 336/************************************************************/ 337void initialize (char * program_filename ,int num_prog_files ) { 338int i ; 339 340init_memory (); 341for (i = 0 ;i < num_prog_files ;i ++ ) { 342load_program (program_filename ); 343while (* program_filename ++ != '\0' ) 344 ; 345 } 346CURRENT_LATCHES .Z = 1 ; 347NEXT_LATCHES = CURRENT_LATCHES ; 348 349RUN_BIT = TRUE; 350} 351 352/***************************************************************/ 353/* */ 354/* Procedure : main */ 355/* */ 356/***************************************************************/ 357int main (int argc ,char * argv []) { 358FILE * dumpsim_file ; 359 360/* Error Checking */ 361if (argc < 2 ) { 362printf ("Error: usage: %s <program_file_1> <program_file_2> ...\n" , 363argv [0 ]); 364exit (1 ); 365 } 366 367printf ("LC-3b Simulator\n\n" ); 368 369initialize (argv [1 ],argc - 1 ); 370 371if ((dumpsim_file = fopen ("dumpsim" ,"w" ))== NULL ) { 372printf ("Error: Can't open dumpsim file\n" ); 373exit (-1 ); 374 } 375 376while (1 )get_command (dumpsim_file ); 377} 378 379// Nothing above this line has been modified 380 381int IR ; 382 383// Pull out bitfields from numbers 384#define FIELD (bits ,loc ,num ) \ 385 ((struct { unsigned x : (bits); }){.x = (num) >> loc}.x) 386 387// Pull out fields from an instruction 388#define R1 NEXT_LATCHES.REGS[FIELD(3, 9, IR)] 389#define R2 CURRENT_LATCHES.REGS[FIELD(3, 6, IR)] 390#define R3 CURRENT_LATCHES.REGS[FIELD(3, 0, IR)] 391#define A FIELD(1, 5, IR) 392#define D FIELD(1, 4, IR) 393#define SEXT_IMM (bits ) ((struct { int x : (bits); }){.x = IR}.x) 394#define PC_OFFSET (bits ) Low16bits(NEXT_LATCHES.PC + (SEXT_IMM(bits) << 1)) 395 396// Higher level operations 397void setcc (int num ) { 398NEXT_LATCHES .N = NEXT_LATCHES .Z = NEXT_LATCHES .P = 0 ; 399if (FIELD (1 ,15 ,num )) { 400NEXT_LATCHES .N = 1 ; 401 }else if (num ) { 402NEXT_LATCHES .P = 1 ; 403 }else { 404NEXT_LATCHES .Z = 1 ; 405 } 406} 407#define GET_WORD (addr ) (MEMORY[(addr) >> 1][0] | MEMORY[(addr) >> 1][1] << 8) 408#define PUT_WORD (addr ,num ) \ 409 (MEMORY[(addr) >> 1][0] = (num)&0xFF, \ 410 MEMORY[(addr) >> 1][1] = (num) >> 8 & 0xFF) 411#define GET_BYTE (addr ) Low16bits((char)(MEMORY[(addr) >> 1][(addr)&1])) 412#define PUT_BYTE (addr ,num ) (MEMORY[(addr) >> 1][(addr)&1] = (num)&0xFF) 413#define SAVE_RET () (NEXT_LATCHES.REGS[7] = CURRENT_LATCHES.PC + 2) 414#define SET_PC (num ) (NEXT_LATCHES.PC = num) 415#define CHECK_BRANCH () \ 416 (FIELD(1, 11, IR) & CURRENT_LATCHES.N || \ 417 FIELD(1, 10, IR) & CURRENT_LATCHES.Z || \ 418 FIELD(1, 9, IR) & CURRENT_LATCHES.P) 419#define DO_ARITHMETIC (op ) (R1 = Low16bits((R2 op(A ? SEXT_IMM(5) : R3)))) 420 421// clang-format off 422enum OP {BR ,ADD ,LDB ,STB ,JSR ,AND ,LDW ,STW ,RTI , 423XOR ,UNUSED1 ,UNUSED2 ,JMP ,SHF ,LEA ,TRAP }; 424 425void process_instruction () { 426NEXT_LATCHES .PC = Low16bits (CURRENT_LATCHES .PC + 2 ); 427switch (((IR = GET_WORD (CURRENT_LATCHES .PC ))& 0xF000 ) >>12 ) { 428case ADD :DO_ARITHMETIC (+ ),setcc (R1 );break ; 429case AND :DO_ARITHMETIC (& ),setcc (R1 );break ; 430case XOR :DO_ARITHMETIC (^),setcc (R1 );break ; 431case SHF :R1 = Low16bits (D ? (A ? (short )R2 :R2 ) >>FIELD (4 ,0 ,IR ) 432 :R2 <<FIELD (4 ,0 ,IR )),setcc (R1 );break ; 433case LDB :R1 = GET_BYTE (R2 + SEXT_IMM (6 )),setcc (R1 );break ; 434case LDW :R1 = GET_WORD (R2 + (SEXT_IMM (6 ) <<1 )),setcc (R1 );break ; 435case STB :PUT_BYTE (R2 + SEXT_IMM (6 ),R1 );break ; 436case STW :PUT_WORD (R2 + (SEXT_IMM (6 ) <<1 ),R1 );break ; 437case LEA :R1 = PC_OFFSET (9 );break ; 438case BR :SET_PC (CHECK_BRANCH () ?PC_OFFSET (9 ) :NEXT_LATCHES .PC );break ; 439case JMP :SET_PC (R2 );break ; 440case JSR :SET_PC (FIELD (1 ,11 ,IR ) ?PC_OFFSET (11 ) :R2 ),SAVE_RET ();break ; 441case TRAP :SET_PC (GET_WORD (FIELD (8 ,0 ,IR ))),SAVE_RET ();break ; 442case RTI :break ; 443default :break ; 444 } 445}