示例#1
0
void test_clear()
{
        clear_seg(SEG_LENGTH);

        Segment t = get_seg(SEG_LENGTH);
        if ( t->length != 0 || t->data != NULL) {
                printf("CLEAR IS A FAILURE!!!\n");
        }

        printf("CLEAR IS A SUCCESS!!!\n");

}
示例#2
0
void test_clear_seg(Segment s)
{
        int length = get_num_segs(s);

        for (int i = 0; i < length; i++) {
                for (int j = 0; j < get_words_in_seg(s, i); j++) {

                        clear_seg(s, i);

                        if (get_words_in_seg(s, i) != 0) {
                                fprintf(stderr, "clear did not clear!\n");
                        }
                }
        }

        return;

}
示例#3
0
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;
}
示例#4
0
void main(void) 
{
  /* put your own code here */
  
  SET_TCNT_PRESCALE( TCNT_PRESCALE_8);
	TSCR1 = TSCR1_INIT;
  
	seg_init();
	CANinit(THE_FLOOR);
	SET_BITS(LED_DDR, LED1_MASK|LED2_MASK);

	node_id = PORTB & 3; //get hardware specified node id
	
	while (!(CANCTL0&0x10));

	CANRFLG = 0xC3;
	CANRIER = 0x01;

	InitializeQueue(&rec);	// Create the Recieve Queue
	
	
	
     clear_seg();

	EnableInterrupts;

  for(;;) 
  {

	if(IsQueueEmpty(rec) != 0)
	{		
		DeQueue(&rec);
		Parse_Floor();
	}
  
  // button 1 press
    if((PTJ & (SWITCH1_MASK |SWITCH2_MASK) )== SWITCH1_MASK) 
    {
        if(THE_FLOOR == F1 || THE_FLOOR == F2)
        {
            if (last != SWITCH1_MASK) 
            {   // check if button already pressed 

            	data[0] = THE_FLOOR;
            	data[1] = CALL_SWITCH1;
            	data[2] = UP;
            	
            	(void)CANSend(CONTROLLER, 0x00, 0x00, DATA_LENGTH, data); 
            	last =  SWITCH1_MASK;        
            }  
        }
        else if(THE_FLOOR == F3)
        {
            if (last != SWITCH1_MASK) 
            {   // check if button already pressed 

                data[0] = THE_FLOOR;
                data[1] = CALL_SWITCH1;
                data[2] = DOWN;

                (void)CANSend(CONTROLLER, 0x00, 0x00, DATA_LENGTH, data); 
                last =  SWITCH1_MASK;        
            }  
        }

    }                
  
  //button 2 press
    else if((PTJ & (SWITCH1_MASK |SWITCH2_MASK) )== SWITCH2_MASK)
    {
        if(THE_FLOOR == F2)
        {
            if (last != SWITCH2_MASK) 
            { // check if button already pressed    
                data[0] = THE_FLOOR;
                data[1] = CALL_SWITCH2;
                data[2] = DOWN;

                CANSend(CONTROLLER, 0x00, 0x00, DATA_LENGTH, data); 
                last =  SWITCH2_MASK;
            } 
        }
 
    }
  
  else
  last = 0;
  
  
  // Updates the LED
  FORCE_BITS(PTS,0b00001100 , led) ;


  

  } /* loop forever */
  /* please make sure that you never leave main */
}