(--
    `types.balsa'
    Types and constants of MIPS R3000 model
--)
 
import [balsa.types.basic]
import [balsa.sim.fileio]
import [balsa.sim.string]
import [balsa.sim.memory]

type InsSize is 11 bits

type DataSize is 9 bits

type InsAndLine is record
	f : File;
	lineNo : InsSize
end 

type DataAndLine is record
	f : File;
	lineNo : DataSize
end 

type word is 32 bits

type Col is record
  ID  : bit;
  EX  : bit;
  MEM : bit
end

type Stage is enumeration PC, ID, MEM, EX end

type CIns is record
  Ins : word;
  C   : Col
end

--- FOR MAIM MEMORY

type InsAdd is record
  a : word;
  C : Col
end

type rNw is enumeration r, w end
type tNf is enumeration true,false end

type dNi is enumeration data, ins end

type DataType is enumeration 
  N, W, WL, WR, H, HU, B, BU
over 3 bits

type MemAdd is record
  a: word;
  dt: DataType;
  rNw: rNw
end

type MemWord is record
  w0: byte;
  w1: byte;
  w2: byte;
  w3: byte
end

-- FOR FORWARDING
type FWcase is enumeration
  non, EXER, MEMR, WBR
end

   
type FRACtrl is record
  EXEA: bit;
  MEMA: bit
end

type FWCtrl is record
  FWcase0: FWcase;
  FWcase1:FWcase
end 
    

--FOR CPU'S REGISTER 

type RegNo is 5 bits

type CP0Write is record
   rNw : rNw;
   RegNo: RegNo;
   RegWD: word
end

type RegRead is record
   RegRNo0: RegNo;
   RegRNo1: RegNo;
   RegWNo: RegNo
end

type Rd is RegNo

type RegWrite is record
   -- r is for reset here
   rNw : rNw;
   Rd:Rd;
   RegData: word
end

type CHRd is record
   RegData:word;
   Rd:Rd
end

-- FOR CONTROL HAZARD

type eNj is enumeration j, e end

type ch is record
   a   : word;
   eNj : eNj;
   St  : Stage;
   C   : Col
end 

type MEMch is record
   rNw : rNw;
   C   : Col 
end 

-- ENCODING OF THE OPCODE FIELD

type Opcode is enumeration
  SPECIAL, RTYPE=SPECIAL, REGIMM, J, JAL, BEQ, BNE, BLEZ, BGTZ, 
  ADDI, ADDIU, SLTI, SLTIU, ANDI, ORI, XORI, LUI,
  COP0, COP1, COP2, COP3, BEQL, BNEL, BLEZL, BGTZL,
  BETA0, BETA1, BETA2, BETA3, SPECIAL2, JALX, E, STAR0, 
  LB, LH, LWL, LW, LBU, LHU, LWR, BETA4, 
  SB, SH, SWL, SW, BETA5, BETA6, SWR, CACHE, 
  LL, LWC1, LWC2, PREF, BETA7, LDC1, LDC2, BETA8, 
  SC, SWC1, SWC2, STAR1, BETA9, SDC1, SDC2, BETA10 
over 6 bits

-- star are reserved for future use
-- BETA present a valid encoding for a higher-order MIPS ISA level.

-- ENCODING OF RTYPE OPCODE FUNCTION FIELD
-- USED ONE RESERVED CODE 000101 FOR INTERRUPT
type Funct is enumeration
   SLL, MOVCI, SRL, SRA, SLLV, INT, SRLV, SRAV, 
   JR, JALR, MOVZ, MOVN, SYSCALL, BREAK, STAR1, SYNC, 
   MFHI, MTHI, MFLO, MTLO, BETA0, STAR2, BETA1, BETA2, 
   MULT, MULTU, DIV, DIVU, BETA3, BETA4, BETA5, BETA6, 
   ADD, ADDU, SUB, SUBU, AND, OR, XOR, NOR, 
   STAR3, STAR4, SLT, SLTU, BETA7, BETA8, BETA9, BETA10,
   TGE, TGEU, TLT, TLTU, TEQ, STAR5, TNE, STAR6,
   BETA11, STAR7, BETA12, BETA13, BETA14,STAR8, BETA15, BETA16
over 6 bits

-- ENCODEING OF REGIMM RT FILED
type RtField is enumeration
   BLTZ, BGEZ, BLTZL, BGEZL, STAR0, STAR1, STAR2, STAR3, 
   TGEI,TGEIU, TLTI, TLTIU, TEQI, STAR4, TNEI, STAR5,
   BLTZAL, BGEZAL, BLTZALL, STAR6, STAR7, STAR8, SATR9
over 5 bits
 
-- INSTRUCTION TYPES

type R_TYPE is record
  funct: Funct;
  shamt: 5 bits;
  rd: RegNo;
  rt: RegNo;
  rs: RegNo;
  Op: Opcode
over word

type I_TYPE is record
   immediate: 16 bits;
   rt: RegNo;
   rs: RegNo;
   Op: Opcode
over word

type  J_TYPE is record
  target: 26 bits;
  Op: Opcode
over word

-- CONTROL SIGNALS

type AccType is enumeration
  read, write,imm, non
over 2 bits

type M is record
  AccType: AccType;
  DataType: DataType
end
  
type EX is enumeration
   -- ADD, SUB, ADDU, SUBU,
   -- AND, OR, XOR, NOR,   
   -- SLLV, SRLV, SRAV, NUN, 
   -- SLL, SRL, SRA, X2,  
   -- EXC, EXCS, SLTU,SLT,
   -- JR, JALR, JAL, MA,
   -- BEQ, BNE, BGTZ, BGEZAL,
   -- BLEZ, BLTZ, BGEZ, BLTZAL  
   BEQ, BNE, BGTZ,BLEZ, BLTZ, BLTZAL, BGEZ, BGEZAL, 
   X0, JR, JALR, JAL,X1, X2, X3, X4, 
   ADD, SUB, ADDU, SUBU, AND, OR, XOR, NOR, 
   EXC, EXCS, MA, COR, SLTU,SLT, X5, X6, 
   SLLV, SRLV, SRAV, NOP, SLL, SRL, SRA, X7, 
   X8, X9, XA, XB, XC, XD, XE, XF,
   MULTU, MULT, DIVU, DIV, MTHI, MTLO, MFHI, MFLO           
over 6 bits

--r is for reset
type wNe is enumeration 
    non, e, w, r
end

type cNp is enumeration c, p end

type WB is record
  wNe : wNe;
  cNp : cNp
end

type EXCtrl is record
   EX:EX;
   M: M;
   WB: WB
   -- C:Col
end 

type BaseAddEX is record
   Add : word;
   C   : Col
end

type MEMCtrl is record
   M: M;
   WB: WB;
   C:Col
end 

type shiftType is enumeration
   SLL, SRL, SRA, NUN1
over 2 bits

type shiftCom is record
   shiftType:shiftType; 
   shiftDist: 5 bits  -- shift distance  
end 

type shift is record
   shiftData: word;
   shiftCom : shiftCom
end
 

type LShiftB is record
  low_end:2 bits;
  high_end:30 bits
end

type LShiftJ is record
  low_end:2 bits;
  middle_end:26 bits;
  high_end:4 bits
end

type MulDiv is record
  Com : 3 bits;
  Op0: word;
  Op1: word
end
