All files / simulator index.ts

94.44% Statements 17/18
33.33% Branches 2/6
100% Functions 3/3
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                                                                                                      8x   8x   8x             8x 8x                 8x                                                                                                         7x   7x   7x 7x           7x   7x 7x 7x 7x 7x            
import {makeBinaryObject, makeBinaryArray} from '@src/simulator/assembler';
import {initialize, initializeMem} from '@src/utils/constants';
import {
  IMapDetail,
  mainProcess,
  makeMappingDetail,
  simulatorOutputType,
} from '@src/utils/functions';
 
export interface IAssemble {
  readonly output: string[];
  readonly mappingDetail: IMapDetail[] | null;
}
 
export interface ISimulatorOutput {
  readonly result: simulatorOutputType;
  history: simulatorOutputType[] | null;
}
 
export function assemble(
  assemblyInstructions: string[],
  mappingDetailRequest = false,
): IAssemble {
  /*  
   * input : assemblyInstructions: string[]
   * Enter the path where the assembly file is located.
   
   * If the assemlbyFile path is /Users/junghaejune/simulator/sample_input/example1.s
   * you can enter path /Users/junghaejune/simulator/sample_input/example1.s into assemblyInstructions.
   * If you don't know the path exactly, you can use makeInput function in functions.ts
   * Let your current directory is simulator (/Users/junghaejune/simulator).
   * Then you only put makeInput("sample_input", "example1.s") into assemblyInstructions
 
   * output: string[]
   * The assembly file is converted to a string in binary form.
    
    ex) 
    input: sample_input/example1.s
    ...
    main:
      and	$17, $17, $0 
      and	$18, $18, $0
    ...
    
    output:
    [00000010001000001000100000100100,
      ...
    00000010010000001001000000100100]
 
  */
 
  const {
    dataSectionSize,
    textSectionSize,
    binaryText,
    binaryData,
    mappingTable,
    dataSeg,
    textSeg,
  } = makeBinaryObject(assemblyInstructions);
 
  let mappingDetail: IMapDetail[] | null = null;
 
  const output: string[] = makeBinaryArray(
    dataSectionSize,
    textSectionSize,
    binaryText,
    binaryData,
  );
 
  Eif (mappingDetailRequest) {
    mappingDetail = makeMappingDetail(
      assemblyInstructions,
      dataSeg,
      textSeg,
      mappingTable,
      output,
    );
  }
 
  return {output, mappingDetail};
}
 
export async function simulator(
  assemblyInstructions: string[],
  cycleNum: number,
  returnHistory = false,
): Promise<ISimulatorOutput> {
  /*
   * input : assemblyInstructions: string[], cycle: number, returnCycles: boolean
   * assemblyInstructions is same as assemblyInstructions in assemble function above.
   * cycle is the number of cycles you want to execute.
   * Executing one cycle means that executing one instruction.
   * returnCycles determines the type of return.
   * If returnCycles = false (default), Returns only the final form of the result.
   * If returnCycles = true, Returns an object containing information of all cycles.
   
    ex) returnCycles = false, you can use this function as below form.
    const result = simulator(makeInput('sample_input', 'example1.s'), 10000, false)
 
    ex) returnCycles = true, you can use this function as below form.
    interface SimulatorResult {
      output: simulatorOutputType;
      cycles: simulatorOutputType[];
    }
 
    const result = simulator(
      makeInput('sample_input', 'example1.s'),
      10000,
      true,
    ) as SimulatorResult;
 
    * output : The object of Register File.
 
    ex)
    [
    {
      PC: '0x00400000',
      registers: {
        R0: '0x00000000',
        R1: '0x00000000',
        ...
        R31: '0x00000000'
      },
      dataSection: {
        '0x10000000': '0x00000064',
        ...
      },
      stackSection: {}
    } 
    ]
  */
  const {dataSectionSize, textSectionSize, binaryText, binaryData} =
    makeBinaryObject(assemblyInstructions);
 
  initializeMem();
 
  const CYCLES: simulatorOutputType[] = new Array<simulatorOutputType>();
  const INST_INFO = initialize(
    binaryText.concat(binaryData),
    textSectionSize,
    dataSectionSize,
  );
 
  const result = await mainProcess(INST_INFO, cycleNum, CYCLES);
 
  return new Promise<ISimulatorOutput>((resolve, reject) => {
    try {
      const output: ISimulatorOutput = {result, history: null};
      Eif (returnHistory) output.history = CYCLES;
      resolve(output);
    } catch (error) {
      reject(error);
    }
  });
}