/* * Returns a pseudo-random number in [min, max]. */ int rand_between(int min, int max) { int r = rand(); kdebug_assert(min >= 0); kdebug_assert(max >= min); while (r < min || r > max) { r = rand(); } return r; }
/* kexception: * Application-specific exception handler, called after registers * have been saved. */ void kexception() { registers_t * reg; cause_reg_t cause; /* Make sure that we are here because of a syscall exception. */ cause.reg = kget_cause(); kdebug_assert(cause.field.exc == 8); /* Get pointer to stored registers. */ reg = kget_registers(); /* Handle the system call (see syscall.S). */ ksyscall_handler(reg); /* Return from exception to instruction following syscall. */ reg->epc_reg += 4; /* Acknowledge syscall exception. */ kset_cause(~0x60, 0); }
/* example of call to a print function */ printstr(a); /* Put '0' on the Malta display. */ display_word(0); /* Setup storage-area for saving registers on exception. */ kset_registers(®s); /* Initialise timer to interrupt in 100 ms (simulated time). */ kload_timer(100 * timer_msec); /* Update the status register to enable timer interrupts. */ kset_sr(0xFFBF00E8, 0x10008001); /* Forever do nothing. */ while (1); }
/* * Releases a list node back to the system. Always returns NULL, to make it * easy to avoid dangling pointers by simply using the return value in an * assignment. */ list_node_t * list_node_free(list_node_t *node) { kdebug_assert(node); node->next_free = g_freelist; g_freelist = node; return NULL; }
/* * See stdlib.h documentation. */ int atoi(const char *str) { int x = 0; int i = 0; int j = 1; int k = 0; kdebug_assert(str); /* Skip white space */ while (str[i] == ' ') { i++; } if (str[i] == '-' || str[i] == '+') { i++; } /* index of start */ k = i; /* 0-9 = 48-57 */ while (str[i] >= '0' && str[i] <= '9') { i++; } /* remove last char \0 */ i--; for ( ; i >= k; i--) { x += (str[i] - '0') * j; j *= 10; } if (str[i] == '-') { x = -x; } return x; }