Ejemplo n.º 1
0
//=================================================================================
int CPU_ctor (FILE* BinFile, cpu_t* cpu) {

    if (cpu == NULL)
        return CPU_NOT_INITIALIZED;

    cpu->PM = NULL;
    cpu->max_counter = 0;
    cpu->prgm_counter = 0;

    for (int i = 0; i < MAX_NUM_OF_REGISTERS; i++)
        cpu->reg[i] = 0;

    stack_ctor(&cpu->Stack);
    stack_resize(&cpu->Stack, STACK_SIZE);

    stack_ctor(&cpu->Address_stack);
    stack_resize(&cpu->Address_stack, ADDRESS_STACK_SIZE);

    if (ReadBinaryFile(BinFile, cpu) != CPU_OK)
        return READING_BINARY_ERROR;

    int check = CPU_VALID(cpu);

    if (check != CPU_OK)
        return check;

    return CPU_OK;
}
Ejemplo n.º 2
0
Error pop(Stack_t *stack, T* out)
{
    Error *err_temp = NULL;
    err_temp = my_error_ctor(err_temp);
    if (is_ok(stack) == 1)
    {
        err_temp->code = STACK_NOT_WORK;
        err_temp->message = "STACK NOT WORK\n";
        return *err_temp;
    }
    if (stack->pos == 0)
    {
        err_temp->code = STACK_UNDERFLOW;
        err_temp->message = "STACK IS UNDERFLOW\n";
        return *err_temp;
    }
    my_error_dtor(err_temp);
    *out = stack->data[--stack->pos];


    Error r_resize = stack_resize(stack, 0);
    if (r_resize.code != NO_ERROR)
        return r_resize;

    return *err_temp;
}
Ejemplo n.º 3
0
void test_stack()
{
    printf("\ntesting stack\n");
    
    int c = 16;
    struct stack* s = stack_create(c);

    int num = 200;
    for (int i = 0; i < num; ++i) {
        stack_push(s, i);
    }

    while (!stack_empty(s)) {
        printf("%d ", stack_pop(s));
    }

    printf("\nresize the stack and test it again\n");

    for (int i = 0; i < num; ++i) {
        stack_push(s, i);
    }

    stack_resize(s, 128);
    for (int i = 0; i < num; ++i) {
        stack_push(s, i);
    }

    while (!stack_empty(s)) {
        printf("%d ", stack_pop(s));
    }

    stack_destroy(s);
}
Ejemplo n.º 4
0
Error push(Stack_t *stack, T in)
{
    Error *err_temp = NULL;
    err_temp = my_error_ctor(err_temp);
    if (is_ok(stack) == 1)
    {
        err_temp->code = STACK_NOT_WORK;
        err_temp->message = "STACK NOT WORK\n";
        return *err_temp;
    }


    if (stack->pos == stack->len)
    {
        Error r_resize = stack_resize(stack, 1);
        if (r_resize.code != NO_ERROR)
            return r_resize;
    }
     if (is_ok(stack) == 1)
    {
        err_temp->code = STACK_NOT_WORK;
        err_temp->message = "STACK NOT WORK\n";
        return *err_temp;
    }


    stack->data[stack->pos++] = in;
    (void)dump(stack);
//why do you need dump after each push?

    my_error_dtor(err_temp);

    return *err_temp;
}
Ejemplo n.º 5
0
Archivo: stack.c Proyecto: berkus/moto
void 
stack_push(Stack *p, void *data) {
	if (p->size >= p->cap) {
		stack_resize(p, p->cap * STACK_RESIZE_FACTOR);
	}
	p->elements[p->size] = data;
	p->size++;
}
Ejemplo n.º 6
0
//returns: 0 if it worked out OK and -1 if something went wrong
int stack_reduce_size(struct stack_t *s)
{
	s->size /= 2;

	if(stack_resize(s) == -1)
		return -1;

	return 0;
}
Ejemplo n.º 7
0
//returns: 0 if it worked out OK and -1 if something went wrongZ
int stack_increase_size(struct stack_t *s)
{
	s->size *= 2;

	if(stack_resize(s) == -1)
		return -1;

	return 0;
}
void stack_push_back(struct stack_t *stack,int value) 
{
    if(stack->top < stack->size) 
    {
        stack->data[stack->top++] = value;
    }
    else 
    {
        stack_resize(stack);
        stack->data[stack->top++] = value;
    }
}
Ejemplo n.º 9
0
int push(stack* st, elem_t a)
{
    if (stack_ok(st))
    {
        if (st -> data  == st -> size)
            stack_resize(st);
        st -> name[st -> data] = a;
        st -> data++ ;
        return 0;
    }
    else
    {
        printf("You will not work with incorrect stack\n");
        return 0;
    }
}
Ejemplo n.º 10
0
int push(stack* st, elem_t a)
{
    if (stack_ok(st))
    {
        if (st -> data < st -> size) // is is always true
            stack_resize(st); // you always resize???
        st -> name[st -> data] = a;
        st -> data++ ;
        return 0;
    }
    else
    {
        printf("You don`t work with incorrect stack\n");
        return 0;
    }
}
int stack_pop_back(struct stack_t *stack) 
{
    if(stack->top != 0) 
    {
        --stack->top;
        if(stack->size > INITIAL_STACK_SIZE)
        {
            stack_resize(stack);
        }
        return stack->data[stack->top];
    }
    else 
    {
        printf("Stack is empty!\n");
        return INT_MIN; //returns the smallest value of int for empty stack
    }
}
Ejemplo n.º 12
0
Archivo: stack.c Proyecto: zrho/Carbon
void stack_create(stack_t *stack, process_t *process) {
    stack->address = MEMORY_USER_STACK_VADDR + process->stack_offset + STACK_LENGTH_MAX;
    process->stack_offset += STACK_LENGTH_MAX;
    stack->length = 0;

    if (UNLIKELY(stack->address >= MEMORY_USER_STACK_VADDR + STACK_PROCESS_MAX))
        PANIC("Exceeded maximum number of stacks per process.");

    uintptr_t old_addr_space = memory_space_get();

    if (old_addr_space != process->addr_space)
        memory_space_switch(process->addr_space);

    stack_resize(stack, 0x1000, process);

    if (old_addr_space != process->addr_space)
        memory_space_switch(old_addr_space);
}
Ejemplo n.º 13
0
int fstack_resize(struct stack_t *stack, size_t size)
{
	return stack_resize(stack, size);
}
Ejemplo n.º 14
0
Archivo: stack.c Proyecto: zrho/Carbon
void stack_dispose(stack_t *stack, process_t *process) {
    // Resize stack to zero
    stack_resize(stack, 0, process);
}