示例#1
0
文件: emulation.c 项目: khooj/chip8
int step(s_emu *emu) {
    emu->display_changed = 0;
    if (execute_opcode(emu, get_opcode(emu)) < 0)
        return -1;
    emu->MC += 2;
    if (emu->delay_timer > 0) emu->delay_timer -= 1;
    if (emu->sound_timer > 0) emu->sound_timer -= 1;
}
void execute_loop(chip_8_cpu cpu, FILE *debug_log) {
    if (pthread_create(&(cpu->delay_decrement_thread), NULL, delay_thread, cpu) != 0) {
        shutdown_cpu(cpu, 1);
    }
    if (pthread_create(&(cpu->sound_decrement_thread), NULL, sound_thread, cpu) != 0) {
        fprintf(stderr, "Failed to start the sound register thread, exiting...\n");
        shutdown_cpu(cpu, 1);
    }
    pthread_detach(cpu->delay_decrement_thread);
    pthread_detach(cpu->sound_decrement_thread);

    cpu->chip_8_screen = init_ncurses(default_window_height, default_window_width);
    refresh_window(cpu->chip_8_screen);

    while (1) {
        if (cpu->program_counter >= MEMORY_SIZE) {
            fprintf(stderr, "ERR - Fatal memory error: invalid access at memory cell: '%d'\n", cpu->program_counter);
            shutdown_cpu(cpu, 1);
        }
        if (cpu->halt) {
            break;
        }
        opcode instr = fetch_opcode(cpu);
        if (debug_log) {
            print_debug_info(debug_log, instr, cpu);
        }

        execute_opcode(instr, cpu);
        if (cpu->performed_jump) {
            cpu->performed_jump = false;
            continue;
        }

        if (cpu->skip_opcode) {
            cpu->program_counter = cpu->program_counter + 2;
            cpu->skip_opcode = false;
        }
        else {
            cpu->program_counter = cpu->program_counter + 1;
        }
    }

    // allow 0.1 seconds for the threads to clean up their memory
    usleep(100000);
    delwin(cpu->chip_8_screen);
    endwin();
}
示例#3
0
int main(int argc, const char * argv[]) {
    // use current time to generate random numbers, in order to be more "random"
    srand((unsigned)time(NULL));
    
    // make call to filereader to create all semaphores
    initialize_semaphores();
    
    // make call to filereader to open all files and initialize process blocks
    initialize_processes();
    
    // set active process to first one in the linked list
    active_process = ready_queue;

    // execute all code read in from source
    while (active_process->PC <= active_process->program_lines) {
        // if process has reached time slice, reset to zero, move to end of RQ, and start next process
        if(active_process->IC == active_process->time_slice) {
            switch_processes();
        }
        
        // copy current line into the instruction register (IR)
        memory_address = get_memory_address(active_process, active_process->PC);
        for (k = 0; k < 6; k++) {
            IR[k] = memory[memory_address][k];
        }
        //printf("PC within main: %d\n", active_process->PC);
        //printf("memory address within main: %d\n", memory_address);
        // calculate integer equivalent of opcode chars
        opcode = (int) (IR[0] - 48) * 10;
        opcode += (int) (IR[1] - 48);
        
        printf("opcode is: %d\n", opcode);
        
        // make call to api to execute relevant function for opcode
        execute_opcode(opcode);
        
        if(!external_switch) {
            active_process->IC++;
            active_process->PC++;
        }
        
        // reset flag
        external_switch = 0;
    }
}