void FPGARegisters::print_register_values() {
  printf("==============================\n");
  printf("Print %d FPGA Registers starting at: Ox%08X\n", size_/4, dev_base_);
  printf("Offset  Hex         Int\n");
  for(int i = 0; i < size_; i+=4)
  {
    printf("0x%04X: 0x%08X  %d\n", i, getRegisterValue(i), getRegisterValue(i));
  }
  printf("==============================\n");
}
Пример #2
0
void searchMemory(struct Processor *proc, char **tokens) {
  int start =0;
  int end = MEMORY_SIZE;
  if (!checkIfAllMemoryLocationsAreValid(tokens+1)) {
    printInvalidCommandMessage();
    return;
  }
  int value = atoi(tokens[1]);
  if (strcmp(tokens[0],"-r")==0) {
    start = getRegisterNumber(token[2]);
    end = getRegisterNumber(tokens[3]);
    if (start>end) {
      printInvalidCommandMessage();
      return;
    }
    if (tokens[4]!=NULL) {
       printInvalidCommandMessage();
       return;
    }
  } else if (strcmp(tokens[0],"-a")!=0) {
    printInvalidCommandMessage();
    return;
  }
  printf("\n(JVG)");
  for (int i = start; i<end ; i++) {
    if (proc->gpr[i]==value) {
      printf("M%i=%i  ",i,getRegisterValue(getRegisterNumber));
    }
  }
  printf("\n(JVG)");
}
Пример #3
0
/*
  This method prints the state of the processor at the end of execution of the
  program; i.e. the data stored in all the registers and the value of program
  counter at the end of the program
  @param proc : this specifies the processor whose state has to be printed
*/
void dumpProcessor(struct Processor *proc) {
  fprintf(stderr, "\n\n-----\n\nPC=%d\n", proc->pc);
  for(int i = 0; i < 4; i++) {
    for(int j = 0; j < 8; j++) {
       int reg = i * 8 + j;
       fprintf(stderr, "R%d=%d\t", reg, getRegisterValue(proc, reg));
    }
      fprintf(stderr, "\n");
   }
}
Пример #4
0
void printReg(struct Processor *proc , char **tokens) {
  int start = 0 ;
  int end = NUMBER_OF_REGISTERS-1;
  if (strcmp("-r",tokens[0])==0) {
    start = getRegisterNumber(tokens[1]);
    end   = getRegisterNumber(tokens[2]);
    if(!start<end && !checkAllRegistersAreValid(tokens+1)){
      printInvalidCommandMessage();
      return;
    }
  }
  else if (strcmp("-m",tokens[0])==0) {
    tokens++;
    if(!checkAllRegistersAreValid(tokens)){
      printInvalidCommandMessage();
      return;
    }
    int i =0;
    while(tokens[i]!=NULL) {
        printf("%s = %i \t",tokens[i],getRegisterValue(proc, getRegisterNumber(tokens[i])));
        i++;
      if (i%8==0) printf("      \n");
    }
    printf("\n (JVG)");    
  
  } 
  else if (!strcmp("-a",tokens[0])==0){
    printInvalidCommandMessage();
    return;
  } 
  printf( "\nstart- > %i   end -> %i\n",start, end);
  for (int i=start; i<end/8 ; i++) {     
    for (int j=0;j<8 ; j++) {
      printf("%s = %i \t",tokens[(i*j) + j],getRegisterValue(proc,getRegisterNumber(tokens[(i*j) + j])));
    }
    printf("      \n");
  }  
}
Пример #5
0
/**********************************************************
 * GetTemperature
 *  Gets the current temperature from the sensor.
 *
 * @return float - The local temperature in degrees C
 **********************************************************/
