All files / simulator/src/utils constants.ts

96.66% Statements 116/120
80% Branches 4/5
85.71% Functions 12/14
96.58% Lines 113/117

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329                                                                                                5x 5x   5x 5x 5x 5x 5x 5x   5x 5x   5x 5x 5x 5x 5x 5x   5x 5x                         5x   5x           5x               5x 5x 5x 5x   5x                 135x 135x 135x 135x                 531x 531x                                         14x 448x 14x                                   264x 264x 264x 264x 264x 264x 264x 264x 264x 264x 264x                 7x     7x 7x   7x     132x 7x   7x 151x 132x 132x 19x 19x   151x     7x   7x 7x                             42x 42x 42x 42x 42x 42x         5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x                                                             5x   5x   5x 32x     5x 14x 14x 14x 14x 14x         14x 14x       5x 30334x     5x       5x 4x    
import {
  initMemory,
  initInstInfo,
  parseData,
  parseInstr,
} from '@utils/functions';
 
export interface Iinstruction {
  opcode: number;
  funcCode: number;
  value: number;
  target: number;
  rs: number;
  rt: number;
  imm: number;
  rd: number;
  shamt: number;
  encoding: number;
  expr: number;
}
 
interface IBcolors {
  readonly BLUE: string;
  readonly YELLOW: string;
  readonly GREEN: string;
  readonly RED: string;
  readonly ENDC: string;
}
 
interface ISection {
  readonly DATA: number;
  readonly TEXT: number;
  readonly MAX_SIZE: number;
}
 
export interface SymbolTableType {
  readonly name: string;
  readonly address: number;
}
 
export interface IinstList {
  readonly [key: string]: instT;
}
 
export interface ISYMBOL_TABLE {
  [key: string]: number;
}
 
export const DEBUG = 0;
export const MAX_SYMBOL_TABLE_SIZE = 1024;
 
export const MEM_TEXT_START = 0x00400000;
export const MEM_TEXT_SIZE = 0x00100000;
export const MEM_DATA_START = 0x10000000;
export const MEM_DATA_SIZE = 0x00100000;
export const MEM_STACK_START = 0x80000000;
export const MEM_STACK_SIZE = 0x00100000;
 
export const BYTES_PER_WORD = 4;
export const INST_LIST_LEN = 27;
 
export const MIPS_REGS = 32;
export const MEM_GROW_UP = 1;
export const MEM_GROW_DOWN = -1;
export const MEM_NREGIONS = 3;
export const DEBUG_SET = 0;
export const MEM_DUMP_SET = 1;
 
export let RUN_BIT = 1;
export let INSTRUCTION_COUNT = 0;
 
/*
  Main memory
  memory will be dynamically allocated at initialization
*/
export let memText: memRegionT;
export let memData: memRegionT;
export let memStack: memRegionT;
export let memRegions: memRegionT[];
export let currentState: cpuState;
export let NUM_INST: number;
 
export const INST_INFO: Iinstruction[] = [];
 
export const section: ISection = {
  DATA: 0,
  TEXT: 1,
  MAX_SIZE: 2,
};
 
export const bcolors: IBcolors = {
  BLUE: '\x1B[34m',
  YELLOW: '\x1B[33m',
  GREEN: '\x1B[32m',
  RED: '\x1B[31m',
  ENDC: '\x1B[0m',
};
 
const start = `[${bcolors.BLUE}START${bcolors.ENDC}]  `;
const done = `[${bcolors.YELLOW}DONE${bcolors.ENDC}]  `;
const success = `[${bcolors.GREEN}SUCCESS${bcolors.ENDC}]  `;
const error = `[${bcolors.RED}ERROR${bcolors.ENDC}]  `;
 
export const pType: string[] = [start, done, success, error];
// Structure Declaration
export class instT {
  name: string;
  type: string;
  op: string;
  funct: string;
 
  constructor(name: string, op: string, type: string, funct: string) {
    this.name = name;
    this.op = op;
    this.type = type;
    this.funct = funct;
  }
}
 
export class symbolT {
  name: string;
  address: number;
 
  constructor() {
    this.name = '';
    this.address = 0;
  }
}
 
export class laStruct {
  op: string;
  rt: string;
  imm: string;
 
  constructor(op: string, rt: string, imm: string) {
    this.op = op;
    this.rt = rt;
    this.imm = imm;
  }
}
 
export class cpuState {
  PC: number;
  REGS: number[];
 
  constructor() {
    this.PC = 0;
    this.REGS = Array.from({length: 32}, () => 0);
    this.REGS[29] = MEM_STACK_START;
  }
}
 
export class instruction {
  opcode: number;
  funcCode: number;
  value: number;
  target: number;
  rs: number;
  rt: number;
  imm: number;
  rd: number;
  shamt: number;
  encoding: number;
  expr: number;
 
