Beispiel #1
1
//----------------------------------
//
// your solution to this question should look something like this
//
//  if you run userprog3.dlx.obj 2, it should output
//
//	A0
//	B0
//	A1
//	A2
//	A3
//	A4
//	A5
//	A6
//	B1
//	A7
//	A8
//	A9
//	B2
//	A10
//	B3
//	A11
//	B4
//	A12
//	B5
//	A13
//	B6
//	A14
//	B7
//	A15
//	B8
//	A16
//	B9
//	A17
//	B10
//	A18
//	B11
//	A19
//	B12
//	A20
//	B13
//	A21
//	B14
//	A22
//	B15
//	A23
//	B16
//	A24
//	B17
//	A25
//	B18
//	A26
//	B19
//	A27
//	B20
//	A28
//	B21
//	A29
//	B22
//	A0
//	B23
//	A1
//	B24
//	A2
//	B25
//	A3
//	B26
//	A4
//	B27
//	A5
//	B28
//	A6
//	B29
//	A7
//	B0
//	A8
//	B1
//	A9
//	B2
//	A10
//	B3
//	A11
//	B4
//	A12
//	B5
//	A13
//	B6
//	A14
//	B7
//	A15
//	B8
//	A16
//	B9
//	A17
//	B10
//	A18
//	B11
//	A19
//	B12
//	A20
//	B13
//	A21
//	B14
//	A22
//	B15
//	A23
//	B16
//	A24
//	B17
//	A25
//	B18
//	A26
//	B19
//	A27
//	B20
//	A28
//	B21
//	A29
//	B22
//	B23
//	B24
//	B25
//	B26
//	B27
//	B28
//	B29
// make sure you understand the scheduling algorithm works b4 you strart hacking
//
//   You are free to modify this file (and other userprogs) to test your solution
//   We will not use the same files to test your code.
//
//----------------------------------------------------------------------------
main (int argc, char *argv[])
{
  int number, i,j,offset;
  uint32 handle;
  sem_t semaphore;
  char num_str[10], semaphore_str[10];

  Printf("\n\n**** argc = %d\n\n\n", argc);

  switch(argc)
  {
    case 2:
    Printf("timer = %d\n", TimerGet());
      number = dstrtol(argv[1], NULL, 10);
      Printf("Setting number = %d\n", number);

      for(i=0; i<50; i++)
      	Printf("1");

	 Printf("timer = %d\n", TimerGet());


      semaphore = sem_create(1);
      ditoa(semaphore, semaphore_str);	//Convert the semaphore to a string

      Printf("timer = %d\n", TimerGet());

      for(i=0; i<number; i++)
      {
        ditoa(i, num_str);
        process_create(i,0,"userprog4.dlx.obj", num_str,semaphore_str,
  			NULL);
      }
      Printf("timer = %d\n", TimerGet());
      yield();
      break;
    case 3:
      offset = dstrtol(argv[1], NULL, 10);       //Get semaphore
      semaphore = dstrtol(argv[2], NULL, 10);       //Get semaphore
      for(i=0;i<30;i++)
      {
        for(j=0;j<50000;j++);
        Printf("%c%d\n",'A'+offset,i);
      }
      for(i=0;i<30;i++)
      {
        sem_wait(semaphore);
        for(j=0;j<50000;j++);
        Printf("%c%d\n",'A'+offset,i);
        sem_signal(semaphore);
      }

      break;
    default:
      Printf("Usage: ");
      Printf(argv[0]);
      Printf(" number\n");
      Printf("argc = %d\n", argc);
      exit();
  }



}
Beispiel #2
0
static void ProfilerLuaEvent(lua_State* L, lua_Debug* ar)
{
  Invocation* i = s_Profiler.m_CurrentInvocation;

  // Update the time for this leaf, which will now go away.
  const uint64_t now = TimerGet();
  i->m_Ticks += now - i->m_StartTick;

  if (LUA_HOOKCALL == ar->event)
  {
    // We're calling a new function.
    // Figure out what function it is.

    FunctionMeta* f = FindFunction(L, ar);

    s_Profiler.m_CurrentInvocation = PushInvocation(f);
    s_Profiler.m_CurrentInvocation->m_CallCount += 1;
  }
  else
  {
    // We're returning to our parent.
    s_Profiler.m_CurrentInvocation = i->m_Parent;
  }

  // Start counting from here on out. This is the last thing we do, so time
  // spent inside this hook will not be counted.
  s_Profiler.m_CurrentInvocation->m_StartTick = TimerGet();
}
Beispiel #3
0
/****************************************************************************************
** NAME:           BackDoorCheck
** PARAMETER:      none
** RETURN VALUE:   none
** DESCRIPTION:    The default backdoor entry feature keeps the bootloader active for a
**                 predetermined time after reset, allowing the host application to
**                 establish a connection and start a programming sequence. This function
**                 controls the opening/closing of the backdoor.
**
****************************************************************************************/
void BackDoorCheck(void)
{
#if (BOOT_BACKDOOR_HOOKS_ENABLE == 0)
  #if (BOOT_COM_ENABLE > 0)
  /* check if a connection with the host was already established. in this case the
   * backdoor stays open anyway, so no need to check if it needs to be closed. 
   */
  if (ComIsConnected() == BLT_TRUE)
  {
    return;
  }
  #endif
  
  /* when the backdoor is still open, check if it's time to close it */
  if (backdoorOpen == BLT_TRUE)
  {
    /* check if the backdoor entry time window elapsed */
    if (TimerGet() >= (BACKDOOR_ENTRY_TIMEOUT_MS + backdoorOpenTime))
    {
      /* close the backdoor */
      backdoorOpen = BLT_FALSE;
      /* this function does not return if a valid user program is present */
      CpuStartUserProgram();
    }
  }
#endif
} /*** end of BackDoorCheck ***/
Beispiel #4
0
/************************************************************************************//**
** \brief     Toggles the LED at a fixed time interval.
** \return    none.
**
****************************************************************************************/
void LedToggle(void)
{
  static unsigned char led_toggle_state = 0;
  static unsigned long timer_counter_last = 0;
  unsigned long timer_counter_now;

  /* check if toggle interval time passed */
  timer_counter_now = TimerGet();
  if ( (timer_counter_now - timer_counter_last) < LED_TOGGLE_MS)
  {
    /* not yet time to toggle */
    return;
  }
  
  /* determine toggle action */
  if (led_toggle_state == 0)
  {
    led_toggle_state = 1;
    /* turn the LED on */
    GPIO_ResetBits(GPIOC, GPIO_Pin_12);
  }
  else
  {
    led_toggle_state = 0;
    /* turn the LED off */
    GPIO_SetBits(GPIOC, GPIO_Pin_12);
  }

  /* store toggle time to determine next toggle interval */
  timer_counter_last = timer_counter_now;
} /*** end of LedToggle ***/
Beispiel #5
0
/************************************************************************************//**
** \brief     Toggles the LED at a fixed time interval.
** \return    none.
**
****************************************************************************************/
void LedToggle(void)
{
  static unsigned char led_toggle_state = 0;
  static unsigned long timer_counter_last = 0;
  unsigned long timer_counter_now;

  /* check if toggle interval time passed */
  timer_counter_now = TimerGet();
  if ( (timer_counter_now - timer_counter_last) < LED_TOGGLE_MS)
  {
    /* not yet time to toggle */
    return;
  }
  
  /* determine toggle action */
  if (led_toggle_state == 0)
  {
    led_toggle_state = 1;
    /* turn the LED on */
    LCD_Symbol(LCD, LCD_SYMBOL_OLIMEX, 1);
  }
  else
  {
    led_toggle_state = 0;
    /* turn the LED off */
    LCD_Symbol(LCD, LCD_SYMBOL_OLIMEX, 0);
  }

  /* store toggle time to determine next toggle interval */
  timer_counter_last = timer_counter_now;
} /*** end of LedToggle ***/
Beispiel #6
0
int main(void)
{
  Timer_t timer;                // Create our timer object!
  Led_t led;                    // Create the LED Object on P1.0! 

  /* Watchdog timer disabled */
  WDTCTL = WDTPW + WDTHOLD;
  
  BspInit();    // Set up
  
  (void) TimerMasterInit(100); //100 *10us = 1ms
  
  TimerCtor(&timer);  
  LedCtor(&led, kPort1, kPin0); //P1.0 (red)

  __eint();        // Enable global interrupts
  while(true)
  {
    if (TimerGet(&timer)>100)  // 0.2s period (1ms*20)
    {
      LedToggle(&led);
      TimerReset(&timer);
    }
  }
}
Beispiel #7
0
TriStatus freettcn::PA::CPlatformAdaptor::TimerStop(const TriTimerId *timerId)
{
  if (!timerId)
    return TRI_ERROR;
  
  try {
    freettcn::PA::CTimer &timer = TimerGet(*timerId);
    timer.Stop();
    
    if (Logging() && LogMask().Get(freettcn::CLogMask::CMD_PA_T_STOP)) {
      TriComponentId comp = { { 0 } };
      tliTStop(0, TimeStampMgr().Get(), 0, 0, comp, *timerId, timer.Read());
    }

    return TRI_OK;
  }
  catch(freettcn::Exception &) {
    return TRI_ERROR;
  }
}
Beispiel #8
0
TriStatus freettcn::PA::CPlatformAdaptor::TimerRunning(const TriTimerId* timerId, unsigned char* running)
{
  if (!timerId)
    return TRI_ERROR;
  
  if (!running)
    return TRI_ERROR;
  
  try {
    freettcn::PA::CTimer &timer = TimerGet(*timerId);
    *running = timer.Running();

    if (Logging() && LogMask().Get(freettcn::CLogMask::CMD_PA_T_RUNNING)) {
      TriComponentId comp = { { 0 } };
      tliTRunning(0, TimeStampMgr().Get(), 0, 0, comp, *timerId, *running ? runningT : expiredT);
    }
  }
  catch(freettcn::Exception &) {
    return TRI_ERROR;
  }
  
  return TRI_OK;
}
Beispiel #9
0
TriStatus freettcn::PA::CPlatformAdaptor::TimerRead(const TriTimerId* timerId, TriTimerDuration* elapsedTime)
{
  if (!timerId)
    return TRI_ERROR;
  
  if (!elapsedTime)
    return TRI_ERROR;
  
  try {
    freettcn::PA::CTimer &timer = TimerGet(*timerId);
    *elapsedTime = timer.Read();
    
    if (Logging() && LogMask().Get(freettcn::CLogMask::CMD_PA_T_READ)) {
      TriComponentId comp = { { 0 } };
      tliTRead(0, TimeStampMgr().Get(), 0, 0, comp, *timerId, *elapsedTime);
    }
    
    return TRI_OK;
  }
  catch(freettcn::Exception &) {
    return TRI_ERROR;
  }
}
Beispiel #10
0
/****************************************************************************************
** NAME:           BackDoorInit
** PARAMETER:      none
** RETURN VALUE:   none
** DESCRIPTION:    Initializes the backdoor entry option.
**
****************************************************************************************/
void BackDoorInit(void)
{
#if (BOOT_BACKDOOR_HOOKS_ENABLE > 0)
  /* initialize application's backdoor functionality */
  BackDoorInitHook();
  
  /* attempt to start the user program when no backdoor entry is requested or
   * a pending COM module connection request.
   */
  if ( (BackDoorEntryHook() == BLT_FALSE) && (ComIsConnectEntryState() == BLT_FALSE) )
  {
    /* this function does not return if a valid user program is present */
    CpuStartUserProgram();
  }
  
#else
  /* open the backdoor after a reset */
  backdoorOpen = BLT_TRUE;
  backdoorOpenTime = TimerGet();
#endif
  /* perform the first check that open/closes the backdoor */
  BackDoorCheck();
} /*** end of BackDoorInit ***/
Beispiel #11
0
void LuaProfilerInit(MemAllocHeap* heap, MemAllocLinear* alloc, lua_State* L)
{
  LuaProfilerState* self = &s_Profiler;

  memset(self, 0, sizeof *self);

  self->m_LuaState            = L;
  self->m_Heap                = heap;
  self->m_Allocator           = alloc;

  HashTableInit(&self->m_Functions, heap, 0);

  RehashInvocationTable(1024);

  s_TopLevel.m_Hash   = 0;
  s_TopLevel.m_String = "toplevel;global;;0";
  s_TopLevel.m_Next   = nullptr;

  s_Profiler.m_CurrentInvocation = PushInvocation(&s_TopLevel);
  s_Profiler.m_CurrentInvocation->m_StartTick = TimerGet();

  // Install debug hook.
  lua_sethook(L, ProfilerLuaEvent, LUA_MASKCALL|LUA_MASKRET, 0);
}
Beispiel #12
0
void CancelKernelTimer (unsigned hTimer)
{
	TimerCancelKernelTimer (TimerGet (), hTimer);
}
Beispiel #13
0
unsigned StartKernelTimer (unsigned nDelay, TKernelTimerHandler *pHandler, void *pParam, void *pContext)
{
	return TimerStartKernelTimer (TimerGet (), nDelay, pHandler, pParam, pContext);
}
Beispiel #14
0
void usDelay (unsigned nMicroSeconds)
{
	TimerusDelay (TimerGet (), nMicroSeconds);
}
Beispiel #15
0
void MsDelay (unsigned nMilliSeconds)
{
	TimerMsDelay (TimerGet (), nMilliSeconds);
}
Beispiel #16
0
//----------------------------------------------------------------------
//
//	doInterrupt
//
//	Handle an interrupt or trap.
//
//----------------------------------------------------------------------
void
dointerrupt (unsigned int cause, unsigned int iar, unsigned int isr,
	     uint32 *trapArgs)
{
  int	result;
  int	i;
  uint32	args[4];
  int	intrs;
  uint32 handle;
  int ihandle;

  dbprintf ('t',"Interrupt cause=0x%x iar=0x%x isr=0x%x args=0x%08x.\n",
	    cause, iar, isr, trapArgs);
  // If the TRAP_INSTR bit is set, this was from a trap instruction.
  // If the bit isn't set, this was a system interrupt.
  if (cause & TRAP_TRAP_INSTR) {
    cause &= ~TRAP_TRAP_INSTR;
    switch (cause) {
    case TRAP_CONTEXT_SWITCH:
      dbprintf ('t', "Got a context switch trap!\n");
      ProcessSchedule ();
      break;
    case TRAP_EXIT:
      dbprintf ('t', "Got an exit trap!\n");
      ProcessDestroy (currentPCB);
      ProcessSchedule ();
      break;
    case TRAP_PROCESS_FORK:
      dbprintf ('t', "Got a fork trap!\n");
      break;
    case TRAP_PROCESS_SLEEP:
      dbprintf ('t', "Got a process sleep trap!\n");
      ProcessSuspend (currentPCB);
      ProcessSchedule ();
      break;
    case TRAP_PRINTF:
      // Call the trap printf handler and pass the arguments and a flag
      // indicating whether the trap was called from system mode.
      dbprintf ('t', "Got a printf trap!\n");
      TrapPrintfHandler (trapArgs, isr & DLX_STATUS_SYSMODE);
      break;
    case TRAP_OPEN:
      // Get the arguments to the trap handler.  If this is a user mode trap,
      // copy them from user space.
      if (isr & DLX_STATUS_SYSMODE) {
	args[0] = trapArgs[0];
	args[1] = trapArgs[1];
      } else {
	char	filename[32];
	// trapArgs points to the trap arguments in user space.  There are
	// two of them, so copy them to to system space.  The first argument
	// is a string, so it has to be copied to system space and the
	// argument replaced with a pointer to the string in system space.
	MemoryCopyUserToSystem (currentPCB, trapArgs, args, sizeof(args[0])*2);
	MemoryCopyUserToSystem (currentPCB, args[0], filename, 31);
	// Null-terminate the string in case it's longer than 31 characters.
	filename[31] = '\0';
	// Set the argument to be the filename
	args[0] = (uint32)filename;
      }
      // Allow Open() calls to be interruptible!
      intrs = EnableIntrs ();
      ProcessSetResult (currentPCB, args[1] + 0x10000);
      printf ("Got an open with parameters ('%s',0x%x)\n", args[0], args[1]);
      RestoreIntrs (intrs);
      break;
    case TRAP_CLOSE:
      // Allow Close() calls to be interruptible!
      intrs = EnableIntrs ();
      ProcessSetResult (currentPCB, -1);
      RestoreIntrs (intrs);
      break;
    case TRAP_READ:
      // Allow Read() calls to be interruptible!
      intrs = EnableIntrs ();
      ProcessSetResult (currentPCB, -1);
      RestoreIntrs (intrs);
      break;
    case TRAP_WRITE:
      // Allow Write() calls to be interruptible!
      intrs = EnableIntrs ();
      ProcessSetResult (currentPCB, -1);
      RestoreIntrs (intrs);
      break;
    case TRAP_DELETE:
      intrs = EnableIntrs ();
      ProcessSetResult (currentPCB, -1);
      RestoreIntrs (intrs);
      break;
    case TRAP_SEEK:
      intrs = EnableIntrs ();
      ProcessSetResult (currentPCB, -1);
      RestoreIntrs (intrs);
      break;
    case TRAP_PROCESS_CREATE:
      TrapProcessCreateHandler(trapArgs, isr & DLX_STATUS_SYSMODE);
      break;
    case TRAP_PROCESS_YIELD:
      Yield();
      //ProcessSetResult(currentPCB, -1);
      break;
    case TRAP_SHARE_CREATE_PAGE:
      handle = MemoryCreateSharedPage(currentPCB);
      ProcessSetResult(currentPCB, handle);
      break;
    case TRAP_SHARE_MAP_PAGE:
      handle = GetUintFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      handle = (uint32)mmap(currentPCB, handle);
      ProcessSetResult(currentPCB, handle);
      break;
    case TRAP_SEM_CREATE:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      ihandle = SemCreate(ihandle);
      ProcessSetResult(currentPCB, ihandle); //Return handle
      break;
    case TRAP_SEM_WAIT:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      handle = SemHandleWait(ihandle);
      ProcessSetResult(currentPCB, handle); //Return 1 or 0
      break;
    case TRAP_SEM_SIGNAL:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      handle = SemHandleSignal(ihandle);
      ProcessSetResult(currentPCB, handle); //Return 1 or 0
      break;
    case TRAP_LOCK_CREATE:
      ihandle = LockCreate();
      ProcessSetResult(currentPCB, ihandle); //Return handle
      break;
    case TRAP_LOCK_ACQUIRE:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      handle = LockHandleAcquire(ihandle);
      ProcessSetResult(currentPCB, handle); //Return 1 or 0
      break;
    case TRAP_LOCK_RELEASE:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      handle = LockHandleRelease(ihandle);
      ProcessSetResult(currentPCB, handle); //Return 1 or 0
      break;
    case TRAP_COND_CREATE:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      ihandle = CondCreate(ihandle);
      ProcessSetResult(currentPCB, ihandle); //Return handle
      break;
    case TRAP_COND_WAIT:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      ihandle = CondHandleWait(ihandle);
      ProcessSetResult(currentPCB, ihandle); //Return 1 or 0
      break;
    case TRAP_COND_SIGNAL:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      ihandle = CondHandleSignal(ihandle);
      ProcessSetResult(currentPCB, ihandle); //Return 1 or 0
      break;
    case TRAP_TIMERGET:
      ihandle = TimerGet();
      ProcessSetResult(currentPCB,ihandle);
      break;
    case TRAP_COND_BROADCAST:
      ihandle = GetIntFromTrapArg(trapArgs, isr & DLX_STATUS_SYSMODE);
      ihandle = CondHandleBroadcast(ihandle);
      ProcessSetResult(currentPCB, ihandle); //Return 1 or 0
      break;
    default:
      printf ("Got an unrecognized trap (0x%x) - exiting!\n",
	      cause);
      exitsim ();
      break;
    }
  } else {
    switch (cause) {
    case TRAP_TIMER:
      dbprintf ('t', "Got a timer interrupt!\n");
      ProcessSchedule ();
      break;
    case TRAP_KBD:
      do {
	i = *((uint32 *)DLX_KBD_NCHARSIN);
	result = *((uint32 *)DLX_KBD_GETCHAR);
	printf ("Got a keyboard interrupt (char=0x%x(%c), nleft=%d)!\n",
		result, result, i);
      } while (i > 1);
      break;
    case TRAP_ACCESS:
      printf ("Exiting after illegal access at iar=0x%x, isr=0x%x\n",
	      iar, isr);
      exitsim ();
      break;
    case TRAP_ADDRESS:
      printf ("Exiting after illegal address at iar=0x%x, isr=0x%x\n",
	      iar, isr);
      exitsim ();
      break;
    case TRAP_ILLEGALINST:
      printf ("Exiting after illegal instruction at iar=0x%x, isr=0x%x\n",
	      iar, isr);
      exitsim ();
      break;
    case TRAP_PAGEFAULT:
      printf ("Exiting after page fault at iar=0x%x, isr=0x%x\n",
	      iar, isr);
      exitsim ();
      break;
    default:
      printf ("Got an unrecognized system interrupt (0x%x) - exiting!\n",
	      cause);
      exitsim ();
      break;
    }
  }
  dbprintf ('t',"About to return from dointerrupt.\n");
  // Note that this return may schedule a new process!
  intrreturn ();
}