float LibTempTMP421::GetTemperature(void) {
    uint8_t in[2];
    float frac = 0.0;
    uint8_t bit;

    setPtrLoc(0x00);                //high-byte
    in[0] = getRegisterValue();

    setPtrLoc(0x10);                //low-byte
    in[1] = getRegisterValue();
    in[1] >>=4;                     //shift-off the unused bits

    /* Assemble the fraction */
    bit = in[1] & 0x01;
    frac += (bit * 0.5) * (bit * 0.5) * (bit * 0.5) * (bit * 0.5);

    in[1] >>= 1;
    bit = in[1] & 0x01;
    frac += (bit * 0.5) * (bit * 0.5) * (bit * 0.5);

    in[1] >>= 1;
    bit = in[1] & 0x01;
    frac += (bit * 0.5) * (bit * 0.5);

    in[1] >>= 1;
    bit = in[1] & 0x01;
    frac += (bit * 0.5);

    /* Add the MSB to the fraction */
    frac += in[0];

	/* frac is unsigned, make it signed to allow for negative temps */
	if (frac > 128.0)
		frac -= 256;
    return frac;
}
Пример #6
0
void searchRegister(struct Processor *proc, char **tokens) {
  int start =0;
  int end = NUMBER_OF_REGISTERS;
  if (!checkIfNumber(tokens[1])) {
    printInvalidCommandMessage();
    return;
  }
  int value = atoi(tokens[1]);
  
  if (!checkAllRegistersAreValid(tokens+1)) {
    printInvalidCommandMessage();
    return;
  }
  
  if (strcmp(tokens[0],"-r")==0) {
    start = getRegisterNumber(token[2]);
    end = getRegisterNumber(tokens[3]);
    if (start>end) {
      printInvalidCommandMessage();
      return;
    }
    if (tokens[4]!=NULL) {
       printInvalidCommandMessage();
       return;
    }
  } else if (strcmp(tokens[0],"-a")!=0) {
    printInvalidCommandMessage();
    return;
  }
  
  printf("\n(JVG)");
  for (int i = start; i<end ; i++) {
    if (proc->gpr[i]==value) {
      printf("$%i=%i  ",i,getRegisterValue(getRegisterNumber));
    }
  }
  printf("\n(JVG)");
}
Пример #7
0
unsigned int PinExecutionContext::getInstructionPointer(void **value) const {
	return getRegisterValue(EREG_INST_POINTER, (unsigned long *) value);
}
Пример #8
0
int carryOutInstruction(struct Processor *processor) {
  uint32_t instruction = getInstructionAtPC(processor);
  uint8_t opcode = getOpcode(instruction);
  uint32_t backupPC = processor->pc;
  int32_t temp;
  div_t division;

  switch (opcode)  {
    case HALT : return 0;
    case ADD  : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction)) + 
                  getRegisterValue(processor, getR3(instruction));
                break;
                
    case ADDI : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction)) + 
                  getImmediateValue(instruction);
                break;
               
    case SUB  : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction)) - 
                  getRegisterValue(processor, getR3(instruction));
                break;
                
    case SUBI : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction)) - 
                   getImmediateValue(instruction);
                 break;
                  
    case MUL  : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction)) * 
                  getRegisterValue(processor, getR3(instruction));
                break;
                  
    case MULI : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction)) * 
                  getImmediateValue(instruction);
                break;
                
    case LW   : processor->gpr[getR1(instruction)] = 
                getMemory(processor, 
                          getRegisterValue(processor, getR2(instruction)) + 
                                            getImmediateValue(instruction));
                break;
                  
    case SW   : setMemory(processor, getRegisterValue
                  (processor, getR2(instruction)) + 
                  getImmediateValue(instruction), 
                  getRegisterValue(processor, getR1(instruction)));
                break;
                
    case BEQ  : if (processor->gpr[getR1(instruction)] == 
                    processor->gpr[getR2(instruction)]) 
                  { processor->pc += getImmediateValue(instruction) * 4;};
                break;
                  
      case BNE  : if (processor->gpr[getR1(instruction)] != 
                      processor->gpr[getR2(instruction)]) 
                    { processor->pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BLT  : if (processor->gpr[getR1(instruction)] <
                      processor->gpr[getR2(instruction)])
                    { processor->pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BGT  : if (processor->gpr[getR1(instruction)] >
                     processor->gpr[getR2(instruction)]) 
                   { processor->pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BLE  : if (processor->gpr[getR1(instruction)] <= 
                      processor->gpr[getR2(instruction)]) 
                  { processor->pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BGE  : if (processor->gpr[getR1(instruction)] >= 
                      processor->gpr[getR2(instruction)]) 
                    { processor->pc += (getImmediateValue(instruction) * 4);};
                  break;
                  
      case JMP  : processor->pc = getAddress(instruction);
                  break;
                  
      case JR   : processor->pc = getRegisterValue
                    (processor, getR1(instruction));
                  break;
                  
      case JAL  : processor->gpr[31] = processor->pc + sizeof(uint32_t); 
                  processor->pc = getAddress(instruction);
                  break;
                  
      case OUT  : printf("%c", (char)getRegisterValue
                    (processor, getR1(instruction)));
                  break;
                  
      case DIV  : division = div(getRegisterValue(processor,getR2(instruction))
                              ,getRegisterValue(processor,getR3(instruction)));
                  processor->gpr[getR1(instruction)] = division.quot;
      
      case DIVI : division = div(getRegisterValue(processor,getR2(instruction))
                              ,getImmediateValue(instruction));
                  processor->gpr[getR1(instruction)] = division.quot;
      
      case MOD  : division = div(getRegisterValue(processor,getR2(instruction))
                              ,getRegisterValue(processor,getR3(instruction)));
                  processor->gpr[getR1(instruction)] = division.rem;
      
      case MODI : division = div(getRegisterValue(processor,getR2(instruction))
                              ,getImmediateValue(instruction));
                  processor->gpr[getR1(instruction)] = division.rem;
      
      case FACT : processor->gpr[getR1(instruction)] = 
                  getRegisterValue(processor, getR2(instruction));
                  for(int i = 1; i<processor->gpr[getR2(instruction)] ; i++) {
                    processor->gpr[getR1(instruction)] = 
                      (getRegisterValue(processor, getR1(instruction)))*
                      (getRegisterValue(processor, getR2(instruction))-i);};
                  break;
      
      case FACTI: processor->gpr[getR1(instruction)] = 
                  getImmediateValue(instruction);
                  for(int i = 1; i<getImmediateValue(instruction) ; i++) {
                    processor->gpr[getR1(instruction)] = 
                      (getRegisterValue(processor, getR1(instruction)))*
                      (getImmediateValue(instruction)-i);};
                  break;
      
      case SWAP : temp = 
                    getRegisterValue(processor, getR1(instruction));
                  processor->gpr[getR1(instruction)] = 
                    getRegisterValue(processor, getR2(instruction));
                  processor->gpr[getR2(instruction)] = temp;
                  break;
                  
      default   : printf("invalid opcode\n");
                  break;
    }
    
    if(processor->pc == backupPC)  processor->pc += sizeof(uint32_t);
    return 1;
}
Пример #9
0
/*
  This method carries out the execution of the binary code in the file specified
  by the arguments at run time. The file is loaded into the memory of the
  structure processor and then the execution is carried out in the while loop.
  The execution of terminates when an instruction with opcode HALT is reached.
  @param argv : this specifies the arguements which were given through the
                terminal when the program was run.
  @param argc : this specifes the number of arguments provided
  @return     : the method returns 0 when the method executes without any errors
*/
int main(int argc, char **argv) {
  assert("There are wrong number of arguents given" && argc==2);
  struct Processor processor;
  char *filepath = argv[1];

  memset(&processor, 0, sizeof(struct Processor));

  binaryFileLoader(filepath, &processor);
    
  while (getInstructionAtPC(&processor)) {
    uint32_t instruction = getInstructionAtPC(&processor);
    uint8_t opcode = getOpcode(instruction);
    uint32_t backupPC = processor.pc;
    int32_t temp;
    div_t division;

    switch (opcode)  {
      case ADD  : processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction)) + 
                    getRegisterValue(&processor, getR3(instruction));
                  break;
                  
      case ADDI : processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction)) + 
                    getImmediateValue(instruction);
                  break;
                 
      case SUB  : processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction)) - 
                    getRegisterValue(&processor, getR3(instruction));
                  break;
                  
      case SUBI : processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction)) - 
                    getImmediateValue(instruction);
                  break;
                  
      case MUL  : processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction)) * 
                    getRegisterValue(&processor, getR3(instruction));
                  break;
                  
      case MULI : processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction)) * 
                    getImmediateValue(instruction);
                  break;
                  
      case LW   : processor.gpr[getR1(instruction)] = 
                  getMemory(&processor, 
                            getRegisterValue(&processor, getR2(instruction)) + 
                                              getImmediateValue(instruction));
                  break;
                  
      case SW   : setMemory(&processor, getRegisterValue
                    (&processor, getR2(instruction)) + 
                    getImmediateValue(instruction), 
                    getRegisterValue(&processor, getR1(instruction)));
                  break;
                  
      case BEQ  : if (processor.gpr[getR1(instruction)] == 
                      processor.gpr[getR2(instruction)]) 
                    { processor.pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BNE  : if (processor.gpr[getR1(instruction)] != 
                      processor.gpr[getR2(instruction)]) 
                    { processor.pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BLT  : if (processor.gpr[getR1(instruction)] <
                      processor.gpr[getR2(instruction)])
                    { processor.pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BGT  : if (processor.gpr[getR1(instruction)] >
                     processor.gpr[getR2(instruction)]) 
                   { processor.pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BLE  : if (processor.gpr[getR1(instruction)] <= 
                      processor.gpr[getR2(instruction)]) 
                  { processor.pc += getImmediateValue(instruction) * 4;};
                  break;
                  
      case BGE  : if (processor.gpr[getR1(instruction)] >= 
                      processor.gpr[getR2(instruction)]) 
                    { processor.pc += (getImmediateValue(instruction) * 4);};
                  break;
                  
      case JMP  : processor.pc = getAddress(instruction);
                  break;
                  
      case JR   : processor.pc = getRegisterValue
                    (&processor, getR1(instruction));
                  break;
                  
      case JAL  : processor.gpr[31] = processor.pc + sizeof(uint32_t); 
                  processor.pc = getAddress(instruction);
                  break;
                  
      case OUT  : printf("%c", (char)getRegisterValue
                    (&processor, getR1(instruction)));
                  break;
                  
      case DIV  : division = div(getRegisterValue(&processor,getR2(instruction))
                              ,getRegisterValue(&processor,getR3(instruction)));
                  processor.gpr[getR1(instruction)] = division.quot;
      
      case DIVI : division = div(getRegisterValue(&processor,getR2(instruction))
                              ,getImmediateValue(instruction));
                  processor.gpr[getR1(instruction)] = division.quot;
      
      case MOD  : division = div(getRegisterValue(&processor,getR2(instruction))
                              ,getRegisterValue(&processor,getR3(instruction)));
                  processor.gpr[getR1(instruction)] = division.rem;
      
      case MODI : division = div(getRegisterValue(&processor,getR2(instruction))
                              ,getImmediateValue(instruction));
                  processor.gpr[getR1(instruction)] = division.rem;
      
      case FACT : processor.gpr[getR1(instruction)] = 
                  getRegisterValue(&processor, getR2(instruction));
                  for(int i = 1; i<processor.gpr[getR2(instruction)] ; i++) {
                    processor.gpr[getR1(instruction)] = 
                      (getRegisterValue(&processor, getR1(instruction)))*
                      (getRegisterValue(&processor, getR2(instruction))-i);};
                  break;
      
      case FACTI: processor.gpr[getR1(instruction)] = 
                  getImmediateValue(instruction);
                  for(int i = 1; i<getImmediateValue(instruction) ; i++) {
                    processor.gpr[getR1(instruction)] = 
                      (getRegisterValue(&processor, getR1(instruction)))*
                      (getImmediateValue(instruction)-i);};
                  break;
      
      case SWAP : temp = 
                    getRegisterValue(&processor, getR1(instruction));
                  processor.gpr[getR1(instruction)] = 
                    getRegisterValue(&processor, getR2(instruction));
                  processor.gpr[getR2(instruction)] = temp;
                  break;
                  
      default   : printf("invalid opcode\n");
                  break;
    }
    
    if(processor.pc == backupPC)
      processor.pc += sizeof(uint32_t);
  }
  
  fflush(stdout);

  dumpProcessor(&processor);

  return EXIT_SUCCESS;
}
void * startPhysicalModel(void * data) {
    
    int sts;
    struct timespec tm;
    modbus_mapping_t *mb_mapping = (modbus_mapping_t *)data;
    
    float tAmbient = AMBIENT_TEMP_DEG_F;
    float tHeater = HEATER_TEMP_DEG_F;
    bool heaterEnabled = false;
    bool heaterOn = HEATER_ON_STATE;
    float tHeaterTarget = HEATER_TARGET_TEMP_DEG_F;
    float tContHysteresis = HEATER_CONTROL_HYSTERESIS_DEG_F;
    float modelDelayTime_hrs = (float)(PHYSICAL_MODEL_DELAY_INTERVAL_MSEC)/
        (1000. * 60. * 60.);
    float tIncrease = 0.0;
    
    heaterEnabled = false;
    heaterOn = false;
    char * heaterEnableState;
    char * heaterElementState;
    char * displayFlag;
    bool displayCnt = true;
    
    if(data == NULL) {
        perror("Illegal modbus mapping struct.");
                exit(1);
    }
    
    while (1) {
        /* get current EPOCH time */
        //clock_gettime(CLOCK_REALTIME, &tm);
        
        usleep(PHYSICAL_MODEL_DELAY_INTERVAL_MSEC * 1000); 

        /* get any modbus registers that may have changed */
        pthread_mutex_lock(&modbusRegisterAccessLock);
        heaterEnabled = getRegisterBit(mb_mapping, HEATER_COIL_ENABLE);
        /* turn off heater if we are disabled */
        if(!heaterEnabled) {
            heaterOn = false;
        }
        tHeaterTarget = (float)(getRegisterValue(mb_mapping, 
                HEATER_WATER_TARGET_TEMP_REG)) / 10.0;
        pthread_mutex_unlock(&modbusRegisterAccessLock);
        
        /* update the model state and, if necessary, update
         modbus registers */
        tIncrease = 0.0;
        if(heaterOn) {
            /* calculate temp increase if heater is on */
            tIncrease = (HEATER_KW * HEATING_CONST) * modelDelayTime_hrs 
                * TIME_SCALING_FACTOR;
        }
        float tCoolDown = tAmbient +            
            (tHeater - tAmbient) * exp(COOLING_CONST * modelDelayTime_hrs
            * TIME_SCALING_FACTOR);
        /* water temp is sum of + and - temperature deltas*/
        tHeater = tCoolDown + tIncrease;
        
        /* update the controller state */
        if(heaterEnabled) {
            if(heaterOn) {
                if(tHeater > (tHeaterTarget + tContHysteresis)) {
                    heaterOn = false;
                }
            }
            else {
                if(tHeater < (tHeaterTarget - tContHysteresis)) {
                    heaterOn = true;
                }
            }
        }
        
        /* lock context and transfer values from the model.  Then unlock.*/
        pthread_mutex_lock(&modbusRegisterAccessLock);
        setInputRegisterValue(mb_mapping, HEATER_WATER_TEMP_REG,
            (short)(tHeater * 10.0));
        setInputRegisterBit(mb_mapping, HEATER_COIL_ENERGIZED, heaterOn);
        pthread_mutex_unlock(&modbusRegisterAccessLock);
        
        if(heaterEnabled) {
            heaterEnableState = "Heater Enabled, ";
        }
        else {
            heaterEnableState = "Heater Disabled, ";
        }
        
        if(heaterOn) {
            heaterElementState = "Heater Element On, ";
        }
        else {
            heaterElementState = "Heater Element Off, ";
        }
        
        if (displayCnt) {
            displayCnt = false;
            displayFlag = "+";
        }
        else {
            displayCnt = true;
            displayFlag = "-";
        }
        
        printf("Model Status(%s): %s%sSet Point: %5.2f, Temp: %5.2f\n",
                displayFlag,
                heaterEnableState, heaterElementState, 
                tHeaterTarget, tHeater);
    }
}