Example #1
0
int		op_lldi(t_vm *vm, t_process *process, int *params, int len)
{
	int		val1;
	int		val2;
	int		res;

	if (valid_reg(params[2]))
	{
		g_error = 0;
		val1 = get_val_of_n_param(vm, process, params, 0);
		if (g_error)
			return (len);
		val2 = get_val_of_n_param(vm, process, params, 1);
		if (g_error)
			return (len);
		res = get_val_at(vm, get_real_addr_of_ind(vm, process->next_instr,
													val1 + val2, 1), REG_SIZE);
		process->registres[params[2] - 1] = res;
		process->carry = ((val1 + val2) == 0);
	}
	return (len);
}
Example #2
0
int		op_ldi(t_vm *vm, t_process *process, int *params, int len)
{
	int				val1;
	int				val2;
	int				res;
	unsigned char	*addr;

	if (valid_reg(params[2]))
	{
		g_error = 0;
		val1 = get_val_of_n_param(vm, process, params, 0);
		if (g_error)
			return (len);
		val2 = get_val_of_n_param(vm, process, params, 1);
		if (g_error)
			return (len);
		addr = vm->memory + (((val1 + val2) % IDX_MOD) +
								(process->next_instr - vm->memory)) % MEM_SIZE;
		res = get_val_at(vm, addr, sizeof(int));
		process->registres[params[2] - 1] = res;
	}
	return (len);
}
Example #3
0
int		op_lld(t_vm *vm, t_process *process, int *params, int len)
{
	int				val;
	unsigned char	*addr;

	if (valid_reg(params[1]))
	{
		if (type_of_n_param(vm, process, 0) == DIR_CODE)
			val = params[0];
		else
		{
			addr = get_real_addr_of_ind(vm, process->next_instr,
														(short)params[0], 0);
			val = get_val_at(vm, addr, 4);
		}
		process->registres[params[1] - 1] = val;
		if (val == 0)
			process->carry = 1;
		else
			process->carry = 0;
	}
	return (len);
}
Example #4
0
int		op_ld(t_vm *vm, t_process *process, int *params, int len)
{
	int				val;
	unsigned char	*addr;

	if (valid_reg(params[1]))
	{
		if (type_of_n_param(vm, process, 0) == DIR_CODE)
			val = params[0];
		else
		{
			addr = vm->memory + ((((short)params[0]) % IDX_MOD) +
					(process->next_instr - vm->memory)) % MEM_SIZE;
			val = get_val_at(vm, addr, 4);
		}
		process->registres[params[1] - 1] = val;
		if (val == 0)
			process->carry = 1;
		else
			process->carry = 0;
	}
	return (len);
}
void execute_program(UM_state um)
{
        int ra =0;
        int rb =0;
        int rc =0;         
        //uint32_t val= 0;
        bool proceed = true;
        int num_instructions = get_words_in_seg(um->memory, 0);
        int i = um->instr_ctr;

        while (i < num_instructions && proceed) {
                uint32_t word = get_word(um->memory, 0, i);
                uint32_t op_code = get_op_code(word);

                /*if (op_code == 13) {
                        int ra = get_reg_num(word, LOAD_RA_LSB);
                        proceed &= valid_reg(ra);

                        uint32_t val = get_val(word);
                        (void) val;
                }

                else {*/
                        ra = get_reg_num(word, RA_LSB);
                        rb = get_reg_num(word, RB_LSB);
                        rc = get_reg_num(word, RC_LSB);

                        proceed &= valid_reg(ra);
                        proceed &= valid_reg(rb);
                        proceed &= valid_reg(rc);
                //}

                if (!proceed) {
                        break;
                }
                        
                if (op_code == 0) {
                        /* Conditional move - op code 0 */

                        if (val_in_reg(um, rc) != 0) {
                                uint32_t val = val_in_reg(um, rb);
                                set_reg_val(um, ra, val);
                        }
                                        
                } else if (op_code == 1) {
                        /* Segmented load - op code 1 */
                        uint32_t val = get_word(um->memory, val_in_reg(um, rb),
                                       val_in_reg(um, rc));
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 2) {

                        /* Segmented store - op code 2 */

                        uint32_t ID = val_in_reg(um, ra);
                        uint32_t offset = val_in_reg(um, rb);
                        uint32_t val = val_in_reg(um, rc);

                        put_word(um->memory, ID, offset, val);
                                        
                } else if (op_code == 3) {

                        /* Add - op code 3 */
                        uint32_t val = (val_in_reg(um, rb) + val_in_reg(um, rc)) % UINT_MAX;
                        set_reg_val(um, ra, val);

                                        
                } else if (op_code == 4) {
                        /* Multiply - op code 4 */
                        uint32_t val = (val_in_reg(um, rb) * val_in_reg(um, rc)) % UINT_MAX;
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 5) {
                        /* Divide - op code 5 */

                        uint32_t val = val_in_reg(um, rb) / val_in_reg(um, rc);
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 6) {
                        /* Bitwise NAND - op code 6 */

                        uint32_t val = ~(val_in_reg(um, rb) & val_in_reg(um, rc));
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 7) {
                        /* Halt */
                        proceed = false;
                                        
                } else if (op_code == 8) {
                        /* Map segment - op code 8 */

                        if (Stack_empty(um->unmapped_IDs)) {
                              
                                uint32_t num_words = val_in_reg(um, rc);

                                proceed &= create_segment(um->memory, num_words);             
                                set_reg_val(um, rb, (get_num_segs(um->memory) - 1));
                        }

                        else {
                                uint32_t ID = get_unmapped_ID(um);
                                uint32_t num_words = val_in_reg(um, rc);

                                proceed &= resize(um->memory, ID, num_words);

                                set_reg_val(um, rb, ID);
                        }

                } else if (op_code == 9) {
                        /* Unmap segment - op code 9 */
                        
                        uint32_t ID = val_in_reg(um, rc);
                        proceed &= clear_seg(um->memory, ID);
                        proceed &= add_unmapped_ID(um, ID);
                                        
                } else if (op_code == 10) {
                        /* Output - op code 10 */
                        uint32_t val = val_in_reg(um, rc);

                        if (val < 256) {
                                fprintf(stdout, "%c", (char) val);
                        } else {
                                proceed &= false;

                        }
                                        
                } else if (op_code == 11) {
                        /* Input - op code 11 */

                        uint32_t val = getc(stdin);

                        if ((int) val == EOF) {
                                val = ~0;

                        } else if (val > 255) {
                                proceed &= false;
                        }

                        set_reg_val(um, rc, val);
                                        
                } else if (op_code == 12) {
                        /* Load program - op code 12 */

                        uint32_t ID = val_in_reg(um, rb);

                        if (ID != 0) {
                                proceed &= clear_seg(um->memory, 0);

                                int num_words = get_words_in_seg(um->memory, ID);
                                
                                resize(um->memory, 0, num_words);

                                for (int i = 0; i < num_words; i++) {
                                        proceed &= put_word(um->memory, 0, i,
                                                               get_word(um->memory, ID, i));
                                }
                        }

                        um->instr_ctr = val_in_reg(um, rc);

                        num_instructions = 
                                get_words_in_seg(um->memory, 0);
                        i = um->instr_ctr;
                                        
                } else if (op_code == 13) {

                       /* Load value - op code 13 */
                        ra = get_reg_num(word, LOAD_RA_LSB);
                        proceed &= valid_reg(ra);

                        uint32_t val = get_val(word);
                        set_reg_val(um, ra, val);
                        
                } else {

                        fprintf(stderr, "op code doesn't exist\n");
                        proceed = false;
                }
                
                if (op_code != 12) {
                        i++;
                }
        }

        return;
}