Ejemplo n.º 1
0
Archivo: memcpy.c Proyecto: taysom/tau
void test (char *test_name, test_loop_f test_loop, void *function)
{
	struct rusage	before;
	struct rusage	after;
	u8		*a;
	u8		*b;
	int		n;
	int		j;

	printf("%s (%s)\n", test_name, meg.legend);
	n = Option.file_size;
	a = alloc_aligned(n);
	b = alloc_aligned(n);
	if (init_buffers) {
		initMem(a, n);
		initMem(b, n);
	}
	for (j = 0; j < Option.loops; j++) {
		getrusage(RUSAGE_SELF, &before);
		test_loop(j, a, b, n, function);
		getrusage(RUSAGE_SELF, &after);
		PrUsage(&before);
		PrUsage(&after);
	}
	free(a);
	free(b);
}
Ejemplo n.º 2
0
/*
* Main function initializes registers and memory, then starts the main loop
*/
int main(int argc, char** argv){


	//Local Variables
        int status;

	//Initializations
	initMem();
#ifdef RPI
	setup_io();
	initGPIOs();
#endif
	//Setup
        printf("%s starting up...\n", VERSTR);
        printf("Command line arguments:\n");
        printf("~q: Quit, ~pm lower upper: Prints mem between lower and upper\n");
        printf("~pr: Prints register contents, ~rm: reset memmory to 0s\n");
	printf("~cp: Changes the period of the clock to given number in nano seconds\n");
	printf("~in: Prompt for file input\n");
	printf("~run <start location>: runs program starting at <Start location>\n");
	printf("~step <start location>: runs program starting at <Start location>. Steps through each execution. ~step without an address continues the current execution.\n");


        for(int i = 1; i < argc; i++)
        {
                if(argv[i] != NULL)
                {
                        printf(" %s", argv[i]);
                }
        }

        status = mainloop();
        return status;

}
Ejemplo n.º 3
0
void* System::realloc(void* block, size_t bytes) {
#ifndef NO_BUFFERPOOL
    initMem();
    return bufferpool->realloc(block, bytes);
#else
	return ::realloc(block, bytes);
#endif
}
Ejemplo n.º 4
0
void* System::malloc(size_t bytes) {
#ifndef NO_BUFFERPOOL
    initMem();
    return bufferpool->malloc(bytes);
#else
    return ::malloc(bytes);
#endif
}
//------------------------------------------------------------------------------
EditLine::EditLine(float _x1,float _y1, float _x2,float _y2){
  initMem();
  //this makes sure that x1 is always the smallest of the pair.
  x1 = _x2;
  y1 = _y2;
  x2 = _x1;
  y2 = _y1;
  fixDirection();
}
Ejemplo n.º 6
0
int main(void){


	char op_code[100];
	uint16_t instAddr;
	uint16_t topAddr;
	uint16_t baseAddr;
	uint16_t mem;
	int run = 1;
	char fileName[100];
	char in[100];

	puts("Initializing memory");
	puts("Usage:\n ~pm lower upper : prints memor from lower to upper, <mem> = 1 for secondary");
	puts("~rm : resets memory");
	puts("~in : import file into memory");
	puts("~wr : writes current MEM to the RAM chip");
	puts("~rr <mem> : reads current RAM into MEM, reads into secondary mem if <mem> = 1");
	puts("~cm : checks secondary memory to main memory");
	puts("~q : quit");

	//setup
	setup_io();
	initGPIOs();
	initMem();

	GPIO_SET = 1 << WE;

	//UNCOMMENT FOR PI AS RAM
	//pthread_t tid;
	//pthread_create(&tid, NULL, memThread, NULL);



	while(run){

		printf("Input: ");
		fgets(in, 99, stdin);
		sscanf(in, "%s %hu %hu %hu", op_code, &instAddr, &topAddr, &mem);

		//Process input
		if(!strcmp(op_code, "~q")){
			printf("Shuting down memory...\n");
			run = 0;
		}
		else if(!strcmp(op_code, "~pm")){
			printMem(instAddr, topAddr, mem);
			printf("Current Address: %hu\n", readAddress());
		}
		else if(!strcmp(op_code, "~rm")){
			freeMem();
			initMem();
		}
		else if(!strcmp(op_code, "~in")){
			printf("Enter file name, followed by an address to load at: ");
        	        scanf("%s %hu", fileName, &baseAddr);
			puts("WARNING, make sure the base address is set correctly when assembling file");
			while(getchar()!= '\n');
			if(readBin(fileName, baseAddr) == -1){
                        	puts("Could not open file");
			}
		}
		else if(!strcmp(op_code, "~wr")){
			printf("Writing current MEM to RAM...\n");
			writeRAMChip();
		}
		else if(!strcmp(op_code, "~rr")){
			printf("Reading RAM into Memory\n");
			readRAMChip(instAddr);
		}
		else if(!strcmp(op_code, "~off")){
			initGPIOs();
		}
		else if(!strcmp(op_code, "~cm")){
			checkMEM(instAddr);
		}


	}

}
EditLine::EditLine(){
  initMem();
}
btInt TEST_MEM_PERF::test()
{
    assert(initMem(vm["enable-warmup"].as<bool>(),
                   vm["wrline-m"].as<bool>()));

    t_test_config config;
    memset(&config, 0, sizeof(config));

    // What's the AFU frequency (MHz)?
    uint64_t afu_mhz = readCommonCSR(CSR_COMMON_FREQ);

    config.cycles = uint64_t(vm["ts"].as<int>()) * afu_mhz * 1000 * 1000;
    if (config.cycles == 0)
    {
        // Didn't specify --ts.  Use cycles instead.
        config.cycles = uint64_t(vm["tc"].as<int>());
    }

    const uint64_t counter_bits = 40;
    if (config.cycles & (int64_t(-1) << counter_bits))
    {
        cerr << "Run length overflows " << counter_bits << " bit counter" << endl;
        exit(1);
    }

    config.vc = uint8_t(vm["vc"].as<int>());
    assert(config.vc < 4);

    config.rdline_s = vm["rdline-s"].as<bool>();
    config.wrline_m = vm["wrline-m"].as<bool>();

    config.mcl = uint64_t(vm["mcl"].as<int>());
    if ((config.mcl > 4) || (config.mcl == 3))
    {
        cerr << "Illegal multi-line (mcl) parameter:  " << config.mcl << endl;
        exit(1);
    }
    // Encode mcl as 3 bits.  The low 2 are the Verilog t_ccip_clLen and the
    // high bit indicates random sizes.
    config.mcl = (config.mcl - 1) & 7;

    if (vm["test-mode"].as<bool>())
    {
        cout << "# Mem Bytes, Stride, " << statsHeader() << endl;
        t_test_stats stats;

        config.buf_lines = 256;
        config.stride = 12;
        config.enable_writes = true;
        config.enable_reads = true;
        assert(runTest(&config, &stats) == 0);

        cout << 0x100 * CL(1) << " "
             << config.stride << " "
             << stats
             << endl;

        return 0;
    }

    uint64_t stride_incr = 1 + config.mcl;

    uint64_t min_stride = uint64_t(vm["min-stride"].as<int>());
    min_stride = (min_stride + stride_incr - 1) & ~ (stride_incr - 1);
    uint64_t max_stride = uint64_t(vm["max-stride"].as<int>()) + 1;

    bool vcmap_all = vm["vcmap-all"].as<bool>();
    bool vcmap_enable = vm["vcmap-enable"].as<bool>();
    bool vcmap_dynamic = vm["vcmap-dynamic"].as<bool>();
    int32_t vcmap_fixed_vl0_ratio = int32_t(vm["vcmap-fixed"].as<int>());
    cout << "# MCL = " << (config.mcl + 1) << endl
         << "# Cycles per test = " << config.cycles << endl
         << "# AFU MHz = " << afu_mhz << endl
         << "# VC = " << vcNumToName(config.vc) << endl
         << "# VC Map enabled: " << (vcmap_enable ? "true" : "false") << endl;
    if (vcmap_enable)
    {
        cout << "# VC Map all: " << (vcmap_all ? "true" : "false") << endl
             << "# VC Map dynamic: " << (vcmap_dynamic ? "true" : "false") << endl;
        if (! vcmap_dynamic)
        {
            cout << "# VC Map fixed VL0 ratio: " << vcmap_fixed_vl0_ratio << " / 64" << endl;
        }
    }

    // Read
    cout << "#" << endl
         << "# Reads " << (config.rdline_s ? "" : "not ") << "cached" << endl
         << "# Mem Bytes, Stride, " << statsHeader()
         << endl;

    for (uint64_t mem_lines = stride_incr; mem_lines * CL(1) <= buffer_bytes; mem_lines <<= 1)
    {
        config.buf_lines = mem_lines;

        // Vary stride
        uint64_t stride_limit = (mem_lines < max_stride ? mem_lines+1 : max_stride);
        for (uint64_t stride = min_stride; stride < stride_limit; stride += stride_incr)
        {
            t_test_stats stats;

            config.stride = stride;
            config.enable_writes = false;
            config.enable_reads = true;
            assert(runTest(&config, &stats) == 0);

            cout << mem_lines * CL(1) << " "
                 << stride << " "
                 << stats
                 << endl;
        }
    }

    // Write
    cout << endl
         << endl
         << "# Writes " << (config.wrline_m ? "" : "not ") << "cached" << endl
         << "# Mem Bytes, Stride, " << statsHeader()
         << endl;

    for (uint64_t mem_lines = stride_incr; mem_lines * CL(1) <= buffer_bytes; mem_lines <<= 1)
    {
        config.buf_lines = mem_lines;

        // Vary stride
        uint64_t stride_limit = (mem_lines < max_stride ? mem_lines+1 : max_stride);
        for (uint64_t stride = min_stride; stride < stride_limit; stride += stride_incr)
        {
            t_test_stats stats;

            config.stride = stride;
            config.enable_writes = true;
            config.enable_reads = false;
            assert(runTest(&config, &stats) == 0);

            cout << mem_lines * CL(1) << " "
                 << stride << " "
                 << stats
                 << endl;
        }
    }

    // Throughput (independent read and write)
    cout << endl
         << endl
         << "# Reads " << (config.rdline_s ? "" : "not ") << "cached +"
         << " Writes " << (config.wrline_m ? "" : "not ") << "cached" << endl
         << "# Mem Bytes, Stride, " << statsHeader()
         << endl;

    for (uint64_t mem_lines = stride_incr; mem_lines * CL(1) <= buffer_bytes; mem_lines <<= 1)
    {
        config.buf_lines = mem_lines;

        // Vary stride
        uint64_t stride_limit = (mem_lines < max_stride ? mem_lines+1 : max_stride);
        for (uint64_t stride = min_stride; stride < stride_limit; stride += stride_incr)
        {
            t_test_stats stats;

            config.stride = stride;
            config.enable_writes = true;
            config.enable_reads = true;
            assert(runTest(&config, &stats) == 0);

            cout << mem_lines * CL(1) << " "
                 << stride << " "
                 << stats
                 << endl;
        }
    }

    return 0;
}
Ejemplo n.º 9
0
int main(int argc, char **argv)
{
	int cycles_to_run;
	
	char *machine_code; // This will hold our "ASCII Object File"
	
	char debug_command[180]="init";
	char *cmdptr;
	unsigned int dbg_addr;
	
		printf("6502 simulator front end for CS 2208a.\n\n");

		// Did the user specify an object file on the command line?
		// If not... help them.
		if(argc < 3) {
			printf("Usage: %s [ASCII Object File name] [# cycles to simulate] {-d}\n",argv[0]);
			printf(" e.g.: %s test.obj 3000 -d\n\n",argv[0]);
			exit(-1);
		}
	
		// Read the object file into a string.
		machine_code = read_obj_file(argv[1]);
	
	// Fire up the system RAM
	initMem();
	
	// Load the object file from the string into our simulated RAM,
	// starting at memory location 'code_location'.
	loadMem(machine_code,code_location);

		// We did something horribly underhanded in read_obj_file()... 
		// we allocated memory with 'malloc' and then passed back a pointer.
		// But note that the onus is on us, the C programmer, to _remember_ 
		// that we did that and free up the memory when it's no longer needed.
		// Not a big deal here, but imagine a bigger program where you're keeping
		// track of hundreds of mallocs and frees. Now you know why C programs
		// leak memory like the titanic.
		free(machine_code);

	
	// Initialize the 6502
	init6502();
	reset6502();
	
	PC = code_location; // Make sure the program counter points to our code!
	
	
	// All set to run the simulator now!
	
	
	// Everything below is just fanciness to give you a rudimentry 6502 debugger
	// to help with your assignment. Without the fancyness, all we're really doing
	// is one call:
	//
	// exec6502(num_cycles);
	
	
	// Run in debug mode, if requested
	if( (argc > 3) && (!strcmp(argv[3],"-d"))) {

		printf("Running in DEBUG MODE.\n\nType '?' for help.\n");
		
		// Debug loop
		while(strcmp(debug_command,"quit")) {

			
			//dumpMem(0x0021); // Check the value we stored to the zero page
			//dumpMem(0x01FF); // Peek at the top of the stack. Remember, the stack
						     // is hard-coded to addresses $0100–$01FF.
			//dump6502reg();	// print registers to the screen
			
			printf("debug> ");
			
			if( (gets(debug_command))[0] != '\0'){
			
			  cmdptr = strtok(debug_command," ");
			
			  switch(cmdptr[0]) {
			
				case 'p':
					if(cmdptr = strtok(NULL," ")) {
					  sscanf(cmdptr, "%x", &dbg_addr);
					  dumpMem((WORD)dbg_addr);
					}
					break;
				
				case 'r':
					dump6502reg();
					break;
					
				case 's':
					exec6502(1); // Execute 1 command
					break;
					
				case '?':
					printf("\n\np [0xAddress] - print value at memory location\n");
					printf("r - print register values\n");
					printf("s - step through program\n");
					printf("quit - exit\n\n");
					break;
			
				default:
					(cmdptr[0] % 2) ? printf("Herp.\n") : printf("Derp.\n");
					
			  }
			}
						
		}
	} else {
	// Otherwise, run in regular mode.
		exec6502(atoi(argv[2]));
	}
}
Ejemplo n.º 10
0
int main (int argc , char *argv[])
{
        signal( SIGINT, &shandler );
        signal( SIGTERM, &shandler );
        signal( SIGSEGV, &shandler );

        write(2, "\n\t\t######################################", 41);
        write(2, "\n\t\t#           Dump HTTP Sigs           #", 41);
        write(2, "\n\t\t# ---------------------------------- #", 41);
        write(2, "\n\t\t#     Written by Ernest Richards     #", 41);
        write(2, "\n\t\t#  Based on code from Chema Garcia   #", 41);
        write(2, "\n\t\t# ---------------------------------- #", 41);
        write(2, "\n\t\t# Github.com/ernesto341/ais-research #", 41);
        write(2, "\n\t\t######################################\n", 42);
        write(2, "\n\t\tX      -----    Active    -----      X\n\n", 43);

        if (DEBUG)
        {
                sprintf(buf, "\n[i] libntoh version: %s\n", ntoh_version());
                write(2, buf, strlen(buf));
        }

        if ( argc < 3 )
        {
                sprintf(buf, "\n[+] Usage: %s <options>\n", argv[0]);
                write(2, buf, strlen(buf));
                write(2, "\n+ Options:", 11);      // 28
                write(2, "\n\t-i | --iface <val> -----> Interface to read packets from", 58);
                write(2, "\n\t-f | --file <val> ------> File path to read packets from", 58);
                write(2, "\n\t-F | --filter <val> ----> Capture filter (must contain \"tcp\" or \"ip\")", 71);
                write(2, "\n\t-c | --client ----------> Receive client data only", 52);
                write(2, "\n\t-s | --server ----------> Receive server data only\n\n", 54);
                exit(1);
        }

        /* parameters parsing */
        int32_t c = 0;

        /* pcap */
        char errbuf[PCAP_ERRBUF_SIZE];
        struct bpf_program fp;
        char filter_exp[] = "ip";
        char *source = 0;
        char *filter = filter_exp;
        const unsigned char *packet = 0;
        struct pcap_pkthdr header;

        /* packet dissection */
        struct ip	*ip;
        uint32_t error = 0;

        /* extra */
        uint32_t ipf, tcps;

        /* check parameters */
        while ( c >= 0 )
        {
                int32_t option_index = 0;
                static struct option long_options[] =
                {
                        { "iface" , 1 , 0 , 'i' },
                        { "file" , 1 , 0 , 'f' },
                        { "filter" , 1 , 0 , 'F' },
                        { "client" , 0 , 0 , 'c' },
                        { "server" , 0 , 0 , 's' },
                        { 0 , 0 , 0 , 0 }
                };

                c = getopt_long( argc, argv, "i:f:F:cs", long_options, &option_index );

                if (c >= 0)
                {

                        switch ( c )
                        {
                                case 'i':
                                        source = optarg;
                                        handle = pcap_open_live( optarg, 65535, 1, 0, errbuf );
                                        break;

                                case 'f':
                                        source = optarg;
                                        handle = pcap_open_offline( optarg, errbuf );
                                        break;

                                case 'F':
                                        filter = optarg;
                                        break;

                                case 'c':
                                        receive |= RECV_CLIENT;
                                        break;

                                case 's':
                                        receive |= RECV_SERVER;
                                        break;
                        }
                }
        }

        if ( !receive )
        {
                receive = (RECV_CLIENT | RECV_SERVER);
        }

        if ( !handle )
        {
                if (DEBUG)
                {
                        fprintf( stderr, "\n[e] Error loading %s: %s\n", source, errbuf );
                }
                exit( -1 );
        }

        if ( pcap_compile( handle, &fp, filter, 0, 0 ) < 0 )
        {
                if (DEBUG)
                {
                        fprintf( stderr, "\n[e] Error compiling filter \"%s\": %s\n\n", filter, pcap_geterr( handle ) );
                }
                pcap_close( handle );
                exit( -2 );
        }

        if ( pcap_setfilter( handle, &fp ) < 0 )
        {
                if (DEBUG)
                {
                        fprintf( stderr, "\n[e] Cannot set filter \"%s\": %s\n\n", filter, pcap_geterr( handle ) );
                }
                pcap_close( handle );
                exit( -3 );
        }
        pcap_freecode( &fp );

        /* verify datalink */
        if ( pcap_datalink( handle ) != DLT_EN10MB )
        {
                if (DEBUG)
                {
                        fprintf ( stderr , "\n[e] libntoh is independent from link layer, but this code only works with ethernet link layer\n");
                }
                pcap_close ( handle );
                exit ( -4 );
        }

        if (DEBUG)
        {
                fprintf( stderr, "\n[i] Source: %s / %s", source, pcap_datalink_val_to_description( pcap_datalink( handle ) ) );
                fprintf( stderr, "\n[i] Filter: %s", filter );

                fprintf( stderr, "\n[i] Receive data from client: ");
                if ( receive & RECV_CLIENT )
                {
                        fprintf( stderr , "Yes");
                }
                else
                {
                        fprintf( stderr , "No");
                }

                fprintf( stderr, "\n[i] Receive data from server: ");
                if ( receive & RECV_SERVER )
                {
                        fprintf( stderr , "Yes");
                }
                else
                {
                        fprintf( stderr , "No");
                }
        }

        /*******************************************/
        /** libntoh initialization process starts **/
        /*******************************************/

        initMem(&snc);

        /* fork and exec retrieve in retdir */

        char *null_args[] = {NULL};
        char *null_envp[] = {NULL};
        
        if ((ret_pid = fork()) == 0) /* child */
        {
                if (execve((char *)"./retdir/retrieve\0", null_args, null_envp) < 0)
                {
                        perror("execve()");
                        shandler(-1);
                }
        }
        else
        {

                if ((t5Convert = (sig_atomic_t *)malloc(sizeof(sig_atomic_t) * t5TplLen)) < (sig_atomic_t *)0)
                {
                        write(2, "\n\t[e] --- Unable to allocate sufficient memory\n", 47);
                        fflush(stderr);
                        _exit(-1);
                }

                ntoh_init ();

                if ( ! (tcp_session = ntoh_tcp_new_session ( 0 , 0 , &error ) ) )
                {
                        if (DEBUG)
                        {
                                fprintf ( stderr , "\n[e] Error %d creating TCP session: %s" , error , ntoh_get_errdesc ( error ) );
                        }
                        exit ( -5 );
                }

                if (DEBUG)
                {
                        fprintf ( stderr , "\n[i] Max. TCP streams allowed: %d" , ntoh_tcp_get_size ( tcp_session ) );
                }

                if ( ! (ipv4_session = ntoh_ipv4_new_session ( 0 , 0 , &error )) )
                {
                        ntoh_tcp_free_session ( tcp_session );
                        if (DEBUG)
                        {
                                fprintf ( stderr , "\n[e] Error %d creating IPv4 session: %s" , error , ntoh_get_errdesc ( error ) );
                        }
                        exit ( -6 );
                }

                if (DEBUG)
                {
                        fprintf ( stderr , "\n[i] Max. IPv4 flows allowed: %d\n\n" , ntoh_ipv4_get_size ( ipv4_session ) );

                        fflush(stderr);
                }

                /* capture starts */
                /* accept signal from consumer to quit */
                while ( ( packet = pcap_next( handle, &header ) ) != 0 && snc.smem.shm[CTL][FLAGS] != CDONE)
                {
                        static int pc = 0;
                        fprintf(stdout, "Packet %d\n", ++pc);
                        fflush(stdout);
                        /* get packet headers */
                        ip = (struct ip*) ( packet + sizeof ( struct ether_header ) );
                        if ( (ip->ip_hl * 4 ) < (int)sizeof(struct ip) )
                        {
                                continue;
                        }

                        /* it is an IPv4 fragment */
                        if ( NTOH_IPV4_IS_FRAGMENT(ip->ip_off) )
                        {
                                send_ipv4_fragment ( ip , &ipv4_callback );
                        }
                        /* or a TCP segment */
                        else if ( ip->ip_p == IPPROTO_TCP )
                        {
                                send_tcp_segment ( ip , &tcp_callback );
                        }
                }
                if (snc.smem.shm[CTL][FLAGS] == CDONE)
                {
                        shandler( 0 );
                }

                tcps = ntoh_tcp_count_streams( tcp_session );
                ipf = ntoh_ipv4_count_flows ( ipv4_session );

                /* no streams left */
                if ( ipf + tcps > 0 )
                {
                        if (DEBUG)
                        {
                                fprintf( stderr, "\n\n[+] There are currently %i stored TCP stream(s) and %i IPv4 flow(s). You can wait for them to get closed or press CTRL+C\n" , tcps , ipf );
                                pause();
                        }
                }

                shandler( 0 );

        }
        //dummy return, should never be called
        return (0);
}
Ejemplo n.º 11
0
void initAll(char *bin_fname) {
    int dbg = 0;
    int i,j,k;
    loop_t          *loop, *preLoop;
    inf_node_t      *ib;
    inf_proc_t      *ip;

    initMem();
    totalBlk = 0;
    curEnumBlk = NULL; 
    for (i=0; i<num_tcfg_loops; i++) {
        loop = loops[i];
        loop->bound = 0;
        loop->rId = -1;
        loop->rType = -1;
        loop->rBound = -1;
    }
    /*set loop bound for all loops*/
    for (i=0; i<prog.num_procs; i++) {
        ip = &(inf_procs[i]);
        for (j=0; j<ip->num_bb; j++) {
            ib = &(ip->inf_cfg[j]);
            loop = getIbLoop(ib);
            if (loop) {
                loop->bound = getIbLB(ib);
                loop->rId = -1;
                loop->rType = -1;
                loop->rBound = -1;
            }
        }
    }
    loops[0]->bound = 1;

    /*read loop iteration constraint*/
    readExtraCons(bin_fname);

    loops[0]->exec = 1;
    /*set absolute max exec count*/
    for (i=num_tcfg_loops-1; i>0; i--) {
        loop = loops[i];
        loop->exec = 1;
        if (loop->rId==-1) {
            if (loop->parent) loop->exec = loop->parent->exec*loop->bound;
        }
        else {
            printf("\n L%d rbIb %d rType %d",loop->id, loop->rId,loop->rType);
            preLoop = loops[loop->rId];
            if (!preLoop) continue;
            loop->exec = 0;
            switch(loop->rType) {
                case EQL_LB:
#if 0
                    for (j = 0; j < preLoop->bound; ) {
                        loop->exec += (j+loop->rBound);
                    }
#endif
                loop->exec = (preLoop->bound*(preLoop->bound-1)/2);
                break;
                case INV_LB:
                    for (j = preLoop->bound; j>=0; j--) {
                        loop->exec +=(loop->rBound - j);
                    }
                break;
                default:
                    printf("\nUnknown relative bound cons type: %d",
                            loop->rType); fflush(stdout);
                    exit(1);
                    break;
            }
            if (preLoop->parent) {
                loop->exec = loop->exec * preLoop->parent->exec;
            }
        }
    }

    if (dbg) {
        for (i=1; i<num_tcfg_loops; i++) {
            loop = loops[i];
            if (loop->rId==-1) {
                fprintf(dbgAddr,"\n Loop L%d lb:%d entry:%d exec:%d",
                        loop->id, loop->bound, loop->parent->exec, loop->exec);
            }
            else {
                preLoop = loops[loop->rId];
                switch(loop->rType) {
                    case EQL_LB:
                    case INV_LB:
                        fprintf(dbgAddr,"\n Loop L%d lb:L%d entry:%d exec:%d",
                                loop->id, preLoop->id, preLoop->exec, loop->exec);
                    break;
                    default:
                    break;
                }

            }
            fflush(dbgAddr);
        }
    }
}
Ejemplo n.º 12
0
/*
* Main loop used to continuously ask for user input
*/
int mainloop(){

	//Local variables
	int mode = 0;
	char op_code[100];
	uint16_t instAddr;
	uint16_t topAddr;
	uint16_t baseAddr;
	char fileName[100];
	char run = 1;
	char in[100];
	regA.data = 0;
	regSTAT.data = 0;
	nibble currentInst;
	int tempAddress = 0;
	int instrRun = 0;
	struct timespec gettime_now;
	struct timespec newTime = {0, 0};
	long totalFirstTime;
	long totalSecondTime;
	long firstTime;
	long secondTime;
	long period = 200000;
	char step = 0;




	while(run){

		if(mode == USERMODE)
			printf("Input: ");
		fgets(in, 99, stdin);
		sscanf(in, "%s %hu %hu", op_code, &instAddr, &topAddr);

		//Process input
		if(!strcmp(op_code, "~q")){
			printf("Halting\n");
			run = 0;
		}
		else if(!strcmp(op_code, "~pm")){
			printMem(instAddr, topAddr);
		}
		else if(!strcmp(op_code, "~pr")){
			printReg();
		}
		else if(!strcmp(op_code, "~in")){
			printf("Enter file name, followed by an address to load at: ");
        	        scanf("%s %hu", fileName, &baseAddr);
			puts("WARNING, make sure the base address is set correctly when assembling file");
			while(getchar()!= '\n');
			if(readBin(fileName, baseAddr) == -1){
                        	printf("Entering User Input Mode");
   				mode = USERMODE;
			}
		}
		else if(!strcmp(op_code, "~run")){
			regPC = instAddr;
			instrRun = 0;
			regSTAT.data &= 0xD;
			mode = FILEMODE;
			step = 0;
		}
		else if(!strcmp(op_code, "~step")){
			if(instAddr != NULL)
			{
				regPC = instAddr;
				regSTAT.data &= 0xD;
			}
			instrRun = 0;
			mode = FILEMODE;
			step = 1;
		}
		else if(!strcmp(op_code, "~cp")){
                        printf("Enter period: ");
                        scanf("%li", &period);
                        while(getchar()!= '\n');
		}
		else if(!strcmp(op_code, "~rm")){
			freeMem();
			initMem();
		}
		else {
			if(mode == USERMODE)
				decode(op_code, instAddr);
		}

		if(mode == FILEMODE){
			if(step == 0)
			{
				puts("Program started...");
			}
			else if (step == 1)
			{
				puts("Stepping forward...");
			}

			//Runs while HLT is off
			while(!(regSTAT.data & 0x2)){

				//Start of file code

				//EXECUTE FIRST 4 BITS
				currentInst = readMem(regPC);

                                clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
                                waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_CLR = 1<<CLKPIN;
								#endif
				
				clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_SET = 1<<CLKPIN;
								#endif
				
				//EXECUTE SECOND 4 BITS
				instAddr = 0;
				tempAddress = 0;
				tempAddress = readMem(++regPC).data;
				instAddr |= (tempAddress << 12);

                                clock_gettime(CLOCK_REALTIME, &gettime_now);
				firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2);
								#ifdef RPI
                                GPIO_CLR = 1 <<CLKPIN;
								#endif
								
				clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_SET = 1<<CLKPIN;
								#endif

				//EXECUTE THIRD 4 BITS
	                        tempAddress = readMem(++regPC).data;
       		                instAddr |= (tempAddress << 8);


                                clock_gettime(CLOCK_REALTIME, &gettime_now);
				firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_CLR = 1<<CLKPIN;
								#endif
								
				clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_SET = 1<<CLKPIN;
								#endif
				//EXECUTE FOURTH 4 BITS
                        	tempAddress = readMem(++regPC).data;
                        	instAddr |= (tempAddress << 4);

                                clock_gettime(CLOCK_REALTIME, &gettime_now);
				firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
				#ifdef RPI
				GPIO_CLR = 1 <<CLKPIN;
				#endif

				clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_SET = 1<<CLKPIN;
								#endif
								
				//EXECUTE FIFTH 4 BITS
                      		tempAddress = readMem(++regPC).data;
	                        instAddr |= (tempAddress);
				regPC++;

                                clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_CLR = 1<<CLKPIN;
								#endif
								
				clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_SET = 1<<CLKPIN;
								#endif	
								
	                        if(currentInst.data == HLT)
        	                        decode("HLT", instAddr);
                	        else if(currentInst.data == LOD)
                        	        decode("LOD", instAddr);
	                        else if(currentInst.data == STR)
        	                       	decode("STR", instAddr);
                	        else if(currentInst.data == ADD)
                        	        decode("ADD", instAddr);
	                        else if(currentInst.data == NOP)
        	                        decode("NOP", instAddr);
                	        else if(currentInst.data == NND)
                        	        decode("NND", instAddr);
	                        else if(currentInst.data == CXA)
        	                       	decode("CXA", instAddr);
                	        else if(currentInst.data == JMP)
                        	        decode("JMP", instAddr);
	                        else
									#ifndef __MINGW32__
        	                        shutdown(UNKNOWNINSTRUCTIONERROR);
									#endif
									#ifdef __MINGW32__
									shutdown_vm4(UNKNOWNINSTRUCTIONERROR);
									#endif

                                clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
                                waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_CLR = 1 <<CLKPIN;
								#endif
								
				clock_gettime(CLOCK_REALTIME, &gettime_now);
                                firstTime = gettime_now.tv_nsec;
				waitForPeriod(firstTime,gettime_now,period/2 );
								#ifdef RPI
                                GPIO_SET = 1<<CLKPIN;
								#endif

				if(step == 1)
				{
					break;
				}

			}
		if((regSTAT.data & 0x2) && step)
		{
			puts("Computer halted");
		}
		mode = USERMODE;
		if(step == 0)
		{
			puts("Program finished");
			printf("Instructions run %d\n", instrRun);
			#ifdef RPI
			GPIO_CLR = 1 << CLKPIN;
			#endif
		}
		}

	}

	return 1;
}