  constructor() {
    this.opcode = 0;
    this.funcCode = 0;
    this.value = 0;
    this.target = 0;
    this.rs = 0;
    this.rt = 0;
    this.imm = 0;
    this.rd = 0;
    this.shamt = 0;
    this.encoding = 0;
    this.expr = 0;
  }
}
 
export function initialize(
  binary: string[],
  textSize: number,
  dataSize: number,
): Iinstruction[] {
  initMemory();
 
  // Load program and service routines into mem
  let textIndex = 0;
  let size = 0;
 
  NUM_INST = ~~(textSize / 4);
 
  // initial memory allocation of text segment
  for (let i = 0; i < NUM_INST; i++) INST_INFO.push(new instruction());
  initInstInfo(NUM_INST, INST_INFO);
 
  binary.forEach(buffer => {
    if (size < textSize) {
      INST_INFO[textIndex] = parseInstr(buffer, size);
      textIndex += 1;
    } else Eif (size < textSize + dataSize) {
      parseData(buffer, size - textSize);
    }
    size += 4;
  });
 
  currentState.PC = MEM_TEXT_START;
 
  RUN_BIT = 1;
  return INST_INFO;
}
/*
  All simulated memory will be managed by this class
  use the mem_write and mem_read functions to
  access/modify the simulated memory
*/
export class memRegionT {
  start: number;
  size: number;
  mem: number[];
  offBound: number;
  type: number;
  dirty: boolean;
  constructor(start: number, size: number, type: number = MEM_GROW_UP) {
    this.start = start;
    this.size = size;
    this.mem = [];
    this.offBound = -(size - 4) * type;
    this.type = type;
    this.dirty = false;
  }
}
 
// Global Variable Declaration
const SLL = new instT('sll', '000000', 'R', '000000');
const SRL = new instT('srl', '000000', 'R', '000010');
const JR = new instT('jr', '000000', 'R', '001000');
const ADD = new instT('add', '000000', 'R', '100000');
const ADDU = new instT('addu', '000000', 'R', '100001');
const AND = new instT('and', '000000', 'R', '100100');
const NOR = new instT('nor', '000000', 'R', '100111');
const OR = new instT('or', '000000', 'R', '100101');
const SLT = new instT('slt', '000000', 'R', '101010');
const SLTU = new instT('sltu', '000000', 'R', '101011');
const SUB = new instT('sub', '000000', 'R', '100010');
const SUBU = new instT('subu', '000000', 'R', '100011');
const LUI = new instT('lui', '001111', 'I', '');
const BEQ = new instT('beq', '000100', 'I', '');
const BNE = new instT('bne', '000101', 'I', '');
const LW = new instT('lw', '100011', 'I', '');
const LHU = new instT('lhu', '100101', 'I', '');
const SW = new instT('sw', '101011', 'I', '');
const SH = new instT('sh', '101001', 'I', '');
const ADDI = new instT('addi', '001000', 'I', '');
const ADDIU = new instT('addiu', '001001', 'I', '');
const ANDI = new instT('andi', '001100', 'I', '');
const ORI = new instT('ori', '001101', 'I', '');
const SLTI = new instT('slti', '001010', 'I', '');
const SLTIU = new instT('sltiu', '001011', 'I', '');
const J = new instT('j', '000010', 'J', '');
const JAL = new instT('jal', '000011', 'J', '');
 
export const instList: IinstList = {
  add: ADD,
  addi: ADDI,
  addiu: ADDIU,
  addu: ADDU,
  and: AND,
  andi: ANDI,
  beq: BEQ,
  bne: BNE,
  j: J,
  jal: JAL,
  jr: JR,
  lhu: LHU,
  lui: LUI,
  lw: LW,
  nor: NOR,
  or: OR,
  ori: ORI,
  slt: SLT,
  slti: SLTI,
  sltiu: SLTIU,
  sltu: SLTU,
  sll: SLL,
  srl: SRL,
  sh: SH,
  sw: SW,
  sub: SUB,
  subu: SUBU,
};
 
// Global symbol table
export const symbolStruct = new symbolT();
 
export let SYMBOL_TABLE: ISYMBOL_TABLE = {};
 
export const resetSymbolTable = () => {
  SYMBOL_TABLE = {};
};
 
export const initializeMem = () => {
  INSTRUCTION_COUNT = 0;
  RUN_BIT = 1;
  memText = new memRegionT(MEM_TEXT_START, MEM_TEXT_SIZE);
  memData = new memRegionT(MEM_DATA_START, MEM_DATA_SIZE);
  memStack = new memRegionT(
    MEM_STACK_START - MEM_STACK_SIZE,
    MEM_STACK_SIZE,
    MEM_GROW_DOWN,
  );
  memRegions = [memText, memData, memStack];
  currentState = new cpuState();
};
 
/* INSTRUCTION COUNT ADD */
export const instAddOne = () => {
  INSTRUCTION_COUNT += 1;
};
 
export const numInstSub = () => {
  NUM_INST -= 1;
};
 
export const changeRunBit = () => {
  RUN_BIT = 0;
};